network.subr revision 256039
125184Sjkh#
2113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3113674Smtm#
4113674Smtm# Redistribution and use in source and binary forms, with or without
5113674Smtm# modification, are permitted provided that the following conditions
6113674Smtm# are met:
7113674Smtm# 1. Redistributions of source code must retain the above copyright
8113674Smtm#    notice, this list of conditions and the following disclaimer.
9113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
10113674Smtm#    notice, this list of conditions and the following disclaimer in the
11113674Smtm#    documentation and/or other materials provided with the distribution.
12113674Smtm#
13113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14113674Smtm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15113674Smtm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16113674Smtm# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17113674Smtm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18113674Smtm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19113674Smtm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20113674Smtm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21113674Smtm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22113674Smtm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23113674Smtm# SUCH DAMAGE.
24113674Smtm#
2550472Speter# $FreeBSD: head/etc/network.subr 256039 2013-10-04 02:44:04Z hrs $
2666830Sobrien#
27252015ShrsIFCONFIG_CMD="/sbin/ifconfig"
2825184Sjkh
29252015Shrs# Maximum number of addresses expanded from a address range specification.
30252015Shrs_IPEXPANDMAX=31
31252015Shrs
32113674Smtm#
33113674Smtm# Subroutines commonly used from network startup scripts.
34113674Smtm# Requires that rc.conf be loaded first.
35113674Smtm#
3625184Sjkh
37178356Ssam# ifn_start ifn
38197147Shrs#	Bring up and configure an interface.  If some configuration is
39225560Sbrueffer#	applied, print the interface configuration.
40178356Ssam#
41178356Ssamifn_start()
42178356Ssam{
43178356Ssam	local ifn cfg
44178356Ssam	ifn="$1"
45178356Ssam	cfg=1
46178356Ssam
47178356Ssam	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
48178356Ssam
49178356Ssam	ifscript_up ${ifn} && cfg=0
50178356Ssam	ifconfig_up ${ifn} && cfg=0
51222515Sbz	afexists inet && ipv4_up ${ifn} && cfg=0
52222515Sbz	afexists inet6 && ipv6_up ${ifn} && cfg=0
53222515Sbz	afexists ipx && ipx_up ${ifn} && cfg=0
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
71222515Sbz	afexists ipx && ipx_down ${ifn} && cfg=0
72222515Sbz	afexists inet6 && ipv6_down ${ifn} && cfg=0
73222515Sbz	afexists inet && ipv4_down ${ifn} && cfg=0
74178356Ssam	ifconfig_down ${ifn} && cfg=0
75178356Ssam	ifscript_down ${ifn} && cfg=0
76197139Shrs	childif_destroy ${ifn} && cfg=0
77178356Ssam
78178356Ssam	return $cfg
79178356Ssam}
80178356Ssam
81113674Smtm# ifconfig_up if
82113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
83113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
84113674Smtm#	arguments were found and executed or 1 if the interface
85147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
86147088Sbrooks#	here.
87113674Smtm#
88113674Smtmifconfig_up()
89113674Smtm{
90197139Shrs	local _cfg _ipv6_opts ifconfig_args
91147088Sbrooks	_cfg=1
92147088Sbrooks
93222515Sbz	# Make sure lo0 always comes up.
94222515Sbz	if [ "$1" = "lo0" ]; then
95222515Sbz		_cfg=0
96222515Sbz	fi
97222515Sbz
98197139Shrs	# ifconfig_IF
99147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
100113674Smtm	if [ -n "${ifconfig_args}" ]; then
101252015Shrs		eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
102147088Sbrooks		_cfg=0
103113674Smtm	fi
104147088Sbrooks
105197139Shrs	# inet6 specific
106197139Shrs	if afexists inet6; then
107222733Shrs		if checkyesno ipv6_activate_all_interfaces; then
108222733Shrs			_ipv6_opts="-ifdisabled"
109222746Shrs		elif [ "$1" != "lo0" ]; then
110222733Shrs			_ipv6_opts="ifdisabled"
111212574Shrs		fi
112197139Shrs
113222733Shrs		# backward compatibility: $ipv6_enable
114222733Shrs		case $ipv6_enable in
115222733Shrs		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
116253520Shrs			case $1 in
117253520Shrs			bridge[0-9]*)
118253520Shrs				# No accept_rtadv by default on if_bridge(4)
119253520Shrs				# to avoid a conflict with the member
120253520Shrs				# interfaces.
121253520Shrs			;;
122253520Shrs			*)
123253520Shrs				if ! checkyesno ipv6_gateway_enable; then
124253520Shrs					_ipv6_opts="${_ipv6_opts} accept_rtadv"
125253520Shrs				fi
126253520Shrs			;;
127253520Shrs			esac
128230453Shrs		;;
129222733Shrs		esac
130222733Shrs
131225521Shrs		case $ipv6_cpe_wanif in
132225521Shrs		$1)
133225521Shrs			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
134225521Shrs		;;
135225521Shrs		esac
136225521Shrs
137212574Shrs		if [ -n "${_ipv6_opts}" ]; then
138252015Shrs			${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
139197526Shrs		fi
140212574Shrs
141212574Shrs		# ifconfig_IF_ipv6
142212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
143212574Shrs		if [ -n "${ifconfig_args}" ]; then
144225522Shrs			# backward compatibility: inet6 keyword
145225522Shrs			case "${ifconfig_args}" in
146225522Shrs			:*|[0-9a-fA-F]*:*)
147225522Shrs				warn "\$ifconfig_$1_ipv6 needs " \
148225522Shrs				    "\"inet6\" keyword for an IPv6 address."
149225522Shrs				ifconfig_args="inet6 ${ifconfig_args}"
150225522Shrs			;;
151225522Shrs			esac
152252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
153252015Shrs			eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
154212574Shrs			_cfg=0
155212574Shrs		fi
156212574Shrs
157230453Shrs		# $ipv6_prefix_IF will be handled in
158230453Shrs		# ipv6_prefix_hostid_addr_common().
159230453Shrs		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
160230453Shrs		if [ -n "${ifconfig_args}" ]; then
161252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
162230453Shrs			_cfg=0
163230453Shrs		fi
164230453Shrs
165229783Suqs		# backward compatibility: $ipv6_ifconfig_IF
166212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
167212574Shrs		if [ -n "${ifconfig_args}" ]; then
168212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
169212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
170252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
171252015Shrs			eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
172212574Shrs			_cfg=0
173212574Shrs		fi
174197139Shrs	fi
175197139Shrs
176252426Shrs	ifalias $1 link alias
177252426Shrs	ifalias $1 ether alias
178252426Shrs
179197139Shrs	if [ ${_cfg} -eq 0 ]; then
180252015Shrs		${IFCONFIG_CMD} $1 up
181197139Shrs	fi
182197139Shrs
183147088Sbrooks	if wpaif $1; then
184147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
185147088Sbrooks		_cfg=0		# XXX: not sure this should count
186252230Srpaulo	elif hostapif $1; then
187252230Srpaulo		/etc/rc.d/hostapd start $1
188252230Srpaulo		_cfg=0
189147088Sbrooks	fi
190147088Sbrooks
191147088Sbrooks	if dhcpif $1; then
192149726Sbrooks		if [ $_cfg -ne 0 ] ; then
193252015Shrs			${IFCONFIG_CMD} $1 up
194149726Sbrooks		fi
195157706Sbrooks		if syncdhcpif $1; then
196157706Sbrooks			/etc/rc.d/dhclient start $1
197157706Sbrooks		fi
198147088Sbrooks		_cfg=0
199147088Sbrooks	fi
200147088Sbrooks
201147121Sbrooks	return $_cfg
202113674Smtm}
20325184Sjkh
204116029Smtm# ifconfig_down if
205161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
206161386Sbrooks#	the interface exists.
207116029Smtm#
208116029Smtmifconfig_down()
209116029Smtm{
210197139Shrs	local _cfg
211147121Sbrooks	_cfg=1
212116029Smtm
213147088Sbrooks	if wpaif $1; then
214147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
215147121Sbrooks		_cfg=0
216252230Srpaulo	elif hostapif $1; then
217252230Srpaulo		/etc/rc.d/hostapd stop $1
218252230Srpaulo		_cfg=0
219147088Sbrooks	fi
220147088Sbrooks
221147088Sbrooks	if dhcpif $1; then
222147088Sbrooks		/etc/rc.d/dhclient stop $1
223147088Sbrooks		_cfg=0
224147088Sbrooks	fi
225147088Sbrooks
226161386Sbrooks	if ifexists $1; then
227252015Shrs		${IFCONFIG_CMD} $1 down
228161386Sbrooks		_cfg=0
229161386Sbrooks	fi
230157706Sbrooks
231147121Sbrooks	return $_cfg
232116029Smtm}
233116029Smtm
234157706Sbrooks# get_if_var if var [default]
235197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
236197147Shrs#	$var is a string containg the sub-string "IF" which will be
237197147Shrs#	replaced with $if after the characters defined in _punct are
238197147Shrs#	replaced with '_'. If the variable is unset, replace it with
239197147Shrs#	$default if given.
240157706Sbrooksget_if_var()
241157706Sbrooks{
242212578Shrs	local _if _punct _punct_c _var _default prefix suffix
243197139Shrs
244157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
245157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
246157706Sbrooks	fi
247157706Sbrooks
248157706Sbrooks	_if=$1
249157706Sbrooks	_punct=". - / +"
250157736Sbrooks	for _punct_c in $_punct; do
251157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
252157706Sbrooks	done
253157706Sbrooks	_var=$2
254157706Sbrooks	_default=$3
255157706Sbrooks
256157706Sbrooks	prefix=${_var%%IF*}
257157706Sbrooks	suffix=${_var##*IF}
258168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
259157706Sbrooks}
260157706Sbrooks
261197139Shrs# _ifconfig_getargs if [af]
262225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
263225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
264147088Sbrooks#	outside this file.
265147088Sbrooks_ifconfig_getargs()
266147088Sbrooks{
267212574Shrs	local _ifn _af
268147088Sbrooks	_ifn=$1
269197139Shrs	_af=${2+_$2}
270197139Shrs
271147088Sbrooks	if [ -z "$_ifn" ]; then
272147088Sbrooks		return 1
273147088Sbrooks	fi
274147088Sbrooks
275212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
276147088Sbrooks}
277147088Sbrooks
278197139Shrs# ifconfig_getargs if [af]
279147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
280147088Sbrooks#	args such as DHCP and WPA.
281147088Sbrooksifconfig_getargs()
282147088Sbrooks{
283197139Shrs	local _tmpargs _arg _args
284197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
285147088Sbrooks	if [ $? -eq 1 ]; then
286147088Sbrooks		return 1
287147088Sbrooks	fi
288147088Sbrooks	_args=
289147088Sbrooks
290147088Sbrooks	for _arg in $_tmpargs; do
291147088Sbrooks		case $_arg in
292157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
293157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
294157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
295157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
296157706Sbrooks		[Ww][Pp][Aa]) ;;
297252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp]) ;;
298147088Sbrooks		*)
299147088Sbrooks			_args="$_args $_arg"
300147088Sbrooks			;;
301147088Sbrooks		esac
302147088Sbrooks	done
303147088Sbrooks
304147088Sbrooks	echo $_args
305147088Sbrooks}
306147088Sbrooks
307149401Sbrooks# autoif
308225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
309149401Sbrooks#	boot time and 1 otherwise.
310149401Sbrooksautoif()
311149401Sbrooks{
312197139Shrs	local _tmpargs _arg
313149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
314197139Shrs
315149401Sbrooks	for _arg in $_tmpargs; do
316149401Sbrooks		case $_arg in
317149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
318149401Sbrooks			return 1
319149401Sbrooks			;;
320149401Sbrooks		esac
321149401Sbrooks	done
322197139Shrs
323149401Sbrooks	return 0
324149401Sbrooks}
325149401Sbrooks
326147088Sbrooks# dhcpif if
327147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
328147088Sbrooksdhcpif()
329147088Sbrooks{
330197139Shrs	local _tmpargs _arg
331147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
332197139Shrs
333252360Sdelphij	case $1 in
334252360Sdelphij	lo[0-9]*|\
335252360Sdelphij	stf[0-9]*|\
336252360Sdelphij	faith[0-9]*|\
337252360Sdelphij	lp[0-9]*|\
338252360Sdelphij	sl[0-9]*)
339252360Sdelphij		return 1
340252360Sdelphij		;;
341252360Sdelphij	esac
342225849Sdelphij	if noafif $1; then
343225849Sdelphij		return 1
344225849Sdelphij	fi
345225849Sdelphij
346147088Sbrooks	for _arg in $_tmpargs; do
347147088Sbrooks		case $_arg in
348147088Sbrooks		[Dd][Hh][Cc][Pp])
349147088Sbrooks			return 0
350147088Sbrooks			;;
351157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
352157706Sbrooks			return 0
353157706Sbrooks			;;
354157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
355157706Sbrooks			return 0
356157706Sbrooks			;;
357147088Sbrooks		esac
358147088Sbrooks	done
359197139Shrs
360147088Sbrooks	return 1
361147088Sbrooks}
362147088Sbrooks
363157706Sbrooks# syncdhcpif
364157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
365157706Sbrooks#	1 otherwise.
366157706Sbrookssyncdhcpif()
367157706Sbrooks{
368197139Shrs	local _tmpargs _arg
369157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
370197139Shrs
371225849Sdelphij	if noafif $1; then
372225849Sdelphij		return 1
373225849Sdelphij	fi
374225849Sdelphij
375157706Sbrooks	for _arg in $_tmpargs; do
376157706Sbrooks		case $_arg in
377157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
378157706Sbrooks			return 1
379157706Sbrooks			;;
380157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
381157706Sbrooks			return 0
382157706Sbrooks			;;
383157706Sbrooks		esac
384157706Sbrooks	done
385197139Shrs
386197139Shrs	checkyesno synchronous_dhclient
387157706Sbrooks}
388157706Sbrooks
389147088Sbrooks# wpaif if
390147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
391147088Sbrookswpaif()
392147088Sbrooks{
393197139Shrs	local _tmpargs _arg
394147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
395197139Shrs
396147088Sbrooks	for _arg in $_tmpargs; do
397147088Sbrooks		case $_arg in
398147088Sbrooks		[Ww][Pp][Aa])
399147088Sbrooks			return 0
400147088Sbrooks			;;
401147088Sbrooks		esac
402147088Sbrooks	done
403197139Shrs
404147088Sbrooks	return 1
405147088Sbrooks}
406147088Sbrooks
407252230Srpaulo# hostapif if
408252230Srpaulo#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
409252230Srpaulohostapif()
410252230Srpaulo{
411252230Srpaulo	local _tmpargs _arg
412252230Srpaulo	_tmpargs=`_ifconfig_getargs $1`
413252230Srpaulo
414252230Srpaulo	for _arg in $_tmpargs; do
415252230Srpaulo		case $_arg in
416252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp])
417252230Srpaulo			return 0
418252230Srpaulo			;;
419252230Srpaulo		esac
420252230Srpaulo	done
421252230Srpaulo
422252230Srpaulo	return 1
423252230Srpaulo}
424252230Srpaulo
425197139Shrs# afexists af
426197139Shrs#	Returns 0 if the address family is enabled in the kernel
427197139Shrs#	1 otherwise.
428197139Shrsafexists()
429197139Shrs{
430197139Shrs	local _af
431197139Shrs	_af=$1
432197139Shrs
433197139Shrs	case ${_af} in
434222996Shrs	inet|inet6)
435222996Shrs		check_kern_features ${_af}
436197139Shrs		;;
437197697Shrs	ipx)
438197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
439197697Shrs		;;
440197697Shrs	atm)
441197697Shrs		if [ -x /sbin/atmconfig ]; then
442197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
443197697Shrs		else
444197697Shrs			return 1
445197697Shrs		fi
446197697Shrs		;;
447252426Shrs	link|ether)
448252426Shrs		return 0
449252426Shrs		;;
450197139Shrs	*)
451197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
452197139Shrs		;;
453197139Shrs	esac
454197139Shrs}
455197139Shrs
456212574Shrs# noafif if
457212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
458212574Shrsnoafif()
459212574Shrs{
460212574Shrs	local _if
461212574Shrs	_if=$1
462212574Shrs
463212574Shrs	case $_if in
464212574Shrs	pflog[0-9]*|\
465212574Shrs	pfsync[0-9]*|\
466212574Shrs	an[0-9]*|\
467212574Shrs	ath[0-9]*|\
468212574Shrs	ipw[0-9]*|\
469212577Shrs	ipfw[0-9]*|\
470212574Shrs	iwi[0-9]*|\
471212574Shrs	iwn[0-9]*|\
472212574Shrs	ral[0-9]*|\
473212574Shrs	wi[0-9]*|\
474212574Shrs	wl[0-9]*|\
475212574Shrs	wpi[0-9]*)
476212574Shrs		return 0
477212574Shrs		;;
478212574Shrs	esac
479212574Shrs
480212574Shrs	return 1
481212574Shrs}
482212574Shrs
483162490Sbrooks# ipv6if if
484162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
485162490Sbrooks#	1 otherwise.
486162490Sbrooksipv6if()
487162490Sbrooks{
488212574Shrs	local _if _tmpargs i
489212574Shrs	_if=$1
490212574Shrs
491197139Shrs	if ! afexists inet6; then
492162490Sbrooks		return 1
493162490Sbrooks	fi
494197139Shrs
495197139Shrs	# lo0 is always IPv6-enabled
496212574Shrs	case $_if in
497197139Shrs	lo0)
498197139Shrs		return 0
499197139Shrs		;;
500197139Shrs	esac
501197139Shrs
502212574Shrs	case "${ipv6_network_interfaces}" in
503212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
504212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
505212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
506212575Shrs		if [ -n "${_tmpargs}" ]; then
507212575Shrs			return 0
508212575Shrs		fi
509197139Shrs
510230453Shrs		# True if $ipv6_prefix_IF is defined.
511230453Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
512230453Shrs		if [ -n "${_tmpargs}" ]; then
513230453Shrs			return 0
514230453Shrs		fi
515230453Shrs
516212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
517212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
518212575Shrs		if [ -n "${_tmpargs}" ]; then
519212574Shrs			return 0
520212574Shrs		fi
521212575Shrs		;;
522212575Shrs	esac
523197139Shrs
524162490Sbrooks	return 1
525162490Sbrooks}
526162490Sbrooks
527197139Shrs# ipv6_autoconfif if
528197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
529225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
530197139Shrsipv6_autoconfif()
531197139Shrs{
532197139Shrs	local _if _tmpargs _arg
533197139Shrs	_if=$1
534197139Shrs
535212577Shrs	case $_if in
536252360Sdelphij	lo[0-9]*|\
537212577Shrs	stf[0-9]*|\
538212577Shrs	faith[0-9]*|\
539212577Shrs	lp[0-9]*|\
540212577Shrs	sl[0-9]*)
541197139Shrs		return 1
542212577Shrs		;;
543212577Shrs	esac
544212574Shrs	if noafif $_if; then
545212574Shrs		return 1
546212574Shrs	fi
547212577Shrs	if ! ipv6if $_if; then
548212577Shrs		return 1
549212577Shrs	fi
550197139Shrs	if checkyesno ipv6_gateway_enable; then
551197139Shrs		return 1
552197139Shrs	fi
553197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
554197526Shrs	if [ -n "${_tmpargs}" ]; then
555197526Shrs		return 1
556197526Shrs	fi
557212574Shrs	# backward compatibility: $ipv6_enable
558212574Shrs	case $ipv6_enable in
559212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
560242181Shrs		if checkyesno ipv6_gateway_enable; then
561242181Shrs			return 1
562253520Shrs		fi
563253520Shrs		case $1 in
564253520Shrs		bridge[0-9]*)
565253520Shrs			# No accept_rtadv by default on if_bridge(4)
566253520Shrs			# to avoid a conflict with the member
567253520Shrs			# interfaces.
568253520Shrs			return 1
569253520Shrs		;;
570253520Shrs		*)
571242181Shrs			return 0
572253520Shrs		;;
573253520Shrs		esac
574212577Shrs	;;
575197526Shrs	esac
576197526Shrs
577212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
578212574Shrs	for _arg in $_tmpargs; do
579212574Shrs		case $_arg in
580212574Shrs		accept_rtadv)
581212574Shrs			return 0
582212574Shrs			;;
583212574Shrs		esac
584212574Shrs	done
585212574Shrs
586212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
587212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
588212574Shrs	for _arg in $_tmpargs; do
589212574Shrs		case $_arg in
590212574Shrs		accept_rtadv)
591212574Shrs			return 0
592212574Shrs			;;
593212574Shrs		esac
594212574Shrs	done
595212574Shrs
596197139Shrs	return 1
597197139Shrs}
598197139Shrs
599161386Sbrooks# ifexists if
600161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
601161386Sbrooksifexists()
602161386Sbrooks{
603197139Shrs	[ -z "$1" ] && return 1
604252015Shrs	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
605161386Sbrooks}
606161386Sbrooks
607152441Sbrooks# ipv4_up if
608212578Shrs#	add IPv4 addresses to the interface $if
609152441Sbrooksipv4_up()
610152441Sbrooks{
611197139Shrs	local _if _ret
612152441Sbrooks	_if=$1
613197139Shrs	_ret=1
614197139Shrs
615222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
616222515Sbz	if [ "${_if}" = "lo0" ]; then
617226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
618222515Sbz		if [ -z "${ifconfig_args}" ]; then
619252015Shrs			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
620222515Sbz		fi
621222515Sbz	fi
622252015Shrs	ifalias ${_if} inet alias && _ret=0
623197139Shrs
624197139Shrs	return $_ret
625152441Sbrooks}
626152441Sbrooks
627197139Shrs# ipv6_up if
628197139Shrs#	add IPv6 addresses to the interface $if
629197139Shrsipv6_up()
630197139Shrs{
631197139Shrs	local _if _ret
632197139Shrs	_if=$1
633197139Shrs	_ret=1
634197139Shrs
635197139Shrs	if ! ipv6if $_if; then
636197139Shrs		return 0
637197139Shrs	fi
638197139Shrs
639252015Shrs	ifalias ${_if} inet6 alias && _ret=0
640226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
641197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
642197139Shrs
643197139Shrs	return $_ret
644197139Shrs}
645197139Shrs
646152441Sbrooks# ipv4_down if
647197147Shrs#	remove IPv4 addresses from the interface $if
648152441Sbrooksipv4_down()
649152441Sbrooks{
650197139Shrs	local _if _ifs _ret inetList oldifs _inet
651152441Sbrooks	_if=$1
652161386Sbrooks	_ifs="^"
653161386Sbrooks	_ret=1
654161386Sbrooks
655252015Shrs	ifalias ${_if} inet -alias && _ret=0
656161386Sbrooks
657255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
658252015Shrs
659161386Sbrooks	oldifs="$IFS"
660161386Sbrooks	IFS="$_ifs"
661161386Sbrooks	for _inet in $inetList ; do
662161386Sbrooks		# get rid of extraneous line
663253924Shrs		case $_inet in
664255653Shrs		inet\ *)	;;
665255653Shrs		*)		continue ;;
666253924Shrs		esac
667161386Sbrooks
668161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
669161386Sbrooks
670161386Sbrooks		IFS="$oldifs"
671252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet} delete
672161386Sbrooks		IFS="$_ifs"
673161386Sbrooks		_ret=0
674161386Sbrooks	done
675161386Sbrooks	IFS="$oldifs"
676161386Sbrooks
677161386Sbrooks	return $_ret
678152441Sbrooks}
679152441Sbrooks
680197139Shrs# ipv6_down if
681197139Shrs#	remove IPv6 addresses from the interface $if
682197139Shrsipv6_down()
683197139Shrs{
684197139Shrs	local _if _ifs _ret inetList oldifs _inet6
685197139Shrs	_if=$1
686197139Shrs	_ifs="^"
687197139Shrs	_ret=1
688197139Shrs
689197139Shrs	if ! ipv6if $_if; then
690197139Shrs		return 0
691197139Shrs	fi
692197139Shrs
693197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
694226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
695252015Shrs	ifalias ${_if} inet6 -alias && _ret=0
696197139Shrs
697255653Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
698197139Shrs
699197139Shrs	oldifs="$IFS"
700197139Shrs	IFS="$_ifs"
701197139Shrs	for _inet6 in $inetList ; do
702197139Shrs		# get rid of extraneous line
703255653Shrs		case $_inet in
704255653Shrs		inet6\ *)	;;
705255653Shrs		*)		continue ;;
706255653Shrs		esac
707197139Shrs
708197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
709197139Shrs
710197139Shrs		IFS="$oldifs"
711252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
712197139Shrs		IFS="$_ifs"
713197139Shrs		_ret=0
714197139Shrs	done
715197139Shrs	IFS="$oldifs"
716197139Shrs
717197139Shrs	return $_ret
718197139Shrs}
719197139Shrs
720252015Shrs# ifalias if af action
721252015Shrs#	Configure or remove aliases for network interface $if.
722113674Smtm#	It returns 0 if at least one alias was configured or
723252015Shrs#	removed, or 1 if there were none.
724113674Smtm#
725252015Shrsifalias()
726113674Smtm{
727197139Shrs	local _ret
728113674Smtm	_ret=1
729197139Shrs
730252015Shrs	afexists $2 || return $_ret
731252015Shrs
732197139Shrs	case "$2" in
733252426Shrs	inet|inet6|link|ether)
734252015Shrs		ifalias_af_common $1 $2 $3 && _ret=0
735197139Shrs		;;
736197139Shrs	esac
737197139Shrs
738197139Shrs	return $_ret
739197139Shrs}
740197139Shrs
741252015Shrs# ifalias_expand_addr af action addr
742252015Shrs#	Expand address range ("N-M") specification in addr.
743252015Shrs#	"addr" must not include an address-family keyword.
744252015Shrs#	The results will include an address-family keyword.
745197139Shrs#
746252015Shrsifalias_expand_addr()
747197139Shrs{
748253505Shrs	local _af _action
749197139Shrs
750253505Shrs	_af=$1
751253505Shrs	_action=$2
752253505Shrs	shift 2
753253505Shrs
754253505Shrs	afexists $_af || return
755253505Shrs	ifalias_expand_addr_$_af $_action $*
756197139Shrs}
757197139Shrs
758252015Shrs# ifalias_expand_addr_inet action addr
759252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv4.
760197139Shrs#
761252015Shrsifalias_expand_addr_inet()
762197139Shrs{
763253505Shrs	local _action _arg _cidr _cidr_addr _exargs
764252015Shrs	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
765252015Shrs	local _retstr _c
766252015Shrs	_action=$1
767252015Shrs	_arg=$2
768253505Shrs	shift 2
769253505Shrs	_exargs=$*
770252015Shrs	_retstr=
771197139Shrs
772253505Shrs	case $_action:$_arg:$_exargs in
773253505Shrs	*:*--*)		return ;;	# invalid
774253505Shrs	tmp:*[0-9]-[0-9]*:*)		# to be expanded
775253505Shrs		_action="alias"
776253505Shrs	;;
777253505Shrs	*:*[0-9]-[0-9]*:*)		# to be expanded
778253505Shrs	;;
779253505Shrs	tmp:*:*netmask*)		# already expanded w/ netmask option
780253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
781253505Shrs	;;
782253505Shrs	tmp:*:*)			# already expanded w/o netmask option
783253505Shrs		echo $_arg $_exargs && return
784253505Shrs	;;
785253505Shrs	*:*:*netmask*)			# already expanded w/ netmask option
786253505Shrs		echo inet ${_arg%/[0-9]*} $_exargs && return
787253505Shrs	;;
788253505Shrs	*:*:*)				# already expanded w/o netmask option
789253505Shrs		echo inet $_arg $_exargs && return
790253505Shrs	;;
791252015Shrs	esac
792252015Shrs
793252015Shrs	for _cidr in $_arg; do
794252015Shrs		_ipaddr=${_cidr%%/*}
795252015Shrs		_plen=${_cidr##*/}
796252015Shrs		# When subnet prefix length is not specified, use /32.
797252015Shrs		case $_plen in
798252015Shrs		$_ipaddr)	_plen=32 ;;	# "/" character not found
799197139Shrs		esac
800197139Shrs
801252015Shrs		OIFS=$IFS
802252015Shrs		IFS=. set -- $_ipaddr
803252015Shrs		_range=
804252015Shrs		_iphead=
805252015Shrs		_iptail=
806252015Shrs		for _c in $@; do
807252015Shrs			case $_range:$_c in
808252015Shrs			:[0-9]*-[0-9]*)
809252015Shrs				_range=$_c
810197139Shrs			;;
811252015Shrs			:*)
812252015Shrs				_iphead="${_iphead}${_iphead:+.}${_c}"
813197139Shrs			;;
814252015Shrs			*:*)
815252015Shrs				_iptail="${_iptail}${_iptail:+.}${_c}"
816252015Shrs			;;
817252015Shrs			esac
818252015Shrs		done
819252015Shrs		IFS=$OIFS
820252015Shrs		_iplow=${_range%-*}
821252015Shrs		_iphigh=${_range#*-}
822252015Shrs
823252015Shrs		# clear netmask when removing aliases
824252015Shrs		if [ "$_action" = "-alias" ]; then
825252015Shrs			_plen=""
826252015Shrs		fi
827252015Shrs
828252015Shrs		_ipcount=$_iplow
829252015Shrs		while [ "$_ipcount" -le "$_iphigh" ]; do
830252015Shrs			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
831252015Shrs			if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
832252015Shrs				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."
833252015Shrs				break
834252015Shrs			else
835252015Shrs				_ipcount=$(($_ipcount + 1))
836252015Shrs			fi
837252015Shrs			# Forcibly set /32 for remaining aliases.
838252015Shrs			_plen=32
839252015Shrs		done
840197139Shrs	done
841197139Shrs
842252015Shrs	for _c in $_retstr; do
843253505Shrs		ifalias_expand_addr_inet $_action $_c $_exargs
844252015Shrs	done
845113674Smtm}
846100280Sgordon
847252015Shrs# ifalias_expand_addr_inet6 action addr
848252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv6.
849116029Smtm#
850252015Shrsifalias_expand_addr_inet6()
851116029Smtm{
852253505Shrs	local _action _arg _cidr _cidr_addr _exargs
853252015Shrs	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
854252015Shrs	local _ipv4part
855252015Shrs	local _retstr _c
856252015Shrs	_action=$1
857252015Shrs	_arg=$2
858253505Shrs	shift 2
859253505Shrs	_exargs=$*
860252015Shrs	_retstr=
861197139Shrs
862253505Shrs	case $_action:$_arg:$_exargs in
863253505Shrs	*:*--*:*)	return ;;	# invalid
864253505Shrs	tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
865253505Shrs		_action="alias"
866253505Shrs	;;
867253505Shrs	*:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)	# to be expanded
868253505Shrs	;;
869253505Shrs	tmp:*:*prefixlen*)	# already expanded w/ prefixlen option
870253505Shrs		echo ${_arg%/[0-9]*} $_exargs && return
871253505Shrs	;;
872253505Shrs	tmp:*:*)		# already expanded w/o prefixlen option
873253505Shrs		echo $_arg $_exargs && return
874253505Shrs	;;
875253505Shrs	*:*:*prefixlen*)	# already expanded w/ prefixlen option
876253505Shrs		echo inet6 ${_arg%/[0-9]*} $_exargs && return
877253505Shrs	;;
878253505Shrs	*:*:*)			# already expanded w/o prefixlen option
879253505Shrs		echo inet6 $_arg $_exargs && return
880253505Shrs	;;
881197139Shrs	esac
882197139Shrs
883252015Shrs	for _cidr in $_arg; do
884252015Shrs		_ipaddr="${_cidr%%/*}"
885252015Shrs		_plen="${_cidr##*/}"
886252015Shrs
887252015Shrs		case $_action:$_ipaddr:$_cidr in
888252015Shrs		-alias:*:*)		unset _plen ;;
889252015Shrs		*:$_cidr:$_ipaddr)	unset _plen ;;
890252015Shrs		esac
891252015Shrs
892252015Shrs		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
893252015Shrs			# Handle !v4mapped && !v4compat addresses.
894252015Shrs
895252015Shrs			# The default prefix length is 64.
896252015Shrs			case $_ipaddr:$_cidr in
897252015Shrs			$_cidr:$_ipaddr)	_plen="64" ;;
898252015Shrs			esac
899252015Shrs			_ipleft=${_ipaddr%-*}
900252015Shrs			_ipright=${_ipaddr#*-}
901252015Shrs			_iplow=${_ipleft##*:}
902252015Shrs			_iphigh=${_ipright%%:*}
903252015Shrs			_ipleft=${_ipleft%:*}
904252015Shrs			_ipright=${_ipright#*:}
905252015Shrs
906252015Shrs			if [ "$_iphigh" = "$_ipright" ]; then
907252015Shrs				unset _ipright
908252015Shrs			else
909252015Shrs				_ipright=:$_ipright
910252015Shrs			fi
911252015Shrs
912252015Shrs			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
913252015Shrs				_iplow=$((0x$_iplow))
914252015Shrs				_iphigh=$((0x$_iphigh))
915252015Shrs				_ipcount=$_iplow
916252015Shrs				while [ $_ipcount -le $_iphigh ]; do
917252015Shrs					_r=`printf "%s:%04x%s%s" \
918252015Shrs					    $_ipleft $_ipcount $_ipright \
919252015Shrs					    ${_plen:+/}$_plen`
920252015Shrs					_retstr="$_retstr $_r"
921252015Shrs					if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
922252015Shrs					then
923252015Shrs						warn "Range specification is too large $(printf '(%s:%04x%s-%s:%04x%s)' $_ipleft $_iplow $_ipright $_ipleft $_iphigh $_ipright). $(printf '%s:%04x%s-%s:%04x%s' $_ipleft $_iplow $_ipright $_ipleft $_ipcount $_ipright) was processed."
924252015Shrs						break
925252015Shrs					else
926252015Shrs						_ipcount=$(($_ipcount + 1))
927252015Shrs					fi
928252015Shrs				done
929252015Shrs			else
930252015Shrs				_retstr="${_ipaddr}${_plen:+/}${_plen}"
931252015Shrs			fi
932252015Shrs
933252015Shrs			for _c in $_retstr; do
934253505Shrs				ifalias_expand_addr_inet6 $_action $_c $_exargs
935252015Shrs			done
936252015Shrs		else
937252015Shrs			# v4mapped/v4compat should handle as an IPv4 alias
938252015Shrs			_ipv4part=${_ipaddr##*:}
939252015Shrs
940252015Shrs			# Adjust prefix length if any.  If not, set the
941252015Shrs			# default prefix length as 32.
942252015Shrs			case $_ipaddr:$_cidr in
943252015Shrs			$_cidr:$_ipaddr)	_plen=32 ;;
944252015Shrs			*)			_plen=$(($_plen - 96)) ;;
945252015Shrs			esac
946252015Shrs
947252015Shrs			_retstr=`ifalias_expand_addr_inet \
948252015Shrs			    tmp ${_ipv4part}${_plen:+/}${_plen}`
949252015Shrs			for _c in $_retstr; do
950253505Shrs				ifalias_expand_addr_inet $_action $_c $_exargs
951252015Shrs			done
952252015Shrs		fi
953252015Shrs	done
954197139Shrs}
955197139Shrs
956252015Shrs# ifalias_af_common_handler if af action args
957252015Shrs#	Helper function for ifalias_af_common().
958197139Shrs#
959252015Shrsifalias_af_common_handler()
960197139Shrs{
961252015Shrs	local _ret _if _af _action _args _c _tmpargs
962252015Shrs
963197139Shrs	_ret=1
964252015Shrs	_if=$1
965252015Shrs	_af=$2
966252015Shrs	_action=$3
967252015Shrs	shift 3
968252015Shrs	_args=$*
969197139Shrs
970252015Shrs	case $_args in
971252015Shrs	${_af}\ *)	;;
972252015Shrs	*)	return	;;
973252015Shrs	esac
974252015Shrs
975252426Shrs	# link(ether) does not support address removal.
976252426Shrs	case $_af:$_action in
977252426Shrs	link:-alias|ether:-alias)	return ;;
978252426Shrs	esac
979252426Shrs
980252015Shrs	_tmpargs=
981252015Shrs	for _c in $_args; do
982252015Shrs		case $_c in
983252015Shrs		${_af})
984252015Shrs			case $_tmpargs in
985252015Shrs			${_af}\ *-*)
986252015Shrs				ifalias_af_common_handler $_if $_af $_action \
987252015Shrs				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
988197139Shrs			;;
989252015Shrs			${_af}\ *)
990252015Shrs				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
991197139Shrs			;;
992252015Shrs			esac
993252015Shrs			_tmpargs=$_af
994252015Shrs		;;
995252015Shrs		*)
996252015Shrs			_tmpargs="$_tmpargs $_c"
997252015Shrs		;;
998197139Shrs		esac
999197139Shrs	done
1000252015Shrs	# Process the last component if any.
1001252015Shrs	if [ -n "$_tmpargs}" ]; then
1002252015Shrs		case $_tmpargs in
1003252015Shrs		${_af}\ *-*)
1004252015Shrs			ifalias_af_common_handler $_if $_af $_action \
1005252015Shrs			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1006252015Shrs		;;
1007252015Shrs		${_af}\ *)
1008252015Shrs			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1009252015Shrs		;;
1010252015Shrs		esac
1011252015Shrs	fi
1012197139Shrs
1013197139Shrs	return $_ret
1014197139Shrs}
1015197139Shrs
1016252015Shrs# ifalias_af_common if af action
1017252015Shrs#	Helper function for ifalias().
1018197139Shrs#
1019252015Shrsifalias_af_common()
1020197139Shrs{
1021252015Shrs	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1022252015Shrs
1023197139Shrs	_ret=1
1024252015Shrs	_aliasn=
1025252015Shrs	_if=$1
1026252015Shrs	_af=$2
1027252015Shrs	_action=$3
1028197139Shrs
1029252015Shrs	# ifconfig_IF_aliasN which starts with $_af
1030197139Shrs	alias=0
1031197139Shrs	while : ; do
1032252015Shrs		ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1033252015Shrs		_iaf=
1034252015Shrs		case $ifconfig_args in
1035252015Shrs		inet\ *)	_iaf=inet ;;
1036252015Shrs		inet6\ *)	_iaf=inet6 ;;
1037252015Shrs		ipx\ *)		_iaf=ipx ;;
1038252426Shrs		link\ *)	_iaf=link ;;
1039252426Shrs		ether\ *)	_iaf=ether ;;
1040252015Shrs		esac
1041252015Shrs
1042252015Shrs		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1043252015Shrs		${_af}:*:${_af}:*)
1044252015Shrs			_aliasn="$_aliasn $ifconfig_args"
1045197139Shrs			;;
1046252015Shrs		${_af}:*:"":"")
1047116029Smtm			break
1048197139Shrs			;;
1049252015Shrs		inet:alias:"":*)
1050252015Shrs			_aliasn="$_aliasn inet $ifconfig_args"
1051252015Shrs			warn "\$ifconfig_${_if}_alias${alias} needs " \
1052252015Shrs			    "\"inet\" keyword for an IPv4 address."
1053197139Shrs		esac
1054252015Shrs		alias=$(($alias + 1))
1055116029Smtm	done
1056197139Shrs
1057197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
1058252015Shrs	case $_af in
1059252015Shrs	inet6)
1060252015Shrs		alias=0
1061252015Shrs		while : ; do
1062252015Shrs			ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1063252015Shrs			case ${_action}:"${ifconfig_args}" in
1064252015Shrs			*:"")
1065252015Shrs				break
1066197139Shrs			;;
1067252015Shrs			alias:*)
1068252015Shrs				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
1069252015Shrs				warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1070252015Shrs				    "is obsolete.  Use ifconfig_$1_aliasN " \
1071252015Shrs				    "instead."
1072252015Shrs			;;
1073252015Shrs			esac
1074252015Shrs			alias=$(($alias + 1))
1075252015Shrs		done
1076252015Shrs	esac
1077252015Shrs
1078252015Shrs	# backward compatibility: ipv4_addrs_IF.
1079252015Shrs	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1080252015Shrs		_aliasn="$_aliasn inet $_tmpargs"
1081252015Shrs	done
1082252015Shrs
1083252015Shrs	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1084252015Shrs	_tmpargs=
1085252015Shrs	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1086252015Shrs		case $_c in
1087252426Shrs		inet|inet6|ipx|link|ether)
1088252015Shrs			case $_tmpargs in
1089252015Shrs			${_af}\ *)
1090252015Shrs				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1091252015Shrs			;;
1092252015Shrs			esac
1093252015Shrs			_tmpargs=$_c
1094252015Shrs		;;
1095197139Shrs		*)
1096252015Shrs			_tmpargs="$_tmpargs $_c"
1097197139Shrs		esac
1098197139Shrs	done
1099252015Shrs	# Process the last component
1100252015Shrs	case $_tmpargs in
1101252015Shrs	${_af}\ *)
1102252015Shrs		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1103252015Shrs	;;
1104252015Shrs	esac
1105197139Shrs
1106116029Smtm	return $_ret
1107116029Smtm}
1108116029Smtm
1109226652Shrs# ipv6_prefix_hostid_addr_common if action
1110226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
1111226652Shrs#
1112226652Shrsipv6_prefix_hostid_addr_common()
1113197139Shrs{
1114253444Shrs	local _if _action prefix j
1115197139Shrs	_if=$1
1116226652Shrs	_action=$2
1117197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1118197139Shrs
1119197139Shrs	if [ -n "${prefix}" ]; then
1120197139Shrs		for j in ${prefix}; do
1121252015Shrs			# The default prefixlen is 64.
1122252015Shrs			plen=${j#*/}
1123252015Shrs			case $j:$plen in
1124252015Shrs			$plen:$j)	plen=64 ;;
1125252015Shrs			*)		j=${j%/*} ;;
1126252015Shrs			esac
1127197139Shrs
1128252015Shrs			# Normalize the last part by removing ":"
1129253444Shrs			j=${j%::*}
1130252015Shrs			j=${j%:}
1131253444Shrs			${IFCONFIG_CMD} ${_if} inet6 $j:: \
1132253444Shrs				prefixlen $plen eui64 ${_action}
1133252015Shrs
1134197139Shrs			# if I am a router, add subnet router
1135197139Shrs			# anycast address (RFC 2373).
1136197139Shrs			if checkyesno ipv6_gateway_enable; then
1137252015Shrs				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1138252015Shrs					prefixlen $plen ${_action} anycast
1139197139Shrs			fi
1140197139Shrs		done
1141197139Shrs	fi
1142197139Shrs}
1143197139Shrs
1144197139Shrs# ipv6_accept_rtadv_up if
1145197139Shrs#	Enable accepting Router Advertisement and send Router
1146197139Shrs#	Solicitation message
1147197139Shrsipv6_accept_rtadv_up()
1148197139Shrs{
1149197139Shrs	if ipv6_autoconfif $1; then
1150252015Shrs		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1151203433Sume		if ! checkyesno rtsold_enable; then
1152203433Sume			rtsol ${rtsol_flags} $1
1153203433Sume		fi
1154197139Shrs	fi
1155197139Shrs}
1156197139Shrs
1157197139Shrs# ipv6_accept_rtadv_down if
1158197139Shrs#	Disable accepting Router Advertisement
1159197139Shrsipv6_accept_rtadv_down()
1160197139Shrs{
1161197139Shrs	if ipv6_autoconfif $1; then
1162252015Shrs		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1163197139Shrs	fi
1164197139Shrs}
1165197139Shrs
1166113674Smtm# ifscript_up if
1167113674Smtm#	Evaluate a startup script for the $if interface.
1168113674Smtm#	It returns 0 if a script was found and processed or
1169113674Smtm#	1 if no script was found.
1170113674Smtm#
1171113674Smtmifscript_up()
1172100280Sgordon{
1173113674Smtm	if [ -r /etc/start_if.$1 ]; then
1174113674Smtm		. /etc/start_if.$1
1175113674Smtm		return 0
1176197139Shrs	else
1177197139Shrs		return 1
1178113674Smtm	fi
1179100280Sgordon}
1180100280Sgordon
1181116029Smtm# ifscript_down if
1182116029Smtm#	Evaluate a shutdown script for the $if interface.
1183116029Smtm#	It returns 0 if a script was found and processed or
1184116029Smtm#	1 if no script was found.
1185116029Smtm#
1186116029Smtmifscript_down()
1187116029Smtm{
1188116029Smtm	if [ -r /etc/stop_if.$1 ]; then
1189116029Smtm		. /etc/stop_if.$1
1190116029Smtm		return 0
1191197139Shrs	else
1192197139Shrs		return 1
1193116029Smtm	fi
1194116029Smtm}
1195116029Smtm
1196197147Shrs# clone_up
1197197147Shrs#	Create cloneable interfaces.
1198113674Smtm#
1199113674Smtmclone_up()
1200100280Sgordon{
1201256039Shrs	local _list ifn ifopt _iflist _n tmpargs
1202113674Smtm	_list=
1203253924Shrs	_iflist=$*
1204197139Shrs
1205197139Shrs	# create_args_IF
1206113674Smtm	for ifn in ${cloned_interfaces}; do
1207253924Shrs		# Parse ifn:ifopt.
1208253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1209253924Shrs		case $_iflist in
1210253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1211253924Shrs		*)	continue ;;
1212253924Shrs		esac
1213256039Shrs		case $ifn in
1214256039Shrs		epair[0-9]*)
1215256039Shrs			# epair(4) uses epair[0-9] for creation and
1216256039Shrs			# epair[0-9][ab] for configuration.
1217256039Shrs			#
1218256039Shrs			# Skip if ${ifn}a or ${ifn}b already exist.
1219256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1220256039Shrs				continue
1221256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1222256039Shrs				continue
1223256039Shrs			fi
1224256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1225256039Shrs			    `get_if_var ${ifn} create_args_IF`
1226256039Shrs			if [ $? -eq 0 ]; then
1227256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1228256039Shrs			fi
1229256039Shrs		;;
1230256039Shrs		*)
1231256039Shrs			# Skip if ${ifn} already exists.
1232256039Shrs			if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1233256039Shrs				continue
1234256039Shrs			fi
1235256039Shrs			${IFCONFIG_CMD} ${ifn} create \
1236256039Shrs			    `get_if_var ${ifn} create_args_IF`
1237256039Shrs			if [ $? -eq 0 ]; then
1238256039Shrs				_list="$_list $ifn"
1239256039Shrs			fi
1240256039Shrs		esac
1241113674Smtm	done
1242253924Shrs	if [ -n "$gif_interfaces" ]; then
1243253924Shrs		warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1244253924Shrs	fi
1245253924Shrs	for ifn in ${gif_interfaces}; do
1246253924Shrs		# Parse ifn:ifopt.
1247253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1248253924Shrs		case $_iflist in
1249253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1250253924Shrs		*)	continue ;;
1251253924Shrs		esac
1252253924Shrs		# Skip if ifn already exists.
1253253924Shrs		if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1254253924Shrs			continue
1255253924Shrs		fi
1256253924Shrs		case $ifn in
1257253924Shrs		gif[0-9]*)
1258253924Shrs			${IFCONFIG_CMD} $ifn create
1259253924Shrs		;;
1260253924Shrs		*)
1261253924Shrs			_n=$(${IFCONFIG_CMD} gif create)
1262253924Shrs			${IFCONFIG_CMD} $_n name $ifn
1263253924Shrs		;;
1264253924Shrs		esac
1265253924Shrs		if [ $? -eq 0 ]; then
1266256039Shrs			_list="$_list $ifn"
1267253924Shrs		fi
1268253924Shrs		tmpargs=$(get_if_var $ifn gifconfig_IF)
1269253924Shrs		eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1270253924Shrs	done
1271256039Shrs	if [ -n "${_list# }" ]; then
1272256039Shrs		echo "Created clone interfaces: ${_list# }."
1273253924Shrs	fi
1274256039Shrs	debug "Cloned: ${_list# }"
1275113674Smtm}
1276100280Sgordon
1277197147Shrs# clone_down
1278197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1279197147Shrs#	standard output.
1280113674Smtm#
1281113674Smtmclone_down()
1282113674Smtm{
1283256039Shrs	local _list ifn _difn ifopt _iflist _sticky
1284113674Smtm	_list=
1285253924Shrs	_iflist=$*
1286197139Shrs
1287253924Shrs	: ${cloned_interfaces_sticky:=NO}
1288253924Shrs	if checkyesno cloned_interfaces_sticky; then
1289253924Shrs		_sticky=1
1290253924Shrs	else
1291253924Shrs		_sticky=0
1292253924Shrs	fi
1293253924Shrs	for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1294253924Shrs		# Parse ifn:ifopt.
1295253924Shrs		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1296253924Shrs		case $ifopt:$_sticky in
1297253924Shrs		sticky:*)	continue ;;	# :sticky => not destroy
1298253924Shrs		nosticky:*)	;;		# :nosticky => destroy
1299253924Shrs		*:1)		continue ;;	# global sticky knob == 1
1300253924Shrs		esac
1301253924Shrs		case $_iflist in
1302253924Shrs		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1303253924Shrs		*)	continue ;;
1304253924Shrs		esac
1305256039Shrs		case $ifn in
1306256039Shrs		epair[0-9]*)
1307256039Shrs			# Note: epair(4) uses epair[0-9] for removal and
1308256039Shrs			# epair[0-9][ab] for configuration.
1309256039Shrs			#
1310256039Shrs			# Skip if both of ${ifn}a and ${ifn}b do not exist.
1311256039Shrs			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1312256039Shrs				_difn=${ifn}a
1313256039Shrs			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1314256039Shrs				_difn=${ifn}b
1315256039Shrs			else
1316256039Shrs				continue
1317256039Shrs			fi
1318256039Shrs			${IFCONFIG_CMD} -n $_difn destroy
1319256039Shrs			if [ $? -eq 0 ]; then
1320256039Shrs				_list="$_list ${ifn}a ${ifn}b"
1321256039Shrs			fi
1322256039Shrs		;;
1323256039Shrs		*)
1324256039Shrs			# Skip if ifn does not exist.
1325256039Shrs			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1326256039Shrs				continue
1327256039Shrs			fi
1328256039Shrs			${IFCONFIG_CMD} -n ${ifn} destroy
1329256039Shrs			if [ $? -eq 0 ]; then
1330256039Shrs				_list="$_list $ifn"
1331256039Shrs			fi
1332256039Shrs		;;
1333256039Shrs		esac
1334113674Smtm	done
1335256039Shrs	if [ -n "${_list# }" ]; then
1336256039Shrs		echo "Destroyed clone interfaces: ${_list# }."
1337253924Shrs	fi
1338256039Shrs	debug "Destroyed clones: ${_list# }"
1339100280Sgordon}
1340100280Sgordon
1341197147Shrs# childif_create
1342197147Shrs#	Create and configure child interfaces.  Return 0 if child
1343197147Shrs#	interfaces are created.
1344178356Ssam#
1345178356Ssamchildif_create()
1346178356Ssam{
1347201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1348178356Ssam	cfg=1
1349178356Ssam	ifn=$1
1350178356Ssam
1351178527Sbrooks	# Create wireless interfaces
1352178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1353178527Sbrooks
1354178527Sbrooks	for child in ${child_wlans}; do
1355183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1356189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1357189759Sbrooks
1358178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1359252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1360189759Sbrooks			if [ -n "${debug_flags}" ]; then
1361189759Sbrooks				wlandebug -i $child ${debug_flags}
1362189759Sbrooks			fi
1363178356Ssam		else
1364252015Shrs			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1365189759Sbrooks			if [ -n "${debug_flags}" ]; then
1366189759Sbrooks				wlandebug -i $i ${debug_flags}
1367189759Sbrooks			fi
1368252015Shrs			${IFCONFIG_CMD} $i name $child && cfg=0
1369178356Ssam		fi
1370188118Sthompsa		if autoif $child; then
1371188118Sthompsa			ifn_start $child
1372188118Sthompsa		fi
1373178356Ssam	done
1374178356Ssam
1375201215Sjhb	# Create vlan interfaces
1376201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1377201215Sjhb
1378201215Sjhb	if [ -n "${child_vlans}" ]; then
1379201215Sjhb		load_kld if_vlan
1380201215Sjhb	fi
1381201215Sjhb
1382201215Sjhb	for child in ${child_vlans}; do
1383201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1384201215Sjhb			child="${ifn}.${child}"
1385201215Sjhb			create_args=`get_if_var $child create_args_IF`
1386252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1387201215Sjhb		else
1388201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1389201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1390252015Shrs				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1391201215Sjhb			else
1392252015Shrs				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1393252015Shrs				${IFCONFIG_CMD} $i name $child && cfg=0
1394201215Sjhb			fi
1395201215Sjhb		fi
1396201215Sjhb		if autoif $child; then
1397201215Sjhb			ifn_start $child
1398201215Sjhb		fi
1399201215Sjhb	done
1400201215Sjhb
1401179001Sbrooks	return ${cfg}
1402178356Ssam}
1403178356Ssam
1404197147Shrs# childif_destroy
1405197147Shrs#	Destroy child interfaces.
1406178356Ssam#
1407178356Ssamchildif_destroy()
1408178356Ssam{
1409201215Sjhb	local cfg child child_vlans child_wlans ifn
1410197139Shrs	cfg=1
1411178356Ssam
1412201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1413178527Sbrooks	for child in ${child_wlans}; do
1414201215Sjhb		if ! ifexists $child; then
1415201215Sjhb			continue
1416201215Sjhb		fi
1417252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1418178356Ssam	done
1419197139Shrs
1420201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1421201215Sjhb	for child in ${child_vlans}; do
1422201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1423201215Sjhb			child="${ifn}.${child}"
1424201215Sjhb		fi
1425201215Sjhb		if ! ifexists $child; then
1426201215Sjhb			continue
1427201215Sjhb		fi
1428252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1429201215Sjhb	done
1430201215Sjhb
1431197139Shrs	return ${cfg}
1432178356Ssam}
1433178356Ssam
1434197147Shrs# ng_mkpeer
1435197147Shrs#	Create netgraph nodes.
1436166583Sflz#
1437197147Shrsng_mkpeer()
1438197147Shrs{
1439166583Sflz	ngctl -f - 2> /dev/null <<EOF
1440166583Sflzmkpeer $*
1441166583Sflzmsg dummy nodeinfo
1442166583SflzEOF
1443166583Sflz}
1444166583Sflz
1445197147Shrs# ng_create_one
1446197147Shrs#	Create netgraph nodes.
1447197147Shrs#
1448197147Shrsng_create_one()
1449197147Shrs{
1450197139Shrs	local t
1451197139Shrs
1452166583Sflz	ng_mkpeer $* | while read line; do
1453166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1454166583Sflz		if [ -n "${t}" ]; then
1455166583Sflz			echo ${t}
1456166583Sflz			return
1457166583Sflz		fi
1458166583Sflz	done
1459166583Sflz}
1460166583Sflz
1461166583Sflz# ng_fec_create ifn
1462197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1463197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1464197139Shrsng_fec_create()
1465197139Shrs{
1466166583Sflz	 local req_iface iface bogus
1467166583Sflz	 req_iface="$1"
1468166583Sflz
1469166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1470166583Sflz
1471166583Sflz	 bogus=""
1472166583Sflz	 while true; do
1473166583Sflz		 iface=`ng_create_one fec dummy fec`
1474166583Sflz		 if [ -z "${iface}" ]; then
1475166583Sflz			 exit 2
1476166583Sflz		 fi
1477166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1478166583Sflz			 break
1479166583Sflz		 fi
1480166583Sflz		 bogus="${bogus} ${iface}"
1481166583Sflz	 done
1482166583Sflz
1483166583Sflz	 for iface in ${bogus}; do
1484166583Sflz		 ngctl shutdown ${iface}:
1485166583Sflz	 done
1486166583Sflz}
1487166583Sflz
1488197147Shrs# fec_up
1489197147Shrs#	Create Fast EtherChannel interfaces.
1490197147Shrsfec_up()
1491197147Shrs{
1492197139Shrs	local i j
1493197139Shrs
1494166583Sflz	for i in ${fec_interfaces}; do
1495166583Sflz		ng_fec_create $i
1496166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1497166583Sflz			case ${j} in
1498100282Sdougb			'')
1499100282Sdougb				continue
1500100282Sdougb				;;
1501100282Sdougb			*)
1502166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1503100282Sdougb				;;
1504100282Sdougb			esac
1505100282Sdougb		done
1506166583Sflz	done
1507100282Sdougb}
1508100282Sdougb
1509113674Smtm# ipx_up ifn
1510197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1511197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1512113674Smtm#
1513113674Smtmipx_up()
1514100280Sgordon{
1515197139Shrs	local ifn
1516113674Smtm	ifn="$1"
1517197139Shrs
1518197139Shrs	# ifconfig_IF_ipx
1519197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1520113674Smtm	if [ -n "${ifconfig_args}" ]; then
1521252015Shrs		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1522113674Smtm		return 0
152385831Sdes	fi
1524197139Shrs
1525113674Smtm	return 1
1526113674Smtm}
152785831Sdes
1528116029Smtm# ipx_down ifn
1529116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1530116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1531113674Smtm#
1532116029Smtmipx_down()
1533116029Smtm{
1534197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1535197139Shrs	_if=$1
1536116100Smtm	_ifs="^"
1537116100Smtm	_ret=1
1538252015Shrs	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1539197139Shrs	oldifs="$IFS"
1540116100Smtm
1541116100Smtm	IFS="$_ifs"
1542116100Smtm	for _ipx in $ipxList ; do
1543116100Smtm		# get rid of extraneous line
1544116100Smtm		[ -z "$_ipx" ] && break
1545116100Smtm
1546116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1547116100Smtm
1548116100Smtm		IFS="$oldifs"
1549252015Shrs		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1550116100Smtm		IFS="$_ifs"
1551116100Smtm		_ret=0
1552116100Smtm	done
1553116100Smtm	IFS="$oldifs"
1554116100Smtm
1555116100Smtm	return $_ret
1556116029Smtm}
1557116029Smtm
1558253924Shrs# ifnet_rename [ifname]
1559253924Shrs#	Rename interfaces if ifconfig_IF_name is defined.
1560116029Smtm#
1561137070Spjdifnet_rename()
1562137070Spjd{
1563197139Shrs	local _if _ifname
1564137070Spjd
1565197139Shrs	# ifconfig_IF_name
1566253924Shrs	for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1567157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1568137070Spjd		if [ ! -z "$_ifname" ]; then
1569252015Shrs			${IFCONFIG_CMD} $_if name $_ifname
1570137070Spjd		fi
1571137070Spjd	done
1572197139Shrs
1573137070Spjd	return 0
1574137070Spjd}
1575137070Spjd
1576113674Smtm# list_net_interfaces type
1577113674Smtm#	List all network interfaces. The type of interface returned
1578113674Smtm#	can be controlled by the type argument. The type
1579113674Smtm#	argument can be any of the following:
1580197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1581197147Shrs#		dhcp	- list only DHCP configured interfaces
1582197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1583197139Shrs#				  Address Autoconf configured interfaces
1584197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1585197139Shrs#				  configured interfaces
1586113674Smtm#	If no argument is specified all network interfaces are output.
1587134429Syar#	Note that the list will include cloned interfaces if applicable.
1588134429Syar#	Cloned interfaces must already exist to have a chance to appear
1589134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1590113674Smtm#
1591113674Smtmlist_net_interfaces()
1592113674Smtm{
1593197139Shrs	local type _tmplist _list _autolist _lo _if
1594113674Smtm	type=$1
159565532Snectar
1596149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
159751231Ssheldonh	#
1598197139Shrs	_tmplist=
159951231Ssheldonh	case ${network_interfaces} in
160051231Ssheldonh	[Aa][Uu][Tt][Oo])
1601252015Shrs		_autolist="`${IFCONFIG_CMD} -l`"
1602149726Sbrooks		_lo=
1603149401Sbrooks		for _if in ${_autolist} ; do
1604149401Sbrooks			if autoif $_if; then
1605149726Sbrooks				if [ "$_if" = "lo0" ]; then
1606149726Sbrooks					_lo="lo0 "
1607149726Sbrooks				else
1608197139Shrs					_tmplist="${_tmplist} ${_if}"
1609149726Sbrooks				fi
1610149401Sbrooks			fi
1611149401Sbrooks		done
1612197139Shrs		_tmplist="${_lo}${_tmplist# }"
1613256039Shrs	;;
161483677Sbrooks	*)
1615256039Shrs		for _if in ${network_interfaces} ${cloned_interfaces}; do
1616256039Shrs			# epair(4) uses epair[0-9] for creation and
1617256039Shrs			# epair[0-9][ab] for configuration.
1618256039Shrs			case $_if in
1619256039Shrs			epair[0-9]*)
1620256039Shrs				_tmplist="$_tmplist ${_if}a ${_if}b"
1621256039Shrs			;;
1622256039Shrs			*)
1623256039Shrs				_tmplist="$_tmplist $_if"
1624256039Shrs			;;
1625256039Shrs			esac
1626256039Shrs		done
1627256039Shrs		#
1628196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1629196478Sdougb		#
1630196478Sdougb		case "$_tmplist" in
1631256039Shrs		lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1632256039Shrs			# This is fine, do nothing
1633256039Shrs			_tmplist="${_tmplist# }"
1634256039Shrs		;;
1635256039Shrs		*)
1636256039Shrs			_tmplist="lo0 ${_tmplist# }"
1637256039Shrs		;;
1638196478Sdougb		esac
1639256039Shrs	;;
164051231Ssheldonh	esac
164149122Sbrian
1642197139Shrs	_list=
1643197139Shrs	case "$type" in
1644197139Shrs	nodhcp)
1645197139Shrs		for _if in ${_tmplist} ; do
1646197139Shrs			if ! dhcpif $_if && \
1647197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1648197139Shrs				_list="${_list# } ${_if}"
1649197147Shrs			fi
1650197139Shrs		done
1651256039Shrs	;;
1652197139Shrs	dhcp)
1653197147Shrs		for _if in ${_tmplist} ; do
1654197147Shrs			if dhcpif $_if; then
1655197139Shrs				_list="${_list# } ${_if}"
1656197147Shrs			fi
1657197147Shrs		done
1658256039Shrs	;;
1659197139Shrs	noautoconf)
1660197139Shrs		for _if in ${_tmplist} ; do
1661197139Shrs			if ! ipv6_autoconfif $_if && \
1662197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1663197139Shrs				_list="${_list# } ${_if}"
1664197139Shrs			fi
1665197139Shrs		done
1666256039Shrs	;;
1667197139Shrs	autoconf)
1668197139Shrs		for _if in ${_tmplist} ; do
1669197139Shrs			if ipv6_autoconfif $_if; then
1670197139Shrs				_list="${_list# } ${_if}"
1671197139Shrs			fi
1672197139Shrs		done
1673256039Shrs	;;
1674197139Shrs	*)
1675197139Shrs		_list=${_tmplist}
1676256039Shrs	;;
1677113674Smtm	esac
1678197139Shrs
1679197139Shrs	echo $_list
1680197139Shrs
1681130151Sschweikh	return 0
168225184Sjkh}
1683114942Sume
1684179003Sbrooks# get_default_if -address_family
1685179003Sbrooks#	Get the interface of the default route for the given address family.
1686179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1687179003Sbrooks#
1688179003Sbrooksget_default_if()
1689179003Sbrooks{
1690197139Shrs	local routeget oldifs defif line
1691197139Shrs	defif=
1692179003Sbrooks	oldifs="$IFS"
1693179003Sbrooks	IFS="
1694179003Sbrooks"
1695197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1696179003Sbrooks		case $line in
1697179003Sbrooks		*interface:*)
1698179003Sbrooks			defif=${line##*: }
1699179003Sbrooks			;;
1700179003Sbrooks		esac
1701179003Sbrooks	done
1702179003Sbrooks	IFS=${oldifs}
1703179003Sbrooks
1704179003Sbrooks	echo $defif
1705179003Sbrooks}
1706179003Sbrooks
1707197147Shrs# hexdigit arg
1708197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1709114942Sumehexdigit()
1710114942Sume{
1711221884Sjilles	printf '%x\n' "$1"
1712114942Sume}
1713114942Sume
1714197147Shrs# hexprint arg
1715197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1716114942Sumehexprint()
1717114942Sume{
1718221884Sjilles	printf '%x\n' "$1"
1719114942Sume}
1720114942Sume
1721196436Sdougbis_wired_interface()
1722196436Sdougb{
1723196436Sdougb	local media
1724196436Sdougb
1725252015Shrs	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1726196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1727196436Sdougb	esac
1728196436Sdougb
1729196436Sdougb	test "$media" = "Ethernet"
1730196436Sdougb}
1731196436Sdougb
1732197147Shrs# network6_getladdr if [flag]
1733197147Shrs#	Echo link-local address from $if if any.
1734197147Shrs#	If flag is defined, tentative ones will be excluded.
1735114942Sumenetwork6_getladdr()
1736114942Sume{
1737252015Shrs	local _if _flag proto addr rest
1738252015Shrs	_if=$1
1739252015Shrs	_flag=$2
1740252015Shrs
1741252015Shrs	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1742252015Shrs		case "${proto}/${addr}/${_flag}/${rest}" in
1743252015Shrs		inet6/fe80::*//*)
1744252015Shrs			echo ${addr}
1745252015Shrs		;;
1746252015Shrs		inet6/fe80:://*tentative*)	# w/o flag
1747252015Shrs			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1748252015Shrs			network6_getladdr $_if $_flags
1749252015Shrs		;;
1750252015Shrs		inet6/fe80::/*/*tentative*)	# w/ flag
1751252015Shrs			echo ${addr}
1752252015Shrs		;;
1753252015Shrs		*)
1754252015Shrs			continue
1755252015Shrs		;;
1756114942Sume		esac
1757252015Shrs
1758252015Shrs		return
1759114942Sume	done
1760114942Sume}
1761