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