summaryrefslogtreecommitdiff
path: root/etc/netstart
blob: f945b1f3161d496c45c0971fd7b9770086d7dd30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh -
#
#	$OpenBSD: netstart,v 1.51 1999/09/01 18:07:34 deraadt Exp $

# Returns true if $1 contains only alphanumerics
isalphanumeric() {
	local _n
	_n=$1
	while [ ${#_n} != 0 ]; do
		case $_n in
			[A-Za-z0-9]*)	;;
			*)		return 1;;
		esac
		_n=${_n#?}
	done
	return 0
}

# /etc/myname contains my symbolic name
#
hostname=`cat /etc/myname`
hostname $hostname
if [ -f /etc/defaultdomain ]; then
	domainname `cat /etc/defaultdomain`
fi

# pick up option configuration
. /etc/rc.conf

# Configure the IP filter before configuring network interfaces
if [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
	echo 'configuring IP filter'
	ipf -Fa -f ${ipfilter_rules} -E
else
	ipfilter=NO
fi

# set the address for the loopback interface
ifconfig lo0 inet localhost

# use loopback, not the wire
route -n add -host $hostname localhost
route -n add -net 127 127.0.0.1 -reject

# configure all of the non-loopback interfaces which we know about.
# refer to hostname.if(5) and bridgename.if(5)
for hn in /etc/hostname.*; do
    # Strip off /etc/hostname. prefix
    if=${hn#/etc/hostname.}

    # Interface names must be alphanumeric only.  We check to avoid
    # configuring backup or temp files, and to catch the "*" case.
    if ! isalphanumeric "$if"; then
        continue
    fi
    ifconfig $if > /dev/null 2>&1
    if [ "$?" != "0" ]; then
	continue
    fi

    # Now parse the hostname.* file
    {
        read af name mask bcaddr extras

	# $af can be either "dhcp", "up" or an address family.
	case "$af" in
	"bridge")
	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
	    ;;
	"dhcp")
	    ifconfig $if $name $mask $bcaddr $extras down
	    cmd="dhclient $if"
	    ;;
	"up")
	    # The only one of these guaranteed to be set is $if
	    # the remaining ones exist so that media controls work
	    cmd="ifconfig $if $name $mask $bcaddr $extras up"
 	    ;;
 	*)
            read dt dtaddr
	    if [ ! -n "$name" ]; then
		echo "/etc/hostname.$if: invalid network configuration file"
		continue
	    fi

	    cmd="ifconfig $if $af $name "
	    if [ "${dt}" = "dest" ]; then cmd="$cmd $dtaddr"; fi
	    if [ -n "$mask" ]; then cmd="$cmd netmask $mask"; fi
	    if [ -n "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
		cmd="$cmd broadcast $bcaddr";
	    fi
	    cmd="$cmd $extras";
	    ;;
	esac

	eval "$cmd"
    } < /etc/hostname.$if
done
for bn in /etc/bridgename.*; do
    # Strip off /etc/bridgename. prefix
    if=${bn#/etc/bridgename.}

    # Interface names must be alphanumeric only.  We check to avoid
    # configuring backup or temp files, and to catch the "*" case.
    if ! isalphanumeric "$if"; then
        continue
    fi
    brconfig $if > /dev/null 2>&1
    if [ "$?" != "0" ]; then
	continue
    fi

    # Now parse the bridgename.* file
    {
	# All lines are run as brconfig(8) commands.
	while read line ; do
	    line=${line%%#*}		# strip comments
	    test -z "$line" && continue
	    brconfig $if $line
	done
    } < /etc/bridgename.$if
done

# /etc/mygate, if it exists, contains the name of my gateway host
# that name must be in /etc/hosts.
if [ -f /etc/mygate ]; then
	route -n add -host default `cat /etc/mygate`
fi

# Multicast routing.
#
# The routing to the 224.0.0.0/4 net is setup according to these rules:
# multicast_host	multicast_router	route		comment
# NO			NO			-reject		no multicast
# NO			YES			none installed	daemon will run
# YES/interface		NO			-interface	YES=def. iface
#	   Any other combination		-reject		config error
case "$multicast_host:$multicast_router" in
NO:NO)
	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
NO:YES)
	;;
*:NO)
	set `if [ $multicast_host = YES ]; then
		ed -s '!route -n show' <<EOF
/^default/p
EOF
	else
		ed -s "!ifconfig $multicast_host" <<EOF
/^	inet /p
EOF
	fi`
	route -n add -net 224.0.0.0/4 -interface $2;;
*:*)
	echo 'config error, multicasting disabled until rc.conf is fixed'
	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
esac
	
# Configure NAT after configuring network interfaces
if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
	echo 'configuring NAT'
	ipnat -CF -f ${ipnat_rules}
else
	ipnat=NO
fi