network revision 1.18
1#!/bin/sh
2#
3# $NetBSD: network,v 1.18 2000/08/02 13:59:11 itojun Exp $
4#
5
6# PROVIDE: network
7# REQUIRE: root mountcritlocal 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" -a -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" -a -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					int=`IFS='.'; set -- $int; echo $2`
98					tmp="$tmp $int"
99				done
100			done
101		else
102			tmp="$net_interfaces"
103		fi
104		echo -n 'Configuring network interfaces:'
105		for int in $tmp; do
106			eval `echo 'args=$ifconfig_'$int`
107			if [ -n "$args" ]; then
108				echo -n " $int"
109				ifconfig $int $args
110			elif [ -f /etc/ifconfig.$int ]; then
111				echo -n " $int"
112				(while read args; do
113					if [ -n "`eval echo '$args'`" ] ; then
114						ifconfig $int $args
115					fi
116				done) < /etc/ifconfig.$int
117			else
118				if ! checkyesno auto_ifconfig; then
119					echo
120					warn \
121			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
122					warn "interface $int not configured."
123				fi
124				continue
125			fi
126			configured_interfaces="$configured_interfaces $int"
127		done
128		echo "."
129	fi
130
131	# Check $defaultroute, then /etc/mygate, for the name of my gateway
132	# host. That name must be in /etc/hosts.
133	#
134	if [ -z "$defaultroute" -a -f /etc/mygate ]; then
135		defaultroute=`cat /etc/mygate`
136	fi
137	if [ -n "$defaultroute" ]; then
138		route add default $defaultroute
139	fi
140
141	# Check if each configured interface xxN has an $ifaliases_xxN variable
142	# associated, then configure additional IP addresses for that interface.
143	# The variable contains a list of "address netmask" pairs, with
144	# "netmask" set to "-" if the interface default netmask is to be used.
145	#
146	# Note that $ifaliases_xxN works only with certain configurations and
147	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
148	# 
149	#
150	if [ -n "$configured_interfaces" ]; then
151		echo "Adding interface aliases:"
152		done_aliases_message=yes
153	fi
154	for int in $configured_interfaces; do
155		eval `echo 'args=$ifaliases_'$int`
156		if [ -n "$args" ]; then
157			set -- $args
158			while [ $# -ge 2 ]; do
159				addr=$1 ; net=$2 ; shift 2
160				if [ "$net" = "-" ]; then
161					# for compatibility only, obsolete
162					ifconfig $int inet alias $addr
163				else
164					ifconfig $int inet alias $addr \
165					    netmask $net
166				fi
167				# Use loopback, not the wire
168				route add $addr 127.0.0.1
169			done
170		fi
171	done
172
173	# /etc/ifaliases, if it exists, contains the names of additional IP
174	# addresses for each interface. It is formatted as a series of lines
175	# that contain
176	#	address interface netmask
177	#
178	# Note that /etc/ifaliases works only with certain cases only and its
179	# use is not recommended.  Use /etc/ifconfig.xxN instead.
180	#
181	#
182	if [ -f /etc/ifaliases ]; then
183	(
184		if [ "$done_aliases_message" != yes ]; then
185			echo "Adding interface aliases:"
186		fi
187		while read addr int net; do
188			if [ -z "$net" ]; then
189				# for compatibility only, obsolete
190				ifconfig $int inet alias $addr
191			else
192				ifconfig $int inet alias $addr netmask $net
193			fi
194			# use loopback, not the wire
195			route add $addr 127.0.0.1
196		done
197	) < /etc/ifaliases
198	fi
199
200	# IPv6
201	# Note that manual configuration can be done in the above, using
202	# ifconfig.
203	#
204	if ifconfig lo0 inet6 >/dev/null 2>&1; then
205		# We have IPv6 support in kernel.
206
207		# disallow link-local unicast dest without outgoing scope
208		# identifiers.
209		#
210		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
211
212		# disallow site-local unicast dest without outgoing scope
213		# identifiers.
214		# If you configure site-locals without scope id (it is
215		# permissible config for routers that are not on scope
216		# boundary), you may want to comment the following one out.
217		#
218		route add -inet6 fec0:: -prefixlen 10 ::1 -reject
219
220		# disallow "internal" addresses to appear on the wire.
221		#
222		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
223
224		# disallow packets to malicious IPv4 compatible prefix
225		#
226		route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
227		route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
228		route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
229		route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
230
231		# disallow packets to malicious 6to4 prefix
232		#
233		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
234		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
235		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
236		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
237
238		# Completely disallow packets to IPv4 compatible prefix.
239		# This may conflict with RFC1933 under following circumstances:
240		# (1) An IPv6-only KAME node tries to originate packets to IPv4
241		#     comatible destination.  The KAME node has no IPv4
242		#     compatible support.  Under RFC1933, it should transmit
243		#     native IPv6 packets toward IPv4 compatible destination,
244		#     hoping it would reach a router that forwards the packet
245		#     toward auto-tunnel interface.
246		# (2) An IPv6-only node originates a packet to IPv4 compatible
247		#     destination.  A KAME node is acting as an IPv6 router, and
248		#     asked to forward it.
249		# Due to rare use of IPv4 compatible address, and security
250		# issues with it, we disable it by default.
251		#
252		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
253
254		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
255		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
256
257		# backward compatibility
258		#
259		if [ -z "$ip6mode" -a -n "$ip6forwarding" ]; then
260			warn 'Please migrate to newer rc.conf' \
261			    '(use ip6mode, not ip6forwarding)'
262			if checkyesno ip6forwarding; then
263				ip6mode=router
264			else
265				if checkyesno rtsol; then
266					ip6mode=autohost
267				else
268					ip6mode=host
269				fi
270			fi
271		fi
272
273		case $ip6mode in
274		router)
275			echo 'IPv6 mode: router'
276			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
277			;;
278
279		autohost)
280			echo 'IPv6 mode: autoconfigured host'
281			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
282			;;
283
284		host)	
285			echo 'IPv6 mode: host'
286			;;
287
288		*)	echo 'WARNING: invalid value in ip6mode'
289			;;
290
291		esac
292
293		# wait till DAD is completed. always invoke it in case
294		# if are configured manually by ifconfig
295		#
296		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
297		sleep $dadcount
298		sleep 1
299
300		if checkyesno rtsol; then
301			if [ "$ip6mode" = "autohost" ]; then
302				echo 'Sending router solicitation...'
303				rtsol $rtsol_flags
304			else
305				echo
306				warn \
307			    "ip6mode must be set to 'autohost' to use rtsol."
308			fi
309
310			# wait till DAD is completed, for global addresses
311			# configured by router advert message.
312			#
313			sleep $dadcount
314			sleep 1
315		fi
316	fi
317
318	# XXX this must die
319	if [ -s /etc/netstart.local ]; then
320		sh /etc/netstart.local start
321	fi
322}
323
324network_stop()
325{
326	echo "Stopping network."
327
328	# XXX this must die
329	if [ -s /etc/netstart.local ]; then
330		sh /etc/netstart.local stop
331	fi
332
333	echo "Deleting aliases."
334	if [ -f /etc/ifaliases ]; then
335	(
336		while read addr int net; do
337			ifconfig $int inet delete $addr
338		done
339	) < /etc/ifaliases
340	fi
341
342	for int in $configured_interfaces; do
343		eval `echo 'args=$ifaliases_'$int`
344		if [ -n "$args" ]; then
345			set -- $args
346			while [ $# -ge 2 ]; do
347				addr=$1 ; net=$2 ; shift 2
348				ifconfig $int inet delete $addr
349			done
350		fi
351	done
352
353	# down interfaces
354	#
355	echo -n 'Downing network interfaces:'
356	if [ "$net_interfaces" != NO ]; then
357		if checkyesno auto_ifconfig; then
358			tmp="`ifconfig -l`"
359		else
360			tmp="$net_interfaces"
361		fi
362		for int in $tmp; do
363			eval `echo 'args=$ifconfig_'$int`
364			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
365				echo -n " $int"
366				ifconfig $int down
367			fi
368		done
369		echo "."
370	fi
371
372	# flush routes
373	#
374	route -n flush
375
376}
377
378load_rc_config $name
379run_rc_command "$1"
380