network.subr revision 242181
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 242181 2012-10-27 17:06:26Z 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)
507242181Shrs		if checkyesno ipv6_gateway_enable; then
508242181Shrs			return 1
509242181Shrs		else
510242181Shrs			return 0
511242181Shrs		fi
512212577Shrs	;;
513197526Shrs	esac
514197526Shrs
515212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
516212574Shrs	for _arg in $_tmpargs; do
517212574Shrs		case $_arg in
518212574Shrs		accept_rtadv)
519212574Shrs			return 0
520212574Shrs			;;
521212574Shrs		esac
522212574Shrs	done
523212574Shrs
524212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
525212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
526212574Shrs	for _arg in $_tmpargs; do
527212574Shrs		case $_arg in
528212574Shrs		accept_rtadv)
529212574Shrs			return 0
530212574Shrs			;;
531212574Shrs		esac
532212574Shrs	done
533212574Shrs
534197139Shrs	return 1
535197139Shrs}
536197139Shrs
537161386Sbrooks# ifexists if
538161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
539161386Sbrooksifexists()
540161386Sbrooks{
541197139Shrs	[ -z "$1" ] && return 1
542169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
543161386Sbrooks}
544161386Sbrooks
545152441Sbrooks# ipv4_up if
546212578Shrs#	add IPv4 addresses to the interface $if
547152441Sbrooksipv4_up()
548152441Sbrooks{
549197139Shrs	local _if _ret
550152441Sbrooks	_if=$1
551197139Shrs	_ret=1
552197139Shrs
553222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
554222515Sbz	if [ "${_if}" = "lo0" ]; then
555226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
556222515Sbz		if [ -z "${ifconfig_args}" ]; then
557222515Sbz			ifconfig ${_if} inet 127.0.0.1/8 alias
558222515Sbz		fi
559222515Sbz	fi
560197139Shrs	ifalias_up ${_if} inet && _ret=0
561197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
562197139Shrs
563197139Shrs	return $_ret
564152441Sbrooks}
565152441Sbrooks
566197139Shrs# ipv6_up if
567197139Shrs#	add IPv6 addresses to the interface $if
568197139Shrsipv6_up()
569197139Shrs{
570197139Shrs	local _if _ret
571197139Shrs	_if=$1
572197139Shrs	_ret=1
573197139Shrs
574197139Shrs	if ! ipv6if $_if; then
575197139Shrs		return 0
576197139Shrs	fi
577197139Shrs
578197139Shrs	ifalias_up ${_if} inet6 && _ret=0
579226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
580197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
581197139Shrs
582197139Shrs	return $_ret
583197139Shrs}
584197139Shrs
585152441Sbrooks# ipv4_down if
586197147Shrs#	remove IPv4 addresses from the interface $if
587152441Sbrooksipv4_down()
588152441Sbrooks{
589197139Shrs	local _if _ifs _ret inetList oldifs _inet
590152441Sbrooks	_if=$1
591161386Sbrooks	_ifs="^"
592161386Sbrooks	_ret=1
593161386Sbrooks
594161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
595161386Sbrooks
596161386Sbrooks	oldifs="$IFS"
597161386Sbrooks	IFS="$_ifs"
598161386Sbrooks	for _inet in $inetList ; do
599161386Sbrooks		# get rid of extraneous line
600161386Sbrooks		[ -z "$_inet" ] && break
601161386Sbrooks
602161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
603161386Sbrooks
604161386Sbrooks		IFS="$oldifs"
605161386Sbrooks		ifconfig ${_if} ${_inet} delete
606161386Sbrooks		IFS="$_ifs"
607161386Sbrooks		_ret=0
608161386Sbrooks	done
609161386Sbrooks	IFS="$oldifs"
610161386Sbrooks
611197139Shrs	ifalias_down ${_if} inet && _ret=0
612161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
613161386Sbrooks
614161386Sbrooks	return $_ret
615152441Sbrooks}
616152441Sbrooks
617197139Shrs# ipv6_down if
618197139Shrs#	remove IPv6 addresses from the interface $if
619197139Shrsipv6_down()
620197139Shrs{
621197139Shrs	local _if _ifs _ret inetList oldifs _inet6
622197139Shrs	_if=$1
623197139Shrs	_ifs="^"
624197139Shrs	_ret=1
625197139Shrs
626197139Shrs	if ! ipv6if $_if; then
627197139Shrs		return 0
628197139Shrs	fi
629197139Shrs
630197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
631226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
632197139Shrs	ifalias_down ${_if} inet6 && _ret=0
633197139Shrs
634197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
635197139Shrs
636197139Shrs	oldifs="$IFS"
637197139Shrs	IFS="$_ifs"
638197139Shrs	for _inet6 in $inetList ; do
639197139Shrs		# get rid of extraneous line
640197139Shrs		[ -z "$_inet6" ] && break
641197139Shrs
642197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
643197139Shrs
644197139Shrs		IFS="$oldifs"
645197139Shrs		ifconfig ${_if} ${_inet6} -alias
646197139Shrs		IFS="$_ifs"
647197139Shrs		_ret=0
648197139Shrs	done
649197139Shrs	IFS="$oldifs"
650197139Shrs
651197139Shrs	return $_ret
652197139Shrs}
653197139Shrs
654152441Sbrooks# ipv4_addrs_common if action
655197147Shrs#	Evaluate the ifconfig_if_ipv4 arguments for interface $if and
656197147Shrs#	use $action to add or remove IPv4 addresses from $if.
657152441Sbrooksipv4_addrs_common()
658197147Shrs{
659197139Shrs	local _ret _if _action _cidr _cidr_addr
660212578Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount
661152441Sbrooks	_ret=1
662152441Sbrooks	_if=$1
663152441Sbrooks	_action=$2
664212578Shrs
665152441Sbrooks	# get ipv4-addresses
666157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
667212578Shrs
668152441Sbrooks	for _cidr in ${cidr_addr}; do
669152441Sbrooks		_ipaddr=${_cidr%%/*}
670152441Sbrooks		_netmask="/"${_cidr##*/}
671152441Sbrooks		_range=${_ipaddr##*.}
672152441Sbrooks		_ipnet=${_ipaddr%.*}
673152441Sbrooks		_iplow=${_range%-*}
674152441Sbrooks		_iphigh=${_range#*-}
675152441Sbrooks
676152441Sbrooks		# clear netmask when removing aliases
677152441Sbrooks		if [ "${_action}" = "-alias" ]; then
678152441Sbrooks			_netmask=""
679152441Sbrooks		fi
680212578Shrs
681152441Sbrooks		_ipcount=${_iplow}
682152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
683152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
684152441Sbrooks			_ipcount=$((${_ipcount}+1))
685152441Sbrooks			_ret=0
686152441Sbrooks
687152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
688152441Sbrooks			if [ "${_action}" != "-alias" ]; then
689152441Sbrooks				_netmask="/32"
690152441Sbrooks			fi
691152441Sbrooks		done
692152441Sbrooks	done
693197139Shrs
694152441Sbrooks	return $_ret
695152441Sbrooks}
696152441Sbrooks
697197139Shrs# ifalias_up if af
698113674Smtm#	Configure aliases for network interface $if.
699113674Smtm#	It returns 0 if at least one alias was configured or
700113674Smtm#	1 if there were none.
701113674Smtm#
702113674Smtmifalias_up()
703113674Smtm{
704197139Shrs	local _ret
705113674Smtm	_ret=1
706197139Shrs
707197139Shrs	case "$2" in
708197139Shrs	inet)
709197139Shrs		_ret=`ifalias_ipv4_up "$1"`
710197139Shrs		;;
711197139Shrs	inet6)
712197139Shrs		_ret=`ifalias_ipv6_up "$1"`
713197139Shrs		;;
714197139Shrs	esac
715197139Shrs
716197139Shrs	return $_ret
717197139Shrs}
718197139Shrs
719197139Shrs# ifalias_ipv4_up if
720197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
721197139Shrs#
722197139Shrsifalias_ipv4_up()
723197139Shrs{
724197139Shrs	local _ret alias ifconfig_args
725197139Shrs	_ret=1
726197139Shrs
727197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
728113674Smtm	alias=0
729113674Smtm	while : ; do
730157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
731197139Shrs		case "${ifconfig_args}" in
732197139Shrs		inet\ *)
733197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
734197139Shrs			;;
735228472Sglebius		inet6\ *)
736228472Sglebius			;;
737197139Shrs		"")
738197139Shrs			break
739197139Shrs			;;
740228472Sglebius		*)
741228472Sglebius			warn "\$ifconfig_$1_alias${alias} needs " \
742228472Sglebius			    "\"inet\" keyword for an IPv4 address."
743228472Sglebius			ifconfig $1 ${ifconfig_args} alias && _ret=0
744228472Sglebius			;;
745197139Shrs		esac
746197147Shrs		alias=$((${alias} + 1))
747197139Shrs	done
748197139Shrs
749197139Shrs	return $_ret
750197139Shrs}
751197139Shrs
752197139Shrs# ifalias_ipv6_up if
753197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
754197139Shrs#
755197139Shrsifalias_ipv6_up()
756197139Shrs{
757197139Shrs	local _ret alias ifconfig_args
758197139Shrs	_ret=1
759197139Shrs
760197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
761197139Shrs	alias=0
762197139Shrs	while : ; do
763197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
764197139Shrs		case "${ifconfig_args}" in
765197139Shrs		inet6\ *)
766197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
767197139Shrs			;;
768197139Shrs		"")
769113674Smtm			break
770197139Shrs			;;
771197139Shrs		esac
772197139Shrs		alias=$((${alias} + 1))
773113674Smtm	done
774197139Shrs
775197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
776197139Shrs	alias=0
777197139Shrs	while : ; do
778197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
779197139Shrs		case "${ifconfig_args}" in
780197139Shrs		"")
781197139Shrs			break
782197139Shrs			;;
783197139Shrs		*)
784197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
785197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
786197139Shrs			    "  Use ifconfig_$1_aliasN instead."
787197139Shrs			;;
788197139Shrs		esac
789197139Shrs		alias=$((${alias} + 1))
790197139Shrs	done
791197139Shrs
792113674Smtm	return $_ret
793113674Smtm}
794100280Sgordon
795197147Shrs# ifalias_down if af
796116029Smtm#	Remove aliases for network interface $if.
797116029Smtm#	It returns 0 if at least one alias was removed or
798116029Smtm#	1 if there were none.
799116029Smtm#
800116029Smtmifalias_down()
801116029Smtm{
802197139Shrs	local _ret
803116029Smtm	_ret=1
804197139Shrs
805197139Shrs	case "$2" in
806197139Shrs	inet)
807197139Shrs		_ret=`ifalias_ipv4_down "$1"`
808197139Shrs		;;
809197139Shrs	inet6)
810197139Shrs		_ret=`ifalias_ipv6_down "$1"`
811197139Shrs		;;
812197139Shrs	esac
813197139Shrs
814197139Shrs	return $_ret
815197139Shrs}
816197139Shrs
817197147Shrs# ifalias_ipv4_down if
818197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
819197139Shrs#
820197139Shrsifalias_ipv4_down()
821197139Shrs{
822197139Shrs	local _ret alias ifconfig_args
823197139Shrs	_ret=1
824197139Shrs
825197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
826116029Smtm	alias=0
827116029Smtm	while : ; do
828157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
829197139Shrs		case "${ifconfig_args}" in
830197139Shrs		inet\ *)
831197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
832197139Shrs			;;
833197139Shrs		"")
834197139Shrs			break
835197139Shrs			;;
836197139Shrs		esac
837197147Shrs		alias=$((${alias} + 1))
838197139Shrs	done
839197139Shrs
840197139Shrs	return $_ret
841197139Shrs}
842197139Shrs
843197147Shrs# ifalias_ipv6_down if
844197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
845197139Shrs#
846197139Shrsifalias_ipv6_down()
847197139Shrs{
848197139Shrs	local _ret alias ifconfig_args
849197139Shrs	_ret=1
850197139Shrs
851197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
852197139Shrs	alias=0
853197139Shrs	while : ; do
854197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
855197139Shrs		case "${ifconfig_args}" in
856197139Shrs		inet6\ *)
857197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
858197139Shrs			;;
859197139Shrs		"")
860116029Smtm			break
861197139Shrs			;;
862197139Shrs		esac
863197139Shrs		alias=$((${alias} + 1))
864116029Smtm	done
865197139Shrs
866197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
867197526Shrs	alias=0
868197139Shrs	while : ; do
869197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
870197139Shrs		case "${ifconfig_args}" in
871197139Shrs		"")
872197139Shrs			break
873197139Shrs			;;
874197139Shrs		*)
875197526Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
876197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
877197139Shrs			    "  Use ifconfig_$1_aliasN instead."
878197139Shrs			;;
879197139Shrs		esac
880197526Shrs		alias=$((${alias} + 1))
881197139Shrs	done
882197139Shrs
883116029Smtm	return $_ret
884116029Smtm}
885116029Smtm
886226652Shrs# ipv6_prefix_hostid_addr_common if action
887226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
888226652Shrs#
889226652Shrsipv6_prefix_hostid_addr_common()
890197139Shrs{
891226652Shrs	local _if _action prefix laddr hostid j address
892197139Shrs	_if=$1
893226652Shrs	_action=$2
894197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
895197139Shrs
896197139Shrs	if [ -n "${prefix}" ]; then
897197139Shrs		laddr=`network6_getladdr ${_if}`
898197139Shrs		hostid=${laddr#fe80::}
899197139Shrs		hostid=${hostid%\%*}
900197139Shrs
901197139Shrs		for j in ${prefix}; do
902197139Shrs			address=$j\:${hostid}
903226652Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 ${_action}
904197139Shrs
905197139Shrs			# if I am a router, add subnet router
906197139Shrs			# anycast address (RFC 2373).
907197139Shrs			if checkyesno ipv6_gateway_enable; then
908197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
909226652Shrs					${_action} anycast
910197139Shrs			fi
911197139Shrs		done
912197139Shrs	fi
913197139Shrs}
914197139Shrs
915197139Shrs# ipv6_accept_rtadv_up if
916197139Shrs#	Enable accepting Router Advertisement and send Router
917197139Shrs#	Solicitation message
918197139Shrsipv6_accept_rtadv_up()
919197139Shrs{
920197139Shrs	if ipv6_autoconfif $1; then
921197139Shrs		ifconfig $1 inet6 accept_rtadv up
922203433Sume		if ! checkyesno rtsold_enable; then
923203433Sume			rtsol ${rtsol_flags} $1
924203433Sume		fi
925197139Shrs	fi
926197139Shrs}
927197139Shrs
928197139Shrs# ipv6_accept_rtadv_down if
929197139Shrs#	Disable accepting Router Advertisement
930197139Shrsipv6_accept_rtadv_down()
931197139Shrs{
932197139Shrs	if ipv6_autoconfif $1; then
933197139Shrs		ifconfig $1 inet6 -accept_rtadv
934197139Shrs	fi
935197139Shrs}
936197139Shrs
937113674Smtm# ifscript_up if
938113674Smtm#	Evaluate a startup script for the $if interface.
939113674Smtm#	It returns 0 if a script was found and processed or
940113674Smtm#	1 if no script was found.
941113674Smtm#
942113674Smtmifscript_up()
943100280Sgordon{
944113674Smtm	if [ -r /etc/start_if.$1 ]; then
945113674Smtm		. /etc/start_if.$1
946113674Smtm		return 0
947197139Shrs	else
948197139Shrs		return 1
949113674Smtm	fi
950100280Sgordon}
951100280Sgordon
952116029Smtm# ifscript_down if
953116029Smtm#	Evaluate a shutdown script for the $if interface.
954116029Smtm#	It returns 0 if a script was found and processed or
955116029Smtm#	1 if no script was found.
956116029Smtm#
957116029Smtmifscript_down()
958116029Smtm{
959116029Smtm	if [ -r /etc/stop_if.$1 ]; then
960116029Smtm		. /etc/stop_if.$1
961116029Smtm		return 0
962197139Shrs	else
963197139Shrs		return 1
964116029Smtm	fi
965116029Smtm}
966116029Smtm
967197147Shrs# clone_up
968197147Shrs#	Create cloneable interfaces.
969113674Smtm#
970113674Smtmclone_up()
971100280Sgordon{
972197139Shrs	local _prefix _list ifn
973113674Smtm	_prefix=
974113674Smtm	_list=
975197139Shrs
976197139Shrs	# create_args_IF
977113674Smtm	for ifn in ${cloned_interfaces}; do
978178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
979116774Skuriyama		if [ $? -eq 0 ]; then
980113674Smtm			_list="${_list}${_prefix}${ifn}"
981113674Smtm			[ -z "$_prefix" ] && _prefix=' '
982113674Smtm		fi
983113674Smtm	done
984113674Smtm	debug "Cloned: ${_list}"
985113674Smtm}
986100280Sgordon
987197147Shrs# clone_down
988197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
989197147Shrs#	standard output.
990113674Smtm#
991113674Smtmclone_down()
992113674Smtm{
993197139Shrs	local _prefix _list ifn
994113674Smtm	_prefix=
995113674Smtm	_list=
996197139Shrs
997113674Smtm	for ifn in ${cloned_interfaces}; do
998208213Sjhb		ifconfig -n ${ifn} destroy
999116774Skuriyama		if [ $? -eq 0 ]; then
1000113674Smtm			_list="${_list}${_prefix}${ifn}"
1001113674Smtm			[ -z "$_prefix" ] && _prefix=' '
1002113674Smtm		fi
1003113674Smtm	done
1004113674Smtm	debug "Destroyed clones: ${_list}"
1005100280Sgordon}
1006100280Sgordon
1007197147Shrs# childif_create
1008197147Shrs#	Create and configure child interfaces.  Return 0 if child
1009197147Shrs#	interfaces are created.
1010178356Ssam#
1011178356Ssamchildif_create()
1012178356Ssam{
1013201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1014178356Ssam	cfg=1
1015178356Ssam	ifn=$1
1016178356Ssam
1017178527Sbrooks	# Create wireless interfaces
1018178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1019178527Sbrooks
1020178527Sbrooks	for child in ${child_wlans}; do
1021183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1022189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1023189759Sbrooks
1024178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1025178356Ssam			ifconfig $child create ${create_args} && cfg=0
1026189759Sbrooks			if [ -n "${debug_flags}" ]; then
1027189759Sbrooks				wlandebug -i $child ${debug_flags}
1028189759Sbrooks			fi
1029178356Ssam		else
1030178356Ssam			i=`ifconfig wlan create ${create_args}`
1031189759Sbrooks			if [ -n "${debug_flags}" ]; then
1032189759Sbrooks				wlandebug -i $i ${debug_flags}
1033189759Sbrooks			fi
1034178356Ssam			ifconfig $i name $child && cfg=0
1035178356Ssam		fi
1036188118Sthompsa		if autoif $child; then
1037188118Sthompsa			ifn_start $child
1038188118Sthompsa		fi
1039178356Ssam	done
1040178356Ssam
1041201215Sjhb	# Create vlan interfaces
1042201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1043201215Sjhb
1044201215Sjhb	if [ -n "${child_vlans}" ]; then
1045201215Sjhb		load_kld if_vlan
1046201215Sjhb	fi
1047201215Sjhb
1048201215Sjhb	for child in ${child_vlans}; do
1049201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1050201215Sjhb			child="${ifn}.${child}"
1051201215Sjhb			create_args=`get_if_var $child create_args_IF`
1052201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1053201215Sjhb		else
1054201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1055201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1056201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1057201215Sjhb			else
1058201215Sjhb				i=`ifconfig vlan create ${create_args}`
1059201215Sjhb				ifconfig $i name $child && cfg=0
1060201215Sjhb			fi
1061201215Sjhb		fi
1062201215Sjhb		if autoif $child; then
1063201215Sjhb			ifn_start $child
1064201215Sjhb		fi
1065201215Sjhb	done
1066201215Sjhb
1067179001Sbrooks	return ${cfg}
1068178356Ssam}
1069178356Ssam
1070197147Shrs# childif_destroy
1071197147Shrs#	Destroy child interfaces.
1072178356Ssam#
1073178356Ssamchildif_destroy()
1074178356Ssam{
1075201215Sjhb	local cfg child child_vlans child_wlans ifn
1076197139Shrs	cfg=1
1077178356Ssam
1078201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1079178527Sbrooks	for child in ${child_wlans}; do
1080201215Sjhb		if ! ifexists $child; then
1081201215Sjhb			continue
1082201215Sjhb		fi
1083208213Sjhb		ifconfig -n $child destroy && cfg=0
1084178356Ssam	done
1085197139Shrs
1086201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1087201215Sjhb	for child in ${child_vlans}; do
1088201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1089201215Sjhb			child="${ifn}.${child}"
1090201215Sjhb		fi
1091201215Sjhb		if ! ifexists $child; then
1092201215Sjhb			continue
1093201215Sjhb		fi
1094208213Sjhb		ifconfig -n $child destroy && cfg=0
1095201215Sjhb	done
1096201215Sjhb
1097197139Shrs	return ${cfg}
1098178356Ssam}
1099178356Ssam
1100197147Shrs# ng_mkpeer
1101197147Shrs#	Create netgraph nodes.
1102166583Sflz#
1103197147Shrsng_mkpeer()
1104197147Shrs{
1105166583Sflz	ngctl -f - 2> /dev/null <<EOF
1106166583Sflzmkpeer $*
1107166583Sflzmsg dummy nodeinfo
1108166583SflzEOF
1109166583Sflz}
1110166583Sflz
1111197147Shrs# ng_create_one
1112197147Shrs#	Create netgraph nodes.
1113197147Shrs#
1114197147Shrsng_create_one()
1115197147Shrs{
1116197139Shrs	local t
1117197139Shrs
1118166583Sflz	ng_mkpeer $* | while read line; do
1119166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1120166583Sflz		if [ -n "${t}" ]; then
1121166583Sflz			echo ${t}
1122166583Sflz			return
1123166583Sflz		fi
1124166583Sflz	done
1125166583Sflz}
1126166583Sflz
1127197147Shrs# gif_up
1128197147Shrs#	Create gif(4) tunnel interfaces.
1129197147Shrsgif_up()
1130197147Shrs{
1131197139Shrs	local i peers
1132197139Shrs
1133166583Sflz	for i in ${gif_interfaces}; do
1134166583Sflz		peers=`get_if_var $i gifconfig_IF`
1135166583Sflz		case ${peers} in
1136166583Sflz		'')
1137166583Sflz			continue
1138166583Sflz			;;
1139166583Sflz		*)
1140177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1141177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1142177682Sbrooks			else
1143177682Sbrooks				gif=`ifconfig gif create`
1144177682Sbrooks				ifconfig $gif name $i
1145177682Sbrooks			fi
1146166583Sflz			ifconfig $i tunnel ${peers}
1147166583Sflz			ifconfig $i up
1148166583Sflz			;;
1149166583Sflz		esac
1150166583Sflz	done
1151166583Sflz}
1152166583Sflz
1153166583Sflz# ng_fec_create ifn
1154197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1155197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1156197139Shrsng_fec_create()
1157197139Shrs{
1158166583Sflz	 local req_iface iface bogus
1159166583Sflz	 req_iface="$1"
1160166583Sflz
1161166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1162166583Sflz
1163166583Sflz	 bogus=""
1164166583Sflz	 while true; do
1165166583Sflz		 iface=`ng_create_one fec dummy fec`
1166166583Sflz		 if [ -z "${iface}" ]; then
1167166583Sflz			 exit 2
1168166583Sflz		 fi
1169166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1170166583Sflz			 break
1171166583Sflz		 fi
1172166583Sflz		 bogus="${bogus} ${iface}"
1173166583Sflz	 done
1174166583Sflz
1175166583Sflz	 for iface in ${bogus}; do
1176166583Sflz		 ngctl shutdown ${iface}:
1177166583Sflz	 done
1178166583Sflz}
1179166583Sflz
1180197147Shrs# fec_up
1181197147Shrs#	Create Fast EtherChannel interfaces.
1182197147Shrsfec_up()
1183197147Shrs{
1184197139Shrs	local i j
1185197139Shrs
1186166583Sflz	for i in ${fec_interfaces}; do
1187166583Sflz		ng_fec_create $i
1188166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1189166583Sflz			case ${j} in
1190100282Sdougb			'')
1191100282Sdougb				continue
1192100282Sdougb				;;
1193100282Sdougb			*)
1194166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1195100282Sdougb				;;
1196100282Sdougb			esac
1197100282Sdougb		done
1198166583Sflz	done
1199100282Sdougb}
1200100282Sdougb
1201113674Smtm# ipx_up ifn
1202197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1203197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1204113674Smtm#
1205113674Smtmipx_up()
1206100280Sgordon{
1207197139Shrs	local ifn
1208113674Smtm	ifn="$1"
1209197139Shrs
1210197139Shrs	# ifconfig_IF_ipx
1211197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1212113674Smtm	if [ -n "${ifconfig_args}" ]; then
1213113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1214113674Smtm		return 0
121585831Sdes	fi
1216197139Shrs
1217113674Smtm	return 1
1218113674Smtm}
121985831Sdes
1220116029Smtm# ipx_down ifn
1221116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1222116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1223113674Smtm#
1224116029Smtmipx_down()
1225116029Smtm{
1226197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1227197139Shrs	_if=$1
1228116100Smtm	_ifs="^"
1229116100Smtm	_ret=1
1230197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1231197139Shrs	oldifs="$IFS"
1232116100Smtm
1233116100Smtm	IFS="$_ifs"
1234116100Smtm	for _ipx in $ipxList ; do
1235116100Smtm		# get rid of extraneous line
1236116100Smtm		[ -z "$_ipx" ] && break
1237116100Smtm
1238116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1239116100Smtm
1240116100Smtm		IFS="$oldifs"
1241197139Shrs		ifconfig ${_if} ${_ipx} delete
1242116100Smtm		IFS="$_ifs"
1243116100Smtm		_ret=0
1244116100Smtm	done
1245116100Smtm	IFS="$oldifs"
1246116100Smtm
1247116100Smtm	return $_ret
1248116029Smtm}
1249116029Smtm
1250137070Spjd# ifnet_rename
1251137070Spjd#	Rename all requested interfaces.
1252116029Smtm#
1253137070Spjdifnet_rename()
1254137070Spjd{
1255197139Shrs	local _if _ifname
1256137070Spjd
1257197139Shrs	# ifconfig_IF_name
1258197139Shrs	for _if in `ifconfig -l`; do
1259157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1260137070Spjd		if [ ! -z "$_ifname" ]; then
1261137070Spjd			ifconfig $_if name $_ifname
1262137070Spjd		fi
1263137070Spjd	done
1264197139Shrs
1265137070Spjd	return 0
1266137070Spjd}
1267137070Spjd
1268113674Smtm# list_net_interfaces type
1269113674Smtm#	List all network interfaces. The type of interface returned
1270113674Smtm#	can be controlled by the type argument. The type
1271113674Smtm#	argument can be any of the following:
1272197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1273197147Shrs#		dhcp	- list only DHCP configured interfaces
1274197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1275197139Shrs#				  Address Autoconf configured interfaces
1276197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1277197139Shrs#				  configured interfaces
1278113674Smtm#	If no argument is specified all network interfaces are output.
1279134429Syar#	Note that the list will include cloned interfaces if applicable.
1280134429Syar#	Cloned interfaces must already exist to have a chance to appear
1281134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1282113674Smtm#
1283113674Smtmlist_net_interfaces()
1284113674Smtm{
1285197139Shrs	local type _tmplist _list _autolist _lo _if
1286113674Smtm	type=$1
128765532Snectar
1288149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
128951231Ssheldonh	#
1290197139Shrs	_tmplist=
129151231Ssheldonh	case ${network_interfaces} in
129251231Ssheldonh	[Aa][Uu][Tt][Oo])
1293149401Sbrooks		_autolist="`ifconfig -l`"
1294149726Sbrooks		_lo=
1295149401Sbrooks		for _if in ${_autolist} ; do
1296149401Sbrooks			if autoif $_if; then
1297149726Sbrooks				if [ "$_if" = "lo0" ]; then
1298149726Sbrooks					_lo="lo0 "
1299149726Sbrooks				else
1300197139Shrs					_tmplist="${_tmplist} ${_if}"
1301149726Sbrooks				fi
1302149401Sbrooks			fi
1303149401Sbrooks		done
1304197139Shrs		_tmplist="${_lo}${_tmplist# }"
130551231Ssheldonh		;;
130683677Sbrooks	*)
1307149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1308196478Sdougb
1309196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1310196478Sdougb		#
1311196478Sdougb		case "$_tmplist" in
1312196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1313196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1314196478Sdougb		esac
131583677Sbrooks		;;
131651231Ssheldonh	esac
131749122Sbrian
1318197139Shrs	_list=
1319197139Shrs	case "$type" in
1320197139Shrs	nodhcp)
1321197139Shrs		for _if in ${_tmplist} ; do
1322197139Shrs			if ! dhcpif $_if && \
1323197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1324197139Shrs				_list="${_list# } ${_if}"
1325197147Shrs			fi
1326197139Shrs		done
1327197139Shrs		;;
1328197139Shrs	dhcp)
1329197147Shrs		for _if in ${_tmplist} ; do
1330197147Shrs			if dhcpif $_if; then
1331197139Shrs				_list="${_list# } ${_if}"
1332197147Shrs			fi
1333197147Shrs		done
1334113674Smtm		;;
1335197139Shrs	noautoconf)
1336197139Shrs		for _if in ${_tmplist} ; do
1337197139Shrs			if ! ipv6_autoconfif $_if && \
1338197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1339197139Shrs				_list="${_list# } ${_if}"
1340197139Shrs			fi
1341197139Shrs		done
1342113674Smtm		;;
1343197139Shrs	autoconf)
1344197139Shrs		for _if in ${_tmplist} ; do
1345197139Shrs			if ipv6_autoconfif $_if; then
1346197139Shrs				_list="${_list# } ${_if}"
1347197139Shrs			fi
1348197139Shrs		done
1349197139Shrs		;;
1350197139Shrs	*)
1351197139Shrs		_list=${_tmplist}
1352197139Shrs		;;
1353113674Smtm	esac
1354197139Shrs
1355197139Shrs	echo $_list
1356197139Shrs
1357130151Sschweikh	return 0
135825184Sjkh}
1359114942Sume
1360179003Sbrooks# get_default_if -address_family
1361179003Sbrooks#	Get the interface of the default route for the given address family.
1362179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1363179003Sbrooks#
1364179003Sbrooksget_default_if()
1365179003Sbrooks{
1366197139Shrs	local routeget oldifs defif line
1367197139Shrs	defif=
1368179003Sbrooks	oldifs="$IFS"
1369179003Sbrooks	IFS="
1370179003Sbrooks"
1371197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1372179003Sbrooks		case $line in
1373179003Sbrooks		*interface:*)
1374179003Sbrooks			defif=${line##*: }
1375179003Sbrooks			;;
1376179003Sbrooks		esac
1377179003Sbrooks	done
1378179003Sbrooks	IFS=${oldifs}
1379179003Sbrooks
1380179003Sbrooks	echo $defif
1381179003Sbrooks}
1382179003Sbrooks
1383197147Shrs# hexdigit arg
1384197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1385114942Sumehexdigit()
1386114942Sume{
1387221884Sjilles	printf '%x\n' "$1"
1388114942Sume}
1389114942Sume
1390197147Shrs# hexprint arg
1391197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1392114942Sumehexprint()
1393114942Sume{
1394221884Sjilles	printf '%x\n' "$1"
1395114942Sume}
1396114942Sume
1397196436Sdougbis_wired_interface()
1398196436Sdougb{
1399196436Sdougb	local media
1400196436Sdougb
1401196436Sdougb	case `ifconfig $1 2>/dev/null` in
1402196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1403196436Sdougb	esac
1404196436Sdougb
1405196436Sdougb	test "$media" = "Ethernet"
1406196436Sdougb}
1407196436Sdougb
1408197147Shrs# network6_getladdr if [flag]
1409197147Shrs#	Echo link-local address from $if if any.
1410197147Shrs#	If flag is defined, tentative ones will be excluded.
1411114942Sumenetwork6_getladdr()
1412114942Sume{
1413197139Shrs	local proto addr rest
1414114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1415114942Sume		case ${proto} in
1416114942Sume		inet6)
1417114942Sume			case ${addr} in
1418114942Sume			fe80::*)
1419114942Sume				if [ -z "$2" ]; then
1420114942Sume					echo ${addr}
1421114942Sume					return
1422114942Sume				fi
1423114942Sume				case ${rest} in
1424114942Sume				*tentative*)
1425114942Sume					continue
1426114942Sume					;;
1427114942Sume				*)
1428114942Sume					echo ${addr}
1429114942Sume					return
1430114942Sume				esac
1431114942Sume			esac
1432114942Sume		esac
1433114942Sume	done
1434114942Sume}
1435