network.subr revision 189759
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 189759 2009-03-13 07:12:25Z brooks $ 2666830Sobrien# 2725184Sjkh 28113674Smtm# 29113674Smtm# Subroutines commonly used from network startup scripts. 30113674Smtm# Requires that rc.conf be loaded first. 31113674Smtm# 3225184Sjkh 33178356Ssam# ifn_start ifn 34178356Ssam# Bring up and configure an interface. If some configuration is applied 35178356Ssam# print the interface configuration. 36178356Ssam# 37178356Ssamifn_start() 38178356Ssam{ 39178356Ssam local ifn cfg 40178356Ssam ifn="$1" 41178356Ssam cfg=1 42178356Ssam 43178356Ssam [ -z "$ifn" ] && err 1 "ifn_start called without an interface" 44178356Ssam 45178356Ssam ifscript_up ${ifn} && cfg=0 46178356Ssam ifconfig_up ${ifn} && cfg=0 47178356Ssam ipv4_up ${ifn} && cfg=0 48178356Ssam ipx_up ${ifn} && cfg=0 49179001Sbrooks childif_create ${ifn} 50178356Ssam 51178356Ssam return $cfg 52178356Ssam} 53178356Ssam 54178356Ssam# ifn_start ifn 55178356Ssam# Shutdown and de-configure an interface. If action is taken print the 56178356Ssam# interface name. 57178356Ssam# 58178356Ssamifn_stop() 59178356Ssam{ 60178356Ssam local ifn cfg 61178356Ssam ifn="$1" 62178356Ssam cfg=1 63178356Ssam 64178356Ssam [ -z "$ifn" ] && return 1 65178356Ssam 66178356Ssam ipx_down ${ifn} && cfg=0 67178356Ssam ipv4_down ${ifn} && cfg=0 68178356Ssam ifconfig_down ${ifn} && cfg=0 69178356Ssam ifscript_down ${ifn} && cfg=0 70179002Sbrooks childif_destroy ${ifn} 71178356Ssam 72178356Ssam return $cfg 73178356Ssam} 74178356Ssam 75113674Smtm# ifconfig_up if 76113674Smtm# Evaluate ifconfig(8) arguments for interface $if and 77113674Smtm# run ifconfig(8) with those arguments. It returns 0 if 78113674Smtm# arguments were found and executed or 1 if the interface 79147088Sbrooks# had no arguments. Pseudo arguments DHCP and WPA are handled 80147088Sbrooks# here. 81113674Smtm# 82113674Smtmifconfig_up() 83113674Smtm{ 84147088Sbrooks _cfg=1 85147088Sbrooks 86147088Sbrooks ifconfig_args=`ifconfig_getargs $1` 87113674Smtm if [ -n "${ifconfig_args}" ]; then 88178356Ssam ifconfig $1 ${ifconfig_args} 89149726Sbrooks ifconfig $1 up 90147088Sbrooks _cfg=0 91113674Smtm fi 92147088Sbrooks 93147088Sbrooks if wpaif $1; then 94147682Sbrooks /etc/rc.d/wpa_supplicant start $1 95147088Sbrooks _cfg=0 # XXX: not sure this should count 96147088Sbrooks fi 97147088Sbrooks 98147088Sbrooks if dhcpif $1; then 99149726Sbrooks if [ $_cfg -ne 0 ] ; then 100149726Sbrooks ifconfig $1 up 101149726Sbrooks fi 102157706Sbrooks if syncdhcpif $1; then 103157706Sbrooks /etc/rc.d/dhclient start $1 104157706Sbrooks fi 105147088Sbrooks _cfg=0 106147088Sbrooks fi 107147088Sbrooks 108147121Sbrooks return $_cfg 109113674Smtm} 11025184Sjkh 111116029Smtm# ifconfig_down if 112161386Sbrooks# returns 1 if wpa_supplicant or dhclient was stopped or 113161386Sbrooks# the interface exists. 114116029Smtm# 115116029Smtmifconfig_down() 116116029Smtm{ 117116029Smtm [ -z "$1" ] && return 1 118147121Sbrooks _cfg=1 119116029Smtm 120147088Sbrooks if wpaif $1; then 121147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 122147121Sbrooks _cfg=0 123147088Sbrooks fi 124147088Sbrooks 125147088Sbrooks if dhcpif $1; then 126147088Sbrooks /etc/rc.d/dhclient stop $1 127147088Sbrooks _cfg=0 128147088Sbrooks fi 129147088Sbrooks 130161386Sbrooks if ifexists $1; then 131161386Sbrooks ifconfig $1 down 132161386Sbrooks _cfg=0 133161386Sbrooks fi 134157706Sbrooks 135147121Sbrooks return $_cfg 136116029Smtm} 137116029Smtm 138157706Sbrooks# get_if_var if var [default] 139157706Sbrooks# Return the value of the pseudo-hash corresponding to $if where 140157706Sbrooks# $var is a string containg the sub-string "IF" which will be 141157706Sbrooks# replaced with $if after the characters defined in _punct are 142157706Sbrooks# replaced with '_'. If the variable is unset, replace it with 143157706Sbrooks# $default if given. 144157706Sbrooksget_if_var() 145157706Sbrooks{ 146157706Sbrooks if [ $# -ne 2 -a $# -ne 3 ]; then 147157706Sbrooks err 3 'USAGE: get_if_var name var [default]' 148157706Sbrooks fi 149157706Sbrooks 150157706Sbrooks _if=$1 151157706Sbrooks _punct=". - / +" 152157736Sbrooks for _punct_c in $_punct; do 153157706Sbrooks _if=`ltr ${_if} ${_punct_c} '_'` 154157706Sbrooks done 155157706Sbrooks _var=$2 156157706Sbrooks _default=$3 157157706Sbrooks 158157706Sbrooks prefix=${_var%%IF*} 159157706Sbrooks suffix=${_var##*IF} 160168033Sache eval echo \${${prefix}${_if}${suffix}-${_default}} 161157706Sbrooks} 162157706Sbrooks 163147088Sbrooks# _ifconfig_getargs if 164147088Sbrooks# Echos the arguments for the supplied interface to stdout. 165147088Sbrooks# returns 1 if empty. In general, ifconfig_getargs should be used 166147088Sbrooks# outside this file. 167147088Sbrooks_ifconfig_getargs() 168147088Sbrooks{ 169147088Sbrooks _ifn=$1 170147088Sbrooks if [ -z "$_ifn" ]; then 171147088Sbrooks return 1 172147088Sbrooks fi 173147088Sbrooks 174157706Sbrooks get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT" 175147088Sbrooks} 176147088Sbrooks 177147088Sbrooks# ifconfig_getargs if 178147088Sbrooks# Takes the result from _ifconfig_getargs and removes pseudo 179147088Sbrooks# args such as DHCP and WPA. 180147088Sbrooksifconfig_getargs() 181147088Sbrooks{ 182147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 183147088Sbrooks if [ $? -eq 1 ]; then 184147088Sbrooks return 1 185147088Sbrooks fi 186147088Sbrooks _args= 187147088Sbrooks 188147088Sbrooks for _arg in $_tmpargs; do 189147088Sbrooks case $_arg in 190157706Sbrooks [Dd][Hh][Cc][Pp]) ;; 191157706Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) ;; 192157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 193157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 194157706Sbrooks [Ww][Pp][Aa]) ;; 195147088Sbrooks *) 196147088Sbrooks _args="$_args $_arg" 197147088Sbrooks ;; 198147088Sbrooks esac 199147088Sbrooks done 200147088Sbrooks 201147088Sbrooks echo $_args 202147088Sbrooks} 203147088Sbrooks 204149401Sbrooks# autoif 205149401Sbrooks# Returns 0 if the interface should be automaticly configured at 206149401Sbrooks# boot time and 1 otherwise. 207149401Sbrooksautoif() 208149401Sbrooks{ 209149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 210149401Sbrooks for _arg in $_tmpargs; do 211149401Sbrooks case $_arg in 212149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 213149401Sbrooks return 1 214149401Sbrooks ;; 215149401Sbrooks esac 216149401Sbrooks done 217149401Sbrooks return 0 218149401Sbrooks} 219149401Sbrooks 220147088Sbrooks# dhcpif if 221147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 222147088Sbrooksdhcpif() 223147088Sbrooks{ 224147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 225147088Sbrooks for _arg in $_tmpargs; do 226147088Sbrooks case $_arg in 227147088Sbrooks [Dd][Hh][Cc][Pp]) 228147088Sbrooks return 0 229147088Sbrooks ;; 230157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 231157706Sbrooks return 0 232157706Sbrooks ;; 233157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 234157706Sbrooks return 0 235157706Sbrooks ;; 236147088Sbrooks esac 237147088Sbrooks done 238147088Sbrooks return 1 239147088Sbrooks} 240147088Sbrooks 241157706Sbrooks# syncdhcpif 242157706Sbrooks# Returns 0 if the interface should be configured synchronously and 243157706Sbrooks# 1 otherwise. 244157706Sbrookssyncdhcpif() 245157706Sbrooks{ 246157706Sbrooks _tmpargs=`_ifconfig_getargs $1` 247157706Sbrooks for _arg in $_tmpargs; do 248157706Sbrooks case $_arg in 249157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 250157706Sbrooks return 1 251157706Sbrooks ;; 252157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 253157706Sbrooks return 0 254157706Sbrooks ;; 255157706Sbrooks esac 256157706Sbrooks done 257157737Sbrooks if checkyesno synchronous_dhclient; then 258157706Sbrooks return 0 259157706Sbrooks else 260157706Sbrooks return 1 261157706Sbrooks fi 262157706Sbrooks} 263157706Sbrooks 264147088Sbrooks# wpaif if 265147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 266147088Sbrookswpaif() 267147088Sbrooks{ 268147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 269147088Sbrooks for _arg in $_tmpargs; do 270147088Sbrooks case $_arg in 271147088Sbrooks [Ww][Pp][Aa]) 272147088Sbrooks return 0 273147088Sbrooks ;; 274147088Sbrooks esac 275147088Sbrooks done 276147088Sbrooks return 1 277147088Sbrooks} 278147088Sbrooks 279162490Sbrooks# ipv6if if 280162490Sbrooks# Returns 0 if the interface should be configured for IPv6 and 281162490Sbrooks# 1 otherwise. 282162490Sbrooksipv6if() 283162490Sbrooks{ 284162490Sbrooks if ! checkyesno ipv6_enable; then 285162490Sbrooks return 1 286162490Sbrooks fi 287162490Sbrooks case "${ipv6_network_interfaces}" in 288162490Sbrooks [Aa][Uu][Tt][Oo]) 289162490Sbrooks return 0 290162490Sbrooks ;; 291162490Sbrooks ''|[Nn][Oo][Nn][Ee]) 292162490Sbrooks return 1 293162490Sbrooks ;; 294162490Sbrooks esac 295162490Sbrooks for v6if in ${ipv6_network_interfaces}; do 296162490Sbrooks if [ "${v6if}" = "${1}" ]; then 297162490Sbrooks return 0 298162490Sbrooks fi 299162490Sbrooks done 300162490Sbrooks return 1 301162490Sbrooks} 302162490Sbrooks 303161386Sbrooks# ifexists if 304161386Sbrooks# Returns 0 if the interface exists and 1 otherwise. 305161386Sbrooksifexists() 306161386Sbrooks{ 307169889Sthompsa ifconfig -n $1 > /dev/null 2>&1 308161386Sbrooks} 309161386Sbrooks 310152441Sbrooks# ipv4_up if 311152441Sbrooks# add IPv4 addresses to the interface $if 312152441Sbrooksipv4_up() 313152441Sbrooks{ 314152441Sbrooks _if=$1 315152441Sbrooks ifalias_up ${_if} 316152441Sbrooks ipv4_addrs_common ${_if} alias 317152441Sbrooks} 318152441Sbrooks 319152441Sbrooks# ipv4_down if 320152441Sbrooks# remove IPv4 addresses from the interface $if 321152441Sbrooksipv4_down() 322152441Sbrooks{ 323152441Sbrooks _if=$1 324161386Sbrooks _ifs="^" 325161386Sbrooks _ret=1 326161386Sbrooks 327161386Sbrooks ifexists ${_if} || return 1 328161386Sbrooks 329161386Sbrooks inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`" 330161386Sbrooks 331161386Sbrooks oldifs="$IFS" 332161386Sbrooks IFS="$_ifs" 333161386Sbrooks for _inet in $inetList ; do 334161386Sbrooks # get rid of extraneous line 335161386Sbrooks [ -z "$_inet" ] && break 336161386Sbrooks 337161386Sbrooks _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 338161386Sbrooks 339161386Sbrooks IFS="$oldifs" 340161386Sbrooks ifconfig ${_if} ${_inet} delete 341161386Sbrooks IFS="$_ifs" 342161386Sbrooks _ret=0 343161386Sbrooks done 344161386Sbrooks IFS="$oldifs" 345161386Sbrooks 346161386Sbrooks ifalias_down ${_if} && _ret=0 347161386Sbrooks ipv4_addrs_common ${_if} -alias && _ret=0 348161386Sbrooks 349161386Sbrooks return $_ret 350152441Sbrooks} 351152441Sbrooks 352152441Sbrooks# ipv4_addrs_common if action 353152441Sbrooks# Evaluate the ifconfig_if_ipv4 arguments for interface $if 354152441Sbrooks# and use $action to add or remove IPv4 addresses from $if. 355152441Sbrooksipv4_addrs_common() 356152441Sbrooks{ 357152441Sbrooks _ret=1 358152441Sbrooks _if=$1 359152441Sbrooks _action=$2 360152441Sbrooks 361152441Sbrooks # get ipv4-addresses 362157706Sbrooks cidr_addr=`get_if_var $_if ipv4_addrs_IF` 363152441Sbrooks 364152441Sbrooks for _cidr in ${cidr_addr}; do 365152441Sbrooks _ipaddr=${_cidr%%/*} 366152441Sbrooks _netmask="/"${_cidr##*/} 367152441Sbrooks _range=${_ipaddr##*.} 368152441Sbrooks _ipnet=${_ipaddr%.*} 369152441Sbrooks _iplow=${_range%-*} 370152441Sbrooks _iphigh=${_range#*-} 371152441Sbrooks 372152441Sbrooks # clear netmask when removing aliases 373152441Sbrooks if [ "${_action}" = "-alias" ]; then 374152441Sbrooks _netmask="" 375152441Sbrooks fi 376152441Sbrooks 377152441Sbrooks _ipcount=${_iplow} 378152441Sbrooks while [ "${_ipcount}" -le "${_iphigh}" ]; do 379152441Sbrooks eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}" 380152441Sbrooks _ipcount=$((${_ipcount}+1)) 381152441Sbrooks _ret=0 382152441Sbrooks 383152441Sbrooks # only the first ipaddr in a subnet need the real netmask 384152441Sbrooks if [ "${_action}" != "-alias" ]; then 385152441Sbrooks _netmask="/32" 386152441Sbrooks fi 387152441Sbrooks done 388152441Sbrooks done 389152441Sbrooks return $_ret 390152441Sbrooks} 391152441Sbrooks 392113674Smtm# ifalias_up if 393113674Smtm# Configure aliases for network interface $if. 394113674Smtm# It returns 0 if at least one alias was configured or 395113674Smtm# 1 if there were none. 396113674Smtm# 397113674Smtmifalias_up() 398113674Smtm{ 399113674Smtm _ret=1 400113674Smtm alias=0 401113674Smtm while : ; do 402157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 403113674Smtm if [ -n "${ifconfig_args}" ]; then 404113674Smtm ifconfig $1 ${ifconfig_args} alias 405113674Smtm alias=$((${alias} + 1)) 406113674Smtm _ret=0 407113674Smtm else 408113674Smtm break 409113674Smtm fi 410113674Smtm done 411113674Smtm return $_ret 412113674Smtm} 413100280Sgordon 414116029Smtm#ifalias_down if 415116029Smtm# Remove aliases for network interface $if. 416116029Smtm# It returns 0 if at least one alias was removed or 417116029Smtm# 1 if there were none. 418116029Smtm# 419116029Smtmifalias_down() 420116029Smtm{ 421116029Smtm _ret=1 422116029Smtm alias=0 423116029Smtm while : ; do 424157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 425116029Smtm if [ -n "${ifconfig_args}" ]; then 426116029Smtm ifconfig $1 ${ifconfig_args} -alias 427116029Smtm alias=$((${alias} + 1)) 428116029Smtm _ret=0 429116029Smtm else 430116029Smtm break 431116029Smtm fi 432116029Smtm done 433116029Smtm return $_ret 434116029Smtm} 435116029Smtm 436113674Smtm# ifscript_up if 437113674Smtm# Evaluate a startup script for the $if interface. 438113674Smtm# It returns 0 if a script was found and processed or 439113674Smtm# 1 if no script was found. 440113674Smtm# 441113674Smtmifscript_up() 442100280Sgordon{ 443113674Smtm if [ -r /etc/start_if.$1 ]; then 444113674Smtm . /etc/start_if.$1 445113674Smtm return 0 446113674Smtm fi 447113674Smtm return 1 448100280Sgordon} 449100280Sgordon 450116029Smtm# ifscript_down if 451116029Smtm# Evaluate a shutdown script for the $if interface. 452116029Smtm# It returns 0 if a script was found and processed or 453116029Smtm# 1 if no script was found. 454116029Smtm# 455116029Smtmifscript_down() 456116029Smtm{ 457116029Smtm if [ -r /etc/stop_if.$1 ]; then 458116029Smtm . /etc/stop_if.$1 459116029Smtm return 0 460116029Smtm fi 461116029Smtm return 1 462116029Smtm} 463116029Smtm 464113674Smtm# Create cloneable interfaces. 465113674Smtm# 466113674Smtmclone_up() 467100280Sgordon{ 468113674Smtm _prefix= 469113674Smtm _list= 470113674Smtm for ifn in ${cloned_interfaces}; do 471178527Sbrooks ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF` 472116774Skuriyama if [ $? -eq 0 ]; then 473113674Smtm _list="${_list}${_prefix}${ifn}" 474113674Smtm [ -z "$_prefix" ] && _prefix=' ' 475113674Smtm fi 476113674Smtm done 477113674Smtm debug "Cloned: ${_list}" 478113674Smtm} 479100280Sgordon 480113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed 481113674Smtm# to standard output. 482113674Smtm# 483113674Smtmclone_down() 484113674Smtm{ 485113674Smtm _prefix= 486113674Smtm _list= 487113674Smtm for ifn in ${cloned_interfaces}; do 488113674Smtm ifconfig ${ifn} destroy 489116774Skuriyama if [ $? -eq 0 ]; then 490113674Smtm _list="${_list}${_prefix}${ifn}" 491113674Smtm [ -z "$_prefix" ] && _prefix=' ' 492113674Smtm fi 493113674Smtm done 494113674Smtm debug "Destroyed clones: ${_list}" 495100280Sgordon} 496100280Sgordon 497178356Ssam# Create and configure child interfaces. 498178356Ssam# Return 0 if child interfaces are created. 499178356Ssam# 500178356Ssamchildif_create() 501178356Ssam{ 502189759Sbrooks local cfg child child_wlans create_args debug_flags ifn i 503178356Ssam cfg=1 504178356Ssam 505178356Ssam ifn=$1 506178356Ssam 507178527Sbrooks # Create wireless interfaces 508178527Sbrooks child_wlans=`get_if_var $ifn wlans_IF` 509178527Sbrooks 510178527Sbrooks for child in ${child_wlans}; do 511183517Sbrooks create_args="wlandev $ifn `get_if_var $child create_args_IF`" 512189759Sbrooks debug_flags="`get_if_var $child wlandebug_IF`" 513189759Sbrooks 514178356Ssam if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 515178356Ssam ifconfig $child create ${create_args} && cfg=0 516189759Sbrooks if [ -n "${debug_flags}" ]; then 517189759Sbrooks wlandebug -i $child ${debug_flags} 518189759Sbrooks fi 519178356Ssam else 520178356Ssam i=`ifconfig wlan create ${create_args}` 521189759Sbrooks if [ -n "${debug_flags}" ]; then 522189759Sbrooks wlandebug -i $i ${debug_flags} 523189759Sbrooks fi 524178356Ssam ifconfig $i name $child && cfg=0 525178356Ssam fi 526188118Sthompsa if autoif $child; then 527188118Sthompsa ifn_start $child 528188118Sthompsa fi 529178356Ssam done 530178356Ssam 531179001Sbrooks return ${cfg} 532178356Ssam} 533178356Ssam 534178356Ssam# Destroy child interfaces. 535178356Ssam# 536178356Ssamchildif_destroy() 537178356Ssam{ 538178527Sbrooks local cfg child child_wlans ifn 539178356Ssam 540178527Sbrooks child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`" 541178527Sbrooks for child in ${child_wlans}; do 542178356Ssam ifconfig $child destroy && cfg=0 543178356Ssam done 544178356Ssam} 545178356Ssam 546166583Sflz# Create netgraph nodes. 547166583Sflz# 548166583Sflzng_mkpeer() { 549166583Sflz ngctl -f - 2> /dev/null <<EOF 550166583Sflzmkpeer $* 551166583Sflzmsg dummy nodeinfo 552166583SflzEOF 553166583Sflz} 554166583Sflz 555166583Sflzng_create_one() { 556166583Sflz ng_mkpeer $* | while read line; do 557166583Sflz t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 558166583Sflz if [ -n "${t}" ]; then 559166583Sflz echo ${t} 560166583Sflz return 561166583Sflz fi 562166583Sflz done 563166583Sflz} 564166583Sflz 565113674Smtmgif_up() { 566166583Sflz for i in ${gif_interfaces}; do 567166583Sflz peers=`get_if_var $i gifconfig_IF` 568166583Sflz case ${peers} in 569166583Sflz '') 570166583Sflz continue 571166583Sflz ;; 572166583Sflz *) 573177682Sbrooks if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then 574177682Sbrooks ifconfig $i create >/dev/null 2>&1 575177682Sbrooks else 576177682Sbrooks gif=`ifconfig gif create` 577177682Sbrooks ifconfig $gif name $i 578177682Sbrooks fi 579166583Sflz ifconfig $i tunnel ${peers} 580166583Sflz ifconfig $i up 581166583Sflz ;; 582166583Sflz esac 583166583Sflz done 584166583Sflz} 585166583Sflz 586166583Sflz# ng_fec_create ifn 587166583Sflz# Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC 588166583Sflz# arguments were found and configured; returns !0 otherwise. 589166583Sflzng_fec_create() { 590166583Sflz local req_iface iface bogus 591166583Sflz req_iface="$1" 592166583Sflz 593166583Sflz ngctl shutdown ${req_iface}: > /dev/null 2>&1 594166583Sflz 595166583Sflz bogus="" 596166583Sflz while true; do 597166583Sflz iface=`ng_create_one fec dummy fec` 598166583Sflz if [ -z "${iface}" ]; then 599166583Sflz exit 2 600166583Sflz fi 601166583Sflz if [ "${iface}" = "${req_iface}" ]; then 602166583Sflz break 603166583Sflz fi 604166583Sflz bogus="${bogus} ${iface}" 605166583Sflz done 606166583Sflz 607166583Sflz for iface in ${bogus}; do 608166583Sflz ngctl shutdown ${iface}: 609166583Sflz done 610166583Sflz} 611166583Sflz 612166583Sflzfec_up() { 613166583Sflz for i in ${fec_interfaces}; do 614166583Sflz ng_fec_create $i 615166583Sflz for j in `get_if_var $i fecconfig_IF`; do 616166583Sflz case ${j} in 617100282Sdougb '') 618100282Sdougb continue 619100282Sdougb ;; 620100282Sdougb *) 621166583Sflz ngctl msg ${i}: add_iface "\"${j}\"" 622100282Sdougb ;; 623100282Sdougb esac 624100282Sdougb done 625166583Sflz done 626100282Sdougb} 627100282Sdougb 628113674Smtm# 629113674Smtm# ipx_up ifn 630113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 631113674Smtm# arguments were found and configured; returns 1 otherwise. 632113674Smtm# 633113674Smtmipx_up() 634100280Sgordon{ 635113674Smtm ifn="$1" 636157706Sbrooks ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx` 637113674Smtm if [ -n "${ifconfig_args}" ]; then 638113674Smtm ifconfig ${ifn} ${ifconfig_args} 639113674Smtm return 0 64085831Sdes fi 641113674Smtm return 1 642113674Smtm} 64385831Sdes 644116029Smtm# ipx_down ifn 645116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 646116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 647113674Smtm# 648116029Smtmipx_down() 649116029Smtm{ 650116100Smtm [ -z "$1" ] && return 1 651116100Smtm _ifs="^" 652116100Smtm _ret=1 653116100Smtm 654161386Sbrooks ifexists $1 || return 1 655161386Sbrooks 656116100Smtm ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`" 657116100Smtm 658116100Smtm oldifs="$IFS" 659116100Smtm IFS="$_ifs" 660116100Smtm for _ipx in $ipxList ; do 661116100Smtm # get rid of extraneous line 662116100Smtm [ -z "$_ipx" ] && break 663116100Smtm 664116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 665116100Smtm 666116100Smtm IFS="$oldifs" 667116100Smtm ifconfig $1 ${_ipx} delete 668116100Smtm IFS="$_ifs" 669116100Smtm _ret=0 670116100Smtm done 671116100Smtm IFS="$oldifs" 672116100Smtm 673116100Smtm return $_ret 674116029Smtm} 675116029Smtm 676137070Spjd# ifnet_rename 677137070Spjd# Rename all requested interfaces. 678116029Smtm# 679137070Spjdifnet_rename() 680137070Spjd{ 681137070Spjd 682138386Srse _ifn_list="`ifconfig -l`" 683137070Spjd [ -z "$_ifn_list" ] && return 0 684137070Spjd for _if in ${_ifn_list} ; do 685157706Sbrooks _ifname=`get_if_var $_if ifconfig_IF_name` 686137070Spjd if [ ! -z "$_ifname" ]; then 687137070Spjd ifconfig $_if name $_ifname 688137070Spjd fi 689137070Spjd done 690137070Spjd return 0 691137070Spjd} 692137070Spjd 693137070Spjd# 694113674Smtm# list_net_interfaces type 695113674Smtm# List all network interfaces. The type of interface returned 696113674Smtm# can be controlled by the type argument. The type 697113674Smtm# argument can be any of the following: 698113674Smtm# nodhcp - all interfaces, excluding DHCP configured interfaces 699113674Smtm# dhcp - list only DHCP configured interfaces 700113674Smtm# If no argument is specified all network interfaces are output. 701134429Syar# Note that the list will include cloned interfaces if applicable. 702134429Syar# Cloned interfaces must already exist to have a chance to appear 703134429Syar# in the list if ${network_interfaces} is set to `auto'. 704113674Smtm# 705113674Smtmlist_net_interfaces() 706113674Smtm{ 707113674Smtm type=$1 70865532Snectar 709149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 71051231Ssheldonh # 71151231Ssheldonh case ${network_interfaces} in 71251231Ssheldonh [Aa][Uu][Tt][Oo]) 713149401Sbrooks _prefix='' 714149401Sbrooks _autolist="`ifconfig -l`" 715149726Sbrooks _lo= 716149401Sbrooks for _if in ${_autolist} ; do 717149401Sbrooks if autoif $_if; then 718149726Sbrooks if [ "$_if" = "lo0" ]; then 719149726Sbrooks _lo="lo0 " 720149726Sbrooks else 721149726Sbrooks _tmplist="${_tmplist}${_prefix}${_if}" 722149726Sbrooks [ -z "$_prefix" ] && _prefix=' ' 723149726Sbrooks fi 724149401Sbrooks fi 725149401Sbrooks done 726149726Sbrooks _tmplist="${_lo}${_tmplist}" 72751231Ssheldonh ;; 72883677Sbrooks *) 729179314Sdougb if [ -z "$type" ]; then 730178695Sbrooks warn "Values of network_interfaces other than" \ 731178695Sbrooks "AUTO are deprecated" 732178695Sbrooks fi 733149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 73483677Sbrooks ;; 73551231Ssheldonh esac 73649122Sbrian 737113674Smtm if [ -z "$type" ]; then 738113674Smtm echo $_tmplist 739113674Smtm return 0 740113674Smtm fi 74149122Sbrian 742138385Srse # Separate out dhcp and non-dhcp interfaces 743113674Smtm # 744113674Smtm _aprefix= 745134376Syar _bprefix= 746113674Smtm for _if in ${_tmplist} ; do 747147684Sbrooks if dhcpif $_if; then 748113674Smtm _dhcplist="${_dhcplist}${_aprefix}${_if}" 749113674Smtm [ -z "$_aprefix" ] && _aprefix=' ' 750157706Sbrooks elif [ -n "`_ifconfig_getargs $_if`" ]; then 751113674Smtm _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 752113674Smtm [ -z "$_bprefix" ] && _bprefix=' ' 753147684Sbrooks fi 75454458Sobrien done 75551231Ssheldonh 756113674Smtm case "$type" in 757113674Smtm nodhcp) 758113674Smtm echo $_nodhcplist 759113674Smtm ;; 760113674Smtm dhcp) 761113674Smtm echo $_dhcplist 762113674Smtm ;; 763113674Smtm esac 764130151Sschweikh return 0 76525184Sjkh} 766114942Sume 767179003Sbrooks# get_default_if -address_family 768179003Sbrooks# Get the interface of the default route for the given address family. 769179003Sbrooks# The -address_family argument must be suitable passing to route(8). 770179003Sbrooks# 771179003Sbrooksget_default_if() 772179003Sbrooks{ 773179003Sbrooks routeget="`route -n get $1 default 2>/dev/null`" 774179003Sbrooks oldifs="$IFS" 775179003Sbrooks IFS=" 776179003Sbrooks" 777179003Sbrooks defif= 778179003Sbrooks for line in $routeget ; do 779179003Sbrooks case $line in 780179003Sbrooks *interface:*) 781179003Sbrooks defif=${line##*: } 782179003Sbrooks ;; 783179003Sbrooks esac 784179003Sbrooks done 785179003Sbrooks IFS=${oldifs} 786179003Sbrooks 787179003Sbrooks echo $defif 788179003Sbrooks} 789179003Sbrooks 790114942Sumehexdigit() 791114942Sume{ 792114942Sume if [ $1 -lt 10 ]; then 793114942Sume echo $1 794114942Sume else 795114942Sume case $1 in 796114942Sume 10) echo a ;; 797114942Sume 11) echo b ;; 798114942Sume 12) echo c ;; 799114942Sume 13) echo d ;; 800114942Sume 14) echo e ;; 801114942Sume 15) echo f ;; 802114942Sume esac 803114942Sume fi 804114942Sume} 805114942Sume 806114942Sumehexprint() 807114942Sume{ 808114942Sume val=$1 809114942Sume str='' 810114942Sume 811114942Sume dig=`hexdigit $((${val} & 15))` 812114942Sume str=${dig}${str} 813114942Sume val=$((${val} >> 4)) 814114942Sume while [ ${val} -gt 0 ]; do 815114942Sume dig=`hexdigit $((${val} & 15))` 816114942Sume str=${dig}${str} 817114942Sume val=$((${val} >> 4)) 818114942Sume done 819114942Sume 820114942Sume echo ${str} 821114942Sume} 822114942Sume 823114942Sume# Setup the interfaces for IPv6 824114942Sumenetwork6_interface_setup() 825114942Sume{ 826114942Sume interfaces=$* 827114942Sume rtsol_interfaces='' 828114942Sume case ${ipv6_gateway_enable} in 829114942Sume [Yy][Ee][Ss]) 830114942Sume rtsol_available=no 831114942Sume ;; 832114942Sume *) 833114942Sume rtsol_available=yes 834114942Sume ;; 835114942Sume esac 836114942Sume for i in $interfaces; do 837114942Sume rtsol_interface=yes 838157706Sbrooks prefix=`get_if_var $i ipv6_prefix_IF` 839114942Sume if [ -n "${prefix}" ]; then 840114942Sume rtsol_available=no 841114942Sume rtsol_interface=no 842114942Sume laddr=`network6_getladdr $i` 843114942Sume hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'` 844114942Sume for j in ${prefix}; do 845114942Sume address=$j\:${hostid} 846114942Sume ifconfig $i inet6 ${address} prefixlen 64 alias 847114942Sume 848114942Sume case ${ipv6_gateway_enable} in 849114942Sume [Yy][Ee][Ss]) 850114942Sume # subnet-router anycast address 851114942Sume # (rfc2373) 852114942Sume ifconfig $i inet6 $j:: prefixlen 64 \ 853114942Sume alias anycast 854114942Sume ;; 855114942Sume esac 856114942Sume done 857114942Sume fi 858157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF` 859114942Sume if [ -n "${ipv6_ifconfig}" ]; then 860114942Sume rtsol_available=no 861114942Sume rtsol_interface=no 862114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 863114942Sume fi 864114942Sume 865114942Sume if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ] 866114942Sume then 867114942Sume case ${i} in 868163759Smlaier lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*|pflog[0-9]*|pfsync[0-9]*) 869114942Sume ;; 870114942Sume *) 871114942Sume rtsol_interfaces="${rtsol_interfaces} ${i}" 872114942Sume ;; 873114942Sume esac 874114942Sume else 875114942Sume ifconfig $i inet6 876114942Sume fi 877114942Sume done 878114942Sume 879114942Sume if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then 880114942Sume # Act as endhost - automatically configured. 881114942Sume # You can configure only single interface, as 882114942Sume # specification assumes that autoconfigured host has 883114942Sume # single interface only. 884114942Sume sysctl net.inet6.ip6.accept_rtadv=1 885114942Sume set ${rtsol_interfaces} 886114942Sume ifconfig $1 up 887118666Sume rtsol ${rtsol_flags} $1 888114942Sume fi 889114942Sume 890114942Sume for i in $interfaces; do 891114942Sume alias=0 892114942Sume while : ; do 893157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}` 894114942Sume if [ -z "${ipv6_ifconfig}" ]; then 895114942Sume break; 896114942Sume fi 897114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 898114942Sume alias=$((${alias} + 1)) 899114942Sume done 900114942Sume done 901114942Sume} 902114942Sume 903114942Sume# Setup IPv6 to IPv4 mapping 904114942Sumenetwork6_stf_setup() 905114942Sume{ 906114942Sume case ${stf_interface_ipv4addr} in 907114942Sume [Nn][Oo] | '') 908114942Sume ;; 909114942Sume *) 910114942Sume # assign IPv6 addr and interface route for 6to4 interface 911114942Sume stf_prefixlen=$((16+${stf_interface_ipv4plen:-0})) 912114942Sume OIFS="$IFS" 913114942Sume IFS=".$IFS" 914114942Sume set ${stf_interface_ipv4addr} 915114942Sume IFS="$OIFS" 916114942Sume hexfrag1=`hexprint $(($1*256 + $2))` 917114942Sume hexfrag2=`hexprint $(($3*256 + $4))` 918114942Sume ipv4_in_hexformat="${hexfrag1}:${hexfrag2}" 919114942Sume case ${stf_interface_ipv6_ifid} in 920114942Sume [Aa][Uu][Tt][Oo] | '') 921114942Sume for i in ${ipv6_network_interfaces}; do 922114942Sume laddr=`network6_getladdr ${i}` 923114942Sume case ${laddr} in 924114942Sume '') 925114942Sume ;; 926114942Sume *) 927114942Sume break 928114942Sume ;; 929114942Sume esac 930114942Sume done 931114942Sume stf_interface_ipv6_ifid=`expr "${laddr}" : \ 932114942Sume 'fe80::\(.*\)%\(.*\)'` 933114942Sume case ${stf_interface_ipv6_ifid} in 934114942Sume '') 935114942Sume stf_interface_ipv6_ifid=0:0:0:1 936114942Sume ;; 937114942Sume esac 938114942Sume ;; 939114942Sume esac 940114942Sume ifconfig stf0 create >/dev/null 2>&1 941114942Sume ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \ 942114942Sume prefixlen ${stf_prefixlen} 943114942Sume # disallow packets to malicious 6to4 prefix 944114942Sume route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 945114942Sume route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 946114942Sume route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 947114942Sume route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 948114942Sume ;; 949114942Sume esac 950114942Sume} 951114942Sume 952114942Sume# Setup static routes 953114942Sumenetwork6_static_routes_setup() 954114942Sume{ 955114942Sume # Set up any static routes. 956114942Sume case ${ipv6_defaultrouter} in 957114942Sume [Nn][Oo] | '') 958114942Sume ;; 959114942Sume *) 960114942Sume ipv6_static_routes="default ${ipv6_static_routes}" 961114942Sume ipv6_route_default="default ${ipv6_defaultrouter}" 962114942Sume ;; 963114942Sume esac 964114942Sume case ${ipv6_static_routes} in 965114942Sume [Nn][Oo] | '') 966114942Sume ;; 967114942Sume *) 968114942Sume for i in ${ipv6_static_routes}; do 969157706Sbrooks ipv6_route_args=`get_if_var $i ipv6_route_IF` 970114942Sume route add -inet6 ${ipv6_route_args} 971114942Sume done 972114942Sume ;; 973114942Sume esac 974114942Sume} 975114942Sume 976114942Sume# Setup faith 977114942Sumenetwork6_faith_setup() 978114942Sume{ 979114942Sume case ${ipv6_faith_prefix} in 980114942Sume [Nn][Oo] | '') 981114942Sume ;; 982114942Sume *) 983114942Sume sysctl net.inet6.ip6.keepfaith=1 984114942Sume ifconfig faith0 create >/dev/null 2>&1 985114942Sume ifconfig faith0 up 986114942Sume for prefix in ${ipv6_faith_prefix}; do 987114942Sume prefixlen=`expr "${prefix}" : ".*/\(.*\)"` 988114942Sume case ${prefixlen} in 989114942Sume '') 990114942Sume prefixlen=96 991114942Sume ;; 992114942Sume *) 993114942Sume prefix=`expr "${prefix}" : \ 994114942Sume "\(.*\)/${prefixlen}"` 995114942Sume ;; 996114942Sume esac 997114942Sume route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1 998114942Sume route change -inet6 ${prefix} -prefixlen ${prefixlen} \ 999114942Sume -ifp faith0 1000114942Sume done 1001114942Sume ;; 1002114942Sume esac 1003114942Sume} 1004114942Sume 1005114942Sume# Install the "default interface" to kernel, which will be used 1006114942Sume# as the default route when there's no router. 1007114942Sumenetwork6_default_interface_setup() 1008114942Sume{ 1009114942Sume # Choose IPv6 default interface if it is not clearly specified. 1010114942Sume case ${ipv6_default_interface} in 1011114942Sume '') 1012114942Sume for i in ${ipv6_network_interfaces}; do 1013114942Sume case $i in 1014114942Sume lo0|faith[0-9]*) 1015114942Sume continue 1016114942Sume ;; 1017114942Sume esac 1018114942Sume laddr=`network6_getladdr $i exclude_tentative` 1019114942Sume case ${laddr} in 1020114942Sume '') 1021114942Sume ;; 1022114942Sume *) 1023114942Sume ipv6_default_interface=$i 1024114942Sume break 1025114942Sume ;; 1026114942Sume esac 1027114942Sume done 1028114942Sume ;; 1029114942Sume esac 1030114942Sume 1031114942Sume # Disallow unicast packets without outgoing scope identifiers, 1032114942Sume # or route such packets to a "default" interface, if it is specified. 1033114942Sume route add -inet6 fe80:: -prefixlen 10 ::1 -reject 1034114942Sume case ${ipv6_default_interface} in 1035114942Sume [Nn][Oo] | '') 1036114942Sume route add -inet6 ff02:: -prefixlen 16 ::1 -reject 1037114942Sume ;; 1038114942Sume *) 1039114942Sume laddr=`network6_getladdr ${ipv6_default_interface}` 1040114942Sume route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \ 1041114942Sume -cloning 1042114942Sume 1043114942Sume # Disable installing the default interface with the 1044114942Sume # case net.inet6.ip6.forwarding=0 and 1045114942Sume # net.inet6.ip6.accept_rtadv=0, due to avoid conflict 1046114942Sume # between the default router list and the manual 1047114942Sume # configured default route. 1048114942Sume case ${ipv6_gateway_enable} in 1049114942Sume [Yy][Ee][Ss]) 1050114942Sume ;; 1051114942Sume *) 1052114942Sume if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ] 1053114942Sume then 1054114942Sume ndp -I ${ipv6_default_interface} 1055114942Sume fi 1056114942Sume ;; 1057114942Sume esac 1058114942Sume ;; 1059114942Sume esac 1060114942Sume} 1061114942Sume 1062114942Sumenetwork6_getladdr() 1063114942Sume{ 1064114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 1065114942Sume case ${proto} in 1066114942Sume inet6) 1067114942Sume case ${addr} in 1068114942Sume fe80::*) 1069114942Sume if [ -z "$2" ]; then 1070114942Sume echo ${addr} 1071114942Sume return 1072114942Sume fi 1073114942Sume case ${rest} in 1074114942Sume *tentative*) 1075114942Sume continue 1076114942Sume ;; 1077114942Sume *) 1078114942Sume echo ${addr} 1079114942Sume return 1080114942Sume esac 1081114942Sume esac 1082114942Sume esac 1083114942Sume done 1084114942Sume} 1085