network.subr revision 225521
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 225521 2011-09-13 00:06:11Z 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
97223506Spluknet		eval 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
116225521Shrs		case $ipv6_cpe_wanif in
117225521Shrs		$1)
118225521Shrs			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
119225521Shrs		;;
120225521Shrs		esac
121225521Shrs
122212574Shrs		if [ -n "${_ipv6_opts}" ]; then
123212574Shrs			ifconfig $1 inet6 ${_ipv6_opts}
124197526Shrs		fi
125212574Shrs
126212574Shrs		# ifconfig_IF_ipv6
127212574Shrs		ifconfig_args=`ifconfig_getargs $1 ipv6`
128212574Shrs		if [ -n "${ifconfig_args}" ]; then
129212574Shrs			ifconfig $1 inet6 -ifdisabled
130212574Shrs			ifconfig $1 ${ifconfig_args}
131212574Shrs			_cfg=0
132212574Shrs		fi
133212574Shrs
134212574Shrs		# backward compatiblity: $ipv6_ifconfig_IF
135212574Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
136212574Shrs		if [ -n "${ifconfig_args}" ]; then
137212574Shrs			warn "\$ipv6_ifconfig_$1 is obsolete." \
138212574Shrs			    "  Use ifconfig_$1_ipv6 instead."
139212574Shrs			ifconfig $1 inet6 -ifdisabled
140212574Shrs			ifconfig $1 inet6 ${ifconfig_args}
141212574Shrs			_cfg=0
142212574Shrs		fi
143197139Shrs	fi
144197139Shrs
145197139Shrs	if [ ${_cfg} -eq 0 ]; then
146197139Shrs		ifconfig $1 up
147197139Shrs	fi
148197139Shrs
149147088Sbrooks	if wpaif $1; then
150147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
151147088Sbrooks		_cfg=0		# XXX: not sure this should count
152147088Sbrooks	fi
153147088Sbrooks
154147088Sbrooks	if dhcpif $1; then
155149726Sbrooks		if [ $_cfg -ne 0 ] ; then
156149726Sbrooks			ifconfig $1 up
157149726Sbrooks		fi
158157706Sbrooks		if syncdhcpif $1; then
159157706Sbrooks			/etc/rc.d/dhclient start $1
160157706Sbrooks		fi
161147088Sbrooks		_cfg=0
162147088Sbrooks	fi
163147088Sbrooks
164147121Sbrooks	return $_cfg
165113674Smtm}
16625184Sjkh
167116029Smtm# ifconfig_down if
168161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
169161386Sbrooks#	the interface exists.
170116029Smtm#
171116029Smtmifconfig_down()
172116029Smtm{
173197139Shrs	local _cfg
174147121Sbrooks	_cfg=1
175116029Smtm
176147088Sbrooks	if wpaif $1; then
177147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
178147121Sbrooks		_cfg=0
179147088Sbrooks	fi
180147088Sbrooks
181147088Sbrooks	if dhcpif $1; then
182147088Sbrooks		/etc/rc.d/dhclient stop $1
183147088Sbrooks		_cfg=0
184147088Sbrooks	fi
185147088Sbrooks
186161386Sbrooks	if ifexists $1; then
187161386Sbrooks		ifconfig $1 down
188161386Sbrooks		_cfg=0
189161386Sbrooks	fi
190157706Sbrooks
191147121Sbrooks	return $_cfg
192116029Smtm}
193116029Smtm
194157706Sbrooks# get_if_var if var [default]
195197147Shrs#	Return the value of the pseudo-hash corresponding to $if where
196197147Shrs#	$var is a string containg the sub-string "IF" which will be
197197147Shrs#	replaced with $if after the characters defined in _punct are
198197147Shrs#	replaced with '_'. If the variable is unset, replace it with
199197147Shrs#	$default if given.
200157706Sbrooksget_if_var()
201157706Sbrooks{
202212578Shrs	local _if _punct _punct_c _var _default prefix suffix
203197139Shrs
204157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
205157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
206157706Sbrooks	fi
207157706Sbrooks
208157706Sbrooks	_if=$1
209157706Sbrooks	_punct=". - / +"
210157736Sbrooks	for _punct_c in $_punct; do
211157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
212157706Sbrooks	done
213157706Sbrooks	_var=$2
214157706Sbrooks	_default=$3
215157706Sbrooks
216157706Sbrooks	prefix=${_var%%IF*}
217157706Sbrooks	suffix=${_var##*IF}
218168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
219157706Sbrooks}
220157706Sbrooks
221197139Shrs# _ifconfig_getargs if [af]
222147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
223147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
224147088Sbrooks#	outside this file.
225147088Sbrooks_ifconfig_getargs()
226147088Sbrooks{
227212574Shrs	local _ifn _af
228147088Sbrooks	_ifn=$1
229197139Shrs	_af=${2+_$2}
230197139Shrs
231147088Sbrooks	if [ -z "$_ifn" ]; then
232147088Sbrooks		return 1
233147088Sbrooks	fi
234147088Sbrooks
235212574Shrs	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
236147088Sbrooks}
237147088Sbrooks
238197139Shrs# ifconfig_getargs if [af]
239147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
240147088Sbrooks#	args such as DHCP and WPA.
241147088Sbrooksifconfig_getargs()
242147088Sbrooks{
243197139Shrs	local _tmpargs _arg _args
244197139Shrs	_tmpargs=`_ifconfig_getargs $1 $2`
245147088Sbrooks	if [ $? -eq 1 ]; then
246147088Sbrooks		return 1
247147088Sbrooks	fi
248147088Sbrooks	_args=
249147088Sbrooks
250147088Sbrooks	for _arg in $_tmpargs; do
251147088Sbrooks		case $_arg in
252157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
253157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
254157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
255157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
256157706Sbrooks		[Ww][Pp][Aa]) ;;
257147088Sbrooks		*)
258147088Sbrooks			_args="$_args $_arg"
259147088Sbrooks			;;
260147088Sbrooks		esac
261147088Sbrooks	done
262147088Sbrooks
263147088Sbrooks	echo $_args
264147088Sbrooks}
265147088Sbrooks
266149401Sbrooks# autoif
267149401Sbrooks#	Returns 0 if the interface should be automaticly configured at
268149401Sbrooks#	boot time and 1 otherwise.
269149401Sbrooksautoif()
270149401Sbrooks{
271197139Shrs	local _tmpargs _arg
272149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
273197139Shrs
274149401Sbrooks	for _arg in $_tmpargs; do
275149401Sbrooks		case $_arg in
276149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
277149401Sbrooks			return 1
278149401Sbrooks			;;
279149401Sbrooks		esac
280149401Sbrooks	done
281197139Shrs
282149401Sbrooks	return 0
283149401Sbrooks}
284149401Sbrooks
285147088Sbrooks# dhcpif if
286147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
287147088Sbrooksdhcpif()
288147088Sbrooks{
289197139Shrs	local _tmpargs _arg
290147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
291197139Shrs
292147088Sbrooks	for _arg in $_tmpargs; do
293147088Sbrooks		case $_arg in
294147088Sbrooks		[Dd][Hh][Cc][Pp])
295147088Sbrooks			return 0
296147088Sbrooks			;;
297157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
298157706Sbrooks			return 0
299157706Sbrooks			;;
300157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
301157706Sbrooks			return 0
302157706Sbrooks			;;
303147088Sbrooks		esac
304147088Sbrooks	done
305197139Shrs
306147088Sbrooks	return 1
307147088Sbrooks}
308147088Sbrooks
309157706Sbrooks# syncdhcpif
310157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
311157706Sbrooks#	1 otherwise.
312157706Sbrookssyncdhcpif()
313157706Sbrooks{
314197139Shrs	local _tmpargs _arg
315157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
316197139Shrs
317157706Sbrooks	for _arg in $_tmpargs; do
318157706Sbrooks		case $_arg in
319157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
320157706Sbrooks			return 1
321157706Sbrooks			;;
322157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
323157706Sbrooks			return 0
324157706Sbrooks			;;
325157706Sbrooks		esac
326157706Sbrooks	done
327197139Shrs
328197139Shrs	checkyesno synchronous_dhclient
329157706Sbrooks}
330157706Sbrooks
331147088Sbrooks# wpaif if
332147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
333147088Sbrookswpaif()
334147088Sbrooks{
335197139Shrs	local _tmpargs _arg
336147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
337197139Shrs
338147088Sbrooks	for _arg in $_tmpargs; do
339147088Sbrooks		case $_arg in
340147088Sbrooks		[Ww][Pp][Aa])
341147088Sbrooks			return 0
342147088Sbrooks			;;
343147088Sbrooks		esac
344147088Sbrooks	done
345197139Shrs
346147088Sbrooks	return 1
347147088Sbrooks}
348147088Sbrooks
349197139Shrs# afexists af
350197139Shrs#	Returns 0 if the address family is enabled in the kernel
351197139Shrs#	1 otherwise.
352197139Shrsafexists()
353197139Shrs{
354197139Shrs	local _af
355197139Shrs	_af=$1
356197139Shrs
357197139Shrs	case ${_af} in
358222996Shrs	inet|inet6)
359222996Shrs		check_kern_features ${_af}
360197139Shrs		;;
361197697Shrs	ipx)
362197697Shrs		${SYSCTL_N} net.ipx > /dev/null 2>&1
363197697Shrs		;;
364197697Shrs	atm)
365197697Shrs		if [ -x /sbin/atmconfig ]; then
366197697Shrs			/sbin/atmconfig diag list > /dev/null 2>&1
367197697Shrs		else
368197697Shrs			return 1
369197697Shrs		fi
370197697Shrs		;;
371197139Shrs	*)
372197139Shrs		err 1 "afexists(): Unsupported address family: $_af"
373197139Shrs		;;
374197139Shrs	esac
375197139Shrs}
376197139Shrs
377212574Shrs# noafif if
378212574Shrs#	Returns 0 if the interface has no af configuration and 1 otherwise.
379212574Shrsnoafif()
380212574Shrs{
381212574Shrs	local _if
382212574Shrs	_if=$1
383212574Shrs
384212574Shrs	case $_if in
385212574Shrs	pflog[0-9]*|\
386212574Shrs	pfsync[0-9]*|\
387212574Shrs	an[0-9]*|\
388212574Shrs	ath[0-9]*|\
389212574Shrs	ipw[0-9]*|\
390212577Shrs	ipfw[0-9]*|\
391212574Shrs	iwi[0-9]*|\
392212574Shrs	iwn[0-9]*|\
393212574Shrs	ral[0-9]*|\
394212574Shrs	wi[0-9]*|\
395212574Shrs	wl[0-9]*|\
396212574Shrs	wpi[0-9]*)
397212574Shrs		return 0
398212574Shrs		;;
399212574Shrs	esac
400212574Shrs
401212574Shrs	return 1
402212574Shrs}
403212574Shrs
404162490Sbrooks# ipv6if if
405162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
406162490Sbrooks#	1 otherwise.
407162490Sbrooksipv6if()
408162490Sbrooks{
409212574Shrs	local _if _tmpargs i
410212574Shrs	_if=$1
411212574Shrs
412197139Shrs	if ! afexists inet6; then
413162490Sbrooks		return 1
414162490Sbrooks	fi
415197139Shrs
416197139Shrs	# lo0 is always IPv6-enabled
417212574Shrs	case $_if in
418197139Shrs	lo0)
419197139Shrs		return 0
420197139Shrs		;;
421197139Shrs	esac
422197139Shrs
423212574Shrs	case "${ipv6_network_interfaces}" in
424212575Shrs	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
425212575Shrs		# True if $ifconfig_IF_ipv6 is defined.
426212575Shrs		_tmpargs=`_ifconfig_getargs $_if ipv6`
427212575Shrs		if [ -n "${_tmpargs}" ]; then
428212575Shrs			return 0
429212575Shrs		fi
430197139Shrs
431212575Shrs		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
432212575Shrs		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
433212575Shrs		if [ -n "${_tmpargs}" ]; then
434212574Shrs			return 0
435212574Shrs		fi
436212575Shrs		;;
437212575Shrs	esac
438197139Shrs
439162490Sbrooks	return 1
440162490Sbrooks}
441162490Sbrooks
442197139Shrs# ipv6_autoconfif if
443197139Shrs#	Returns 0 if the interface should be configured for IPv6 with
444197139Shrs#	Stateless Address Configuration, 1 otherwise.
445197139Shrsipv6_autoconfif()
446197139Shrs{
447197139Shrs	local _if _tmpargs _arg
448197139Shrs	_if=$1
449197139Shrs
450212577Shrs	case $_if in
451212577Shrs	lo0|\
452212577Shrs	stf[0-9]*|\
453212577Shrs	faith[0-9]*|\
454212577Shrs	lp[0-9]*|\
455212577Shrs	sl[0-9]*)
456197139Shrs		return 1
457212577Shrs		;;
458212577Shrs	esac
459212574Shrs	if noafif $_if; then
460212574Shrs		return 1
461212574Shrs	fi
462212577Shrs	if ! ipv6if $_if; then
463212577Shrs		return 1
464212577Shrs	fi
465197139Shrs	if checkyesno ipv6_gateway_enable; then
466197139Shrs		return 1
467197139Shrs	fi
468197526Shrs	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
469197526Shrs	if [ -n "${_tmpargs}" ]; then
470197526Shrs		return 1
471197526Shrs	fi
472212574Shrs	# backward compatibility: $ipv6_enable
473212574Shrs	case $ipv6_enable in
474212577Shrs	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
475197526Shrs		return 0
476212577Shrs	;;
477197526Shrs	esac
478197526Shrs
479212574Shrs	_tmpargs=`_ifconfig_getargs $_if ipv6`
480212574Shrs	for _arg in $_tmpargs; do
481212574Shrs		case $_arg in
482212574Shrs		accept_rtadv)
483212574Shrs			return 0
484212574Shrs			;;
485212574Shrs		esac
486212574Shrs	done
487212574Shrs
488212574Shrs	# backward compatibility: $ipv6_ifconfig_IF
489212574Shrs	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
490212574Shrs	for _arg in $_tmpargs; do
491212574Shrs		case $_arg in
492212574Shrs		accept_rtadv)
493212574Shrs			return 0
494212574Shrs			;;
495212574Shrs		esac
496212574Shrs	done
497212574Shrs
498197139Shrs	return 1
499197139Shrs}
500197139Shrs
501161386Sbrooks# ifexists if
502161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
503161386Sbrooksifexists()
504161386Sbrooks{
505197139Shrs	[ -z "$1" ] && return 1
506169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
507161386Sbrooks}
508161386Sbrooks
509152441Sbrooks# ipv4_up if
510212578Shrs#	add IPv4 addresses to the interface $if
511152441Sbrooksipv4_up()
512152441Sbrooks{
513197139Shrs	local _if _ret
514152441Sbrooks	_if=$1
515197139Shrs	_ret=1
516197139Shrs
517222515Sbz	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
518222515Sbz	if [ "${_if}" = "lo0" ]; then
519222515Sbz		ifconfig_args=`ifconfig_getargs ${_if}`
520222515Sbz		if [ -z "${ifconfig_args}" ]; then
521222515Sbz			ifconfig ${_if} inet 127.0.0.1/8 alias
522222515Sbz		fi
523222515Sbz	fi
524197139Shrs	ifalias_up ${_if} inet && _ret=0
525197139Shrs	ipv4_addrs_common ${_if} alias && _ret=0
526197139Shrs
527197139Shrs	return $_ret
528152441Sbrooks}
529152441Sbrooks
530197139Shrs# ipv6_up if
531197139Shrs#	add IPv6 addresses to the interface $if
532197139Shrsipv6_up()
533197139Shrs{
534197139Shrs	local _if _ret
535197139Shrs	_if=$1
536197139Shrs	_ret=1
537197139Shrs
538197139Shrs	if ! ipv6if $_if; then
539197139Shrs		return 0
540197139Shrs	fi
541197139Shrs
542197139Shrs	ifalias_up ${_if} inet6 && _ret=0
543197139Shrs	ipv6_prefix_hostid_addr_up ${_if} && _ret=0
544197139Shrs	ipv6_accept_rtadv_up ${_if} && _ret=0
545197139Shrs
546197139Shrs	# wait for DAD
547197139Shrs	sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
548197139Shrs	sleep 1
549197139Shrs
550197139Shrs	return $_ret
551197139Shrs}
552197139Shrs
553152441Sbrooks# ipv4_down if
554197147Shrs#	remove IPv4 addresses from the interface $if
555152441Sbrooksipv4_down()
556152441Sbrooks{
557197139Shrs	local _if _ifs _ret inetList oldifs _inet
558152441Sbrooks	_if=$1
559161386Sbrooks	_ifs="^"
560161386Sbrooks	_ret=1
561161386Sbrooks
562161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
563161386Sbrooks
564161386Sbrooks	oldifs="$IFS"
565161386Sbrooks	IFS="$_ifs"
566161386Sbrooks	for _inet in $inetList ; do
567161386Sbrooks		# get rid of extraneous line
568161386Sbrooks		[ -z "$_inet" ] && break
569161386Sbrooks
570161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
571161386Sbrooks
572161386Sbrooks		IFS="$oldifs"
573161386Sbrooks		ifconfig ${_if} ${_inet} delete
574161386Sbrooks		IFS="$_ifs"
575161386Sbrooks		_ret=0
576161386Sbrooks	done
577161386Sbrooks	IFS="$oldifs"
578161386Sbrooks
579197139Shrs	ifalias_down ${_if} inet && _ret=0
580161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
581161386Sbrooks
582161386Sbrooks	return $_ret
583152441Sbrooks}
584152441Sbrooks
585197139Shrs# ipv6_down if
586197139Shrs#	remove IPv6 addresses from the interface $if
587197139Shrsipv6_down()
588197139Shrs{
589197139Shrs	local _if _ifs _ret inetList oldifs _inet6
590197139Shrs	_if=$1
591197139Shrs	_ifs="^"
592197139Shrs	_ret=1
593197139Shrs
594197139Shrs	if ! ipv6if $_if; then
595197139Shrs		return 0
596197139Shrs	fi
597197139Shrs
598197139Shrs	ipv6_accept_rtadv_down ${_if} && _ret=0
599197139Shrs	ifalias_down ${_if} inet6 && _ret=0
600197139Shrs
601197139Shrs	inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
602197139Shrs
603197139Shrs	oldifs="$IFS"
604197139Shrs	IFS="$_ifs"
605197139Shrs	for _inet6 in $inetList ; do
606197139Shrs		# get rid of extraneous line
607197139Shrs		[ -z "$_inet6" ] && break
608197139Shrs
609197139Shrs		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
610197139Shrs
611197139Shrs		IFS="$oldifs"
612197139Shrs		ifconfig ${_if} ${_inet6} -alias
613197139Shrs		IFS="$_ifs"
614197139Shrs		_ret=0
615197139Shrs	done
616197139Shrs	IFS="$oldifs"
617197139Shrs
618197139Shrs	return $_ret
619197139Shrs}
620197139Shrs
621152441Sbrooks# ipv4_addrs_common if action
622197147Shrs#	Evaluate the ifconfig_if_ipv4 arguments for interface $if and
623197147Shrs#	use $action to add or remove IPv4 addresses from $if.
624152441Sbrooksipv4_addrs_common()
625197147Shrs{
626197139Shrs	local _ret _if _action _cidr _cidr_addr
627212578Shrs	local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount
628152441Sbrooks	_ret=1
629152441Sbrooks	_if=$1
630152441Sbrooks	_action=$2
631212578Shrs
632152441Sbrooks	# get ipv4-addresses
633157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
634212578Shrs
635152441Sbrooks	for _cidr in ${cidr_addr}; do
636152441Sbrooks		_ipaddr=${_cidr%%/*}
637152441Sbrooks		_netmask="/"${_cidr##*/}
638152441Sbrooks		_range=${_ipaddr##*.}
639152441Sbrooks		_ipnet=${_ipaddr%.*}
640152441Sbrooks		_iplow=${_range%-*}
641152441Sbrooks		_iphigh=${_range#*-}
642152441Sbrooks
643152441Sbrooks		# clear netmask when removing aliases
644152441Sbrooks		if [ "${_action}" = "-alias" ]; then
645152441Sbrooks			_netmask=""
646152441Sbrooks		fi
647212578Shrs
648152441Sbrooks		_ipcount=${_iplow}
649152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
650152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
651152441Sbrooks			_ipcount=$((${_ipcount}+1))
652152441Sbrooks			_ret=0
653152441Sbrooks
654152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
655152441Sbrooks			if [ "${_action}" != "-alias" ]; then
656152441Sbrooks				_netmask="/32"
657152441Sbrooks			fi
658152441Sbrooks		done
659152441Sbrooks	done
660197139Shrs
661152441Sbrooks	return $_ret
662152441Sbrooks}
663152441Sbrooks
664197139Shrs# ifalias_up if af
665113674Smtm#	Configure aliases for network interface $if.
666113674Smtm#	It returns 0 if at least one alias was configured or
667113674Smtm#	1 if there were none.
668113674Smtm#
669113674Smtmifalias_up()
670113674Smtm{
671197139Shrs	local _ret
672113674Smtm	_ret=1
673197139Shrs
674197139Shrs	case "$2" in
675197139Shrs	inet)
676197139Shrs		_ret=`ifalias_ipv4_up "$1"`
677197139Shrs		;;
678197139Shrs	inet6)
679197139Shrs		_ret=`ifalias_ipv6_up "$1"`
680197139Shrs		;;
681197139Shrs	esac
682197139Shrs
683197139Shrs	return $_ret
684197139Shrs}
685197139Shrs
686197139Shrs# ifalias_ipv4_up if
687197139Shrs#	Helper function for ifalias_up().  Handles IPv4.
688197139Shrs#
689197139Shrsifalias_ipv4_up()
690197139Shrs{
691197139Shrs	local _ret alias ifconfig_args
692197139Shrs	_ret=1
693197139Shrs
694197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
695113674Smtm	alias=0
696113674Smtm	while : ; do
697157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
698197139Shrs		case "${ifconfig_args}" in
699197139Shrs		inet\ *)
700197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
701197139Shrs			;;
702197139Shrs		"")
703197139Shrs			break
704197139Shrs			;;
705197139Shrs		esac
706197147Shrs		alias=$((${alias} + 1))
707197139Shrs	done
708197139Shrs
709197139Shrs	return $_ret
710197139Shrs}
711197139Shrs
712197139Shrs# ifalias_ipv6_up if
713197139Shrs#	Helper function for ifalias_up().  Handles IPv6.
714197139Shrs#
715197139Shrsifalias_ipv6_up()
716197139Shrs{
717197139Shrs	local _ret alias ifconfig_args
718197139Shrs	_ret=1
719197139Shrs
720197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
721197139Shrs	alias=0
722197139Shrs	while : ; do
723197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
724197139Shrs		case "${ifconfig_args}" in
725197139Shrs		inet6\ *)
726197139Shrs			ifconfig $1 ${ifconfig_args} alias && _ret=0
727197139Shrs			;;
728197139Shrs		"")
729113674Smtm			break
730197139Shrs			;;
731197139Shrs		esac
732197139Shrs		alias=$((${alias} + 1))
733113674Smtm	done
734197139Shrs
735197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
736197139Shrs	alias=0
737197139Shrs	while : ; do
738197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
739197139Shrs		case "${ifconfig_args}" in
740197139Shrs		"")
741197139Shrs			break
742197139Shrs			;;
743197139Shrs		*)
744197139Shrs			ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
745197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
746197139Shrs			    "  Use ifconfig_$1_aliasN instead."
747197139Shrs			;;
748197139Shrs		esac
749197139Shrs		alias=$((${alias} + 1))
750197139Shrs	done
751197139Shrs
752113674Smtm	return $_ret
753113674Smtm}
754100280Sgordon
755197147Shrs# ifalias_down if af
756116029Smtm#	Remove aliases for network interface $if.
757116029Smtm#	It returns 0 if at least one alias was removed or
758116029Smtm#	1 if there were none.
759116029Smtm#
760116029Smtmifalias_down()
761116029Smtm{
762197139Shrs	local _ret
763116029Smtm	_ret=1
764197139Shrs
765197139Shrs	case "$2" in
766197139Shrs	inet)
767197139Shrs		_ret=`ifalias_ipv4_down "$1"`
768197139Shrs		;;
769197139Shrs	inet6)
770197139Shrs		_ret=`ifalias_ipv6_down "$1"`
771197139Shrs		;;
772197139Shrs	esac
773197139Shrs
774197139Shrs	return $_ret
775197139Shrs}
776197139Shrs
777197147Shrs# ifalias_ipv4_down if
778197139Shrs#	Helper function for ifalias_down().  Handles IPv4.
779197139Shrs#
780197139Shrsifalias_ipv4_down()
781197139Shrs{
782197139Shrs	local _ret alias ifconfig_args
783197139Shrs	_ret=1
784197139Shrs
785197139Shrs	# ifconfig_IF_aliasN which starts with "inet"
786116029Smtm	alias=0
787116029Smtm	while : ; do
788157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
789197139Shrs		case "${ifconfig_args}" in
790197139Shrs		inet\ *)
791197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
792197139Shrs			;;
793197139Shrs		"")
794197139Shrs			break
795197139Shrs			;;
796197139Shrs		esac
797197147Shrs		alias=$((${alias} + 1))
798197139Shrs	done
799197139Shrs
800197139Shrs	return $_ret
801197139Shrs}
802197139Shrs
803197147Shrs# ifalias_ipv6_down if
804197139Shrs#	Helper function for ifalias_down().  Handles IPv6.
805197139Shrs#
806197139Shrsifalias_ipv6_down()
807197139Shrs{
808197139Shrs	local _ret alias ifconfig_args
809197139Shrs	_ret=1
810197139Shrs
811197139Shrs	# ifconfig_IF_aliasN which starts with "inet6"
812197139Shrs	alias=0
813197139Shrs	while : ; do
814197139Shrs		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
815197139Shrs		case "${ifconfig_args}" in
816197139Shrs		inet6\ *)
817197139Shrs			ifconfig $1 ${ifconfig_args} -alias && _ret=0
818197139Shrs			;;
819197139Shrs		"")
820116029Smtm			break
821197139Shrs			;;
822197139Shrs		esac
823197139Shrs		alias=$((${alias} + 1))
824116029Smtm	done
825197139Shrs
826197139Shrs	# backward compatibility: ipv6_ifconfig_IF_aliasN.
827197526Shrs	alias=0
828197139Shrs	while : ; do
829197139Shrs		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
830197139Shrs		case "${ifconfig_args}" in
831197139Shrs		"")
832197139Shrs			break
833197139Shrs			;;
834197139Shrs		*)
835197526Shrs			ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
836197526Shrs			warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
837197139Shrs			    "  Use ifconfig_$1_aliasN instead."
838197139Shrs			;;
839197139Shrs		esac
840197526Shrs		alias=$((${alias} + 1))
841197139Shrs	done
842197139Shrs
843116029Smtm	return $_ret
844116029Smtm}
845116029Smtm
846197139Shrs# ipv6_prefix_hostid_addr_up if
847197139Shrs#	add IPv6 prefix + hostid addr to the interface $if
848197139Shrsipv6_prefix_hostid_addr_up()
849197139Shrs{
850197139Shrs	local _if prefix laddr hostid j address
851197139Shrs	_if=$1
852197139Shrs	prefix=`get_if_var ${_if} ipv6_prefix_IF`
853197139Shrs
854197139Shrs	if [ -n "${prefix}" ]; then
855197139Shrs		laddr=`network6_getladdr ${_if}`
856197139Shrs		hostid=${laddr#fe80::}
857197139Shrs		hostid=${hostid%\%*}
858197139Shrs
859197139Shrs		for j in ${prefix}; do
860197139Shrs			address=$j\:${hostid}
861197139Shrs			ifconfig ${_if} inet6 ${address} prefixlen 64 alias
862197139Shrs
863197139Shrs			# if I am a router, add subnet router
864197139Shrs			# anycast address (RFC 2373).
865197139Shrs			if checkyesno ipv6_gateway_enable; then
866197139Shrs				ifconfig ${_if} inet6 $j:: prefixlen 64 \
867197139Shrs					alias anycast
868197139Shrs			fi
869197139Shrs		done
870197139Shrs	fi
871197139Shrs}
872197139Shrs
873197139Shrs# ipv6_accept_rtadv_up if
874197139Shrs#	Enable accepting Router Advertisement and send Router
875197139Shrs#	Solicitation message
876197139Shrsipv6_accept_rtadv_up()
877197139Shrs{
878197139Shrs	if ipv6_autoconfif $1; then
879197139Shrs		ifconfig $1 inet6 accept_rtadv up
880203433Sume		if ! checkyesno rtsold_enable; then
881203433Sume			rtsol ${rtsol_flags} $1
882203433Sume		fi
883197139Shrs	fi
884197139Shrs}
885197139Shrs
886197139Shrs# ipv6_accept_rtadv_down if
887197139Shrs#	Disable accepting Router Advertisement
888197139Shrsipv6_accept_rtadv_down()
889197139Shrs{
890197139Shrs	if ipv6_autoconfif $1; then
891197139Shrs		ifconfig $1 inet6 -accept_rtadv
892197139Shrs	fi
893197139Shrs}
894197139Shrs
895113674Smtm# ifscript_up if
896113674Smtm#	Evaluate a startup script for the $if interface.
897113674Smtm#	It returns 0 if a script was found and processed or
898113674Smtm#	1 if no script was found.
899113674Smtm#
900113674Smtmifscript_up()
901100280Sgordon{
902113674Smtm	if [ -r /etc/start_if.$1 ]; then
903113674Smtm		. /etc/start_if.$1
904113674Smtm		return 0
905197139Shrs	else
906197139Shrs		return 1
907113674Smtm	fi
908100280Sgordon}
909100280Sgordon
910116029Smtm# ifscript_down if
911116029Smtm#	Evaluate a shutdown script for the $if interface.
912116029Smtm#	It returns 0 if a script was found and processed or
913116029Smtm#	1 if no script was found.
914116029Smtm#
915116029Smtmifscript_down()
916116029Smtm{
917116029Smtm	if [ -r /etc/stop_if.$1 ]; then
918116029Smtm		. /etc/stop_if.$1
919116029Smtm		return 0
920197139Shrs	else
921197139Shrs		return 1
922116029Smtm	fi
923116029Smtm}
924116029Smtm
925197147Shrs# clone_up
926197147Shrs#	Create cloneable interfaces.
927113674Smtm#
928113674Smtmclone_up()
929100280Sgordon{
930197139Shrs	local _prefix _list ifn
931113674Smtm	_prefix=
932113674Smtm	_list=
933197139Shrs
934197139Shrs	# create_args_IF
935113674Smtm	for ifn in ${cloned_interfaces}; do
936178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
937116774Skuriyama		if [ $? -eq 0 ]; then
938113674Smtm			_list="${_list}${_prefix}${ifn}"
939113674Smtm			[ -z "$_prefix" ] && _prefix=' '
940113674Smtm		fi
941113674Smtm	done
942113674Smtm	debug "Cloned: ${_list}"
943113674Smtm}
944100280Sgordon
945197147Shrs# clone_down
946197147Shrs#	Destroy cloned interfaces. Destroyed interfaces are echoed to
947197147Shrs#	standard output.
948113674Smtm#
949113674Smtmclone_down()
950113674Smtm{
951197139Shrs	local _prefix _list ifn
952113674Smtm	_prefix=
953113674Smtm	_list=
954197139Shrs
955113674Smtm	for ifn in ${cloned_interfaces}; do
956208213Sjhb		ifconfig -n ${ifn} destroy
957116774Skuriyama		if [ $? -eq 0 ]; then
958113674Smtm			_list="${_list}${_prefix}${ifn}"
959113674Smtm			[ -z "$_prefix" ] && _prefix=' '
960113674Smtm		fi
961113674Smtm	done
962113674Smtm	debug "Destroyed clones: ${_list}"
963100280Sgordon}
964100280Sgordon
965197147Shrs# childif_create
966197147Shrs#	Create and configure child interfaces.  Return 0 if child
967197147Shrs#	interfaces are created.
968178356Ssam#
969178356Ssamchildif_create()
970178356Ssam{
971201215Sjhb	local cfg child child_vlans child_wlans create_args debug_flags ifn i
972178356Ssam	cfg=1
973178356Ssam	ifn=$1
974178356Ssam
975178527Sbrooks	# Create wireless interfaces
976178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
977178527Sbrooks
978178527Sbrooks	for child in ${child_wlans}; do
979183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
980189759Sbrooks		debug_flags="`get_if_var $child wlandebug_IF`"
981189759Sbrooks
982178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
983178356Ssam			ifconfig $child create ${create_args} && cfg=0
984189759Sbrooks			if [ -n "${debug_flags}" ]; then
985189759Sbrooks				wlandebug -i $child ${debug_flags}
986189759Sbrooks			fi
987178356Ssam		else
988178356Ssam			i=`ifconfig wlan create ${create_args}`
989189759Sbrooks			if [ -n "${debug_flags}" ]; then
990189759Sbrooks				wlandebug -i $i ${debug_flags}
991189759Sbrooks			fi
992178356Ssam			ifconfig $i name $child && cfg=0
993178356Ssam		fi
994188118Sthompsa		if autoif $child; then
995188118Sthompsa			ifn_start $child
996188118Sthompsa		fi
997178356Ssam	done
998178356Ssam
999201215Sjhb	# Create vlan interfaces
1000201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1001201215Sjhb
1002201215Sjhb	if [ -n "${child_vlans}" ]; then
1003201215Sjhb		load_kld if_vlan
1004201215Sjhb	fi
1005201215Sjhb
1006201215Sjhb	for child in ${child_vlans}; do
1007201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1008201215Sjhb			child="${ifn}.${child}"
1009201215Sjhb			create_args=`get_if_var $child create_args_IF`
1010201215Sjhb			ifconfig $child create ${create_args} && cfg=0
1011201215Sjhb		else
1012201215Sjhb			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1013201215Sjhb			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1014201215Sjhb				ifconfig $child create ${create_args} && cfg=0
1015201215Sjhb			else
1016201215Sjhb				i=`ifconfig vlan create ${create_args}`
1017201215Sjhb				ifconfig $i name $child && cfg=0
1018201215Sjhb			fi
1019201215Sjhb		fi
1020201215Sjhb		if autoif $child; then
1021201215Sjhb			ifn_start $child
1022201215Sjhb		fi
1023201215Sjhb	done
1024201215Sjhb
1025179001Sbrooks	return ${cfg}
1026178356Ssam}
1027178356Ssam
1028197147Shrs# childif_destroy
1029197147Shrs#	Destroy child interfaces.
1030178356Ssam#
1031178356Ssamchildif_destroy()
1032178356Ssam{
1033201215Sjhb	local cfg child child_vlans child_wlans ifn
1034197139Shrs	cfg=1
1035178356Ssam
1036201216Sjhb	child_wlans=`get_if_var $ifn wlans_IF`
1037178527Sbrooks	for child in ${child_wlans}; do
1038201215Sjhb		if ! ifexists $child; then
1039201215Sjhb			continue
1040201215Sjhb		fi
1041208213Sjhb		ifconfig -n $child destroy && cfg=0
1042178356Ssam	done
1043197139Shrs
1044201215Sjhb	child_vlans=`get_if_var $ifn vlans_IF`
1045201215Sjhb	for child in ${child_vlans}; do
1046201215Sjhb		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1047201215Sjhb			child="${ifn}.${child}"
1048201215Sjhb		fi
1049201215Sjhb		if ! ifexists $child; then
1050201215Sjhb			continue
1051201215Sjhb		fi
1052208213Sjhb		ifconfig -n $child destroy && cfg=0
1053201215Sjhb	done
1054201215Sjhb
1055197139Shrs	return ${cfg}
1056178356Ssam}
1057178356Ssam
1058197147Shrs# ng_mkpeer
1059197147Shrs#	Create netgraph nodes.
1060166583Sflz#
1061197147Shrsng_mkpeer()
1062197147Shrs{
1063166583Sflz	ngctl -f - 2> /dev/null <<EOF
1064166583Sflzmkpeer $*
1065166583Sflzmsg dummy nodeinfo
1066166583SflzEOF
1067166583Sflz}
1068166583Sflz
1069197147Shrs# ng_create_one
1070197147Shrs#	Create netgraph nodes.
1071197147Shrs#
1072197147Shrsng_create_one()
1073197147Shrs{
1074197139Shrs	local t
1075197139Shrs
1076166583Sflz	ng_mkpeer $* | while read line; do
1077166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1078166583Sflz		if [ -n "${t}" ]; then
1079166583Sflz			echo ${t}
1080166583Sflz			return
1081166583Sflz		fi
1082166583Sflz	done
1083166583Sflz}
1084166583Sflz
1085197147Shrs# gif_up
1086197147Shrs#	Create gif(4) tunnel interfaces.
1087197147Shrsgif_up()
1088197147Shrs{
1089197139Shrs	local i peers
1090197139Shrs
1091166583Sflz	for i in ${gif_interfaces}; do
1092166583Sflz		peers=`get_if_var $i gifconfig_IF`
1093166583Sflz		case ${peers} in
1094166583Sflz		'')
1095166583Sflz			continue
1096166583Sflz			;;
1097166583Sflz		*)
1098177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1099177682Sbrooks				ifconfig $i create >/dev/null 2>&1
1100177682Sbrooks			else
1101177682Sbrooks				gif=`ifconfig gif create`
1102177682Sbrooks				ifconfig $gif name $i
1103177682Sbrooks			fi
1104166583Sflz			ifconfig $i tunnel ${peers}
1105166583Sflz			ifconfig $i up
1106166583Sflz			;;
1107166583Sflz		esac
1108166583Sflz	done
1109166583Sflz}
1110166583Sflz
1111166583Sflz# ng_fec_create ifn
1112197147Shrs#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1113197147Shrs#	FEC arguments were found and configured; returns !0 otherwise.
1114197139Shrsng_fec_create()
1115197139Shrs{
1116166583Sflz	 local req_iface iface bogus
1117166583Sflz	 req_iface="$1"
1118166583Sflz
1119166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1120166583Sflz
1121166583Sflz	 bogus=""
1122166583Sflz	 while true; do
1123166583Sflz		 iface=`ng_create_one fec dummy fec`
1124166583Sflz		 if [ -z "${iface}" ]; then
1125166583Sflz			 exit 2
1126166583Sflz		 fi
1127166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
1128166583Sflz			 break
1129166583Sflz		 fi
1130166583Sflz		 bogus="${bogus} ${iface}"
1131166583Sflz	 done
1132166583Sflz
1133166583Sflz	 for iface in ${bogus}; do
1134166583Sflz		 ngctl shutdown ${iface}:
1135166583Sflz	 done
1136166583Sflz}
1137166583Sflz
1138197147Shrs# fec_up
1139197147Shrs#	Create Fast EtherChannel interfaces.
1140197147Shrsfec_up()
1141197147Shrs{
1142197139Shrs	local i j
1143197139Shrs
1144166583Sflz	for i in ${fec_interfaces}; do
1145166583Sflz		ng_fec_create $i
1146166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
1147166583Sflz			case ${j} in
1148100282Sdougb			'')
1149100282Sdougb				continue
1150100282Sdougb				;;
1151100282Sdougb			*)
1152166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
1153100282Sdougb				;;
1154100282Sdougb			esac
1155100282Sdougb		done
1156166583Sflz	done
1157100282Sdougb}
1158100282Sdougb
1159113674Smtm# ipx_up ifn
1160197147Shrs#	Configure any IPX addresses for interface $ifn. Returns 0 if
1161197147Shrs#	IPX arguments were found and configured; returns 1 otherwise.
1162113674Smtm#
1163113674Smtmipx_up()
1164100280Sgordon{
1165197139Shrs	local ifn
1166113674Smtm	ifn="$1"
1167197139Shrs
1168197139Shrs	# ifconfig_IF_ipx
1169197139Shrs	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1170113674Smtm	if [ -n "${ifconfig_args}" ]; then
1171113674Smtm		ifconfig ${ifn} ${ifconfig_args}
1172113674Smtm		return 0
117385831Sdes	fi
1174197139Shrs
1175113674Smtm	return 1
1176113674Smtm}
117785831Sdes
1178116029Smtm# ipx_down ifn
1179116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1180116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
1181113674Smtm#
1182116029Smtmipx_down()
1183116029Smtm{
1184197139Shrs	local _if _ifs _ret ipxList oldifs _ipx
1185197139Shrs	_if=$1
1186116100Smtm	_ifs="^"
1187116100Smtm	_ret=1
1188197139Shrs	ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1189197139Shrs	oldifs="$IFS"
1190116100Smtm
1191116100Smtm	IFS="$_ifs"
1192116100Smtm	for _ipx in $ipxList ; do
1193116100Smtm		# get rid of extraneous line
1194116100Smtm		[ -z "$_ipx" ] && break
1195116100Smtm
1196116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1197116100Smtm
1198116100Smtm		IFS="$oldifs"
1199197139Shrs		ifconfig ${_if} ${_ipx} delete
1200116100Smtm		IFS="$_ifs"
1201116100Smtm		_ret=0
1202116100Smtm	done
1203116100Smtm	IFS="$oldifs"
1204116100Smtm
1205116100Smtm	return $_ret
1206116029Smtm}
1207116029Smtm
1208137070Spjd# ifnet_rename
1209137070Spjd#	Rename all requested interfaces.
1210116029Smtm#
1211137070Spjdifnet_rename()
1212137070Spjd{
1213197139Shrs	local _if _ifname
1214137070Spjd
1215197139Shrs	# ifconfig_IF_name
1216197139Shrs	for _if in `ifconfig -l`; do
1217157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
1218137070Spjd		if [ ! -z "$_ifname" ]; then
1219137070Spjd			ifconfig $_if name $_ifname
1220137070Spjd		fi
1221137070Spjd	done
1222197139Shrs
1223137070Spjd	return 0
1224137070Spjd}
1225137070Spjd
1226113674Smtm# list_net_interfaces type
1227113674Smtm#	List all network interfaces. The type of interface returned
1228113674Smtm#	can be controlled by the type argument. The type
1229113674Smtm#	argument can be any of the following:
1230197147Shrs#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1231197147Shrs#		dhcp	- list only DHCP configured interfaces
1232197139Shrs#		noautoconf	- all interfaces, excluding IPv6 Stateless
1233197139Shrs#				  Address Autoconf configured interfaces
1234197139Shrs#		autoconf	- list only IPv6 Stateless Address Autoconf
1235197139Shrs#				  configured interfaces
1236113674Smtm#	If no argument is specified all network interfaces are output.
1237134429Syar#	Note that the list will include cloned interfaces if applicable.
1238134429Syar#	Cloned interfaces must already exist to have a chance to appear
1239134429Syar#	in the list if ${network_interfaces} is set to `auto'.
1240113674Smtm#
1241113674Smtmlist_net_interfaces()
1242113674Smtm{
1243197139Shrs	local type _tmplist _list _autolist _lo _if
1244113674Smtm	type=$1
124565532Snectar
1246149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
124751231Ssheldonh	#
1248197139Shrs	_tmplist=
124951231Ssheldonh	case ${network_interfaces} in
125051231Ssheldonh	[Aa][Uu][Tt][Oo])
1251149401Sbrooks		_autolist="`ifconfig -l`"
1252149726Sbrooks		_lo=
1253149401Sbrooks		for _if in ${_autolist} ; do
1254149401Sbrooks			if autoif $_if; then
1255149726Sbrooks				if [ "$_if" = "lo0" ]; then
1256149726Sbrooks					_lo="lo0 "
1257149726Sbrooks				else
1258197139Shrs					_tmplist="${_tmplist} ${_if}"
1259149726Sbrooks				fi
1260149401Sbrooks			fi
1261149401Sbrooks		done
1262197139Shrs		_tmplist="${_lo}${_tmplist# }"
126351231Ssheldonh		;;
126483677Sbrooks	*)
1265149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
1266196478Sdougb
1267196478Sdougb		# lo0 is effectively mandatory, so help prevent foot-shooting
1268196478Sdougb		#
1269196478Sdougb		case "$_tmplist" in
1270196523Sdougb		lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1271196478Sdougb		*)	_tmplist="lo0 ${_tmplist}" ;;
1272196478Sdougb		esac
127383677Sbrooks		;;
127451231Ssheldonh	esac
127549122Sbrian
1276197139Shrs	_list=
1277197139Shrs	case "$type" in
1278197139Shrs	nodhcp)
1279197139Shrs		for _if in ${_tmplist} ; do
1280197139Shrs			if ! dhcpif $_if && \
1281197139Shrs			   [ -n "`_ifconfig_getargs $_if`" ]; then
1282197139Shrs				_list="${_list# } ${_if}"
1283197147Shrs			fi
1284197139Shrs		done
1285197139Shrs		;;
1286197139Shrs	dhcp)
1287197147Shrs		for _if in ${_tmplist} ; do
1288197147Shrs			if dhcpif $_if; then
1289197139Shrs				_list="${_list# } ${_if}"
1290197147Shrs			fi
1291197147Shrs		done
1292113674Smtm		;;
1293197139Shrs	noautoconf)
1294197139Shrs		for _if in ${_tmplist} ; do
1295197139Shrs			if ! ipv6_autoconfif $_if && \
1296197139Shrs			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1297197139Shrs				_list="${_list# } ${_if}"
1298197139Shrs			fi
1299197139Shrs		done
1300113674Smtm		;;
1301197139Shrs	autoconf)
1302197139Shrs		for _if in ${_tmplist} ; do
1303197139Shrs			if ipv6_autoconfif $_if; then
1304197139Shrs				_list="${_list# } ${_if}"
1305197139Shrs			fi
1306197139Shrs		done
1307197139Shrs		;;
1308197139Shrs	*)
1309197139Shrs		_list=${_tmplist}
1310197139Shrs		;;
1311113674Smtm	esac
1312197139Shrs
1313197139Shrs	echo $_list
1314197139Shrs
1315130151Sschweikh	return 0
131625184Sjkh}
1317114942Sume
1318179003Sbrooks# get_default_if -address_family
1319179003Sbrooks#	Get the interface of the default route for the given address family.
1320179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
1321179003Sbrooks#
1322179003Sbrooksget_default_if()
1323179003Sbrooks{
1324197139Shrs	local routeget oldifs defif line
1325197139Shrs	defif=
1326179003Sbrooks	oldifs="$IFS"
1327179003Sbrooks	IFS="
1328179003Sbrooks"
1329197139Shrs	for line in `route -n get $1 default 2>/dev/null`; do
1330179003Sbrooks		case $line in
1331179003Sbrooks		*interface:*)
1332179003Sbrooks			defif=${line##*: }
1333179003Sbrooks			;;
1334179003Sbrooks		esac
1335179003Sbrooks	done
1336179003Sbrooks	IFS=${oldifs}
1337179003Sbrooks
1338179003Sbrooks	echo $defif
1339179003Sbrooks}
1340179003Sbrooks
1341197147Shrs# hexdigit arg
1342197147Shrs#	Echo decimal number $arg (single digit) in hexadecimal format.
1343114942Sumehexdigit()
1344114942Sume{
1345221884Sjilles	printf '%x\n' "$1"
1346114942Sume}
1347114942Sume
1348197147Shrs# hexprint arg
1349197147Shrs#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1350114942Sumehexprint()
1351114942Sume{
1352221884Sjilles	printf '%x\n' "$1"
1353114942Sume}
1354114942Sume
1355196436Sdougbis_wired_interface()
1356196436Sdougb{
1357196436Sdougb	local media
1358196436Sdougb
1359196436Sdougb	case `ifconfig $1 2>/dev/null` in
1360196436Sdougb	*media:?Ethernet*) media=Ethernet ;;
1361196436Sdougb	esac
1362196436Sdougb
1363196436Sdougb	test "$media" = "Ethernet"
1364196436Sdougb}
1365196436Sdougb
1366197147Shrs# network6_getladdr if [flag]
1367197147Shrs#	Echo link-local address from $if if any.
1368197147Shrs#	If flag is defined, tentative ones will be excluded.
1369114942Sumenetwork6_getladdr()
1370114942Sume{
1371197139Shrs	local proto addr rest
1372114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1373114942Sume		case ${proto} in
1374114942Sume		inet6)
1375114942Sume			case ${addr} in
1376114942Sume			fe80::*)
1377114942Sume				if [ -z "$2" ]; then
1378114942Sume					echo ${addr}
1379114942Sume					return
1380114942Sume				fi
1381114942Sume				case ${rest} in
1382114942Sume				*tentative*)
1383114942Sume					continue
1384114942Sume					;;
1385114942Sume				*)
1386114942Sume					echo ${addr}
1387114942Sume					return
1388114942Sume				esac
1389114942Sume			esac
1390114942Sume		esac
1391114942Sume	done
1392114942Sume}
1393