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
53 childif_create ${ifn} && cfg=0
59 # Shutdown and de-configure an interface. If action is taken,
60 # print the interface name.
68 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
70 if ! noafif $ifn; then
71 afexists inet6 && ipv6_down ${ifn} && cfg=0
72 afexists inet && ipv4_down ${ifn} && cfg=0
74 ifconfig_down ${ifn} && cfg=0
75 ifscript_down ${ifn} && cfg=0
76 childif_destroy ${ifn} && cfg=0
82 # Move ifn to the specified vnet jail.
91 # Reclaim ifn from the specified vnet jail.
99 # ifn_vnet0 ifn action
100 # Helper function for ifn_vnetup and ifn_vnetdown.
104 local _ifn _cfg _action _vnet
109 if _vnet=$(vnetif $_ifn); then
110 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
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
125 local _cfg _ipv6_opts ifconfig_args
128 # Make sure lo0 always comes up.
129 if [ "$1" = "lo0" ]; then
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"
141 # backward compatibility: $ipv6_enable
143 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
146 # No accept_rtadv by default on if_bridge(4)
147 # to avoid a conflict with the member
151 if ! checkyesno ipv6_gateway_enable; then
152 _ipv6_opts="${_ipv6_opts} accept_rtadv"
159 case $ipv6_cpe_wanif in
161 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
165 if [ -n "${_ipv6_opts}" ]; then
166 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
171 ifconfig_args=`ifconfig_getargs $1`
172 if [ -n "${ifconfig_args}" ]; then
173 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
178 if ! noafif $1 && afexists inet6; then
180 ifconfig_args=`ifconfig_getargs $1 ipv6`
181 if [ -n "${ifconfig_args}" ]; then
182 # backward compatibility: inet6 keyword
183 case "${ifconfig_args}" in
185 warn "\$ifconfig_$1_ipv6 needs leading" \
186 "\"inet6\" keyword for an IPv6 address."
187 ifconfig_args="inet6 ${ifconfig_args}"
190 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
191 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
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
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}
214 ifalias $1 link alias
215 ifalias $1 ether alias
217 if [ ${_cfg} -eq 0 ]; then
218 ${IFCONFIG_CMD} $1 up
222 /etc/rc.d/wpa_supplicant start $1
223 _cfg=0 # XXX: not sure this should count
224 elif hostapif $1; then
225 /etc/rc.d/hostapd start $1
230 if [ $_cfg -ne 0 ] ; then
231 ${IFCONFIG_CMD} $1 up
233 if syncdhcpif $1; then
234 /etc/rc.d/dhclient start $1
243 # returns 1 if wpa_supplicant or dhclient was stopped or
244 # the interface exists.
252 /etc/rc.d/wpa_supplicant stop $1
254 elif hostapif $1; then
255 /etc/rc.d/hostapd stop $1
260 /etc/rc.d/dhclient stop $1
265 ${IFCONFIG_CMD} $1 down
272 # get_if_var if var [default]
273 # Return the value of the pseudo-hash corresponding to $if where
274 # $var is a string containg the sub-string "IF" which will be
275 # replaced with $if after the characters defined in _punct are
276 # replaced with '_'. If the variable is unset, replace it with
280 local _if _punct _punct_c _var _default prefix suffix
282 if [ $# -ne 2 -a $# -ne 3 ]; then
283 err 3 'USAGE: get_if_var name var [default]'
288 ltr ${_if} "${_punct}" '_' _if
294 eval echo \${${prefix}${_if}${suffix}-${_default}}
297 # _ifconfig_getargs if [af]
298 # Prints the arguments for the supplied interface to stdout.
299 # Returns 1 if empty. In general, ifconfig_getargs should be used
307 if [ -z "$_ifn" ]; then
311 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
314 # ifconfig_getargs if [af]
315 # Takes the result from _ifconfig_getargs and removes pseudo
316 # args such as DHCP and WPA.
319 local _tmpargs _arg _args _vnet
320 _tmpargs=`_ifconfig_getargs $1 $2`
321 if [ $? -eq 1 ]; then
327 for _arg in $_tmpargs; do
329 [Dd][Hh][Cc][Pp]:0) ;;
330 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
331 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
332 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
334 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
347 # Returns 0 if the interface should be automatically configured at
348 # boot time and 1 otherwise.
352 _tmpargs=`_ifconfig_getargs $1`
354 for _arg in $_tmpargs; do
356 [Nn][Oo][Aa][Uu][Tt][Oo])
366 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
370 _tmpargs=`_ifconfig_getargs $1`
384 for _arg in $_tmpargs; do
389 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
392 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
402 # Returns 0 if the interface should be configured synchronously and
407 _tmpargs=`_ifconfig_getargs $1`
413 for _arg in $_tmpargs; do
415 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
418 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
424 checkyesno synchronous_dhclient
428 # Returns 0 if the interface is a WPA interface and 1 otherwise.
432 _tmpargs=`_ifconfig_getargs $1`
434 for _arg in $_tmpargs; do
446 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
450 _tmpargs=`_ifconfig_getargs $1`
452 for _arg in $_tmpargs; do
454 [Hh][Oo][Ss][Tt][Aa][Pp])
464 # Returns 0 and echo jail if "vnet" keyword is specified on the
465 # interface, and 1 otherwise.
468 local _tmpargs _arg _vnet
469 _tmpargs=`_ifconfig_getargs $1`
472 for _arg in $_tmpargs; do
475 *:1) echo $_arg; return 0 ;;
483 # Returns 0 if the address family is enabled in the kernel
492 check_kern_features ${_af}
495 if [ -x /sbin/atmconfig ]; then
496 /sbin/atmconfig diag list > /dev/null 2>&1
505 err 1 "afexists(): Unsupported address family: $_af"
511 # Returns 0 if the interface has no af configuration and 1 otherwise.
539 # Returns 0 if the interface should be configured for IPv6 and
546 if ! afexists inet6; then
550 # lo0 is always IPv6-enabled
557 case "${ipv6_network_interfaces}" in
558 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
559 # True if $ifconfig_IF_ipv6 is defined.
560 _tmpargs=`_ifconfig_getargs $_if ipv6`
561 if [ -n "${_tmpargs}" ]; then
565 # True if $ipv6_prefix_IF is defined.
566 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
567 if [ -n "${_tmpargs}" ]; then
571 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
572 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
573 if [ -n "${_tmpargs}" ]; then
583 # Returns 0 if the interface should be configured for IPv6 with
584 # Stateless Address Configuration; 1 otherwise.
587 local _if _tmpargs _arg
601 if ! ipv6if $_if; then
604 if checkyesno ipv6_gateway_enable; then
607 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
608 if [ -n "${_tmpargs}" ]; then
611 # backward compatibility: $ipv6_enable
613 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
614 if checkyesno ipv6_gateway_enable; then
619 # No accept_rtadv by default on if_bridge(4)
620 # to avoid a conflict with the member
631 _tmpargs=`_ifconfig_getargs $_if ipv6`
632 for _arg in $_tmpargs; do
640 # backward compatibility: $ipv6_ifconfig_IF
641 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
642 for _arg in $_tmpargs; do
654 # Returns 0 if the interface exists and 1 otherwise.
657 [ -z "$1" ] && return 1
658 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
662 # add IPv4 addresses to the interface $if
669 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
670 if [ "${_if}" = "lo0" ]; then
671 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
672 if [ -z "${ifconfig_args}" ]; then
673 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
676 ifalias ${_if} inet alias && _ret=0
682 # add IPv6 addresses to the interface $if
689 if ! ipv6if $_if; then
693 ifalias ${_if} inet6 alias && _ret=0
694 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
695 ipv6_accept_rtadv_up ${_if} && _ret=0
701 # remove IPv4 addresses from the interface $if
704 local _if _ifs _ret inetList oldifs _inet
709 ifalias ${_if} inet -alias && _ret=0
711 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
715 for _inet in $inetList ; do
716 # get rid of extraneous line
722 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
725 ${IFCONFIG_CMD} ${_if} ${_inet} delete
735 # remove IPv6 addresses from the interface $if
738 local _if _ifs _ret inetList oldifs _inet6
743 if ! ipv6if $_if; then
747 ipv6_accept_rtadv_down ${_if} && _ret=0
748 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
749 ifalias ${_if} inet6 -alias && _ret=0
751 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
755 for _inet6 in $inetList ; do
756 # get rid of extraneous line
762 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
765 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
774 # ifalias if af action
775 # Configure or remove aliases for network interface $if.
776 # It returns 0 if at least one alias was configured or
777 # removed, or 1 if there were none.
784 afexists $2 || return $_ret
787 inet|inet6|link|ether)
788 ifalias_af_common $1 $2 $3 && _ret=0
795 # ifalias_expand_addr af action addr
796 # Expand address range ("N-M") specification in addr.
797 # "addr" must not include an address-family keyword.
798 # The results will include an address-family keyword.
800 ifalias_expand_addr()
808 afexists $_af || return
809 ifalias_expand_addr_$_af $_action $*
812 # ifalias_expand_addr_inet action addr
813 # Helper function for ifalias_expand_addr(). Handles IPv4.
815 ifalias_expand_addr_inet()
817 local _action _arg _cidr _cidr_addr _exargs
818 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
826 case $_action:$_arg:$_exargs in
827 *:*--*) return ;; # invalid
828 tmp:*[0-9]-[0-9]*:*) # to be expanded
831 *:*[0-9]-[0-9]*:*) # to be expanded
833 tmp:*:*netmask*) # already expanded w/ netmask option
834 echo ${_arg%/[0-9]*} $_exargs && return
836 tmp:*:*) # already expanded w/o netmask option
837 echo $_arg $_exargs && return
839 *:*:*netmask*) # already expanded w/ netmask option
840 echo inet ${_arg%/[0-9]*} $_exargs && return
842 *:*:*) # already expanded w/o netmask option
843 echo inet $_arg $_exargs && return
847 for _cidr in $_arg; do
850 # When subnet prefix length is not specified, use /32.
852 $_ipaddr) _plen=32 ;; # "/" character not found
856 IFS=. set -- $_ipaddr
866 _iphead="${_iphead}${_iphead:+.}${_c}"
869 _iptail="${_iptail}${_iptail:+.}${_c}"
877 # clear netmask when removing aliases
878 if [ "$_action" = "-alias" ]; then
883 while [ "$_ipcount" -le "$_iphigh" ]; do
884 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
885 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
886 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."
889 _ipcount=$(($_ipcount + 1))
891 # Forcibly set /32 for remaining aliases.
896 for _c in $_retstr; do
897 ifalias_expand_addr_inet $_action $_c $_exargs
901 # ifalias_expand_addr_inet6 action addr
902 # Helper function for ifalias_expand_addr(). Handles IPv6.
904 ifalias_expand_addr_inet6()
906 local _action _arg _cidr _cidr_addr _exargs
907 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
916 case $_action:$_arg:$_exargs in
917 *:*--*:*) return ;; # invalid
918 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
921 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
923 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
924 echo ${_arg%/[0-9]*} $_exargs && return
926 tmp:*:*) # already expanded w/o prefixlen option
927 echo $_arg $_exargs && return
929 *:*:*prefixlen*) # already expanded w/ prefixlen option
930 echo inet6 ${_arg%/[0-9]*} $_exargs && return
932 *:*:*) # already expanded w/o prefixlen option
933 echo inet6 $_arg $_exargs && return
937 for _cidr in $_arg; do
938 _ipaddr="${_cidr%%/*}"
941 case $_action:$_ipaddr:$_cidr in
942 -alias:*:*) unset _plen ;;
943 *:$_cidr:$_ipaddr) unset _plen ;;
946 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
947 # Handle !v4mapped && !v4compat addresses.
949 # The default prefix length is 64.
950 case $_ipaddr:$_cidr in
951 $_cidr:$_ipaddr) _plen="64" ;;
953 _ipleft=${_ipaddr%-*}
954 _ipright=${_ipaddr#*-}
955 _iplow=${_ipleft##*:}
956 _iphigh=${_ipright%%:*}
957 _ipleft=${_ipleft%:*}
958 _ipright=${_ipright#*:}
960 if [ "$_iphigh" = "$_ipright" ]; then
966 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
967 _iplow=$((0x$_iplow))
968 _iphigh=$((0x$_iphigh))
970 while [ $_ipcount -le $_iphigh ]; do
971 _r=`printf "%s:%04x%s%s" \
972 $_ipleft $_ipcount $_ipright \
974 _retstr="$_retstr $_r"
975 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
977 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."
980 _ipcount=$(($_ipcount + 1))
984 _retstr="${_ipaddr}${_plen:+/}${_plen}"
987 for _c in $_retstr; do
988 ifalias_expand_addr_inet6 $_action $_c $_exargs
991 # v4mapped/v4compat should handle as an IPv4 alias
992 _ipv4part=${_ipaddr##*:}
994 # Adjust prefix length if any. If not, set the
995 # default prefix length as 32.
996 case $_ipaddr:$_cidr in
997 $_cidr:$_ipaddr) _plen=32 ;;
998 *) _plen=$(($_plen - 96)) ;;
1001 _retstr=`ifalias_expand_addr_inet \
1002 tmp ${_ipv4part}${_plen:+/}${_plen}`
1003 for _c in $_retstr; do
1004 ifalias_expand_addr_inet $_action $_c $_exargs
1010 # ifalias_af_common_handler if af action args
1011 # Helper function for ifalias_af_common().
1013 ifalias_af_common_handler()
1015 local _ret _if _af _action _args _c _tmpargs
1029 # link(ether) does not support address removal.
1030 case $_af:$_action in
1031 link:-alias|ether:-alias) return ;;
1035 for _c in $_args; do
1039 ${_af}\ *[0-9a-fA-F]-*)
1040 ifalias_af_common_handler $_if $_af $_action \
1041 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1044 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1050 _tmpargs="$_tmpargs $_c"
1054 # Process the last component if any.
1055 if [ -n "$_tmpargs}" ]; then
1057 ${_af}\ *[0-9a-fA-F]-*)
1058 ifalias_af_common_handler $_if $_af $_action \
1059 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1062 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1070 # ifalias_af_common if af action
1071 # Helper function for ifalias().
1075 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1076 local _vif _punct=".-/+"
1084 # Normalize $_if before using it in a pattern to list_vars()
1085 ltr "$_if" "$_punct" "_" _vif
1087 # ifconfig_IF_aliasN which starts with $_af
1088 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1089 sort_lite -nk1.$((9+${#_vif}+7))`
1091 eval ifconfig_args=\"\$$alias\"
1093 case $ifconfig_args in
1094 inet\ *) _iaf=inet ;;
1095 inet6\ *) _iaf=inet6 ;;
1096 link\ *) _iaf=link ;;
1097 ether\ *) _iaf=ether ;;
1100 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1102 _aliasn="$_aliasn $ifconfig_args"
1108 _aliasn="$_aliasn inet $ifconfig_args"
1109 warn "\$${alias} needs leading" \
1110 "\"inet\" keyword for an IPv4 address."
1114 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1117 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1118 sort_lite -nk1.$((14+${#_vif}+7))`
1120 eval ifconfig_args=\"\$$alias\"
1121 case ${_action}:"${ifconfig_args}" in
1126 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1127 warn "\$${alias} is obsolete. " \
1128 "Use ifconfig_${_vif}_aliasN instead."
1134 # backward compatibility: ipv4_addrs_IF.
1135 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1136 _aliasn="$_aliasn inet $_tmpargs"
1139 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1141 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1143 inet|inet6|link|ether)
1146 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1152 _tmpargs="$_tmpargs $_c"
1155 # Process the last component
1158 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1165 # ipv6_prefix_hostid_addr_common if action
1166 # Add or remove IPv6 prefix + hostid addr on the interface $if
1168 ipv6_prefix_hostid_addr_common()
1170 local _if _action prefix j
1173 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1175 if [ -n "${prefix}" ]; then
1176 for j in ${prefix}; do
1177 # The default prefixlen is 64.
1180 $plen:$j) plen=64 ;;
1184 # Normalize the last part by removing ":"
1187 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1188 prefixlen $plen eui64 ${_action}
1190 # if I am a router, add subnet router
1191 # anycast address (RFC 2373).
1192 if checkyesno ipv6_gateway_enable; then
1193 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1194 prefixlen $plen ${_action} anycast
1200 # ipv6_accept_rtadv_up if
1201 # Enable accepting Router Advertisement and send Router
1202 # Solicitation message
1203 ipv6_accept_rtadv_up()
1205 if ipv6_autoconfif $1; then
1206 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1207 if ! checkyesno rtsold_enable; then
1208 rtsol ${rtsol_flags} $1
1213 # ipv6_accept_rtadv_down if
1214 # Disable accepting Router Advertisement
1215 ipv6_accept_rtadv_down()
1217 if ipv6_autoconfif $1; then
1218 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1223 # Evaluate a startup script for the $if interface.
1224 # It returns 0 if a script was found and processed or
1225 # 1 if no script was found.
1229 if [ -r /etc/start_if.$1 ]; then
1238 # Evaluate a shutdown script for the $if interface.
1239 # It returns 0 if a script was found and processed or
1240 # 1 if no script was found.
1244 if [ -r /etc/stop_if.$1 ]; then
1253 # Create IEEE802.3 interfaces.
1257 local _list _iflist wlan parent ifn
1261 for wlan in `set | egrep ^wlans_[a-z]+[0-9]+=[a-z]+[0-9]+`; do
1262 # Parse wlans_$parent=$ifn
1263 wlan=`echo $wlan | sed -E 's/wlans_([a-z]+[0-9]+)=([a-z]+[0-9]+)/\1:\2/'`
1264 OIFS=$IFS; IFS=:; set -- $wlan; parent=$1; ifn=$2; IFS=$OIFS
1266 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1269 # Skip if ${ifn} already exists.
1270 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1273 ${IFCONFIG_CMD} ${ifn} create wlandev ${parent}
1274 if [ $? -eq 0 ]; then
1278 if [ -n "${_list# }" ]; then
1279 echo "Created wlan(4) interfaces: ${_list# }."
1281 debug "Created wlan(4)s: ${_list# }"
1285 # Destroy IEEE802.3 interfaces.
1289 local _list _iflist wlan parent ifn
1293 for wlan in `set | egrep ^wlans_[a-z]+[0-9]+=[a-z]+[0-9]+`; do
1294 # Parse wlans_$parent=$ifn
1295 wlan=`echo $wlan | sed -E 's/wlans_([a-z]+[0-9]+)=([a-z]+[0-9]+)/\1:\2/'`
1296 OIFS=$IFS; IFS=:; set -- $wlan; parent=$1; ifn=$2; IFS=$OIFS
1298 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1301 # Skip if ${ifn} doesn't exists.
1302 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1305 ${IFCONFIG_CMD} -n ${ifn} destroy
1306 if [ $? -eq 0 ]; then
1310 if [ -n "${_list# }" ]; then
1311 echo "Destroyed wlan(4) interfaces: ${_list# }."
1313 debug "Destroyed wlan(4)s: ${_list# }"
1317 # Create cloneable interfaces.
1321 local _list ifn ifopt _iflist _n tmpargs
1326 for ifn in ${cloned_interfaces}; do
1328 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1330 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1335 # epair(4) uses epair[0-9] for creation and
1336 # epair[0-9][ab] for configuration.
1338 # Skip if ${ifn}a or ${ifn}b already exist.
1339 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1341 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1344 ${IFCONFIG_CMD} ${ifn} create \
1345 `get_if_var ${ifn} create_args_IF`
1346 if [ $? -eq 0 ]; then
1347 _list="$_list ${ifn}a ${ifn}b"
1351 # Skip if ${ifn} already exists.
1352 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1355 ${IFCONFIG_CMD} ${ifn} create \
1356 `get_if_var ${ifn} create_args_IF`
1357 if [ $? -eq 0 ]; then
1362 if [ -n "$gif_interfaces" ]; then
1363 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1365 for ifn in ${gif_interfaces}; do
1367 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1369 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1372 # Skip if ifn already exists.
1373 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1378 ${IFCONFIG_CMD} $ifn create
1381 _n=$(${IFCONFIG_CMD} gif create)
1382 ${IFCONFIG_CMD} $_n name $ifn
1385 if [ $? -eq 0 ]; then
1388 tmpargs=$(get_if_var $ifn gifconfig_IF)
1389 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1391 if [ -n "${_list# }" ]; then
1392 echo "Created clone interfaces: ${_list# }."
1394 debug "Cloned: ${_list# }"
1398 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1403 local _list ifn _difn ifopt _iflist _sticky
1407 : ${cloned_interfaces_sticky:=NO}
1408 if checkyesno cloned_interfaces_sticky; then
1413 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1415 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1416 case $ifopt:$_sticky in
1417 sticky:*) continue ;; # :sticky => not destroy
1418 nosticky:*) ;; # :nosticky => destroy
1419 *:1) continue ;; # global sticky knob == 1
1422 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1427 # Note: epair(4) uses epair[0-9] for removal and
1428 # epair[0-9][ab] for configuration.
1430 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1431 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1433 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1438 ${IFCONFIG_CMD} -n $_difn destroy
1439 if [ $? -eq 0 ]; then
1440 _list="$_list ${ifn}a ${ifn}b"
1444 # Skip if ifn does not exist.
1445 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1448 ${IFCONFIG_CMD} -n ${ifn} destroy
1449 if [ $? -eq 0 ]; then
1455 if [ -n "${_list# }" ]; then
1456 echo "Destroyed clone interfaces: ${_list# }."
1458 debug "Destroyed clones: ${_list# }"
1462 # Create and configure child interfaces. Return 0 if child
1463 # interfaces are created.
1465 # XXXGL: the wlan code in this functions is superseded by wlan_up(),
1466 # and will go away soon.
1470 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1474 # Create wireless interfaces
1475 child_wlans=`get_if_var $ifn wlans_IF`
1477 for child in ${child_wlans}; do
1478 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1479 debug_flags="`get_if_var $child wlandebug_IF`"
1481 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1482 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1483 if [ -n "${debug_flags}" ]; then
1484 wlandebug -i $child ${debug_flags}
1487 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1488 if [ -n "${debug_flags}" ]; then
1489 wlandebug -i $i ${debug_flags}
1491 ${IFCONFIG_CMD} $i name $child && cfg=0
1493 if autoif $child; then
1498 # Create vlan interfaces
1499 child_vlans=`get_if_var $ifn vlans_IF`
1501 if [ -n "${child_vlans}" ]; then
1505 for child in ${child_vlans}; do
1506 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1507 child="${ifn}.${child}"
1508 create_args=`get_if_var $child create_args_IF`
1509 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1511 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1512 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1513 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1515 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1516 ${IFCONFIG_CMD} $i name $child && cfg=0
1519 if autoif $child; then
1528 # Destroy child interfaces.
1532 local cfg child child_vlans child_wlans ifn
1535 child_wlans=`get_if_var $ifn wlans_IF`
1536 for child in ${child_wlans}; do
1537 if ! ifexists $child; then
1540 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1543 child_vlans=`get_if_var $ifn vlans_IF`
1544 for child in ${child_vlans}; do
1545 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1546 child="${ifn}.${child}"
1548 if ! ifexists $child; then
1551 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1558 # Create netgraph nodes.
1562 ngctl -f - 2> /dev/null <<EOF
1569 # Create netgraph nodes.
1575 ng_mkpeer $* | while read line; do
1576 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1577 if [ -n "${t}" ]; then
1584 # ifnet_rename [ifname]
1585 # Rename interfaces if ifconfig_IF_name is defined.
1592 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1593 _ifname=`get_if_var $_if ifconfig_IF_name`
1594 if [ ! -z "$_ifname" ]; then
1595 ${IFCONFIG_CMD} $_if name $_ifname
1602 # list_net_interfaces type
1603 # List all network interfaces. The type of interface returned
1604 # can be controlled by the type argument. The type
1605 # argument can be any of the following:
1606 # nodhcp - all interfaces, excluding DHCP configured interfaces
1607 # dhcp - list only DHCP configured interfaces
1608 # noautoconf - all interfaces, excluding IPv6 Stateless
1609 # Address Autoconf configured interfaces
1610 # autoconf - list only IPv6 Stateless Address Autoconf
1611 # configured interfaces
1612 # If no argument is specified all network interfaces are output.
1613 # Note that the list will include cloned interfaces if applicable.
1614 # Cloned interfaces must already exist to have a chance to appear
1615 # in the list if ${network_interfaces} is set to `auto'.
1617 list_net_interfaces()
1619 local type _tmplist _list _autolist _lo _if
1622 # Get a list of ALL the interfaces and make lo0 first if it's there.
1625 case ${network_interfaces} in
1627 _autolist="`${IFCONFIG_CMD} -l`"
1629 for _if in ${_autolist} ; do
1630 if autoif $_if; then
1631 if [ "$_if" = "lo0" ]; then
1634 _tmplist="${_tmplist} ${_if}"
1638 _tmplist="${_lo}${_tmplist# }"
1641 for _if in ${network_interfaces} ${cloned_interfaces}; do
1642 # epair(4) uses epair[0-9] for creation and
1643 # epair[0-9][ab] for configuration.
1646 _tmplist="$_tmplist ${_if}a ${_if}b"
1649 _tmplist="$_tmplist $_if"
1654 # lo0 is effectively mandatory, so help prevent foot-shooting
1657 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1658 # This is fine, do nothing
1659 _tmplist="${_tmplist# }"
1662 _tmplist="lo0 ${_tmplist# }"
1671 for _if in ${_tmplist} ; do
1672 if ! dhcpif $_if && \
1673 [ -n "`_ifconfig_getargs $_if`" ]; then
1674 _list="${_list# } ${_if}"
1679 for _if in ${_tmplist} ; do
1680 if dhcpif $_if; then
1681 _list="${_list# } ${_if}"
1686 for _if in ${_tmplist} ; do
1687 if ! ipv6_autoconfif $_if && \
1688 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1689 _list="${_list# } ${_if}"
1694 for _if in ${_tmplist} ; do
1695 if ipv6_autoconfif $_if; then
1696 _list="${_list# } ${_if}"
1710 # get_default_if -address_family
1711 # Get the interface of the default route for the given address family.
1712 # The -address_family argument must be suitable passing to route(8).
1716 local routeget oldifs defif line
1721 for line in `route -n get $1 default 2>/dev/null`; do
1734 # Echo decimal number $arg (single digit) in hexadecimal format.
1741 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1747 is_wired_interface()
1751 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1752 *media:?Ethernet*) media=Ethernet ;;
1755 test "$media" = "Ethernet"
1758 # network6_getladdr if [flag]
1759 # Echo link-local address from $if if any.
1760 # If flag is defined, tentative ones will be excluded.
1763 local _if _flag proto addr rest
1767 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1768 case "${proto}/${addr}/${_flag}/${rest}" in
1772 inet6/fe80:://*tentative*) # w/o flag
1773 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1774 network6_getladdr $_if $_flags
1776 inet6/fe80::/*/*tentative*) # w/ flag