network.subr revision 183517
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 183517 2008-10-01 18:46:46Z brooks $
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
34178356Ssam# Bring up and configure an interface.  If some configuration is applied
35178356Ssam# print the interface configuration.
36178356Ssam#
37178356Ssamifn_start()
38178356Ssam{
39178356Ssam	local ifn cfg
40178356Ssam	ifn="$1"
41178356Ssam	cfg=1
42178356Ssam
43178356Ssam	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
44178356Ssam
45178356Ssam	ifscript_up ${ifn} && cfg=0
46178356Ssam	ifconfig_up ${ifn} && cfg=0
47178356Ssam	ipv4_up ${ifn} && cfg=0
48178356Ssam	ipx_up ${ifn} && cfg=0
49179001Sbrooks	childif_create ${ifn}
50178356Ssam
51178356Ssam	return $cfg
52178356Ssam}
53178356Ssam
54178356Ssam# ifn_start ifn
55178356Ssam# Shutdown and de-configure an interface.  If action is taken print the
56178356Ssam# interface name.
57178356Ssam#
58178356Ssamifn_stop()
59178356Ssam{
60178356Ssam	local ifn cfg
61178356Ssam	ifn="$1"
62178356Ssam	cfg=1
63178356Ssam
64178356Ssam	[ -z "$ifn" ] && return 1
65178356Ssam
66178356Ssam	ipx_down ${ifn} && cfg=0
67178356Ssam	ipv4_down ${ifn} && cfg=0
68178356Ssam	ifconfig_down ${ifn} && cfg=0
69178356Ssam	ifscript_down ${ifn} && cfg=0
70179002Sbrooks	childif_destroy ${ifn}
71178356Ssam
72178356Ssam	return $cfg
73178356Ssam}
74178356Ssam
75113674Smtm# ifconfig_up if
76113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
77113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
78113674Smtm#	arguments were found and executed or 1 if the interface
79147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
80147088Sbrooks#	here.
81113674Smtm#
82113674Smtmifconfig_up()
83113674Smtm{
84147088Sbrooks	_cfg=1
85147088Sbrooks
86147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
87113674Smtm	if [ -n "${ifconfig_args}" ]; then
88178356Ssam		ifconfig $1 ${ifconfig_args}
89149726Sbrooks		ifconfig $1 up
90147088Sbrooks		_cfg=0
91113674Smtm	fi
92147088Sbrooks
93147088Sbrooks	if wpaif $1; then
94147682Sbrooks		/etc/rc.d/wpa_supplicant start $1
95147088Sbrooks		_cfg=0		# XXX: not sure this should count
96147088Sbrooks	fi
97147088Sbrooks
98147088Sbrooks	if dhcpif $1; then
99149726Sbrooks		if [ $_cfg -ne 0 ] ; then
100149726Sbrooks			ifconfig $1 up
101149726Sbrooks		fi
102157706Sbrooks		if syncdhcpif $1; then
103157706Sbrooks			/etc/rc.d/dhclient start $1
104157706Sbrooks		fi
105147088Sbrooks		_cfg=0
106147088Sbrooks	fi
107147088Sbrooks
108147121Sbrooks	return $_cfg
109113674Smtm}
11025184Sjkh
111116029Smtm# ifconfig_down if
112161386Sbrooks#	returns 1 if wpa_supplicant or dhclient was stopped or
113161386Sbrooks#	the interface exists.
114116029Smtm#
115116029Smtmifconfig_down()
116116029Smtm{
117116029Smtm	[ -z "$1" ] && return 1
118147121Sbrooks	_cfg=1
119116029Smtm
120147088Sbrooks	if wpaif $1; then
121147682Sbrooks		/etc/rc.d/wpa_supplicant stop $1
122147121Sbrooks		_cfg=0
123147088Sbrooks	fi
124147088Sbrooks
125147088Sbrooks	if dhcpif $1; then
126147088Sbrooks		/etc/rc.d/dhclient stop $1
127147088Sbrooks		_cfg=0
128147088Sbrooks	fi
129147088Sbrooks
130161386Sbrooks	if ifexists $1; then
131161386Sbrooks		ifconfig $1 down
132161386Sbrooks		_cfg=0
133161386Sbrooks	fi
134157706Sbrooks
135147121Sbrooks	return $_cfg
136116029Smtm}
137116029Smtm
138157706Sbrooks# get_if_var if var [default]
139157706Sbrooks#       Return the value of the pseudo-hash corresponding to $if where
140157706Sbrooks#       $var is a string containg the sub-string "IF" which will be
141157706Sbrooks#       replaced with $if after the characters defined in _punct are
142157706Sbrooks#       replaced with '_'. If the variable is unset, replace it with
143157706Sbrooks#       $default if given.
144157706Sbrooksget_if_var()
145157706Sbrooks{
146157706Sbrooks	if [ $# -ne 2 -a $# -ne 3 ]; then
147157706Sbrooks		err 3 'USAGE: get_if_var name var [default]'
148157706Sbrooks	fi
149157706Sbrooks
150157706Sbrooks	_if=$1
151157706Sbrooks	_punct=". - / +"
152157736Sbrooks	for _punct_c in $_punct; do
153157706Sbrooks		_if=`ltr ${_if} ${_punct_c} '_'`
154157706Sbrooks	done
155157706Sbrooks	_var=$2
156157706Sbrooks	_default=$3
157157706Sbrooks
158157706Sbrooks	prefix=${_var%%IF*}
159157706Sbrooks	suffix=${_var##*IF}
160168033Sache	eval echo \${${prefix}${_if}${suffix}-${_default}}
161157706Sbrooks}
162157706Sbrooks
163147088Sbrooks# _ifconfig_getargs if
164147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
165147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
166147088Sbrooks#	outside this file.
167147088Sbrooks_ifconfig_getargs()
168147088Sbrooks{
169147088Sbrooks	_ifn=$1
170147088Sbrooks	if [ -z "$_ifn" ]; then
171147088Sbrooks		return 1
172147088Sbrooks	fi
173147088Sbrooks
174157706Sbrooks	get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
175147088Sbrooks}
176147088Sbrooks
177147088Sbrooks# ifconfig_getargs if
178147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
179147088Sbrooks#	args such as DHCP and WPA.
180147088Sbrooksifconfig_getargs()
181147088Sbrooks{
182147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
183147088Sbrooks	if [ $? -eq 1 ]; then
184147088Sbrooks		return 1
185147088Sbrooks	fi
186147088Sbrooks	_args=
187147088Sbrooks
188147088Sbrooks	for _arg in $_tmpargs; do
189147088Sbrooks		case $_arg in
190157706Sbrooks		[Dd][Hh][Cc][Pp]) ;;
191157706Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo]) ;;
192157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
193157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
194157706Sbrooks		[Ww][Pp][Aa]) ;;
195147088Sbrooks		*)
196147088Sbrooks			_args="$_args $_arg"
197147088Sbrooks			;;
198147088Sbrooks		esac
199147088Sbrooks	done
200147088Sbrooks
201147088Sbrooks	echo $_args
202147088Sbrooks}
203147088Sbrooks
204149401Sbrooks# autoif
205149401Sbrooks#	Returns 0 if the interface should be automaticly configured at
206149401Sbrooks#	boot time and 1 otherwise.
207149401Sbrooksautoif()
208149401Sbrooks{
209149401Sbrooks	_tmpargs=`_ifconfig_getargs $1`
210149401Sbrooks	for _arg in $_tmpargs; do
211149401Sbrooks		case $_arg in
212149401Sbrooks		[Nn][Oo][Aa][Uu][Tt][Oo])
213149401Sbrooks			return 1
214149401Sbrooks			;;
215149401Sbrooks		esac
216149401Sbrooks	done
217149401Sbrooks	return 0
218149401Sbrooks}
219149401Sbrooks
220147088Sbrooks# dhcpif if
221147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
222147088Sbrooksdhcpif()
223147088Sbrooks{
224147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
225147088Sbrooks	for _arg in $_tmpargs; do
226147088Sbrooks		case $_arg in
227147088Sbrooks		[Dd][Hh][Cc][Pp])
228147088Sbrooks			return 0
229147088Sbrooks			;;
230157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
231157706Sbrooks			return 0
232157706Sbrooks			;;
233157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
234157706Sbrooks			return 0
235157706Sbrooks			;;
236147088Sbrooks		esac
237147088Sbrooks	done
238147088Sbrooks	return 1
239147088Sbrooks}
240147088Sbrooks
241157706Sbrooks# syncdhcpif
242157706Sbrooks#	Returns 0 if the interface should be configured synchronously and
243157706Sbrooks#	1 otherwise.
244157706Sbrookssyncdhcpif()
245157706Sbrooks{
246157706Sbrooks	_tmpargs=`_ifconfig_getargs $1`
247157706Sbrooks	for _arg in $_tmpargs; do
248157706Sbrooks		case $_arg in
249157706Sbrooks		[Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
250157706Sbrooks			return 1
251157706Sbrooks			;;
252157706Sbrooks		[Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
253157706Sbrooks			return 0
254157706Sbrooks			;;
255157706Sbrooks		esac
256157706Sbrooks	done
257157737Sbrooks	if checkyesno synchronous_dhclient; then
258157706Sbrooks		return 0
259157706Sbrooks	else
260157706Sbrooks		return 1
261157706Sbrooks	fi
262157706Sbrooks}
263157706Sbrooks
264147088Sbrooks# wpaif if
265147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
266147088Sbrookswpaif()
267147088Sbrooks{
268147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
269147088Sbrooks	for _arg in $_tmpargs; do
270147088Sbrooks		case $_arg in
271147088Sbrooks		[Ww][Pp][Aa])
272147088Sbrooks			return 0
273147088Sbrooks			;;
274147088Sbrooks		esac
275147088Sbrooks	done
276147088Sbrooks	return 1
277147088Sbrooks}
278147088Sbrooks
279162490Sbrooks# ipv6if if
280162490Sbrooks#	Returns 0 if the interface should be configured for IPv6 and
281162490Sbrooks#	1 otherwise.
282162490Sbrooksipv6if()
283162490Sbrooks{
284162490Sbrooks	if ! checkyesno ipv6_enable; then
285162490Sbrooks		return 1
286162490Sbrooks	fi
287162490Sbrooks	case "${ipv6_network_interfaces}" in
288162490Sbrooks	[Aa][Uu][Tt][Oo])
289162490Sbrooks		return 0
290162490Sbrooks		;;
291162490Sbrooks	''|[Nn][Oo][Nn][Ee])
292162490Sbrooks		return 1
293162490Sbrooks		;;
294162490Sbrooks	esac
295162490Sbrooks	for v6if in ${ipv6_network_interfaces}; do
296162490Sbrooks		if [ "${v6if}" = "${1}" ]; then
297162490Sbrooks			return 0
298162490Sbrooks		fi
299162490Sbrooks	done
300162490Sbrooks	return 1
301162490Sbrooks}
302162490Sbrooks
303161386Sbrooks# ifexists if
304161386Sbrooks#	Returns 0 if the interface exists and 1 otherwise.
305161386Sbrooksifexists()
306161386Sbrooks{
307169889Sthompsa	ifconfig -n $1 > /dev/null 2>&1
308161386Sbrooks}
309161386Sbrooks
310152441Sbrooks# ipv4_up if
311152441Sbrooks#  add IPv4 addresses to the interface $if 
312152441Sbrooksipv4_up()
313152441Sbrooks{
314152441Sbrooks	_if=$1
315152441Sbrooks	ifalias_up ${_if}
316152441Sbrooks	ipv4_addrs_common ${_if} alias
317152441Sbrooks}
318152441Sbrooks
319152441Sbrooks# ipv4_down if
320152441Sbrooks#  remove IPv4 addresses from the interface $if
321152441Sbrooksipv4_down()
322152441Sbrooks{
323152441Sbrooks	_if=$1
324161386Sbrooks	_ifs="^"
325161386Sbrooks	_ret=1
326161386Sbrooks
327161386Sbrooks	ifexists ${_if} || return 1
328161386Sbrooks
329161386Sbrooks	inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
330161386Sbrooks
331161386Sbrooks	oldifs="$IFS"
332161386Sbrooks	IFS="$_ifs"
333161386Sbrooks	for _inet in $inetList ; do
334161386Sbrooks		# get rid of extraneous line
335161386Sbrooks		[ -z "$_inet" ] && break
336161386Sbrooks
337161386Sbrooks		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
338161386Sbrooks
339161386Sbrooks		IFS="$oldifs"
340161386Sbrooks		ifconfig ${_if} ${_inet} delete
341161386Sbrooks		IFS="$_ifs"
342161386Sbrooks		_ret=0
343161386Sbrooks	done
344161386Sbrooks	IFS="$oldifs"
345161386Sbrooks
346161386Sbrooks	ifalias_down ${_if} && _ret=0
347161386Sbrooks	ipv4_addrs_common ${_if} -alias && _ret=0
348161386Sbrooks
349161386Sbrooks	return $_ret
350152441Sbrooks}
351152441Sbrooks
352152441Sbrooks# ipv4_addrs_common if action
353152441Sbrooks#   Evaluate the ifconfig_if_ipv4 arguments for interface $if
354152441Sbrooks#   and use $action to add or remove IPv4 addresses from $if.
355152441Sbrooksipv4_addrs_common()
356152441Sbrooks{  
357152441Sbrooks	_ret=1
358152441Sbrooks	_if=$1
359152441Sbrooks	_action=$2
360152441Sbrooks    
361152441Sbrooks	# get ipv4-addresses
362157706Sbrooks	cidr_addr=`get_if_var $_if ipv4_addrs_IF`
363152441Sbrooks    
364152441Sbrooks	for _cidr in ${cidr_addr}; do
365152441Sbrooks		_ipaddr=${_cidr%%/*}
366152441Sbrooks		_netmask="/"${_cidr##*/}
367152441Sbrooks		_range=${_ipaddr##*.}
368152441Sbrooks		_ipnet=${_ipaddr%.*}
369152441Sbrooks		_iplow=${_range%-*}
370152441Sbrooks		_iphigh=${_range#*-}
371152441Sbrooks
372152441Sbrooks		# clear netmask when removing aliases
373152441Sbrooks		if [ "${_action}" = "-alias" ]; then
374152441Sbrooks			_netmask=""
375152441Sbrooks		fi
376152441Sbrooks        
377152441Sbrooks		_ipcount=${_iplow}
378152441Sbrooks		while [ "${_ipcount}" -le "${_iphigh}" ]; do
379152441Sbrooks			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
380152441Sbrooks			_ipcount=$((${_ipcount}+1))
381152441Sbrooks			_ret=0
382152441Sbrooks
383152441Sbrooks			# only the first ipaddr in a subnet need the real netmask
384152441Sbrooks			if [ "${_action}" != "-alias" ]; then
385152441Sbrooks				_netmask="/32"
386152441Sbrooks			fi
387152441Sbrooks		done
388152441Sbrooks	done
389152441Sbrooks	return $_ret
390152441Sbrooks}
391152441Sbrooks
392113674Smtm# ifalias_up if
393113674Smtm#	Configure aliases for network interface $if.
394113674Smtm#	It returns 0 if at least one alias was configured or
395113674Smtm#	1 if there were none.
396113674Smtm#
397113674Smtmifalias_up()
398113674Smtm{
399113674Smtm	_ret=1
400113674Smtm	alias=0
401113674Smtm	while : ; do
402157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
403113674Smtm		if [ -n "${ifconfig_args}" ]; then
404113674Smtm			ifconfig $1 ${ifconfig_args} alias
405113674Smtm			alias=$((${alias} + 1))
406113674Smtm			_ret=0
407113674Smtm		else
408113674Smtm			break
409113674Smtm		fi
410113674Smtm	done
411113674Smtm	return $_ret
412113674Smtm}
413100280Sgordon
414116029Smtm#ifalias_down if
415116029Smtm#	Remove aliases for network interface $if.
416116029Smtm#	It returns 0 if at least one alias was removed or
417116029Smtm#	1 if there were none.
418116029Smtm#
419116029Smtmifalias_down()
420116029Smtm{
421116029Smtm	_ret=1
422116029Smtm	alias=0
423116029Smtm	while : ; do
424157706Sbrooks		ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
425116029Smtm		if [ -n "${ifconfig_args}" ]; then
426116029Smtm			ifconfig $1 ${ifconfig_args} -alias
427116029Smtm			alias=$((${alias} + 1))
428116029Smtm			_ret=0
429116029Smtm		else
430116029Smtm			break
431116029Smtm		fi
432116029Smtm	done
433116029Smtm	return $_ret
434116029Smtm}
435116029Smtm
436113674Smtm# ifscript_up if
437113674Smtm#	Evaluate a startup script for the $if interface.
438113674Smtm#	It returns 0 if a script was found and processed or
439113674Smtm#	1 if no script was found.
440113674Smtm#
441113674Smtmifscript_up()
442100280Sgordon{
443113674Smtm	if [ -r /etc/start_if.$1 ]; then
444113674Smtm		. /etc/start_if.$1
445113674Smtm		return 0
446113674Smtm	fi
447113674Smtm	return 1
448100280Sgordon}
449100280Sgordon
450116029Smtm# ifscript_down if
451116029Smtm#	Evaluate a shutdown script for the $if interface.
452116029Smtm#	It returns 0 if a script was found and processed or
453116029Smtm#	1 if no script was found.
454116029Smtm#
455116029Smtmifscript_down()
456116029Smtm{
457116029Smtm	if [ -r /etc/stop_if.$1 ]; then
458116029Smtm		. /etc/stop_if.$1
459116029Smtm		return 0
460116029Smtm	fi
461116029Smtm	return 1
462116029Smtm}
463116029Smtm
464113674Smtm# Create cloneable interfaces.
465113674Smtm#
466113674Smtmclone_up()
467100280Sgordon{
468113674Smtm	_prefix=
469113674Smtm	_list=
470113674Smtm	for ifn in ${cloned_interfaces}; do
471178527Sbrooks		ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
472116774Skuriyama		if [ $? -eq 0 ]; then
473113674Smtm			_list="${_list}${_prefix}${ifn}"
474113674Smtm			[ -z "$_prefix" ] && _prefix=' '
475113674Smtm		fi
476113674Smtm	done
477113674Smtm	debug "Cloned: ${_list}"
478113674Smtm}
479100280Sgordon
480113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed
481113674Smtm# to standard output.
482113674Smtm#
483113674Smtmclone_down()
484113674Smtm{
485113674Smtm	_prefix=
486113674Smtm	_list=
487113674Smtm	for ifn in ${cloned_interfaces}; do
488113674Smtm		ifconfig ${ifn} destroy
489116774Skuriyama		if [ $? -eq 0 ]; then
490113674Smtm			_list="${_list}${_prefix}${ifn}"
491113674Smtm			[ -z "$_prefix" ] && _prefix=' '
492113674Smtm		fi
493113674Smtm	done
494113674Smtm	debug "Destroyed clones: ${_list}"
495100280Sgordon}
496100280Sgordon
497178356Ssam# Create and configure child interfaces.
498178356Ssam# Return 0 if child interfaces are created.
499178356Ssam#
500178356Ssamchildif_create()
501178356Ssam{
502178527Sbrooks	local cfg child child_wlans create_args ifn i
503178356Ssam	cfg=1
504178356Ssam
505178356Ssam	ifn=$1
506178356Ssam
507178527Sbrooks	# Create wireless interfaces
508178527Sbrooks	child_wlans=`get_if_var $ifn wlans_IF`
509178527Sbrooks
510178527Sbrooks	for child in ${child_wlans}; do
511183517Sbrooks		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
512178356Ssam		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
513178356Ssam			ifconfig $child create ${create_args} && cfg=0
514178356Ssam		else
515178356Ssam			i=`ifconfig wlan create ${create_args}`
516178356Ssam			ifconfig $i name $child && cfg=0
517178356Ssam		fi
518178356Ssam		ifn_start $child
519178356Ssam	done
520178356Ssam
521179001Sbrooks	return ${cfg}
522178356Ssam}
523178356Ssam
524178356Ssam# Destroy child interfaces.
525178356Ssam#
526178356Ssamchildif_destroy()
527178356Ssam{
528178527Sbrooks	local cfg child child_wlans ifn
529178356Ssam
530178527Sbrooks	child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
531178527Sbrooks	for child in ${child_wlans}; do
532178356Ssam		ifconfig $child destroy && cfg=0
533178356Ssam	done
534178356Ssam}
535178356Ssam
536166583Sflz# Create netgraph nodes.
537166583Sflz#
538166583Sflzng_mkpeer() {
539166583Sflz	ngctl -f - 2> /dev/null <<EOF
540166583Sflzmkpeer $*
541166583Sflzmsg dummy nodeinfo
542166583SflzEOF
543166583Sflz}
544166583Sflz
545166583Sflzng_create_one() {
546166583Sflz	ng_mkpeer $* | while read line; do
547166583Sflz		t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
548166583Sflz		if [ -n "${t}" ]; then
549166583Sflz			echo ${t}
550166583Sflz			return
551166583Sflz		fi
552166583Sflz	done
553166583Sflz}
554166583Sflz
555113674Smtmgif_up() {
556166583Sflz	for i in ${gif_interfaces}; do
557166583Sflz		peers=`get_if_var $i gifconfig_IF`
558166583Sflz		case ${peers} in
559166583Sflz		'')
560166583Sflz			continue
561166583Sflz			;;
562166583Sflz		*)
563177682Sbrooks			if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
564177682Sbrooks				ifconfig $i create >/dev/null 2>&1
565177682Sbrooks			else
566177682Sbrooks				gif=`ifconfig gif create`
567177682Sbrooks				ifconfig $gif name $i
568177682Sbrooks			fi
569166583Sflz			ifconfig $i tunnel ${peers}
570166583Sflz			ifconfig $i up
571166583Sflz			;;
572166583Sflz		esac
573166583Sflz	done
574166583Sflz}
575166583Sflz
576166583Sflz# ng_fec_create ifn
577166583Sflz# Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
578166583Sflz# arguments were found and configured; returns !0 otherwise.
579166583Sflzng_fec_create() {
580166583Sflz	 local req_iface iface bogus
581166583Sflz	 req_iface="$1"
582166583Sflz
583166583Sflz	 ngctl shutdown ${req_iface}: > /dev/null 2>&1
584166583Sflz
585166583Sflz	 bogus=""
586166583Sflz	 while true; do
587166583Sflz		 iface=`ng_create_one fec dummy fec`
588166583Sflz		 if [ -z "${iface}" ]; then
589166583Sflz			 exit 2
590166583Sflz		 fi
591166583Sflz		 if [ "${iface}" = "${req_iface}" ]; then
592166583Sflz			 break
593166583Sflz		 fi
594166583Sflz		 bogus="${bogus} ${iface}"
595166583Sflz	 done
596166583Sflz
597166583Sflz	 for iface in ${bogus}; do
598166583Sflz		 ngctl shutdown ${iface}:
599166583Sflz	 done
600166583Sflz}
601166583Sflz
602166583Sflzfec_up() {
603166583Sflz	for i in ${fec_interfaces}; do
604166583Sflz		ng_fec_create $i
605166583Sflz		for j in `get_if_var $i fecconfig_IF`; do
606166583Sflz			case ${j} in
607100282Sdougb			'')
608100282Sdougb				continue
609100282Sdougb				;;
610100282Sdougb			*)
611166583Sflz				ngctl msg ${i}: add_iface "\"${j}\""
612100282Sdougb				;;
613100282Sdougb			esac
614100282Sdougb		done
615166583Sflz	done
616100282Sdougb}
617100282Sdougb
618113674Smtm#
619113674Smtm# ipx_up ifn
620113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
621113674Smtm# arguments were found and configured; returns 1 otherwise.
622113674Smtm#
623113674Smtmipx_up()
624100280Sgordon{
625113674Smtm	ifn="$1"
626157706Sbrooks	ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
627113674Smtm	if [ -n "${ifconfig_args}" ]; then
628113674Smtm		ifconfig ${ifn} ${ifconfig_args}
629113674Smtm		return 0
63085831Sdes	fi
631113674Smtm	return 1
632113674Smtm}
63385831Sdes
634116029Smtm# ipx_down ifn
635116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
636116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
637113674Smtm#
638116029Smtmipx_down()
639116029Smtm{
640116100Smtm	[ -z "$1" ] && return 1
641116100Smtm	_ifs="^"
642116100Smtm	_ret=1
643116100Smtm
644161386Sbrooks	ifexists $1 || return 1
645161386Sbrooks
646116100Smtm	ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
647116100Smtm
648116100Smtm	oldifs="$IFS"
649116100Smtm	IFS="$_ifs"
650116100Smtm	for _ipx in $ipxList ; do
651116100Smtm		# get rid of extraneous line
652116100Smtm		[ -z "$_ipx" ] && break
653116100Smtm
654116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
655116100Smtm
656116100Smtm		IFS="$oldifs"
657116100Smtm		ifconfig $1 ${_ipx} delete
658116100Smtm		IFS="$_ifs"
659116100Smtm		_ret=0
660116100Smtm	done
661116100Smtm	IFS="$oldifs"
662116100Smtm
663116100Smtm	return $_ret
664116029Smtm}
665116029Smtm
666137070Spjd# ifnet_rename
667137070Spjd#	Rename all requested interfaces.
668116029Smtm#
669137070Spjdifnet_rename()
670137070Spjd{
671137070Spjd
672138386Srse	_ifn_list="`ifconfig -l`"
673137070Spjd	[ -z "$_ifn_list" ] && return 0
674137070Spjd	for _if in ${_ifn_list} ; do
675157706Sbrooks		_ifname=`get_if_var $_if ifconfig_IF_name`
676137070Spjd		if [ ! -z "$_ifname" ]; then
677137070Spjd			ifconfig $_if name $_ifname
678137070Spjd		fi
679137070Spjd	done
680137070Spjd	return 0
681137070Spjd}
682137070Spjd
683137070Spjd#
684113674Smtm# list_net_interfaces type
685113674Smtm#	List all network interfaces. The type of interface returned
686113674Smtm#	can be controlled by the type argument. The type
687113674Smtm#	argument can be any of the following:
688113674Smtm#		nodhcp - all interfaces, excluding DHCP configured interfaces
689113674Smtm#		dhcp   - list only DHCP configured interfaces
690113674Smtm#	If no argument is specified all network interfaces are output.
691134429Syar#	Note that the list will include cloned interfaces if applicable.
692134429Syar#	Cloned interfaces must already exist to have a chance to appear
693134429Syar#	in the list if ${network_interfaces} is set to `auto'.
694113674Smtm#
695113674Smtmlist_net_interfaces()
696113674Smtm{
697113674Smtm	type=$1
69865532Snectar
699149726Sbrooks	# Get a list of ALL the interfaces and make lo0 first if it's there.
70051231Ssheldonh	#
70151231Ssheldonh	case ${network_interfaces} in
70251231Ssheldonh	[Aa][Uu][Tt][Oo])
703149401Sbrooks		_prefix=''
704149401Sbrooks		_autolist="`ifconfig -l`"
705149726Sbrooks		_lo=
706149401Sbrooks		for _if in ${_autolist} ; do
707149401Sbrooks			if autoif $_if; then
708149726Sbrooks				if [ "$_if" = "lo0" ]; then
709149726Sbrooks					_lo="lo0 "
710149726Sbrooks				else
711149726Sbrooks					_tmplist="${_tmplist}${_prefix}${_if}"
712149726Sbrooks					[ -z "$_prefix" ] && _prefix=' '
713149726Sbrooks				fi
714149401Sbrooks			fi
715149401Sbrooks		done
716149726Sbrooks		_tmplist="${_lo}${_tmplist}"
71751231Ssheldonh		;;
71883677Sbrooks	*)
719179314Sdougb		if [ -z "$type" ]; then
720178695Sbrooks			warn "Values of network_interfaces other than" \
721178695Sbrooks			    "AUTO are deprecated"
722178695Sbrooks		fi
723149401Sbrooks		_tmplist="${network_interfaces} ${cloned_interfaces}"
72483677Sbrooks		;;
72551231Ssheldonh	esac
72649122Sbrian
727113674Smtm	if [ -z "$type" ]; then
728113674Smtm		echo $_tmplist
729113674Smtm		return 0
730113674Smtm	fi
73149122Sbrian
732138385Srse	# Separate out dhcp and non-dhcp interfaces
733113674Smtm	#
734113674Smtm	_aprefix=
735134376Syar	_bprefix=
736113674Smtm	for _if in ${_tmplist} ; do
737147684Sbrooks		if dhcpif $_if; then
738113674Smtm			_dhcplist="${_dhcplist}${_aprefix}${_if}"
739113674Smtm			[ -z "$_aprefix" ] && _aprefix=' '
740157706Sbrooks		elif [ -n "`_ifconfig_getargs $_if`" ]; then
741113674Smtm			_nodhcplist="${_nodhcplist}${_bprefix}${_if}"
742113674Smtm			[ -z "$_bprefix" ] && _bprefix=' '
743147684Sbrooks		fi
74454458Sobrien	done
74551231Ssheldonh
746113674Smtm	case "$type" in
747113674Smtm	nodhcp)
748113674Smtm		echo $_nodhcplist
749113674Smtm		;;
750113674Smtm	dhcp)
751113674Smtm		echo $_dhcplist
752113674Smtm		;;
753113674Smtm	esac
754130151Sschweikh	return 0
75525184Sjkh}
756114942Sume
757179003Sbrooks# get_default_if -address_family
758179003Sbrooks#	Get the interface of the default route for the given address family.
759179003Sbrooks#	The -address_family argument must be suitable passing to route(8).
760179003Sbrooks#
761179003Sbrooksget_default_if()
762179003Sbrooks{
763179003Sbrooks	routeget="`route -n get $1 default 2>/dev/null`"
764179003Sbrooks	oldifs="$IFS"
765179003Sbrooks	IFS="
766179003Sbrooks"
767179003Sbrooks	defif=
768179003Sbrooks	for line in $routeget ; do
769179003Sbrooks		case $line in
770179003Sbrooks		*interface:*)
771179003Sbrooks			defif=${line##*: }
772179003Sbrooks			;;
773179003Sbrooks		esac
774179003Sbrooks	done
775179003Sbrooks	IFS=${oldifs}
776179003Sbrooks
777179003Sbrooks	echo $defif
778179003Sbrooks}
779179003Sbrooks
780114942Sumehexdigit()
781114942Sume{
782114942Sume	if [ $1 -lt 10 ]; then
783114942Sume		echo $1
784114942Sume	else
785114942Sume		case $1 in
786114942Sume		10)	echo a ;;
787114942Sume		11)	echo b ;;
788114942Sume		12)	echo c ;;
789114942Sume		13)	echo d ;;
790114942Sume		14)	echo e ;;
791114942Sume		15)	echo f ;;
792114942Sume		esac
793114942Sume	fi
794114942Sume}
795114942Sume
796114942Sumehexprint()
797114942Sume{
798114942Sume	val=$1
799114942Sume	str=''
800114942Sume
801114942Sume	dig=`hexdigit $((${val} & 15))`
802114942Sume	str=${dig}${str}
803114942Sume	val=$((${val} >> 4))
804114942Sume	while [ ${val} -gt 0 ]; do
805114942Sume		dig=`hexdigit $((${val} & 15))`
806114942Sume		str=${dig}${str}
807114942Sume		val=$((${val} >> 4))
808114942Sume	done
809114942Sume
810114942Sume	echo ${str}
811114942Sume}
812114942Sume
813114942Sume# Setup the interfaces for IPv6
814114942Sumenetwork6_interface_setup()
815114942Sume{
816114942Sume	interfaces=$*
817114942Sume	rtsol_interfaces=''
818114942Sume	case ${ipv6_gateway_enable} in
819114942Sume	[Yy][Ee][Ss])
820114942Sume		rtsol_available=no
821114942Sume		;;
822114942Sume	*)
823114942Sume		rtsol_available=yes
824114942Sume		;;
825114942Sume	esac
826114942Sume	for i in $interfaces; do
827114942Sume		rtsol_interface=yes
828157706Sbrooks		prefix=`get_if_var $i ipv6_prefix_IF`
829114942Sume		if [ -n "${prefix}" ]; then
830114942Sume			rtsol_available=no
831114942Sume			rtsol_interface=no
832114942Sume			laddr=`network6_getladdr $i`
833114942Sume			hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
834114942Sume			for j in ${prefix}; do
835114942Sume				address=$j\:${hostid}
836114942Sume				ifconfig $i inet6 ${address} prefixlen 64 alias
837114942Sume
838114942Sume				case ${ipv6_gateway_enable} in
839114942Sume				[Yy][Ee][Ss])
840114942Sume					# subnet-router anycast address
841114942Sume					# (rfc2373)
842114942Sume					ifconfig $i inet6 $j:: prefixlen 64 \
843114942Sume						alias anycast
844114942Sume					;;
845114942Sume				esac
846114942Sume			done
847114942Sume		fi
848157706Sbrooks		ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
849114942Sume		if [ -n "${ipv6_ifconfig}" ]; then
850114942Sume			rtsol_available=no
851114942Sume			rtsol_interface=no
852114942Sume			ifconfig $i inet6 ${ipv6_ifconfig} alias
853114942Sume		fi
854114942Sume
855114942Sume		if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
856114942Sume		then
857114942Sume			case ${i} in
858163759Smlaier			lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*|pflog[0-9]*|pfsync[0-9]*)
859114942Sume				;;
860114942Sume			*)
861114942Sume				rtsol_interfaces="${rtsol_interfaces} ${i}"
862114942Sume				;;
863114942Sume			esac
864114942Sume		else
865114942Sume			ifconfig $i inet6
866114942Sume		fi
867114942Sume	done
868114942Sume
869114942Sume	if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
870114942Sume		# Act as endhost - automatically configured.
871114942Sume		# You can configure only single interface, as
872114942Sume		# specification assumes that autoconfigured host has
873114942Sume		# single interface only.
874114942Sume		sysctl net.inet6.ip6.accept_rtadv=1
875114942Sume		set ${rtsol_interfaces}
876114942Sume		ifconfig $1 up
877118666Sume		rtsol ${rtsol_flags} $1
878114942Sume	fi
879114942Sume
880114942Sume	for i in $interfaces; do
881114942Sume		alias=0
882114942Sume		while : ; do
883157706Sbrooks			ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
884114942Sume			if [ -z "${ipv6_ifconfig}" ]; then
885114942Sume				break;
886114942Sume			fi
887114942Sume			ifconfig $i inet6 ${ipv6_ifconfig} alias
888114942Sume			alias=$((${alias} + 1))
889114942Sume		done
890114942Sume	done
891114942Sume}
892114942Sume
893114942Sume# Setup IPv6 to IPv4 mapping
894114942Sumenetwork6_stf_setup()
895114942Sume{
896114942Sume	case ${stf_interface_ipv4addr} in
897114942Sume	[Nn][Oo] | '')
898114942Sume		;;
899114942Sume	*)
900114942Sume		# assign IPv6 addr and interface route for 6to4 interface
901114942Sume		stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
902114942Sume		OIFS="$IFS"
903114942Sume		IFS=".$IFS"
904114942Sume		set ${stf_interface_ipv4addr}
905114942Sume		IFS="$OIFS"
906114942Sume		hexfrag1=`hexprint $(($1*256 + $2))`
907114942Sume		hexfrag2=`hexprint $(($3*256 + $4))`
908114942Sume		ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
909114942Sume		case ${stf_interface_ipv6_ifid} in
910114942Sume		[Aa][Uu][Tt][Oo] | '')
911114942Sume			for i in ${ipv6_network_interfaces}; do
912114942Sume				laddr=`network6_getladdr ${i}`
913114942Sume				case ${laddr} in
914114942Sume				'')
915114942Sume					;;
916114942Sume				*)
917114942Sume					break
918114942Sume					;;
919114942Sume				esac
920114942Sume			done
921114942Sume			stf_interface_ipv6_ifid=`expr "${laddr}" : \
922114942Sume						      'fe80::\(.*\)%\(.*\)'`
923114942Sume			case ${stf_interface_ipv6_ifid} in
924114942Sume			'')
925114942Sume				stf_interface_ipv6_ifid=0:0:0:1
926114942Sume				;;
927114942Sume			esac
928114942Sume			;;
929114942Sume		esac
930114942Sume		ifconfig stf0 create >/dev/null 2>&1
931114942Sume		ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
932114942Sume			prefixlen ${stf_prefixlen}
933114942Sume		# disallow packets to malicious 6to4 prefix
934114942Sume		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
935114942Sume		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
936114942Sume		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
937114942Sume		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
938114942Sume		;;
939114942Sume	esac
940114942Sume}
941114942Sume
942114942Sume# Setup static routes
943114942Sumenetwork6_static_routes_setup()
944114942Sume{
945114942Sume	# Set up any static routes.
946114942Sume	case ${ipv6_defaultrouter} in
947114942Sume	[Nn][Oo] | '')
948114942Sume		;;
949114942Sume	*)
950114942Sume		ipv6_static_routes="default ${ipv6_static_routes}"
951114942Sume		ipv6_route_default="default ${ipv6_defaultrouter}"
952114942Sume		;;
953114942Sume	esac
954114942Sume	case ${ipv6_static_routes} in
955114942Sume	[Nn][Oo] | '')
956114942Sume		;;
957114942Sume	*)
958114942Sume		for i in ${ipv6_static_routes}; do
959157706Sbrooks			ipv6_route_args=`get_if_var $i ipv6_route_IF`
960114942Sume			route add -inet6 ${ipv6_route_args}
961114942Sume		done
962114942Sume		;;
963114942Sume	esac
964114942Sume}
965114942Sume
966114942Sume# Setup faith
967114942Sumenetwork6_faith_setup()
968114942Sume{
969114942Sume	case ${ipv6_faith_prefix} in
970114942Sume	[Nn][Oo] | '')
971114942Sume		;;
972114942Sume	*)
973114942Sume		sysctl net.inet6.ip6.keepfaith=1
974114942Sume		ifconfig faith0 create >/dev/null 2>&1
975114942Sume		ifconfig faith0 up
976114942Sume		for prefix in ${ipv6_faith_prefix}; do
977114942Sume			prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
978114942Sume			case ${prefixlen} in
979114942Sume			'')
980114942Sume				prefixlen=96
981114942Sume				;;
982114942Sume			*)
983114942Sume				prefix=`expr "${prefix}" : \
984114942Sume					     "\(.*\)/${prefixlen}"`
985114942Sume				;;
986114942Sume			esac
987114942Sume			route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
988114942Sume			route change -inet6 ${prefix} -prefixlen ${prefixlen} \
989114942Sume				-ifp faith0
990114942Sume		done
991114942Sume		;;
992114942Sume	esac
993114942Sume}
994114942Sume
995114942Sume# Install the "default interface" to kernel, which will be used
996114942Sume# as the default route when there's no router.
997114942Sumenetwork6_default_interface_setup()
998114942Sume{
999114942Sume	# Choose IPv6 default interface if it is not clearly specified.
1000114942Sume	case ${ipv6_default_interface} in
1001114942Sume	'')
1002114942Sume		for i in ${ipv6_network_interfaces}; do
1003114942Sume			case $i in
1004114942Sume			lo0|faith[0-9]*)
1005114942Sume				continue
1006114942Sume				;;
1007114942Sume			esac
1008114942Sume			laddr=`network6_getladdr $i exclude_tentative`
1009114942Sume			case ${laddr} in
1010114942Sume			'')
1011114942Sume				;;
1012114942Sume			*)
1013114942Sume				ipv6_default_interface=$i
1014114942Sume				break
1015114942Sume				;;
1016114942Sume			esac
1017114942Sume		done
1018114942Sume		;;
1019114942Sume	esac
1020114942Sume
1021114942Sume	# Disallow unicast packets without outgoing scope identifiers,
1022114942Sume	# or route such packets to a "default" interface, if it is specified.
1023114942Sume	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1024114942Sume	case ${ipv6_default_interface} in
1025114942Sume	[Nn][Oo] | '')
1026114942Sume		route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1027114942Sume		;;
1028114942Sume	*)
1029114942Sume		laddr=`network6_getladdr ${ipv6_default_interface}`
1030114942Sume		route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1031114942Sume			-cloning
1032114942Sume
1033114942Sume		# Disable installing the default interface with the
1034114942Sume		# case net.inet6.ip6.forwarding=0 and
1035114942Sume		# net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1036114942Sume		# between the default router list and the manual
1037114942Sume		# configured default route.
1038114942Sume		case ${ipv6_gateway_enable} in
1039114942Sume		[Yy][Ee][Ss])
1040114942Sume			;;
1041114942Sume		*)
1042114942Sume			if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1043114942Sume			then
1044114942Sume				ndp -I ${ipv6_default_interface}
1045114942Sume			fi
1046114942Sume			;;
1047114942Sume		esac
1048114942Sume		;;
1049114942Sume	esac
1050114942Sume}
1051114942Sume
1052114942Sumenetwork6_getladdr()
1053114942Sume{
1054114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
1055114942Sume		case ${proto} in
1056114942Sume		inet6)
1057114942Sume			case ${addr} in
1058114942Sume			fe80::*)
1059114942Sume				if [ -z "$2" ]; then
1060114942Sume					echo ${addr}
1061114942Sume					return
1062114942Sume				fi
1063114942Sume				case ${rest} in
1064114942Sume				*tentative*)
1065114942Sume					continue
1066114942Sume					;;
1067114942Sume				*)
1068114942Sume					echo ${addr}
1069114942Sume					return
1070114942Sume				esac
1071114942Sume			esac
1072114942Sume		esac
1073114942Sume	done
1074114942Sume}
1075