network.subr revision 147121
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 147121 2005-06-07 23:59:45Z brooks $
2666830Sobrien#
2725184Sjkh
28113674Smtm#
29113674Smtm# Subroutines commonly used from network startup scripts.
30113674Smtm# Requires that rc.conf be loaded first.
31113674Smtm#
3225184Sjkh
33113674Smtm# ifconfig_up if
34113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
35113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
36113674Smtm#	arguments were found and executed or 1 if the interface
37147088Sbrooks#	had no arguments.  Pseudo arguments DHCP and WPA are handled
38147088Sbrooks#	here.
39113674Smtm#
40113674Smtmifconfig_up()
41113674Smtm{
42147088Sbrooks	_cfg=1
43147088Sbrooks
44147088Sbrooks	ifconfig_args=`ifconfig_getargs $1`
45113674Smtm	if [ -n "${ifconfig_args}" ]; then
46113674Smtm		ifconfig $1 ${ifconfig_args}
47147088Sbrooks		_cfg=0
48113674Smtm	fi
49147088Sbrooks
50147088Sbrooks	if wpaif $1; then
51147088Sbrooks		#/etc/rc.d/wpa_supplicant start $1
52147088Sbrooks		_cfg=0		# XXX: not sure this should count
53147088Sbrooks	fi
54147088Sbrooks
55147088Sbrooks	if dhcpif $1; then
56147088Sbrooks		/etc/rc.d/dhclient start $1
57147088Sbrooks		_cfg=0
58147088Sbrooks	fi
59147088Sbrooks
60147121Sbrooks	return $_cfg
61113674Smtm}
6225184Sjkh
63116029Smtm# ifconfig_down if
64116029Smtm#	Remove all inet entries from the $if interface. It returns
65116029Smtm#	0 if inet entries were found and removed. It returns 1 if
66116100Smtm#	no entries were found or they could not be removed.
67116029Smtm#
68116029Smtmifconfig_down()
69116029Smtm{
70116029Smtm	[ -z "$1" ] && return 1
71116029Smtm	_ifs="^"
72147121Sbrooks	_cfg=1
73116029Smtm
74116029Smtm	inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
75116029Smtm
76116029Smtm	oldifs="$IFS"
77116029Smtm	IFS="$_ifs"
78116029Smtm	for _inet in $inetList ; do
79116029Smtm		# get rid of extraneous line
80116029Smtm		[ -z "$_inet" ] && break
81116029Smtm
82116029Smtm		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
83116029Smtm
84116032Smtm		IFS="$oldifs"
85116032Smtm		ifconfig $1 ${_inet} delete
86116032Smtm		IFS="$_ifs"
87147121Sbrooks		_cfg=0
88116029Smtm	done
89116029Smtm	IFS="$oldifs"
90116029Smtm
91147088Sbrooks	if wpaif $1; then
92147088Sbrooks		#/etc/rc.d/wpa_supplicant stop $1
93147121Sbrooks		_cfg=0
94147088Sbrooks	fi
95147088Sbrooks
96147088Sbrooks	if dhcpif $1; then
97147088Sbrooks		/etc/rc.d/dhclient stop $1
98147088Sbrooks		_cfg=0
99147088Sbrooks	fi
100147088Sbrooks
101147121Sbrooks	return $_cfg
102116029Smtm}
103116029Smtm
104147088Sbrooks# _ifconfig_getargs if
105147088Sbrooks#	Echos the arguments for the supplied interface to stdout.
106147088Sbrooks#	returns 1 if empty.  In general, ifconfig_getargs should be used
107147088Sbrooks#	outside this file.
108147088Sbrooks_ifconfig_getargs()
109147088Sbrooks{
110147088Sbrooks	_ifn=$1
111147088Sbrooks	if [ -z "$_ifn" ]; then
112147088Sbrooks		return 1
113147088Sbrooks	fi
114147088Sbrooks
115147088Sbrooks	eval _args=\$ifconfig_$1
116147088Sbrooks	if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
117147088Sbrooks		for _if in ${removable_interfaces} ; do
118147088Sbrooks			if [ "$_if" = "$_ifn" ] ; then
119147088Sbrooks				_args=${pccard_ifconfig}
120147088Sbrooks				break
121147088Sbrooks			fi
122147088Sbrooks		done
123147088Sbrooks	fi
124147088Sbrooks
125147088Sbrooks	echo $_args
126147088Sbrooks}
127147088Sbrooks
128147088Sbrooks# ifconfig_getargs if
129147088Sbrooks#	Takes the result from _ifconfig_getargs and removes pseudo
130147088Sbrooks#	args such as DHCP and WPA.
131147088Sbrooksifconfig_getargs()
132147088Sbrooks{
133147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
134147088Sbrooks	if [ $? -eq 1 ]; then
135147088Sbrooks		return 1
136147088Sbrooks	fi
137147088Sbrooks	_args=
138147088Sbrooks
139147088Sbrooks	for _arg in $_tmpargs; do
140147088Sbrooks		case $_arg in
141147088Sbrooks		[Dd][Hh][Cc][Pp])
142147088Sbrooks			;;
143147088Sbrooks		[Ww][Pp][Aa])
144147088Sbrooks			;;
145147088Sbrooks		*)
146147088Sbrooks			_args="$_args $_arg"
147147088Sbrooks			;;
148147088Sbrooks		esac
149147088Sbrooks	done
150147088Sbrooks
151147088Sbrooks	echo $_args
152147088Sbrooks}
153147088Sbrooks
154147088Sbrooks# dhcpif if
155147088Sbrooks#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
156147088Sbrooksdhcpif()
157147088Sbrooks{
158147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
159147088Sbrooks	for _arg in $_tmpargs; do
160147088Sbrooks		case $_arg in
161147088Sbrooks		[Dd][Hh][Cc][Pp])
162147088Sbrooks			return 0
163147088Sbrooks			;;
164147088Sbrooks		esac
165147088Sbrooks	done
166147088Sbrooks	return 1
167147088Sbrooks}
168147088Sbrooks
169147088Sbrooks# wpaif if
170147088Sbrooks#	Returns 0 if the interface is a WPA interface and 1 otherwise.
171147088Sbrookswpaif()
172147088Sbrooks{
173147088Sbrooks	_tmpargs=`_ifconfig_getargs $1`
174147088Sbrooks	for _arg in $_tmpargs; do
175147088Sbrooks		case $_arg in
176147088Sbrooks		[Ww][Pp][Aa])
177147088Sbrooks			return 0
178147088Sbrooks			;;
179147088Sbrooks		esac
180147088Sbrooks	done
181147088Sbrooks	return 1
182147088Sbrooks}
183147088Sbrooks
184113674Smtm# ifalias_up if
185113674Smtm#	Configure aliases for network interface $if.
186113674Smtm#	It returns 0 if at least one alias was configured or
187113674Smtm#	1 if there were none.
188113674Smtm#
189113674Smtmifalias_up()
190113674Smtm{
191113674Smtm	_ret=1
192113674Smtm	alias=0
193113674Smtm	while : ; do
194113674Smtm		eval ifconfig_args=\$ifconfig_$1_alias${alias}
195113674Smtm		if [ -n "${ifconfig_args}" ]; then
196113674Smtm			ifconfig $1 ${ifconfig_args} alias
197113674Smtm			alias=$((${alias} + 1))
198113674Smtm			_ret=0
199113674Smtm		else
200113674Smtm			break
201113674Smtm		fi
202113674Smtm	done
203113674Smtm	return $_ret
204113674Smtm}
205100280Sgordon
206116029Smtm#ifalias_down if
207116029Smtm#	Remove aliases for network interface $if.
208116029Smtm#	It returns 0 if at least one alias was removed or
209116029Smtm#	1 if there were none.
210116029Smtm#
211116029Smtmifalias_down()
212116029Smtm{
213116029Smtm	_ret=1
214116029Smtm	alias=0
215116029Smtm	while : ; do
216116029Smtm		eval ifconfig_args=\$ifconfig_$1_alias${alias}
217116029Smtm		if [ -n "${ifconfig_args}" ]; then
218116029Smtm			ifconfig $1 ${ifconfig_args} -alias
219116029Smtm			alias=$((${alias} + 1))
220116029Smtm			_ret=0
221116029Smtm		else
222116029Smtm			break
223116029Smtm		fi
224116029Smtm	done
225116029Smtm	return $_ret
226116029Smtm}
227116029Smtm
228113674Smtm# ifscript_up if
229113674Smtm#	Evaluate a startup script for the $if interface.
230113674Smtm#	It returns 0 if a script was found and processed or
231113674Smtm#	1 if no script was found.
232113674Smtm#
233113674Smtmifscript_up()
234100280Sgordon{
235113674Smtm	if [ -r /etc/start_if.$1 ]; then
236113674Smtm		. /etc/start_if.$1
237113674Smtm		return 0
238113674Smtm	fi
239113674Smtm	return 1
240100280Sgordon}
241100280Sgordon
242116029Smtm# ifscript_down if
243116029Smtm#	Evaluate a shutdown script for the $if interface.
244116029Smtm#	It returns 0 if a script was found and processed or
245116029Smtm#	1 if no script was found.
246116029Smtm#
247116029Smtmifscript_down()
248116029Smtm{
249116029Smtm	if [ -r /etc/stop_if.$1 ]; then
250116029Smtm		. /etc/stop_if.$1
251116029Smtm		return 0
252116029Smtm	fi
253116029Smtm	return 1
254116029Smtm}
255116029Smtm
256113674Smtm# Create cloneable interfaces.
257113674Smtm#
258113674Smtmclone_up()
259100280Sgordon{
260113674Smtm	_prefix=
261113674Smtm	_list=
262113674Smtm	for ifn in ${cloned_interfaces}; do
263113674Smtm		ifconfig ${ifn} create
264116774Skuriyama		if [ $? -eq 0 ]; then
265113674Smtm			_list="${_list}${_prefix}${ifn}"
266113674Smtm			[ -z "$_prefix" ] && _prefix=' '
267113674Smtm		fi
268113674Smtm	done
269113674Smtm	debug "Cloned: ${_list}"
270113674Smtm}
271100280Sgordon
272113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed
273113674Smtm# to standard output.
274113674Smtm#
275113674Smtmclone_down()
276113674Smtm{
277113674Smtm	_prefix=
278113674Smtm	_list=
279113674Smtm	for ifn in ${cloned_interfaces}; do
280113674Smtm		ifconfig ${ifn} destroy
281116774Skuriyama		if [ $? -eq 0 ]; then
282113674Smtm			_list="${_list}${_prefix}${ifn}"
283113674Smtm			[ -z "$_prefix" ] && _prefix=' '
284113674Smtm		fi
285113674Smtm	done
286113674Smtm	debug "Destroyed clones: ${_list}"
287100280Sgordon}
288100280Sgordon
289113674Smtmgif_up() {
290100282Sdougb	case ${gif_interfaces} in
291100282Sdougb	[Nn][Oo] | '')
292100282Sdougb		;;
293100282Sdougb	*)
294100282Sdougb		for i in ${gif_interfaces}; do
295100282Sdougb			eval peers=\$gifconfig_$i
296100282Sdougb			case ${peers} in
297100282Sdougb			'')
298100282Sdougb				continue
299100282Sdougb				;;
300100282Sdougb			*)
301100282Sdougb				ifconfig $i create >/dev/null 2>&1
302100282Sdougb				ifconfig $i tunnel ${peers}
303103710Sume				ifconfig $i up
304100282Sdougb				;;
305100282Sdougb			esac
306100282Sdougb		done
307100282Sdougb		;;
308100282Sdougb	esac
309100282Sdougb}
310100282Sdougb
311113674Smtm#
312113674Smtm# ipx_up ifn
313113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
314113674Smtm# arguments were found and configured; returns 1 otherwise.
315113674Smtm#
316113674Smtmipx_up()
317100280Sgordon{
318113674Smtm	ifn="$1"
319113674Smtm	eval ifconfig_args=\$ifconfig_${ifn}_ipx
320113674Smtm	if [ -n "${ifconfig_args}" ]; then
321113674Smtm		ifconfig ${ifn} ${ifconfig_args}
322113674Smtm		return 0
32385831Sdes	fi
324113674Smtm	return 1
325113674Smtm}
32685831Sdes
327116029Smtm# ipx_down ifn
328116029Smtm#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
329116029Smtm#	addresses were found and unconfigured. It returns 1, otherwise.
330113674Smtm#
331116029Smtmipx_down()
332116029Smtm{
333116100Smtm	[ -z "$1" ] && return 1
334116100Smtm	_ifs="^"
335116100Smtm	_ret=1
336116100Smtm
337116100Smtm	ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
338116100Smtm
339116100Smtm	oldifs="$IFS"
340116100Smtm	IFS="$_ifs"
341116100Smtm	for _ipx in $ipxList ; do
342116100Smtm		# get rid of extraneous line
343116100Smtm		[ -z "$_ipx" ] && break
344116100Smtm
345116100Smtm		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
346116100Smtm
347116100Smtm		IFS="$oldifs"
348116100Smtm		ifconfig $1 ${_ipx} delete
349116100Smtm		IFS="$_ifs"
350116100Smtm		_ret=0
351116100Smtm	done
352116100Smtm	IFS="$oldifs"
353116100Smtm
354116100Smtm	return $_ret
355116029Smtm}
356116029Smtm
357137070Spjd# ifnet_rename
358137070Spjd#	Rename all requested interfaces.
359116029Smtm#
360137070Spjdifnet_rename()
361137070Spjd{
362137070Spjd
363138386Srse	_ifn_list="`ifconfig -l`"
364137070Spjd	[ -z "$_ifn_list" ] && return 0
365137070Spjd	for _if in ${_ifn_list} ; do
366137070Spjd		eval _ifname=\$ifconfig_${_if}_name
367137070Spjd		if [ ! -z "$_ifname" ]; then
368137070Spjd			ifconfig $_if name $_ifname
369137070Spjd		fi
370137070Spjd	done
371137070Spjd	return 0
372137070Spjd}
373137070Spjd
374137070Spjd#
375113674Smtm# list_net_interfaces type
376113674Smtm#	List all network interfaces. The type of interface returned
377113674Smtm#	can be controlled by the type argument. The type
378113674Smtm#	argument can be any of the following:
379113674Smtm#		nodhcp - all interfaces, excluding DHCP configured interfaces
380113674Smtm#		dhcp   - list only DHCP configured interfaces
381113674Smtm#	If no argument is specified all network interfaces are output.
382134429Syar#	Note that the list will include cloned interfaces if applicable.
383134429Syar#	Cloned interfaces must already exist to have a chance to appear
384134429Syar#	in the list if ${network_interfaces} is set to `auto'.
385113674Smtm#
386113674Smtmlist_net_interfaces()
387113674Smtm{
388113674Smtm	type=$1
38965532Snectar
390113674Smtm	# Get a list of ALL the interfaces
39151231Ssheldonh	#
39251231Ssheldonh	case ${network_interfaces} in
39351231Ssheldonh	[Aa][Uu][Tt][Oo])
394113674Smtm		_tmplist="`ifconfig -l`"
39551231Ssheldonh		;;
39683677Sbrooks	*)
397134429Syar		_tmplist="${network_interfaces} ${cloned_interfaces}"
39883677Sbrooks		;;
39951231Ssheldonh	esac
40049122Sbrian
401113674Smtm	if [ -z "$type" ]; then
402113674Smtm		echo $_tmplist
403113674Smtm		return 0
404113674Smtm	fi
40549122Sbrian
406138385Srse	# Separate out dhcp and non-dhcp interfaces
407113674Smtm	#
408113674Smtm	_aprefix=
409134376Syar	_bprefix=
410113674Smtm	for _if in ${_tmplist} ; do
411113674Smtm		eval _ifarg="\$ifconfig_${_if}"
412113674Smtm		case "$_ifarg" in
41351231Ssheldonh		[Dd][Hh][Cc][Pp])
414113674Smtm			_dhcplist="${_dhcplist}${_aprefix}${_if}"
415113674Smtm			[ -z "$_aprefix" ] && _aprefix=' '
41651231Ssheldonh			;;
417113674Smtm		''|*)
418113674Smtm			_nodhcplist="${_nodhcplist}${_bprefix}${_if}"
419113674Smtm			[ -z "$_bprefix" ] && _bprefix=' '
42051231Ssheldonh			;;
42151231Ssheldonh		esac
42254458Sobrien	done
42351231Ssheldonh
424118797Smbr	case ${pccard_ifconfig} in
425118797Smbr	[Dd][Hh][Cc][Pp])
426118797Smbr		for _if in ${removable_interfaces} ; do
427118797Smbr			_test_if=`ifconfig ${_if} 2>&1`
428118797Smbr			case "$_test_if" in
429118797Smbr			"ifconfig: interface $_if does not exist")
430118797Smbr				;;
431118797Smbr			*)
432118797Smbr				_dhcplist="${_dhcplist}${_aprefix}${_if}"
433118797Smbr				[ -z "$_aprefix" ] && _aprefix=' '
434118797Smbr				;;
435118797Smbr			esac
436118797Smbr		done
437118797Smbr		;;
438118797Smbr	*)
439118797Smbr		;;
440118797Smbr	esac
441118797Smbr
442113674Smtm	case "$type" in
443113674Smtm	nodhcp)
444113674Smtm		echo $_nodhcplist
445113674Smtm		;;
446113674Smtm	dhcp)
447113674Smtm		echo $_dhcplist
448113674Smtm		;;
449113674Smtm	esac
450130151Sschweikh	return 0
45125184Sjkh}
452114942Sume
453114942Sumehexdigit()
454114942Sume{
455114942Sume	if [ $1 -lt 10 ]; then
456114942Sume		echo $1
457114942Sume	else
458114942Sume		case $1 in
459114942Sume		10)	echo a ;;
460114942Sume		11)	echo b ;;
461114942Sume		12)	echo c ;;
462114942Sume		13)	echo d ;;
463114942Sume		14)	echo e ;;
464114942Sume		15)	echo f ;;
465114942Sume		esac
466114942Sume	fi
467114942Sume}
468114942Sume
469114942Sumehexprint()
470114942Sume{
471114942Sume	val=$1
472114942Sume	str=''
473114942Sume
474114942Sume	dig=`hexdigit $((${val} & 15))`
475114942Sume	str=${dig}${str}
476114942Sume	val=$((${val} >> 4))
477114942Sume	while [ ${val} -gt 0 ]; do
478114942Sume		dig=`hexdigit $((${val} & 15))`
479114942Sume		str=${dig}${str}
480114942Sume		val=$((${val} >> 4))
481114942Sume	done
482114942Sume
483114942Sume	echo ${str}
484114942Sume}
485114942Sume
486114942Sume# Setup the interfaces for IPv6
487114942Sumenetwork6_interface_setup()
488114942Sume{
489114942Sume	interfaces=$*
490114942Sume	rtsol_interfaces=''
491114942Sume	case ${ipv6_gateway_enable} in
492114942Sume	[Yy][Ee][Ss])
493114942Sume		rtsol_available=no
494114942Sume		;;
495114942Sume	*)
496114942Sume		rtsol_available=yes
497114942Sume		;;
498114942Sume	esac
499114942Sume	for i in $interfaces; do
500114942Sume		rtsol_interface=yes
501114942Sume		eval prefix=\$ipv6_prefix_$i
502114942Sume		if [ -n "${prefix}" ]; then
503114942Sume			rtsol_available=no
504114942Sume			rtsol_interface=no
505114942Sume			laddr=`network6_getladdr $i`
506114942Sume			hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
507114942Sume			for j in ${prefix}; do
508114942Sume				address=$j\:${hostid}
509114942Sume				ifconfig $i inet6 ${address} prefixlen 64 alias
510114942Sume
511114942Sume				case ${ipv6_gateway_enable} in
512114942Sume				[Yy][Ee][Ss])
513114942Sume					# subnet-router anycast address
514114942Sume					# (rfc2373)
515114942Sume					ifconfig $i inet6 $j:: prefixlen 64 \
516114942Sume						alias anycast
517114942Sume					;;
518114942Sume				esac
519114942Sume			done
520114942Sume		fi
521114942Sume		eval ipv6_ifconfig=\$ipv6_ifconfig_$i
522114942Sume		if [ -n "${ipv6_ifconfig}" ]; then
523114942Sume			rtsol_available=no
524114942Sume			rtsol_interface=no
525114942Sume			ifconfig $i inet6 ${ipv6_ifconfig} alias
526114942Sume		fi
527114942Sume
528114942Sume		if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
529114942Sume		then
530114942Sume			case ${i} in
531114942Sume			lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
532114942Sume				;;
533114942Sume			*)
534114942Sume				rtsol_interfaces="${rtsol_interfaces} ${i}"
535114942Sume				;;
536114942Sume			esac
537114942Sume		else
538114942Sume			ifconfig $i inet6
539114942Sume		fi
540114942Sume	done
541114942Sume
542114942Sume	if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
543114942Sume		# Act as endhost - automatically configured.
544114942Sume		# You can configure only single interface, as
545114942Sume		# specification assumes that autoconfigured host has
546114942Sume		# single interface only.
547114942Sume		sysctl net.inet6.ip6.accept_rtadv=1
548114942Sume		set ${rtsol_interfaces}
549114942Sume		ifconfig $1 up
550118666Sume		rtsol ${rtsol_flags} $1
551114942Sume	fi
552114942Sume
553114942Sume	for i in $interfaces; do
554114942Sume		alias=0
555114942Sume		while : ; do
556114942Sume			eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
557114942Sume			if [ -z "${ipv6_ifconfig}" ]; then
558114942Sume				break;
559114942Sume			fi
560114942Sume			ifconfig $i inet6 ${ipv6_ifconfig} alias
561114942Sume			alias=$((${alias} + 1))
562114942Sume		done
563114942Sume	done
564114942Sume}
565114942Sume
566114942Sume# Setup IPv6 to IPv4 mapping
567114942Sumenetwork6_stf_setup()
568114942Sume{
569114942Sume	case ${stf_interface_ipv4addr} in
570114942Sume	[Nn][Oo] | '')
571114942Sume		;;
572114942Sume	*)
573114942Sume		# assign IPv6 addr and interface route for 6to4 interface
574114942Sume		stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
575114942Sume		OIFS="$IFS"
576114942Sume		IFS=".$IFS"
577114942Sume		set ${stf_interface_ipv4addr}
578114942Sume		IFS="$OIFS"
579114942Sume		hexfrag1=`hexprint $(($1*256 + $2))`
580114942Sume		hexfrag2=`hexprint $(($3*256 + $4))`
581114942Sume		ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
582114942Sume		case ${stf_interface_ipv6_ifid} in
583114942Sume		[Aa][Uu][Tt][Oo] | '')
584114942Sume			for i in ${ipv6_network_interfaces}; do
585114942Sume				laddr=`network6_getladdr ${i}`
586114942Sume				case ${laddr} in
587114942Sume				'')
588114942Sume					;;
589114942Sume				*)
590114942Sume					break
591114942Sume					;;
592114942Sume				esac
593114942Sume			done
594114942Sume			stf_interface_ipv6_ifid=`expr "${laddr}" : \
595114942Sume						      'fe80::\(.*\)%\(.*\)'`
596114942Sume			case ${stf_interface_ipv6_ifid} in
597114942Sume			'')
598114942Sume				stf_interface_ipv6_ifid=0:0:0:1
599114942Sume				;;
600114942Sume			esac
601114942Sume			;;
602114942Sume		esac
603114942Sume		ifconfig stf0 create >/dev/null 2>&1
604114942Sume		ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
605114942Sume			prefixlen ${stf_prefixlen}
606114942Sume		# disallow packets to malicious 6to4 prefix
607114942Sume		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
608114942Sume		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
609114942Sume		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
610114942Sume		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
611114942Sume		;;
612114942Sume	esac
613114942Sume}
614114942Sume
615114942Sume# Setup static routes
616114942Sumenetwork6_static_routes_setup()
617114942Sume{
618114942Sume	# Set up any static routes.
619114942Sume	case ${ipv6_defaultrouter} in
620114942Sume	[Nn][Oo] | '')
621114942Sume		;;
622114942Sume	*)
623114942Sume		ipv6_static_routes="default ${ipv6_static_routes}"
624114942Sume		ipv6_route_default="default ${ipv6_defaultrouter}"
625114942Sume		;;
626114942Sume	esac
627114942Sume	case ${ipv6_static_routes} in
628114942Sume	[Nn][Oo] | '')
629114942Sume		;;
630114942Sume	*)
631114942Sume		for i in ${ipv6_static_routes}; do
632114942Sume			eval ipv6_route_args=\$ipv6_route_${i}
633114942Sume			route add -inet6 ${ipv6_route_args}
634114942Sume		done
635114942Sume		;;
636114942Sume	esac
637114942Sume}
638114942Sume
639114942Sume# Setup faith
640114942Sumenetwork6_faith_setup()
641114942Sume{
642114942Sume	case ${ipv6_faith_prefix} in
643114942Sume	[Nn][Oo] | '')
644114942Sume		;;
645114942Sume	*)
646114942Sume		sysctl net.inet6.ip6.keepfaith=1
647114942Sume		ifconfig faith0 create >/dev/null 2>&1
648114942Sume		ifconfig faith0 up
649114942Sume		for prefix in ${ipv6_faith_prefix}; do
650114942Sume			prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
651114942Sume			case ${prefixlen} in
652114942Sume			'')
653114942Sume				prefixlen=96
654114942Sume				;;
655114942Sume			*)
656114942Sume				prefix=`expr "${prefix}" : \
657114942Sume					     "\(.*\)/${prefixlen}"`
658114942Sume				;;
659114942Sume			esac
660114942Sume			route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
661114942Sume			route change -inet6 ${prefix} -prefixlen ${prefixlen} \
662114942Sume				-ifp faith0
663114942Sume		done
664114942Sume		;;
665114942Sume	esac
666114942Sume}
667114942Sume
668114942Sume# Install the "default interface" to kernel, which will be used
669114942Sume# as the default route when there's no router.
670114942Sumenetwork6_default_interface_setup()
671114942Sume{
672114942Sume	# Choose IPv6 default interface if it is not clearly specified.
673114942Sume	case ${ipv6_default_interface} in
674114942Sume	'')
675114942Sume		for i in ${ipv6_network_interfaces}; do
676114942Sume			case $i in
677114942Sume			lo0|faith[0-9]*)
678114942Sume				continue
679114942Sume				;;
680114942Sume			esac
681114942Sume			laddr=`network6_getladdr $i exclude_tentative`
682114942Sume			case ${laddr} in
683114942Sume			'')
684114942Sume				;;
685114942Sume			*)
686114942Sume				ipv6_default_interface=$i
687114942Sume				break
688114942Sume				;;
689114942Sume			esac
690114942Sume		done
691114942Sume		;;
692114942Sume	esac
693114942Sume
694114942Sume	# Disallow unicast packets without outgoing scope identifiers,
695114942Sume	# or route such packets to a "default" interface, if it is specified.
696114942Sume	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
697114942Sume	case ${ipv6_default_interface} in
698114942Sume	[Nn][Oo] | '')
699114942Sume		route add -inet6 ff02:: -prefixlen 16 ::1 -reject
700114942Sume		;;
701114942Sume	*)
702114942Sume		laddr=`network6_getladdr ${ipv6_default_interface}`
703114942Sume		route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
704114942Sume			-cloning
705114942Sume
706114942Sume		# Disable installing the default interface with the
707114942Sume		# case net.inet6.ip6.forwarding=0 and
708114942Sume		# net.inet6.ip6.accept_rtadv=0, due to avoid conflict
709114942Sume		# between the default router list and the manual
710114942Sume		# configured default route.
711114942Sume		case ${ipv6_gateway_enable} in
712114942Sume		[Yy][Ee][Ss])
713114942Sume			;;
714114942Sume		*)
715114942Sume			if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
716114942Sume			then
717114942Sume				ndp -I ${ipv6_default_interface}
718114942Sume			fi
719114942Sume			;;
720114942Sume		esac
721114942Sume		;;
722114942Sume	esac
723114942Sume}
724114942Sume
725114942Sumenetwork6_getladdr()
726114942Sume{
727114942Sume	ifconfig $1 2>/dev/null | while read proto addr rest; do
728114942Sume		case ${proto} in
729114942Sume		inet6)
730114942Sume			case ${addr} in
731114942Sume			fe80::*)
732114942Sume				if [ -z "$2" ]; then
733114942Sume					echo ${addr}
734114942Sume					return
735114942Sume				fi
736114942Sume				case ${rest} in
737114942Sume				*tentative*)
738114942Sume					continue
739114942Sume					;;
740114942Sume				*)
741114942Sume					echo ${addr}
742114942Sume					return
743114942Sume				esac
744114942Sume			esac
745114942Sume		esac
746114942Sume	done
747114942Sume}
748