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