network.subr revision 226649
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 226649 2011-10-23 05:56:59Z hrs $ 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 34197147Shrs# Bring up and configure an interface. If some configuration is 35225560Sbrueffer# applied, 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 47222515Sbz afexists inet && ipv4_up ${ifn} && cfg=0 48222515Sbz afexists inet6 && ipv6_up ${ifn} && cfg=0 49222515Sbz afexists ipx && ipx_up ${ifn} && cfg=0 50197139Shrs childif_create ${ifn} && cfg=0 51178356Ssam 52178356Ssam return $cfg 53178356Ssam} 54178356Ssam 55197139Shrs# ifn_stop ifn 56225560Sbrueffer# Shutdown and de-configure an interface. If action is taken, 57197147Shrs# print the interface name. 58178356Ssam# 59178356Ssamifn_stop() 60178356Ssam{ 61178356Ssam local ifn cfg 62178356Ssam ifn="$1" 63178356Ssam cfg=1 64178356Ssam 65197139Shrs [ -z "$ifn" ] && err 1 "ifn_stop called without an interface" 66178356Ssam 67222515Sbz afexists ipx && ipx_down ${ifn} && cfg=0 68222515Sbz afexists inet6 && ipv6_down ${ifn} && cfg=0 69222515Sbz afexists inet && ipv4_down ${ifn} && cfg=0 70178356Ssam ifconfig_down ${ifn} && cfg=0 71178356Ssam ifscript_down ${ifn} && cfg=0 72197139Shrs childif_destroy ${ifn} && cfg=0 73178356Ssam 74178356Ssam return $cfg 75178356Ssam} 76178356Ssam 77113674Smtm# ifconfig_up if 78113674Smtm# Evaluate ifconfig(8) arguments for interface $if and 79113674Smtm# run ifconfig(8) with those arguments. It returns 0 if 80113674Smtm# arguments were found and executed or 1 if the interface 81147088Sbrooks# had no arguments. Pseudo arguments DHCP and WPA are handled 82147088Sbrooks# here. 83113674Smtm# 84113674Smtmifconfig_up() 85113674Smtm{ 86197139Shrs local _cfg _ipv6_opts ifconfig_args 87147088Sbrooks _cfg=1 88147088Sbrooks 89222515Sbz # Make sure lo0 always comes up. 90222515Sbz if [ "$1" = "lo0" ]; then 91222515Sbz _cfg=0 92222515Sbz fi 93222515Sbz 94197139Shrs # ifconfig_IF 95147088Sbrooks ifconfig_args=`ifconfig_getargs $1` 96113674Smtm if [ -n "${ifconfig_args}" ]; then 97223506Spluknet eval ifconfig $1 ${ifconfig_args} 98147088Sbrooks _cfg=0 99113674Smtm fi 100147088Sbrooks 101197139Shrs # inet6 specific 102197139Shrs if afexists inet6; then 103222733Shrs if checkyesno ipv6_activate_all_interfaces; then 104222733Shrs _ipv6_opts="-ifdisabled" 105222746Shrs elif [ "$1" != "lo0" ]; then 106222733Shrs _ipv6_opts="ifdisabled" 107212574Shrs fi 108197139Shrs 109222733Shrs # backward compatibility: $ipv6_enable 110222733Shrs case $ipv6_enable in 111222733Shrs [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 112222733Shrs _ipv6_opts="${_ipv6_opts} accept_rtadv" 113222733Shrs ;; 114222733Shrs esac 115222733Shrs 116225521Shrs case $ipv6_cpe_wanif in 117225521Shrs $1) 118225521Shrs _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv" 119225521Shrs ;; 120225521Shrs esac 121225521Shrs 122212574Shrs if [ -n "${_ipv6_opts}" ]; then 123212574Shrs ifconfig $1 inet6 ${_ipv6_opts} 124197526Shrs fi 125212574Shrs 126212574Shrs # ifconfig_IF_ipv6 127212574Shrs ifconfig_args=`ifconfig_getargs $1 ipv6` 128212574Shrs if [ -n "${ifconfig_args}" ]; then 129225522Shrs # backward compatibility: inet6 keyword 130225522Shrs case "${ifconfig_args}" in 131225522Shrs :*|[0-9a-fA-F]*:*) 132225522Shrs warn "\$ifconfig_$1_ipv6 needs " \ 133225522Shrs "\"inet6\" keyword for an IPv6 address." 134225522Shrs ifconfig_args="inet6 ${ifconfig_args}" 135225522Shrs ;; 136225522Shrs esac 137212574Shrs ifconfig $1 inet6 -ifdisabled 138225522Shrs eval ifconfig $1 ${ifconfig_args} 139212574Shrs _cfg=0 140212574Shrs fi 141212574Shrs 142212574Shrs # backward compatiblity: $ipv6_ifconfig_IF 143212574Shrs ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF` 144212574Shrs if [ -n "${ifconfig_args}" ]; then 145212574Shrs warn "\$ipv6_ifconfig_$1 is obsolete." \ 146212574Shrs " Use ifconfig_$1_ipv6 instead." 147212574Shrs ifconfig $1 inet6 -ifdisabled 148225522Shrs eval ifconfig $1 inet6 ${ifconfig_args} 149212574Shrs _cfg=0 150212574Shrs fi 151197139Shrs fi 152197139Shrs 153197139Shrs if [ ${_cfg} -eq 0 ]; then 154197139Shrs ifconfig $1 up 155197139Shrs fi 156197139Shrs 157147088Sbrooks if wpaif $1; then 158147682Sbrooks /etc/rc.d/wpa_supplicant start $1 159147088Sbrooks _cfg=0 # XXX: not sure this should count 160147088Sbrooks fi 161147088Sbrooks 162147088Sbrooks if dhcpif $1; then 163149726Sbrooks if [ $_cfg -ne 0 ] ; then 164149726Sbrooks ifconfig $1 up 165149726Sbrooks fi 166157706Sbrooks if syncdhcpif $1; then 167157706Sbrooks /etc/rc.d/dhclient start $1 168157706Sbrooks fi 169147088Sbrooks _cfg=0 170147088Sbrooks fi 171147088Sbrooks 172147121Sbrooks return $_cfg 173113674Smtm} 17425184Sjkh 175116029Smtm# ifconfig_down if 176161386Sbrooks# returns 1 if wpa_supplicant or dhclient was stopped or 177161386Sbrooks# the interface exists. 178116029Smtm# 179116029Smtmifconfig_down() 180116029Smtm{ 181197139Shrs local _cfg 182147121Sbrooks _cfg=1 183116029Smtm 184147088Sbrooks if wpaif $1; then 185147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 186147121Sbrooks _cfg=0 187147088Sbrooks fi 188147088Sbrooks 189147088Sbrooks if dhcpif $1; then 190147088Sbrooks /etc/rc.d/dhclient stop $1 191147088Sbrooks _cfg=0 192147088Sbrooks fi 193147088Sbrooks 194161386Sbrooks if ifexists $1; then 195161386Sbrooks ifconfig $1 down 196161386Sbrooks _cfg=0 197161386Sbrooks fi 198157706Sbrooks 199147121Sbrooks return $_cfg 200116029Smtm} 201116029Smtm 202157706Sbrooks# get_if_var if var [default] 203197147Shrs# Return the value of the pseudo-hash corresponding to $if where 204197147Shrs# $var is a string containg the sub-string "IF" which will be 205197147Shrs# replaced with $if after the characters defined in _punct are 206197147Shrs# replaced with '_'. If the variable is unset, replace it with 207197147Shrs# $default if given. 208157706Sbrooksget_if_var() 209157706Sbrooks{ 210212578Shrs local _if _punct _punct_c _var _default prefix suffix 211197139Shrs 212157706Sbrooks if [ $# -ne 2 -a $# -ne 3 ]; then 213157706Sbrooks err 3 'USAGE: get_if_var name var [default]' 214157706Sbrooks fi 215157706Sbrooks 216157706Sbrooks _if=$1 217157706Sbrooks _punct=". - / +" 218157736Sbrooks for _punct_c in $_punct; do 219157706Sbrooks _if=`ltr ${_if} ${_punct_c} '_'` 220157706Sbrooks done 221157706Sbrooks _var=$2 222157706Sbrooks _default=$3 223157706Sbrooks 224157706Sbrooks prefix=${_var%%IF*} 225157706Sbrooks suffix=${_var##*IF} 226168033Sache eval echo \${${prefix}${_if}${suffix}-${_default}} 227157706Sbrooks} 228157706Sbrooks 229197139Shrs# _ifconfig_getargs if [af] 230225560Sbrueffer# Prints the arguments for the supplied interface to stdout. 231225560Sbrueffer# Returns 1 if empty. In general, ifconfig_getargs should be used 232147088Sbrooks# outside this file. 233147088Sbrooks_ifconfig_getargs() 234147088Sbrooks{ 235212574Shrs local _ifn _af 236147088Sbrooks _ifn=$1 237197139Shrs _af=${2+_$2} 238197139Shrs 239147088Sbrooks if [ -z "$_ifn" ]; then 240147088Sbrooks return 1 241147088Sbrooks fi 242147088Sbrooks 243212574Shrs get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT" 244147088Sbrooks} 245147088Sbrooks 246197139Shrs# ifconfig_getargs if [af] 247147088Sbrooks# Takes the result from _ifconfig_getargs and removes pseudo 248147088Sbrooks# args such as DHCP and WPA. 249147088Sbrooksifconfig_getargs() 250147088Sbrooks{ 251197139Shrs local _tmpargs _arg _args 252197139Shrs _tmpargs=`_ifconfig_getargs $1 $2` 253147088Sbrooks if [ $? -eq 1 ]; then 254147088Sbrooks return 1 255147088Sbrooks fi 256147088Sbrooks _args= 257147088Sbrooks 258147088Sbrooks for _arg in $_tmpargs; do 259147088Sbrooks case $_arg in 260157706Sbrooks [Dd][Hh][Cc][Pp]) ;; 261157706Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) ;; 262157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 263157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 264157706Sbrooks [Ww][Pp][Aa]) ;; 265147088Sbrooks *) 266147088Sbrooks _args="$_args $_arg" 267147088Sbrooks ;; 268147088Sbrooks esac 269147088Sbrooks done 270147088Sbrooks 271147088Sbrooks echo $_args 272147088Sbrooks} 273147088Sbrooks 274149401Sbrooks# autoif 275225560Sbrueffer# Returns 0 if the interface should be automatically configured at 276149401Sbrooks# boot time and 1 otherwise. 277149401Sbrooksautoif() 278149401Sbrooks{ 279197139Shrs local _tmpargs _arg 280149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 281197139Shrs 282149401Sbrooks for _arg in $_tmpargs; do 283149401Sbrooks case $_arg in 284149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 285149401Sbrooks return 1 286149401Sbrooks ;; 287149401Sbrooks esac 288149401Sbrooks done 289197139Shrs 290149401Sbrooks return 0 291149401Sbrooks} 292149401Sbrooks 293147088Sbrooks# dhcpif if 294147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 295147088Sbrooksdhcpif() 296147088Sbrooks{ 297197139Shrs local _tmpargs _arg 298147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 299197139Shrs 300225849Sdelphij if noafif $1; then 301225849Sdelphij return 1 302225849Sdelphij fi 303225849Sdelphij 304147088Sbrooks for _arg in $_tmpargs; do 305147088Sbrooks case $_arg in 306147088Sbrooks [Dd][Hh][Cc][Pp]) 307147088Sbrooks return 0 308147088Sbrooks ;; 309157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 310157706Sbrooks return 0 311157706Sbrooks ;; 312157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 313157706Sbrooks return 0 314157706Sbrooks ;; 315147088Sbrooks esac 316147088Sbrooks done 317197139Shrs 318147088Sbrooks return 1 319147088Sbrooks} 320147088Sbrooks 321157706Sbrooks# syncdhcpif 322157706Sbrooks# Returns 0 if the interface should be configured synchronously and 323157706Sbrooks# 1 otherwise. 324157706Sbrookssyncdhcpif() 325157706Sbrooks{ 326197139Shrs local _tmpargs _arg 327157706Sbrooks _tmpargs=`_ifconfig_getargs $1` 328197139Shrs 329225849Sdelphij if noafif $1; then 330225849Sdelphij return 1 331225849Sdelphij fi 332225849Sdelphij 333157706Sbrooks for _arg in $_tmpargs; do 334157706Sbrooks case $_arg in 335157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 336157706Sbrooks return 1 337157706Sbrooks ;; 338157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 339157706Sbrooks return 0 340157706Sbrooks ;; 341157706Sbrooks esac 342157706Sbrooks done 343197139Shrs 344197139Shrs checkyesno synchronous_dhclient 345157706Sbrooks} 346157706Sbrooks 347147088Sbrooks# wpaif if 348147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 349147088Sbrookswpaif() 350147088Sbrooks{ 351197139Shrs local _tmpargs _arg 352147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 353197139Shrs 354147088Sbrooks for _arg in $_tmpargs; do 355147088Sbrooks case $_arg in 356147088Sbrooks [Ww][Pp][Aa]) 357147088Sbrooks return 0 358147088Sbrooks ;; 359147088Sbrooks esac 360147088Sbrooks done 361197139Shrs 362147088Sbrooks return 1 363147088Sbrooks} 364147088Sbrooks 365197139Shrs# afexists af 366197139Shrs# Returns 0 if the address family is enabled in the kernel 367197139Shrs# 1 otherwise. 368197139Shrsafexists() 369197139Shrs{ 370197139Shrs local _af 371197139Shrs _af=$1 372197139Shrs 373197139Shrs case ${_af} in 374222996Shrs inet|inet6) 375222996Shrs check_kern_features ${_af} 376197139Shrs ;; 377197697Shrs ipx) 378197697Shrs ${SYSCTL_N} net.ipx > /dev/null 2>&1 379197697Shrs ;; 380197697Shrs atm) 381197697Shrs if [ -x /sbin/atmconfig ]; then 382197697Shrs /sbin/atmconfig diag list > /dev/null 2>&1 383197697Shrs else 384197697Shrs return 1 385197697Shrs fi 386197697Shrs ;; 387197139Shrs *) 388197139Shrs err 1 "afexists(): Unsupported address family: $_af" 389197139Shrs ;; 390197139Shrs esac 391197139Shrs} 392197139Shrs 393212574Shrs# noafif if 394212574Shrs# Returns 0 if the interface has no af configuration and 1 otherwise. 395212574Shrsnoafif() 396212574Shrs{ 397212574Shrs local _if 398212574Shrs _if=$1 399212574Shrs 400212574Shrs case $_if in 401212574Shrs pflog[0-9]*|\ 402212574Shrs pfsync[0-9]*|\ 403212574Shrs an[0-9]*|\ 404212574Shrs ath[0-9]*|\ 405212574Shrs ipw[0-9]*|\ 406212577Shrs ipfw[0-9]*|\ 407212574Shrs iwi[0-9]*|\ 408212574Shrs iwn[0-9]*|\ 409212574Shrs ral[0-9]*|\ 410212574Shrs wi[0-9]*|\ 411212574Shrs wl[0-9]*|\ 412212574Shrs wpi[0-9]*) 413212574Shrs return 0 414212574Shrs ;; 415212574Shrs esac 416212574Shrs 417212574Shrs return 1 418212574Shrs} 419212574Shrs 420162490Sbrooks# ipv6if if 421162490Sbrooks# Returns 0 if the interface should be configured for IPv6 and 422162490Sbrooks# 1 otherwise. 423162490Sbrooksipv6if() 424162490Sbrooks{ 425212574Shrs local _if _tmpargs i 426212574Shrs _if=$1 427212574Shrs 428197139Shrs if ! afexists inet6; then 429162490Sbrooks return 1 430162490Sbrooks fi 431197139Shrs 432197139Shrs # lo0 is always IPv6-enabled 433212574Shrs case $_if in 434197139Shrs lo0) 435197139Shrs return 0 436197139Shrs ;; 437197139Shrs esac 438197139Shrs 439212574Shrs case "${ipv6_network_interfaces}" in 440212575Shrs $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo]) 441212575Shrs # True if $ifconfig_IF_ipv6 is defined. 442212575Shrs _tmpargs=`_ifconfig_getargs $_if ipv6` 443212575Shrs if [ -n "${_tmpargs}" ]; then 444212575Shrs return 0 445212575Shrs fi 446197139Shrs 447212575Shrs # backward compatibility: True if $ipv6_ifconfig_IF is defined. 448212575Shrs _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 449212575Shrs if [ -n "${_tmpargs}" ]; then 450212574Shrs return 0 451212574Shrs fi 452212575Shrs ;; 453212575Shrs esac 454197139Shrs 455162490Sbrooks return 1 456162490Sbrooks} 457162490Sbrooks 458197139Shrs# ipv6_autoconfif if 459197139Shrs# Returns 0 if the interface should be configured for IPv6 with 460225560Sbrueffer# Stateless Address Configuration; 1 otherwise. 461197139Shrsipv6_autoconfif() 462197139Shrs{ 463197139Shrs local _if _tmpargs _arg 464197139Shrs _if=$1 465197139Shrs 466212577Shrs case $_if in 467212577Shrs lo0|\ 468212577Shrs stf[0-9]*|\ 469212577Shrs faith[0-9]*|\ 470212577Shrs lp[0-9]*|\ 471212577Shrs sl[0-9]*) 472197139Shrs return 1 473212577Shrs ;; 474212577Shrs esac 475212574Shrs if noafif $_if; then 476212574Shrs return 1 477212574Shrs fi 478212577Shrs if ! ipv6if $_if; then 479212577Shrs return 1 480212577Shrs fi 481197139Shrs if checkyesno ipv6_gateway_enable; then 482197139Shrs return 1 483197139Shrs fi 484197526Shrs _tmpargs=`get_if_var $_if ipv6_prefix_IF` 485197526Shrs if [ -n "${_tmpargs}" ]; then 486197526Shrs return 1 487197526Shrs fi 488212574Shrs # backward compatibility: $ipv6_enable 489212574Shrs case $ipv6_enable in 490212577Shrs [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 491197526Shrs return 0 492212577Shrs ;; 493197526Shrs esac 494197526Shrs 495212574Shrs _tmpargs=`_ifconfig_getargs $_if ipv6` 496212574Shrs for _arg in $_tmpargs; do 497212574Shrs case $_arg in 498212574Shrs accept_rtadv) 499212574Shrs return 0 500212574Shrs ;; 501212574Shrs esac 502212574Shrs done 503212574Shrs 504212574Shrs # backward compatibility: $ipv6_ifconfig_IF 505212574Shrs _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 506212574Shrs for _arg in $_tmpargs; do 507212574Shrs case $_arg in 508212574Shrs accept_rtadv) 509212574Shrs return 0 510212574Shrs ;; 511212574Shrs esac 512212574Shrs done 513212574Shrs 514197139Shrs return 1 515197139Shrs} 516197139Shrs 517161386Sbrooks# ifexists if 518161386Sbrooks# Returns 0 if the interface exists and 1 otherwise. 519161386Sbrooksifexists() 520161386Sbrooks{ 521197139Shrs [ -z "$1" ] && return 1 522169889Sthompsa ifconfig -n $1 > /dev/null 2>&1 523161386Sbrooks} 524161386Sbrooks 525152441Sbrooks# ipv4_up if 526212578Shrs# add IPv4 addresses to the interface $if 527152441Sbrooksipv4_up() 528152441Sbrooks{ 529197139Shrs local _if _ret 530152441Sbrooks _if=$1 531197139Shrs _ret=1 532197139Shrs 533222515Sbz # Add 127.0.0.1/8 to lo0 unless otherwise specified. 534222515Sbz if [ "${_if}" = "lo0" ]; then 535226649Shrs ifconfig_args=`get_if_var ${_if} ifconfig_IF` 536222515Sbz if [ -z "${ifconfig_args}" ]; then 537222515Sbz ifconfig ${_if} inet 127.0.0.1/8 alias 538222515Sbz fi 539222515Sbz fi 540197139Shrs ifalias_up ${_if} inet && _ret=0 541197139Shrs ipv4_addrs_common ${_if} alias && _ret=0 542197139Shrs 543197139Shrs return $_ret 544152441Sbrooks} 545152441Sbrooks 546197139Shrs# ipv6_up if 547197139Shrs# add IPv6 addresses to the interface $if 548197139Shrsipv6_up() 549197139Shrs{ 550197139Shrs local _if _ret 551197139Shrs _if=$1 552197139Shrs _ret=1 553197139Shrs 554197139Shrs if ! ipv6if $_if; then 555197139Shrs return 0 556197139Shrs fi 557197139Shrs 558197139Shrs ifalias_up ${_if} inet6 && _ret=0 559197139Shrs ipv6_prefix_hostid_addr_up ${_if} && _ret=0 560197139Shrs ipv6_accept_rtadv_up ${_if} && _ret=0 561197139Shrs 562197139Shrs # wait for DAD 563197139Shrs sleep `${SYSCTL_N} net.inet6.ip6.dad_count` 564197139Shrs sleep 1 565197139Shrs 566197139Shrs return $_ret 567197139Shrs} 568197139Shrs 569152441Sbrooks# ipv4_down if 570197147Shrs# remove IPv4 addresses from the interface $if 571152441Sbrooksipv4_down() 572152441Sbrooks{ 573197139Shrs local _if _ifs _ret inetList oldifs _inet 574152441Sbrooks _if=$1 575161386Sbrooks _ifs="^" 576161386Sbrooks _ret=1 577161386Sbrooks 578161386Sbrooks inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`" 579161386Sbrooks 580161386Sbrooks oldifs="$IFS" 581161386Sbrooks IFS="$_ifs" 582161386Sbrooks for _inet in $inetList ; do 583161386Sbrooks # get rid of extraneous line 584161386Sbrooks [ -z "$_inet" ] && break 585161386Sbrooks 586161386Sbrooks _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 587161386Sbrooks 588161386Sbrooks IFS="$oldifs" 589161386Sbrooks ifconfig ${_if} ${_inet} delete 590161386Sbrooks IFS="$_ifs" 591161386Sbrooks _ret=0 592161386Sbrooks done 593161386Sbrooks IFS="$oldifs" 594161386Sbrooks 595197139Shrs ifalias_down ${_if} inet && _ret=0 596161386Sbrooks ipv4_addrs_common ${_if} -alias && _ret=0 597161386Sbrooks 598161386Sbrooks return $_ret 599152441Sbrooks} 600152441Sbrooks 601197139Shrs# ipv6_down if 602197139Shrs# remove IPv6 addresses from the interface $if 603197139Shrsipv6_down() 604197139Shrs{ 605197139Shrs local _if _ifs _ret inetList oldifs _inet6 606197139Shrs _if=$1 607197139Shrs _ifs="^" 608197139Shrs _ret=1 609197139Shrs 610197139Shrs if ! ipv6if $_if; then 611197139Shrs return 0 612197139Shrs fi 613197139Shrs 614197139Shrs ipv6_accept_rtadv_down ${_if} && _ret=0 615197139Shrs ifalias_down ${_if} inet6 && _ret=0 616197139Shrs 617197139Shrs inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`" 618197139Shrs 619197139Shrs oldifs="$IFS" 620197139Shrs IFS="$_ifs" 621197139Shrs for _inet6 in $inetList ; do 622197139Shrs # get rid of extraneous line 623197139Shrs [ -z "$_inet6" ] && break 624197139Shrs 625197139Shrs _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'` 626197139Shrs 627197139Shrs IFS="$oldifs" 628197139Shrs ifconfig ${_if} ${_inet6} -alias 629197139Shrs IFS="$_ifs" 630197139Shrs _ret=0 631197139Shrs done 632197139Shrs IFS="$oldifs" 633197139Shrs 634197139Shrs return $_ret 635197139Shrs} 636197139Shrs 637152441Sbrooks# ipv4_addrs_common if action 638197147Shrs# Evaluate the ifconfig_if_ipv4 arguments for interface $if and 639197147Shrs# use $action to add or remove IPv4 addresses from $if. 640152441Sbrooksipv4_addrs_common() 641197147Shrs{ 642197139Shrs local _ret _if _action _cidr _cidr_addr 643212578Shrs local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount 644152441Sbrooks _ret=1 645152441Sbrooks _if=$1 646152441Sbrooks _action=$2 647212578Shrs 648152441Sbrooks # get ipv4-addresses 649157706Sbrooks cidr_addr=`get_if_var $_if ipv4_addrs_IF` 650212578Shrs 651152441Sbrooks for _cidr in ${cidr_addr}; do 652152441Sbrooks _ipaddr=${_cidr%%/*} 653152441Sbrooks _netmask="/"${_cidr##*/} 654152441Sbrooks _range=${_ipaddr##*.} 655152441Sbrooks _ipnet=${_ipaddr%.*} 656152441Sbrooks _iplow=${_range%-*} 657152441Sbrooks _iphigh=${_range#*-} 658152441Sbrooks 659152441Sbrooks # clear netmask when removing aliases 660152441Sbrooks if [ "${_action}" = "-alias" ]; then 661152441Sbrooks _netmask="" 662152441Sbrooks fi 663212578Shrs 664152441Sbrooks _ipcount=${_iplow} 665152441Sbrooks while [ "${_ipcount}" -le "${_iphigh}" ]; do 666152441Sbrooks eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}" 667152441Sbrooks _ipcount=$((${_ipcount}+1)) 668152441Sbrooks _ret=0 669152441Sbrooks 670152441Sbrooks # only the first ipaddr in a subnet need the real netmask 671152441Sbrooks if [ "${_action}" != "-alias" ]; then 672152441Sbrooks _netmask="/32" 673152441Sbrooks fi 674152441Sbrooks done 675152441Sbrooks done 676197139Shrs 677152441Sbrooks return $_ret 678152441Sbrooks} 679152441Sbrooks 680197139Shrs# ifalias_up if af 681113674Smtm# Configure aliases for network interface $if. 682113674Smtm# It returns 0 if at least one alias was configured or 683113674Smtm# 1 if there were none. 684113674Smtm# 685113674Smtmifalias_up() 686113674Smtm{ 687197139Shrs local _ret 688113674Smtm _ret=1 689197139Shrs 690197139Shrs case "$2" in 691197139Shrs inet) 692197139Shrs _ret=`ifalias_ipv4_up "$1"` 693197139Shrs ;; 694197139Shrs inet6) 695197139Shrs _ret=`ifalias_ipv6_up "$1"` 696197139Shrs ;; 697197139Shrs esac 698197139Shrs 699197139Shrs return $_ret 700197139Shrs} 701197139Shrs 702197139Shrs# ifalias_ipv4_up if 703197139Shrs# Helper function for ifalias_up(). Handles IPv4. 704197139Shrs# 705197139Shrsifalias_ipv4_up() 706197139Shrs{ 707197139Shrs local _ret alias ifconfig_args 708197139Shrs _ret=1 709197139Shrs 710197139Shrs # ifconfig_IF_aliasN which starts with "inet" 711113674Smtm alias=0 712113674Smtm while : ; do 713157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 714197139Shrs case "${ifconfig_args}" in 715197139Shrs inet\ *) 716197139Shrs ifconfig $1 ${ifconfig_args} alias && _ret=0 717197139Shrs ;; 718197139Shrs "") 719197139Shrs break 720197139Shrs ;; 721197139Shrs esac 722197147Shrs alias=$((${alias} + 1)) 723197139Shrs done 724197139Shrs 725197139Shrs return $_ret 726197139Shrs} 727197139Shrs 728197139Shrs# ifalias_ipv6_up if 729197139Shrs# Helper function for ifalias_up(). Handles IPv6. 730197139Shrs# 731197139Shrsifalias_ipv6_up() 732197139Shrs{ 733197139Shrs local _ret alias ifconfig_args 734197139Shrs _ret=1 735197139Shrs 736197139Shrs # ifconfig_IF_aliasN which starts with "inet6" 737197139Shrs alias=0 738197139Shrs while : ; do 739197139Shrs ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 740197139Shrs case "${ifconfig_args}" in 741197139Shrs inet6\ *) 742197139Shrs ifconfig $1 ${ifconfig_args} alias && _ret=0 743197139Shrs ;; 744197139Shrs "") 745113674Smtm break 746197139Shrs ;; 747197139Shrs esac 748197139Shrs alias=$((${alias} + 1)) 749113674Smtm done 750197139Shrs 751197139Shrs # backward compatibility: ipv6_ifconfig_IF_aliasN. 752197139Shrs alias=0 753197139Shrs while : ; do 754197139Shrs ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}` 755197139Shrs case "${ifconfig_args}" in 756197139Shrs "") 757197139Shrs break 758197139Shrs ;; 759197139Shrs *) 760197139Shrs ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0 761197526Shrs warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \ 762197139Shrs " Use ifconfig_$1_aliasN instead." 763197139Shrs ;; 764197139Shrs esac 765197139Shrs alias=$((${alias} + 1)) 766197139Shrs done 767197139Shrs 768113674Smtm return $_ret 769113674Smtm} 770100280Sgordon 771197147Shrs# ifalias_down if af 772116029Smtm# Remove aliases for network interface $if. 773116029Smtm# It returns 0 if at least one alias was removed or 774116029Smtm# 1 if there were none. 775116029Smtm# 776116029Smtmifalias_down() 777116029Smtm{ 778197139Shrs local _ret 779116029Smtm _ret=1 780197139Shrs 781197139Shrs case "$2" in 782197139Shrs inet) 783197139Shrs _ret=`ifalias_ipv4_down "$1"` 784197139Shrs ;; 785197139Shrs inet6) 786197139Shrs _ret=`ifalias_ipv6_down "$1"` 787197139Shrs ;; 788197139Shrs esac 789197139Shrs 790197139Shrs return $_ret 791197139Shrs} 792197139Shrs 793197147Shrs# ifalias_ipv4_down if 794197139Shrs# Helper function for ifalias_down(). Handles IPv4. 795197139Shrs# 796197139Shrsifalias_ipv4_down() 797197139Shrs{ 798197139Shrs local _ret alias ifconfig_args 799197139Shrs _ret=1 800197139Shrs 801197139Shrs # ifconfig_IF_aliasN which starts with "inet" 802116029Smtm alias=0 803116029Smtm while : ; do 804157706Sbrooks ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 805197139Shrs case "${ifconfig_args}" in 806197139Shrs inet\ *) 807197139Shrs ifconfig $1 ${ifconfig_args} -alias && _ret=0 808197139Shrs ;; 809197139Shrs "") 810197139Shrs break 811197139Shrs ;; 812197139Shrs esac 813197147Shrs alias=$((${alias} + 1)) 814197139Shrs done 815197139Shrs 816197139Shrs return $_ret 817197139Shrs} 818197139Shrs 819197147Shrs# ifalias_ipv6_down if 820197139Shrs# Helper function for ifalias_down(). Handles IPv6. 821197139Shrs# 822197139Shrsifalias_ipv6_down() 823197139Shrs{ 824197139Shrs local _ret alias ifconfig_args 825197139Shrs _ret=1 826197139Shrs 827197139Shrs # ifconfig_IF_aliasN which starts with "inet6" 828197139Shrs alias=0 829197139Shrs while : ; do 830197139Shrs ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}` 831197139Shrs case "${ifconfig_args}" in 832197139Shrs inet6\ *) 833197139Shrs ifconfig $1 ${ifconfig_args} -alias && _ret=0 834197139Shrs ;; 835197139Shrs "") 836116029Smtm break 837197139Shrs ;; 838197139Shrs esac 839197139Shrs alias=$((${alias} + 1)) 840116029Smtm done 841197139Shrs 842197139Shrs # backward compatibility: ipv6_ifconfig_IF_aliasN. 843197526Shrs alias=0 844197139Shrs while : ; do 845197139Shrs ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}` 846197139Shrs case "${ifconfig_args}" in 847197139Shrs "") 848197139Shrs break 849197139Shrs ;; 850197139Shrs *) 851197526Shrs ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0 852197526Shrs warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \ 853197139Shrs " Use ifconfig_$1_aliasN instead." 854197139Shrs ;; 855197139Shrs esac 856197526Shrs alias=$((${alias} + 1)) 857197139Shrs done 858197139Shrs 859116029Smtm return $_ret 860116029Smtm} 861116029Smtm 862197139Shrs# ipv6_prefix_hostid_addr_up if 863197139Shrs# add IPv6 prefix + hostid addr to the interface $if 864197139Shrsipv6_prefix_hostid_addr_up() 865197139Shrs{ 866197139Shrs local _if prefix laddr hostid j address 867197139Shrs _if=$1 868197139Shrs prefix=`get_if_var ${_if} ipv6_prefix_IF` 869197139Shrs 870197139Shrs if [ -n "${prefix}" ]; then 871197139Shrs laddr=`network6_getladdr ${_if}` 872197139Shrs hostid=${laddr#fe80::} 873197139Shrs hostid=${hostid%\%*} 874197139Shrs 875197139Shrs for j in ${prefix}; do 876197139Shrs address=$j\:${hostid} 877197139Shrs ifconfig ${_if} inet6 ${address} prefixlen 64 alias 878197139Shrs 879197139Shrs # if I am a router, add subnet router 880197139Shrs # anycast address (RFC 2373). 881197139Shrs if checkyesno ipv6_gateway_enable; then 882197139Shrs ifconfig ${_if} inet6 $j:: prefixlen 64 \ 883197139Shrs alias anycast 884197139Shrs fi 885197139Shrs done 886197139Shrs fi 887197139Shrs} 888197139Shrs 889197139Shrs# ipv6_accept_rtadv_up if 890197139Shrs# Enable accepting Router Advertisement and send Router 891197139Shrs# Solicitation message 892197139Shrsipv6_accept_rtadv_up() 893197139Shrs{ 894197139Shrs if ipv6_autoconfif $1; then 895197139Shrs ifconfig $1 inet6 accept_rtadv up 896203433Sume if ! checkyesno rtsold_enable; then 897203433Sume rtsol ${rtsol_flags} $1 898203433Sume fi 899197139Shrs fi 900197139Shrs} 901197139Shrs 902197139Shrs# ipv6_accept_rtadv_down if 903197139Shrs# Disable accepting Router Advertisement 904197139Shrsipv6_accept_rtadv_down() 905197139Shrs{ 906197139Shrs if ipv6_autoconfif $1; then 907197139Shrs ifconfig $1 inet6 -accept_rtadv 908197139Shrs fi 909197139Shrs} 910197139Shrs 911113674Smtm# ifscript_up if 912113674Smtm# Evaluate a startup script for the $if interface. 913113674Smtm# It returns 0 if a script was found and processed or 914113674Smtm# 1 if no script was found. 915113674Smtm# 916113674Smtmifscript_up() 917100280Sgordon{ 918113674Smtm if [ -r /etc/start_if.$1 ]; then 919113674Smtm . /etc/start_if.$1 920113674Smtm return 0 921197139Shrs else 922197139Shrs return 1 923113674Smtm fi 924100280Sgordon} 925100280Sgordon 926116029Smtm# ifscript_down if 927116029Smtm# Evaluate a shutdown script for the $if interface. 928116029Smtm# It returns 0 if a script was found and processed or 929116029Smtm# 1 if no script was found. 930116029Smtm# 931116029Smtmifscript_down() 932116029Smtm{ 933116029Smtm if [ -r /etc/stop_if.$1 ]; then 934116029Smtm . /etc/stop_if.$1 935116029Smtm return 0 936197139Shrs else 937197139Shrs return 1 938116029Smtm fi 939116029Smtm} 940116029Smtm 941197147Shrs# clone_up 942197147Shrs# Create cloneable interfaces. 943113674Smtm# 944113674Smtmclone_up() 945100280Sgordon{ 946197139Shrs local _prefix _list ifn 947113674Smtm _prefix= 948113674Smtm _list= 949197139Shrs 950197139Shrs # create_args_IF 951113674Smtm for ifn in ${cloned_interfaces}; do 952178527Sbrooks ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF` 953116774Skuriyama if [ $? -eq 0 ]; then 954113674Smtm _list="${_list}${_prefix}${ifn}" 955113674Smtm [ -z "$_prefix" ] && _prefix=' ' 956113674Smtm fi 957113674Smtm done 958113674Smtm debug "Cloned: ${_list}" 959113674Smtm} 960100280Sgordon 961197147Shrs# clone_down 962197147Shrs# Destroy cloned interfaces. Destroyed interfaces are echoed to 963197147Shrs# standard output. 964113674Smtm# 965113674Smtmclone_down() 966113674Smtm{ 967197139Shrs local _prefix _list ifn 968113674Smtm _prefix= 969113674Smtm _list= 970197139Shrs 971113674Smtm for ifn in ${cloned_interfaces}; do 972208213Sjhb ifconfig -n ${ifn} destroy 973116774Skuriyama if [ $? -eq 0 ]; then 974113674Smtm _list="${_list}${_prefix}${ifn}" 975113674Smtm [ -z "$_prefix" ] && _prefix=' ' 976113674Smtm fi 977113674Smtm done 978113674Smtm debug "Destroyed clones: ${_list}" 979100280Sgordon} 980100280Sgordon 981197147Shrs# childif_create 982197147Shrs# Create and configure child interfaces. Return 0 if child 983197147Shrs# interfaces are created. 984178356Ssam# 985178356Ssamchildif_create() 986178356Ssam{ 987201215Sjhb local cfg child child_vlans child_wlans create_args debug_flags ifn i 988178356Ssam cfg=1 989178356Ssam ifn=$1 990178356Ssam 991178527Sbrooks # Create wireless interfaces 992178527Sbrooks child_wlans=`get_if_var $ifn wlans_IF` 993178527Sbrooks 994178527Sbrooks for child in ${child_wlans}; do 995183517Sbrooks create_args="wlandev $ifn `get_if_var $child create_args_IF`" 996189759Sbrooks debug_flags="`get_if_var $child wlandebug_IF`" 997189759Sbrooks 998178356Ssam if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 999178356Ssam ifconfig $child create ${create_args} && cfg=0 1000189759Sbrooks if [ -n "${debug_flags}" ]; then 1001189759Sbrooks wlandebug -i $child ${debug_flags} 1002189759Sbrooks fi 1003178356Ssam else 1004178356Ssam i=`ifconfig wlan create ${create_args}` 1005189759Sbrooks if [ -n "${debug_flags}" ]; then 1006189759Sbrooks wlandebug -i $i ${debug_flags} 1007189759Sbrooks fi 1008178356Ssam ifconfig $i name $child && cfg=0 1009178356Ssam fi 1010188118Sthompsa if autoif $child; then 1011188118Sthompsa ifn_start $child 1012188118Sthompsa fi 1013178356Ssam done 1014178356Ssam 1015201215Sjhb # Create vlan interfaces 1016201215Sjhb child_vlans=`get_if_var $ifn vlans_IF` 1017201215Sjhb 1018201215Sjhb if [ -n "${child_vlans}" ]; then 1019201215Sjhb load_kld if_vlan 1020201215Sjhb fi 1021201215Sjhb 1022201215Sjhb for child in ${child_vlans}; do 1023201215Sjhb if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1024201215Sjhb child="${ifn}.${child}" 1025201215Sjhb create_args=`get_if_var $child create_args_IF` 1026201215Sjhb ifconfig $child create ${create_args} && cfg=0 1027201215Sjhb else 1028201215Sjhb create_args="vlandev $ifn `get_if_var $child create_args_IF`" 1029201215Sjhb if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then 1030201215Sjhb ifconfig $child create ${create_args} && cfg=0 1031201215Sjhb else 1032201215Sjhb i=`ifconfig vlan create ${create_args}` 1033201215Sjhb ifconfig $i name $child && cfg=0 1034201215Sjhb fi 1035201215Sjhb fi 1036201215Sjhb if autoif $child; then 1037201215Sjhb ifn_start $child 1038201215Sjhb fi 1039201215Sjhb done 1040201215Sjhb 1041179001Sbrooks return ${cfg} 1042178356Ssam} 1043178356Ssam 1044197147Shrs# childif_destroy 1045197147Shrs# Destroy child interfaces. 1046178356Ssam# 1047178356Ssamchildif_destroy() 1048178356Ssam{ 1049201215Sjhb local cfg child child_vlans child_wlans ifn 1050197139Shrs cfg=1 1051178356Ssam 1052201216Sjhb child_wlans=`get_if_var $ifn wlans_IF` 1053178527Sbrooks for child in ${child_wlans}; do 1054201215Sjhb if ! ifexists $child; then 1055201215Sjhb continue 1056201215Sjhb fi 1057208213Sjhb ifconfig -n $child destroy && cfg=0 1058178356Ssam done 1059197139Shrs 1060201215Sjhb child_vlans=`get_if_var $ifn vlans_IF` 1061201215Sjhb for child in ${child_vlans}; do 1062201215Sjhb if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1063201215Sjhb child="${ifn}.${child}" 1064201215Sjhb fi 1065201215Sjhb if ! ifexists $child; then 1066201215Sjhb continue 1067201215Sjhb fi 1068208213Sjhb ifconfig -n $child destroy && cfg=0 1069201215Sjhb done 1070201215Sjhb 1071197139Shrs return ${cfg} 1072178356Ssam} 1073178356Ssam 1074197147Shrs# ng_mkpeer 1075197147Shrs# Create netgraph nodes. 1076166583Sflz# 1077197147Shrsng_mkpeer() 1078197147Shrs{ 1079166583Sflz ngctl -f - 2> /dev/null <<EOF 1080166583Sflzmkpeer $* 1081166583Sflzmsg dummy nodeinfo 1082166583SflzEOF 1083166583Sflz} 1084166583Sflz 1085197147Shrs# ng_create_one 1086197147Shrs# Create netgraph nodes. 1087197147Shrs# 1088197147Shrsng_create_one() 1089197147Shrs{ 1090197139Shrs local t 1091197139Shrs 1092166583Sflz ng_mkpeer $* | while read line; do 1093166583Sflz t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 1094166583Sflz if [ -n "${t}" ]; then 1095166583Sflz echo ${t} 1096166583Sflz return 1097166583Sflz fi 1098166583Sflz done 1099166583Sflz} 1100166583Sflz 1101197147Shrs# gif_up 1102197147Shrs# Create gif(4) tunnel interfaces. 1103197147Shrsgif_up() 1104197147Shrs{ 1105197139Shrs local i peers 1106197139Shrs 1107166583Sflz for i in ${gif_interfaces}; do 1108166583Sflz peers=`get_if_var $i gifconfig_IF` 1109166583Sflz case ${peers} in 1110166583Sflz '') 1111166583Sflz continue 1112166583Sflz ;; 1113166583Sflz *) 1114177682Sbrooks if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then 1115177682Sbrooks ifconfig $i create >/dev/null 2>&1 1116177682Sbrooks else 1117177682Sbrooks gif=`ifconfig gif create` 1118177682Sbrooks ifconfig $gif name $i 1119177682Sbrooks fi 1120166583Sflz ifconfig $i tunnel ${peers} 1121166583Sflz ifconfig $i up 1122166583Sflz ;; 1123166583Sflz esac 1124166583Sflz done 1125166583Sflz} 1126166583Sflz 1127166583Sflz# ng_fec_create ifn 1128197147Shrs# Configure Fast EtherChannel for interface $ifn. Returns 0 if 1129197147Shrs# FEC arguments were found and configured; returns !0 otherwise. 1130197139Shrsng_fec_create() 1131197139Shrs{ 1132166583Sflz local req_iface iface bogus 1133166583Sflz req_iface="$1" 1134166583Sflz 1135166583Sflz ngctl shutdown ${req_iface}: > /dev/null 2>&1 1136166583Sflz 1137166583Sflz bogus="" 1138166583Sflz while true; do 1139166583Sflz iface=`ng_create_one fec dummy fec` 1140166583Sflz if [ -z "${iface}" ]; then 1141166583Sflz exit 2 1142166583Sflz fi 1143166583Sflz if [ "${iface}" = "${req_iface}" ]; then 1144166583Sflz break 1145166583Sflz fi 1146166583Sflz bogus="${bogus} ${iface}" 1147166583Sflz done 1148166583Sflz 1149166583Sflz for iface in ${bogus}; do 1150166583Sflz ngctl shutdown ${iface}: 1151166583Sflz done 1152166583Sflz} 1153166583Sflz 1154197147Shrs# fec_up 1155197147Shrs# Create Fast EtherChannel interfaces. 1156197147Shrsfec_up() 1157197147Shrs{ 1158197139Shrs local i j 1159197139Shrs 1160166583Sflz for i in ${fec_interfaces}; do 1161166583Sflz ng_fec_create $i 1162166583Sflz for j in `get_if_var $i fecconfig_IF`; do 1163166583Sflz case ${j} in 1164100282Sdougb '') 1165100282Sdougb continue 1166100282Sdougb ;; 1167100282Sdougb *) 1168166583Sflz ngctl msg ${i}: add_iface "\"${j}\"" 1169100282Sdougb ;; 1170100282Sdougb esac 1171100282Sdougb done 1172166583Sflz done 1173100282Sdougb} 1174100282Sdougb 1175113674Smtm# ipx_up ifn 1176197147Shrs# Configure any IPX addresses for interface $ifn. Returns 0 if 1177197147Shrs# IPX arguments were found and configured; returns 1 otherwise. 1178113674Smtm# 1179113674Smtmipx_up() 1180100280Sgordon{ 1181197139Shrs local ifn 1182113674Smtm ifn="$1" 1183197139Shrs 1184197139Shrs # ifconfig_IF_ipx 1185197139Shrs ifconfig_args=`_ifconfig_getargs $ifn ipx` 1186113674Smtm if [ -n "${ifconfig_args}" ]; then 1187113674Smtm ifconfig ${ifn} ${ifconfig_args} 1188113674Smtm return 0 118985831Sdes fi 1190197139Shrs 1191113674Smtm return 1 1192113674Smtm} 119385831Sdes 1194116029Smtm# ipx_down ifn 1195116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 1196116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 1197113674Smtm# 1198116029Smtmipx_down() 1199116029Smtm{ 1200197139Shrs local _if _ifs _ret ipxList oldifs _ipx 1201197139Shrs _if=$1 1202116100Smtm _ifs="^" 1203116100Smtm _ret=1 1204197139Shrs ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`" 1205197139Shrs oldifs="$IFS" 1206116100Smtm 1207116100Smtm IFS="$_ifs" 1208116100Smtm for _ipx in $ipxList ; do 1209116100Smtm # get rid of extraneous line 1210116100Smtm [ -z "$_ipx" ] && break 1211116100Smtm 1212116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 1213116100Smtm 1214116100Smtm IFS="$oldifs" 1215197139Shrs ifconfig ${_if} ${_ipx} delete 1216116100Smtm IFS="$_ifs" 1217116100Smtm _ret=0 1218116100Smtm done 1219116100Smtm IFS="$oldifs" 1220116100Smtm 1221116100Smtm return $_ret 1222116029Smtm} 1223116029Smtm 1224137070Spjd# ifnet_rename 1225137070Spjd# Rename all requested interfaces. 1226116029Smtm# 1227137070Spjdifnet_rename() 1228137070Spjd{ 1229197139Shrs local _if _ifname 1230137070Spjd 1231197139Shrs # ifconfig_IF_name 1232197139Shrs for _if in `ifconfig -l`; do 1233157706Sbrooks _ifname=`get_if_var $_if ifconfig_IF_name` 1234137070Spjd if [ ! -z "$_ifname" ]; then 1235137070Spjd ifconfig $_if name $_ifname 1236137070Spjd fi 1237137070Spjd done 1238197139Shrs 1239137070Spjd return 0 1240137070Spjd} 1241137070Spjd 1242113674Smtm# list_net_interfaces type 1243113674Smtm# List all network interfaces. The type of interface returned 1244113674Smtm# can be controlled by the type argument. The type 1245113674Smtm# argument can be any of the following: 1246197147Shrs# nodhcp - all interfaces, excluding DHCP configured interfaces 1247197147Shrs# dhcp - list only DHCP configured interfaces 1248197139Shrs# noautoconf - all interfaces, excluding IPv6 Stateless 1249197139Shrs# Address Autoconf configured interfaces 1250197139Shrs# autoconf - list only IPv6 Stateless Address Autoconf 1251197139Shrs# configured interfaces 1252113674Smtm# If no argument is specified all network interfaces are output. 1253134429Syar# Note that the list will include cloned interfaces if applicable. 1254134429Syar# Cloned interfaces must already exist to have a chance to appear 1255134429Syar# in the list if ${network_interfaces} is set to `auto'. 1256113674Smtm# 1257113674Smtmlist_net_interfaces() 1258113674Smtm{ 1259197139Shrs local type _tmplist _list _autolist _lo _if 1260113674Smtm type=$1 126165532Snectar 1262149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 126351231Ssheldonh # 1264197139Shrs _tmplist= 126551231Ssheldonh case ${network_interfaces} in 126651231Ssheldonh [Aa][Uu][Tt][Oo]) 1267149401Sbrooks _autolist="`ifconfig -l`" 1268149726Sbrooks _lo= 1269149401Sbrooks for _if in ${_autolist} ; do 1270149401Sbrooks if autoif $_if; then 1271149726Sbrooks if [ "$_if" = "lo0" ]; then 1272149726Sbrooks _lo="lo0 " 1273149726Sbrooks else 1274197139Shrs _tmplist="${_tmplist} ${_if}" 1275149726Sbrooks fi 1276149401Sbrooks fi 1277149401Sbrooks done 1278197139Shrs _tmplist="${_lo}${_tmplist# }" 127951231Ssheldonh ;; 128083677Sbrooks *) 1281149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 1282196478Sdougb 1283196478Sdougb # lo0 is effectively mandatory, so help prevent foot-shooting 1284196478Sdougb # 1285196478Sdougb case "$_tmplist" in 1286196523Sdougb lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing 1287196478Sdougb *) _tmplist="lo0 ${_tmplist}" ;; 1288196478Sdougb esac 128983677Sbrooks ;; 129051231Ssheldonh esac 129149122Sbrian 1292197139Shrs _list= 1293197139Shrs case "$type" in 1294197139Shrs nodhcp) 1295197139Shrs for _if in ${_tmplist} ; do 1296197139Shrs if ! dhcpif $_if && \ 1297197139Shrs [ -n "`_ifconfig_getargs $_if`" ]; then 1298197139Shrs _list="${_list# } ${_if}" 1299197147Shrs fi 1300197139Shrs done 1301197139Shrs ;; 1302197139Shrs dhcp) 1303197147Shrs for _if in ${_tmplist} ; do 1304197147Shrs if dhcpif $_if; then 1305197139Shrs _list="${_list# } ${_if}" 1306197147Shrs fi 1307197147Shrs done 1308113674Smtm ;; 1309197139Shrs noautoconf) 1310197139Shrs for _if in ${_tmplist} ; do 1311197139Shrs if ! ipv6_autoconfif $_if && \ 1312197139Shrs [ -n "`_ifconfig_getargs $_if ipv6`" ]; then 1313197139Shrs _list="${_list# } ${_if}" 1314197139Shrs fi 1315197139Shrs done 1316113674Smtm ;; 1317197139Shrs autoconf) 1318197139Shrs for _if in ${_tmplist} ; do 1319197139Shrs if ipv6_autoconfif $_if; then 1320197139Shrs _list="${_list# } ${_if}" 1321197139Shrs fi 1322197139Shrs done 1323197139Shrs ;; 1324197139Shrs *) 1325197139Shrs _list=${_tmplist} 1326197139Shrs ;; 1327113674Smtm esac 1328197139Shrs 1329197139Shrs echo $_list 1330197139Shrs 1331130151Sschweikh return 0 133225184Sjkh} 1333114942Sume 1334179003Sbrooks# get_default_if -address_family 1335179003Sbrooks# Get the interface of the default route for the given address family. 1336179003Sbrooks# The -address_family argument must be suitable passing to route(8). 1337179003Sbrooks# 1338179003Sbrooksget_default_if() 1339179003Sbrooks{ 1340197139Shrs local routeget oldifs defif line 1341197139Shrs defif= 1342179003Sbrooks oldifs="$IFS" 1343179003Sbrooks IFS=" 1344179003Sbrooks" 1345197139Shrs for line in `route -n get $1 default 2>/dev/null`; do 1346179003Sbrooks case $line in 1347179003Sbrooks *interface:*) 1348179003Sbrooks defif=${line##*: } 1349179003Sbrooks ;; 1350179003Sbrooks esac 1351179003Sbrooks done 1352179003Sbrooks IFS=${oldifs} 1353179003Sbrooks 1354179003Sbrooks echo $defif 1355179003Sbrooks} 1356179003Sbrooks 1357197147Shrs# hexdigit arg 1358197147Shrs# Echo decimal number $arg (single digit) in hexadecimal format. 1359114942Sumehexdigit() 1360114942Sume{ 1361221884Sjilles printf '%x\n' "$1" 1362114942Sume} 1363114942Sume 1364197147Shrs# hexprint arg 1365197147Shrs# Echo decimal number $arg (multiple digits) in hexadecimal format. 1366114942Sumehexprint() 1367114942Sume{ 1368221884Sjilles printf '%x\n' "$1" 1369114942Sume} 1370114942Sume 1371196436Sdougbis_wired_interface() 1372196436Sdougb{ 1373196436Sdougb local media 1374196436Sdougb 1375196436Sdougb case `ifconfig $1 2>/dev/null` in 1376196436Sdougb *media:?Ethernet*) media=Ethernet ;; 1377196436Sdougb esac 1378196436Sdougb 1379196436Sdougb test "$media" = "Ethernet" 1380196436Sdougb} 1381196436Sdougb 1382197147Shrs# network6_getladdr if [flag] 1383197147Shrs# Echo link-local address from $if if any. 1384197147Shrs# If flag is defined, tentative ones will be excluded. 1385114942Sumenetwork6_getladdr() 1386114942Sume{ 1387197139Shrs local proto addr rest 1388114942Sume ifconfig $1 2>/dev/null | while read proto addr rest; do 1389114942Sume case ${proto} in 1390114942Sume inet6) 1391114942Sume case ${addr} in 1392114942Sume fe80::*) 1393114942Sume if [ -z "$2" ]; then 1394114942Sume echo ${addr} 1395114942Sume return 1396114942Sume fi 1397114942Sume case ${rest} in 1398114942Sume *tentative*) 1399114942Sume continue 1400114942Sume ;; 1401114942Sume *) 1402114942Sume echo ${addr} 1403114942Sume return 1404114942Sume esac 1405114942Sume esac 1406114942Sume esac 1407114942Sume done 1408114942Sume} 1409