network.subr revision 230453
125184Sjkh#
2113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3113674Smtm#
4113674Smtm# Redistribution and use in source and binary forms, with or without
5113674Smtm# modification, are permitted provided that the following conditions
6113674Smtm# are met:
7113674Smtm# 1. Redistributions of source code must retain the above copyright
8113674Smtm#    notice, this list of conditions and the following disclaimer.
9113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
10113674Smtm#    notice, this list of conditions and the following disclaimer in the
11113674Smtm#    documentation and/or other materials provided with the distribution.
12113674Smtm#
13113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14113674Smtm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15113674Smtm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16113674Smtm# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17113674Smtm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18113674Smtm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19113674Smtm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20113674Smtm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21113674Smtm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22113674Smtm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23113674Smtm# SUCH DAMAGE.
24113674Smtm#
2550472Speter# $FreeBSD: head/etc/network.subr 230453 2012-01-22 10:57:32Z hrs $
2666830Sobrien#
2725184Sjkh
28113674Smtm#
29113674Smtm# Subroutines commonly used from network startup scripts.
30113674Smtm# Requires that rc.conf be loaded first.
31113674Smtm#
3225184Sjkh
33178356Ssam# ifn_start ifn
34197147Shrs#	Bring up and configure an interface.  If some configuration is
35225560Sbrueffer#	applied, print the interface configuration.
36178356Ssam#
37178356Ssamifn_start()
38178356Ssam{
39178356Ssam	local ifn cfg
40178356Ssam	ifn="$1"
41178356Ssam	cfg=1
42178356Ssam
43178356Ssam	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
44178356Ssam
45178356Ssam	ifscript_up ${ifn} && cfg=0
46178356Ssam	ifconfig_up ${ifn} && cfg=0
47222515Sbz	afexists inet && ipv4_up ${ifn} && cfg=0
48222515Sbz	afexists inet6 && ipv6_up ${ifn} && cfg=0
49222515Sbz	afexists ipx && ipx_up ${ifn} && cfg=0
50197139Shrs	childif_create ${ifn} && cfg=0
51178356Ssam
52178356Ssam	return $cfg
53178356Ssam}
54178356Ssam
55197139Shrs# ifn_stop ifn
56225560Sbrueffer#	Shutdown and de-configure an interface.  If action is taken,
57197147Shrs#	print the interface name.
58178356Ssam#
59178356Ssamifn_stop()
60178356Ssam{
61178356Ssam	local ifn cfg
62178356Ssam	ifn="$1"
63178356Ssam	cfg=1
64178356Ssam
65197139Shrs	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
66178356Ssam
67222515Sbz	afexists ipx && ipx_down ${ifn} && cfg=0
68222515Sbz	afexists inet6 && ipv6_down ${ifn} && cfg=0
69222515Sbz	afexists inet && ipv4_down ${ifn} && cfg=0
70178356Ssam	ifconfig_down ${ifn} && cfg=0
71178356Ssam	ifscript_down ${ifn} && cfg=0
72197139Shrs	childif_destroy ${ifn} && cfg=0
73178356Ssam
74178356Ssam	return $cfg
75178356Ssam}
76178356Ssam
77113674Smtm# ifconfig_up if
78113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
79113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
80113674Smtm#	arguments were found and executed or 1 if the interface
81147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
82147088Sbrooks#	here.
83113674Smtm#
84113674Smtmifconfig_up()
85113674Smtm{
86197139Shrs	local _cfg _ipv6_opts ifconfig_args
87147088Sbrooks	_cfg=1
88147088Sbrooks
89222515Sbz	# Make sure lo0 always comes up.
90222515Sbz	if [ "$1" = "lo0" ]; then
91222515Sbz		_cfg=0
92222515Sbz	fi
93222515Sbz
94197139Shrs	# ifconfig_IF
95147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
96113674Smtm	if [ -n "${ifconfig_args}" ]; then
97223506Spluknet		eval ifconfig $1 ${ifconfig_args}
98147088Sbrooks		_cfg=0
99113674Smtm	fi
100147088Sbrooks
101197139Shrs	# inet6 specific
102197139Shrs	if afexists inet6; then
103222733Shrs		if checkyesno ipv6_activate_all_interfaces; then
104222733Shrs			_ipv6_opts="-ifdisabled"
105222746Shrs		elif [ "$1" != "lo0" ]; then
106222733Shrs			_ipv6_opts="ifdisabled"
107212574Shrs		fi
108197139Shrs
109222733Shrs		# backward compatibility: $ipv6_enable
110222733Shrs		case $ipv6_enable in
111222733Shrs		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
112230453Shrs			if ! checkyesno ipv6_gateway_enable; then
113230453Shrs				_ipv6_opts="${_ipv6_opts} accept_rtadv"
114230453Shrs			fi
115230453Shrs		;;
116222733Shrs		esac
117222733Shrs
118225521Shrs		case $ipv6_cpe_wanif in
119225521Shrs		$1)
120225521Shrs			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
121225521Shrs		;;
122225521Shrs		esac
123225521Shrs
124212574Shrs		if [ -n "${_ipv6_opts}" ]; then
125212574Shrs			ifconfig $1 inet6 ${_ipv6_opts}
126197526Shrs		fi
127212574Shrs
128212574Shrs		# ifconfig_IF_ipv6
129212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
130212574Shrs		if [ -n "${ifconfig_args}" ]; then
131225522Shrs			# backward compatibility: inet6 keyword
132225522Shrs			case "${ifconfig_args}" in
133225522Shrs			:*|[0-9a-fA-F]*:*)
134225522Shrs				warn "\$ifconfig_$1_ipv6 needs " \
135225522Shrs				    "\"inet6\" keyword for an IPv6 address."
136225522Shrs				ifconfig_args="inet6 ${ifconfig_args}"
137225522Shrs			;;
138225522Shrs			esac
139212574Shrs			ifconfig $1 inet6 -ifdisabled
140225522Shrs			eval ifconfig $1 ${ifconfig_args}
141212574Shrs			_cfg=0
142212574Shrs		fi
143212574Shrs
144230453Shrs		# $ipv6_prefix_IF will be handled in
145230453Shrs		# ipv6_prefix_hostid_addr_common().
146230453Shrs		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
147230453Shrs		if [ -n "${ifconfig_args}" ]; then
148230453Shrs			ifconfig $1 inet6 -ifdisabled
149230453Shrs			_cfg=0
150230453Shrs		fi
151230453Shrs
152229783Suqs		# backward compatibility: $ipv6_ifconfig_IF
153212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
154212574Shrs		if [ -n "${ifconfig_args}" ]; then
155212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
156212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
157212574Shrs			ifconfig $1 inet6 -ifdisabled
158225522Shrs			eval ifconfig $1 inet6 ${ifconfig_args}
159212574Shrs			_cfg=0
160212574Shrs		fi
161197139Shrs	fi
162197139Shrs
163197139Shrs	if [ ${_cfg} -eq 0 ]; then
164197139Shrs		ifconfig $1 up
165197139Shrs	fi
166197139Shrs
167147088Sbrooks	if wpaif $1; then
168147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
169147088Sbrooks		_cfg=0		# XXX: not sure this should count
170147088Sbrooks	fi
171147088Sbrooks
172147088Sbrooks	if dhcpif $1; then
173149726Sbrooks		if [ $_cfg -ne 0 ] ; then
174149726Sbrooks			ifconfig $1 up
175149726Sbrooks		fi
176157706Sbrooks		if syncdhcpif $1; then
177157706Sbrooks			/etc/rc.d/dhclient start $1
178157706Sbrooks		fi
179147088Sbrooks		_cfg=0
180147088Sbrooks	fi
181147088Sbrooks
182147121Sbrooks	return $_cfg
183113674Smtm}
18425184Sjkh
185116029Smtm# ifconfig_down if
186161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
187161386Sbrooks#	the interface exists.
188116029Smtm#
189116029Smtmifconfig_down()
190116029Smtm{
191197139Shrs	local _cfg
192147121Sbrooks	_cfg=1
193116029Smtm
194147088Sbrooks	if wpaif $1; then
195147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
196147121Sbrooks		_cfg=0
197147088Sbrooks	fi
198147088Sbrooks
199147088Sbrooks	if dhcpif $1; then
200147088Sbrooks		/etc/rc.d/dhclient stop $1
201147088Sbrooks		_cfg=0
202147088Sbrooks	fi
203147088Sbrooks
204161386Sbrooks	if ifexists $1; then
205161386Sbrooks		ifconfig $1 down
206161386Sbrooks		_cfg=0
207161386Sbrooks	fi
208157706Sbrooks
209147121Sbrooks	return $_cfg
210116029Smtm}
211116029Smtm
212157706Sbrooks# get_if_var if var [default]
213197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
214197147Shrs#	$var is a string containg the sub-string "IF" which will be
215197147Shrs#	replaced with $if after the characters defined in _punct are
216197147Shrs#	replaced with '_'. If the variable is unset, replace it with
217197147Shrs#	$default if given.
218157706Sbrooksget_if_var()
219157706Sbrooks{
220212578Shrs	local _if _punct _punct_c _var _default prefix suffix
221197139Shrs
222157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
223157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
224157706Sbrooks	fi
225157706Sbrooks
226157706Sbrooks	_if=$1
227157706Sbrooks	_punct=". - / +"
228157736Sbrooks	for _punct_c in $_punct; do
229157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
230157706Sbrooks	done
231157706Sbrooks	_var=$2
232157706Sbrooks	_default=$3
233157706Sbrooks
234157706Sbrooks	prefix=${_var%%IF*}
235157706Sbrooks	suffix=${_var##*IF}
236168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
237157706Sbrooks}
238157706Sbrooks
239197139Shrs# _ifconfig_getargs if [af]
240225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
241225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
242147088Sbrooks#	outside this file.
243147088Sbrooks_ifconfig_getargs()
244147088Sbrooks{
245212574Shrs	local _ifn _af
246147088Sbrooks	_ifn=$1
247197139Shrs	_af=${2+_$2}
248197139Shrs
249147088Sbrooks	if [ -z "$_ifn" ]; then
250147088Sbrooks		return 1
251147088Sbrooks	fi
252147088Sbrooks
253212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
254147088Sbrooks}
255147088Sbrooks
256197139Shrs# ifconfig_getargs if [af]
257147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
258147088Sbrooks#	args such as DHCP and WPA.
259147088Sbrooksifconfig_getargs()
260147088Sbrooks{
261197139Shrs	local _tmpargs _arg _args
262197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
263147088Sbrooks	if [ $? -eq 1 ]; then
264147088Sbrooks		return 1
265147088Sbrooks	fi
266147088Sbrooks	_args=
267147088Sbrooks
268147088Sbrooks	for _arg in $_tmpargs; do
269147088Sbrooks		case $_arg in
270157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
271157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
272157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
273157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
274157706Sbrooks		[Ww][Pp][Aa]) ;;
275147088Sbrooks		*)
276147088Sbrooks			_args="$_args $_arg"
277147088Sbrooks			;;
278147088Sbrooks		esac
279147088Sbrooks	done
280147088Sbrooks
281147088Sbrooks	echo $_args
282147088Sbrooks}
283147088Sbrooks
284149401Sbrooks# autoif
285225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
286149401Sbrooks#	boot time and 1 otherwise.
287149401Sbrooksautoif()
288149401Sbrooks{
289197139Shrs	local _tmpargs _arg
290149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
291197139Shrs
292149401Sbrooks	for _arg in $_tmpargs; do
293149401Sbrooks		case $_arg in
294149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
295149401Sbrooks			return 1
296149401Sbrooks			;;
297149401Sbrooks		esac
298149401Sbrooks	done
299197139Shrs
300149401Sbrooks	return 0
301149401Sbrooks}
302149401Sbrooks
303147088Sbrooks# dhcpif if
304147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
305147088Sbrooksdhcpif()
306147088Sbrooks{
307197139Shrs	local _tmpargs _arg
308147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
309197139Shrs
310225849Sdelphij	if noafif $1; then
311225849Sdelphij		return 1
312225849Sdelphij	fi
313225849Sdelphij
314147088Sbrooks	for _arg in $_tmpargs; do
315147088Sbrooks		case $_arg in
316147088Sbrooks		[Dd][Hh][Cc][Pp])
317147088Sbrooks			return 0
318147088Sbrooks			;;
319157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
320157706Sbrooks			return 0
321157706Sbrooks			;;
322157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
323157706Sbrooks			return 0
324157706Sbrooks			;;
325147088Sbrooks		esac
326147088Sbrooks	done
327197139Shrs
328147088Sbrooks	return 1
329147088Sbrooks}
330147088Sbrooks
331157706Sbrooks# syncdhcpif
332157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
333157706Sbrooks#	1 otherwise.
334157706Sbrookssyncdhcpif()
335157706Sbrooks{
336197139Shrs	local _tmpargs _arg
337157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
338197139Shrs
339225849Sdelphij	if noafif $1; then
340225849Sdelphij		return 1
341225849Sdelphij	fi
342225849Sdelphij
343157706Sbrooks	for _arg in $_tmpargs; do
344157706Sbrooks		case $_arg in
345157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
346157706Sbrooks			return 1
347157706Sbrooks			;;
348157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
349157706Sbrooks			return 0
350157706Sbrooks			;;
351157706Sbrooks		esac
352157706Sbrooks	done
353197139Shrs
354197139Shrs	checkyesno synchronous_dhclient
355157706Sbrooks}
356157706Sbrooks
357147088Sbrooks# wpaif if
358147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
359147088Sbrookswpaif()
360147088Sbrooks{
361197139Shrs	local _tmpargs _arg
362147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
363197139Shrs
364147088Sbrooks	for _arg in $_tmpargs; do
365147088Sbrooks		case $_arg in
366147088Sbrooks		[Ww][Pp][Aa])
367147088Sbrooks			return 0
368147088Sbrooks			;;
369147088Sbrooks		esac
370147088Sbrooks	done
371197139Shrs
372147088Sbrooks	return 1
373147088Sbrooks}
374147088Sbrooks
375197139Shrs# afexists af
376197139Shrs#	Returns 0 if the address family is enabled in the kernel
377197139Shrs#	1 otherwise.
378197139Shrsafexists()
379197139Shrs{
380197139Shrs	local _af
381197139Shrs	_af=$1
382197139Shrs
383197139Shrs	case ${_af} in
384222996Shrs	inet|inet6)
385222996Shrs		check_kern_features ${_af}
386197139Shrs		;;
387197697Shrs	ipx)
388197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
389197697Shrs		;;
390197697Shrs	atm)
391197697Shrs		if [ -x /sbin/atmconfig ]; then
392197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
393197697Shrs		else
394197697Shrs			return 1
395197697Shrs		fi
396197697Shrs		;;
397197139Shrs	*)
398197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
399197139Shrs		;;
400197139Shrs	esac
401197139Shrs}
402197139Shrs
403212574Shrs# noafif if
404212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
405212574Shrsnoafif()
406212574Shrs{
407212574Shrs	local _if
408212574Shrs	_if=$1
409212574Shrs
410212574Shrs	case $_if in
411212574Shrs	pflog[0-9]*|\
412212574Shrs	pfsync[0-9]*|\
413212574Shrs	an[0-9]*|\
414212574Shrs	ath[0-9]*|\
415212574Shrs	ipw[0-9]*|\
416212577Shrs	ipfw[0-9]*|\
417212574Shrs	iwi[0-9]*|\
418212574Shrs	iwn[0-9]*|\
419212574Shrs	ral[0-9]*|\
420212574Shrs	wi[0-9]*|\
421212574Shrs	wl[0-9]*|\
422212574Shrs	wpi[0-9]*)
423212574Shrs		return 0
424212574Shrs		;;
425212574Shrs	esac
426212574Shrs
427212574Shrs	return 1
428212574Shrs}
429212574Shrs
430162490Sbrooks# ipv6if if
431162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
432162490Sbrooks#	1 otherwise.
433162490Sbrooksipv6if()
434162490Sbrooks{
435212574Shrs	local _if _tmpargs i
436212574Shrs	_if=$1
437212574Shrs
438197139Shrs	if ! afexists inet6; then
439162490Sbrooks		return 1
440162490Sbrooks	fi
441197139Shrs
442197139Shrs	# lo0 is always IPv6-enabled
443212574Shrs	case $_if in
444197139Shrs	lo0)
445197139Shrs		return 0
446197139Shrs		;;
447197139Shrs	esac
448197139Shrs
449212574Shrs	case "${ipv6_network_interfaces}" in
450212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
451212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
452212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
453212575Shrs		if [ -n "${_tmpargs}" ]; then
454212575Shrs			return 0
455212575Shrs		fi
456197139Shrs
457230453Shrs		# True if $ipv6_prefix_IF is defined.
458230453Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
459230453Shrs		if [ -n "${_tmpargs}" ]; then
460230453Shrs			return 0
461230453Shrs		fi
462230453Shrs
463212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
464212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
465212575Shrs		if [ -n "${_tmpargs}" ]; then
466212574Shrs			return 0
467212574Shrs		fi
468212575Shrs		;;
469212575Shrs	esac
470197139Shrs
471162490Sbrooks	return 1
472162490Sbrooks}
473162490Sbrooks
474197139Shrs# ipv6_autoconfif if
475197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
476225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
477197139Shrsipv6_autoconfif()
478197139Shrs{
479197139Shrs	local _if _tmpargs _arg
480197139Shrs	_if=$1
481197139Shrs
482212577Shrs	case $_if in
483212577Shrs	lo0|\
484212577Shrs	stf[0-9]*|\
485212577Shrs	faith[0-9]*|\
486212577Shrs	lp[0-9]*|\
487212577Shrs	sl[0-9]*)
488197139Shrs		return 1
489212577Shrs		;;
490212577Shrs	esac
491212574Shrs	if noafif $_if; then
492212574Shrs		return 1
493212574Shrs	fi
494212577Shrs	if ! ipv6if $_if; then
495212577Shrs		return 1
496212577Shrs	fi
497197139Shrs	if checkyesno ipv6_gateway_enable; then
498197139Shrs		return 1
499197139Shrs	fi
500197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
501197526Shrs	if [ -n "${_tmpargs}" ]; then
502197526Shrs		return 1
503197526Shrs	fi
504212574Shrs	# backward compatibility: $ipv6_enable
505212574Shrs	case $ipv6_enable in
506212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
507197526Shrs		return 0
508212577Shrs	;;
509197526Shrs	esac
510197526Shrs
511212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
512212574Shrs	for _arg in $_tmpargs; do
513212574Shrs		case $_arg in
514212574Shrs		accept_rtadv)
515212574Shrs			return 0
516212574Shrs			;;
517212574Shrs		esac
518212574Shrs	done
519212574Shrs
520212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
521212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
522212574Shrs	for _arg in $_tmpargs; do
523212574Shrs		case $_arg in
524212574Shrs		accept_rtadv)
525212574Shrs			return 0
526212574Shrs			;;
527212574Shrs		esac
528212574Shrs	done
529212574Shrs
530197139Shrs	return 1
531197139Shrs}
532197139Shrs
533161386Sbrooks# ifexists if
534161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
535161386Sbrooksifexists()
536161386Sbrooks{
537197139Shrs	[ -z "$1" ] && return 1
538169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
539161386Sbrooks}
540161386Sbrooks
541152441Sbrooks# ipv4_up if
542212578Shrs#	add IPv4 addresses to the interface $if
543152441Sbrooksipv4_up()
544152441Sbrooks{
545197139Shrs	local _if _ret
546152441Sbrooks	_if=$1
547197139Shrs	_ret=1
548197139Shrs
549222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
550222515Sbz	if [ "${_if}" = "lo0" ]; then
551226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
552222515Sbz		if [ -z "${ifconfig_args}" ]; then
553222515Sbz			ifconfig ${_if} inet 127.0.0.1/8 alias
554222515Sbz		fi
555222515Sbz	fi
556197139Shrs	ifalias_up ${_if} inet && _ret=0
557197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
558197139Shrs
559197139Shrs	return $_ret
560152441Sbrooks}
561152441Sbrooks
562197139Shrs# ipv6_up if
563197139Shrs#	add IPv6 addresses to the interface $if
564197139Shrsipv6_up()
565197139Shrs{
566197139Shrs	local _if _ret
567197139Shrs	_if=$1
568197139Shrs	_ret=1
569197139Shrs
570197139Shrs	if ! ipv6if $_if; then
571197139Shrs		return 0
572197139Shrs	fi
573197139Shrs
574197139Shrs	ifalias_up ${_if} inet6 && _ret=0
575226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
576197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
577197139Shrs
578197139Shrs	return $_ret
579197139Shrs}
580197139Shrs
581152441Sbrooks# ipv4_down if
582197147Shrs#	remove IPv4 addresses from the interface $if
583152441Sbrooksipv4_down()
584152441Sbrooks{
585197139Shrs	local _if _ifs _ret inetList oldifs _inet
586152441Sbrooks	_if=$1
587161386Sbrooks	_ifs="^"
588161386Sbrooks	_ret=1
589161386Sbrooks
590161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
591161386Sbrooks
592161386Sbrooks	oldifs="$IFS"
593161386Sbrooks	IFS="$_ifs"
594161386Sbrooks	for _inet in $inetList ; do
595161386Sbrooks		# get rid of extraneous line
596161386Sbrooks		[ -z "$_inet" ] && break
597161386Sbrooks
598161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
599161386Sbrooks
600161386Sbrooks		IFS="$oldifs"
601161386Sbrooks		ifconfig ${_if} ${_inet} delete
602161386Sbrooks		IFS="$_ifs"
603161386Sbrooks		_ret=0
604161386Sbrooks	done
605161386Sbrooks	IFS="$oldifs"
606161386Sbrooks
607197139Shrs	ifalias_down ${_if} inet && _ret=0
608161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
609161386Sbrooks
610161386Sbrooks	return $_ret
611152441Sbrooks}
612152441Sbrooks
613197139Shrs# ipv6_down if
614197139Shrs#	remove IPv6 addresses from the interface $if
615197139Shrsipv6_down()
616197139Shrs{
617197139Shrs	local _if _ifs _ret inetList oldifs _inet6
618197139Shrs	_if=$1
619197139Shrs	_ifs="^"
620197139Shrs	_ret=1
621197139Shrs
622197139Shrs	if ! ipv6if $_if; then
623197139Shrs		return 0
624197139Shrs	fi
625197139Shrs
626197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
627226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
628197139Shrs	ifalias_down ${_if} inet6 && _ret=0
629197139Shrs
630197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
631197139Shrs
632197139Shrs	oldifs="$IFS"
633197139Shrs	IFS="$_ifs"
634197139Shrs	for _inet6 in $inetList ; do
635197139Shrs		# get rid of extraneous line
636197139Shrs		[ -z "$_inet6" ] && break
637197139Shrs
638197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
639197139Shrs
640197139Shrs		IFS="$oldifs"
641197139Shrs		ifconfig ${_if} ${_inet6} -alias
642197139Shrs		IFS="$_ifs"
643197139Shrs		_ret=0
644197139Shrs	done
645197139Shrs	IFS="$oldifs"
646197139Shrs
647197139Shrs	return $_ret
648197139Shrs}
649197139Shrs
650152441Sbrooks# ipv4_addrs_common if action
651197147Shrs#	Evaluate the ifconfig_if_ipv4 arguments for interface $if and
652197147Shrs#	use $action to add or remove IPv4 addresses from $if.
653152441Sbrooksipv4_addrs_common()
654197147Shrs{
655197139Shrs	local _ret _if _action _cidr _cidr_addr
656212578Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount
657152441Sbrooks	_ret=1
658152441Sbrooks	_if=$1
659152441Sbrooks	_action=$2
660212578Shrs
661152441Sbrooks	# get ipv4-addresses
662157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
663212578Shrs
664152441Sbrooks	for _cidr in ${cidr_addr}; do
665152441Sbrooks		_ipaddr=${_cidr%%/*}
666152441Sbrooks		_netmask="/"${_cidr##*/}
667152441Sbrooks		_range=${_ipaddr##*.}
668152441Sbrooks		_ipnet=${_ipaddr%.*}
669152441Sbrooks		_iplow=${_range%-*}
670152441Sbrooks		_iphigh=${_range#*-}
671152441Sbrooks
672152441Sbrooks		# clear netmask when removing aliases
673152441Sbrooks		if [ "${_action}" = "-alias" ]; then
674152441Sbrooks			_netmask=""
675152441Sbrooks		fi
676212578Shrs
677152441Sbrooks		_ipcount=${_iplow}
678152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
679152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
680152441Sbrooks			_ipcount=$((${_ipcount}+1))
681152441Sbrooks			_ret=0
682152441Sbrooks
683152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
684152441Sbrooks			if [ "${_action}" != "-alias" ]; then
685152441Sbrooks				_netmask="/32"
686152441Sbrooks			fi
687152441Sbrooks		done
688152441Sbrooks	done
689197139Shrs
690152441Sbrooks	return $_ret
691152441Sbrooks}
692152441Sbrooks
693197139Shrs# ifalias_up if af
694113674Smtm#	Configure aliases for network interface $if.
695113674Smtm#	It returns 0 if at least one alias was configured or
696113674Smtm#	1 if there were none.
697113674Smtm#
698113674Smtmifalias_up()
699113674Smtm{
700197139Shrs	local _ret
701113674Smtm	_ret=1
702197139Shrs
703197139Shrs	case "$2" in
704197139Shrs	inet)
705197139Shrs		_ret=`ifalias_ipv4_up "$1"`
706197139Shrs		;;
707197139Shrs	inet6)
708197139Shrs		_ret=`ifalias_ipv6_up "$1"`
709197139Shrs		;;
710197139Shrs	esac
711197139Shrs
712197139Shrs	return $_ret
713197139Shrs}
714197139Shrs
715197139Shrs# ifalias_ipv4_up if
716197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
717197139Shrs#
718197139Shrsifalias_ipv4_up()
719197139Shrs{
720197139Shrs	local _ret alias ifconfig_args
721197139Shrs	_ret=1
722197139Shrs
723197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
724113674Smtm	alias=0
725113674Smtm	while : ; do
726157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
727197139Shrs		case "${ifconfig_args}" in
728197139Shrs		inet\ *)
729197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
730197139Shrs			;;
731228472Sglebius		inet6\ *)
732228472Sglebius			;;
733197139Shrs		"")
734197139Shrs			break
735197139Shrs			;;
736228472Sglebius		*)
737228472Sglebius			warn "\$ifconfig_$1_alias${alias} needs " \
738228472Sglebius			    "\"inet\" keyword for an IPv4 address."
739228472Sglebius			ifconfig $1 ${ifconfig_args} alias && _ret=0
740228472Sglebius			;;
741197139Shrs		esac
742197147Shrs		alias=$((${alias} + 1))
743197139Shrs	done
744197139Shrs
745197139Shrs	return $_ret
746197139Shrs}
747197139Shrs
748197139Shrs# ifalias_ipv6_up if
749197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
750197139Shrs#
751197139Shrsifalias_ipv6_up()
752197139Shrs{
753197139Shrs	local _ret alias ifconfig_args
754197139Shrs	_ret=1
755197139Shrs
756197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
757197139Shrs	alias=0
758197139Shrs	while : ; do
759197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
760197139Shrs		case "${ifconfig_args}" in
761197139Shrs		inet6\ *)
762197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
763197139Shrs			;;
764197139Shrs		"")
765113674Smtm			break
766197139Shrs			;;
767197139Shrs		esac
768197139Shrs		alias=$((${alias} + 1))
769113674Smtm	done
770197139Shrs
771197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
772197139Shrs	alias=0
773197139Shrs	while : ; do
774197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
775197139Shrs		case "${ifconfig_args}" in
776197139Shrs		"")
777197139Shrs			break
778197139Shrs			;;
779197139Shrs		*)
780197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
781197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
782197139Shrs			    "  Use ifconfig_$1_aliasN instead."
783197139Shrs			;;
784197139Shrs		esac
785197139Shrs		alias=$((${alias} + 1))
786197139Shrs	done
787197139Shrs
788113674Smtm	return $_ret
789113674Smtm}
790100280Sgordon
791197147Shrs# ifalias_down if af
792116029Smtm#	Remove aliases for network interface $if.
793116029Smtm#	It returns 0 if at least one alias was removed or
794116029Smtm#	1 if there were none.
795116029Smtm#
796116029Smtmifalias_down()
797116029Smtm{
798197139Shrs	local _ret
799116029Smtm	_ret=1
800197139Shrs
801197139Shrs	case "$2" in
802197139Shrs	inet)
803197139Shrs		_ret=`ifalias_ipv4_down "$1"`
804197139Shrs		;;
805197139Shrs	inet6)
806197139Shrs		_ret=`ifalias_ipv6_down "$1"`
807197139Shrs		;;
808197139Shrs	esac
809197139Shrs
810197139Shrs	return $_ret
811197139Shrs}
812197139Shrs
813197147Shrs# ifalias_ipv4_down if
814197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
815197139Shrs#
816197139Shrsifalias_ipv4_down()
817197139Shrs{
818197139Shrs	local _ret alias ifconfig_args
819197139Shrs	_ret=1
820197139Shrs
821197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
822116029Smtm	alias=0
823116029Smtm	while : ; do
824157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
825197139Shrs		case "${ifconfig_args}" in
826197139Shrs		inet\ *)
827197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
828197139Shrs			;;
829197139Shrs		"")
830197139Shrs			break
831197139Shrs			;;
832197139Shrs		esac
833197147Shrs		alias=$((${alias} + 1))
834197139Shrs	done
835197139Shrs
836197139Shrs	return $_ret
837197139Shrs}
838197139Shrs
839197147Shrs# ifalias_ipv6_down if
840197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
841197139Shrs#
842197139Shrsifalias_ipv6_down()
843197139Shrs{
844197139Shrs	local _ret alias ifconfig_args
845197139Shrs	_ret=1
846197139Shrs
847197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
848197139Shrs	alias=0
849197139Shrs	while : ; do
850197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
851197139Shrs		case "${ifconfig_args}" in
852197139Shrs		inet6\ *)
853197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
854197139Shrs			;;
855197139Shrs		"")
856116029Smtm			break
857197139Shrs			;;
858197139Shrs		esac
859197139Shrs		alias=$((${alias} + 1))
860116029Smtm	done
861197139Shrs
862197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
863197526Shrs	alias=0
864197139Shrs	while : ; do
865197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
866197139Shrs		case "${ifconfig_args}" in
867197139Shrs		"")
868197139Shrs			break
869197139Shrs			;;
870197139Shrs		*)
871197526Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
872197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
873197139Shrs			    "  Use ifconfig_$1_aliasN instead."
874197139Shrs			;;
875197139Shrs		esac
876197526Shrs		alias=$((${alias} + 1))
877197139Shrs	done
878197139Shrs
879116029Smtm	return $_ret
880116029Smtm}
881116029Smtm
882226652Shrs# ipv6_prefix_hostid_addr_common if action
883226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
884226652Shrs#
885226652Shrsipv6_prefix_hostid_addr_common()
886197139Shrs{
887226652Shrs	local _if _action prefix laddr hostid j address
888197139Shrs	_if=$1
889226652Shrs	_action=$2
890197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
891197139Shrs
892197139Shrs	if [ -n "${prefix}" ]; then
893197139Shrs		laddr=`network6_getladdr ${_if}`
894197139Shrs		hostid=${laddr#fe80::}
895197139Shrs		hostid=${hostid%\%*}
896197139Shrs
897197139Shrs		for j in ${prefix}; do
898197139Shrs			address=$j\:${hostid}
899226652Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 ${_action}
900197139Shrs
901197139Shrs			# if I am a router, add subnet router
902197139Shrs			# anycast address (RFC 2373).
903197139Shrs			if checkyesno ipv6_gateway_enable; then
904197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
905226652Shrs					${_action} anycast
906197139Shrs			fi
907197139Shrs		done
908197139Shrs	fi
909197139Shrs}
910197139Shrs
911197139Shrs# ipv6_accept_rtadv_up if
912197139Shrs#	Enable accepting Router Advertisement and send Router
913197139Shrs#	Solicitation message
914197139Shrsipv6_accept_rtadv_up()
915197139Shrs{
916197139Shrs	if ipv6_autoconfif $1; then
917197139Shrs		ifconfig $1 inet6 accept_rtadv up
918203433Sume		if ! checkyesno rtsold_enable; then
919203433Sume			rtsol ${rtsol_flags} $1
920203433Sume		fi
921197139Shrs	fi
922197139Shrs}
923197139Shrs
924197139Shrs# ipv6_accept_rtadv_down if
925197139Shrs#	Disable accepting Router Advertisement
926197139Shrsipv6_accept_rtadv_down()
927197139Shrs{
928197139Shrs	if ipv6_autoconfif $1; then
929197139Shrs		ifconfig $1 inet6 -accept_rtadv
930197139Shrs	fi
931197139Shrs}
932197139Shrs
933113674Smtm# ifscript_up if
934113674Smtm#	Evaluate a startup script for the $if interface.
935113674Smtm#	It returns 0 if a script was found and processed or
936113674Smtm#	1 if no script was found.
937113674Smtm#
938113674Smtmifscript_up()
939100280Sgordon{
940113674Smtm	if [ -r /etc/start_if.$1 ]; then
941113674Smtm		. /etc/start_if.$1
942113674Smtm		return 0
943197139Shrs	else
944197139Shrs		return 1
945113674Smtm	fi
946100280Sgordon}
947100280Sgordon
948116029Smtm# ifscript_down if
949116029Smtm#	Evaluate a shutdown script for the $if interface.
950116029Smtm#	It returns 0 if a script was found and processed or
951116029Smtm#	1 if no script was found.
952116029Smtm#
953116029Smtmifscript_down()
954116029Smtm{
955116029Smtm	if [ -r /etc/stop_if.$1 ]; then
956116029Smtm		. /etc/stop_if.$1
957116029Smtm		return 0
958197139Shrs	else
959197139Shrs		return 1
960116029Smtm	fi
961116029Smtm}
962116029Smtm
963197147Shrs# clone_up
964197147Shrs#	Create cloneable interfaces.
965113674Smtm#
966113674Smtmclone_up()
967100280Sgordon{
968197139Shrs	local _prefix _list ifn
969113674Smtm	_prefix=
970113674Smtm	_list=
971197139Shrs
972197139Shrs	# create_args_IF
973113674Smtm	for ifn in ${cloned_interfaces}; do
974178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
975116774Skuriyama		if [ $? -eq 0 ]; then
976113674Smtm			_list="${_list}${_prefix}${ifn}"
977113674Smtm			[ -z "$_prefix" ] && _prefix=' '
978113674Smtm		fi
979113674Smtm	done
980113674Smtm	debug "Cloned: ${_list}"
981113674Smtm}
982100280Sgordon
983197147Shrs# clone_down
984197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
985197147Shrs#	standard output.
986113674Smtm#
987113674Smtmclone_down()
988113674Smtm{
989197139Shrs	local _prefix _list ifn
990113674Smtm	_prefix=
991113674Smtm	_list=
992197139Shrs
993113674Smtm	for ifn in ${cloned_interfaces}; do
994208213Sjhb		ifconfig -n ${ifn} destroy
995116774Skuriyama		if [ $? -eq 0 ]; then
996113674Smtm			_list="${_list}${_prefix}${ifn}"
997113674Smtm			[ -z "$_prefix" ] && _prefix=' '
998113674Smtm		fi
999113674Smtm	done
1000113674Smtm	debug "Destroyed clones: ${_list}"
1001100280Sgordon}
1002100280Sgordon
1003197147Shrs# childif_create
1004197147Shrs#	Create and configure child interfaces.  Return 0 if child
1005197147Shrs#	interfaces are created.
1006178356Ssam#
1007178356Ssamchildif_create()
1008178356Ssam{
1009201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1010178356Ssam	cfg=1
1011178356Ssam	ifn=$1
1012178356Ssam
1013178527Sbrooks	# Create wireless interfaces
1014178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1015178527Sbrooks
1016178527Sbrooks	for child in ${child_wlans}; do
1017183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1018189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1019189759Sbrooks
1020178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1021178356Ssam			ifconfig $child create ${create_args} && cfg=0
1022189759Sbrooks			if [ -n "${debug_flags}" ]; then
1023189759Sbrooks				wlandebug -i $child ${debug_flags}
1024189759Sbrooks			fi
1025178356Ssam		else
1026178356Ssam			i=`ifconfig wlan create ${create_args}`
1027189759Sbrooks			if [ -n "${debug_flags}" ]; then
1028189759Sbrooks				wlandebug -i $i ${debug_flags}
1029189759Sbrooks			fi
1030178356Ssam			ifconfig $i name $child && cfg=0
1031178356Ssam		fi
1032188118Sthompsa		if autoif $child; then
1033188118Sthompsa			ifn_start $child
1034188118Sthompsa		fi
1035178356Ssam	done
1036178356Ssam
1037201215Sjhb	# Create vlan interfaces
1038201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1039201215Sjhb
1040201215Sjhb	if [ -n "${child_vlans}" ]; then
1041201215Sjhb		load_kld if_vlan
1042201215Sjhb	fi
1043201215Sjhb
1044201215Sjhb	for child in ${child_vlans}; do
1045201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1046201215Sjhb			child="${ifn}.${child}"
1047201215Sjhb			create_args=`get_if_var $child create_args_IF`
1048201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1049201215Sjhb		else
1050201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1051201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1052201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1053201215Sjhb			else
1054201215Sjhb				i=`ifconfig vlan create ${create_args}`
1055201215Sjhb				ifconfig $i name $child && cfg=0
1056201215Sjhb			fi
1057201215Sjhb		fi
1058201215Sjhb		if autoif $child; then
1059201215Sjhb			ifn_start $child
1060201215Sjhb		fi
1061201215Sjhb	done
1062201215Sjhb
1063179001Sbrooks	return ${cfg}
1064178356Ssam}
1065178356Ssam
1066197147Shrs# childif_destroy
1067197147Shrs#	Destroy child interfaces.
1068178356Ssam#
1069178356Ssamchildif_destroy()
1070178356Ssam{
1071201215Sjhb	local cfg child child_vlans child_wlans ifn
1072197139Shrs	cfg=1
1073178356Ssam
1074201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1075178527Sbrooks	for child in ${child_wlans}; do
1076201215Sjhb		if ! ifexists $child; then
1077201215Sjhb			continue
1078201215Sjhb		fi
1079208213Sjhb		ifconfig -n $child destroy && cfg=0
1080178356Ssam	done
1081197139Shrs
1082201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1083201215Sjhb	for child in ${child_vlans}; do
1084201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1085201215Sjhb			child="${ifn}.${child}"
1086201215Sjhb		fi
1087201215Sjhb		if ! ifexists $child; then
1088201215Sjhb			continue
1089201215Sjhb		fi
1090208213Sjhb		ifconfig -n $child destroy && cfg=0
1091201215Sjhb	done
1092201215Sjhb
1093197139Shrs	return ${cfg}
1094178356Ssam}
1095178356Ssam
1096197147Shrs# ng_mkpeer
1097197147Shrs#	Create netgraph nodes.
1098166583Sflz#
1099197147Shrsng_mkpeer()
1100197147Shrs{
1101166583Sflz	ngctl -f - 2> /dev/null <<EOF
1102166583Sflzmkpeer $*
1103166583Sflzmsg dummy nodeinfo
1104166583SflzEOF
1105166583Sflz}
1106166583Sflz
1107197147Shrs# ng_create_one
1108197147Shrs#	Create netgraph nodes.
1109197147Shrs#
1110197147Shrsng_create_one()
1111197147Shrs{
1112197139Shrs	local t
1113197139Shrs
1114166583Sflz	ng_mkpeer $* | while read line; do
1115166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1116166583Sflz		if [ -n "${t}" ]; then
1117166583Sflz			echo ${t}
1118166583Sflz			return
1119166583Sflz		fi
1120166583Sflz	done
1121166583Sflz}
1122166583Sflz
1123197147Shrs# gif_up
1124197147Shrs#	Create gif(4) tunnel interfaces.
1125197147Shrsgif_up()
1126197147Shrs{
1127197139Shrs	local i peers
1128197139Shrs
1129166583Sflz	for i in ${gif_interfaces}; do
1130166583Sflz		peers=`get_if_var $i gifconfig_IF`
1131166583Sflz		case ${peers} in
1132166583Sflz		'')
1133166583Sflz			continue
1134166583Sflz			;;
1135166583Sflz		*)
1136177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1137177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1138177682Sbrooks			else
1139177682Sbrooks				gif=`ifconfig gif create`
1140177682Sbrooks				ifconfig $gif name $i
1141177682Sbrooks			fi
1142166583Sflz			ifconfig $i tunnel ${peers}
1143166583Sflz			ifconfig $i up
1144166583Sflz			;;
1145166583Sflz		esac
1146166583Sflz	done
1147166583Sflz}
1148166583Sflz
1149166583Sflz# ng_fec_create ifn
1150197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1151197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1152197139Shrsng_fec_create()
1153197139Shrs{
1154166583Sflz	 local req_iface iface bogus
1155166583Sflz	 req_iface="$1"
1156166583Sflz
1157166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1158166583Sflz
1159166583Sflz	 bogus=""
1160166583Sflz	 while true; do
1161166583Sflz		 iface=`ng_create_one fec dummy fec`
1162166583Sflz		 if [ -z "${iface}" ]; then
1163166583Sflz			 exit 2
1164166583Sflz		 fi
1165166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1166166583Sflz			 break
1167166583Sflz		 fi
1168166583Sflz		 bogus="${bogus} ${iface}"
1169166583Sflz	 done
1170166583Sflz
1171166583Sflz	 for iface in ${bogus}; do
1172166583Sflz		 ngctl shutdown ${iface}:
1173166583Sflz	 done
1174166583Sflz}
1175166583Sflz
1176197147Shrs# fec_up
1177197147Shrs#	Create Fast EtherChannel interfaces.
1178197147Shrsfec_up()
1179197147Shrs{
1180197139Shrs	local i j
1181197139Shrs
1182166583Sflz	for i in ${fec_interfaces}; do
1183166583Sflz		ng_fec_create $i
1184166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1185166583Sflz			case ${j} in
1186100282Sdougb			'')
1187100282Sdougb				continue
1188100282Sdougb				;;
1189100282Sdougb			*)
1190166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1191100282Sdougb				;;
1192100282Sdougb			esac
1193100282Sdougb		done
1194166583Sflz	done
1195100282Sdougb}
1196100282Sdougb
1197113674Smtm# ipx_up ifn
1198197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1199197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1200113674Smtm#
1201113674Smtmipx_up()
1202100280Sgordon{
1203197139Shrs	local ifn
1204113674Smtm	ifn="$1"
1205197139Shrs
1206197139Shrs	# ifconfig_IF_ipx
1207197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1208113674Smtm	if [ -n "${ifconfig_args}" ]; then
1209113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1210113674Smtm		return 0
121185831Sdes	fi
1212197139Shrs
1213113674Smtm	return 1
1214113674Smtm}
121585831Sdes
1216116029Smtm# ipx_down ifn
1217116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1218116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1219113674Smtm#
1220116029Smtmipx_down()
1221116029Smtm{
1222197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1223197139Shrs	_if=$1
1224116100Smtm	_ifs="^"
1225116100Smtm	_ret=1
1226197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1227197139Shrs	oldifs="$IFS"
1228116100Smtm
1229116100Smtm	IFS="$_ifs"
1230116100Smtm	for _ipx in $ipxList ; do
1231116100Smtm		# get rid of extraneous line
1232116100Smtm		[ -z "$_ipx" ] && break
1233116100Smtm
1234116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1235116100Smtm
1236116100Smtm		IFS="$oldifs"
1237197139Shrs		ifconfig ${_if} ${_ipx} delete
1238116100Smtm		IFS="$_ifs"
1239116100Smtm		_ret=0
1240116100Smtm	done
1241116100Smtm	IFS="$oldifs"
1242116100Smtm
1243116100Smtm	return $_ret
1244116029Smtm}
1245116029Smtm
1246137070Spjd# ifnet_rename
1247137070Spjd#	Rename all requested interfaces.
1248116029Smtm#
1249137070Spjdifnet_rename()
1250137070Spjd{
1251197139Shrs	local _if _ifname
1252137070Spjd
1253197139Shrs	# ifconfig_IF_name
1254197139Shrs	for _if in `ifconfig -l`; do
1255157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1256137070Spjd		if [ ! -z "$_ifname" ]; then
1257137070Spjd			ifconfig $_if name $_ifname
1258137070Spjd		fi
1259137070Spjd	done
1260197139Shrs
1261137070Spjd	return 0
1262137070Spjd}
1263137070Spjd
1264113674Smtm# list_net_interfaces type
1265113674Smtm#	List all network interfaces. The type of interface returned
1266113674Smtm#	can be controlled by the type argument. The type
1267113674Smtm#	argument can be any of the following:
1268197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1269197147Shrs#		dhcp	- list only DHCP configured interfaces
1270197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1271197139Shrs#				  Address Autoconf configured interfaces
1272197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1273197139Shrs#				  configured interfaces
1274113674Smtm#	If no argument is specified all network interfaces are output.
1275134429Syar#	Note that the list will include cloned interfaces if applicable.
1276134429Syar#	Cloned interfaces must already exist to have a chance to appear
1277134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1278113674Smtm#
1279113674Smtmlist_net_interfaces()
1280113674Smtm{
1281197139Shrs	local type _tmplist _list _autolist _lo _if
1282113674Smtm	type=$1
128365532Snectar
1284149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
128551231Ssheldonh	#
1286197139Shrs	_tmplist=
128751231Ssheldonh	case ${network_interfaces} in
128851231Ssheldonh	[Aa][Uu][Tt][Oo])
1289149401Sbrooks		_autolist="`ifconfig -l`"
1290149726Sbrooks		_lo=
1291149401Sbrooks		for _if in ${_autolist} ; do
1292149401Sbrooks			if autoif $_if; then
1293149726Sbrooks				if [ "$_if" = "lo0" ]; then
1294149726Sbrooks					_lo="lo0 "
1295149726Sbrooks				else
1296197139Shrs					_tmplist="${_tmplist} ${_if}"
1297149726Sbrooks				fi
1298149401Sbrooks			fi
1299149401Sbrooks		done
1300197139Shrs		_tmplist="${_lo}${_tmplist# }"
130151231Ssheldonh		;;
130283677Sbrooks	*)
1303149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1304196478Sdougb
1305196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1306196478Sdougb		#
1307196478Sdougb		case "$_tmplist" in
1308196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1309196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1310196478Sdougb		esac
131183677Sbrooks		;;
131251231Ssheldonh	esac
131349122Sbrian
1314197139Shrs	_list=
1315197139Shrs	case "$type" in
1316197139Shrs	nodhcp)
1317197139Shrs		for _if in ${_tmplist} ; do
1318197139Shrs			if ! dhcpif $_if && \
1319197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1320197139Shrs				_list="${_list# } ${_if}"
1321197147Shrs			fi
1322197139Shrs		done
1323197139Shrs		;;
1324197139Shrs	dhcp)
1325197147Shrs		for _if in ${_tmplist} ; do
1326197147Shrs			if dhcpif $_if; then
1327197139Shrs				_list="${_list# } ${_if}"
1328197147Shrs			fi
1329197147Shrs		done
1330113674Smtm		;;
1331197139Shrs	noautoconf)
1332197139Shrs		for _if in ${_tmplist} ; do
1333197139Shrs			if ! ipv6_autoconfif $_if && \
1334197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1335197139Shrs				_list="${_list# } ${_if}"
1336197139Shrs			fi
1337197139Shrs		done
1338113674Smtm		;;
1339197139Shrs	autoconf)
1340197139Shrs		for _if in ${_tmplist} ; do
1341197139Shrs			if ipv6_autoconfif $_if; then
1342197139Shrs				_list="${_list# } ${_if}"
1343197139Shrs			fi
1344197139Shrs		done
1345197139Shrs		;;
1346197139Shrs	*)
1347197139Shrs		_list=${_tmplist}
1348197139Shrs		;;
1349113674Smtm	esac
1350197139Shrs
1351197139Shrs	echo $_list
1352197139Shrs
1353130151Sschweikh	return 0
135425184Sjkh}
1355114942Sume
1356179003Sbrooks# get_default_if -address_family
1357179003Sbrooks#	Get the interface of the default route for the given address family.
1358179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1359179003Sbrooks#
1360179003Sbrooksget_default_if()
1361179003Sbrooks{
1362197139Shrs	local routeget oldifs defif line
1363197139Shrs	defif=
1364179003Sbrooks	oldifs="$IFS"
1365179003Sbrooks	IFS="
1366179003Sbrooks"
1367197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1368179003Sbrooks		case $line in
1369179003Sbrooks		*interface:*)
1370179003Sbrooks			defif=${line##*: }
1371179003Sbrooks			;;
1372179003Sbrooks		esac
1373179003Sbrooks	done
1374179003Sbrooks	IFS=${oldifs}
1375179003Sbrooks
1376179003Sbrooks	echo $defif
1377179003Sbrooks}
1378179003Sbrooks
1379197147Shrs# hexdigit arg
1380197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1381114942Sumehexdigit()
1382114942Sume{
1383221884Sjilles	printf '%x\n' "$1"
1384114942Sume}
1385114942Sume
1386197147Shrs# hexprint arg
1387197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1388114942Sumehexprint()
1389114942Sume{
1390221884Sjilles	printf '%x\n' "$1"
1391114942Sume}
1392114942Sume
1393196436Sdougbis_wired_interface()
1394196436Sdougb{
1395196436Sdougb	local media
1396196436Sdougb
1397196436Sdougb	case `ifconfig $1 2>/dev/null` in
1398196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1399196436Sdougb	esac
1400196436Sdougb
1401196436Sdougb	test "$media" = "Ethernet"
1402196436Sdougb}
1403196436Sdougb
1404197147Shrs# network6_getladdr if [flag]
1405197147Shrs#	Echo link-local address from $if if any.
1406197147Shrs#	If flag is defined, tentative ones will be excluded.
1407114942Sumenetwork6_getladdr()
1408114942Sume{
1409197139Shrs	local proto addr rest
1410114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1411114942Sume		case ${proto} in
1412114942Sume		inet6)
1413114942Sume			case ${addr} in
1414114942Sume			fe80::*)
1415114942Sume				if [ -z "$2" ]; then
1416114942Sume					echo ${addr}
1417114942Sume					return
1418114942Sume				fi
1419114942Sume				case ${rest} in
1420114942Sume				*tentative*)
1421114942Sume					continue
1422114942Sume					;;
1423114942Sume				*)
1424114942Sume					echo ${addr}
1425114942Sume					return
1426114942Sume				esac
1427114942Sume			esac
1428114942Sume		esac
1429114942Sume	done
1430114942Sume}
1431