network revision 1.21
1#!/bin/sh
2#
3# $NetBSD: network,v 1.21 2000/11/17 04:19:06 lukem Exp $
4#
5
6# PROVIDE: network
7# REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl
8
9. /etc/rc.subr
10
11name="network"
12start_cmd="network_start"
13stop_cmd="network_stop"
14
15network_start()
16{
17	# set hostname, turn on network
18	#
19	echo "Starting network."
20
21	# If $hostname is set, use it for my Internet name,
22	# otherwise use /etc/myname
23	#
24	if [ -z "$hostname" ] && [ -f /etc/myname ]; then
25		hostname=`cat /etc/myname`
26	fi
27	if [ -n "$hostname" ]; then
28		echo "Hostname: $hostname"
29		hostname $hostname
30	else
31		# Don't warn about it if we're going to run
32		# DHCP later, as we will probably get the
33		# hostname at that time.
34		#
35		if ! checkyesno dhclient && [ -z "`hostname`" ]; then
36			warn "\$hostname not set."
37		fi
38	fi
39
40	# Check $domainname first, then /etc/defaultdomain,
41	# for NIS/YP domain name
42	#
43	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
44		domainname=`cat /etc/defaultdomain`
45	fi
46	if [ -n "$domainname" ]; then
47		echo "NIS domainname: $domainname"
48		domainname $domainname
49	fi
50
51	# Flush all routes just to make sure it is clean
52	if checkyesno flushroutes; then
53		route -n flush
54	fi
55
56	# Set the address for the first loopback interface, so that the
57	# auto-route from a newly configured interface's address to lo0
58	# works correctly.
59	#
60	# NOTE: obscure networking problems may occur if lo0 isn't configured...
61	#
62	ifconfig lo0 inet 127.0.0.1
63
64	# According to RFC1122, 127.0.0.0/8 should not leave the node.
65	#
66	route add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
67
68	# Configure all of the network interfaces listed in $net_interfaces;
69	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
70	# In the following, "xxN" stands in for interface names, like "le0".
71	# For any interfaces that has an $ifconfig_xxN variable associated,
72	# we do "ifconfig xxN $ifconfig_xxN".
73	# If there is no such variable, we take the contents of the file
74	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
75	# line of the file as the arguments for a seperate "ifconfig"
76	# invocation.
77	#
78	# In order to configure an interface reasonably, you at the very least
79	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
80	# and probably a netmask (as in "netmask 0xffffffe0"). You will
81	# frequently need to specify a media type, as in "media UTP", for
82	# interface cards with multiple media connections that do not
83	# autoconfigure. See the ifconfig manual page for details.
84	#
85	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
86	# configuration is possible:
87	#	inet 10.1.1.1 netmask 0xffffff00
88	#	inet 10.1.1.2 netmask 0xffffff00 alias
89	#	inet6 fec0::1 prefixlen 64 alias
90	#
91	if [ "$net_interfaces" != NO ]; then
92		if checkyesno auto_ifconfig; then
93			tmp="`ifconfig -l`"
94			for cloner in `ifconfig -C 2>/dev/null`; do
95				for int \
96		    in `/bin/ls /etc/ifconfig.${cloner}[0-9]* 2>/dev/null`; do
97					tmp="$tmp ${int##*.}"
98				done
99			done
100		else
101			tmp="$net_interfaces"
102		fi
103		echo -n 'Configuring network interfaces:'
104		for int in $tmp; do
105			eval args=\$ifconfig_$int
106			if [ -n "$args" ]; then
107				echo -n " $int"
108				ifconfig $int $args
109			elif [ -f /etc/ifconfig.$int ]; then
110				echo -n " $int"
111				while read args; do
112					if [ -n "`eval echo '$args'`" ] ; then
113						ifconfig $int $args
114					fi
115				done < /etc/ifconfig.$int
116			else
117				if ! checkyesno auto_ifconfig; then
118					echo
119					warn \
120			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
121					warn "interface $int not configured."
122				fi
123				continue
124			fi
125			configured_interfaces="$configured_interfaces $int"
126		done
127		echo "."
128	fi
129
130	# Check $defaultroute, then /etc/mygate, for the name of my gateway
131	# host. That name must be in /etc/hosts.
132	#
133	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
134		defaultroute=`cat /etc/mygate`
135	fi
136	if [ -n "$defaultroute" ]; then
137		route add default $defaultroute
138	fi
139
140	# Check if each configured interface xxN has an $ifaliases_xxN variable
141	# associated, then configure additional IP addresses for that interface.
142	# The variable contains a list of "address netmask" pairs, with
143	# "netmask" set to "-" if the interface default netmask is to be used.
144	#
145	# Note that $ifaliases_xxN works only with certain configurations and
146	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
147	# 
148	#
149	if [ -n "$configured_interfaces" ]; then
150		echo "Adding interface aliases:"
151		done_aliases_message=yes
152	fi
153	for int in $configured_interfaces; do
154		eval args=\$ifaliases_$int
155		if [ -n "$args" ]; then
156			set -- $args
157			while [ $# -ge 2 ]; do
158				addr=$1 ; net=$2 ; shift 2
159				if [ "$net" = "-" ]; then
160					# for compatibility only, obsolete
161					ifconfig $int inet alias $addr
162				else
163					ifconfig $int inet alias $addr \
164					    netmask $net
165				fi
166				# Use loopback, not the wire
167				route add $addr 127.0.0.1
168			done
169		fi
170	done
171
172	# /etc/ifaliases, if it exists, contains the names of additional IP
173	# addresses for each interface. It is formatted as a series of lines
174	# that contain
175	#	address interface netmask
176	#
177	# Note that /etc/ifaliases works only with certain cases only and its
178	# use is not recommended.  Use /etc/ifconfig.xxN instead.
179	#
180	#
181	if [ -f /etc/ifaliases ]; then
182		if [ "$done_aliases_message" != yes ]; then
183			echo "Adding interface aliases:"
184		fi
185		while read addr int net; do
186			if [ -z "$net" ]; then
187				# for compatibility only, obsolete
188				ifconfig $int inet alias $addr
189			else
190				ifconfig $int inet alias $addr netmask $net
191			fi
192			# use loopback, not the wire
193			route add $addr 127.0.0.1
194		done < /etc/ifaliases
195	fi
196
197	# IPv6
198	# Note that manual configuration can be done in the above, using
199	# ifconfig.
200	#
201	if ifconfig lo0 inet6 >/dev/null 2>&1; then
202		# We have IPv6 support in kernel.
203
204		# disallow link-local unicast dest without outgoing scope
205		# identifiers.
206		#
207		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
208
209		# disallow site-local unicast dest without outgoing scope
210		# identifiers.
211		# If you configure site-locals without scope id (it is
212		# permissible config for routers that are not on scope
213		# boundary), you may want to comment the following one out.
214		#
215		route add -inet6 fec0:: -prefixlen 10 ::1 -reject
216
217		# disallow "internal" addresses to appear on the wire.
218		#
219		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
220
221		# disallow packets to malicious IPv4 compatible prefix
222		#
223		route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
224		route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
225		route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
226		route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
227
228		# disallow packets to malicious 6to4 prefix
229		#
230		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
231		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
232		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
233		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
234
235		# Completely disallow packets to IPv4 compatible prefix.
236		# This may conflict with RFC1933 under following circumstances:
237		# (1) An IPv6-only KAME node tries to originate packets to IPv4
238		#     comatible destination.  The KAME node has no IPv4
239		#     compatible support.  Under RFC1933, it should transmit
240		#     native IPv6 packets toward IPv4 compatible destination,
241		#     hoping it would reach a router that forwards the packet
242		#     toward auto-tunnel interface.
243		# (2) An IPv6-only node originates a packet to IPv4 compatible
244		#     destination.  A KAME node is acting as an IPv6 router, and
245		#     asked to forward it.
246		# Due to rare use of IPv4 compatible address, and security
247		# issues with it, we disable it by default.
248		#
249		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
250
251		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
252		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
253
254		# backward compatibility
255		#
256		if [ -z "$ip6mode" ] && [ -n "$ip6forwarding" ]; then
257			warn 'Please migrate to newer rc.conf' \
258			    '(use ip6mode, not ip6forwarding)'
259			if checkyesno ip6forwarding; then
260				ip6mode=router
261			elif checkyesno rtsol; then
262				ip6mode=autohost
263			else
264				ip6mode=host
265			fi
266		fi
267
268		case $ip6mode in
269		router)
270			echo 'IPv6 mode: router'
271			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
272			;;
273
274		autohost)
275			echo 'IPv6 mode: autoconfigured host'
276			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
277			;;
278
279		host)	
280			echo 'IPv6 mode: host'
281			;;
282
283		*)	echo 'WARNING: invalid value in ip6mode'
284			;;
285
286		esac
287
288		# wait till DAD is completed. always invoke it in case
289		# if are configured manually by ifconfig
290		#
291		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
292		sleep $dadcount
293		sleep 1
294
295		if checkyesno rtsol; then
296			if [ "$ip6mode" = "autohost" ]; then
297				echo 'Sending router solicitation...'
298				rtsol $rtsol_flags
299			else
300				echo
301				warn \
302			    "ip6mode must be set to 'autohost' to use rtsol."
303			fi
304
305			# wait till DAD is completed, for global addresses
306			# configured by router advert message.
307			#
308			sleep $dadcount
309			sleep 1
310		fi
311	fi
312
313	# XXX this must die
314	if [ -s /etc/netstart.local ]; then
315		sh /etc/netstart.local start
316	fi
317}
318
319network_stop()
320{
321	echo "Stopping network."
322
323	# XXX this must die
324	if [ -s /etc/netstart.local ]; then
325		sh /etc/netstart.local stop
326	fi
327
328	echo "Deleting aliases."
329	if [ -f /etc/ifaliases ]; then
330		while read addr int net; do
331			ifconfig $int inet delete $addr
332		done < /etc/ifaliases
333	fi
334
335	for int in `ifconfig -lu`; do
336		eval args=\$ifaliases_$int
337		if [ -n "$args" ]; then
338			set -- $args
339			while [ $# -ge 2 ]; do
340				addr=$1 ; net=$2 ; shift 2
341				ifconfig $int inet delete $addr
342			done
343		fi
344	done
345
346	# down interfaces
347	#
348	echo -n 'Downing network interfaces:'
349	if [ "$net_interfaces" != NO ]; then
350		if checkyesno auto_ifconfig; then
351			tmp="`ifconfig -l`"
352		else
353			tmp="$net_interfaces"
354		fi
355		for int in $tmp; do
356			eval args=\$ifconfig_$int
357			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
358				echo -n " $int"
359				ifconfig $int down
360			fi
361		done
362		echo "."
363	fi
364
365	# flush routes
366	#
367	route -n flush
368
369}
370
371load_rc_config $name
372run_rc_command "$1"
373