netstart revision 1.60
1#!/bin/sh -
2#
3#	$OpenBSD: netstart,v 1.60 2000/01/02 06:42:13 itojun Exp $
4
5# Returns true if $1 contains only alphanumerics
6isalphanumeric() {
7	local _n
8	_n=$1
9	while [ ${#_n} != 0 ]; do
10		case $_n in
11			[A-Za-z0-9]*)	;;
12			*)		return 1;;
13		esac
14		_n=${_n#?}
15	done
16	return 0
17}
18
19# /etc/myname contains my symbolic name
20#
21hostname=`cat /etc/myname`
22hostname $hostname
23if [ -f /etc/defaultdomain ]; then
24	domainname `cat /etc/defaultdomain`
25fi
26
27# pick up option configuration
28. /etc/rc.conf
29
30# Configure the IP filter before configuring network interfaces
31if [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
32	echo 'configuring IP filter'
33	ipf -Fa -f ${ipfilter_rules} -E
34else
35	ipfilter=NO
36fi
37
38# set the address for the loopback interface
39# it will also initialize IPv6 address for lo0 (::1 and others).
40ifconfig lo0 inet localhost
41
42# use loopback, not the wire
43route -n add -host $hostname localhost
44route -n add -net 127 127.0.0.1 -reject
45
46if ifconfig lo0 inet6 >/dev/null 2>&1; then
47	# IPv6 configurations.
48	ip6kernel=YES
49
50	# disallow scoped unicast dest without outgoing scope identifiers.
51	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
52	route add -inet6 fc80:: -prefixlen 10 ::1 -reject
53	# disallow "internal" addresses to appear on the wire.
54	route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
55	route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
56
57	rtsolif=""
58else
59	ip6kernel=NO
60fi
61
62# configure all of the non-loopback interfaces which we know about.
63# refer to hostname.if(5) and bridgename.if(5)
64for hn in /etc/hostname.*; do
65    # Strip off /etc/hostname. prefix
66    if=${hn#/etc/hostname.}
67
68    # Interface names must be alphanumeric only.  We check to avoid
69    # configuring backup or temp files, and to catch the "*" case.
70    if ! isalphanumeric "$if"; then
71	continue
72    fi
73    ifconfig $if > /dev/null 2>&1
74    if [ "$?" != "0" ]; then
75	continue
76    fi
77
78    # Now parse the hostname.* file
79    while :; do
80	if [ "$cmd2" ]; then
81	    # we are carrying over from the 'read dt dtaddr' last time
82	    set -- $cmd2
83	    af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" ext2="$6"
84	    cmd2=
85	else
86	    # read the next line or exit the while loop
87	    read af name mask bcaddr ext1 ext2 || break
88	fi
89	# skip comments
90	[ "${af#*#}" = "${af}" ] || continue
91	# $af can be either "dhcp", "up" or an address family.
92	case "$af" in
93	"bridge")
94	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
95	    ;;
96	"dhcp")
97	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
98	    cmd="dhclient $if"
99	    ;;
100	"rtsol")
101	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
102	    rtsolif="$rtsolif $if"
103	    cmd=
104	    ;;
105	"up")
106	    # The only one of these guaranteed to be set is $if
107	    # the remaining ones exist so that media controls work
108	    cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
109	    ;;
110	*)
111	    read dt dtaddr
112	    if [ "$name"  = "alias" ]; then
113		# perform a 'shift' of sorts
114		alias=$name
115		name=$mask
116		mask=$bcaddr
117		bcaddr=$ext1
118		ext1=$ext2
119		ext2=
120	    fi
121	    cmd="ifconfig $if $af $alias $name "
122	    case $dt in
123	    dest)
124		cmd="$cmd $dtaddr"
125		;;
126	    [a-z]*)
127		cmd2="$dt $dtaddr"
128		;;
129	    esac
130	    if [ ! -n "$name" ]; then
131		    echo "/etc/hostname.$if: invalid network configuration file"
132		return
133	    fi
134	    case $af in
135	    inet)
136		[ "$mask" ] && cmd="$cmd netmask $mask"
137		if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
138		    cmd="$cmd broadcast $bcaddr"
139		fi
140		[ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
141	    ;;
142	    inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
143		cmd="$cmd $bcaddr"
144		;;
145	    *) cmd="$cmd $mask $bcaddr"
146	    esac
147	    cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
148	    ;;
149	esac
150	eval "$cmd"
151    done < /etc/hostname.$if
152done
153
154if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
155	fw=`sysctl -n net.inet6.ip6.forwarding`
156	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
157	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
158		echo "IPv6 autoconf:$rtsolif"
159		rtsol $rtsolif
160	else
161		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
162	fi
163fi
164if [ "$ip6kernel" = "YES" ]; then
165	# this is to make sure DAD is completed before going further.
166	sleep `sysctl -n net.inet6.ip6.dad_count`
167	sleep 1
168fi
169
170for bn in /etc/bridgename.*; do
171    # Strip off /etc/bridgename. prefix
172    if=${bn#/etc/bridgename.}
173
174    # Interface names must be alphanumeric only.  We check to avoid
175    # configuring backup or temp files, and to catch the "*" case.
176    if ! isalphanumeric "$if"; then
177        continue
178    fi
179    brconfig $if > /dev/null 2>&1
180    if [ "$?" != "0" ]; then
181	continue
182    fi
183
184    # Now parse the bridgename.* file
185    {
186	# All lines are run as brconfig(8) commands.
187	while read line ; do
188	    line=${line%%#*}		# strip comments
189	    test -z "$line" && continue
190	    brconfig $if $line
191	done
192    } < /etc/bridgename.$if
193done
194
195# /etc/mygate, if it exists, contains the name of my gateway host
196# that name must be in /etc/hosts.
197if [ -f /etc/mygate ]; then
198	route -n add -host default `cat /etc/mygate`
199fi
200
201# Multicast routing.
202#
203# The routing to the 224.0.0.0/4 net is setup according to these rules:
204# multicast_host	multicast_router	route		comment
205# NO			NO			-reject		no multicast
206# NO			YES			none installed	daemon will run
207# YES/interface		NO			-interface	YES=def. iface
208#	   Any other combination		-reject		config error
209case "$multicast_host:$multicast_router" in
210NO:NO)
211	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
212NO:YES)
213	;;
214*:NO)
215	set `if [ $multicast_host = YES ]; then
216		ed -s '!route -n show' <<EOF
217/^default/p
218EOF
219	else
220		ed -s "!ifconfig $multicast_host" <<EOF
221/^	inet /p
222EOF
223	fi`
224	route -n add -net 224.0.0.0/4 -interface $2;;
225*:*)
226	echo 'config error, multicasting disabled until rc.conf is fixed'
227	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
228esac
229
230# Configure NAT after configuring network interfaces
231if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
232	echo 'configuring NAT'
233	ipnat -CF -f ${ipnat_rules}
234else
235	ipnat=NO
236fi
237