network.subr revision 103710
1100280Sgordon#!/bin/sh -x
225184Sjkh#
350472Speter# $FreeBSD: head/etc/network.subr 103710 2002-09-20 18:31:55Z ume $
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
64100282Sdougbnetwork_gif_setup() {
65100282Sdougb	case ${gif_interfaces} in
66100282Sdougb	[Nn][Oo] | '')
67100282Sdougb		;;
68100282Sdougb	*)
69100282Sdougb		for i in ${gif_interfaces}; do
70100282Sdougb			eval peers=\$gifconfig_$i
71100282Sdougb			case ${peers} in
72100282Sdougb			'')
73100282Sdougb				continue
74100282Sdougb				;;
75100282Sdougb			*)
76100282Sdougb				ifconfig $i create >/dev/null 2>&1
77100282Sdougb				ifconfig $i tunnel ${peers}
78103710Sume				ifconfig $i up
79100282Sdougb				;;
80100282Sdougb			esac
81100282Sdougb		done
82100282Sdougb		;;
83100282Sdougb	esac
84100282Sdougb}
85100282Sdougb
86100280Sgordonnetwork_start()
87100280Sgordon{
88100280Sgordon	# set hostname, turn on network
89100280Sgordon	#
90100280Sgordon	echo -n "Doing initial network setup:"
91100280Sgordon
9285831Sdes	# Generate host.conf for compatibility
9385831Sdes	#
9485831Sdes	if [ -f "/etc/nsswitch.conf" ]; then
9586163Sfenner		echo -n ' host.conf'
9685831Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
9785831Sdes	fi
9885831Sdes
9965532Snectar	# Convert host.conf to nsswitch.conf if necessary
10085831Sdes	#
10185831Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
10270108Sdougb		echo ''
10370108Sdougb		echo 'Warning: /etc/host.conf is no longer used'
10485831Sdes		echo '  /etc/nsswitch.conf will be created for you'
10585831Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
10665532Snectar	fi
10765532Snectar
10851231Ssheldonh	# Set the host name if it is not already set
10951231Ssheldonh	#
11051231Ssheldonh	if [ -z "`hostname -s`" ]; then
11151231Ssheldonh		hostname ${hostname}
11251231Ssheldonh		echo -n ' hostname'
11351231Ssheldonh	fi
11425184Sjkh
11551231Ssheldonh	# Set the domainname if we're using NIS
11651231Ssheldonh	#
117100283Sdougb	case ${nisdomainname} in
118100283Sdougb	[Nn][Oo]|'')
119100283Sdougb		;;
120100283Sdougb	*)
12151231Ssheldonh		domainname ${nisdomainname}
12251231Ssheldonh		echo -n ' domain'
123100283Sdougb		;;
124100283Sdougb	esac
12540006Sphk
126100282Sdougb	echo '.'
127100282Sdougb
12883677Sbrooks	# Attempt to create cloned interfaces.
12983677Sbrooks	for ifn in ${cloned_interfaces}; do
13083677Sbrooks		ifconfig ${ifn} create
13183677Sbrooks	done
13283677Sbrooks
133100282Sdougb	# gifconfig
134100282Sdougb	network_gif_setup
135100282Sdougb
13651231Ssheldonh	# Set up all the network interfaces, calling startup scripts if needed
13751231Ssheldonh	#
13851231Ssheldonh	case ${network_interfaces} in
13951231Ssheldonh	[Aa][Uu][Tt][Oo])
14051231Ssheldonh		network_interfaces="`ifconfig -l`"
14151231Ssheldonh		;;
14283677Sbrooks	*)
14383677Sbrooks		network_interfaces="${network_interfaces} ${cloned_interfaces}"
14483677Sbrooks		;;
14551231Ssheldonh	esac
14649122Sbrian
14754458Sobrien	dhcp_interfaces=""
14851231Ssheldonh	for ifn in ${network_interfaces}; do
14951231Ssheldonh		if [ -r /etc/start_if.${ifn} ]; then
15051231Ssheldonh			. /etc/start_if.${ifn}
15154458Sobrien			eval showstat_$ifn=1
15251231Ssheldonh		fi
15349122Sbrian
15451231Ssheldonh		# Do the primary ifconfig if specified
15551231Ssheldonh		#
15651231Ssheldonh		eval ifconfig_args=\$ifconfig_${ifn}
15729300Sdanny
15851231Ssheldonh		case ${ifconfig_args} in
15951231Ssheldonh		'')
16051231Ssheldonh			;;
16151231Ssheldonh		[Dd][Hh][Cc][Pp])
16254458Sobrien			# DHCP inits are done all in one go below
16354458Sobrien			dhcp_interfaces="$dhcp_interfaces $ifn"
16454458Sobrien			eval showstat_$ifn=1
16551231Ssheldonh			;;
16651231Ssheldonh		*)
16751231Ssheldonh			ifconfig ${ifn} ${ifconfig_args}
16854458Sobrien			eval showstat_$ifn=1
16951231Ssheldonh			;;
17051231Ssheldonh		esac
17154458Sobrien	done
17251231Ssheldonh
17354458Sobrien	if [ ! -z "${dhcp_interfaces}" ]; then
174100281Sgordon		${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces}
175100281Sgordon	fi
17654458Sobrien
17754458Sobrien	for ifn in ${network_interfaces}; do
17851231Ssheldonh		# Check to see if aliases need to be added
17951231Ssheldonh		#
18051231Ssheldonh		alias=0
18151231Ssheldonh		while : ; do
18251231Ssheldonh			eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
18351231Ssheldonh			if [ -n "${ifconfig_args}" ]; then
18451231Ssheldonh				ifconfig ${ifn} ${ifconfig_args} alias
18554458Sobrien				eval showstat_$ifn=1
18686342Ssheldonh				alias=$((${alias} + 1))
18751231Ssheldonh			else
18851231Ssheldonh				break;
18951231Ssheldonh			fi
19051231Ssheldonh		done
19151231Ssheldonh
19251231Ssheldonh		# Do ipx address if specified
19351231Ssheldonh		#
19451231Ssheldonh		eval ifconfig_args=\$ifconfig_${ifn}_ipx
19551231Ssheldonh		if [ -n "${ifconfig_args}" ]; then
19651231Ssheldonh			ifconfig ${ifn} ${ifconfig_args}
19754458Sobrien			eval showstat_$ifn=1
19851231Ssheldonh		fi
19954458Sobrien	done
20051231Ssheldonh
201101594Sgordon	# Display ifconfiged interfaces
20254458Sobrien	for ifn in ${network_interfaces}; do
20354458Sobrien		eval showstat=\$showstat_${ifn}
20454458Sobrien		if [ ! -z ${showstat} ]; then
20551231Ssheldonh			ifconfig ${ifn}
20654458Sobrien		fi
20751231Ssheldonh	done
20851231Ssheldonh
209100280Sgordon	# Resync ipfilter
210100280Sgordon	/etc/rc.d/ipfilter resync
21125184Sjkh}
21225184Sjkh
213100280Sgordonnetwork_stop()
214100280Sgordon{
215100282Sdougb	echo -n "Stopping network:"
21625184Sjkh
217100280Sgordon	# flush routes
218100280Sgordon	#
219100282Sdougb	echo -n " flush routes"
220100280Sgordon	route -n flush
22153314Sache
222100282Sdougb	echo '.'
22353314Sache}
22465532Snectar
225100280Sgordonload_rc_config $name
226100280Sgordonrun_rc_command "$1"
227