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