network.subr revision 152441
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 152441 2005-11-14 23:34:50Z 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 46149726Sbrooks ifconfig $1 up 47149483Sbrooks eval "ifconfig $1 ${ifconfig_args}" 48147088Sbrooks _cfg=0 49113674Smtm fi 50147088Sbrooks 51147088Sbrooks if wpaif $1; then 52149726Sbrooks if [ $_cfg -ne 0 ] ; then 53149726Sbrooks ifconfig $1 up 54149726Sbrooks fi 55147682Sbrooks /etc/rc.d/wpa_supplicant start $1 56147088Sbrooks _cfg=0 # XXX: not sure this should count 57147088Sbrooks fi 58147088Sbrooks 59147088Sbrooks if dhcpif $1; then 60149726Sbrooks if [ $_cfg -ne 0 ] ; then 61149726Sbrooks ifconfig $1 up 62149726Sbrooks fi 63147088Sbrooks /etc/rc.d/dhclient start $1 64147088Sbrooks _cfg=0 65147088Sbrooks fi 66147088Sbrooks 67147121Sbrooks return $_cfg 68113674Smtm} 6925184Sjkh 70116029Smtm# ifconfig_down if 71116029Smtm# Remove all inet entries from the $if interface. It returns 72116029Smtm# 0 if inet entries were found and removed. It returns 1 if 73116100Smtm# no entries were found or they could not be removed. 74116029Smtm# 75116029Smtmifconfig_down() 76116029Smtm{ 77116029Smtm [ -z "$1" ] && return 1 78116029Smtm _ifs="^" 79147121Sbrooks _cfg=1 80116029Smtm 81116029Smtm inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`" 82116029Smtm 83116029Smtm oldifs="$IFS" 84116029Smtm IFS="$_ifs" 85116029Smtm for _inet in $inetList ; do 86116029Smtm # get rid of extraneous line 87116029Smtm [ -z "$_inet" ] && break 88116029Smtm 89116029Smtm _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 90116029Smtm 91116032Smtm IFS="$oldifs" 92116032Smtm ifconfig $1 ${_inet} delete 93116032Smtm IFS="$_ifs" 94147121Sbrooks _cfg=0 95116029Smtm done 96116029Smtm IFS="$oldifs" 97116029Smtm 98147088Sbrooks if wpaif $1; then 99147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 100147121Sbrooks _cfg=0 101147088Sbrooks fi 102147088Sbrooks 103147088Sbrooks if dhcpif $1; then 104147088Sbrooks /etc/rc.d/dhclient stop $1 105147088Sbrooks _cfg=0 106147088Sbrooks fi 107147088Sbrooks 108147121Sbrooks return $_cfg 109116029Smtm} 110116029Smtm 111147088Sbrooks# _ifconfig_getargs if 112147088Sbrooks# Echos the arguments for the supplied interface to stdout. 113147088Sbrooks# returns 1 if empty. In general, ifconfig_getargs should be used 114147088Sbrooks# outside this file. 115147088Sbrooks_ifconfig_getargs() 116147088Sbrooks{ 117147088Sbrooks _ifn=$1 118147088Sbrooks if [ -z "$_ifn" ]; then 119147088Sbrooks return 1 120147088Sbrooks fi 121147088Sbrooks 122149726Sbrooks eval _args=\${ifconfig_$1-$ifconfig_DEFAULT} 123147088Sbrooks 124147684Sbrooks 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 ;; 142149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 143149401Sbrooks ;; 144147088Sbrooks [Ww][Pp][Aa]) 145147088Sbrooks ;; 146147088Sbrooks *) 147147088Sbrooks _args="$_args $_arg" 148147088Sbrooks ;; 149147088Sbrooks esac 150147088Sbrooks done 151147088Sbrooks 152147088Sbrooks echo $_args 153147088Sbrooks} 154147088Sbrooks 155149401Sbrooks# autoif 156149401Sbrooks# Returns 0 if the interface should be automaticly configured at 157149401Sbrooks# boot time and 1 otherwise. 158149401Sbrooksautoif() 159149401Sbrooks{ 160149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 161149401Sbrooks for _arg in $_tmpargs; do 162149401Sbrooks case $_arg in 163149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 164149401Sbrooks return 1 165149401Sbrooks ;; 166149401Sbrooks esac 167149401Sbrooks done 168149401Sbrooks return 0 169149401Sbrooks} 170149401Sbrooks 171147088Sbrooks# dhcpif if 172147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 173147088Sbrooksdhcpif() 174147088Sbrooks{ 175147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 176147088Sbrooks for _arg in $_tmpargs; do 177147088Sbrooks case $_arg in 178147088Sbrooks [Dd][Hh][Cc][Pp]) 179147088Sbrooks return 0 180147088Sbrooks ;; 181147088Sbrooks esac 182147088Sbrooks done 183147088Sbrooks return 1 184147088Sbrooks} 185147088Sbrooks 186147088Sbrooks# wpaif if 187147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 188147088Sbrookswpaif() 189147088Sbrooks{ 190147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 191147088Sbrooks for _arg in $_tmpargs; do 192147088Sbrooks case $_arg in 193147088Sbrooks [Ww][Pp][Aa]) 194147088Sbrooks return 0 195147088Sbrooks ;; 196147088Sbrooks esac 197147088Sbrooks done 198147088Sbrooks return 1 199147088Sbrooks} 200147088Sbrooks 201152441Sbrooks# ipv4_up if 202152441Sbrooks# add IPv4 addresses to the interface $if 203152441Sbrooksipv4_up() 204152441Sbrooks{ 205152441Sbrooks _if=$1 206152441Sbrooks ifalias_up ${_if} 207152441Sbrooks ipv4_addrs_common ${_if} alias 208152441Sbrooks} 209152441Sbrooks 210152441Sbrooks# ipv4_down if 211152441Sbrooks# remove IPv4 addresses from the interface $if 212152441Sbrooksipv4_down() 213152441Sbrooks{ 214152441Sbrooks _if=$1 215152441Sbrooks ifalias_down ${_if} 216152441Sbrooks ipv4_addrs_common ${_if} -alias 217152441Sbrooks} 218152441Sbrooks 219152441Sbrooks# ipv4_addrs_common if action 220152441Sbrooks# Evaluate the ifconfig_if_ipv4 arguments for interface $if 221152441Sbrooks# and use $action to add or remove IPv4 addresses from $if. 222152441Sbrooksipv4_addrs_common() 223152441Sbrooks{ 224152441Sbrooks _ret=1 225152441Sbrooks _if=$1 226152441Sbrooks _action=$2 227152441Sbrooks 228152441Sbrooks # get ipv4-addresses 229152441Sbrooks eval cidr_addr=\${ipv4_addrs_${_if}} 230152441Sbrooks 231152441Sbrooks for _cidr in ${cidr_addr}; do 232152441Sbrooks _ipaddr=${_cidr%%/*} 233152441Sbrooks _netmask="/"${_cidr##*/} 234152441Sbrooks _range=${_ipaddr##*.} 235152441Sbrooks _ipnet=${_ipaddr%.*} 236152441Sbrooks _iplow=${_range%-*} 237152441Sbrooks _iphigh=${_range#*-} 238152441Sbrooks 239152441Sbrooks # clear netmask when removing aliases 240152441Sbrooks if [ "${_action}" = "-alias" ]; then 241152441Sbrooks _netmask="" 242152441Sbrooks fi 243152441Sbrooks 244152441Sbrooks _ipcount=${_iplow} 245152441Sbrooks while [ "${_ipcount}" -le "${_iphigh}" ]; do 246152441Sbrooks eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}" 247152441Sbrooks _ipcount=$((${_ipcount}+1)) 248152441Sbrooks _ret=0 249152441Sbrooks 250152441Sbrooks # only the first ipaddr in a subnet need the real netmask 251152441Sbrooks if [ "${_action}" != "-alias" ]; then 252152441Sbrooks _netmask="/32" 253152441Sbrooks fi 254152441Sbrooks done 255152441Sbrooks done 256152441Sbrooks return $_ret 257152441Sbrooks} 258152441Sbrooks 259113674Smtm# ifalias_up if 260113674Smtm# Configure aliases for network interface $if. 261113674Smtm# It returns 0 if at least one alias was configured or 262113674Smtm# 1 if there were none. 263113674Smtm# 264113674Smtmifalias_up() 265113674Smtm{ 266113674Smtm _ret=1 267113674Smtm alias=0 268113674Smtm while : ; do 269113674Smtm eval ifconfig_args=\$ifconfig_$1_alias${alias} 270113674Smtm if [ -n "${ifconfig_args}" ]; then 271113674Smtm ifconfig $1 ${ifconfig_args} alias 272113674Smtm alias=$((${alias} + 1)) 273113674Smtm _ret=0 274113674Smtm else 275113674Smtm break 276113674Smtm fi 277113674Smtm done 278113674Smtm return $_ret 279113674Smtm} 280100280Sgordon 281116029Smtm#ifalias_down if 282116029Smtm# Remove aliases for network interface $if. 283116029Smtm# It returns 0 if at least one alias was removed or 284116029Smtm# 1 if there were none. 285116029Smtm# 286116029Smtmifalias_down() 287116029Smtm{ 288116029Smtm _ret=1 289116029Smtm alias=0 290116029Smtm while : ; do 291116029Smtm eval ifconfig_args=\$ifconfig_$1_alias${alias} 292116029Smtm if [ -n "${ifconfig_args}" ]; then 293116029Smtm ifconfig $1 ${ifconfig_args} -alias 294116029Smtm alias=$((${alias} + 1)) 295116029Smtm _ret=0 296116029Smtm else 297116029Smtm break 298116029Smtm fi 299116029Smtm done 300116029Smtm return $_ret 301116029Smtm} 302116029Smtm 303113674Smtm# ifscript_up if 304113674Smtm# Evaluate a startup script for the $if interface. 305113674Smtm# It returns 0 if a script was found and processed or 306113674Smtm# 1 if no script was found. 307113674Smtm# 308113674Smtmifscript_up() 309100280Sgordon{ 310113674Smtm if [ -r /etc/start_if.$1 ]; then 311113674Smtm . /etc/start_if.$1 312113674Smtm return 0 313113674Smtm fi 314113674Smtm return 1 315100280Sgordon} 316100280Sgordon 317116029Smtm# ifscript_down if 318116029Smtm# Evaluate a shutdown script for the $if interface. 319116029Smtm# It returns 0 if a script was found and processed or 320116029Smtm# 1 if no script was found. 321116029Smtm# 322116029Smtmifscript_down() 323116029Smtm{ 324116029Smtm if [ -r /etc/stop_if.$1 ]; then 325116029Smtm . /etc/stop_if.$1 326116029Smtm return 0 327116029Smtm fi 328116029Smtm return 1 329116029Smtm} 330116029Smtm 331113674Smtm# Create cloneable interfaces. 332113674Smtm# 333113674Smtmclone_up() 334100280Sgordon{ 335113674Smtm _prefix= 336113674Smtm _list= 337113674Smtm for ifn in ${cloned_interfaces}; do 338113674Smtm ifconfig ${ifn} create 339116774Skuriyama if [ $? -eq 0 ]; then 340113674Smtm _list="${_list}${_prefix}${ifn}" 341113674Smtm [ -z "$_prefix" ] && _prefix=' ' 342113674Smtm fi 343113674Smtm done 344113674Smtm debug "Cloned: ${_list}" 345113674Smtm} 346100280Sgordon 347113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed 348113674Smtm# to standard output. 349113674Smtm# 350113674Smtmclone_down() 351113674Smtm{ 352113674Smtm _prefix= 353113674Smtm _list= 354113674Smtm for ifn in ${cloned_interfaces}; do 355113674Smtm ifconfig ${ifn} destroy 356116774Skuriyama if [ $? -eq 0 ]; then 357113674Smtm _list="${_list}${_prefix}${ifn}" 358113674Smtm [ -z "$_prefix" ] && _prefix=' ' 359113674Smtm fi 360113674Smtm done 361113674Smtm debug "Destroyed clones: ${_list}" 362100280Sgordon} 363100280Sgordon 364113674Smtmgif_up() { 365100282Sdougb case ${gif_interfaces} in 366100282Sdougb [Nn][Oo] | '') 367100282Sdougb ;; 368100282Sdougb *) 369100282Sdougb for i in ${gif_interfaces}; do 370100282Sdougb eval peers=\$gifconfig_$i 371100282Sdougb case ${peers} in 372100282Sdougb '') 373100282Sdougb continue 374100282Sdougb ;; 375100282Sdougb *) 376100282Sdougb ifconfig $i create >/dev/null 2>&1 377100282Sdougb ifconfig $i tunnel ${peers} 378103710Sume ifconfig $i up 379100282Sdougb ;; 380100282Sdougb esac 381100282Sdougb done 382100282Sdougb ;; 383100282Sdougb esac 384100282Sdougb} 385100282Sdougb 386113674Smtm# 387113674Smtm# ipx_up ifn 388113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 389113674Smtm# arguments were found and configured; returns 1 otherwise. 390113674Smtm# 391113674Smtmipx_up() 392100280Sgordon{ 393113674Smtm ifn="$1" 394113674Smtm eval ifconfig_args=\$ifconfig_${ifn}_ipx 395113674Smtm if [ -n "${ifconfig_args}" ]; then 396113674Smtm ifconfig ${ifn} ${ifconfig_args} 397113674Smtm return 0 39885831Sdes fi 399113674Smtm return 1 400113674Smtm} 40185831Sdes 402116029Smtm# ipx_down ifn 403116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 404116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 405113674Smtm# 406116029Smtmipx_down() 407116029Smtm{ 408116100Smtm [ -z "$1" ] && return 1 409116100Smtm _ifs="^" 410116100Smtm _ret=1 411116100Smtm 412116100Smtm ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`" 413116100Smtm 414116100Smtm oldifs="$IFS" 415116100Smtm IFS="$_ifs" 416116100Smtm for _ipx in $ipxList ; do 417116100Smtm # get rid of extraneous line 418116100Smtm [ -z "$_ipx" ] && break 419116100Smtm 420116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 421116100Smtm 422116100Smtm IFS="$oldifs" 423116100Smtm ifconfig $1 ${_ipx} delete 424116100Smtm IFS="$_ifs" 425116100Smtm _ret=0 426116100Smtm done 427116100Smtm IFS="$oldifs" 428116100Smtm 429116100Smtm return $_ret 430116029Smtm} 431116029Smtm 432137070Spjd# ifnet_rename 433137070Spjd# Rename all requested interfaces. 434116029Smtm# 435137070Spjdifnet_rename() 436137070Spjd{ 437137070Spjd 438138386Srse _ifn_list="`ifconfig -l`" 439137070Spjd [ -z "$_ifn_list" ] && return 0 440137070Spjd for _if in ${_ifn_list} ; do 441137070Spjd eval _ifname=\$ifconfig_${_if}_name 442137070Spjd if [ ! -z "$_ifname" ]; then 443137070Spjd ifconfig $_if name $_ifname 444137070Spjd fi 445137070Spjd done 446137070Spjd return 0 447137070Spjd} 448137070Spjd 449137070Spjd# 450113674Smtm# list_net_interfaces type 451113674Smtm# List all network interfaces. The type of interface returned 452113674Smtm# can be controlled by the type argument. The type 453113674Smtm# argument can be any of the following: 454113674Smtm# nodhcp - all interfaces, excluding DHCP configured interfaces 455113674Smtm# dhcp - list only DHCP configured interfaces 456113674Smtm# If no argument is specified all network interfaces are output. 457134429Syar# Note that the list will include cloned interfaces if applicable. 458134429Syar# Cloned interfaces must already exist to have a chance to appear 459134429Syar# in the list if ${network_interfaces} is set to `auto'. 460113674Smtm# 461113674Smtmlist_net_interfaces() 462113674Smtm{ 463113674Smtm type=$1 46465532Snectar 465149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 46651231Ssheldonh # 46751231Ssheldonh case ${network_interfaces} in 46851231Ssheldonh [Aa][Uu][Tt][Oo]) 469149401Sbrooks _prefix='' 470149401Sbrooks _autolist="`ifconfig -l`" 471149726Sbrooks _lo= 472149401Sbrooks for _if in ${_autolist} ; do 473149401Sbrooks if autoif $_if; then 474149726Sbrooks if [ "$_if" = "lo0" ]; then 475149726Sbrooks _lo="lo0 " 476149726Sbrooks else 477149726Sbrooks _tmplist="${_tmplist}${_prefix}${_if}" 478149726Sbrooks [ -z "$_prefix" ] && _prefix=' ' 479149726Sbrooks fi 480149401Sbrooks fi 481149401Sbrooks done 482149726Sbrooks _tmplist="${_lo}${_tmplist}" 48351231Ssheldonh ;; 48483677Sbrooks *) 485149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 48683677Sbrooks ;; 48751231Ssheldonh esac 48849122Sbrian 489113674Smtm if [ -z "$type" ]; then 490113674Smtm echo $_tmplist 491113674Smtm return 0 492113674Smtm fi 49349122Sbrian 494138385Srse # Separate out dhcp and non-dhcp interfaces 495113674Smtm # 496113674Smtm _aprefix= 497134376Syar _bprefix= 498113674Smtm for _if in ${_tmplist} ; do 499147684Sbrooks if dhcpif $_if; then 500113674Smtm _dhcplist="${_dhcplist}${_aprefix}${_if}" 501113674Smtm [ -z "$_aprefix" ] && _aprefix=' ' 502147684Sbrooks elif [ -n "`_ifconfig_getargs $if`" ]; then 503113674Smtm _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 504113674Smtm [ -z "$_bprefix" ] && _bprefix=' ' 505147684Sbrooks fi 50654458Sobrien done 50751231Ssheldonh 508113674Smtm case "$type" in 509113674Smtm nodhcp) 510113674Smtm echo $_nodhcplist 511113674Smtm ;; 512113674Smtm dhcp) 513113674Smtm echo $_dhcplist 514113674Smtm ;; 515113674Smtm esac 516130151Sschweikh return 0 51725184Sjkh} 518114942Sume 519114942Sumehexdigit() 520114942Sume{ 521114942Sume if [ $1 -lt 10 ]; then 522114942Sume echo $1 523114942Sume else 524114942Sume case $1 in 525114942Sume 10) echo a ;; 526114942Sume 11) echo b ;; 527114942Sume 12) echo c ;; 528114942Sume 13) echo d ;; 529114942Sume 14) echo e ;; 530114942Sume 15) echo f ;; 531114942Sume esac 532114942Sume fi 533114942Sume} 534114942Sume 535114942Sumehexprint() 536114942Sume{ 537114942Sume val=$1 538114942Sume str='' 539114942Sume 540114942Sume dig=`hexdigit $((${val} & 15))` 541114942Sume str=${dig}${str} 542114942Sume val=$((${val} >> 4)) 543114942Sume while [ ${val} -gt 0 ]; do 544114942Sume dig=`hexdigit $((${val} & 15))` 545114942Sume str=${dig}${str} 546114942Sume val=$((${val} >> 4)) 547114942Sume done 548114942Sume 549114942Sume echo ${str} 550114942Sume} 551114942Sume 552114942Sume# Setup the interfaces for IPv6 553114942Sumenetwork6_interface_setup() 554114942Sume{ 555114942Sume interfaces=$* 556114942Sume rtsol_interfaces='' 557114942Sume case ${ipv6_gateway_enable} in 558114942Sume [Yy][Ee][Ss]) 559114942Sume rtsol_available=no 560114942Sume ;; 561114942Sume *) 562114942Sume rtsol_available=yes 563114942Sume ;; 564114942Sume esac 565114942Sume for i in $interfaces; do 566114942Sume rtsol_interface=yes 567114942Sume eval prefix=\$ipv6_prefix_$i 568114942Sume if [ -n "${prefix}" ]; then 569114942Sume rtsol_available=no 570114942Sume rtsol_interface=no 571114942Sume laddr=`network6_getladdr $i` 572114942Sume hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'` 573114942Sume for j in ${prefix}; do 574114942Sume address=$j\:${hostid} 575114942Sume ifconfig $i inet6 ${address} prefixlen 64 alias 576114942Sume 577114942Sume case ${ipv6_gateway_enable} in 578114942Sume [Yy][Ee][Ss]) 579114942Sume # subnet-router anycast address 580114942Sume # (rfc2373) 581114942Sume ifconfig $i inet6 $j:: prefixlen 64 \ 582114942Sume alias anycast 583114942Sume ;; 584114942Sume esac 585114942Sume done 586114942Sume fi 587114942Sume eval ipv6_ifconfig=\$ipv6_ifconfig_$i 588114942Sume if [ -n "${ipv6_ifconfig}" ]; then 589114942Sume rtsol_available=no 590114942Sume rtsol_interface=no 591114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 592114942Sume fi 593114942Sume 594114942Sume if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ] 595114942Sume then 596114942Sume case ${i} in 597114942Sume lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*) 598114942Sume ;; 599114942Sume *) 600114942Sume rtsol_interfaces="${rtsol_interfaces} ${i}" 601114942Sume ;; 602114942Sume esac 603114942Sume else 604114942Sume ifconfig $i inet6 605114942Sume fi 606114942Sume done 607114942Sume 608114942Sume if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then 609114942Sume # Act as endhost - automatically configured. 610114942Sume # You can configure only single interface, as 611114942Sume # specification assumes that autoconfigured host has 612114942Sume # single interface only. 613114942Sume sysctl net.inet6.ip6.accept_rtadv=1 614114942Sume set ${rtsol_interfaces} 615114942Sume ifconfig $1 up 616118666Sume rtsol ${rtsol_flags} $1 617114942Sume fi 618114942Sume 619114942Sume for i in $interfaces; do 620114942Sume alias=0 621114942Sume while : ; do 622114942Sume eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias} 623114942Sume if [ -z "${ipv6_ifconfig}" ]; then 624114942Sume break; 625114942Sume fi 626114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 627114942Sume alias=$((${alias} + 1)) 628114942Sume done 629114942Sume done 630114942Sume} 631114942Sume 632114942Sume# Setup IPv6 to IPv4 mapping 633114942Sumenetwork6_stf_setup() 634114942Sume{ 635114942Sume case ${stf_interface_ipv4addr} in 636114942Sume [Nn][Oo] | '') 637114942Sume ;; 638114942Sume *) 639114942Sume # assign IPv6 addr and interface route for 6to4 interface 640114942Sume stf_prefixlen=$((16+${stf_interface_ipv4plen:-0})) 641114942Sume OIFS="$IFS" 642114942Sume IFS=".$IFS" 643114942Sume set ${stf_interface_ipv4addr} 644114942Sume IFS="$OIFS" 645114942Sume hexfrag1=`hexprint $(($1*256 + $2))` 646114942Sume hexfrag2=`hexprint $(($3*256 + $4))` 647114942Sume ipv4_in_hexformat="${hexfrag1}:${hexfrag2}" 648114942Sume case ${stf_interface_ipv6_ifid} in 649114942Sume [Aa][Uu][Tt][Oo] | '') 650114942Sume for i in ${ipv6_network_interfaces}; do 651114942Sume laddr=`network6_getladdr ${i}` 652114942Sume case ${laddr} in 653114942Sume '') 654114942Sume ;; 655114942Sume *) 656114942Sume break 657114942Sume ;; 658114942Sume esac 659114942Sume done 660114942Sume stf_interface_ipv6_ifid=`expr "${laddr}" : \ 661114942Sume 'fe80::\(.*\)%\(.*\)'` 662114942Sume case ${stf_interface_ipv6_ifid} in 663114942Sume '') 664114942Sume stf_interface_ipv6_ifid=0:0:0:1 665114942Sume ;; 666114942Sume esac 667114942Sume ;; 668114942Sume esac 669114942Sume ifconfig stf0 create >/dev/null 2>&1 670114942Sume ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \ 671114942Sume prefixlen ${stf_prefixlen} 672114942Sume # disallow packets to malicious 6to4 prefix 673114942Sume route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 674114942Sume route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 675114942Sume route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 676114942Sume route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 677114942Sume ;; 678114942Sume esac 679114942Sume} 680114942Sume 681114942Sume# Setup static routes 682114942Sumenetwork6_static_routes_setup() 683114942Sume{ 684114942Sume # Set up any static routes. 685114942Sume case ${ipv6_defaultrouter} in 686114942Sume [Nn][Oo] | '') 687114942Sume ;; 688114942Sume *) 689114942Sume ipv6_static_routes="default ${ipv6_static_routes}" 690114942Sume ipv6_route_default="default ${ipv6_defaultrouter}" 691114942Sume ;; 692114942Sume esac 693114942Sume case ${ipv6_static_routes} in 694114942Sume [Nn][Oo] | '') 695114942Sume ;; 696114942Sume *) 697114942Sume for i in ${ipv6_static_routes}; do 698114942Sume eval ipv6_route_args=\$ipv6_route_${i} 699114942Sume route add -inet6 ${ipv6_route_args} 700114942Sume done 701114942Sume ;; 702114942Sume esac 703114942Sume} 704114942Sume 705114942Sume# Setup faith 706114942Sumenetwork6_faith_setup() 707114942Sume{ 708114942Sume case ${ipv6_faith_prefix} in 709114942Sume [Nn][Oo] | '') 710114942Sume ;; 711114942Sume *) 712114942Sume sysctl net.inet6.ip6.keepfaith=1 713114942Sume ifconfig faith0 create >/dev/null 2>&1 714114942Sume ifconfig faith0 up 715114942Sume for prefix in ${ipv6_faith_prefix}; do 716114942Sume prefixlen=`expr "${prefix}" : ".*/\(.*\)"` 717114942Sume case ${prefixlen} in 718114942Sume '') 719114942Sume prefixlen=96 720114942Sume ;; 721114942Sume *) 722114942Sume prefix=`expr "${prefix}" : \ 723114942Sume "\(.*\)/${prefixlen}"` 724114942Sume ;; 725114942Sume esac 726114942Sume route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1 727114942Sume route change -inet6 ${prefix} -prefixlen ${prefixlen} \ 728114942Sume -ifp faith0 729114942Sume done 730114942Sume ;; 731114942Sume esac 732114942Sume} 733114942Sume 734114942Sume# Install the "default interface" to kernel, which will be used 735114942Sume# as the default route when there's no router. 736114942Sumenetwork6_default_interface_setup() 737114942Sume{ 738114942Sume # Choose IPv6 default interface if it is not clearly specified. 739114942Sume case ${ipv6_default_interface} in 740114942Sume '') 741114942Sume for i in ${ipv6_network_interfaces}; do 742114942Sume case $i in 743114942Sume lo0|faith[0-9]*) 744114942Sume continue 745114942Sume ;; 746114942Sume esac 747114942Sume laddr=`network6_getladdr $i exclude_tentative` 748114942Sume case ${laddr} in 749114942Sume '') 750114942Sume ;; 751114942Sume *) 752114942Sume ipv6_default_interface=$i 753114942Sume break 754114942Sume ;; 755114942Sume esac 756114942Sume done 757114942Sume ;; 758114942Sume esac 759114942Sume 760114942Sume # Disallow unicast packets without outgoing scope identifiers, 761114942Sume # or route such packets to a "default" interface, if it is specified. 762114942Sume route add -inet6 fe80:: -prefixlen 10 ::1 -reject 763114942Sume case ${ipv6_default_interface} in 764114942Sume [Nn][Oo] | '') 765114942Sume route add -inet6 ff02:: -prefixlen 16 ::1 -reject 766114942Sume ;; 767114942Sume *) 768114942Sume laddr=`network6_getladdr ${ipv6_default_interface}` 769114942Sume route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \ 770114942Sume -cloning 771114942Sume 772114942Sume # Disable installing the default interface with the 773114942Sume # case net.inet6.ip6.forwarding=0 and 774114942Sume # net.inet6.ip6.accept_rtadv=0, due to avoid conflict 775114942Sume # between the default router list and the manual 776114942Sume # configured default route. 777114942Sume case ${ipv6_gateway_enable} in 778114942Sume [Yy][Ee][Ss]) 779114942Sume ;; 780114942Sume *) 781114942Sume if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ] 782114942Sume then 783114942Sume ndp -I ${ipv6_default_interface} 784114942Sume fi 785114942Sume ;; 786114942Sume esac 787114942Sume ;; 788114942Sume esac 789114942Sume} 790114942Sume 791114942Sumenetwork6_getladdr() 792114942Sume{ 793114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 794114942Sume case ${proto} in 795114942Sume inet6) 796114942Sume case ${addr} in 797114942Sume fe80::*) 798114942Sume if [ -z "$2" ]; then 799114942Sume echo ${addr} 800114942Sume return 801114942Sume fi 802114942Sume case ${rest} in 803114942Sume *tentative*) 804114942Sume continue 805114942Sume ;; 806114942Sume *) 807114942Sume echo ${addr} 808114942Sume return 809114942Sume esac 810114942Sume esac 811114942Sume esac 812114942Sume done 813114942Sume} 814