network.subr revision 222746
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 222746 2011-06-06 11:36:10Z hrs $
2666830Sobrien#
2725184Sjkh
28113674Smtm#
29113674Smtm# Subroutines commonly used from network startup scripts.
30113674Smtm# Requires that rc.conf be loaded first.
31113674Smtm#
3225184Sjkh
33178356Ssam# ifn_start ifn
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
47222515Sbz	afexists inet && ipv4_up ${ifn} && cfg=0
48222515Sbz	afexists inet6 && ipv6_up ${ifn} && cfg=0
49222515Sbz	afexists ipx && 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
67222515Sbz	afexists ipx && ipx_down ${ifn} && cfg=0
68222515Sbz	afexists inet6 && ipv6_down ${ifn} && cfg=0
69222515Sbz	afexists inet && 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
89222515Sbz	# Make sure lo0 always comes up.
90222515Sbz	if [ "$1" = "lo0" ]; then
91222515Sbz		_cfg=0
92222515Sbz	fi
93222515Sbz
94197139Shrs	# ifconfig_IF
95147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
96113674Smtm	if [ -n "${ifconfig_args}" ]; then
97178356Ssam		ifconfig $1 ${ifconfig_args}
98147088Sbrooks		_cfg=0
99113674Smtm	fi
100147088Sbrooks
101197139Shrs	# inet6 specific
102197139Shrs	if afexists inet6; then
103222733Shrs		if checkyesno ipv6_activate_all_interfaces; then
104222733Shrs			_ipv6_opts="-ifdisabled"
105222746Shrs		elif [ "$1" != "lo0" ]; then
106222733Shrs			_ipv6_opts="ifdisabled"
107212574Shrs		fi
108197139Shrs
109222733Shrs		# backward compatibility: $ipv6_enable
110222733Shrs		case $ipv6_enable in
111222733Shrs		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
112222733Shrs			_ipv6_opts="${_ipv6_opts} accept_rtadv"
113222733Shrs			;;
114222733Shrs		esac
115222733Shrs
116212574Shrs		if [ -n "${_ipv6_opts}" ]; then
117212574Shrs			ifconfig $1 inet6 ${_ipv6_opts}
118197526Shrs		fi
119212574Shrs
120212574Shrs		# ifconfig_IF_ipv6
121212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
122212574Shrs		if [ -n "${ifconfig_args}" ]; then
123212574Shrs			ifconfig $1 inet6 -ifdisabled
124212574Shrs			ifconfig $1 ${ifconfig_args}
125212574Shrs			_cfg=0
126212574Shrs		fi
127212574Shrs
128212574Shrs		# backward compatiblity: $ipv6_ifconfig_IF
129212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
130212574Shrs		if [ -n "${ifconfig_args}" ]; then
131212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
132212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
133212574Shrs			ifconfig $1 inet6 -ifdisabled
134212574Shrs			ifconfig $1 inet6 ${ifconfig_args}
135212574Shrs			_cfg=0
136212574Shrs		fi
137197139Shrs	fi
138197139Shrs
139197139Shrs	if [ ${_cfg} -eq 0 ]; then
140197139Shrs		ifconfig $1 up
141197139Shrs	fi
142197139Shrs
143147088Sbrooks	if wpaif $1; then
144147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
145147088Sbrooks		_cfg=0		# XXX: not sure this should count
146147088Sbrooks	fi
147147088Sbrooks
148147088Sbrooks	if dhcpif $1; then
149149726Sbrooks		if [ $_cfg -ne 0 ] ; then
150149726Sbrooks			ifconfig $1 up
151149726Sbrooks		fi
152157706Sbrooks		if syncdhcpif $1; then
153157706Sbrooks			/etc/rc.d/dhclient start $1
154157706Sbrooks		fi
155147088Sbrooks		_cfg=0
156147088Sbrooks	fi
157147088Sbrooks
158147121Sbrooks	return $_cfg
159113674Smtm}
16025184Sjkh
161116029Smtm# ifconfig_down if
162161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
163161386Sbrooks#	the interface exists.
164116029Smtm#
165116029Smtmifconfig_down()
166116029Smtm{
167197139Shrs	local _cfg
168147121Sbrooks	_cfg=1
169116029Smtm
170147088Sbrooks	if wpaif $1; then
171147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
172147121Sbrooks		_cfg=0
173147088Sbrooks	fi
174147088Sbrooks
175147088Sbrooks	if dhcpif $1; then
176147088Sbrooks		/etc/rc.d/dhclient stop $1
177147088Sbrooks		_cfg=0
178147088Sbrooks	fi
179147088Sbrooks
180161386Sbrooks	if ifexists $1; then
181161386Sbrooks		ifconfig $1 down
182161386Sbrooks		_cfg=0
183161386Sbrooks	fi
184157706Sbrooks
185147121Sbrooks	return $_cfg
186116029Smtm}
187116029Smtm
188157706Sbrooks# get_if_var if var [default]
189197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
190197147Shrs#	$var is a string containg the sub-string "IF" which will be
191197147Shrs#	replaced with $if after the characters defined in _punct are
192197147Shrs#	replaced with '_'. If the variable is unset, replace it with
193197147Shrs#	$default if given.
194157706Sbrooksget_if_var()
195157706Sbrooks{
196212578Shrs	local _if _punct _punct_c _var _default prefix suffix
197197139Shrs
198157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
199157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
200157706Sbrooks	fi
201157706Sbrooks
202157706Sbrooks	_if=$1
203157706Sbrooks	_punct=". - / +"
204157736Sbrooks	for _punct_c in $_punct; do
205157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
206157706Sbrooks	done
207157706Sbrooks	_var=$2
208157706Sbrooks	_default=$3
209157706Sbrooks
210157706Sbrooks	prefix=${_var%%IF*}
211157706Sbrooks	suffix=${_var##*IF}
212168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
213157706Sbrooks}
214157706Sbrooks
215197139Shrs# _ifconfig_getargs if [af]
216147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
217147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
218147088Sbrooks#	outside this file.
219147088Sbrooks_ifconfig_getargs()
220147088Sbrooks{
221212574Shrs	local _ifn _af
222147088Sbrooks	_ifn=$1
223197139Shrs	_af=${2+_$2}
224197139Shrs
225147088Sbrooks	if [ -z "$_ifn" ]; then
226147088Sbrooks		return 1
227147088Sbrooks	fi
228147088Sbrooks
229212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
230147088Sbrooks}
231147088Sbrooks
232197139Shrs# ifconfig_getargs if [af]
233147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
234147088Sbrooks#	args such as DHCP and WPA.
235147088Sbrooksifconfig_getargs()
236147088Sbrooks{
237197139Shrs	local _tmpargs _arg _args
238197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
239147088Sbrooks	if [ $? -eq 1 ]; then
240147088Sbrooks		return 1
241147088Sbrooks	fi
242147088Sbrooks	_args=
243147088Sbrooks
244147088Sbrooks	for _arg in $_tmpargs; do
245147088Sbrooks		case $_arg in
246157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
247157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
248157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
249157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
250157706Sbrooks		[Ww][Pp][Aa]) ;;
251147088Sbrooks		*)
252147088Sbrooks			_args="$_args $_arg"
253147088Sbrooks			;;
254147088Sbrooks		esac
255147088Sbrooks	done
256147088Sbrooks
257147088Sbrooks	echo $_args
258147088Sbrooks}
259147088Sbrooks
260149401Sbrooks# autoif
261149401Sbrooks#	Returns 0 if the interface should be automaticly configured at
262149401Sbrooks#	boot time and 1 otherwise.
263149401Sbrooksautoif()
264149401Sbrooks{
265197139Shrs	local _tmpargs _arg
266149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
267197139Shrs
268149401Sbrooks	for _arg in $_tmpargs; do
269149401Sbrooks		case $_arg in
270149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
271149401Sbrooks			return 1
272149401Sbrooks			;;
273149401Sbrooks		esac
274149401Sbrooks	done
275197139Shrs
276149401Sbrooks	return 0
277149401Sbrooks}
278149401Sbrooks
279147088Sbrooks# dhcpif if
280147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
281147088Sbrooksdhcpif()
282147088Sbrooks{
283197139Shrs	local _tmpargs _arg
284147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
285197139Shrs
286147088Sbrooks	for _arg in $_tmpargs; do
287147088Sbrooks		case $_arg in
288147088Sbrooks		[Dd][Hh][Cc][Pp])
289147088Sbrooks			return 0
290147088Sbrooks			;;
291157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
292157706Sbrooks			return 0
293157706Sbrooks			;;
294157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
295157706Sbrooks			return 0
296157706Sbrooks			;;
297147088Sbrooks		esac
298147088Sbrooks	done
299197139Shrs
300147088Sbrooks	return 1
301147088Sbrooks}
302147088Sbrooks
303157706Sbrooks# syncdhcpif
304157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
305157706Sbrooks#	1 otherwise.
306157706Sbrookssyncdhcpif()
307157706Sbrooks{
308197139Shrs	local _tmpargs _arg
309157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
310197139Shrs
311157706Sbrooks	for _arg in $_tmpargs; do
312157706Sbrooks		case $_arg in
313157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
314157706Sbrooks			return 1
315157706Sbrooks			;;
316157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
317157706Sbrooks			return 0
318157706Sbrooks			;;
319157706Sbrooks		esac
320157706Sbrooks	done
321197139Shrs
322197139Shrs	checkyesno synchronous_dhclient
323157706Sbrooks}
324157706Sbrooks
325147088Sbrooks# wpaif if
326147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
327147088Sbrookswpaif()
328147088Sbrooks{
329197139Shrs	local _tmpargs _arg
330147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
331197139Shrs
332147088Sbrooks	for _arg in $_tmpargs; do
333147088Sbrooks		case $_arg in
334147088Sbrooks		[Ww][Pp][Aa])
335147088Sbrooks			return 0
336147088Sbrooks			;;
337147088Sbrooks		esac
338147088Sbrooks	done
339197139Shrs
340147088Sbrooks	return 1
341147088Sbrooks}
342147088Sbrooks
343197139Shrs# afexists af
344197139Shrs#	Returns 0 if the address family is enabled in the kernel
345197139Shrs#	1 otherwise.
346197139Shrsafexists()
347197139Shrs{
348197139Shrs	local _af
349197139Shrs	_af=$1
350197139Shrs
351197139Shrs	case ${_af} in
352197139Shrs	inet)
353222515Sbz		${SYSCTL_N} kern.features.inet > /dev/null 2>&1
354197139Shrs		;;
355197139Shrs	inet6)
356222515Sbz		${SYSCTL_N} kern.features.inet6 > /dev/null 2>&1
357197139Shrs		;;
358197697Shrs	ipx)
359197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
360197697Shrs		;;
361197697Shrs	atm)
362197697Shrs		if [ -x /sbin/atmconfig ]; then
363197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
364197697Shrs		else
365197697Shrs			return 1
366197697Shrs		fi
367197697Shrs		;;
368197139Shrs	*)
369197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
370197139Shrs		;;
371197139Shrs	esac
372197139Shrs}
373197139Shrs
374212574Shrs# noafif if
375212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
376212574Shrsnoafif()
377212574Shrs{
378212574Shrs	local _if
379212574Shrs	_if=$1
380212574Shrs
381212574Shrs	case $_if in
382212574Shrs	pflog[0-9]*|\
383212574Shrs	pfsync[0-9]*|\
384212574Shrs	an[0-9]*|\
385212574Shrs	ath[0-9]*|\
386212574Shrs	ipw[0-9]*|\
387212577Shrs	ipfw[0-9]*|\
388212574Shrs	iwi[0-9]*|\
389212574Shrs	iwn[0-9]*|\
390212574Shrs	ral[0-9]*|\
391212574Shrs	wi[0-9]*|\
392212574Shrs	wl[0-9]*|\
393212574Shrs	wpi[0-9]*)
394212574Shrs		return 0
395212574Shrs		;;
396212574Shrs	esac
397212574Shrs
398212574Shrs	return 1
399212574Shrs}
400212574Shrs
401162490Sbrooks# ipv6if if
402162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
403162490Sbrooks#	1 otherwise.
404162490Sbrooksipv6if()
405162490Sbrooks{
406212574Shrs	local _if _tmpargs i
407212574Shrs	_if=$1
408212574Shrs
409197139Shrs	if ! afexists inet6; then
410162490Sbrooks		return 1
411162490Sbrooks	fi
412197139Shrs
413197139Shrs	# lo0 is always IPv6-enabled
414212574Shrs	case $_if in
415197139Shrs	lo0)
416197139Shrs		return 0
417197139Shrs		;;
418197139Shrs	esac
419197139Shrs
420212574Shrs	case "${ipv6_network_interfaces}" in
421212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
422212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
423212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
424212575Shrs		if [ -n "${_tmpargs}" ]; then
425212575Shrs			return 0
426212575Shrs		fi
427197139Shrs
428212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
429212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
430212575Shrs		if [ -n "${_tmpargs}" ]; then
431212574Shrs			return 0
432212574Shrs		fi
433212575Shrs		;;
434212575Shrs	esac
435197139Shrs
436162490Sbrooks	return 1
437162490Sbrooks}
438162490Sbrooks
439197139Shrs# ipv6_autoconfif if
440197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
441197139Shrs#	Stateless Address Configuration, 1 otherwise.
442197139Shrsipv6_autoconfif()
443197139Shrs{
444197139Shrs	local _if _tmpargs _arg
445197139Shrs	_if=$1
446197139Shrs
447212577Shrs	case $_if in
448212577Shrs	lo0|\
449212577Shrs	stf[0-9]*|\
450212577Shrs	faith[0-9]*|\
451212577Shrs	lp[0-9]*|\
452212577Shrs	sl[0-9]*)
453197139Shrs		return 1
454212577Shrs		;;
455212577Shrs	esac
456212574Shrs	if noafif $_if; then
457212574Shrs		return 1
458212574Shrs	fi
459212577Shrs	if ! ipv6if $_if; then
460212577Shrs		return 1
461212577Shrs	fi
462197139Shrs	if checkyesno ipv6_gateway_enable; then
463197139Shrs		return 1
464197139Shrs	fi
465197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
466197526Shrs	if [ -n "${_tmpargs}" ]; then
467197526Shrs		return 1
468197526Shrs	fi
469212574Shrs	# backward compatibility: $ipv6_enable
470212574Shrs	case $ipv6_enable in
471212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
472197526Shrs		return 0
473212577Shrs	;;
474197526Shrs	esac
475197526Shrs
476212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
477212574Shrs	for _arg in $_tmpargs; do
478212574Shrs		case $_arg in
479212574Shrs		accept_rtadv)
480212574Shrs			return 0
481212574Shrs			;;
482212574Shrs		esac
483212574Shrs	done
484212574Shrs
485212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
486212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
487212574Shrs	for _arg in $_tmpargs; do
488212574Shrs		case $_arg in
489212574Shrs		accept_rtadv)
490212574Shrs			return 0
491212574Shrs			;;
492212574Shrs		esac
493212574Shrs	done
494212574Shrs
495197139Shrs	return 1
496197139Shrs}
497197139Shrs
498161386Sbrooks# ifexists if
499161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
500161386Sbrooksifexists()
501161386Sbrooks{
502197139Shrs	[ -z "$1" ] && return 1
503169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
504161386Sbrooks}
505161386Sbrooks
506152441Sbrooks# ipv4_up if
507212578Shrs#	add IPv4 addresses to the interface $if
508152441Sbrooksipv4_up()
509152441Sbrooks{
510197139Shrs	local _if _ret
511152441Sbrooks	_if=$1
512197139Shrs	_ret=1
513197139Shrs
514222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
515222515Sbz	if [ "${_if}" = "lo0" ]; then
516222515Sbz		ifconfig_args=`ifconfig_getargs ${_if}`
517222515Sbz		if [ -z "${ifconfig_args}" ]; then
518222515Sbz			ifconfig ${_if} inet 127.0.0.1/8 alias
519222515Sbz		fi
520222515Sbz	fi
521197139Shrs	ifalias_up ${_if} inet && _ret=0
522197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
523197139Shrs
524197139Shrs	return $_ret
525152441Sbrooks}
526152441Sbrooks
527197139Shrs# ipv6_up if
528197139Shrs#	add IPv6 addresses to the interface $if
529197139Shrsipv6_up()
530197139Shrs{
531197139Shrs	local _if _ret
532197139Shrs	_if=$1
533197139Shrs	_ret=1
534197139Shrs
535197139Shrs	if ! ipv6if $_if; then
536197139Shrs		return 0
537197139Shrs	fi
538197139Shrs
539197139Shrs	ifalias_up ${_if} inet6 && _ret=0
540197139Shrs	ipv6_prefix_hostid_addr_up ${_if} && _ret=0
541197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
542197139Shrs
543197139Shrs	# wait for DAD
544197139Shrs	sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
545197139Shrs	sleep 1
546197139Shrs
547197139Shrs	return $_ret
548197139Shrs}
549197139Shrs
550152441Sbrooks# ipv4_down if
551197147Shrs#	remove IPv4 addresses from the interface $if
552152441Sbrooksipv4_down()
553152441Sbrooks{
554197139Shrs	local _if _ifs _ret inetList oldifs _inet
555152441Sbrooks	_if=$1
556161386Sbrooks	_ifs="^"
557161386Sbrooks	_ret=1
558161386Sbrooks
559161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
560161386Sbrooks
561161386Sbrooks	oldifs="$IFS"
562161386Sbrooks	IFS="$_ifs"
563161386Sbrooks	for _inet in $inetList ; do
564161386Sbrooks		# get rid of extraneous line
565161386Sbrooks		[ -z "$_inet" ] && break
566161386Sbrooks
567161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
568161386Sbrooks
569161386Sbrooks		IFS="$oldifs"
570161386Sbrooks		ifconfig ${_if} ${_inet} delete
571161386Sbrooks		IFS="$_ifs"
572161386Sbrooks		_ret=0
573161386Sbrooks	done
574161386Sbrooks	IFS="$oldifs"
575161386Sbrooks
576197139Shrs	ifalias_down ${_if} inet && _ret=0
577161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
578161386Sbrooks
579161386Sbrooks	return $_ret
580152441Sbrooks}
581152441Sbrooks
582197139Shrs# ipv6_down if
583197139Shrs#	remove IPv6 addresses from the interface $if
584197139Shrsipv6_down()
585197139Shrs{
586197139Shrs	local _if _ifs _ret inetList oldifs _inet6
587197139Shrs	_if=$1
588197139Shrs	_ifs="^"
589197139Shrs	_ret=1
590197139Shrs
591197139Shrs	if ! ipv6if $_if; then
592197139Shrs		return 0
593197139Shrs	fi
594197139Shrs
595197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
596197139Shrs	ifalias_down ${_if} inet6 && _ret=0
597197139Shrs
598197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
599197139Shrs
600197139Shrs	oldifs="$IFS"
601197139Shrs	IFS="$_ifs"
602197139Shrs	for _inet6 in $inetList ; do
603197139Shrs		# get rid of extraneous line
604197139Shrs		[ -z "$_inet6" ] && break
605197139Shrs
606197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
607197139Shrs
608197139Shrs		IFS="$oldifs"
609197139Shrs		ifconfig ${_if} ${_inet6} -alias
610197139Shrs		IFS="$_ifs"
611197139Shrs		_ret=0
612197139Shrs	done
613197139Shrs	IFS="$oldifs"
614197139Shrs
615197139Shrs	return $_ret
616197139Shrs}
617197139Shrs
618152441Sbrooks# ipv4_addrs_common if action
619197147Shrs#	Evaluate the ifconfig_if_ipv4 arguments for interface $if and
620197147Shrs#	use $action to add or remove IPv4 addresses from $if.
621152441Sbrooksipv4_addrs_common()
622197147Shrs{
623197139Shrs	local _ret _if _action _cidr _cidr_addr
624212578Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount
625152441Sbrooks	_ret=1
626152441Sbrooks	_if=$1
627152441Sbrooks	_action=$2
628212578Shrs
629152441Sbrooks	# get ipv4-addresses
630157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
631212578Shrs
632152441Sbrooks	for _cidr in ${cidr_addr}; do
633152441Sbrooks		_ipaddr=${_cidr%%/*}
634152441Sbrooks		_netmask="/"${_cidr##*/}
635152441Sbrooks		_range=${_ipaddr##*.}
636152441Sbrooks		_ipnet=${_ipaddr%.*}
637152441Sbrooks		_iplow=${_range%-*}
638152441Sbrooks		_iphigh=${_range#*-}
639152441Sbrooks
640152441Sbrooks		# clear netmask when removing aliases
641152441Sbrooks		if [ "${_action}" = "-alias" ]; then
642152441Sbrooks			_netmask=""
643152441Sbrooks		fi
644212578Shrs
645152441Sbrooks		_ipcount=${_iplow}
646152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
647152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
648152441Sbrooks			_ipcount=$((${_ipcount}+1))
649152441Sbrooks			_ret=0
650152441Sbrooks
651152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
652152441Sbrooks			if [ "${_action}" != "-alias" ]; then
653152441Sbrooks				_netmask="/32"
654152441Sbrooks			fi
655152441Sbrooks		done
656152441Sbrooks	done
657197139Shrs
658152441Sbrooks	return $_ret
659152441Sbrooks}
660152441Sbrooks
661197139Shrs# ifalias_up if af
662113674Smtm#	Configure aliases for network interface $if.
663113674Smtm#	It returns 0 if at least one alias was configured or
664113674Smtm#	1 if there were none.
665113674Smtm#
666113674Smtmifalias_up()
667113674Smtm{
668197139Shrs	local _ret
669113674Smtm	_ret=1
670197139Shrs
671197139Shrs	case "$2" in
672197139Shrs	inet)
673197139Shrs		_ret=`ifalias_ipv4_up "$1"`
674197139Shrs		;;
675197139Shrs	inet6)
676197139Shrs		_ret=`ifalias_ipv6_up "$1"`
677197139Shrs		;;
678197139Shrs	esac
679197139Shrs
680197139Shrs	return $_ret
681197139Shrs}
682197139Shrs
683197139Shrs# ifalias_ipv4_up if
684197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
685197139Shrs#
686197139Shrsifalias_ipv4_up()
687197139Shrs{
688197139Shrs	local _ret alias ifconfig_args
689197139Shrs	_ret=1
690197139Shrs
691197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
692113674Smtm	alias=0
693113674Smtm	while : ; do
694157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
695197139Shrs		case "${ifconfig_args}" in
696197139Shrs		inet\ *)
697197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
698197139Shrs			;;
699197139Shrs		"")
700197139Shrs			break
701197139Shrs			;;
702197139Shrs		esac
703197147Shrs		alias=$((${alias} + 1))
704197139Shrs	done
705197139Shrs
706197139Shrs	return $_ret
707197139Shrs}
708197139Shrs
709197139Shrs# ifalias_ipv6_up if
710197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
711197139Shrs#
712197139Shrsifalias_ipv6_up()
713197139Shrs{
714197139Shrs	local _ret alias ifconfig_args
715197139Shrs	_ret=1
716197139Shrs
717197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
718197139Shrs	alias=0
719197139Shrs	while : ; do
720197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
721197139Shrs		case "${ifconfig_args}" in
722197139Shrs		inet6\ *)
723197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
724197139Shrs			;;
725197139Shrs		"")
726113674Smtm			break
727197139Shrs			;;
728197139Shrs		esac
729197139Shrs		alias=$((${alias} + 1))
730113674Smtm	done
731197139Shrs
732197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
733197139Shrs	alias=0
734197139Shrs	while : ; do
735197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
736197139Shrs		case "${ifconfig_args}" in
737197139Shrs		"")
738197139Shrs			break
739197139Shrs			;;
740197139Shrs		*)
741197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
742197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
743197139Shrs			    "  Use ifconfig_$1_aliasN instead."
744197139Shrs			;;
745197139Shrs		esac
746197139Shrs		alias=$((${alias} + 1))
747197139Shrs	done
748197139Shrs
749113674Smtm	return $_ret
750113674Smtm}
751100280Sgordon
752197147Shrs# ifalias_down if af
753116029Smtm#	Remove aliases for network interface $if.
754116029Smtm#	It returns 0 if at least one alias was removed or
755116029Smtm#	1 if there were none.
756116029Smtm#
757116029Smtmifalias_down()
758116029Smtm{
759197139Shrs	local _ret
760116029Smtm	_ret=1
761197139Shrs
762197139Shrs	case "$2" in
763197139Shrs	inet)
764197139Shrs		_ret=`ifalias_ipv4_down "$1"`
765197139Shrs		;;
766197139Shrs	inet6)
767197139Shrs		_ret=`ifalias_ipv6_down "$1"`
768197139Shrs		;;
769197139Shrs	esac
770197139Shrs
771197139Shrs	return $_ret
772197139Shrs}
773197139Shrs
774197147Shrs# ifalias_ipv4_down if
775197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
776197139Shrs#
777197139Shrsifalias_ipv4_down()
778197139Shrs{
779197139Shrs	local _ret alias ifconfig_args
780197139Shrs	_ret=1
781197139Shrs
782197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
783116029Smtm	alias=0
784116029Smtm	while : ; do
785157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
786197139Shrs		case "${ifconfig_args}" in
787197139Shrs		inet\ *)
788197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
789197139Shrs			;;
790197139Shrs		"")
791197139Shrs			break
792197139Shrs			;;
793197139Shrs		esac
794197147Shrs		alias=$((${alias} + 1))
795197139Shrs	done
796197139Shrs
797197139Shrs	return $_ret
798197139Shrs}
799197139Shrs
800197147Shrs# ifalias_ipv6_down if
801197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
802197139Shrs#
803197139Shrsifalias_ipv6_down()
804197139Shrs{
805197139Shrs	local _ret alias ifconfig_args
806197139Shrs	_ret=1
807197139Shrs
808197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
809197139Shrs	alias=0
810197139Shrs	while : ; do
811197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
812197139Shrs		case "${ifconfig_args}" in
813197139Shrs		inet6\ *)
814197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
815197139Shrs			;;
816197139Shrs		"")
817116029Smtm			break
818197139Shrs			;;
819197139Shrs		esac
820197139Shrs		alias=$((${alias} + 1))
821116029Smtm	done
822197139Shrs
823197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
824197526Shrs	alias=0
825197139Shrs	while : ; do
826197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
827197139Shrs		case "${ifconfig_args}" in
828197139Shrs		"")
829197139Shrs			break
830197139Shrs			;;
831197139Shrs		*)
832197526Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
833197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
834197139Shrs			    "  Use ifconfig_$1_aliasN instead."
835197139Shrs			;;
836197139Shrs		esac
837197526Shrs		alias=$((${alias} + 1))
838197139Shrs	done
839197139Shrs
840116029Smtm	return $_ret
841116029Smtm}
842116029Smtm
843197139Shrs# ipv6_prefix_hostid_addr_up if
844197139Shrs#	add IPv6 prefix + hostid addr to the interface $if
845197139Shrsipv6_prefix_hostid_addr_up()
846197139Shrs{
847197139Shrs	local _if prefix laddr hostid j address
848197139Shrs	_if=$1
849197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
850197139Shrs
851197139Shrs	if [ -n "${prefix}" ]; then
852197139Shrs		laddr=`network6_getladdr ${_if}`
853197139Shrs		hostid=${laddr#fe80::}
854197139Shrs		hostid=${hostid%\%*}
855197139Shrs
856197139Shrs		for j in ${prefix}; do
857197139Shrs			address=$j\:${hostid}
858197139Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 alias
859197139Shrs
860197139Shrs			# if I am a router, add subnet router
861197139Shrs			# anycast address (RFC 2373).
862197139Shrs			if checkyesno ipv6_gateway_enable; then
863197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
864197139Shrs					alias anycast
865197139Shrs			fi
866197139Shrs		done
867197139Shrs	fi
868197139Shrs}
869197139Shrs
870197139Shrs# ipv6_accept_rtadv_up if
871197139Shrs#	Enable accepting Router Advertisement and send Router
872197139Shrs#	Solicitation message
873197139Shrsipv6_accept_rtadv_up()
874197139Shrs{
875197139Shrs	if ipv6_autoconfif $1; then
876197139Shrs		ifconfig $1 inet6 accept_rtadv up
877203433Sume		if ! checkyesno rtsold_enable; then
878203433Sume			rtsol ${rtsol_flags} $1
879203433Sume		fi
880197139Shrs	fi
881197139Shrs}
882197139Shrs
883197139Shrs# ipv6_accept_rtadv_down if
884197139Shrs#	Disable accepting Router Advertisement
885197139Shrsipv6_accept_rtadv_down()
886197139Shrs{
887197139Shrs	if ipv6_autoconfif $1; then
888197139Shrs		ifconfig $1 inet6 -accept_rtadv
889197139Shrs	fi
890197139Shrs}
891197139Shrs
892113674Smtm# ifscript_up if
893113674Smtm#	Evaluate a startup script for the $if interface.
894113674Smtm#	It returns 0 if a script was found and processed or
895113674Smtm#	1 if no script was found.
896113674Smtm#
897113674Smtmifscript_up()
898100280Sgordon{
899113674Smtm	if [ -r /etc/start_if.$1 ]; then
900113674Smtm		. /etc/start_if.$1
901113674Smtm		return 0
902197139Shrs	else
903197139Shrs		return 1
904113674Smtm	fi
905100280Sgordon}
906100280Sgordon
907116029Smtm# ifscript_down if
908116029Smtm#	Evaluate a shutdown script for the $if interface.
909116029Smtm#	It returns 0 if a script was found and processed or
910116029Smtm#	1 if no script was found.
911116029Smtm#
912116029Smtmifscript_down()
913116029Smtm{
914116029Smtm	if [ -r /etc/stop_if.$1 ]; then
915116029Smtm		. /etc/stop_if.$1
916116029Smtm		return 0
917197139Shrs	else
918197139Shrs		return 1
919116029Smtm	fi
920116029Smtm}
921116029Smtm
922197147Shrs# clone_up
923197147Shrs#	Create cloneable interfaces.
924113674Smtm#
925113674Smtmclone_up()
926100280Sgordon{
927197139Shrs	local _prefix _list ifn
928113674Smtm	_prefix=
929113674Smtm	_list=
930197139Shrs
931197139Shrs	# create_args_IF
932113674Smtm	for ifn in ${cloned_interfaces}; do
933178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
934116774Skuriyama		if [ $? -eq 0 ]; then
935113674Smtm			_list="${_list}${_prefix}${ifn}"
936113674Smtm			[ -z "$_prefix" ] && _prefix=' '
937113674Smtm		fi
938113674Smtm	done
939113674Smtm	debug "Cloned: ${_list}"
940113674Smtm}
941100280Sgordon
942197147Shrs# clone_down
943197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
944197147Shrs#	standard output.
945113674Smtm#
946113674Smtmclone_down()
947113674Smtm{
948197139Shrs	local _prefix _list ifn
949113674Smtm	_prefix=
950113674Smtm	_list=
951197139Shrs
952113674Smtm	for ifn in ${cloned_interfaces}; do
953208213Sjhb		ifconfig -n ${ifn} destroy
954116774Skuriyama		if [ $? -eq 0 ]; then
955113674Smtm			_list="${_list}${_prefix}${ifn}"
956113674Smtm			[ -z "$_prefix" ] && _prefix=' '
957113674Smtm		fi
958113674Smtm	done
959113674Smtm	debug "Destroyed clones: ${_list}"
960100280Sgordon}
961100280Sgordon
962197147Shrs# childif_create
963197147Shrs#	Create and configure child interfaces.  Return 0 if child
964197147Shrs#	interfaces are created.
965178356Ssam#
966178356Ssamchildif_create()
967178356Ssam{
968201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
969178356Ssam	cfg=1
970178356Ssam	ifn=$1
971178356Ssam
972178527Sbrooks	# Create wireless interfaces
973178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
974178527Sbrooks
975178527Sbrooks	for child in ${child_wlans}; do
976183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
977189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
978189759Sbrooks
979178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
980178356Ssam			ifconfig $child create ${create_args} && cfg=0
981189759Sbrooks			if [ -n "${debug_flags}" ]; then
982189759Sbrooks				wlandebug -i $child ${debug_flags}
983189759Sbrooks			fi
984178356Ssam		else
985178356Ssam			i=`ifconfig wlan create ${create_args}`
986189759Sbrooks			if [ -n "${debug_flags}" ]; then
987189759Sbrooks				wlandebug -i $i ${debug_flags}
988189759Sbrooks			fi
989178356Ssam			ifconfig $i name $child && cfg=0
990178356Ssam		fi
991188118Sthompsa		if autoif $child; then
992188118Sthompsa			ifn_start $child
993188118Sthompsa		fi
994178356Ssam	done
995178356Ssam
996201215Sjhb	# Create vlan interfaces
997201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
998201215Sjhb
999201215Sjhb	if [ -n "${child_vlans}" ]; then
1000201215Sjhb		load_kld if_vlan
1001201215Sjhb	fi
1002201215Sjhb
1003201215Sjhb	for child in ${child_vlans}; do
1004201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1005201215Sjhb			child="${ifn}.${child}"
1006201215Sjhb			create_args=`get_if_var $child create_args_IF`
1007201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1008201215Sjhb		else
1009201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1010201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1011201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1012201215Sjhb			else
1013201215Sjhb				i=`ifconfig vlan create ${create_args}`
1014201215Sjhb				ifconfig $i name $child && cfg=0
1015201215Sjhb			fi
1016201215Sjhb		fi
1017201215Sjhb		if autoif $child; then
1018201215Sjhb			ifn_start $child
1019201215Sjhb		fi
1020201215Sjhb	done
1021201215Sjhb
1022179001Sbrooks	return ${cfg}
1023178356Ssam}
1024178356Ssam
1025197147Shrs# childif_destroy
1026197147Shrs#	Destroy child interfaces.
1027178356Ssam#
1028178356Ssamchildif_destroy()
1029178356Ssam{
1030201215Sjhb	local cfg child child_vlans child_wlans ifn
1031197139Shrs	cfg=1
1032178356Ssam
1033201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1034178527Sbrooks	for child in ${child_wlans}; do
1035201215Sjhb		if ! ifexists $child; then
1036201215Sjhb			continue
1037201215Sjhb		fi
1038208213Sjhb		ifconfig -n $child destroy && cfg=0
1039178356Ssam	done
1040197139Shrs
1041201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1042201215Sjhb	for child in ${child_vlans}; do
1043201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1044201215Sjhb			child="${ifn}.${child}"
1045201215Sjhb		fi
1046201215Sjhb		if ! ifexists $child; then
1047201215Sjhb			continue
1048201215Sjhb		fi
1049208213Sjhb		ifconfig -n $child destroy && cfg=0
1050201215Sjhb	done
1051201215Sjhb
1052197139Shrs	return ${cfg}
1053178356Ssam}
1054178356Ssam
1055197147Shrs# ng_mkpeer
1056197147Shrs#	Create netgraph nodes.
1057166583Sflz#
1058197147Shrsng_mkpeer()
1059197147Shrs{
1060166583Sflz	ngctl -f - 2> /dev/null <<EOF
1061166583Sflzmkpeer $*
1062166583Sflzmsg dummy nodeinfo
1063166583SflzEOF
1064166583Sflz}
1065166583Sflz
1066197147Shrs# ng_create_one
1067197147Shrs#	Create netgraph nodes.
1068197147Shrs#
1069197147Shrsng_create_one()
1070197147Shrs{
1071197139Shrs	local t
1072197139Shrs
1073166583Sflz	ng_mkpeer $* | while read line; do
1074166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1075166583Sflz		if [ -n "${t}" ]; then
1076166583Sflz			echo ${t}
1077166583Sflz			return
1078166583Sflz		fi
1079166583Sflz	done
1080166583Sflz}
1081166583Sflz
1082197147Shrs# gif_up
1083197147Shrs#	Create gif(4) tunnel interfaces.
1084197147Shrsgif_up()
1085197147Shrs{
1086197139Shrs	local i peers
1087197139Shrs
1088166583Sflz	for i in ${gif_interfaces}; do
1089166583Sflz		peers=`get_if_var $i gifconfig_IF`
1090166583Sflz		case ${peers} in
1091166583Sflz		'')
1092166583Sflz			continue
1093166583Sflz			;;
1094166583Sflz		*)
1095177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1096177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1097177682Sbrooks			else
1098177682Sbrooks				gif=`ifconfig gif create`
1099177682Sbrooks				ifconfig $gif name $i
1100177682Sbrooks			fi
1101166583Sflz			ifconfig $i tunnel ${peers}
1102166583Sflz			ifconfig $i up
1103166583Sflz			;;
1104166583Sflz		esac
1105166583Sflz	done
1106166583Sflz}
1107166583Sflz
1108166583Sflz# ng_fec_create ifn
1109197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1110197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1111197139Shrsng_fec_create()
1112197139Shrs{
1113166583Sflz	 local req_iface iface bogus
1114166583Sflz	 req_iface="$1"
1115166583Sflz
1116166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1117166583Sflz
1118166583Sflz	 bogus=""
1119166583Sflz	 while true; do
1120166583Sflz		 iface=`ng_create_one fec dummy fec`
1121166583Sflz		 if [ -z "${iface}" ]; then
1122166583Sflz			 exit 2
1123166583Sflz		 fi
1124166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1125166583Sflz			 break
1126166583Sflz		 fi
1127166583Sflz		 bogus="${bogus} ${iface}"
1128166583Sflz	 done
1129166583Sflz
1130166583Sflz	 for iface in ${bogus}; do
1131166583Sflz		 ngctl shutdown ${iface}:
1132166583Sflz	 done
1133166583Sflz}
1134166583Sflz
1135197147Shrs# fec_up
1136197147Shrs#	Create Fast EtherChannel interfaces.
1137197147Shrsfec_up()
1138197147Shrs{
1139197139Shrs	local i j
1140197139Shrs
1141166583Sflz	for i in ${fec_interfaces}; do
1142166583Sflz		ng_fec_create $i
1143166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1144166583Sflz			case ${j} in
1145100282Sdougb			'')
1146100282Sdougb				continue
1147100282Sdougb				;;
1148100282Sdougb			*)
1149166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1150100282Sdougb				;;
1151100282Sdougb			esac
1152100282Sdougb		done
1153166583Sflz	done
1154100282Sdougb}
1155100282Sdougb
1156113674Smtm# ipx_up ifn
1157197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1158197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1159113674Smtm#
1160113674Smtmipx_up()
1161100280Sgordon{
1162197139Shrs	local ifn
1163113674Smtm	ifn="$1"
1164197139Shrs
1165197139Shrs	# ifconfig_IF_ipx
1166197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1167113674Smtm	if [ -n "${ifconfig_args}" ]; then
1168113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1169113674Smtm		return 0
117085831Sdes	fi
1171197139Shrs
1172113674Smtm	return 1
1173113674Smtm}
117485831Sdes
1175116029Smtm# ipx_down ifn
1176116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1177116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1178113674Smtm#
1179116029Smtmipx_down()
1180116029Smtm{
1181197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1182197139Shrs	_if=$1
1183116100Smtm	_ifs="^"
1184116100Smtm	_ret=1
1185197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1186197139Shrs	oldifs="$IFS"
1187116100Smtm
1188116100Smtm	IFS="$_ifs"
1189116100Smtm	for _ipx in $ipxList ; do
1190116100Smtm		# get rid of extraneous line
1191116100Smtm		[ -z "$_ipx" ] && break
1192116100Smtm
1193116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1194116100Smtm
1195116100Smtm		IFS="$oldifs"
1196197139Shrs		ifconfig ${_if} ${_ipx} delete
1197116100Smtm		IFS="$_ifs"
1198116100Smtm		_ret=0
1199116100Smtm	done
1200116100Smtm	IFS="$oldifs"
1201116100Smtm
1202116100Smtm	return $_ret
1203116029Smtm}
1204116029Smtm
1205137070Spjd# ifnet_rename
1206137070Spjd#	Rename all requested interfaces.
1207116029Smtm#
1208137070Spjdifnet_rename()
1209137070Spjd{
1210197139Shrs	local _if _ifname
1211137070Spjd
1212197139Shrs	# ifconfig_IF_name
1213197139Shrs	for _if in `ifconfig -l`; do
1214157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1215137070Spjd		if [ ! -z "$_ifname" ]; then
1216137070Spjd			ifconfig $_if name $_ifname
1217137070Spjd		fi
1218137070Spjd	done
1219197139Shrs
1220137070Spjd	return 0
1221137070Spjd}
1222137070Spjd
1223113674Smtm# list_net_interfaces type
1224113674Smtm#	List all network interfaces. The type of interface returned
1225113674Smtm#	can be controlled by the type argument. The type
1226113674Smtm#	argument can be any of the following:
1227197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1228197147Shrs#		dhcp	- list only DHCP configured interfaces
1229197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1230197139Shrs#				  Address Autoconf configured interfaces
1231197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1232197139Shrs#				  configured interfaces
1233113674Smtm#	If no argument is specified all network interfaces are output.
1234134429Syar#	Note that the list will include cloned interfaces if applicable.
1235134429Syar#	Cloned interfaces must already exist to have a chance to appear
1236134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1237113674Smtm#
1238113674Smtmlist_net_interfaces()
1239113674Smtm{
1240197139Shrs	local type _tmplist _list _autolist _lo _if
1241113674Smtm	type=$1
124265532Snectar
1243149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
124451231Ssheldonh	#
1245197139Shrs	_tmplist=
124651231Ssheldonh	case ${network_interfaces} in
124751231Ssheldonh	[Aa][Uu][Tt][Oo])
1248149401Sbrooks		_autolist="`ifconfig -l`"
1249149726Sbrooks		_lo=
1250149401Sbrooks		for _if in ${_autolist} ; do
1251149401Sbrooks			if autoif $_if; then
1252149726Sbrooks				if [ "$_if" = "lo0" ]; then
1253149726Sbrooks					_lo="lo0 "
1254149726Sbrooks				else
1255197139Shrs					_tmplist="${_tmplist} ${_if}"
1256149726Sbrooks				fi
1257149401Sbrooks			fi
1258149401Sbrooks		done
1259197139Shrs		_tmplist="${_lo}${_tmplist# }"
126051231Ssheldonh		;;
126183677Sbrooks	*)
1262149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1263196478Sdougb
1264196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1265196478Sdougb		#
1266196478Sdougb		case "$_tmplist" in
1267196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1268196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1269196478Sdougb		esac
127083677Sbrooks		;;
127151231Ssheldonh	esac
127249122Sbrian
1273197139Shrs	_list=
1274197139Shrs	case "$type" in
1275197139Shrs	nodhcp)
1276197139Shrs		for _if in ${_tmplist} ; do
1277197139Shrs			if ! dhcpif $_if && \
1278197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1279197139Shrs				_list="${_list# } ${_if}"
1280197147Shrs			fi
1281197139Shrs		done
1282197139Shrs		;;
1283197139Shrs	dhcp)
1284197147Shrs		for _if in ${_tmplist} ; do
1285197147Shrs			if dhcpif $_if; then
1286197139Shrs				_list="${_list# } ${_if}"
1287197147Shrs			fi
1288197147Shrs		done
1289113674Smtm		;;
1290197139Shrs	noautoconf)
1291197139Shrs		for _if in ${_tmplist} ; do
1292197139Shrs			if ! ipv6_autoconfif $_if && \
1293197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1294197139Shrs				_list="${_list# } ${_if}"
1295197139Shrs			fi
1296197139Shrs		done
1297113674Smtm		;;
1298197139Shrs	autoconf)
1299197139Shrs		for _if in ${_tmplist} ; do
1300197139Shrs			if ipv6_autoconfif $_if; then
1301197139Shrs				_list="${_list# } ${_if}"
1302197139Shrs			fi
1303197139Shrs		done
1304197139Shrs		;;
1305197139Shrs	*)
1306197139Shrs		_list=${_tmplist}
1307197139Shrs		;;
1308113674Smtm	esac
1309197139Shrs
1310197139Shrs	echo $_list
1311197139Shrs
1312130151Sschweikh	return 0
131325184Sjkh}
1314114942Sume
1315179003Sbrooks# get_default_if -address_family
1316179003Sbrooks#	Get the interface of the default route for the given address family.
1317179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1318179003Sbrooks#
1319179003Sbrooksget_default_if()
1320179003Sbrooks{
1321197139Shrs	local routeget oldifs defif line
1322197139Shrs	defif=
1323179003Sbrooks	oldifs="$IFS"
1324179003Sbrooks	IFS="
1325179003Sbrooks"
1326197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1327179003Sbrooks		case $line in
1328179003Sbrooks		*interface:*)
1329179003Sbrooks			defif=${line##*: }
1330179003Sbrooks			;;
1331179003Sbrooks		esac
1332179003Sbrooks	done
1333179003Sbrooks	IFS=${oldifs}
1334179003Sbrooks
1335179003Sbrooks	echo $defif
1336179003Sbrooks}
1337179003Sbrooks
1338197147Shrs# hexdigit arg
1339197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1340114942Sumehexdigit()
1341114942Sume{
1342221884Sjilles	printf '%x\n' "$1"
1343114942Sume}
1344114942Sume
1345197147Shrs# hexprint arg
1346197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1347114942Sumehexprint()
1348114942Sume{
1349221884Sjilles	printf '%x\n' "$1"
1350114942Sume}
1351114942Sume
1352196436Sdougbis_wired_interface()
1353196436Sdougb{
1354196436Sdougb	local media
1355196436Sdougb
1356196436Sdougb	case `ifconfig $1 2>/dev/null` in
1357196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1358196436Sdougb	esac
1359196436Sdougb
1360196436Sdougb	test "$media" = "Ethernet"
1361196436Sdougb}
1362196436Sdougb
1363197147Shrs# network6_getladdr if [flag]
1364197147Shrs#	Echo link-local address from $if if any.
1365197147Shrs#	If flag is defined, tentative ones will be excluded.
1366114942Sumenetwork6_getladdr()
1367114942Sume{
1368197139Shrs	local proto addr rest
1369114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1370114942Sume		case ${proto} in
1371114942Sume		inet6)
1372114942Sume			case ${addr} in
1373114942Sume			fe80::*)
1374114942Sume				if [ -z "$2" ]; then
1375114942Sume					echo ${addr}
1376114942Sume					return
1377114942Sume				fi
1378114942Sume				case ${rest} in
1379114942Sume				*tentative*)
1380114942Sume					continue
1381114942Sume					;;
1382114942Sume				*)
1383114942Sume					echo ${addr}
1384114942Sume					return
1385114942Sume				esac
1386114942Sume			esac
1387114942Sume		esac
1388114942Sume	done
1389114942Sume}
1390