network.subr revision 149726
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 149726 2005-09-02 17:11:13Z 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 201113674Smtm# ifalias_up if 202113674Smtm# Configure aliases for network interface $if. 203113674Smtm# It returns 0 if at least one alias was configured or 204113674Smtm# 1 if there were none. 205113674Smtm# 206113674Smtmifalias_up() 207113674Smtm{ 208113674Smtm _ret=1 209113674Smtm alias=0 210113674Smtm while : ; do 211113674Smtm eval ifconfig_args=\$ifconfig_$1_alias${alias} 212113674Smtm if [ -n "${ifconfig_args}" ]; then 213113674Smtm ifconfig $1 ${ifconfig_args} alias 214113674Smtm alias=$((${alias} + 1)) 215113674Smtm _ret=0 216113674Smtm else 217113674Smtm break 218113674Smtm fi 219113674Smtm done 220113674Smtm return $_ret 221113674Smtm} 222100280Sgordon 223116029Smtm#ifalias_down if 224116029Smtm# Remove aliases for network interface $if. 225116029Smtm# It returns 0 if at least one alias was removed or 226116029Smtm# 1 if there were none. 227116029Smtm# 228116029Smtmifalias_down() 229116029Smtm{ 230116029Smtm _ret=1 231116029Smtm alias=0 232116029Smtm while : ; do 233116029Smtm eval ifconfig_args=\$ifconfig_$1_alias${alias} 234116029Smtm if [ -n "${ifconfig_args}" ]; then 235116029Smtm ifconfig $1 ${ifconfig_args} -alias 236116029Smtm alias=$((${alias} + 1)) 237116029Smtm _ret=0 238116029Smtm else 239116029Smtm break 240116029Smtm fi 241116029Smtm done 242116029Smtm return $_ret 243116029Smtm} 244116029Smtm 245113674Smtm# ifscript_up if 246113674Smtm# Evaluate a startup script for the $if interface. 247113674Smtm# It returns 0 if a script was found and processed or 248113674Smtm# 1 if no script was found. 249113674Smtm# 250113674Smtmifscript_up() 251100280Sgordon{ 252113674Smtm if [ -r /etc/start_if.$1 ]; then 253113674Smtm . /etc/start_if.$1 254113674Smtm return 0 255113674Smtm fi 256113674Smtm return 1 257100280Sgordon} 258100280Sgordon 259116029Smtm# ifscript_down if 260116029Smtm# Evaluate a shutdown script for the $if interface. 261116029Smtm# It returns 0 if a script was found and processed or 262116029Smtm# 1 if no script was found. 263116029Smtm# 264116029Smtmifscript_down() 265116029Smtm{ 266116029Smtm if [ -r /etc/stop_if.$1 ]; then 267116029Smtm . /etc/stop_if.$1 268116029Smtm return 0 269116029Smtm fi 270116029Smtm return 1 271116029Smtm} 272116029Smtm 273113674Smtm# Create cloneable interfaces. 274113674Smtm# 275113674Smtmclone_up() 276100280Sgordon{ 277113674Smtm _prefix= 278113674Smtm _list= 279113674Smtm for ifn in ${cloned_interfaces}; do 280113674Smtm ifconfig ${ifn} create 281116774Skuriyama if [ $? -eq 0 ]; then 282113674Smtm _list="${_list}${_prefix}${ifn}" 283113674Smtm [ -z "$_prefix" ] && _prefix=' ' 284113674Smtm fi 285113674Smtm done 286113674Smtm debug "Cloned: ${_list}" 287113674Smtm} 288100280Sgordon 289113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed 290113674Smtm# to standard output. 291113674Smtm# 292113674Smtmclone_down() 293113674Smtm{ 294113674Smtm _prefix= 295113674Smtm _list= 296113674Smtm for ifn in ${cloned_interfaces}; do 297113674Smtm ifconfig ${ifn} destroy 298116774Skuriyama if [ $? -eq 0 ]; then 299113674Smtm _list="${_list}${_prefix}${ifn}" 300113674Smtm [ -z "$_prefix" ] && _prefix=' ' 301113674Smtm fi 302113674Smtm done 303113674Smtm debug "Destroyed clones: ${_list}" 304100280Sgordon} 305100280Sgordon 306113674Smtmgif_up() { 307100282Sdougb case ${gif_interfaces} in 308100282Sdougb [Nn][Oo] | '') 309100282Sdougb ;; 310100282Sdougb *) 311100282Sdougb for i in ${gif_interfaces}; do 312100282Sdougb eval peers=\$gifconfig_$i 313100282Sdougb case ${peers} in 314100282Sdougb '') 315100282Sdougb continue 316100282Sdougb ;; 317100282Sdougb *) 318100282Sdougb ifconfig $i create >/dev/null 2>&1 319100282Sdougb ifconfig $i tunnel ${peers} 320103710Sume ifconfig $i up 321100282Sdougb ;; 322100282Sdougb esac 323100282Sdougb done 324100282Sdougb ;; 325100282Sdougb esac 326100282Sdougb} 327100282Sdougb 328113674Smtm# 329113674Smtm# ipx_up ifn 330113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 331113674Smtm# arguments were found and configured; returns 1 otherwise. 332113674Smtm# 333113674Smtmipx_up() 334100280Sgordon{ 335113674Smtm ifn="$1" 336113674Smtm eval ifconfig_args=\$ifconfig_${ifn}_ipx 337113674Smtm if [ -n "${ifconfig_args}" ]; then 338113674Smtm ifconfig ${ifn} ${ifconfig_args} 339113674Smtm return 0 34085831Sdes fi 341113674Smtm return 1 342113674Smtm} 34385831Sdes 344116029Smtm# ipx_down ifn 345116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 346116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 347113674Smtm# 348116029Smtmipx_down() 349116029Smtm{ 350116100Smtm [ -z "$1" ] && return 1 351116100Smtm _ifs="^" 352116100Smtm _ret=1 353116100Smtm 354116100Smtm ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`" 355116100Smtm 356116100Smtm oldifs="$IFS" 357116100Smtm IFS="$_ifs" 358116100Smtm for _ipx in $ipxList ; do 359116100Smtm # get rid of extraneous line 360116100Smtm [ -z "$_ipx" ] && break 361116100Smtm 362116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 363116100Smtm 364116100Smtm IFS="$oldifs" 365116100Smtm ifconfig $1 ${_ipx} delete 366116100Smtm IFS="$_ifs" 367116100Smtm _ret=0 368116100Smtm done 369116100Smtm IFS="$oldifs" 370116100Smtm 371116100Smtm return $_ret 372116029Smtm} 373116029Smtm 374137070Spjd# ifnet_rename 375137070Spjd# Rename all requested interfaces. 376116029Smtm# 377137070Spjdifnet_rename() 378137070Spjd{ 379137070Spjd 380138386Srse _ifn_list="`ifconfig -l`" 381137070Spjd [ -z "$_ifn_list" ] && return 0 382137070Spjd for _if in ${_ifn_list} ; do 383137070Spjd eval _ifname=\$ifconfig_${_if}_name 384137070Spjd if [ ! -z "$_ifname" ]; then 385137070Spjd ifconfig $_if name $_ifname 386137070Spjd fi 387137070Spjd done 388137070Spjd return 0 389137070Spjd} 390137070Spjd 391137070Spjd# 392113674Smtm# list_net_interfaces type 393113674Smtm# List all network interfaces. The type of interface returned 394113674Smtm# can be controlled by the type argument. The type 395113674Smtm# argument can be any of the following: 396113674Smtm# nodhcp - all interfaces, excluding DHCP configured interfaces 397113674Smtm# dhcp - list only DHCP configured interfaces 398113674Smtm# If no argument is specified all network interfaces are output. 399134429Syar# Note that the list will include cloned interfaces if applicable. 400134429Syar# Cloned interfaces must already exist to have a chance to appear 401134429Syar# in the list if ${network_interfaces} is set to `auto'. 402113674Smtm# 403113674Smtmlist_net_interfaces() 404113674Smtm{ 405113674Smtm type=$1 40665532Snectar 407149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 40851231Ssheldonh # 40951231Ssheldonh case ${network_interfaces} in 41051231Ssheldonh [Aa][Uu][Tt][Oo]) 411149401Sbrooks _prefix='' 412149401Sbrooks _autolist="`ifconfig -l`" 413149726Sbrooks _lo= 414149401Sbrooks for _if in ${_autolist} ; do 415149401Sbrooks if autoif $_if; then 416149726Sbrooks if [ "$_if" = "lo0" ]; then 417149726Sbrooks _lo="lo0 " 418149726Sbrooks else 419149726Sbrooks _tmplist="${_tmplist}${_prefix}${_if}" 420149726Sbrooks [ -z "$_prefix" ] && _prefix=' ' 421149726Sbrooks fi 422149401Sbrooks fi 423149401Sbrooks done 424149726Sbrooks _tmplist="${_lo}${_tmplist}" 42551231Ssheldonh ;; 42683677Sbrooks *) 427149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 42883677Sbrooks ;; 42951231Ssheldonh esac 43049122Sbrian 431113674Smtm if [ -z "$type" ]; then 432113674Smtm echo $_tmplist 433113674Smtm return 0 434113674Smtm fi 43549122Sbrian 436138385Srse # Separate out dhcp and non-dhcp interfaces 437113674Smtm # 438113674Smtm _aprefix= 439134376Syar _bprefix= 440113674Smtm for _if in ${_tmplist} ; do 441147684Sbrooks if dhcpif $_if; then 442113674Smtm _dhcplist="${_dhcplist}${_aprefix}${_if}" 443113674Smtm [ -z "$_aprefix" ] && _aprefix=' ' 444147684Sbrooks elif [ -n "`_ifconfig_getargs $if`" ]; then 445113674Smtm _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 446113674Smtm [ -z "$_bprefix" ] && _bprefix=' ' 447147684Sbrooks fi 44854458Sobrien done 44951231Ssheldonh 450113674Smtm case "$type" in 451113674Smtm nodhcp) 452113674Smtm echo $_nodhcplist 453113674Smtm ;; 454113674Smtm dhcp) 455113674Smtm echo $_dhcplist 456113674Smtm ;; 457113674Smtm esac 458130151Sschweikh return 0 45925184Sjkh} 460114942Sume 461114942Sumehexdigit() 462114942Sume{ 463114942Sume if [ $1 -lt 10 ]; then 464114942Sume echo $1 465114942Sume else 466114942Sume case $1 in 467114942Sume 10) echo a ;; 468114942Sume 11) echo b ;; 469114942Sume 12) echo c ;; 470114942Sume 13) echo d ;; 471114942Sume 14) echo e ;; 472114942Sume 15) echo f ;; 473114942Sume esac 474114942Sume fi 475114942Sume} 476114942Sume 477114942Sumehexprint() 478114942Sume{ 479114942Sume val=$1 480114942Sume str='' 481114942Sume 482114942Sume dig=`hexdigit $((${val} & 15))` 483114942Sume str=${dig}${str} 484114942Sume val=$((${val} >> 4)) 485114942Sume while [ ${val} -gt 0 ]; do 486114942Sume dig=`hexdigit $((${val} & 15))` 487114942Sume str=${dig}${str} 488114942Sume val=$((${val} >> 4)) 489114942Sume done 490114942Sume 491114942Sume echo ${str} 492114942Sume} 493114942Sume 494114942Sume# Setup the interfaces for IPv6 495114942Sumenetwork6_interface_setup() 496114942Sume{ 497114942Sume interfaces=$* 498114942Sume rtsol_interfaces='' 499114942Sume case ${ipv6_gateway_enable} in 500114942Sume [Yy][Ee][Ss]) 501114942Sume rtsol_available=no 502114942Sume ;; 503114942Sume *) 504114942Sume rtsol_available=yes 505114942Sume ;; 506114942Sume esac 507114942Sume for i in $interfaces; do 508114942Sume rtsol_interface=yes 509114942Sume eval prefix=\$ipv6_prefix_$i 510114942Sume if [ -n "${prefix}" ]; then 511114942Sume rtsol_available=no 512114942Sume rtsol_interface=no 513114942Sume laddr=`network6_getladdr $i` 514114942Sume hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'` 515114942Sume for j in ${prefix}; do 516114942Sume address=$j\:${hostid} 517114942Sume ifconfig $i inet6 ${address} prefixlen 64 alias 518114942Sume 519114942Sume case ${ipv6_gateway_enable} in 520114942Sume [Yy][Ee][Ss]) 521114942Sume # subnet-router anycast address 522114942Sume # (rfc2373) 523114942Sume ifconfig $i inet6 $j:: prefixlen 64 \ 524114942Sume alias anycast 525114942Sume ;; 526114942Sume esac 527114942Sume done 528114942Sume fi 529114942Sume eval ipv6_ifconfig=\$ipv6_ifconfig_$i 530114942Sume if [ -n "${ipv6_ifconfig}" ]; then 531114942Sume rtsol_available=no 532114942Sume rtsol_interface=no 533114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 534114942Sume fi 535114942Sume 536114942Sume if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ] 537114942Sume then 538114942Sume case ${i} in 539114942Sume lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*) 540114942Sume ;; 541114942Sume *) 542114942Sume rtsol_interfaces="${rtsol_interfaces} ${i}" 543114942Sume ;; 544114942Sume esac 545114942Sume else 546114942Sume ifconfig $i inet6 547114942Sume fi 548114942Sume done 549114942Sume 550114942Sume if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then 551114942Sume # Act as endhost - automatically configured. 552114942Sume # You can configure only single interface, as 553114942Sume # specification assumes that autoconfigured host has 554114942Sume # single interface only. 555114942Sume sysctl net.inet6.ip6.accept_rtadv=1 556114942Sume set ${rtsol_interfaces} 557114942Sume ifconfig $1 up 558118666Sume rtsol ${rtsol_flags} $1 559114942Sume fi 560114942Sume 561114942Sume for i in $interfaces; do 562114942Sume alias=0 563114942Sume while : ; do 564114942Sume eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias} 565114942Sume if [ -z "${ipv6_ifconfig}" ]; then 566114942Sume break; 567114942Sume fi 568114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 569114942Sume alias=$((${alias} + 1)) 570114942Sume done 571114942Sume done 572114942Sume} 573114942Sume 574114942Sume# Setup IPv6 to IPv4 mapping 575114942Sumenetwork6_stf_setup() 576114942Sume{ 577114942Sume case ${stf_interface_ipv4addr} in 578114942Sume [Nn][Oo] | '') 579114942Sume ;; 580114942Sume *) 581114942Sume # assign IPv6 addr and interface route for 6to4 interface 582114942Sume stf_prefixlen=$((16+${stf_interface_ipv4plen:-0})) 583114942Sume OIFS="$IFS" 584114942Sume IFS=".$IFS" 585114942Sume set ${stf_interface_ipv4addr} 586114942Sume IFS="$OIFS" 587114942Sume hexfrag1=`hexprint $(($1*256 + $2))` 588114942Sume hexfrag2=`hexprint $(($3*256 + $4))` 589114942Sume ipv4_in_hexformat="${hexfrag1}:${hexfrag2}" 590114942Sume case ${stf_interface_ipv6_ifid} in 591114942Sume [Aa][Uu][Tt][Oo] | '') 592114942Sume for i in ${ipv6_network_interfaces}; do 593114942Sume laddr=`network6_getladdr ${i}` 594114942Sume case ${laddr} in 595114942Sume '') 596114942Sume ;; 597114942Sume *) 598114942Sume break 599114942Sume ;; 600114942Sume esac 601114942Sume done 602114942Sume stf_interface_ipv6_ifid=`expr "${laddr}" : \ 603114942Sume 'fe80::\(.*\)%\(.*\)'` 604114942Sume case ${stf_interface_ipv6_ifid} in 605114942Sume '') 606114942Sume stf_interface_ipv6_ifid=0:0:0:1 607114942Sume ;; 608114942Sume esac 609114942Sume ;; 610114942Sume esac 611114942Sume ifconfig stf0 create >/dev/null 2>&1 612114942Sume ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \ 613114942Sume prefixlen ${stf_prefixlen} 614114942Sume # disallow packets to malicious 6to4 prefix 615114942Sume route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 616114942Sume route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 617114942Sume route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 618114942Sume route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 619114942Sume ;; 620114942Sume esac 621114942Sume} 622114942Sume 623114942Sume# Setup static routes 624114942Sumenetwork6_static_routes_setup() 625114942Sume{ 626114942Sume # Set up any static routes. 627114942Sume case ${ipv6_defaultrouter} in 628114942Sume [Nn][Oo] | '') 629114942Sume ;; 630114942Sume *) 631114942Sume ipv6_static_routes="default ${ipv6_static_routes}" 632114942Sume ipv6_route_default="default ${ipv6_defaultrouter}" 633114942Sume ;; 634114942Sume esac 635114942Sume case ${ipv6_static_routes} in 636114942Sume [Nn][Oo] | '') 637114942Sume ;; 638114942Sume *) 639114942Sume for i in ${ipv6_static_routes}; do 640114942Sume eval ipv6_route_args=\$ipv6_route_${i} 641114942Sume route add -inet6 ${ipv6_route_args} 642114942Sume done 643114942Sume ;; 644114942Sume esac 645114942Sume} 646114942Sume 647114942Sume# Setup faith 648114942Sumenetwork6_faith_setup() 649114942Sume{ 650114942Sume case ${ipv6_faith_prefix} in 651114942Sume [Nn][Oo] | '') 652114942Sume ;; 653114942Sume *) 654114942Sume sysctl net.inet6.ip6.keepfaith=1 655114942Sume ifconfig faith0 create >/dev/null 2>&1 656114942Sume ifconfig faith0 up 657114942Sume for prefix in ${ipv6_faith_prefix}; do 658114942Sume prefixlen=`expr "${prefix}" : ".*/\(.*\)"` 659114942Sume case ${prefixlen} in 660114942Sume '') 661114942Sume prefixlen=96 662114942Sume ;; 663114942Sume *) 664114942Sume prefix=`expr "${prefix}" : \ 665114942Sume "\(.*\)/${prefixlen}"` 666114942Sume ;; 667114942Sume esac 668114942Sume route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1 669114942Sume route change -inet6 ${prefix} -prefixlen ${prefixlen} \ 670114942Sume -ifp faith0 671114942Sume done 672114942Sume ;; 673114942Sume esac 674114942Sume} 675114942Sume 676114942Sume# Install the "default interface" to kernel, which will be used 677114942Sume# as the default route when there's no router. 678114942Sumenetwork6_default_interface_setup() 679114942Sume{ 680114942Sume # Choose IPv6 default interface if it is not clearly specified. 681114942Sume case ${ipv6_default_interface} in 682114942Sume '') 683114942Sume for i in ${ipv6_network_interfaces}; do 684114942Sume case $i in 685114942Sume lo0|faith[0-9]*) 686114942Sume continue 687114942Sume ;; 688114942Sume esac 689114942Sume laddr=`network6_getladdr $i exclude_tentative` 690114942Sume case ${laddr} in 691114942Sume '') 692114942Sume ;; 693114942Sume *) 694114942Sume ipv6_default_interface=$i 695114942Sume break 696114942Sume ;; 697114942Sume esac 698114942Sume done 699114942Sume ;; 700114942Sume esac 701114942Sume 702114942Sume # Disallow unicast packets without outgoing scope identifiers, 703114942Sume # or route such packets to a "default" interface, if it is specified. 704114942Sume route add -inet6 fe80:: -prefixlen 10 ::1 -reject 705114942Sume case ${ipv6_default_interface} in 706114942Sume [Nn][Oo] | '') 707114942Sume route add -inet6 ff02:: -prefixlen 16 ::1 -reject 708114942Sume ;; 709114942Sume *) 710114942Sume laddr=`network6_getladdr ${ipv6_default_interface}` 711114942Sume route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \ 712114942Sume -cloning 713114942Sume 714114942Sume # Disable installing the default interface with the 715114942Sume # case net.inet6.ip6.forwarding=0 and 716114942Sume # net.inet6.ip6.accept_rtadv=0, due to avoid conflict 717114942Sume # between the default router list and the manual 718114942Sume # configured default route. 719114942Sume case ${ipv6_gateway_enable} in 720114942Sume [Yy][Ee][Ss]) 721114942Sume ;; 722114942Sume *) 723114942Sume if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ] 724114942Sume then 725114942Sume ndp -I ${ipv6_default_interface} 726114942Sume fi 727114942Sume ;; 728114942Sume esac 729114942Sume ;; 730114942Sume esac 731114942Sume} 732114942Sume 733114942Sumenetwork6_getladdr() 734114942Sume{ 735114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 736114942Sume case ${proto} in 737114942Sume inet6) 738114942Sume case ${addr} in 739114942Sume fe80::*) 740114942Sume if [ -z "$2" ]; then 741114942Sume echo ${addr} 742114942Sume return 743114942Sume fi 744114942Sume case ${rest} in 745114942Sume *tentative*) 746114942Sume continue 747114942Sume ;; 748114942Sume *) 749114942Sume echo ${addr} 750114942Sume return 751114942Sume esac 752114942Sume esac 753114942Sume esac 754114942Sume done 755114942Sume} 756