xref: /netbsd-src/etc/rc.d/network (revision 69b6d498973bb4d7230c2d3c12bd9a032738ec8e)
1#!/bin/sh
2#
3# $NetBSD: network,v 1.49 2005/06/28 13:36:40 symka 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
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" ] && [ -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 && [ -z "$(hostname)" ]; 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" ] && [ -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 -qn 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 will 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 must not leave the node.
66	#
67	route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
68
69	# IPv6 routing setups, and host/router mode selection.
70	#
71	if ifconfig lo0 inet6 >/dev/null 2>&1; then
72		# We have IPv6 support in kernel.
73
74		# disallow link-local unicast dest without outgoing scope
75		# identifiers.
76		#
77		route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
78
79		# disallow site-local unicast dest without outgoing scope
80		# identifiers.
81		# If you configure site-locals without scope id (it is
82		# permissible config for routers that are not on scope
83		# boundary), you may want to comment the following one out.
84		#
85		if ! checkyesno ip6sitelocal; then
86			route -q add -inet6 fec0:: -prefixlen 10 ::1 -reject
87		fi
88
89		# disallow "internal" addresses to appear on the wire.
90		#
91		route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
92
93		# disallow packets to malicious IPv4 compatible prefix
94		#
95		route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
96		route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
97		route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
98		route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
99
100		# disallow packets to malicious 6to4 prefix
101		#
102		route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
103		route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
104		route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
105		route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
106
107		# Completely disallow packets to IPv4 compatible prefix.
108		# This may conflict with RFC1933 under following circumstances:
109		# (1) An IPv6-only KAME node tries to originate packets to IPv4
110		#     comatible destination.  The KAME node has no IPv4
111		#     compatible support.  Under RFC1933, it should transmit
112		#     native IPv6 packets toward IPv4 compatible destination,
113		#     hoping it would reach a router that forwards the packet
114		#     toward auto-tunnel interface.
115		# (2) An IPv6-only node originates a packet to IPv4 compatible
116		#     destination.  A KAME node is acting as an IPv6 router, and
117		#     asked to forward it.
118		# Due to rare use of IPv4 compatible address, and security
119		# issues with it, we disable it by default.
120		#
121		route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
122
123		sysctl -qw net.inet6.ip6.forwarding=0
124		sysctl -qw net.inet6.ip6.accept_rtadv=0
125
126		case $ip6mode in
127		router)
128			echo 'IPv6 mode: router'
129			sysctl -qw net.inet6.ip6.forwarding=1
130			;;
131
132		autohost)
133			echo 'IPv6 mode: autoconfigured host'
134			sysctl -qw net.inet6.ip6.accept_rtadv=1
135			;;
136
137		host)
138			echo 'IPv6 mode: host'
139			;;
140
141		*)	warn "invalid \$ip6mode value "\"$ip6mode\"
142			;;
143
144		esac
145	fi
146
147	# Configure all of the network interfaces listed in $net_interfaces;
148	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
149	# In the following, "xxN" stands in for interface names, like "le0".
150	# For any interfaces that has an $ifconfig_xxN variable associated,
151	# we do "ifconfig xxN $ifconfig_xxN".
152	# If there is no such variable, we take the contents of the file
153	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
154	# line of the file as the arguments for a separate "ifconfig"
155	# invocation.
156	#
157	# In order to configure an interface reasonably, you at the very least
158	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
159	# and probably a netmask (as in "netmask 0xffffffe0"). You will
160	# frequently need to specify a media type, as in "media UTP", for
161	# interface cards with multiple media connections that do not
162	# autoconfigure. See the ifconfig manual page for details.
163	#
164	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
165	# configuration is possible:
166	#	inet 10.1.1.1 netmask 0xffffff00
167	#	inet 10.1.1.2 netmask 0xffffff00 alias
168	#	inet6 fec0::1 prefixlen 64 alias
169	#
170	# You can put shell script fragment into /etc/ifconfig.xxN by
171	# starting a line with "!".  Refer to ifconfig.if(5) for details.
172	#
173	if [ "$net_interfaces" != NO ]; then
174		if checkyesno auto_ifconfig; then
175			tmp=$(ifconfig -l)
176			for cloner in $(ifconfig -C 2>/dev/null); do
177				for int in /etc/ifconfig.${cloner}[0-9]*; do
178					[ ! -f $int ] && break
179					tmp="$tmp ${int##*.}"
180				done
181			done
182		else
183			tmp="$net_interfaces"
184		fi
185		echo -n 'Configuring network interfaces:'
186		for int in $tmp; do
187			eval args=\$ifconfig_$int
188			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
189				if ifconfig $int create 2>/dev/null && \
190				   checkyesno ipfilter; then
191					# resync ipf(4)
192					ipf -y >/dev/null
193				fi
194			fi
195			if [ -n "$args" ]; then
196				echo -n " $int"
197				ifconfig $int $args
198			elif [ -f /etc/ifconfig.$int ]; then
199				echo -n " $int"
200				while read args; do
201					[ -z "$args" ] && continue
202					case "$args" in
203					"#"*|create)
204						;;
205					"!"*)
206						eval ${args#*!}
207						;;
208					*)
209						eval ifconfig $int $args
210						;;
211					esac
212				done < /etc/ifconfig.$int
213			else
214				if ! checkyesno auto_ifconfig; then
215					echo
216					warn \
217			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
218					warn "interface $int not configured."
219				fi
220				continue
221			fi
222			configured_interfaces="$configured_interfaces $int"
223		done
224		echo "."
225	fi
226
227	# Check $defaultroute, then /etc/mygate, for the name or address
228	# of my IPv4 gateway host. If using a name, that name must be in
229	# /etc/hosts.
230	#
231	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
232		defaultroute=$(cat /etc/mygate)
233	fi
234	if [ -n "$defaultroute" ]; then
235		route add default $defaultroute
236	fi
237
238	# Check $defaultroute6, then /etc/mygate6, for the name or address
239	# of my IPv6 gateway host. If using a name, that name must be in
240	# /etc/hosts.  Note that the gateway host address must be a link-local
241	# address if it is not using an stf* interface.
242	#
243	if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then
244		defaultroute6=$(cat /etc/mygate6)
245	fi
246	if [ -n "$defaultroute6" ]; then
247		if [ "$ip6mode" = "autohost" ]; then
248			echo
249			warn \
250	    "ip6mode is set to 'autohost' and a v6 default route is also set."
251		fi
252		route add -inet6 default $defaultroute6
253	fi
254
255	echo -n "Adding interface aliases:"
256
257	# Check if each configured interface xxN has an $ifaliases_xxN variable
258	# associated, then configure additional IP addresses for that interface.
259	# The variable contains a list of "address netmask" pairs, with
260	# "netmask" set to "-" if the interface default netmask is to be used.
261	#
262	# Note that $ifaliases_xxN works only with certain configurations and
263	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
264	#
265	#
266	for int in lo0 $configured_interfaces; do
267		eval args=\$ifaliases_$int
268		if [ -n "$args" ]; then
269			set -- $args
270			while [ $# -ge 2 ]; do
271				addr=$1 ; net=$2 ; shift 2
272				if [ "$net" = "-" ]; then
273					# for compatibility only, obsolete
274					ifconfig $int inet alias $addr
275				else
276					ifconfig $int inet alias $addr \
277					    netmask $net
278				fi
279				echo -n " $int:$addr"
280			done
281		fi
282	done
283
284	# /etc/ifaliases, if it exists, contains the names of additional IP
285	# addresses for each interface. It is formatted as a series of lines
286	# that contain
287	#	address interface netmask
288	#
289	# Note that /etc/ifaliases works only with certain cases only and its
290	# use is not recommended.  Use /etc/ifconfig.xxN instead.
291	#
292	#
293	if [ -f /etc/ifaliases ]; then
294		while read addr int net; do
295			if [ -z "$net" ]; then
296				# for compatibility only, obsolete
297				ifconfig $int inet alias $addr
298			else
299				ifconfig $int inet alias $addr netmask $net
300			fi
301		done < /etc/ifaliases
302	fi
303
304	echo
305
306	# IPv6 interface autoconfiguration.
307	#
308	if ifconfig lo0 inet6 >/dev/null 2>&1; then
309		# wait till DAD is completed. always invoke it in case
310		# if are configured manually by ifconfig
311		#
312		dadcount=$(sysctl -n net.inet6.ip6.dad_count 2>/dev/null)
313		sleep $dadcount
314		sleep 1
315
316		if checkyesno rtsol; then
317			if [ "$ip6mode" = "autohost" ]; then
318				echo 'Sending router solicitation...'
319				rtsol $rtsol_flags
320			else
321				echo
322				warn \
323			    "ip6mode must be set to 'autohost' to use rtsol."
324			fi
325
326			# wait till DAD is completed, for global addresses
327			# configured by router advert message.
328			#
329			sleep $dadcount
330			sleep 1
331		fi
332	fi
333
334	# XXX this must die
335	if [ -s /etc/netstart.local ]; then
336		sh /etc/netstart.local start
337	fi
338}
339
340network_stop()
341{
342	echo "Stopping network."
343
344	# XXX this must die
345	if [ -s /etc/netstart.local ]; then
346		sh /etc/netstart.local stop
347	fi
348
349	echo "Deleting aliases."
350	if [ -f /etc/ifaliases ]; then
351		while read addr int net; do
352			ifconfig $int inet delete $addr
353		done < /etc/ifaliases
354	fi
355
356	for int in $(ifconfig -lu); do
357		eval args=\$ifaliases_$int
358		if [ -n "$args" ]; then
359			set -- $args
360			while [ $# -ge 2 ]; do
361				addr=$1 ; net=$2 ; shift 2
362				ifconfig $int inet delete $addr
363			done
364		fi
365	done
366
367	# down interfaces
368	#
369	echo -n 'Downing network interfaces:'
370	if [ "$net_interfaces" != NO ]; then
371		if checkyesno auto_ifconfig; then
372			tmp=$(ifconfig -l)
373		else
374			tmp="$net_interfaces"
375		fi
376		for int in $tmp; do
377			eval args=\$ifconfig_$int
378			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
379				echo -n " $int"
380				ifconfig $int down
381				if ifconfig $int destroy 2>/dev/null && \
382				   checkyesno ipfilter; then
383					# resync ipf(4)
384					ipf -y >/dev/null
385				fi
386			fi
387		done
388		echo "."
389	fi
390
391	# flush routes
392	#
393	route -qn flush
394
395}
396
397load_rc_config $name
398load_rc_config_var dhclient dhclient
399load_rc_config_var ipfilter ipfilter
400run_rc_command "$1"
401