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: releng/10.3/etc/network.subr 279327 2015-02-26 19:56:48Z rpaulo $ 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 # inet6 specific 136 if ! noafif $1 && afexists inet6; then 137 if checkyesno ipv6_activate_all_interfaces; then 138 _ipv6_opts="-ifdisabled" 139 elif [ "$1" != "lo0" ]; then 140 _ipv6_opts="ifdisabled" 141 fi 142 143 # backward compatibility: $ipv6_enable 144 case $ipv6_enable in 145 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 146 case $1 in 147 bridge[0-9]*) 148 # No accept_rtadv by default on if_bridge(4) 149 # to avoid a conflict with the member 150 # interfaces. 151 ;; 152 *) 153 if ! checkyesno ipv6_gateway_enable; then 154 _ipv6_opts="${_ipv6_opts} accept_rtadv" 155 fi 156 ;; 157 esac 158 ;; 159 esac 160 161 case $ipv6_cpe_wanif in 162 $1) 163 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv" 164 ;; 165 esac 166 167 if [ -n "${_ipv6_opts}" ]; then 168 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts} 169 fi 170 fi 171 172 # ifconfig_IF 173 ifconfig_args=`ifconfig_getargs $1` 174 if [ -n "${ifconfig_args}" ]; then 175 eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 176 _cfg=0 177 fi 178 179 # inet6 specific 180 if ! noafif $1 && afexists inet6; then 181 # ifconfig_IF_ipv6 182 ifconfig_args=`ifconfig_getargs $1 ipv6` 183 if [ -n "${ifconfig_args}" ]; then 184 # backward compatibility: inet6 keyword 185 case "${ifconfig_args}" in 186 :*|[0-9a-fA-F]*:*) 187 warn "\$ifconfig_$1_ipv6 needs leading" \ 188 "\"inet6\" keyword for an IPv6 address." 189 ifconfig_args="inet6 ${ifconfig_args}" 190 ;; 191 esac 192 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 193 eval ${IFCONFIG_CMD} $1 ${ifconfig_args} 194 _cfg=0 195 fi 196 197 # $ipv6_prefix_IF will be handled in 198 # ipv6_prefix_hostid_addr_common(). 199 ifconfig_args=`get_if_var $1 ipv6_prefix_IF` 200 if [ -n "${ifconfig_args}" ]; then 201 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 202 _cfg=0 203 fi 204 205 # backward compatibility: $ipv6_ifconfig_IF 206 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF` 207 if [ -n "${ifconfig_args}" ]; then 208 warn "\$ipv6_ifconfig_$1 is obsolete." \ 209 " Use ifconfig_$1_ipv6 instead." 210 ${IFCONFIG_CMD} $1 inet6 -ifdisabled 211 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args} 212 _cfg=0 213 fi 214 fi 215 216 ifalias $1 link alias 217 ifalias $1 ether alias 218 219 if [ ${_cfg} -eq 0 ]; then 220 ${IFCONFIG_CMD} $1 up 221 fi 222 223 if wpaif $1; then 224 /etc/rc.d/wpa_supplicant start $1 225 _cfg=0 # XXX: not sure this should count 226 elif hostapif $1; then 227 /etc/rc.d/hostapd start $1 228 _cfg=0 229 fi 230 231 if dhcpif $1; then 232 if [ $_cfg -ne 0 ] ; then 233 ${IFCONFIG_CMD} $1 up 234 fi 235 if syncdhcpif $1; then 236 /etc/rc.d/dhclient start $1 237 fi 238 _cfg=0 239 fi 240 241 return $_cfg 242} 243 244# ifconfig_down if 245# returns 1 if wpa_supplicant or dhclient was stopped or 246# the interface exists. 247# 248ifconfig_down() 249{ 250 local _cfg 251 _cfg=1 252 253 if wpaif $1; then 254 /etc/rc.d/wpa_supplicant stop $1 255 _cfg=0 256 elif hostapif $1; then 257 /etc/rc.d/hostapd stop $1 258 _cfg=0 259 fi 260 261 if dhcpif $1; then 262 /etc/rc.d/dhclient stop $1 263 _cfg=0 264 fi 265 266 if ifexists $1; then 267 ${IFCONFIG_CMD} $1 down 268 _cfg=0 269 fi 270 271 return $_cfg 272} 273 274# get_if_var if var [default] 275# Return the value of the pseudo-hash corresponding to $if where 276# $var is a string containg the sub-string "IF" which will be 277# replaced with $if after the characters defined in _punct are 278# replaced with '_'. If the variable is unset, replace it with 279# $default if given. 280get_if_var() 281{ 282 local _if _punct _punct_c _var _default prefix suffix 283 284 if [ $# -ne 2 -a $# -ne 3 ]; then 285 err 3 'USAGE: get_if_var name var [default]' 286 fi 287 288 _if=$1 289 _punct=".-/+" 290 ltr ${_if} "${_punct}" '_' _if 291 _var=$2 292 _default=$3 293 294 prefix=${_var%%IF*} 295 suffix=${_var##*IF} 296 eval echo \${${prefix}${_if}${suffix}-${_default}} 297} 298 299# _ifconfig_getargs if [af] 300# Prints the arguments for the supplied interface to stdout. 301# Returns 1 if empty. In general, ifconfig_getargs should be used 302# outside this file. 303_ifconfig_getargs() 304{ 305 local _ifn _af 306 _ifn=$1 307 _af=${2+_$2} 308 309 if [ -z "$_ifn" ]; then 310 return 1 311 fi 312 313 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT" 314} 315 316# ifconfig_getargs if [af] 317# Takes the result from _ifconfig_getargs and removes pseudo 318# args such as DHCP and WPA. 319ifconfig_getargs() 320{ 321 local _tmpargs _arg _args _vnet 322 _tmpargs=`_ifconfig_getargs $1 $2` 323 if [ $? -eq 1 ]; then 324 return 1 325 fi 326 _args= 327 _vnet=0 328 329 for _arg in $_tmpargs; do 330 case $_arg:$_vnet in 331 [Dd][Hh][Cc][Pp]:0) ;; 332 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;; 333 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;; 334 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;; 335 [Ww][Pp][Aa]:0) ;; 336 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;; 337 vnet:0) _vnet=1 ;; 338 *:1) _vnet=0 ;; 339 *:0) 340 _args="$_args $_arg" 341 ;; 342 esac 343 done 344 345 echo $_args 346} 347 348# autoif 349# Returns 0 if the interface should be automatically configured at 350# boot time and 1 otherwise. 351autoif() 352{ 353 local _tmpargs _arg 354 _tmpargs=`_ifconfig_getargs $1` 355 356 for _arg in $_tmpargs; do 357 case $_arg in 358 [Nn][Oo][Aa][Uu][Tt][Oo]) 359 return 1 360 ;; 361 esac 362 done 363 364 return 0 365} 366 367# dhcpif if 368# Returns 0 if the interface is a DHCP interface and 1 otherwise. 369dhcpif() 370{ 371 local _tmpargs _arg 372 _tmpargs=`_ifconfig_getargs $1` 373 374 case $1 in 375 lo[0-9]*|\ 376 stf[0-9]*|\ 377 faith[0-9]*|\ 378 lp[0-9]*|\ 379 sl[0-9]*) 380 return 1 381 ;; 382 esac 383 if noafif $1; then 384 return 1 385 fi 386 387 for _arg in $_tmpargs; do 388 case $_arg in 389 [Dd][Hh][Cc][Pp]) 390 return 0 391 ;; 392 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 393 return 0 394 ;; 395 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 396 return 0 397 ;; 398 esac 399 done 400 401 return 1 402} 403 404# syncdhcpif 405# Returns 0 if the interface should be configured synchronously and 406# 1 otherwise. 407syncdhcpif() 408{ 409 local _tmpargs _arg 410 _tmpargs=`_ifconfig_getargs $1` 411 412 if noafif $1; then 413 return 1 414 fi 415 416 for _arg in $_tmpargs; do 417 case $_arg in 418 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 419 return 1 420 ;; 421 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) 422 return 0 423 ;; 424 esac 425 done 426 427 checkyesno synchronous_dhclient 428} 429 430# wpaif if 431# Returns 0 if the interface is a WPA interface and 1 otherwise. 432wpaif() 433{ 434 local _tmpargs _arg 435 _tmpargs=`_ifconfig_getargs $1` 436 437 for _arg in $_tmpargs; do 438 case $_arg in 439 [Ww][Pp][Aa]) 440 return 0 441 ;; 442 esac 443 done 444 445 return 1 446} 447 448# hostapif if 449# Returns 0 if the interface is a HOSTAP interface and 1 otherwise. 450hostapif() 451{ 452 local _tmpargs _arg 453 _tmpargs=`_ifconfig_getargs $1` 454 455 for _arg in $_tmpargs; do 456 case $_arg in 457 [Hh][Oo][Ss][Tt][Aa][Pp]) 458 return 0 459 ;; 460 esac 461 done 462 463 return 1 464} 465 466# vnetif if 467# Returns 0 and echo jail if "vnet" keyword is specified on the 468# interface, and 1 otherwise. 469vnetif() 470{ 471 local _tmpargs _arg _vnet 472 _tmpargs=`_ifconfig_getargs $1` 473 474 _vnet=0 475 for _arg in $_tmpargs; do 476 case $_arg:$_vnet in 477 vnet:0) _vnet=1 ;; 478 *:1) echo $_arg; return 0 ;; 479 esac 480 done 481 482 return 1 483} 484 485# afexists af 486# Returns 0 if the address family is enabled in the kernel 487# 1 otherwise. 488afexists() 489{ 490 local _af 491 _af=$1 492 493 case ${_af} in 494 inet|inet6) 495 check_kern_features ${_af} 496 ;; 497 ipx) 498 ${SYSCTL_N} net.ipx > /dev/null 2>&1 499 ;; 500 atm) 501 if [ -x /sbin/atmconfig ]; then 502 /sbin/atmconfig diag list > /dev/null 2>&1 503 else 504 return 1 505 fi 506 ;; 507 link|ether) 508 return 0 509 ;; 510 *) 511 err 1 "afexists(): Unsupported address family: $_af" 512 ;; 513 esac 514} 515 516# noafif if 517# Returns 0 if the interface has no af configuration and 1 otherwise. 518noafif() 519{ 520 local _if 521 _if=$1 522 523 case $_if in 524 pflog[0-9]*|\ 525 pfsync[0-9]*|\ 526 usbus[0-9]*|\ 527 an[0-9]*|\ 528 ath[0-9]*|\ 529 ipw[0-9]*|\ 530 ipfw[0-9]*|\ 531 iwi[0-9]*|\ 532 iwn[0-9]*|\ 533 ral[0-9]*|\ 534 wi[0-9]*|\ 535 wl[0-9]*|\ 536 wpi[0-9]*) 537 return 0 538 ;; 539 esac 540 541 return 1 542} 543 544# ipv6if if 545# Returns 0 if the interface should be configured for IPv6 and 546# 1 otherwise. 547ipv6if() 548{ 549 local _if _tmpargs i 550 _if=$1 551 552 if ! afexists inet6; then 553 return 1 554 fi 555 556 # lo0 is always IPv6-enabled 557 case $_if in 558 lo0) 559 return 0 560 ;; 561 esac 562 563 case "${ipv6_network_interfaces}" in 564 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo]) 565 # True if $ifconfig_IF_ipv6 is defined. 566 _tmpargs=`_ifconfig_getargs $_if ipv6` 567 if [ -n "${_tmpargs}" ]; then 568 return 0 569 fi 570 571 # True if $ipv6_prefix_IF is defined. 572 _tmpargs=`get_if_var $_if ipv6_prefix_IF` 573 if [ -n "${_tmpargs}" ]; then 574 return 0 575 fi 576 577 # backward compatibility: True if $ipv6_ifconfig_IF is defined. 578 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 579 if [ -n "${_tmpargs}" ]; then 580 return 0 581 fi 582 ;; 583 esac 584 585 return 1 586} 587 588# ipv6_autoconfif if 589# Returns 0 if the interface should be configured for IPv6 with 590# Stateless Address Configuration; 1 otherwise. 591ipv6_autoconfif() 592{ 593 local _if _tmpargs _arg 594 _if=$1 595 596 case $_if in 597 lo[0-9]*|\ 598 stf[0-9]*|\ 599 faith[0-9]*|\ 600 lp[0-9]*|\ 601 sl[0-9]*) 602 return 1 603 ;; 604 esac 605 if noafif $_if; then 606 return 1 607 fi 608 if ! ipv6if $_if; then 609 return 1 610 fi 611 if checkyesno ipv6_gateway_enable; then 612 return 1 613 fi 614 _tmpargs=`get_if_var $_if ipv6_prefix_IF` 615 if [ -n "${_tmpargs}" ]; then 616 return 1 617 fi 618 # backward compatibility: $ipv6_enable 619 case $ipv6_enable in 620 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 621 if checkyesno ipv6_gateway_enable; then 622 return 1 623 fi 624 case $1 in 625 bridge[0-9]*) 626 # No accept_rtadv by default on if_bridge(4) 627 # to avoid a conflict with the member 628 # interfaces. 629 return 1 630 ;; 631 *) 632 return 0 633 ;; 634 esac 635 ;; 636 esac 637 638 _tmpargs=`_ifconfig_getargs $_if ipv6` 639 for _arg in $_tmpargs; do 640 case $_arg in 641 accept_rtadv) 642 return 0 643 ;; 644 esac 645 done 646 647 # backward compatibility: $ipv6_ifconfig_IF 648 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF` 649 for _arg in $_tmpargs; do 650 case $_arg in 651 accept_rtadv) 652 return 0 653 ;; 654 esac 655 done 656 657 return 1 658} 659 660# ifexists if 661# Returns 0 if the interface exists and 1 otherwise. 662ifexists() 663{ 664 [ -z "$1" ] && return 1 665 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1 666} 667 668# ipv4_up if 669# add IPv4 addresses to the interface $if 670ipv4_up() 671{ 672 local _if _ret 673 _if=$1 674 _ret=1 675 676 # Add 127.0.0.1/8 to lo0 unless otherwise specified. 677 if [ "${_if}" = "lo0" ]; then 678 ifconfig_args=`get_if_var ${_if} ifconfig_IF` 679 if [ -z "${ifconfig_args}" ]; then 680 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias 681 fi 682 fi 683 ifalias ${_if} inet alias && _ret=0 684 685 return $_ret 686} 687 688# ipv6_up if 689# add IPv6 addresses to the interface $if 690ipv6_up() 691{ 692 local _if _ret 693 _if=$1 694 _ret=1 695 696 if ! ipv6if $_if; then 697 return 0 698 fi 699 700 ifalias ${_if} inet6 alias && _ret=0 701 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0 702 ipv6_accept_rtadv_up ${_if} && _ret=0 703 704 return $_ret 705} 706 707# ipv4_down if 708# remove IPv4 addresses from the interface $if 709ipv4_down() 710{ 711 local _if _ifs _ret inetList oldifs _inet 712 _if=$1 713 _ifs="^" 714 _ret=1 715 716 ifalias ${_if} inet -alias && _ret=0 717 718 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`" 719 720 oldifs="$IFS" 721 IFS="$_ifs" 722 for _inet in $inetList ; do 723 # get rid of extraneous line 724 case $_inet in 725 inet\ *) ;; 726 *) continue ;; 727 esac 728 729 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'` 730 731 IFS="$oldifs" 732 ${IFCONFIG_CMD} ${_if} ${_inet} delete 733 IFS="$_ifs" 734 _ret=0 735 done 736 IFS="$oldifs" 737 738 return $_ret 739} 740 741# ipv6_down if 742# remove IPv6 addresses from the interface $if 743ipv6_down() 744{ 745 local _if _ifs _ret inetList oldifs _inet6 746 _if=$1 747 _ifs="^" 748 _ret=1 749 750 if ! ipv6if $_if; then 751 return 0 752 fi 753 754 ipv6_accept_rtadv_down ${_if} && _ret=0 755 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0 756 ifalias ${_if} inet6 -alias && _ret=0 757 758 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`" 759 760 oldifs="$IFS" 761 IFS="$_ifs" 762 for _inet6 in $inetList ; do 763 # get rid of extraneous line 764 case $_inet6 in 765 inet6\ *) ;; 766 *) continue ;; 767 esac 768 769 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'` 770 771 IFS="$oldifs" 772 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias 773 IFS="$_ifs" 774 _ret=0 775 done 776 IFS="$oldifs" 777 778 return $_ret 779} 780 781# ifalias if af action 782# Configure or remove aliases for network interface $if. 783# It returns 0 if at least one alias was configured or 784# removed, or 1 if there were none. 785# 786ifalias() 787{ 788 local _ret 789 _ret=1 790 791 afexists $2 || return $_ret 792 793 case "$2" in 794 inet|inet6|link|ether) 795 ifalias_af_common $1 $2 $3 && _ret=0 796 ;; 797 esac 798 799 return $_ret 800} 801 802# ifalias_expand_addr af action addr 803# Expand address range ("N-M") specification in addr. 804# "addr" must not include an address-family keyword. 805# The results will include an address-family keyword. 806# 807ifalias_expand_addr() 808{ 809 local _af _action 810 811 _af=$1 812 _action=$2 813 shift 2 814 815 afexists $_af || return 816 ifalias_expand_addr_$_af $_action $* 817} 818 819# ifalias_expand_addr_inet action addr 820# Helper function for ifalias_expand_addr(). Handles IPv4. 821# 822ifalias_expand_addr_inet() 823{ 824 local _action _arg _cidr _cidr_addr _exargs 825 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount 826 local _retstr _c 827 _action=$1 828 _arg=$2 829 shift 2 830 _exargs=$* 831 _retstr= 832 833 case $_action:$_arg:$_exargs in 834 *:*--*) return ;; # invalid 835 tmp:*[0-9]-[0-9]*:*) # to be expanded 836 _action="alias" 837 ;; 838 *:*[0-9]-[0-9]*:*) # to be expanded 839 ;; 840 tmp:*:*netmask*) # already expanded w/ netmask option 841 echo ${_arg%/[0-9]*} $_exargs && return 842 ;; 843 tmp:*:*) # already expanded w/o netmask option 844 echo $_arg $_exargs && return 845 ;; 846 *:*:*netmask*) # already expanded w/ netmask option 847 echo inet ${_arg%/[0-9]*} $_exargs && return 848 ;; 849 *:*:*) # already expanded w/o netmask option 850 echo inet $_arg $_exargs && return 851 ;; 852 esac 853 854 for _cidr in $_arg; do 855 _ipaddr=${_cidr%%/*} 856 _plen=${_cidr##*/} 857 # When subnet prefix length is not specified, use /32. 858 case $_plen in 859 $_ipaddr) _plen=32 ;; # "/" character not found 860 esac 861 862 OIFS=$IFS 863 IFS=. set -- $_ipaddr 864 _range= 865 _iphead= 866 _iptail= 867 for _c in $@; do 868 case $_range:$_c in 869 :[0-9]*-[0-9]*) 870 _range=$_c 871 ;; 872 :*) 873 _iphead="${_iphead}${_iphead:+.}${_c}" 874 ;; 875 *:*) 876 _iptail="${_iptail}${_iptail:+.}${_c}" 877 ;; 878 esac 879 done 880 IFS=$OIFS 881 _iplow=${_range%-*} 882 _iphigh=${_range#*-} 883 884 # clear netmask when removing aliases 885 if [ "$_action" = "-alias" ]; then 886 _plen="" 887 fi 888 889 _ipcount=$_iplow 890 while [ "$_ipcount" -le "$_iphigh" ]; do 891 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}" 892 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then 893 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." 894 break 895 else 896 _ipcount=$(($_ipcount + 1)) 897 fi 898 # Forcibly set /32 for remaining aliases. 899 _plen=32 900 done 901 done 902 903 for _c in $_retstr; do 904 ifalias_expand_addr_inet $_action $_c $_exargs 905 done 906} 907 908# ifalias_expand_addr_inet6 action addr 909# Helper function for ifalias_expand_addr(). Handles IPv6. 910# 911ifalias_expand_addr_inet6() 912{ 913 local _action _arg _cidr _cidr_addr _exargs 914 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount 915 local _ipv4part 916 local _retstr _c 917 _action=$1 918 _arg=$2 919 shift 2 920 _exargs=$* 921 _retstr= 922 923 case $_action:$_arg:$_exargs in 924 *:*--*:*) return ;; # invalid 925 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded 926 _action="alias" 927 ;; 928 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded 929 ;; 930 tmp:*:*prefixlen*) # already expanded w/ prefixlen option 931 echo ${_arg%/[0-9]*} $_exargs && return 932 ;; 933 tmp:*:*) # already expanded w/o prefixlen option 934 echo $_arg $_exargs && return 935 ;; 936 *:*:*prefixlen*) # already expanded w/ prefixlen option 937 echo inet6 ${_arg%/[0-9]*} $_exargs && return 938 ;; 939 *:*:*) # already expanded w/o prefixlen option 940 echo inet6 $_arg $_exargs && return 941 ;; 942 esac 943 944 for _cidr in $_arg; do 945 _ipaddr="${_cidr%%/*}" 946 _plen="${_cidr##*/}" 947 948 case $_action:$_ipaddr:$_cidr in 949 -alias:*:*) unset _plen ;; 950 *:$_cidr:$_ipaddr) unset _plen ;; 951 esac 952 953 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then 954 # Handle !v4mapped && !v4compat addresses. 955 956 # The default prefix length is 64. 957 case $_ipaddr:$_cidr in 958 $_cidr:$_ipaddr) _plen="64" ;; 959 esac 960 _ipleft=${_ipaddr%-*} 961 _ipright=${_ipaddr#*-} 962 _iplow=${_ipleft##*:} 963 _iphigh=${_ipright%%:*} 964 _ipleft=${_ipleft%:*} 965 _ipright=${_ipright#*:} 966 967 if [ "$_iphigh" = "$_ipright" ]; then 968 unset _ipright 969 else 970 _ipright=:$_ipright 971 fi 972 973 if [ -n "$_iplow" -a -n "$_iphigh" ]; then 974 _iplow=$((0x$_iplow)) 975 _iphigh=$((0x$_iphigh)) 976 _ipcount=$_iplow 977 while [ $_ipcount -le $_iphigh ]; do 978 _r=`printf "%s:%04x%s%s" \ 979 $_ipleft $_ipcount $_ipright \ 980 ${_plen:+/}$_plen` 981 _retstr="$_retstr $_r" 982 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ] 983 then 984 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." 985 break 986 else 987 _ipcount=$(($_ipcount + 1)) 988 fi 989 done 990 else 991 _retstr="${_ipaddr}${_plen:+/}${_plen}" 992 fi 993 994 for _c in $_retstr; do 995 ifalias_expand_addr_inet6 $_action $_c $_exargs 996 done 997 else 998 # v4mapped/v4compat should handle as an IPv4 alias 999 _ipv4part=${_ipaddr##*:} 1000 1001 # Adjust prefix length if any. If not, set the 1002 # default prefix length as 32. 1003 case $_ipaddr:$_cidr in 1004 $_cidr:$_ipaddr) _plen=32 ;; 1005 *) _plen=$(($_plen - 96)) ;; 1006 esac 1007 1008 _retstr=`ifalias_expand_addr_inet \ 1009 tmp ${_ipv4part}${_plen:+/}${_plen}` 1010 for _c in $_retstr; do 1011 ifalias_expand_addr_inet $_action $_c $_exargs 1012 done 1013 fi 1014 done 1015} 1016 1017# ifalias_af_common_handler if af action args 1018# Helper function for ifalias_af_common(). 1019# 1020ifalias_af_common_handler() 1021{ 1022 local _ret _if _af _action _args _c _tmpargs 1023 1024 _ret=1 1025 _if=$1 1026 _af=$2 1027 _action=$3 1028 shift 3 1029 _args=$* 1030 1031 case $_args in 1032 ${_af}\ *) ;; 1033 *) return ;; 1034 esac 1035 1036 # link(ether) does not support address removal. 1037 case $_af:$_action in 1038 link:-alias|ether:-alias) return ;; 1039 esac 1040 1041 _tmpargs= 1042 for _c in $_args; do 1043 case $_c in 1044 ${_af}) 1045 case $_tmpargs in 1046 ${_af}\ *[0-9a-fA-F]-*) 1047 ifalias_af_common_handler $_if $_af $_action \ 1048 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 1049 ;; 1050 ${_af}\ *) 1051 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 1052 ;; 1053 esac 1054 _tmpargs=$_af 1055 ;; 1056 *) 1057 _tmpargs="$_tmpargs $_c" 1058 ;; 1059 esac 1060 done 1061 # Process the last component if any. 1062 if [ -n "$_tmpargs}" ]; then 1063 case $_tmpargs in 1064 ${_af}\ *[0-9a-fA-F]-*) 1065 ifalias_af_common_handler $_if $_af $_action \ 1066 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }` 1067 ;; 1068 ${_af}\ *) 1069 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 1070 ;; 1071 esac 1072 fi 1073 1074 return $_ret 1075} 1076 1077# ifalias_af_common if af action 1078# Helper function for ifalias(). 1079# 1080ifalias_af_common() 1081{ 1082 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf 1083 local _vif _punct=".-/+" 1084 1085 _ret=1 1086 _aliasn= 1087 _if=$1 1088 _af=$2 1089 _action=$3 1090 1091 # Normalize $_if before using it in a pattern to list_vars() 1092 ltr "$_if" "$_punct" "_" _vif 1093 1094 # ifconfig_IF_aliasN which starts with $_af 1095 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* | 1096 sort_lite -nk1.$((9+${#_vif}+7))` 1097 do 1098 eval ifconfig_args=\"\$$alias\" 1099 _iaf= 1100 case $ifconfig_args in 1101 inet\ *) _iaf=inet ;; 1102 inet6\ *) _iaf=inet6 ;; 1103 ipx\ *) _iaf=ipx ;; 1104 link\ *) _iaf=link ;; 1105 ether\ *) _iaf=ether ;; 1106 esac 1107 1108 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in 1109 ${_af}:*:${_af}:*) 1110 _aliasn="$_aliasn $ifconfig_args" 1111 ;; 1112 ${_af}:*:"":"") 1113 break 1114 ;; 1115 inet:alias:"":*) 1116 _aliasn="$_aliasn inet $ifconfig_args" 1117 warn "\$${alias} needs leading" \ 1118 "\"inet\" keyword for an IPv4 address." 1119 esac 1120 done 1121 1122 # backward compatibility: ipv6_ifconfig_IF_aliasN. 1123 case $_af in 1124 inet6) 1125 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* | 1126 sort_lite -nk1.$((14+${#_vif}+7))` 1127 do 1128 eval ifconfig_args=\"\$$alias\" 1129 case ${_action}:"${ifconfig_args}" in 1130 *:"") 1131 break 1132 ;; 1133 alias:*) 1134 _aliasn="${_aliasn} inet6 ${ifconfig_args}" 1135 warn "\$${alias} is obsolete. " \ 1136 "Use ifconfig_${_vif}_aliasN 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