network.subr revision 203433
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 203433 2010-02-03 16:18:42Z ume $
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
885203433Sume		if ! checkyesno rtsold_enable; then
886203433Sume			rtsol ${rtsol_flags} $1
887203433Sume		fi
888197139Shrs	fi
889197139Shrs}
890197139Shrs
891197139Shrs# ipv6_accept_rtadv_down if
892197139Shrs#	Disable accepting Router Advertisement
893197139Shrsipv6_accept_rtadv_down()
894197139Shrs{
895197139Shrs	if ipv6_autoconfif $1; then
896197139Shrs		ifconfig $1 inet6 -accept_rtadv
897197139Shrs	fi
898197139Shrs}
899197139Shrs
900113674Smtm# ifscript_up if
901113674Smtm#	Evaluate a startup script for the $if interface.
902113674Smtm#	It returns 0 if a script was found and processed or
903113674Smtm#	1 if no script was found.
904113674Smtm#
905113674Smtmifscript_up()
906100280Sgordon{
907113674Smtm	if [ -r /etc/start_if.$1 ]; then
908113674Smtm		. /etc/start_if.$1
909113674Smtm		return 0
910197139Shrs	else
911197139Shrs		return 1
912113674Smtm	fi
913100280Sgordon}
914100280Sgordon
915116029Smtm# ifscript_down if
916116029Smtm#	Evaluate a shutdown script for the $if interface.
917116029Smtm#	It returns 0 if a script was found and processed or
918116029Smtm#	1 if no script was found.
919116029Smtm#
920116029Smtmifscript_down()
921116029Smtm{
922116029Smtm	if [ -r /etc/stop_if.$1 ]; then
923116029Smtm		. /etc/stop_if.$1
924116029Smtm		return 0
925197139Shrs	else
926197139Shrs		return 1
927116029Smtm	fi
928116029Smtm}
929116029Smtm
930197147Shrs# clone_up
931197147Shrs#	Create cloneable interfaces.
932113674Smtm#
933113674Smtmclone_up()
934100280Sgordon{
935197139Shrs	local _prefix _list ifn
936113674Smtm	_prefix=
937113674Smtm	_list=
938197139Shrs
939197139Shrs	# create_args_IF
940113674Smtm	for ifn in ${cloned_interfaces}; do
941178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
942116774Skuriyama		if [ $? -eq 0 ]; then
943113674Smtm			_list="${_list}${_prefix}${ifn}"
944113674Smtm			[ -z "$_prefix" ] && _prefix=' '
945113674Smtm		fi
946113674Smtm	done
947113674Smtm	debug "Cloned: ${_list}"
948113674Smtm}
949100280Sgordon
950197147Shrs# clone_down
951197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
952197147Shrs#	standard output.
953113674Smtm#
954113674Smtmclone_down()
955113674Smtm{
956197139Shrs	local _prefix _list ifn
957113674Smtm	_prefix=
958113674Smtm	_list=
959197139Shrs
960113674Smtm	for ifn in ${cloned_interfaces}; do
961113674Smtm		ifconfig ${ifn} destroy
962116774Skuriyama		if [ $? -eq 0 ]; then
963113674Smtm			_list="${_list}${_prefix}${ifn}"
964113674Smtm			[ -z "$_prefix" ] && _prefix=' '
965113674Smtm		fi
966113674Smtm	done
967113674Smtm	debug "Destroyed clones: ${_list}"
968100280Sgordon}
969100280Sgordon
970197147Shrs# childif_create
971197147Shrs#	Create and configure child interfaces.  Return 0 if child
972197147Shrs#	interfaces are created.
973178356Ssam#
974178356Ssamchildif_create()
975178356Ssam{
976201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
977178356Ssam	cfg=1
978178356Ssam	ifn=$1
979178356Ssam
980178527Sbrooks	# Create wireless interfaces
981178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
982178527Sbrooks
983178527Sbrooks	for child in ${child_wlans}; do
984183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
985189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
986189759Sbrooks
987178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
988178356Ssam			ifconfig $child create ${create_args} && cfg=0
989189759Sbrooks			if [ -n "${debug_flags}" ]; then
990189759Sbrooks				wlandebug -i $child ${debug_flags}
991189759Sbrooks			fi
992178356Ssam		else
993178356Ssam			i=`ifconfig wlan create ${create_args}`
994189759Sbrooks			if [ -n "${debug_flags}" ]; then
995189759Sbrooks				wlandebug -i $i ${debug_flags}
996189759Sbrooks			fi
997178356Ssam			ifconfig $i name $child && cfg=0
998178356Ssam		fi
999188118Sthompsa		if autoif $child; then
1000188118Sthompsa			ifn_start $child
1001188118Sthompsa		fi
1002178356Ssam	done
1003178356Ssam
1004201215Sjhb	# Create vlan interfaces
1005201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1006201215Sjhb
1007201215Sjhb	if [ -n "${child_vlans}" ]; then
1008201215Sjhb		load_kld if_vlan
1009201215Sjhb	fi
1010201215Sjhb
1011201215Sjhb	for child in ${child_vlans}; do
1012201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1013201215Sjhb			child="${ifn}.${child}"
1014201215Sjhb			create_args=`get_if_var $child create_args_IF`
1015201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1016201215Sjhb		else
1017201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1018201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1019201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1020201215Sjhb			else
1021201215Sjhb				i=`ifconfig vlan create ${create_args}`
1022201215Sjhb				ifconfig $i name $child && cfg=0
1023201215Sjhb			fi
1024201215Sjhb		fi
1025201215Sjhb		if autoif $child; then
1026201215Sjhb			ifn_start $child
1027201215Sjhb		fi
1028201215Sjhb	done
1029201215Sjhb
1030179001Sbrooks	return ${cfg}
1031178356Ssam}
1032178356Ssam
1033197147Shrs# childif_destroy
1034197147Shrs#	Destroy child interfaces.
1035178356Ssam#
1036178356Ssamchildif_destroy()
1037178356Ssam{
1038201215Sjhb	local cfg child child_vlans child_wlans ifn
1039197139Shrs	cfg=1
1040178356Ssam
1041201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1042178527Sbrooks	for child in ${child_wlans}; do
1043201215Sjhb		if ! ifexists $child; then
1044201215Sjhb			continue
1045201215Sjhb		fi
1046201215Sjhb		if autoif $child; then
1047201215Sjhb			ifn_stop $child
1048201215Sjhb		fi
1049178356Ssam		ifconfig $child destroy && cfg=0
1050178356Ssam	done
1051197139Shrs
1052201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1053201215Sjhb	for child in ${child_vlans}; do
1054201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1055201215Sjhb			child="${ifn}.${child}"
1056201215Sjhb		fi
1057201215Sjhb		if ! ifexists $child; then
1058201215Sjhb			continue
1059201215Sjhb		fi
1060201215Sjhb		if autoif $child; then
1061201215Sjhb			ifn_stop $child
1062201215Sjhb		fi
1063201215Sjhb		ifconfig $child destroy && cfg=0
1064201215Sjhb	done
1065201215Sjhb
1066197139Shrs	return ${cfg}
1067178356Ssam}
1068178356Ssam
1069197147Shrs# ng_mkpeer
1070197147Shrs#	Create netgraph nodes.
1071166583Sflz#
1072197147Shrsng_mkpeer()
1073197147Shrs{
1074166583Sflz	ngctl -f - 2> /dev/null <<EOF
1075166583Sflzmkpeer $*
1076166583Sflzmsg dummy nodeinfo
1077166583SflzEOF
1078166583Sflz}
1079166583Sflz
1080197147Shrs# ng_create_one
1081197147Shrs#	Create netgraph nodes.
1082197147Shrs#
1083197147Shrsng_create_one()
1084197147Shrs{
1085197139Shrs	local t
1086197139Shrs
1087166583Sflz	ng_mkpeer $* | while read line; do
1088166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1089166583Sflz		if [ -n "${t}" ]; then
1090166583Sflz			echo ${t}
1091166583Sflz			return
1092166583Sflz		fi
1093166583Sflz	done
1094166583Sflz}
1095166583Sflz
1096197147Shrs# gif_up
1097197147Shrs#	Create gif(4) tunnel interfaces.
1098197147Shrsgif_up()
1099197147Shrs{
1100197139Shrs	local i peers
1101197139Shrs
1102166583Sflz	for i in ${gif_interfaces}; do
1103166583Sflz		peers=`get_if_var $i gifconfig_IF`
1104166583Sflz		case ${peers} in
1105166583Sflz		'')
1106166583Sflz			continue
1107166583Sflz			;;
1108166583Sflz		*)
1109177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1110177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1111177682Sbrooks			else
1112177682Sbrooks				gif=`ifconfig gif create`
1113177682Sbrooks				ifconfig $gif name $i
1114177682Sbrooks			fi
1115166583Sflz			ifconfig $i tunnel ${peers}
1116166583Sflz			ifconfig $i up
1117166583Sflz			;;
1118166583Sflz		esac
1119166583Sflz	done
1120166583Sflz}
1121166583Sflz
1122166583Sflz# ng_fec_create ifn
1123197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1124197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1125197139Shrsng_fec_create()
1126197139Shrs{
1127166583Sflz	 local req_iface iface bogus
1128166583Sflz	 req_iface="$1"
1129166583Sflz
1130166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1131166583Sflz
1132166583Sflz	 bogus=""
1133166583Sflz	 while true; do
1134166583Sflz		 iface=`ng_create_one fec dummy fec`
1135166583Sflz		 if [ -z "${iface}" ]; then
1136166583Sflz			 exit 2
1137166583Sflz		 fi
1138166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1139166583Sflz			 break
1140166583Sflz		 fi
1141166583Sflz		 bogus="${bogus} ${iface}"
1142166583Sflz	 done
1143166583Sflz
1144166583Sflz	 for iface in ${bogus}; do
1145166583Sflz		 ngctl shutdown ${iface}:
1146166583Sflz	 done
1147166583Sflz}
1148166583Sflz
1149197147Shrs# fec_up
1150197147Shrs#	Create Fast EtherChannel interfaces.
1151197147Shrsfec_up()
1152197147Shrs{
1153197139Shrs	local i j
1154197139Shrs
1155166583Sflz	for i in ${fec_interfaces}; do
1156166583Sflz		ng_fec_create $i
1157166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1158166583Sflz			case ${j} in
1159100282Sdougb			'')
1160100282Sdougb				continue
1161100282Sdougb				;;
1162100282Sdougb			*)
1163166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1164100282Sdougb				;;
1165100282Sdougb			esac
1166100282Sdougb		done
1167166583Sflz	done
1168100282Sdougb}
1169100282Sdougb
1170113674Smtm# ipx_up ifn
1171197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1172197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1173113674Smtm#
1174113674Smtmipx_up()
1175100280Sgordon{
1176197139Shrs	local ifn
1177113674Smtm	ifn="$1"
1178197139Shrs
1179197139Shrs	# ifconfig_IF_ipx
1180197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1181113674Smtm	if [ -n "${ifconfig_args}" ]; then
1182113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1183113674Smtm		return 0
118485831Sdes	fi
1185197139Shrs
1186113674Smtm	return 1
1187113674Smtm}
118885831Sdes
1189116029Smtm# ipx_down ifn
1190116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1191116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1192113674Smtm#
1193116029Smtmipx_down()
1194116029Smtm{
1195197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1196197139Shrs	_if=$1
1197116100Smtm	_ifs="^"
1198116100Smtm	_ret=1
1199197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1200197139Shrs	oldifs="$IFS"
1201116100Smtm
1202116100Smtm	IFS="$_ifs"
1203116100Smtm	for _ipx in $ipxList ; do
1204116100Smtm		# get rid of extraneous line
1205116100Smtm		[ -z "$_ipx" ] && break
1206116100Smtm
1207116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1208116100Smtm
1209116100Smtm		IFS="$oldifs"
1210197139Shrs		ifconfig ${_if} ${_ipx} delete
1211116100Smtm		IFS="$_ifs"
1212116100Smtm		_ret=0
1213116100Smtm	done
1214116100Smtm	IFS="$oldifs"
1215116100Smtm
1216116100Smtm	return $_ret
1217116029Smtm}
1218116029Smtm
1219137070Spjd# ifnet_rename
1220137070Spjd#	Rename all requested interfaces.
1221116029Smtm#
1222137070Spjdifnet_rename()
1223137070Spjd{
1224197139Shrs	local _if _ifname
1225137070Spjd
1226197139Shrs	# ifconfig_IF_name
1227197139Shrs	for _if in `ifconfig -l`; do
1228157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1229137070Spjd		if [ ! -z "$_ifname" ]; then
1230137070Spjd			ifconfig $_if name $_ifname
1231137070Spjd		fi
1232137070Spjd	done
1233197139Shrs
1234137070Spjd	return 0
1235137070Spjd}
1236137070Spjd
1237113674Smtm# list_net_interfaces type
1238113674Smtm#	List all network interfaces. The type of interface returned
1239113674Smtm#	can be controlled by the type argument. The type
1240113674Smtm#	argument can be any of the following:
1241197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1242197147Shrs#		dhcp	- list only DHCP configured interfaces
1243197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1244197139Shrs#				  Address Autoconf configured interfaces
1245197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1246197139Shrs#				  configured interfaces
1247113674Smtm#	If no argument is specified all network interfaces are output.
1248134429Syar#	Note that the list will include cloned interfaces if applicable.
1249134429Syar#	Cloned interfaces must already exist to have a chance to appear
1250134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1251113674Smtm#
1252113674Smtmlist_net_interfaces()
1253113674Smtm{
1254197139Shrs	local type _tmplist _list _autolist _lo _if
1255113674Smtm	type=$1
125665532Snectar
1257149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
125851231Ssheldonh	#
1259197139Shrs	_tmplist=
126051231Ssheldonh	case ${network_interfaces} in
126151231Ssheldonh	[Aa][Uu][Tt][Oo])
1262149401Sbrooks		_autolist="`ifconfig -l`"
1263149726Sbrooks		_lo=
1264149401Sbrooks		for _if in ${_autolist} ; do
1265149401Sbrooks			if autoif $_if; then
1266149726Sbrooks				if [ "$_if" = "lo0" ]; then
1267149726Sbrooks					_lo="lo0 "
1268149726Sbrooks				else
1269197139Shrs					_tmplist="${_tmplist} ${_if}"
1270149726Sbrooks				fi
1271149401Sbrooks			fi
1272149401Sbrooks		done
1273197139Shrs		_tmplist="${_lo}${_tmplist# }"
127451231Ssheldonh		;;
127583677Sbrooks	*)
1276149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1277196478Sdougb
1278196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1279196478Sdougb		#
1280196478Sdougb		case "$_tmplist" in
1281196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1282196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1283196478Sdougb		esac
128483677Sbrooks		;;
128551231Ssheldonh	esac
128649122Sbrian
1287197139Shrs	_list=
1288197139Shrs	case "$type" in
1289197139Shrs	nodhcp)
1290197139Shrs		for _if in ${_tmplist} ; do
1291197139Shrs			if ! dhcpif $_if && \
1292197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1293197139Shrs				_list="${_list# } ${_if}"
1294197147Shrs			fi
1295197139Shrs		done
1296197139Shrs		;;
1297197139Shrs	dhcp)
1298197147Shrs		for _if in ${_tmplist} ; do
1299197147Shrs			if dhcpif $_if; then
1300197139Shrs				_list="${_list# } ${_if}"
1301197147Shrs			fi
1302197147Shrs		done
1303113674Smtm		;;
1304197139Shrs	noautoconf)
1305197139Shrs		for _if in ${_tmplist} ; do
1306197139Shrs			if ! ipv6_autoconfif $_if && \
1307197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1308197139Shrs				_list="${_list# } ${_if}"
1309197139Shrs			fi
1310197139Shrs		done
1311113674Smtm		;;
1312197139Shrs	autoconf)
1313197139Shrs		for _if in ${_tmplist} ; do
1314197139Shrs			if ipv6_autoconfif $_if; then
1315197139Shrs				_list="${_list# } ${_if}"
1316197139Shrs			fi
1317197139Shrs		done
1318197139Shrs		;;
1319197139Shrs	*)
1320197139Shrs		_list=${_tmplist}
1321197139Shrs		;;
1322113674Smtm	esac
1323197139Shrs
1324197139Shrs	echo $_list
1325197139Shrs
1326130151Sschweikh	return 0
132725184Sjkh}
1328114942Sume
1329179003Sbrooks# get_default_if -address_family
1330179003Sbrooks#	Get the interface of the default route for the given address family.
1331179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1332179003Sbrooks#
1333179003Sbrooksget_default_if()
1334179003Sbrooks{
1335197139Shrs	local routeget oldifs defif line
1336197139Shrs	defif=
1337179003Sbrooks	oldifs="$IFS"
1338179003Sbrooks	IFS="
1339179003Sbrooks"
1340197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1341179003Sbrooks		case $line in
1342179003Sbrooks		*interface:*)
1343179003Sbrooks			defif=${line##*: }
1344179003Sbrooks			;;
1345179003Sbrooks		esac
1346179003Sbrooks	done
1347179003Sbrooks	IFS=${oldifs}
1348179003Sbrooks
1349179003Sbrooks	echo $defif
1350179003Sbrooks}
1351179003Sbrooks
1352197147Shrs# hexdigit arg
1353197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1354114942Sumehexdigit()
1355114942Sume{
1356114942Sume	if [ $1 -lt 10 ]; then
1357114942Sume		echo $1
1358114942Sume	else
1359114942Sume		case $1 in
1360114942Sume		10)	echo a ;;
1361114942Sume		11)	echo b ;;
1362114942Sume		12)	echo c ;;
1363114942Sume		13)	echo d ;;
1364114942Sume		14)	echo e ;;
1365114942Sume		15)	echo f ;;
1366114942Sume		esac
1367114942Sume	fi
1368114942Sume}
1369114942Sume
1370197147Shrs# hexprint arg
1371197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1372114942Sumehexprint()
1373114942Sume{
1374197139Shrs	local val str dig
1375114942Sume	val=$1
1376114942Sume	str=''
1377114942Sume	dig=`hexdigit $((${val} & 15))`
1378114942Sume	str=${dig}${str}
1379114942Sume	val=$((${val} >> 4))
1380197139Shrs
1381114942Sume	while [ ${val} -gt 0 ]; do
1382114942Sume		dig=`hexdigit $((${val} & 15))`
1383114942Sume		str=${dig}${str}
1384114942Sume		val=$((${val} >> 4))
1385114942Sume	done
1386114942Sume
1387114942Sume	echo ${str}
1388114942Sume}
1389114942Sume
1390196436Sdougbis_wired_interface()
1391196436Sdougb{
1392196436Sdougb	local media
1393196436Sdougb
1394196436Sdougb	case `ifconfig $1 2>/dev/null` in
1395196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1396196436Sdougb	esac
1397196436Sdougb
1398196436Sdougb	test "$media" = "Ethernet"
1399196436Sdougb}
1400196436Sdougb
1401197147Shrs# network6_getladdr if [flag]
1402197147Shrs#	Echo link-local address from $if if any.
1403197147Shrs#	If flag is defined, tentative ones will be excluded.
1404114942Sumenetwork6_getladdr()
1405114942Sume{
1406197139Shrs	local proto addr rest
1407114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1408114942Sume		case ${proto} in
1409114942Sume		inet6)
1410114942Sume			case ${addr} in
1411114942Sume			fe80::*)
1412114942Sume				if [ -z "$2" ]; then
1413114942Sume					echo ${addr}
1414114942Sume					return
1415114942Sume				fi
1416114942Sume				case ${rest} in
1417114942Sume				*tentative*)
1418114942Sume					continue
1419114942Sume					;;
1420114942Sume				*)
1421114942Sume					echo ${addr}
1422114942Sume					return
1423114942Sume				esac
1424114942Sume			esac
1425114942Sume		esac
1426114942Sume	done
1427114942Sume}
1428