network revision 1.56
1#!/bin/sh
2#
3# $NetBSD: network,v 1.56 2008/09/26 10:31:46 apb Exp $
4#
5
6# PROVIDE: network
7# REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl
8# BEFORE:  NETWORKING
9
10$_rc_subr_loaded . /etc/rc.subr
11
12name="network"
13start_cmd="network_start"
14stop_cmd="network_stop"
15
16nl='
17' # a newline
18
19network_start()
20{
21	# set hostname, turn on network
22	#
23	echo "Starting network."
24
25	# If $hostname is set, use it for my Internet name,
26	# otherwise use /etc/myname
27	#
28	if [ -z "$hostname" ] && [ -f /etc/myname ]; then
29		hostname=$(cat /etc/myname)
30	fi
31	if [ -n "$hostname" ]; then
32		echo "Hostname: $hostname"
33		hostname $hostname
34	else
35		# Don't warn about it if we're going to run
36		# DHCP later, as we will probably get the
37		# hostname at that time.
38		#
39		if ! checkyesno dhclient && [ -z "$(hostname)" ]; then
40			warn "\$hostname not set."
41		fi
42	fi
43
44	# Check $domainname first, then /etc/defaultdomain,
45	# for NIS/YP domain name
46	#
47	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
48		domainname=$(cat /etc/defaultdomain)
49	fi
50	if [ -n "$domainname" ]; then
51		echo "NIS domainname: $domainname"
52		domainname $domainname
53	fi
54
55	# Flush all routes just to make sure it is clean
56	if checkyesno flushroutes; then
57		/sbin/route -qn flush
58	fi
59
60	# Set the address for the first loopback interface, so that the
61	# auto-route from a newly configured interface's address to lo0
62	# works correctly.
63	#
64	# NOTE: obscure networking problems will occur if lo0 isn't configured.
65	#
66	/sbin/ifconfig lo0 inet 127.0.0.1
67
68	# According to RFC1122, 127.0.0.0/8 must not leave the node.
69	#
70	/sbin/route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
71
72	# IPv6 routing setups, and host/router mode selection.
73	#
74	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
75		# We have IPv6 support in kernel.
76
77		# disallow link-local unicast dest without outgoing scope
78		# identifiers.
79		#
80		/sbin/route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
81
82		# disallow the use of the RFC3849 documentation address
83		#
84		/sbin/route -q add -inet6 2001:db8:: -prefixlen 32 ::1 -reject
85
86		# IPv6 site-local scoped address prefix (fec0::/10)
87		# has been deprecated by RFC3879.
88		#
89		if [ -n "$ip6sitelocal" ]; then
90			warn "\$ip6sitelocal is no longer valid"
91		fi
92
93		# disallow "internal" addresses to appear on the wire.
94		#
95		/sbin/route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
96
97		# disallow packets to malicious IPv4 compatible prefix
98		#
99		/sbin/route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
100		/sbin/route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
101		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
102		/sbin/route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
103
104		# disallow packets to malicious 6to4 prefix
105		#
106		/sbin/route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
107		/sbin/route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
108		/sbin/route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
109		/sbin/route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
110
111		# Completely disallow packets to IPv4 compatible prefix.
112		# This may conflict with RFC1933 under following circumstances:
113		# (1) An IPv6-only KAME node tries to originate packets to IPv4
114		#     compatible destination.  The KAME node has no IPv4
115		#     compatible support.  Under RFC1933, it should transmit
116		#     native IPv6 packets toward IPv4 compatible destination,
117		#     hoping it would reach a router that forwards the packet
118		#     toward auto-tunnel interface.
119		# (2) An IPv6-only node originates a packet to IPv4 compatible
120		#     destination.  A KAME node is acting as an IPv6 router, and
121		#     asked to forward it.
122		# Due to rare use of IPv4 compatible address, and security
123		# issues with it, we disable it by default.
124		#
125		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
126
127		/sbin/sysctl -qw net.inet6.ip6.forwarding=0
128		/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=0
129
130		case $ip6mode in
131		router)
132			echo 'IPv6 mode: router'
133			/sbin/sysctl -qw net.inet6.ip6.forwarding=1
134
135			# disallow unique-local unicast forwarding without
136			# explicit configuration.
137			if ! checkyesno ip6uniquelocal; then
138				/sbin/route -q add -inet6 fc00:: -prefixlen 7 \
139				    ::1 -reject
140			fi
141			;;
142
143		autohost)
144			echo 'IPv6 mode: autoconfigured host'
145			/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=1
146			;;
147
148		host)	
149			echo 'IPv6 mode: host'
150			;;
151
152		*)	warn "invalid \$ip6mode value "\"$ip6mode\"
153			;;
154
155		esac
156	fi
157
158	# Configure all of the network interfaces listed in $net_interfaces;
159	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
160	# In the following, "xxN" stands in for interface names, like "le0".
161	#
162	# For any interfaces that has an $ifconfig_xxN variable
163	# associated, we break it into lines using ';' as a separator,
164	# then process it just like the contents of an /etc/ifconfig.xxN
165	# file.
166	#
167	# For each line from the $ifconfig_xxN variable or the
168	# /etc/ifconfig.xxN file, we ignore comments and blank lines,
169	# treat lines beginning with "!" as commands to execute, treat
170	# "dhcp" as a special case to invoke dhcpcd, and for any other
171	# line we run "ifconfig xxN", using each line of the file as the
172	# arguments for a separate "ifconfig" invocation.
173	#
174	# In order to configure an interface reasonably, you at the very least
175	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
176	# and probably a netmask (as in "netmask 0xffffffe0"). You will
177	# frequently need to specify a media type, as in "media UTP", for
178	# interface cards with multiple media connections that do not
179	# autoconfigure. See the ifconfig manual page for details.
180	#
181	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
182	# configuration is possible:
183	#	inet 10.1.1.1 netmask 0xffffff00
184	#	inet 10.1.1.2 netmask 0xffffff00 alias
185	#	inet6 2001:db8::1 prefixlen 64 alias
186	#
187	# You can put shell script fragment into /etc/ifconfig.xxN by
188	# starting a line with "!".  Refer to ifconfig.if(5) for details.
189	#
190	if [ "$net_interfaces" != NO ]; then
191		if checkyesno auto_ifconfig; then
192			tmp=$(/sbin/ifconfig -l)
193			for cloner in $(/sbin/ifconfig -C 2>/dev/null); do
194				for int in /etc/ifconfig.${cloner}[0-9]*; do
195					[ ! -f $int ] && break
196					tmp="$tmp ${int##*.}"
197				done
198			done
199		else
200			tmp="$net_interfaces"
201		fi
202		echo -n 'Configuring network interfaces:'
203		for int in $tmp; do
204			eval argslist=\$ifconfig_$int
205
206			# Skip interfaces that do not have explicit
207			# configuration information.  If auto_ifconfig is
208			# false then also warn about such interfaces.
209			#
210			if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ]
211			then
212				if ! checkyesno auto_ifconfig; then
213					echo
214					warn \
215			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
216					warn "interface $int not configured."
217				fi
218				continue
219			fi
220
221			echo -n " $int"
222
223			# Create the interface if necessary.
224			# If the interface did not exist before,
225			# then also resync ipf(4).
226			#
227			if /sbin/ifconfig $int create 2>/dev/null && \
228			   checkyesno ipfilter; then
229				/sbin/ipf -y >/dev/null
230			fi
231
232			# If $ifconfig_xxN is empty, then use
233			# /etc/ifconfig.xxN, which we know exists due to
234			# an earlier test.
235			#
236			# If $ifconfig_xxN is non-empty and contains a
237			# newline, then just use it as is.  (This allows
238			# semicolons through unmolested.)
239			#
240			# If $ifconfig_xxN is non-empty and does not
241			# contain a newline, then convert all semicolons
242			# to newlines.
243			#
244			case "$argslist" in
245			'')
246				cat /etc/ifconfig.$int
247				;;
248			*"${nl}"*)
249				echo "$argslist"
250				;;
251			*)
252				(
253					set -o noglob
254					IFS=';'; set -- $argslist
255					#echo >&2 "[$#] [$1] [$2] [$3] [$4]"
256					IFS="$nl"; echo "$*"
257				)
258				;;
259			esac |
260			while read -r args; do
261				case "$args" in
262				''|"#"*|create)
263					;;
264				"!"*)
265					# Run arbitrary command in a subshell.
266					( eval "${args#*!}" )
267					;;
268				dhcp)
269					/sbin/dhcpcd -n ${dhcpcd_flags} $int
270					;;
271				*)
272					# Pass args to ifconfig.  Note
273					# that args may contain embedded
274					# shell metacharacters, such as
275					# "ssid 'foo;*>bar'".
276					(
277						set -o noglob
278						set -- $args
279						#echo >&2 "[$#] [$1] [$2] [$3]"
280						/sbin/ifconfig $int "$@"
281					)
282					;;
283				esac
284			done
285			configured_interfaces="$configured_interfaces $int"
286		done
287		echo "."
288	fi
289
290	echo -n "Adding interface aliases:"
291
292	# Check if each configured interface xxN has an $ifaliases_xxN variable
293	# associated, then configure additional IP addresses for that interface.
294	# The variable contains a list of "address netmask" pairs, with
295	# "netmask" set to "-" if the interface default netmask is to be used.
296	#
297	# Note that $ifaliases_xxN works only in certain cases and its
298	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
299	# commands in $ifconfig_xxN instead.
300	#
301	for int in lo0 $configured_interfaces; do
302		eval args=\$ifaliases_$int
303		if [ -n "$args" ]; then
304			set -- $args
305			while [ $# -ge 2 ]; do
306				addr=$1 ; net=$2 ; shift 2
307				if [ "$net" = "-" ]; then
308					# for compatibility only, obsolete
309					/sbin/ifconfig $int inet alias $addr
310				else
311					/sbin/ifconfig $int inet alias $addr \
312					    netmask $net
313				fi
314				echo -n " $int:$addr"
315			done
316		fi
317	done
318
319	# /etc/ifaliases, if it exists, contains the names of additional IP
320	# addresses for each interface. It is formatted as a series of lines
321	# that contain
322	#	address interface netmask
323	#
324	# Note that /etc/ifaliases works only in certain cases and its
325	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
326	# commands in $ifconfig_xxN instead.
327	#
328	if [ -f /etc/ifaliases ]; then
329		while read addr int net; do
330			if [ -z "$net" ]; then
331				# for compatibility only, obsolete
332				/sbin/ifconfig $int inet alias $addr
333			else
334				/sbin/ifconfig $int inet alias $addr netmask $net
335			fi
336		done < /etc/ifaliases
337	fi
338
339	echo "." # for "Adding interface aliases:"
340
341	# Check $defaultroute, then /etc/mygate, for the name or address
342	# of my IPv4 gateway host. If using a name, that name must be in
343	# /etc/hosts.
344	#
345	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
346		defaultroute=$(cat /etc/mygate)
347	fi
348	if [ -n "$defaultroute" ]; then
349		/sbin/route add default $defaultroute
350	fi
351
352	# Check $defaultroute6, then /etc/mygate6, for the name or address
353	# of my IPv6 gateway host. If using a name, that name must be in
354	# /etc/hosts.  Note that the gateway host address must be a link-local
355	# address if it is not using an stf* interface.
356	#
357	if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then
358		defaultroute6=$(cat /etc/mygate6)
359	fi
360	if [ -n "$defaultroute6" ]; then
361		if [ "$ip6mode" = "autohost" ]; then
362			echo
363			warn \
364	    "ip6mode is set to 'autohost' and a v6 default route is also set."
365		fi
366		/sbin/route add -inet6 default $defaultroute6
367	fi
368
369	# IPv6 interface autoconfiguration.
370	#
371	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
372		# wait till DAD is completed. always invoke it in case
373		# if are configured manually by ifconfig
374		#
375		dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null)
376		sleep $dadcount
377		sleep 1
378
379		if checkyesno rtsol; then
380			if [ "$ip6mode" = "autohost" ]; then
381				echo 'Sending router solicitation...'
382				/sbin/rtsol $rtsol_flags
383			else
384				echo
385				warn \
386			    "ip6mode must be set to 'autohost' to use rtsol."
387			fi
388
389			# wait till DAD is completed, for global addresses
390			# configured by router advert message.
391			#
392			sleep $dadcount
393			sleep 1
394		fi
395	fi
396
397	# XXX this must die
398	if [ -s /etc/netstart.local ]; then
399		sh /etc/netstart.local start
400	fi
401}
402
403network_stop()
404{
405	echo "Stopping network."
406
407	# XXX this must die
408	if [ -s /etc/netstart.local ]; then
409		sh /etc/netstart.local stop
410	fi
411
412	echo "Deleting aliases."
413	if [ -f /etc/ifaliases ]; then
414		while read addr int net; do
415			/sbin/ifconfig $int inet delete $addr
416		done < /etc/ifaliases
417	fi
418
419	for int in $(/sbin/ifconfig -lu); do
420		eval args=\$ifaliases_$int
421		if [ -n "$args" ]; then
422			set -- $args
423			while [ $# -ge 2 ]; do
424				addr=$1 ; net=$2 ; shift 2
425				/sbin/ifconfig $int inet delete $addr
426			done
427		fi
428	done
429
430	# down interfaces
431	#
432	echo -n 'Downing network interfaces:'
433	if [ "$net_interfaces" != NO ]; then
434		if checkyesno auto_ifconfig; then
435			tmp=$(/sbin/ifconfig -l)
436		else
437			tmp="$net_interfaces"
438		fi
439		for int in $tmp; do
440			eval args=\$ifconfig_$int
441			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
442				echo -n " $int"
443				/sbin/dhcpcd -k $int 2> /dev/null
444				/sbin/ifconfig $int down
445				if /sbin/ifconfig $int destroy 2>/dev/null && \
446				   checkyesno ipfilter; then
447					# resync ipf(4)
448					/sbin/ipf -y >/dev/null
449				fi
450			fi
451		done
452		echo "."
453	fi
454
455	# flush routes
456	#
457	/sbin/route -qn flush
458
459}
460
461load_rc_config $name
462load_rc_config_var dhclient dhclient
463load_rc_config_var ipfilter ipfilter
464run_rc_command "$1"
465