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