network.subr revision 264438
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 264438 2014-04-14 01:44:56Z 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 " \ 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 _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" "_" _if 1092 1093 # ifconfig_IF_aliasN which starts with $_af 1094 for alias in `list_vars ifconfig_${_if}_alias[0-9]\* | 1095 sort_lite -nk1.$((9+${#_if}+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 "\$ifconfig_${_if}_alias${alias} needs " \ 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_${_if}_alias[0-9]\* | 1125 sort_lite -nk1.$((14+${#_if}+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 "\$ipv6_ifconfig_${_if}_alias${alias} " \ 1135 "is obsolete. Use ifconfig_$1_aliasN " \ 1136 "instead." 1137 ;; 1138 esac 1139 done 1140 esac 1141 1142 # backward compatibility: ipv4_addrs_IF. 1143 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do 1144 _aliasn="$_aliasn inet $_tmpargs" 1145 done 1146 1147 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others. 1148 _tmpargs= 1149 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do 1150 case $_c in 1151 inet|inet6|ipx|link|ether) 1152 case $_tmpargs in 1153 ${_af}\ *) 1154 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1155 ;; 1156 esac 1157 _tmpargs=$_c 1158 ;; 1159 *) 1160 _tmpargs="$_tmpargs $_c" 1161 esac 1162 done 1163 # Process the last component 1164 case $_tmpargs in 1165 ${_af}\ *) 1166 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 1167 ;; 1168 esac 1169 1170 return $_ret 1171} 1172 1173# ipv6_prefix_hostid_addr_common if action 1174# Add or remove IPv6 prefix + hostid addr on the interface $if 1175# 1176ipv6_prefix_hostid_addr_common() 1177{ 1178 local _if _action prefix j 1179 _if=$1 1180 _action=$2 1181 prefix=`get_if_var ${_if} ipv6_prefix_IF` 1182 1183 if [ -n "${prefix}" ]; then 1184 for j in ${prefix}; do 1185 # The default prefixlen is 64. 1186 plen=${j#*/} 1187 case $j:$plen in 1188 $plen:$j) plen=64 ;; 1189 *) j=${j%/*} ;; 1190 esac 1191 1192 # Normalize the last part by removing ":" 1193 j=${j%::*} 1194 j=${j%:} 1195 ${IFCONFIG_CMD} ${_if} inet6 $j:: \ 1196 prefixlen $plen eui64 ${_action} 1197 1198 # if I am a router, add subnet router 1199 # anycast address (RFC 2373). 1200 if checkyesno ipv6_gateway_enable; then 1201 ${IFCONFIG_CMD} ${_if} inet6 $j:: \ 1202 prefixlen $plen ${_action} anycast 1203 fi 1204 done 1205 fi 1206} 1207 1208# ipv6_accept_rtadv_up if 1209# Enable accepting Router Advertisement and send Router 1210# Solicitation message 1211ipv6_accept_rtadv_up() 1212{ 1213 if ipv6_autoconfif $1; then 1214 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up 1215 if ! checkyesno rtsold_enable; then 1216 rtsol ${rtsol_flags} $1 1217 fi 1218 fi 1219} 1220 1221# ipv6_accept_rtadv_down if 1222# Disable accepting Router Advertisement 1223ipv6_accept_rtadv_down() 1224{ 1225 if ipv6_autoconfif $1; then 1226 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv 1227 fi 1228} 1229 1230# ifscript_up if 1231# Evaluate a startup script for the $if interface. 1232# It returns 0 if a script was found and processed or 1233# 1 if no script was found. 1234# 1235ifscript_up() 1236{ 1237 if [ -r /etc/start_if.$1 ]; then 1238 . /etc/start_if.$1 1239 return 0 1240 else 1241 return 1 1242 fi 1243} 1244 1245# ifscript_down if 1246# Evaluate a shutdown script for the $if interface. 1247# It returns 0 if a script was found and processed or 1248# 1 if no script was found. 1249# 1250ifscript_down() 1251{ 1252 if [ -r /etc/stop_if.$1 ]; then 1253 . /etc/stop_if.$1 1254 return 0 1255 else 1256 return 1 1257 fi 1258} 1259 1260# clone_up 1261# Create cloneable interfaces. 1262# 1263clone_up() 1264{ 1265 local _list ifn ifopt _iflist _n tmpargs 1266 _list= 1267 _iflist=$* 1268 1269 # create_args_IF 1270 for ifn in ${cloned_interfaces}; do 1271 # Parse ifn:ifopt. 1272 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1273 case $_iflist in 1274 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1275 *) continue ;; 1276 esac 1277 case $ifn in 1278 epair[0-9]*) 1279 # epair(4) uses epair[0-9] for creation and 1280 # epair[0-9][ab] for configuration. 1281 # 1282 # Skip if ${ifn}a or ${ifn}b already exist. 1283 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then 1284 continue 1285 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then 1286 continue 1287 fi 1288 ${IFCONFIG_CMD} ${ifn} create \ 1289 `get_if_var ${ifn} create_args_IF` 1290 if [ $? -eq 0 ]; then 1291 _list="$_list ${ifn}a ${ifn}b" 1292 fi 1293 ;; 1294 *) 1295 # Skip if ${ifn} already exists. 1296 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1297 continue 1298 fi 1299 ${IFCONFIG_CMD} ${ifn} create \ 1300 `get_if_var ${ifn} create_args_IF` 1301 if [ $? -eq 0 ]; then 1302 _list="$_list $ifn" 1303 fi 1304 esac 1305 done 1306 if [ -n "$gif_interfaces" ]; then 1307 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead." 1308 fi 1309 for ifn in ${gif_interfaces}; do 1310 # Parse ifn:ifopt. 1311 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1312 case $_iflist in 1313 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1314 *) continue ;; 1315 esac 1316 # Skip if ifn already exists. 1317 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1318 continue 1319 fi 1320 case $ifn in 1321 gif[0-9]*) 1322 ${IFCONFIG_CMD} $ifn create 1323 ;; 1324 *) 1325 _n=$(${IFCONFIG_CMD} gif create) 1326 ${IFCONFIG_CMD} $_n name $ifn 1327 ;; 1328 esac 1329 if [ $? -eq 0 ]; then 1330 _list="$_list $ifn" 1331 fi 1332 tmpargs=$(get_if_var $ifn gifconfig_IF) 1333 eval ifconfig_${ifn}=\"tunnel \$tmpargs\" 1334 done 1335 if [ -n "${_list# }" ]; then 1336 echo "Created clone interfaces: ${_list# }." 1337 fi 1338 debug "Cloned: ${_list# }" 1339} 1340 1341# clone_down 1342# Destroy cloned interfaces. Destroyed interfaces are echoed to 1343# standard output. 1344# 1345clone_down() 1346{ 1347 local _list ifn _difn ifopt _iflist _sticky 1348 _list= 1349 _iflist=$* 1350 1351 : ${cloned_interfaces_sticky:=NO} 1352 if checkyesno cloned_interfaces_sticky; then 1353 _sticky=1 1354 else 1355 _sticky=0 1356 fi 1357 for ifn in ${cloned_interfaces} ${gif_interfaces}; do 1358 # Parse ifn:ifopt. 1359 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS 1360 case $ifopt:$_sticky in 1361 sticky:*) continue ;; # :sticky => not destroy 1362 nosticky:*) ;; # :nosticky => destroy 1363 *:1) continue ;; # global sticky knob == 1 1364 esac 1365 case $_iflist in 1366 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;; 1367 *) continue ;; 1368 esac 1369 case $ifn in 1370 epair[0-9]*) 1371 # Note: epair(4) uses epair[0-9] for removal and 1372 # epair[0-9][ab] for configuration. 1373 # 1374 # Skip if both of ${ifn}a and ${ifn}b do not exist. 1375 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then 1376 _difn=${ifn}a 1377 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then 1378 _difn=${ifn}b 1379 else 1380 continue 1381 fi 1382 ${IFCONFIG_CMD} -n $_difn destroy 1383 if [ $? -eq 0 ]; then 1384 _list="$_list ${ifn}a ${ifn}b" 1385 fi 1386 ;; 1387 *) 1388 # Skip if ifn does not exist. 1389 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then 1390 continue 1391 fi 1392 ${IFCONFIG_CMD} -n ${ifn} destroy 1393 if [ $? -eq 0 ]; then 1394 _list="$_list $ifn" 1395 fi 1396 ;; 1397 esac 1398 done 1399 if [ -n "${_list# }" ]; then 1400 echo "Destroyed clone interfaces: ${_list# }." 1401 fi 1402 debug "Destroyed clones: ${_list# }" 1403} 1404 1405# childif_create 1406# Create and configure child interfaces. Return 0 if child 1407# interfaces are created. 1408# 1409childif_create() 1410{ 1411 local cfg child child_vlans child_wlans create_args debug_flags ifn i 1412 cfg=1 1413 ifn=$1 1414 1415 # Create wireless interfaces 1416 child_wlans=`get_if_var $ifn wlans_IF` 1417 1418 for child in ${child_wlans}; do 1419 create_args="wlandev $ifn `get_if_var $child create_args_IF`" 1420 debug_flags="`get_if_var $child wlandebug_IF`" 1421 1422 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then 1423 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1424 if [ -n "${debug_flags}" ]; then 1425 wlandebug -i $child ${debug_flags} 1426 fi 1427 else 1428 i=`${IFCONFIG_CMD} wlan create ${create_args}` 1429 if [ -n "${debug_flags}" ]; then 1430 wlandebug -i $i ${debug_flags} 1431 fi 1432 ${IFCONFIG_CMD} $i name $child && cfg=0 1433 fi 1434 if autoif $child; then 1435 ifn_start $child 1436 fi 1437 done 1438 1439 # Create vlan interfaces 1440 child_vlans=`get_if_var $ifn vlans_IF` 1441 1442 if [ -n "${child_vlans}" ]; then 1443 load_kld if_vlan 1444 fi 1445 1446 for child in ${child_vlans}; do 1447 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1448 child="${ifn}.${child}" 1449 create_args=`get_if_var $child create_args_IF` 1450 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1451 else 1452 create_args="vlandev $ifn `get_if_var $child create_args_IF`" 1453 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then 1454 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0 1455 else 1456 i=`${IFCONFIG_CMD} vlan create ${create_args}` 1457 ${IFCONFIG_CMD} $i name $child && cfg=0 1458 fi 1459 fi 1460 if autoif $child; then 1461 ifn_start $child 1462 fi 1463 done 1464 1465 return ${cfg} 1466} 1467 1468# childif_destroy 1469# Destroy child interfaces. 1470# 1471childif_destroy() 1472{ 1473 local cfg child child_vlans child_wlans ifn 1474 cfg=1 1475 1476 child_wlans=`get_if_var $ifn wlans_IF` 1477 for child in ${child_wlans}; do 1478 if ! ifexists $child; then 1479 continue 1480 fi 1481 ${IFCONFIG_CMD} -n $child destroy && cfg=0 1482 done 1483 1484 child_vlans=`get_if_var $ifn vlans_IF` 1485 for child in ${child_vlans}; do 1486 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then 1487 child="${ifn}.${child}" 1488 fi 1489 if ! ifexists $child; then 1490 continue 1491 fi 1492 ${IFCONFIG_CMD} -n $child destroy && cfg=0 1493 done 1494 1495 return ${cfg} 1496} 1497 1498# ng_mkpeer 1499# Create netgraph nodes. 1500# 1501ng_mkpeer() 1502{ 1503 ngctl -f - 2> /dev/null <<EOF 1504mkpeer $* 1505msg dummy nodeinfo 1506EOF 1507} 1508 1509# ng_create_one 1510# Create netgraph nodes. 1511# 1512ng_create_one() 1513{ 1514 local t 1515 1516 ng_mkpeer $* | while read line; do 1517 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'` 1518 if [ -n "${t}" ]; then 1519 echo ${t} 1520 return 1521 fi 1522 done 1523} 1524 1525# ng_fec_create ifn 1526# Configure Fast EtherChannel for interface $ifn. Returns 0 if 1527# FEC arguments were found and configured; returns !0 otherwise. 1528ng_fec_create() 1529{ 1530 local req_iface iface bogus 1531 req_iface="$1" 1532 1533 ngctl shutdown ${req_iface}: > /dev/null 2>&1 1534 1535 bogus="" 1536 while true; do 1537 iface=`ng_create_one fec dummy fec` 1538 if [ -z "${iface}" ]; then 1539 exit 2 1540 fi 1541 if [ "${iface}" = "${req_iface}" ]; then 1542 break 1543 fi 1544 bogus="${bogus} ${iface}" 1545 done 1546 1547 for iface in ${bogus}; do 1548 ngctl shutdown ${iface}: 1549 done 1550} 1551 1552# fec_up 1553# Create Fast EtherChannel interfaces. 1554fec_up() 1555{ 1556 local i j 1557 1558 for i in ${fec_interfaces}; do 1559 ng_fec_create $i 1560 for j in `get_if_var $i fecconfig_IF`; do 1561 case ${j} in 1562 '') 1563 continue 1564 ;; 1565 *) 1566 ngctl msg ${i}: add_iface "\"${j}\"" 1567 ;; 1568 esac 1569 done 1570 done 1571} 1572 1573# ipx_up ifn 1574# Configure any IPX addresses for interface $ifn. Returns 0 if 1575# IPX arguments were found and configured; returns 1 otherwise. 1576# 1577ipx_up() 1578{ 1579 local ifn 1580 ifn="$1" 1581 1582 # ifconfig_IF_ipx 1583 ifconfig_args=`_ifconfig_getargs $ifn ipx` 1584 if [ -n "${ifconfig_args}" ]; then 1585 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args} 1586 return 0 1587 fi 1588 1589 return 1 1590} 1591 1592# ipx_down ifn 1593# Remove IPX addresses for interface $ifn. Returns 0 if IPX 1594# addresses were found and unconfigured. It returns 1, otherwise. 1595# 1596ipx_down() 1597{ 1598 local _if _ifs _ret ipxList oldifs _ipx 1599 _if=$1 1600 _ifs="^" 1601 _ret=1 1602 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`" 1603 oldifs="$IFS" 1604 1605 IFS="$_ifs" 1606 for _ipx in $ipxList ; do 1607 # get rid of extraneous line 1608 [ -z "$_ipx" ] && break 1609 1610 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'` 1611 1612 IFS="$oldifs" 1613 ${IFCONFIG_CMD} ${_if} ${_ipx} delete 1614 IFS="$_ifs" 1615 _ret=0 1616 done 1617 IFS="$oldifs" 1618 1619 return $_ret 1620} 1621 1622# ifnet_rename [ifname] 1623# Rename interfaces if ifconfig_IF_name is defined. 1624# 1625ifnet_rename() 1626{ 1627 local _if _ifname 1628 1629 # ifconfig_IF_name 1630 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do 1631 _ifname=`get_if_var $_if ifconfig_IF_name` 1632 if [ ! -z "$_ifname" ]; then 1633 ${IFCONFIG_CMD} $_if name $_ifname 1634 fi 1635 done 1636 1637 return 0 1638} 1639 1640# list_net_interfaces type 1641# List all network interfaces. The type of interface returned 1642# can be controlled by the type argument. The type 1643# argument can be any of the following: 1644# nodhcp - all interfaces, excluding DHCP configured interfaces 1645# dhcp - list only DHCP configured interfaces 1646# noautoconf - all interfaces, excluding IPv6 Stateless 1647# Address Autoconf configured interfaces 1648# autoconf - list only IPv6 Stateless Address Autoconf 1649# configured interfaces 1650# If no argument is specified all network interfaces are output. 1651# Note that the list will include cloned interfaces if applicable. 1652# Cloned interfaces must already exist to have a chance to appear 1653# in the list if ${network_interfaces} is set to `auto'. 1654# 1655list_net_interfaces() 1656{ 1657 local type _tmplist _list _autolist _lo _if 1658 type=$1 1659 1660 # Get a list of ALL the interfaces and make lo0 first if it's there. 1661 # 1662 _tmplist= 1663 case ${network_interfaces} in 1664 [Aa][Uu][Tt][Oo]) 1665 _autolist="`${IFCONFIG_CMD} -l`" 1666 _lo= 1667 for _if in ${_autolist} ; do 1668 if autoif $_if; then 1669 if [ "$_if" = "lo0" ]; then 1670 _lo="lo0 " 1671 else 1672 _tmplist="${_tmplist} ${_if}" 1673 fi 1674 fi 1675 done 1676 _tmplist="${_lo}${_tmplist# }" 1677 ;; 1678 *) 1679 for _if in ${network_interfaces} ${cloned_interfaces}; do 1680 # epair(4) uses epair[0-9] for creation and 1681 # epair[0-9][ab] for configuration. 1682 case $_if in 1683 epair[0-9]*) 1684 _tmplist="$_tmplist ${_if}a ${_if}b" 1685 ;; 1686 *) 1687 _tmplist="$_tmplist $_if" 1688 ;; 1689 esac 1690 done 1691 # 1692 # lo0 is effectively mandatory, so help prevent foot-shooting 1693 # 1694 case "$_tmplist" in 1695 lo0|'lo0 '*|*' lo0'|*' lo0 '*) 1696 # This is fine, do nothing 1697 _tmplist="${_tmplist# }" 1698 ;; 1699 *) 1700 _tmplist="lo0 ${_tmplist# }" 1701 ;; 1702 esac 1703 ;; 1704 esac 1705 1706 _list= 1707 case "$type" in 1708 nodhcp) 1709 for _if in ${_tmplist} ; do 1710 if ! dhcpif $_if && \ 1711 [ -n "`_ifconfig_getargs $_if`" ]; then 1712 _list="${_list# } ${_if}" 1713 fi 1714 done 1715 ;; 1716 dhcp) 1717 for _if in ${_tmplist} ; do 1718 if dhcpif $_if; then 1719 _list="${_list# } ${_if}" 1720 fi 1721 done 1722 ;; 1723 noautoconf) 1724 for _if in ${_tmplist} ; do 1725 if ! ipv6_autoconfif $_if && \ 1726 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then 1727 _list="${_list# } ${_if}" 1728 fi 1729 done 1730 ;; 1731 autoconf) 1732 for _if in ${_tmplist} ; do 1733 if ipv6_autoconfif $_if; then 1734 _list="${_list# } ${_if}" 1735 fi 1736 done 1737 ;; 1738 *) 1739 _list=${_tmplist} 1740 ;; 1741 esac 1742 1743 echo $_list 1744 1745 return 0 1746} 1747 1748# get_default_if -address_family 1749# Get the interface of the default route for the given address family. 1750# The -address_family argument must be suitable passing to route(8). 1751# 1752get_default_if() 1753{ 1754 local routeget oldifs defif line 1755 defif= 1756 oldifs="$IFS" 1757 IFS=" 1758" 1759 for line in `route -n get $1 default 2>/dev/null`; do 1760 case $line in 1761 *interface:*) 1762 defif=${line##*: } 1763 ;; 1764 esac 1765 done 1766 IFS=${oldifs} 1767 1768 echo $defif 1769} 1770 1771# hexdigit arg 1772# Echo decimal number $arg (single digit) in hexadecimal format. 1773hexdigit() 1774{ 1775 printf '%x\n' "$1" 1776} 1777 1778# hexprint arg 1779# Echo decimal number $arg (multiple digits) in hexadecimal format. 1780hexprint() 1781{ 1782 printf '%x\n' "$1" 1783} 1784 1785is_wired_interface() 1786{ 1787 local media 1788 1789 case `${IFCONFIG_CMD} $1 2>/dev/null` in 1790 *media:?Ethernet*) media=Ethernet ;; 1791 esac 1792 1793 test "$media" = "Ethernet" 1794} 1795 1796# network6_getladdr if [flag] 1797# Echo link-local address from $if if any. 1798# If flag is defined, tentative ones will be excluded. 1799network6_getladdr() 1800{ 1801 local _if _flag proto addr rest 1802 _if=$1 1803 _flag=$2 1804 1805 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do 1806 case "${proto}/${addr}/${_flag}/${rest}" in 1807 inet6/fe80::*//*) 1808 echo ${addr} 1809 ;; 1810 inet6/fe80:://*tentative*) # w/o flag 1811 sleep `${SYSCTL_N} net.inet6.ip6.dad_count` 1812 network6_getladdr $_if $_flags 1813 ;; 1814 inet6/fe80::/*/*tentative*) # w/ flag 1815 echo ${addr} 1816 ;; 1817 *) 1818 continue 1819 ;; 1820 esac 1821 1822 return 1823 done 1824} 1825