summaryrefslogtreecommitdiffstats
path: root/etc/network.subr
blob: b2c3ddb3da43caf59d1a8fc9d5c94300886bf633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/sh -x
#
# $FreeBSD$
#

# PROVIDE: network1
# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
# KEYWORD: FreeBSD

. /etc/rc.subr

name="network1"
start_cmd="network_start"
stop_cmd="network_stop"

convert_host_conf()
{
    host_conf=$1; shift;
    nsswitch_conf=$1; shift;
    awk '                                                                   \
        /^[:blank:]*#/       { next }                                       \
        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
        /nis/                { nsswitch[c] = "nis";   c++; next }           \
        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
        END {                                                               \
                printf "hosts: ";                                           \
                for (i in nsswitch) printf "%s ", nsswitch[i];              \
                printf "\n";                                                \
        }' < $host_conf > $nsswitch_conf
}

generate_host_conf()
{
    nsswitch_conf=$1; shift;
    host_conf=$1; shift;

    awk '
BEGIN {
    xlat["files"] = "hosts";
    xlat["dns"] = "bind";
    xlat["nis"] = "nis";
    cont = 0;
}
sub(/^[\t ]*hosts:/, "") || cont {
    if (!cont)
        srcs = ""
    sub(/#.*/, "")
    gsub(/[][]/, " & ")
    cont = sub(/\\$/, "")
    srcs = srcs " " $0
}
END {
    print "# Auto-generated from nsswitch.conf, do not edit"
    ns = split(srcs, s)
    for (n = 1; n <= ns; ++n) {
        if (s[n] in xlat)
            print xlat[s[n]]
    }
}
' <$nsswitch_conf >$host_conf
}

network_start()
{
	# set hostname, turn on network
	#
	echo -n "Doing initial network setup:"

	# Generate host.conf for compatibility
	#
	if [ -f "/etc/nsswitch.conf" ]; then
		echo -n ' host.conf'
		generate_host_conf /etc/nsswitch.conf /etc/host.conf
	fi

	# Convert host.conf to nsswitch.conf if necessary
	#
	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
		echo ''
		echo 'Warning: /etc/host.conf is no longer used'
		echo '  /etc/nsswitch.conf will be created for you'
		convert_host_conf /etc/host.conf /etc/nsswitch.conf
	fi

	# Set the host name if it is not already set
	#
	if [ -z "`hostname -s`" ]; then
		hostname ${hostname}
		echo -n ' hostname'
	fi

	# Set the domainname if we're using NIS
	#
	if checkyesno nisdomainname ; then
		domainname ${nisdomainname}
		echo -n ' domain'
	fi

	# Attempt to create cloned interfaces.
	for ifn in ${cloned_interfaces}; do
		ifconfig ${ifn} create
	done

	# Set up all the network interfaces, calling startup scripts if needed
	#
	case ${network_interfaces} in
	[Aa][Uu][Tt][Oo])
		network_interfaces="`ifconfig -l`"
		;;
	*)
		network_interfaces="${network_interfaces} ${cloned_interfaces}"
		;;
	esac

	dhcp_interfaces=""
	for ifn in ${network_interfaces}; do
		if [ -r /etc/start_if.${ifn} ]; then
			. /etc/start_if.${ifn}
			eval showstat_$ifn=1
		fi

		# Do the primary ifconfig if specified
		#
		eval ifconfig_args=\$ifconfig_${ifn}

		case ${ifconfig_args} in
		'')
			;;
		[Dd][Hh][Cc][Pp])
			# DHCP inits are done all in one go below
			dhcp_interfaces="$dhcp_interfaces $ifn"
			eval showstat_$ifn=1
			;;
		*)
			ifconfig ${ifn} ${ifconfig_args}
			eval showstat_$ifn=1
			;;
		esac
	done

	if [ ! -z "${dhcp_interfaces}" ]; then
		${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces}	fi

	for ifn in ${network_interfaces}; do
		# Check to see if aliases need to be added
		#
		alias=0
		while : ; do
			eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
			if [ -n "${ifconfig_args}" ]; then
				ifconfig ${ifn} ${ifconfig_args} alias
				eval showstat_$ifn=1
				alias=$((${alias} + 1))
			else
				break;
			fi
		done

		# Do ipx address if specified
		#
		eval ifconfig_args=\$ifconfig_${ifn}_ipx
		if [ -n "${ifconfig_args}" ]; then
			ifconfig ${ifn} ${ifconfig_args}
			eval showstat_$ifn=1
		fi
	done

	# catch-all for interfaces that haven't been 'ifconfig'ed so far
	for ifn in ${network_interfaces}; do
		eval showstat=\$showstat_${ifn}
		if [ ! -z ${showstat} ]; then
			ifconfig ${ifn}
		fi
	done

	

	# Check $defaultroute, then /etc/mygate, for the name of my gateway
	# host. That name must be in /etc/hosts.
	#
	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
		defaultroute=`cat /etc/mygate`
	fi
	if [ -n "$defaultroute" ]; then
		route add default $defaultroute
	fi

	# Check if each configured interface xxN has an $ifaliases_xxN variable
	# associated, then configure additional IP addresses for that interface.
	# The variable contains a list of "address netmask" pairs, with
	# "netmask" set to "-" if the interface default netmask is to be used.
	#
	# Note that $ifaliases_xxN works only with certain configurations and
	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
	# 
	#
	if [ -n "$configured_interfaces" ]; then
		echo "Adding interface aliases:"
		done_aliases_message=yes
	fi
	for int in $configured_interfaces; do
		eval args=\$ifaliases_$int
		if [ -n "$args" ]; then
			set -- $args
			while [ $# -ge 2 ]; do
				addr=$1 ; net=$2 ; shift 2
				if [ "$net" = "-" ]; then
					# for compatibility only, obsolete
					ifconfig $int inet alias $addr
				else
					ifconfig $int inet alias $addr \
					    netmask $net
				fi
				# Use loopback, not the wire
				route add $addr 127.0.0.1
			done
		fi
	done

	# /etc/ifaliases, if it exists, contains the names of additional IP
	# addresses for each interface. It is formatted as a series of lines
	# that contain
	#	address interface netmask
	#
	# Note that /etc/ifaliases works only with certain cases only and its
	# use is not recommended.  Use /etc/ifconfig.xxN instead.
	#
	#
	if [ -f /etc/ifaliases ]; then
		if [ "$done_aliases_message" != yes ]; then
			echo "Adding interface aliases:"
		fi
		while read addr int net; do
			if [ -z "$net" ]; then
				# for compatibility only, obsolete
				ifconfig $int inet alias $addr
			else
				ifconfig $int inet alias $addr netmask $net
			fi
			# use loopback, not the wire
			route add $addr 127.0.0.1
		done < /etc/ifaliases
	fi

	# IPv6 interface autoconfiguration.
	#
	if ifconfig lo0 inet6 >/dev/null 2>&1; then
		# wait till DAD is completed. always invoke it in case
		# if are configured manually by ifconfig
		#
		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
		sleep $dadcount
		sleep 1

		if checkyesno rtsol; then
			if [ "$ip6mode" = "autohost" ]; then
				echo 'Sending router solicitation...'
				rtsol $rtsol_flags
			else
				echo
				warn \
			    "ip6mode must be set to 'autohost' to use rtsol."
			fi

			# wait till DAD is completed, for global addresses
			# configured by router advert message.
			#
			sleep $dadcount
			sleep 1
		fi
	fi

	# XXX this must die
	if [ -s /etc/netstart.local ]; then
		sh /etc/netstart.local start
	fi
	fi
	echo '.'

	# Resync ipfilter
	/etc/rc.d/ipfilter resync
}

network_stop()
{
	echo "Stopping network."

	# XXX this must die
	if [ -s /etc/netstart.local ]; then
		sh /etc/netstart.local stop
	fi

	echo "Deleting aliases."
	if [ -f /etc/ifaliases ]; then
		while read addr int net; do
			ifconfig $int inet delete $addr
		done < /etc/ifaliases
	fi

	for int in `ifconfig -lu`; do
		eval args=\$ifaliases_$int
		if [ -n "$args" ]; then
			set -- $args
			while [ $# -ge 2 ]; do
				addr=$1 ; net=$2 ; shift 2
				ifconfig $int inet delete $addr
			done
		fi
	done

	# down interfaces
	#
	echo -n 'Downing network interfaces:'
	if [ "$net_interfaces" != NO ]; then
		if checkyesno auto_ifconfig; then
			tmp=`ifconfig -l`
		else
			tmp="$net_interfaces"
		fi
		for int in $tmp; do
			eval args=\$ifconfig_$int
			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
				echo -n " $int"
				ifconfig $int down
			fi
		done
		echo "."
	fi

	# flush routes
	#
	route -n flush

}

load_rc_config $name
run_rc_command "$1"
OpenPOWER on IntegriCloud