network.subr revision 269035
1# 2# Copyright (c) 2003 The FreeBSD Project. All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions 6# are met: 7# 1. Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# 2. Redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution. 12# 13# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23# SUCH DAMAGE. 24# 25# $FreeBSD: stable/10/etc/network.subr 269035 2014-07-23 22:47:00Z dteske $ 26# 27IFCONFIG_CMD="/sbin/ifconfig" 28 29# Maximum number of addresses expanded from a address range specification. 30_IPEXPANDMAX=31 31 32# 33# Subroutines commonly used from network startup scripts. 34# Requires that rc.conf be loaded first. 35# 36 37# ifn_start ifn 38# Bring up and configure an interface. If some configuration is 39# applied, print the interface configuration. 40# 41ifn_start() 42{ 43 local ifn cfg 44 ifn="$1" 45 cfg=1 46 47 [ -z "$ifn" ] && err 1 "ifn_start called without an interface" 48 49 ifscript_up ${ifn} && cfg=0 50 ifconfig_up ${ifn} && cfg=0 51 if ! noafif $ifn; then 52 afexists inet && ipv4_up ${ifn} && cfg=0 53 afexists inet6 && ipv6_up ${ifn} && cfg=0 54 afexists ipx && ipx_up ${ifn} && cfg=0 55 fi 56 childif_create ${ifn} && cfg=0 57 58 return $cfg 59} 60 61# ifn_stop ifn 62# Shutdown and de-configure an interface. If action is taken, 63# print the interface name. 64# 65ifn_stop() 66{ 67 local ifn cfg 68 ifn="$1" 69 cfg=1 70 71 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface" 72 73 if ! noafif $ifn; then 74 afexists ipx && ipx_down ${ifn} && cfg=0 75 afexists inet6 && ipv6_down ${ifn} && cfg=0 76 afexists inet && ipv4_down ${ifn} && cfg=0 77 fi 78 ifconfig_down ${ifn} && cfg=0 79 ifscript_down ${ifn} && cfg=0 80 childif_destroy ${ifn} && cfg=0 81 82 return $cfg 83} 84 85# ifn_vnetup ifn 86# Move ifn to the specified vnet jail. 87# 88ifn_vnetup() 89{ 90 91 ifn_vnet0 $1 vnet 92} 93 94# ifn_vnetdown ifn 95# Reclaim ifn from the specified vnet jail. 96# 97ifn_vnetdown() 98{ 99 100 ifn_vnet0 $1 -vnet 101} 102 103# ifn_vnet0 ifn action 104# Helper function for ifn_vnetup and ifn_vnetdown. 105# 106ifn_vnet0() 107{ 108 local _ifn _cfg _action _vnet 109 _ifn="$1" 110 _action="$2" 111 _cfg=1 112 113 if _vnet=$(vnetif $_ifn); then 114 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0 115 fi 116 117 return $_cfg 118} 119 120# ifconfig_up if 121# Evaluate ifconfig(8) arguments for interface $if and 122# run ifconfig(8) with those arguments. It returns 0 if 123# arguments were found and executed or 1 if the interface 124# had no arguments. Pseudo arguments DHCP and WPA are handled 125# here. 126# 127ifconfig_up() 128{ 129 local _cfg _ipv6_opts ifconfig_args 130 _cfg=1 131 132 # Make sure lo0 always comes up. 133 if [ "$1" = "lo0" ]; then 134 _cfg=0 135 fi 136 137 # ifconfig_IF 138 ifconfig_args=`ifconfig_getargs $1` 139 if [ -n "${ifconfig_args}" ]; then 140 eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 141 _cfg=0 142 fi 143 144 # inet6 specific 145 if ! noafif $1 && afexists inet6; then 146 if checkyesno ipv6_activate_all_interfaces; then 147 _ipv6_opts="-ifdisabled" 148 elif [ "$1" != "lo0" ]; then 149 _ipv6_opts="ifdisabled" 150 fi 151 152 # backward compatibility: $ipv6_enable 153 case $ipv6_enable in 154 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 155 case $1 in 156 bridge[0-9]*) 157 # No accept_rtadv by default on if_bridge(4) 158 # to avoid a conflict with the member 159 # interfaces. 160 ;; 161 *) 162 if ! checkyesno ipv6_gateway_enable; then 163 _ipv6_opts="${_ipv6_opts} accept_rtadv" 164 fi 165 ;; 166 esac 167 ;; 168 esac 169 170 case $ipv6_cpe_wanif in 171 $1) 172 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv" 173 ;; 174 esac 175 176 if [ -n "${_ipv6_opts}" ]; then 177 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts} 178 fi 179 180 # ifconfig_IF_ipv6 181 ifconfig_args=`ifconfig_getargs $1 ipv6` 182 if [ -n "${ifconfig_args}" ]; then 183 # backward compatibility: inet6 keyword 184 case "${ifconfig_args}" in 185 :*|[0-9a-fA-F]*:*) 186 warn "\$ifconfig_$1_ipv6 needs leading" \ 187 "\"inet6\" keyword for an IPv6 address." 188 ifconfig_args="inet6 ${ifconfig_args}" 189 ;; 190 esac 191 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 192 eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 193 _cfg=0 194 fi 195 196 # $ipv6_prefix_IF will be handled in 197 # ipv6_prefix_hostid_addr_common(). 198 ifconfig_args=`get_if_var $1 ipv6_prefix_IF` 199 if [ -n "${ifconfig_args}" ]; then 200 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 201 _cfg=0 202 fi 203 204 # backward compatibility: $ipv6_ifconfig_IF 205 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF` 206 if [ -n "${ifconfig_args}" ]; then 207 warn "\$ipv6_ifconfig_$1 is obsolete." \ 208 " Use ifconfig_$1_ipv6 instead." 209 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 210 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args} 211 _cfg=0 212 fi 213 fi 214 215 ifalias $1 link alias 216 ifalias $1 ether alias 217 218 if [ ${_cfg} -eq 0 ]; then 219 ${IFCONFIG_CMD} $1 up 220 fi 221 222 if wpaif $1; then 223 /etc/rc.d/wpa_supplicant start $1 224 _cfg=0 # XXX: not sure this should count 225 elif hostapif $1; then 226 /etc/rc.d/hostapd start $1 227 _cfg=0 228 fi 229 230 if dhcpif $1; then 231 if [ $_cfg -ne 0 ] ; then 232 ${IFCONFIG_CMD} $1 up 233 fi 234 if syncdhcpif $1; then 235 /etc/rc.d/dhclient start $1 236 fi 237 _cfg=0 238 fi 239 240 return $_cfg 241} 242 243# ifconfig_down if 244# returns 1 if wpa_supplicant or dhclient was stopped or 245# the interface exists. 246# 247ifconfig_down() 248{ 249 local _cfg 250 _cfg=1 251 252 if wpaif $1; then 253 /etc/rc.d/wpa_supplicant stop $1 254 _cfg=0 255 elif hostapif $1; then 256 /etc/rc.d/hostapd stop $1 257 _cfg=0 258 fi 259 260 if dhcpif $1; then 261 /etc/rc.d/dhclient stop $1 262 _cfg=0 263 fi 264 265 if ifexists $1; then 266 ${IFCONFIG_CMD} $1 down 267 _cfg=0 268 fi 269 270 return $_cfg 271} 272 273# get_if_var if var [default] 274# Return the value of the pseudo-hash corresponding to $if where 275# $var is a string containg the sub-string "IF" which will be 276# replaced with $if after the characters defined in _punct are 277# replaced with '_'. If the variable is unset, replace it with 278# $default if given. 279get_if_var() 280{ 281 local _if _punct _punct_c _var _default prefix suffix 282 283 if [ $# -ne 2 -a $# -ne 3 ]; then 284 err 3 'USAGE: get_if_var name var [default]' 285 fi 286 287 _if=$1 288 _punct=".-/+" 289 ltr ${_if} "${_punct}" '_' _if 290 _var=$2 291 _default=$3 292 293 prefix=${_var%%IF*} 294 suffix=${_var##*IF} 295 eval echo \${${prefix}${_if}${suffix}-${_default}} 296} 297 298# _ifconfig_getargs if [af] 299# Prints the arguments for the supplied interface to stdout. 300# Returns 1 if empty. In general, ifconfig_getargs should be used 301# outside this file. 302_ifconfig_getargs() 303{ 304 local _ifn _af 305 _ifn=$1 306 _af=${2+_$2} 307 308 if [ -z "$_ifn" ]; then 309 return 1 310 fi 311 312 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT" 313} 314 315# ifconfig_getargs if [af] 316# Takes the result from _ifconfig_getargs and removes pseudo 317# args such as DHCP and WPA. 318ifconfig_getargs() 319{ 320 local _tmpargs _arg _args _vnet 321 _tmpargs=`_ifconfig_getargs $1 $2` 322 if [ $? -eq 1 ]; then 323 return 1 324 fi 325 _args= 326 _vnet=0 327 328 for _arg in $_tmpargs; do 329 case $_arg:$_vnet in 330 [Dd][Hh][Cc][Pp]:0) ;; 331 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;; 332 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;; 333 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;; 334 [Ww][Pp][Aa]:0) ;; 335 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;; 336 vnet:0) _vnet=1 ;; 337 *:1) _vnet=0 ;; 338 *:0) 339 _args="$_args $_arg" 340 ;; 341 esac 342 done 343 344 echo $_args 345} 346 347# autoif 348# Returns 0 if the interface should be automatically configured at 349# boot time and 1 otherwise. 350autoif() 351{ 352 local _tmpargs _arg 353 _tmpargs=`_ifconfig_getargs $1` 354 355 for _arg in $_tmpargs; do 356 case $_arg in 357 [Nn][Oo][Aa][Uu][Tt][Oo]) 358 return 1 359 ;; 360 esac 361 done 362 363 return 0 364} 365 366# dhcpif if 367# Returns 0 if the interface is a DHCP interface and 1 otherwise. 368dhcpif() 369{ 370 local _tmpargs _arg 371 _tmpargs=`_ifconfig_getargs $1` 372 373 case $1 in 374 lo[0-9]*|\ 375 stf[0-9]*|\ 376 faith[0-9]*|\ 377 lp[0-9]*|\ 378 sl[0-9]*) 379 return 1 380 ;; 381 esac 382 if noafif $1; then 383 return 1 384 fi 385 386 for _arg in $_tmpargs; do 387 case $_arg in 388 [Dd][Hh][Cc][Pp]) 389 return 0 390 ;; 391 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 392 return 0 393 ;; 394 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 395 return 0 396 ;; 397 esac 398 done 399 400 return 1 401} 402 403# syncdhcpif 404# Returns 0 if the interface should be configured synchronously and 405# 1 otherwise. 406syncdhcpif() 407{ 408 local _tmpargs _arg 409 _tmpargs=`_ifconfig_getargs $1` 410 411 if noafif $1; then 412 return 1 413 fi 414 415 for _arg in $_tmpargs; do 416 case $_arg in 417 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 418 return 1 419 ;; 420 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 421 return 0 422 ;; 423 esac 424 done 425 426 checkyesno synchronous_dhclient 427} 428 429# wpaif if 430# Returns 0 if the interface is a WPA interface and 1 otherwise. 431wpaif() 432{ 433 local _tmpargs _arg 434 _tmpargs=`_ifconfig_getargs $1` 435 436 for _arg in $_tmpargs; do 437 case $_arg in 438 [Ww][Pp][Aa]) 439 return 0 440 ;; 441 esac 442 done 443 444 return 1 445} 446 447# hostapif if 448# Returns 0 if the interface is a HOSTAP interface and 1 otherwise. 449hostapif() 450{ 451 local _tmpargs _arg 452 _tmpargs=`_ifconfig_getargs $1` 453 454 for _arg in $_tmpargs; do 455 case $_arg in 456 [Hh][Oo][Ss][Tt][Aa][Pp]) 457 return 0 458 ;; 459 esac 460 done 461 462 return 1 463} 464 465# vnetif if 466# Returns 0 and echo jail if "vnet" keyword is specified on the 467# interface, and 1 otherwise. 468vnetif() 469{ 470 local _tmpargs _arg _vnet 471 _tmpargs=`_ifconfig_getargs $1` 472 473 _vnet=0 474 for _arg in $_tmpargs; do 475 case $_arg:$_vnet in 476 vnet:0) _vnet=1 ;; 477 *:1) echo $_arg; return 0 ;; 478 esac 479 done 480 481 return 1 482} 483 484# afexists af 485# Returns 0 if the address family is enabled in the kernel 486# 1 otherwise. 487afexists() 488{ 489 local _af 490 _af=$1 491 492 case ${_af} in 493 inet|inet6) 494 check_kern_features ${_af} 495 ;; 496 ipx) 497 ${SYSCTL_N} net.ipx > /dev/null 2>&1 498 ;; 499 atm) 500 if [ -x /sbin/atmconfig ]; then 501 /sbin/atmconfig diag list > /dev/null 2>&1 502 else 503 return 1 504 fi 505 ;; 506 link|ether) 507 return 0 508 ;; 509 *) 510 err 1 "afexists(): Unsupported address family: $_af" 511 ;; 512 esac 513} 514 515# noafif if 516# Returns 0 if the interface has no af configuration and 1 otherwise. 517noafif() 518{ 519 local _if 520 _if=$1 521 522 case $_if in 523 pflog[0-9]*|\ 524 pfsync[0-9]*|\ 525 usbus[0-9]*|\ 526 an[0-9]*|\ 527 ath[0-9]*|\ 528 ipw[0-9]*|\ 529 ipfw[0-9]*|\ 530 iwi[0-9]*|\ 531 iwn[0-9]*|\ 532 ral[0-9]*|\ 533 wi[0-9]*|\ 534 wl[0-9]*|\ 535 wpi[0-9]*) 536 return 0 537 ;; 538 esac 539 540 return 1 541} 542 543# ipv6if if 544# Returns 0 if the interface should be configured for IPv6 and 545# 1 otherwise. 546ipv6if() 547{ 548 local _if _tmpargs i 549 _if=$1 550 551 if ! afexists inet6; then 552 return 1 553 fi 554 555 # lo0 is always IPv6-enabled 556 case $_if in 557 lo0) 558 return 0 559 ;; 560 esac 561 562 case "${ipv6_network_interfaces}" in 563 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo]) 564 # True if $ifconfig_IF_ipv6 is defined. 565 _tmpargs=`_ifconfig_getargs $_if ipv6` 566 if [ -n "${_tmpargs}" ]; then 567 return 0 568 fi 569 570 # True if $ipv6_prefix_IF is defined. 571 _tmpargs=`get_if_var $_if ipv6_prefix_IF` 572 if [ -n "${_tmpargs}" ]; then 573 return 0 574 fi 575 576 # backward compatibility: True if $ipv6_ifconfig_IF is defined. 577 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 578 if [ -n "${_tmpargs}" ]; then 579 return 0 580 fi 581 ;; 582 esac 583 584 return 1 585} 586 587# ipv6_autoconfif if 588# Returns 0 if the interface should be configured for IPv6 with 589# Stateless Address Configuration; 1 otherwise. 590ipv6_autoconfif() 591{ 592 local _if _tmpargs _arg 593 _if=$1 594 595 case $_if in 596 lo[0-9]*|\ 597 stf[0-9]*|\ 598 faith[0-9]*|\ 599 lp[0-9]*|\ 600 sl[0-9]*) 601 return 1 602 ;; 603 esac 604 if noafif $_if; then 605 return 1 606 fi 607 if ! ipv6if $_if; then 608 return 1 609 fi 610 if checkyesno ipv6_gateway_enable; then 611 return 1 612 fi 613 _tmpargs=`get_if_var $_if ipv6_prefix_IF` 614 if [ -n "${_tmpargs}" ]; then 615 return 1 616 fi 617 # backward compatibility: $ipv6_enable 618 case $ipv6_enable in 619 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 620 if checkyesno ipv6_gateway_enable; then 621 return 1 622 fi 623 case $1 in 624 bridge[0-9]*) 625 # No accept_rtadv by default on if_bridge(4) 626 # to avoid a conflict with the member 627 # interfaces. 628 return 1 629 ;; 630 *) 631 return 0 632 ;; 633 esac 634 ;; 635 esac 636 637 _tmpargs=`_ifconfig_getargs $_if ipv6` 638 for _arg in $_tmpargs; do 639 case $_arg in 640 accept_rtadv) 641 return 0 642 ;; 643 esac 644 done 645 646 # backward compatibility: $ipv6_ifconfig_IF 647 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 648 for _arg in $_tmpargs; do 649 case $_arg in 650 accept_rtadv) 651 return 0 652 ;; 653 esac 654 done 655 656 return 1 657} 658 659# ifexists if 660# Returns 0 if the interface exists and 1 otherwise. 661ifexists() 662{ 663 [ -z "$1" ] && return 1 664 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1 665} 666 667# ipv4_up if 668# add IPv4 addresses to the interface $if 669ipv4_up() 670{ 671 local _if _ret 672 _if=$1 673 _ret=1 674 675 # Add 127.0.0.1/8 to lo0 unless otherwise specified. 676 if [ "${_if}" = "lo0" ]; then 677 ifconfig_args=`get_if_var ${_if} ifconfig_IF` 678 if [ -z "${ifconfig_args}" ]; then 679 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias 680 fi 681 fi 682 ifalias ${_if} inet alias && _ret=0 683 684 return $_ret 685} 686 687# ipv6_up if 688# add IPv6 addresses to the interface $if 689ipv6_up() 690{ 691 local _if _ret 692 _if=$1 693 _ret=1 694 695 if ! ipv6if $_if; then 696 return 0 697 fi 698 699 ifalias ${_if} inet6 alias && _ret=0 700 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0 701 ipv6_accept_rtadv_up ${_if} && _ret=0 702 703 return $_ret 704} 705 706# ipv4_down if 707# remove IPv4 addresses from the interface $if 708ipv4_down() 709{ 710 local _if _ifs _ret inetList oldifs _inet 711 _if=$1 712 _ifs="^" 713 _ret=1 714 715 ifalias ${_if} inet -alias && _ret=0 716 717 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`" 718 719 oldifs="$IFS" 720 IFS="$_ifs" 721 for _inet in $inetList ; do 722 # get rid of extraneous line 723 case $_inet in 724 inet\ *) ;; 725 *) continue ;; 726 esac 727 728 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 729 730 IFS="$oldifs" 731 ${IFCONFIG_CMD} ${_if} ${_inet} delete 732 IFS="$_ifs" 733 _ret=0 734 done 735 IFS="$oldifs" 736 737 return $_ret 738} 739 740# ipv6_down if 741# remove IPv6 addresses from the interface $if 742ipv6_down() 743{ 744 local _if _ifs _ret inetList oldifs _inet6 745 _if=$1 746 _ifs="^" 747 _ret=1 748 749 if ! ipv6if $_if; then 750 return 0 751 fi 752 753 ipv6_accept_rtadv_down ${_if} && _ret=0 754 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0 755 ifalias ${_if} inet6 -alias && _ret=0 756 757 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`" 758 759 oldifs="$IFS" 760 IFS="$_ifs" 761 for _inet6 in $inetList ; do 762 # get rid of extraneous line 763 case $_inet in 764 inet6\ *) ;; 765 *) continue ;; 766 esac 767 768 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'` 769 770 IFS="$oldifs" 771 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias 772 IFS="$_ifs" 773 _ret=0 774 done 775 IFS="$oldifs" 776 777 return $_ret 778} 779 780# ifalias if af action 781# Configure or remove aliases for network interface $if. 782# It returns 0 if at least one alias was configured or 783# removed, or 1 if there were none. 784# 785ifalias() 786{ 787 local _ret 788 _ret=1 789 790 afexists $2 || return $_ret 791 792 case "$2" in 793 inet|inet6|link|ether) 794 ifalias_af_common $1 $2 $3 && _ret=0 795 ;; 796 esac 797 798 return $_ret 799} 800 801# ifalias_expand_addr af action addr 802# Expand address range ("N-M") specification in addr. 803# "addr" must not include an address-family keyword. 804# The results will include an address-family keyword. 805# 806ifalias_expand_addr() 807{ 808 local _af _action 809 810 _af=$1 811 _action=$2 812 shift 2 813 814 afexists $_af || return 815 ifalias_expand_addr_$_af $_action $* 816} 817 818# ifalias_expand_addr_inet action addr 819# Helper function for ifalias_expand_addr(). Handles IPv4. 820# 821ifalias_expand_addr_inet() 822{ 823 local _action _arg _cidr _cidr_addr _exargs 824 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount 825 local _retstr _c 826 _action=$1 827 _arg=$2 828 shift 2 829 _exargs=$* 830 _retstr= 831 832 case $_action:$_arg:$_exargs in 833 *:*--*) return ;; # invalid 834 tmp:*[0-9]-[0-9]*:*) # to be expanded 835 _action="alias" 836 ;; 837 *:*[0-9]-[0-9]*:*) # to be expanded 838 ;; 839 tmp:*:*netmask*) # already expanded w/ netmask option 840 echo ${_arg%/[0-9]*} $_exargs && return 841 ;; 842 tmp:*:*) # already expanded w/o netmask option 843 echo $_arg $_exargs && return 844 ;; 845 *:*:*netmask*) # already expanded w/ netmask option 846 echo inet ${_arg%/[0-9]*} $_exargs && return 847 ;; 848 *:*:*) # already expanded w/o netmask option 849 echo inet $_arg $_exargs && return 850 ;; 851 esac 852 853 for _cidr in $_arg; do 854 _ipaddr=${_cidr%%/*} 855 _plen=${_cidr##*/} 856 # When subnet prefix length is not specified, use /32. 857 case $_plen in 858 $_ipaddr) _plen=32 ;; # "/" character not found 859 esac 860 861 OIFS=$IFS 862 IFS=. set -- $_ipaddr 863 _range= 864 _iphead= 865 _iptail= 866 for _c in $@; do 867 case $_range:$_c in 868 :[0-9]*-[0-9]*) 869 _range=$_c 870 ;; 871 :*) 872 _iphead="${_iphead}${_iphead:+.}${_c}" 873 ;; 874 *:*) 875 _iptail="${_iptail}${_iptail:+.}${_c}" 876 ;; 877 esac 878 done 879 IFS=$OIFS 880 _iplow=${_range%-*} 881 _iphigh=${_range#*-} 882 883 # clear netmask when removing aliases 884 if [ "$_action" = "-alias" ]; then 885 _plen="" 886 fi 887 888 _ipcount=$_iplow 889 while [ "$_ipcount" -le "$_iphigh" ]; do 890 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}" 891 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then 892 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." 893 break 894 else 895 _ipcount=$(($_ipcount + 1)) 896 fi 897 # Forcibly set /32 for remaining aliases. 898 _plen=32 899 done 900 done 901 902 for _c in $_retstr; do 903 ifalias_expand_addr_inet $_action $_c $_exargs 904 done 905} 906 907# ifalias_expand_addr_inet6 action addr 908# Helper function for ifalias_expand_addr(). Handles IPv6. 909# 910ifalias_expand_addr_inet6() 911{ 912 local _action _arg _cidr _cidr_addr _exargs 913 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount 914 local _ipv4part 915 local _retstr _c 916 _action=$1 917 _arg=$2 918 shift 2 919 _exargs=$* 920 _retstr= 921 922 case $_action:$_arg:$_exargs in 923 *:*--*:*) return ;; # invalid 924 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded 925 _action="alias" 926 ;; 927 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded 928 ;; 929 tmp:*:*prefixlen*) # already expanded w/ prefixlen option 930 echo ${_arg%/[0-9]*} $_exargs && return 931 ;; 932 tmp:*:*) # already expanded w/o prefixlen option 933 echo $_arg $_exargs && return 934 ;; 935 *:*:*prefixlen*) # already expanded w/ prefixlen option 936 echo inet6 ${_arg%/[0-9]*} $_exargs && return 937 ;; 938 *:*:*) # already expanded w/o prefixlen option 939 echo inet6 $_arg $_exargs && return 940 ;; 941 esac 942 943 for _cidr in $_arg; do 944 _ipaddr="${_cidr%%/*}" 945 _plen="${_cidr##*/}" 946 947 case $_action:$_ipaddr:$_cidr in 948 -alias:*:*) unset _plen ;; 949 *:$_cidr:$_ipaddr) unset _plen ;; 950 esac 951 952 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then 953 # Handle !v4mapped && !v4compat addresses. 954 955 # The default prefix length is 64. 956 case $_ipaddr:$_cidr in 957 $_cidr:$_ipaddr) _plen="64" ;; 958 esac 959 _ipleft=${_ipaddr%-*} 960 _ipright=${_ipaddr#*-} 961 _iplow=${_ipleft##*:} 962 _iphigh=${_ipright%%:*} 963 _ipleft=${_ipleft%:*} 964 _ipright=${_ipright#*:} 965 966 if [ "$_iphigh" = "$_ipright" ]; then 967 unset _ipright 968 else 969 _ipright=:$_ipright 970 fi 971 972 if [ -n "$_iplow" -a -n "$_iphigh" ]; then 973 _iplow=$((0x$_iplow)) 974 _iphigh=$((0x$_iphigh)) 975 _ipcount=$_iplow 976 while [ $_ipcount -le $_iphigh ]; do 977 _r=`printf "%s:%04x%s%s" \ 978 $_ipleft $_ipcount $_ipright \ 979 ${_plen:+/}$_plen` 980 _retstr="$_retstr $_r" 981 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ] 982 then 983 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." 984 break 985 else 986 _ipcount=$(($_ipcount + 1)) 987 fi 988 done 989 else 990 _retstr="${_ipaddr}${_plen:+/}${_plen}" 991 fi 992 993 for _c in $_retstr; do 994 ifalias_expand_addr_inet6 $_action $_c $_exargs 995 done 996 else 997 # v4mapped/v4compat should handle as an IPv4 alias 998 _ipv4part=${_ipaddr##*:} 999 1000 # Adjust prefix length if any. If not, set the 1001 # default prefix length as 32. 1002 case $_ipaddr:$_cidr in 1003 $_cidr:$_ipaddr) _plen=32 ;; 1004 *) _plen=$(($_plen - 96)) ;; 1005 esac 1006 1007 _retstr=`ifalias_expand_addr_inet \ 1008 tmp ${_ipv4part}${_plen:+/}${_plen}` 1009 for _c in $_retstr; do 1010 ifalias_expand_addr_inet $_action $_c $_exargs 1011 done 1012 fi 1013 done 1014} 1015 1016# ifalias_af_common_handler if af action args 1017# Helper function for ifalias_af_common(). 1018# 1019ifalias_af_common_handler() 1020{ 1021 local _ret _if _af _action _args _c _tmpargs 1022 1023 _ret=1 1024 _if=$1 1025 _af=$2 1026 _action=$3 1027 shift 3 1028 _args=$* 1029 1030 case $_args in 1031 ${_af}\ *) ;; 1032 *) return ;; 1033 esac 1034 1035 # link(ether) does not support address removal. 1036 case $_af:$_action in 1037 link:-alias|ether:-alias) return ;; 1038 esac 1039 1040 _tmpargs= 1041 for _c in $_args; do 1042 case $_c in 1043 ${_af}) 1044 case $_tmpargs in 1045 ${_af}\ *-*) 1046 ifalias_af_common_handler $_if $_af $_action \ 1047 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 1048 ;; 1049 ${_af}\ *) 1050 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 1051 ;; 1052 esac 1053 _tmpargs=$_af 1054 ;; 1055 *) 1056 _tmpargs="$_tmpargs $_c" 1057 ;; 1058 esac 1059 done 1060 # Process the last component if any. 1061 if [ -n "$_tmpargs}" ]; then 1062 case $_tmpargs in 1063 ${_af}\ *-*) 1064 ifalias_af_common_handler $_if $_af $_action \ 1065 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 1066 ;; 1067 ${_af}\ *) 1068 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 1069 ;; 1070 esac 1071 fi 1072 1073 return $_ret 1074} 1075 1076# ifalias_af_common if af action 1077# Helper function for ifalias(). 1078# 1079ifalias_af_common() 1080{ 1081 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf 1082 local _vif _punct=".-/+" 1083 1084 _ret=1 1085 _aliasn= 1086 _if=$1 1087 _af=$2 1088 _action=$3 1089 1090 # Normalize $_if before using it in a pattern to list_vars() 1091 ltr "$_if" "$_punct" "_" _vif 1092 1093 # ifconfig_IF_aliasN which starts with $_af 1094 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* | 1095 sort_lite -nk1.$((9+${#_vif}+7))` 1096 do 1097 eval ifconfig_args=\"\$$alias\" 1098 _iaf= 1099 case $ifconfig_args in 1100 inet\ *) _iaf=inet ;; 1101 inet6\ *) _iaf=inet6 ;; 1102 ipx\ *) _iaf=ipx ;; 1103 link\ *) _iaf=link ;; 1104 ether\ *) _iaf=ether ;; 1105 esac 1106 1107 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in 1108 ${_af}:*:${_af}:*) 1109 _aliasn="$_aliasn $ifconfig_args" 1110 ;; 1111 ${_af}:*:"":"") 1112 break 1113 ;; 1114 inet:alias:"":*) 1115 _aliasn="$_aliasn inet $ifconfig_args" 1116 warn "\$${alias} needs leading" \ 1117 "\"inet\" keyword for an IPv4 address." 1118 esac 1119 done 1120 1121 # backward compatibility: ipv6_ifconfig_IF_aliasN. 1122 case $_af in 1123 inet6) 1124 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* | 1125 sort_lite -nk1.$((14+${#_vif}+7))` 1126 do 1127 eval ifconfig_args=\"\$$alias\" 1128 case ${_action}:"${ifconfig_args}" in 1129 *:"") 1130 break 1131 ;; 1132 alias:*) 1133 _aliasn="${_aliasn} inet6 ${ifconfig_args}" 1134 warn "\$${alias} is obsolete. " \ 1135 "Use ifconfig_${_vif}_aliasN instead." 1136 ;; 1137 esac 1138 done 1139 esac 1140 1141 # backward compatibility: ipv4_addrs_IF. 1142 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do 1143 _aliasn="$_aliasn inet $_tmpargs" 1144 done 1145 1146 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others. 1147 _tmpargs= 1148 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do 1149 case $_c in 1150 inet|inet6|ipx|link|ether) 1151 case $_tmpargs in 1152 ${_af}\ *) 1153 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1154 ;; 1155 esac 1156 _tmpargs=$_c 1157 ;; 1158 *) 1159 _tmpargs="$_tmpargs $_c" 1160 esac 1161 done 1162 # Process the last component 1163 case $_tmpargs in 1164 ${_af}\ *) 1165 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1166 ;; 1167 esac 1168 1169 return $_ret 1170} 1171 1172# ipv6_prefix_hostid_addr_common if action 1173# Add or remove IPv6 prefix + hostid addr on the interface $if 1174# 1175ipv6_prefix_hostid_addr_common() 1176{ 1177 local _if _action prefix j 1178 _if=$1 1179 _action=$2 1180 prefix=`get_if_var ${_if} ipv6_prefix_IF` 1181 1182 if [ -n "${prefix}" ]; then 1183 for j in ${prefix}; do 1184 # The default prefixlen is 64. 1185 plen=${j#*/} 1186 case $j:$plen in 1187 $plen:$j) plen=64 ;; 1188 *) j=${j%/*} ;; 1189 esac 1190 1191 # Normalize the last part by removing ":" 1192 j=${j%::*} 1193 j=${j%:} 1194 ${IFCONFIG_CMD} ${_if} inet6 $j:: \ 1195 prefixlen $plen eui64 ${_action} 1196 1197 # if I am a router, add subnet router 1198 # anycast address (RFC 2373). 1199 if checkyesno ipv6_gateway_enable; then 1200 ${IFCONFIG_CMD} ${_if} inet6 $j:: \ 1201 prefixlen $plen ${_action} anycast 1202 fi 1203 done 1204 fi 1205} 1206 1207# ipv6_accept_rtadv_up if 1208# Enable accepting Router Advertisement and send Router 1209# Solicitation message 1210ipv6_accept_rtadv_up() 1211{ 1212 if ipv6_autoconfif $1; then 1213 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up 1214 if ! checkyesno rtsold_enable; then 1215 rtsol ${rtsol_flags} $1 1216 fi 1217 fi 1218} 1219 1220# ipv6_accept_rtadv_down if 1221# Disable accepting Router Advertisement 1222ipv6_accept_rtadv_down() 1223{ 1224 if ipv6_autoconfif $1; then 1225 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv 1226 fi 1227} 1228 1229# ifscript_up if 1230# Evaluate a startup script for the $if interface. 1231# It returns 0 if a script was found and processed or 1232# 1 if no script was found. 1233# 1234ifscript_up() 1235{ 1236 if [ -r /etc/start_if.$1 ]; then 1237 . /etc/start_if.$1 1238 return 0 1239 else 1240 return 1 1241 fi 1242} 1243 1244# ifscript_down if 1245# Evaluate a shutdown script for the $if interface. 1246# It returns 0 if a script was found and processed or 1247# 1 if no script was found. 1248# 1249ifscript_down() 1250{ 1251 if [ -r /etc/stop_if.$1 ]; then 1252 . /etc/stop_if.$1 1253 return 0 1254 else 1255 return 1 1256 fi 1257} 1258 1259# clone_up 1260# Create cloneable interfaces. 1261# 1262clone_up() 1263{ 1264 local _list ifn ifopt _iflist _n tmpargs 1265 _list= 1266 _iflist=$* 1267 1268 # create_args_IF 1269 for ifn in ${cloned_interfaces}; do 1270 # Parse ifn:ifopt. 1271 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1272 case $_iflist in 1273 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1274 *) continue ;; 1275 esac 1276 case $ifn in 1277 epair[0-9]*) 1278 # epair(4) uses epair[0-9] for creation and 1279 # epair[0-9][ab] for configuration. 1280 # 1281 # Skip if ${ifn}a or ${ifn}b already exist. 1282 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then 1283 continue 1284 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then 1285 continue 1286 fi 1287 ${IFCONFIG_CMD} ${ifn} create \ 1288 `get_if_var ${ifn} create_args_IF` 1289 if [ $? -eq 0 ]; then 1290 _list="$_list ${ifn}a ${ifn}b" 1291 fi 1292 ;; 1293 *) 1294 # Skip if ${ifn} already exists. 1295 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1296 continue 1297 fi 1298 ${IFCONFIG_CMD} ${ifn} create \ 1299 `get_if_var ${ifn} create_args_IF` 1300 if [ $? -eq 0 ]; then 1301 _list="$_list $ifn" 1302 fi 1303 esac 1304 done 1305 if [ -n "$gif_interfaces" ]; then 1306 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead." 1307 fi 1308 for ifn in ${gif_interfaces}; do 1309 # Parse ifn:ifopt. 1310 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1311 case $_iflist in 1312 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1313 *) continue ;; 1314 esac 1315 # Skip if ifn already exists. 1316 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1317 continue 1318 fi 1319 case $ifn in 1320 gif[0-9]*) 1321 ${IFCONFIG_CMD} $ifn create 1322 ;; 1323 *) 1324 _n=$(${IFCONFIG_CMD} gif create) 1325 ${IFCONFIG_CMD} $_n name $ifn 1326 ;; 1327 esac 1328 if [ $? -eq 0 ]; then 1329 _list="$_list $ifn" 1330 fi 1331 tmpargs=$(get_if_var $ifn gifconfig_IF) 1332 eval ifconfig_${ifn}=\"tunnel \$tmpargs\" 1333 done 1334 if [ -n "${_list# }" ]; then 1335 echo "Created clone interfaces: ${_list# }." 1336 fi 1337 debug "Cloned: ${_list# }" 1338} 1339 1340# clone_down 1341# Destroy cloned interfaces. Destroyed interfaces are echoed to 1342# standard output. 1343# 1344clone_down() 1345{ 1346 local _list ifn _difn ifopt _iflist _sticky 1347 _list= 1348 _iflist=$* 1349 1350 : ${cloned_interfaces_sticky:=NO} 1351 if checkyesno cloned_interfaces_sticky; then 1352 _sticky=1 1353 else 1354 _sticky=0 1355 fi 1356 for ifn in ${cloned_interfaces} ${gif_interfaces}; do 1357 # Parse ifn:ifopt. 1358 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1359 case $ifopt:$_sticky in 1360 sticky:*) continue ;; # :sticky => not destroy 1361 nosticky:*) ;; # :nosticky => destroy 1362 *:1) continue ;; # global sticky knob == 1 1363 esac 1364 case $_iflist in 1365 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1366 *) continue ;; 1367 esac 1368 case $ifn in 1369 epair[0-9]*) 1370 # Note: epair(4) uses epair[0-9] for removal and 1371 # epair[0-9][ab] for configuration. 1372 # 1373 # Skip if both of ${ifn}a and ${ifn}b do not exist. 1374 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then 1375 _difn=${ifn}a 1376 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then 1377 _difn=${ifn}b 1378 else 1379 continue 1380 fi 1381 ${IFCONFIG_CMD} -n $_difn destroy 1382 if [ $? -eq 0 ]; then 1383 _list="$_list ${ifn}a ${ifn}b" 1384 fi 1385 ;; 1386 *) 1387 # Skip if ifn does not exist. 1388 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1389 continue 1390 fi 1391 ${IFCONFIG_CMD} -n ${ifn} destroy 1392 if [ $? -eq 0 ]; then 1393 _list="$_list $ifn" 1394 fi 1395 ;; 1396 esac 1397 done 1398 if [ -n "${_list# }" ]; then 1399 echo "Destroyed clone interfaces: ${_list# }." 1400 fi 1401 debug "Destroyed clones: ${_list# }" 1402} 1403 1404# childif_create 1405# Create and configure child interfaces. Return 0 if child 1406# interfaces are created. 1407# 1408childif_create() 1409{ 1410 local cfg child child_vlans child_wlans create_args debug_flags ifn i 1411 cfg=1 1412 ifn=$1 1413 1414 # Create wireless interfaces 1415 child_wlans=`get_if_var $ifn wlans_IF` 1416 1417 for child in ${child_wlans}; do 1418 create_args="wlandev $ifn `get_if_var $child create_args_IF`" 1419 debug_flags="`get_if_var $child wlandebug_IF`" 1420 1421 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 1422 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1423 if [ -n "${debug_flags}" ]; then 1424 wlandebug -i $child ${debug_flags} 1425 fi 1426 else 1427 i=`${IFCONFIG_CMD} wlan create ${create_args}` 1428 if [ -n "${debug_flags}" ]; then 1429 wlandebug -i $i ${debug_flags} 1430 fi 1431 ${IFCONFIG_CMD} $i name $child && cfg=0 1432 fi 1433 if autoif $child; then 1434 ifn_start $child 1435 fi 1436 done 1437 1438 # Create vlan interfaces 1439 child_vlans=`get_if_var $ifn vlans_IF` 1440 1441 if [ -n "${child_vlans}" ]; then 1442 load_kld if_vlan 1443 fi 1444 1445 for child in ${child_vlans}; do 1446 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1447 child="${ifn}.${child}" 1448 create_args=`get_if_var $child create_args_IF` 1449 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1450 else 1451 create_args="vlandev $ifn `get_if_var $child create_args_IF`" 1452 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then 1453 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1454 else 1455 i=`${IFCONFIG_CMD} vlan create ${create_args}` 1456 ${IFCONFIG_CMD} $i name $child && cfg=0 1457 fi 1458 fi 1459 if autoif $child; then 1460 ifn_start $child 1461 fi 1462 done 1463 1464 return ${cfg} 1465} 1466 1467# childif_destroy 1468# Destroy child interfaces. 1469# 1470childif_destroy() 1471{ 1472 local cfg child child_vlans child_wlans ifn 1473 cfg=1 1474 1475 child_wlans=`get_if_var $ifn wlans_IF` 1476 for child in ${child_wlans}; do 1477 if ! ifexists $child; then 1478 continue 1479 fi 1480 ${IFCONFIG_CMD} -n $child destroy && cfg=0 1481 done 1482 1483 child_vlans=`get_if_var $ifn vlans_IF` 1484 for child in ${child_vlans}; do 1485 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1486 child="${ifn}.${child}" 1487 fi 1488 if ! ifexists $child; then 1489 continue 1490 fi 1491 ${IFCONFIG_CMD} -n $child destroy && cfg=0 1492 done 1493 1494 return ${cfg} 1495} 1496 1497# ng_mkpeer 1498# Create netgraph nodes. 1499# 1500ng_mkpeer() 1501{ 1502 ngctl -f - 2> /dev/null <<EOF 1503mkpeer $* 1504msg dummy nodeinfo 1505EOF 1506} 1507 1508# ng_create_one 1509# Create netgraph nodes. 1510# 1511ng_create_one() 1512{ 1513 local t 1514 1515 ng_mkpeer $* | while read line; do 1516 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 1517 if [ -n "${t}" ]; then 1518 echo ${t} 1519 return 1520 fi 1521 done 1522} 1523 1524# ng_fec_create ifn 1525# Configure Fast EtherChannel for interface $ifn. Returns 0 if 1526# FEC arguments were found and configured; returns !0 otherwise. 1527ng_fec_create() 1528{ 1529 local req_iface iface bogus 1530 req_iface="$1" 1531 1532 ngctl shutdown ${req_iface}: > /dev/null 2>&1 1533 1534 bogus="" 1535 while true; do 1536 iface=`ng_create_one fec dummy fec` 1537 if [ -z "${iface}" ]; then 1538 exit 2 1539 fi 1540 if [ "${iface}" = "${req_iface}" ]; then 1541 break 1542 fi 1543 bogus="${bogus} ${iface}" 1544 done 1545 1546 for iface in ${bogus}; do 1547 ngctl shutdown ${iface}: 1548 done 1549} 1550 1551# fec_up 1552# Create Fast EtherChannel interfaces. 1553fec_up() 1554{ 1555 local i j 1556 1557 for i in ${fec_interfaces}; do 1558 ng_fec_create $i 1559 for j in `get_if_var $i fecconfig_IF`; do 1560 case ${j} in 1561 '') 1562 continue 1563 ;; 1564 *) 1565 ngctl msg ${i}: add_iface "\"${j}\"" 1566 ;; 1567 esac 1568 done 1569 done 1570} 1571 1572# ipx_up ifn 1573# Configure any IPX addresses for interface $ifn. Returns 0 if 1574# IPX arguments were found and configured; returns 1 otherwise. 1575# 1576ipx_up() 1577{ 1578 local ifn 1579 ifn="$1" 1580 1581 # ifconfig_IF_ipx 1582 ifconfig_args=`_ifconfig_getargs $ifn ipx` 1583 if [ -n "${ifconfig_args}" ]; then 1584 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args} 1585 return 0 1586 fi 1587 1588 return 1 1589} 1590 1591# ipx_down ifn 1592# Remove IPX addresses for interface $ifn. Returns 0 if IPX 1593# addresses were found and unconfigured. It returns 1, otherwise. 1594# 1595ipx_down() 1596{ 1597 local _if _ifs _ret ipxList oldifs _ipx 1598 _if=$1 1599 _ifs="^" 1600 _ret=1 1601 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`" 1602 oldifs="$IFS" 1603 1604 IFS="$_ifs" 1605 for _ipx in $ipxList ; do 1606 # get rid of extraneous line 1607 [ -z "$_ipx" ] && break 1608 1609 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 1610 1611 IFS="$oldifs" 1612 ${IFCONFIG_CMD} ${_if} ${_ipx} delete 1613 IFS="$_ifs" 1614 _ret=0 1615 done 1616 IFS="$oldifs" 1617 1618 return $_ret 1619} 1620 1621# ifnet_rename [ifname] 1622# Rename interfaces if ifconfig_IF_name is defined. 1623# 1624ifnet_rename() 1625{ 1626 local _if _ifname 1627 1628 # ifconfig_IF_name 1629 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do 1630 _ifname=`get_if_var $_if ifconfig_IF_name` 1631 if [ ! -z "$_ifname" ]; then 1632 ${IFCONFIG_CMD} $_if name $_ifname 1633 fi 1634 done 1635 1636 return 0 1637} 1638 1639# list_net_interfaces type 1640# List all network interfaces. The type of interface returned 1641# can be controlled by the type argument. The type 1642# argument can be any of the following: 1643# nodhcp - all interfaces, excluding DHCP configured interfaces 1644# dhcp - list only DHCP configured interfaces 1645# noautoconf - all interfaces, excluding IPv6 Stateless 1646# Address Autoconf configured interfaces 1647# autoconf - list only IPv6 Stateless Address Autoconf 1648# configured interfaces 1649# If no argument is specified all network interfaces are output. 1650# Note that the list will include cloned interfaces if applicable. 1651# Cloned interfaces must already exist to have a chance to appear 1652# in the list if ${network_interfaces} is set to `auto'. 1653# 1654list_net_interfaces() 1655{ 1656 local type _tmplist _list _autolist _lo _if 1657 type=$1 1658 1659 # Get a list of ALL the interfaces and make lo0 first if it's there. 1660 # 1661 _tmplist= 1662 case ${network_interfaces} in 1663 [Aa][Uu][Tt][Oo]) 1664 _autolist="`${IFCONFIG_CMD} -l`" 1665 _lo= 1666 for _if in ${_autolist} ; do 1667 if autoif $_if; then 1668 if [ "$_if" = "lo0" ]; then 1669 _lo="lo0 " 1670 else 1671 _tmplist="${_tmplist} ${_if}" 1672 fi 1673 fi 1674 done 1675 _tmplist="${_lo}${_tmplist# }" 1676 ;; 1677 *) 1678 for _if in ${network_interfaces} ${cloned_interfaces}; do 1679 # epair(4) uses epair[0-9] for creation and 1680 # epair[0-9][ab] for configuration. 1681 case $_if in 1682 epair[0-9]*) 1683 _tmplist="$_tmplist ${_if}a ${_if}b" 1684 ;; 1685 *) 1686 _tmplist="$_tmplist $_if" 1687 ;; 1688 esac 1689 done 1690 # 1691 # lo0 is effectively mandatory, so help prevent foot-shooting 1692 # 1693 case "$_tmplist" in 1694 lo0|'lo0 '*|*' lo0'|*' lo0 '*) 1695 # This is fine, do nothing 1696 _tmplist="${_tmplist# }" 1697 ;; 1698 *) 1699 _tmplist="lo0 ${_tmplist# }" 1700 ;; 1701 esac 1702 ;; 1703 esac 1704 1705 _list= 1706 case "$type" in 1707 nodhcp) 1708 for _if in ${_tmplist} ; do 1709 if ! dhcpif $_if && \ 1710 [ -n "`_ifconfig_getargs $_if`" ]; then 1711 _list="${_list# } ${_if}" 1712 fi 1713 done 1714 ;; 1715 dhcp) 1716 for _if in ${_tmplist} ; do 1717 if dhcpif $_if; then 1718 _list="${_list# } ${_if}" 1719 fi 1720 done 1721 ;; 1722 noautoconf) 1723 for _if in ${_tmplist} ; do 1724 if ! ipv6_autoconfif $_if && \ 1725 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then 1726 _list="${_list# } ${_if}" 1727 fi 1728 done 1729 ;; 1730 autoconf) 1731 for _if in ${_tmplist} ; do 1732 if ipv6_autoconfif $_if; then 1733 _list="${_list# } ${_if}" 1734 fi 1735 done 1736 ;; 1737 *) 1738 _list=${_tmplist} 1739 ;; 1740 esac 1741 1742 echo $_list 1743 1744 return 0 1745} 1746 1747# get_default_if -address_family 1748# Get the interface of the default route for the given address family. 1749# The -address_family argument must be suitable passing to route(8). 1750# 1751get_default_if() 1752{ 1753 local routeget oldifs defif line 1754 defif= 1755 oldifs="$IFS" 1756 IFS=" 1757" 1758 for line in `route -n get $1 default 2>/dev/null`; do 1759 case $line in 1760 *interface:*) 1761 defif=${line##*: } 1762 ;; 1763 esac 1764 done 1765 IFS=${oldifs} 1766 1767 echo $defif 1768} 1769 1770# hexdigit arg 1771# Echo decimal number $arg (single digit) in hexadecimal format. 1772hexdigit() 1773{ 1774 printf '%x\n' "$1" 1775} 1776 1777# hexprint arg 1778# Echo decimal number $arg (multiple digits) in hexadecimal format. 1779hexprint() 1780{ 1781 printf '%x\n' "$1" 1782} 1783 1784is_wired_interface() 1785{ 1786 local media 1787 1788 case `${IFCONFIG_CMD} $1 2>/dev/null` in 1789 *media:?Ethernet*) media=Ethernet ;; 1790 esac 1791 1792 test "$media" = "Ethernet" 1793} 1794 1795# network6_getladdr if [flag] 1796# Echo link-local address from $if if any. 1797# If flag is defined, tentative ones will be excluded. 1798network6_getladdr() 1799{ 1800 local _if _flag proto addr rest 1801 _if=$1 1802 _flag=$2 1803 1804 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do 1805 case "${proto}/${addr}/${_flag}/${rest}" in 1806 inet6/fe80::*//*) 1807 echo ${addr} 1808 ;; 1809 inet6/fe80:://*tentative*) # w/o flag 1810 sleep `${SYSCTL_N} net.inet6.ip6.dad_count` 1811 network6_getladdr $_if $_flags 1812 ;; 1813 inet6/fe80::/*/*tentative*) # w/ flag 1814 echo ${addr} 1815 ;; 1816 *) 1817 continue 1818 ;; 1819 esac 1820 1821 return 1822 done 1823} 1824