xref: /trueos/etc/network.subr (revision 17d83a70d11062ccf00ec19e142b61af05794ef2)
1#
2# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD$
26#
27IFCONFIG_CMD="/sbin/ifconfig"
28: ${netif_ipexpand_max:=2048}
29
30#
31# Subroutines commonly used from network startup scripts.
32# Requires that rc.conf be loaded first.
33#
34
35# ifn_start ifn
36#	Bring up and configure an interface.  If some configuration is
37#	applied, print the interface configuration.
38#
39ifn_start()
40{
41	local ifn cfg
42	ifn="$1"
43	cfg=1
44
45	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
46
47	ifscript_up ${ifn} && cfg=0
48	ifconfig_up ${ifn} && cfg=0
49	if ! noafif $ifn; then
50		afexists inet && ipv4_up ${ifn} && cfg=0
51		afexists inet6 && ipv6_up ${ifn} && cfg=0
52		afexists ipx && ipx_up ${ifn} && cfg=0
53	fi
54	childif_create ${ifn} && cfg=0
55
56	return $cfg
57}
58
59# ifn_stop ifn
60#	Shutdown and de-configure an interface.  If action is taken,
61#	print the interface name.
62#
63ifn_stop()
64{
65	local ifn cfg
66	ifn="$1"
67	cfg=1
68
69	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
70
71	if ! noafif $ifn; then
72		afexists ipx && ipx_down ${ifn} && cfg=0
73		afexists inet6 && ipv6_down ${ifn} && cfg=0
74		afexists inet && ipv4_down ${ifn} && cfg=0
75	fi
76	ifconfig_down ${ifn} && cfg=0
77	ifscript_down ${ifn} && cfg=0
78	childif_destroy ${ifn} && cfg=0
79
80	return $cfg
81}
82
83# ifn_vnetup ifn
84#	Move ifn to the specified vnet jail.
85#
86ifn_vnetup()
87{
88
89	ifn_vnet0 $1 vnet
90}
91
92# ifn_vnetdown ifn
93#	Reclaim ifn from the specified vnet jail.
94#
95ifn_vnetdown()
96{
97
98	ifn_vnet0 $1 -vnet
99}
100
101# ifn_vnet0 ifn action
102#	Helper function for ifn_vnetup and ifn_vnetdown.
103#
104ifn_vnet0()
105{
106	local _ifn _cfg _action _vnet
107	_ifn="$1"
108	_action="$2"
109	_cfg=1
110
111	if _vnet=$(vnetif $_ifn); then
112		${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
113	fi
114
115	return $_cfg
116}
117
118# ifconfig_up if
119#	Evaluate ifconfig(8) arguments for interface $if and
120#	run ifconfig(8) with those arguments. It returns 0 if
121#	arguments were found and executed or 1 if the interface
122#	had no arguments.  Pseudo arguments DHCP and WPA are handled
123#	here.
124#
125ifconfig_up()
126{
127	local _cfg _ipv6_opts ifconfig_args
128	_cfg=1
129
130	# Make sure lo0 always comes up.
131	if [ "$1" = "lo0" ]; then
132		_cfg=0
133	fi
134
135	# inet6 specific
136	if ! noafif $1 && afexists inet6; then
137		if checkyesno ipv6_activate_all_interfaces; then
138			_ipv6_opts="-ifdisabled"
139		elif [ "$1" != "lo0" ]; then
140			_ipv6_opts="ifdisabled"
141		fi
142
143		# backward compatibility: $ipv6_enable
144		case $ipv6_enable in
145		[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
146			case $1 in
147			bridge[0-9]*)
148				# No accept_rtadv by default on if_bridge(4)
149				# to avoid a conflict with the member
150				# interfaces.
151			;;
152			*)
153				if ! checkyesno ipv6_gateway_enable; then
154					_ipv6_opts="${_ipv6_opts} accept_rtadv"
155				fi
156			;;
157			esac
158		;;
159		esac
160
161		case $ipv6_cpe_wanif in
162		$1)
163			_ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
164		;;
165		esac
166
167		if [ -n "${_ipv6_opts}" ]; then
168			${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
169		fi
170	fi
171
172	# ifconfig_IF
173	ifconfig_args=`ifconfig_getargs $1`
174	if [ -n "${ifconfig_args}" ]; then
175		eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
176		_cfg=0
177	fi
178
179	# inet6 specific
180	if ! noafif $1 && afexists inet6; then
181		# ifconfig_IF_ipv6
182		ifconfig_args=`ifconfig_getargs $1 ipv6`
183		if [ -n "${ifconfig_args}" ]; then
184			# backward compatibility: inet6 keyword
185			case "${ifconfig_args}" in
186			:*|[0-9a-fA-F]*:*)
187				warn "\$ifconfig_$1_ipv6 needs leading" \
188				    "\"inet6\" keyword for an IPv6 address."
189				ifconfig_args="inet6 ${ifconfig_args}"
190			;;
191			esac
192			${IFCONFIG_CMD} $1 inet6 -ifdisabled
193			eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
194			_cfg=0
195		fi
196
197		# $ipv6_prefix_IF will be handled in
198		# ipv6_prefix_hostid_addr_common().
199		ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
200		if [ -n "${ifconfig_args}" ]; then
201			${IFCONFIG_CMD} $1 inet6 -ifdisabled
202			_cfg=0
203		fi
204
205		# backward compatibility: $ipv6_ifconfig_IF
206		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
207		if [ -n "${ifconfig_args}" ]; then
208			warn "\$ipv6_ifconfig_$1 is obsolete." \
209			    "  Use ifconfig_$1_ipv6 instead."
210			${IFCONFIG_CMD} $1 inet6 -ifdisabled
211			eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
212			_cfg=0
213		fi
214	fi
215
216	ifalias $1 link alias
217	ifalias $1 ether alias
218
219	if [ ${_cfg} -eq 0 ]; then
220		${IFCONFIG_CMD} $1 up
221	fi
222
223	if wpaif $1; then
224		/etc/rc.d/wpa_supplicant start $1
225		_cfg=0		# XXX: not sure this should count
226	elif hostapif $1; then
227		/etc/rc.d/hostapd start $1
228		_cfg=0
229	fi
230
231	if dhcpif $1; then
232		if [ $_cfg -ne 0 ] ; then
233			${IFCONFIG_CMD} $1 up
234		fi
235		if syncdhcpif $1; then
236			/etc/rc.d/dhclient start $1
237		fi
238		_cfg=0
239	fi
240
241	if [ ${1##lagg} != $1 ]; then
242		checkyesno failover_enable && sleep 60
243	fi
244
245	return $_cfg
246}
247
248# ifconfig_down if
249#	returns 1 if wpa_supplicant or dhclient was stopped or
250#	the interface exists.
251#
252ifconfig_down()
253{
254	local _cfg
255	_cfg=1
256
257	if wpaif $1; then
258		/etc/rc.d/wpa_supplicant stop $1
259		_cfg=0
260	elif hostapif $1; then
261		/etc/rc.d/hostapd stop $1
262		_cfg=0
263	fi
264
265	if dhcpif $1; then
266		/etc/rc.d/dhclient stop $1
267		_cfg=0
268	fi
269
270	if ifexists $1; then
271		${IFCONFIG_CMD} $1 down
272		_cfg=0
273	fi
274
275	return $_cfg
276}
277
278# get_if_var if var [default]
279#	Return the value of the pseudo-hash corresponding to $if where
280#	$var is a string containg the sub-string "IF" which will be
281#	replaced with $if after the characters defined in _punct are
282#	replaced with '_'. If the variable is unset, replace it with
283#	$default if given.
284get_if_var()
285{
286	local _if _punct _punct_c _var _default prefix suffix
287
288	if [ $# -ne 2 -a $# -ne 3 ]; then
289		err 3 'USAGE: get_if_var name var [default]'
290	fi
291
292	_if=$1
293	_punct=".-/+"
294	ltr ${_if} "${_punct}" '_' _if
295	_var=$2
296	_default=$3
297
298	prefix=${_var%%IF*}
299	suffix=${_var##*IF}
300	eval echo \${${prefix}${_if}${suffix}-${_default}}
301}
302
303# _ifconfig_getargs if [af]
304#	Prints the arguments for the supplied interface to stdout.
305#	Returns 1 if empty.  In general, ifconfig_getargs should be used
306#	outside this file.
307_ifconfig_getargs()
308{
309	local _ifn _af
310	_ifn=$1
311	_af=${2+_$2}
312
313	if [ -z "$_ifn" ]; then
314		return 1
315	fi
316
317	get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
318}
319
320# ifconfig_getargs if [af]
321#	Takes the result from _ifconfig_getargs and removes pseudo
322#	args such as DHCP and WPA.
323ifconfig_getargs()
324{
325	local _tmpargs _arg _args _vnet
326	_tmpargs=`_ifconfig_getargs $1 $2`
327	if [ $? -eq 1 ]; then
328		return 1
329	fi
330	_args=
331	_vnet=0
332
333	for _arg in $_tmpargs; do
334		case $_arg:$_vnet in
335		[Dd][Hh][Cc][Pp]:0) ;;
336		[Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
337		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
338		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
339		[Ww][Pp][Aa]:0) ;;
340		[Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
341		vnet:0)	_vnet=1 ;;
342		*:1)	_vnet=0 ;;
343		*:0)
344			_args="$_args $_arg"
345		;;
346		esac
347	done
348
349	echo $_args
350}
351
352# autoif
353#	Returns 0 if the interface should be automatically configured at
354#	boot time and 1 otherwise.
355autoif()
356{
357	local _tmpargs _arg
358	_tmpargs=`_ifconfig_getargs $1`
359
360	for _arg in $_tmpargs; do
361		case $_arg in
362		[Nn][Oo][Aa][Uu][Tt][Oo])
363			return 1
364			;;
365		esac
366	done
367
368	return 0
369}
370
371# dhcpif if
372#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
373dhcpif()
374{
375	local _tmpargs _arg
376	_tmpargs=`_ifconfig_getargs $1`
377
378	case $1 in
379	lo[0-9]*|\
380	stf[0-9]*|\
381	faith[0-9]*|\
382	lp[0-9]*|\
383	sl[0-9]*)
384		return 1
385		;;
386	esac
387	if noafif $1; then
388		return 1
389	fi
390
391	for _arg in $_tmpargs; do
392		case $_arg in
393		[Dd][Hh][Cc][Pp])
394			return 0
395			;;
396		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
397			return 0
398			;;
399		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
400			return 0
401			;;
402		esac
403	done
404
405	return 1
406}
407
408# syncdhcpif
409#	Returns 0 if the interface should be configured synchronously and
410#	1 otherwise.
411syncdhcpif()
412{
413	local _tmpargs _arg
414	_tmpargs=`_ifconfig_getargs $1`
415
416	if noafif $1; then
417		return 1
418	fi
419
420	for _arg in $_tmpargs; do
421		case $_arg in
422		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
423			return 1
424			;;
425		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
426			return 0
427			;;
428		esac
429	done
430
431	checkyesno synchronous_dhclient
432}
433
434# wpaif if
435#	Returns 0 if the interface is a WPA interface and 1 otherwise.
436wpaif()
437{
438	local _tmpargs _arg
439	_tmpargs=`_ifconfig_getargs $1`
440
441	for _arg in $_tmpargs; do
442		case $_arg in
443		[Ww][Pp][Aa])
444			return 0
445			;;
446		esac
447	done
448
449	return 1
450}
451
452# hostapif if
453#	Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
454hostapif()
455{
456	local _tmpargs _arg
457	_tmpargs=`_ifconfig_getargs $1`
458
459	for _arg in $_tmpargs; do
460		case $_arg in
461		[Hh][Oo][Ss][Tt][Aa][Pp])
462			return 0
463			;;
464		esac
465	done
466
467	return 1
468}
469
470# vnetif if
471#	Returns 0 and echo jail if "vnet" keyword is specified on the
472#	interface, and 1 otherwise.
473vnetif()
474{
475	local _tmpargs _arg _vnet
476	_tmpargs=`_ifconfig_getargs $1`
477
478	_vnet=0
479	for _arg in $_tmpargs; do
480		case $_arg:$_vnet in
481		vnet:0)	_vnet=1 ;;
482		*:1)	echo $_arg; return 0 ;;
483		esac
484	done
485
486	return 1
487}
488
489# afexists af
490#	Returns 0 if the address family is enabled in the kernel
491#	1 otherwise.
492afexists()
493{
494	local _af
495	_af=$1
496
497	case ${_af} in
498	inet|inet6)
499		check_kern_features ${_af}
500		;;
501	ipx)
502		${SYSCTL_N} net.ipx > /dev/null 2>&1
503		;;
504	atm)
505		if [ -x /sbin/atmconfig ]; then
506			/sbin/atmconfig diag list > /dev/null 2>&1
507		else
508			return 1
509		fi
510		;;
511	link|ether)
512		return 0
513		;;
514	*)
515		err 1 "afexists(): Unsupported address family: $_af"
516		;;
517	esac
518}
519
520# noafif if
521#	Returns 0 if the interface has no af configuration and 1 otherwise.
522noafif()
523{
524	local _if
525	_if=$1
526
527	case $_if in
528	pflog[0-9]*|\
529	pfsync[0-9]*|\
530	usbus[0-9]*|\
531	an[0-9]*|\
532	ath[0-9]*|\
533	ipw[0-9]*|\
534	ipfw[0-9]*|\
535	iwi[0-9]*|\
536	iwn[0-9]*|\
537	ral[0-9]*|\
538	wi[0-9]*|\
539	wl[0-9]*|\
540	wpi[0-9]*)
541		return 0
542		;;
543	esac
544
545	return 1
546}
547
548# ipv6if if
549#	Returns 0 if the interface should be configured for IPv6 and
550#	1 otherwise.
551ipv6if()
552{
553	local _if _tmpargs i
554	_if=$1
555
556	if ! afexists inet6; then
557		return 1
558	fi
559
560	# lo0 is always IPv6-enabled
561	case $_if in
562	lo0)
563		return 0
564		;;
565	esac
566
567	case "${ipv6_network_interfaces}" in
568	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
569		# True if $ifconfig_IF_ipv6 is defined.
570		_tmpargs=`_ifconfig_getargs $_if ipv6`
571		if [ -n "${_tmpargs}" ]; then
572			return 0
573		fi
574
575		# True if $ipv6_prefix_IF is defined.
576		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
577		if [ -n "${_tmpargs}" ]; then
578			return 0
579		fi
580
581		# backward compatibility: True if $ipv6_ifconfig_IF is defined.
582		_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
583		if [ -n "${_tmpargs}" ]; then
584			return 0
585		fi
586		;;
587	esac
588
589	return 1
590}
591
592# ipv6_autoconfif if
593#	Returns 0 if the interface should be configured for IPv6 with
594#	Stateless Address Configuration; 1 otherwise.
595ipv6_autoconfif()
596{
597	local _if _tmpargs _arg
598	_if=$1
599
600	case $_if in
601	lo[0-9]*|\
602	stf[0-9]*|\
603	faith[0-9]*|\
604	lp[0-9]*|\
605	sl[0-9]*)
606		return 1
607		;;
608	esac
609	if noafif $_if; then
610		return 1
611	fi
612	if ! ipv6if $_if; then
613		return 1
614	fi
615	if checkyesno ipv6_gateway_enable; then
616		return 1
617	fi
618	_tmpargs=`get_if_var $_if ipv6_prefix_IF`
619	if [ -n "${_tmpargs}" ]; then
620		return 1
621	fi
622	# backward compatibility: $ipv6_enable
623	case $ipv6_enable in
624	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
625		if checkyesno ipv6_gateway_enable; then
626			return 1
627		fi
628		case $1 in
629		bridge[0-9]*)
630			# No accept_rtadv by default on if_bridge(4)
631			# to avoid a conflict with the member
632			# interfaces.
633			return 1
634		;;
635		*)
636			return 0
637		;;
638		esac
639	;;
640	esac
641
642	_tmpargs=`_ifconfig_getargs $_if ipv6`
643	for _arg in $_tmpargs; do
644		case $_arg in
645		accept_rtadv)
646			return 0
647			;;
648		esac
649	done
650
651	# backward compatibility: $ipv6_ifconfig_IF
652	_tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
653	for _arg in $_tmpargs; do
654		case $_arg in
655		accept_rtadv)
656			return 0
657			;;
658		esac
659	done
660
661	return 1
662}
663
664# ifexists if
665#	Returns 0 if the interface exists and 1 otherwise.
666ifexists()
667{
668	[ -z "$1" ] && return 1
669	${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
670}
671
672# ipv4_up if
673#	add IPv4 addresses to the interface $if
674ipv4_up()
675{
676	local _if _ret
677	_if=$1
678	_ret=1
679
680	# Add 127.0.0.1/8 to lo0 unless otherwise specified.
681	if [ "${_if}" = "lo0" ]; then
682		ifconfig_args=`get_if_var ${_if} ifconfig_IF`
683		if [ -z "${ifconfig_args}" ]; then
684			${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
685		fi
686	fi
687	ifalias ${_if} inet alias && _ret=0
688
689	return $_ret
690}
691
692# ipv6_up if
693#	add IPv6 addresses to the interface $if
694ipv6_up()
695{
696	local _if _ret
697	_if=$1
698	_ret=1
699
700	if ! ipv6if $_if; then
701		return 0
702	fi
703
704	ifalias ${_if} inet6 alias && _ret=0
705	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
706	ipv6_accept_rtadv_up ${_if} && _ret=0
707
708	return $_ret
709}
710
711# ipv4_down if
712#	remove IPv4 addresses from the interface $if
713ipv4_down()
714{
715	local _if _ifs _ret inetList oldifs _inet
716	_if=$1
717	_ifs="^"
718	_ret=1
719
720	ifalias ${_if} inet -alias && _ret=0
721
722	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
723
724	oldifs="$IFS"
725	IFS="$_ifs"
726	for _inet in $inetList ; do
727		# get rid of extraneous line
728		case $_inet in
729		inet\ *)	;;
730		*)		continue ;;
731		esac
732
733		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
734
735		IFS="$oldifs"
736		${IFCONFIG_CMD} ${_if} ${_inet} delete
737		IFS="$_ifs"
738		_ret=0
739	done
740	IFS="$oldifs"
741
742	return $_ret
743}
744
745# ipv6_down if
746#	remove IPv6 addresses from the interface $if
747ipv6_down()
748{
749	local _if _ifs _ret inetList oldifs _inet6
750	_if=$1
751	_ifs="^"
752	_ret=1
753
754	if ! ipv6if $_if; then
755		return 0
756	fi
757
758	ipv6_accept_rtadv_down ${_if} && _ret=0
759	ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
760	ifalias ${_if} inet6 -alias && _ret=0
761
762	inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
763
764	oldifs="$IFS"
765	IFS="$_ifs"
766	for _inet6 in $inetList ; do
767		# get rid of extraneous line
768		case $_inet6 in
769		inet6\ *)	;;
770		*)		continue ;;
771		esac
772
773		_inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
774
775		IFS="$oldifs"
776		${IFCONFIG_CMD} ${_if} ${_inet6} -alias
777		IFS="$_ifs"
778		_ret=0
779	done
780	IFS="$oldifs"
781
782	return $_ret
783}
784
785# ifalias if af action
786#	Configure or remove aliases for network interface $if.
787#	It returns 0 if at least one alias was configured or
788#	removed, or 1 if there were none.
789#
790ifalias()
791{
792	local _ret
793	_ret=1
794
795	afexists $2 || return $_ret
796
797	case "$2" in
798	inet|inet6|link|ether)
799		ifalias_af_common $1 $2 $3 && _ret=0
800		;;
801	esac
802
803	return $_ret
804}
805
806# ifalias_expand_addr af action addr
807#	Expand address range ("N-M") specification in addr.
808#	"addr" must not include an address-family keyword.
809#	The results will include an address-family keyword.
810#
811ifalias_expand_addr()
812{
813	local _af _action
814
815	_af=$1
816	_action=$2
817	shift 2
818
819	afexists $_af || return
820	ifalias_expand_addr_$_af $_action $*
821}
822
823# ifalias_expand_addr_inet action addr
824#	Helper function for ifalias_expand_addr().  Handles IPv4.
825#
826ifalias_expand_addr_inet()
827{
828	local _action _arg _cidr _cidr_addr _exargs
829	local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
830	local _retstr _c
831	_action=$1
832	_arg=$2
833	shift 2
834	_exargs=$*
835	_retstr=
836
837	case $_action:$_arg:$_exargs in
838	*:*--*)		return ;;	# invalid
839	tmp:*[0-9]-[0-9]*:*)		# to be expanded
840		_action="alias"
841	;;
842	*:*[0-9]-[0-9]*:*)		# to be expanded
843	;;
844	tmp:*:*netmask*)		# already expanded w/ netmask option
845		echo ${_arg%/[0-9]*} $_exargs && return
846	;;
847	tmp:*:*)			# already expanded w/o netmask option
848		echo $_arg $_exargs && return
849	;;
850	*:*:*netmask*)			# already expanded w/ netmask option
851		echo inet ${_arg%/[0-9]*} $_exargs && return
852	;;
853	*:*:*)				# already expanded w/o netmask option
854		echo inet $_arg $_exargs && return
855	;;
856	esac
857
858	for _cidr in $_arg; do
859		_ipaddr=${_cidr%%/*}
860		_plen=${_cidr##*/}
861		# When subnet prefix length is not specified, use /32.
862		case $_plen in
863		$_ipaddr)	_plen=32 ;;	# "/" character not found
864		esac
865
866		OIFS=$IFS
867		IFS=. set -- $_ipaddr
868		_range=
869		_iphead=
870		_iptail=
871		for _c in $@; do
872			case $_range:$_c in
873			:[0-9]*-[0-9]*)
874				_range=$_c
875			;;
876			:*)
877				_iphead="${_iphead}${_iphead:+.}${_c}"
878			;;
879			*:*)
880				_iptail="${_iptail}${_iptail:+.}${_c}"
881			;;
882			esac
883		done
884		IFS=$OIFS
885		_iplow=${_range%-*}
886		_iphigh=${_range#*-}
887
888		# clear netmask when removing aliases
889		if [ "$_action" = "-alias" ]; then
890			_plen=""
891		fi
892
893		_ipcount=$_iplow
894		while [ "$_ipcount" -le "$_iphigh" ]; do
895			_retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
896			if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
897				warn "Range specification is too large (${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_iphigh}${_iptail:+.}${_iptail}).  ${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail} was processed.  Increase \$netif_ipexpand_max in rc.conf."
898				break
899			else
900				_ipcount=$(($_ipcount + 1))
901			fi
902			# Forcibly set /32 for remaining aliases.
903			_plen=32
904		done
905	done
906
907	for _c in $_retstr; do
908		ifalias_expand_addr_inet $_action $_c $_exargs
909	done
910}
911
912# ifalias_expand_addr_inet6 action addr
913#	Helper function for ifalias_expand_addr().  Handles IPv6.
914#
915ifalias_expand_addr_inet6()
916{
917	local _action _arg _cidr _cidr_addr _exargs
918	local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
919	local _ipv4part
920	local _retstr _c
921	_action=$1
922	_arg=$2
923	shift 2
924	_exargs=$*
925	_retstr=
926
927	case $_action:$_arg:$_exargs in
928	*:*--*:*)	return ;;	# invalid
929	tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
930		_action="alias"
931	;;
932	*:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)	# to be expanded
933	;;
934	tmp:*:*prefixlen*)	# already expanded w/ prefixlen option
935		echo ${_arg%/[0-9]*} $_exargs && return
936	;;
937	tmp:*:*)		# already expanded w/o prefixlen option
938		echo $_arg $_exargs && return
939	;;
940	*:*:*prefixlen*)	# already expanded w/ prefixlen option
941		echo inet6 ${_arg%/[0-9]*} $_exargs && return
942	;;
943	*:*:*)			# already expanded w/o prefixlen option
944		echo inet6 $_arg $_exargs && return
945	;;
946	esac
947
948	for _cidr in $_arg; do
949		_ipaddr="${_cidr%%/*}"
950		_plen="${_cidr##*/}"
951
952		case $_action:$_ipaddr:$_cidr in
953		-alias:*:*)		unset _plen ;;
954		*:$_cidr:$_ipaddr)	unset _plen ;;
955		esac
956
957		if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
958			# Handle !v4mapped && !v4compat addresses.
959
960			# The default prefix length is 64.
961			case $_ipaddr:$_cidr in
962			$_cidr:$_ipaddr)	_plen="64" ;;
963			esac
964			_ipleft=${_ipaddr%-*}
965			_ipright=${_ipaddr#*-}
966			_iplow=${_ipleft##*:}
967			_iphigh=${_ipright%%:*}
968			_ipleft=${_ipleft%:*}
969			_ipright=${_ipright#*:}
970
971			if [ "$_iphigh" = "$_ipright" ]; then
972				unset _ipright
973			else
974				_ipright=:$_ipright
975			fi
976
977			if [ -n "$_iplow" -a -n "$_iphigh" ]; then
978				_iplow=$((0x$_iplow))
979				_iphigh=$((0x$_iphigh))
980				_ipcount=$_iplow
981				while [ $_ipcount -le $_iphigh ]; do
982					_r=`printf "%s:%04x%s%s" \
983					    $_ipleft $_ipcount $_ipright \
984					    ${_plen:+/}$_plen`
985					_retstr="$_retstr $_r"
986					if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
987					then
988						warn "Range specification is too large $(printf '(%s:%x%s-%s:%x%s)' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_iphigh" "$_ipright"). $(printf '%s:%x%s-%s:%x%s' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_ipcount" "$_ipright") was processed.  Increase \$netif_ipexpand_max in rc.conf."
989						break
990					else
991						_ipcount=$(($_ipcount + 1))
992					fi
993				done
994			else
995				_retstr="${_ipaddr}${_plen:+/}${_plen}"
996			fi
997
998			for _c in $_retstr; do
999				ifalias_expand_addr_inet6 $_action $_c $_exargs
1000			done
1001		else
1002			# v4mapped/v4compat should handle as an IPv4 alias
1003			_ipv4part=${_ipaddr##*:}
1004
1005			# Adjust prefix length if any.  If not, set the
1006			# default prefix length as 32.
1007			case $_ipaddr:$_cidr in
1008			$_cidr:$_ipaddr)	_plen=32 ;;
1009			*)			_plen=$(($_plen - 96)) ;;
1010			esac
1011
1012			_retstr=`ifalias_expand_addr_inet \
1013			    tmp ${_ipv4part}${_plen:+/}${_plen}`
1014			for _c in $_retstr; do
1015				ifalias_expand_addr_inet $_action $_c $_exargs
1016			done
1017		fi
1018	done
1019}
1020
1021# ifalias_af_common_handler if af action args
1022#	Helper function for ifalias_af_common().
1023#
1024ifalias_af_common_handler()
1025{
1026	local _ret _if _af _action _args _c _tmpargs
1027
1028	_ret=1
1029	_if=$1
1030	_af=$2
1031	_action=$3
1032	shift 3
1033	_args=$*
1034
1035	case $_args in
1036	${_af}\ *)	;;
1037	*)	return	;;
1038	esac
1039
1040	# link(ether) does not support address removal.
1041	case $_af:$_action in
1042	link:-alias|ether:-alias)	return ;;
1043	esac
1044
1045	_tmpargs=
1046	for _c in $_args; do
1047		case $_c in
1048		${_af})
1049			case $_tmpargs in
1050			${_af}\ *[0-9a-fA-F]-*)
1051				ifalias_af_common_handler $_if $_af $_action \
1052				`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1053			;;
1054			${_af}\ *)
1055				${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1056			;;
1057			esac
1058			_tmpargs=$_af
1059		;;
1060		*)
1061			_tmpargs="$_tmpargs $_c"
1062		;;
1063		esac
1064	done
1065	# Process the last component if any.
1066	if [ -n "$_tmpargs}" ]; then
1067		case $_tmpargs in
1068		${_af}\ *[0-9a-fA-F]-*)
1069			ifalias_af_common_handler $_if $_af $_action \
1070			`ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1071		;;
1072		${_af}\ *)
1073			${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1074		;;
1075		esac
1076	fi
1077
1078	return $_ret
1079}
1080
1081# ifalias_af_common if af action
1082#	Helper function for ifalias().
1083#
1084ifalias_af_common()
1085{
1086	local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1087	local _vif _punct=".-/+"
1088
1089	_ret=1
1090	_aliasn=
1091	_if=$1
1092	_af=$2
1093	_action=$3
1094
1095	# Normalize $_if before using it in a pattern to list_vars()
1096	ltr "$_if" "$_punct" "_" _vif
1097
1098	# ifconfig_IF_aliasN which starts with $_af
1099	for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1100		sort_lite -nk1.$((9+${#_vif}+7))`
1101	do
1102		eval ifconfig_args=\"\$$alias\"
1103		_iaf=
1104		case $ifconfig_args in
1105		inet\ *)	_iaf=inet ;;
1106		inet6\ *)	_iaf=inet6 ;;
1107		ipx\ *)		_iaf=ipx ;;
1108		link\ *)	_iaf=link ;;
1109		ether\ *)	_iaf=ether ;;
1110		esac
1111
1112		case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1113		${_af}:*:${_af}:*)
1114			_aliasn="$_aliasn $ifconfig_args"
1115			;;
1116		${_af}:*:"":"")
1117			break
1118			;;
1119		inet:alias:"":*)
1120			_aliasn="$_aliasn inet $ifconfig_args"
1121			warn "\$${alias} needs leading" \
1122			    "\"inet\" keyword for an IPv4 address."
1123		esac
1124	done
1125
1126	# backward compatibility: ipv6_ifconfig_IF_aliasN.
1127	case $_af in
1128	inet6)
1129		for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1130			sort_lite -nk1.$((14+${#_vif}+7))`
1131		do
1132			eval ifconfig_args=\"\$$alias\"
1133			case ${_action}:"${ifconfig_args}" in
1134			*:"")
1135				break
1136			;;
1137			alias:*)
1138				_aliasn="${_aliasn} inet6 ${ifconfig_args}"
1139				warn "\$${alias} is obsolete. " \
1140				    "Use ifconfig_${_vif}_aliasN instead."
1141			;;
1142			esac
1143		done
1144	esac
1145
1146	# backward compatibility: ipv4_addrs_IF.
1147	for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1148		_aliasn="$_aliasn inet $_tmpargs"
1149	done
1150
1151	# Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1152	_tmpargs=
1153	for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1154		case $_c in
1155		inet|inet6|ipx|link|ether)
1156			case $_tmpargs in
1157			${_af}\ *)
1158				eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1159			;;
1160			esac
1161			_tmpargs=$_c
1162		;;
1163		*)
1164			_tmpargs="$_tmpargs $_c"
1165		esac
1166	done
1167	# Process the last component
1168	case $_tmpargs in
1169	${_af}\ *)
1170		ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1171	;;
1172	esac
1173
1174	return $_ret
1175}
1176
1177# ipv6_prefix_hostid_addr_common if action
1178#	Add or remove IPv6 prefix + hostid addr on the interface $if
1179#
1180ipv6_prefix_hostid_addr_common()
1181{
1182	local _if _action prefix j
1183	_if=$1
1184	_action=$2
1185	prefix=`get_if_var ${_if} ipv6_prefix_IF`
1186
1187	if [ -n "${prefix}" ]; then
1188		for j in ${prefix}; do
1189			# The default prefixlen is 64.
1190			plen=${j#*/}
1191			case $j:$plen in
1192			$plen:$j)	plen=64 ;;
1193			*)		j=${j%/*} ;;
1194			esac
1195
1196			# Normalize the last part by removing ":"
1197			j=${j%::*}
1198			j=${j%:}
1199			${IFCONFIG_CMD} ${_if} inet6 $j:: \
1200				prefixlen $plen eui64 ${_action}
1201
1202			# if I am a router, add subnet router
1203			# anycast address (RFC 2373).
1204			if checkyesno ipv6_gateway_enable; then
1205				${IFCONFIG_CMD} ${_if} inet6 $j:: \
1206					prefixlen $plen ${_action} anycast
1207			fi
1208		done
1209	fi
1210}
1211
1212# ipv6_accept_rtadv_up if
1213#	Enable accepting Router Advertisement and send Router
1214#	Solicitation message
1215ipv6_accept_rtadv_up()
1216{
1217	if ipv6_autoconfif $1; then
1218		${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1219		if ! checkyesno rtsold_enable; then
1220			rtsol ${rtsol_flags} $1
1221		fi
1222	fi
1223}
1224
1225# ipv6_accept_rtadv_down if
1226#	Disable accepting Router Advertisement
1227ipv6_accept_rtadv_down()
1228{
1229	if ipv6_autoconfif $1; then
1230		${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1231	fi
1232}
1233
1234# ifscript_up if
1235#	Evaluate a startup script for the $if interface.
1236#	It returns 0 if a script was found and processed or
1237#	1 if no script was found.
1238#
1239ifscript_up()
1240{
1241	if [ -r /etc/start_if.$1 ]; then
1242		. /etc/start_if.$1
1243		return 0
1244	else
1245		return 1
1246	fi
1247}
1248
1249# ifscript_down if
1250#	Evaluate a shutdown script for the $if interface.
1251#	It returns 0 if a script was found and processed or
1252#	1 if no script was found.
1253#
1254ifscript_down()
1255{
1256	if [ -r /etc/stop_if.$1 ]; then
1257		. /etc/stop_if.$1
1258		return 0
1259	else
1260		return 1
1261	fi
1262}
1263
1264# clone_up
1265#	Create cloneable interfaces.
1266#
1267clone_up()
1268{
1269	local _list ifn ifopt _iflist _n tmpargs
1270	_list=
1271	_iflist=$*
1272
1273	# create_args_IF
1274	for ifn in ${cloned_interfaces}; do
1275		# Parse ifn:ifopt.
1276		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1277		case $_iflist in
1278		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1279		*)	continue ;;
1280		esac
1281		case $ifn in
1282		epair[0-9]*)
1283			# epair(4) uses epair[0-9] for creation and
1284			# epair[0-9][ab] for configuration.
1285			#
1286			# Skip if ${ifn}a or ${ifn}b already exist.
1287			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1288				continue
1289			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1290				continue
1291			fi
1292			${IFCONFIG_CMD} ${ifn} create \
1293			    `get_if_var ${ifn} create_args_IF`
1294			if [ $? -eq 0 ]; then
1295				_list="$_list ${ifn}a ${ifn}b"
1296			fi
1297		;;
1298		*)
1299			# Skip if ${ifn} already exists.
1300			if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1301				continue
1302			fi
1303			${IFCONFIG_CMD} ${ifn} create \
1304			    `get_if_var ${ifn} create_args_IF`
1305			if [ $? -eq 0 ]; then
1306				_list="$_list $ifn"
1307			fi
1308		esac
1309	done
1310	if [ -n "$gif_interfaces" ]; then
1311		warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1312	fi
1313	for ifn in ${gif_interfaces}; do
1314		# Parse ifn:ifopt.
1315		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1316		case $_iflist in
1317		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1318		*)	continue ;;
1319		esac
1320		# Skip if ifn already exists.
1321		if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1322			continue
1323		fi
1324		case $ifn in
1325		gif[0-9]*)
1326			${IFCONFIG_CMD} $ifn create
1327		;;
1328		*)
1329			_n=$(${IFCONFIG_CMD} gif create)
1330			${IFCONFIG_CMD} $_n name $ifn
1331		;;
1332		esac
1333		if [ $? -eq 0 ]; then
1334			_list="$_list $ifn"
1335		fi
1336		tmpargs=$(get_if_var $ifn gifconfig_IF)
1337		eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1338	done
1339	if [ -n "${_list# }" ]; then
1340		echo "Created clone interfaces: ${_list# }."
1341	fi
1342	debug "Cloned: ${_list# }"
1343}
1344
1345# clone_down
1346#	Destroy cloned interfaces. Destroyed interfaces are echoed to
1347#	standard output.
1348#
1349clone_down()
1350{
1351	local _list ifn _difn ifopt _iflist _sticky
1352	_list=
1353	_iflist=$*
1354
1355	: ${cloned_interfaces_sticky:=NO}
1356	if checkyesno cloned_interfaces_sticky; then
1357		_sticky=1
1358	else
1359		_sticky=0
1360	fi
1361	for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1362		# Parse ifn:ifopt.
1363		OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1364		case $ifopt:$_sticky in
1365		sticky:*)	continue ;;	# :sticky => not destroy
1366		nosticky:*)	;;		# :nosticky => destroy
1367		*:1)		continue ;;	# global sticky knob == 1
1368		esac
1369		case $_iflist in
1370		""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)	;;
1371		*)	continue ;;
1372		esac
1373		case $ifn in
1374		epair[0-9]*)
1375			# Note: epair(4) uses epair[0-9] for removal and
1376			# epair[0-9][ab] for configuration.
1377			#
1378			# Skip if both of ${ifn}a and ${ifn}b do not exist.
1379			if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1380				_difn=${ifn}a
1381			elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1382				_difn=${ifn}b
1383			else
1384				continue
1385			fi
1386			${IFCONFIG_CMD} -n $_difn destroy
1387			if [ $? -eq 0 ]; then
1388				_list="$_list ${ifn}a ${ifn}b"
1389			fi
1390		;;
1391		*)
1392			# Skip if ifn does not exist.
1393			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1394				continue
1395			fi
1396			${IFCONFIG_CMD} -n ${ifn} destroy
1397			if [ $? -eq 0 ]; then
1398				_list="$_list $ifn"
1399			fi
1400		;;
1401		esac
1402	done
1403	if [ -n "${_list# }" ]; then
1404		echo "Destroyed clone interfaces: ${_list# }."
1405	fi
1406	debug "Destroyed clones: ${_list# }"
1407}
1408
1409# childif_create
1410#	Create and configure child interfaces.  Return 0 if child
1411#	interfaces are created.
1412#
1413childif_create()
1414{
1415	local cfg child child_vlans child_wlans create_args debug_flags ifn i
1416	cfg=1
1417	ifn=$1
1418
1419	# Create wireless interfaces
1420	child_wlans=`get_if_var $ifn wlans_IF`
1421
1422	for child in ${child_wlans}; do
1423		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1424		debug_flags="`get_if_var $child wlandebug_IF`"
1425
1426		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1427			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1428			if [ -n "${debug_flags}" ]; then
1429				wlandebug -i $child ${debug_flags}
1430			fi
1431		else
1432			i=`${IFCONFIG_CMD} wlan create ${create_args}`
1433			if [ -n "${debug_flags}" ]; then
1434				wlandebug -i $i ${debug_flags}
1435			fi
1436			${IFCONFIG_CMD} $i name $child && cfg=0
1437		fi
1438		if autoif $child; then
1439			ifn_start $child
1440		fi
1441	done
1442
1443	# Create vlan interfaces
1444	child_vlans=`get_if_var $ifn vlans_IF`
1445
1446	if [ -n "${child_vlans}" ]; then
1447		load_kld if_vlan
1448	fi
1449
1450	for child in ${child_vlans}; do
1451		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1452			child="${ifn}.${child}"
1453			create_args=`get_if_var $child create_args_IF`
1454			${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1455		else
1456			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1457			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1458				${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1459			else
1460				i=`${IFCONFIG_CMD} vlan create ${create_args}`
1461				${IFCONFIG_CMD} $i name $child && cfg=0
1462			fi
1463		fi
1464		if autoif $child; then
1465			ifn_start $child
1466		fi
1467	done
1468
1469	return ${cfg}
1470}
1471
1472# childif_destroy
1473#	Destroy child interfaces.
1474#
1475childif_destroy()
1476{
1477	local cfg child child_vlans child_wlans ifn
1478	cfg=1
1479
1480	child_wlans=`get_if_var $ifn wlans_IF`
1481	for child in ${child_wlans}; do
1482		if ! ifexists $child; then
1483			continue
1484		fi
1485		${IFCONFIG_CMD} -n $child destroy && cfg=0
1486	done
1487
1488	child_vlans=`get_if_var $ifn vlans_IF`
1489	for child in ${child_vlans}; do
1490		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1491			child="${ifn}.${child}"
1492		fi
1493		if ! ifexists $child; then
1494			continue
1495		fi
1496		${IFCONFIG_CMD} -n $child destroy && cfg=0
1497	done
1498
1499	return ${cfg}
1500}
1501
1502# ng_mkpeer
1503#	Create netgraph nodes.
1504#
1505ng_mkpeer()
1506{
1507	ngctl -f - 2> /dev/null <<EOF
1508mkpeer $*
1509msg dummy nodeinfo
1510EOF
1511}
1512
1513# ng_create_one
1514#	Create netgraph nodes.
1515#
1516ng_create_one()
1517{
1518	local t
1519
1520	ng_mkpeer $* | while read line; do
1521		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1522		if [ -n "${t}" ]; then
1523			echo ${t}
1524			return
1525		fi
1526	done
1527}
1528
1529# ng_fec_create ifn
1530#	Configure Fast EtherChannel for interface $ifn. Returns 0 if
1531#	FEC arguments were found and configured; returns !0 otherwise.
1532ng_fec_create()
1533{
1534	 local req_iface iface bogus
1535	 req_iface="$1"
1536
1537	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1538
1539	 bogus=""
1540	 while true; do
1541		 iface=`ng_create_one fec dummy fec`
1542		 if [ -z "${iface}" ]; then
1543			 exit 2
1544		 fi
1545		 if [ "${iface}" = "${req_iface}" ]; then
1546			 break
1547		 fi
1548		 bogus="${bogus} ${iface}"
1549	 done
1550
1551	 for iface in ${bogus}; do
1552		 ngctl shutdown ${iface}:
1553	 done
1554}
1555
1556# fec_up
1557#	Create Fast EtherChannel interfaces.
1558fec_up()
1559{
1560	local i j
1561
1562	for i in ${fec_interfaces}; do
1563		ng_fec_create $i
1564		for j in `get_if_var $i fecconfig_IF`; do
1565			case ${j} in
1566			'')
1567				continue
1568				;;
1569			*)
1570				ngctl msg ${i}: add_iface "\"${j}\""
1571				;;
1572			esac
1573		done
1574	done
1575}
1576
1577# ipx_up ifn
1578#	Configure any IPX addresses for interface $ifn. Returns 0 if
1579#	IPX arguments were found and configured; returns 1 otherwise.
1580#
1581ipx_up()
1582{
1583	local ifn
1584	ifn="$1"
1585
1586	# ifconfig_IF_ipx
1587	ifconfig_args=`_ifconfig_getargs $ifn ipx`
1588	if [ -n "${ifconfig_args}" ]; then
1589		${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1590		return 0
1591	fi
1592
1593	return 1
1594}
1595
1596# ipx_down ifn
1597#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
1598#	addresses were found and unconfigured. It returns 1, otherwise.
1599#
1600ipx_down()
1601{
1602	local _if _ifs _ret ipxList oldifs _ipx
1603	_if=$1
1604	_ifs="^"
1605	_ret=1
1606	ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1607	oldifs="$IFS"
1608
1609	IFS="$_ifs"
1610	for _ipx in $ipxList ; do
1611		# get rid of extraneous line
1612		[ -z "$_ipx" ] && break
1613
1614		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1615
1616		IFS="$oldifs"
1617		${IFCONFIG_CMD} ${_if} ${_ipx} delete
1618		IFS="$_ifs"
1619		_ret=0
1620	done
1621	IFS="$oldifs"
1622
1623	return $_ret
1624}
1625
1626# ifnet_rename [ifname]
1627#	Rename interfaces if ifconfig_IF_name is defined.
1628#
1629ifnet_rename()
1630{
1631	local _if _ifname
1632
1633	# ifconfig_IF_name
1634	for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1635		_ifname=`get_if_var $_if ifconfig_IF_name`
1636		if [ ! -z "$_ifname" ]; then
1637			${IFCONFIG_CMD} $_if name $_ifname
1638		fi
1639	done
1640
1641	return 0
1642}
1643
1644# list_net_interfaces type
1645#	List all network interfaces. The type of interface returned
1646#	can be controlled by the type argument. The type
1647#	argument can be any of the following:
1648#		nodhcp	- all interfaces, excluding DHCP configured interfaces
1649#		dhcp	- list only DHCP configured interfaces
1650#		noautoconf	- all interfaces, excluding IPv6 Stateless
1651#				  Address Autoconf configured interfaces
1652#		autoconf	- list only IPv6 Stateless Address Autoconf
1653#				  configured interfaces
1654#	If no argument is specified all network interfaces are output.
1655#	Note that the list will include cloned interfaces if applicable.
1656#	Cloned interfaces must already exist to have a chance to appear
1657#	in the list if ${network_interfaces} is set to `auto'.
1658#
1659list_net_interfaces()
1660{
1661	local type _tmplist _list _autolist _lo _if
1662	type=$1
1663
1664	# Get a list of ALL the interfaces and make lo0 first if it's there.
1665	#
1666	_tmplist=
1667	case ${network_interfaces} in
1668	[Aa][Uu][Tt][Oo])
1669		_autolist="`${IFCONFIG_CMD} -l`"
1670		_lo=
1671		for _if in ${_autolist} ; do
1672			if autoif $_if; then
1673				if [ "$_if" = "lo0" ]; then
1674					_lo="lo0 "
1675				else
1676					_tmplist="${_tmplist} ${_if}"
1677				fi
1678			fi
1679		done
1680		_tmplist="${_lo}${_tmplist# }"
1681	;;
1682	*)
1683		for _if in ${network_interfaces} ${cloned_interfaces}; do
1684			# epair(4) uses epair[0-9] for creation and
1685			# epair[0-9][ab] for configuration.
1686			case $_if in
1687			epair[0-9]*)
1688				_tmplist="$_tmplist ${_if}a ${_if}b"
1689			;;
1690			*)
1691				_tmplist="$_tmplist $_if"
1692			;;
1693			esac
1694		done
1695		#
1696		# lo0 is effectively mandatory, so help prevent foot-shooting
1697		#
1698		case "$_tmplist" in
1699		lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1700			# This is fine, do nothing
1701			_tmplist="${_tmplist# }"
1702		;;
1703		*)
1704			_tmplist="lo0 ${_tmplist# }"
1705		;;
1706		esac
1707	;;
1708	esac
1709
1710	_list=
1711	case "$type" in
1712	nodhcp)
1713		for _if in ${_tmplist} ; do
1714			if ! dhcpif $_if && \
1715			   [ -n "`_ifconfig_getargs $_if`" ]; then
1716				_list="${_list# } ${_if}"
1717			fi
1718		done
1719	;;
1720	dhcp)
1721		for _if in ${_tmplist} ; do
1722			if dhcpif $_if; then
1723				_list="${_list# } ${_if}"
1724			fi
1725		done
1726	;;
1727	noautoconf)
1728		for _if in ${_tmplist} ; do
1729			if ! ipv6_autoconfif $_if && \
1730			   [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1731				_list="${_list# } ${_if}"
1732			fi
1733		done
1734	;;
1735	autoconf)
1736		for _if in ${_tmplist} ; do
1737			if ipv6_autoconfif $_if; then
1738				_list="${_list# } ${_if}"
1739			fi
1740		done
1741	;;
1742	*)
1743		_list=${_tmplist}
1744	;;
1745	esac
1746
1747	echo $_list
1748
1749	return 0
1750}
1751
1752# get_default_if -address_family
1753#	Get the interface of the default route for the given address family.
1754#	The -address_family argument must be suitable passing to route(8).
1755#
1756get_default_if()
1757{
1758	local routeget oldifs defif line
1759	defif=
1760	oldifs="$IFS"
1761	IFS="
1762"
1763	for line in `route -n get $1 default 2>/dev/null`; do
1764		case $line in
1765		*interface:*)
1766			defif=${line##*: }
1767			;;
1768		esac
1769	done
1770	IFS=${oldifs}
1771
1772	echo $defif
1773}
1774
1775# hexdigit arg
1776#	Echo decimal number $arg (single digit) in hexadecimal format.
1777hexdigit()
1778{
1779	printf '%x\n' "$1"
1780}
1781
1782# hexprint arg
1783#	Echo decimal number $arg (multiple digits) in hexadecimal format.
1784hexprint()
1785{
1786	printf '%x\n' "$1"
1787}
1788
1789is_wired_interface()
1790{
1791	local media
1792
1793	case `${IFCONFIG_CMD} $1 2>/dev/null` in
1794	*media:?Ethernet*) media=Ethernet ;;
1795	esac
1796
1797	test "$media" = "Ethernet"
1798}
1799
1800# network6_getladdr if [flag]
1801#	Echo link-local address from $if if any.
1802#	If flag is defined, tentative ones will be excluded.
1803network6_getladdr()
1804{
1805	local _if _flag proto addr rest
1806	_if=$1
1807	_flag=$2
1808
1809	${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1810		case "${proto}/${addr}/${_flag}/${rest}" in
1811		inet6/fe80::*//*)
1812			echo ${addr}
1813		;;
1814		inet6/fe80:://*tentative*)	# w/o flag
1815			sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1816			network6_getladdr $_if $_flags
1817		;;
1818		inet6/fe80::/*/*tentative*)	# w/ flag
1819			echo ${addr}
1820		;;
1821		*)
1822			continue
1823		;;
1824		esac
1825
1826		return
1827	done
1828}
1829