125184Sjkh#
2113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3113674Smtm#
4113674Smtm# Redistribution and use in source and binary forms, with or without
5113674Smtm# modification, are permitted provided that the following conditions
6113674Smtm# are met:
7113674Smtm# 1. Redistributions of source code must retain the above copyright
8113674Smtm#    notice, this list of conditions and the following disclaimer.
9113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
10113674Smtm#    notice, this list of conditions and the following disclaimer in the
11113674Smtm#    documentation and/or other materials provided with the distribution.
12113674Smtm#
13113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14113674Smtm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15113674Smtm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16113674Smtm# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17113674Smtm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18113674Smtm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19113674Smtm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20113674Smtm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21113674Smtm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22113674Smtm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23113674Smtm# SUCH DAMAGE.
24113674Smtm#
2550472Speter# $FreeBSD$
2666830Sobrien#
27253227ShrsIFCONFIG_CMD="/sbin/ifconfig"
2825184Sjkh
29253227Shrs# Maximum number of addresses expanded from a address range specification.
30253227Shrs_IPEXPANDMAX=31
31253227Shrs
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
101253227Shrs		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)
116253683Shrs			case $1 in
117253683Shrs			bridge[0-9]*)
118253683Shrs				# No accept_rtadv by default on if_bridge(4)
119253683Shrs				# to avoid a conflict with the member
120253683Shrs				# interfaces.
121253683Shrs			;;
122253683Shrs			*)
123253683Shrs				if ! checkyesno ipv6_gateway_enable; then
124253683Shrs					_ipv6_opts="${_ipv6_opts} accept_rtadv"
125253683Shrs				fi
126253683Shrs			;;
127253683Shrs			esac
128253422Shrs		;;
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
138253227Shrs			${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
152253227Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
153253227Shrs			eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
154212574Shrs			_cfg=0
155212574Shrs		fi
156212574Shrs
157253227Shrs		# $ipv6_prefix_IF will be handled in
158253227Shrs		# ipv6_prefix_hostid_addr_common().
159253227Shrs		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
160253227Shrs		if [ -n "${ifconfig_args}" ]; then
161253227Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
162253227Shrs			_cfg=0
163253227Shrs		fi
164253227Shrs
165253227Shrs		# 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."
170253227Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
171253227Shrs			eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
172212574Shrs			_cfg=0
173212574Shrs		fi
174197139Shrs	fi
175197139Shrs
176253227Shrs	ifalias $1 link alias
177253227Shrs	ifalias $1 ether alias
178253227Shrs
179197139Shrs	if [ ${_cfg} -eq 0 ]; then
180253227Shrs		${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
186252748Srpaulo	elif hostapif $1; then
187252748Srpaulo		/etc/rc.d/hostapd start $1
188252748Srpaulo		_cfg=0
189147088Sbrooks	fi
190147088Sbrooks
191147088Sbrooks	if dhcpif $1; then
192149726Sbrooks		if [ $_cfg -ne 0 ] ; then
193253227Shrs			${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
216252748Srpaulo	elif hostapif $1; then
217252748Srpaulo		/etc/rc.d/hostapd stop $1
218252748Srpaulo		_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
227253227Shrs		${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
249264439Sdteske	_punct=".-/+"
250264439Sdteske	ltr ${_if} "${_punct}" '_' _if
251157706Sbrooks	_var=$2
252157706Sbrooks	_default=$3
253157706Sbrooks
254157706Sbrooks	prefix=${_var%%IF*}
255157706Sbrooks	suffix=${_var##*IF}
256168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
257157706Sbrooks}
258157706Sbrooks
259197139Shrs# _ifconfig_getargs if [af]
260225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
261225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
262147088Sbrooks#	outside this file.
263147088Sbrooks_ifconfig_getargs()
264147088Sbrooks{
265212574Shrs	local _ifn _af
266147088Sbrooks	_ifn=$1
267197139Shrs	_af=${2+_$2}
268197139Shrs
269147088Sbrooks	if [ -z "$_ifn" ]; then
270147088Sbrooks		return 1
271147088Sbrooks	fi
272147088Sbrooks
273212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
274147088Sbrooks}
275147088Sbrooks
276197139Shrs# ifconfig_getargs if [af]
277147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
278147088Sbrooks#	args such as DHCP and WPA.
279147088Sbrooksifconfig_getargs()
280147088Sbrooks{
281197139Shrs	local _tmpargs _arg _args
282197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
283147088Sbrooks	if [ $? -eq 1 ]; then
284147088Sbrooks		return 1
285147088Sbrooks	fi
286147088Sbrooks	_args=
287147088Sbrooks
288147088Sbrooks	for _arg in $_tmpargs; do
289147088Sbrooks		case $_arg in
290157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
291157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
292157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
293157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
294157706Sbrooks		[Ww][Pp][Aa]) ;;
295252748Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp]) ;;
296147088Sbrooks		*)
297147088Sbrooks			_args="$_args $_arg"
298147088Sbrooks			;;
299147088Sbrooks		esac
300147088Sbrooks	done
301147088Sbrooks
302147088Sbrooks	echo $_args
303147088Sbrooks}
304147088Sbrooks
305149401Sbrooks# autoif
306225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
307149401Sbrooks#	boot time and 1 otherwise.
308149401Sbrooksautoif()
309149401Sbrooks{
310197139Shrs	local _tmpargs _arg
311149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
312197139Shrs
313149401Sbrooks	for _arg in $_tmpargs; do
314149401Sbrooks		case $_arg in
315149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
316149401Sbrooks			return 1
317149401Sbrooks			;;
318149401Sbrooks		esac
319149401Sbrooks	done
320197139Shrs
321149401Sbrooks	return 0
322149401Sbrooks}
323149401Sbrooks
324147088Sbrooks# dhcpif if
325147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
326147088Sbrooksdhcpif()
327147088Sbrooks{
328197139Shrs	local _tmpargs _arg
329147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
330197139Shrs
331252768Sdelphij	case $1 in
332252768Sdelphij	lo[0-9]*|\
333252768Sdelphij	stf[0-9]*|\
334252768Sdelphij	faith[0-9]*|\
335252768Sdelphij	lp[0-9]*|\
336252768Sdelphij	sl[0-9]*)
337252768Sdelphij		return 1
338252768Sdelphij		;;
339252768Sdelphij	esac
340227910Sdelphij	if noafif $1; then
341227910Sdelphij		return 1
342227910Sdelphij	fi
343227910Sdelphij
344147088Sbrooks	for _arg in $_tmpargs; do
345147088Sbrooks		case $_arg in
346147088Sbrooks		[Dd][Hh][Cc][Pp])
347147088Sbrooks			return 0
348147088Sbrooks			;;
349157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
350157706Sbrooks			return 0
351157706Sbrooks			;;
352157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
353157706Sbrooks			return 0
354157706Sbrooks			;;
355147088Sbrooks		esac
356147088Sbrooks	done
357197139Shrs
358147088Sbrooks	return 1
359147088Sbrooks}
360147088Sbrooks
361157706Sbrooks# syncdhcpif
362157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
363157706Sbrooks#	1 otherwise.
364157706Sbrookssyncdhcpif()
365157706Sbrooks{
366197139Shrs	local _tmpargs _arg
367157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
368197139Shrs
369227910Sdelphij	if noafif $1; then
370227910Sdelphij		return 1
371227910Sdelphij	fi
372227910Sdelphij
373157706Sbrooks	for _arg in $_tmpargs; do
374157706Sbrooks		case $_arg in
375157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
376157706Sbrooks			return 1
377157706Sbrooks			;;
378157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
379157706Sbrooks			return 0
380157706Sbrooks			;;
381157706Sbrooks		esac
382157706Sbrooks	done
383197139Shrs
384197139Shrs	checkyesno synchronous_dhclient
385157706Sbrooks}
386157706Sbrooks
387147088Sbrooks# wpaif if
388147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
389147088Sbrookswpaif()
390147088Sbrooks{
391197139Shrs	local _tmpargs _arg
392147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
393197139Shrs
394147088Sbrooks	for _arg in $_tmpargs; do
395147088Sbrooks		case $_arg in
396147088Sbrooks		[Ww][Pp][Aa])
397147088Sbrooks			return 0
398147088Sbrooks			;;
399147088Sbrooks		esac
400147088Sbrooks	done
401197139Shrs
402147088Sbrooks	return 1
403147088Sbrooks}
404147088Sbrooks
405252748Srpaulo# hostapif if
406252748Srpaulo#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
407252748Srpaulohostapif()
408252748Srpaulo{
409252748Srpaulo	local _tmpargs _arg
410252748Srpaulo	_tmpargs=`_ifconfig_getargs $1`
411252748Srpaulo
412252748Srpaulo	for _arg in $_tmpargs; do
413252748Srpaulo		case $_arg in
414252748Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp])
415252748Srpaulo			return 0
416252748Srpaulo			;;
417252748Srpaulo		esac
418252748Srpaulo	done
419252748Srpaulo
420252748Srpaulo	return 1
421252748Srpaulo}
422252748Srpaulo
423197139Shrs# afexists af
424197139Shrs#	Returns 0 if the address family is enabled in the kernel
425197139Shrs#	1 otherwise.
426197139Shrsafexists()
427197139Shrs{
428197139Shrs	local _af
429197139Shrs	_af=$1
430197139Shrs
431197139Shrs	case ${_af} in
432222996Shrs	inet|inet6)
433222996Shrs		check_kern_features ${_af}
434197139Shrs		;;
435197697Shrs	ipx)
436197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
437197697Shrs		;;
438197697Shrs	atm)
439197697Shrs		if [ -x /sbin/atmconfig ]; then
440197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
441197697Shrs		else
442197697Shrs			return 1
443197697Shrs		fi
444197697Shrs		;;
445253227Shrs	link|ether)
446253227Shrs		return 0
447253227Shrs		;;
448197139Shrs	*)
449197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
450197139Shrs		;;
451197139Shrs	esac
452197139Shrs}
453197139Shrs
454212574Shrs# noafif if
455212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
456212574Shrsnoafif()
457212574Shrs{
458212574Shrs	local _if
459212574Shrs	_if=$1
460212574Shrs
461212574Shrs	case $_if in
462212574Shrs	pflog[0-9]*|\
463212574Shrs	pfsync[0-9]*|\
464212574Shrs	an[0-9]*|\
465212574Shrs	ath[0-9]*|\
466212574Shrs	ipw[0-9]*|\
467212577Shrs	ipfw[0-9]*|\
468212574Shrs	iwi[0-9]*|\
469212574Shrs	iwn[0-9]*|\
470212574Shrs	ral[0-9]*|\
471212574Shrs	wi[0-9]*|\
472212574Shrs	wl[0-9]*|\
473212574Shrs	wpi[0-9]*)
474212574Shrs		return 0
475212574Shrs		;;
476212574Shrs	esac
477212574Shrs
478212574Shrs	return 1
479212574Shrs}
480212574Shrs
481162490Sbrooks# ipv6if if
482162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
483162490Sbrooks#	1 otherwise.
484162490Sbrooksipv6if()
485162490Sbrooks{
486212574Shrs	local _if _tmpargs i
487212574Shrs	_if=$1
488212574Shrs
489197139Shrs	if ! afexists inet6; then
490162490Sbrooks		return 1
491162490Sbrooks	fi
492197139Shrs
493197139Shrs	# lo0 is always IPv6-enabled
494212574Shrs	case $_if in
495197139Shrs	lo0)
496197139Shrs		return 0
497197139Shrs		;;
498197139Shrs	esac
499197139Shrs
500212574Shrs	case "${ipv6_network_interfaces}" in
501212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
502212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
503212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
504212575Shrs		if [ -n "${_tmpargs}" ]; then
505212575Shrs			return 0
506212575Shrs		fi
507197139Shrs
508253422Shrs		# True if $ipv6_prefix_IF is defined.
509253422Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
510253422Shrs		if [ -n "${_tmpargs}" ]; then
511253422Shrs			return 0
512253422Shrs		fi
513253422Shrs
514212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
515212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
516212575Shrs		if [ -n "${_tmpargs}" ]; then
517212574Shrs			return 0
518212574Shrs		fi
519212575Shrs		;;
520212575Shrs	esac
521197139Shrs
522162490Sbrooks	return 1
523162490Sbrooks}
524162490Sbrooks
525197139Shrs# ipv6_autoconfif if
526197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
527225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
528197139Shrsipv6_autoconfif()
529197139Shrs{
530197139Shrs	local _if _tmpargs _arg
531197139Shrs	_if=$1
532197139Shrs
533212577Shrs	case $_if in
534252768Sdelphij	lo[0-9]*|\
535212577Shrs	stf[0-9]*|\
536212577Shrs	faith[0-9]*|\
537212577Shrs	lp[0-9]*|\
538212577Shrs	sl[0-9]*)
539197139Shrs		return 1
540212577Shrs		;;
541212577Shrs	esac
542212574Shrs	if noafif $_if; then
543212574Shrs		return 1
544212574Shrs	fi
545212577Shrs	if ! ipv6if $_if; then
546212577Shrs		return 1
547212577Shrs	fi
548197139Shrs	if checkyesno ipv6_gateway_enable; then
549197139Shrs		return 1
550197139Shrs	fi
551197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
552197526Shrs	if [ -n "${_tmpargs}" ]; then
553197526Shrs		return 1
554197526Shrs	fi
555212574Shrs	# backward compatibility: $ipv6_enable
556212574Shrs	case $ipv6_enable in
557212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
558242187Shrs		if checkyesno ipv6_gateway_enable; then
559242187Shrs			return 1
560253683Shrs		fi
561253683Shrs		case $1 in
562253683Shrs		bridge[0-9]*)
563253683Shrs			# No accept_rtadv by default on if_bridge(4)
564253683Shrs			# to avoid a conflict with the member
565253683Shrs			# interfaces.
566253683Shrs			return 1
567253683Shrs		;;
568253683Shrs		*)
569242187Shrs			return 0
570253683Shrs		;;
571253683Shrs		esac
572212577Shrs	;;
573197526Shrs	esac
574197526Shrs
575212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
576212574Shrs	for _arg in $_tmpargs; do
577212574Shrs		case $_arg in
578212574Shrs		accept_rtadv)
579212574Shrs			return 0
580212574Shrs			;;
581212574Shrs		esac
582212574Shrs	done
583212574Shrs
584212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
585212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
586212574Shrs	for _arg in $_tmpargs; do
587212574Shrs		case $_arg in
588212574Shrs		accept_rtadv)
589212574Shrs			return 0
590212574Shrs			;;
591212574Shrs		esac
592212574Shrs	done
593212574Shrs
594197139Shrs	return 1
595197139Shrs}
596197139Shrs
597161386Sbrooks# ifexists if
598161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
599161386Sbrooksifexists()
600161386Sbrooks{
601197139Shrs	[ -z "$1" ] && return 1
602253227Shrs	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
603161386Sbrooks}
604161386Sbrooks
605152441Sbrooks# ipv4_up if
606212578Shrs#	add IPv4 addresses to the interface $if
607152441Sbrooksipv4_up()
608152441Sbrooks{
609197139Shrs	local _if _ret
610152441Sbrooks	_if=$1
611197139Shrs	_ret=1
612197139Shrs
613222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
614222515Sbz	if [ "${_if}" = "lo0" ]; then
615228245Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
616222515Sbz		if [ -z "${ifconfig_args}" ]; then
617253227Shrs			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
618222515Sbz		fi
619222515Sbz	fi
620253227Shrs	ifalias ${_if} inet alias && _ret=0
621197139Shrs
622197139Shrs	return $_ret
623152441Sbrooks}
624152441Sbrooks
625197139Shrs# ipv6_up if
626197139Shrs#	add IPv6 addresses to the interface $if
627197139Shrsipv6_up()
628197139Shrs{
629197139Shrs	local _if _ret
630197139Shrs	_if=$1
631197139Shrs	_ret=1
632197139Shrs
633197139Shrs	if ! ipv6if $_if; then
634197139Shrs		return 0
635197139Shrs	fi
636197139Shrs
637253227Shrs	ifalias ${_if} inet6 alias && _ret=0
638228245Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
639197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
640197139Shrs
641197139Shrs	return $_ret
642197139Shrs}
643197139Shrs
644152441Sbrooks# ipv4_down if
645197147Shrs#	remove IPv4 addresses from the interface $if
646152441Sbrooksipv4_down()
647152441Sbrooks{
648197139Shrs	local _if _ifs _ret inetList oldifs _inet
649152441Sbrooks	_if=$1
650161386Sbrooks	_ifs="^"
651161386Sbrooks	_ret=1
652161386Sbrooks
653253227Shrs	ifalias ${_if} inet -alias && _ret=0
654161386Sbrooks
655253227Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
656253227Shrs
657161386Sbrooks	oldifs="$IFS"
658161386Sbrooks	IFS="$_ifs"
659161386Sbrooks	for _inet in $inetList ; do
660161386Sbrooks		# get rid of extraneous line
661161386Sbrooks		[ -z "$_inet" ] && break
662161386Sbrooks
663161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
664161386Sbrooks
665161386Sbrooks		IFS="$oldifs"
666253227Shrs		${IFCONFIG_CMD} ${_if} ${_inet} delete
667161386Sbrooks		IFS="$_ifs"
668161386Sbrooks		_ret=0
669161386Sbrooks	done
670161386Sbrooks	IFS="$oldifs"
671161386Sbrooks
672161386Sbrooks	return $_ret
673152441Sbrooks}
674152441Sbrooks
675197139Shrs# ipv6_down if
676197139Shrs#	remove IPv6 addresses from the interface $if
677197139Shrsipv6_down()
678197139Shrs{
679197139Shrs	local _if _ifs _ret inetList oldifs _inet6
680197139Shrs	_if=$1
681197139Shrs	_ifs="^"
682197139Shrs	_ret=1
683197139Shrs
684197139Shrs	if ! ipv6if $_if; then
685197139Shrs		return 0
686197139Shrs	fi
687197139Shrs
688197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
689228245Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
690253227Shrs	ifalias ${_if} inet6 -alias && _ret=0
691197139Shrs
692253227Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
693197139Shrs
694197139Shrs	oldifs="$IFS"
695197139Shrs	IFS="$_ifs"
696197139Shrs	for _inet6 in $inetList ; do
697197139Shrs		# get rid of extraneous line
698197139Shrs		[ -z "$_inet6" ] && break
699197139Shrs
700197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
701197139Shrs
702197139Shrs		IFS="$oldifs"
703253227Shrs		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
704197139Shrs		IFS="$_ifs"
705197139Shrs		_ret=0
706197139Shrs	done
707197139Shrs	IFS="$oldifs"
708197139Shrs
709197139Shrs	return $_ret
710197139Shrs}
711197139Shrs
712253227Shrs# ifalias if af action
713253227Shrs#	Configure or remove aliases for network interface $if.
714113674Smtm#	It returns 0 if at least one alias was configured or
715253227Shrs#	removed, or 1 if there were none.
716113674Smtm#
717253227Shrsifalias()
718113674Smtm{
719197139Shrs	local _ret
720113674Smtm	_ret=1
721197139Shrs
722253227Shrs	afexists $2 || return $_ret
723253227Shrs
724197139Shrs	case "$2" in
725253227Shrs	inet|inet6|link|ether)
726253227Shrs		ifalias_af_common $1 $2 $3 && _ret=0
727197139Shrs		;;
728197139Shrs	esac
729197139Shrs
730197139Shrs	return $_ret
731197139Shrs}
732197139Shrs
733253227Shrs# ifalias_expand_addr af action addr
734253227Shrs#	Expand address range ("N-M") specification in addr.
735253227Shrs#	"addr" must not include an address-family keyword.
736253227Shrs#	The results will include an address-family keyword.
737197139Shrs#
738253227Shrsifalias_expand_addr()
739197139Shrs{
740253683Shrs	local _af _action
741197139Shrs
742253683Shrs	_af=$1
743253683Shrs	_action=$2
744253683Shrs	shift 2
745253683Shrs
746253683Shrs	afexists $_af || return
747253683Shrs	ifalias_expand_addr_$_af $_action $*
748197139Shrs}
749197139Shrs
750253227Shrs# ifalias_expand_addr_inet action addr
751253227Shrs#	Helper function for ifalias_expand_addr().  Handles IPv4.
752197139Shrs#
753253227Shrsifalias_expand_addr_inet()
754197139Shrs{
755253683Shrs	local _action _arg _cidr _cidr_addr _exargs
756253227Shrs	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
757253227Shrs	local _retstr _c
758253227Shrs	_action=$1
759253227Shrs	_arg=$2
760253683Shrs	shift 2
761253683Shrs	_exargs=$*
762253227Shrs	_retstr=
763197139Shrs
764253683Shrs	case $_action:$_arg:$_exargs in
765253683Shrs	*:*--*)		return ;;	# invalid
766253683Shrs	tmp:*[0-9]-[0-9]*:*)		# to be expanded
767253683Shrs		_action="alias"
768253683Shrs	;;
769253683Shrs	*:*[0-9]-[0-9]*:*)		# to be expanded
770253683Shrs	;;
771253683Shrs	tmp:*:*netmask*)		# already expanded w/ netmask option
772253683Shrs		echo ${_arg%/[0-9]*} $_exargs && return
773253683Shrs	;;
774253683Shrs	tmp:*:*)			# already expanded w/o netmask option
775253683Shrs		echo $_arg $_exargs && return
776253683Shrs	;;
777253683Shrs	*:*:*netmask*)			# already expanded w/ netmask option
778253683Shrs		echo inet ${_arg%/[0-9]*} $_exargs && return
779253683Shrs	;;
780253683Shrs	*:*:*)				# already expanded w/o netmask option
781253683Shrs		echo inet $_arg $_exargs && return
782253683Shrs	;;
783253227Shrs	esac
784253227Shrs
785253227Shrs	for _cidr in $_arg; do
786253227Shrs		_ipaddr=${_cidr%%/*}
787253227Shrs		_plen=${_cidr##*/}
788253227Shrs		# When subnet prefix length is not specified, use /32.
789253227Shrs		case $_plen in
790253227Shrs		$_ipaddr)	_plen=32 ;;	# "/" character not found
791197139Shrs		esac
792197139Shrs
793253227Shrs		OIFS=$IFS
794253227Shrs		IFS=. set -- $_ipaddr
795253227Shrs		_range=
796253227Shrs		_iphead=
797253227Shrs		_iptail=
798253227Shrs		for _c in $@; do
799253227Shrs			case $_range:$_c in
800253227Shrs			:[0-9]*-[0-9]*)
801253227Shrs				_range=$_c
802197139Shrs			;;
803253227Shrs			:*)
804253227Shrs				_iphead="${_iphead}${_iphead:+.}${_c}"
805197139Shrs			;;
806253227Shrs			*:*)
807253227Shrs				_iptail="${_iptail}${_iptail:+.}${_c}"
808253227Shrs			;;
809253227Shrs			esac
810253227Shrs		done
811253227Shrs		IFS=$OIFS
812253227Shrs		_iplow=${_range%-*}
813253227Shrs		_iphigh=${_range#*-}
814253227Shrs
815253227Shrs		# clear netmask when removing aliases
816253227Shrs		if [ "$_action" = "-alias" ]; then
817253227Shrs			_plen=""
818253227Shrs		fi
819253227Shrs
820253227Shrs		_ipcount=$_iplow
821253227Shrs		while [ "$_ipcount" -le "$_iphigh" ]; do
822253227Shrs			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
823253227Shrs			if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
824253227Shrs				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."
825253227Shrs				break
826253227Shrs			else
827253227Shrs				_ipcount=$(($_ipcount + 1))
828253227Shrs			fi
829253227Shrs			# Forcibly set /32 for remaining aliases.
830253227Shrs			_plen=32
831253227Shrs		done
832197139Shrs	done
833197139Shrs
834253227Shrs	for _c in $_retstr; do
835253683Shrs		ifalias_expand_addr_inet $_action $_c $_exargs
836253227Shrs	done
837113674Smtm}
838100280Sgordon
839253227Shrs# ifalias_expand_addr_inet6 action addr
840253227Shrs#	Helper function for ifalias_expand_addr().  Handles IPv6.
841116029Smtm#
842253227Shrsifalias_expand_addr_inet6()
843116029Smtm{
844253683Shrs	local _action _arg _cidr _cidr_addr _exargs
845253227Shrs	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
846253227Shrs	local _ipv4part
847253227Shrs	local _retstr _c
848253227Shrs	_action=$1
849253227Shrs	_arg=$2
850253683Shrs	shift 2
851253683Shrs	_exargs=$*
852253227Shrs	_retstr=
853197139Shrs
854253683Shrs	case $_action:$_arg:$_exargs in
855253683Shrs	*:*--*:*)	return ;;	# invalid
856253683Shrs	tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
857253683Shrs		_action="alias"
858253683Shrs	;;
859253683Shrs	*:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)	# to be expanded
860253683Shrs	;;
861253683Shrs	tmp:*:*prefixlen*)	# already expanded w/ prefixlen option
862253683Shrs		echo ${_arg%/[0-9]*} $_exargs && return
863253683Shrs	;;
864253683Shrs	tmp:*:*)		# already expanded w/o prefixlen option
865253683Shrs		echo $_arg $_exargs && return
866253683Shrs	;;
867253683Shrs	*:*:*prefixlen*)	# already expanded w/ prefixlen option
868253683Shrs		echo inet6 ${_arg%/[0-9]*} $_exargs && return
869253683Shrs	;;
870253683Shrs	*:*:*)			# already expanded w/o prefixlen option
871253683Shrs		echo inet6 $_arg $_exargs && return
872253683Shrs	;;
873197139Shrs	esac
874197139Shrs
875253227Shrs	for _cidr in $_arg; do
876253227Shrs		_ipaddr="${_cidr%%/*}"
877253227Shrs		_plen="${_cidr##*/}"
878253227Shrs
879253227Shrs		case $_action:$_ipaddr:$_cidr in
880253227Shrs		-alias:*:*)		unset _plen ;;
881253227Shrs		*:$_cidr:$_ipaddr)	unset _plen ;;
882253227Shrs		esac
883253227Shrs
884253227Shrs		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
885253227Shrs			# Handle !v4mapped && !v4compat addresses.
886253227Shrs
887253227Shrs			# The default prefix length is 64.
888253227Shrs			case $_ipaddr:$_cidr in
889253227Shrs			$_cidr:$_ipaddr)	_plen="64" ;;
890253227Shrs			esac
891253227Shrs			_ipleft=${_ipaddr%-*}
892253227Shrs			_ipright=${_ipaddr#*-}
893253227Shrs			_iplow=${_ipleft##*:}
894253227Shrs			_iphigh=${_ipright%%:*}
895253227Shrs			_ipleft=${_ipleft%:*}
896253227Shrs			_ipright=${_ipright#*:}
897253227Shrs
898253227Shrs			if [ "$_iphigh" = "$_ipright" ]; then
899253227Shrs				unset _ipright
900253227Shrs			else
901253227Shrs				_ipright=:$_ipright
902253227Shrs			fi
903253227Shrs
904253227Shrs			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
905253227Shrs				_iplow=$((0x$_iplow))
906253227Shrs				_iphigh=$((0x$_iphigh))
907253227Shrs				_ipcount=$_iplow
908253227Shrs				while [ $_ipcount -le $_iphigh ]; do
909253227Shrs					_r=`printf "%s:%04x%s%s" \
910253227Shrs					    $_ipleft $_ipcount $_ipright \
911253227Shrs					    ${_plen:+/}$_plen`
912253227Shrs					_retstr="$_retstr $_r"
913253227Shrs					if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
914253227Shrs					then
915253227Shrs						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."
916253227Shrs						break
917253227Shrs					else
918253227Shrs						_ipcount=$(($_ipcount + 1))
919253227Shrs					fi
920253227Shrs				done
921253227Shrs			else
922253227Shrs				_retstr="${_ipaddr}${_plen:+/}${_plen}"
923253227Shrs			fi
924253227Shrs
925253227Shrs			for _c in $_retstr; do
926253683Shrs				ifalias_expand_addr_inet6 $_action $_c $_exargs
927253227Shrs			done
928253227Shrs		else
929253227Shrs			# v4mapped/v4compat should handle as an IPv4 alias
930253227Shrs			_ipv4part=${_ipaddr##*:}
931253227Shrs
932253227Shrs			# Adjust prefix length if any.  If not, set the
933253227Shrs			# default prefix length as 32.
934253227Shrs			case $_ipaddr:$_cidr in
935253227Shrs			$_cidr:$_ipaddr)	_plen=32 ;;
936253227Shrs			*)			_plen=$(($_plen - 96)) ;;
937253227Shrs			esac
938253227Shrs
939253227Shrs			_retstr=`ifalias_expand_addr_inet \
940253227Shrs			    tmp ${_ipv4part}${_plen:+/}${_plen}`
941253227Shrs			for _c in $_retstr; do
942253683Shrs				ifalias_expand_addr_inet $_action $_c $_exargs
943253227Shrs			done
944253227Shrs		fi
945253227Shrs	done
946197139Shrs}
947197139Shrs
948253227Shrs# ifalias_af_common_handler if af action args
949253227Shrs#	Helper function for ifalias_af_common().
950197139Shrs#
951253227Shrsifalias_af_common_handler()
952197139Shrs{
953253227Shrs	local _ret _if _af _action _args _c _tmpargs
954253227Shrs
955197139Shrs	_ret=1
956253227Shrs	_if=$1
957253227Shrs	_af=$2
958253227Shrs	_action=$3
959253227Shrs	shift 3
960253227Shrs	_args=$*
961197139Shrs
962253227Shrs	case $_args in
963253227Shrs	${_af}\ *)	;;
964253227Shrs	*)	return	;;
965253227Shrs	esac
966253227Shrs
967253227Shrs	# link(ether) does not support address removal.
968253227Shrs	case $_af:$_action in
969253227Shrs	link:-alias|ether:-alias)	return ;;
970253227Shrs	esac
971253227Shrs
972253227Shrs	_tmpargs=
973253227Shrs	for _c in $_args; do
974253227Shrs		case $_c in
975253227Shrs		${_af})
976253227Shrs			case $_tmpargs in
977253227Shrs			${_af}\ *-*)
978253227Shrs				ifalias_af_common_handler $_if $_af $_action \
979253227Shrs				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
980197139Shrs			;;
981253227Shrs			${_af}\ *)
982253227Shrs				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
983197139Shrs			;;
984253227Shrs			esac
985253227Shrs			_tmpargs=$_af
986253227Shrs		;;
987253227Shrs		*)
988253227Shrs			_tmpargs="$_tmpargs $_c"
989253227Shrs		;;
990197139Shrs		esac
991197139Shrs	done
992253227Shrs	# Process the last component if any.
993253227Shrs	if [ -n "$_tmpargs}" ]; then
994253227Shrs		case $_tmpargs in
995253227Shrs		${_af}\ *-*)
996253227Shrs			ifalias_af_common_handler $_if $_af $_action \
997253227Shrs			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
998253227Shrs		;;
999253227Shrs		${_af}\ *)
1000253227Shrs			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1001253227Shrs		;;
1002253227Shrs		esac
1003253227Shrs	fi
1004197139Shrs
1005197139Shrs	return $_ret
1006197139Shrs}
1007197139Shrs
1008253227Shrs# ifalias_af_common if af action
1009253227Shrs#	Helper function for ifalias().
1010197139Shrs#
1011253227Shrsifalias_af_common()
1012197139Shrs{
1013253227Shrs	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1014264439Sdteske	local _punct=".-/+"
1015253227Shrs
1016197139Shrs	_ret=1
1017253227Shrs	_aliasn=
1018253227Shrs	_if=$1
1019253227Shrs	_af=$2
1020253227Shrs	_action=$3
1021197139Shrs
1022264439Sdteske	# Normalize $_if before using it in a pattern to list_vars()
1023264439Sdteske	ltr "$_if" "$_punct" "_" _if
1024264439Sdteske
1025253227Shrs	# ifconfig_IF_aliasN which starts with $_af
1026264439Sdteske	for alias in `list_vars ifconfig_${_if}_alias[0-9]\* |
1027264439Sdteske		sort_lite -nk1.$((9+${#_if}+7))`
1028264439Sdteske	do
1029264439Sdteske		eval ifconfig_args=\"\$$alias\"
1030253227Shrs		_iaf=
1031253227Shrs		case $ifconfig_args in
1032253227Shrs		inet\ *)	_iaf=inet ;;
1033253227Shrs		inet6\ *)	_iaf=inet6 ;;
1034253227Shrs		ipx\ *)		_iaf=ipx ;;
1035253227Shrs		link\ *)	_iaf=link ;;
1036253227Shrs		ether\ *)	_iaf=ether ;;
1037253227Shrs		esac
1038253227Shrs
1039253227Shrs		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1040253227Shrs		${_af}:*:${_af}:*)
1041253227Shrs			_aliasn="$_aliasn $ifconfig_args"
1042197139Shrs			;;
1043253227Shrs		${_af}:*:"":"")
1044116029Smtm			break
1045197139Shrs			;;
1046253227Shrs		inet:alias:"":*)
1047253227Shrs			_aliasn="$_aliasn inet $ifconfig_args"
1048253227Shrs			warn "\$ifconfig_${_if}_alias${alias} needs " \
1049253227Shrs			    "\"inet\" keyword for an IPv4 address."
1050197139Shrs		esac
1051116029Smtm	done
1052197139Shrs
1053197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
1054253227Shrs	case $_af in
1055253227Shrs	inet6)
1056264439Sdteske		for alias in `list_vars ipv6_ifconfig_${_if}_alias[0-9]\* |
1057264439Sdteske			sort_lite -nk1.$((14+${#_if}+7))`
1058264439Sdteske		do
1059264439Sdteske			eval ifconfig_args=\"\$$alias\"
1060253227Shrs			case ${_action}:"${ifconfig_args}" in
1061253227Shrs			*:"")
1062253227Shrs				break
1063197139Shrs			;;
1064253227Shrs			alias:*)
1065253227Shrs				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
1066253227Shrs				warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1067253227Shrs				    "is obsolete.  Use ifconfig_$1_aliasN " \
1068253227Shrs				    "instead."
1069253227Shrs			;;
1070253227Shrs			esac
1071253227Shrs		done
1072253227Shrs	esac
1073253227Shrs
1074253227Shrs	# backward compatibility: ipv4_addrs_IF.
1075253227Shrs	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1076253227Shrs		_aliasn="$_aliasn inet $_tmpargs"
1077253227Shrs	done
1078253227Shrs
1079253227Shrs	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1080253227Shrs	_tmpargs=
1081253227Shrs	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1082253227Shrs		case $_c in
1083253227Shrs		inet|inet6|ipx|link|ether)
1084253227Shrs			case $_tmpargs in
1085253227Shrs			${_af}\ *)
1086253227Shrs				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1087253227Shrs			;;
1088253227Shrs			esac
1089253227Shrs			_tmpargs=$_c
1090253227Shrs		;;
1091197139Shrs		*)
1092253227Shrs			_tmpargs="$_tmpargs $_c"
1093197139Shrs		esac
1094197139Shrs	done
1095253227Shrs	# Process the last component
1096253227Shrs	case $_tmpargs in
1097253227Shrs	${_af}\ *)
1098253227Shrs		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1099253227Shrs	;;
1100253227Shrs	esac
1101197139Shrs
1102116029Smtm	return $_ret
1103116029Smtm}
1104116029Smtm
1105228245Shrs# ipv6_prefix_hostid_addr_common if action
1106228245Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
1107228245Shrs#
1108228245Shrsipv6_prefix_hostid_addr_common()
1109197139Shrs{
1110253683Shrs	local _if _action prefix j
1111197139Shrs	_if=$1
1112228245Shrs	_action=$2
1113197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1114197139Shrs
1115197139Shrs	if [ -n "${prefix}" ]; then
1116197139Shrs		for j in ${prefix}; do
1117253227Shrs			# The default prefixlen is 64.
1118253227Shrs			plen=${j#*/}
1119253227Shrs			case $j:$plen in
1120253227Shrs			$plen:$j)	plen=64 ;;
1121253227Shrs			*)		j=${j%/*} ;;
1122253227Shrs			esac
1123197139Shrs
1124253227Shrs			# Normalize the last part by removing ":"
1125253683Shrs			j=${j%::*}
1126253227Shrs			j=${j%:}
1127253683Shrs			${IFCONFIG_CMD} ${_if} inet6 $j:: \
1128253683Shrs				prefixlen $plen eui64 ${_action}
1129253227Shrs
1130197139Shrs			# if I am a router, add subnet router
1131197139Shrs			# anycast address (RFC 2373).
1132197139Shrs			if checkyesno ipv6_gateway_enable; then
1133253227Shrs				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1134253227Shrs					prefixlen $plen ${_action} anycast
1135197139Shrs			fi
1136197139Shrs		done
1137197139Shrs	fi
1138197139Shrs}
1139197139Shrs
1140197139Shrs# ipv6_accept_rtadv_up if
1141197139Shrs#	Enable accepting Router Advertisement and send Router
1142197139Shrs#	Solicitation message
1143197139Shrsipv6_accept_rtadv_up()
1144197139Shrs{
1145197139Shrs	if ipv6_autoconfif $1; then
1146253227Shrs		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1147203433Sume		if ! checkyesno rtsold_enable; then
1148203433Sume			rtsol ${rtsol_flags} $1
1149203433Sume		fi
1150197139Shrs	fi
1151197139Shrs}
1152197139Shrs
1153197139Shrs# ipv6_accept_rtadv_down if
1154197139Shrs#	Disable accepting Router Advertisement
1155197139Shrsipv6_accept_rtadv_down()
1156197139Shrs{
1157197139Shrs	if ipv6_autoconfif $1; then
1158253227Shrs		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1159197139Shrs	fi
1160197139Shrs}
1161197139Shrs
1162113674Smtm# ifscript_up if
1163113674Smtm#	Evaluate a startup script for the $if interface.
1164113674Smtm#	It returns 0 if a script was found and processed or
1165113674Smtm#	1 if no script was found.
1166113674Smtm#
1167113674Smtmifscript_up()
1168100280Sgordon{
1169113674Smtm	if [ -r /etc/start_if.$1 ]; then
1170113674Smtm		. /etc/start_if.$1
1171113674Smtm		return 0
1172197139Shrs	else
1173197139Shrs		return 1
1174113674Smtm	fi
1175100280Sgordon}
1176100280Sgordon
1177116029Smtm# ifscript_down if
1178116029Smtm#	Evaluate a shutdown script for the $if interface.
1179116029Smtm#	It returns 0 if a script was found and processed or
1180116029Smtm#	1 if no script was found.
1181116029Smtm#
1182116029Smtmifscript_down()
1183116029Smtm{
1184116029Smtm	if [ -r /etc/stop_if.$1 ]; then
1185116029Smtm		. /etc/stop_if.$1
1186116029Smtm		return 0
1187197139Shrs	else
1188197139Shrs		return 1
1189116029Smtm	fi
1190116029Smtm}
1191116029Smtm
1192197147Shrs# clone_up
1193197147Shrs#	Create cloneable interfaces.
1194113674Smtm#
1195113674Smtmclone_up()
1196100280Sgordon{
1197197139Shrs	local _prefix _list ifn
1198113674Smtm	_prefix=
1199113674Smtm	_list=
1200197139Shrs
1201197139Shrs	# create_args_IF
1202113674Smtm	for ifn in ${cloned_interfaces}; do
1203253227Shrs		${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1204116774Skuriyama		if [ $? -eq 0 ]; then
1205113674Smtm			_list="${_list}${_prefix}${ifn}"
1206113674Smtm			[ -z "$_prefix" ] && _prefix=' '
1207113674Smtm		fi
1208113674Smtm	done
1209113674Smtm	debug "Cloned: ${_list}"
1210113674Smtm}
1211100280Sgordon
1212197147Shrs# clone_down
1213197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1214197147Shrs#	standard output.
1215113674Smtm#
1216113674Smtmclone_down()
1217113674Smtm{
1218197139Shrs	local _prefix _list ifn
1219113674Smtm	_prefix=
1220113674Smtm	_list=
1221197139Shrs
1222113674Smtm	for ifn in ${cloned_interfaces}; do
1223253227Shrs		${IFCONFIG_CMD} -n ${ifn} destroy
1224116774Skuriyama		if [ $? -eq 0 ]; then
1225113674Smtm			_list="${_list}${_prefix}${ifn}"
1226113674Smtm			[ -z "$_prefix" ] && _prefix=' '
1227113674Smtm		fi
1228113674Smtm	done
1229113674Smtm	debug "Destroyed clones: ${_list}"
1230100280Sgordon}
1231100280Sgordon
1232197147Shrs# childif_create
1233197147Shrs#	Create and configure child interfaces.  Return 0 if child
1234197147Shrs#	interfaces are created.
1235178356Ssam#
1236178356Ssamchildif_create()
1237178356Ssam{
1238201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1239178356Ssam	cfg=1
1240178356Ssam	ifn=$1
1241178356Ssam
1242178527Sbrooks	# Create wireless interfaces
1243178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1244178527Sbrooks
1245178527Sbrooks	for child in ${child_wlans}; do
1246183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1247189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1248189759Sbrooks
1249178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1250253227Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1251189759Sbrooks			if [ -n "${debug_flags}" ]; then
1252189759Sbrooks				wlandebug -i $child ${debug_flags}
1253189759Sbrooks			fi
1254178356Ssam		else
1255253227Shrs			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1256189759Sbrooks			if [ -n "${debug_flags}" ]; then
1257189759Sbrooks				wlandebug -i $i ${debug_flags}
1258189759Sbrooks			fi
1259253227Shrs			${IFCONFIG_CMD} $i name $child && cfg=0
1260178356Ssam		fi
1261188118Sthompsa		if autoif $child; then
1262188118Sthompsa			ifn_start $child
1263188118Sthompsa		fi
1264178356Ssam	done
1265178356Ssam
1266201215Sjhb	# Create vlan interfaces
1267201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1268201215Sjhb
1269201215Sjhb	if [ -n "${child_vlans}" ]; then
1270201215Sjhb		load_kld if_vlan
1271201215Sjhb	fi
1272201215Sjhb
1273201215Sjhb	for child in ${child_vlans}; do
1274201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1275201215Sjhb			child="${ifn}.${child}"
1276201215Sjhb			create_args=`get_if_var $child create_args_IF`
1277253227Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1278201215Sjhb		else
1279201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1280201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1281253227Shrs				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1282201215Sjhb			else
1283253227Shrs				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1284253227Shrs				${IFCONFIG_CMD} $i name $child && cfg=0
1285201215Sjhb			fi
1286201215Sjhb		fi
1287201215Sjhb		if autoif $child; then
1288201215Sjhb			ifn_start $child
1289201215Sjhb		fi
1290201215Sjhb	done
1291201215Sjhb
1292179001Sbrooks	return ${cfg}
1293178356Ssam}
1294178356Ssam
1295197147Shrs# childif_destroy
1296197147Shrs#	Destroy child interfaces.
1297178356Ssam#
1298178356Ssamchildif_destroy()
1299178356Ssam{
1300201215Sjhb	local cfg child child_vlans child_wlans ifn
1301197139Shrs	cfg=1
1302178356Ssam
1303201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1304178527Sbrooks	for child in ${child_wlans}; do
1305201215Sjhb		if ! ifexists $child; then
1306201215Sjhb			continue
1307201215Sjhb		fi
1308253227Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1309178356Ssam	done
1310197139Shrs
1311201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1312201215Sjhb	for child in ${child_vlans}; do
1313201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1314201215Sjhb			child="${ifn}.${child}"
1315201215Sjhb		fi
1316201215Sjhb		if ! ifexists $child; then
1317201215Sjhb			continue
1318201215Sjhb		fi
1319253227Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1320201215Sjhb	done
1321201215Sjhb
1322197139Shrs	return ${cfg}
1323178356Ssam}
1324178356Ssam
1325197147Shrs# ng_mkpeer
1326197147Shrs#	Create netgraph nodes.
1327166583Sflz#
1328197147Shrsng_mkpeer()
1329197147Shrs{
1330166583Sflz	ngctl -f - 2> /dev/null <<EOF
1331166583Sflzmkpeer $*
1332166583Sflzmsg dummy nodeinfo
1333166583SflzEOF
1334166583Sflz}
1335166583Sflz
1336197147Shrs# ng_create_one
1337197147Shrs#	Create netgraph nodes.
1338197147Shrs#
1339197147Shrsng_create_one()
1340197147Shrs{
1341197139Shrs	local t
1342197139Shrs
1343166583Sflz	ng_mkpeer $* | while read line; do
1344166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1345166583Sflz		if [ -n "${t}" ]; then
1346166583Sflz			echo ${t}
1347166583Sflz			return
1348166583Sflz		fi
1349166583Sflz	done
1350166583Sflz}
1351166583Sflz
1352197147Shrs# gif_up
1353197147Shrs#	Create gif(4) tunnel interfaces.
1354197147Shrsgif_up()
1355197147Shrs{
1356197139Shrs	local i peers
1357197139Shrs
1358166583Sflz	for i in ${gif_interfaces}; do
1359166583Sflz		peers=`get_if_var $i gifconfig_IF`
1360166583Sflz		case ${peers} in
1361166583Sflz		'')
1362166583Sflz			continue
1363166583Sflz			;;
1364166583Sflz		*)
1365177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1366253227Shrs				${IFCONFIG_CMD} $i create >/dev/null 2>&1
1367177682Sbrooks			else
1368253227Shrs				gif=`${IFCONFIG_CMD} gif create`
1369253227Shrs				${IFCONFIG_CMD} $gif name $i
1370177682Sbrooks			fi
1371253227Shrs			${IFCONFIG_CMD} $i tunnel ${peers}
1372253227Shrs			${IFCONFIG_CMD} $i up
1373166583Sflz			;;
1374166583Sflz		esac
1375166583Sflz	done
1376166583Sflz}
1377166583Sflz
1378166583Sflz# ng_fec_create ifn
1379197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1380197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1381197139Shrsng_fec_create()
1382197139Shrs{
1383166583Sflz	 local req_iface iface bogus
1384166583Sflz	 req_iface="$1"
1385166583Sflz
1386166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1387166583Sflz
1388166583Sflz	 bogus=""
1389166583Sflz	 while true; do
1390166583Sflz		 iface=`ng_create_one fec dummy fec`
1391166583Sflz		 if [ -z "${iface}" ]; then
1392166583Sflz			 exit 2
1393166583Sflz		 fi
1394166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1395166583Sflz			 break
1396166583Sflz		 fi
1397166583Sflz		 bogus="${bogus} ${iface}"
1398166583Sflz	 done
1399166583Sflz
1400166583Sflz	 for iface in ${bogus}; do
1401166583Sflz		 ngctl shutdown ${iface}:
1402166583Sflz	 done
1403166583Sflz}
1404166583Sflz
1405197147Shrs# fec_up
1406197147Shrs#	Create Fast EtherChannel interfaces.
1407197147Shrsfec_up()
1408197147Shrs{
1409197139Shrs	local i j
1410197139Shrs
1411166583Sflz	for i in ${fec_interfaces}; do
1412166583Sflz		ng_fec_create $i
1413166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1414166583Sflz			case ${j} in
1415100282Sdougb			'')
1416100282Sdougb				continue
1417100282Sdougb				;;
1418100282Sdougb			*)
1419166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1420100282Sdougb				;;
1421100282Sdougb			esac
1422100282Sdougb		done
1423166583Sflz	done
1424100282Sdougb}
1425100282Sdougb
1426113674Smtm# ipx_up ifn
1427197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1428197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1429113674Smtm#
1430113674Smtmipx_up()
1431100280Sgordon{
1432197139Shrs	local ifn
1433113674Smtm	ifn="$1"
1434197139Shrs
1435197139Shrs	# ifconfig_IF_ipx
1436197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1437113674Smtm	if [ -n "${ifconfig_args}" ]; then
1438253227Shrs		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1439113674Smtm		return 0
144085831Sdes	fi
1441197139Shrs
1442113674Smtm	return 1
1443113674Smtm}
144485831Sdes
1445116029Smtm# ipx_down ifn
1446116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1447116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1448113674Smtm#
1449116029Smtmipx_down()
1450116029Smtm{
1451197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1452197139Shrs	_if=$1
1453116100Smtm	_ifs="^"
1454116100Smtm	_ret=1
1455253227Shrs	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1456197139Shrs	oldifs="$IFS"
1457116100Smtm
1458116100Smtm	IFS="$_ifs"
1459116100Smtm	for _ipx in $ipxList ; do
1460116100Smtm		# get rid of extraneous line
1461116100Smtm		[ -z "$_ipx" ] && break
1462116100Smtm
1463116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1464116100Smtm
1465116100Smtm		IFS="$oldifs"
1466253227Shrs		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1467116100Smtm		IFS="$_ifs"
1468116100Smtm		_ret=0
1469116100Smtm	done
1470116100Smtm	IFS="$oldifs"
1471116100Smtm
1472116100Smtm	return $_ret
1473116029Smtm}
1474116029Smtm
1475137070Spjd# ifnet_rename
1476137070Spjd#	Rename all requested interfaces.
1477116029Smtm#
1478137070Spjdifnet_rename()
1479137070Spjd{
1480197139Shrs	local _if _ifname
1481137070Spjd
1482197139Shrs	# ifconfig_IF_name
1483253227Shrs	for _if in `${IFCONFIG_CMD} -l`; do
1484157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1485137070Spjd		if [ ! -z "$_ifname" ]; then
1486253227Shrs			${IFCONFIG_CMD} $_if name $_ifname
1487137070Spjd		fi
1488137070Spjd	done
1489197139Shrs
1490137070Spjd	return 0
1491137070Spjd}
1492137070Spjd
1493113674Smtm# list_net_interfaces type
1494113674Smtm#	List all network interfaces. The type of interface returned
1495113674Smtm#	can be controlled by the type argument. The type
1496113674Smtm#	argument can be any of the following:
1497197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1498197147Shrs#		dhcp	- list only DHCP configured interfaces
1499197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1500197139Shrs#				  Address Autoconf configured interfaces
1501197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1502197139Shrs#				  configured interfaces
1503113674Smtm#	If no argument is specified all network interfaces are output.
1504134429Syar#	Note that the list will include cloned interfaces if applicable.
1505134429Syar#	Cloned interfaces must already exist to have a chance to appear
1506134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1507113674Smtm#
1508113674Smtmlist_net_interfaces()
1509113674Smtm{
1510197139Shrs	local type _tmplist _list _autolist _lo _if
1511113674Smtm	type=$1
151265532Snectar
1513149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
151451231Ssheldonh	#
1515197139Shrs	_tmplist=
151651231Ssheldonh	case ${network_interfaces} in
151751231Ssheldonh	[Aa][Uu][Tt][Oo])
1518253227Shrs		_autolist="`${IFCONFIG_CMD} -l`"
1519149726Sbrooks		_lo=
1520149401Sbrooks		for _if in ${_autolist} ; do
1521149401Sbrooks			if autoif $_if; then
1522149726Sbrooks				if [ "$_if" = "lo0" ]; then
1523149726Sbrooks					_lo="lo0 "
1524149726Sbrooks				else
1525197139Shrs					_tmplist="${_tmplist} ${_if}"
1526149726Sbrooks				fi
1527149401Sbrooks			fi
1528149401Sbrooks		done
1529197139Shrs		_tmplist="${_lo}${_tmplist# }"
153051231Ssheldonh		;;
153183677Sbrooks	*)
1532149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1533196478Sdougb
1534196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1535196478Sdougb		#
1536196478Sdougb		case "$_tmplist" in
1537196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1538196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1539196478Sdougb		esac
154083677Sbrooks		;;
154151231Ssheldonh	esac
154249122Sbrian
1543197139Shrs	_list=
1544197139Shrs	case "$type" in
1545197139Shrs	nodhcp)
1546197139Shrs		for _if in ${_tmplist} ; do
1547197139Shrs			if ! dhcpif $_if && \
1548197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1549197139Shrs				_list="${_list# } ${_if}"
1550197147Shrs			fi
1551197139Shrs		done
1552197139Shrs		;;
1553197139Shrs	dhcp)
1554197147Shrs		for _if in ${_tmplist} ; do
1555197147Shrs			if dhcpif $_if; then
1556197139Shrs				_list="${_list# } ${_if}"
1557197147Shrs			fi
1558197147Shrs		done
1559113674Smtm		;;
1560197139Shrs	noautoconf)
1561197139Shrs		for _if in ${_tmplist} ; do
1562197139Shrs			if ! ipv6_autoconfif $_if && \
1563197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1564197139Shrs				_list="${_list# } ${_if}"
1565197139Shrs			fi
1566197139Shrs		done
1567113674Smtm		;;
1568197139Shrs	autoconf)
1569197139Shrs		for _if in ${_tmplist} ; do
1570197139Shrs			if ipv6_autoconfif $_if; then
1571197139Shrs				_list="${_list# } ${_if}"
1572197139Shrs			fi
1573197139Shrs		done
1574197139Shrs		;;
1575197139Shrs	*)
1576197139Shrs		_list=${_tmplist}
1577197139Shrs		;;
1578113674Smtm	esac
1579197139Shrs
1580197139Shrs	echo $_list
1581197139Shrs
1582130151Sschweikh	return 0
158325184Sjkh}
1584114942Sume
1585179003Sbrooks# get_default_if -address_family
1586179003Sbrooks#	Get the interface of the default route for the given address family.
1587179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1588179003Sbrooks#
1589179003Sbrooksget_default_if()
1590179003Sbrooks{
1591197139Shrs	local routeget oldifs defif line
1592197139Shrs	defif=
1593179003Sbrooks	oldifs="$IFS"
1594179003Sbrooks	IFS="
1595179003Sbrooks"
1596197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1597179003Sbrooks		case $line in
1598179003Sbrooks		*interface:*)
1599179003Sbrooks			defif=${line##*: }
1600179003Sbrooks			;;
1601179003Sbrooks		esac
1602179003Sbrooks	done
1603179003Sbrooks	IFS=${oldifs}
1604179003Sbrooks
1605179003Sbrooks	echo $defif
1606179003Sbrooks}
1607179003Sbrooks
1608197147Shrs# hexdigit arg
1609197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1610114942Sumehexdigit()
1611114942Sume{
1612221884Sjilles	printf '%x\n' "$1"
1613114942Sume}
1614114942Sume
1615197147Shrs# hexprint arg
1616197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1617114942Sumehexprint()
1618114942Sume{
1619221884Sjilles	printf '%x\n' "$1"
1620114942Sume}
1621114942Sume
1622196436Sdougbis_wired_interface()
1623196436Sdougb{
1624196436Sdougb	local media
1625196436Sdougb
1626253227Shrs	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1627196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1628196436Sdougb	esac
1629196436Sdougb
1630196436Sdougb	test "$media" = "Ethernet"
1631196436Sdougb}
1632196436Sdougb
1633197147Shrs# network6_getladdr if [flag]
1634197147Shrs#	Echo link-local address from $if if any.
1635197147Shrs#	If flag is defined, tentative ones will be excluded.
1636114942Sumenetwork6_getladdr()
1637114942Sume{
1638253227Shrs	local _if _flag proto addr rest
1639253227Shrs	_if=$1
1640253227Shrs	_flag=$2
1641253227Shrs
1642253227Shrs	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1643253227Shrs		case "${proto}/${addr}/${_flag}/${rest}" in
1644253227Shrs		inet6/fe80::*//*)
1645253227Shrs			echo ${addr}
1646253227Shrs		;;
1647253227Shrs		inet6/fe80:://*tentative*)	# w/o flag
1648253227Shrs			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1649253227Shrs			network6_getladdr $_if $_flags
1650253227Shrs		;;
1651253227Shrs		inet6/fe80::/*/*tentative*)	# w/ flag
1652253227Shrs			echo ${addr}
1653253227Shrs		;;
1654253227Shrs		*)
1655253227Shrs			continue
1656253227Shrs		;;
1657114942Sume		esac
1658253227Shrs
1659253227Shrs		return
1660114942Sume	done
1661114942Sume}
1662