xref: /dflybsd-src/etc/network.subr (revision ae75c143462d5e92cbae686b9ad9b86ce29d38cd)
19c600e7dSMatthew Dillon#
29c600e7dSMatthew Dillon# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
39c600e7dSMatthew Dillon#
49c600e7dSMatthew Dillon# Redistribution and use in source and binary forms, with or without
59c600e7dSMatthew Dillon# modification, are permitted provided that the following conditions
69c600e7dSMatthew Dillon# are met:
79c600e7dSMatthew Dillon# 1. Redistributions of source code must retain the above copyright
89c600e7dSMatthew Dillon#    notice, this list of conditions and the following disclaimer.
99c600e7dSMatthew Dillon# 2. Redistributions in binary form must reproduce the above copyright
109c600e7dSMatthew Dillon#    notice, this list of conditions and the following disclaimer in the
119c600e7dSMatthew Dillon#    documentation and/or other materials provided with the distribution.
129c600e7dSMatthew Dillon#
139c600e7dSMatthew Dillon# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
149c600e7dSMatthew Dillon# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
159c600e7dSMatthew Dillon# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
169c600e7dSMatthew Dillon# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
179c600e7dSMatthew Dillon# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
189c600e7dSMatthew Dillon# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199c600e7dSMatthew Dillon# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
209c600e7dSMatthew Dillon# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
219c600e7dSMatthew Dillon# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
229c600e7dSMatthew Dillon# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
239c600e7dSMatthew Dillon# SUCH DAMAGE.
249c600e7dSMatthew Dillon#
25f26c267aSSascha Wildner# $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
269c600e7dSMatthew Dillon#
279c600e7dSMatthew Dillon
289c600e7dSMatthew Dillon#
299c600e7dSMatthew Dillon# Subroutines commonly used from network startup scripts.
30e4b0f1d1SAaron LI# Requires that /etc/rc.subr be loaded first.
319c600e7dSMatthew Dillon#
329c600e7dSMatthew Dillon
339c600e7dSMatthew Dillon# ifconfig_up if
349c600e7dSMatthew Dillon#	Evaluate ifconfig(8) arguments for interface $if and
359c600e7dSMatthew Dillon#	run ifconfig(8) with those arguments. It returns 0 if
369c600e7dSMatthew Dillon#	arguments were found and executed or 1 if the interface
37f26c267aSSascha Wildner#	had no arguments.  Pseudo arguments DHCP and WPA are handled
38f26c267aSSascha Wildner#	here.
399c600e7dSMatthew Dillon#
409c600e7dSMatthew Dillonifconfig_up()
419c600e7dSMatthew Dillon{
42e4b0f1d1SAaron LI	local _cfg ifconfig_args
43f26c267aSSascha Wildner	_cfg=1
44f26c267aSSascha Wildner
45f26c267aSSascha Wildner	ifconfig_args=`ifconfig_getargs $1`
469c600e7dSMatthew Dillon	if [ -n "${ifconfig_args}" ]; then
479c600e7dSMatthew Dillon		ifconfig $1 ${ifconfig_args}
485ad36ff1SAaron LI		ifconfig $1 up
49f26c267aSSascha Wildner		_cfg=0
509c600e7dSMatthew Dillon	fi
51f26c267aSSascha Wildner
52f26c267aSSascha Wildner	if wpaif $1; then
535ad36ff1SAaron LI		ifconfig $1 up
54f26c267aSSascha Wildner		/etc/rc.d/wpa_supplicant start $1
55e9136cbfSAaron LI		# NOTE: wpa_supplicant(8) needs to control the interface's
56e9136cbfSAaron LI		#	state in order to perform the SSID scan.  But
57e9136cbfSAaron LI		#	dhcpcd(8), which may be started by the "dhcp_client"
58e9136cbfSAaron LI		#	below, can race against wpa_supplicant(8) and modify
59e9136cbfSAaron LI		#	the interface's state, breaking the SSID scan and
60e9136cbfSAaron LI		#	preventing the SSID association.
61e9136cbfSAaron LI		#	Insert a small delay here to workaround the issue.
62e9136cbfSAaron LI		sleep 1
63f26c267aSSascha Wildner		_cfg=0		# XXX: not sure this should count
64f26c267aSSascha Wildner	fi
65f26c267aSSascha Wildner
66f26c267aSSascha Wildner	if dhcpif $1; then
67817cf585SAaron LI		/etc/rc.d/dhcp_client start $1
68f26c267aSSascha Wildner		_cfg=0
69f26c267aSSascha Wildner	fi
70f26c267aSSascha Wildner
71f26c267aSSascha Wildner	return $_cfg
729c600e7dSMatthew Dillon}
739c600e7dSMatthew Dillon
749c600e7dSMatthew Dillon# ifconfig_down if
759c600e7dSMatthew Dillon#	Remove all inet entries from the $if interface. It returns
769c600e7dSMatthew Dillon#	0 if inet entries were found and removed. It returns 1 if
779c600e7dSMatthew Dillon#	no entries were found or they could not be removed.
789c600e7dSMatthew Dillon#
799c600e7dSMatthew Dillonifconfig_down()
809c600e7dSMatthew Dillon{
81e4b0f1d1SAaron LI	local _cfg _ifs oldifs _inet inetList
82e4b0f1d1SAaron LI
839c600e7dSMatthew Dillon	[ -z "$1" ] && return 1
849c600e7dSMatthew Dillon	_ifs="^"
85f26c267aSSascha Wildner	_cfg=1
869c600e7dSMatthew Dillon
879c600e7dSMatthew Dillon	inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
889c600e7dSMatthew Dillon
899c600e7dSMatthew Dillon	oldifs="$IFS"
909c600e7dSMatthew Dillon	IFS="$_ifs"
919c600e7dSMatthew Dillon	for _inet in $inetList ; do
929c600e7dSMatthew Dillon		# get rid of extraneous line
939c600e7dSMatthew Dillon		[ -z "$_inet" ] && break
949c600e7dSMatthew Dillon
959c600e7dSMatthew Dillon		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
969c600e7dSMatthew Dillon
979c600e7dSMatthew Dillon		IFS="$oldifs"
989c600e7dSMatthew Dillon		ifconfig $1 ${_inet} delete
999c600e7dSMatthew Dillon		IFS="$_ifs"
100f26c267aSSascha Wildner		_cfg=0
1019c600e7dSMatthew Dillon	done
1029c600e7dSMatthew Dillon	IFS="$oldifs"
1039c600e7dSMatthew Dillon
104f26c267aSSascha Wildner	if wpaif $1; then
105f26c267aSSascha Wildner		/etc/rc.d/wpa_supplicant stop $1
106f26c267aSSascha Wildner	fi
107f26c267aSSascha Wildner
108f26c267aSSascha Wildner	if dhcpif $1; then
109817cf585SAaron LI		/etc/rc.d/dhcp_client stop $1
110f26c267aSSascha Wildner		_cfg=0
111f26c267aSSascha Wildner	fi
112f26c267aSSascha Wildner
1135ad36ff1SAaron LI	if ifexists $1; then
1145ad36ff1SAaron LI		ifconfig $1 down
1155ad36ff1SAaron LI		_cfg=0
1165ad36ff1SAaron LI	fi
1175ad36ff1SAaron LI
118f26c267aSSascha Wildner	return $_cfg
119f26c267aSSascha Wildner}
120f26c267aSSascha Wildner
121d86e211fSAlex Hornung# get_if_var if var [default]
122d86e211fSAlex Hornung#	Return the value of the pseudo-hash corresponding to $if where
123d86e211fSAlex Hornung#	$var is a string containg the sub-string "IF" which will be
124d86e211fSAlex Hornung#	replaced with $if after the characters defined in _punct are
125d86e211fSAlex Hornung#	replaced with '_'. If the variable is unset, replace it with
126d86e211fSAlex Hornung#	$default if given.
127e4b0f1d1SAaron LI#
128d86e211fSAlex Hornungget_if_var()
129d86e211fSAlex Hornung{
130d86e211fSAlex Hornung	local _if _punct_c _punct _var _default prefix suffix
131d86e211fSAlex Hornung
132d86e211fSAlex Hornung	if [ $# -ne 2 -a $# -ne 3 ]; then
133d86e211fSAlex Hornung		err 3 'USAGE: get_if_var name var [default]'
134d86e211fSAlex Hornung	fi
135d86e211fSAlex Hornung
136d86e211fSAlex Hornung	_if=$1
137d86e211fSAlex Hornung	_punct=". - / +"
138d86e211fSAlex Hornung	for _punct_c in $_punct; do
139d86e211fSAlex Hornung		_if=`ltr ${_if} ${_punct_c} '_'`
140d86e211fSAlex Hornung	done
141d86e211fSAlex Hornung	_var=$2
142d86e211fSAlex Hornung	_default=$3
143d86e211fSAlex Hornung
144d86e211fSAlex Hornung	prefix=${_var%%IF*}
145d86e211fSAlex Hornung	suffix=${_var##*IF}
146d86e211fSAlex Hornung	eval echo \${${prefix}${_if}${suffix}-${_default}}
147d86e211fSAlex Hornung}
148d86e211fSAlex Hornung
1498cd2e074SAaron LI# _ifconfig_getargs if [af]
150f26c267aSSascha Wildner#	Echos the arguments for the supplied interface to stdout.
151e4b0f1d1SAaron LI#	Returns 1 if no interface is specified.
152e4b0f1d1SAaron LI#	In general, the ifconfig_getargs() below should be used outside
153e4b0f1d1SAaron LI#	this file.
154e4b0f1d1SAaron LI#
155f26c267aSSascha Wildner_ifconfig_getargs()
156f26c267aSSascha Wildner{
157e4b0f1d1SAaron LI	local _if _ifn _af _args
158e4b0f1d1SAaron LI
159f26c267aSSascha Wildner	_ifn=$1
1608cd2e074SAaron LI	_af=${2:+${2}_}
1618cd2e074SAaron LI
162f26c267aSSascha Wildner	if [ -z "$_ifn" ]; then
163f26c267aSSascha Wildner		return 1
164f26c267aSSascha Wildner	fi
165f26c267aSSascha Wildner
1668cd2e074SAaron LI	_args=`get_if_var $_ifn ${_af}ifconfig_IF`
167f26c267aSSascha Wildner	if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
168f26c267aSSascha Wildner		for _if in ${removable_interfaces} ; do
169f26c267aSSascha Wildner			if [ "$_if" = "$_ifn" ] ; then
170f26c267aSSascha Wildner				_args=${pccard_ifconfig}
171f26c267aSSascha Wildner				break
172f26c267aSSascha Wildner			fi
173f26c267aSSascha Wildner		done
174f26c267aSSascha Wildner	fi
175f26c267aSSascha Wildner
176f26c267aSSascha Wildner	echo $_args
177f26c267aSSascha Wildner}
178f26c267aSSascha Wildner
1798cd2e074SAaron LI# ifconfig_getargs if [af]
180e4b0f1d1SAaron LI#	Takes the result from _ifconfig_getargs() and removes pseudo
181f26c267aSSascha Wildner#	args such as DHCP and WPA.
182e4b0f1d1SAaron LI#
183f26c267aSSascha Wildnerifconfig_getargs()
184f26c267aSSascha Wildner{
185e4b0f1d1SAaron LI	local _tmpargs _arg _args is_optarg
186e4b0f1d1SAaron LI
1878cd2e074SAaron LI	_tmpargs=`_ifconfig_getargs $1 $2`
188f26c267aSSascha Wildner	if [ $? -eq 1 ]; then
189f26c267aSSascha Wildner		return 1
190f26c267aSSascha Wildner	fi
191f26c267aSSascha Wildner	_args=
192f26c267aSSascha Wildner
1931f03f592SSascha Wildner	is_optarg=no
194f26c267aSSascha Wildner	for _arg in $_tmpargs; do
1951f03f592SSascha Wildner		if [ "$is_optarg" = "no" ]; then
196f26c267aSSascha Wildner			case $_arg in
197f26c267aSSascha Wildner			[Dd][Hh][Cc][Pp])
198f26c267aSSascha Wildner				;;
199f26c267aSSascha Wildner			[Ww][Pp][Aa])
200f26c267aSSascha Wildner				;;
201f26c267aSSascha Wildner			*)
202f26c267aSSascha Wildner				_args="$_args $_arg"
2031f03f592SSascha Wildner				case $_arg in
2041f03f592SSascha Wildner				authmode)
2051f03f592SSascha Wildner					is_optarg=yes
206f26c267aSSascha Wildner					;;
207f26c267aSSascha Wildner				esac
2081f03f592SSascha Wildner				;;
2091f03f592SSascha Wildner			esac
2101f03f592SSascha Wildner		else
2111f03f592SSascha Wildner			_args="$_args $_arg"
2121f03f592SSascha Wildner			is_optarg=no
2131f03f592SSascha Wildner		fi
214f26c267aSSascha Wildner	done
215f26c267aSSascha Wildner
216f26c267aSSascha Wildner	echo $_args
217f26c267aSSascha Wildner}
218f26c267aSSascha Wildner
219da4dd514SAaron LI# ipv6if if
220da4dd514SAaron LI#	Returns 0 if the interface should be configured for IPv6 and
221da4dd514SAaron LI#	1 otherwise.
222e4b0f1d1SAaron LI#
223da4dd514SAaron LIipv6if()
224da4dd514SAaron LI{
225da4dd514SAaron LI	local _if _tmpargs
226da4dd514SAaron LI	_if=$1
227da4dd514SAaron LI
228da4dd514SAaron LI	# lo0 is always IPv6-enabled
229e4b0f1d1SAaron LI	if [ "$_if" = "lo0" ]; then
230da4dd514SAaron LI		return 0
231e4b0f1d1SAaron LI	fi
232da4dd514SAaron LI
233da4dd514SAaron LI	case ${ipv6_enable} in
234da4dd514SAaron LI	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
235da4dd514SAaron LI		return 1
236da4dd514SAaron LI		;;
237da4dd514SAaron LI	esac
238da4dd514SAaron LI
239da4dd514SAaron LI	case "${ipv6_network_interfaces}" in
240da4dd514SAaron LI	$_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
241da4dd514SAaron LI		# True if $ipv6_ifconfig_IF is defined.
242da4dd514SAaron LI		_tmpargs=`_ifconfig_getargs $_if ipv6`
243da4dd514SAaron LI		if [ -n "${_tmpargs}" ]; then
244da4dd514SAaron LI			return 0
245da4dd514SAaron LI		fi
246da4dd514SAaron LI
247da4dd514SAaron LI		# True if $ipv6_prefix_IF is defined.
248da4dd514SAaron LI		_tmpargs=`get_if_var $_if ipv6_prefix_IF`
249da4dd514SAaron LI		if [ -n "${_tmpargs}" ]; then
250da4dd514SAaron LI			return 0
251da4dd514SAaron LI		fi
252da4dd514SAaron LI
253da4dd514SAaron LI		;;
254da4dd514SAaron LI	esac
255da4dd514SAaron LI
256da4dd514SAaron LI	return 1
257da4dd514SAaron LI}
258da4dd514SAaron LI
259329743deSAaron LI# dhcpif if [ipv4|ipv6]
260329743deSAaron LI#	Returns 0 if the interface needs DHCP for IPv4/IPv6 and 1 otherwise.
261329743deSAaron LI#	If the second argument is "ipv4" (or "ipv6"), then only IPv4 (or
262329743deSAaron LI#	IPv6) is checked, otherwise both are checked.
263e4b0f1d1SAaron LI#
264f26c267aSSascha Wildnerdhcpif()
265f26c267aSSascha Wildner{
266329743deSAaron LI	local _tmpargs _arg _if _af
267329743deSAaron LI	_if=$1
268329743deSAaron LI	_af=$2
269329743deSAaron LI
270329743deSAaron LI	if [ -z "$_af" -o "$_af" = "ipv4" ]; then
271329743deSAaron LI		_tmpargs=`_ifconfig_getargs $_if`
272f26c267aSSascha Wildner		for _arg in $_tmpargs; do
273f26c267aSSascha Wildner			case $_arg in
274f26c267aSSascha Wildner			[Dd][Hh][Cc][Pp])
275f26c267aSSascha Wildner				return 0
276f26c267aSSascha Wildner				;;
277f26c267aSSascha Wildner			esac
278f26c267aSSascha Wildner		done
279329743deSAaron LI	fi
280329743deSAaron LI
281329743deSAaron LI	if [ -z "$_af" -o "$_af" = "ipv6" ] && ipv6if $_if; then
282329743deSAaron LI		_tmpargs=`_ifconfig_getargs $_if ipv6`
283329743deSAaron LI		for _arg in $_tmpargs; do
284329743deSAaron LI			case $_arg in
285329743deSAaron LI			[Dd][Hh][Cc][Pp])
286329743deSAaron LI				return 0
287329743deSAaron LI				;;
288329743deSAaron LI			esac
289329743deSAaron LI		done
290329743deSAaron LI	fi
291329743deSAaron LI
292f26c267aSSascha Wildner	return 1
293f26c267aSSascha Wildner}
294f26c267aSSascha Wildner
295f26c267aSSascha Wildner# wpaif if
296f26c267aSSascha Wildner#	Returns 0 if the interface is a WPA interface and 1 otherwise.
297e4b0f1d1SAaron LI#
298f26c267aSSascha Wildnerwpaif()
299f26c267aSSascha Wildner{
300e4b0f1d1SAaron LI	local _tmpargs _arg is_optarg
301e4b0f1d1SAaron LI
302f26c267aSSascha Wildner	_tmpargs=`_ifconfig_getargs $1`
3031f03f592SSascha Wildner	is_optarg=no
304f26c267aSSascha Wildner	for _arg in $_tmpargs; do
3051f03f592SSascha Wildner		if [ "$is_optarg" = "no" ]; then
306f26c267aSSascha Wildner			case $_arg in
307f26c267aSSascha Wildner			[Ww][Pp][Aa])
308f26c267aSSascha Wildner				return 0
309f26c267aSSascha Wildner				;;
3101f03f592SSascha Wildner			authmode)
3111f03f592SSascha Wildner				is_optarg=yes
3121f03f592SSascha Wildner				;;
313f26c267aSSascha Wildner			esac
3141f03f592SSascha Wildner		else
3151f03f592SSascha Wildner			is_optarg=no
3161f03f592SSascha Wildner		fi
317f26c267aSSascha Wildner	done
318e4b0f1d1SAaron LI
319f26c267aSSascha Wildner	return 1
3209c600e7dSMatthew Dillon}
3219c600e7dSMatthew Dillon
322cc419283SAaron LI# ifexists if
323cc419283SAaron LI#	Returns 0 if the interface exists and 1 otherwise.
324e4b0f1d1SAaron LI#
325cc419283SAaron LIifexists()
326cc419283SAaron LI{
327cc419283SAaron LI	[ -z "$1" ] && return 1
328e4b0f1d1SAaron LI	ifconfig -n $1 >/dev/null 2>&1
329cc419283SAaron LI}
330cc419283SAaron LI
3313b4e0f2aSAaron LI# ifalias_common if action [ipv6]
3323b4e0f2aSAaron LI#	Helper function for ifalias_up() and ifalias_down().
3333b4e0f2aSAaron LI#	The $action argument can be either "alias" (to add an
3343b4e0f2aSAaron LI#	alias) or "-alias" (to remove an alias).
3353b4e0f2aSAaron LI#	Returns 0 if at least one alias was added/removed or
3363b4e0f2aSAaron LI#	1 if there were none.
3373b4e0f2aSAaron LI#
3383b4e0f2aSAaron LIifalias_common()
3393b4e0f2aSAaron LI{
3403b4e0f2aSAaron LI	local _if _action _af _af2 _ret _var _args _alias
3413b4e0f2aSAaron LI	_if=$1
3423b4e0f2aSAaron LI	_action=$2
3433b4e0f2aSAaron LI	_af=$3
3443b4e0f2aSAaron LI
3453b4e0f2aSAaron LI	_ret=1
3463b4e0f2aSAaron LI	_alias=0
3473b4e0f2aSAaron LI	while : ; do
3483b4e0f2aSAaron LI		if [ "${_af}" = "ipv6" ]; then
3493b4e0f2aSAaron LI			_af2="inet6"
3503b4e0f2aSAaron LI			_var="ipv6_ifconfig_IF_alias${_alias}"
3513b4e0f2aSAaron LI		else
3523b4e0f2aSAaron LI			_af2="inet"
3533b4e0f2aSAaron LI			_var="ifconfig_IF_alias${_alias}"
3543b4e0f2aSAaron LI		fi
3553b4e0f2aSAaron LI		_args=`get_if_var $_if $_var`
3563b4e0f2aSAaron LI		_args="${_args#${_af2} }"
3573b4e0f2aSAaron LI		if [ -z "${_args}" ]; then
3583b4e0f2aSAaron LI			break
3593b4e0f2aSAaron LI		fi
3603b4e0f2aSAaron LI		ifconfig $_if $_af2 $_args $_action
3613b4e0f2aSAaron LI		_alias=$((${_alias} + 1))
3623b4e0f2aSAaron LI		_ret=0
3633b4e0f2aSAaron LI	done
3643b4e0f2aSAaron LI	return $_ret
3653b4e0f2aSAaron LI}
3663b4e0f2aSAaron LI
3673b4e0f2aSAaron LI# ifalias_up if [ipv6]
3683b4e0f2aSAaron LI#	Configure IPv4 aliases for network interface $if or
3693b4e0f2aSAaron LI#	IPv6 aliases if the second argument is "ipv6".
3709c600e7dSMatthew Dillon#	It returns 0 if at least one alias was configured or
3719c600e7dSMatthew Dillon#	1 if there were none.
3729c600e7dSMatthew Dillon#
3739c600e7dSMatthew Dillonifalias_up()
3749c600e7dSMatthew Dillon{
3753b4e0f2aSAaron LI	ifalias_common $1 alias $2
3769c600e7dSMatthew Dillon}
3779c600e7dSMatthew Dillon
3783b4e0f2aSAaron LI# ifalias_down if [ipv6]
3793b4e0f2aSAaron LI#	Remove IPv4 aliases for network interface $if or
3803b4e0f2aSAaron LI#	IPv6 aliases if the second argument is "ipv6".
3819c600e7dSMatthew Dillon#	It returns 0 if at least one alias was removed or
3829c600e7dSMatthew Dillon#	1 if there were none.
3839c600e7dSMatthew Dillon#
3849c600e7dSMatthew Dillonifalias_down()
3859c600e7dSMatthew Dillon{
3863b4e0f2aSAaron LI	ifalias_common $1 -alias $2
3879c600e7dSMatthew Dillon}
3889c600e7dSMatthew Dillon
3899c600e7dSMatthew Dillon# ifscript_up if
3909c600e7dSMatthew Dillon#	Evaluate a startup script for the $if interface.
3919c600e7dSMatthew Dillon#	It returns 0 if a script was found and processed or
3929c600e7dSMatthew Dillon#	1 if no script was found.
3939c600e7dSMatthew Dillon#
3949c600e7dSMatthew Dillonifscript_up()
3959c600e7dSMatthew Dillon{
3969c600e7dSMatthew Dillon	if [ -r /etc/start_if.$1 ]; then
3979c600e7dSMatthew Dillon		. /etc/start_if.$1
3989c600e7dSMatthew Dillon		return 0
3999c600e7dSMatthew Dillon	fi
4009c600e7dSMatthew Dillon	return 1
4019c600e7dSMatthew Dillon}
4029c600e7dSMatthew Dillon
4039c600e7dSMatthew Dillon# ifscript_down if
4049c600e7dSMatthew Dillon#	Evaluate a shutdown script for the $if interface.
4059c600e7dSMatthew Dillon#	It returns 0 if a script was found and processed or
4069c600e7dSMatthew Dillon#	1 if no script was found.
4079c600e7dSMatthew Dillon#
4089c600e7dSMatthew Dillonifscript_down()
4099c600e7dSMatthew Dillon{
4109c600e7dSMatthew Dillon	if [ -r /etc/stop_if.$1 ]; then
4119c600e7dSMatthew Dillon		. /etc/stop_if.$1
4129c600e7dSMatthew Dillon		return 0
4139c600e7dSMatthew Dillon	fi
4149c600e7dSMatthew Dillon	return 1
4159c600e7dSMatthew Dillon}
4169c600e7dSMatthew Dillon
41769dc5784SDaniel Fojt# wlan_get_unused
41869dc5784SDaniel Fojt# walk through net.wlan and find unused device that can be created
41969dc5784SDaniel Fojt#
42069dc5784SDaniel Fojtwlan_get_unused()
42169dc5784SDaniel Fojt{
42269dc5784SDaniel Fojt	local idx
42369dc5784SDaniel Fojt
42469dc5784SDaniel Fojt	idx=0
42569dc5784SDaniel Fojt
42669dc5784SDaniel Fojt	while : ; do
42769dc5784SDaniel Fojt		if ! ${SYSCTL_N} -q net.wlan.${idx}.%parent >/dev/null; then
42869dc5784SDaniel Fojt			echo "wlan${idx}"
42969dc5784SDaniel Fojt			break
43069dc5784SDaniel Fojt		fi
43169dc5784SDaniel Fojt		idx=$((${idx} + 1))
43269dc5784SDaniel Fojt	done
43369dc5784SDaniel Fojt}
43469dc5784SDaniel Fojt
43569dc5784SDaniel Fojt# wlan_is_parent
43669dc5784SDaniel Fojt# check if given interface is parent for any existing wlan device
43769dc5784SDaniel Fojt#
43869dc5784SDaniel Fojtwlan_is_parent()
43969dc5784SDaniel Fojt{
44069dc5784SDaniel Fojt	sysctl -q net.wlan | grep -q "%parent: ${1}"
44169dc5784SDaniel Fojt}
44269dc5784SDaniel Fojt
4433928a593SAaron LI# wlan_up
4443928a593SAaron LI#	Create IEEE 802.11 interfaces.
4453928a593SAaron LI#
4463928a593SAaron LIwlan_up()
4473928a593SAaron LI{
4483928a593SAaron LI	local _prefix _list parent child child_wlans create_args debug_flags
4493928a593SAaron LI	_prefix=
4503928a593SAaron LI	_list=
4513928a593SAaron LI
45269dc5784SDaniel Fojt	local _rcconf _auto
45369dc5784SDaniel Fojt	_rcconf=""
45469dc5784SDaniel Fojt	_auto=""
45569dc5784SDaniel Fojt
456*4c258131SDaniel Fojt	local wlan_devices
457*4c258131SDaniel Fojt	if [ -n "$1" ]; then
458*4c258131SDaniel Fojt		wlan_devices="$1"
459*4c258131SDaniel Fojt	else
460*4c258131SDaniel Fojt		wlan_devices="`${SYSCTL_N} -q net.wlan.devices`"
461*4c258131SDaniel Fojt	fi
462*4c258131SDaniel Fojt
46369dc5784SDaniel Fojt	# Order detected devices so that interfaces configured via rc.conf are
46469dc5784SDaniel Fojt	# created first, and then all other devices are automatically assigned
465*4c258131SDaniel Fojt	for parent in ${wlan_devices}; do
46669dc5784SDaniel Fojt		child_wlans=`get_if_var $parent wlans_IF`
46769dc5784SDaniel Fojt		if [ -n "${child_wlans}" ]; then
46869dc5784SDaniel Fojt			_rcconf="${_rcconf} ${parent}"
46969dc5784SDaniel Fojt		else
47069dc5784SDaniel Fojt			_auto="${_auto} ${parent}"
47169dc5784SDaniel Fojt		fi
47269dc5784SDaniel Fojt	done
47369dc5784SDaniel Fojt
47469dc5784SDaniel Fojt	for parent in ${_rcconf} ${_auto}; do
47569dc5784SDaniel Fojt		if wlan_is_parent $parent; then
47669dc5784SDaniel Fojt			continue
47769dc5784SDaniel Fojt		fi
4783928a593SAaron LI		# Parse wlans_$parent="$child ..."
4793928a593SAaron LI		child_wlans=`get_if_var $parent wlans_IF`
48069dc5784SDaniel Fojt		# Or find first unused wlan device to create
48169dc5784SDaniel Fojt		if [ -z "${child_wlans}" ]; then
48269dc5784SDaniel Fojt			child_wlans=`wlan_get_unused`
48369dc5784SDaniel Fojt		fi
4843928a593SAaron LI		for child in ${child_wlans}; do
4853928a593SAaron LI			if ifexists $child; then
4863928a593SAaron LI				continue
4873928a593SAaron LI			fi
4883928a593SAaron LI
4893928a593SAaron LI			create_args="wlandev $parent `get_if_var $child create_args_IF`"
4903928a593SAaron LI			debug_flags="`get_if_var $child wlandebug_IF`"
4913928a593SAaron LI			if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
4923928a593SAaron LI				ifconfig $child create ${create_args}
4933928a593SAaron LI			else
4943928a593SAaron LI				ifconfig wlan create ${create_args} name $child
4953928a593SAaron LI			fi
4963928a593SAaron LI			if [ $? -eq 0 ]; then
4973928a593SAaron LI				_list="${_list}${_prefix}${child}"
4983928a593SAaron LI				[ -z "$_prefix" ] && _prefix=' '
4993928a593SAaron LI			fi
5003928a593SAaron LI			if [ -n "${debug_flags}" ]; then
5013928a593SAaron LI				wlandebug -i $child ${debug_flags}
5023928a593SAaron LI			fi
5033928a593SAaron LI		done
5043928a593SAaron LI	done
5053928a593SAaron LI
5063928a593SAaron LI	if [ -n "${_list}" ]; then
5073928a593SAaron LI		echo "Created wlan interfaces: ${_list}"
5083928a593SAaron LI	fi
5093928a593SAaron LI	debug "Created wlan interfaces: ${_list}"
5103928a593SAaron LI}
5113928a593SAaron LI
5123928a593SAaron LI# wlan_down
5133928a593SAaron LI#	Destroy IEEE 802.11 interfaces.
5143928a593SAaron LI#
5153928a593SAaron LIwlan_down()
5163928a593SAaron LI{
5173928a593SAaron LI	local _prefix _list parent child child_wlans
5183928a593SAaron LI	_prefix=
5193928a593SAaron LI	_list=
5203928a593SAaron LI
521*4c258131SDaniel Fojt	local wlan_devices
522*4c258131SDaniel Fojt	if [ -n "$1" ]; then
523*4c258131SDaniel Fojt		wlan_devices="$1"
524*4c258131SDaniel Fojt	else
525*4c258131SDaniel Fojt		wlan_devices="`${SYSCTL_N} -q net.wlan.devices`"
526*4c258131SDaniel Fojt	fi
527*4c258131SDaniel Fojt
528*4c258131SDaniel Fojt	for parent in ${wlan_devices}; do
5293928a593SAaron LI		child_wlans=`get_if_var $parent wlans_IF`
5303928a593SAaron LI		for child in ${child_wlans}; do
5313928a593SAaron LI			if ! ifexists $child; then
5323928a593SAaron LI				continue
5333928a593SAaron LI			fi
5343928a593SAaron LI
5353928a593SAaron LI			ifconfig -n $child destroy
5363928a593SAaron LI			if [ $? -eq 0 ]; then
5373928a593SAaron LI				_list="${_list}${_prefix}${child}"
5383928a593SAaron LI				[ -z "$_prefix" ] && _prefix=' '
5393928a593SAaron LI			fi
5403928a593SAaron LI		done
5413928a593SAaron LI	done
5423928a593SAaron LI
5433928a593SAaron LI	if [ -n "${_list}" ]; then
5443928a593SAaron LI		echo "Destroyed wlan interfaces: ${_list}"
5453928a593SAaron LI	fi
5463928a593SAaron LI	debug "Destroyed wlan interfaces: ${_list}"
5473928a593SAaron LI}
5483928a593SAaron LI
549e4b0f1d1SAaron LI# clone_up
5509c600e7dSMatthew Dillon#	Create cloneable interfaces.
5519c600e7dSMatthew Dillon#
5529c600e7dSMatthew Dillonclone_up()
5539c600e7dSMatthew Dillon{
554e4b0f1d1SAaron LI	local _prefix _list ifn
5559c600e7dSMatthew Dillon	_prefix=
5569c600e7dSMatthew Dillon	_list=
557e4b0f1d1SAaron LI
5589c600e7dSMatthew Dillon	for ifn in ${cloned_interfaces}; do
5599c600e7dSMatthew Dillon		ifconfig ${ifn} create
5609c600e7dSMatthew Dillon		if [ $? -eq 0 ]; then
5619c600e7dSMatthew Dillon			_list="${_list}${_prefix}${ifn}"
5629c600e7dSMatthew Dillon			[ -z "$_prefix" ] && _prefix=' '
5639c600e7dSMatthew Dillon		fi
5649c600e7dSMatthew Dillon	done
565e4b0f1d1SAaron LI	if [ -n "${_list}" ]; then
566e4b0f1d1SAaron LI		echo "Created clone interfaces: ${_list}"
567e4b0f1d1SAaron LI	fi
568e4b0f1d1SAaron LI	debug "Created clone interfaces: ${_list}"
5699c600e7dSMatthew Dillon}
5709c600e7dSMatthew Dillon
571e4b0f1d1SAaron LI# clone_down
572e4b0f1d1SAaron LI#	Destroy cloned interfaces.
5739c600e7dSMatthew Dillon#
5749c600e7dSMatthew Dillonclone_down()
5759c600e7dSMatthew Dillon{
576e4b0f1d1SAaron LI	local _prefix _list ifn
5779c600e7dSMatthew Dillon	_prefix=
5789c600e7dSMatthew Dillon	_list=
579e4b0f1d1SAaron LI
5809c600e7dSMatthew Dillon	for ifn in ${cloned_interfaces}; do
5819c600e7dSMatthew Dillon		ifconfig ${ifn} destroy
5829c600e7dSMatthew Dillon		if [ $? -eq 0 ]; then
5839c600e7dSMatthew Dillon			_list="${_list}${_prefix}${ifn}"
5849c600e7dSMatthew Dillon			[ -z "$_prefix" ] && _prefix=' '
5859c600e7dSMatthew Dillon		fi
5869c600e7dSMatthew Dillon	done
587e4b0f1d1SAaron LI	if [ -n "${_list}" ]; then
588e4b0f1d1SAaron LI		echo "Destroyed clone interfaces: ${_list}"
589e4b0f1d1SAaron LI	fi
590e4b0f1d1SAaron LI	debug "Destroyed clone interfaces: ${_list}"
5919c600e7dSMatthew Dillon}
5929c600e7dSMatthew Dillon
593e4b0f1d1SAaron LI# gif_up
594e4b0f1d1SAaron LI#	Create IPv6<-->IPv4 tunnels
595e4b0f1d1SAaron LI#
5969c600e7dSMatthew Dillongif_up() {
597e4b0f1d1SAaron LI	local _if _peers
598e4b0f1d1SAaron LI
5999c600e7dSMatthew Dillon	case ${gif_interfaces} in
6009c600e7dSMatthew Dillon	[Nn][Oo] | '')
601e4b0f1d1SAaron LI		return
6029c600e7dSMatthew Dillon		;;
603e4b0f1d1SAaron LI	esac
604e4b0f1d1SAaron LI
605e4b0f1d1SAaron LI	for _if in ${gif_interfaces}; do
606e4b0f1d1SAaron LI		eval _peers=\$gifconfig_${_if}
607e4b0f1d1SAaron LI		case ${_peers} in
6089c600e7dSMatthew Dillon		'')
6099c600e7dSMatthew Dillon			continue
6109c600e7dSMatthew Dillon			;;
6119c600e7dSMatthew Dillon		*)
612e4b0f1d1SAaron LI			ifconfig $_if create >/dev/null 2>&1
613e4b0f1d1SAaron LI			ifconfig $_if tunnel ${_peers}
614e4b0f1d1SAaron LI			ifconfig $_if up
6159c600e7dSMatthew Dillon			;;
6169c600e7dSMatthew Dillon		esac
6179c600e7dSMatthew Dillon	done
6189c600e7dSMatthew Dillon}
6199c600e7dSMatthew Dillon
620f26c267aSSascha Wildner# ifnet_rename
621f26c267aSSascha Wildner#	Rename all requested interfaces.
622f26c267aSSascha Wildner#
623f26c267aSSascha Wildnerifnet_rename()
624f26c267aSSascha Wildner{
625e4b0f1d1SAaron LI	local _ifn_list _if _ifname
626f26c267aSSascha Wildner
627e4b0f1d1SAaron LI	_ifn_list=$(ifconfig -l)
628f26c267aSSascha Wildner	[ -z "$_ifn_list" ] && return 0
629e4b0f1d1SAaron LI
630f26c267aSSascha Wildner	for _if in ${_ifn_list} ; do
631a939f930SAlex Hornung		_ifname=`get_if_var $_if ifconfig_IF_name`
632e4b0f1d1SAaron LI		if [ -n "$_ifname" ]; then
633f26c267aSSascha Wildner			ifconfig $_if name $_ifname
634f26c267aSSascha Wildner		fi
635f26c267aSSascha Wildner	done
636f26c267aSSascha Wildner	return 0
637f26c267aSSascha Wildner}
638f26c267aSSascha Wildner
6390e387345SAaron LI# list_net_interfaces
6400e387345SAaron LI#	List all network interfaces.
641acba6ed7SSimon Schubert#	Note that the list will include cloned interfaces if applicable.
642acba6ed7SSimon Schubert#	Cloned interfaces must already exist to have a chance to appear
643acba6ed7SSimon Schubert#	in the list if ${network_interfaces} is set to `auto'.
6449c600e7dSMatthew Dillon#
6459c600e7dSMatthew Dillonlist_net_interfaces()
6469c600e7dSMatthew Dillon{
6470e387345SAaron LI	local _tmplist _autolist _lo _if
6489c600e7dSMatthew Dillon
6499c600e7dSMatthew Dillon	case ${network_interfaces} in
6509c600e7dSMatthew Dillon	[Aa][Uu][Tt][Oo])
6513928a593SAaron LI		_autolist=$(ifconfig -l)
652667713feSHasso Tepper		_lo=
653667713feSHasso Tepper		for _if in ${_autolist} ; do
654667713feSHasso Tepper			if [ "$_if" = "lo0" ]; then
655667713feSHasso Tepper				_lo="lo0"
656667713feSHasso Tepper			else
657667713feSHasso Tepper				_tmplist="${_tmplist} ${_if}"
658667713feSHasso Tepper			fi
659667713feSHasso Tepper		done
660667713feSHasso Tepper		_tmplist="${_lo} ${_tmplist}"
6619c600e7dSMatthew Dillon		;;
6629c600e7dSMatthew Dillon	*)
663acba6ed7SSimon Schubert		_tmplist="${network_interfaces} ${cloned_interfaces}"
6649c600e7dSMatthew Dillon		;;
6659c600e7dSMatthew Dillon	esac
6669c600e7dSMatthew Dillon
6679c600e7dSMatthew Dillon	echo $_tmplist
6689c600e7dSMatthew Dillon}
6699c600e7dSMatthew Dillon
6709c600e7dSMatthew Dillonhexdigit()
6719c600e7dSMatthew Dillon{
6729c600e7dSMatthew Dillon	if [ $1 -lt 10 ]; then
6739c600e7dSMatthew Dillon		echo $1
6749c600e7dSMatthew Dillon	else
6759c600e7dSMatthew Dillon		case $1 in
6769c600e7dSMatthew Dillon		10)	echo a ;;
6779c600e7dSMatthew Dillon		11)	echo b ;;
6789c600e7dSMatthew Dillon		12)	echo c ;;
6799c600e7dSMatthew Dillon		13)	echo d ;;
6809c600e7dSMatthew Dillon		14)	echo e ;;
6819c600e7dSMatthew Dillon		15)	echo f ;;
6829c600e7dSMatthew Dillon		esac
6839c600e7dSMatthew Dillon	fi
6849c600e7dSMatthew Dillon}
6859c600e7dSMatthew Dillon
6869c600e7dSMatthew Dillonhexprint()
6879c600e7dSMatthew Dillon{
688e4b0f1d1SAaron LI	local val str dig
6899c600e7dSMatthew Dillon	val=$1
6909c600e7dSMatthew Dillon	str=''
6919c600e7dSMatthew Dillon
6929c600e7dSMatthew Dillon	dig=`hexdigit $((${val} & 15))`
6939c600e7dSMatthew Dillon	str=${dig}${str}
6949c600e7dSMatthew Dillon	val=$((${val} >> 4))
6959c600e7dSMatthew Dillon	while [ ${val} -gt 0 ]; do
6969c600e7dSMatthew Dillon		dig=`hexdigit $((${val} & 15))`
6979c600e7dSMatthew Dillon		str=${dig}${str}
6989c600e7dSMatthew Dillon		val=$((${val} >> 4))
6999c600e7dSMatthew Dillon	done
7009c600e7dSMatthew Dillon
7019c600e7dSMatthew Dillon	echo ${str}
7029c600e7dSMatthew Dillon}
7039c600e7dSMatthew Dillon
7048716355dSDaniel Fojtis_wired_interface()
7058716355dSDaniel Fojt{
7068716355dSDaniel Fojt	local media
7078716355dSDaniel Fojt
7088716355dSDaniel Fojt	case `ifconfig $1 2>/dev/null` in
7098716355dSDaniel Fojt		*media:?Ethernet*) media=Ethernet ;;
7108716355dSDaniel Fojt	esac
7118716355dSDaniel Fojt
7128716355dSDaniel Fojt	test "$media" = "Ethernet"
7138716355dSDaniel Fojt}
7148716355dSDaniel Fojt
715e4b0f1d1SAaron LI#
716e4b0f1d1SAaron LI# IPv6-specific setup subroutines
717e4b0f1d1SAaron LI#
718e4b0f1d1SAaron LI
7199c600e7dSMatthew Dillon# Setup the interfaces for IPv6
7209c600e7dSMatthew Dillonnetwork6_interface_setup()
7219c600e7dSMatthew Dillon{
722e4b0f1d1SAaron LI	local interfaces rtsol_interfaces ipv6_ifconfig
723e4b0f1d1SAaron LI	local rtsol_available rtsol_interface
7243b4e0f2aSAaron LI	local prefix laddr hostid address
725e4b0f1d1SAaron LI	local _if j
726e4b0f1d1SAaron LI
7279c600e7dSMatthew Dillon	interfaces=$*
7289c600e7dSMatthew Dillon	rtsol_interfaces=''
7299c600e7dSMatthew Dillon	case ${ipv6_gateway_enable} in
7309c600e7dSMatthew Dillon	[Yy][Ee][Ss])
7319c600e7dSMatthew Dillon		rtsol_available=no
7329c600e7dSMatthew Dillon		;;
7339c600e7dSMatthew Dillon	*)
7349c600e7dSMatthew Dillon		rtsol_available=yes
7359c600e7dSMatthew Dillon		;;
7369c600e7dSMatthew Dillon	esac
737e4b0f1d1SAaron LI	for _if in $interfaces; do
7389c600e7dSMatthew Dillon		rtsol_interface=yes
739e4b0f1d1SAaron LI		prefix=`get_if_var $_if ipv6_prefix_IF`
7409c600e7dSMatthew Dillon		if [ -n "${prefix}" ]; then
7419c600e7dSMatthew Dillon			rtsol_available=no
7429c600e7dSMatthew Dillon			rtsol_interface=no
743e4b0f1d1SAaron LI			laddr=`network6_getladdr $_if`
7449c600e7dSMatthew Dillon			hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
7459c600e7dSMatthew Dillon			for j in ${prefix}; do
7469c600e7dSMatthew Dillon				address=$j\:${hostid}
747e4b0f1d1SAaron LI				ifconfig $_if inet6 ${address} prefixlen 64 alias
7489c600e7dSMatthew Dillon
7499c600e7dSMatthew Dillon				case ${ipv6_gateway_enable} in
7509c600e7dSMatthew Dillon				[Yy][Ee][Ss])
7519c600e7dSMatthew Dillon					# subnet-router anycast address
7529c600e7dSMatthew Dillon					# (rfc2373)
753e4b0f1d1SAaron LI					ifconfig $_if inet6 $j:: prefixlen 64 \
7549c600e7dSMatthew Dillon						alias anycast
7559c600e7dSMatthew Dillon					;;
7569c600e7dSMatthew Dillon				esac
7579c600e7dSMatthew Dillon			done
7589c600e7dSMatthew Dillon		fi
759e4b0f1d1SAaron LI		ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
760203bea78SAaron LI		ipv6_ifconfig="${ipv6_ifconfig#inet6 }"
7619c600e7dSMatthew Dillon		if [ -n "${ipv6_ifconfig}" ]; then
7629c600e7dSMatthew Dillon			rtsol_available=no
7639c600e7dSMatthew Dillon			rtsol_interface=no
764e4b0f1d1SAaron LI			ifconfig $_if inet6 ${ipv6_ifconfig} alias
7659c600e7dSMatthew Dillon		fi
7669c600e7dSMatthew Dillon
767e4b0f1d1SAaron LI		if [ "${rtsol_available}" = "yes" -a \
768e4b0f1d1SAaron LI		     "${rtsol_interface}" = "yes" ]; then
7699c600e7dSMatthew Dillon			case ${i} in
77006937ef9SSascha Wildner			lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
7719c600e7dSMatthew Dillon				;;
7729c600e7dSMatthew Dillon			*)
773e4b0f1d1SAaron LI				rtsol_interfaces="${rtsol_interfaces} ${_if}"
7749c600e7dSMatthew Dillon				;;
7759c600e7dSMatthew Dillon			esac
7769c600e7dSMatthew Dillon		else
777e4b0f1d1SAaron LI			ifconfig $_if inet6
7789c600e7dSMatthew Dillon		fi
7799c600e7dSMatthew Dillon	done
7809c600e7dSMatthew Dillon
781e4b0f1d1SAaron LI	if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
7829c600e7dSMatthew Dillon		# Act as endhost - automatically configured.
7839c600e7dSMatthew Dillon		# You can configure only single interface, as
7849c600e7dSMatthew Dillon		# specification assumes that autoconfigured host has
7859c600e7dSMatthew Dillon		# single interface only.
786b0a4258dSAaron LI		${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
7879c600e7dSMatthew Dillon		set ${rtsol_interfaces}
7889c600e7dSMatthew Dillon		ifconfig $1 up
789e4b0f1d1SAaron LI		echo "Auto configuring interface $1 ..."
7909c600e7dSMatthew Dillon		rtsol $1
7919c600e7dSMatthew Dillon	fi
7929c600e7dSMatthew Dillon
793e4b0f1d1SAaron LI	for _if in $interfaces; do
7943b4e0f2aSAaron LI		ifalias_up $_if ipv6
7959c600e7dSMatthew Dillon	done
7969c600e7dSMatthew Dillon}
7979c600e7dSMatthew Dillon
7989c600e7dSMatthew Dillon# Setup IPv6 to IPv4 mapping
7999c600e7dSMatthew Dillonnetwork6_stf_setup()
8009c600e7dSMatthew Dillon{
801e4b0f1d1SAaron LI	local stf_prefixlen stf_interface_ipv6_ifid
802e4b0f1d1SAaron LI	local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
803e4b0f1d1SAaron LI	local _if OIFS
804e4b0f1d1SAaron LI
8059c600e7dSMatthew Dillon	case ${stf_interface_ipv4addr} in
8069c600e7dSMatthew Dillon	[Nn][Oo] | '')
8079c600e7dSMatthew Dillon		;;
8089c600e7dSMatthew Dillon	*)
8099c600e7dSMatthew Dillon		# assign IPv6 addr and interface route for 6to4 interface
8109c600e7dSMatthew Dillon		stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
8119c600e7dSMatthew Dillon		OIFS="$IFS"
8129c600e7dSMatthew Dillon		IFS=".$IFS"
8139c600e7dSMatthew Dillon		set ${stf_interface_ipv4addr}
8149c600e7dSMatthew Dillon		IFS="$OIFS"
8159c600e7dSMatthew Dillon		hexfrag1=`hexprint $(($1*256 + $2))`
8169c600e7dSMatthew Dillon		hexfrag2=`hexprint $(($3*256 + $4))`
8179c600e7dSMatthew Dillon		ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
8189c600e7dSMatthew Dillon		case ${stf_interface_ipv6_ifid} in
8199c600e7dSMatthew Dillon		[Aa][Uu][Tt][Oo] | '')
820e4b0f1d1SAaron LI			for _if in ${ipv6_network_interfaces}; do
821e4b0f1d1SAaron LI				laddr=`network6_getladdr $_if`
8229c600e7dSMatthew Dillon				case ${laddr} in
8239c600e7dSMatthew Dillon				'')
8249c600e7dSMatthew Dillon					;;
8259c600e7dSMatthew Dillon				*)
8269c600e7dSMatthew Dillon					break
8279c600e7dSMatthew Dillon					;;
8289c600e7dSMatthew Dillon				esac
8299c600e7dSMatthew Dillon			done
8309c600e7dSMatthew Dillon			stf_interface_ipv6_ifid=`expr "${laddr}" : \
8319c600e7dSMatthew Dillon						      'fe80::\(.*\)%\(.*\)'`
8329c600e7dSMatthew Dillon			case ${stf_interface_ipv6_ifid} in
8339c600e7dSMatthew Dillon			'')
8349c600e7dSMatthew Dillon				stf_interface_ipv6_ifid=0:0:0:1
8359c600e7dSMatthew Dillon				;;
8369c600e7dSMatthew Dillon			esac
8379c600e7dSMatthew Dillon			;;
8389c600e7dSMatthew Dillon		esac
8399c600e7dSMatthew Dillon		ifconfig stf0 create >/dev/null 2>&1
8409c600e7dSMatthew Dillon		ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
8419c600e7dSMatthew Dillon			prefixlen ${stf_prefixlen}
8429c600e7dSMatthew Dillon		# disallow packets to malicious 6to4 prefix
8439c600e7dSMatthew Dillon		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
8449c600e7dSMatthew Dillon		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
8459c600e7dSMatthew Dillon		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
8469c600e7dSMatthew Dillon		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
8479c600e7dSMatthew Dillon		;;
8489c600e7dSMatthew Dillon	esac
8499c600e7dSMatthew Dillon}
8509c600e7dSMatthew Dillon
8519c600e7dSMatthew Dillon# Setup static routes
8529c600e7dSMatthew Dillonnetwork6_static_routes_setup()
8539c600e7dSMatthew Dillon{
854e4b0f1d1SAaron LI	local _rt
855e4b0f1d1SAaron LI
8569c600e7dSMatthew Dillon	# Set up any static routes.
8579c600e7dSMatthew Dillon	case ${ipv6_defaultrouter} in
8589c600e7dSMatthew Dillon	[Nn][Oo] | '')
8599c600e7dSMatthew Dillon		;;
8609c600e7dSMatthew Dillon	*)
8619c600e7dSMatthew Dillon		ipv6_static_routes="default ${ipv6_static_routes}"
8629c600e7dSMatthew Dillon		ipv6_route_default="default ${ipv6_defaultrouter}"
8639c600e7dSMatthew Dillon		;;
8649c600e7dSMatthew Dillon	esac
8659c600e7dSMatthew Dillon	case ${ipv6_static_routes} in
8669c600e7dSMatthew Dillon	[Nn][Oo] | '')
8679c600e7dSMatthew Dillon		;;
8689c600e7dSMatthew Dillon	*)
869e4b0f1d1SAaron LI		for _rt in ${ipv6_static_routes}; do
870e4b0f1d1SAaron LI			eval ipv6_route_args=\$ipv6_route_${_rt}
8719c600e7dSMatthew Dillon			route add -inet6 ${ipv6_route_args}
8729c600e7dSMatthew Dillon		done
8739c600e7dSMatthew Dillon		;;
8749c600e7dSMatthew Dillon	esac
8759c600e7dSMatthew Dillon}
8769c600e7dSMatthew Dillon
8779c600e7dSMatthew Dillon# Install the "default interface" to kernel, which will be used
8789c600e7dSMatthew Dillon# as the default route when there's no router.
8799c600e7dSMatthew Dillonnetwork6_default_interface_setup()
8809c600e7dSMatthew Dillon{
881e4b0f1d1SAaron LI	local _if laddr
882e4b0f1d1SAaron LI
8839c600e7dSMatthew Dillon	# Choose IPv6 default interface if it is not clearly specified.
8849c600e7dSMatthew Dillon	case ${ipv6_default_interface} in
8859c600e7dSMatthew Dillon	'')
886e4b0f1d1SAaron LI		for _if in ${ipv6_network_interfaces}; do
887e4b0f1d1SAaron LI			if [ "${_if}" = "lo0" ]; then
8889c600e7dSMatthew Dillon				continue
889e4b0f1d1SAaron LI			fi
890e4b0f1d1SAaron LI
891e4b0f1d1SAaron LI			laddr=`network6_getladdr $_if exclude_tentative`
8929c600e7dSMatthew Dillon			case ${laddr} in
8939c600e7dSMatthew Dillon			'')
8949c600e7dSMatthew Dillon				;;
8959c600e7dSMatthew Dillon			*)
896e4b0f1d1SAaron LI				ipv6_default_interface=$_if
8979c600e7dSMatthew Dillon				break
8989c600e7dSMatthew Dillon				;;
8999c600e7dSMatthew Dillon			esac
9009c600e7dSMatthew Dillon		done
9019c600e7dSMatthew Dillon		;;
9029c600e7dSMatthew Dillon	esac
9039c600e7dSMatthew Dillon
9049c600e7dSMatthew Dillon	# Disallow unicast packets without outgoing scope identifiers,
9059c600e7dSMatthew Dillon	# or route such packets to a "default" interface, if it is specified.
9069c600e7dSMatthew Dillon	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
9079c600e7dSMatthew Dillon	case ${ipv6_default_interface} in
9089c600e7dSMatthew Dillon	[Nn][Oo] | '')
9099c600e7dSMatthew Dillon		route add -inet6 ff02:: -prefixlen 16 ::1 -reject
9109c600e7dSMatthew Dillon		;;
9119c600e7dSMatthew Dillon	*)
9129c600e7dSMatthew Dillon		laddr=`network6_getladdr ${ipv6_default_interface}`
9139c600e7dSMatthew Dillon		route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
9149c600e7dSMatthew Dillon			-cloning
9159c600e7dSMatthew Dillon
9169c600e7dSMatthew Dillon		# Disable installing the default interface with the
9179c600e7dSMatthew Dillon		# case net.inet6.ip6.forwarding=0 and
9189c600e7dSMatthew Dillon		# net.inet6.ip6.accept_rtadv=0, due to avoid conflict
9199c600e7dSMatthew Dillon		# between the default router list and the manual
9209c600e7dSMatthew Dillon		# configured default route.
9219c600e7dSMatthew Dillon		case ${ipv6_gateway_enable} in
9229c600e7dSMatthew Dillon		[Yy][Ee][Ss])
9239c600e7dSMatthew Dillon			;;
9249c600e7dSMatthew Dillon		*)
925b0a4258dSAaron LI			if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
9269c600e7dSMatthew Dillon			then
9279c600e7dSMatthew Dillon				ndp -I ${ipv6_default_interface}
9289c600e7dSMatthew Dillon			fi
9299c600e7dSMatthew Dillon			;;
9309c600e7dSMatthew Dillon		esac
9319c600e7dSMatthew Dillon		;;
9329c600e7dSMatthew Dillon	esac
9339c600e7dSMatthew Dillon}
9349c600e7dSMatthew Dillon
9359c600e7dSMatthew Dillonnetwork6_getladdr()
9369c600e7dSMatthew Dillon{
937e4b0f1d1SAaron LI	local proto addr rest
938e4b0f1d1SAaron LI
9399c600e7dSMatthew Dillon	ifconfig $1 2>/dev/null | while read proto addr rest; do
9409c600e7dSMatthew Dillon		case ${proto} in
9419c600e7dSMatthew Dillon		inet6)
9429c600e7dSMatthew Dillon			case ${addr} in
9439c600e7dSMatthew Dillon			fe80::*)
9449c600e7dSMatthew Dillon				if [ -z "$2" ]; then
9459c600e7dSMatthew Dillon					echo ${addr}
9469c600e7dSMatthew Dillon					return
9479c600e7dSMatthew Dillon				fi
9489c600e7dSMatthew Dillon				case ${rest} in
9499c600e7dSMatthew Dillon				*tentative*)
9509c600e7dSMatthew Dillon					continue
9519c600e7dSMatthew Dillon					;;
9529c600e7dSMatthew Dillon				*)
9539c600e7dSMatthew Dillon					echo ${addr}
9549c600e7dSMatthew Dillon					return
9559c600e7dSMatthew Dillon				esac
9569c600e7dSMatthew Dillon			esac
9579c600e7dSMatthew Dillon		esac
9589c600e7dSMatthew Dillon	done
9599c600e7dSMatthew Dillon}
960