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