2 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
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.
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
27 IFCONFIG_CMD="/sbin/ifconfig"
28 : ${netif_ipexpand_max:=2048}
31 # Subroutines commonly used from network startup scripts.
32 # Requires that rc.conf be loaded first.
36 # Bring up and configure an interface. If some configuration is
37 # applied, print the interface configuration.
45 [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
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
54 childif_create ${ifn} && cfg=0
60 # Shutdown and de-configure an interface. If action is taken,
61 # print the interface name.
69 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
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
76 ifconfig_down ${ifn} && cfg=0
77 ifscript_down ${ifn} && cfg=0
78 childif_destroy ${ifn} && cfg=0
84 # Move ifn to the specified vnet jail.
93 # Reclaim ifn from the specified vnet jail.
101 # ifn_vnet0 ifn action
102 # Helper function for ifn_vnetup and ifn_vnetdown.
106 local _ifn _cfg _action _vnet
111 if _vnet=$(vnetif $_ifn); then
112 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
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
127 local _cfg _ifconfig_descr _ipv6_opts ifconfig_args
130 # Make sure lo0 always comes up.
131 if [ "$1" = "lo0" ]; then
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"
143 # backward compatibility: $ipv6_enable
145 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
148 # No accept_rtadv by default on if_bridge(4)
149 # to avoid a conflict with the member
153 if ! checkyesno ipv6_gateway_enable; then
154 _ipv6_opts="${_ipv6_opts} accept_rtadv"
161 case $ipv6_cpe_wanif in
163 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
167 if [ -n "${_ipv6_opts}" ]; then
168 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
173 ifconfig_args=`ifconfig_getargs $1`
174 if [ -n "${ifconfig_args}" ]; then
175 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
180 if ! noafif $1 && afexists inet6; then
182 ifconfig_args=`ifconfig_getargs $1 ipv6`
183 if [ -n "${ifconfig_args}" ]; then
184 # backward compatibility: inet6 keyword
185 case "${ifconfig_args}" in
187 warn "\$ifconfig_$1_ipv6 needs leading" \
188 "\"inet6\" keyword for an IPv6 address."
189 ifconfig_args="inet6 ${ifconfig_args}"
192 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
193 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
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
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}
216 ifalias $1 link alias
217 ifalias $1 ether alias
219 if [ ${_cfg} -eq 0 ]; then
220 ${IFCONFIG_CMD} $1 up
223 _ifconfig_descr=`get_if_var $1 ifconfig_IF_descr`
224 if [ -n "${_ifconfig_descr}" ]; then
225 ${IFCONFIG_CMD} $1 description "${_ifconfig_descr}"
229 /etc/rc.d/wpa_supplicant start $1
230 _cfg=0 # XXX: not sure this should count
231 elif hostapif $1; then
232 /etc/rc.d/hostapd start $1
237 if [ $_cfg -ne 0 ] ; then
238 ${IFCONFIG_CMD} $1 up
240 if syncdhcpif $1; then
241 /etc/rc.d/dhclient start $1
250 # returns 1 if wpa_supplicant or dhclient was stopped or
251 # the interface exists.
259 /etc/rc.d/wpa_supplicant stop $1
261 elif hostapif $1; then
262 /etc/rc.d/hostapd stop $1
267 /etc/rc.d/dhclient stop $1
272 ${IFCONFIG_CMD} $1 down
279 # get_if_var if var [default]
280 # Return the value of the pseudo-hash corresponding to $if where
281 # $var is a string containg the sub-string "IF" which will be
282 # replaced with $if after the characters defined in _punct are
283 # replaced with '_'. If the variable is unset, replace it with
287 local _if _punct _punct_c _var _default prefix suffix
289 if [ $# -ne 2 -a $# -ne 3 ]; then
290 err 3 'USAGE: get_if_var name var [default]'
295 ltr ${_if} "${_punct}" '_' _if
301 eval echo \${${prefix}${_if}${suffix}-${_default}}
304 # _ifconfig_getargs if [af]
305 # Prints the arguments for the supplied interface to stdout.
306 # Returns 1 if empty. In general, ifconfig_getargs should be used
314 if [ -z "$_ifn" ]; then
318 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
321 # ifconfig_getargs if [af]
322 # Takes the result from _ifconfig_getargs and removes pseudo
323 # args such as DHCP and WPA.
326 local _tmpargs _arg _args _vnet
327 _tmpargs=`_ifconfig_getargs $1 $2`
328 if [ $? -eq 1 ]; then
334 for _arg in $_tmpargs; do
336 [Dd][Hh][Cc][Pp]:0) ;;
337 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
338 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
339 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
341 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
354 # Returns 0 if the interface should be automatically configured at
355 # boot time and 1 otherwise.
359 _tmpargs=`_ifconfig_getargs $1`
361 for _arg in $_tmpargs; do
363 [Nn][Oo][Aa][Uu][Tt][Oo])
373 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
377 _tmpargs=`_ifconfig_getargs $1`
392 for _arg in $_tmpargs; do
397 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
400 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
410 # Returns 0 if the interface should be configured synchronously and
415 _tmpargs=`_ifconfig_getargs $1`
421 for _arg in $_tmpargs; do
423 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
426 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
432 checkyesno synchronous_dhclient
436 # Returns 0 if the interface is a WPA interface and 1 otherwise.
440 _tmpargs=`_ifconfig_getargs $1`
442 for _arg in $_tmpargs; do
454 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
458 _tmpargs=`_ifconfig_getargs $1`
460 for _arg in $_tmpargs; do
462 [Hh][Oo][Ss][Tt][Aa][Pp])
472 # Returns 0 and echo jail if "vnet" keyword is specified on the
473 # interface, and 1 otherwise.
476 local _tmpargs _arg _vnet
477 _tmpargs=`_ifconfig_getargs $1`
480 for _arg in $_tmpargs; do
483 *:1) echo $_arg; return 0 ;;
491 # Returns 0 if the address family is enabled in the kernel
500 check_kern_features ${_af}
503 ${SYSCTL_N} net.ipx > /dev/null 2>&1
506 if [ -x /sbin/atmconfig ]; then
507 /sbin/atmconfig diag list > /dev/null 2>&1
516 err 1 "afexists(): Unsupported address family: $_af"
522 # Returns 0 if the interface has no af configuration and 1 otherwise.
550 # Returns 0 if the interface should be configured for IPv6 and
557 if ! afexists inet6; then
561 # lo0 is always IPv6-enabled
568 case "${ipv6_network_interfaces}" in
569 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
570 # True if $ifconfig_IF_ipv6 is defined.
571 _tmpargs=`_ifconfig_getargs $_if ipv6`
572 if [ -n "${_tmpargs}" ]; then
576 # True if $ipv6_prefix_IF is defined.
577 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
578 if [ -n "${_tmpargs}" ]; then
582 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
583 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
584 if [ -n "${_tmpargs}" ]; then
594 # Returns 0 if the interface should be configured for IPv6 with
595 # Stateless Address Configuration; 1 otherwise.
598 local _if _tmpargs _arg
613 if ! ipv6if $_if; then
616 if checkyesno ipv6_gateway_enable; then
619 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
620 if [ -n "${_tmpargs}" ]; then
623 # backward compatibility: $ipv6_enable
625 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
626 if checkyesno ipv6_gateway_enable; then
631 # No accept_rtadv by default on if_bridge(4)
632 # to avoid a conflict with the member
643 _tmpargs=`_ifconfig_getargs $_if ipv6`
644 for _arg in $_tmpargs; do
652 # backward compatibility: $ipv6_ifconfig_IF
653 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
654 for _arg in $_tmpargs; do
666 # Returns 0 if the interface exists and 1 otherwise.
669 [ -z "$1" ] && return 1
670 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
674 # add IPv4 addresses to the interface $if
681 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
682 if [ "${_if}" = "lo0" ]; then
683 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
684 if [ -z "${ifconfig_args}" ]; then
685 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
688 ifalias ${_if} inet alias && _ret=0
694 # add IPv6 addresses to the interface $if
701 if ! ipv6if $_if; then
705 ifalias ${_if} inet6 alias && _ret=0
706 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
707 ipv6_accept_rtadv_up ${_if} && _ret=0
713 # remove IPv4 addresses from the interface $if
716 local _if _ifs _ret inetList oldifs _inet
721 ifalias ${_if} inet -alias && _ret=0
723 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
727 for _inet in $inetList ; do
728 # get rid of extraneous line
734 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
737 ${IFCONFIG_CMD} ${_if} ${_inet} delete
747 # remove IPv6 addresses from the interface $if
750 local _if _ifs _ret inetList oldifs _inet6
755 if ! ipv6if $_if; then
759 ipv6_accept_rtadv_down ${_if} && _ret=0
760 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
761 ifalias ${_if} inet6 -alias && _ret=0
763 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
767 for _inet6 in $inetList ; do
768 # get rid of extraneous line
774 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
777 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
786 # ifalias if af action
787 # Configure or remove aliases for network interface $if.
788 # It returns 0 if at least one alias was configured or
789 # removed, or 1 if there were none.
796 afexists $2 || return $_ret
799 inet|inet6|link|ether)
800 ifalias_af_common $1 $2 $3 && _ret=0
807 # ifalias_expand_addr af action addr
808 # Expand address range ("N-M") specification in addr.
809 # "addr" must not include an address-family keyword.
810 # The results will include an address-family keyword.
812 ifalias_expand_addr()
820 afexists $_af || return
821 ifalias_expand_addr_$_af $_action $*
824 # ifalias_expand_addr_inet action addr
825 # Helper function for ifalias_expand_addr(). Handles IPv4.
827 ifalias_expand_addr_inet()
829 local _action _arg _cidr _cidr_addr _exargs
830 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
838 case $_action:$_arg:$_exargs in
839 *:*--*) return ;; # invalid
840 tmp:*[0-9]-[0-9]*:*) # to be expanded
843 *:*[0-9]-[0-9]*:*) # to be expanded
845 tmp:*:*netmask*) # already expanded w/ netmask option
846 echo ${_arg%/[0-9]*} $_exargs && return
848 tmp:*:*) # already expanded w/o netmask option
849 echo $_arg $_exargs && return
851 *:*:*netmask*) # already expanded w/ netmask option
852 echo inet ${_arg%/[0-9]*} $_exargs && return
854 *:*:*) # already expanded w/o netmask option
855 echo inet $_arg $_exargs && return
859 for _cidr in $_arg; do
862 # When subnet prefix length is not specified, use /32.
864 $_ipaddr) _plen=32 ;; # "/" character not found
868 IFS=. set -- $_ipaddr
878 _iphead="${_iphead}${_iphead:+.}${_c}"
881 _iptail="${_iptail}${_iptail:+.}${_c}"
889 # clear netmask when removing aliases
890 if [ "$_action" = "-alias" ]; then
895 while [ "$_ipcount" -le "$_iphigh" ]; do
896 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
897 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
898 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."
901 _ipcount=$(($_ipcount + 1))
903 # Forcibly set /32 for remaining aliases.
908 for _c in $_retstr; do
909 ifalias_expand_addr_inet $_action $_c $_exargs
913 # ifalias_expand_addr_inet6 action addr
914 # Helper function for ifalias_expand_addr(). Handles IPv6.
916 ifalias_expand_addr_inet6()
918 local _action _arg _cidr _cidr_addr _exargs
919 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
928 case $_action:$_arg:$_exargs in
929 *:*--*:*) return ;; # invalid
930 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
933 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
935 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
936 echo ${_arg%/[0-9]*} $_exargs && return
938 tmp:*:*) # already expanded w/o prefixlen option
939 echo $_arg $_exargs && return
941 *:*:*prefixlen*) # already expanded w/ prefixlen option
942 echo inet6 ${_arg%/[0-9]*} $_exargs && return
944 *:*:*) # already expanded w/o prefixlen option
945 echo inet6 $_arg $_exargs && return
949 for _cidr in $_arg; do
950 _ipaddr="${_cidr%%/*}"
953 case $_action:$_ipaddr:$_cidr in
954 -alias:*:*) unset _plen ;;
955 *:$_cidr:$_ipaddr) unset _plen ;;
958 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
959 # Handle !v4mapped && !v4compat addresses.
961 # The default prefix length is 64.
962 case $_ipaddr:$_cidr in
963 $_cidr:$_ipaddr) _plen="64" ;;
965 _ipleft=${_ipaddr%-*}
966 _ipright=${_ipaddr#*-}
967 _iplow=${_ipleft##*:}
968 _iphigh=${_ipright%%:*}
969 _ipleft=${_ipleft%:*}
970 _ipright=${_ipright#*:}
972 if [ "$_iphigh" = "$_ipright" ]; then
978 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
979 _iplow=$((0x$_iplow))
980 _iphigh=$((0x$_iphigh))
982 while [ $_ipcount -le $_iphigh ]; do
983 _r=`printf "%s:%04x%s%s" \
984 $_ipleft $_ipcount $_ipright \
986 _retstr="$_retstr $_r"
987 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
989 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."
992 _ipcount=$(($_ipcount + 1))
996 _retstr="${_ipaddr}${_plen:+/}${_plen}"
999 for _c in $_retstr; do
1000 ifalias_expand_addr_inet6 $_action $_c $_exargs
1003 # v4mapped/v4compat should handle as an IPv4 alias
1004 _ipv4part=${_ipaddr##*:}
1006 # Adjust prefix length if any. If not, set the
1007 # default prefix length as 32.
1008 case $_ipaddr:$_cidr in
1009 $_cidr:$_ipaddr) _plen=32 ;;
1010 *) _plen=$(($_plen - 96)) ;;
1013 _retstr=`ifalias_expand_addr_inet \
1014 tmp ${_ipv4part}${_plen:+/}${_plen}`
1015 for _c in $_retstr; do
1016 ifalias_expand_addr_inet $_action $_c $_exargs
1022 # ifalias_af_common_handler if af action args
1023 # Helper function for ifalias_af_common().
1025 ifalias_af_common_handler()
1027 local _ret _if _af _action _args _c _tmpargs
1041 # link(ether) does not support address removal.
1042 case $_af:$_action in
1043 link:-alias|ether:-alias) return ;;
1047 for _c in $_args; do
1051 ${_af}\ *[0-9a-fA-F]-*)
1052 ifalias_af_common_handler $_if $_af $_action \
1053 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1056 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1062 _tmpargs="$_tmpargs $_c"
1066 # Process the last component if any.
1067 if [ -n "$_tmpargs}" ]; then
1069 ${_af}\ *[0-9a-fA-F]-*)
1070 ifalias_af_common_handler $_if $_af $_action \
1071 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1074 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1082 # ifalias_af_common if af action
1083 # Helper function for ifalias().
1087 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1088 local _vif _punct=".-/+"
1096 # Normalize $_if before using it in a pattern to list_vars()
1097 ltr "$_if" "$_punct" "_" _vif
1099 # ifconfig_IF_aliasN which starts with $_af
1100 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1101 sort_lite -nk1.$((9+${#_vif}+7))`
1103 eval ifconfig_args=\"\$$alias\"
1105 case $ifconfig_args in
1106 inet\ *) _iaf=inet ;;
1107 inet6\ *) _iaf=inet6 ;;
1109 link\ *) _iaf=link ;;
1110 ether\ *) _iaf=ether ;;
1113 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1115 _aliasn="$_aliasn $ifconfig_args"
1121 _aliasn="$_aliasn inet $ifconfig_args"
1122 warn "\$${alias} needs leading" \
1123 "\"inet\" keyword for an IPv4 address."
1127 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1130 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1131 sort_lite -nk1.$((14+${#_vif}+7))`
1133 eval ifconfig_args=\"\$$alias\"
1134 case ${_action}:"${ifconfig_args}" in
1139 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1140 warn "\$${alias} is obsolete. " \
1141 "Use ifconfig_${_vif}_aliasN instead."
1147 # backward compatibility: ipv4_addrs_IF.
1148 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1149 _aliasn="$_aliasn inet $_tmpargs"
1152 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1154 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1156 inet|inet6|ipx|link|ether)
1159 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1165 _tmpargs="$_tmpargs $_c"
1168 # Process the last component
1171 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1178 # ipv6_prefix_hostid_addr_common if action
1179 # Add or remove IPv6 prefix + hostid addr on the interface $if
1181 ipv6_prefix_hostid_addr_common()
1183 local _if _action prefix j
1186 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1188 if [ -n "${prefix}" ]; then
1189 for j in ${prefix}; do
1190 # The default prefixlen is 64.
1193 $plen:$j) plen=64 ;;
1197 # Normalize the last part by removing ":"
1200 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1201 prefixlen $plen eui64 ${_action}
1203 # if I am a router, add subnet router
1204 # anycast address (RFC 2373).
1205 if checkyesno ipv6_gateway_enable; then
1206 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1207 prefixlen $plen ${_action} anycast
1213 # ipv6_accept_rtadv_up if
1214 # Enable accepting Router Advertisement and send Router
1215 # Solicitation message
1216 ipv6_accept_rtadv_up()
1218 if ipv6_autoconfif $1; then
1219 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1220 if ! checkyesno rtsold_enable; then
1221 rtsol ${rtsol_flags} $1
1226 # ipv6_accept_rtadv_down if
1227 # Disable accepting Router Advertisement
1228 ipv6_accept_rtadv_down()
1230 if ipv6_autoconfif $1; then
1231 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1236 # Evaluate a startup script for the $if interface.
1237 # It returns 0 if a script was found and processed or
1238 # 1 if no script was found.
1242 if [ -r /etc/start_if.$1 ]; then
1251 # Evaluate a shutdown script for the $if interface.
1252 # It returns 0 if a script was found and processed or
1253 # 1 if no script was found.
1257 if [ -r /etc/stop_if.$1 ]; then
1266 # Create cloneable interfaces.
1270 local _list ifn ifopt _iflist _n tmpargs
1275 for ifn in ${cloned_interfaces}; do
1277 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1279 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1284 # epair(4) uses epair[0-9] for creation and
1285 # epair[0-9][ab] for configuration.
1287 # Skip if ${ifn}a or ${ifn}b already exist.
1288 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1290 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1293 ${IFCONFIG_CMD} ${ifn} create \
1294 `get_if_var ${ifn} create_args_IF`
1295 if [ $? -eq 0 ]; then
1296 _list="$_list ${ifn}a ${ifn}b"
1300 # Skip if ${ifn} already exists.
1301 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1304 ${IFCONFIG_CMD} ${ifn} create \
1305 `get_if_var ${ifn} create_args_IF`
1306 if [ $? -eq 0 ]; then
1311 if [ -n "$gif_interfaces" ]; then
1312 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1314 for ifn in ${gif_interfaces}; do
1316 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1318 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1321 # Skip if ifn already exists.
1322 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1327 ${IFCONFIG_CMD} $ifn create
1330 _n=$(${IFCONFIG_CMD} gif create)
1331 ${IFCONFIG_CMD} $_n name $ifn
1334 if [ $? -eq 0 ]; then
1337 tmpargs=$(get_if_var $ifn gifconfig_IF)
1338 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1340 if [ -n "${_list# }" ]; then
1341 echo "Created clone interfaces: ${_list# }."
1343 debug "Cloned: ${_list# }"
1347 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1352 local _list ifn _difn ifopt _iflist _sticky
1356 : ${cloned_interfaces_sticky:=NO}
1357 if checkyesno cloned_interfaces_sticky; then
1362 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1364 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1365 case $ifopt:$_sticky in
1366 sticky:*) continue ;; # :sticky => not destroy
1367 nosticky:*) ;; # :nosticky => destroy
1368 *:1) continue ;; # global sticky knob == 1
1371 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1376 # Note: epair(4) uses epair[0-9] for removal and
1377 # epair[0-9][ab] for configuration.
1379 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1380 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1382 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1387 ${IFCONFIG_CMD} -n $_difn destroy
1388 if [ $? -eq 0 ]; then
1389 _list="$_list ${ifn}a ${ifn}b"
1393 # Skip if ifn does not exist.
1394 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1397 ${IFCONFIG_CMD} -n ${ifn} destroy
1398 if [ $? -eq 0 ]; then
1404 if [ -n "${_list# }" ]; then
1405 echo "Destroyed clone interfaces: ${_list# }."
1407 debug "Destroyed clones: ${_list# }"
1411 # Create and configure child interfaces. Return 0 if child
1412 # interfaces are created.
1416 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1420 # Create wireless interfaces
1421 child_wlans=`get_if_var $ifn wlans_IF`
1423 for child in ${child_wlans}; do
1424 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1425 debug_flags="`get_if_var $child wlandebug_IF`"
1427 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1428 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1429 if [ -n "${debug_flags}" ]; then
1430 wlandebug -i $child ${debug_flags}
1433 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1434 if [ -n "${debug_flags}" ]; then
1435 wlandebug -i $i ${debug_flags}
1437 ${IFCONFIG_CMD} $i name $child && cfg=0
1439 if autoif $child; then
1444 # Create vlan interfaces
1445 child_vlans=`get_if_var $ifn vlans_IF`
1447 if [ -n "${child_vlans}" ]; then
1451 for child in ${child_vlans}; do
1452 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1453 child="${ifn}.${child}"
1454 create_args=`get_if_var $child create_args_IF`
1455 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1457 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1458 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1459 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1461 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1462 ${IFCONFIG_CMD} $i name $child && cfg=0
1465 if autoif $child; then
1474 # Destroy child interfaces.
1478 local cfg child child_vlans child_wlans ifn
1481 child_wlans=`get_if_var $ifn wlans_IF`
1482 for child in ${child_wlans}; do
1483 if ! ifexists $child; then
1486 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1489 child_vlans=`get_if_var $ifn vlans_IF`
1490 for child in ${child_vlans}; do
1491 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1492 child="${ifn}.${child}"
1494 if ! ifexists $child; then
1497 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1504 # Create netgraph nodes.
1508 ngctl -f - 2> /dev/null <<EOF
1515 # Create netgraph nodes.
1521 ng_mkpeer $* | while read line; do
1522 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1523 if [ -n "${t}" ]; then
1531 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1532 # FEC arguments were found and configured; returns !0 otherwise.
1535 local req_iface iface bogus
1538 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1542 iface=`ng_create_one fec dummy fec`
1543 if [ -z "${iface}" ]; then
1546 if [ "${iface}" = "${req_iface}" ]; then
1549 bogus="${bogus} ${iface}"
1552 for iface in ${bogus}; do
1553 ngctl shutdown ${iface}:
1558 # Create Fast EtherChannel interfaces.
1563 for i in ${fec_interfaces}; do
1565 for j in `get_if_var $i fecconfig_IF`; do
1571 ngctl msg ${i}: add_iface "\"${j}\""
1579 # Configure any IPX addresses for interface $ifn. Returns 0 if
1580 # IPX arguments were found and configured; returns 1 otherwise.
1588 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1589 if [ -n "${ifconfig_args}" ]; then
1590 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1598 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1599 # addresses were found and unconfigured. It returns 1, otherwise.
1603 local _if _ifs _ret ipxList oldifs _ipx
1607 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1611 for _ipx in $ipxList ; do
1612 # get rid of extraneous line
1613 [ -z "$_ipx" ] && break
1615 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1618 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1627 # ifnet_rename [ifname]
1628 # Rename interfaces if ifconfig_IF_name is defined.
1635 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1636 _ifname=`get_if_var $_if ifconfig_IF_name`
1637 if [ ! -z "$_ifname" ]; then
1638 ${IFCONFIG_CMD} $_if name $_ifname
1645 # list_net_interfaces type
1646 # List all network interfaces. The type of interface returned
1647 # can be controlled by the type argument. The type
1648 # argument can be any of the following:
1649 # nodhcp - all interfaces, excluding DHCP configured interfaces
1650 # dhcp - list only DHCP configured interfaces
1651 # noautoconf - all interfaces, excluding IPv6 Stateless
1652 # Address Autoconf configured interfaces
1653 # autoconf - list only IPv6 Stateless Address Autoconf
1654 # configured interfaces
1655 # If no argument is specified all network interfaces are output.
1656 # Note that the list will include cloned interfaces if applicable.
1657 # Cloned interfaces must already exist to have a chance to appear
1658 # in the list if ${network_interfaces} is set to `auto'.
1660 list_net_interfaces()
1662 local type _tmplist _list _autolist _lo _if
1665 # Get a list of ALL the interfaces and make lo0 first if it's there.
1668 case ${network_interfaces} in
1670 _autolist="`${IFCONFIG_CMD} -l`"
1672 for _if in ${_autolist} ; do
1673 if autoif $_if; then
1674 if [ "$_if" = "lo0" ]; then
1677 _tmplist="${_tmplist} ${_if}"
1681 _tmplist="${_lo}${_tmplist# }"
1684 for _if in ${network_interfaces} ${cloned_interfaces}; do
1685 # epair(4) uses epair[0-9] for creation and
1686 # epair[0-9][ab] for configuration.
1689 _tmplist="$_tmplist ${_if}a ${_if}b"
1692 _tmplist="$_tmplist $_if"
1697 # lo0 is effectively mandatory, so help prevent foot-shooting
1700 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1701 # This is fine, do nothing
1702 _tmplist="${_tmplist# }"
1705 _tmplist="lo0 ${_tmplist# }"
1714 for _if in ${_tmplist} ; do
1715 if ! dhcpif $_if && \
1716 [ -n "`_ifconfig_getargs $_if`" ]; then
1717 _list="${_list# } ${_if}"
1722 for _if in ${_tmplist} ; do
1723 if dhcpif $_if; then
1724 _list="${_list# } ${_if}"
1729 for _if in ${_tmplist} ; do
1730 if ! ipv6_autoconfif $_if && \
1731 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1732 _list="${_list# } ${_if}"
1737 for _if in ${_tmplist} ; do
1738 if ipv6_autoconfif $_if; then
1739 _list="${_list# } ${_if}"
1753 # get_default_if -address_family
1754 # Get the interface of the default route for the given address family.
1755 # The -address_family argument must be suitable passing to route(8).
1759 local routeget oldifs defif line
1764 for line in `route -n get $1 default 2>/dev/null`; do
1777 # Echo decimal number $arg (single digit) in hexadecimal format.
1784 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1790 is_wired_interface()
1794 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1795 *media:?Ethernet*) media=Ethernet ;;
1798 test "$media" = "Ethernet"
1801 # network6_getladdr if [flag]
1802 # Echo link-local address from $if if any.
1803 # If flag is defined, tentative ones will be excluded.
1806 local _if _flag proto addr rest
1810 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1811 case "${proto}/${addr}/${_flag}/${rest}" in
1815 inet6/fe80:://*tentative*) # w/o flag
1816 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1817 network6_getladdr $_if $_flags
1819 inet6/fe80::/*/*tentative*) # w/ flag