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: stable/10/etc/network.subr 339680 2018-10-24 05:15:46Z eugen $
2666830Sobrien#
27252015ShrsIFCONFIG_CMD="/sbin/ifconfig"
28272577Sbdrewery: ${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{
127325799Seugen	local _cfg _ifconfig_descr _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
170272856Shrs	fi
171212574Shrs
172272856Shrs	# ifconfig_IF
173272856Shrs	ifconfig_args=`ifconfig_getargs $1`
174272856Shrs	if [ -n "${ifconfig_args}" ]; then
175272856Shrs		eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
176272856Shrs		_cfg=0
177272856Shrs	fi
178272856Shrs
179272856Shrs	# inet6 specific
180272856Shrs	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
219325803Seugen	if [ ${_cfg} -eq 0 ]; then
220325803Seugen		${IFCONFIG_CMD} $1 up
221325803Seugen	fi
222325803Seugen
223325799Seugen	_ifconfig_descr=`get_if_var $1 ifconfig_IF_descr`
224325799Seugen	if [ -n "${_ifconfig_descr}" ]; then
225325799Seugen		${IFCONFIG_CMD} $1 description "${_ifconfig_descr}"
226197139Shrs	fi
227197139Shrs
228147088Sbrooks	if wpaif $1; then
229147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
230147088Sbrooks		_cfg=0		# XXX: not sure this should count
231252230Srpaulo	elif hostapif $1; then
232252230Srpaulo		/etc/rc.d/hostapd start $1
233252230Srpaulo		_cfg=0
234147088Sbrooks	fi
235147088Sbrooks
236147088Sbrooks	if dhcpif $1; then
237149726Sbrooks		if [ $_cfg -ne 0 ] ; then
238252015Shrs			${IFCONFIG_CMD} $1 up
239149726Sbrooks		fi
240157706Sbrooks		if syncdhcpif $1; then
241157706Sbrooks			/etc/rc.d/dhclient start $1
242157706Sbrooks		fi
243147088Sbrooks		_cfg=0
244147088Sbrooks	fi
245147088Sbrooks
246147121Sbrooks	return $_cfg
247113674Smtm}
24825184Sjkh
249116029Smtm# ifconfig_down if
250161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
251161386Sbrooks#	the interface exists.
252116029Smtm#
253116029Smtmifconfig_down()
254116029Smtm{
255197139Shrs	local _cfg
256147121Sbrooks	_cfg=1
257116029Smtm
258147088Sbrooks	if wpaif $1; then
259147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
260147121Sbrooks		_cfg=0
261252230Srpaulo	elif hostapif $1; then
262252230Srpaulo		/etc/rc.d/hostapd stop $1
263252230Srpaulo		_cfg=0
264147088Sbrooks	fi
265147088Sbrooks
266147088Sbrooks	if dhcpif $1; then
267147088Sbrooks		/etc/rc.d/dhclient stop $1
268147088Sbrooks		_cfg=0
269147088Sbrooks	fi
270147088Sbrooks
271161386Sbrooks	if ifexists $1; then
272252015Shrs		${IFCONFIG_CMD} $1 down
273161386Sbrooks		_cfg=0
274161386Sbrooks	fi
275157706Sbrooks
276147121Sbrooks	return $_cfg
277116029Smtm}
278116029Smtm
279157706Sbrooks# get_if_var if var [default]
280197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
281197147Shrs#	$var is a string containg the sub-string "IF" which will be
282197147Shrs#	replaced with $if after the characters defined in _punct are
283197147Shrs#	replaced with '_'. If the variable is unset, replace it with
284197147Shrs#	$default if given.
285157706Sbrooksget_if_var()
286157706Sbrooks{
287212578Shrs	local _if _punct _punct_c _var _default prefix suffix
288197139Shrs
289157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
290157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
291157706Sbrooks	fi
292157706Sbrooks
293157706Sbrooks	_if=$1
294264438Sdteske	_punct=".-/+"
295264438Sdteske	ltr ${_if} "${_punct}" '_' _if
296157706Sbrooks	_var=$2
297157706Sbrooks	_default=$3
298157706Sbrooks
299157706Sbrooks	prefix=${_var%%IF*}
300157706Sbrooks	suffix=${_var##*IF}
301168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
302157706Sbrooks}
303157706Sbrooks
304197139Shrs# _ifconfig_getargs if [af]
305225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
306225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
307147088Sbrooks#	outside this file.
308147088Sbrooks_ifconfig_getargs()
309147088Sbrooks{
310212574Shrs	local _ifn _af
311147088Sbrooks	_ifn=$1
312197139Shrs	_af=${2+_$2}
313197139Shrs
314147088Sbrooks	if [ -z "$_ifn" ]; then
315147088Sbrooks		return 1
316147088Sbrooks	fi
317147088Sbrooks
318212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
319147088Sbrooks}
320147088Sbrooks
321197139Shrs# ifconfig_getargs if [af]
322147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
323147088Sbrooks#	args such as DHCP and WPA.
324147088Sbrooksifconfig_getargs()
325147088Sbrooks{
326256255Shrs	local _tmpargs _arg _args _vnet
327197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
328147088Sbrooks	if [ $? -eq 1 ]; then
329147088Sbrooks		return 1
330147088Sbrooks	fi
331147088Sbrooks	_args=
332256255Shrs	_vnet=0
333147088Sbrooks
334147088Sbrooks	for _arg in $_tmpargs; do
335256255Shrs		case $_arg:$_vnet in
336256255Shrs		[Dd][Hh][Cc][Pp]:0) ;;
337256255Shrs		[Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
338256255Shrs		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
339256255Shrs		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
340256255Shrs		[Ww][Pp][Aa]:0) ;;
341256255Shrs		[Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
342256255Shrs		vnet:0)	_vnet=1 ;;
343256255Shrs		*:1)	_vnet=0 ;;
344256255Shrs		*:0)
345147088Sbrooks			_args="$_args $_arg"
346256255Shrs		;;
347147088Sbrooks		esac
348147088Sbrooks	done
349147088Sbrooks
350147088Sbrooks	echo $_args
351147088Sbrooks}
352147088Sbrooks
353149401Sbrooks# autoif
354225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
355149401Sbrooks#	boot time and 1 otherwise.
356149401Sbrooksautoif()
357149401Sbrooks{
358197139Shrs	local _tmpargs _arg
359149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
360197139Shrs
361149401Sbrooks	for _arg in $_tmpargs; do
362149401Sbrooks		case $_arg in
363149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
364149401Sbrooks			return 1
365149401Sbrooks			;;
366149401Sbrooks		esac
367149401Sbrooks	done
368197139Shrs
369149401Sbrooks	return 0
370149401Sbrooks}
371149401Sbrooks
372147088Sbrooks# dhcpif if
373147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
374147088Sbrooksdhcpif()
375147088Sbrooks{
376197139Shrs	local _tmpargs _arg
377147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
378197139Shrs
379252360Sdelphij	case $1 in
380252360Sdelphij	lo[0-9]*|\
381252360Sdelphij	stf[0-9]*|\
382252360Sdelphij	faith[0-9]*|\
383252360Sdelphij	lp[0-9]*|\
384252360Sdelphij	sl[0-9]*)
385252360Sdelphij		return 1
386252360Sdelphij		;;
387252360Sdelphij	esac
388225849Sdelphij	if noafif $1; then
389225849Sdelphij		return 1
390225849Sdelphij	fi
391225849Sdelphij
392147088Sbrooks	for _arg in $_tmpargs; do
393147088Sbrooks		case $_arg in
394147088Sbrooks		[Dd][Hh][Cc][Pp])
395147088Sbrooks			return 0
396147088Sbrooks			;;
397157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
398157706Sbrooks			return 0
399157706Sbrooks			;;
400157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
401157706Sbrooks			return 0
402157706Sbrooks			;;
403147088Sbrooks		esac
404147088Sbrooks	done
405197139Shrs
406147088Sbrooks	return 1
407147088Sbrooks}
408147088Sbrooks
409157706Sbrooks# syncdhcpif
410157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
411157706Sbrooks#	1 otherwise.
412157706Sbrookssyncdhcpif()
413157706Sbrooks{
414197139Shrs	local _tmpargs _arg
415157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
416197139Shrs
417225849Sdelphij	if noafif $1; then
418225849Sdelphij		return 1
419225849Sdelphij	fi
420225849Sdelphij
421157706Sbrooks	for _arg in $_tmpargs; do
422157706Sbrooks		case $_arg in
423157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
424157706Sbrooks			return 1
425157706Sbrooks			;;
426157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
427157706Sbrooks			return 0
428157706Sbrooks			;;
429157706Sbrooks		esac
430157706Sbrooks	done
431197139Shrs
432197139Shrs	checkyesno synchronous_dhclient
433157706Sbrooks}
434157706Sbrooks
435147088Sbrooks# wpaif if
436147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
437147088Sbrookswpaif()
438147088Sbrooks{
439197139Shrs	local _tmpargs _arg
440147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
441197139Shrs
442147088Sbrooks	for _arg in $_tmpargs; do
443147088Sbrooks		case $_arg in
444147088Sbrooks		[Ww][Pp][Aa])
445147088Sbrooks			return 0
446147088Sbrooks			;;
447147088Sbrooks		esac
448147088Sbrooks	done
449197139Shrs
450147088Sbrooks	return 1
451147088Sbrooks}
452147088Sbrooks
453252230Srpaulo# hostapif if
454252230Srpaulo#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
455252230Srpaulohostapif()
456252230Srpaulo{
457252230Srpaulo	local _tmpargs _arg
458252230Srpaulo	_tmpargs=`_ifconfig_getargs $1`
459252230Srpaulo
460252230Srpaulo	for _arg in $_tmpargs; do
461252230Srpaulo		case $_arg in
462252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp])
463252230Srpaulo			return 0
464252230Srpaulo			;;
465252230Srpaulo		esac
466252230Srpaulo	done
467252230Srpaulo
468252230Srpaulo	return 1
469252230Srpaulo}
470252230Srpaulo
471256255Shrs# vnetif if
472256255Shrs#	Returns 0 and echo jail if "vnet" keyword is specified on the
473256255Shrs#	interface, and 1 otherwise.
474256255Shrsvnetif()
475256255Shrs{
476256255Shrs	local _tmpargs _arg _vnet
477256255Shrs	_tmpargs=`_ifconfig_getargs $1`
478256255Shrs
479256255Shrs	_vnet=0
480256255Shrs	for _arg in $_tmpargs; do
481256255Shrs		case $_arg:$_vnet in
482256255Shrs		vnet:0)	_vnet=1 ;;
483256255Shrs		*:1)	echo $_arg; return 0 ;;
484256255Shrs		esac
485256255Shrs	done
486256255Shrs
487256255Shrs	return 1
488256255Shrs}
489256255Shrs
490197139Shrs# afexists af
491197139Shrs#	Returns 0 if the address family is enabled in the kernel
492197139Shrs#	1 otherwise.
493197139Shrsafexists()
494197139Shrs{
495197139Shrs	local _af
496197139Shrs	_af=$1
497197139Shrs
498197139Shrs	case ${_af} in
499222996Shrs	inet|inet6)
500222996Shrs		check_kern_features ${_af}
501197139Shrs		;;
502197697Shrs	ipx)
503197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
504197697Shrs		;;
505197697Shrs	atm)
506197697Shrs		if [ -x /sbin/atmconfig ]; then
507197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
508197697Shrs		else
509197697Shrs			return 1
510197697Shrs		fi
511197697Shrs		;;
512252426Shrs	link|ether)
513252426Shrs		return 0
514252426Shrs		;;
515197139Shrs	*)
516197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
517197139Shrs		;;
518197139Shrs	esac
519197139Shrs}
520197139Shrs
521212574Shrs# noafif if
522212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
523212574Shrsnoafif()
524212574Shrs{
525212574Shrs	local _if
526212574Shrs	_if=$1
527212574Shrs
528212574Shrs	case $_if in
529212574Shrs	pflog[0-9]*|\
530212574Shrs	pfsync[0-9]*|\
531256040Shrs	usbus[0-9]*|\
532212574Shrs	an[0-9]*|\
533212574Shrs	ath[0-9]*|\
534212574Shrs	ipw[0-9]*|\
535212577Shrs	ipfw[0-9]*|\
536212574Shrs	iwi[0-9]*|\
537212574Shrs	iwn[0-9]*|\
538212574Shrs	ral[0-9]*|\
539212574Shrs	wi[0-9]*|\
540212574Shrs	wl[0-9]*|\
541212574Shrs	wpi[0-9]*)
542212574Shrs		return 0
543212574Shrs		;;
544212574Shrs	esac
545212574Shrs
546212574Shrs	return 1
547212574Shrs}
548212574Shrs
549162490Sbrooks# ipv6if if
550162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
551162490Sbrooks#	1 otherwise.
552162490Sbrooksipv6if()
553162490Sbrooks{
554212574Shrs	local _if _tmpargs i
555212574Shrs	_if=$1
556212574Shrs
557197139Shrs	if ! afexists inet6; then
558162490Sbrooks		return 1
559162490Sbrooks	fi
560197139Shrs
561197139Shrs	# lo0 is always IPv6-enabled
562212574Shrs	case $_if in
563197139Shrs	lo0)
564197139Shrs		return 0
565197139Shrs		;;
566197139Shrs	esac
567197139Shrs
568212574Shrs	case "${ipv6_network_interfaces}" in
569212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
570212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
571212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
572212575Shrs		if [ -n "${_tmpargs}" ]; then
573212575Shrs			return 0
574212575Shrs		fi
575197139Shrs
576230453Shrs		# True if $ipv6_prefix_IF is defined.
577230453Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
578230453Shrs		if [ -n "${_tmpargs}" ]; then
579230453Shrs			return 0
580230453Shrs		fi
581230453Shrs
582212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
583212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
584212575Shrs		if [ -n "${_tmpargs}" ]; then
585212574Shrs			return 0
586212574Shrs		fi
587212575Shrs		;;
588212575Shrs	esac
589197139Shrs
590162490Sbrooks	return 1
591162490Sbrooks}
592162490Sbrooks
593197139Shrs# ipv6_autoconfif if
594197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
595225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
596197139Shrsipv6_autoconfif()
597197139Shrs{
598197139Shrs	local _if _tmpargs _arg
599197139Shrs	_if=$1
600197139Shrs
601212577Shrs	case $_if in
602252360Sdelphij	lo[0-9]*|\
603212577Shrs	stf[0-9]*|\
604212577Shrs	faith[0-9]*|\
605212577Shrs	lp[0-9]*|\
606212577Shrs	sl[0-9]*)
607197139Shrs		return 1
608212577Shrs		;;
609212577Shrs	esac
610212574Shrs	if noafif $_if; then
611212574Shrs		return 1
612212574Shrs	fi
613212577Shrs	if ! ipv6if $_if; then
614212577Shrs		return 1
615212577Shrs	fi
616197139Shrs	if checkyesno ipv6_gateway_enable; then
617197139Shrs		return 1
618197139Shrs	fi
619197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
620197526Shrs	if [ -n "${_tmpargs}" ]; then
621197526Shrs		return 1
622197526Shrs	fi
623212574Shrs	# backward compatibility: $ipv6_enable
624212574Shrs	case $ipv6_enable in
625212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
626242181Shrs		if checkyesno ipv6_gateway_enable; then
627242181Shrs			return 1
628253520Shrs		fi
629253520Shrs		case $1 in
630253520Shrs		bridge[0-9]*)
631253520Shrs			# No accept_rtadv by default on if_bridge(4)
632253520Shrs			# to avoid a conflict with the member
633253520Shrs			# interfaces.
634253520Shrs			return 1
635253520Shrs		;;
636253520Shrs		*)
637242181Shrs			return 0
638253520Shrs		;;
639253520Shrs		esac
640212577Shrs	;;
641197526Shrs	esac
642197526Shrs
643212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
644212574Shrs	for _arg in $_tmpargs; do
645212574Shrs		case $_arg in
646212574Shrs		accept_rtadv)
647212574Shrs			return 0
648212574Shrs			;;
649212574Shrs		esac
650212574Shrs	done
651212574Shrs
652212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
653212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
654212574Shrs	for _arg in $_tmpargs; do
655212574Shrs		case $_arg in
656212574Shrs		accept_rtadv)
657212574Shrs			return 0
658212574Shrs			;;
659212574Shrs		esac
660212574Shrs	done
661212574Shrs
662197139Shrs	return 1
663197139Shrs}
664197139Shrs
665161386Sbrooks# ifexists if
666161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
667161386Sbrooksifexists()
668161386Sbrooks{
669197139Shrs	[ -z "$1" ] && return 1
670252015Shrs	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
671161386Sbrooks}
672161386Sbrooks
673152441Sbrooks# ipv4_up if
674212578Shrs#	add IPv4 addresses to the interface $if
675152441Sbrooksipv4_up()
676152441Sbrooks{
677197139Shrs	local _if _ret
678152441Sbrooks	_if=$1
679197139Shrs	_ret=1
680197139Shrs
681222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
682222515Sbz	if [ "${_if}" = "lo0" ]; then
683226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
684222515Sbz		if [ -z "${ifconfig_args}" ]; then
685252015Shrs			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
686222515Sbz		fi
687222515Sbz	fi
688252015Shrs	ifalias ${_if} inet alias && _ret=0
689197139Shrs
690197139Shrs	return $_ret
691152441Sbrooks}
692152441Sbrooks
693197139Shrs# ipv6_up if
694197139Shrs#	add IPv6 addresses to the interface $if
695197139Shrsipv6_up()
696197139Shrs{
697197139Shrs	local _if _ret
698197139Shrs	_if=$1
699197139Shrs	_ret=1
700197139Shrs
701197139Shrs	if ! ipv6if $_if; then
702197139Shrs		return 0
703197139Shrs	fi
704197139Shrs
705252015Shrs	ifalias ${_if} inet6 alias && _ret=0
706226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
707197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
708197139Shrs
709197139Shrs	return $_ret
710197139Shrs}
711197139Shrs
712152441Sbrooks# ipv4_down if
713197147Shrs#	remove IPv4 addresses from the interface $if
714152441Sbrooksipv4_down()
715152441Sbrooks{
716197139Shrs	local _if _ifs _ret inetList oldifs _inet
717152441Sbrooks	_if=$1
718161386Sbrooks	_ifs="^"
719161386Sbrooks	_ret=1
720161386Sbrooks
721252015Shrs	ifalias ${_if} inet -alias && _ret=0
722161386Sbrooks
723255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
724252015Shrs
725161386Sbrooks	oldifs="$IFS"
726161386Sbrooks	IFS="$_ifs"
727161386Sbrooks	for _inet in $inetList ; do
728161386Sbrooks		# get rid of extraneous line
729253924Shrs		case $_inet in
730255653Shrs		inet\ *)	;;
731255653Shrs		*)		continue ;;
732253924Shrs		esac
733161386Sbrooks
734161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
735161386Sbrooks
736161386Sbrooks		IFS="$oldifs"
737252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet} delete
738161386Sbrooks		IFS="$_ifs"
739161386Sbrooks		_ret=0
740161386Sbrooks	done
741161386Sbrooks	IFS="$oldifs"
742161386Sbrooks
743161386Sbrooks	return $_ret
744152441Sbrooks}
745152441Sbrooks
746197139Shrs# ipv6_down if
747197139Shrs#	remove IPv6 addresses from the interface $if
748197139Shrsipv6_down()
749197139Shrs{
750197139Shrs	local _if _ifs _ret inetList oldifs _inet6
751197139Shrs	_if=$1
752197139Shrs	_ifs="^"
753197139Shrs	_ret=1
754197139Shrs
755197139Shrs	if ! ipv6if $_if; then
756197139Shrs		return 0
757197139Shrs	fi
758197139Shrs
759197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
760226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
761252015Shrs	ifalias ${_if} inet6 -alias && _ret=0
762197139Shrs
763255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
764197139Shrs
765197139Shrs	oldifs="$IFS"
766197139Shrs	IFS="$_ifs"
767197139Shrs	for _inet6 in $inetList ; do
768197139Shrs		# get rid of extraneous line
769279327Srpaulo		case $_inet6 in
770255653Shrs		inet6\ *)	;;
771255653Shrs		*)		continue ;;
772255653Shrs		esac
773197139Shrs
774197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
775197139Shrs
776197139Shrs		IFS="$oldifs"
777252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
778197139Shrs		IFS="$_ifs"
779197139Shrs		_ret=0
780197139Shrs	done
781197139Shrs	IFS="$oldifs"
782197139Shrs
783197139Shrs	return $_ret
784197139Shrs}
785197139Shrs
786252015Shrs# ifalias if af action
787252015Shrs#	Configure or remove aliases for network interface $if.
788113674Smtm#	It returns 0 if at least one alias was configured or
789252015Shrs#	removed, or 1 if there were none.
790113674Smtm#
791252015Shrsifalias()
792113674Smtm{
793197139Shrs	local _ret
794113674Smtm	_ret=1
795197139Shrs
796252015Shrs	afexists $2 || return $_ret
797252015Shrs
798197139Shrs	case "$2" in
799252426Shrs	inet|inet6|link|ether)
800252015Shrs		ifalias_af_common $1 $2 $3 && _ret=0
801197139Shrs		;;
802197139Shrs	esac
803197139Shrs
804197139Shrs	return $_ret
805197139Shrs}
806197139Shrs
807252015Shrs# ifalias_expand_addr af action addr
808252015Shrs#	Expand address range ("N-M") specification in addr.
809252015Shrs#	"addr" must not include an address-family keyword.
810252015Shrs#	The results will include an address-family keyword.
811197139Shrs#
812252015Shrsifalias_expand_addr()
813197139Shrs{
814253505Shrs	local _af _action
815197139Shrs
816253505Shrs	_af=$1
817253505Shrs	_action=$2
818253505Shrs	shift 2
819253505Shrs
820253505Shrs	afexists $_af || return
821253505Shrs	ifalias_expand_addr_$_af $_action $*
822197139Shrs}
823197139Shrs
824252015Shrs# ifalias_expand_addr_inet action addr
825252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv4.
826197139Shrs#
827252015Shrsifalias_expand_addr_inet()
828197139Shrs{
829253505Shrs	local _action _arg _cidr _cidr_addr _exargs
830252015Shrs	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
831252015Shrs	local _retstr _c
832252015Shrs	_action=$1
833252015Shrs	_arg=$2
834253505Shrs	shift 2
835253505Shrs	_exargs=$*
836252015Shrs	_retstr=
837197139Shrs
838253505Shrs	case $_action:$_arg:$_exargs in
839253505Shrs	*:*--*)		return ;;	# invalid
840253505Shrs	tmp:*[0-9]-[0-9]*:*)		# to be expanded
841253505Shrs		_action="alias"
842253505Shrs	;;
843253505Shrs	*:*[0-9]-[0-9]*:*)		# to be expanded
844253505Shrs	;;
845253505Shrs	tmp:*:*netmask*)		# already expanded w/ netmask option
846253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
847253505Shrs	;;
848253505Shrs	tmp:*:*)			# already expanded w/o netmask option
849253505Shrs		echo $_arg $_exargs && return
850253505Shrs	;;
851253505Shrs	*:*:*netmask*)			# already expanded w/ netmask option
852253505Shrs		echo inet ${_arg%/[0-9]*} $_exargs && return
853253505Shrs	;;
854253505Shrs	*:*:*)				# already expanded w/o netmask option
855253505Shrs		echo inet $_arg $_exargs && return
856253505Shrs	;;
857252015Shrs	esac
858252015Shrs
859252015Shrs	for _cidr in $_arg; do
860252015Shrs		_ipaddr=${_cidr%%/*}
861252015Shrs		_plen=${_cidr##*/}
862252015Shrs		# When subnet prefix length is not specified, use /32.
863252015Shrs		case $_plen in
864252015Shrs		$_ipaddr)	_plen=32 ;;	# "/" character not found
865197139Shrs		esac
866197139Shrs
867252015Shrs		OIFS=$IFS
868252015Shrs		IFS=. set -- $_ipaddr
869252015Shrs		_range=
870252015Shrs		_iphead=
871252015Shrs		_iptail=
872252015Shrs		for _c in $@; do
873252015Shrs			case $_range:$_c in
874252015Shrs			:[0-9]*-[0-9]*)
875252015Shrs				_range=$_c
876197139Shrs			;;
877252015Shrs			:*)
878252015Shrs				_iphead="${_iphead}${_iphead:+.}${_c}"
879197139Shrs			;;
880252015Shrs			*:*)
881252015Shrs				_iptail="${_iptail}${_iptail:+.}${_c}"
882252015Shrs			;;
883252015Shrs			esac
884252015Shrs		done
885252015Shrs		IFS=$OIFS
886252015Shrs		_iplow=${_range%-*}
887252015Shrs		_iphigh=${_range#*-}
888252015Shrs
889252015Shrs		# clear netmask when removing aliases
890252015Shrs		if [ "$_action" = "-alias" ]; then
891252015Shrs			_plen=""
892252015Shrs		fi
893252015Shrs
894252015Shrs		_ipcount=$_iplow
895252015Shrs		while [ "$_ipcount" -le "$_iphigh" ]; do
896252015Shrs			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
897272577Sbdrewery			if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
898272577Sbdrewery				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."
899252015Shrs				break
900252015Shrs			else
901252015Shrs				_ipcount=$(($_ipcount + 1))
902252015Shrs			fi
903252015Shrs			# Forcibly set /32 for remaining aliases.
904252015Shrs			_plen=32
905252015Shrs		done
906197139Shrs	done
907197139Shrs
908252015Shrs	for _c in $_retstr; do
909253505Shrs		ifalias_expand_addr_inet $_action $_c $_exargs
910252015Shrs	done
911113674Smtm}
912100280Sgordon
913252015Shrs# ifalias_expand_addr_inet6 action addr
914252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv6.
915116029Smtm#
916252015Shrsifalias_expand_addr_inet6()
917116029Smtm{
918253505Shrs	local _action _arg _cidr _cidr_addr _exargs
919252015Shrs	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
920252015Shrs	local _ipv4part
921252015Shrs	local _retstr _c
922252015Shrs	_action=$1
923252015Shrs	_arg=$2
924253505Shrs	shift 2
925253505Shrs	_exargs=$*
926252015Shrs	_retstr=
927197139Shrs
928253505Shrs	case $_action:$_arg:$_exargs in
929253505Shrs	*:*--*:*)	return ;;	# invalid
930253505Shrs	tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
931253505Shrs		_action="alias"
932253505Shrs	;;
933253505Shrs	*:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)	# to be expanded
934253505Shrs	;;
935253505Shrs	tmp:*:*prefixlen*)	# already expanded w/ prefixlen option
936253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
937253505Shrs	;;
938253505Shrs	tmp:*:*)		# already expanded w/o prefixlen option
939253505Shrs		echo $_arg $_exargs && return
940253505Shrs	;;
941253505Shrs	*:*:*prefixlen*)	# already expanded w/ prefixlen option
942253505Shrs		echo inet6 ${_arg%/[0-9]*} $_exargs && return
943253505Shrs	;;
944253505Shrs	*:*:*)			# already expanded w/o prefixlen option
945253505Shrs		echo inet6 $_arg $_exargs && return
946253505Shrs	;;
947197139Shrs	esac
948197139Shrs
949252015Shrs	for _cidr in $_arg; do
950252015Shrs		_ipaddr="${_cidr%%/*}"
951252015Shrs		_plen="${_cidr##*/}"
952252015Shrs
953252015Shrs		case $_action:$_ipaddr:$_cidr in
954252015Shrs		-alias:*:*)		unset _plen ;;
955252015Shrs		*:$_cidr:$_ipaddr)	unset _plen ;;
956252015Shrs		esac
957252015Shrs
958252015Shrs		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
959252015Shrs			# Handle !v4mapped && !v4compat addresses.
960252015Shrs
961252015Shrs			# The default prefix length is 64.
962252015Shrs			case $_ipaddr:$_cidr in
963252015Shrs			$_cidr:$_ipaddr)	_plen="64" ;;
964252015Shrs			esac
965252015Shrs			_ipleft=${_ipaddr%-*}
966252015Shrs			_ipright=${_ipaddr#*-}
967252015Shrs			_iplow=${_ipleft##*:}
968252015Shrs			_iphigh=${_ipright%%:*}
969252015Shrs			_ipleft=${_ipleft%:*}
970252015Shrs			_ipright=${_ipright#*:}
971252015Shrs
972252015Shrs			if [ "$_iphigh" = "$_ipright" ]; then
973252015Shrs				unset _ipright
974252015Shrs			else
975252015Shrs				_ipright=:$_ipright
976252015Shrs			fi
977252015Shrs
978252015Shrs			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
979252015Shrs				_iplow=$((0x$_iplow))
980252015Shrs				_iphigh=$((0x$_iphigh))
981252015Shrs				_ipcount=$_iplow
982252015Shrs				while [ $_ipcount -le $_iphigh ]; do
983252015Shrs					_r=`printf "%s:%04x%s%s" \
984252015Shrs					    $_ipleft $_ipcount $_ipright \
985252015Shrs					    ${_plen:+/}$_plen`
986252015Shrs					_retstr="$_retstr $_r"
987272577Sbdrewery					if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
988252015Shrs					then
989272577Sbdrewery						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."
990252015Shrs						break
991252015Shrs					else
992252015Shrs						_ipcount=$(($_ipcount + 1))
993252015Shrs					fi
994252015Shrs				done
995252015Shrs			else
996252015Shrs				_retstr="${_ipaddr}${_plen:+/}${_plen}"
997252015Shrs			fi
998252015Shrs
999252015Shrs			for _c in $_retstr; do
1000253505Shrs				ifalias_expand_addr_inet6 $_action $_c $_exargs
1001252015Shrs			done
1002252015Shrs		else
1003252015Shrs			# v4mapped/v4compat should handle as an IPv4 alias
1004252015Shrs			_ipv4part=${_ipaddr##*:}
1005252015Shrs
1006252015Shrs			# Adjust prefix length if any.  If not, set the
1007252015Shrs			# default prefix length as 32.
1008252015Shrs			case $_ipaddr:$_cidr in
1009252015Shrs			$_cidr:$_ipaddr)	_plen=32 ;;
1010252015Shrs			*)			_plen=$(($_plen - 96)) ;;
1011252015Shrs			esac
1012252015Shrs
1013252015Shrs			_retstr=`ifalias_expand_addr_inet \
1014252015Shrs			    tmp ${_ipv4part}${_plen:+/}${_plen}`
1015252015Shrs			for _c in $_retstr; do
1016253505Shrs				ifalias_expand_addr_inet $_action $_c $_exargs
1017252015Shrs			done
1018252015Shrs		fi
1019252015Shrs	done
1020197139Shrs}
1021197139Shrs
1022252015Shrs# ifalias_af_common_handler if af action args
1023252015Shrs#	Helper function for ifalias_af_common().
1024197139Shrs#
1025252015Shrsifalias_af_common_handler()
1026197139Shrs{
1027252015Shrs	local _ret _if _af _action _args _c _tmpargs
1028252015Shrs
1029197139Shrs	_ret=1
1030252015Shrs	_if=$1
1031252015Shrs	_af=$2
1032252015Shrs	_action=$3
1033252015Shrs	shift 3
1034252015Shrs	_args=$*
1035197139Shrs
1036252015Shrs	case $_args in
1037252015Shrs	${_af}\ *)	;;
1038252015Shrs	*)	return	;;
1039252015Shrs	esac
1040252015Shrs
1041252426Shrs	# link(ether) does not support address removal.
1042252426Shrs	case $_af:$_action in
1043252426Shrs	link:-alias|ether:-alias)	return ;;
1044252426Shrs	esac
1045252426Shrs
1046252015Shrs	_tmpargs=
1047252015Shrs	for _c in $_args; do
1048252015Shrs		case $_c in
1049252015Shrs		${_af})
1050252015Shrs			case $_tmpargs in
1051272858Shrs			${_af}\ *[0-9a-fA-F]-*)
1052252015Shrs				ifalias_af_common_handler $_if $_af $_action \
1053252015Shrs				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1054197139Shrs			;;
1055252015Shrs			${_af}\ *)
1056252015Shrs				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1057197139Shrs			;;
1058252015Shrs			esac
1059252015Shrs			_tmpargs=$_af
1060252015Shrs		;;
1061252015Shrs		*)
1062252015Shrs			_tmpargs="$_tmpargs $_c"
1063252015Shrs		;;
1064197139Shrs		esac
1065197139Shrs	done
1066252015Shrs	# Process the last component if any.
1067252015Shrs	if [ -n "$_tmpargs}" ]; then
1068252015Shrs		case $_tmpargs in
1069272858Shrs		${_af}\ *[0-9a-fA-F]-*)
1070252015Shrs			ifalias_af_common_handler $_if $_af $_action \
1071252015Shrs			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1072252015Shrs		;;
1073252015Shrs		${_af}\ *)
1074252015Shrs			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1075252015Shrs		;;
1076252015Shrs		esac
1077252015Shrs	fi
1078197139Shrs
1079197139Shrs	return $_ret
1080197139Shrs}
1081197139Shrs
1082252015Shrs# ifalias_af_common if af action
1083252015Shrs#	Helper function for ifalias().
1084197139Shrs#
1085252015Shrsifalias_af_common()
1086197139Shrs{
1087252015Shrs	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1088269035Sdteske	local _vif _punct=".-/+"
1089252015Shrs
1090197139Shrs	_ret=1
1091252015Shrs	_aliasn=
1092252015Shrs	_if=$1
1093252015Shrs	_af=$2
1094252015Shrs	_action=$3
1095197139Shrs
1096264438Sdteske	# Normalize $_if before using it in a pattern to list_vars()
1097269035Sdteske	ltr "$_if" "$_punct" "_" _vif
1098264438Sdteske
1099252015Shrs	# ifconfig_IF_aliasN which starts with $_af
1100269035Sdteske	for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1101269035Sdteske		sort_lite -nk1.$((9+${#_vif}+7))`
1102264438Sdteske	do
1103264438Sdteske		eval ifconfig_args=\"\$$alias\"
1104252015Shrs		_iaf=
1105252015Shrs		case $ifconfig_args in
1106252015Shrs		inet\ *)	_iaf=inet ;;
1107252015Shrs		inet6\ *)	_iaf=inet6 ;;
1108252015Shrs		ipx\ *)		_iaf=ipx ;;
1109252426Shrs		link\ *)	_iaf=link ;;
1110252426Shrs		ether\ *)	_iaf=ether ;;
1111252015Shrs		esac
1112252015Shrs
1113252015Shrs		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1114252015Shrs		${_af}:*:${_af}:*)
1115252015Shrs			_aliasn="$_aliasn $ifconfig_args"
1116197139Shrs			;;
1117252015Shrs		${_af}:*:"":"")
1118116029Smtm			break
1119197139Shrs			;;
1120252015Shrs		inet:alias:"":*)
1121252015Shrs			_aliasn="$_aliasn inet $ifconfig_args"
1122269035Sdteske			warn "\$${alias} needs leading" \
1123252015Shrs			    "\"inet\" keyword for an IPv4 address."
1124197139Shrs		esac
1125116029Smtm	done
1126197139Shrs
1127197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
1128252015Shrs	case $_af in
1129252015Shrs	inet6)
1130269035Sdteske		for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1131269035Sdteske			sort_lite -nk1.$((14+${#_vif}+7))`
1132264438Sdteske		do
1133264438Sdteske			eval ifconfig_args=\"\$$alias\"
1134252015Shrs			case ${_action}:"${ifconfig_args}" in
1135252015Shrs			*:"")
1136252015Shrs				break
1137197139Shrs			;;
1138252015Shrs			alias:*)
1139252015Shrs				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
1140269035Sdteske				warn "\$${alias} is obsolete. " \
1141269035Sdteske				    "Use ifconfig_${_vif}_aliasN instead."
1142252015Shrs			;;
1143252015Shrs			esac
1144252015Shrs		done
1145252015Shrs	esac
1146252015Shrs
1147252015Shrs	# backward compatibility: ipv4_addrs_IF.
1148252015Shrs	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1149252015Shrs		_aliasn="$_aliasn inet $_tmpargs"
1150252015Shrs	done
1151252015Shrs
1152252015Shrs	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1153252015Shrs	_tmpargs=
1154252015Shrs	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1155252015Shrs		case $_c in
1156252426Shrs		inet|inet6|ipx|link|ether)
1157252015Shrs			case $_tmpargs in
1158252015Shrs			${_af}\ *)
1159252015Shrs				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1160252015Shrs			;;
1161252015Shrs			esac
1162252015Shrs			_tmpargs=$_c
1163252015Shrs		;;
1164197139Shrs		*)
1165252015Shrs			_tmpargs="$_tmpargs $_c"
1166197139Shrs		esac
1167197139Shrs	done
1168252015Shrs	# Process the last component
1169252015Shrs	case $_tmpargs in
1170252015Shrs	${_af}\ *)
1171252015Shrs		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1172252015Shrs	;;
1173252015Shrs	esac
1174197139Shrs
1175116029Smtm	return $_ret
1176116029Smtm}
1177116029Smtm
1178226652Shrs# ipv6_prefix_hostid_addr_common if action
1179226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
1180226652Shrs#
1181226652Shrsipv6_prefix_hostid_addr_common()
1182197139Shrs{
1183253444Shrs	local _if _action prefix j
1184197139Shrs	_if=$1
1185226652Shrs	_action=$2
1186197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1187197139Shrs
1188197139Shrs	if [ -n "${prefix}" ]; then
1189197139Shrs		for j in ${prefix}; do
1190252015Shrs			# The default prefixlen is 64.
1191252015Shrs			plen=${j#*/}
1192252015Shrs			case $j:$plen in
1193252015Shrs			$plen:$j)	plen=64 ;;
1194252015Shrs			*)		j=${j%/*} ;;
1195252015Shrs			esac
1196197139Shrs
1197252015Shrs			# Normalize the last part by removing ":"
1198253444Shrs			j=${j%::*}
1199252015Shrs			j=${j%:}
1200253444Shrs			${IFCONFIG_CMD} ${_if} inet6 $j:: \
1201253444Shrs				prefixlen $plen eui64 ${_action}
1202252015Shrs
1203197139Shrs			# if I am a router, add subnet router
1204197139Shrs			# anycast address (RFC 2373).
1205197139Shrs			if checkyesno ipv6_gateway_enable; then
1206252015Shrs				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1207252015Shrs					prefixlen $plen ${_action} anycast
1208197139Shrs			fi
1209197139Shrs		done
1210197139Shrs	fi
1211197139Shrs}
1212197139Shrs
1213197139Shrs# ipv6_accept_rtadv_up if
1214197139Shrs#	Enable accepting Router Advertisement and send Router
1215197139Shrs#	Solicitation message
1216197139Shrsipv6_accept_rtadv_up()
1217197139Shrs{
1218197139Shrs	if ipv6_autoconfif $1; then
1219252015Shrs		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1220203433Sume		if ! checkyesno rtsold_enable; then
1221203433Sume			rtsol ${rtsol_flags} $1
1222203433Sume		fi
1223197139Shrs	fi
1224197139Shrs}
1225197139Shrs
1226197139Shrs# ipv6_accept_rtadv_down if
1227197139Shrs#	Disable accepting Router Advertisement
1228197139Shrsipv6_accept_rtadv_down()
1229197139Shrs{
1230197139Shrs	if ipv6_autoconfif $1; then
1231252015Shrs		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1232197139Shrs	fi
1233197139Shrs}
1234197139Shrs
1235113674Smtm# ifscript_up if
1236113674Smtm#	Evaluate a startup script for the $if interface.
1237113674Smtm#	It returns 0 if a script was found and processed or
1238113674Smtm#	1 if no script was found.
1239113674Smtm#
1240113674Smtmifscript_up()
1241100280Sgordon{
1242113674Smtm	if [ -r /etc/start_if.$1 ]; then
1243113674Smtm		. /etc/start_if.$1
1244113674Smtm		return 0
1245197139Shrs	else
1246197139Shrs		return 1
1247113674Smtm	fi
1248100280Sgordon}
1249100280Sgordon
1250116029Smtm# ifscript_down if
1251116029Smtm#	Evaluate a shutdown script for the $if interface.
1252116029Smtm#	It returns 0 if a script was found and processed or
1253116029Smtm#	1 if no script was found.
1254116029Smtm#
1255116029Smtmifscript_down()
1256116029Smtm{
1257116029Smtm	if [ -r /etc/stop_if.$1 ]; then
1258116029Smtm		. /etc/stop_if.$1
1259116029Smtm		return 0
1260197139Shrs	else
1261197139Shrs		return 1
1262116029Smtm	fi
1263116029Smtm}
1264116029Smtm
1265197147Shrs# clone_up
1266197147Shrs#	Create cloneable interfaces.
1267113674Smtm#
1268113674Smtmclone_up()
1269100280Sgordon{
1270256039Shrs	local _list ifn ifopt _iflist _n tmpargs
1271113674Smtm	_list=
1272253924Shrs	_iflist=$*
1273197139Shrs
1274197139Shrs	# create_args_IF
1275113674Smtm	for ifn in ${cloned_interfaces}; do
1276253924Shrs		# Parse ifn:ifopt.
1277253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1278253924Shrs		case $_iflist in
1279253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1280253924Shrs		*)	continue ;;
1281253924Shrs		esac
1282256039Shrs		case $ifn in
1283256039Shrs		epair[0-9]*)
1284256039Shrs			# epair(4) uses epair[0-9] for creation and
1285256039Shrs			# epair[0-9][ab] for configuration.
1286256039Shrs			#
1287256039Shrs			# Skip if ${ifn}a or ${ifn}b already exist.
1288256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1289256039Shrs				continue
1290256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1291256039Shrs				continue
1292256039Shrs			fi
1293256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1294256039Shrs			    `get_if_var ${ifn} create_args_IF`
1295256039Shrs			if [ $? -eq 0 ]; then
1296256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1297256039Shrs			fi
1298256039Shrs		;;
1299256039Shrs		*)
1300256039Shrs			# Skip if ${ifn} already exists.
1301256039Shrs			if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1302256039Shrs				continue
1303256039Shrs			fi
1304256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1305256039Shrs			    `get_if_var ${ifn} create_args_IF`
1306256039Shrs			if [ $? -eq 0 ]; then
1307256039Shrs				_list="$_list $ifn"
1308256039Shrs			fi
1309256039Shrs		esac
1310113674Smtm	done
1311253924Shrs	if [ -n "$gif_interfaces" ]; then
1312253924Shrs		warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1313253924Shrs	fi
1314253924Shrs	for ifn in ${gif_interfaces}; do
1315253924Shrs		# Parse ifn:ifopt.
1316253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1317253924Shrs		case $_iflist in
1318253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1319253924Shrs		*)	continue ;;
1320253924Shrs		esac
1321253924Shrs		# Skip if ifn already exists.
1322253924Shrs		if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1323253924Shrs			continue
1324253924Shrs		fi
1325253924Shrs		case $ifn in
1326253924Shrs		gif[0-9]*)
1327253924Shrs			${IFCONFIG_CMD} $ifn create
1328253924Shrs		;;
1329253924Shrs		*)
1330253924Shrs			_n=$(${IFCONFIG_CMD} gif create)
1331253924Shrs			${IFCONFIG_CMD} $_n name $ifn
1332253924Shrs		;;
1333253924Shrs		esac
1334253924Shrs		if [ $? -eq 0 ]; then
1335256039Shrs			_list="$_list $ifn"
1336253924Shrs		fi
1337253924Shrs		tmpargs=$(get_if_var $ifn gifconfig_IF)
1338339680Seugen		eval ifconfig_${ifn}=\"\$ifconfig_${ifn} tunnel \$tmpargs\"
1339253924Shrs	done
1340256039Shrs	if [ -n "${_list# }" ]; then
1341256039Shrs		echo "Created clone interfaces: ${_list# }."
1342253924Shrs	fi
1343256039Shrs	debug "Cloned: ${_list# }"
1344113674Smtm}
1345100280Sgordon
1346197147Shrs# clone_down
1347197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1348197147Shrs#	standard output.
1349113674Smtm#
1350113674Smtmclone_down()
1351113674Smtm{
1352256039Shrs	local _list ifn _difn ifopt _iflist _sticky
1353113674Smtm	_list=
1354253924Shrs	_iflist=$*
1355197139Shrs
1356253924Shrs	: ${cloned_interfaces_sticky:=NO}
1357253924Shrs	if checkyesno cloned_interfaces_sticky; then
1358253924Shrs		_sticky=1
1359253924Shrs	else
1360253924Shrs		_sticky=0
1361253924Shrs	fi
1362253924Shrs	for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1363253924Shrs		# Parse ifn:ifopt.
1364253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1365253924Shrs		case $ifopt:$_sticky in
1366253924Shrs		sticky:*)	continue ;;	# :sticky => not destroy
1367253924Shrs		nosticky:*)	;;		# :nosticky => destroy
1368253924Shrs		*:1)		continue ;;	# global sticky knob == 1
1369253924Shrs		esac
1370253924Shrs		case $_iflist in
1371253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1372253924Shrs		*)	continue ;;
1373253924Shrs		esac
1374256039Shrs		case $ifn in
1375256039Shrs		epair[0-9]*)
1376256039Shrs			# Note: epair(4) uses epair[0-9] for removal and
1377256039Shrs			# epair[0-9][ab] for configuration.
1378256039Shrs			#
1379256039Shrs			# Skip if both of ${ifn}a and ${ifn}b do not exist.
1380256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1381256039Shrs				_difn=${ifn}a
1382256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1383256039Shrs				_difn=${ifn}b
1384256039Shrs			else
1385256039Shrs				continue
1386256039Shrs			fi
1387256039Shrs			${IFCONFIG_CMD} -n $_difn destroy
1388256039Shrs			if [ $? -eq 0 ]; then
1389256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1390256039Shrs			fi
1391256039Shrs		;;
1392256039Shrs		*)
1393256039Shrs			# Skip if ifn does not exist.
1394256039Shrs			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1395256039Shrs				continue
1396256039Shrs			fi
1397256039Shrs			${IFCONFIG_CMD} -n ${ifn} destroy
1398256039Shrs			if [ $? -eq 0 ]; then
1399256039Shrs				_list="$_list $ifn"
1400256039Shrs			fi
1401256039Shrs		;;
1402256039Shrs		esac
1403113674Smtm	done
1404256039Shrs	if [ -n "${_list# }" ]; then
1405256039Shrs		echo "Destroyed clone interfaces: ${_list# }."
1406253924Shrs	fi
1407256039Shrs	debug "Destroyed clones: ${_list# }"
1408100280Sgordon}
1409100280Sgordon
1410197147Shrs# childif_create
1411197147Shrs#	Create and configure child interfaces.  Return 0 if child
1412197147Shrs#	interfaces are created.
1413178356Ssam#
1414178356Ssamchildif_create()
1415178356Ssam{
1416201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1417178356Ssam	cfg=1
1418178356Ssam	ifn=$1
1419178356Ssam
1420178527Sbrooks	# Create wireless interfaces
1421178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1422178527Sbrooks
1423178527Sbrooks	for child in ${child_wlans}; do
1424183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1425189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1426189759Sbrooks
1427178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1428252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1429189759Sbrooks			if [ -n "${debug_flags}" ]; then
1430189759Sbrooks				wlandebug -i $child ${debug_flags}
1431189759Sbrooks			fi
1432178356Ssam		else
1433252015Shrs			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1434189759Sbrooks			if [ -n "${debug_flags}" ]; then
1435189759Sbrooks				wlandebug -i $i ${debug_flags}
1436189759Sbrooks			fi
1437252015Shrs			${IFCONFIG_CMD} $i name $child && cfg=0
1438178356Ssam		fi
1439188118Sthompsa		if autoif $child; then
1440188118Sthompsa			ifn_start $child
1441188118Sthompsa		fi
1442178356Ssam	done
1443178356Ssam
1444201215Sjhb	# Create vlan interfaces
1445201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1446201215Sjhb
1447201215Sjhb	if [ -n "${child_vlans}" ]; then
1448201215Sjhb		load_kld if_vlan
1449201215Sjhb	fi
1450201215Sjhb
1451201215Sjhb	for child in ${child_vlans}; do
1452201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1453201215Sjhb			child="${ifn}.${child}"
1454201215Sjhb			create_args=`get_if_var $child create_args_IF`
1455252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1456201215Sjhb		else
1457201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1458201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1459252015Shrs				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1460201215Sjhb			else
1461252015Shrs				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1462252015Shrs				${IFCONFIG_CMD} $i name $child && cfg=0
1463201215Sjhb			fi
1464201215Sjhb		fi
1465201215Sjhb		if autoif $child; then
1466201215Sjhb			ifn_start $child
1467201215Sjhb		fi
1468201215Sjhb	done
1469201215Sjhb
1470179001Sbrooks	return ${cfg}
1471178356Ssam}
1472178356Ssam
1473197147Shrs# childif_destroy
1474197147Shrs#	Destroy child interfaces.
1475178356Ssam#
1476178356Ssamchildif_destroy()
1477178356Ssam{
1478201215Sjhb	local cfg child child_vlans child_wlans ifn
1479197139Shrs	cfg=1
1480178356Ssam
1481201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1482178527Sbrooks	for child in ${child_wlans}; do
1483201215Sjhb		if ! ifexists $child; then
1484201215Sjhb			continue
1485201215Sjhb		fi
1486252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1487178356Ssam	done
1488197139Shrs
1489201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1490201215Sjhb	for child in ${child_vlans}; do
1491201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1492201215Sjhb			child="${ifn}.${child}"
1493201215Sjhb		fi
1494201215Sjhb		if ! ifexists $child; then
1495201215Sjhb			continue
1496201215Sjhb		fi
1497252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1498201215Sjhb	done
1499201215Sjhb
1500197139Shrs	return ${cfg}
1501178356Ssam}
1502178356Ssam
1503197147Shrs# ng_mkpeer
1504197147Shrs#	Create netgraph nodes.
1505166583Sflz#
1506197147Shrsng_mkpeer()
1507197147Shrs{
1508166583Sflz	ngctl -f - 2> /dev/null <<EOF
1509166583Sflzmkpeer $*
1510166583Sflzmsg dummy nodeinfo
1511166583SflzEOF
1512166583Sflz}
1513166583Sflz
1514197147Shrs# ng_create_one
1515197147Shrs#	Create netgraph nodes.
1516197147Shrs#
1517197147Shrsng_create_one()
1518197147Shrs{
1519197139Shrs	local t
1520197139Shrs
1521166583Sflz	ng_mkpeer $* | while read line; do
1522166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1523166583Sflz		if [ -n "${t}" ]; then
1524166583Sflz			echo ${t}
1525166583Sflz			return
1526166583Sflz		fi
1527166583Sflz	done
1528166583Sflz}
1529166583Sflz
1530166583Sflz# ng_fec_create ifn
1531197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1532197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1533197139Shrsng_fec_create()
1534197139Shrs{
1535166583Sflz	 local req_iface iface bogus
1536166583Sflz	 req_iface="$1"
1537166583Sflz
1538166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1539166583Sflz
1540166583Sflz	 bogus=""
1541166583Sflz	 while true; do
1542166583Sflz		 iface=`ng_create_one fec dummy fec`
1543166583Sflz		 if [ -z "${iface}" ]; then
1544166583Sflz			 exit 2
1545166583Sflz		 fi
1546166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1547166583Sflz			 break
1548166583Sflz		 fi
1549166583Sflz		 bogus="${bogus} ${iface}"
1550166583Sflz	 done
1551166583Sflz
1552166583Sflz	 for iface in ${bogus}; do
1553166583Sflz		 ngctl shutdown ${iface}:
1554166583Sflz	 done
1555166583Sflz}
1556166583Sflz
1557197147Shrs# fec_up
1558197147Shrs#	Create Fast EtherChannel interfaces.
1559197147Shrsfec_up()
1560197147Shrs{
1561197139Shrs	local i j
1562197139Shrs
1563166583Sflz	for i in ${fec_interfaces}; do
1564166583Sflz		ng_fec_create $i
1565166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1566166583Sflz			case ${j} in
1567100282Sdougb			'')
1568100282Sdougb				continue
1569100282Sdougb				;;
1570100282Sdougb			*)
1571166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1572100282Sdougb				;;
1573100282Sdougb			esac
1574100282Sdougb		done
1575166583Sflz	done
1576100282Sdougb}
1577100282Sdougb
1578113674Smtm# ipx_up ifn
1579197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1580197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1581113674Smtm#
1582113674Smtmipx_up()
1583100280Sgordon{
1584197139Shrs	local ifn
1585113674Smtm	ifn="$1"
1586197139Shrs
1587197139Shrs	# ifconfig_IF_ipx
1588197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1589113674Smtm	if [ -n "${ifconfig_args}" ]; then
1590252015Shrs		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1591113674Smtm		return 0
159285831Sdes	fi
1593197139Shrs
1594113674Smtm	return 1
1595113674Smtm}
159685831Sdes
1597116029Smtm# ipx_down ifn
1598116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1599116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1600113674Smtm#
1601116029Smtmipx_down()
1602116029Smtm{
1603197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1604197139Shrs	_if=$1
1605116100Smtm	_ifs="^"
1606116100Smtm	_ret=1
1607252015Shrs	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1608197139Shrs	oldifs="$IFS"
1609116100Smtm
1610116100Smtm	IFS="$_ifs"
1611116100Smtm	for _ipx in $ipxList ; do
1612116100Smtm		# get rid of extraneous line
1613116100Smtm		[ -z "$_ipx" ] && break
1614116100Smtm
1615116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1616116100Smtm
1617116100Smtm		IFS="$oldifs"
1618252015Shrs		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1619116100Smtm		IFS="$_ifs"
1620116100Smtm		_ret=0
1621116100Smtm	done
1622116100Smtm	IFS="$oldifs"
1623116100Smtm
1624116100Smtm	return $_ret
1625116029Smtm}
1626116029Smtm
1627253924Shrs# ifnet_rename [ifname]
1628253924Shrs#	Rename interfaces if ifconfig_IF_name is defined.
1629116029Smtm#
1630137070Spjdifnet_rename()
1631137070Spjd{
1632197139Shrs	local _if _ifname
1633137070Spjd
1634197139Shrs	# ifconfig_IF_name
1635253924Shrs	for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1636157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1637137070Spjd		if [ ! -z "$_ifname" ]; then
1638252015Shrs			${IFCONFIG_CMD} $_if name $_ifname
1639137070Spjd		fi
1640137070Spjd	done
1641197139Shrs
1642137070Spjd	return 0
1643137070Spjd}
1644137070Spjd
1645113674Smtm# list_net_interfaces type
1646113674Smtm#	List all network interfaces. The type of interface returned
1647113674Smtm#	can be controlled by the type argument. The type
1648113674Smtm#	argument can be any of the following:
1649197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1650197147Shrs#		dhcp	- list only DHCP configured interfaces
1651197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1652197139Shrs#				  Address Autoconf configured interfaces
1653197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1654197139Shrs#				  configured interfaces
1655113674Smtm#	If no argument is specified all network interfaces are output.
1656134429Syar#	Note that the list will include cloned interfaces if applicable.
1657134429Syar#	Cloned interfaces must already exist to have a chance to appear
1658134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1659113674Smtm#
1660113674Smtmlist_net_interfaces()
1661113674Smtm{
1662197139Shrs	local type _tmplist _list _autolist _lo _if
1663113674Smtm	type=$1
166465532Snectar
1665149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
166651231Ssheldonh	#
1667197139Shrs	_tmplist=
166851231Ssheldonh	case ${network_interfaces} in
166951231Ssheldonh	[Aa][Uu][Tt][Oo])
1670252015Shrs		_autolist="`${IFCONFIG_CMD} -l`"
1671149726Sbrooks		_lo=
1672149401Sbrooks		for _if in ${_autolist} ; do
1673149401Sbrooks			if autoif $_if; then
1674149726Sbrooks				if [ "$_if" = "lo0" ]; then
1675149726Sbrooks					_lo="lo0 "
1676149726Sbrooks				else
1677197139Shrs					_tmplist="${_tmplist} ${_if}"
1678149726Sbrooks				fi
1679149401Sbrooks			fi
1680149401Sbrooks		done
1681197139Shrs		_tmplist="${_lo}${_tmplist# }"
1682256039Shrs	;;
168383677Sbrooks	*)
1684256039Shrs		for _if in ${network_interfaces} ${cloned_interfaces}; do
1685256039Shrs			# epair(4) uses epair[0-9] for creation and
1686256039Shrs			# epair[0-9][ab] for configuration.
1687256039Shrs			case $_if in
1688256039Shrs			epair[0-9]*)
1689256039Shrs				_tmplist="$_tmplist ${_if}a ${_if}b"
1690256039Shrs			;;
1691256039Shrs			*)
1692256039Shrs				_tmplist="$_tmplist $_if"
1693256039Shrs			;;
1694256039Shrs			esac
1695256039Shrs		done
1696256039Shrs		#
1697196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1698196478Sdougb		#
1699196478Sdougb		case "$_tmplist" in
1700256039Shrs		lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1701256039Shrs			# This is fine, do nothing
1702256039Shrs			_tmplist="${_tmplist# }"
1703256039Shrs		;;
1704256039Shrs		*)
1705256039Shrs			_tmplist="lo0 ${_tmplist# }"
1706256039Shrs		;;
1707196478Sdougb		esac
1708256039Shrs	;;
170951231Ssheldonh	esac
171049122Sbrian
1711197139Shrs	_list=
1712197139Shrs	case "$type" in
1713197139Shrs	nodhcp)
1714197139Shrs		for _if in ${_tmplist} ; do
1715197139Shrs			if ! dhcpif $_if && \
1716197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1717197139Shrs				_list="${_list# } ${_if}"
1718197147Shrs			fi
1719197139Shrs		done
1720256039Shrs	;;
1721197139Shrs	dhcp)
1722197147Shrs		for _if in ${_tmplist} ; do
1723197147Shrs			if dhcpif $_if; then
1724197139Shrs				_list="${_list# } ${_if}"
1725197147Shrs			fi
1726197147Shrs		done
1727256039Shrs	;;
1728197139Shrs	noautoconf)
1729197139Shrs		for _if in ${_tmplist} ; do
1730197139Shrs			if ! ipv6_autoconfif $_if && \
1731197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1732197139Shrs				_list="${_list# } ${_if}"
1733197139Shrs			fi
1734197139Shrs		done
1735256039Shrs	;;
1736197139Shrs	autoconf)
1737197139Shrs		for _if in ${_tmplist} ; do
1738197139Shrs			if ipv6_autoconfif $_if; then
1739197139Shrs				_list="${_list# } ${_if}"
1740197139Shrs			fi
1741197139Shrs		done
1742256039Shrs	;;
1743197139Shrs	*)
1744197139Shrs		_list=${_tmplist}
1745256039Shrs	;;
1746113674Smtm	esac
1747197139Shrs
1748197139Shrs	echo $_list
1749197139Shrs
1750130151Sschweikh	return 0
175125184Sjkh}
1752114942Sume
1753179003Sbrooks# get_default_if -address_family
1754179003Sbrooks#	Get the interface of the default route for the given address family.
1755179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1756179003Sbrooks#
1757179003Sbrooksget_default_if()
1758179003Sbrooks{
1759197139Shrs	local routeget oldifs defif line
1760197139Shrs	defif=
1761179003Sbrooks	oldifs="$IFS"
1762179003Sbrooks	IFS="
1763179003Sbrooks"
1764197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1765179003Sbrooks		case $line in
1766179003Sbrooks		*interface:*)
1767179003Sbrooks			defif=${line##*: }
1768179003Sbrooks			;;
1769179003Sbrooks		esac
1770179003Sbrooks	done
1771179003Sbrooks	IFS=${oldifs}
1772179003Sbrooks
1773179003Sbrooks	echo $defif
1774179003Sbrooks}
1775179003Sbrooks
1776197147Shrs# hexdigit arg
1777197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1778114942Sumehexdigit()
1779114942Sume{
1780221884Sjilles	printf '%x\n' "$1"
1781114942Sume}
1782114942Sume
1783197147Shrs# hexprint arg
1784197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1785114942Sumehexprint()
1786114942Sume{
1787221884Sjilles	printf '%x\n' "$1"
1788114942Sume}
1789114942Sume
1790196436Sdougbis_wired_interface()
1791196436Sdougb{
1792196436Sdougb	local media
1793196436Sdougb
1794252015Shrs	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1795196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1796196436Sdougb	esac
1797196436Sdougb
1798196436Sdougb	test "$media" = "Ethernet"
1799196436Sdougb}
1800196436Sdougb
1801197147Shrs# network6_getladdr if [flag]
1802197147Shrs#	Echo link-local address from $if if any.
1803197147Shrs#	If flag is defined, tentative ones will be excluded.
1804114942Sumenetwork6_getladdr()
1805114942Sume{
1806252015Shrs	local _if _flag proto addr rest
1807252015Shrs	_if=$1
1808252015Shrs	_flag=$2
1809252015Shrs
1810252015Shrs	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1811252015Shrs		case "${proto}/${addr}/${_flag}/${rest}" in
1812252015Shrs		inet6/fe80::*//*)
1813252015Shrs			echo ${addr}
1814252015Shrs		;;
1815252015Shrs		inet6/fe80:://*tentative*)	# w/o flag
1816252015Shrs			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1817252015Shrs			network6_getladdr $_if $_flags
1818252015Shrs		;;
1819252015Shrs		inet6/fe80::/*/*tentative*)	# w/ flag
1820252015Shrs			echo ${addr}
1821252015Shrs		;;
1822252015Shrs		*)
1823252015Shrs			continue
1824252015Shrs		;;
1825114942Sume		esac
1826252015Shrs
1827252015Shrs		return
1828114942Sume	done
1829114942Sume}
1830