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