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