network.subr revision 100280
1100280Sgordon#!/bin/sh -x
225184Sjkh#
350472Speter# $FreeBSD: head/etc/network.subr 100280 2002-07-18 05:00:17Z gordon $
466830Sobrien#
525184Sjkh
6100280Sgordon# PROVIDE: network1
7100280Sgordon# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
8100280Sgordon# KEYWORD: FreeBSD
925184Sjkh
10100280Sgordon. /etc/rc.subr
1125184Sjkh
12100280Sgordonname="network1"
13100280Sgordonstart_cmd="network_start"
14100280Sgordonstop_cmd="network_stop"
15100280Sgordon
16100280Sgordonconvert_host_conf()
17100280Sgordon{
18100280Sgordon    host_conf=$1; shift;
19100280Sgordon    nsswitch_conf=$1; shift;
20100280Sgordon    awk '                                                                   \
21100280Sgordon        /^[:blank:]*#/       { next }                                       \
22100280Sgordon        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
23100280Sgordon        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
24100280Sgordon        /nis/                { nsswitch[c] = "nis";   c++; next }           \
25100280Sgordon        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
26100280Sgordon        END {                                                               \
27100280Sgordon                printf "hosts: ";                                           \
28100280Sgordon                for (i in nsswitch) printf "%s ", nsswitch[i];              \
29100280Sgordon                printf "\n";                                                \
30100280Sgordon        }' < $host_conf > $nsswitch_conf
31100280Sgordon}
32100280Sgordon
33100280Sgordongenerate_host_conf()
34100280Sgordon{
35100280Sgordon    nsswitch_conf=$1; shift;
36100280Sgordon    host_conf=$1; shift;
37100280Sgordon
38100280Sgordon    awk '
39100280SgordonBEGIN {
40100280Sgordon    xlat["files"] = "hosts";
41100280Sgordon    xlat["dns"] = "bind";
42100280Sgordon    xlat["nis"] = "nis";
43100280Sgordon    cont = 0;
44100280Sgordon}
45100280Sgordonsub(/^[\t ]*hosts:/, "") || cont {
46100280Sgordon    if (!cont)
47100280Sgordon        srcs = ""
48100280Sgordon    sub(/#.*/, "")
49100280Sgordon    gsub(/[][]/, " & ")
50100280Sgordon    cont = sub(/\\$/, "")
51100280Sgordon    srcs = srcs " " $0
52100280Sgordon}
53100280SgordonEND {
54100280Sgordon    print "# Auto-generated from nsswitch.conf, do not edit"
55100280Sgordon    ns = split(srcs, s)
56100280Sgordon    for (n = 1; n <= ns; ++n) {
57100280Sgordon        if (s[n] in xlat)
58100280Sgordon            print xlat[s[n]]
59100280Sgordon    }
60100280Sgordon}
61100280Sgordon' <$nsswitch_conf >$host_conf
62100280Sgordon}
63100280Sgordon
64100280Sgordonnetwork_start()
65100280Sgordon{
66100280Sgordon	# set hostname, turn on network
67100280Sgordon	#
68100280Sgordon	echo -n "Doing initial network setup:"
69100280Sgordon
7085831Sdes	# Generate host.conf for compatibility
7185831Sdes	#
7285831Sdes	if [ -f "/etc/nsswitch.conf" ]; then
7386163Sfenner		echo -n ' host.conf'
7485831Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
7585831Sdes	fi
7685831Sdes
7765532Snectar	# Convert host.conf to nsswitch.conf if necessary
7885831Sdes	#
7985831Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
8070108Sdougb		echo ''
8170108Sdougb		echo 'Warning: /etc/host.conf is no longer used'
8285831Sdes		echo '  /etc/nsswitch.conf will be created for you'
8385831Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
8465532Snectar	fi
8565532Snectar
8651231Ssheldonh	# Set the host name if it is not already set
8751231Ssheldonh	#
8851231Ssheldonh	if [ -z "`hostname -s`" ]; then
8951231Ssheldonh		hostname ${hostname}
9051231Ssheldonh		echo -n ' hostname'
9151231Ssheldonh	fi
9225184Sjkh
9351231Ssheldonh	# Set the domainname if we're using NIS
9451231Ssheldonh	#
95100280Sgordon	if checkyesno nisdomainname ; then
9651231Ssheldonh		domainname ${nisdomainname}
9751231Ssheldonh		echo -n ' domain'
98100280Sgordon	fi
9940006Sphk
10083677Sbrooks	# Attempt to create cloned interfaces.
10183677Sbrooks	for ifn in ${cloned_interfaces}; do
10283677Sbrooks		ifconfig ${ifn} create
10383677Sbrooks	done
10483677Sbrooks
10551231Ssheldonh	# Set up all the network interfaces, calling startup scripts if needed
10651231Ssheldonh	#
10751231Ssheldonh	case ${network_interfaces} in
10851231Ssheldonh	[Aa][Uu][Tt][Oo])
10951231Ssheldonh		network_interfaces="`ifconfig -l`"
11051231Ssheldonh		;;
11183677Sbrooks	*)
11283677Sbrooks		network_interfaces="${network_interfaces} ${cloned_interfaces}"
11383677Sbrooks		;;
11451231Ssheldonh	esac
11549122Sbrian
11654458Sobrien	dhcp_interfaces=""
11751231Ssheldonh	for ifn in ${network_interfaces}; do
11851231Ssheldonh		if [ -r /etc/start_if.${ifn} ]; then
11951231Ssheldonh			. /etc/start_if.${ifn}
12054458Sobrien			eval showstat_$ifn=1
12151231Ssheldonh		fi
12249122Sbrian
12351231Ssheldonh		# Do the primary ifconfig if specified
12451231Ssheldonh		#
12551231Ssheldonh		eval ifconfig_args=\$ifconfig_${ifn}
12629300Sdanny
12751231Ssheldonh		case ${ifconfig_args} in
12851231Ssheldonh		'')
12951231Ssheldonh			;;
13051231Ssheldonh		[Dd][Hh][Cc][Pp])
13154458Sobrien			# DHCP inits are done all in one go below
13254458Sobrien			dhcp_interfaces="$dhcp_interfaces $ifn"
13354458Sobrien			eval showstat_$ifn=1
13451231Ssheldonh			;;
13551231Ssheldonh		*)
13651231Ssheldonh			ifconfig ${ifn} ${ifconfig_args}
13754458Sobrien			eval showstat_$ifn=1
13851231Ssheldonh			;;
13951231Ssheldonh		esac
14054458Sobrien	done
14151231Ssheldonh
14254458Sobrien	if [ ! -z "${dhcp_interfaces}" ]; then
143100280Sgordon		${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces}	fi
14454458Sobrien
14554458Sobrien	for ifn in ${network_interfaces}; do
14651231Ssheldonh		# Check to see if aliases need to be added
14751231Ssheldonh		#
14851231Ssheldonh		alias=0
14951231Ssheldonh		while : ; do
15051231Ssheldonh			eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
15151231Ssheldonh			if [ -n "${ifconfig_args}" ]; then
15251231Ssheldonh				ifconfig ${ifn} ${ifconfig_args} alias
15354458Sobrien				eval showstat_$ifn=1
15486342Ssheldonh				alias=$((${alias} + 1))
15551231Ssheldonh			else
15651231Ssheldonh				break;
15751231Ssheldonh			fi
15851231Ssheldonh		done
15951231Ssheldonh
16051231Ssheldonh		# Do ipx address if specified
16151231Ssheldonh		#
16251231Ssheldonh		eval ifconfig_args=\$ifconfig_${ifn}_ipx
16351231Ssheldonh		if [ -n "${ifconfig_args}" ]; then
16451231Ssheldonh			ifconfig ${ifn} ${ifconfig_args}
16554458Sobrien			eval showstat_$ifn=1
16651231Ssheldonh		fi
16754458Sobrien	done
16851231Ssheldonh
169100280Sgordon	# catch-all for interfaces that haven't been 'ifconfig'ed so far
17054458Sobrien	for ifn in ${network_interfaces}; do
17154458Sobrien		eval showstat=\$showstat_${ifn}
17254458Sobrien		if [ ! -z ${showstat} ]; then
17351231Ssheldonh			ifconfig ${ifn}
17454458Sobrien		fi
17551231Ssheldonh	done
17651231Ssheldonh
177100280Sgordon	
17857012Shm
179100280Sgordon	# Check $defaultroute, then /etc/mygate, for the name of my gateway
180100280Sgordon	# host. That name must be in /etc/hosts.
18151231Ssheldonh	#
182100280Sgordon	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
183100280Sgordon		defaultroute=`cat /etc/mygate`
184100280Sgordon	fi
185100280Sgordon	if [ -n "$defaultroute" ]; then
186100280Sgordon		route add default $defaultroute
187100280Sgordon	fi
18851231Ssheldonh
189100280Sgordon	# Check if each configured interface xxN has an $ifaliases_xxN variable
190100280Sgordon	# associated, then configure additional IP addresses for that interface.
191100280Sgordon	# The variable contains a list of "address netmask" pairs, with
192100280Sgordon	# "netmask" set to "-" if the interface default netmask is to be used.
19385222Sdarrenr	#
194100280Sgordon	# Note that $ifaliases_xxN works only with certain configurations and
195100280Sgordon	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
196100280Sgordon	# 
19751231Ssheldonh	#
198100280Sgordon	if [ -n "$configured_interfaces" ]; then
199100280Sgordon		echo "Adding interface aliases:"
200100280Sgordon		done_aliases_message=yes
20129300Sdanny	fi
202100280Sgordon	for int in $configured_interfaces; do
203100280Sgordon		eval args=\$ifaliases_$int
204100280Sgordon		if [ -n "$args" ]; then
205100280Sgordon			set -- $args
206100280Sgordon			while [ $# -ge 2 ]; do
207100280Sgordon				addr=$1 ; net=$2 ; shift 2
208100280Sgordon				if [ "$net" = "-" ]; then
209100280Sgordon					# for compatibility only, obsolete
210100280Sgordon					ifconfig $int inet alias $addr
211100280Sgordon				else
212100280Sgordon					ifconfig $int inet alias $addr \
213100280Sgordon					    netmask $net
214100280Sgordon				fi
215100280Sgordon				# Use loopback, not the wire
216100280Sgordon				route add $addr 127.0.0.1
217100280Sgordon			done
21851231Ssheldonh		fi
219100280Sgordon	done
22044992Sbrian
221100280Sgordon	# /etc/ifaliases, if it exists, contains the names of additional IP
222100280Sgordon	# addresses for each interface. It is formatted as a series of lines
223100280Sgordon	# that contain
224100280Sgordon	#	address interface netmask
22551231Ssheldonh	#
226100280Sgordon	# Note that /etc/ifaliases works only with certain cases only and its
227100280Sgordon	# use is not recommended.  Use /etc/ifconfig.xxN instead.
228100280Sgordon	#
229100280Sgordon	#
230100280Sgordon	if [ -f /etc/ifaliases ]; then
231100280Sgordon		if [ "$done_aliases_message" != yes ]; then
232100280Sgordon			echo "Adding interface aliases:"
23344992Sbrian		fi
234100280Sgordon		while read addr int net; do
235100280Sgordon			if [ -z "$net" ]; then
236100280Sgordon				# for compatibility only, obsolete
237100280Sgordon				ifconfig $int inet alias $addr
238100280Sgordon			else
239100280Sgordon				ifconfig $int inet alias $addr netmask $net
24051231Ssheldonh			fi
241100280Sgordon			# use loopback, not the wire
242100280Sgordon			route add $addr 127.0.0.1
243100280Sgordon		done < /etc/ifaliases
24429300Sdanny	fi
24525184Sjkh
246100280Sgordon	# IPv6 interface autoconfiguration.
24751231Ssheldonh	#
248100280Sgordon	if ifconfig lo0 inet6 >/dev/null 2>&1; then
249100280Sgordon		# wait till DAD is completed. always invoke it in case
250100280Sgordon		# if are configured manually by ifconfig
251100280Sgordon		#
252100280Sgordon		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
253100280Sgordon		sleep $dadcount
254100280Sgordon		sleep 1
25540006Sphk
256100280Sgordon		if checkyesno rtsol; then
257100280Sgordon			if [ "$ip6mode" = "autohost" ]; then
258100280Sgordon				echo 'Sending router solicitation...'
259100280Sgordon				rtsol $rtsol_flags
260100280Sgordon			else
261100280Sgordon				echo
262100280Sgordon				warn \
263100280Sgordon			    "ip6mode must be set to 'autohost' to use rtsol."
264100280Sgordon			fi
26529300Sdanny
266100280Sgordon			# wait till DAD is completed, for global addresses
267100280Sgordon			# configured by router advert message.
268100280Sgordon			#
269100280Sgordon			sleep $dadcount
270100280Sgordon			sleep 1
27160628Sdillon		fi
272100280Sgordon	fi
27360628Sdillon
274100280Sgordon	# XXX this must die
275100280Sgordon	if [ -s /etc/netstart.local ]; then
276100280Sgordon		sh /etc/netstart.local start
277100280Sgordon	fi
278100280Sgordon	fi
27951231Ssheldonh	echo '.'
28051231Ssheldonh
281100280Sgordon	# Resync ipfilter
282100280Sgordon	/etc/rc.d/ipfilter resync
28325184Sjkh}
28425184Sjkh
285100280Sgordonnetwork_stop()
286100280Sgordon{
287100280Sgordon	echo "Stopping network."
28825184Sjkh
289100280Sgordon	# XXX this must die
290100280Sgordon	if [ -s /etc/netstart.local ]; then
291100280Sgordon		sh /etc/netstart.local stop
292100280Sgordon	fi
29325184Sjkh
294100280Sgordon	echo "Deleting aliases."
295100280Sgordon	if [ -f /etc/ifaliases ]; then
296100280Sgordon		while read addr int net; do
297100280Sgordon			ifconfig $int inet delete $addr
298100280Sgordon		done < /etc/ifaliases
29951231Ssheldonh	fi
30051231Ssheldonh
301100280Sgordon	for int in `ifconfig -lu`; do
302100280Sgordon		eval args=\$ifaliases_$int
303100280Sgordon		if [ -n "$args" ]; then
304100280Sgordon			set -- $args
305100280Sgordon			while [ $# -ge 2 ]; do
306100280Sgordon				addr=$1 ; net=$2 ; shift 2
307100280Sgordon				ifconfig $int inet delete $addr
308100280Sgordon			done
30951231Ssheldonh		fi
310100280Sgordon	done
31125184Sjkh
312100280Sgordon	# down interfaces
313100280Sgordon	#
314100280Sgordon	echo -n 'Downing network interfaces:'
315100280Sgordon	if [ "$net_interfaces" != NO ]; then
316100280Sgordon		if checkyesno auto_ifconfig; then
317100280Sgordon			tmp=`ifconfig -l`
318100280Sgordon		else
319100280Sgordon			tmp="$net_interfaces"
32053611Sbrian		fi
321100280Sgordon		for int in $tmp; do
322100280Sgordon			eval args=\$ifconfig_$int
323100280Sgordon			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
324100280Sgordon				echo -n " $int"
325100280Sgordon				ifconfig $int down
32692638Sdes			fi
327100280Sgordon		done
328100280Sgordon		echo "."
329100280Sgordon	fi
33057459Smarkm
331100280Sgordon	# flush routes
332100280Sgordon	#
333100280Sgordon	route -n flush
33453314Sache
33553314Sache}
33665532Snectar
337100280Sgordonload_rc_config $name
338100280Sgordonrun_rc_command "$1"
339