netstart revision 1.113
1#!/bin/sh -
2#
3#	$OpenBSD: netstart,v 1.113 2005/12/28 04:55:35 david Exp $
4
5# Strip comments (and leading/trailing whitespace if IFS is set)
6# from a file and spew to stdout
7stripcom() {
8	local _l
9	[[ -f $1 ]] || return
10	while read _l; do
11		[[ -n ${_l%%#*} ]] && echo $_l
12	done<$1
13}
14
15# Returns true if $1 contains only alphanumerics
16isalphanumeric() {
17	local _n
18	_n=$1
19	while [ ${#_n} != 0 ]; do
20		case $_n in
21			[A-Za-z0-9]*)	;;
22			*)		return 1;;
23		esac
24		_n=${_n#?}
25	done
26	return 0
27}
28
29# Start the $1 interface
30ifstart() {
31	if=$1
32	# Interface names must be alphanumeric only.  We check to avoid
33	# configuring backup or temp files, and to catch the "*" case.
34	if ! isalphanumeric "$if"; then
35		return
36	fi
37
38	ifconfig $if > /dev/null 2>&1
39	if [ "$?" != "0" ]; then
40		# Try to create interface if it does not exist
41		ifconfig $if create > /dev/null 2>&1
42		if [ "$?" != "0" ]; then
43			return
44		fi
45	fi
46
47	# Now parse the hostname.* file
48	while :; do
49		if [ "$cmd2" ]; then
50			# We are carrying over from the 'read dt dtaddr'
51			# last time.
52			set -- $cmd2
53			af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
54			# Make sure and get any remaining args in ext2,
55			# like the read below
56			i=1
57			while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
58			ext2="$@"
59		else
60			# Read the next line or exit the while loop.
61			read af name mask bcaddr ext1 ext2 || break
62		fi
63		# $af can be "dhcp", "up", "rtsol", an address family,
64		# commands, or a comment.
65		case "$af" in
66		"#"*|"") # skip comments and empty lines
67			continue
68			;;
69		"!"*) # parse commands
70			cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
71			;;
72		"bridge")
73			cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files"
74			;;
75		"dhcp")
76			[ "$name" = "NONE" ] && name=
77			[ "$mask" = "NONE" ] && mask=
78			[ "$bcaddr" = "NONE" ] && bcaddr=
79			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
80			cmd="$cmd;dhclient $if"
81			dhcpif="$dhcpif $if"
82			;;
83		"rtsol")
84			rtsolif="$rtsolif $if"
85			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
86			;;
87		"up")
88			# The only one of these guaranteed to be set is $if.
89			# The remaining ones exist so that media controls work.
90			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
91			;;
92		*)
93			read dt dtaddr
94			if [ "$name"  = "alias" ]; then
95				# perform a 'shift' of sorts
96				alias=$name
97				name=$mask
98				mask=$bcaddr
99				bcaddr=$ext1
100				ext1=$ext2
101				ext2=
102			else
103				alias=
104			fi
105			cmd="ifconfig $if $af $alias $name "
106			case "$dt" in
107			dest)
108				cmd="$cmd $dtaddr"
109				;;
110			[a-z!]*)
111				cmd2="$dt $dtaddr"
112				;;
113			esac
114			if [ ! -n "$name" ]; then
115				echo "/etc/hostname.$if: invalid network configuration file"
116				return
117			fi
118			case $af in
119			inet)
120				[ "$mask" ] && cmd="$cmd netmask $mask"
121				if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
122					cmd="$cmd broadcast $bcaddr"
123				fi
124				[ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1"
125				;;
126			inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
127				cmd="$cmd $bcaddr"
128				;;
129			*)
130				cmd="$cmd $mask $bcaddr"
131				;;
132			esac
133			cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
134			;;
135		esac
136		eval "$cmd"
137	done < /etc/hostname.$if
138}
139
140# Start multiple:
141#   start "$1" interfaces in order or all interfaces if empty
142#   don't start "$2" interfaces
143ifmstart() {
144	for sif in ${1:-ALL}; do
145		for hn in /etc/hostname.*; do
146			# Strip off /etc/hostname. prefix
147			if=${hn#/etc/hostname.}
148			test "$if" = "*" && continue
149
150			# Skip unwanted ifs
151			s=""
152			for xf in $2; do
153				test "$xf" = "${if%%[0-9]*}" && s="1" && break
154			done
155			test "$s" = "1" && continue
156
157			# Start wanted ifs
158			test "$sif" = "ALL" -o \
159			     "$sif" = "${if%%[0-9]*}" \
160				&& ifstart $if
161		done
162	done
163}
164
165# Start the $1 bridge
166bridgestart() {
167	# Interface names must be alphanumeric only.  We check to avoid
168	# configuring backup or temp files, and to catch the "*" case.
169	if ! isalphanumeric "$1"; then
170		return
171	fi
172	brconfig $1 > /dev/null 2>&1
173	if [ "$?" != "0" ]; then
174		# Try to create interface if it does not exist
175		ifconfig $1 create > /dev/null 2>&1
176		if [ "$?" != "0" ]; then
177			return
178		fi
179	fi
180
181	# Now parse the bridgename.* file
182	# All lines are run as brconfig(8) commands.
183	while read line ; do
184		line=${line%%#*}		# strip comments
185		test -z "$line" && continue
186		case "$line" in
187		"!"*)
188			cmd="${line#*!}"
189			;;
190		*)
191			cmd="brconfig $1 $line"
192			;;
193		esac
194		eval "$cmd"
195	done < /etc/bridgename.$1
196}
197
198# Re-read /etc/rc.conf
199. /etc/rc.conf
200
201# If we were invoked with a list of interface names, just reconfigure these
202# interfaces (or bridges) and return.
203if [ $1x = autobootx ]; then
204	shift
205fi
206if [ $# -gt 0 ]; then
207	while [ $# -gt 0 ]; do
208		if [ -f /etc/bridgename.$1 ]; then
209			bridgestart $1
210		else
211			ifstart $1
212		fi
213		shift
214	done
215	return
216fi
217
218# Otherwise, process with the complete network initialization.
219
220# /etc/myname contains my symbolic name
221if [ -f /etc/myname ]; then
222	hostname=`stripcom /etc/myname`
223	hostname $hostname
224else
225	hostname=`hostname`
226fi
227
228if [ -f /etc/defaultdomain ]; then
229	domainname `stripcom /etc/defaultdomain`
230fi
231
232# Set the address for the loopback interface.  Bringing the
233# interface up, automatically invokes the IPv6 address ::1)
234ifconfig lo0 inet 127.0.0.1
235
236if ifconfig lo0 inet6 >/dev/null 2>&1; then
237	# IPv6 configurations.
238	ip6kernel=YES
239
240	# Disallow link-local unicast dest without outgoing scope identifiers.
241	route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
242
243	# Disallow site-local unicast dest without outgoing scope identifiers.
244	# If you configure site-locals without scope id (it is permissible
245	# config for routers that are not on scope boundary), you may want
246	# to comment the line out.
247	route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
248
249	# Disallow "internal" addresses to appear on the wire.
250	route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
251
252	# Disallow packets to malicious IPv4 compatible prefix.
253	route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
254	route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
255	route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
256	route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
257
258	# Disallow packets to malicious 6to4 prefix.
259	route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
260	route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
261	route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
262	route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
263
264	# Completely disallow packets to IPv4 compatible prefix.
265	# This may conflict with RFC1933 under following circumstances:
266	# (1) An IPv6-only KAME node tries to originate packets to IPv4
267	#     compatible destination.  The KAME node has no IPv4 compatible
268	#     support.  Under RFC1933, it should transmit native IPv6
269	#     packets toward IPv4 compatible destination, hoping it would
270	#     reach a router that forwards the packet toward auto-tunnel
271	#     interface.
272	# (2) An IPv6-only node originates a packet to an IPv4 compatible
273	#     destination.  A KAME node is acting as an IPv6 router, and
274	#     asked to forward it.
275	# Due to rare use of IPv4 compatible addresses, and security issues
276	# with it, we disable it by default.
277	route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
278
279	rtsolif=""
280else
281	ip6kernel=NO
282fi
283
284
285# Configure all the non-loopback interfaces which we know about, but
286# do not start interfaces which must be delayed.
287# Refer to hostname.if(5) and bridgename.if(5)
288ifmstart "" "trunk vlan carp gif gre pfsync pppoe"
289
290if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
291	fw=`sysctl -n net.inet6.ip6.forwarding`
292	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
293	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
294		echo "IPv6 autoconf:$rtsolif"
295		rtsol $rtsolif
296	else
297		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
298	fi
299fi
300if [ "$ip6kernel" = "YES" ]; then
301	# this is to make sure DAD is completed before going further.
302	sleep `sysctl -n net.inet6.ip6.dad_count`
303fi
304
305# The trunk interfaces need to come up first in this list.
306# The vlan interfaces need to come up after trunk.
307# The pfsync interfaces need to come up before carp.
308# Configure all the carp interfaces which we know about.
309# They must come up after pfsync but before default route.
310ifmstart "trunk vlan pfsync carp"
311
312# /etc/mygate, if it exists, contains the name of my gateway host
313# that name must be in /etc/hosts.
314[[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
315		[[ $gw == @(*:*) ]] && continue
316		route -qn delete default > /dev/null 2>&1
317		route -qn add -host default $gw && break
318done
319[[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
320		[[ $gw == !(*:*) ]] && continue
321		route -qn delete -inet6 default > /dev/null 2>&1
322		route -qn add -host -inet6 default $gw && break
323done
324
325# Multicast routing.
326#
327# The routing to the 224.0.0.0/4 net is setup according to these rules:
328# multicast_host	multicast_router	route		comment
329# NO			NO			-reject		no multicast
330# NO			YES			none installed	daemon will run
331# YES/interface		NO			-interface	YES=def. iface
332#	   Any other combination		-reject		config error
333route -qn delete 224.0.0.0/4 > /dev/null 2>&1
334case "$multicast_host:$multicast_router" in
335NO:NO)
336	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
337	;;
338NO:YES)
339	;;
340*:NO)
341	maddr=`if [ "$multicast_host" = "YES" ]; then
342		ed -s '!route -qn show -inet' <<EOF
343/^default/p
344EOF
345	else
346		ed -s "!ifconfig $multicast_host" <<EOF
347/^	inet /p
348EOF
349	fi 2> /dev/null`
350	if [ "X${maddr}" != "X" ]; then
351		set $maddr
352		route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null
353	else
354		route -qn add -net 224.0.0.0/4 -interface \
355			127.0.0.1 -reject > /dev/null
356	fi
357	;;
358*:*)
359	echo 'config error, multicasting disabled until rc.conf is fixed'
360	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
361	;;
362esac
363
364
365# Configure PPPoE, GIF, GRE interfaces, delayed because they require routes
366# to be set.  PPPoE must be first, as GIF and GRE may depend on it.
367ifmstart "pppoe gif gre"
368
369# reject 127/8 other than 127.0.0.1
370route -qn add -net 127 127.0.0.1 -reject > /dev/null
371
372# Configure all the bridges.
373for bn in /etc/bridgename.*; do
374	# Strip off /etc/bridgename. prefix
375	if=${bn#/etc/bridgename.}
376	test "$if" = "*" && continue
377
378	bridgestart $if
379done
380