network.subr revision 253227
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: stable/9/etc/network.subr 253227 2013-07-12 00:40:49Z hrs $ 2666830Sobrien# 27253227ShrsIFCONFIG_CMD="/sbin/ifconfig" 2825184Sjkh 29253227Shrs# Maximum number of addresses expanded from a address range specification. 30253227Shrs_IPEXPANDMAX=31 31253227Shrs 32113674Smtm# 33113674Smtm# Subroutines commonly used from network startup scripts. 34113674Smtm# Requires that rc.conf be loaded first. 35113674Smtm# 3625184Sjkh 37178356Ssam# ifn_start ifn 38197147Shrs# Bring up and configure an interface. If some configuration is 39225560Sbrueffer# applied, print the interface configuration. 40178356Ssam# 41178356Ssamifn_start() 42178356Ssam{ 43178356Ssam local ifn cfg 44178356Ssam ifn="$1" 45178356Ssam cfg=1 46178356Ssam 47178356Ssam [ -z "$ifn" ] && err 1 "ifn_start called without an interface" 48178356Ssam 49178356Ssam ifscript_up ${ifn} && cfg=0 50178356Ssam ifconfig_up ${ifn} && cfg=0 51222515Sbz afexists inet && ipv4_up ${ifn} && cfg=0 52222515Sbz afexists inet6 && ipv6_up ${ifn} && cfg=0 53222515Sbz afexists ipx && ipx_up ${ifn} && cfg=0 54197139Shrs childif_create ${ifn} && cfg=0 55178356Ssam 56178356Ssam return $cfg 57178356Ssam} 58178356Ssam 59197139Shrs# ifn_stop ifn 60225560Sbrueffer# Shutdown and de-configure an interface. If action is taken, 61197147Shrs# print the interface name. 62178356Ssam# 63178356Ssamifn_stop() 64178356Ssam{ 65178356Ssam local ifn cfg 66178356Ssam ifn="$1" 67178356Ssam cfg=1 68178356Ssam 69197139Shrs [ -z "$ifn" ] && err 1 "ifn_stop called without an interface" 70178356Ssam 71222515Sbz afexists ipx && ipx_down ${ifn} && cfg=0 72222515Sbz afexists inet6 && ipv6_down ${ifn} && cfg=0 73222515Sbz afexists inet && ipv4_down ${ifn} && cfg=0 74178356Ssam ifconfig_down ${ifn} && cfg=0 75178356Ssam ifscript_down ${ifn} && cfg=0 76197139Shrs childif_destroy ${ifn} && cfg=0 77178356Ssam 78178356Ssam return $cfg 79178356Ssam} 80178356Ssam 81113674Smtm# ifconfig_up if 82113674Smtm# Evaluate ifconfig(8) arguments for interface $if and 83113674Smtm# run ifconfig(8) with those arguments. It returns 0 if 84113674Smtm# arguments were found and executed or 1 if the interface 85147088Sbrooks# had no arguments. Pseudo arguments DHCP and WPA are handled 86147088Sbrooks# here. 87113674Smtm# 88113674Smtmifconfig_up() 89113674Smtm{ 90197139Shrs local _cfg _ipv6_opts ifconfig_args 91147088Sbrooks _cfg=1 92147088Sbrooks 93222515Sbz # Make sure lo0 always comes up. 94222515Sbz if [ "$1" = "lo0" ]; then 95222515Sbz _cfg=0 96222515Sbz fi 97222515Sbz 98197139Shrs # ifconfig_IF 99147088Sbrooks ifconfig_args=`ifconfig_getargs $1` 100113674Smtm if [ -n "${ifconfig_args}" ]; then 101253227Shrs eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 102147088Sbrooks _cfg=0 103113674Smtm fi 104147088Sbrooks 105197139Shrs # inet6 specific 106197139Shrs if afexists inet6; then 107222733Shrs if checkyesno ipv6_activate_all_interfaces; then 108222733Shrs _ipv6_opts="-ifdisabled" 109222746Shrs elif [ "$1" != "lo0" ]; then 110222733Shrs _ipv6_opts="ifdisabled" 111212574Shrs fi 112197139Shrs 113222733Shrs # backward compatibility: $ipv6_enable 114222733Shrs case $ipv6_enable in 115222733Shrs [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 116242187Shrs if ! checkyesno ipv6_gateway_enable; then 117242187Shrs _ipv6_opts="${_ipv6_opts} accept_rtadv" 118242187Shrs fi 119222733Shrs ;; 120222733Shrs esac 121222733Shrs 122225521Shrs case $ipv6_cpe_wanif in 123225521Shrs $1) 124225521Shrs _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv" 125225521Shrs ;; 126225521Shrs esac 127225521Shrs 128212574Shrs if [ -n "${_ipv6_opts}" ]; then 129253227Shrs ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts} 130197526Shrs fi 131212574Shrs 132212574Shrs # ifconfig_IF_ipv6 133212574Shrs ifconfig_args=`ifconfig_getargs $1 ipv6` 134212574Shrs if [ -n "${ifconfig_args}" ]; then 135225522Shrs # backward compatibility: inet6 keyword 136225522Shrs case "${ifconfig_args}" in 137225522Shrs :*|[0-9a-fA-F]*:*) 138225522Shrs warn "\$ifconfig_$1_ipv6 needs " \ 139225522Shrs "\"inet6\" keyword for an IPv6 address." 140225522Shrs ifconfig_args="inet6 ${ifconfig_args}" 141225522Shrs ;; 142225522Shrs esac 143253227Shrs ${IFCONFIG_CMD} $1 inet6 -ifdisabled 144253227Shrs eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 145212574Shrs _cfg=0 146212574Shrs fi 147212574Shrs 148253227Shrs # $ipv6_prefix_IF will be handled in 149253227Shrs # ipv6_prefix_hostid_addr_common(). 150253227Shrs ifconfig_args=`get_if_var $1 ipv6_prefix_IF` 151253227Shrs if [ -n "${ifconfig_args}" ]; then 152253227Shrs ${IFCONFIG_CMD} $1 inet6 -ifdisabled 153253227Shrs _cfg=0 154253227Shrs fi 155253227Shrs 156253227Shrs # backward compatibility: $ipv6_ifconfig_IF 157212574Shrs ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF` 158212574Shrs if [ -n "${ifconfig_args}" ]; then 159212574Shrs warn "\$ipv6_ifconfig_$1 is obsolete." \ 160212574Shrs " Use ifconfig_$1_ipv6 instead." 161253227Shrs ${IFCONFIG_CMD} $1 inet6 -ifdisabled 162253227Shrs eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args} 163212574Shrs _cfg=0 164212574Shrs fi 165197139Shrs fi 166197139Shrs 167253227Shrs ifalias $1 link alias 168253227Shrs ifalias $1 ether alias 169253227Shrs 170197139Shrs if [ ${_cfg} -eq 0 ]; then 171253227Shrs ${IFCONFIG_CMD} $1 up 172197139Shrs fi 173197139Shrs 174147088Sbrooks if wpaif $1; then 175147682Sbrooks /etc/rc.d/wpa_supplicant start $1 176147088Sbrooks _cfg=0 # XXX: not sure this should count 177252748Srpaulo elif hostapif $1; then 178252748Srpaulo /etc/rc.d/hostapd start $1 179252748Srpaulo _cfg=0 180147088Sbrooks fi 181147088Sbrooks 182147088Sbrooks if dhcpif $1; then 183149726Sbrooks if [ $_cfg -ne 0 ] ; then 184253227Shrs ${IFCONFIG_CMD} $1 up 185149726Sbrooks fi 186157706Sbrooks if syncdhcpif $1; then 187157706Sbrooks /etc/rc.d/dhclient start $1 188157706Sbrooks fi 189147088Sbrooks _cfg=0 190147088Sbrooks fi 191147088Sbrooks 192147121Sbrooks return $_cfg 193113674Smtm} 19425184Sjkh 195116029Smtm# ifconfig_down if 196161386Sbrooks# returns 1 if wpa_supplicant or dhclient was stopped or 197161386Sbrooks# the interface exists. 198116029Smtm# 199116029Smtmifconfig_down() 200116029Smtm{ 201197139Shrs local _cfg 202147121Sbrooks _cfg=1 203116029Smtm 204147088Sbrooks if wpaif $1; then 205147682Sbrooks /etc/rc.d/wpa_supplicant stop $1 206147121Sbrooks _cfg=0 207252748Srpaulo elif hostapif $1; then 208252748Srpaulo /etc/rc.d/hostapd stop $1 209252748Srpaulo _cfg=0 210147088Sbrooks fi 211147088Sbrooks 212147088Sbrooks if dhcpif $1; then 213147088Sbrooks /etc/rc.d/dhclient stop $1 214147088Sbrooks _cfg=0 215147088Sbrooks fi 216147088Sbrooks 217161386Sbrooks if ifexists $1; then 218253227Shrs ${IFCONFIG_CMD} $1 down 219161386Sbrooks _cfg=0 220161386Sbrooks fi 221157706Sbrooks 222147121Sbrooks return $_cfg 223116029Smtm} 224116029Smtm 225157706Sbrooks# get_if_var if var [default] 226197147Shrs# Return the value of the pseudo-hash corresponding to $if where 227197147Shrs# $var is a string containg the sub-string "IF" which will be 228197147Shrs# replaced with $if after the characters defined in _punct are 229197147Shrs# replaced with '_'. If the variable is unset, replace it with 230197147Shrs# $default if given. 231157706Sbrooksget_if_var() 232157706Sbrooks{ 233212578Shrs local _if _punct _punct_c _var _default prefix suffix 234197139Shrs 235157706Sbrooks if [ $# -ne 2 -a $# -ne 3 ]; then 236157706Sbrooks err 3 'USAGE: get_if_var name var [default]' 237157706Sbrooks fi 238157706Sbrooks 239157706Sbrooks _if=$1 240157706Sbrooks _punct=". - / +" 241157736Sbrooks for _punct_c in $_punct; do 242157706Sbrooks _if=`ltr ${_if} ${_punct_c} '_'` 243157706Sbrooks done 244157706Sbrooks _var=$2 245157706Sbrooks _default=$3 246157706Sbrooks 247157706Sbrooks prefix=${_var%%IF*} 248157706Sbrooks suffix=${_var##*IF} 249168033Sache eval echo \${${prefix}${_if}${suffix}-${_default}} 250157706Sbrooks} 251157706Sbrooks 252197139Shrs# _ifconfig_getargs if [af] 253225560Sbrueffer# Prints the arguments for the supplied interface to stdout. 254225560Sbrueffer# Returns 1 if empty. In general, ifconfig_getargs should be used 255147088Sbrooks# outside this file. 256147088Sbrooks_ifconfig_getargs() 257147088Sbrooks{ 258212574Shrs local _ifn _af 259147088Sbrooks _ifn=$1 260197139Shrs _af=${2+_$2} 261197139Shrs 262147088Sbrooks if [ -z "$_ifn" ]; then 263147088Sbrooks return 1 264147088Sbrooks fi 265147088Sbrooks 266212574Shrs get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT" 267147088Sbrooks} 268147088Sbrooks 269197139Shrs# ifconfig_getargs if [af] 270147088Sbrooks# Takes the result from _ifconfig_getargs and removes pseudo 271147088Sbrooks# args such as DHCP and WPA. 272147088Sbrooksifconfig_getargs() 273147088Sbrooks{ 274197139Shrs local _tmpargs _arg _args 275197139Shrs _tmpargs=`_ifconfig_getargs $1 $2` 276147088Sbrooks if [ $? -eq 1 ]; then 277147088Sbrooks return 1 278147088Sbrooks fi 279147088Sbrooks _args= 280147088Sbrooks 281147088Sbrooks for _arg in $_tmpargs; do 282147088Sbrooks case $_arg in 283157706Sbrooks [Dd][Hh][Cc][Pp]) ;; 284157706Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) ;; 285157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 286157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;; 287157706Sbrooks [Ww][Pp][Aa]) ;; 288252748Srpaulo [Hh][Oo][Ss][Tt][Aa][Pp]) ;; 289147088Sbrooks *) 290147088Sbrooks _args="$_args $_arg" 291147088Sbrooks ;; 292147088Sbrooks esac 293147088Sbrooks done 294147088Sbrooks 295147088Sbrooks echo $_args 296147088Sbrooks} 297147088Sbrooks 298149401Sbrooks# autoif 299225560Sbrueffer# Returns 0 if the interface should be automatically configured at 300149401Sbrooks# boot time and 1 otherwise. 301149401Sbrooksautoif() 302149401Sbrooks{ 303197139Shrs local _tmpargs _arg 304149401Sbrooks _tmpargs=`_ifconfig_getargs $1` 305197139Shrs 306149401Sbrooks for _arg in $_tmpargs; do 307149401Sbrooks case $_arg in 308149401Sbrooks [Nn][Oo][Aa][Uu][Tt][Oo]) 309149401Sbrooks return 1 310149401Sbrooks ;; 311149401Sbrooks esac 312149401Sbrooks done 313197139Shrs 314149401Sbrooks return 0 315149401Sbrooks} 316149401Sbrooks 317147088Sbrooks# dhcpif if 318147088Sbrooks# Returns 0 if the interface is a DHCP interface and 1 otherwise. 319147088Sbrooksdhcpif() 320147088Sbrooks{ 321197139Shrs local _tmpargs _arg 322147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 323197139Shrs 324252768Sdelphij case $1 in 325252768Sdelphij lo[0-9]*|\ 326252768Sdelphij stf[0-9]*|\ 327252768Sdelphij faith[0-9]*|\ 328252768Sdelphij lp[0-9]*|\ 329252768Sdelphij sl[0-9]*) 330252768Sdelphij return 1 331252768Sdelphij ;; 332252768Sdelphij esac 333227910Sdelphij if noafif $1; then 334227910Sdelphij return 1 335227910Sdelphij fi 336227910Sdelphij 337147088Sbrooks for _arg in $_tmpargs; do 338147088Sbrooks case $_arg in 339147088Sbrooks [Dd][Hh][Cc][Pp]) 340147088Sbrooks return 0 341147088Sbrooks ;; 342157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 343157706Sbrooks return 0 344157706Sbrooks ;; 345157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 346157706Sbrooks return 0 347157706Sbrooks ;; 348147088Sbrooks esac 349147088Sbrooks done 350197139Shrs 351147088Sbrooks return 1 352147088Sbrooks} 353147088Sbrooks 354157706Sbrooks# syncdhcpif 355157706Sbrooks# Returns 0 if the interface should be configured synchronously and 356157706Sbrooks# 1 otherwise. 357157706Sbrookssyncdhcpif() 358157706Sbrooks{ 359197139Shrs local _tmpargs _arg 360157706Sbrooks _tmpargs=`_ifconfig_getargs $1` 361197139Shrs 362227910Sdelphij if noafif $1; then 363227910Sdelphij return 1 364227910Sdelphij fi 365227910Sdelphij 366157706Sbrooks for _arg in $_tmpargs; do 367157706Sbrooks case $_arg in 368157706Sbrooks [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 369157706Sbrooks return 1 370157706Sbrooks ;; 371157706Sbrooks [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 372157706Sbrooks return 0 373157706Sbrooks ;; 374157706Sbrooks esac 375157706Sbrooks done 376197139Shrs 377197139Shrs checkyesno synchronous_dhclient 378157706Sbrooks} 379157706Sbrooks 380147088Sbrooks# wpaif if 381147088Sbrooks# Returns 0 if the interface is a WPA interface and 1 otherwise. 382147088Sbrookswpaif() 383147088Sbrooks{ 384197139Shrs local _tmpargs _arg 385147088Sbrooks _tmpargs=`_ifconfig_getargs $1` 386197139Shrs 387147088Sbrooks for _arg in $_tmpargs; do 388147088Sbrooks case $_arg in 389147088Sbrooks [Ww][Pp][Aa]) 390147088Sbrooks return 0 391147088Sbrooks ;; 392147088Sbrooks esac 393147088Sbrooks done 394197139Shrs 395147088Sbrooks return 1 396147088Sbrooks} 397147088Sbrooks 398252748Srpaulo# hostapif if 399252748Srpaulo# Returns 0 if the interface is a HOSTAP interface and 1 otherwise. 400252748Srpaulohostapif() 401252748Srpaulo{ 402252748Srpaulo local _tmpargs _arg 403252748Srpaulo _tmpargs=`_ifconfig_getargs $1` 404252748Srpaulo 405252748Srpaulo for _arg in $_tmpargs; do 406252748Srpaulo case $_arg in 407252748Srpaulo [Hh][Oo][Ss][Tt][Aa][Pp]) 408252748Srpaulo return 0 409252748Srpaulo ;; 410252748Srpaulo esac 411252748Srpaulo done 412252748Srpaulo 413252748Srpaulo return 1 414252748Srpaulo} 415252748Srpaulo 416197139Shrs# afexists af 417197139Shrs# Returns 0 if the address family is enabled in the kernel 418197139Shrs# 1 otherwise. 419197139Shrsafexists() 420197139Shrs{ 421197139Shrs local _af 422197139Shrs _af=$1 423197139Shrs 424197139Shrs case ${_af} in 425222996Shrs inet|inet6) 426222996Shrs check_kern_features ${_af} 427197139Shrs ;; 428197697Shrs ipx) 429197697Shrs ${SYSCTL_N} net.ipx > /dev/null 2>&1 430197697Shrs ;; 431197697Shrs atm) 432197697Shrs if [ -x /sbin/atmconfig ]; then 433197697Shrs /sbin/atmconfig diag list > /dev/null 2>&1 434197697Shrs else 435197697Shrs return 1 436197697Shrs fi 437197697Shrs ;; 438253227Shrs link|ether) 439253227Shrs return 0 440253227Shrs ;; 441197139Shrs *) 442197139Shrs err 1 "afexists(): Unsupported address family: $_af" 443197139Shrs ;; 444197139Shrs esac 445197139Shrs} 446197139Shrs 447212574Shrs# noafif if 448212574Shrs# Returns 0 if the interface has no af configuration and 1 otherwise. 449212574Shrsnoafif() 450212574Shrs{ 451212574Shrs local _if 452212574Shrs _if=$1 453212574Shrs 454212574Shrs case $_if in 455212574Shrs pflog[0-9]*|\ 456212574Shrs pfsync[0-9]*|\ 457212574Shrs an[0-9]*|\ 458212574Shrs ath[0-9]*|\ 459212574Shrs ipw[0-9]*|\ 460212577Shrs ipfw[0-9]*|\ 461212574Shrs iwi[0-9]*|\ 462212574Shrs iwn[0-9]*|\ 463212574Shrs ral[0-9]*|\ 464212574Shrs wi[0-9]*|\ 465212574Shrs wl[0-9]*|\ 466212574Shrs wpi[0-9]*) 467212574Shrs return 0 468212574Shrs ;; 469212574Shrs esac 470212574Shrs 471212574Shrs return 1 472212574Shrs} 473212574Shrs 474162490Sbrooks# ipv6if if 475162490Sbrooks# Returns 0 if the interface should be configured for IPv6 and 476162490Sbrooks# 1 otherwise. 477162490Sbrooksipv6if() 478162490Sbrooks{ 479212574Shrs local _if _tmpargs i 480212574Shrs _if=$1 481212574Shrs 482197139Shrs if ! afexists inet6; then 483162490Sbrooks return 1 484162490Sbrooks fi 485197139Shrs 486197139Shrs # lo0 is always IPv6-enabled 487212574Shrs case $_if in 488197139Shrs lo0) 489197139Shrs return 0 490197139Shrs ;; 491197139Shrs esac 492197139Shrs 493212574Shrs case "${ipv6_network_interfaces}" in 494212575Shrs $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo]) 495212575Shrs # True if $ifconfig_IF_ipv6 is defined. 496212575Shrs _tmpargs=`_ifconfig_getargs $_if ipv6` 497212575Shrs if [ -n "${_tmpargs}" ]; then 498212575Shrs return 0 499212575Shrs fi 500197139Shrs 501212575Shrs # backward compatibility: True if $ipv6_ifconfig_IF is defined. 502212575Shrs _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 503212575Shrs if [ -n "${_tmpargs}" ]; then 504212574Shrs return 0 505212574Shrs fi 506212575Shrs ;; 507212575Shrs esac 508197139Shrs 509162490Sbrooks return 1 510162490Sbrooks} 511162490Sbrooks 512197139Shrs# ipv6_autoconfif if 513197139Shrs# Returns 0 if the interface should be configured for IPv6 with 514225560Sbrueffer# Stateless Address Configuration; 1 otherwise. 515197139Shrsipv6_autoconfif() 516197139Shrs{ 517197139Shrs local _if _tmpargs _arg 518197139Shrs _if=$1 519197139Shrs 520212577Shrs case $_if in 521252768Sdelphij lo[0-9]*|\ 522212577Shrs stf[0-9]*|\ 523212577Shrs faith[0-9]*|\ 524212577Shrs lp[0-9]*|\ 525212577Shrs sl[0-9]*) 526197139Shrs return 1 527212577Shrs ;; 528212577Shrs esac 529212574Shrs if noafif $_if; then 530212574Shrs return 1 531212574Shrs fi 532212577Shrs if ! ipv6if $_if; then 533212577Shrs return 1 534212577Shrs fi 535197139Shrs if checkyesno ipv6_gateway_enable; then 536197139Shrs return 1 537197139Shrs fi 538197526Shrs _tmpargs=`get_if_var $_if ipv6_prefix_IF` 539197526Shrs if [ -n "${_tmpargs}" ]; then 540197526Shrs return 1 541197526Shrs fi 542212574Shrs # backward compatibility: $ipv6_enable 543212574Shrs case $ipv6_enable in 544212577Shrs [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 545242187Shrs if checkyesno ipv6_gateway_enable; then 546242187Shrs return 1 547242187Shrs else 548242187Shrs return 0 549242187Shrs fi 550212577Shrs ;; 551197526Shrs esac 552197526Shrs 553212574Shrs _tmpargs=`_ifconfig_getargs $_if ipv6` 554212574Shrs for _arg in $_tmpargs; do 555212574Shrs case $_arg in 556212574Shrs accept_rtadv) 557212574Shrs return 0 558212574Shrs ;; 559212574Shrs esac 560212574Shrs done 561212574Shrs 562212574Shrs # backward compatibility: $ipv6_ifconfig_IF 563212574Shrs _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 564212574Shrs for _arg in $_tmpargs; do 565212574Shrs case $_arg in 566212574Shrs accept_rtadv) 567212574Shrs return 0 568212574Shrs ;; 569212574Shrs esac 570212574Shrs done 571212574Shrs 572197139Shrs return 1 573197139Shrs} 574197139Shrs 575161386Sbrooks# ifexists if 576161386Sbrooks# Returns 0 if the interface exists and 1 otherwise. 577161386Sbrooksifexists() 578161386Sbrooks{ 579197139Shrs [ -z "$1" ] && return 1 580253227Shrs ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1 581161386Sbrooks} 582161386Sbrooks 583152441Sbrooks# ipv4_up if 584212578Shrs# add IPv4 addresses to the interface $if 585152441Sbrooksipv4_up() 586152441Sbrooks{ 587197139Shrs local _if _ret 588152441Sbrooks _if=$1 589197139Shrs _ret=1 590197139Shrs 591222515Sbz # Add 127.0.0.1/8 to lo0 unless otherwise specified. 592222515Sbz if [ "${_if}" = "lo0" ]; then 593228245Shrs ifconfig_args=`get_if_var ${_if} ifconfig_IF` 594222515Sbz if [ -z "${ifconfig_args}" ]; then 595253227Shrs ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias 596222515Sbz fi 597222515Sbz fi 598253227Shrs ifalias ${_if} inet alias && _ret=0 599197139Shrs 600197139Shrs return $_ret 601152441Sbrooks} 602152441Sbrooks 603197139Shrs# ipv6_up if 604197139Shrs# add IPv6 addresses to the interface $if 605197139Shrsipv6_up() 606197139Shrs{ 607197139Shrs local _if _ret 608197139Shrs _if=$1 609197139Shrs _ret=1 610197139Shrs 611197139Shrs if ! ipv6if $_if; then 612197139Shrs return 0 613197139Shrs fi 614197139Shrs 615253227Shrs ifalias ${_if} inet6 alias && _ret=0 616228245Shrs ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0 617197139Shrs ipv6_accept_rtadv_up ${_if} && _ret=0 618197139Shrs 619197139Shrs # wait for DAD 620197139Shrs sleep `${SYSCTL_N} net.inet6.ip6.dad_count` 621197139Shrs sleep 1 622197139Shrs 623197139Shrs return $_ret 624197139Shrs} 625197139Shrs 626152441Sbrooks# ipv4_down if 627197147Shrs# remove IPv4 addresses from the interface $if 628152441Sbrooksipv4_down() 629152441Sbrooks{ 630197139Shrs local _if _ifs _ret inetList oldifs _inet 631152441Sbrooks _if=$1 632161386Sbrooks _ifs="^" 633161386Sbrooks _ret=1 634161386Sbrooks 635253227Shrs ifalias ${_if} inet -alias && _ret=0 636161386Sbrooks 637253227Shrs inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`" 638253227Shrs 639161386Sbrooks oldifs="$IFS" 640161386Sbrooks IFS="$_ifs" 641161386Sbrooks for _inet in $inetList ; do 642161386Sbrooks # get rid of extraneous line 643161386Sbrooks [ -z "$_inet" ] && break 644161386Sbrooks 645161386Sbrooks _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 646161386Sbrooks 647161386Sbrooks IFS="$oldifs" 648253227Shrs ${IFCONFIG_CMD} ${_if} ${_inet} delete 649161386Sbrooks IFS="$_ifs" 650161386Sbrooks _ret=0 651161386Sbrooks done 652161386Sbrooks IFS="$oldifs" 653161386Sbrooks 654161386Sbrooks return $_ret 655152441Sbrooks} 656152441Sbrooks 657197139Shrs# ipv6_down if 658197139Shrs# remove IPv6 addresses from the interface $if 659197139Shrsipv6_down() 660197139Shrs{ 661197139Shrs local _if _ifs _ret inetList oldifs _inet6 662197139Shrs _if=$1 663197139Shrs _ifs="^" 664197139Shrs _ret=1 665197139Shrs 666197139Shrs if ! ipv6if $_if; then 667197139Shrs return 0 668197139Shrs fi 669197139Shrs 670197139Shrs ipv6_accept_rtadv_down ${_if} && _ret=0 671228245Shrs ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0 672253227Shrs ifalias ${_if} inet6 -alias && _ret=0 673197139Shrs 674253227Shrs inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`" 675197139Shrs 676197139Shrs oldifs="$IFS" 677197139Shrs IFS="$_ifs" 678197139Shrs for _inet6 in $inetList ; do 679197139Shrs # get rid of extraneous line 680197139Shrs [ -z "$_inet6" ] && break 681197139Shrs 682197139Shrs _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'` 683197139Shrs 684197139Shrs IFS="$oldifs" 685253227Shrs ${IFCONFIG_CMD} ${_if} ${_inet6} -alias 686197139Shrs IFS="$_ifs" 687197139Shrs _ret=0 688197139Shrs done 689197139Shrs IFS="$oldifs" 690197139Shrs 691197139Shrs return $_ret 692197139Shrs} 693197139Shrs 694253227Shrs# ifalias if af action 695253227Shrs# Configure or remove aliases for network interface $if. 696113674Smtm# It returns 0 if at least one alias was configured or 697253227Shrs# removed, or 1 if there were none. 698113674Smtm# 699253227Shrsifalias() 700113674Smtm{ 701197139Shrs local _ret 702113674Smtm _ret=1 703197139Shrs 704253227Shrs afexists $2 || return $_ret 705253227Shrs 706197139Shrs case "$2" in 707253227Shrs inet|inet6|link|ether) 708253227Shrs ifalias_af_common $1 $2 $3 && _ret=0 709197139Shrs ;; 710197139Shrs esac 711197139Shrs 712197139Shrs return $_ret 713197139Shrs} 714197139Shrs 715253227Shrs# ifalias_expand_addr af action addr 716253227Shrs# Expand address range ("N-M") specification in addr. 717253227Shrs# "addr" must not include an address-family keyword. 718253227Shrs# The results will include an address-family keyword. 719197139Shrs# 720253227Shrsifalias_expand_addr() 721197139Shrs{ 722197139Shrs 723253227Shrs afexists $1 || return 724253227Shrs ifalias_expand_addr_$1 $2 $3 725197139Shrs} 726197139Shrs 727253227Shrs# ifalias_expand_addr_inet action addr 728253227Shrs# Helper function for ifalias_expand_addr(). Handles IPv4. 729197139Shrs# 730253227Shrsifalias_expand_addr_inet() 731197139Shrs{ 732253227Shrs local _action _arg _cidr _cidr_addr 733253227Shrs local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount 734253227Shrs local _retstr _c 735253227Shrs _action=$1 736253227Shrs _arg=$2 737253227Shrs _retstr= 738197139Shrs 739253227Shrs case $_action:$_arg in 740253227Shrs *:*--*) return ;; # invalid 741253227Shrs tmp:*) echo $_arg && return ;; # already expanded 742253227Shrs tmp:*-*) _action="alias" ;; # to be expanded 743253227Shrs *:*-*) ;; # to be expanded 744253227Shrs *:*) echo inet $_arg && return ;; # already expanded 745253227Shrs esac 746253227Shrs 747253227Shrs for _cidr in $_arg; do 748253227Shrs _ipaddr=${_cidr%%/*} 749253227Shrs _plen=${_cidr##*/} 750253227Shrs # When subnet prefix length is not specified, use /32. 751253227Shrs case $_plen in 752253227Shrs $_ipaddr) _plen=32 ;; # "/" character not found 753197139Shrs esac 754197139Shrs 755253227Shrs OIFS=$IFS 756253227Shrs IFS=. set -- $_ipaddr 757253227Shrs _range= 758253227Shrs _iphead= 759253227Shrs _iptail= 760253227Shrs for _c in $@; do 761253227Shrs case $_range:$_c in 762253227Shrs :[0-9]*-[0-9]*) 763253227Shrs _range=$_c 764197139Shrs ;; 765253227Shrs :*) 766253227Shrs _iphead="${_iphead}${_iphead:+.}${_c}" 767197139Shrs ;; 768253227Shrs *:*) 769253227Shrs _iptail="${_iptail}${_iptail:+.}${_c}" 770253227Shrs ;; 771253227Shrs esac 772253227Shrs done 773253227Shrs IFS=$OIFS 774253227Shrs _iplow=${_range%-*} 775253227Shrs _iphigh=${_range#*-} 776253227Shrs 777253227Shrs # clear netmask when removing aliases 778253227Shrs if [ "$_action" = "-alias" ]; then 779253227Shrs _plen="" 780253227Shrs fi 781253227Shrs 782253227Shrs _ipcount=$_iplow 783253227Shrs while [ "$_ipcount" -le "$_iphigh" ]; do 784253227Shrs _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}" 785253227Shrs if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then 786253227Shrs warn "Range specification is too large (${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_iphigh}${_iptail:+.}${_iptail}). ${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail} was processed." 787253227Shrs break 788253227Shrs else 789253227Shrs _ipcount=$(($_ipcount + 1)) 790253227Shrs fi 791253227Shrs # Forcibly set /32 for remaining aliases. 792253227Shrs _plen=32 793253227Shrs done 794197139Shrs done 795197139Shrs 796253227Shrs for _c in $_retstr; do 797253227Shrs ifalias_expand_addr_inet $_action $_c 798253227Shrs done 799113674Smtm} 800100280Sgordon 801253227Shrs# ifalias_expand_addr_inet6 action addr 802253227Shrs# Helper function for ifalias_expand_addr(). Handles IPv6. 803116029Smtm# 804253227Shrsifalias_expand_addr_inet6() 805116029Smtm{ 806253227Shrs local _action _arg _cidr _cidr_addr 807253227Shrs local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount 808253227Shrs local _ipv4part 809253227Shrs local _retstr _c 810253227Shrs _action=$1 811253227Shrs _arg=$2 812253227Shrs _retstr= 813197139Shrs 814253227Shrs case $_action:$_arg in 815253227Shrs *:*--*) return ;; # invalid 816253227Shrs tmp:*) echo $_arg && return ;; 817253227Shrs tmp:*-*) _action="alias" ;; 818253227Shrs *:*-*) ;; 819253227Shrs *:*) echo inet6 $_arg && return ;; 820197139Shrs esac 821197139Shrs 822253227Shrs for _cidr in $_arg; do 823253227Shrs _ipaddr="${_cidr%%/*}" 824253227Shrs _plen="${_cidr##*/}" 825253227Shrs 826253227Shrs case $_action:$_ipaddr:$_cidr in 827253227Shrs -alias:*:*) unset _plen ;; 828253227Shrs *:$_cidr:$_ipaddr) unset _plen ;; 829253227Shrs esac 830253227Shrs 831253227Shrs if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then 832253227Shrs # Handle !v4mapped && !v4compat addresses. 833253227Shrs 834253227Shrs # The default prefix length is 64. 835253227Shrs case $_ipaddr:$_cidr in 836253227Shrs $_cidr:$_ipaddr) _plen="64" ;; 837253227Shrs esac 838253227Shrs _ipleft=${_ipaddr%-*} 839253227Shrs _ipright=${_ipaddr#*-} 840253227Shrs _iplow=${_ipleft##*:} 841253227Shrs _iphigh=${_ipright%%:*} 842253227Shrs _ipleft=${_ipleft%:*} 843253227Shrs _ipright=${_ipright#*:} 844253227Shrs 845253227Shrs if [ "$_iphigh" = "$_ipright" ]; then 846253227Shrs unset _ipright 847253227Shrs else 848253227Shrs _ipright=:$_ipright 849253227Shrs fi 850253227Shrs 851253227Shrs if [ -n "$_iplow" -a -n "$_iphigh" ]; then 852253227Shrs _iplow=$((0x$_iplow)) 853253227Shrs _iphigh=$((0x$_iphigh)) 854253227Shrs _ipcount=$_iplow 855253227Shrs while [ $_ipcount -le $_iphigh ]; do 856253227Shrs _r=`printf "%s:%04x%s%s" \ 857253227Shrs $_ipleft $_ipcount $_ipright \ 858253227Shrs ${_plen:+/}$_plen` 859253227Shrs _retstr="$_retstr $_r" 860253227Shrs if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ] 861253227Shrs then 862253227Shrs warn "Range specification is too large $(printf '(%s:%04x%s-%s:%04x%s)' $_ipleft $_iplow $_ipright $_ipleft $_iphigh $_ipright). $(printf '%s:%04x%s-%s:%04x%s' $_ipleft $_iplow $_ipright $_ipleft $_ipcount $_ipright) was processed." 863253227Shrs break 864253227Shrs else 865253227Shrs _ipcount=$(($_ipcount + 1)) 866253227Shrs fi 867253227Shrs done 868253227Shrs else 869253227Shrs _retstr="${_ipaddr}${_plen:+/}${_plen}" 870253227Shrs fi 871253227Shrs 872253227Shrs for _c in $_retstr; do 873253227Shrs ifalias_expand_addr_inet6 $_action $_c 874253227Shrs done 875253227Shrs else 876253227Shrs # v4mapped/v4compat should handle as an IPv4 alias 877253227Shrs _ipv4part=${_ipaddr##*:} 878253227Shrs 879253227Shrs # Adjust prefix length if any. If not, set the 880253227Shrs # default prefix length as 32. 881253227Shrs case $_ipaddr:$_cidr in 882253227Shrs $_cidr:$_ipaddr) _plen=32 ;; 883253227Shrs *) _plen=$(($_plen - 96)) ;; 884253227Shrs esac 885253227Shrs 886253227Shrs _retstr=`ifalias_expand_addr_inet \ 887253227Shrs tmp ${_ipv4part}${_plen:+/}${_plen}` 888253227Shrs for _c in $_retstr; do 889253227Shrs ifalias_expand_addr_inet $_action $_c 890253227Shrs done 891253227Shrs fi 892253227Shrs done 893197139Shrs} 894197139Shrs 895253227Shrs# ifalias_af_common_handler if af action args 896253227Shrs# Helper function for ifalias_af_common(). 897197139Shrs# 898253227Shrsifalias_af_common_handler() 899197139Shrs{ 900253227Shrs local _ret _if _af _action _args _c _tmpargs 901253227Shrs 902197139Shrs _ret=1 903253227Shrs _if=$1 904253227Shrs _af=$2 905253227Shrs _action=$3 906253227Shrs shift 3 907253227Shrs _args=$* 908197139Shrs 909253227Shrs case $_args in 910253227Shrs ${_af}\ *) ;; 911253227Shrs *) return ;; 912253227Shrs esac 913253227Shrs 914253227Shrs # link(ether) does not support address removal. 915253227Shrs case $_af:$_action in 916253227Shrs link:-alias|ether:-alias) return ;; 917253227Shrs esac 918253227Shrs 919253227Shrs _tmpargs= 920253227Shrs for _c in $_args; do 921253227Shrs case $_c in 922253227Shrs ${_af}) 923253227Shrs case $_tmpargs in 924253227Shrs ${_af}\ *-*) 925253227Shrs ifalias_af_common_handler $_if $_af $_action \ 926253227Shrs `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 927197139Shrs ;; 928253227Shrs ${_af}\ *) 929253227Shrs ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 930197139Shrs ;; 931253227Shrs esac 932253227Shrs _tmpargs=$_af 933253227Shrs ;; 934253227Shrs *) 935253227Shrs _tmpargs="$_tmpargs $_c" 936253227Shrs ;; 937197139Shrs esac 938197139Shrs done 939253227Shrs # Process the last component if any. 940253227Shrs if [ -n "$_tmpargs}" ]; then 941253227Shrs case $_tmpargs in 942253227Shrs ${_af}\ *-*) 943253227Shrs ifalias_af_common_handler $_if $_af $_action \ 944253227Shrs `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 945253227Shrs ;; 946253227Shrs ${_af}\ *) 947253227Shrs ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 948253227Shrs ;; 949253227Shrs esac 950253227Shrs fi 951197139Shrs 952197139Shrs return $_ret 953197139Shrs} 954197139Shrs 955253227Shrs# ifalias_af_common if af action 956253227Shrs# Helper function for ifalias(). 957197139Shrs# 958253227Shrsifalias_af_common() 959197139Shrs{ 960253227Shrs local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf 961253227Shrs 962197139Shrs _ret=1 963253227Shrs _aliasn= 964253227Shrs _if=$1 965253227Shrs _af=$2 966253227Shrs _action=$3 967197139Shrs 968253227Shrs # ifconfig_IF_aliasN which starts with $_af 969197139Shrs alias=0 970197139Shrs while : ; do 971253227Shrs ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}` 972253227Shrs _iaf= 973253227Shrs case $ifconfig_args in 974253227Shrs inet\ *) _iaf=inet ;; 975253227Shrs inet6\ *) _iaf=inet6 ;; 976253227Shrs ipx\ *) _iaf=ipx ;; 977253227Shrs link\ *) _iaf=link ;; 978253227Shrs ether\ *) _iaf=ether ;; 979253227Shrs esac 980253227Shrs 981253227Shrs case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in 982253227Shrs ${_af}:*:${_af}:*) 983253227Shrs _aliasn="$_aliasn $ifconfig_args" 984197139Shrs ;; 985253227Shrs ${_af}:*:"":"") 986116029Smtm break 987197139Shrs ;; 988253227Shrs inet:alias:"":*) 989253227Shrs _aliasn="$_aliasn inet $ifconfig_args" 990253227Shrs warn "\$ifconfig_${_if}_alias${alias} needs " \ 991253227Shrs "\"inet\" keyword for an IPv4 address." 992197139Shrs esac 993253227Shrs alias=$(($alias + 1)) 994116029Smtm done 995197139Shrs 996197139Shrs # backward compatibility: ipv6_ifconfig_IF_aliasN. 997253227Shrs case $_af in 998253227Shrs inet6) 999253227Shrs alias=0 1000253227Shrs while : ; do 1001253227Shrs ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}` 1002253227Shrs case ${_action}:"${ifconfig_args}" in 1003253227Shrs *:"") 1004253227Shrs break 1005197139Shrs ;; 1006253227Shrs alias:*) 1007253227Shrs _aliasn="${_aliasn} inet6 ${ifconfig_args}" 1008253227Shrs warn "\$ipv6_ifconfig_${_if}_alias${alias} " \ 1009253227Shrs "is obsolete. Use ifconfig_$1_aliasN " \ 1010253227Shrs "instead." 1011253227Shrs ;; 1012253227Shrs esac 1013253227Shrs alias=$(($alias + 1)) 1014253227Shrs done 1015253227Shrs esac 1016253227Shrs 1017253227Shrs # backward compatibility: ipv4_addrs_IF. 1018253227Shrs for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do 1019253227Shrs _aliasn="$_aliasn inet $_tmpargs" 1020253227Shrs done 1021253227Shrs 1022253227Shrs # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others. 1023253227Shrs _tmpargs= 1024253227Shrs for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do 1025253227Shrs case $_c in 1026253227Shrs inet|inet6|ipx|link|ether) 1027253227Shrs case $_tmpargs in 1028253227Shrs ${_af}\ *) 1029253227Shrs eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1030253227Shrs ;; 1031253227Shrs esac 1032253227Shrs _tmpargs=$_c 1033253227Shrs ;; 1034197139Shrs *) 1035253227Shrs _tmpargs="$_tmpargs $_c" 1036197139Shrs esac 1037197139Shrs done 1038253227Shrs # Process the last component 1039253227Shrs case $_tmpargs in 1040253227Shrs ${_af}\ *) 1041253227Shrs ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1042253227Shrs ;; 1043253227Shrs esac 1044197139Shrs 1045116029Smtm return $_ret 1046116029Smtm} 1047116029Smtm 1048228245Shrs# ipv6_prefix_hostid_addr_common if action 1049228245Shrs# Add or remove IPv6 prefix + hostid addr on the interface $if 1050228245Shrs# 1051228245Shrsipv6_prefix_hostid_addr_common() 1052197139Shrs{ 1053228245Shrs local _if _action prefix laddr hostid j address 1054197139Shrs _if=$1 1055228245Shrs _action=$2 1056197139Shrs prefix=`get_if_var ${_if} ipv6_prefix_IF` 1057197139Shrs 1058197139Shrs if [ -n "${prefix}" ]; then 1059197139Shrs laddr=`network6_getladdr ${_if}` 1060197139Shrs hostid=${laddr#fe80::} 1061197139Shrs hostid=${hostid%\%*} 1062197139Shrs 1063197139Shrs for j in ${prefix}; do 1064253227Shrs # The default prefixlen is 64. 1065253227Shrs plen=${j#*/} 1066253227Shrs case $j:$plen in 1067253227Shrs $plen:$j) plen=64 ;; 1068253227Shrs *) j=${j%/*} ;; 1069253227Shrs esac 1070197139Shrs 1071253227Shrs # Normalize the last part by removing ":" 1072253227Shrs j=${j%:*} 1073253227Shrs j=${j%:} 1074253227Shrs OIFS=$IFS; IFS=":"; set -- $j; nj=$#; IFS=$OIFS 1075253227Shrs OIFS=$IFS; IFS=":"; set -- $hostid; nh=$#; IFS=$OIFS 1076253227Shrs if [ $(($nj + $nh)) -eq 8 ]; then 1077253227Shrs address=$j\:$hostid 1078253227Shrs else 1079253227Shrs address=$j\::$hostid 1080253227Shrs fi 1081253227Shrs 1082253227Shrs ${IFCONFIG_CMD} ${_if} inet6 ${address} \ 1083253227Shrs prefixlen $plen ${_action} 1084253227Shrs 1085197139Shrs # if I am a router, add subnet router 1086197139Shrs # anycast address (RFC 2373). 1087197139Shrs if checkyesno ipv6_gateway_enable; then 1088253227Shrs ${IFCONFIG_CMD} ${_if} inet6 $j:: \ 1089253227Shrs prefixlen $plen ${_action} anycast 1090197139Shrs fi 1091197139Shrs done 1092197139Shrs fi 1093197139Shrs} 1094197139Shrs 1095197139Shrs# ipv6_accept_rtadv_up if 1096197139Shrs# Enable accepting Router Advertisement and send Router 1097197139Shrs# Solicitation message 1098197139Shrsipv6_accept_rtadv_up() 1099197139Shrs{ 1100197139Shrs if ipv6_autoconfif $1; then 1101253227Shrs ${IFCONFIG_CMD} $1 inet6 accept_rtadv up 1102203433Sume if ! checkyesno rtsold_enable; then 1103203433Sume rtsol ${rtsol_flags} $1 1104203433Sume fi 1105197139Shrs fi 1106197139Shrs} 1107197139Shrs 1108197139Shrs# ipv6_accept_rtadv_down if 1109197139Shrs# Disable accepting Router Advertisement 1110197139Shrsipv6_accept_rtadv_down() 1111197139Shrs{ 1112197139Shrs if ipv6_autoconfif $1; then 1113253227Shrs ${IFCONFIG_CMD} $1 inet6 -accept_rtadv 1114197139Shrs fi 1115197139Shrs} 1116197139Shrs 1117113674Smtm# ifscript_up if 1118113674Smtm# Evaluate a startup script for the $if interface. 1119113674Smtm# It returns 0 if a script was found and processed or 1120113674Smtm# 1 if no script was found. 1121113674Smtm# 1122113674Smtmifscript_up() 1123100280Sgordon{ 1124113674Smtm if [ -r /etc/start_if.$1 ]; then 1125113674Smtm . /etc/start_if.$1 1126113674Smtm return 0 1127197139Shrs else 1128197139Shrs return 1 1129113674Smtm fi 1130100280Sgordon} 1131100280Sgordon 1132116029Smtm# ifscript_down if 1133116029Smtm# Evaluate a shutdown script for the $if interface. 1134116029Smtm# It returns 0 if a script was found and processed or 1135116029Smtm# 1 if no script was found. 1136116029Smtm# 1137116029Smtmifscript_down() 1138116029Smtm{ 1139116029Smtm if [ -r /etc/stop_if.$1 ]; then 1140116029Smtm . /etc/stop_if.$1 1141116029Smtm return 0 1142197139Shrs else 1143197139Shrs return 1 1144116029Smtm fi 1145116029Smtm} 1146116029Smtm 1147197147Shrs# clone_up 1148197147Shrs# Create cloneable interfaces. 1149113674Smtm# 1150113674Smtmclone_up() 1151100280Sgordon{ 1152197139Shrs local _prefix _list ifn 1153113674Smtm _prefix= 1154113674Smtm _list= 1155197139Shrs 1156197139Shrs # create_args_IF 1157113674Smtm for ifn in ${cloned_interfaces}; do 1158253227Shrs ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF` 1159116774Skuriyama if [ $? -eq 0 ]; then 1160113674Smtm _list="${_list}${_prefix}${ifn}" 1161113674Smtm [ -z "$_prefix" ] && _prefix=' ' 1162113674Smtm fi 1163113674Smtm done 1164113674Smtm debug "Cloned: ${_list}" 1165113674Smtm} 1166100280Sgordon 1167197147Shrs# clone_down 1168197147Shrs# Destroy cloned interfaces. Destroyed interfaces are echoed to 1169197147Shrs# standard output. 1170113674Smtm# 1171113674Smtmclone_down() 1172113674Smtm{ 1173197139Shrs local _prefix _list ifn 1174113674Smtm _prefix= 1175113674Smtm _list= 1176197139Shrs 1177113674Smtm for ifn in ${cloned_interfaces}; do 1178253227Shrs ${IFCONFIG_CMD} -n ${ifn} destroy 1179116774Skuriyama if [ $? -eq 0 ]; then 1180113674Smtm _list="${_list}${_prefix}${ifn}" 1181113674Smtm [ -z "$_prefix" ] && _prefix=' ' 1182113674Smtm fi 1183113674Smtm done 1184113674Smtm debug "Destroyed clones: ${_list}" 1185100280Sgordon} 1186100280Sgordon 1187197147Shrs# childif_create 1188197147Shrs# Create and configure child interfaces. Return 0 if child 1189197147Shrs# interfaces are created. 1190178356Ssam# 1191178356Ssamchildif_create() 1192178356Ssam{ 1193201215Sjhb local cfg child child_vlans child_wlans create_args debug_flags ifn i 1194178356Ssam cfg=1 1195178356Ssam ifn=$1 1196178356Ssam 1197178527Sbrooks # Create wireless interfaces 1198178527Sbrooks child_wlans=`get_if_var $ifn wlans_IF` 1199178527Sbrooks 1200178527Sbrooks for child in ${child_wlans}; do 1201183517Sbrooks create_args="wlandev $ifn `get_if_var $child create_args_IF`" 1202189759Sbrooks debug_flags="`get_if_var $child wlandebug_IF`" 1203189759Sbrooks 1204178356Ssam if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 1205253227Shrs ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1206189759Sbrooks if [ -n "${debug_flags}" ]; then 1207189759Sbrooks wlandebug -i $child ${debug_flags} 1208189759Sbrooks fi 1209178356Ssam else 1210253227Shrs i=`${IFCONFIG_CMD} wlan create ${create_args}` 1211189759Sbrooks if [ -n "${debug_flags}" ]; then 1212189759Sbrooks wlandebug -i $i ${debug_flags} 1213189759Sbrooks fi 1214253227Shrs ${IFCONFIG_CMD} $i name $child && cfg=0 1215178356Ssam fi 1216188118Sthompsa if autoif $child; then 1217188118Sthompsa ifn_start $child 1218188118Sthompsa fi 1219178356Ssam done 1220178356Ssam 1221201215Sjhb # Create vlan interfaces 1222201215Sjhb child_vlans=`get_if_var $ifn vlans_IF` 1223201215Sjhb 1224201215Sjhb if [ -n "${child_vlans}" ]; then 1225201215Sjhb load_kld if_vlan 1226201215Sjhb fi 1227201215Sjhb 1228201215Sjhb for child in ${child_vlans}; do 1229201215Sjhb if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1230201215Sjhb child="${ifn}.${child}" 1231201215Sjhb create_args=`get_if_var $child create_args_IF` 1232253227Shrs ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1233201215Sjhb else 1234201215Sjhb create_args="vlandev $ifn `get_if_var $child create_args_IF`" 1235201215Sjhb if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then 1236253227Shrs ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1237201215Sjhb else 1238253227Shrs i=`${IFCONFIG_CMD} vlan create ${create_args}` 1239253227Shrs ${IFCONFIG_CMD} $i name $child && cfg=0 1240201215Sjhb fi 1241201215Sjhb fi 1242201215Sjhb if autoif $child; then 1243201215Sjhb ifn_start $child 1244201215Sjhb fi 1245201215Sjhb done 1246201215Sjhb 1247179001Sbrooks return ${cfg} 1248178356Ssam} 1249178356Ssam 1250197147Shrs# childif_destroy 1251197147Shrs# Destroy child interfaces. 1252178356Ssam# 1253178356Ssamchildif_destroy() 1254178356Ssam{ 1255201215Sjhb local cfg child child_vlans child_wlans ifn 1256197139Shrs cfg=1 1257178356Ssam 1258201216Sjhb child_wlans=`get_if_var $ifn wlans_IF` 1259178527Sbrooks for child in ${child_wlans}; do 1260201215Sjhb if ! ifexists $child; then 1261201215Sjhb continue 1262201215Sjhb fi 1263253227Shrs ${IFCONFIG_CMD} -n $child destroy && cfg=0 1264178356Ssam done 1265197139Shrs 1266201215Sjhb child_vlans=`get_if_var $ifn vlans_IF` 1267201215Sjhb for child in ${child_vlans}; do 1268201215Sjhb if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1269201215Sjhb child="${ifn}.${child}" 1270201215Sjhb fi 1271201215Sjhb if ! ifexists $child; then 1272201215Sjhb continue 1273201215Sjhb fi 1274253227Shrs ${IFCONFIG_CMD} -n $child destroy && cfg=0 1275201215Sjhb done 1276201215Sjhb 1277197139Shrs return ${cfg} 1278178356Ssam} 1279178356Ssam 1280197147Shrs# ng_mkpeer 1281197147Shrs# Create netgraph nodes. 1282166583Sflz# 1283197147Shrsng_mkpeer() 1284197147Shrs{ 1285166583Sflz ngctl -f - 2> /dev/null <<EOF 1286166583Sflzmkpeer $* 1287166583Sflzmsg dummy nodeinfo 1288166583SflzEOF 1289166583Sflz} 1290166583Sflz 1291197147Shrs# ng_create_one 1292197147Shrs# Create netgraph nodes. 1293197147Shrs# 1294197147Shrsng_create_one() 1295197147Shrs{ 1296197139Shrs local t 1297197139Shrs 1298166583Sflz ng_mkpeer $* | while read line; do 1299166583Sflz t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 1300166583Sflz if [ -n "${t}" ]; then 1301166583Sflz echo ${t} 1302166583Sflz return 1303166583Sflz fi 1304166583Sflz done 1305166583Sflz} 1306166583Sflz 1307197147Shrs# gif_up 1308197147Shrs# Create gif(4) tunnel interfaces. 1309197147Shrsgif_up() 1310197147Shrs{ 1311197139Shrs local i peers 1312197139Shrs 1313166583Sflz for i in ${gif_interfaces}; do 1314166583Sflz peers=`get_if_var $i gifconfig_IF` 1315166583Sflz case ${peers} in 1316166583Sflz '') 1317166583Sflz continue 1318166583Sflz ;; 1319166583Sflz *) 1320177682Sbrooks if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then 1321253227Shrs ${IFCONFIG_CMD} $i create >/dev/null 2>&1 1322177682Sbrooks else 1323253227Shrs gif=`${IFCONFIG_CMD} gif create` 1324253227Shrs ${IFCONFIG_CMD} $gif name $i 1325177682Sbrooks fi 1326253227Shrs ${IFCONFIG_CMD} $i tunnel ${peers} 1327253227Shrs ${IFCONFIG_CMD} $i up 1328166583Sflz ;; 1329166583Sflz esac 1330166583Sflz done 1331166583Sflz} 1332166583Sflz 1333166583Sflz# ng_fec_create ifn 1334197147Shrs# Configure Fast EtherChannel for interface $ifn. Returns 0 if 1335197147Shrs# FEC arguments were found and configured; returns !0 otherwise. 1336197139Shrsng_fec_create() 1337197139Shrs{ 1338166583Sflz local req_iface iface bogus 1339166583Sflz req_iface="$1" 1340166583Sflz 1341166583Sflz ngctl shutdown ${req_iface}: > /dev/null 2>&1 1342166583Sflz 1343166583Sflz bogus="" 1344166583Sflz while true; do 1345166583Sflz iface=`ng_create_one fec dummy fec` 1346166583Sflz if [ -z "${iface}" ]; then 1347166583Sflz exit 2 1348166583Sflz fi 1349166583Sflz if [ "${iface}" = "${req_iface}" ]; then 1350166583Sflz break 1351166583Sflz fi 1352166583Sflz bogus="${bogus} ${iface}" 1353166583Sflz done 1354166583Sflz 1355166583Sflz for iface in ${bogus}; do 1356166583Sflz ngctl shutdown ${iface}: 1357166583Sflz done 1358166583Sflz} 1359166583Sflz 1360197147Shrs# fec_up 1361197147Shrs# Create Fast EtherChannel interfaces. 1362197147Shrsfec_up() 1363197147Shrs{ 1364197139Shrs local i j 1365197139Shrs 1366166583Sflz for i in ${fec_interfaces}; do 1367166583Sflz ng_fec_create $i 1368166583Sflz for j in `get_if_var $i fecconfig_IF`; do 1369166583Sflz case ${j} in 1370100282Sdougb '') 1371100282Sdougb continue 1372100282Sdougb ;; 1373100282Sdougb *) 1374166583Sflz ngctl msg ${i}: add_iface "\"${j}\"" 1375100282Sdougb ;; 1376100282Sdougb esac 1377100282Sdougb done 1378166583Sflz done 1379100282Sdougb} 1380100282Sdougb 1381113674Smtm# ipx_up ifn 1382197147Shrs# Configure any IPX addresses for interface $ifn. Returns 0 if 1383197147Shrs# IPX arguments were found and configured; returns 1 otherwise. 1384113674Smtm# 1385113674Smtmipx_up() 1386100280Sgordon{ 1387197139Shrs local ifn 1388113674Smtm ifn="$1" 1389197139Shrs 1390197139Shrs # ifconfig_IF_ipx 1391197139Shrs ifconfig_args=`_ifconfig_getargs $ifn ipx` 1392113674Smtm if [ -n "${ifconfig_args}" ]; then 1393253227Shrs ${IFCONFIG_CMD} ${ifn} ${ifconfig_args} 1394113674Smtm return 0 139585831Sdes fi 1396197139Shrs 1397113674Smtm return 1 1398113674Smtm} 139985831Sdes 1400116029Smtm# ipx_down ifn 1401116029Smtm# Remove IPX addresses for interface $ifn. Returns 0 if IPX 1402116029Smtm# addresses were found and unconfigured. It returns 1, otherwise. 1403113674Smtm# 1404116029Smtmipx_down() 1405116029Smtm{ 1406197139Shrs local _if _ifs _ret ipxList oldifs _ipx 1407197139Shrs _if=$1 1408116100Smtm _ifs="^" 1409116100Smtm _ret=1 1410253227Shrs ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`" 1411197139Shrs oldifs="$IFS" 1412116100Smtm 1413116100Smtm IFS="$_ifs" 1414116100Smtm for _ipx in $ipxList ; do 1415116100Smtm # get rid of extraneous line 1416116100Smtm [ -z "$_ipx" ] && break 1417116100Smtm 1418116100Smtm _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 1419116100Smtm 1420116100Smtm IFS="$oldifs" 1421253227Shrs ${IFCONFIG_CMD} ${_if} ${_ipx} delete 1422116100Smtm IFS="$_ifs" 1423116100Smtm _ret=0 1424116100Smtm done 1425116100Smtm IFS="$oldifs" 1426116100Smtm 1427116100Smtm return $_ret 1428116029Smtm} 1429116029Smtm 1430137070Spjd# ifnet_rename 1431137070Spjd# Rename all requested interfaces. 1432116029Smtm# 1433137070Spjdifnet_rename() 1434137070Spjd{ 1435197139Shrs local _if _ifname 1436137070Spjd 1437197139Shrs # ifconfig_IF_name 1438253227Shrs for _if in `${IFCONFIG_CMD} -l`; do 1439157706Sbrooks _ifname=`get_if_var $_if ifconfig_IF_name` 1440137070Spjd if [ ! -z "$_ifname" ]; then 1441253227Shrs ${IFCONFIG_CMD} $_if name $_ifname 1442137070Spjd fi 1443137070Spjd done 1444197139Shrs 1445137070Spjd return 0 1446137070Spjd} 1447137070Spjd 1448113674Smtm# list_net_interfaces type 1449113674Smtm# List all network interfaces. The type of interface returned 1450113674Smtm# can be controlled by the type argument. The type 1451113674Smtm# argument can be any of the following: 1452197147Shrs# nodhcp - all interfaces, excluding DHCP configured interfaces 1453197147Shrs# dhcp - list only DHCP configured interfaces 1454197139Shrs# noautoconf - all interfaces, excluding IPv6 Stateless 1455197139Shrs# Address Autoconf configured interfaces 1456197139Shrs# autoconf - list only IPv6 Stateless Address Autoconf 1457197139Shrs# configured interfaces 1458113674Smtm# If no argument is specified all network interfaces are output. 1459134429Syar# Note that the list will include cloned interfaces if applicable. 1460134429Syar# Cloned interfaces must already exist to have a chance to appear 1461134429Syar# in the list if ${network_interfaces} is set to `auto'. 1462113674Smtm# 1463113674Smtmlist_net_interfaces() 1464113674Smtm{ 1465197139Shrs local type _tmplist _list _autolist _lo _if 1466113674Smtm type=$1 146765532Snectar 1468149726Sbrooks # Get a list of ALL the interfaces and make lo0 first if it's there. 146951231Ssheldonh # 1470197139Shrs _tmplist= 147151231Ssheldonh case ${network_interfaces} in 147251231Ssheldonh [Aa][Uu][Tt][Oo]) 1473253227Shrs _autolist="`${IFCONFIG_CMD} -l`" 1474149726Sbrooks _lo= 1475149401Sbrooks for _if in ${_autolist} ; do 1476149401Sbrooks if autoif $_if; then 1477149726Sbrooks if [ "$_if" = "lo0" ]; then 1478149726Sbrooks _lo="lo0 " 1479149726Sbrooks else 1480197139Shrs _tmplist="${_tmplist} ${_if}" 1481149726Sbrooks fi 1482149401Sbrooks fi 1483149401Sbrooks done 1484197139Shrs _tmplist="${_lo}${_tmplist# }" 148551231Ssheldonh ;; 148683677Sbrooks *) 1487149401Sbrooks _tmplist="${network_interfaces} ${cloned_interfaces}" 1488196478Sdougb 1489196478Sdougb # lo0 is effectively mandatory, so help prevent foot-shooting 1490196478Sdougb # 1491196478Sdougb case "$_tmplist" in 1492196523Sdougb lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing 1493196478Sdougb *) _tmplist="lo0 ${_tmplist}" ;; 1494196478Sdougb esac 149583677Sbrooks ;; 149651231Ssheldonh esac 149749122Sbrian 1498197139Shrs _list= 1499197139Shrs case "$type" in 1500197139Shrs nodhcp) 1501197139Shrs for _if in ${_tmplist} ; do 1502197139Shrs if ! dhcpif $_if && \ 1503197139Shrs [ -n "`_ifconfig_getargs $_if`" ]; then 1504197139Shrs _list="${_list# } ${_if}" 1505197147Shrs fi 1506197139Shrs done 1507197139Shrs ;; 1508197139Shrs dhcp) 1509197147Shrs for _if in ${_tmplist} ; do 1510197147Shrs if dhcpif $_if; then 1511197139Shrs _list="${_list# } ${_if}" 1512197147Shrs fi 1513197147Shrs done 1514113674Smtm ;; 1515197139Shrs noautoconf) 1516197139Shrs for _if in ${_tmplist} ; do 1517197139Shrs if ! ipv6_autoconfif $_if && \ 1518197139Shrs [ -n "`_ifconfig_getargs $_if ipv6`" ]; then 1519197139Shrs _list="${_list# } ${_if}" 1520197139Shrs fi 1521197139Shrs done 1522113674Smtm ;; 1523197139Shrs autoconf) 1524197139Shrs for _if in ${_tmplist} ; do 1525197139Shrs if ipv6_autoconfif $_if; then 1526197139Shrs _list="${_list# } ${_if}" 1527197139Shrs fi 1528197139Shrs done 1529197139Shrs ;; 1530197139Shrs *) 1531197139Shrs _list=${_tmplist} 1532197139Shrs ;; 1533113674Smtm esac 1534197139Shrs 1535197139Shrs echo $_list 1536197139Shrs 1537130151Sschweikh return 0 153825184Sjkh} 1539114942Sume 1540179003Sbrooks# get_default_if -address_family 1541179003Sbrooks# Get the interface of the default route for the given address family. 1542179003Sbrooks# The -address_family argument must be suitable passing to route(8). 1543179003Sbrooks# 1544179003Sbrooksget_default_if() 1545179003Sbrooks{ 1546197139Shrs local routeget oldifs defif line 1547197139Shrs defif= 1548179003Sbrooks oldifs="$IFS" 1549179003Sbrooks IFS=" 1550179003Sbrooks" 1551197139Shrs for line in `route -n get $1 default 2>/dev/null`; do 1552179003Sbrooks case $line in 1553179003Sbrooks *interface:*) 1554179003Sbrooks defif=${line##*: } 1555179003Sbrooks ;; 1556179003Sbrooks esac 1557179003Sbrooks done 1558179003Sbrooks IFS=${oldifs} 1559179003Sbrooks 1560179003Sbrooks echo $defif 1561179003Sbrooks} 1562179003Sbrooks 1563197147Shrs# hexdigit arg 1564197147Shrs# Echo decimal number $arg (single digit) in hexadecimal format. 1565114942Sumehexdigit() 1566114942Sume{ 1567221884Sjilles printf '%x\n' "$1" 1568114942Sume} 1569114942Sume 1570197147Shrs# hexprint arg 1571197147Shrs# Echo decimal number $arg (multiple digits) in hexadecimal format. 1572114942Sumehexprint() 1573114942Sume{ 1574221884Sjilles printf '%x\n' "$1" 1575114942Sume} 1576114942Sume 1577196436Sdougbis_wired_interface() 1578196436Sdougb{ 1579196436Sdougb local media 1580196436Sdougb 1581253227Shrs case `${IFCONFIG_CMD} $1 2>/dev/null` in 1582196436Sdougb *media:?Ethernet*) media=Ethernet ;; 1583196436Sdougb esac 1584196436Sdougb 1585196436Sdougb test "$media" = "Ethernet" 1586196436Sdougb} 1587196436Sdougb 1588197147Shrs# network6_getladdr if [flag] 1589197147Shrs# Echo link-local address from $if if any. 1590197147Shrs# If flag is defined, tentative ones will be excluded. 1591114942Sumenetwork6_getladdr() 1592114942Sume{ 1593253227Shrs local _if _flag proto addr rest 1594253227Shrs _if=$1 1595253227Shrs _flag=$2 1596253227Shrs 1597253227Shrs ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do 1598253227Shrs case "${proto}/${addr}/${_flag}/${rest}" in 1599253227Shrs inet6/fe80::*//*) 1600253227Shrs echo ${addr} 1601253227Shrs ;; 1602253227Shrs inet6/fe80:://*tentative*) # w/o flag 1603253227Shrs sleep `${SYSCTL_N} net.inet6.ip6.dad_count` 1604253227Shrs network6_getladdr $_if $_flags 1605253227Shrs ;; 1606253227Shrs inet6/fe80::/*/*tentative*) # w/ flag 1607253227Shrs echo ${addr} 1608253227Shrs ;; 1609253227Shrs *) 1610253227Shrs continue 1611253227Shrs ;; 1612114942Sume esac 1613253227Shrs 1614253227Shrs return 1615114942Sume done 1616114942Sume} 1617