network.subr revision 197139
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 197139 2009-09-12 22:13:41Z hrs $
2666830Sobrien#
2725184Sjkh
28113674Smtm#
29113674Smtm# Subroutines commonly used from network startup scripts.
30113674Smtm# Requires that rc.conf be loaded first.
31113674Smtm#
3225184Sjkh
33178356Ssam# ifn_start ifn
34178356Ssam# Bring up and configure an interface.  If some configuration is applied
35178356Ssam# print the interface configuration.
36178356Ssam#
37178356Ssamifn_start()
38178356Ssam{
39178356Ssam	local ifn cfg
40178356Ssam	ifn="$1"
41178356Ssam	cfg=1
42178356Ssam
43178356Ssam	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
44178356Ssam
45178356Ssam	ifscript_up ${ifn} && cfg=0
46178356Ssam	ifconfig_up ${ifn} && cfg=0
47178356Ssam	ipv4_up ${ifn} && cfg=0
48197139Shrs	ipv6_up ${ifn} && cfg=0
49178356Ssam	ipx_up ${ifn} && cfg=0
50197139Shrs	childif_create ${ifn} && cfg=0
51178356Ssam
52178356Ssam	return $cfg
53178356Ssam}
54178356Ssam
55197139Shrs# ifn_stop ifn
56178356Ssam# Shutdown and de-configure an interface.  If action is taken print the
57178356Ssam# interface name.
58178356Ssam#
59178356Ssamifn_stop()
60178356Ssam{
61178356Ssam	local ifn cfg
62178356Ssam	ifn="$1"
63178356Ssam	cfg=1
64178356Ssam
65197139Shrs	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
66178356Ssam
67178356Ssam	ipx_down ${ifn} && cfg=0
68197139Shrs	ipv6_down ${ifn} && cfg=0
69178356Ssam	ipv4_down ${ifn} && cfg=0
70178356Ssam	ifconfig_down ${ifn} && cfg=0
71178356Ssam	ifscript_down ${ifn} && cfg=0
72197139Shrs	childif_destroy ${ifn} && cfg=0
73178356Ssam
74178356Ssam	return $cfg
75178356Ssam}
76178356Ssam
77113674Smtm# ifconfig_up if
78113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
79113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
80113674Smtm#	arguments were found and executed or 1 if the interface
81147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
82147088Sbrooks#	here.
83113674Smtm#
84113674Smtmifconfig_up()
85113674Smtm{
86197139Shrs	local _cfg _ipv6_opts ifconfig_args
87147088Sbrooks	_cfg=1
88147088Sbrooks
89197139Shrs	# ifconfig_IF
90147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
91113674Smtm	if [ -n "${ifconfig_args}" ]; then
92178356Ssam		ifconfig $1 ${ifconfig_args}
93147088Sbrooks		_cfg=0
94113674Smtm	fi
95147088Sbrooks
96197139Shrs	# inet6 specific
97197139Shrs	if afexists inet6; then
98197139Shrs		if ipv6if $1; then
99197139Shrs			if checkyesno ipv6_gateway_enable; then
100197139Shrs				_ipv6_opts="-accept_rtadv auto_linklocal"
101197139Shrs			else
102197139Shrs				_ipv6_opts="auto_linklocal"
103197139Shrs			fi
104197139Shrs		else
105197139Shrs			_ipv6_opts="-auto_linklocal ifdisabled"
106197139Shrs		fi
107197139Shrs
108197139Shrs		ifconfig $1 inet6 ${_ipv6_opts}
109197139Shrs
110197139Shrs		# ifconfig_IF_ipv6
111197139Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
112197139Shrs		if [ -n "${ifconfig_args}" ]; then
113197139Shrs			ifconfig $1 inet6 -ifdisabled
114197139Shrs			ifconfig $1 ${ifconfig_args}
115197139Shrs			_cfg=0
116197139Shrs		fi
117197139Shrs
118197139Shrs		# backward compatiblity: $ipv6_ifconfig_IF
119197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
120197139Shrs		if [ -n "${ifconfig_args}" ]; then
121197139Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
122197139Shrs			    "  Use ifconfig_$1_ipv6 instead."
123197139Shrs			ifconfig $1 inet6 -ifdisabled
124197139Shrs			ifconfig $1 inet6 ${ifconfig_args}
125197139Shrs			_cfg=0
126197139Shrs		fi
127197139Shrs	fi
128197139Shrs
129197139Shrs	if [ ${_cfg} -eq 0 ]; then
130197139Shrs		ifconfig $1 up
131197139Shrs	fi
132197139Shrs
133147088Sbrooks	if wpaif $1; then
134147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
135147088Sbrooks		_cfg=0		# XXX: not sure this should count
136147088Sbrooks	fi
137147088Sbrooks
138147088Sbrooks	if dhcpif $1; then
139149726Sbrooks		if [ $_cfg -ne 0 ] ; then
140149726Sbrooks			ifconfig $1 up
141149726Sbrooks		fi
142157706Sbrooks		if syncdhcpif $1; then
143157706Sbrooks			/etc/rc.d/dhclient start $1
144157706Sbrooks		fi
145147088Sbrooks		_cfg=0
146147088Sbrooks	fi
147147088Sbrooks
148147121Sbrooks	return $_cfg
149113674Smtm}
15025184Sjkh
151116029Smtm# ifconfig_down if
152161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
153161386Sbrooks#	the interface exists.
154116029Smtm#
155116029Smtmifconfig_down()
156116029Smtm{
157197139Shrs	local _cfg
158147121Sbrooks	_cfg=1
159116029Smtm
160147088Sbrooks	if wpaif $1; then
161147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
162147121Sbrooks		_cfg=0
163147088Sbrooks	fi
164147088Sbrooks
165147088Sbrooks	if dhcpif $1; then
166147088Sbrooks		/etc/rc.d/dhclient stop $1
167147088Sbrooks		_cfg=0
168147088Sbrooks	fi
169147088Sbrooks
170161386Sbrooks	if ifexists $1; then
171161386Sbrooks		ifconfig $1 down
172161386Sbrooks		_cfg=0
173161386Sbrooks	fi
174157706Sbrooks
175147121Sbrooks	return $_cfg
176116029Smtm}
177116029Smtm
178157706Sbrooks# get_if_var if var [default]
179157706Sbrooks#       Return the value of the pseudo-hash corresponding to $if where
180157706Sbrooks#       $var is a string containg the sub-string "IF" which will be
181157706Sbrooks#       replaced with $if after the characters defined in _punct are
182157706Sbrooks#       replaced with '_'. If the variable is unset, replace it with
183157706Sbrooks#       $default if given.
184157706Sbrooksget_if_var()
185157706Sbrooks{
186197139Shrs	local _if _punct _var _default prefix suffix
187197139Shrs
188157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
189157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
190157706Sbrooks	fi
191157706Sbrooks
192157706Sbrooks	_if=$1
193157706Sbrooks	_punct=". - / +"
194157736Sbrooks	for _punct_c in $_punct; do
195157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
196157706Sbrooks	done
197157706Sbrooks	_var=$2
198157706Sbrooks	_default=$3
199157706Sbrooks
200157706Sbrooks	prefix=${_var%%IF*}
201157706Sbrooks	suffix=${_var##*IF}
202168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
203157706Sbrooks}
204157706Sbrooks
205197139Shrs# _ifconfig_getargs if [af]
206147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
207147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
208147088Sbrooks#	outside this file.
209147088Sbrooks_ifconfig_getargs()
210147088Sbrooks{
211197139Shrs	local _ifn _af
212147088Sbrooks	_ifn=$1
213197139Shrs	_af=${2+_$2}
214197139Shrs
215147088Sbrooks	if [ -z "$_ifn" ]; then
216147088Sbrooks		return 1
217147088Sbrooks	fi
218147088Sbrooks
219197139Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
220147088Sbrooks}
221147088Sbrooks
222197139Shrs# ifconfig_getargs if [af]
223147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
224147088Sbrooks#	args such as DHCP and WPA.
225147088Sbrooksifconfig_getargs()
226147088Sbrooks{
227197139Shrs	local _tmpargs _arg _args
228197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
229147088Sbrooks	if [ $? -eq 1 ]; then
230147088Sbrooks		return 1
231147088Sbrooks	fi
232147088Sbrooks	_args=
233147088Sbrooks
234147088Sbrooks	for _arg in $_tmpargs; do
235147088Sbrooks		case $_arg in
236157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
237157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
238157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
239157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
240157706Sbrooks		[Ww][Pp][Aa]) ;;
241147088Sbrooks		*)
242147088Sbrooks			_args="$_args $_arg"
243147088Sbrooks			;;
244147088Sbrooks		esac
245147088Sbrooks	done
246147088Sbrooks
247147088Sbrooks	echo $_args
248147088Sbrooks}
249147088Sbrooks
250149401Sbrooks# autoif
251149401Sbrooks#	Returns 0 if the interface should be automaticly configured at
252149401Sbrooks#	boot time and 1 otherwise.
253149401Sbrooksautoif()
254149401Sbrooks{
255197139Shrs	local _tmpargs _arg
256149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
257197139Shrs
258149401Sbrooks	for _arg in $_tmpargs; do
259149401Sbrooks		case $_arg in
260149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
261149401Sbrooks			return 1
262149401Sbrooks			;;
263149401Sbrooks		esac
264149401Sbrooks	done
265197139Shrs
266149401Sbrooks	return 0
267149401Sbrooks}
268149401Sbrooks
269147088Sbrooks# dhcpif if
270147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
271147088Sbrooksdhcpif()
272147088Sbrooks{
273197139Shrs	local _tmpargs _arg
274147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
275197139Shrs
276147088Sbrooks	for _arg in $_tmpargs; do
277147088Sbrooks		case $_arg in
278147088Sbrooks		[Dd][Hh][Cc][Pp])
279147088Sbrooks			return 0
280147088Sbrooks			;;
281157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
282157706Sbrooks			return 0
283157706Sbrooks			;;
284157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
285157706Sbrooks			return 0
286157706Sbrooks			;;
287147088Sbrooks		esac
288147088Sbrooks	done
289197139Shrs
290147088Sbrooks	return 1
291147088Sbrooks}
292147088Sbrooks
293157706Sbrooks# syncdhcpif
294157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
295157706Sbrooks#	1 otherwise.
296157706Sbrookssyncdhcpif()
297157706Sbrooks{
298197139Shrs	local _tmpargs _arg
299157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
300197139Shrs
301157706Sbrooks	for _arg in $_tmpargs; do
302157706Sbrooks		case $_arg in
303157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
304157706Sbrooks			return 1
305157706Sbrooks			;;
306157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
307157706Sbrooks			return 0
308157706Sbrooks			;;
309157706Sbrooks		esac
310157706Sbrooks	done
311197139Shrs
312197139Shrs	checkyesno synchronous_dhclient
313157706Sbrooks}
314157706Sbrooks
315147088Sbrooks# wpaif if
316147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
317147088Sbrookswpaif()
318147088Sbrooks{
319197139Shrs	local _tmpargs _arg
320147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
321197139Shrs
322147088Sbrooks	for _arg in $_tmpargs; do
323147088Sbrooks		case $_arg in
324147088Sbrooks		[Ww][Pp][Aa])
325147088Sbrooks			return 0
326147088Sbrooks			;;
327147088Sbrooks		esac
328147088Sbrooks	done
329197139Shrs
330147088Sbrooks	return 1
331147088Sbrooks}
332147088Sbrooks
333197139Shrs# afexists af
334197139Shrs#	Returns 0 if the address family is enabled in the kernel
335197139Shrs#	1 otherwise.
336197139Shrsafexists()
337197139Shrs{
338197139Shrs	local _af
339197139Shrs	_af=$1
340197139Shrs
341197139Shrs	case ${_af} in
342197139Shrs	inet)
343197139Shrs		${SYSCTL_N} net.inet > /dev/null 2>&1
344197139Shrs		;;
345197139Shrs	inet6)
346197139Shrs		${SYSCTL_N} net.inet6 > /dev/null 2>&1
347197139Shrs		;;
348197139Shrs	*)
349197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
350197139Shrs		;;
351197139Shrs	esac
352197139Shrs}
353197139Shrs
354197139Shrs# noafif if
355197139Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
356197139Shrsnoafif()
357197139Shrs{
358197139Shrs	local _if
359197139Shrs	_if=$1
360197139Shrs
361197139Shrs	case $_if in
362197139Shrs	pflog[0-9]*|\
363197139Shrs	pfsync[0-9]*|\
364197139Shrs	an[0-9]*|\
365197139Shrs	ath[0-9]*|\
366197139Shrs	ipw[0-9]*|\
367197139Shrs	iwi[0-9]*|\
368197139Shrs	iwn[0-9]*|\
369197139Shrs	ral[0-9]*|\
370197139Shrs	wi[0-9]*|\
371197139Shrs	wl[0-9]*|\
372197139Shrs	wpi[0-9]*)
373197139Shrs		return 0
374197139Shrs		;;
375197139Shrs	esac
376197139Shrs
377197139Shrs	return 1
378197139Shrs}
379197139Shrs
380162490Sbrooks# ipv6if if
381162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
382162490Sbrooks#	1 otherwise.
383162490Sbrooksipv6if()
384162490Sbrooks{
385197139Shrs	local _if i
386197139Shrs	_if=$1
387197139Shrs
388197139Shrs	if ! afexists inet6; then
389162490Sbrooks		return 1
390162490Sbrooks	fi
391197139Shrs
392197139Shrs	# lo0 is always IPv6-enabled
393197139Shrs	case $_if in
394197139Shrs	lo0)
395197139Shrs		return 0
396197139Shrs		;;
397197139Shrs	esac
398197139Shrs
399162490Sbrooks	case "${ipv6_network_interfaces}" in
400162490Sbrooks	[Aa][Uu][Tt][Oo])
401162490Sbrooks		return 0
402162490Sbrooks		;;
403162490Sbrooks	''|[Nn][Oo][Nn][Ee])
404162490Sbrooks		return 1
405162490Sbrooks		;;
406162490Sbrooks	esac
407197139Shrs
408197139Shrs	for i in ${ipv6_network_interfaces}; do
409197139Shrs		if [ "$i" = "$_if" ]; then
410162490Sbrooks			return 0
411162490Sbrooks		fi
412162490Sbrooks	done
413197139Shrs
414162490Sbrooks	return 1
415162490Sbrooks}
416162490Sbrooks
417197139Shrs# ipv6_autoconfif if
418197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
419197139Shrs#	Stateless Address Configuration, 1 otherwise.
420197139Shrsipv6_autoconfif()
421197139Shrs{
422197139Shrs	local _if _tmpargs _arg
423197139Shrs	_if=$1
424197139Shrs
425197139Shrs	if ! ipv6if $_if; then
426197139Shrs		return 1
427197139Shrs	fi
428197139Shrs	if noafif $_if; then
429197139Shrs		return 1
430197139Shrs	fi
431197139Shrs	if checkyesno ipv6_gateway_enable; then
432197139Shrs		return 1
433197139Shrs	fi
434197139Shrs
435197139Shrs	case $_if in
436197139Shrs	lo0|\
437197139Shrs	stf[0-9]*|\
438197139Shrs	faith[0-9]*|\
439197139Shrs	lp[0-9]*|\
440197139Shrs	sl[0-9]*)
441197139Shrs		return 1
442197139Shrs		;;
443197139Shrs	esac
444197139Shrs
445197139Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
446197139Shrs	for _arg in $_tmpargs; do
447197139Shrs		case $_arg in
448197139Shrs		accept_rtadv)
449197139Shrs			return 0
450197139Shrs			;;
451197139Shrs		esac
452197139Shrs	done
453197139Shrs
454197139Shrs	return 1
455197139Shrs}
456197139Shrs
457161386Sbrooks# ifexists if
458161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
459161386Sbrooksifexists()
460161386Sbrooks{
461197139Shrs	[ -z "$1" ] && return 1
462169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
463161386Sbrooks}
464161386Sbrooks
465152441Sbrooks# ipv4_up if
466152441Sbrooks#  add IPv4 addresses to the interface $if 
467152441Sbrooksipv4_up()
468152441Sbrooks{
469197139Shrs	local _if _ret
470152441Sbrooks	_if=$1
471197139Shrs	_ret=1
472197139Shrs
473197139Shrs	ifalias_up ${_if} inet && _ret=0
474197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
475197139Shrs
476197139Shrs	return $_ret
477152441Sbrooks}
478152441Sbrooks
479197139Shrs# ipv6_up if
480197139Shrs#	add IPv6 addresses to the interface $if
481197139Shrsipv6_up()
482197139Shrs{
483197139Shrs	local _if _ret
484197139Shrs	_if=$1
485197139Shrs	_ret=1
486197139Shrs
487197139Shrs	if ! ipv6if $_if; then
488197139Shrs		return 0
489197139Shrs	fi
490197139Shrs
491197139Shrs	ifalias_up ${_if} inet6 && _ret=0
492197139Shrs	ipv6_prefix_hostid_addr_up ${_if} && _ret=0
493197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
494197139Shrs
495197139Shrs	# wait for DAD
496197139Shrs	sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
497197139Shrs	sleep 1
498197139Shrs
499197139Shrs	return $_ret
500197139Shrs}
501197139Shrs
502152441Sbrooks# ipv4_down if
503152441Sbrooks#  remove IPv4 addresses from the interface $if
504152441Sbrooksipv4_down()
505152441Sbrooks{
506197139Shrs	local _if _ifs _ret inetList oldifs _inet
507152441Sbrooks	_if=$1
508161386Sbrooks	_ifs="^"
509161386Sbrooks	_ret=1
510161386Sbrooks
511161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
512161386Sbrooks
513161386Sbrooks	oldifs="$IFS"
514161386Sbrooks	IFS="$_ifs"
515161386Sbrooks	for _inet in $inetList ; do
516161386Sbrooks		# get rid of extraneous line
517161386Sbrooks		[ -z "$_inet" ] && break
518161386Sbrooks
519161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
520161386Sbrooks
521161386Sbrooks		IFS="$oldifs"
522161386Sbrooks		ifconfig ${_if} ${_inet} delete
523161386Sbrooks		IFS="$_ifs"
524161386Sbrooks		_ret=0
525161386Sbrooks	done
526161386Sbrooks	IFS="$oldifs"
527161386Sbrooks
528197139Shrs	ifalias_down ${_if} inet && _ret=0
529161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
530161386Sbrooks
531161386Sbrooks	return $_ret
532152441Sbrooks}
533152441Sbrooks
534197139Shrs# ipv6_down if
535197139Shrs#	remove IPv6 addresses from the interface $if
536197139Shrsipv6_down()
537197139Shrs{
538197139Shrs	local _if _ifs _ret inetList oldifs _inet6
539197139Shrs	_if=$1
540197139Shrs	_ifs="^"
541197139Shrs	_ret=1
542197139Shrs
543197139Shrs	if ! ipv6if $_if; then
544197139Shrs		return 0
545197139Shrs	fi
546197139Shrs
547197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
548197139Shrs	ifalias_down ${_if} inet6 && _ret=0
549197139Shrs
550197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
551197139Shrs
552197139Shrs	oldifs="$IFS"
553197139Shrs	IFS="$_ifs"
554197139Shrs	for _inet6 in $inetList ; do
555197139Shrs		# get rid of extraneous line
556197139Shrs		[ -z "$_inet6" ] && break
557197139Shrs
558197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
559197139Shrs
560197139Shrs		IFS="$oldifs"
561197139Shrs		ifconfig ${_if} ${_inet6} -alias
562197139Shrs		IFS="$_ifs"
563197139Shrs		_ret=0
564197139Shrs	done
565197139Shrs	IFS="$oldifs"
566197139Shrs
567197139Shrs	return $_ret
568197139Shrs}
569197139Shrs
570152441Sbrooks# ipv4_addrs_common if action
571152441Sbrooks#   Evaluate the ifconfig_if_ipv4 arguments for interface $if
572152441Sbrooks#   and use $action to add or remove IPv4 addresses from $if.
573152441Sbrooksipv4_addrs_common()
574152441Sbrooks{  
575197139Shrs	local _ret _if _action _cidr _cidr_addr
576197139Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount 
577152441Sbrooks	_ret=1
578152441Sbrooks	_if=$1
579152441Sbrooks	_action=$2
580152441Sbrooks    
581152441Sbrooks	# get ipv4-addresses
582157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
583152441Sbrooks    
584152441Sbrooks	for _cidr in ${cidr_addr}; do
585152441Sbrooks		_ipaddr=${_cidr%%/*}
586152441Sbrooks		_netmask="/"${_cidr##*/}
587152441Sbrooks		_range=${_ipaddr##*.}
588152441Sbrooks		_ipnet=${_ipaddr%.*}
589152441Sbrooks		_iplow=${_range%-*}
590152441Sbrooks		_iphigh=${_range#*-}
591152441Sbrooks
592152441Sbrooks		# clear netmask when removing aliases
593152441Sbrooks		if [ "${_action}" = "-alias" ]; then
594152441Sbrooks			_netmask=""
595152441Sbrooks		fi
596152441Sbrooks        
597152441Sbrooks		_ipcount=${_iplow}
598152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
599152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
600152441Sbrooks			_ipcount=$((${_ipcount}+1))
601152441Sbrooks			_ret=0
602152441Sbrooks
603152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
604152441Sbrooks			if [ "${_action}" != "-alias" ]; then
605152441Sbrooks				_netmask="/32"
606152441Sbrooks			fi
607152441Sbrooks		done
608152441Sbrooks	done
609197139Shrs
610152441Sbrooks	return $_ret
611152441Sbrooks}
612152441Sbrooks
613197139Shrs# ifalias_up if af
614113674Smtm#	Configure aliases for network interface $if.
615113674Smtm#	It returns 0 if at least one alias was configured or
616113674Smtm#	1 if there were none.
617113674Smtm#
618113674Smtmifalias_up()
619113674Smtm{
620197139Shrs	local _ret
621113674Smtm	_ret=1
622197139Shrs
623197139Shrs	case "$2" in
624197139Shrs	inet)
625197139Shrs		_ret=`ifalias_ipv4_up "$1"`
626197139Shrs		;;
627197139Shrs	inet6)
628197139Shrs		_ret=`ifalias_ipv6_up "$1"`
629197139Shrs		;;
630197139Shrs	esac
631197139Shrs
632197139Shrs	return $_ret
633197139Shrs}
634197139Shrs
635197139Shrs# ifalias_ipv4_up if
636197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
637197139Shrs#
638197139Shrsifalias_ipv4_up()
639197139Shrs{
640197139Shrs	local _ret alias ifconfig_args
641197139Shrs	_ret=1
642197139Shrs
643197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
644113674Smtm	alias=0
645113674Smtm	while : ; do
646157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
647197139Shrs		case "${ifconfig_args}" in
648197139Shrs		inet\ *)
649197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
650197139Shrs			;;
651197139Shrs		"")
652197139Shrs			break
653197139Shrs			;;
654197139Shrs		esac
655113674Smtm			alias=$((${alias} + 1))
656197139Shrs	done
657197139Shrs
658197139Shrs	return $_ret
659197139Shrs}
660197139Shrs
661197139Shrs# ifalias_ipv6_up if
662197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
663197139Shrs#
664197139Shrsifalias_ipv6_up()
665197139Shrs{
666197139Shrs	local _ret alias ifconfig_args
667197139Shrs	_ret=1
668197139Shrs
669197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
670197139Shrs	alias=0
671197139Shrs	while : ; do
672197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
673197139Shrs		case "${ifconfig_args}" in
674197139Shrs		inet6\ *)
675197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
676197139Shrs			;;
677197139Shrs		"")
678113674Smtm			break
679197139Shrs			;;
680197139Shrs		esac
681197139Shrs		alias=$((${alias} + 1))
682113674Smtm	done
683197139Shrs
684197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
685197139Shrs	alias=0
686197139Shrs	while : ; do
687197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
688197139Shrs		case "${ifconfig_args}" in
689197139Shrs		"")
690197139Shrs			break
691197139Shrs			;;
692197139Shrs		*)
693197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
694197139Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete."
695197139Shrs			    "  Use ifconfig_$1_aliasN instead."
696197139Shrs			;;
697197139Shrs		esac
698197139Shrs		alias=$((${alias} + 1))
699197139Shrs	done
700197139Shrs
701113674Smtm	return $_ret
702113674Smtm}
703100280Sgordon
704197139Shrs#ifalias_down if af
705116029Smtm#	Remove aliases for network interface $if.
706116029Smtm#	It returns 0 if at least one alias was removed or
707116029Smtm#	1 if there were none.
708116029Smtm#
709116029Smtmifalias_down()
710116029Smtm{
711197139Shrs	local _ret
712116029Smtm	_ret=1
713197139Shrs
714197139Shrs	case "$2" in
715197139Shrs	inet)
716197139Shrs		_ret=`ifalias_ipv4_down "$1"`
717197139Shrs		;;
718197139Shrs	inet6)
719197139Shrs		_ret=`ifalias_ipv6_down "$1"`
720197139Shrs		;;
721197139Shrs	esac
722197139Shrs
723197139Shrs	return $_ret
724197139Shrs}
725197139Shrs
726197139Shrs#ifalias_ipv4_down if
727197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
728197139Shrs#
729197139Shrsifalias_ipv4_down()
730197139Shrs{
731197139Shrs	local _ret alias ifconfig_args
732197139Shrs	_ret=1
733197139Shrs
734197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
735116029Smtm	alias=0
736116029Smtm	while : ; do
737157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
738197139Shrs		case "${ifconfig_args}" in
739197139Shrs		inet\ *)
740197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
741197139Shrs			;;
742197139Shrs		"")
743197139Shrs			break
744197139Shrs			;;
745197139Shrs		esac
746116029Smtm			alias=$((${alias} + 1))
747197139Shrs	done
748197139Shrs
749197139Shrs	return $_ret
750197139Shrs}
751197139Shrs
752197139Shrs#ifalias_ipv6_down if
753197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
754197139Shrs#
755197139Shrsifalias_ipv6_down()
756197139Shrs{
757197139Shrs	local _ret alias ifconfig_args
758197139Shrs	_ret=1
759197139Shrs
760197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
761197139Shrs	alias=0
762197139Shrs	while : ; do
763197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
764197139Shrs		case "${ifconfig_args}" in
765197139Shrs		inet6\ *)
766197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
767197139Shrs			;;
768197139Shrs		"")
769116029Smtm			break
770197139Shrs			;;
771197139Shrs		esac
772197139Shrs		alias=$((${alias} + 1))
773116029Smtm	done
774197139Shrs
775197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
776197139Shrs	while : ; do
777197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
778197139Shrs		case "${ifconfig_args}" in
779197139Shrs		"")
780197139Shrs			break
781197139Shrs			;;
782197139Shrs		*)
783197139Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias
784197139Shrs			alias=$((${alias} + 1))
785197139Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete."
786197139Shrs			    "  Use ifconfig_$1_aliasN instead."
787197139Shrs			_ret=0
788197139Shrs			;;
789197139Shrs		esac
790197139Shrs	done
791197139Shrs
792116029Smtm	return $_ret
793116029Smtm}
794116029Smtm
795197139Shrs# ipv6_prefix_hostid_addr_up if
796197139Shrs#	add IPv6 prefix + hostid addr to the interface $if
797197139Shrsipv6_prefix_hostid_addr_up()
798197139Shrs{
799197139Shrs	local _if prefix laddr hostid j address
800197139Shrs	_if=$1
801197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
802197139Shrs
803197139Shrs	if [ -n "${prefix}" ]; then
804197139Shrs		laddr=`network6_getladdr ${_if}`
805197139Shrs		hostid=${laddr#fe80::}
806197139Shrs		hostid=${hostid%\%*}
807197139Shrs
808197139Shrs		for j in ${prefix}; do
809197139Shrs			address=$j\:${hostid}
810197139Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 alias
811197139Shrs
812197139Shrs			# if I am a router, add subnet router
813197139Shrs			# anycast address (RFC 2373).
814197139Shrs			if checkyesno ipv6_gateway_enable; then
815197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
816197139Shrs					alias anycast
817197139Shrs			fi
818197139Shrs		done
819197139Shrs	fi
820197139Shrs}
821197139Shrs
822197139Shrs# ipv6_accept_rtadv_up if
823197139Shrs#	Enable accepting Router Advertisement and send Router
824197139Shrs#	Solicitation message
825197139Shrsipv6_accept_rtadv_up()
826197139Shrs{
827197139Shrs	if ipv6_autoconfif $1; then
828197139Shrs		ifconfig $1 inet6 accept_rtadv up
829197139Shrs		rtsol ${rtsol_flags} $1
830197139Shrs	fi
831197139Shrs}
832197139Shrs
833197139Shrs# ipv6_accept_rtadv_down if
834197139Shrs#	Disable accepting Router Advertisement
835197139Shrsipv6_accept_rtadv_down()
836197139Shrs{
837197139Shrs	if ipv6_autoconfif $1; then
838197139Shrs		ifconfig $1 inet6 -accept_rtadv
839197139Shrs	fi
840197139Shrs}
841197139Shrs
842113674Smtm# ifscript_up if
843113674Smtm#	Evaluate a startup script for the $if interface.
844113674Smtm#	It returns 0 if a script was found and processed or
845113674Smtm#	1 if no script was found.
846113674Smtm#
847113674Smtmifscript_up()
848100280Sgordon{
849113674Smtm	if [ -r /etc/start_if.$1 ]; then
850113674Smtm		. /etc/start_if.$1
851113674Smtm		return 0
852197139Shrs	else
853197139Shrs		return 1
854113674Smtm	fi
855100280Sgordon}
856100280Sgordon
857116029Smtm# ifscript_down if
858116029Smtm#	Evaluate a shutdown script for the $if interface.
859116029Smtm#	It returns 0 if a script was found and processed or
860116029Smtm#	1 if no script was found.
861116029Smtm#
862116029Smtmifscript_down()
863116029Smtm{
864116029Smtm	if [ -r /etc/stop_if.$1 ]; then
865116029Smtm		. /etc/stop_if.$1
866116029Smtm		return 0
867197139Shrs	else
868197139Shrs		return 1
869116029Smtm	fi
870116029Smtm}
871116029Smtm
872113674Smtm# Create cloneable interfaces.
873113674Smtm#
874113674Smtmclone_up()
875100280Sgordon{
876197139Shrs	local _prefix _list ifn
877113674Smtm	_prefix=
878113674Smtm	_list=
879197139Shrs
880197139Shrs	# create_args_IF
881113674Smtm	for ifn in ${cloned_interfaces}; do
882178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
883116774Skuriyama		if [ $? -eq 0 ]; then
884113674Smtm			_list="${_list}${_prefix}${ifn}"
885113674Smtm			[ -z "$_prefix" ] && _prefix=' '
886113674Smtm		fi
887113674Smtm	done
888113674Smtm	debug "Cloned: ${_list}"
889113674Smtm}
890100280Sgordon
891113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed
892113674Smtm# to standard output.
893113674Smtm#
894113674Smtmclone_down()
895113674Smtm{
896197139Shrs	local _prefix _list ifn
897113674Smtm	_prefix=
898113674Smtm	_list=
899197139Shrs
900113674Smtm	for ifn in ${cloned_interfaces}; do
901113674Smtm		ifconfig ${ifn} destroy
902116774Skuriyama		if [ $? -eq 0 ]; then
903113674Smtm			_list="${_list}${_prefix}${ifn}"
904113674Smtm			[ -z "$_prefix" ] && _prefix=' '
905113674Smtm		fi
906113674Smtm	done
907113674Smtm	debug "Destroyed clones: ${_list}"
908100280Sgordon}
909100280Sgordon
910178356Ssam# Create and configure child interfaces.
911178356Ssam# Return 0 if child interfaces are created.
912178356Ssam#
913178356Ssamchildif_create()
914178356Ssam{
915189759Sbrooks	local cfg child child_wlans create_args debug_flags ifn i
916178356Ssam	cfg=1
917178356Ssam	ifn=$1
918178356Ssam
919178527Sbrooks	# Create wireless interfaces
920178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
921178527Sbrooks
922178527Sbrooks	for child in ${child_wlans}; do
923183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
924189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
925189759Sbrooks
926178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
927178356Ssam			ifconfig $child create ${create_args} && cfg=0
928189759Sbrooks			if [ -n "${debug_flags}" ]; then
929189759Sbrooks				wlandebug -i $child ${debug_flags}
930189759Sbrooks			fi
931178356Ssam		else
932178356Ssam			i=`ifconfig wlan create ${create_args}`
933189759Sbrooks			if [ -n "${debug_flags}" ]; then
934189759Sbrooks				wlandebug -i $i ${debug_flags}
935189759Sbrooks			fi
936178356Ssam			ifconfig $i name $child && cfg=0
937178356Ssam		fi
938188118Sthompsa		if autoif $child; then
939188118Sthompsa			ifn_start $child
940188118Sthompsa		fi
941178356Ssam	done
942178356Ssam
943179001Sbrooks	return ${cfg}
944178356Ssam}
945178356Ssam
946178356Ssam# Destroy child interfaces.
947178356Ssam#
948178356Ssamchildif_destroy()
949178356Ssam{
950178527Sbrooks	local cfg child child_wlans ifn
951197139Shrs	cfg=1
952178356Ssam
953178527Sbrooks	child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
954178527Sbrooks	for child in ${child_wlans}; do
955178356Ssam		ifconfig $child destroy && cfg=0
956178356Ssam	done
957197139Shrs
958197139Shrs	return ${cfg}
959178356Ssam}
960178356Ssam
961166583Sflz# Create netgraph nodes.
962166583Sflz#
963166583Sflzng_mkpeer() {
964166583Sflz	ngctl -f - 2> /dev/null <<EOF
965166583Sflzmkpeer $*
966166583Sflzmsg dummy nodeinfo
967166583SflzEOF
968166583Sflz}
969166583Sflz
970166583Sflzng_create_one() {
971197139Shrs	local t
972197139Shrs
973166583Sflz	ng_mkpeer $* | while read line; do
974166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
975166583Sflz		if [ -n "${t}" ]; then
976166583Sflz			echo ${t}
977166583Sflz			return
978166583Sflz		fi
979166583Sflz	done
980166583Sflz}
981166583Sflz
982113674Smtmgif_up() {
983197139Shrs	local i peers
984197139Shrs
985166583Sflz	for i in ${gif_interfaces}; do
986166583Sflz		peers=`get_if_var $i gifconfig_IF`
987166583Sflz		case ${peers} in
988166583Sflz		'')
989166583Sflz			continue
990166583Sflz			;;
991166583Sflz		*)
992177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
993177682Sbrooks				ifconfig $i create >/dev/null 2>&1
994177682Sbrooks			else
995177682Sbrooks				gif=`ifconfig gif create`
996177682Sbrooks				ifconfig $gif name $i
997177682Sbrooks			fi
998166583Sflz			ifconfig $i tunnel ${peers}
999166583Sflz			ifconfig $i up
1000166583Sflz			;;
1001166583Sflz		esac
1002166583Sflz	done
1003166583Sflz}
1004166583Sflz
1005166583Sflz# ng_fec_create ifn
1006166583Sflz# Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
1007166583Sflz# arguments were found and configured; returns !0 otherwise.
1008197139Shrsng_fec_create()
1009197139Shrs{
1010166583Sflz	 local req_iface iface bogus
1011166583Sflz	 req_iface="$1"
1012166583Sflz
1013166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1014166583Sflz
1015166583Sflz	 bogus=""
1016166583Sflz	 while true; do
1017166583Sflz		 iface=`ng_create_one fec dummy fec`
1018166583Sflz		 if [ -z "${iface}" ]; then
1019166583Sflz			 exit 2
1020166583Sflz		 fi
1021166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1022166583Sflz			 break
1023166583Sflz		 fi
1024166583Sflz		 bogus="${bogus} ${iface}"
1025166583Sflz	 done
1026166583Sflz
1027166583Sflz	 for iface in ${bogus}; do
1028166583Sflz		 ngctl shutdown ${iface}:
1029166583Sflz	 done
1030166583Sflz}
1031166583Sflz
1032166583Sflzfec_up() {
1033197139Shrs	local i j
1034197139Shrs
1035166583Sflz	for i in ${fec_interfaces}; do
1036166583Sflz		ng_fec_create $i
1037166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1038166583Sflz			case ${j} in
1039100282Sdougb			'')
1040100282Sdougb				continue
1041100282Sdougb				;;
1042100282Sdougb			*)
1043166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1044100282Sdougb				;;
1045100282Sdougb			esac
1046100282Sdougb		done
1047166583Sflz	done
1048100282Sdougb}
1049100282Sdougb
1050113674Smtm#
1051113674Smtm# ipx_up ifn
1052113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
1053113674Smtm# arguments were found and configured; returns 1 otherwise.
1054113674Smtm#
1055113674Smtmipx_up()
1056100280Sgordon{
1057197139Shrs	local ifn
1058113674Smtm	ifn="$1"
1059197139Shrs
1060197139Shrs	# ifconfig_IF_ipx
1061197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1062113674Smtm	if [ -n "${ifconfig_args}" ]; then
1063113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1064113674Smtm		return 0
106585831Sdes	fi
1066197139Shrs
1067113674Smtm	return 1
1068113674Smtm}
106985831Sdes
1070116029Smtm# ipx_down ifn
1071116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1072116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1073113674Smtm#
1074116029Smtmipx_down()
1075116029Smtm{
1076197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1077197139Shrs	_if=$1
1078116100Smtm	_ifs="^"
1079116100Smtm	_ret=1
1080197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1081197139Shrs	oldifs="$IFS"
1082116100Smtm
1083116100Smtm	IFS="$_ifs"
1084116100Smtm	for _ipx in $ipxList ; do
1085116100Smtm		# get rid of extraneous line
1086116100Smtm		[ -z "$_ipx" ] && break
1087116100Smtm
1088116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1089116100Smtm
1090116100Smtm		IFS="$oldifs"
1091197139Shrs		ifconfig ${_if} ${_ipx} delete
1092116100Smtm		IFS="$_ifs"
1093116100Smtm		_ret=0
1094116100Smtm	done
1095116100Smtm	IFS="$oldifs"
1096116100Smtm
1097116100Smtm	return $_ret
1098116029Smtm}
1099116029Smtm
1100137070Spjd# ifnet_rename
1101137070Spjd#	Rename all requested interfaces.
1102116029Smtm#
1103137070Spjdifnet_rename()
1104137070Spjd{
1105197139Shrs	local _if _ifname
1106137070Spjd
1107197139Shrs	# ifconfig_IF_name
1108197139Shrs	for _if in `ifconfig -l`; do
1109157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1110137070Spjd		if [ ! -z "$_ifname" ]; then
1111137070Spjd			ifconfig $_if name $_ifname
1112137070Spjd		fi
1113137070Spjd	done
1114197139Shrs
1115137070Spjd	return 0
1116137070Spjd}
1117137070Spjd
1118113674Smtm# list_net_interfaces type
1119113674Smtm#	List all network interfaces. The type of interface returned
1120113674Smtm#	can be controlled by the type argument. The type
1121113674Smtm#	argument can be any of the following:
1122113674Smtm#		nodhcp - all interfaces, excluding DHCP configured interfaces
1123113674Smtm#		dhcp   - list only DHCP configured interfaces
1124197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1125197139Shrs#				  Address Autoconf configured interfaces
1126197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1127197139Shrs#				  configured interfaces
1128113674Smtm#	If no argument is specified all network interfaces are output.
1129134429Syar#	Note that the list will include cloned interfaces if applicable.
1130134429Syar#	Cloned interfaces must already exist to have a chance to appear
1131134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1132113674Smtm#
1133113674Smtmlist_net_interfaces()
1134113674Smtm{
1135197139Shrs	local type _tmplist _list _autolist _lo _if
1136113674Smtm	type=$1
113765532Snectar
1138149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
113951231Ssheldonh	#
1140197139Shrs	_tmplist=
114151231Ssheldonh	case ${network_interfaces} in
114251231Ssheldonh	[Aa][Uu][Tt][Oo])
1143149401Sbrooks		_autolist="`ifconfig -l`"
1144149726Sbrooks		_lo=
1145149401Sbrooks		for _if in ${_autolist} ; do
1146149401Sbrooks			if autoif $_if; then
1147149726Sbrooks				if [ "$_if" = "lo0" ]; then
1148149726Sbrooks					_lo="lo0 "
1149149726Sbrooks				else
1150197139Shrs					_tmplist="${_tmplist} ${_if}"
1151149726Sbrooks				fi
1152149401Sbrooks			fi
1153149401Sbrooks		done
1154197139Shrs		_tmplist="${_lo}${_tmplist# }"
115551231Ssheldonh		;;
115683677Sbrooks	*)
1157149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1158196478Sdougb
1159196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1160196478Sdougb		#
1161196478Sdougb		case "$_tmplist" in
1162196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1163196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1164196478Sdougb		esac
116583677Sbrooks		;;
116651231Ssheldonh	esac
116749122Sbrian
1168197139Shrs	_list=
1169197139Shrs	case "$type" in
1170197139Shrs	nodhcp)
1171197139Shrs		for _if in ${_tmplist} ; do
1172197139Shrs			if ! dhcpif $_if && \
1173197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1174197139Shrs				_list="${_list# } ${_if}"
1175113674Smtm	fi
1176197139Shrs		done
1177197139Shrs		;;
1178197139Shrs	dhcp)
1179113674Smtm	for _if in ${_tmplist} ; do
1180147684Sbrooks		if dhcpif $_if; then
1181197139Shrs				_list="${_list# } ${_if}"
1182147684Sbrooks		fi
118354458Sobrien	done
1184113674Smtm		;;
1185197139Shrs	noautoconf)
1186197139Shrs		for _if in ${_tmplist} ; do
1187197139Shrs			if ! ipv6_autoconfif $_if && \
1188197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1189197139Shrs				_list="${_list# } ${_if}"
1190197139Shrs			fi
1191197139Shrs		done
1192113674Smtm		;;
1193197139Shrs	autoconf)
1194197139Shrs		for _if in ${_tmplist} ; do
1195197139Shrs			if ipv6_autoconfif $_if; then
1196197139Shrs				_list="${_list# } ${_if}"
1197197139Shrs			fi
1198197139Shrs		done
1199197139Shrs		;;
1200197139Shrs	*)
1201197139Shrs		_list=${_tmplist}
1202197139Shrs		;;
1203113674Smtm	esac
1204197139Shrs
1205197139Shrs	echo $_list
1206197139Shrs
1207130151Sschweikh	return 0
120825184Sjkh}
1209114942Sume
1210179003Sbrooks# get_default_if -address_family
1211179003Sbrooks#	Get the interface of the default route for the given address family.
1212179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1213179003Sbrooks#
1214179003Sbrooksget_default_if()
1215179003Sbrooks{
1216197139Shrs	local routeget oldifs defif line
1217197139Shrs	defif=
1218179003Sbrooks	oldifs="$IFS"
1219179003Sbrooks	IFS="
1220179003Sbrooks"
1221197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1222179003Sbrooks		case $line in
1223179003Sbrooks		*interface:*)
1224179003Sbrooks			defif=${line##*: }
1225179003Sbrooks			;;
1226179003Sbrooks		esac
1227179003Sbrooks	done
1228179003Sbrooks	IFS=${oldifs}
1229179003Sbrooks
1230179003Sbrooks	echo $defif
1231179003Sbrooks}
1232179003Sbrooks
1233114942Sumehexdigit()
1234114942Sume{
1235114942Sume	if [ $1 -lt 10 ]; then
1236114942Sume		echo $1
1237114942Sume	else
1238114942Sume		case $1 in
1239114942Sume		10)	echo a ;;
1240114942Sume		11)	echo b ;;
1241114942Sume		12)	echo c ;;
1242114942Sume		13)	echo d ;;
1243114942Sume		14)	echo e ;;
1244114942Sume		15)	echo f ;;
1245114942Sume		esac
1246114942Sume	fi
1247114942Sume}
1248114942Sume
1249114942Sumehexprint()
1250114942Sume{
1251197139Shrs	local val str dig
1252114942Sume	val=$1
1253114942Sume	str=''
1254114942Sume	dig=`hexdigit $((${val} & 15))`
1255114942Sume	str=${dig}${str}
1256114942Sume	val=$((${val} >> 4))
1257197139Shrs
1258114942Sume	while [ ${val} -gt 0 ]; do
1259114942Sume		dig=`hexdigit $((${val} & 15))`
1260114942Sume		str=${dig}${str}
1261114942Sume		val=$((${val} >> 4))
1262114942Sume	done
1263114942Sume
1264114942Sume	echo ${str}
1265114942Sume}
1266114942Sume
1267196436Sdougbis_wired_interface()
1268196436Sdougb{
1269196436Sdougb	local media
1270196436Sdougb
1271196436Sdougb	case `ifconfig $1 2>/dev/null` in
1272196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1273196436Sdougb	esac
1274196436Sdougb
1275196436Sdougb	test "$media" = "Ethernet"
1276196436Sdougb}
1277196436Sdougb
1278114942Sumenetwork6_getladdr()
1279114942Sume{
1280197139Shrs	local proto addr rest
1281114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1282114942Sume		case ${proto} in
1283114942Sume		inet6)
1284114942Sume			case ${addr} in
1285114942Sume			fe80::*)
1286114942Sume				if [ -z "$2" ]; then
1287114942Sume					echo ${addr}
1288114942Sume					return
1289114942Sume				fi
1290114942Sume				case ${rest} in
1291114942Sume				*tentative*)
1292114942Sume					continue
1293114942Sume					;;
1294114942Sume				*)
1295114942Sume					echo ${addr}
1296114942Sume					return
1297114942Sume				esac
1298114942Sume			esac
1299114942Sume		esac
1300114942Sume	done
1301114942Sume}
1302