network.subr revision 101594
1#!/bin/sh -x
2#
3# $FreeBSD: head/etc/network.subr 101594 2002-08-09 17:33:07Z gordon $
4#
5
6# PROVIDE: network1
7# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
8# KEYWORD: FreeBSD
9
10. /etc/rc.subr
11
12name="network1"
13start_cmd="network_start"
14stop_cmd="network_stop"
15
16convert_host_conf()
17{
18    host_conf=$1; shift;
19    nsswitch_conf=$1; shift;
20    awk '                                                                   \
21        /^[:blank:]*#/       { next }                                       \
22        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
23        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
24        /nis/                { nsswitch[c] = "nis";   c++; next }           \
25        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
26        END {                                                               \
27                printf "hosts: ";                                           \
28                for (i in nsswitch) printf "%s ", nsswitch[i];              \
29                printf "\n";                                                \
30        }' < $host_conf > $nsswitch_conf
31}
32
33generate_host_conf()
34{
35    nsswitch_conf=$1; shift;
36    host_conf=$1; shift;
37
38    awk '
39BEGIN {
40    xlat["files"] = "hosts";
41    xlat["dns"] = "bind";
42    xlat["nis"] = "nis";
43    cont = 0;
44}
45sub(/^[\t ]*hosts:/, "") || cont {
46    if (!cont)
47        srcs = ""
48    sub(/#.*/, "")
49    gsub(/[][]/, " & ")
50    cont = sub(/\\$/, "")
51    srcs = srcs " " $0
52}
53END {
54    print "# Auto-generated from nsswitch.conf, do not edit"
55    ns = split(srcs, s)
56    for (n = 1; n <= ns; ++n) {
57        if (s[n] in xlat)
58            print xlat[s[n]]
59    }
60}
61' <$nsswitch_conf >$host_conf
62}
63
64network_gif_setup() {
65	case ${gif_interfaces} in
66	[Nn][Oo] | '')
67		;;
68	*)
69		for i in ${gif_interfaces}; do
70			eval peers=\$gifconfig_$i
71			case ${peers} in
72			'')
73				continue
74				;;
75			*)
76				ifconfig $i create >/dev/null 2>&1
77				ifconfig $i tunnel ${peers}
78				;;
79			esac
80		done
81		;;
82	esac
83}
84
85network_start()
86{
87	# set hostname, turn on network
88	#
89	echo -n "Doing initial network setup:"
90
91	# Generate host.conf for compatibility
92	#
93	if [ -f "/etc/nsswitch.conf" ]; then
94		echo -n ' host.conf'
95		generate_host_conf /etc/nsswitch.conf /etc/host.conf
96	fi
97
98	# Convert host.conf to nsswitch.conf if necessary
99	#
100	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
101		echo ''
102		echo 'Warning: /etc/host.conf is no longer used'
103		echo '  /etc/nsswitch.conf will be created for you'
104		convert_host_conf /etc/host.conf /etc/nsswitch.conf
105	fi
106
107	# Set the host name if it is not already set
108	#
109	if [ -z "`hostname -s`" ]; then
110		hostname ${hostname}
111		echo -n ' hostname'
112	fi
113
114	# Set the domainname if we're using NIS
115	#
116	case ${nisdomainname} in
117	[Nn][Oo]|'')
118		;;
119	*)
120		domainname ${nisdomainname}
121		echo -n ' domain'
122		;;
123	esac
124
125	echo '.'
126
127	# Attempt to create cloned interfaces.
128	for ifn in ${cloned_interfaces}; do
129		ifconfig ${ifn} create
130	done
131
132	# gifconfig
133	network_gif_setup
134
135	# Set up all the network interfaces, calling startup scripts if needed
136	#
137	case ${network_interfaces} in
138	[Aa][Uu][Tt][Oo])
139		network_interfaces="`ifconfig -l`"
140		;;
141	*)
142		network_interfaces="${network_interfaces} ${cloned_interfaces}"
143		;;
144	esac
145
146	dhcp_interfaces=""
147	for ifn in ${network_interfaces}; do
148		if [ -r /etc/start_if.${ifn} ]; then
149			. /etc/start_if.${ifn}
150			eval showstat_$ifn=1
151		fi
152
153		# Do the primary ifconfig if specified
154		#
155		eval ifconfig_args=\$ifconfig_${ifn}
156
157		case ${ifconfig_args} in
158		'')
159			;;
160		[Dd][Hh][Cc][Pp])
161			# DHCP inits are done all in one go below
162			dhcp_interfaces="$dhcp_interfaces $ifn"
163			eval showstat_$ifn=1
164			;;
165		*)
166			ifconfig ${ifn} ${ifconfig_args}
167			eval showstat_$ifn=1
168			;;
169		esac
170	done
171
172	if [ ! -z "${dhcp_interfaces}" ]; then
173		${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces}
174	fi
175
176	for ifn in ${network_interfaces}; do
177		# Check to see if aliases need to be added
178		#
179		alias=0
180		while : ; do
181			eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
182			if [ -n "${ifconfig_args}" ]; then
183				ifconfig ${ifn} ${ifconfig_args} alias
184				eval showstat_$ifn=1
185				alias=$((${alias} + 1))
186			else
187				break;
188			fi
189		done
190
191		# Do ipx address if specified
192		#
193		eval ifconfig_args=\$ifconfig_${ifn}_ipx
194		if [ -n "${ifconfig_args}" ]; then
195			ifconfig ${ifn} ${ifconfig_args}
196			eval showstat_$ifn=1
197		fi
198	done
199
200	# Display ifconfiged interfaces
201	for ifn in ${network_interfaces}; do
202		eval showstat=\$showstat_${ifn}
203		if [ ! -z ${showstat} ]; then
204			ifconfig ${ifn}
205		fi
206	done
207
208	# Resync ipfilter
209	/etc/rc.d/ipfilter resync
210}
211
212network_stop()
213{
214	echo -n "Stopping network:"
215
216	# flush routes
217	#
218	echo -n " flush routes"
219	route -n flush
220
221	echo '.'
222}
223
224load_rc_config $name
225run_rc_command "$1"
226