network.subr revision 252360
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 252360 2013-06-28 22:25:37Z delphij $
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)
116230453Shrs			if ! checkyesno ipv6_gateway_enable; then
117230453Shrs				_ipv6_opts="${_ipv6_opts} accept_rtadv"
118230453Shrs			fi
119230453Shrs		;;
120222733Shrs		esac
121222733Shrs
122225521Shrs		case $ipv6_cpe_wanif in
123225521Shrs		$1)
124225521Shrs			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
125225521Shrs		;;
126225521Shrs		esac
127225521Shrs
128212574Shrs		if [ -n "${_ipv6_opts}" ]; then
129252015Shrs			${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
130197526Shrs		fi
131212574Shrs
132212574Shrs		# ifconfig_IF_ipv6
133212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
134212574Shrs		if [ -n "${ifconfig_args}" ]; then
135225522Shrs			# backward compatibility: inet6 keyword
136225522Shrs			case "${ifconfig_args}" in
137225522Shrs			:*|[0-9a-fA-F]*:*)
138225522Shrs				warn "\$ifconfig_$1_ipv6 needs " \
139225522Shrs				    "\"inet6\" keyword for an IPv6 address."
140225522Shrs				ifconfig_args="inet6 ${ifconfig_args}"
141225522Shrs			;;
142225522Shrs			esac
143252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
144252015Shrs			eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
145212574Shrs			_cfg=0
146212574Shrs		fi
147212574Shrs
148230453Shrs		# $ipv6_prefix_IF will be handled in
149230453Shrs		# ipv6_prefix_hostid_addr_common().
150230453Shrs		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
151230453Shrs		if [ -n "${ifconfig_args}" ]; then
152252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
153230453Shrs			_cfg=0
154230453Shrs		fi
155230453Shrs
156229783Suqs		# backward compatibility: $ipv6_ifconfig_IF
157212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
158212574Shrs		if [ -n "${ifconfig_args}" ]; then
159212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
160212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
161252015Shrs			${IFCONFIG_CMD} $1 inet6 -ifdisabled
162252015Shrs			eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
163212574Shrs			_cfg=0
164212574Shrs		fi
165197139Shrs	fi
166197139Shrs
167197139Shrs	if [ ${_cfg} -eq 0 ]; then
168252015Shrs		${IFCONFIG_CMD} $1 up
169197139Shrs	fi
170197139Shrs
171147088Sbrooks	if wpaif $1; then
172147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
173147088Sbrooks		_cfg=0		# XXX: not sure this should count
174252230Srpaulo	elif hostapif $1; then
175252230Srpaulo		/etc/rc.d/hostapd start $1
176252230Srpaulo		_cfg=0
177147088Sbrooks	fi
178147088Sbrooks
179147088Sbrooks	if dhcpif $1; then
180149726Sbrooks		if [ $_cfg -ne 0 ] ; then
181252015Shrs			${IFCONFIG_CMD} $1 up
182149726Sbrooks		fi
183157706Sbrooks		if syncdhcpif $1; then
184157706Sbrooks			/etc/rc.d/dhclient start $1
185157706Sbrooks		fi
186147088Sbrooks		_cfg=0
187147088Sbrooks	fi
188147088Sbrooks
189147121Sbrooks	return $_cfg
190113674Smtm}
19125184Sjkh
192116029Smtm# ifconfig_down if
193161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
194161386Sbrooks#	the interface exists.
195116029Smtm#
196116029Smtmifconfig_down()
197116029Smtm{
198197139Shrs	local _cfg
199147121Sbrooks	_cfg=1
200116029Smtm
201147088Sbrooks	if wpaif $1; then
202147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
203147121Sbrooks		_cfg=0
204252230Srpaulo	elif hostapif $1; then
205252230Srpaulo		/etc/rc.d/hostapd stop $1
206252230Srpaulo		_cfg=0
207147088Sbrooks	fi
208147088Sbrooks
209147088Sbrooks	if dhcpif $1; then
210147088Sbrooks		/etc/rc.d/dhclient stop $1
211147088Sbrooks		_cfg=0
212147088Sbrooks	fi
213147088Sbrooks
214161386Sbrooks	if ifexists $1; then
215252015Shrs		${IFCONFIG_CMD} $1 down
216161386Sbrooks		_cfg=0
217161386Sbrooks	fi
218157706Sbrooks
219147121Sbrooks	return $_cfg
220116029Smtm}
221116029Smtm
222157706Sbrooks# get_if_var if var [default]
223197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
224197147Shrs#	$var is a string containg the sub-string "IF" which will be
225197147Shrs#	replaced with $if after the characters defined in _punct are
226197147Shrs#	replaced with '_'. If the variable is unset, replace it with
227197147Shrs#	$default if given.
228157706Sbrooksget_if_var()
229157706Sbrooks{
230212578Shrs	local _if _punct _punct_c _var _default prefix suffix
231197139Shrs
232157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
233157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
234157706Sbrooks	fi
235157706Sbrooks
236157706Sbrooks	_if=$1
237157706Sbrooks	_punct=". - / +"
238157736Sbrooks	for _punct_c in $_punct; do
239157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
240157706Sbrooks	done
241157706Sbrooks	_var=$2
242157706Sbrooks	_default=$3
243157706Sbrooks
244157706Sbrooks	prefix=${_var%%IF*}
245157706Sbrooks	suffix=${_var##*IF}
246168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
247157706Sbrooks}
248157706Sbrooks
249197139Shrs# _ifconfig_getargs if [af]
250225560Sbrueffer#	Prints the arguments for the supplied interface to stdout.
251225560Sbrueffer#	Returns 1 if empty.  In general, ifconfig_getargs should be used
252147088Sbrooks#	outside this file.
253147088Sbrooks_ifconfig_getargs()
254147088Sbrooks{
255212574Shrs	local _ifn _af
256147088Sbrooks	_ifn=$1
257197139Shrs	_af=${2+_$2}
258197139Shrs
259147088Sbrooks	if [ -z "$_ifn" ]; then
260147088Sbrooks		return 1
261147088Sbrooks	fi
262147088Sbrooks
263212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
264147088Sbrooks}
265147088Sbrooks
266197139Shrs# ifconfig_getargs if [af]
267147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
268147088Sbrooks#	args such as DHCP and WPA.
269147088Sbrooksifconfig_getargs()
270147088Sbrooks{
271197139Shrs	local _tmpargs _arg _args
272197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
273147088Sbrooks	if [ $? -eq 1 ]; then
274147088Sbrooks		return 1
275147088Sbrooks	fi
276147088Sbrooks	_args=
277147088Sbrooks
278147088Sbrooks	for _arg in $_tmpargs; do
279147088Sbrooks		case $_arg in
280157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
281157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
282157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
283157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
284157706Sbrooks		[Ww][Pp][Aa]) ;;
285252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp]) ;;
286147088Sbrooks		*)
287147088Sbrooks			_args="$_args $_arg"
288147088Sbrooks			;;
289147088Sbrooks		esac
290147088Sbrooks	done
291147088Sbrooks
292147088Sbrooks	echo $_args
293147088Sbrooks}
294147088Sbrooks
295149401Sbrooks# autoif
296225560Sbrueffer#	Returns 0 if the interface should be automatically configured at
297149401Sbrooks#	boot time and 1 otherwise.
298149401Sbrooksautoif()
299149401Sbrooks{
300197139Shrs	local _tmpargs _arg
301149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
302197139Shrs
303149401Sbrooks	for _arg in $_tmpargs; do
304149401Sbrooks		case $_arg in
305149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
306149401Sbrooks			return 1
307149401Sbrooks			;;
308149401Sbrooks		esac
309149401Sbrooks	done
310197139Shrs
311149401Sbrooks	return 0
312149401Sbrooks}
313149401Sbrooks
314147088Sbrooks# dhcpif if
315147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
316147088Sbrooksdhcpif()
317147088Sbrooks{
318197139Shrs	local _tmpargs _arg
319147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
320197139Shrs
321252360Sdelphij	case $1 in
322252360Sdelphij	lo[0-9]*|\
323252360Sdelphij	stf[0-9]*|\
324252360Sdelphij	faith[0-9]*|\
325252360Sdelphij	lp[0-9]*|\
326252360Sdelphij	sl[0-9]*)
327252360Sdelphij		return 1
328252360Sdelphij		;;
329252360Sdelphij	esac
330225849Sdelphij	if noafif $1; then
331225849Sdelphij		return 1
332225849Sdelphij	fi
333225849Sdelphij
334147088Sbrooks	for _arg in $_tmpargs; do
335147088Sbrooks		case $_arg in
336147088Sbrooks		[Dd][Hh][Cc][Pp])
337147088Sbrooks			return 0
338147088Sbrooks			;;
339157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
340157706Sbrooks			return 0
341157706Sbrooks			;;
342157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
343157706Sbrooks			return 0
344157706Sbrooks			;;
345147088Sbrooks		esac
346147088Sbrooks	done
347197139Shrs
348147088Sbrooks	return 1
349147088Sbrooks}
350147088Sbrooks
351157706Sbrooks# syncdhcpif
352157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
353157706Sbrooks#	1 otherwise.
354157706Sbrookssyncdhcpif()
355157706Sbrooks{
356197139Shrs	local _tmpargs _arg
357157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
358197139Shrs
359225849Sdelphij	if noafif $1; then
360225849Sdelphij		return 1
361225849Sdelphij	fi
362225849Sdelphij
363157706Sbrooks	for _arg in $_tmpargs; do
364157706Sbrooks		case $_arg in
365157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
366157706Sbrooks			return 1
367157706Sbrooks			;;
368157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
369157706Sbrooks			return 0
370157706Sbrooks			;;
371157706Sbrooks		esac
372157706Sbrooks	done
373197139Shrs
374197139Shrs	checkyesno synchronous_dhclient
375157706Sbrooks}
376157706Sbrooks
377147088Sbrooks# wpaif if
378147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
379147088Sbrookswpaif()
380147088Sbrooks{
381197139Shrs	local _tmpargs _arg
382147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
383197139Shrs
384147088Sbrooks	for _arg in $_tmpargs; do
385147088Sbrooks		case $_arg in
386147088Sbrooks		[Ww][Pp][Aa])
387147088Sbrooks			return 0
388147088Sbrooks			;;
389147088Sbrooks		esac
390147088Sbrooks	done
391197139Shrs
392147088Sbrooks	return 1
393147088Sbrooks}
394147088Sbrooks
395252230Srpaulo# hostapif if
396252230Srpaulo#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
397252230Srpaulohostapif()
398252230Srpaulo{
399252230Srpaulo	local _tmpargs _arg
400252230Srpaulo	_tmpargs=`_ifconfig_getargs $1`
401252230Srpaulo
402252230Srpaulo	for _arg in $_tmpargs; do
403252230Srpaulo		case $_arg in
404252230Srpaulo		[Hh][Oo][Ss][Tt][Aa][Pp])
405252230Srpaulo			return 0
406252230Srpaulo			;;
407252230Srpaulo		esac
408252230Srpaulo	done
409252230Srpaulo
410252230Srpaulo	return 1
411252230Srpaulo}
412252230Srpaulo
413197139Shrs# afexists af
414197139Shrs#	Returns 0 if the address family is enabled in the kernel
415197139Shrs#	1 otherwise.
416197139Shrsafexists()
417197139Shrs{
418197139Shrs	local _af
419197139Shrs	_af=$1
420197139Shrs
421197139Shrs	case ${_af} in
422222996Shrs	inet|inet6)
423222996Shrs		check_kern_features ${_af}
424197139Shrs		;;
425197697Shrs	ipx)
426197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
427197697Shrs		;;
428197697Shrs	atm)
429197697Shrs		if [ -x /sbin/atmconfig ]; then
430197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
431197697Shrs		else
432197697Shrs			return 1
433197697Shrs		fi
434197697Shrs		;;
435197139Shrs	*)
436197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
437197139Shrs		;;
438197139Shrs	esac
439197139Shrs}
440197139Shrs
441212574Shrs# noafif if
442212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
443212574Shrsnoafif()
444212574Shrs{
445212574Shrs	local _if
446212574Shrs	_if=$1
447212574Shrs
448212574Shrs	case $_if in
449212574Shrs	pflog[0-9]*|\
450212574Shrs	pfsync[0-9]*|\
451212574Shrs	an[0-9]*|\
452212574Shrs	ath[0-9]*|\
453212574Shrs	ipw[0-9]*|\
454212577Shrs	ipfw[0-9]*|\
455212574Shrs	iwi[0-9]*|\
456212574Shrs	iwn[0-9]*|\
457212574Shrs	ral[0-9]*|\
458212574Shrs	wi[0-9]*|\
459212574Shrs	wl[0-9]*|\
460212574Shrs	wpi[0-9]*)
461212574Shrs		return 0
462212574Shrs		;;
463212574Shrs	esac
464212574Shrs
465212574Shrs	return 1
466212574Shrs}
467212574Shrs
468162490Sbrooks# ipv6if if
469162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
470162490Sbrooks#	1 otherwise.
471162490Sbrooksipv6if()
472162490Sbrooks{
473212574Shrs	local _if _tmpargs i
474212574Shrs	_if=$1
475212574Shrs
476197139Shrs	if ! afexists inet6; then
477162490Sbrooks		return 1
478162490Sbrooks	fi
479197139Shrs
480197139Shrs	# lo0 is always IPv6-enabled
481212574Shrs	case $_if in
482197139Shrs	lo0)
483197139Shrs		return 0
484197139Shrs		;;
485197139Shrs	esac
486197139Shrs
487212574Shrs	case "${ipv6_network_interfaces}" in
488212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
489212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
490212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
491212575Shrs		if [ -n "${_tmpargs}" ]; then
492212575Shrs			return 0
493212575Shrs		fi
494197139Shrs
495230453Shrs		# True if $ipv6_prefix_IF is defined.
496230453Shrs		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
497230453Shrs		if [ -n "${_tmpargs}" ]; then
498230453Shrs			return 0
499230453Shrs		fi
500230453Shrs
501212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
502212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
503212575Shrs		if [ -n "${_tmpargs}" ]; then
504212574Shrs			return 0
505212574Shrs		fi
506212575Shrs		;;
507212575Shrs	esac
508197139Shrs
509162490Sbrooks	return 1
510162490Sbrooks}
511162490Sbrooks
512197139Shrs# ipv6_autoconfif if
513197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
514225560Sbrueffer#	Stateless Address Configuration; 1 otherwise.
515197139Shrsipv6_autoconfif()
516197139Shrs{
517197139Shrs	local _if _tmpargs _arg
518197139Shrs	_if=$1
519197139Shrs
520212577Shrs	case $_if in
521252360Sdelphij	lo[0-9]*|\
522212577Shrs	stf[0-9]*|\
523212577Shrs	faith[0-9]*|\
524212577Shrs	lp[0-9]*|\
525212577Shrs	sl[0-9]*)
526197139Shrs		return 1
527212577Shrs		;;
528212577Shrs	esac
529212574Shrs	if noafif $_if; then
530212574Shrs		return 1
531212574Shrs	fi
532212577Shrs	if ! ipv6if $_if; then
533212577Shrs		return 1
534212577Shrs	fi
535197139Shrs	if checkyesno ipv6_gateway_enable; then
536197139Shrs		return 1
537197139Shrs	fi
538197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
539197526Shrs	if [ -n "${_tmpargs}" ]; then
540197526Shrs		return 1
541197526Shrs	fi
542212574Shrs	# backward compatibility: $ipv6_enable
543212574Shrs	case $ipv6_enable in
544212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
545242181Shrs		if checkyesno ipv6_gateway_enable; then
546242181Shrs			return 1
547242181Shrs		else
548242181Shrs			return 0
549242181Shrs		fi
550212577Shrs	;;
551197526Shrs	esac
552197526Shrs
553212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
554212574Shrs	for _arg in $_tmpargs; do
555212574Shrs		case $_arg in
556212574Shrs		accept_rtadv)
557212574Shrs			return 0
558212574Shrs			;;
559212574Shrs		esac
560212574Shrs	done
561212574Shrs
562212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
563212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
564212574Shrs	for _arg in $_tmpargs; do
565212574Shrs		case $_arg in
566212574Shrs		accept_rtadv)
567212574Shrs			return 0
568212574Shrs			;;
569212574Shrs		esac
570212574Shrs	done
571212574Shrs
572197139Shrs	return 1
573197139Shrs}
574197139Shrs
575161386Sbrooks# ifexists if
576161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
577161386Sbrooksifexists()
578161386Sbrooks{
579197139Shrs	[ -z "$1" ] && return 1
580252015Shrs	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
581161386Sbrooks}
582161386Sbrooks
583152441Sbrooks# ipv4_up if
584212578Shrs#	add IPv4 addresses to the interface $if
585152441Sbrooksipv4_up()
586152441Sbrooks{
587197139Shrs	local _if _ret
588152441Sbrooks	_if=$1
589197139Shrs	_ret=1
590197139Shrs
591222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
592222515Sbz	if [ "${_if}" = "lo0" ]; then
593226649Shrs		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
594222515Sbz		if [ -z "${ifconfig_args}" ]; then
595252015Shrs			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
596222515Sbz		fi
597222515Sbz	fi
598252015Shrs	ifalias ${_if} inet alias && _ret=0
599197139Shrs
600197139Shrs	return $_ret
601152441Sbrooks}
602152441Sbrooks
603197139Shrs# ipv6_up if
604197139Shrs#	add IPv6 addresses to the interface $if
605197139Shrsipv6_up()
606197139Shrs{
607197139Shrs	local _if _ret
608197139Shrs	_if=$1
609197139Shrs	_ret=1
610197139Shrs
611197139Shrs	if ! ipv6if $_if; then
612197139Shrs		return 0
613197139Shrs	fi
614197139Shrs
615252015Shrs	ifalias ${_if} inet6 alias && _ret=0
616226652Shrs	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
617197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
618197139Shrs
619197139Shrs	return $_ret
620197139Shrs}
621197139Shrs
622152441Sbrooks# ipv4_down if
623197147Shrs#	remove IPv4 addresses from the interface $if
624152441Sbrooksipv4_down()
625152441Sbrooks{
626197139Shrs	local _if _ifs _ret inetList oldifs _inet
627152441Sbrooks	_if=$1
628161386Sbrooks	_ifs="^"
629161386Sbrooks	_ret=1
630161386Sbrooks
631252015Shrs	ifalias ${_if} inet -alias && _ret=0
632161386Sbrooks
633252015Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
634252015Shrs
635161386Sbrooks	oldifs="$IFS"
636161386Sbrooks	IFS="$_ifs"
637161386Sbrooks	for _inet in $inetList ; do
638161386Sbrooks		# get rid of extraneous line
639161386Sbrooks		[ -z "$_inet" ] && break
640161386Sbrooks
641161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
642161386Sbrooks
643161386Sbrooks		IFS="$oldifs"
644252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet} delete
645161386Sbrooks		IFS="$_ifs"
646161386Sbrooks		_ret=0
647161386Sbrooks	done
648161386Sbrooks	IFS="$oldifs"
649161386Sbrooks
650161386Sbrooks	return $_ret
651152441Sbrooks}
652152441Sbrooks
653197139Shrs# ipv6_down if
654197139Shrs#	remove IPv6 addresses from the interface $if
655197139Shrsipv6_down()
656197139Shrs{
657197139Shrs	local _if _ifs _ret inetList oldifs _inet6
658197139Shrs	_if=$1
659197139Shrs	_ifs="^"
660197139Shrs	_ret=1
661197139Shrs
662197139Shrs	if ! ipv6if $_if; then
663197139Shrs		return 0
664197139Shrs	fi
665197139Shrs
666197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
667226652Shrs	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
668252015Shrs	ifalias ${_if} inet6 -alias && _ret=0
669197139Shrs
670252015Shrs	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
671197139Shrs
672197139Shrs	oldifs="$IFS"
673197139Shrs	IFS="$_ifs"
674197139Shrs	for _inet6 in $inetList ; do
675197139Shrs		# get rid of extraneous line
676197139Shrs		[ -z "$_inet6" ] && break
677197139Shrs
678197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
679197139Shrs
680197139Shrs		IFS="$oldifs"
681252015Shrs		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
682197139Shrs		IFS="$_ifs"
683197139Shrs		_ret=0
684197139Shrs	done
685197139Shrs	IFS="$oldifs"
686197139Shrs
687197139Shrs	return $_ret
688197139Shrs}
689197139Shrs
690252015Shrs# ifalias if af action
691252015Shrs#	Configure or remove aliases for network interface $if.
692113674Smtm#	It returns 0 if at least one alias was configured or
693252015Shrs#	removed, or 1 if there were none.
694113674Smtm#
695252015Shrsifalias()
696113674Smtm{
697197139Shrs	local _ret
698113674Smtm	_ret=1
699197139Shrs
700252015Shrs	afexists $2 || return $_ret
701252015Shrs
702197139Shrs	case "$2" in
703252015Shrs	inet|inet6)
704252015Shrs		ifalias_af_common $1 $2 $3 && _ret=0
705197139Shrs		;;
706197139Shrs	esac
707197139Shrs
708197139Shrs	return $_ret
709197139Shrs}
710197139Shrs
711252015Shrs# ifalias_expand_addr af action addr
712252015Shrs#	Expand address range ("N-M") specification in addr.
713252015Shrs#	"addr" must not include an address-family keyword.
714252015Shrs#	The results will include an address-family keyword.
715197139Shrs#
716252015Shrsifalias_expand_addr()
717197139Shrs{
718197139Shrs
719252015Shrs	afexists $1 || return
720252015Shrs	ifalias_expand_addr_$1 $2 $3
721197139Shrs}
722197139Shrs
723252015Shrs# ifalias_expand_addr_inet action addr
724252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv4.
725197139Shrs#
726252015Shrsifalias_expand_addr_inet()
727197139Shrs{
728252015Shrs	local _action _arg _cidr _cidr_addr
729252015Shrs	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
730252015Shrs	local _retstr _c
731252015Shrs	_action=$1
732252015Shrs	_arg=$2
733252015Shrs	_retstr=
734197139Shrs
735252015Shrs	case $_action:$_arg in
736252015Shrs	*:*--*)		return ;;			# invalid
737252015Shrs	tmp:*)		echo $_arg && return ;;		# already expanded
738252015Shrs	tmp:*-*)	_action="alias"	;;		# to be expanded
739252015Shrs	*:*-*)		;;				# to be expanded
740252015Shrs	*:*)		echo inet $_arg && return ;;	# already expanded
741252015Shrs	esac
742252015Shrs
743252015Shrs	for _cidr in $_arg; do
744252015Shrs		_ipaddr=${_cidr%%/*}
745252015Shrs		_plen=${_cidr##*/}
746252015Shrs		# When subnet prefix length is not specified, use /32.
747252015Shrs		case $_plen in
748252015Shrs		$_ipaddr)	_plen=32 ;;	# "/" character not found
749197139Shrs		esac
750197139Shrs
751252015Shrs		OIFS=$IFS
752252015Shrs		IFS=. set -- $_ipaddr
753252015Shrs		_range=
754252015Shrs		_iphead=
755252015Shrs		_iptail=
756252015Shrs		for _c in $@; do
757252015Shrs			case $_range:$_c in
758252015Shrs			:[0-9]*-[0-9]*)
759252015Shrs				_range=$_c
760197139Shrs			;;
761252015Shrs			:*)
762252015Shrs				_iphead="${_iphead}${_iphead:+.}${_c}"
763197139Shrs			;;
764252015Shrs			*:*)
765252015Shrs				_iptail="${_iptail}${_iptail:+.}${_c}"
766252015Shrs			;;
767252015Shrs			esac
768252015Shrs		done
769252015Shrs		IFS=$OIFS
770252015Shrs		_iplow=${_range%-*}
771252015Shrs		_iphigh=${_range#*-}
772252015Shrs
773252015Shrs		# clear netmask when removing aliases
774252015Shrs		if [ "$_action" = "-alias" ]; then
775252015Shrs			_plen=""
776252015Shrs		fi
777252015Shrs
778252015Shrs		_ipcount=$_iplow
779252015Shrs		while [ "$_ipcount" -le "$_iphigh" ]; do
780252015Shrs			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
781252015Shrs			if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
782252015Shrs				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."
783252015Shrs				break
784252015Shrs			else
785252015Shrs				_ipcount=$(($_ipcount + 1))
786252015Shrs			fi
787252015Shrs			# Forcibly set /32 for remaining aliases.
788252015Shrs			_plen=32
789252015Shrs		done
790197139Shrs	done
791197139Shrs
792252015Shrs	for _c in $_retstr; do
793252015Shrs		ifalias_expand_addr_inet $_action $_c
794252015Shrs	done
795113674Smtm}
796100280Sgordon
797252015Shrs# ifalias_expand_addr_inet6 action addr
798252015Shrs#	Helper function for ifalias_expand_addr().  Handles IPv6.
799116029Smtm#
800252015Shrsifalias_expand_addr_inet6()
801116029Smtm{
802252015Shrs	local _action _arg _cidr _cidr_addr
803252015Shrs	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
804252015Shrs	local _ipv4part
805252015Shrs	local _retstr _c
806252015Shrs	_action=$1
807252015Shrs	_arg=$2
808252015Shrs	_retstr=
809197139Shrs
810252015Shrs	case $_action:$_arg in
811252015Shrs	*:*--*)		return ;;			# invalid
812252015Shrs	tmp:*)		echo $_arg && return ;;
813252015Shrs	tmp:*-*)	_action="alias"	;;
814252015Shrs	*:*-*)		;;
815252015Shrs	*:*)		echo inet6 $_arg && return ;;
816197139Shrs	esac
817197139Shrs
818252015Shrs	for _cidr in $_arg; do
819252015Shrs		_ipaddr="${_cidr%%/*}"
820252015Shrs		_plen="${_cidr##*/}"
821252015Shrs
822252015Shrs		case $_action:$_ipaddr:$_cidr in
823252015Shrs		-alias:*:*)		unset _plen ;;
824252015Shrs		*:$_cidr:$_ipaddr)	unset _plen ;;
825252015Shrs		esac
826252015Shrs
827252015Shrs		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
828252015Shrs			# Handle !v4mapped && !v4compat addresses.
829252015Shrs
830252015Shrs			# The default prefix length is 64.
831252015Shrs			case $_ipaddr:$_cidr in
832252015Shrs			$_cidr:$_ipaddr)	_plen="64" ;;
833252015Shrs			esac
834252015Shrs			_ipleft=${_ipaddr%-*}
835252015Shrs			_ipright=${_ipaddr#*-}
836252015Shrs			_iplow=${_ipleft##*:}
837252015Shrs			_iphigh=${_ipright%%:*}
838252015Shrs			_ipleft=${_ipleft%:*}
839252015Shrs			_ipright=${_ipright#*:}
840252015Shrs
841252015Shrs			if [ "$_iphigh" = "$_ipright" ]; then
842252015Shrs				unset _ipright
843252015Shrs			else
844252015Shrs				_ipright=:$_ipright
845252015Shrs			fi
846252015Shrs
847252015Shrs			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
848252015Shrs				_iplow=$((0x$_iplow))
849252015Shrs				_iphigh=$((0x$_iphigh))
850252015Shrs				_ipcount=$_iplow
851252015Shrs				while [ $_ipcount -le $_iphigh ]; do
852252015Shrs					_r=`printf "%s:%04x%s%s" \
853252015Shrs					    $_ipleft $_ipcount $_ipright \
854252015Shrs					    ${_plen:+/}$_plen`
855252015Shrs					_retstr="$_retstr $_r"
856252015Shrs					if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
857252015Shrs					then
858252015Shrs						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."
859252015Shrs						break
860252015Shrs					else
861252015Shrs						_ipcount=$(($_ipcount + 1))
862252015Shrs					fi
863252015Shrs				done
864252015Shrs			else
865252015Shrs				_retstr="${_ipaddr}${_plen:+/}${_plen}"
866252015Shrs			fi
867252015Shrs
868252015Shrs			for _c in $_retstr; do
869252015Shrs				ifalias_expand_addr_inet6 $_action $_c
870252015Shrs			done
871252015Shrs		else
872252015Shrs			# v4mapped/v4compat should handle as an IPv4 alias
873252015Shrs			_ipv4part=${_ipaddr##*:}
874252015Shrs
875252015Shrs			# Adjust prefix length if any.  If not, set the
876252015Shrs			# default prefix length as 32.
877252015Shrs			case $_ipaddr:$_cidr in
878252015Shrs			$_cidr:$_ipaddr)	_plen=32 ;;
879252015Shrs			*)			_plen=$(($_plen - 96)) ;;
880252015Shrs			esac
881252015Shrs
882252015Shrs			_retstr=`ifalias_expand_addr_inet \
883252015Shrs			    tmp ${_ipv4part}${_plen:+/}${_plen}`
884252015Shrs			for _c in $_retstr; do
885252015Shrs				ifalias_expand_addr_inet $_action $_c
886252015Shrs			done
887252015Shrs		fi
888252015Shrs	done
889197139Shrs}
890197139Shrs
891252015Shrs# ifalias_af_common_handler if af action args
892252015Shrs#	Helper function for ifalias_af_common().
893197139Shrs#
894252015Shrsifalias_af_common_handler()
895197139Shrs{
896252015Shrs	local _ret _if _af _action _args _c _tmpargs
897252015Shrs
898197139Shrs	_ret=1
899252015Shrs	_if=$1
900252015Shrs	_af=$2
901252015Shrs	_action=$3
902252015Shrs	shift 3
903252015Shrs	_args=$*
904197139Shrs
905252015Shrs	case $_args in
906252015Shrs	${_af}\ *)	;;
907252015Shrs	*)	return	;;
908252015Shrs	esac
909252015Shrs
910252015Shrs	_tmpargs=
911252015Shrs	for _c in $_args; do
912252015Shrs		case $_c in
913252015Shrs		${_af})
914252015Shrs			case $_tmpargs in
915252015Shrs			${_af}\ *-*)
916252015Shrs				ifalias_af_common_handler $_if $_af $_action \
917252015Shrs				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
918197139Shrs			;;
919252015Shrs			${_af}\ *)
920252015Shrs				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
921197139Shrs			;;
922252015Shrs			esac
923252015Shrs			_tmpargs=$_af
924252015Shrs		;;
925252015Shrs		*)
926252015Shrs			_tmpargs="$_tmpargs $_c"
927252015Shrs		;;
928197139Shrs		esac
929197139Shrs	done
930252015Shrs	# Process the last component if any.
931252015Shrs	if [ -n "$_tmpargs}" ]; then
932252015Shrs		case $_tmpargs in
933252015Shrs		${_af}\ *-*)
934252015Shrs			ifalias_af_common_handler $_if $_af $_action \
935252015Shrs			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
936252015Shrs		;;
937252015Shrs		${_af}\ *)
938252015Shrs			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
939252015Shrs		;;
940252015Shrs		esac
941252015Shrs	fi
942197139Shrs
943197139Shrs	return $_ret
944197139Shrs}
945197139Shrs
946252015Shrs# ifalias_af_common if af action
947252015Shrs#	Helper function for ifalias().
948197139Shrs#
949252015Shrsifalias_af_common()
950197139Shrs{
951252015Shrs	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
952252015Shrs
953197139Shrs	_ret=1
954252015Shrs	_aliasn=
955252015Shrs	_if=$1
956252015Shrs	_af=$2
957252015Shrs	_action=$3
958197139Shrs
959252015Shrs	# ifconfig_IF_aliasN which starts with $_af
960197139Shrs	alias=0
961197139Shrs	while : ; do
962252015Shrs		ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
963252015Shrs		_iaf=
964252015Shrs		case $ifconfig_args in
965252015Shrs		inet\ *)	_iaf=inet ;;
966252015Shrs		inet6\ *)	_iaf=inet6 ;;
967252015Shrs		ipx\ *)		_iaf=ipx ;;
968252015Shrs		esac
969252015Shrs
970252015Shrs		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
971252015Shrs		${_af}:*:${_af}:*)
972252015Shrs			_aliasn="$_aliasn $ifconfig_args"
973197139Shrs			;;
974252015Shrs		${_af}:*:"":"")
975116029Smtm			break
976197139Shrs			;;
977252015Shrs		inet:alias:"":*)
978252015Shrs			_aliasn="$_aliasn inet $ifconfig_args"
979252015Shrs			warn "\$ifconfig_${_if}_alias${alias} needs " \
980252015Shrs			    "\"inet\" keyword for an IPv4 address."
981197139Shrs		esac
982252015Shrs		alias=$(($alias + 1))
983116029Smtm	done
984197139Shrs
985197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
986252015Shrs	case $_af in
987252015Shrs	inet6)
988252015Shrs		alias=0
989252015Shrs		while : ; do
990252015Shrs			ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
991252015Shrs			case ${_action}:"${ifconfig_args}" in
992252015Shrs			*:"")
993252015Shrs				break
994197139Shrs			;;
995252015Shrs			alias:*)
996252015Shrs				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
997252015Shrs				warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
998252015Shrs				    "is obsolete.  Use ifconfig_$1_aliasN " \
999252015Shrs				    "instead."
1000252015Shrs			;;
1001252015Shrs			esac
1002252015Shrs			alias=$(($alias + 1))
1003252015Shrs		done
1004252015Shrs	esac
1005252015Shrs
1006252015Shrs	# backward compatibility: ipv4_addrs_IF.
1007252015Shrs	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1008252015Shrs		_aliasn="$_aliasn inet $_tmpargs"
1009252015Shrs	done
1010252015Shrs
1011252015Shrs	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1012252015Shrs	_tmpargs=
1013252015Shrs	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1014252015Shrs		case $_c in
1015252015Shrs		inet|inet6|ipx)
1016252015Shrs			case $_tmpargs in
1017252015Shrs			${_af}\ *)
1018252015Shrs				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1019252015Shrs			;;
1020252015Shrs			esac
1021252015Shrs			_tmpargs=$_c
1022252015Shrs		;;
1023197139Shrs		*)
1024252015Shrs			_tmpargs="$_tmpargs $_c"
1025197139Shrs		esac
1026197139Shrs	done
1027252015Shrs	# Process the last component
1028252015Shrs	case $_tmpargs in
1029252015Shrs	${_af}\ *)
1030252015Shrs		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1031252015Shrs	;;
1032252015Shrs	esac
1033197139Shrs
1034116029Smtm	return $_ret
1035116029Smtm}
1036116029Smtm
1037226652Shrs# ipv6_prefix_hostid_addr_common if action
1038226652Shrs#	Add or remove IPv6 prefix + hostid addr on the interface $if
1039226652Shrs#
1040226652Shrsipv6_prefix_hostid_addr_common()
1041197139Shrs{
1042226652Shrs	local _if _action prefix laddr hostid j address
1043197139Shrs	_if=$1
1044226652Shrs	_action=$2
1045197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1046197139Shrs
1047197139Shrs	if [ -n "${prefix}" ]; then
1048197139Shrs		laddr=`network6_getladdr ${_if}`
1049197139Shrs		hostid=${laddr#fe80::}
1050197139Shrs		hostid=${hostid%\%*}
1051197139Shrs
1052197139Shrs		for j in ${prefix}; do
1053252015Shrs			# The default prefixlen is 64.
1054252015Shrs			plen=${j#*/}
1055252015Shrs			case $j:$plen in
1056252015Shrs			$plen:$j)	plen=64 ;;
1057252015Shrs			*)		j=${j%/*} ;;
1058252015Shrs			esac
1059197139Shrs
1060252015Shrs			# Normalize the last part by removing ":"
1061252015Shrs			j=${j%:*}
1062252015Shrs			j=${j%:}
1063252015Shrs			OIFS=$IFS; IFS=":"; set -- $j; nj=$#; IFS=$OIFS
1064252015Shrs			OIFS=$IFS; IFS=":"; set -- $hostid; nh=$#; IFS=$OIFS
1065252015Shrs			if [ $(($nj + $nh)) -eq 8 ]; then
1066252015Shrs				address=$j\:$hostid
1067252015Shrs			else
1068252015Shrs				address=$j\::$hostid
1069252015Shrs			fi
1070252015Shrs
1071252015Shrs			${IFCONFIG_CMD} ${_if} inet6 ${address} \
1072252015Shrs				prefixlen $plen ${_action}
1073252015Shrs
1074197139Shrs			# if I am a router, add subnet router
1075197139Shrs			# anycast address (RFC 2373).
1076197139Shrs			if checkyesno ipv6_gateway_enable; then
1077252015Shrs				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1078252015Shrs					prefixlen $plen ${_action} anycast
1079197139Shrs			fi
1080197139Shrs		done
1081197139Shrs	fi
1082197139Shrs}
1083197139Shrs
1084197139Shrs# ipv6_accept_rtadv_up if
1085197139Shrs#	Enable accepting Router Advertisement and send Router
1086197139Shrs#	Solicitation message
1087197139Shrsipv6_accept_rtadv_up()
1088197139Shrs{
1089197139Shrs	if ipv6_autoconfif $1; then
1090252015Shrs		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1091203433Sume		if ! checkyesno rtsold_enable; then
1092203433Sume			rtsol ${rtsol_flags} $1
1093203433Sume		fi
1094197139Shrs	fi
1095197139Shrs}
1096197139Shrs
1097197139Shrs# ipv6_accept_rtadv_down if
1098197139Shrs#	Disable accepting Router Advertisement
1099197139Shrsipv6_accept_rtadv_down()
1100197139Shrs{
1101197139Shrs	if ipv6_autoconfif $1; then
1102252015Shrs		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1103197139Shrs	fi
1104197139Shrs}
1105197139Shrs
1106113674Smtm# ifscript_up if
1107113674Smtm#	Evaluate a startup script for the $if interface.
1108113674Smtm#	It returns 0 if a script was found and processed or
1109113674Smtm#	1 if no script was found.
1110113674Smtm#
1111113674Smtmifscript_up()
1112100280Sgordon{
1113113674Smtm	if [ -r /etc/start_if.$1 ]; then
1114113674Smtm		. /etc/start_if.$1
1115113674Smtm		return 0
1116197139Shrs	else
1117197139Shrs		return 1
1118113674Smtm	fi
1119100280Sgordon}
1120100280Sgordon
1121116029Smtm# ifscript_down if
1122116029Smtm#	Evaluate a shutdown script for the $if interface.
1123116029Smtm#	It returns 0 if a script was found and processed or
1124116029Smtm#	1 if no script was found.
1125116029Smtm#
1126116029Smtmifscript_down()
1127116029Smtm{
1128116029Smtm	if [ -r /etc/stop_if.$1 ]; then
1129116029Smtm		. /etc/stop_if.$1
1130116029Smtm		return 0
1131197139Shrs	else
1132197139Shrs		return 1
1133116029Smtm	fi
1134116029Smtm}
1135116029Smtm
1136197147Shrs# clone_up
1137197147Shrs#	Create cloneable interfaces.
1138113674Smtm#
1139113674Smtmclone_up()
1140100280Sgordon{
1141197139Shrs	local _prefix _list ifn
1142113674Smtm	_prefix=
1143113674Smtm	_list=
1144197139Shrs
1145197139Shrs	# create_args_IF
1146113674Smtm	for ifn in ${cloned_interfaces}; do
1147252015Shrs		${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1148116774Skuriyama		if [ $? -eq 0 ]; then
1149113674Smtm			_list="${_list}${_prefix}${ifn}"
1150113674Smtm			[ -z "$_prefix" ] && _prefix=' '
1151113674Smtm		fi
1152113674Smtm	done
1153113674Smtm	debug "Cloned: ${_list}"
1154113674Smtm}
1155100280Sgordon
1156197147Shrs# clone_down
1157197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1158197147Shrs#	standard output.
1159113674Smtm#
1160113674Smtmclone_down()
1161113674Smtm{
1162197139Shrs	local _prefix _list ifn
1163113674Smtm	_prefix=
1164113674Smtm	_list=
1165197139Shrs
1166113674Smtm	for ifn in ${cloned_interfaces}; do
1167252015Shrs		${IFCONFIG_CMD} -n ${ifn} destroy
1168116774Skuriyama		if [ $? -eq 0 ]; then
1169113674Smtm			_list="${_list}${_prefix}${ifn}"
1170113674Smtm			[ -z "$_prefix" ] && _prefix=' '
1171113674Smtm		fi
1172113674Smtm	done
1173113674Smtm	debug "Destroyed clones: ${_list}"
1174100280Sgordon}
1175100280Sgordon
1176197147Shrs# childif_create
1177197147Shrs#	Create and configure child interfaces.  Return 0 if child
1178197147Shrs#	interfaces are created.
1179178356Ssam#
1180178356Ssamchildif_create()
1181178356Ssam{
1182201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1183178356Ssam	cfg=1
1184178356Ssam	ifn=$1
1185178356Ssam
1186178527Sbrooks	# Create wireless interfaces
1187178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
1188178527Sbrooks
1189178527Sbrooks	for child in ${child_wlans}; do
1190183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1191189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
1192189759Sbrooks
1193178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1194252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1195189759Sbrooks			if [ -n "${debug_flags}" ]; then
1196189759Sbrooks				wlandebug -i $child ${debug_flags}
1197189759Sbrooks			fi
1198178356Ssam		else
1199252015Shrs			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1200189759Sbrooks			if [ -n "${debug_flags}" ]; then
1201189759Sbrooks				wlandebug -i $i ${debug_flags}
1202189759Sbrooks			fi
1203252015Shrs			${IFCONFIG_CMD} $i name $child && cfg=0
1204178356Ssam		fi
1205188118Sthompsa		if autoif $child; then
1206188118Sthompsa			ifn_start $child
1207188118Sthompsa		fi
1208178356Ssam	done
1209178356Ssam
1210201215Sjhb	# Create vlan interfaces
1211201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1212201215Sjhb
1213201215Sjhb	if [ -n "${child_vlans}" ]; then
1214201215Sjhb		load_kld if_vlan
1215201215Sjhb	fi
1216201215Sjhb
1217201215Sjhb	for child in ${child_vlans}; do
1218201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1219201215Sjhb			child="${ifn}.${child}"
1220201215Sjhb			create_args=`get_if_var $child create_args_IF`
1221252015Shrs			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1222201215Sjhb		else
1223201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1224201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1225252015Shrs				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1226201215Sjhb			else
1227252015Shrs				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1228252015Shrs				${IFCONFIG_CMD} $i name $child && cfg=0
1229201215Sjhb			fi
1230201215Sjhb		fi
1231201215Sjhb		if autoif $child; then
1232201215Sjhb			ifn_start $child
1233201215Sjhb		fi
1234201215Sjhb	done
1235201215Sjhb
1236179001Sbrooks	return ${cfg}
1237178356Ssam}
1238178356Ssam
1239197147Shrs# childif_destroy
1240197147Shrs#	Destroy child interfaces.
1241178356Ssam#
1242178356Ssamchildif_destroy()
1243178356Ssam{
1244201215Sjhb	local cfg child child_vlans child_wlans ifn
1245197139Shrs	cfg=1
1246178356Ssam
1247201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1248178527Sbrooks	for child in ${child_wlans}; do
1249201215Sjhb		if ! ifexists $child; then
1250201215Sjhb			continue
1251201215Sjhb		fi
1252252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1253178356Ssam	done
1254197139Shrs
1255201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1256201215Sjhb	for child in ${child_vlans}; do
1257201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1258201215Sjhb			child="${ifn}.${child}"
1259201215Sjhb		fi
1260201215Sjhb		if ! ifexists $child; then
1261201215Sjhb			continue
1262201215Sjhb		fi
1263252015Shrs		${IFCONFIG_CMD} -n $child destroy && cfg=0
1264201215Sjhb	done
1265201215Sjhb
1266197139Shrs	return ${cfg}
1267178356Ssam}
1268178356Ssam
1269197147Shrs# ng_mkpeer
1270197147Shrs#	Create netgraph nodes.
1271166583Sflz#
1272197147Shrsng_mkpeer()
1273197147Shrs{
1274166583Sflz	ngctl -f - 2> /dev/null <<EOF
1275166583Sflzmkpeer $*
1276166583Sflzmsg dummy nodeinfo
1277166583SflzEOF
1278166583Sflz}
1279166583Sflz
1280197147Shrs# ng_create_one
1281197147Shrs#	Create netgraph nodes.
1282197147Shrs#
1283197147Shrsng_create_one()
1284197147Shrs{
1285197139Shrs	local t
1286197139Shrs
1287166583Sflz	ng_mkpeer $* | while read line; do
1288166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1289166583Sflz		if [ -n "${t}" ]; then
1290166583Sflz			echo ${t}
1291166583Sflz			return
1292166583Sflz		fi
1293166583Sflz	done
1294166583Sflz}
1295166583Sflz
1296197147Shrs# gif_up
1297197147Shrs#	Create gif(4) tunnel interfaces.
1298197147Shrsgif_up()
1299197147Shrs{
1300197139Shrs	local i peers
1301197139Shrs
1302166583Sflz	for i in ${gif_interfaces}; do
1303166583Sflz		peers=`get_if_var $i gifconfig_IF`
1304166583Sflz		case ${peers} in
1305166583Sflz		'')
1306166583Sflz			continue
1307166583Sflz			;;
1308166583Sflz		*)
1309177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1310252015Shrs				${IFCONFIG_CMD} $i create >/dev/null 2>&1
1311177682Sbrooks			else
1312252015Shrs				gif=`${IFCONFIG_CMD} gif create`
1313252015Shrs				${IFCONFIG_CMD} $gif name $i
1314177682Sbrooks			fi
1315252015Shrs			${IFCONFIG_CMD} $i tunnel ${peers}
1316252015Shrs			${IFCONFIG_CMD} $i up
1317166583Sflz			;;
1318166583Sflz		esac
1319166583Sflz	done
1320166583Sflz}
1321166583Sflz
1322166583Sflz# ng_fec_create ifn
1323197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1324197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1325197139Shrsng_fec_create()
1326197139Shrs{
1327166583Sflz	 local req_iface iface bogus
1328166583Sflz	 req_iface="$1"
1329166583Sflz
1330166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1331166583Sflz
1332166583Sflz	 bogus=""
1333166583Sflz	 while true; do
1334166583Sflz		 iface=`ng_create_one fec dummy fec`
1335166583Sflz		 if [ -z "${iface}" ]; then
1336166583Sflz			 exit 2
1337166583Sflz		 fi
1338166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1339166583Sflz			 break
1340166583Sflz		 fi
1341166583Sflz		 bogus="${bogus} ${iface}"
1342166583Sflz	 done
1343166583Sflz
1344166583Sflz	 for iface in ${bogus}; do
1345166583Sflz		 ngctl shutdown ${iface}:
1346166583Sflz	 done
1347166583Sflz}
1348166583Sflz
1349197147Shrs# fec_up
1350197147Shrs#	Create Fast EtherChannel interfaces.
1351197147Shrsfec_up()
1352197147Shrs{
1353197139Shrs	local i j
1354197139Shrs
1355166583Sflz	for i in ${fec_interfaces}; do
1356166583Sflz		ng_fec_create $i
1357166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1358166583Sflz			case ${j} in
1359100282Sdougb			'')
1360100282Sdougb				continue
1361100282Sdougb				;;
1362100282Sdougb			*)
1363166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1364100282Sdougb				;;
1365100282Sdougb			esac
1366100282Sdougb		done
1367166583Sflz	done
1368100282Sdougb}
1369100282Sdougb
1370113674Smtm# ipx_up ifn
1371197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1372197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1373113674Smtm#
1374113674Smtmipx_up()
1375100280Sgordon{
1376197139Shrs	local ifn
1377113674Smtm	ifn="$1"
1378197139Shrs
1379197139Shrs	# ifconfig_IF_ipx
1380197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1381113674Smtm	if [ -n "${ifconfig_args}" ]; then
1382252015Shrs		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1383113674Smtm		return 0
138485831Sdes	fi
1385197139Shrs
1386113674Smtm	return 1
1387113674Smtm}
138885831Sdes
1389116029Smtm# ipx_down ifn
1390116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1391116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1392113674Smtm#
1393116029Smtmipx_down()
1394116029Smtm{
1395197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1396197139Shrs	_if=$1
1397116100Smtm	_ifs="^"
1398116100Smtm	_ret=1
1399252015Shrs	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1400197139Shrs	oldifs="$IFS"
1401116100Smtm
1402116100Smtm	IFS="$_ifs"
1403116100Smtm	for _ipx in $ipxList ; do
1404116100Smtm		# get rid of extraneous line
1405116100Smtm		[ -z "$_ipx" ] && break
1406116100Smtm
1407116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1408116100Smtm
1409116100Smtm		IFS="$oldifs"
1410252015Shrs		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1411116100Smtm		IFS="$_ifs"
1412116100Smtm		_ret=0
1413116100Smtm	done
1414116100Smtm	IFS="$oldifs"
1415116100Smtm
1416116100Smtm	return $_ret
1417116029Smtm}
1418116029Smtm
1419137070Spjd# ifnet_rename
1420137070Spjd#	Rename all requested interfaces.
1421116029Smtm#
1422137070Spjdifnet_rename()
1423137070Spjd{
1424197139Shrs	local _if _ifname
1425137070Spjd
1426197139Shrs	# ifconfig_IF_name
1427252015Shrs	for _if in `${IFCONFIG_CMD} -l`; do
1428157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1429137070Spjd		if [ ! -z "$_ifname" ]; then
1430252015Shrs			${IFCONFIG_CMD} $_if name $_ifname
1431137070Spjd		fi
1432137070Spjd	done
1433197139Shrs
1434137070Spjd	return 0
1435137070Spjd}
1436137070Spjd
1437113674Smtm# list_net_interfaces type
1438113674Smtm#	List all network interfaces. The type of interface returned
1439113674Smtm#	can be controlled by the type argument. The type
1440113674Smtm#	argument can be any of the following:
1441197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1442197147Shrs#		dhcp	- list only DHCP configured interfaces
1443197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1444197139Shrs#				  Address Autoconf configured interfaces
1445197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1446197139Shrs#				  configured interfaces
1447113674Smtm#	If no argument is specified all network interfaces are output.
1448134429Syar#	Note that the list will include cloned interfaces if applicable.
1449134429Syar#	Cloned interfaces must already exist to have a chance to appear
1450134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1451113674Smtm#
1452113674Smtmlist_net_interfaces()
1453113674Smtm{
1454197139Shrs	local type _tmplist _list _autolist _lo _if
1455113674Smtm	type=$1
145665532Snectar
1457149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
145851231Ssheldonh	#
1459197139Shrs	_tmplist=
146051231Ssheldonh	case ${network_interfaces} in
146151231Ssheldonh	[Aa][Uu][Tt][Oo])
1462252015Shrs		_autolist="`${IFCONFIG_CMD} -l`"
1463149726Sbrooks		_lo=
1464149401Sbrooks		for _if in ${_autolist} ; do
1465149401Sbrooks			if autoif $_if; then
1466149726Sbrooks				if [ "$_if" = "lo0" ]; then
1467149726Sbrooks					_lo="lo0 "
1468149726Sbrooks				else
1469197139Shrs					_tmplist="${_tmplist} ${_if}"
1470149726Sbrooks				fi
1471149401Sbrooks			fi
1472149401Sbrooks		done
1473197139Shrs		_tmplist="${_lo}${_tmplist# }"
147451231Ssheldonh		;;
147583677Sbrooks	*)
1476149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1477196478Sdougb
1478196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1479196478Sdougb		#
1480196478Sdougb		case "$_tmplist" in
1481196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1482196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1483196478Sdougb		esac
148483677Sbrooks		;;
148551231Ssheldonh	esac
148649122Sbrian
1487197139Shrs	_list=
1488197139Shrs	case "$type" in
1489197139Shrs	nodhcp)
1490197139Shrs		for _if in ${_tmplist} ; do
1491197139Shrs			if ! dhcpif $_if && \
1492197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1493197139Shrs				_list="${_list# } ${_if}"
1494197147Shrs			fi
1495197139Shrs		done
1496197139Shrs		;;
1497197139Shrs	dhcp)
1498197147Shrs		for _if in ${_tmplist} ; do
1499197147Shrs			if dhcpif $_if; then
1500197139Shrs				_list="${_list# } ${_if}"
1501197147Shrs			fi
1502197147Shrs		done
1503113674Smtm		;;
1504197139Shrs	noautoconf)
1505197139Shrs		for _if in ${_tmplist} ; do
1506197139Shrs			if ! ipv6_autoconfif $_if && \
1507197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1508197139Shrs				_list="${_list# } ${_if}"
1509197139Shrs			fi
1510197139Shrs		done
1511113674Smtm		;;
1512197139Shrs	autoconf)
1513197139Shrs		for _if in ${_tmplist} ; do
1514197139Shrs			if ipv6_autoconfif $_if; then
1515197139Shrs				_list="${_list# } ${_if}"
1516197139Shrs			fi
1517197139Shrs		done
1518197139Shrs		;;
1519197139Shrs	*)
1520197139Shrs		_list=${_tmplist}
1521197139Shrs		;;
1522113674Smtm	esac
1523197139Shrs
1524197139Shrs	echo $_list
1525197139Shrs
1526130151Sschweikh	return 0
152725184Sjkh}
1528114942Sume
1529179003Sbrooks# get_default_if -address_family
1530179003Sbrooks#	Get the interface of the default route for the given address family.
1531179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1532179003Sbrooks#
1533179003Sbrooksget_default_if()
1534179003Sbrooks{
1535197139Shrs	local routeget oldifs defif line
1536197139Shrs	defif=
1537179003Sbrooks	oldifs="$IFS"
1538179003Sbrooks	IFS="
1539179003Sbrooks"
1540197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1541179003Sbrooks		case $line in
1542179003Sbrooks		*interface:*)
1543179003Sbrooks			defif=${line##*: }
1544179003Sbrooks			;;
1545179003Sbrooks		esac
1546179003Sbrooks	done
1547179003Sbrooks	IFS=${oldifs}
1548179003Sbrooks
1549179003Sbrooks	echo $defif
1550179003Sbrooks}
1551179003Sbrooks
1552197147Shrs# hexdigit arg
1553197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1554114942Sumehexdigit()
1555114942Sume{
1556221884Sjilles	printf '%x\n' "$1"
1557114942Sume}
1558114942Sume
1559197147Shrs# hexprint arg
1560197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1561114942Sumehexprint()
1562114942Sume{
1563221884Sjilles	printf '%x\n' "$1"
1564114942Sume}
1565114942Sume
1566196436Sdougbis_wired_interface()
1567196436Sdougb{
1568196436Sdougb	local media
1569196436Sdougb
1570252015Shrs	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1571196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1572196436Sdougb	esac
1573196436Sdougb
1574196436Sdougb	test "$media" = "Ethernet"
1575196436Sdougb}
1576196436Sdougb
1577197147Shrs# network6_getladdr if [flag]
1578197147Shrs#	Echo link-local address from $if if any.
1579197147Shrs#	If flag is defined, tentative ones will be excluded.
1580114942Sumenetwork6_getladdr()
1581114942Sume{
1582252015Shrs	local _if _flag proto addr rest
1583252015Shrs	_if=$1
1584252015Shrs	_flag=$2
1585252015Shrs
1586252015Shrs	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1587252015Shrs		case "${proto}/${addr}/${_flag}/${rest}" in
1588252015Shrs		inet6/fe80::*//*)
1589252015Shrs			echo ${addr}
1590252015Shrs		;;
1591252015Shrs		inet6/fe80:://*tentative*)	# w/o flag
1592252015Shrs			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1593252015Shrs			network6_getladdr $_if $_flags
1594252015Shrs		;;
1595252015Shrs		inet6/fe80::/*/*tentative*)	# w/ flag
1596252015Shrs			echo ${addr}
1597252015Shrs		;;
1598252015Shrs		*)
1599252015Shrs			continue
1600252015Shrs		;;
1601114942Sume		esac
1602252015Shrs
1603252015Shrs		return
1604114942Sume	done
1605114942Sume}
1606