network.subr revision 179001
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 179001 2008-05-14 23:53:39Z 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 if [ "$cfg" -eq 0 ]; then 52178356Ssam ifconfig ${ifn} 53178356Ssam fi 54178356Ssam 55178356Ssam return $cfg 56178356Ssam} 57178356Ssam 58178356Ssam# ifn_start ifn 59178356Ssam# Shutdown and de-configure an interface. If action is taken print the 60178356Ssam# interface name. 61178356Ssam# 62178356Ssamifn_stop() 63178356Ssam{ 64178356Ssam local ifn cfg 65178356Ssam ifn="$1" 66178356Ssam cfg=1 67178356Ssam 68178356Ssam [ -z "$ifn" ] && return 1 69178356Ssam 70178356Ssam ipx_down ${ifn} && cfg=0 71178356Ssam ipv4_down ${ifn} && cfg=0 72178356Ssam ifconfig_down ${ifn} && cfg=0 73178356Ssam ifscript_down ${ifn} && cfg=0 74179001Sbrooks childif_destroy 75178356Ssam 76178356Ssam if [ "$cfg" -eq 0 ]; then 77178356Ssam echo -n " ${ifn}" 78178356Ssam fi 79178356Ssam 80178356Ssam return $cfg 81178356Ssam} 82178356Ssam 83113674Smtm# ifconfig_up if 84113674Smtm# Evaluate ifconfig(8) arguments for interface $if and 85113674Smtm# run ifconfig(8) with those arguments. It returns 0 if 86113674Smtm# arguments were found and executed or 1 if the interface 87147088Sbrooks# had no arguments. Pseudo arguments DHCP and WPA are handled 88147088Sbrooks# here. 89113674Smtm# 90113674Smtmifconfig_up() 91113674Smtm{ 92147088Sbrooks _cfg=1 93147088Sbrooks 94147088Sbrooks ifconfig_args=`ifconfig_getargs $1` 95113674Smtm if [ -n "${ifconfig_args}" ]; then 96178356Ssam ifconfig $1 ${ifconfig_args} 97149726Sbrooks ifconfig $1 up 98147088Sbrooks _cfg=0 99113674Smtm fi 100147088Sbrooks 101147088Sbrooks if wpaif $1; then 102147682Sbrooks /etc/rc.d/wpa_supplicant start $1 103147088Sbrooks _cfg=0 # XXX: not sure this should count 104147088Sbrooks fi 105147088Sbrooks 106147088Sbrooks if dhcpif $1; then 107149726Sbrooks if [ $_cfg -ne 0 ] ; then 108149726Sbrooks ifconfig $1 up 109149726Sbrooks fi 110157706Sbrooks if syncdhcpif $1; then 111157706Sbrooks /etc/rc.d/dhclient start $1 112157706Sbrooks fi 113147088Sbrooks _cfg=0 114147088Sbrooks fi 115147088Sbrooks 116147121Sbrooks return $_cfg 117113674Smtm} 11825184Sjkh 119116029Smtm# ifconfig_down if 120161386Sbrooks# returns 1 if wpa_supplicant or dhclient was stopped or 121161386Sbrooks# the interface exists. 122116029Smtm# 123116029Smtmifconfig_down() 124116029Smtm{ 125116029Smtm [ -z "$1" ] && return 1 126147121Sbrooks _cfg=1 127116029Smtm 128147088Sbrooks if wpaif $1; then 129147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 130147121Sbrooks _cfg=0 131147088Sbrooks fi 132147088Sbrooks 133147088Sbrooks if dhcpif $1; then 134147088Sbrooks /etc/rc.d/dhclient stop $1 135147088Sbrooks _cfg=0 136147088Sbrooks fi 137147088Sbrooks 138161386Sbrooks if ifexists $1; then 139161386Sbrooks ifconfig $1 down 140161386Sbrooks _cfg=0 141161386Sbrooks fi 142157706Sbrooks 143147121Sbrooks return $_cfg 144116029Smtm} 145116029Smtm 146157706Sbrooks# get_if_var if var [default] 147157706Sbrooks# Return the value of the pseudo-hash corresponding to $if where 148157706Sbrooks# $var is a string containg the sub-string "IF" which will be 149157706Sbrooks# replaced with $if after the characters defined in _punct are 150157706Sbrooks# replaced with '_'. If the variable is unset, replace it with 151157706Sbrooks# $default if given. 152157706Sbrooksget_if_var() 153157706Sbrooks{ 154157706Sbrooks if [ $# -ne 2 -a $# -ne 3 ]; then 155157706Sbrooks err 3 'USAGE: get_if_var name var [default]' 156157706Sbrooks fi 157157706Sbrooks 158157706Sbrooks _if=$1 159157706Sbrooks _punct=". - / +" 160157736Sbrooks for _punct_c in $_punct; do 161157706Sbrooks _if=`ltr ${_if} ${_punct_c} '_'` 162157706Sbrooks done 163157706Sbrooks _var=$2 164157706Sbrooks _default=$3 165157706Sbrooks 166157706Sbrooks prefix=${_var%%IF*} 167157706Sbrooks suffix=${_var##*IF} 168168033Sache eval echo \${${prefix}${_if}${suffix}-${_default}} 169157706Sbrooks} 170157706Sbrooks 171147088Sbrooks# _ifconfig_getargs if 172147088Sbrooks# Echos the arguments for the supplied interface to stdout. 173147088Sbrooks# returns 1 if empty. In general, ifconfig_getargs should be used 174147088Sbrooks# outside this file. 175147088Sbrooks_ifconfig_getargs() 176147088Sbrooks{ 177147088Sbrooks _ifn=$1 178147088Sbrooks if [ -z "$_ifn" ]; then 179147088Sbrooks return 1 180147088Sbrooks fi 181147088Sbrooks 182157706Sbrooks get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT" 183147088Sbrooks} 184147088Sbrooks 185147088Sbrooks# ifconfig_getargs if 186147088Sbrooks# Takes the result from _ifconfig_getargs and removes pseudo 187147088Sbrooks# args such as DHCP and WPA. 188147088Sbrooksifconfig_getargs() 189147088Sbrooks{ 190147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 191147088Sbrooks if [ $? -eq 1 ]; then 192147088Sbrooks return 1 193147088Sbrooks fi 194147088Sbrooks _args= 195147088Sbrooks 196147088Sbrooks for _arg in $_tmpargs; do 197147088Sbrooks case $_arg in 198157706Sbrooks [Dd][Hh][Cc][Pp]) ;; 199157706Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) ;; 200157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 201157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 202157706Sbrooks [Ww][Pp][Aa]) ;; 203147088Sbrooks *) 204147088Sbrooks _args="$_args $_arg" 205147088Sbrooks ;; 206147088Sbrooks esac 207147088Sbrooks done 208147088Sbrooks 209147088Sbrooks echo $_args 210147088Sbrooks} 211147088Sbrooks 212149401Sbrooks# autoif 213149401Sbrooks# Returns 0 if the interface should be automaticly configured at 214149401Sbrooks# boot time and 1 otherwise. 215149401Sbrooksautoif() 216149401Sbrooks{ 217149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 218149401Sbrooks for _arg in $_tmpargs; do 219149401Sbrooks case $_arg in 220149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 221149401Sbrooks return 1 222149401Sbrooks ;; 223149401Sbrooks esac 224149401Sbrooks done 225149401Sbrooks return 0 226149401Sbrooks} 227149401Sbrooks 228147088Sbrooks# dhcpif if 229147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 230147088Sbrooksdhcpif() 231147088Sbrooks{ 232147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 233147088Sbrooks for _arg in $_tmpargs; do 234147088Sbrooks case $_arg in 235147088Sbrooks [Dd][Hh][Cc][Pp]) 236147088Sbrooks return 0 237147088Sbrooks ;; 238157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 239157706Sbrooks return 0 240157706Sbrooks ;; 241157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 242157706Sbrooks return 0 243157706Sbrooks ;; 244147088Sbrooks esac 245147088Sbrooks done 246147088Sbrooks return 1 247147088Sbrooks} 248147088Sbrooks 249157706Sbrooks# syncdhcpif 250157706Sbrooks# Returns 0 if the interface should be configured synchronously and 251157706Sbrooks# 1 otherwise. 252157706Sbrookssyncdhcpif() 253157706Sbrooks{ 254157706Sbrooks _tmpargs=`_ifconfig_getargs $1` 255157706Sbrooks for _arg in $_tmpargs; do 256157706Sbrooks case $_arg in 257157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 258157706Sbrooks return 1 259157706Sbrooks ;; 260157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 261157706Sbrooks return 0 262157706Sbrooks ;; 263157706Sbrooks esac 264157706Sbrooks done 265157737Sbrooks if checkyesno synchronous_dhclient; then 266157706Sbrooks return 0 267157706Sbrooks else 268157706Sbrooks return 1 269157706Sbrooks fi 270157706Sbrooks} 271157706Sbrooks 272147088Sbrooks# wpaif if 273147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 274147088Sbrookswpaif() 275147088Sbrooks{ 276147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 277147088Sbrooks for _arg in $_tmpargs; do 278147088Sbrooks case $_arg in 279147088Sbrooks [Ww][Pp][Aa]) 280147088Sbrooks return 0 281147088Sbrooks ;; 282147088Sbrooks esac 283147088Sbrooks done 284147088Sbrooks return 1 285147088Sbrooks} 286147088Sbrooks 287162490Sbrooks# ipv6if if 288162490Sbrooks# Returns 0 if the interface should be configured for IPv6 and 289162490Sbrooks# 1 otherwise. 290162490Sbrooksipv6if() 291162490Sbrooks{ 292162490Sbrooks if ! checkyesno ipv6_enable; then 293162490Sbrooks return 1 294162490Sbrooks fi 295162490Sbrooks case "${ipv6_network_interfaces}" in 296162490Sbrooks [Aa][Uu][Tt][Oo]) 297162490Sbrooks return 0 298162490Sbrooks ;; 299162490Sbrooks ''|[Nn][Oo][Nn][Ee]) 300162490Sbrooks return 1 301162490Sbrooks ;; 302162490Sbrooks esac 303162490Sbrooks for v6if in ${ipv6_network_interfaces}; do 304162490Sbrooks if [ "${v6if}" = "${1}" ]; then 305162490Sbrooks return 0 306162490Sbrooks fi 307162490Sbrooks done 308162490Sbrooks return 1 309162490Sbrooks} 310162490Sbrooks 311161386Sbrooks# ifexists if 312161386Sbrooks# Returns 0 if the interface exists and 1 otherwise. 313161386Sbrooksifexists() 314161386Sbrooks{ 315169889Sthompsa ifconfig -n $1 > /dev/null 2>&1 316161386Sbrooks} 317161386Sbrooks 318152441Sbrooks# ipv4_up if 319152441Sbrooks# add IPv4 addresses to the interface $if 320152441Sbrooksipv4_up() 321152441Sbrooks{ 322152441Sbrooks _if=$1 323152441Sbrooks ifalias_up ${_if} 324152441Sbrooks ipv4_addrs_common ${_if} alias 325152441Sbrooks} 326152441Sbrooks 327152441Sbrooks# ipv4_down if 328152441Sbrooks# remove IPv4 addresses from the interface $if 329152441Sbrooksipv4_down() 330152441Sbrooks{ 331152441Sbrooks _if=$1 332161386Sbrooks _ifs="^" 333161386Sbrooks _ret=1 334161386Sbrooks 335161386Sbrooks ifexists ${_if} || return 1 336161386Sbrooks 337161386Sbrooks inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`" 338161386Sbrooks 339161386Sbrooks oldifs="$IFS" 340161386Sbrooks IFS="$_ifs" 341161386Sbrooks for _inet in $inetList ; do 342161386Sbrooks # get rid of extraneous line 343161386Sbrooks [ -z "$_inet" ] && break 344161386Sbrooks 345161386Sbrooks _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 346161386Sbrooks 347161386Sbrooks IFS="$oldifs" 348161386Sbrooks ifconfig ${_if} ${_inet} delete 349161386Sbrooks IFS="$_ifs" 350161386Sbrooks _ret=0 351161386Sbrooks done 352161386Sbrooks IFS="$oldifs" 353161386Sbrooks 354161386Sbrooks ifalias_down ${_if} && _ret=0 355161386Sbrooks ipv4_addrs_common ${_if} -alias && _ret=0 356161386Sbrooks 357161386Sbrooks return $_ret 358152441Sbrooks} 359152441Sbrooks 360152441Sbrooks# ipv4_addrs_common if action 361152441Sbrooks# Evaluate the ifconfig_if_ipv4 arguments for interface $if 362152441Sbrooks# and use $action to add or remove IPv4 addresses from $if. 363152441Sbrooksipv4_addrs_common() 364152441Sbrooks{ 365152441Sbrooks _ret=1 366152441Sbrooks _if=$1 367152441Sbrooks _action=$2 368152441Sbrooks 369152441Sbrooks # get ipv4-addresses 370157706Sbrooks cidr_addr=`get_if_var $_if ipv4_addrs_IF` 371152441Sbrooks 372152441Sbrooks for _cidr in ${cidr_addr}; do 373152441Sbrooks _ipaddr=${_cidr%%/*} 374152441Sbrooks _netmask="/"${_cidr##*/} 375152441Sbrooks _range=${_ipaddr##*.} 376152441Sbrooks _ipnet=${_ipaddr%.*} 377152441Sbrooks _iplow=${_range%-*} 378152441Sbrooks _iphigh=${_range#*-} 379152441Sbrooks 380152441Sbrooks # clear netmask when removing aliases 381152441Sbrooks if [ "${_action}" = "-alias" ]; then 382152441Sbrooks _netmask="" 383152441Sbrooks fi 384152441Sbrooks 385152441Sbrooks _ipcount=${_iplow} 386152441Sbrooks while [ "${_ipcount}" -le "${_iphigh}" ]; do 387152441Sbrooks eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}" 388152441Sbrooks _ipcount=$((${_ipcount}+1)) 389152441Sbrooks _ret=0 390152441Sbrooks 391152441Sbrooks # only the first ipaddr in a subnet need the real netmask 392152441Sbrooks if [ "${_action}" != "-alias" ]; then 393152441Sbrooks _netmask="/32" 394152441Sbrooks fi 395152441Sbrooks done 396152441Sbrooks done 397152441Sbrooks return $_ret 398152441Sbrooks} 399152441Sbrooks 400113674Smtm# ifalias_up if 401113674Smtm# Configure aliases for network interface $if. 402113674Smtm# It returns 0 if at least one alias was configured or 403113674Smtm# 1 if there were none. 404113674Smtm# 405113674Smtmifalias_up() 406113674Smtm{ 407113674Smtm _ret=1 408113674Smtm alias=0 409113674Smtm while : ; do 410157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 411113674Smtm if [ -n "${ifconfig_args}" ]; then 412113674Smtm ifconfig $1 ${ifconfig_args} alias 413113674Smtm alias=$((${alias} + 1)) 414113674Smtm _ret=0 415113674Smtm else 416113674Smtm break 417113674Smtm fi 418113674Smtm done 419113674Smtm return $_ret 420113674Smtm} 421100280Sgordon 422116029Smtm#ifalias_down if 423116029Smtm# Remove aliases for network interface $if. 424116029Smtm# It returns 0 if at least one alias was removed or 425116029Smtm# 1 if there were none. 426116029Smtm# 427116029Smtmifalias_down() 428116029Smtm{ 429116029Smtm _ret=1 430116029Smtm alias=0 431116029Smtm while : ; do 432157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 433116029Smtm if [ -n "${ifconfig_args}" ]; then 434116029Smtm ifconfig $1 ${ifconfig_args} -alias 435116029Smtm alias=$((${alias} + 1)) 436116029Smtm _ret=0 437116029Smtm else 438116029Smtm break 439116029Smtm fi 440116029Smtm done 441116029Smtm return $_ret 442116029Smtm} 443116029Smtm 444113674Smtm# ifscript_up if 445113674Smtm# Evaluate a startup script for the $if interface. 446113674Smtm# It returns 0 if a script was found and processed or 447113674Smtm# 1 if no script was found. 448113674Smtm# 449113674Smtmifscript_up() 450100280Sgordon{ 451113674Smtm if [ -r /etc/start_if.$1 ]; then 452113674Smtm . /etc/start_if.$1 453113674Smtm return 0 454113674Smtm fi 455113674Smtm return 1 456100280Sgordon} 457100280Sgordon 458116029Smtm# ifscript_down if 459116029Smtm# Evaluate a shutdown script for the $if interface. 460116029Smtm# It returns 0 if a script was found and processed or 461116029Smtm# 1 if no script was found. 462116029Smtm# 463116029Smtmifscript_down() 464116029Smtm{ 465116029Smtm if [ -r /etc/stop_if.$1 ]; then 466116029Smtm . /etc/stop_if.$1 467116029Smtm return 0 468116029Smtm fi 469116029Smtm return 1 470116029Smtm} 471116029Smtm 472113674Smtm# Create cloneable interfaces. 473113674Smtm# 474113674Smtmclone_up() 475100280Sgordon{ 476113674Smtm _prefix= 477113674Smtm _list= 478113674Smtm for ifn in ${cloned_interfaces}; do 479178527Sbrooks ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF` 480116774Skuriyama if [ $? -eq 0 ]; then 481113674Smtm _list="${_list}${_prefix}${ifn}" 482113674Smtm [ -z "$_prefix" ] && _prefix=' ' 483113674Smtm fi 484113674Smtm done 485113674Smtm debug "Cloned: ${_list}" 486113674Smtm} 487100280Sgordon 488113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed 489113674Smtm# to standard output. 490113674Smtm# 491113674Smtmclone_down() 492113674Smtm{ 493113674Smtm _prefix= 494113674Smtm _list= 495113674Smtm for ifn in ${cloned_interfaces}; do 496113674Smtm ifconfig ${ifn} destroy 497116774Skuriyama if [ $? -eq 0 ]; then 498113674Smtm _list="${_list}${_prefix}${ifn}" 499113674Smtm [ -z "$_prefix" ] && _prefix=' ' 500113674Smtm fi 501113674Smtm done 502113674Smtm debug "Destroyed clones: ${_list}" 503100280Sgordon} 504100280Sgordon 505178356Ssam# Create and configure child interfaces. 506178356Ssam# Return 0 if child interfaces are created. 507178356Ssam# 508178356Ssamchildif_create() 509178356Ssam{ 510178527Sbrooks local cfg child child_wlans create_args ifn i 511178356Ssam cfg=1 512178356Ssam 513178356Ssam ifn=$1 514178356Ssam 515178527Sbrooks # Create wireless interfaces 516178527Sbrooks child_wlans=`get_if_var $ifn wlans_IF` 517178527Sbrooks if [ -z "${child_wlans}" ]; then 518178527Sbrooks child_wlans=`get_if_var $ifn vaps_IF` 519178527Sbrooks if [ -n "${child_wlans}" ]; then 520178527Sbrooks warn "soon to be deleted vaps_$ifn variable defined use wlans_$ifn" 521178527Sbrooks fi 522178527Sbrooks fi 523178527Sbrooks 524178527Sbrooks for child in ${child_wlans}; do 525178527Sbrooks create_args="wlandev $ifn `get_if_var $child create_args_IF` `get_if_var $child vap_create_IF`" 526178356Ssam if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 527178356Ssam ifconfig $child create ${create_args} && cfg=0 528178356Ssam else 529178356Ssam i=`ifconfig wlan create ${create_args}` 530178356Ssam ifconfig $i name $child && cfg=0 531178356Ssam fi 532178356Ssam ifn_start $child 533178356Ssam done 534178356Ssam 535179001Sbrooks return ${cfg} 536178356Ssam} 537178356Ssam 538178356Ssam# Destroy child interfaces. 539178356Ssam# 540178356Ssamchildif_destroy() 541178356Ssam{ 542178527Sbrooks local cfg child child_wlans ifn 543178356Ssam 544178527Sbrooks child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`" 545178527Sbrooks for child in ${child_wlans}; do 546178356Ssam ifconfig $child destroy && cfg=0 547178356Ssam done 548178356Ssam} 549178356Ssam 550166583Sflz# Create netgraph nodes. 551166583Sflz# 552166583Sflzng_mkpeer() { 553166583Sflz ngctl -f - 2> /dev/null <<EOF 554166583Sflzmkpeer $* 555166583Sflzmsg dummy nodeinfo 556166583SflzEOF 557166583Sflz} 558166583Sflz 559166583Sflzng_create_one() { 560166583Sflz ng_mkpeer $* | while read line; do 561166583Sflz t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 562166583Sflz if [ -n "${t}" ]; then 563166583Sflz echo ${t} 564166583Sflz return 565166583Sflz fi 566166583Sflz done 567166583Sflz} 568166583Sflz 569113674Smtmgif_up() { 570166583Sflz for i in ${gif_interfaces}; do 571166583Sflz peers=`get_if_var $i gifconfig_IF` 572166583Sflz case ${peers} in 573166583Sflz '') 574166583Sflz continue 575166583Sflz ;; 576166583Sflz *) 577177682Sbrooks if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then 578177682Sbrooks ifconfig $i create >/dev/null 2>&1 579177682Sbrooks else 580177682Sbrooks gif=`ifconfig gif create` 581177682Sbrooks ifconfig $gif name $i 582177682Sbrooks fi 583166583Sflz ifconfig $i tunnel ${peers} 584166583Sflz ifconfig $i up 585166583Sflz ;; 586166583Sflz esac 587166583Sflz done 588166583Sflz} 589166583Sflz 590166583Sflz# ng_fec_create ifn 591166583Sflz# Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC 592166583Sflz# arguments were found and configured; returns !0 otherwise. 593166583Sflzng_fec_create() { 594166583Sflz local req_iface iface bogus 595166583Sflz req_iface="$1" 596166583Sflz 597166583Sflz ngctl shutdown ${req_iface}: > /dev/null 2>&1 598166583Sflz 599166583Sflz bogus="" 600166583Sflz while true; do 601166583Sflz iface=`ng_create_one fec dummy fec` 602166583Sflz if [ -z "${iface}" ]; then 603166583Sflz exit 2 604166583Sflz fi 605166583Sflz if [ "${iface}" = "${req_iface}" ]; then 606166583Sflz break 607166583Sflz fi 608166583Sflz bogus="${bogus} ${iface}" 609166583Sflz done 610166583Sflz 611166583Sflz for iface in ${bogus}; do 612166583Sflz ngctl shutdown ${iface}: 613166583Sflz done 614166583Sflz} 615166583Sflz 616166583Sflzfec_up() { 617166583Sflz for i in ${fec_interfaces}; do 618166583Sflz ng_fec_create $i 619166583Sflz for j in `get_if_var $i fecconfig_IF`; do 620166583Sflz case ${j} in 621100282Sdougb '') 622100282Sdougb continue 623100282Sdougb ;; 624100282Sdougb *) 625166583Sflz ngctl msg ${i}: add_iface "\"${j}\"" 626100282Sdougb ;; 627100282Sdougb esac 628100282Sdougb done 629166583Sflz done 630100282Sdougb} 631100282Sdougb 632113674Smtm# 633113674Smtm# ipx_up ifn 634113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX 635113674Smtm# arguments were found and configured; returns 1 otherwise. 636113674Smtm# 637113674Smtmipx_up() 638100280Sgordon{ 639113674Smtm ifn="$1" 640157706Sbrooks ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx` 641113674Smtm if [ -n "${ifconfig_args}" ]; then 642113674Smtm ifconfig ${ifn} ${ifconfig_args} 643113674Smtm return 0 64485831Sdes fi 645113674Smtm return 1 646113674Smtm} 64785831Sdes 648116029Smtm# ipx_down ifn 649116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 650116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 651113674Smtm# 652116029Smtmipx_down() 653116029Smtm{ 654116100Smtm [ -z "$1" ] && return 1 655116100Smtm _ifs="^" 656116100Smtm _ret=1 657116100Smtm 658161386Sbrooks ifexists $1 || return 1 659161386Sbrooks 660116100Smtm ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`" 661116100Smtm 662116100Smtm oldifs="$IFS" 663116100Smtm IFS="$_ifs" 664116100Smtm for _ipx in $ipxList ; do 665116100Smtm # get rid of extraneous line 666116100Smtm [ -z "$_ipx" ] && break 667116100Smtm 668116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 669116100Smtm 670116100Smtm IFS="$oldifs" 671116100Smtm ifconfig $1 ${_ipx} delete 672116100Smtm IFS="$_ifs" 673116100Smtm _ret=0 674116100Smtm done 675116100Smtm IFS="$oldifs" 676116100Smtm 677116100Smtm return $_ret 678116029Smtm} 679116029Smtm 680137070Spjd# ifnet_rename 681137070Spjd# Rename all requested interfaces. 682116029Smtm# 683137070Spjdifnet_rename() 684137070Spjd{ 685137070Spjd 686138386Srse _ifn_list="`ifconfig -l`" 687137070Spjd [ -z "$_ifn_list" ] && return 0 688137070Spjd for _if in ${_ifn_list} ; do 689157706Sbrooks _ifname=`get_if_var $_if ifconfig_IF_name` 690137070Spjd if [ ! -z "$_ifname" ]; then 691137070Spjd ifconfig $_if name $_ifname 692137070Spjd fi 693137070Spjd done 694137070Spjd return 0 695137070Spjd} 696137070Spjd 697137070Spjd# 698113674Smtm# list_net_interfaces type 699113674Smtm# List all network interfaces. The type of interface returned 700113674Smtm# can be controlled by the type argument. The type 701113674Smtm# argument can be any of the following: 702113674Smtm# nodhcp - all interfaces, excluding DHCP configured interfaces 703113674Smtm# dhcp - list only DHCP configured interfaces 704113674Smtm# If no argument is specified all network interfaces are output. 705134429Syar# Note that the list will include cloned interfaces if applicable. 706134429Syar# Cloned interfaces must already exist to have a chance to appear 707134429Syar# in the list if ${network_interfaces} is set to `auto'. 708113674Smtm# 709113674Smtmlist_net_interfaces() 710113674Smtm{ 711113674Smtm type=$1 71265532Snectar 713149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 71451231Ssheldonh # 71551231Ssheldonh case ${network_interfaces} in 71651231Ssheldonh [Aa][Uu][Tt][Oo]) 717149401Sbrooks _prefix='' 718149401Sbrooks _autolist="`ifconfig -l`" 719149726Sbrooks _lo= 720149401Sbrooks for _if in ${_autolist} ; do 721149401Sbrooks if autoif $_if; then 722149726Sbrooks if [ "$_if" = "lo0" ]; then 723149726Sbrooks _lo="lo0 " 724149726Sbrooks else 725149726Sbrooks _tmplist="${_tmplist}${_prefix}${_if}" 726149726Sbrooks [ -z "$_prefix" ] && _prefix=' ' 727149726Sbrooks fi 728149401Sbrooks fi 729149401Sbrooks done 730149726Sbrooks _tmplist="${_lo}${_tmplist}" 73151231Ssheldonh ;; 73283677Sbrooks *) 733178695Sbrooks if [ -z "$type"]; then 734178695Sbrooks warn "Values of network_interfaces other than" \ 735178695Sbrooks "AUTO are deprecated" 736178695Sbrooks fi 737149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 73883677Sbrooks ;; 73951231Ssheldonh esac 74049122Sbrian 741113674Smtm if [ -z "$type" ]; then 742113674Smtm echo $_tmplist 743113674Smtm return 0 744113674Smtm fi 74549122Sbrian 746138385Srse # Separate out dhcp and non-dhcp interfaces 747113674Smtm # 748113674Smtm _aprefix= 749134376Syar _bprefix= 750113674Smtm for _if in ${_tmplist} ; do 751147684Sbrooks if dhcpif $_if; then 752113674Smtm _dhcplist="${_dhcplist}${_aprefix}${_if}" 753113674Smtm [ -z "$_aprefix" ] && _aprefix=' ' 754157706Sbrooks elif [ -n "`_ifconfig_getargs $_if`" ]; then 755113674Smtm _nodhcplist="${_nodhcplist}${_bprefix}${_if}" 756113674Smtm [ -z "$_bprefix" ] && _bprefix=' ' 757147684Sbrooks fi 75854458Sobrien done 75951231Ssheldonh 760113674Smtm case "$type" in 761113674Smtm nodhcp) 762113674Smtm echo $_nodhcplist 763113674Smtm ;; 764113674Smtm dhcp) 765113674Smtm echo $_dhcplist 766113674Smtm ;; 767113674Smtm esac 768130151Sschweikh return 0 76925184Sjkh} 770114942Sume 771114942Sumehexdigit() 772114942Sume{ 773114942Sume if [ $1 -lt 10 ]; then 774114942Sume echo $1 775114942Sume else 776114942Sume case $1 in 777114942Sume 10) echo a ;; 778114942Sume 11) echo b ;; 779114942Sume 12) echo c ;; 780114942Sume 13) echo d ;; 781114942Sume 14) echo e ;; 782114942Sume 15) echo f ;; 783114942Sume esac 784114942Sume fi 785114942Sume} 786114942Sume 787114942Sumehexprint() 788114942Sume{ 789114942Sume val=$1 790114942Sume str='' 791114942Sume 792114942Sume dig=`hexdigit $((${val} & 15))` 793114942Sume str=${dig}${str} 794114942Sume val=$((${val} >> 4)) 795114942Sume while [ ${val} -gt 0 ]; do 796114942Sume dig=`hexdigit $((${val} & 15))` 797114942Sume str=${dig}${str} 798114942Sume val=$((${val} >> 4)) 799114942Sume done 800114942Sume 801114942Sume echo ${str} 802114942Sume} 803114942Sume 804114942Sume# Setup the interfaces for IPv6 805114942Sumenetwork6_interface_setup() 806114942Sume{ 807114942Sume interfaces=$* 808114942Sume rtsol_interfaces='' 809114942Sume case ${ipv6_gateway_enable} in 810114942Sume [Yy][Ee][Ss]) 811114942Sume rtsol_available=no 812114942Sume ;; 813114942Sume *) 814114942Sume rtsol_available=yes 815114942Sume ;; 816114942Sume esac 817114942Sume for i in $interfaces; do 818114942Sume rtsol_interface=yes 819157706Sbrooks prefix=`get_if_var $i ipv6_prefix_IF` 820114942Sume if [ -n "${prefix}" ]; then 821114942Sume rtsol_available=no 822114942Sume rtsol_interface=no 823114942Sume laddr=`network6_getladdr $i` 824114942Sume hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'` 825114942Sume for j in ${prefix}; do 826114942Sume address=$j\:${hostid} 827114942Sume ifconfig $i inet6 ${address} prefixlen 64 alias 828114942Sume 829114942Sume case ${ipv6_gateway_enable} in 830114942Sume [Yy][Ee][Ss]) 831114942Sume # subnet-router anycast address 832114942Sume # (rfc2373) 833114942Sume ifconfig $i inet6 $j:: prefixlen 64 \ 834114942Sume alias anycast 835114942Sume ;; 836114942Sume esac 837114942Sume done 838114942Sume fi 839157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF` 840114942Sume if [ -n "${ipv6_ifconfig}" ]; then 841114942Sume rtsol_available=no 842114942Sume rtsol_interface=no 843114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 844114942Sume fi 845114942Sume 846114942Sume if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ] 847114942Sume then 848114942Sume case ${i} in 849163759Smlaier 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]*) 850114942Sume ;; 851114942Sume *) 852114942Sume rtsol_interfaces="${rtsol_interfaces} ${i}" 853114942Sume ;; 854114942Sume esac 855114942Sume else 856114942Sume ifconfig $i inet6 857114942Sume fi 858114942Sume done 859114942Sume 860114942Sume if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then 861114942Sume # Act as endhost - automatically configured. 862114942Sume # You can configure only single interface, as 863114942Sume # specification assumes that autoconfigured host has 864114942Sume # single interface only. 865114942Sume sysctl net.inet6.ip6.accept_rtadv=1 866114942Sume set ${rtsol_interfaces} 867114942Sume ifconfig $1 up 868118666Sume rtsol ${rtsol_flags} $1 869114942Sume fi 870114942Sume 871114942Sume for i in $interfaces; do 872114942Sume alias=0 873114942Sume while : ; do 874157706Sbrooks ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}` 875114942Sume if [ -z "${ipv6_ifconfig}" ]; then 876114942Sume break; 877114942Sume fi 878114942Sume ifconfig $i inet6 ${ipv6_ifconfig} alias 879114942Sume alias=$((${alias} + 1)) 880114942Sume done 881114942Sume done 882114942Sume} 883114942Sume 884114942Sume# Setup IPv6 to IPv4 mapping 885114942Sumenetwork6_stf_setup() 886114942Sume{ 887114942Sume case ${stf_interface_ipv4addr} in 888114942Sume [Nn][Oo] | '') 889114942Sume ;; 890114942Sume *) 891114942Sume # assign IPv6 addr and interface route for 6to4 interface 892114942Sume stf_prefixlen=$((16+${stf_interface_ipv4plen:-0})) 893114942Sume OIFS="$IFS" 894114942Sume IFS=".$IFS" 895114942Sume set ${stf_interface_ipv4addr} 896114942Sume IFS="$OIFS" 897114942Sume hexfrag1=`hexprint $(($1*256 + $2))` 898114942Sume hexfrag2=`hexprint $(($3*256 + $4))` 899114942Sume ipv4_in_hexformat="${hexfrag1}:${hexfrag2}" 900114942Sume case ${stf_interface_ipv6_ifid} in 901114942Sume [Aa][Uu][Tt][Oo] | '') 902114942Sume for i in ${ipv6_network_interfaces}; do 903114942Sume laddr=`network6_getladdr ${i}` 904114942Sume case ${laddr} in 905114942Sume '') 906114942Sume ;; 907114942Sume *) 908114942Sume break 909114942Sume ;; 910114942Sume esac 911114942Sume done 912114942Sume stf_interface_ipv6_ifid=`expr "${laddr}" : \ 913114942Sume 'fe80::\(.*\)%\(.*\)'` 914114942Sume case ${stf_interface_ipv6_ifid} in 915114942Sume '') 916114942Sume stf_interface_ipv6_ifid=0:0:0:1 917114942Sume ;; 918114942Sume esac 919114942Sume ;; 920114942Sume esac 921114942Sume ifconfig stf0 create >/dev/null 2>&1 922114942Sume ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \ 923114942Sume prefixlen ${stf_prefixlen} 924114942Sume # disallow packets to malicious 6to4 prefix 925114942Sume route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject 926114942Sume route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject 927114942Sume route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject 928114942Sume route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject 929114942Sume ;; 930114942Sume esac 931114942Sume} 932114942Sume 933114942Sume# Setup static routes 934114942Sumenetwork6_static_routes_setup() 935114942Sume{ 936114942Sume # Set up any static routes. 937114942Sume case ${ipv6_defaultrouter} in 938114942Sume [Nn][Oo] | '') 939114942Sume ;; 940114942Sume *) 941114942Sume ipv6_static_routes="default ${ipv6_static_routes}" 942114942Sume ipv6_route_default="default ${ipv6_defaultrouter}" 943114942Sume ;; 944114942Sume esac 945114942Sume case ${ipv6_static_routes} in 946114942Sume [Nn][Oo] | '') 947114942Sume ;; 948114942Sume *) 949114942Sume for i in ${ipv6_static_routes}; do 950157706Sbrooks ipv6_route_args=`get_if_var $i ipv6_route_IF` 951114942Sume route add -inet6 ${ipv6_route_args} 952114942Sume done 953114942Sume ;; 954114942Sume esac 955114942Sume} 956114942Sume 957114942Sume# Setup faith 958114942Sumenetwork6_faith_setup() 959114942Sume{ 960114942Sume case ${ipv6_faith_prefix} in 961114942Sume [Nn][Oo] | '') 962114942Sume ;; 963114942Sume *) 964114942Sume sysctl net.inet6.ip6.keepfaith=1 965114942Sume ifconfig faith0 create >/dev/null 2>&1 966114942Sume ifconfig faith0 up 967114942Sume for prefix in ${ipv6_faith_prefix}; do 968114942Sume prefixlen=`expr "${prefix}" : ".*/\(.*\)"` 969114942Sume case ${prefixlen} in 970114942Sume '') 971114942Sume prefixlen=96 972114942Sume ;; 973114942Sume *) 974114942Sume prefix=`expr "${prefix}" : \ 975114942Sume "\(.*\)/${prefixlen}"` 976114942Sume ;; 977114942Sume esac 978114942Sume route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1 979114942Sume route change -inet6 ${prefix} -prefixlen ${prefixlen} \ 980114942Sume -ifp faith0 981114942Sume done 982114942Sume ;; 983114942Sume esac 984114942Sume} 985114942Sume 986114942Sume# Install the "default interface" to kernel, which will be used 987114942Sume# as the default route when there's no router. 988114942Sumenetwork6_default_interface_setup() 989114942Sume{ 990114942Sume # Choose IPv6 default interface if it is not clearly specified. 991114942Sume case ${ipv6_default_interface} in 992114942Sume '') 993114942Sume for i in ${ipv6_network_interfaces}; do 994114942Sume case $i in 995114942Sume lo0|faith[0-9]*) 996114942Sume continue 997114942Sume ;; 998114942Sume esac 999114942Sume laddr=`network6_getladdr $i exclude_tentative` 1000114942Sume case ${laddr} in 1001114942Sume '') 1002114942Sume ;; 1003114942Sume *) 1004114942Sume ipv6_default_interface=$i 1005114942Sume break 1006114942Sume ;; 1007114942Sume esac 1008114942Sume done 1009114942Sume ;; 1010114942Sume esac 1011114942Sume 1012114942Sume # Disallow unicast packets without outgoing scope identifiers, 1013114942Sume # or route such packets to a "default" interface, if it is specified. 1014114942Sume route add -inet6 fe80:: -prefixlen 10 ::1 -reject 1015114942Sume case ${ipv6_default_interface} in 1016114942Sume [Nn][Oo] | '') 1017114942Sume route add -inet6 ff02:: -prefixlen 16 ::1 -reject 1018114942Sume ;; 1019114942Sume *) 1020114942Sume laddr=`network6_getladdr ${ipv6_default_interface}` 1021114942Sume route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \ 1022114942Sume -cloning 1023114942Sume 1024114942Sume # Disable installing the default interface with the 1025114942Sume # case net.inet6.ip6.forwarding=0 and 1026114942Sume # net.inet6.ip6.accept_rtadv=0, due to avoid conflict 1027114942Sume # between the default router list and the manual 1028114942Sume # configured default route. 1029114942Sume case ${ipv6_gateway_enable} in 1030114942Sume [Yy][Ee][Ss]) 1031114942Sume ;; 1032114942Sume *) 1033114942Sume if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ] 1034114942Sume then 1035114942Sume ndp -I ${ipv6_default_interface} 1036114942Sume fi 1037114942Sume ;; 1038114942Sume esac 1039114942Sume ;; 1040114942Sume esac 1041114942Sume} 1042114942Sume 1043114942Sumenetwork6_getladdr() 1044114942Sume{ 1045114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 1046114942Sume case ${proto} in 1047114942Sume inet6) 1048114942Sume case ${addr} in 1049114942Sume fe80::*) 1050114942Sume if [ -z "$2" ]; then 1051114942Sume echo ${addr} 1052114942Sume return 1053114942Sume fi 1054114942Sume case ${rest} in 1055114942Sume *tentative*) 1056114942Sume continue 1057114942Sume ;; 1058114942Sume *) 1059114942Sume echo ${addr} 1060114942Sume return 1061114942Sume esac 1062114942Sume esac 1063114942Sume esac 1064114942Sume done 1065114942Sume} 1066