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$
2666830Sobrien#
27252015ShrsIFCONFIG_CMD="/sbin/ifconfig"
28273039Sbdrewery: ${netif_ipexpand_max:=2048}
2925184Sjkh
30113674Smtm#
31113674Smtm# Subroutines commonly used from network startup scripts.
32113674Smtm# Requires that rc.conf be loaded first.
33113674Smtm#
3425184Sjkh
35178356Ssam# ifn_start ifn
36197147Shrs#	Bring up and configure an interface.  If some configuration is
37225560Sbrueffer#	applied, print the interface configuration.
38178356Ssam#
39178356Ssamifn_start()
40178356Ssam{
41178356Ssam	local ifn cfg
42178356Ssam	ifn="$1"
43178356Ssam	cfg=1
44178356Ssam
45178356Ssam	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
46178356Ssam
47178356Ssam	ifscript_up ${ifn} && cfg=0
48178356Ssam	ifconfig_up ${ifn} && cfg=0
49256040Shrs	if ! noafif $ifn; then
50256040Shrs		afexists inet && ipv4_up ${ifn} && cfg=0
51256040Shrs		afexists inet6 && ipv6_up ${ifn} && cfg=0
52256040Shrs		afexists ipx && ipx_up ${ifn} && cfg=0
53256040Shrs	fi
54197139Shrs	childif_create ${ifn} && cfg=0
55178356Ssam
56178356Ssam	return $cfg
57178356Ssam}
58178356Ssam
59197139Shrs# ifn_stop ifn
60225560Sbrueffer#	Shutdown and de-configure an interface.  If action is taken,
61197147Shrs#	print the interface name.
62178356Ssam#
63178356Ssamifn_stop()
64178356Ssam{
65178356Ssam	local ifn cfg
66178356Ssam	ifn="$1"
67178356Ssam	cfg=1
68178356Ssam
69197139Shrs	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
70178356Ssam
71256040Shrs	if ! noafif $ifn; then
72256040Shrs		afexists ipx && ipx_down ${ifn} && cfg=0
73256040Shrs		afexists inet6 && ipv6_down ${ifn} && cfg=0
74256040Shrs		afexists inet && ipv4_down ${ifn} && cfg=0
75256040Shrs	fi
76178356Ssam	ifconfig_down ${ifn} && cfg=0
77178356Ssam	ifscript_down ${ifn} && cfg=0
78197139Shrs	childif_destroy ${ifn} && cfg=0
79178356Ssam
80178356Ssam	return $cfg
81178356Ssam}
82178356Ssam
83256255Shrs# ifn_vnetup ifn
84256255Shrs#	Move ifn to the specified vnet jail.
85256255Shrs#
86256255Shrsifn_vnetup()
87256255Shrs{
88256255Shrs
89256255Shrs	ifn_vnet0 $1 vnet
90256255Shrs}
91256255Shrs
92256255Shrs# ifn_vnetdown ifn
93256255Shrs#	Reclaim ifn from the specified vnet jail.
94256255Shrs#
95256255Shrsifn_vnetdown()
96256255Shrs{
97256255Shrs
98256255Shrs	ifn_vnet0 $1 -vnet
99256255Shrs}
100256255Shrs
101256255Shrs# ifn_vnet0 ifn action
102256255Shrs#	Helper function for ifn_vnetup and ifn_vnetdown.
103256255Shrs#
104256255Shrsifn_vnet0()
105256255Shrs{
106256255Shrs	local _ifn _cfg _action _vnet
107256255Shrs	_ifn="$1"
108256255Shrs	_action="$2"
109256255Shrs	_cfg=1
110256255Shrs
111256255Shrs	if _vnet=$(vnetif $_ifn); then
112256255Shrs		${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
113256255Shrs	fi
114256255Shrs
115256255Shrs	return $_cfg
116256255Shrs}
117256255Shrs
118113674Smtm# ifconfig_up if
119113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
120113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
121113674Smtm#	arguments were found and executed or 1 if the interface
122147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
123147088Sbrooks#	here.
124113674Smtm#
125113674Smtmifconfig_up()
126113674Smtm{
127197139Shrs	local _cfg _ipv6_opts ifconfig_args
128147088Sbrooks	_cfg=1
129147088Sbrooks
130222515Sbz	# Make sure lo0 always comes up.
131222515Sbz	if [ "$1" = "lo0" ]; then
132222515Sbz		_cfg=0
133222515Sbz	fi
134222515Sbz
135197139Shrs	# inet6 specific
136256040Shrs	if ! noafif $1 && afexists inet6; then
137222733Shrs		if checkyesno ipv6_activate_all_interfaces; then
138222733Shrs			_ipv6_opts="-ifdisabled"
139222746Shrs		elif [ "$1" != "lo0" ]; then
140222733Shrs			_ipv6_opts="ifdisabled"
141212574Shrs		fi
142197139Shrs
143222733Shrs		# backward compatibility: $ipv6_enable
144222733Shrs		case $ipv6_enable in
145222733Shrs		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
146253520Shrs			case $1 in
147253520Shrs			bridge[0-9]*)
148253520Shrs				# No accept_rtadv by default on if_bridge(4)
149253520Shrs				# to avoid a conflict with the member
150253520Shrs				# interfaces.
151253520Shrs			;;
152253520Shrs			*)
153253520Shrs				if ! checkyesno ipv6_gateway_enable; then
154253520Shrs					_ipv6_opts="${_ipv6_opts} accept_rtadv"
155253520Shrs				fi
156253520Shrs			;;
157253520Shrs			esac
158230453Shrs		;;
159222733Shrs		esac
160222733Shrs
161225521Shrs		case $ipv6_cpe_wanif in
162225521Shrs		$1)
163225521Shrs			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
164225521Shrs		;;
165225521Shrs		esac
166225521Shrs
167212574Shrs		if [ -n "${_ipv6_opts}" ]; then
168252015Shrs			${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
169197526Shrs		fi
170273188Shrs	fi
171212574Shrs
172273188Shrs	# ifconfig_IF
173273188Shrs	ifconfig_args=`ifconfig_getargs $1`
174273188Shrs	if [ -n "${ifconfig_args}" ]; then
175273188Shrs		eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
176273188Shrs		_cfg=0
177273188Shrs	fi
178273188Shrs
179273188Shrs	# inet6 specific
180273188Shrs	if ! noafif $1 && afexists inet6; then
181212574Shrs		# ifconfig_IF_ipv6
182212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
183212574Shrs		if [ -n "${ifconfig_args}" ]; then
184225522Shrs			# backward compatibility: inet6 keyword
185225522Shrs			case "${ifconfig_args}" in
186225522Shrs			:*|[0-9a-fA-F]*:*)
187269035Sdteske				warn "\$ifconfig_$1_ipv6 needs leading" \
188225522Shrs				    "\"inet6\" keyword for an IPv6 address."
189225522Shrs				ifconfig_args="inet6 ${ifconfig_args}"
190225522Shrs			;;
191225522Shrs			esac
192252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
193252015Shrs			eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
194212574Shrs			_cfg=0
195212574Shrs		fi
196212574Shrs
197230453Shrs		# $ipv6_prefix_IF will be handled in
198230453Shrs		# ipv6_prefix_hostid_addr_common().
199230453Shrs		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
200230453Shrs		if [ -n "${ifconfig_args}" ]; then
201252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
202230453Shrs			_cfg=0
203230453Shrs		fi
204230453Shrs
205229783Suqs		# backward compatibility: $ipv6_ifconfig_IF
206212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
207212574Shrs		if [ -n "${ifconfig_args}" ]; then
208212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
209212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
210252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
211252015Shrs			eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
212212574Shrs			_cfg=0
213212574Shrs		fi
214197139Shrs	fi
215197139Shrs
216252426Shrs	ifalias $1 link alias
217252426Shrs	ifalias $1 ether alias
218252426Shrs
219197139Shrs	if [ ${_cfg} -eq 0 ]; then
220252015Shrs		${IFCONFIG_CMD} $1 up
221197139Shrs	fi
222197139Shrs
223147088Sbrooks	if wpaif $1; then
224147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
225147088Sbrooks		_cfg=0		# XXX: not sure this should count
226252230Srpaulo	elif hostapif $1; then
227252230Srpaulo		/etc/rc.d/hostapd start $1
228252230Srpaulo		_cfg=0
229147088Sbrooks	fi
230147088Sbrooks
231147088Sbrooks	if dhcpif $1; then
232149726Sbrooks		if [ $_cfg -ne 0 ] ; then
233252015Shrs			${IFCONFIG_CMD} $1 up
234149726Sbrooks		fi
235157706Sbrooks		if syncdhcpif $1; then
236157706Sbrooks			/etc/rc.d/dhclient start $1
237157706Sbrooks		fi
238147088Sbrooks		_cfg=0
239147088Sbrooks	fi
240147088Sbrooks
241147121Sbrooks	return $_cfg
242113674Smtm}
24325184Sjkh
244116029Smtm# ifconfig_down if
245161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
246161386Sbrooks#	the interface exists.
247116029Smtm#
248116029Smtmifconfig_down()
249116029Smtm{
250197139Shrs	local _cfg
251147121Sbrooks	_cfg=1
252116029Smtm
253147088Sbrooks	if wpaif $1; then
254147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
255147121Sbrooks		_cfg=0
256252230Srpaulo	elif hostapif $1; then
257252230Srpaulo		/etc/rc.d/hostapd stop $1
258252230Srpaulo		_cfg=0
259147088Sbrooks	fi
260147088Sbrooks
261147088Sbrooks	if dhcpif $1; then
262147088Sbrooks		/etc/rc.d/dhclient stop $1
263147088Sbrooks		_cfg=0
264147088Sbrooks	fi
265147088Sbrooks
266161386Sbrooks	if ifexists $1; then
267252015Shrs		${IFCONFIG_CMD} $1 down
268161386Sbrooks		_cfg=0
269161386Sbrooks	fi
270157706Sbrooks
271147121Sbrooks	return $_cfg
272116029Smtm}
273116029Smtm
274157706Sbrooks# get_if_var if var [default]
275197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
276197147Shrs#	$var is a string containg the sub-string "IF" which will be
277197147Shrs#	replaced with $if after the characters defined in _punct are
278197147Shrs#	replaced with '_'. If the variable is unset, replace it with
279197147Shrs#	$default if given.
280157706Sbrooksget_if_var()
281157706Sbrooks{
282212578Shrs	local _if _punct _punct_c _var _default prefix suffix
283197139Shrs
284157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
285157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
286157706Sbrooks	fi
287157706Sbrooks
288157706Sbrooks	_if=$1
289264438Sdteske	_punct=".-/+"
290264438Sdteske	ltr ${_if} "${_punct}" '_' _if
291157706Sbrooks	_var=$2
292157706Sbrooks	_default=$3
293157706Sbrooks
294157706Sbrooks	prefix=${_var%%IF*}
295157706Sbrooks	suffix=${_var##*IF}
296168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
297157706Sbrooks}
298157706Sbrooks
299197139Shrs# _ifconfig_getargs if [af]
300225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
301225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
302147088Sbrooks#	outside this file.
303147088Sbrooks_ifconfig_getargs()
304147088Sbrooks{
305212574Shrs	local _ifn _af
306147088Sbrooks	_ifn=$1
307197139Shrs	_af=${2+_$2}
308197139Shrs
309147088Sbrooks	if [ -z "$_ifn" ]; then
310147088Sbrooks		return 1
311147088Sbrooks	fi
312147088Sbrooks
313212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
314147088Sbrooks}
315147088Sbrooks
316197139Shrs# ifconfig_getargs if [af]
317147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
318147088Sbrooks#	args such as DHCP and WPA.
319147088Sbrooksifconfig_getargs()
320147088Sbrooks{
321256255Shrs	local _tmpargs _arg _args _vnet
322197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
323147088Sbrooks	if [ $? -eq 1 ]; then
324147088Sbrooks		return 1
325147088Sbrooks	fi
326147088Sbrooks	_args=
327256255Shrs	_vnet=0
328147088Sbrooks
329147088Sbrooks	for _arg in $_tmpargs; do
330256255Shrs		case $_arg:$_vnet in
331256255Shrs		[Dd][Hh][Cc][Pp]:0) ;;
332256255Shrs		[Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
333256255Shrs		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
334256255Shrs		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
335256255Shrs		[Ww][Pp][Aa]:0) ;;
336256255Shrs		[Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
337256255Shrs		vnet:0)	_vnet=1 ;;
338256255Shrs		*:1)	_vnet=0 ;;
339256255Shrs		*:0)
340147088Sbrooks			_args="$_args $_arg"
341256255Shrs		;;
342147088Sbrooks		esac
343147088Sbrooks	done
344147088Sbrooks
345147088Sbrooks	echo $_args
346147088Sbrooks}
347147088Sbrooks
348149401Sbrooks# autoif
349225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
350149401Sbrooks#	boot time and 1 otherwise.
351149401Sbrooksautoif()
352149401Sbrooks{
353197139Shrs	local _tmpargs _arg
354149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
355197139Shrs
356149401Sbrooks	for _arg in $_tmpargs; do
357149401Sbrooks		case $_arg in
358149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
359149401Sbrooks			return 1
360149401Sbrooks			;;
361149401Sbrooks		esac
362149401Sbrooks	done
363197139Shrs
364149401Sbrooks	return 0
365149401Sbrooks}
366149401Sbrooks
367147088Sbrooks# dhcpif if
368147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
369147088Sbrooksdhcpif()
370147088Sbrooks{
371197139Shrs	local _tmpargs _arg
372147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
373197139Shrs
374252360Sdelphij	case $1 in
375252360Sdelphij	lo[0-9]*|\
376252360Sdelphij	stf[0-9]*|\
377252360Sdelphij	faith[0-9]*|\
378252360Sdelphij	lp[0-9]*|\
379252360Sdelphij	sl[0-9]*)
380252360Sdelphij		return 1
381252360Sdelphij		;;
382252360Sdelphij	esac
383225849Sdelphij	if noafif $1; then
384225849Sdelphij		return 1
385225849Sdelphij	fi
386225849Sdelphij
387147088Sbrooks	for _arg in $_tmpargs; do
388147088Sbrooks		case $_arg in
389147088Sbrooks		[Dd][Hh][Cc][Pp])
390147088Sbrooks			return 0
391147088Sbrooks			;;
392157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
393157706Sbrooks			return 0
394157706Sbrooks			;;
395157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
396157706Sbrooks			return 0
397157706Sbrooks			;;
398147088Sbrooks		esac
399147088Sbrooks	done
400197139Shrs
401147088Sbrooks	return 1
402147088Sbrooks}
403147088Sbrooks
404157706Sbrooks# syncdhcpif
405157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
406157706Sbrooks#	1 otherwise.
407157706Sbrookssyncdhcpif()
408157706Sbrooks{
409197139Shrs	local _tmpargs _arg
410157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
411197139Shrs
412225849Sdelphij	if noafif $1; then
413225849Sdelphij		return 1
414225849Sdelphij	fi
415225849Sdelphij
416157706Sbrooks	for _arg in $_tmpargs; do
417157706Sbrooks		case $_arg in
418157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
419157706Sbrooks			return 1
420157706Sbrooks			;;
421157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
422157706Sbrooks			return 0
423157706Sbrooks			;;
424157706Sbrooks		esac
425157706Sbrooks	done
426197139Shrs
427197139Shrs	checkyesno synchronous_dhclient
428157706Sbrooks}
429157706Sbrooks
430147088Sbrooks# wpaif if
431147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
432147088Sbrookswpaif()
433147088Sbrooks{
434197139Shrs	local _tmpargs _arg
435147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
436197139Shrs
437147088Sbrooks	for _arg in $_tmpargs; do
438147088Sbrooks		case $_arg in
439147088Sbrooks		[Ww][Pp][Aa])
440147088Sbrooks			return 0
441147088Sbrooks			;;
442147088Sbrooks		esac
443147088Sbrooks	done
444197139Shrs
445147088Sbrooks	return 1
446147088Sbrooks}
447147088Sbrooks
448252230Srpaulo# hostapif if
449252230Srpaulo#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
450252230Srpaulohostapif()
451252230Srpaulo{
452252230Srpaulo	local _tmpargs _arg
453252230Srpaulo	_tmpargs=`_ifconfig_getargs $1`
454252230Srpaulo
455252230Srpaulo	for _arg in $_tmpargs; do
456252230Srpaulo		case $_arg in
457252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp])
458252230Srpaulo			return 0
459252230Srpaulo			;;
460252230Srpaulo		esac
461252230Srpaulo	done
462252230Srpaulo
463252230Srpaulo	return 1
464252230Srpaulo}
465252230Srpaulo
466256255Shrs# vnetif if
467256255Shrs#	Returns 0 and echo jail if "vnet" keyword is specified on the
468256255Shrs#	interface, and 1 otherwise.
469256255Shrsvnetif()
470256255Shrs{
471256255Shrs	local _tmpargs _arg _vnet
472256255Shrs	_tmpargs=`_ifconfig_getargs $1`
473256255Shrs
474256255Shrs	_vnet=0
475256255Shrs	for _arg in $_tmpargs; do
476256255Shrs		case $_arg:$_vnet in
477256255Shrs		vnet:0)	_vnet=1 ;;
478256255Shrs		*:1)	echo $_arg; return 0 ;;
479256255Shrs		esac
480256255Shrs	done
481256255Shrs
482256255Shrs	return 1
483256255Shrs}
484256255Shrs
485197139Shrs# afexists af
486197139Shrs#	Returns 0 if the address family is enabled in the kernel
487197139Shrs#	1 otherwise.
488197139Shrsafexists()
489197139Shrs{
490197139Shrs	local _af
491197139Shrs	_af=$1
492197139Shrs
493197139Shrs	case ${_af} in
494222996Shrs	inet|inet6)
495222996Shrs		check_kern_features ${_af}
496197139Shrs		;;
497197697Shrs	ipx)
498197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
499197697Shrs		;;
500197697Shrs	atm)
501197697Shrs		if [ -x /sbin/atmconfig ]; then
502197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
503197697Shrs		else
504197697Shrs			return 1
505197697Shrs		fi
506197697Shrs		;;
507252426Shrs	link|ether)
508252426Shrs		return 0
509252426Shrs		;;
510197139Shrs	*)
511197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
512197139Shrs		;;
513197139Shrs	esac
514197139Shrs}
515197139Shrs
516212574Shrs# noafif if
517212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
518212574Shrsnoafif()
519212574Shrs{
520212574Shrs	local _if
521212574Shrs	_if=$1
522212574Shrs
523212574Shrs	case $_if in
524212574Shrs	pflog[0-9]*|\
525212574Shrs	pfsync[0-9]*|\
526256040Shrs	usbus[0-9]*|\
527212574Shrs	an[0-9]*|\
528212574Shrs	ath[0-9]*|\
529212574Shrs	ipw[0-9]*|\
530212577Shrs	ipfw[0-9]*|\
531212574Shrs	iwi[0-9]*|\
532212574Shrs	iwn[0-9]*|\
533212574Shrs	ral[0-9]*|\
534212574Shrs	wi[0-9]*|\
535212574Shrs	wl[0-9]*|\
536212574Shrs	wpi[0-9]*)
537212574Shrs		return 0
538212574Shrs		;;
539212574Shrs	esac
540212574Shrs
541212574Shrs	return 1
542212574Shrs}
543212574Shrs
544162490Sbrooks# ipv6if if
545162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
546162490Sbrooks#	1 otherwise.
547162490Sbrooksipv6if()
548162490Sbrooks{
549212574Shrs	local _if _tmpargs i
550212574Shrs	_if=$1
551212574Shrs
552197139Shrs	if ! afexists inet6; then
553162490Sbrooks		return 1
554162490Sbrooks	fi
555197139Shrs
556197139Shrs	# lo0 is always IPv6-enabled
557212574Shrs	case $_if in
558197139Shrs	lo0)
559197139Shrs		return 0
560197139Shrs		;;
561197139Shrs	esac
562197139Shrs
563212574Shrs	case "${ipv6_network_interfaces}" in
564212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
565212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
566212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
567212575Shrs		if [ -n "${_tmpargs}" ]; then
568212575Shrs			return 0
569212575Shrs		fi
570197139Shrs
571230453Shrs		# True if $ipv6_prefix_IF is defined.
572230453Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
573230453Shrs		if [ -n "${_tmpargs}" ]; then
574230453Shrs			return 0
575230453Shrs		fi
576230453Shrs
577212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
578212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
579212575Shrs		if [ -n "${_tmpargs}" ]; then
580212574Shrs			return 0
581212574Shrs		fi
582212575Shrs		;;
583212575Shrs	esac
584197139Shrs
585162490Sbrooks	return 1
586162490Sbrooks}
587162490Sbrooks
588197139Shrs# ipv6_autoconfif if
589197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
590225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
591197139Shrsipv6_autoconfif()
592197139Shrs{
593197139Shrs	local _if _tmpargs _arg
594197139Shrs	_if=$1
595197139Shrs
596212577Shrs	case $_if in
597252360Sdelphij	lo[0-9]*|\
598212577Shrs	stf[0-9]*|\
599212577Shrs	faith[0-9]*|\
600212577Shrs	lp[0-9]*|\
601212577Shrs	sl[0-9]*)
602197139Shrs		return 1
603212577Shrs		;;
604212577Shrs	esac
605212574Shrs	if noafif $_if; then
606212574Shrs		return 1
607212574Shrs	fi
608212577Shrs	if ! ipv6if $_if; then
609212577Shrs		return 1
610212577Shrs	fi
611197139Shrs	if checkyesno ipv6_gateway_enable; then
612197139Shrs		return 1
613197139Shrs	fi
614197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
615197526Shrs	if [ -n "${_tmpargs}" ]; then
616197526Shrs		return 1
617197526Shrs	fi
618212574Shrs	# backward compatibility: $ipv6_enable
619212574Shrs	case $ipv6_enable in
620212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
621242181Shrs		if checkyesno ipv6_gateway_enable; then
622242181Shrs			return 1
623253520Shrs		fi
624253520Shrs		case $1 in
625253520Shrs		bridge[0-9]*)
626253520Shrs			# No accept_rtadv by default on if_bridge(4)
627253520Shrs			# to avoid a conflict with the member
628253520Shrs			# interfaces.
629253520Shrs			return 1
630253520Shrs		;;
631253520Shrs		*)
632242181Shrs			return 0
633253520Shrs		;;
634253520Shrs		esac
635212577Shrs	;;
636197526Shrs	esac
637197526Shrs
638212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
639212574Shrs	for _arg in $_tmpargs; do
640212574Shrs		case $_arg in
641212574Shrs		accept_rtadv)
642212574Shrs			return 0
643212574Shrs			;;
644212574Shrs		esac
645212574Shrs	done
646212574Shrs
647212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
648212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
649212574Shrs	for _arg in $_tmpargs; do
650212574Shrs		case $_arg in
651212574Shrs		accept_rtadv)
652212574Shrs			return 0
653212574Shrs			;;
654212574Shrs		esac
655212574Shrs	done
656212574Shrs
657197139Shrs	return 1
658197139Shrs}
659197139Shrs
660161386Sbrooks# ifexists if
661161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
662161386Sbrooksifexists()
663161386Sbrooks{
664197139Shrs	[ -z "$1" ] && return 1
665252015Shrs	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
666161386Sbrooks}
667161386Sbrooks
668152441Sbrooks# ipv4_up if
669212578Shrs#	add IPv4 addresses to the interface $if
670152441Sbrooksipv4_up()
671152441Sbrooks{
672197139Shrs	local _if _ret
673152441Sbrooks	_if=$1
674197139Shrs	_ret=1
675197139Shrs
676222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
677222515Sbz	if [ "${_if}" = "lo0" ]; then
678226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
679222515Sbz		if [ -z "${ifconfig_args}" ]; then
680252015Shrs			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
681222515Sbz		fi
682222515Sbz	fi
683252015Shrs	ifalias ${_if} inet alias && _ret=0
684197139Shrs
685197139Shrs	return $_ret
686152441Sbrooks}
687152441Sbrooks
688197139Shrs# ipv6_up if
689197139Shrs#	add IPv6 addresses to the interface $if
690197139Shrsipv6_up()
691197139Shrs{
692197139Shrs	local _if _ret
693197139Shrs	_if=$1
694197139Shrs	_ret=1
695197139Shrs
696197139Shrs	if ! ipv6if $_if; then
697197139Shrs		return 0
698197139Shrs	fi
699197139Shrs
700252015Shrs	ifalias ${_if} inet6 alias && _ret=0
701226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
702197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
703197139Shrs
704197139Shrs	return $_ret
705197139Shrs}
706197139Shrs
707152441Sbrooks# ipv4_down if
708197147Shrs#	remove IPv4 addresses from the interface $if
709152441Sbrooksipv4_down()
710152441Sbrooks{
711197139Shrs	local _if _ifs _ret inetList oldifs _inet
712152441Sbrooks	_if=$1
713161386Sbrooks	_ifs="^"
714161386Sbrooks	_ret=1
715161386Sbrooks
716252015Shrs	ifalias ${_if} inet -alias && _ret=0
717161386Sbrooks
718255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
719252015Shrs
720161386Sbrooks	oldifs="$IFS"
721161386Sbrooks	IFS="$_ifs"
722161386Sbrooks	for _inet in $inetList ; do
723161386Sbrooks		# get rid of extraneous line
724253924Shrs		case $_inet in
725255653Shrs		inet\ *)	;;
726255653Shrs		*)		continue ;;
727253924Shrs		esac
728161386Sbrooks
729161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
730161386Sbrooks
731161386Sbrooks		IFS="$oldifs"
732252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet} delete
733161386Sbrooks		IFS="$_ifs"
734161386Sbrooks		_ret=0
735161386Sbrooks	done
736161386Sbrooks	IFS="$oldifs"
737161386Sbrooks
738161386Sbrooks	return $_ret
739152441Sbrooks}
740152441Sbrooks
741197139Shrs# ipv6_down if
742197139Shrs#	remove IPv6 addresses from the interface $if
743197139Shrsipv6_down()
744197139Shrs{
745197139Shrs	local _if _ifs _ret inetList oldifs _inet6
746197139Shrs	_if=$1
747197139Shrs	_ifs="^"
748197139Shrs	_ret=1
749197139Shrs
750197139Shrs	if ! ipv6if $_if; then
751197139Shrs		return 0
752197139Shrs	fi
753197139Shrs
754197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
755226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
756252015Shrs	ifalias ${_if} inet6 -alias && _ret=0
757197139Shrs
758255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
759197139Shrs
760197139Shrs	oldifs="$IFS"
761197139Shrs	IFS="$_ifs"
762197139Shrs	for _inet6 in $inetList ; do
763197139Shrs		# get rid of extraneous line
764255653Shrs		case $_inet in
765255653Shrs		inet6\ *)	;;
766255653Shrs		*)		continue ;;
767255653Shrs		esac
768197139Shrs
769197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
770197139Shrs
771197139Shrs		IFS="$oldifs"
772252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
773197139Shrs		IFS="$_ifs"
774197139Shrs		_ret=0
775197139Shrs	done
776197139Shrs	IFS="$oldifs"
777197139Shrs
778197139Shrs	return $_ret
779197139Shrs}
780197139Shrs
781252015Shrs# ifalias if af action
782252015Shrs#	Configure or remove aliases for network interface $if.
783113674Smtm#	It returns 0 if at least one alias was configured or
784252015Shrs#	removed, or 1 if there were none.
785113674Smtm#
786252015Shrsifalias()
787113674Smtm{
788197139Shrs	local _ret
789113674Smtm	_ret=1
790197139Shrs
791252015Shrs	afexists $2 || return $_ret
792252015Shrs
793197139Shrs	case "$2" in
794252426Shrs	inet|inet6|link|ether)
795252015Shrs		ifalias_af_common $1 $2 $3 && _ret=0
796197139Shrs		;;
797197139Shrs	esac
798197139Shrs
799197139Shrs	return $_ret
800197139Shrs}
801197139Shrs
802252015Shrs# ifalias_expand_addr af action addr
803252015Shrs#	Expand address range ("N-M") specification in addr.
804252015Shrs#	"addr" must not include an address-family keyword.
805252015Shrs#	The results will include an address-family keyword.
806197139Shrs#
807252015Shrsifalias_expand_addr()
808197139Shrs{
809253505Shrs	local _af _action
810197139Shrs
811253505Shrs	_af=$1
812253505Shrs	_action=$2
813253505Shrs	shift 2
814253505Shrs
815253505Shrs	afexists $_af || return
816253505Shrs	ifalias_expand_addr_$_af $_action $*
817197139Shrs}
818197139Shrs
819252015Shrs# ifalias_expand_addr_inet action addr
820252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv4.
821197139Shrs#
822252015Shrsifalias_expand_addr_inet()
823197139Shrs{
824253505Shrs	local _action _arg _cidr _cidr_addr _exargs
825252015Shrs	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
826252015Shrs	local _retstr _c
827252015Shrs	_action=$1
828252015Shrs	_arg=$2
829253505Shrs	shift 2
830253505Shrs	_exargs=$*
831252015Shrs	_retstr=
832197139Shrs
833253505Shrs	case $_action:$_arg:$_exargs in
834253505Shrs	*:*--*)		return ;;	# invalid
835253505Shrs	tmp:*[0-9]-[0-9]*:*)		# to be expanded
836253505Shrs		_action="alias"
837253505Shrs	;;
838253505Shrs	*:*[0-9]-[0-9]*:*)		# to be expanded
839253505Shrs	;;
840253505Shrs	tmp:*:*netmask*)		# already expanded w/ netmask option
841253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
842253505Shrs	;;
843253505Shrs	tmp:*:*)			# already expanded w/o netmask option
844253505Shrs		echo $_arg $_exargs && return
845253505Shrs	;;
846253505Shrs	*:*:*netmask*)			# already expanded w/ netmask option
847253505Shrs		echo inet ${_arg%/[0-9]*} $_exargs && return
848253505Shrs	;;
849253505Shrs	*:*:*)				# already expanded w/o netmask option
850253505Shrs		echo inet $_arg $_exargs && return
851253505Shrs	;;
852252015Shrs	esac
853252015Shrs
854252015Shrs	for _cidr in $_arg; do
855252015Shrs		_ipaddr=${_cidr%%/*}
856252015Shrs		_plen=${_cidr##*/}
857252015Shrs		# When subnet prefix length is not specified, use /32.
858252015Shrs		case $_plen in
859252015Shrs		$_ipaddr)	_plen=32 ;;	# "/" character not found
860197139Shrs		esac
861197139Shrs
862252015Shrs		OIFS=$IFS
863252015Shrs		IFS=. set -- $_ipaddr
864252015Shrs		_range=
865252015Shrs		_iphead=
866252015Shrs		_iptail=
867252015Shrs		for _c in $@; do
868252015Shrs			case $_range:$_c in
869252015Shrs			:[0-9]*-[0-9]*)
870252015Shrs				_range=$_c
871197139Shrs			;;
872252015Shrs			:*)
873252015Shrs				_iphead="${_iphead}${_iphead:+.}${_c}"
874197139Shrs			;;
875252015Shrs			*:*)
876252015Shrs				_iptail="${_iptail}${_iptail:+.}${_c}"
877252015Shrs			;;
878252015Shrs			esac
879252015Shrs		done
880252015Shrs		IFS=$OIFS
881252015Shrs		_iplow=${_range%-*}
882252015Shrs		_iphigh=${_range#*-}
883252015Shrs
884252015Shrs		# clear netmask when removing aliases
885252015Shrs		if [ "$_action" = "-alias" ]; then
886252015Shrs			_plen=""
887252015Shrs		fi
888252015Shrs
889252015Shrs		_ipcount=$_iplow
890252015Shrs		while [ "$_ipcount" -le "$_iphigh" ]; do
891252015Shrs			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
892273039Sbdrewery			if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
893273039Sbdrewery				warn "Range specification is too large (${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_iphigh}${_iptail:+.}${_iptail}).  ${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail} was processed.  Increase \$netif_ipexpand_max in rc.conf."
894252015Shrs				break
895252015Shrs			else
896252015Shrs				_ipcount=$(($_ipcount + 1))
897252015Shrs			fi
898252015Shrs			# Forcibly set /32 for remaining aliases.
899252015Shrs			_plen=32
900252015Shrs		done
901197139Shrs	done
902197139Shrs
903252015Shrs	for _c in $_retstr; do
904253505Shrs		ifalias_expand_addr_inet $_action $_c $_exargs
905252015Shrs	done
906113674Smtm}
907100280Sgordon
908252015Shrs# ifalias_expand_addr_inet6 action addr
909252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv6.
910116029Smtm#
911252015Shrsifalias_expand_addr_inet6()
912116029Smtm{
913253505Shrs	local _action _arg _cidr _cidr_addr _exargs
914252015Shrs	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
915252015Shrs	local _ipv4part
916252015Shrs	local _retstr _c
917252015Shrs	_action=$1
918252015Shrs	_arg=$2
919253505Shrs	shift 2
920253505Shrs	_exargs=$*
921252015Shrs	_retstr=
922197139Shrs
923253505Shrs	case $_action:$_arg:$_exargs in
924253505Shrs	*:*--*:*)	return ;;	# invalid
925253505Shrs	tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
926253505Shrs		_action="alias"
927253505Shrs	;;
928253505Shrs	*:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)	# to be expanded
929253505Shrs	;;
930253505Shrs	tmp:*:*prefixlen*)	# already expanded w/ prefixlen option
931253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
932253505Shrs	;;
933253505Shrs	tmp:*:*)		# already expanded w/o prefixlen option
934253505Shrs		echo $_arg $_exargs && return
935253505Shrs	;;
936253505Shrs	*:*:*prefixlen*)	# already expanded w/ prefixlen option
937253505Shrs		echo inet6 ${_arg%/[0-9]*} $_exargs && return
938253505Shrs	;;
939253505Shrs	*:*:*)			# already expanded w/o prefixlen option
940253505Shrs		echo inet6 $_arg $_exargs && return
941253505Shrs	;;
942197139Shrs	esac
943197139Shrs
944252015Shrs	for _cidr in $_arg; do
945252015Shrs		_ipaddr="${_cidr%%/*}"
946252015Shrs		_plen="${_cidr##*/}"
947252015Shrs
948252015Shrs		case $_action:$_ipaddr:$_cidr in
949252015Shrs		-alias:*:*)		unset _plen ;;
950252015Shrs		*:$_cidr:$_ipaddr)	unset _plen ;;
951252015Shrs		esac
952252015Shrs
953252015Shrs		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
954252015Shrs			# Handle !v4mapped && !v4compat addresses.
955252015Shrs
956252015Shrs			# The default prefix length is 64.
957252015Shrs			case $_ipaddr:$_cidr in
958252015Shrs			$_cidr:$_ipaddr)	_plen="64" ;;
959252015Shrs			esac
960252015Shrs			_ipleft=${_ipaddr%-*}
961252015Shrs			_ipright=${_ipaddr#*-}
962252015Shrs			_iplow=${_ipleft##*:}
963252015Shrs			_iphigh=${_ipright%%:*}
964252015Shrs			_ipleft=${_ipleft%:*}
965252015Shrs			_ipright=${_ipright#*:}
966252015Shrs
967252015Shrs			if [ "$_iphigh" = "$_ipright" ]; then
968252015Shrs				unset _ipright
969252015Shrs			else
970252015Shrs				_ipright=:$_ipright
971252015Shrs			fi
972252015Shrs
973252015Shrs			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
974252015Shrs				_iplow=$((0x$_iplow))
975252015Shrs				_iphigh=$((0x$_iphigh))
976252015Shrs				_ipcount=$_iplow
977252015Shrs				while [ $_ipcount -le $_iphigh ]; do
978252015Shrs					_r=`printf "%s:%04x%s%s" \
979252015Shrs					    $_ipleft $_ipcount $_ipright \
980252015Shrs					    ${_plen:+/}$_plen`
981252015Shrs					_retstr="$_retstr $_r"
982273039Sbdrewery					if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
983252015Shrs					then
984273039Sbdrewery						warn "Range specification is too large $(printf '(%s:%x%s-%s:%x%s)' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_iphigh" "$_ipright"). $(printf '%s:%x%s-%s:%x%s' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_ipcount" "$_ipright") was processed.  Increase \$netif_ipexpand_max in rc.conf."
985252015Shrs						break
986252015Shrs					else
987252015Shrs						_ipcount=$(($_ipcount + 1))
988252015Shrs					fi
989252015Shrs				done
990252015Shrs			else
991252015Shrs				_retstr="${_ipaddr}${_plen:+/}${_plen}"
992252015Shrs			fi
993252015Shrs
994252015Shrs			for _c in $_retstr; do
995253505Shrs				ifalias_expand_addr_inet6 $_action $_c $_exargs
996252015Shrs			done
997252015Shrs		else
998252015Shrs			# v4mapped/v4compat should handle as an IPv4 alias
999252015Shrs			_ipv4part=${_ipaddr##*:}
1000252015Shrs
1001252015Shrs			# Adjust prefix length if any.  If not, set the
1002252015Shrs			# default prefix length as 32.
1003252015Shrs			case $_ipaddr:$_cidr in
1004252015Shrs			$_cidr:$_ipaddr)	_plen=32 ;;
1005252015Shrs			*)			_plen=$(($_plen - 96)) ;;
1006252015Shrs			esac
1007252015Shrs
1008252015Shrs			_retstr=`ifalias_expand_addr_inet \
1009252015Shrs			    tmp ${_ipv4part}${_plen:+/}${_plen}`
1010252015Shrs			for _c in $_retstr; do
1011253505Shrs				ifalias_expand_addr_inet $_action $_c $_exargs
1012252015Shrs			done
1013252015Shrs		fi
1014252015Shrs	done
1015197139Shrs}
1016197139Shrs
1017252015Shrs# ifalias_af_common_handler if af action args
1018252015Shrs#	Helper function for ifalias_af_common().
1019197139Shrs#
1020252015Shrsifalias_af_common_handler()
1021197139Shrs{
1022252015Shrs	local _ret _if _af _action _args _c _tmpargs
1023252015Shrs
1024197139Shrs	_ret=1
1025252015Shrs	_if=$1
1026252015Shrs	_af=$2
1027252015Shrs	_action=$3
1028252015Shrs	shift 3
1029252015Shrs	_args=$*
1030197139Shrs
1031252015Shrs	case $_args in
1032252015Shrs	${_af}\ *)	;;
1033252015Shrs	*)	return	;;
1034252015Shrs	esac
1035252015Shrs
1036252426Shrs	# link(ether) does not support address removal.
1037252426Shrs	case $_af:$_action in
1038252426Shrs	link:-alias|ether:-alias)	return ;;
1039252426Shrs	esac
1040252426Shrs
1041252015Shrs	_tmpargs=
1042252015Shrs	for _c in $_args; do
1043252015Shrs		case $_c in
1044252015Shrs		${_af})
1045252015Shrs			case $_tmpargs in
1046273188Shrs			${_af}\ *[0-9a-fA-F]-*)
1047252015Shrs				ifalias_af_common_handler $_if $_af $_action \
1048252015Shrs				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1049197139Shrs			;;
1050252015Shrs			${_af}\ *)
1051252015Shrs				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1052197139Shrs			;;
1053252015Shrs			esac
1054252015Shrs			_tmpargs=$_af
1055252015Shrs		;;
1056252015Shrs		*)
1057252015Shrs			_tmpargs="$_tmpargs $_c"
1058252015Shrs		;;
1059197139Shrs		esac
1060197139Shrs	done
1061252015Shrs	# Process the last component if any.
1062252015Shrs	if [ -n "$_tmpargs}" ]; then
1063252015Shrs		case $_tmpargs in
1064273188Shrs		${_af}\ *[0-9a-fA-F]-*)
1065252015Shrs			ifalias_af_common_handler $_if $_af $_action \
1066252015Shrs			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1067252015Shrs		;;
1068252015Shrs		${_af}\ *)
1069252015Shrs			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1070252015Shrs		;;
1071252015Shrs		esac
1072252015Shrs	fi
1073197139Shrs
1074197139Shrs	return $_ret
1075197139Shrs}
1076197139Shrs
1077252015Shrs# ifalias_af_common if af action
1078252015Shrs#	Helper function for ifalias().
1079197139Shrs#
1080252015Shrsifalias_af_common()
1081197139Shrs{
1082252015Shrs	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1083269035Sdteske	local _vif _punct=".-/+"
1084252015Shrs
1085197139Shrs	_ret=1
1086252015Shrs	_aliasn=
1087252015Shrs	_if=$1
1088252015Shrs	_af=$2
1089252015Shrs	_action=$3
1090197139Shrs
1091264438Sdteske	# Normalize $_if before using it in a pattern to list_vars()
1092269035Sdteske	ltr "$_if" "$_punct" "_" _vif
1093264438Sdteske
1094252015Shrs	# ifconfig_IF_aliasN which starts with $_af
1095269035Sdteske	for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1096269035Sdteske		sort_lite -nk1.$((9+${#_vif}+7))`
1097264438Sdteske	do
1098264438Sdteske		eval ifconfig_args=\"\$$alias\"
1099252015Shrs		_iaf=
1100252015Shrs		case $ifconfig_args in
1101252015Shrs		inet\ *)	_iaf=inet ;;
1102252015Shrs		inet6\ *)	_iaf=inet6 ;;
1103252015Shrs		ipx\ *)		_iaf=ipx ;;
1104252426Shrs		link\ *)	_iaf=link ;;
1105252426Shrs		ether\ *)	_iaf=ether ;;
1106252015Shrs		esac
1107252015Shrs
1108252015Shrs		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1109252015Shrs		${_af}:*:${_af}:*)
1110252015Shrs			_aliasn="$_aliasn $ifconfig_args"
1111197139Shrs			;;
1112252015Shrs		${_af}:*:"":"")
1113116029Smtm			break
1114197139Shrs			;;
1115252015Shrs		inet:alias:"":*)
1116252015Shrs			_aliasn="$_aliasn inet $ifconfig_args"
1117269035Sdteske			warn "\$${alias} needs leading" \
1118252015Shrs			    "\"inet\" keyword for an IPv4 address."
1119197139Shrs		esac
1120116029Smtm	done
1121197139Shrs
1122197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
1123252015Shrs	case $_af in
1124252015Shrs	inet6)
1125269035Sdteske		for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1126269035Sdteske			sort_lite -nk1.$((14+${#_vif}+7))`
1127264438Sdteske		do
1128264438Sdteske			eval ifconfig_args=\"\$$alias\"
1129252015Shrs			case ${_action}:"${ifconfig_args}" in
1130252015Shrs			*:"")
1131252015Shrs				break
1132197139Shrs			;;
1133252015Shrs			alias:*)
1134252015Shrs				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
1135269035Sdteske				warn "\$${alias} is obsolete. " \
1136269035Sdteske				    "Use ifconfig_${_vif}_aliasN instead."
1137252015Shrs			;;
1138252015Shrs			esac
1139252015Shrs		done
1140252015Shrs	esac
1141252015Shrs
1142252015Shrs	# backward compatibility: ipv4_addrs_IF.
1143252015Shrs	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1144252015Shrs		_aliasn="$_aliasn inet $_tmpargs"
1145252015Shrs	done
1146252015Shrs
1147252015Shrs	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1148252015Shrs	_tmpargs=
1149252015Shrs	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1150252015Shrs		case $_c in
1151252426Shrs		inet|inet6|ipx|link|ether)
1152252015Shrs			case $_tmpargs in
1153252015Shrs			${_af}\ *)
1154252015Shrs				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1155252015Shrs			;;
1156252015Shrs			esac
1157252015Shrs			_tmpargs=$_c
1158252015Shrs		;;
1159197139Shrs		*)
1160252015Shrs			_tmpargs="$_tmpargs $_c"
1161197139Shrs		esac
1162197139Shrs	done
1163252015Shrs	# Process the last component
1164252015Shrs	case $_tmpargs in
1165252015Shrs	${_af}\ *)
1166252015Shrs		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1167252015Shrs	;;
1168252015Shrs	esac
1169197139Shrs
1170116029Smtm	return $_ret
1171116029Smtm}
1172116029Smtm
1173226652Shrs# ipv6_prefix_hostid_addr_common if action
1174226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
1175226652Shrs#
1176226652Shrsipv6_prefix_hostid_addr_common()
1177197139Shrs{
1178253444Shrs	local _if _action prefix j
1179197139Shrs	_if=$1
1180226652Shrs	_action=$2
1181197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1182197139Shrs
1183197139Shrs	if [ -n "${prefix}" ]; then
1184197139Shrs		for j in ${prefix}; do
1185252015Shrs			# The default prefixlen is 64.
1186252015Shrs			plen=${j#*/}
1187252015Shrs			case $j:$plen in
1188252015Shrs			$plen:$j)	plen=64 ;;
1189252015Shrs			*)		j=${j%/*} ;;
1190252015Shrs			esac
1191197139Shrs
1192252015Shrs			# Normalize the last part by removing ":"
1193253444Shrs			j=${j%::*}
1194252015Shrs			j=${j%:}
1195253444Shrs			${IFCONFIG_CMD} ${_if} inet6 $j:: \
1196253444Shrs				prefixlen $plen eui64 ${_action}
1197252015Shrs
1198197139Shrs			# if I am a router, add subnet router
1199197139Shrs			# anycast address (RFC 2373).
1200197139Shrs			if checkyesno ipv6_gateway_enable; then
1201252015Shrs				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1202252015Shrs					prefixlen $plen ${_action} anycast
1203197139Shrs			fi
1204197139Shrs		done
1205197139Shrs	fi
1206197139Shrs}
1207197139Shrs
1208197139Shrs# ipv6_accept_rtadv_up if
1209197139Shrs#	Enable accepting Router Advertisement and send Router
1210197139Shrs#	Solicitation message
1211197139Shrsipv6_accept_rtadv_up()
1212197139Shrs{
1213197139Shrs	if ipv6_autoconfif $1; then
1214252015Shrs		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1215203433Sume		if ! checkyesno rtsold_enable; then
1216203433Sume			rtsol ${rtsol_flags} $1
1217203433Sume		fi
1218197139Shrs	fi
1219197139Shrs}
1220197139Shrs
1221197139Shrs# ipv6_accept_rtadv_down if
1222197139Shrs#	Disable accepting Router Advertisement
1223197139Shrsipv6_accept_rtadv_down()
1224197139Shrs{
1225197139Shrs	if ipv6_autoconfif $1; then
1226252015Shrs		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1227197139Shrs	fi
1228197139Shrs}
1229197139Shrs
1230113674Smtm# ifscript_up if
1231113674Smtm#	Evaluate a startup script for the $if interface.
1232113674Smtm#	It returns 0 if a script was found and processed or
1233113674Smtm#	1 if no script was found.
1234113674Smtm#
1235113674Smtmifscript_up()
1236100280Sgordon{
1237113674Smtm	if [ -r /etc/start_if.$1 ]; then
1238113674Smtm		. /etc/start_if.$1
1239113674Smtm		return 0
1240197139Shrs	else
1241197139Shrs		return 1
1242113674Smtm	fi
1243100280Sgordon}
1244100280Sgordon
1245116029Smtm# ifscript_down if
1246116029Smtm#	Evaluate a shutdown script for the $if interface.
1247116029Smtm#	It returns 0 if a script was found and processed or
1248116029Smtm#	1 if no script was found.
1249116029Smtm#
1250116029Smtmifscript_down()
1251116029Smtm{
1252116029Smtm	if [ -r /etc/stop_if.$1 ]; then
1253116029Smtm		. /etc/stop_if.$1
1254116029Smtm		return 0
1255197139Shrs	else
1256197139Shrs		return 1
1257116029Smtm	fi
1258116029Smtm}
1259116029Smtm
1260197147Shrs# clone_up
1261197147Shrs#	Create cloneable interfaces.
1262113674Smtm#
1263113674Smtmclone_up()
1264100280Sgordon{
1265256039Shrs	local _list ifn ifopt _iflist _n tmpargs
1266113674Smtm	_list=
1267253924Shrs	_iflist=$*
1268197139Shrs
1269197139Shrs	# create_args_IF
1270113674Smtm	for ifn in ${cloned_interfaces}; do
1271253924Shrs		# Parse ifn:ifopt.
1272253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1273253924Shrs		case $_iflist in
1274253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1275253924Shrs		*)	continue ;;
1276253924Shrs		esac
1277256039Shrs		case $ifn in
1278256039Shrs		epair[0-9]*)
1279256039Shrs			# epair(4) uses epair[0-9] for creation and
1280256039Shrs			# epair[0-9][ab] for configuration.
1281256039Shrs			#
1282256039Shrs			# Skip if ${ifn}a or ${ifn}b already exist.
1283256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1284256039Shrs				continue
1285256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1286256039Shrs				continue
1287256039Shrs			fi
1288256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1289256039Shrs			    `get_if_var ${ifn} create_args_IF`
1290256039Shrs			if [ $? -eq 0 ]; then
1291256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1292256039Shrs			fi
1293256039Shrs		;;
1294256039Shrs		*)
1295256039Shrs			# Skip if ${ifn} already exists.
1296256039Shrs			if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1297256039Shrs				continue
1298256039Shrs			fi
1299256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1300256039Shrs			    `get_if_var ${ifn} create_args_IF`
1301256039Shrs			if [ $? -eq 0 ]; then
1302256039Shrs				_list="$_list $ifn"
1303256039Shrs			fi
1304256039Shrs		esac
1305113674Smtm	done
1306253924Shrs	if [ -n "$gif_interfaces" ]; then
1307253924Shrs		warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1308253924Shrs	fi
1309253924Shrs	for ifn in ${gif_interfaces}; do
1310253924Shrs		# Parse ifn:ifopt.
1311253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1312253924Shrs		case $_iflist in
1313253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1314253924Shrs		*)	continue ;;
1315253924Shrs		esac
1316253924Shrs		# Skip if ifn already exists.
1317253924Shrs		if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1318253924Shrs			continue
1319253924Shrs		fi
1320253924Shrs		case $ifn in
1321253924Shrs		gif[0-9]*)
1322253924Shrs			${IFCONFIG_CMD} $ifn create
1323253924Shrs		;;
1324253924Shrs		*)
1325253924Shrs			_n=$(${IFCONFIG_CMD} gif create)
1326253924Shrs			${IFCONFIG_CMD} $_n name $ifn
1327253924Shrs		;;
1328253924Shrs		esac
1329253924Shrs		if [ $? -eq 0 ]; then
1330256039Shrs			_list="$_list $ifn"
1331253924Shrs		fi
1332253924Shrs		tmpargs=$(get_if_var $ifn gifconfig_IF)
1333253924Shrs		eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1334253924Shrs	done
1335256039Shrs	if [ -n "${_list# }" ]; then
1336256039Shrs		echo "Created clone interfaces: ${_list# }."
1337253924Shrs	fi
1338256039Shrs	debug "Cloned: ${_list# }"
1339113674Smtm}
1340100280Sgordon
1341197147Shrs# clone_down
1342197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1343197147Shrs#	standard output.
1344113674Smtm#
1345113674Smtmclone_down()
1346113674Smtm{
1347256039Shrs	local _list ifn _difn ifopt _iflist _sticky
1348113674Smtm	_list=
1349253924Shrs	_iflist=$*
1350197139Shrs
1351253924Shrs	: ${cloned_interfaces_sticky:=NO}
1352253924Shrs	if checkyesno cloned_interfaces_sticky; then
1353253924Shrs		_sticky=1
1354253924Shrs	else
1355253924Shrs		_sticky=0
1356253924Shrs	fi
1357253924Shrs	for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1358253924Shrs		# Parse ifn:ifopt.
1359253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1360253924Shrs		case $ifopt:$_sticky in
1361253924Shrs		sticky:*)	continue ;;	# :sticky => not destroy
1362253924Shrs		nosticky:*)	;;		# :nosticky => destroy
1363253924Shrs		*:1)		continue ;;	# global sticky knob == 1
1364253924Shrs		esac
1365253924Shrs		case $_iflist in
1366253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1367253924Shrs		*)	continue ;;
1368253924Shrs		esac
1369256039Shrs		case $ifn in
1370256039Shrs		epair[0-9]*)
1371256039Shrs			# Note: epair(4) uses epair[0-9] for removal and
1372256039Shrs			# epair[0-9][ab] for configuration.
1373256039Shrs			#
1374256039Shrs			# Skip if both of ${ifn}a and ${ifn}b do not exist.
1375256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1376256039Shrs				_difn=${ifn}a
1377256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1378256039Shrs				_difn=${ifn}b
1379256039Shrs			else
1380256039Shrs				continue
1381256039Shrs			fi
1382256039Shrs			${IFCONFIG_CMD} -n $_difn destroy
1383256039Shrs			if [ $? -eq 0 ]; then
1384256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1385256039Shrs			fi
1386256039Shrs		;;
1387256039Shrs		*)
1388256039Shrs			# Skip if ifn does not exist.
1389256039Shrs			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1390256039Shrs				continue
1391256039Shrs			fi
1392256039Shrs			${IFCONFIG_CMD} -n ${ifn} destroy
1393256039Shrs			if [ $? -eq 0 ]; then
1394256039Shrs				_list="$_list $ifn"
1395256039Shrs			fi
1396256039Shrs		;;
1397256039Shrs		esac
1398113674Smtm	done
1399256039Shrs	if [ -n "${_list# }" ]; then
1400256039Shrs		echo "Destroyed clone interfaces: ${_list# }."
1401253924Shrs	fi
1402256039Shrs	debug "Destroyed clones: ${_list# }"
1403100280Sgordon}
1404100280Sgordon
1405197147Shrs# childif_create
1406197147Shrs#	Create and configure child interfaces.  Return 0 if child
1407197147Shrs#	interfaces are created.
1408178356Ssam#
1409178356Ssamchildif_create()
1410178356Ssam{
1411201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1412178356Ssam	cfg=1
1413178356Ssam	ifn=$1
1414178356Ssam
1415178527Sbrooks	# Create wireless interfaces
1416178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1417178527Sbrooks
1418178527Sbrooks	for child in ${child_wlans}; do
1419183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1420189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1421189759Sbrooks
1422178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1423252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1424189759Sbrooks			if [ -n "${debug_flags}" ]; then
1425189759Sbrooks				wlandebug -i $child ${debug_flags}
1426189759Sbrooks			fi
1427178356Ssam		else
1428252015Shrs			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1429189759Sbrooks			if [ -n "${debug_flags}" ]; then
1430189759Sbrooks				wlandebug -i $i ${debug_flags}
1431189759Sbrooks			fi
1432252015Shrs			${IFCONFIG_CMD} $i name $child && cfg=0
1433178356Ssam		fi
1434188118Sthompsa		if autoif $child; then
1435188118Sthompsa			ifn_start $child
1436188118Sthompsa		fi
1437178356Ssam	done
1438178356Ssam
1439201215Sjhb	# Create vlan interfaces
1440201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1441201215Sjhb
1442201215Sjhb	if [ -n "${child_vlans}" ]; then
1443201215Sjhb		load_kld if_vlan
1444201215Sjhb	fi
1445201215Sjhb
1446201215Sjhb	for child in ${child_vlans}; do
1447201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1448201215Sjhb			child="${ifn}.${child}"
1449201215Sjhb			create_args=`get_if_var $child create_args_IF`
1450252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1451201215Sjhb		else
1452201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1453201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1454252015Shrs				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1455201215Sjhb			else
1456252015Shrs				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1457252015Shrs				${IFCONFIG_CMD} $i name $child && cfg=0
1458201215Sjhb			fi
1459201215Sjhb		fi
1460201215Sjhb		if autoif $child; then
1461201215Sjhb			ifn_start $child
1462201215Sjhb		fi
1463201215Sjhb	done
1464201215Sjhb
1465179001Sbrooks	return ${cfg}
1466178356Ssam}
1467178356Ssam
1468197147Shrs# childif_destroy
1469197147Shrs#	Destroy child interfaces.
1470178356Ssam#
1471178356Ssamchildif_destroy()
1472178356Ssam{
1473201215Sjhb	local cfg child child_vlans child_wlans ifn
1474197139Shrs	cfg=1
1475178356Ssam
1476201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1477178527Sbrooks	for child in ${child_wlans}; do
1478201215Sjhb		if ! ifexists $child; then
1479201215Sjhb			continue
1480201215Sjhb		fi
1481252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1482178356Ssam	done
1483197139Shrs
1484201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1485201215Sjhb	for child in ${child_vlans}; do
1486201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1487201215Sjhb			child="${ifn}.${child}"
1488201215Sjhb		fi
1489201215Sjhb		if ! ifexists $child; then
1490201215Sjhb			continue
1491201215Sjhb		fi
1492252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1493201215Sjhb	done
1494201215Sjhb
1495197139Shrs	return ${cfg}
1496178356Ssam}
1497178356Ssam
1498197147Shrs# ng_mkpeer
1499197147Shrs#	Create netgraph nodes.
1500166583Sflz#
1501197147Shrsng_mkpeer()
1502197147Shrs{
1503166583Sflz	ngctl -f - 2> /dev/null <<EOF
1504166583Sflzmkpeer $*
1505166583Sflzmsg dummy nodeinfo
1506166583SflzEOF
1507166583Sflz}
1508166583Sflz
1509197147Shrs# ng_create_one
1510197147Shrs#	Create netgraph nodes.
1511197147Shrs#
1512197147Shrsng_create_one()
1513197147Shrs{
1514197139Shrs	local t
1515197139Shrs
1516166583Sflz	ng_mkpeer $* | while read line; do
1517166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1518166583Sflz		if [ -n "${t}" ]; then
1519166583Sflz			echo ${t}
1520166583Sflz			return
1521166583Sflz		fi
1522166583Sflz	done
1523166583Sflz}
1524166583Sflz
1525166583Sflz# ng_fec_create ifn
1526197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1527197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1528197139Shrsng_fec_create()
1529197139Shrs{
1530166583Sflz	 local req_iface iface bogus
1531166583Sflz	 req_iface="$1"
1532166583Sflz
1533166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1534166583Sflz
1535166583Sflz	 bogus=""
1536166583Sflz	 while true; do
1537166583Sflz		 iface=`ng_create_one fec dummy fec`
1538166583Sflz		 if [ -z "${iface}" ]; then
1539166583Sflz			 exit 2
1540166583Sflz		 fi
1541166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1542166583Sflz			 break
1543166583Sflz		 fi
1544166583Sflz		 bogus="${bogus} ${iface}"
1545166583Sflz	 done
1546166583Sflz
1547166583Sflz	 for iface in ${bogus}; do
1548166583Sflz		 ngctl shutdown ${iface}:
1549166583Sflz	 done
1550166583Sflz}
1551166583Sflz
1552197147Shrs# fec_up
1553197147Shrs#	Create Fast EtherChannel interfaces.
1554197147Shrsfec_up()
1555197147Shrs{
1556197139Shrs	local i j
1557197139Shrs
1558166583Sflz	for i in ${fec_interfaces}; do
1559166583Sflz		ng_fec_create $i
1560166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1561166583Sflz			case ${j} in
1562100282Sdougb			'')
1563100282Sdougb				continue
1564100282Sdougb				;;
1565100282Sdougb			*)
1566166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1567100282Sdougb				;;
1568100282Sdougb			esac
1569100282Sdougb		done
1570166583Sflz	done
1571100282Sdougb}
1572100282Sdougb
1573113674Smtm# ipx_up ifn
1574197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1575197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1576113674Smtm#
1577113674Smtmipx_up()
1578100280Sgordon{
1579197139Shrs	local ifn
1580113674Smtm	ifn="$1"
1581197139Shrs
1582197139Shrs	# ifconfig_IF_ipx
1583197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1584113674Smtm	if [ -n "${ifconfig_args}" ]; then
1585252015Shrs		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1586113674Smtm		return 0
158785831Sdes	fi
1588197139Shrs
1589113674Smtm	return 1
1590113674Smtm}
159185831Sdes
1592116029Smtm# ipx_down ifn
1593116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1594116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1595113674Smtm#
1596116029Smtmipx_down()
1597116029Smtm{
1598197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1599197139Shrs	_if=$1
1600116100Smtm	_ifs="^"
1601116100Smtm	_ret=1
1602252015Shrs	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1603197139Shrs	oldifs="$IFS"
1604116100Smtm
1605116100Smtm	IFS="$_ifs"
1606116100Smtm	for _ipx in $ipxList ; do
1607116100Smtm		# get rid of extraneous line
1608116100Smtm		[ -z "$_ipx" ] && break
1609116100Smtm
1610116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1611116100Smtm
1612116100Smtm		IFS="$oldifs"
1613252015Shrs		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1614116100Smtm		IFS="$_ifs"
1615116100Smtm		_ret=0
1616116100Smtm	done
1617116100Smtm	IFS="$oldifs"
1618116100Smtm
1619116100Smtm	return $_ret
1620116029Smtm}
1621116029Smtm
1622253924Shrs# ifnet_rename [ifname]
1623253924Shrs#	Rename interfaces if ifconfig_IF_name is defined.
1624116029Smtm#
1625137070Spjdifnet_rename()
1626137070Spjd{
1627197139Shrs	local _if _ifname
1628137070Spjd
1629197139Shrs	# ifconfig_IF_name
1630253924Shrs	for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1631157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1632137070Spjd		if [ ! -z "$_ifname" ]; then
1633252015Shrs			${IFCONFIG_CMD} $_if name $_ifname
1634137070Spjd		fi
1635137070Spjd	done
1636197139Shrs
1637137070Spjd	return 0
1638137070Spjd}
1639137070Spjd
1640113674Smtm# list_net_interfaces type
1641113674Smtm#	List all network interfaces. The type of interface returned
1642113674Smtm#	can be controlled by the type argument. The type
1643113674Smtm#	argument can be any of the following:
1644197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1645197147Shrs#		dhcp	- list only DHCP configured interfaces
1646197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1647197139Shrs#				  Address Autoconf configured interfaces
1648197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1649197139Shrs#				  configured interfaces
1650113674Smtm#	If no argument is specified all network interfaces are output.
1651134429Syar#	Note that the list will include cloned interfaces if applicable.
1652134429Syar#	Cloned interfaces must already exist to have a chance to appear
1653134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1654113674Smtm#
1655113674Smtmlist_net_interfaces()
1656113674Smtm{
1657197139Shrs	local type _tmplist _list _autolist _lo _if
1658113674Smtm	type=$1
165965532Snectar
1660149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
166151231Ssheldonh	#
1662197139Shrs	_tmplist=
166351231Ssheldonh	case ${network_interfaces} in
166451231Ssheldonh	[Aa][Uu][Tt][Oo])
1665252015Shrs		_autolist="`${IFCONFIG_CMD} -l`"
1666149726Sbrooks		_lo=
1667149401Sbrooks		for _if in ${_autolist} ; do
1668149401Sbrooks			if autoif $_if; then
1669149726Sbrooks				if [ "$_if" = "lo0" ]; then
1670149726Sbrooks					_lo="lo0 "
1671149726Sbrooks				else
1672197139Shrs					_tmplist="${_tmplist} ${_if}"
1673149726Sbrooks				fi
1674149401Sbrooks			fi
1675149401Sbrooks		done
1676197139Shrs		_tmplist="${_lo}${_tmplist# }"
1677256039Shrs	;;
167883677Sbrooks	*)
1679256039Shrs		for _if in ${network_interfaces} ${cloned_interfaces}; do
1680256039Shrs			# epair(4) uses epair[0-9] for creation and
1681256039Shrs			# epair[0-9][ab] for configuration.
1682256039Shrs			case $_if in
1683256039Shrs			epair[0-9]*)
1684256039Shrs				_tmplist="$_tmplist ${_if}a ${_if}b"
1685256039Shrs			;;
1686256039Shrs			*)
1687256039Shrs				_tmplist="$_tmplist $_if"
1688256039Shrs			;;
1689256039Shrs			esac
1690256039Shrs		done
1691256039Shrs		#
1692196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1693196478Sdougb		#
1694196478Sdougb		case "$_tmplist" in
1695256039Shrs		lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1696256039Shrs			# This is fine, do nothing
1697256039Shrs			_tmplist="${_tmplist# }"
1698256039Shrs		;;
1699256039Shrs		*)
1700256039Shrs			_tmplist="lo0 ${_tmplist# }"
1701256039Shrs		;;
1702196478Sdougb		esac
1703256039Shrs	;;
170451231Ssheldonh	esac
170549122Sbrian
1706197139Shrs	_list=
1707197139Shrs	case "$type" in
1708197139Shrs	nodhcp)
1709197139Shrs		for _if in ${_tmplist} ; do
1710197139Shrs			if ! dhcpif $_if && \
1711197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1712197139Shrs				_list="${_list# } ${_if}"
1713197147Shrs			fi
1714197139Shrs		done
1715256039Shrs	;;
1716197139Shrs	dhcp)
1717197147Shrs		for _if in ${_tmplist} ; do
1718197147Shrs			if dhcpif $_if; then
1719197139Shrs				_list="${_list# } ${_if}"
1720197147Shrs			fi
1721197147Shrs		done
1722256039Shrs	;;
1723197139Shrs	noautoconf)
1724197139Shrs		for _if in ${_tmplist} ; do
1725197139Shrs			if ! ipv6_autoconfif $_if && \
1726197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1727197139Shrs				_list="${_list# } ${_if}"
1728197139Shrs			fi
1729197139Shrs		done
1730256039Shrs	;;
1731197139Shrs	autoconf)
1732197139Shrs		for _if in ${_tmplist} ; do
1733197139Shrs			if ipv6_autoconfif $_if; then
1734197139Shrs				_list="${_list# } ${_if}"
1735197139Shrs			fi
1736197139Shrs		done
1737256039Shrs	;;
1738197139Shrs	*)
1739197139Shrs		_list=${_tmplist}
1740256039Shrs	;;
1741113674Smtm	esac
1742197139Shrs
1743197139Shrs	echo $_list
1744197139Shrs
1745130151Sschweikh	return 0
174625184Sjkh}
1747114942Sume
1748179003Sbrooks# get_default_if -address_family
1749179003Sbrooks#	Get the interface of the default route for the given address family.
1750179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1751179003Sbrooks#
1752179003Sbrooksget_default_if()
1753179003Sbrooks{
1754197139Shrs	local routeget oldifs defif line
1755197139Shrs	defif=
1756179003Sbrooks	oldifs="$IFS"
1757179003Sbrooks	IFS="
1758179003Sbrooks"
1759197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1760179003Sbrooks		case $line in
1761179003Sbrooks		*interface:*)
1762179003Sbrooks			defif=${line##*: }
1763179003Sbrooks			;;
1764179003Sbrooks		esac
1765179003Sbrooks	done
1766179003Sbrooks	IFS=${oldifs}
1767179003Sbrooks
1768179003Sbrooks	echo $defif
1769179003Sbrooks}
1770179003Sbrooks
1771197147Shrs# hexdigit arg
1772197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1773114942Sumehexdigit()
1774114942Sume{
1775221884Sjilles	printf '%x\n' "$1"
1776114942Sume}
1777114942Sume
1778197147Shrs# hexprint arg
1779197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1780114942Sumehexprint()
1781114942Sume{
1782221884Sjilles	printf '%x\n' "$1"
1783114942Sume}
1784114942Sume
1785196436Sdougbis_wired_interface()
1786196436Sdougb{
1787196436Sdougb	local media
1788196436Sdougb
1789252015Shrs	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1790196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1791196436Sdougb	esac
1792196436Sdougb
1793196436Sdougb	test "$media" = "Ethernet"
1794196436Sdougb}
1795196436Sdougb
1796197147Shrs# network6_getladdr if [flag]
1797197147Shrs#	Echo link-local address from $if if any.
1798197147Shrs#	If flag is defined, tentative ones will be excluded.
1799114942Sumenetwork6_getladdr()
1800114942Sume{
1801252015Shrs	local _if _flag proto addr rest
1802252015Shrs	_if=$1
1803252015Shrs	_flag=$2
1804252015Shrs
1805252015Shrs	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1806252015Shrs		case "${proto}/${addr}/${_flag}/${rest}" in
1807252015Shrs		inet6/fe80::*//*)
1808252015Shrs			echo ${addr}
1809252015Shrs		;;
1810252015Shrs		inet6/fe80:://*tentative*)	# w/o flag
1811252015Shrs			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1812252015Shrs			network6_getladdr $_if $_flags
1813252015Shrs		;;
1814252015Shrs		inet6/fe80::/*/*tentative*)	# w/ flag
1815252015Shrs			echo ${addr}
1816252015Shrs		;;
1817252015Shrs		*)
1818252015Shrs			continue
1819252015Shrs		;;
1820114942Sume		esac
1821252015Shrs
1822252015Shrs		return
1823114942Sume	done
1824114942Sume}
1825