network.subr revision 161386
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 161386 2006-08-17 03:03:38Z 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 47157706Sbrooks 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 63157706Sbrooks if syncdhcpif $1; then 64157706Sbrooks /etc/rc.d/dhclient start $1 65157706Sbrooks fi 66147088Sbrooks _cfg=0 67147088Sbrooks fi 68147088Sbrooks 69147121Sbrooks return $_cfg 70113674Smtm} 7125184Sjkh 72116029Smtm# ifconfig_down if 73161386Sbrooks# returns 1 if wpa_supplicant or dhclient was stopped or 74161386Sbrooks# the interface exists. 75116029Smtm# 76116029Smtmifconfig_down() 77116029Smtm{ 78116029Smtm [ -z "$1" ] && return 1 79147121Sbrooks _cfg=1 80116029Smtm 81147088Sbrooks if wpaif $1; then 82147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 83147121Sbrooks _cfg=0 84147088Sbrooks fi 85147088Sbrooks 86147088Sbrooks if dhcpif $1; then 87147088Sbrooks /etc/rc.d/dhclient stop $1 88147088Sbrooks _cfg=0 89147088Sbrooks fi 90147088Sbrooks 91161386Sbrooks if ifexists $1; then 92161386Sbrooks ifconfig $1 down 93161386Sbrooks _cfg=0 94161386Sbrooks fi 95157706Sbrooks 96147121Sbrooks return $_cfg 97116029Smtm} 98116029Smtm 99157706Sbrooks# get_if_var if var [default] 100157706Sbrooks# Return the value of the pseudo-hash corresponding to $if where 101157706Sbrooks# $var is a string containg the sub-string "IF" which will be 102157706Sbrooks# replaced with $if after the characters defined in _punct are 103157706Sbrooks# replaced with '_'. If the variable is unset, replace it with 104157706Sbrooks# $default if given. 105157706Sbrooksget_if_var() 106157706Sbrooks{ 107157706Sbrooks if [ $# -ne 2 -a $# -ne 3 ]; then 108157706Sbrooks err 3 'USAGE: get_if_var name var [default]' 109157706Sbrooks fi 110157706Sbrooks 111157706Sbrooks _if=$1 112157706Sbrooks _punct=". - / +" 113157736Sbrooks for _punct_c in $_punct; do 114157706Sbrooks _if=`ltr ${_if} ${_punct_c} '_'` 115157706Sbrooks done 116157706Sbrooks _var=$2 117157706Sbrooks _default=$3 118157706Sbrooks 119157706Sbrooks prefix=${_var%%IF*} 120157706Sbrooks suffix=${_var##*IF} 121157706Sbrooks eval echo \${${prefix}${_if}${suffix}-${_default}} 122157706Sbrooks} 123157706Sbrooks 124147088Sbrooks# _ifconfig_getargs if 125147088Sbrooks# Echos the arguments for the supplied interface to stdout. 126147088Sbrooks# returns 1 if empty. In general, ifconfig_getargs should be used 127147088Sbrooks# outside this file. 128147088Sbrooks_ifconfig_getargs() 129147088Sbrooks{ 130147088Sbrooks _ifn=$1 131147088Sbrooks if [ -z "$_ifn" ]; then 132147088Sbrooks return 1 133147088Sbrooks fi 134147088Sbrooks 135157706Sbrooks get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT" 136147088Sbrooks} 137147088Sbrooks 138147088Sbrooks# ifconfig_getargs if 139147088Sbrooks# Takes the result from _ifconfig_getargs and removes pseudo 140147088Sbrooks# args such as DHCP and WPA. 141147088Sbrooksifconfig_getargs() 142147088Sbrooks{ 143147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 144147088Sbrooks if [ $? -eq 1 ]; then 145147088Sbrooks return 1 146147088Sbrooks fi 147147088Sbrooks _args= 148147088Sbrooks 149147088Sbrooks for _arg in $_tmpargs; do 150147088Sbrooks case $_arg in 151157706Sbrooks [Dd][Hh][Cc][Pp]) ;; 152157706Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) ;; 153157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 154157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 155157706Sbrooks [Ww][Pp][Aa]) ;; 156147088Sbrooks *) 157147088Sbrooks _args="$_args $_arg" 158147088Sbrooks ;; 159147088Sbrooks esac 160147088Sbrooks done 161147088Sbrooks 162147088Sbrooks echo $_args 163147088Sbrooks} 164147088Sbrooks 165149401Sbrooks# autoif 166149401Sbrooks# Returns 0 if the interface should be automaticly configured at 167149401Sbrooks# boot time and 1 otherwise. 168149401Sbrooksautoif() 169149401Sbrooks{ 170149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 171149401Sbrooks for _arg in $_tmpargs; do 172149401Sbrooks case $_arg in 173149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 174149401Sbrooks return 1 175149401Sbrooks ;; 176149401Sbrooks esac 177149401Sbrooks done 178149401Sbrooks return 0 179149401Sbrooks} 180149401Sbrooks 181147088Sbrooks# dhcpif if 182147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 183147088Sbrooksdhcpif() 184147088Sbrooks{ 185147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 186147088Sbrooks for _arg in $_tmpargs; do 187147088Sbrooks case $_arg in 188147088Sbrooks [Dd][Hh][Cc][Pp]) 189147088Sbrooks return 0 190147088Sbrooks ;; 191157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 192157706Sbrooks return 0 193157706Sbrooks ;; 194157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 195157706Sbrooks return 0 196157706Sbrooks ;; 197147088Sbrooks esac 198147088Sbrooks done 199147088Sbrooks return 1 200147088Sbrooks} 201147088Sbrooks 202157706Sbrooks# syncdhcpif 203157706Sbrooks# Returns 0 if the interface should be configured synchronously and 204157706Sbrooks# 1 otherwise. 205157706Sbrookssyncdhcpif() 206157706Sbrooks{ 207157706Sbrooks _tmpargs=`_ifconfig_getargs $1` 208157706Sbrooks for _arg in $_tmpargs; do 209157706Sbrooks case $_arg in 210157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 211157706Sbrooks return 1 212157706Sbrooks ;; 213157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 214157706Sbrooks return 0 215157706Sbrooks ;; 216157706Sbrooks esac 217157706Sbrooks done 218157737Sbrooks if checkyesno synchronous_dhclient; then 219157706Sbrooks return 0 220157706Sbrooks else 221157706Sbrooks return 1 222157706Sbrooks fi 223157706Sbrooks} 224157706Sbrooks 225147088Sbrooks# wpaif if 226147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 227147088Sbrookswpaif() 228147088Sbrooks{ 229147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 230147088Sbrooks for _arg in $_tmpargs; do 231147088Sbrooks case $_arg in 232147088Sbrooks [Ww][Pp][Aa]) 233147088Sbrooks return 0 234147088Sbrooks ;; 235147088Sbrooks esac 236147088Sbrooks done 237147088Sbrooks return 1 238147088Sbrooks} 239147088Sbrooks 240161386Sbrooks# ifexists if 241161386Sbrooks# Returns 0 if the interface exists and 1 otherwise. 242161386Sbrooksifexists() 243161386Sbrooks{ 244161386Sbrooks ifconfig $1 > /dev/null 2>&1 245161386Sbrooks} 246161386Sbrooks 247152441Sbrooks# ipv4_up if 248152441Sbrooks# add IPv4 addresses to the interface $if 249152441Sbrooksipv4_up() 250152441Sbrooks{ 251152441Sbrooks _if=$1 252152441Sbrooks ifalias_up ${_if} 253152441Sbrooks ipv4_addrs_common ${_if} alias 254152441Sbrooks} 255152441Sbrooks 256152441Sbrooks# ipv4_down if 257152441Sbrooks# remove IPv4 addresses from the interface $if 258152441Sbrooksipv4_down() 259152441Sbrooks{ 260152441Sbrooks _if=$1 261161386Sbrooks _ifs="^" 262161386Sbrooks _ret=1 263161386Sbrooks 264161386Sbrooks ifexists ${_if} || return 1 265161386Sbrooks 266161386Sbrooks inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`" 267161386Sbrooks 268161386Sbrooks oldifs="$IFS" 269161386Sbrooks IFS="$_ifs" 270161386Sbrooks for _inet in $inetList ; do 271161386Sbrooks # get rid of extraneous line 272161386Sbrooks [ -z "$_inet" ] && break 273161386Sbrooks 274161386Sbrooks _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 275161386Sbrooks 276161386Sbrooks IFS="$oldifs" 277161386Sbrooks ifconfig ${_if} ${_inet} delete 278161386Sbrooks IFS="$_ifs" 279161386Sbrooks _ret=0 280161386Sbrooks done 281161386Sbrooks IFS="$oldifs" 282161386Sbrooks 283161386Sbrooks ifalias_down ${_if} && _ret=0 284161386Sbrooks ipv4_addrs_common ${_if} -alias && _ret=0 285161386Sbrooks 286161386Sbrooks return $_ret 287152441Sbrooks} 288152441Sbrooks 289152441Sbrooks# ipv4_addrs_common if action 290152441Sbrooks# Evaluate the ifconfig_if_ipv4 arguments for interface $if 291152441Sbrooks# and use $action to add or remove IPv4 addresses from $if. 292152441Sbrooksipv4_addrs_common() 293152441Sbrooks{ 294152441Sbrooks _ret=1 295152441Sbrooks _if=$1 296152441Sbrooks _action=$2 297152441Sbrooks 298152441Sbrooks # get ipv4-addresses 299157706Sbrooks cidr_addr=`get_if_var $_if ipv4_addrs_IF` 300152441Sbrooks 301152441Sbrooks for _cidr in ${cidr_addr}; do 302152441Sbrooks _ipaddr=${_cidr%%/*} 303152441Sbrooks _netmask="/"${_cidr##*/} 304152441Sbrooks _range=${_ipaddr##*.} 305152441Sbrooks _ipnet=${_ipaddr%.*} 306152441Sbrooks _iplow=${_range%-*} 307152441Sbrooks _iphigh=${_range#*-} 308152441Sbrooks 309152441Sbrooks # clear netmask when removing aliases 310152441Sbrooks if [ "${_action}" = "-alias" ]; then 311152441Sbrooks _netmask="" 312152441Sbrooks fi 313152441Sbrooks 314152441Sbrooks _ipcount=${_iplow} 315152441Sbrooks while [ "${_ipcount}" -le "${_iphigh}" ]; do 316152441Sbrooks eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}" 317152441Sbrooks _ipcount=$((${_ipcount}+1)) 318152441Sbrooks _ret=0 319152441Sbrooks 320152441Sbrooks # only the first ipaddr in a subnet need the real netmask 321152441Sbrooks if [ "${_action}" != "-alias" ]; then 322152441Sbrooks _netmask="/32" 323152441Sbrooks fi 324152441Sbrooks done 325152441Sbrooks done 326152441Sbrooks return $_ret 327152441Sbrooks} 328152441Sbrooks 329113674Smtm# ifalias_up if 330113674Smtm# Configure aliases for network interface $if. 331113674Smtm# It returns 0 if at least one alias was configured or 332113674Smtm# 1 if there were none. 333113674Smtm# 334113674Smtmifalias_up() 335113674Smtm{ 336113674Smtm _ret=1 337113674Smtm alias=0 338113674Smtm while : ; do 339157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 340113674Smtm if [ -n "${ifconfig_args}" ]; then 341113674Smtm ifconfig $1 ${ifconfig_args} alias 342113674Smtm alias=$((${alias} + 1)) 343113674Smtm _ret=0 344113674Smtm else 345113674Smtm break 346113674Smtm fi 347113674Smtm done 348113674Smtm return $_ret 349113674Smtm} 350100280Sgordon 351116029Smtm#ifalias_down if 352116029Smtm# Remove aliases for network interface $if. 353116029Smtm# It returns 0 if at least one alias was removed or 354116029Smtm# 1 if there were none. 355116029Smtm# 356116029Smtmifalias_down() 357116029Smtm{ 358116029Smtm _ret=1 359116029Smtm alias=0 360116029Smtm while : ; do 361157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 362116029Smtm if [ -n "${ifconfig_args}" ]; then 363116029Smtm ifconfig $1 ${ifconfig_args} -alias 364116029Smtm alias=$((${alias} + 1)) 365116029Smtm _ret=0 366116029Smtm else 367116029Smtm break 368116029Smtm fi 369116029Smtm done 370116029Smtm return $_ret 371116029Smtm} 372116029Smtm 373113674Smtm# ifscript_up if 374113674Smtm# Evaluate a startup script for the $if interface. 375113674Smtm# It returns 0 if a script was found and processed or 376113674Smtm# 1 if no script was found. 377113674Smtm# 378113674Smtmifscript_up() 379100280Sgordon{ 380113674Smtm if [ -r /etc/start_if.$1 ]; then 381113674Smtm . /etc/start_if.$1 382113674Smtm return 0 383113674Smtm fi 384113674Smtm return 1 385100280Sgordon} 386100280Sgordon 387116029Smtm# ifscript_down if 388116029Smtm# Evaluate a shutdown script for the $if interface. 389116029Smtm# It returns 0 if a script was found and processed or 390116029Smtm# 1 if no script was found. 391116029Smtm# 392116029Smtmifscript_down() 393116029Smtm{ 394116029Smtm if [ -r /etc/stop_if.$1 ]; then 395116029Smtm . /etc/stop_if.$1 396116029Smtm return 0 397116029Smtm fi 398116029Smtm return 1 399116029Smtm} 400116029Smtm 401113674Smtm# Create cloneable interfaces. 402113674Smtm# 403113674Smtmclone_up() 404100280Sgordon{ 405113674Smtm _prefix= 406113674Smtm _list= 407113674Smtm for ifn in ${cloned_interfaces}; do 408113674Smtm ifconfig ${ifn} create 409116774Skuriyama if [ $? -eq 0 ]; then 410113674Smtm _list="${_list}${_prefix}${ifn}" 411113674Smtm [ -z "$_prefix" ] && _prefix=' ' 412113674Smtm fi 413113674Smtm done 414113674Smtm debug "Cloned: ${_list}" 415113674Smtm} 416100280Sgordon 417113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed 418113674Smtm# to standard output. 419113674Smtm# 420113674Smtmclone_down() 421113674Smtm{ 422113674Smtm _prefix= 423113674Smtm _list= 424113674Smtm for ifn in ${cloned_interfaces}; do 425113674Smtm ifconfig ${ifn} destroy 426116774Skuriyama if [ $? -eq 0 ]; then 427113674Smtm _list="${_list}${_prefix}${ifn}" 428113674Smtm [ -z "$_prefix" ] && _prefix=' ' 429113674Smtm fi 430113674Smtm done 431113674Smtm debug "Destroyed clones: ${_list}" 432100280Sgordon} 433100280Sgordon 434113674Smtmgif_up() { 435100282Sdougb case ${gif_interfaces} in 436100282Sdougb [Nn][Oo] | '') 437100282Sdougb ;; 438100282Sdougb *) 439100282Sdougb for i in ${gif_interfaces}; do 440157706Sbrooks peers=`get_if_var $i gifconfig_IF` 441100282Sdougb case ${peers} in 442100282Sdougb '') 443100282Sdougb continue 444100282Sdougb ;; 445100282Sdougb *) 446100282Sdougb ifconfig $i create >/dev/null 2>&1 447100282Sdougb ifconfig $i tunnel ${peers} 448103710Sume ifconfig $i up 449100282Sdougb ;; 450100282Sdougb esac 451100282Sdougb done 452100282Sdougb ;; 453100282Sdougb esac 454100282Sdougb} 455100282Sdougb 456113674Smtm# 457113674Smtm# ipx_up ifn 458113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 459113674Smtm# arguments were found and configured; returns 1 otherwise. 460113674Smtm# 461113674Smtmipx_up() 462100280Sgordon{ 463113674Smtm ifn="$1" 464157706Sbrooks ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx` 465113674Smtm if [ -n "${ifconfig_args}" ]; then 466113674Smtm ifconfig ${ifn} ${ifconfig_args} 467113674Smtm return 0 46885831Sdes fi 469113674Smtm return 1 470113674Smtm} 47185831Sdes 472116029Smtm# ipx_down ifn 473116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 474116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 475113674Smtm# 476116029Smtmipx_down() 477116029Smtm{ 478116100Smtm [ -z "$1" ] && return 1 479116100Smtm _ifs="^" 480116100Smtm _ret=1 481116100Smtm 482161386Sbrooks ifexists $1 || return 1 483161386Sbrooks 484116100Smtm ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`" 485116100Smtm 486116100Smtm oldifs="$IFS" 487116100Smtm IFS="$_ifs" 488116100Smtm for _ipx in $ipxList ; do 489116100Smtm # get rid of extraneous line 490116100Smtm [ -z "$_ipx" ] && break 491116100Smtm 492116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 493116100Smtm 494116100Smtm IFS="$oldifs" 495116100Smtm ifconfig $1 ${_ipx} delete 496116100Smtm IFS="$_ifs" 497116100Smtm _ret=0 498116100Smtm done 499116100Smtm IFS="$oldifs" 500116100Smtm 501116100Smtm return $_ret 502116029Smtm} 503116029Smtm 504137070Spjd# ifnet_rename 505137070Spjd# Rename all requested interfaces. 506116029Smtm# 507137070Spjdifnet_rename() 508137070Spjd{ 509137070Spjd 510138386Srse _ifn_list="`ifconfig -l`" 511137070Spjd [ -z "$_ifn_list" ] && return 0 512137070Spjd for _if in ${_ifn_list} ; do 513157706Sbrooks _ifname=`get_if_var $_if ifconfig_IF_name` 514137070Spjd if [ ! -z "$_ifname" ]; then 515137070Spjd ifconfig $_if name $_ifname 516137070Spjd fi 517137070Spjd done 518137070Spjd return 0 519137070Spjd} 520137070Spjd 521137070Spjd# 522113674Smtm# list_net_interfaces type 523113674Smtm# List all network interfaces. The type of interface returned 524113674Smtm# can be controlled by the type argument. The type 525113674Smtm# argument can be any of the following: 526113674Smtm# nodhcp - all interfaces, excluding DHCP configured interfaces 527113674Smtm# dhcp - list only DHCP configured interfaces 528113674Smtm# If no argument is specified all network interfaces are output. 529134429Syar# Note that the list will include cloned interfaces if applicable. 530134429Syar# Cloned interfaces must already exist to have a chance to appear 531134429Syar# in the list if ${network_interfaces} is set to `auto'. 532113674Smtm# 533113674Smtmlist_net_interfaces() 534113674Smtm{ 535113674Smtm type=$1 53665532Snectar 537149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 53851231Ssheldonh # 53951231Ssheldonh case ${network_interfaces} in 54051231Ssheldonh [Aa][Uu][Tt][Oo]) 541149401Sbrooks _prefix='' 542149401Sbrooks _autolist="`ifconfig -l`" 543149726Sbrooks _lo= 544149401Sbrooks for _if in ${_autolist} ; do 545149401Sbrooks if autoif $_if; then 546149726Sbrooks if [ "$_if" = "lo0" ]; then 547149726Sbrooks _lo="lo0 " 548149726Sbrooks else 549149726Sbrooks _tmplist="${_tmplist}${_prefix}${_if}" 550149726Sbrooks [ -z "$_prefix" ] && _prefix=' ' 551149726Sbrooks fi 552149401Sbrooks fi 553149401Sbrooks done 554149726Sbrooks _tmplist="${_lo}${_tmplist}" 55551231Ssheldonh ;; 55683677Sbrooks *) 557149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 55883677Sbrooks ;; 55951231Ssheldonh esac 56049122Sbrian 561113674Smtm if [ -z "$type" ]; then 562113674Smtm echo $_tmplist 563113674Smtm return 0 564113674Smtm fi 56549122Sbrian 566138385Srse # Separate out dhcp and non-dhcp interfaces 567113674Smtm # 568113674Smtm _aprefix= 569134376Syar _bprefix= 570113674Smtm for _if in ${_tmplist} ; do 571147684Sbrooks if dhcpif $_if; then 572113674Smtm _dhcplist="${_dhcplist}${_aprefix}${_if}" 573113674Smtm [ -z "$_aprefix" ] && _aprefix=' ' 574157706Sbrooks elif [ -n "`_ifconfig_getargs $_if`" ]; then 575113674Smtm _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 576113674Smtm [ -z "$_bprefix" ] && _bprefix=' ' 577147684Sbrooks fi 57854458Sobrien done 57951231Ssheldonh 580113674Smtm case "$type" in 581113674Smtm nodhcp) 582113674Smtm echo $_nodhcplist 583113674Smtm ;; 584113674Smtm dhcp) 585113674Smtm echo $_dhcplist 586113674Smtm ;; 587113674Smtm esac 588130151Sschweikh return 0 58925184Sjkh} 590114942Sume 591114942Sumehexdigit() 592114942Sume{ 593114942Sume if [ $1 -lt 10 ]; then 594114942Sume echo $1 595114942Sume else 596114942Sume case $1 in 597114942Sume 10) echo a ;; 598114942Sume 11) echo b ;; 599114942Sume 12) echo c ;; 600114942Sume 13) echo d ;; 601114942Sume 14) echo e ;; 602114942Sume 15) echo f ;; 603114942Sume esac 604114942Sume fi 605114942Sume} 606114942Sume 607114942Sumehexprint() 608114942Sume{ 609114942Sume val=$1 610114942Sume str='' 611114942Sume 612114942Sume dig=`hexdigit $((${val} & 15))` 613114942Sume str=${dig}${str} 614114942Sume val=$((${val} >> 4)) 615114942Sume while [ ${val} -gt 0 ]; do 616114942Sume dig=`hexdigit $((${val} & 15))` 617114942Sume str=${dig}${str} 618114942Sume val=$((${val} >> 4)) 619114942Sume done 620114942Sume 621114942Sume echo ${str} 622114942Sume} 623114942Sume 624114942Sume# Setup the interfaces for IPv6 625114942Sumenetwork6_interface_setup() 626114942Sume{ 627114942Sume interfaces=$* 628114942Sume rtsol_interfaces='' 629114942Sume case ${ipv6_gateway_enable} in 630114942Sume [Yy][Ee][Ss]) 631114942Sume rtsol_available=no 632114942Sume ;; 633114942Sume *) 634114942Sume rtsol_available=yes 635114942Sume ;; 636114942Sume esac 637114942Sume for i in $interfaces; do 638114942Sume rtsol_interface=yes 639157706Sbrooks prefix=`get_if_var $i ipv6_prefix_IF` 640114942Sume if [ -n "${prefix}" ]; then 641114942Sume rtsol_available=no 642114942Sume rtsol_interface=no 643114942Sume laddr=`network6_getladdr $i` 644114942Sume hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'` 645114942Sume for j in ${prefix}; do 646114942Sume address=$j\:${hostid} 647114942Sume ifconfig $i inet6 ${address} prefixlen 64 alias 648114942Sume 649114942Sume case ${ipv6_gateway_enable} in 650114942Sume [Yy][Ee][Ss]) 651114942Sume # subnet-router anycast address 652114942Sume # (rfc2373) 653114942Sume ifconfig $i inet6 $j:: prefixlen 64 \ 654114942Sume alias anycast 655114942Sume ;; 656114942Sume esac 657114942Sume done 658114942Sume fi 659157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF` 660114942Sume if [ -n "${ipv6_ifconfig}" ]; then 661114942Sume rtsol_available=no 662114942Sume rtsol_interface=no 663114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 664114942Sume fi 665114942Sume 666114942Sume if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ] 667114942Sume then 668114942Sume case ${i} in 669114942Sume lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*) 670114942Sume ;; 671114942Sume *) 672114942Sume rtsol_interfaces="${rtsol_interfaces} ${i}" 673114942Sume ;; 674114942Sume esac 675114942Sume else 676114942Sume ifconfig $i inet6 677114942Sume fi 678114942Sume done 679114942Sume 680114942Sume if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then 681114942Sume # Act as endhost - automatically configured. 682114942Sume # You can configure only single interface, as 683114942Sume # specification assumes that autoconfigured host has 684114942Sume # single interface only. 685114942Sume sysctl net.inet6.ip6.accept_rtadv=1 686114942Sume set ${rtsol_interfaces} 687114942Sume ifconfig $1 up 688118666Sume rtsol ${rtsol_flags} $1 689114942Sume fi 690114942Sume 691114942Sume for i in $interfaces; do 692114942Sume alias=0 693114942Sume while : ; do 694157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}` 695114942Sume if [ -z "${ipv6_ifconfig}" ]; then 696114942Sume break; 697114942Sume fi 698114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 699114942Sume alias=$((${alias} + 1)) 700114942Sume done 701114942Sume done 702114942Sume} 703114942Sume 704114942Sume# Setup IPv6 to IPv4 mapping 705114942Sumenetwork6_stf_setup() 706114942Sume{ 707114942Sume case ${stf_interface_ipv4addr} in 708114942Sume [Nn][Oo] | '') 709114942Sume ;; 710114942Sume *) 711114942Sume # assign IPv6 addr and interface route for 6to4 interface 712114942Sume stf_prefixlen=$((16+${stf_interface_ipv4plen:-0})) 713114942Sume OIFS="$IFS" 714114942Sume IFS=".$IFS" 715114942Sume set ${stf_interface_ipv4addr} 716114942Sume IFS="$OIFS" 717114942Sume hexfrag1=`hexprint $(($1*256 + $2))` 718114942Sume hexfrag2=`hexprint $(($3*256 + $4))` 719114942Sume ipv4_in_hexformat="${hexfrag1}:${hexfrag2}" 720114942Sume case ${stf_interface_ipv6_ifid} in 721114942Sume [Aa][Uu][Tt][Oo] | '') 722114942Sume for i in ${ipv6_network_interfaces}; do 723114942Sume laddr=`network6_getladdr ${i}` 724114942Sume case ${laddr} in 725114942Sume '') 726114942Sume ;; 727114942Sume *) 728114942Sume break 729114942Sume ;; 730114942Sume esac 731114942Sume done 732114942Sume stf_interface_ipv6_ifid=`expr "${laddr}" : \ 733114942Sume 'fe80::\(.*\)%\(.*\)'` 734114942Sume case ${stf_interface_ipv6_ifid} in 735114942Sume '') 736114942Sume stf_interface_ipv6_ifid=0:0:0:1 737114942Sume ;; 738114942Sume esac 739114942Sume ;; 740114942Sume esac 741114942Sume ifconfig stf0 create >/dev/null 2>&1 742114942Sume ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \ 743114942Sume prefixlen ${stf_prefixlen} 744114942Sume # disallow packets to malicious 6to4 prefix 745114942Sume route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 746114942Sume route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 747114942Sume route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 748114942Sume route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 749114942Sume ;; 750114942Sume esac 751114942Sume} 752114942Sume 753114942Sume# Setup static routes 754114942Sumenetwork6_static_routes_setup() 755114942Sume{ 756114942Sume # Set up any static routes. 757114942Sume case ${ipv6_defaultrouter} in 758114942Sume [Nn][Oo] | '') 759114942Sume ;; 760114942Sume *) 761114942Sume ipv6_static_routes="default ${ipv6_static_routes}" 762114942Sume ipv6_route_default="default ${ipv6_defaultrouter}" 763114942Sume ;; 764114942Sume esac 765114942Sume case ${ipv6_static_routes} in 766114942Sume [Nn][Oo] | '') 767114942Sume ;; 768114942Sume *) 769114942Sume for i in ${ipv6_static_routes}; do 770157706Sbrooks ipv6_route_args=`get_if_var $i ipv6_route_IF` 771114942Sume route add -inet6 ${ipv6_route_args} 772114942Sume done 773114942Sume ;; 774114942Sume esac 775114942Sume} 776114942Sume 777114942Sume# Setup faith 778114942Sumenetwork6_faith_setup() 779114942Sume{ 780114942Sume case ${ipv6_faith_prefix} in 781114942Sume [Nn][Oo] | '') 782114942Sume ;; 783114942Sume *) 784114942Sume sysctl net.inet6.ip6.keepfaith=1 785114942Sume ifconfig faith0 create >/dev/null 2>&1 786114942Sume ifconfig faith0 up 787114942Sume for prefix in ${ipv6_faith_prefix}; do 788114942Sume prefixlen=`expr "${prefix}" : ".*/\(.*\)"` 789114942Sume case ${prefixlen} in 790114942Sume '') 791114942Sume prefixlen=96 792114942Sume ;; 793114942Sume *) 794114942Sume prefix=`expr "${prefix}" : \ 795114942Sume "\(.*\)/${prefixlen}"` 796114942Sume ;; 797114942Sume esac 798114942Sume route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1 799114942Sume route change -inet6 ${prefix} -prefixlen ${prefixlen} \ 800114942Sume -ifp faith0 801114942Sume done 802114942Sume ;; 803114942Sume esac 804114942Sume} 805114942Sume 806114942Sume# Install the "default interface" to kernel, which will be used 807114942Sume# as the default route when there's no router. 808114942Sumenetwork6_default_interface_setup() 809114942Sume{ 810114942Sume # Choose IPv6 default interface if it is not clearly specified. 811114942Sume case ${ipv6_default_interface} in 812114942Sume '') 813114942Sume for i in ${ipv6_network_interfaces}; do 814114942Sume case $i in 815114942Sume lo0|faith[0-9]*) 816114942Sume continue 817114942Sume ;; 818114942Sume esac 819114942Sume laddr=`network6_getladdr $i exclude_tentative` 820114942Sume case ${laddr} in 821114942Sume '') 822114942Sume ;; 823114942Sume *) 824114942Sume ipv6_default_interface=$i 825114942Sume break 826114942Sume ;; 827114942Sume esac 828114942Sume done 829114942Sume ;; 830114942Sume esac 831114942Sume 832114942Sume # Disallow unicast packets without outgoing scope identifiers, 833114942Sume # or route such packets to a "default" interface, if it is specified. 834114942Sume route add -inet6 fe80:: -prefixlen 10 ::1 -reject 835114942Sume case ${ipv6_default_interface} in 836114942Sume [Nn][Oo] | '') 837114942Sume route add -inet6 ff02:: -prefixlen 16 ::1 -reject 838114942Sume ;; 839114942Sume *) 840114942Sume laddr=`network6_getladdr ${ipv6_default_interface}` 841114942Sume route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \ 842114942Sume -cloning 843114942Sume 844114942Sume # Disable installing the default interface with the 845114942Sume # case net.inet6.ip6.forwarding=0 and 846114942Sume # net.inet6.ip6.accept_rtadv=0, due to avoid conflict 847114942Sume # between the default router list and the manual 848114942Sume # configured default route. 849114942Sume case ${ipv6_gateway_enable} in 850114942Sume [Yy][Ee][Ss]) 851114942Sume ;; 852114942Sume *) 853114942Sume if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ] 854114942Sume then 855114942Sume ndp -I ${ipv6_default_interface} 856114942Sume fi 857114942Sume ;; 858114942Sume esac 859114942Sume ;; 860114942Sume esac 861114942Sume} 862114942Sume 863114942Sumenetwork6_getladdr() 864114942Sume{ 865114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 866114942Sume case ${proto} in 867114942Sume inet6) 868114942Sume case ${addr} in 869114942Sume fe80::*) 870114942Sume if [ -z "$2" ]; then 871114942Sume echo ${addr} 872114942Sume return 873114942Sume fi 874114942Sume case ${rest} in 875114942Sume *tentative*) 876114942Sume continue 877114942Sume ;; 878114942Sume *) 879114942Sume echo ${addr} 880114942Sume return 881114942Sume esac 882114942Sume esac 883114942Sume esac 884114942Sume done 885114942Sume} 886