network.subr revision 201215
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 201215 2009-12-29 21:03:36Z jhb $
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
34197147Shrs#	Bring up and configure an interface.  If some configuration is
35197147Shrs#	applied 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
56197147Shrs#	Shutdown and de-configure an interface.  If action is taken
57197147Shrs#	print the 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
100197526Shrs				_ipv6_opts="-accept_rtadv"
101197526Shrs			fi
102197526Shrs		else
103197526Shrs			if checkyesno ipv6_prefer; then
104197526Shrs				_ipv6_opts="-ifdisabled"
105197139Shrs			else
106197526Shrs				_ipv6_opts="ifdisabled"
107197139Shrs			fi
108197526Shrs
109197526Shrs			# backward compatibility: $ipv6_enable
110197526Shrs			case $ipv6_enable in
111197526Shrs			[Yy][Ee][Ss])
112197526Shrs				_ipv6_opts="${_ipv6_opts} accept_rtadv"
113197526Shrs				;;
114197526Shrs			esac
115197139Shrs		fi
116197139Shrs
117197526Shrs		if [ -n "${_ipv6_opts}" ]; then
118197526Shrs			ifconfig $1 inet6 ${_ipv6_opts}
119197526Shrs		fi
120197139Shrs
121197139Shrs		# ifconfig_IF_ipv6
122197139Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
123197139Shrs		if [ -n "${ifconfig_args}" ]; then
124197139Shrs			ifconfig $1 inet6 -ifdisabled
125197139Shrs			ifconfig $1 ${ifconfig_args}
126197139Shrs			_cfg=0
127197139Shrs		fi
128197139Shrs
129197139Shrs		# backward compatiblity: $ipv6_ifconfig_IF
130197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
131197139Shrs		if [ -n "${ifconfig_args}" ]; then
132197139Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
133197139Shrs			    "  Use ifconfig_$1_ipv6 instead."
134197139Shrs			ifconfig $1 inet6 -ifdisabled
135197139Shrs			ifconfig $1 inet6 ${ifconfig_args}
136197139Shrs			_cfg=0
137197139Shrs		fi
138197139Shrs	fi
139197139Shrs
140197139Shrs	if [ ${_cfg} -eq 0 ]; then
141197139Shrs		ifconfig $1 up
142197139Shrs	fi
143197139Shrs
144147088Sbrooks	if wpaif $1; then
145147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
146147088Sbrooks		_cfg=0		# XXX: not sure this should count
147147088Sbrooks	fi
148147088Sbrooks
149147088Sbrooks	if dhcpif $1; then
150149726Sbrooks		if [ $_cfg -ne 0 ] ; then
151149726Sbrooks			ifconfig $1 up
152149726Sbrooks		fi
153157706Sbrooks		if syncdhcpif $1; then
154157706Sbrooks			/etc/rc.d/dhclient start $1
155157706Sbrooks		fi
156147088Sbrooks		_cfg=0
157147088Sbrooks	fi
158147088Sbrooks
159147121Sbrooks	return $_cfg
160113674Smtm}
16125184Sjkh
162116029Smtm# ifconfig_down if
163161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
164161386Sbrooks#	the interface exists.
165116029Smtm#
166116029Smtmifconfig_down()
167116029Smtm{
168197139Shrs	local _cfg
169147121Sbrooks	_cfg=1
170116029Smtm
171147088Sbrooks	if wpaif $1; then
172147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
173147121Sbrooks		_cfg=0
174147088Sbrooks	fi
175147088Sbrooks
176147088Sbrooks	if dhcpif $1; then
177147088Sbrooks		/etc/rc.d/dhclient stop $1
178147088Sbrooks		_cfg=0
179147088Sbrooks	fi
180147088Sbrooks
181161386Sbrooks	if ifexists $1; then
182161386Sbrooks		ifconfig $1 down
183161386Sbrooks		_cfg=0
184161386Sbrooks	fi
185157706Sbrooks
186147121Sbrooks	return $_cfg
187116029Smtm}
188116029Smtm
189157706Sbrooks# get_if_var if var [default]
190197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
191197147Shrs#	$var is a string containg the sub-string "IF" which will be
192197147Shrs#	replaced with $if after the characters defined in _punct are
193197147Shrs#	replaced with '_'. If the variable is unset, replace it with
194197147Shrs#	$default if given.
195157706Sbrooksget_if_var()
196157706Sbrooks{
197197139Shrs	local _if _punct _var _default prefix suffix
198197139Shrs
199157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
200157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
201157706Sbrooks	fi
202157706Sbrooks
203157706Sbrooks	_if=$1
204157706Sbrooks	_punct=". - / +"
205157736Sbrooks	for _punct_c in $_punct; do
206157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
207157706Sbrooks	done
208157706Sbrooks	_var=$2
209157706Sbrooks	_default=$3
210157706Sbrooks
211157706Sbrooks	prefix=${_var%%IF*}
212157706Sbrooks	suffix=${_var##*IF}
213168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
214157706Sbrooks}
215157706Sbrooks
216197139Shrs# _ifconfig_getargs if [af]
217147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
218147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
219147088Sbrooks#	outside this file.
220147088Sbrooks_ifconfig_getargs()
221147088Sbrooks{
222197139Shrs	local _ifn _af
223147088Sbrooks	_ifn=$1
224197139Shrs	_af=${2+_$2}
225197139Shrs
226147088Sbrooks	if [ -z "$_ifn" ]; then
227147088Sbrooks		return 1
228147088Sbrooks	fi
229147088Sbrooks
230197139Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
231147088Sbrooks}
232147088Sbrooks
233197139Shrs# ifconfig_getargs if [af]
234147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
235147088Sbrooks#	args such as DHCP and WPA.
236147088Sbrooksifconfig_getargs()
237147088Sbrooks{
238197139Shrs	local _tmpargs _arg _args
239197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
240147088Sbrooks	if [ $? -eq 1 ]; then
241147088Sbrooks		return 1
242147088Sbrooks	fi
243147088Sbrooks	_args=
244147088Sbrooks
245147088Sbrooks	for _arg in $_tmpargs; do
246147088Sbrooks		case $_arg in
247157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
248157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
249157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
250157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
251157706Sbrooks		[Ww][Pp][Aa]) ;;
252147088Sbrooks		*)
253147088Sbrooks			_args="$_args $_arg"
254147088Sbrooks			;;
255147088Sbrooks		esac
256147088Sbrooks	done
257147088Sbrooks
258147088Sbrooks	echo $_args
259147088Sbrooks}
260147088Sbrooks
261149401Sbrooks# autoif
262149401Sbrooks#	Returns 0 if the interface should be automaticly configured at
263149401Sbrooks#	boot time and 1 otherwise.
264149401Sbrooksautoif()
265149401Sbrooks{
266197139Shrs	local _tmpargs _arg
267149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
268197139Shrs
269149401Sbrooks	for _arg in $_tmpargs; do
270149401Sbrooks		case $_arg in
271149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
272149401Sbrooks			return 1
273149401Sbrooks			;;
274149401Sbrooks		esac
275149401Sbrooks	done
276197139Shrs
277149401Sbrooks	return 0
278149401Sbrooks}
279149401Sbrooks
280147088Sbrooks# dhcpif if
281147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
282147088Sbrooksdhcpif()
283147088Sbrooks{
284197139Shrs	local _tmpargs _arg
285147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
286197139Shrs
287147088Sbrooks	for _arg in $_tmpargs; do
288147088Sbrooks		case $_arg in
289147088Sbrooks		[Dd][Hh][Cc][Pp])
290147088Sbrooks			return 0
291147088Sbrooks			;;
292157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
293157706Sbrooks			return 0
294157706Sbrooks			;;
295157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
296157706Sbrooks			return 0
297157706Sbrooks			;;
298147088Sbrooks		esac
299147088Sbrooks	done
300197139Shrs
301147088Sbrooks	return 1
302147088Sbrooks}
303147088Sbrooks
304157706Sbrooks# syncdhcpif
305157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
306157706Sbrooks#	1 otherwise.
307157706Sbrookssyncdhcpif()
308157706Sbrooks{
309197139Shrs	local _tmpargs _arg
310157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
311197139Shrs
312157706Sbrooks	for _arg in $_tmpargs; do
313157706Sbrooks		case $_arg in
314157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
315157706Sbrooks			return 1
316157706Sbrooks			;;
317157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
318157706Sbrooks			return 0
319157706Sbrooks			;;
320157706Sbrooks		esac
321157706Sbrooks	done
322197139Shrs
323197139Shrs	checkyesno synchronous_dhclient
324157706Sbrooks}
325157706Sbrooks
326147088Sbrooks# wpaif if
327147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
328147088Sbrookswpaif()
329147088Sbrooks{
330197139Shrs	local _tmpargs _arg
331147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
332197139Shrs
333147088Sbrooks	for _arg in $_tmpargs; do
334147088Sbrooks		case $_arg in
335147088Sbrooks		[Ww][Pp][Aa])
336147088Sbrooks			return 0
337147088Sbrooks			;;
338147088Sbrooks		esac
339147088Sbrooks	done
340197139Shrs
341147088Sbrooks	return 1
342147088Sbrooks}
343147088Sbrooks
344197139Shrs# afexists af
345197139Shrs#	Returns 0 if the address family is enabled in the kernel
346197139Shrs#	1 otherwise.
347197139Shrsafexists()
348197139Shrs{
349197139Shrs	local _af
350197139Shrs	_af=$1
351197139Shrs
352197139Shrs	case ${_af} in
353197139Shrs	inet)
354197139Shrs		${SYSCTL_N} net.inet > /dev/null 2>&1
355197139Shrs		;;
356197139Shrs	inet6)
357197139Shrs		${SYSCTL_N} net.inet6 > /dev/null 2>&1
358197139Shrs		;;
359197697Shrs	ipx)
360197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
361197697Shrs		;;
362197697Shrs	atm)
363197697Shrs		if [ -x /sbin/atmconfig ]; then
364197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
365197697Shrs		else
366197697Shrs			return 1
367197697Shrs		fi
368197697Shrs		;;
369197139Shrs	*)
370197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
371197139Shrs		;;
372197139Shrs	esac
373197139Shrs}
374197139Shrs
375197139Shrs# noafif if
376197139Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
377197139Shrsnoafif()
378197139Shrs{
379197139Shrs	local _if
380197139Shrs	_if=$1
381197139Shrs
382197139Shrs	case $_if in
383197139Shrs	pflog[0-9]*|\
384197139Shrs	pfsync[0-9]*|\
385197139Shrs	an[0-9]*|\
386197139Shrs	ath[0-9]*|\
387197139Shrs	ipw[0-9]*|\
388197139Shrs	iwi[0-9]*|\
389197139Shrs	iwn[0-9]*|\
390197139Shrs	ral[0-9]*|\
391197139Shrs	wi[0-9]*|\
392197139Shrs	wl[0-9]*|\
393197139Shrs	wpi[0-9]*)
394197139Shrs		return 0
395197139Shrs		;;
396197139Shrs	esac
397197139Shrs
398197139Shrs	return 1
399197139Shrs}
400197139Shrs
401162490Sbrooks# ipv6if if
402162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
403162490Sbrooks#	1 otherwise.
404162490Sbrooksipv6if()
405162490Sbrooks{
406197526Shrs	local _if _tmpargs i
407197139Shrs	_if=$1
408197139Shrs
409197139Shrs	if ! afexists inet6; then
410162490Sbrooks		return 1
411162490Sbrooks	fi
412197139Shrs
413197139Shrs	# lo0 is always IPv6-enabled
414197139Shrs	case $_if in
415197139Shrs	lo0)
416197139Shrs		return 0
417197139Shrs		;;
418197139Shrs	esac
419197139Shrs
420197526Shrs	# True if $ifconfig_IF_ipv6 is defined.
421197526Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
422197526Shrs	if [ -n "${_tmpargs}" ]; then
423197526Shrs		return 0
424197526Shrs	fi
425197526Shrs
426197526Shrs	# backward compatibility: True if $ipv6_ifconfig_IF is defined.
427197526Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
428197526Shrs	if [ -n "${_tmpargs}" ]; then
429197526Shrs		return 0
430197526Shrs	fi
431197526Shrs
432162490Sbrooks	case "${ipv6_network_interfaces}" in
433162490Sbrooks	[Aa][Uu][Tt][Oo])
434162490Sbrooks		return 0
435162490Sbrooks		;;
436162490Sbrooks	''|[Nn][Oo][Nn][Ee])
437162490Sbrooks		return 1
438162490Sbrooks		;;
439162490Sbrooks	esac
440197139Shrs
441197139Shrs	for i in ${ipv6_network_interfaces}; do
442197139Shrs		if [ "$i" = "$_if" ]; then
443162490Sbrooks			return 0
444162490Sbrooks		fi
445162490Sbrooks	done
446197139Shrs
447162490Sbrooks	return 1
448162490Sbrooks}
449162490Sbrooks
450197139Shrs# ipv6_autoconfif if
451197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
452197139Shrs#	Stateless Address Configuration, 1 otherwise.
453197139Shrsipv6_autoconfif()
454197139Shrs{
455197139Shrs	local _if _tmpargs _arg
456197139Shrs	_if=$1
457197139Shrs
458197139Shrs	if ! ipv6if $_if; then
459197139Shrs		return 1
460197139Shrs	fi
461197139Shrs	if noafif $_if; then
462197139Shrs		return 1
463197139Shrs	fi
464197139Shrs	if checkyesno ipv6_gateway_enable; then
465197139Shrs		return 1
466197139Shrs	fi
467197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
468197526Shrs	if [ -n "${_tmpargs}" ]; then
469197526Shrs		return 1
470197526Shrs	fi
471197139Shrs
472197139Shrs	case $_if in
473197139Shrs	lo0|\
474197139Shrs	stf[0-9]*|\
475197139Shrs	faith[0-9]*|\
476197139Shrs	lp[0-9]*|\
477197526Shrs	sl[0-9]*|\
478197526Shrs	pflog[0-9]*|\
479197526Shrs	pfsync[0-9]*)
480197139Shrs		return 1
481197139Shrs		;;
482197139Shrs	esac
483197139Shrs
484197526Shrs	# backward compatibility: $ipv6_enable
485197526Shrs	case $ipv6_enable in
486197526Shrs	[Yy][Ee][Ss])
487197526Shrs		return 0
488197526Shrs		;;
489197526Shrs	esac
490197526Shrs
491197139Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
492197139Shrs	for _arg in $_tmpargs; do
493197139Shrs		case $_arg in
494197139Shrs		accept_rtadv)
495197139Shrs			return 0
496197139Shrs			;;
497197139Shrs		esac
498197139Shrs	done
499197139Shrs
500197526Shrs	# backward compatibility: $ipv6_ifconfig_IF
501197526Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
502197526Shrs	for _arg in $_tmpargs; do
503197526Shrs		case $_arg in
504197526Shrs		accept_rtadv)
505197526Shrs			return 0
506197526Shrs			;;
507197526Shrs		esac
508197526Shrs	done
509197526Shrs
510197139Shrs	return 1
511197139Shrs}
512197139Shrs
513161386Sbrooks# ifexists if
514161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
515161386Sbrooksifexists()
516161386Sbrooks{
517197139Shrs	[ -z "$1" ] && return 1
518169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
519161386Sbrooks}
520161386Sbrooks
521152441Sbrooks# ipv4_up if
522197147Shrs#	add IPv4 addresses to the interface $if 
523152441Sbrooksipv4_up()
524152441Sbrooks{
525197139Shrs	local _if _ret
526152441Sbrooks	_if=$1
527197139Shrs	_ret=1
528197139Shrs
529197139Shrs	ifalias_up ${_if} inet && _ret=0
530197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
531197139Shrs
532197139Shrs	return $_ret
533152441Sbrooks}
534152441Sbrooks
535197139Shrs# ipv6_up if
536197139Shrs#	add IPv6 addresses to the interface $if
537197139Shrsipv6_up()
538197139Shrs{
539197139Shrs	local _if _ret
540197139Shrs	_if=$1
541197139Shrs	_ret=1
542197139Shrs
543197139Shrs	if ! ipv6if $_if; then
544197139Shrs		return 0
545197139Shrs	fi
546197139Shrs
547197139Shrs	ifalias_up ${_if} inet6 && _ret=0
548197139Shrs	ipv6_prefix_hostid_addr_up ${_if} && _ret=0
549197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
550197139Shrs
551197139Shrs	# wait for DAD
552197139Shrs	sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
553197139Shrs	sleep 1
554197139Shrs
555197139Shrs	return $_ret
556197139Shrs}
557197139Shrs
558152441Sbrooks# ipv4_down if
559197147Shrs#	remove IPv4 addresses from the interface $if
560152441Sbrooksipv4_down()
561152441Sbrooks{
562197139Shrs	local _if _ifs _ret inetList oldifs _inet
563152441Sbrooks	_if=$1
564161386Sbrooks	_ifs="^"
565161386Sbrooks	_ret=1
566161386Sbrooks
567161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
568161386Sbrooks
569161386Sbrooks	oldifs="$IFS"
570161386Sbrooks	IFS="$_ifs"
571161386Sbrooks	for _inet in $inetList ; do
572161386Sbrooks		# get rid of extraneous line
573161386Sbrooks		[ -z "$_inet" ] && break
574161386Sbrooks
575161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
576161386Sbrooks
577161386Sbrooks		IFS="$oldifs"
578161386Sbrooks		ifconfig ${_if} ${_inet} delete
579161386Sbrooks		IFS="$_ifs"
580161386Sbrooks		_ret=0
581161386Sbrooks	done
582161386Sbrooks	IFS="$oldifs"
583161386Sbrooks
584197139Shrs	ifalias_down ${_if} inet && _ret=0
585161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
586161386Sbrooks
587161386Sbrooks	return $_ret
588152441Sbrooks}
589152441Sbrooks
590197139Shrs# ipv6_down if
591197139Shrs#	remove IPv6 addresses from the interface $if
592197139Shrsipv6_down()
593197139Shrs{
594197139Shrs	local _if _ifs _ret inetList oldifs _inet6
595197139Shrs	_if=$1
596197139Shrs	_ifs="^"
597197139Shrs	_ret=1
598197139Shrs
599197139Shrs	if ! ipv6if $_if; then
600197139Shrs		return 0
601197139Shrs	fi
602197139Shrs
603197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
604197139Shrs	ifalias_down ${_if} inet6 && _ret=0
605197139Shrs
606197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
607197139Shrs
608197139Shrs	oldifs="$IFS"
609197139Shrs	IFS="$_ifs"
610197139Shrs	for _inet6 in $inetList ; do
611197139Shrs		# get rid of extraneous line
612197139Shrs		[ -z "$_inet6" ] && break
613197139Shrs
614197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
615197139Shrs
616197139Shrs		IFS="$oldifs"
617197139Shrs		ifconfig ${_if} ${_inet6} -alias
618197139Shrs		IFS="$_ifs"
619197139Shrs		_ret=0
620197139Shrs	done
621197139Shrs	IFS="$oldifs"
622197139Shrs
623197139Shrs	return $_ret
624197139Shrs}
625197139Shrs
626152441Sbrooks# ipv4_addrs_common if action
627197147Shrs#	Evaluate the ifconfig_if_ipv4 arguments for interface $if and
628197147Shrs#	use $action to add or remove IPv4 addresses from $if.
629152441Sbrooksipv4_addrs_common()
630197147Shrs{
631197139Shrs	local _ret _if _action _cidr _cidr_addr
632197139Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount 
633152441Sbrooks	_ret=1
634152441Sbrooks	_if=$1
635152441Sbrooks	_action=$2
636152441Sbrooks    
637152441Sbrooks	# get ipv4-addresses
638157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
639152441Sbrooks    
640152441Sbrooks	for _cidr in ${cidr_addr}; do
641152441Sbrooks		_ipaddr=${_cidr%%/*}
642152441Sbrooks		_netmask="/"${_cidr##*/}
643152441Sbrooks		_range=${_ipaddr##*.}
644152441Sbrooks		_ipnet=${_ipaddr%.*}
645152441Sbrooks		_iplow=${_range%-*}
646152441Sbrooks		_iphigh=${_range#*-}
647152441Sbrooks
648152441Sbrooks		# clear netmask when removing aliases
649152441Sbrooks		if [ "${_action}" = "-alias" ]; then
650152441Sbrooks			_netmask=""
651152441Sbrooks		fi
652152441Sbrooks        
653152441Sbrooks		_ipcount=${_iplow}
654152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
655152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
656152441Sbrooks			_ipcount=$((${_ipcount}+1))
657152441Sbrooks			_ret=0
658152441Sbrooks
659152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
660152441Sbrooks			if [ "${_action}" != "-alias" ]; then
661152441Sbrooks				_netmask="/32"
662152441Sbrooks			fi
663152441Sbrooks		done
664152441Sbrooks	done
665197139Shrs
666152441Sbrooks	return $_ret
667152441Sbrooks}
668152441Sbrooks
669197139Shrs# ifalias_up if af
670113674Smtm#	Configure aliases for network interface $if.
671113674Smtm#	It returns 0 if at least one alias was configured or
672113674Smtm#	1 if there were none.
673113674Smtm#
674113674Smtmifalias_up()
675113674Smtm{
676197139Shrs	local _ret
677113674Smtm	_ret=1
678197139Shrs
679197139Shrs	case "$2" in
680197139Shrs	inet)
681197139Shrs		_ret=`ifalias_ipv4_up "$1"`
682197139Shrs		;;
683197139Shrs	inet6)
684197139Shrs		_ret=`ifalias_ipv6_up "$1"`
685197139Shrs		;;
686197139Shrs	esac
687197139Shrs
688197139Shrs	return $_ret
689197139Shrs}
690197139Shrs
691197139Shrs# ifalias_ipv4_up if
692197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
693197139Shrs#
694197139Shrsifalias_ipv4_up()
695197139Shrs{
696197139Shrs	local _ret alias ifconfig_args
697197139Shrs	_ret=1
698197139Shrs
699197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
700113674Smtm	alias=0
701113674Smtm	while : ; do
702157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
703197139Shrs		case "${ifconfig_args}" in
704197139Shrs		inet\ *)
705197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
706197139Shrs			;;
707197139Shrs		"")
708197139Shrs			break
709197139Shrs			;;
710197139Shrs		esac
711197147Shrs		alias=$((${alias} + 1))
712197139Shrs	done
713197139Shrs
714197139Shrs	return $_ret
715197139Shrs}
716197139Shrs
717197139Shrs# ifalias_ipv6_up if
718197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
719197139Shrs#
720197139Shrsifalias_ipv6_up()
721197139Shrs{
722197139Shrs	local _ret alias ifconfig_args
723197139Shrs	_ret=1
724197139Shrs
725197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
726197139Shrs	alias=0
727197139Shrs	while : ; do
728197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
729197139Shrs		case "${ifconfig_args}" in
730197139Shrs		inet6\ *)
731197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
732197139Shrs			;;
733197139Shrs		"")
734113674Smtm			break
735197139Shrs			;;
736197139Shrs		esac
737197139Shrs		alias=$((${alias} + 1))
738113674Smtm	done
739197139Shrs
740197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
741197139Shrs	alias=0
742197139Shrs	while : ; do
743197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
744197139Shrs		case "${ifconfig_args}" in
745197139Shrs		"")
746197139Shrs			break
747197139Shrs			;;
748197139Shrs		*)
749197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
750197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
751197139Shrs			    "  Use ifconfig_$1_aliasN instead."
752197139Shrs			;;
753197139Shrs		esac
754197139Shrs		alias=$((${alias} + 1))
755197139Shrs	done
756197139Shrs
757113674Smtm	return $_ret
758113674Smtm}
759100280Sgordon
760197147Shrs# ifalias_down if af
761116029Smtm#	Remove aliases for network interface $if.
762116029Smtm#	It returns 0 if at least one alias was removed or
763116029Smtm#	1 if there were none.
764116029Smtm#
765116029Smtmifalias_down()
766116029Smtm{
767197139Shrs	local _ret
768116029Smtm	_ret=1
769197139Shrs
770197139Shrs	case "$2" in
771197139Shrs	inet)
772197139Shrs		_ret=`ifalias_ipv4_down "$1"`
773197139Shrs		;;
774197139Shrs	inet6)
775197139Shrs		_ret=`ifalias_ipv6_down "$1"`
776197139Shrs		;;
777197139Shrs	esac
778197139Shrs
779197139Shrs	return $_ret
780197139Shrs}
781197139Shrs
782197147Shrs# ifalias_ipv4_down if
783197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
784197139Shrs#
785197139Shrsifalias_ipv4_down()
786197139Shrs{
787197139Shrs	local _ret alias ifconfig_args
788197139Shrs	_ret=1
789197139Shrs
790197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
791116029Smtm	alias=0
792116029Smtm	while : ; do
793157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
794197139Shrs		case "${ifconfig_args}" in
795197139Shrs		inet\ *)
796197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
797197139Shrs			;;
798197139Shrs		"")
799197139Shrs			break
800197139Shrs			;;
801197139Shrs		esac
802197147Shrs		alias=$((${alias} + 1))
803197139Shrs	done
804197139Shrs
805197139Shrs	return $_ret
806197139Shrs}
807197139Shrs
808197147Shrs# ifalias_ipv6_down if
809197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
810197139Shrs#
811197139Shrsifalias_ipv6_down()
812197139Shrs{
813197139Shrs	local _ret alias ifconfig_args
814197139Shrs	_ret=1
815197139Shrs
816197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
817197139Shrs	alias=0
818197139Shrs	while : ; do
819197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
820197139Shrs		case "${ifconfig_args}" in
821197139Shrs		inet6\ *)
822197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
823197139Shrs			;;
824197139Shrs		"")
825116029Smtm			break
826197139Shrs			;;
827197139Shrs		esac
828197139Shrs		alias=$((${alias} + 1))
829116029Smtm	done
830197139Shrs
831197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
832197526Shrs	alias=0
833197139Shrs	while : ; do
834197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
835197139Shrs		case "${ifconfig_args}" in
836197139Shrs		"")
837197139Shrs			break
838197139Shrs			;;
839197139Shrs		*)
840197526Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
841197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
842197139Shrs			    "  Use ifconfig_$1_aliasN instead."
843197139Shrs			;;
844197139Shrs		esac
845197526Shrs		alias=$((${alias} + 1))
846197139Shrs	done
847197139Shrs
848116029Smtm	return $_ret
849116029Smtm}
850116029Smtm
851197139Shrs# ipv6_prefix_hostid_addr_up if
852197139Shrs#	add IPv6 prefix + hostid addr to the interface $if
853197139Shrsipv6_prefix_hostid_addr_up()
854197139Shrs{
855197139Shrs	local _if prefix laddr hostid j address
856197139Shrs	_if=$1
857197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
858197139Shrs
859197139Shrs	if [ -n "${prefix}" ]; then
860197139Shrs		laddr=`network6_getladdr ${_if}`
861197139Shrs		hostid=${laddr#fe80::}
862197139Shrs		hostid=${hostid%\%*}
863197139Shrs
864197139Shrs		for j in ${prefix}; do
865197139Shrs			address=$j\:${hostid}
866197139Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 alias
867197139Shrs
868197139Shrs			# if I am a router, add subnet router
869197139Shrs			# anycast address (RFC 2373).
870197139Shrs			if checkyesno ipv6_gateway_enable; then
871197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
872197139Shrs					alias anycast
873197139Shrs			fi
874197139Shrs		done
875197139Shrs	fi
876197139Shrs}
877197139Shrs
878197139Shrs# ipv6_accept_rtadv_up if
879197139Shrs#	Enable accepting Router Advertisement and send Router
880197139Shrs#	Solicitation message
881197139Shrsipv6_accept_rtadv_up()
882197139Shrs{
883197139Shrs	if ipv6_autoconfif $1; then
884197139Shrs		ifconfig $1 inet6 accept_rtadv up
885197139Shrs		rtsol ${rtsol_flags} $1
886197139Shrs	fi
887197139Shrs}
888197139Shrs
889197139Shrs# ipv6_accept_rtadv_down if
890197139Shrs#	Disable accepting Router Advertisement
891197139Shrsipv6_accept_rtadv_down()
892197139Shrs{
893197139Shrs	if ipv6_autoconfif $1; then
894197139Shrs		ifconfig $1 inet6 -accept_rtadv
895197139Shrs	fi
896197139Shrs}
897197139Shrs
898113674Smtm# ifscript_up if
899113674Smtm#	Evaluate a startup script for the $if interface.
900113674Smtm#	It returns 0 if a script was found and processed or
901113674Smtm#	1 if no script was found.
902113674Smtm#
903113674Smtmifscript_up()
904100280Sgordon{
905113674Smtm	if [ -r /etc/start_if.$1 ]; then
906113674Smtm		. /etc/start_if.$1
907113674Smtm		return 0
908197139Shrs	else
909197139Shrs		return 1
910113674Smtm	fi
911100280Sgordon}
912100280Sgordon
913116029Smtm# ifscript_down if
914116029Smtm#	Evaluate a shutdown script for the $if interface.
915116029Smtm#	It returns 0 if a script was found and processed or
916116029Smtm#	1 if no script was found.
917116029Smtm#
918116029Smtmifscript_down()
919116029Smtm{
920116029Smtm	if [ -r /etc/stop_if.$1 ]; then
921116029Smtm		. /etc/stop_if.$1
922116029Smtm		return 0
923197139Shrs	else
924197139Shrs		return 1
925116029Smtm	fi
926116029Smtm}
927116029Smtm
928197147Shrs# clone_up
929197147Shrs#	Create cloneable interfaces.
930113674Smtm#
931113674Smtmclone_up()
932100280Sgordon{
933197139Shrs	local _prefix _list ifn
934113674Smtm	_prefix=
935113674Smtm	_list=
936197139Shrs
937197139Shrs	# create_args_IF
938113674Smtm	for ifn in ${cloned_interfaces}; do
939178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
940116774Skuriyama		if [ $? -eq 0 ]; then
941113674Smtm			_list="${_list}${_prefix}${ifn}"
942113674Smtm			[ -z "$_prefix" ] && _prefix=' '
943113674Smtm		fi
944113674Smtm	done
945113674Smtm	debug "Cloned: ${_list}"
946113674Smtm}
947100280Sgordon
948197147Shrs# clone_down
949197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
950197147Shrs#	standard output.
951113674Smtm#
952113674Smtmclone_down()
953113674Smtm{
954197139Shrs	local _prefix _list ifn
955113674Smtm	_prefix=
956113674Smtm	_list=
957197139Shrs
958113674Smtm	for ifn in ${cloned_interfaces}; do
959113674Smtm		ifconfig ${ifn} destroy
960116774Skuriyama		if [ $? -eq 0 ]; then
961113674Smtm			_list="${_list}${_prefix}${ifn}"
962113674Smtm			[ -z "$_prefix" ] && _prefix=' '
963113674Smtm		fi
964113674Smtm	done
965113674Smtm	debug "Destroyed clones: ${_list}"
966100280Sgordon}
967100280Sgordon
968197147Shrs# childif_create
969197147Shrs#	Create and configure child interfaces.  Return 0 if child
970197147Shrs#	interfaces are created.
971178356Ssam#
972178356Ssamchildif_create()
973178356Ssam{
974201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
975178356Ssam	cfg=1
976178356Ssam	ifn=$1
977178356Ssam
978178527Sbrooks	# Create wireless interfaces
979178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
980178527Sbrooks
981178527Sbrooks	for child in ${child_wlans}; do
982183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
983189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
984189759Sbrooks
985178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
986178356Ssam			ifconfig $child create ${create_args} && cfg=0
987189759Sbrooks			if [ -n "${debug_flags}" ]; then
988189759Sbrooks				wlandebug -i $child ${debug_flags}
989189759Sbrooks			fi
990178356Ssam		else
991178356Ssam			i=`ifconfig wlan create ${create_args}`
992189759Sbrooks			if [ -n "${debug_flags}" ]; then
993189759Sbrooks				wlandebug -i $i ${debug_flags}
994189759Sbrooks			fi
995178356Ssam			ifconfig $i name $child && cfg=0
996178356Ssam		fi
997188118Sthompsa		if autoif $child; then
998188118Sthompsa			ifn_start $child
999188118Sthompsa		fi
1000178356Ssam	done
1001178356Ssam
1002201215Sjhb	# Create vlan interfaces
1003201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1004201215Sjhb
1005201215Sjhb	if [ -n "${child_vlans}" ]; then
1006201215Sjhb		load_kld if_vlan
1007201215Sjhb	fi
1008201215Sjhb
1009201215Sjhb	for child in ${child_vlans}; do
1010201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1011201215Sjhb			child="${ifn}.${child}"
1012201215Sjhb			create_args=`get_if_var $child create_args_IF`
1013201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1014201215Sjhb		else
1015201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1016201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1017201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1018201215Sjhb			else
1019201215Sjhb				i=`ifconfig vlan create ${create_args}`
1020201215Sjhb				ifconfig $i name $child && cfg=0
1021201215Sjhb			fi
1022201215Sjhb		fi
1023201215Sjhb		if autoif $child; then
1024201215Sjhb			ifn_start $child
1025201215Sjhb		fi
1026201215Sjhb	done
1027201215Sjhb
1028179001Sbrooks	return ${cfg}
1029178356Ssam}
1030178356Ssam
1031197147Shrs# childif_destroy
1032197147Shrs#	Destroy child interfaces.
1033178356Ssam#
1034178356Ssamchildif_destroy()
1035178356Ssam{
1036201215Sjhb	local cfg child child_vlans child_wlans ifn
1037197139Shrs	cfg=1
1038178356Ssam
1039178527Sbrooks	child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
1040178527Sbrooks	for child in ${child_wlans}; do
1041201215Sjhb		if ! ifexists $child; then
1042201215Sjhb			continue
1043201215Sjhb		fi
1044201215Sjhb		if autoif $child; then
1045201215Sjhb			ifn_stop $child
1046201215Sjhb		fi
1047178356Ssam		ifconfig $child destroy && cfg=0
1048178356Ssam	done
1049197139Shrs
1050201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1051201215Sjhb	for child in ${child_vlans}; do
1052201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1053201215Sjhb			child="${ifn}.${child}"
1054201215Sjhb		fi
1055201215Sjhb		if ! ifexists $child; then
1056201215Sjhb			continue
1057201215Sjhb		fi
1058201215Sjhb		if autoif $child; then
1059201215Sjhb			ifn_stop $child
1060201215Sjhb		fi
1061201215Sjhb		ifconfig $child destroy && cfg=0
1062201215Sjhb	done
1063201215Sjhb
1064197139Shrs	return ${cfg}
1065178356Ssam}
1066178356Ssam
1067197147Shrs# ng_mkpeer
1068197147Shrs#	Create netgraph nodes.
1069166583Sflz#
1070197147Shrsng_mkpeer()
1071197147Shrs{
1072166583Sflz	ngctl -f - 2> /dev/null <<EOF
1073166583Sflzmkpeer $*
1074166583Sflzmsg dummy nodeinfo
1075166583SflzEOF
1076166583Sflz}
1077166583Sflz
1078197147Shrs# ng_create_one
1079197147Shrs#	Create netgraph nodes.
1080197147Shrs#
1081197147Shrsng_create_one()
1082197147Shrs{
1083197139Shrs	local t
1084197139Shrs
1085166583Sflz	ng_mkpeer $* | while read line; do
1086166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1087166583Sflz		if [ -n "${t}" ]; then
1088166583Sflz			echo ${t}
1089166583Sflz			return
1090166583Sflz		fi
1091166583Sflz	done
1092166583Sflz}
1093166583Sflz
1094197147Shrs# gif_up
1095197147Shrs#	Create gif(4) tunnel interfaces.
1096197147Shrsgif_up()
1097197147Shrs{
1098197139Shrs	local i peers
1099197139Shrs
1100166583Sflz	for i in ${gif_interfaces}; do
1101166583Sflz		peers=`get_if_var $i gifconfig_IF`
1102166583Sflz		case ${peers} in
1103166583Sflz		'')
1104166583Sflz			continue
1105166583Sflz			;;
1106166583Sflz		*)
1107177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1108177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1109177682Sbrooks			else
1110177682Sbrooks				gif=`ifconfig gif create`
1111177682Sbrooks				ifconfig $gif name $i
1112177682Sbrooks			fi
1113166583Sflz			ifconfig $i tunnel ${peers}
1114166583Sflz			ifconfig $i up
1115166583Sflz			;;
1116166583Sflz		esac
1117166583Sflz	done
1118166583Sflz}
1119166583Sflz
1120166583Sflz# ng_fec_create ifn
1121197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1122197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1123197139Shrsng_fec_create()
1124197139Shrs{
1125166583Sflz	 local req_iface iface bogus
1126166583Sflz	 req_iface="$1"
1127166583Sflz
1128166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1129166583Sflz
1130166583Sflz	 bogus=""
1131166583Sflz	 while true; do
1132166583Sflz		 iface=`ng_create_one fec dummy fec`
1133166583Sflz		 if [ -z "${iface}" ]; then
1134166583Sflz			 exit 2
1135166583Sflz		 fi
1136166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1137166583Sflz			 break
1138166583Sflz		 fi
1139166583Sflz		 bogus="${bogus} ${iface}"
1140166583Sflz	 done
1141166583Sflz
1142166583Sflz	 for iface in ${bogus}; do
1143166583Sflz		 ngctl shutdown ${iface}:
1144166583Sflz	 done
1145166583Sflz}
1146166583Sflz
1147197147Shrs# fec_up
1148197147Shrs#	Create Fast EtherChannel interfaces.
1149197147Shrsfec_up()
1150197147Shrs{
1151197139Shrs	local i j
1152197139Shrs
1153166583Sflz	for i in ${fec_interfaces}; do
1154166583Sflz		ng_fec_create $i
1155166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1156166583Sflz			case ${j} in
1157100282Sdougb			'')
1158100282Sdougb				continue
1159100282Sdougb				;;
1160100282Sdougb			*)
1161166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1162100282Sdougb				;;
1163100282Sdougb			esac
1164100282Sdougb		done
1165166583Sflz	done
1166100282Sdougb}
1167100282Sdougb
1168113674Smtm# ipx_up ifn
1169197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1170197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1171113674Smtm#
1172113674Smtmipx_up()
1173100280Sgordon{
1174197139Shrs	local ifn
1175113674Smtm	ifn="$1"
1176197139Shrs
1177197139Shrs	# ifconfig_IF_ipx
1178197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1179113674Smtm	if [ -n "${ifconfig_args}" ]; then
1180113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1181113674Smtm		return 0
118285831Sdes	fi
1183197139Shrs
1184113674Smtm	return 1
1185113674Smtm}
118685831Sdes
1187116029Smtm# ipx_down ifn
1188116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1189116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1190113674Smtm#
1191116029Smtmipx_down()
1192116029Smtm{
1193197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1194197139Shrs	_if=$1
1195116100Smtm	_ifs="^"
1196116100Smtm	_ret=1
1197197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1198197139Shrs	oldifs="$IFS"
1199116100Smtm
1200116100Smtm	IFS="$_ifs"
1201116100Smtm	for _ipx in $ipxList ; do
1202116100Smtm		# get rid of extraneous line
1203116100Smtm		[ -z "$_ipx" ] && break
1204116100Smtm
1205116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1206116100Smtm
1207116100Smtm		IFS="$oldifs"
1208197139Shrs		ifconfig ${_if} ${_ipx} delete
1209116100Smtm		IFS="$_ifs"
1210116100Smtm		_ret=0
1211116100Smtm	done
1212116100Smtm	IFS="$oldifs"
1213116100Smtm
1214116100Smtm	return $_ret
1215116029Smtm}
1216116029Smtm
1217137070Spjd# ifnet_rename
1218137070Spjd#	Rename all requested interfaces.
1219116029Smtm#
1220137070Spjdifnet_rename()
1221137070Spjd{
1222197139Shrs	local _if _ifname
1223137070Spjd
1224197139Shrs	# ifconfig_IF_name
1225197139Shrs	for _if in `ifconfig -l`; do
1226157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1227137070Spjd		if [ ! -z "$_ifname" ]; then
1228137070Spjd			ifconfig $_if name $_ifname
1229137070Spjd		fi
1230137070Spjd	done
1231197139Shrs
1232137070Spjd	return 0
1233137070Spjd}
1234137070Spjd
1235113674Smtm# list_net_interfaces type
1236113674Smtm#	List all network interfaces. The type of interface returned
1237113674Smtm#	can be controlled by the type argument. The type
1238113674Smtm#	argument can be any of the following:
1239197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1240197147Shrs#		dhcp	- list only DHCP configured interfaces
1241197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1242197139Shrs#				  Address Autoconf configured interfaces
1243197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1244197139Shrs#				  configured interfaces
1245113674Smtm#	If no argument is specified all network interfaces are output.
1246134429Syar#	Note that the list will include cloned interfaces if applicable.
1247134429Syar#	Cloned interfaces must already exist to have a chance to appear
1248134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1249113674Smtm#
1250113674Smtmlist_net_interfaces()
1251113674Smtm{
1252197139Shrs	local type _tmplist _list _autolist _lo _if
1253113674Smtm	type=$1
125465532Snectar
1255149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
125651231Ssheldonh	#
1257197139Shrs	_tmplist=
125851231Ssheldonh	case ${network_interfaces} in
125951231Ssheldonh	[Aa][Uu][Tt][Oo])
1260149401Sbrooks		_autolist="`ifconfig -l`"
1261149726Sbrooks		_lo=
1262149401Sbrooks		for _if in ${_autolist} ; do
1263149401Sbrooks			if autoif $_if; then
1264149726Sbrooks				if [ "$_if" = "lo0" ]; then
1265149726Sbrooks					_lo="lo0 "
1266149726Sbrooks				else
1267197139Shrs					_tmplist="${_tmplist} ${_if}"
1268149726Sbrooks				fi
1269149401Sbrooks			fi
1270149401Sbrooks		done
1271197139Shrs		_tmplist="${_lo}${_tmplist# }"
127251231Ssheldonh		;;
127383677Sbrooks	*)
1274149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1275196478Sdougb
1276196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1277196478Sdougb		#
1278196478Sdougb		case "$_tmplist" in
1279196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1280196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1281196478Sdougb		esac
128283677Sbrooks		;;
128351231Ssheldonh	esac
128449122Sbrian
1285197139Shrs	_list=
1286197139Shrs	case "$type" in
1287197139Shrs	nodhcp)
1288197139Shrs		for _if in ${_tmplist} ; do
1289197139Shrs			if ! dhcpif $_if && \
1290197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1291197139Shrs				_list="${_list# } ${_if}"
1292197147Shrs			fi
1293197139Shrs		done
1294197139Shrs		;;
1295197139Shrs	dhcp)
1296197147Shrs		for _if in ${_tmplist} ; do
1297197147Shrs			if dhcpif $_if; then
1298197139Shrs				_list="${_list# } ${_if}"
1299197147Shrs			fi
1300197147Shrs		done
1301113674Smtm		;;
1302197139Shrs	noautoconf)
1303197139Shrs		for _if in ${_tmplist} ; do
1304197139Shrs			if ! ipv6_autoconfif $_if && \
1305197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1306197139Shrs				_list="${_list# } ${_if}"
1307197139Shrs			fi
1308197139Shrs		done
1309113674Smtm		;;
1310197139Shrs	autoconf)
1311197139Shrs		for _if in ${_tmplist} ; do
1312197139Shrs			if ipv6_autoconfif $_if; then
1313197139Shrs				_list="${_list# } ${_if}"
1314197139Shrs			fi
1315197139Shrs		done
1316197139Shrs		;;
1317197139Shrs	*)
1318197139Shrs		_list=${_tmplist}
1319197139Shrs		;;
1320113674Smtm	esac
1321197139Shrs
1322197139Shrs	echo $_list
1323197139Shrs
1324130151Sschweikh	return 0
132525184Sjkh}
1326114942Sume
1327179003Sbrooks# get_default_if -address_family
1328179003Sbrooks#	Get the interface of the default route for the given address family.
1329179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1330179003Sbrooks#
1331179003Sbrooksget_default_if()
1332179003Sbrooks{
1333197139Shrs	local routeget oldifs defif line
1334197139Shrs	defif=
1335179003Sbrooks	oldifs="$IFS"
1336179003Sbrooks	IFS="
1337179003Sbrooks"
1338197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1339179003Sbrooks		case $line in
1340179003Sbrooks		*interface:*)
1341179003Sbrooks			defif=${line##*: }
1342179003Sbrooks			;;
1343179003Sbrooks		esac
1344179003Sbrooks	done
1345179003Sbrooks	IFS=${oldifs}
1346179003Sbrooks
1347179003Sbrooks	echo $defif
1348179003Sbrooks}
1349179003Sbrooks
1350197147Shrs# hexdigit arg
1351197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1352114942Sumehexdigit()
1353114942Sume{
1354114942Sume	if [ $1 -lt 10 ]; then
1355114942Sume		echo $1
1356114942Sume	else
1357114942Sume		case $1 in
1358114942Sume		10)	echo a ;;
1359114942Sume		11)	echo b ;;
1360114942Sume		12)	echo c ;;
1361114942Sume		13)	echo d ;;
1362114942Sume		14)	echo e ;;
1363114942Sume		15)	echo f ;;
1364114942Sume		esac
1365114942Sume	fi
1366114942Sume}
1367114942Sume
1368197147Shrs# hexprint arg
1369197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1370114942Sumehexprint()
1371114942Sume{
1372197139Shrs	local val str dig
1373114942Sume	val=$1
1374114942Sume	str=''
1375114942Sume	dig=`hexdigit $((${val} & 15))`
1376114942Sume	str=${dig}${str}
1377114942Sume	val=$((${val} >> 4))
1378197139Shrs
1379114942Sume	while [ ${val} -gt 0 ]; do
1380114942Sume		dig=`hexdigit $((${val} & 15))`
1381114942Sume		str=${dig}${str}
1382114942Sume		val=$((${val} >> 4))
1383114942Sume	done
1384114942Sume
1385114942Sume	echo ${str}
1386114942Sume}
1387114942Sume
1388196436Sdougbis_wired_interface()
1389196436Sdougb{
1390196436Sdougb	local media
1391196436Sdougb
1392196436Sdougb	case `ifconfig $1 2>/dev/null` in
1393196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1394196436Sdougb	esac
1395196436Sdougb
1396196436Sdougb	test "$media" = "Ethernet"
1397196436Sdougb}
1398196436Sdougb
1399197147Shrs# network6_getladdr if [flag]
1400197147Shrs#	Echo link-local address from $if if any.
1401197147Shrs#	If flag is defined, tentative ones will be excluded.
1402114942Sumenetwork6_getladdr()
1403114942Sume{
1404197139Shrs	local proto addr rest
1405114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1406114942Sume		case ${proto} in
1407114942Sume		inet6)
1408114942Sume			case ${addr} in
1409114942Sume			fe80::*)
1410114942Sume				if [ -z "$2" ]; then
1411114942Sume					echo ${addr}
1412114942Sume					return
1413114942Sume				fi
1414114942Sume				case ${rest} in
1415114942Sume				*tentative*)
1416114942Sume					continue
1417114942Sume					;;
1418114942Sume				*)
1419114942Sume					echo ${addr}
1420114942Sume					return
1421114942Sume				esac
1422114942Sume			esac
1423114942Sume		esac
1424114942Sume	done
1425114942Sume}
1426