network.subr revision 152441
121526Sdavidn#
225901Sgpalmer# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
321526Sdavidn#
421526Sdavidn# Redistribution and use in source and binary forms, with or without
521526Sdavidn# modification, are permitted provided that the following conditions
621526Sdavidn# are met:
721526Sdavidn# 1. Redistributions of source code must retain the above copyright
821526Sdavidn#    notice, this list of conditions and the following disclaimer.
921526Sdavidn# 2. Redistributions in binary form must reproduce the above copyright
1021526Sdavidn#    notice, this list of conditions and the following disclaimer in the
1121526Sdavidn#    documentation and/or other materials provided with the distribution.
1242587Sasami#
1321526Sdavidn# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1421526Sdavidn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1539375Smsmith# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1639375Smsmith# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
1721526Sdavidn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1842149Shoek# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1921526Sdavidn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2021526Sdavidn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2121526Sdavidn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2221526Sdavidn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2321526Sdavidn# SUCH DAMAGE.
2421526Sdavidn#
2521943Sdavidn# $FreeBSD: head/etc/network.subr 152441 2005-11-14 23:34:50Z brooks $
2621526Sdavidn#
2742515Sasami
2839375Smsmith#
2939375Smsmith# Subroutines commonly used from network startup scripts.
3039375Smsmith# Requires that rc.conf be loaded first.
3139375Smsmith#
3239375Smsmith
3339375Smsmith# ifconfig_up if
3439375Smsmith#	Evaluate ifconfig(8) arguments for interface $if and
3539375Smsmith#	run ifconfig(8) with those arguments. It returns 0 if
3639375Smsmith#	arguments were found and executed or 1 if the interface
3721538Sdavidn#	had no arguments.  Pseudo arguments DHCP and WPA are handled
3821526Sdavidn#	here.
3939375Smsmith#
4021526Sdavidnifconfig_up()
4121943Sdavidn{
4221538Sdavidn	_cfg=1
4339375Smsmith
4439375Smsmith	ifconfig_args=`ifconfig_getargs $1`
4539375Smsmith	if [ -n "${ifconfig_args}" ]; then
4621538Sdavidn		ifconfig $1 up
4739375Smsmith		eval "ifconfig $1 ${ifconfig_args}"
4839375Smsmith		_cfg=0
4921538Sdavidn	fi
5039375Smsmith
5121526Sdavidn	if wpaif $1; then
5239375Smsmith		if [ $_cfg -ne 0 ] ; then
5339375Smsmith			ifconfig $1 up
5439424Sdt		fi
5539375Smsmith		/etc/rc.d/wpa_supplicant start $1
5639375Smsmith		_cfg=0		# XXX: not sure this should count
5739375Smsmith	fi
5839375Smsmith
5921526Sdavidn	if dhcpif $1; then
6021526Sdavidn		if [ $_cfg -ne 0 ] ; then
6139375Smsmith			ifconfig $1 up
6221526Sdavidn		fi
6321526Sdavidn		/etc/rc.d/dhclient start $1
6439375Smsmith		_cfg=0
6539375Smsmith	fi
6621526Sdavidn
6721526Sdavidn	return $_cfg
6839375Smsmith}
6921526Sdavidn
7039375Smsmith# ifconfig_down if
7139375Smsmith#	Remove all inet entries from the $if interface. It returns
7239375Smsmith#	0 if inet entries were found and removed. It returns 1 if
7321526Sdavidn#	no entries were found or they could not be removed.
7421526Sdavidn#
7521526Sdavidnifconfig_down()
7639375Smsmith{
7739375Smsmith	[ -z "$1" ] && return 1
7839375Smsmith	_ifs="^"
7939375Smsmith	_cfg=1
8039375Smsmith
8139375Smsmith	inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
8239375Smsmith
8339375Smsmith	oldifs="$IFS"
8439375Smsmith	IFS="$_ifs"
8539375Smsmith	for _inet in $inetList ; do
8639375Smsmith		# get rid of extraneous line
8721526Sdavidn		[ -z "$_inet" ] && break
8839375Smsmith
8939375Smsmith		_inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
9021526Sdavidn
9139375Smsmith		IFS="$oldifs"
9239375Smsmith		ifconfig $1 ${_inet} delete
9339375Smsmith		IFS="$_ifs"
9421526Sdavidn		_cfg=0
9539375Smsmith	done
9639375Smsmith	IFS="$oldifs"
9721526Sdavidn
9821526Sdavidn	if wpaif $1; then
9939375Smsmith		/etc/rc.d/wpa_supplicant stop $1
10039375Smsmith		_cfg=0
10139375Smsmith	fi
10221526Sdavidn
10339375Smsmith	if dhcpif $1; then
10439375Smsmith		/etc/rc.d/dhclient stop $1
10539375Smsmith		_cfg=0
10639375Smsmith	fi
10739375Smsmith
10839375Smsmith	return $_cfg
10939375Smsmith}
11039375Smsmith
11139375Smsmith# _ifconfig_getargs if
11239375Smsmith#	Echos the arguments for the supplied interface to stdout.
11339375Smsmith#	returns 1 if empty.  In general, ifconfig_getargs should be used
11439375Smsmith#	outside this file.
11539375Smsmith_ifconfig_getargs()
11639375Smsmith{
11721526Sdavidn	_ifn=$1
11821526Sdavidn	if [ -z "$_ifn" ]; then
11939375Smsmith		return 1
12039375Smsmith	fi
12139375Smsmith
12239375Smsmith	eval _args=\${ifconfig_$1-$ifconfig_DEFAULT}
12339375Smsmith
12439375Smsmith	echo "$_args"
12539375Smsmith}
12639375Smsmith
12739375Smsmith# ifconfig_getargs if
12842587Sasami#	Takes the result from _ifconfig_getargs and removes pseudo
12939375Smsmith#	args such as DHCP and WPA.
13039375Smsmithifconfig_getargs()
13139375Smsmith{
13239375Smsmith	_tmpargs=`_ifconfig_getargs $1`
13339375Smsmith	if [ $? -eq 1 ]; then
13439375Smsmith		return 1
13539375Smsmith	fi
13639375Smsmith	_args=
13739375Smsmith
13839375Smsmith	for _arg in $_tmpargs; do
13939375Smsmith		case $_arg in
14039375Smsmith		[Dd][Hh][Cc][Pp])
14139375Smsmith			;;
14239375Smsmith		[Nn][Oo][Aa][Uu][Tt][Oo])
14339375Smsmith			;;
14421526Sdavidn		[Ww][Pp][Aa])
14521526Sdavidn			;;
14639375Smsmith		*)
14739375Smsmith			_args="$_args $_arg"
14839375Smsmith			;;
14939375Smsmith		esac
15039375Smsmith	done
15139375Smsmith
15239375Smsmith	echo $_args
15339375Smsmith}
15439375Smsmith
15539375Smsmith# autoif
15639375Smsmith#	Returns 0 if the interface should be automaticly configured at
15739375Smsmith#	boot time and 1 otherwise.
15839375Smsmithautoif()
15925369Sache{
16025369Sache	_tmpargs=`_ifconfig_getargs $1`
16139375Smsmith	for _arg in $_tmpargs; do
16239375Smsmith		case $_arg in
16339375Smsmith		[Nn][Oo][Aa][Uu][Tt][Oo])
16439375Smsmith			return 1
16539375Smsmith			;;
16639375Smsmith		esac
16739375Smsmith	done
16839375Smsmith	return 0
16939375Smsmith}
17039375Smsmith
17139375Smsmith# dhcpif if
17239375Smsmith#	Returns 0 if the interface is a DHCP interface and 1 otherwise.
17339375Smsmithdhcpif()
17439375Smsmith{
17539375Smsmith	_tmpargs=`_ifconfig_getargs $1`
17639375Smsmith	for _arg in $_tmpargs; do
17739375Smsmith		case $_arg in
17839375Smsmith		[Dd][Hh][Cc][Pp])
17939375Smsmith			return 0
18039375Smsmith			;;
18139375Smsmith		esac
18239375Smsmith	done
18339375Smsmith	return 1
18439375Smsmith}
18539375Smsmith
18639375Smsmith# wpaif if
18739375Smsmith#	Returns 0 if the interface is a WPA interface and 1 otherwise.
18839375Smsmithwpaif()
18939375Smsmith{
19039375Smsmith	_tmpargs=`_ifconfig_getargs $1`
19139375Smsmith	for _arg in $_tmpargs; do
19239375Smsmith		case $_arg in
19339375Smsmith		[Ww][Pp][Aa])
19439375Smsmith			return 0
19539375Smsmith			;;
19639375Smsmith		esac
19739375Smsmith	done
19839375Smsmith	return 1
19939375Smsmith}
20039375Smsmith
20139375Smsmith# ipv4_up if
20239375Smsmith#  add IPv4 addresses to the interface $if 
20339375Smsmithipv4_up()
20439375Smsmith{
20539375Smsmith	_if=$1
20639375Smsmith	ifalias_up ${_if}
20739375Smsmith	ipv4_addrs_common ${_if} alias
20839375Smsmith}
20939375Smsmith
21039375Smsmith# ipv4_down if
21139375Smsmith#  remove IPv4 addresses from the interface $if
21239375Smsmithipv4_down()
21339375Smsmith{
21439375Smsmith	_if=$1
21539375Smsmith	ifalias_down ${_if}
21639375Smsmith	ipv4_addrs_common ${_if} -alias
21739375Smsmith}
21839375Smsmith
21939375Smsmith# ipv4_addrs_common if action
22039375Smsmith#   Evaluate the ifconfig_if_ipv4 arguments for interface $if
22139375Smsmith#   and use $action to add or remove IPv4 addresses from $if.
22239375Smsmithipv4_addrs_common()
22339375Smsmith{  
22439375Smsmith	_ret=1
22539375Smsmith	_if=$1
22639375Smsmith	_action=$2
22739375Smsmith    
22839375Smsmith	# get ipv4-addresses
22939375Smsmith	eval cidr_addr=\${ipv4_addrs_${_if}}
23039375Smsmith    
23139375Smsmith	for _cidr in ${cidr_addr}; do
23239375Smsmith		_ipaddr=${_cidr%%/*}
23339375Smsmith		_netmask="/"${_cidr##*/}
23439375Smsmith		_range=${_ipaddr##*.}
23539375Smsmith		_ipnet=${_ipaddr%.*}
23639375Smsmith		_iplow=${_range%-*}
23739375Smsmith		_iphigh=${_range#*-}
23839375Smsmith
23939375Smsmith		# clear netmask when removing aliases
24039375Smsmith		if [ "${_action}" = "-alias" ]; then
24139375Smsmith			_netmask=""
24239375Smsmith		fi
24339375Smsmith        
24439375Smsmith		_ipcount=${_iplow}
24539375Smsmith		while [ "${_ipcount}" -le "${_iphigh}" ]; do
24639375Smsmith			eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
24739375Smsmith			_ipcount=$((${_ipcount}+1))
24839375Smsmith			_ret=0
24939375Smsmith
25039375Smsmith			# only the first ipaddr in a subnet need the real netmask
25139375Smsmith			if [ "${_action}" != "-alias" ]; then
25239375Smsmith				_netmask="/32"
25339375Smsmith			fi
25439375Smsmith		done
25539375Smsmith	done
25639375Smsmith	return $_ret
25739375Smsmith}
25839375Smsmith
25939375Smsmith# ifalias_up if
26039375Smsmith#	Configure aliases for network interface $if.
26139375Smsmith#	It returns 0 if at least one alias was configured or
26239375Smsmith#	1 if there were none.
26339375Smsmith#
26439375Smsmithifalias_up()
26539375Smsmith{
26639375Smsmith	_ret=1
26739375Smsmith	alias=0
26839375Smsmith	while : ; do
26939375Smsmith		eval ifconfig_args=\$ifconfig_$1_alias${alias}
27039375Smsmith		if [ -n "${ifconfig_args}" ]; then
27139375Smsmith			ifconfig $1 ${ifconfig_args} alias
27239375Smsmith			alias=$((${alias} + 1))
27339375Smsmith			_ret=0
27439375Smsmith		else
27539375Smsmith			break
27639375Smsmith		fi
27739375Smsmith	done
27839375Smsmith	return $_ret
27939375Smsmith}
28039375Smsmith
28139375Smsmith#ifalias_down if
28239375Smsmith#	Remove aliases for network interface $if.
28339375Smsmith#	It returns 0 if at least one alias was removed or
28439375Smsmith#	1 if there were none.
28539375Smsmith#
28639375Smsmithifalias_down()
28739375Smsmith{
28839375Smsmith	_ret=1
28939375Smsmith	alias=0
29039375Smsmith	while : ; do
29139375Smsmith		eval ifconfig_args=\$ifconfig_$1_alias${alias}
29239375Smsmith		if [ -n "${ifconfig_args}" ]; then
29339375Smsmith			ifconfig $1 ${ifconfig_args} -alias
29439375Smsmith			alias=$((${alias} + 1))
29539375Smsmith			_ret=0
29639375Smsmith		else
29739375Smsmith			break
29839375Smsmith		fi
29939375Smsmith	done
30039375Smsmith	return $_ret
30139375Smsmith}
30239375Smsmith
30339375Smsmith# ifscript_up if
30439375Smsmith#	Evaluate a startup script for the $if interface.
30539375Smsmith#	It returns 0 if a script was found and processed or
30639375Smsmith#	1 if no script was found.
30739375Smsmith#
30839375Smsmithifscript_up()
30939375Smsmith{
31039375Smsmith	if [ -r /etc/start_if.$1 ]; then
31139375Smsmith		. /etc/start_if.$1
31239375Smsmith		return 0
31339375Smsmith	fi
31439375Smsmith	return 1
31539375Smsmith}
31639375Smsmith
31739375Smsmith# ifscript_down if
31839375Smsmith#	Evaluate a shutdown script for the $if interface.
31939375Smsmith#	It returns 0 if a script was found and processed or
32039375Smsmith#	1 if no script was found.
32139375Smsmith#
32239375Smsmithifscript_down()
32339375Smsmith{
32439375Smsmith	if [ -r /etc/stop_if.$1 ]; then
32539375Smsmith		. /etc/stop_if.$1
32639375Smsmith		return 0
32742113Scwt	fi
328	return 1
329}
330
331# Create cloneable interfaces.
332#
333clone_up()
334{
335	_prefix=
336	_list=
337	for ifn in ${cloned_interfaces}; do
338		ifconfig ${ifn} create
339		if [ $? -eq 0 ]; then
340			_list="${_list}${_prefix}${ifn}"
341			[ -z "$_prefix" ] && _prefix=' '
342		fi
343	done
344	debug "Cloned: ${_list}"
345}
346
347# Destroy cloned interfaces. Destroyed interfaces are echoed
348# to standard output.
349#
350clone_down()
351{
352	_prefix=
353	_list=
354	for ifn in ${cloned_interfaces}; do
355		ifconfig ${ifn} destroy
356		if [ $? -eq 0 ]; then
357			_list="${_list}${_prefix}${ifn}"
358			[ -z "$_prefix" ] && _prefix=' '
359		fi
360	done
361	debug "Destroyed clones: ${_list}"
362}
363
364gif_up() {
365	case ${gif_interfaces} in
366	[Nn][Oo] | '')
367		;;
368	*)
369		for i in ${gif_interfaces}; do
370			eval peers=\$gifconfig_$i
371			case ${peers} in
372			'')
373				continue
374				;;
375			*)
376				ifconfig $i create >/dev/null 2>&1
377				ifconfig $i tunnel ${peers}
378				ifconfig $i up
379				;;
380			esac
381		done
382		;;
383	esac
384}
385
386#
387# ipx_up ifn
388# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
389# arguments were found and configured; returns 1 otherwise.
390#
391ipx_up()
392{
393	ifn="$1"
394	eval ifconfig_args=\$ifconfig_${ifn}_ipx
395	if [ -n "${ifconfig_args}" ]; then
396		ifconfig ${ifn} ${ifconfig_args}
397		return 0
398	fi
399	return 1
400}
401
402# ipx_down ifn
403#	Remove IPX addresses for interface $ifn. Returns 0 if IPX
404#	addresses were found and unconfigured. It returns 1, otherwise.
405#
406ipx_down()
407{
408	[ -z "$1" ] && return 1
409	_ifs="^"
410	_ret=1
411
412	ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
413
414	oldifs="$IFS"
415	IFS="$_ifs"
416	for _ipx in $ipxList ; do
417		# get rid of extraneous line
418		[ -z "$_ipx" ] && break
419
420		_ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
421
422		IFS="$oldifs"
423		ifconfig $1 ${_ipx} delete
424		IFS="$_ifs"
425		_ret=0
426	done
427	IFS="$oldifs"
428
429	return $_ret
430}
431
432# ifnet_rename
433#	Rename all requested interfaces.
434#
435ifnet_rename()
436{
437
438	_ifn_list="`ifconfig -l`"
439	[ -z "$_ifn_list" ] && return 0
440	for _if in ${_ifn_list} ; do
441		eval _ifname=\$ifconfig_${_if}_name
442		if [ ! -z "$_ifname" ]; then
443			ifconfig $_if name $_ifname
444		fi
445	done
446	return 0
447}
448
449#
450# list_net_interfaces type
451#	List all network interfaces. The type of interface returned
452#	can be controlled by the type argument. The type
453#	argument can be any of the following:
454#		nodhcp - all interfaces, excluding DHCP configured interfaces
455#		dhcp   - list only DHCP configured interfaces
456#	If no argument is specified all network interfaces are output.
457#	Note that the list will include cloned interfaces if applicable.
458#	Cloned interfaces must already exist to have a chance to appear
459#	in the list if ${network_interfaces} is set to `auto'.
460#
461list_net_interfaces()
462{
463	type=$1
464
465	# Get a list of ALL the interfaces and make lo0 first if it's there.
466	#
467	case ${network_interfaces} in
468	[Aa][Uu][Tt][Oo])
469		_prefix=''
470		_autolist="`ifconfig -l`"
471		_lo=
472		for _if in ${_autolist} ; do
473			if autoif $_if; then
474				if [ "$_if" = "lo0" ]; then
475					_lo="lo0 "
476				else
477					_tmplist="${_tmplist}${_prefix}${_if}"
478					[ -z "$_prefix" ] && _prefix=' '
479				fi
480			fi
481		done
482		_tmplist="${_lo}${_tmplist}"
483		;;
484	*)
485		_tmplist="${network_interfaces} ${cloned_interfaces}"
486		;;
487	esac
488
489	if [ -z "$type" ]; then
490		echo $_tmplist
491		return 0
492	fi
493
494	# Separate out dhcp and non-dhcp interfaces
495	#
496	_aprefix=
497	_bprefix=
498	for _if in ${_tmplist} ; do
499		if dhcpif $_if; then
500			_dhcplist="${_dhcplist}${_aprefix}${_if}"
501			[ -z "$_aprefix" ] && _aprefix=' '
502		elif [ -n "`_ifconfig_getargs $if`" ]; then
503			_nodhcplist="${_nodhcplist}${_bprefix}${_if}"
504			[ -z "$_bprefix" ] && _bprefix=' '
505		fi
506	done
507
508	case "$type" in
509	nodhcp)
510		echo $_nodhcplist
511		;;
512	dhcp)
513		echo $_dhcplist
514		;;
515	esac
516	return 0
517}
518
519hexdigit()
520{
521	if [ $1 -lt 10 ]; then
522		echo $1
523	else
524		case $1 in
525		10)	echo a ;;
526		11)	echo b ;;
527		12)	echo c ;;
528		13)	echo d ;;
529		14)	echo e ;;
530		15)	echo f ;;
531		esac
532	fi
533}
534
535hexprint()
536{
537	val=$1
538	str=''
539
540	dig=`hexdigit $((${val} & 15))`
541	str=${dig}${str}
542	val=$((${val} >> 4))
543	while [ ${val} -gt 0 ]; do
544		dig=`hexdigit $((${val} & 15))`
545		str=${dig}${str}
546		val=$((${val} >> 4))
547	done
548
549	echo ${str}
550}
551
552# Setup the interfaces for IPv6
553network6_interface_setup()
554{
555	interfaces=$*
556	rtsol_interfaces=''
557	case ${ipv6_gateway_enable} in
558	[Yy][Ee][Ss])
559		rtsol_available=no
560		;;
561	*)
562		rtsol_available=yes
563		;;
564	esac
565	for i in $interfaces; do
566		rtsol_interface=yes
567		eval prefix=\$ipv6_prefix_$i
568		if [ -n "${prefix}" ]; then
569			rtsol_available=no
570			rtsol_interface=no
571			laddr=`network6_getladdr $i`
572			hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
573			for j in ${prefix}; do
574				address=$j\:${hostid}
575				ifconfig $i inet6 ${address} prefixlen 64 alias
576
577				case ${ipv6_gateway_enable} in
578				[Yy][Ee][Ss])
579					# subnet-router anycast address
580					# (rfc2373)
581					ifconfig $i inet6 $j:: prefixlen 64 \
582						alias anycast
583					;;
584				esac
585			done
586		fi
587		eval ipv6_ifconfig=\$ipv6_ifconfig_$i
588		if [ -n "${ipv6_ifconfig}" ]; then
589			rtsol_available=no
590			rtsol_interface=no
591			ifconfig $i inet6 ${ipv6_ifconfig} alias
592		fi
593
594		if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
595		then
596			case ${i} in
597			lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
598				;;
599			*)
600				rtsol_interfaces="${rtsol_interfaces} ${i}"
601				;;
602			esac
603		else
604			ifconfig $i inet6
605		fi
606	done
607
608	if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
609		# Act as endhost - automatically configured.
610		# You can configure only single interface, as
611		# specification assumes that autoconfigured host has
612		# single interface only.
613		sysctl net.inet6.ip6.accept_rtadv=1
614		set ${rtsol_interfaces}
615		ifconfig $1 up
616		rtsol ${rtsol_flags} $1
617	fi
618
619	for i in $interfaces; do
620		alias=0
621		while : ; do
622			eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
623			if [ -z "${ipv6_ifconfig}" ]; then
624				break;
625			fi
626			ifconfig $i inet6 ${ipv6_ifconfig} alias
627			alias=$((${alias} + 1))
628		done
629	done
630}
631
632# Setup IPv6 to IPv4 mapping
633network6_stf_setup()
634{
635	case ${stf_interface_ipv4addr} in
636	[Nn][Oo] | '')
637		;;
638	*)
639		# assign IPv6 addr and interface route for 6to4 interface
640		stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
641		OIFS="$IFS"
642		IFS=".$IFS"
643		set ${stf_interface_ipv4addr}
644		IFS="$OIFS"
645		hexfrag1=`hexprint $(($1*256 + $2))`
646		hexfrag2=`hexprint $(($3*256 + $4))`
647		ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
648		case ${stf_interface_ipv6_ifid} in
649		[Aa][Uu][Tt][Oo] | '')
650			for i in ${ipv6_network_interfaces}; do
651				laddr=`network6_getladdr ${i}`
652				case ${laddr} in
653				'')
654					;;
655				*)
656					break
657					;;
658				esac
659			done
660			stf_interface_ipv6_ifid=`expr "${laddr}" : \
661						      'fe80::\(.*\)%\(.*\)'`
662			case ${stf_interface_ipv6_ifid} in
663			'')
664				stf_interface_ipv6_ifid=0:0:0:1
665				;;
666			esac
667			;;
668		esac
669		ifconfig stf0 create >/dev/null 2>&1
670		ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
671			prefixlen ${stf_prefixlen}
672		# disallow packets to malicious 6to4 prefix
673		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
674		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
675		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
676		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
677		;;
678	esac
679}
680
681# Setup static routes
682network6_static_routes_setup()
683{
684	# Set up any static routes.
685	case ${ipv6_defaultrouter} in
686	[Nn][Oo] | '')
687		;;
688	*)
689		ipv6_static_routes="default ${ipv6_static_routes}"
690		ipv6_route_default="default ${ipv6_defaultrouter}"
691		;;
692	esac
693	case ${ipv6_static_routes} in
694	[Nn][Oo] | '')
695		;;
696	*)
697		for i in ${ipv6_static_routes}; do
698			eval ipv6_route_args=\$ipv6_route_${i}
699			route add -inet6 ${ipv6_route_args}
700		done
701		;;
702	esac
703}
704
705# Setup faith
706network6_faith_setup()
707{
708	case ${ipv6_faith_prefix} in
709	[Nn][Oo] | '')
710		;;
711	*)
712		sysctl net.inet6.ip6.keepfaith=1
713		ifconfig faith0 create >/dev/null 2>&1
714		ifconfig faith0 up
715		for prefix in ${ipv6_faith_prefix}; do
716			prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
717			case ${prefixlen} in
718			'')
719				prefixlen=96
720				;;
721			*)
722				prefix=`expr "${prefix}" : \
723					     "\(.*\)/${prefixlen}"`
724				;;
725			esac
726			route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
727			route change -inet6 ${prefix} -prefixlen ${prefixlen} \
728				-ifp faith0
729		done
730		;;
731	esac
732}
733
734# Install the "default interface" to kernel, which will be used
735# as the default route when there's no router.
736network6_default_interface_setup()
737{
738	# Choose IPv6 default interface if it is not clearly specified.
739	case ${ipv6_default_interface} in
740	'')
741		for i in ${ipv6_network_interfaces}; do
742			case $i in
743			lo0|faith[0-9]*)
744				continue
745				;;
746			esac
747			laddr=`network6_getladdr $i exclude_tentative`
748			case ${laddr} in
749			'')
750				;;
751			*)
752				ipv6_default_interface=$i
753				break
754				;;
755			esac
756		done
757		;;
758	esac
759
760	# Disallow unicast packets without outgoing scope identifiers,
761	# or route such packets to a "default" interface, if it is specified.
762	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
763	case ${ipv6_default_interface} in
764	[Nn][Oo] | '')
765		route add -inet6 ff02:: -prefixlen 16 ::1 -reject
766		;;
767	*)
768		laddr=`network6_getladdr ${ipv6_default_interface}`
769		route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
770			-cloning
771
772		# Disable installing the default interface with the
773		# case net.inet6.ip6.forwarding=0 and
774		# net.inet6.ip6.accept_rtadv=0, due to avoid conflict
775		# between the default router list and the manual
776		# configured default route.
777		case ${ipv6_gateway_enable} in
778		[Yy][Ee][Ss])
779			;;
780		*)
781			if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
782			then
783				ndp -I ${ipv6_default_interface}
784			fi
785			;;
786		esac
787		;;
788	esac
789}
790
791network6_getladdr()
792{
793	ifconfig $1 2>/dev/null | while read proto addr rest; do
794		case ${proto} in
795		inet6)
796			case ${addr} in
797			fe80::*)
798				if [ -z "$2" ]; then
799					echo ${addr}
800					return
801				fi
802				case ${rest} in
803				*tentative*)
804					continue
805					;;
806				*)
807					echo ${addr}
808					return
809				esac
810			esac
811		esac
812	done
813}
814