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
218 /etc/rc.d/wpa_supplicant start $1
219 _cfg=0 # XXX: not sure this should count
220 elif hostapif $1; then
221 /etc/rc.d/hostapd start $1
223 elif [ ${_cfg} -eq 0 ]; then
224 ${IFCONFIG_CMD} $1 up
228 if [ $_cfg -ne 0 ] ; then
229 ${IFCONFIG_CMD} $1 up
231 if syncdhcpif $1; then
232 /etc/rc.d/dhclient start $1
241 # returns 1 if wpa_supplicant or dhclient was stopped or
242 # the interface exists.
250 /etc/rc.d/wpa_supplicant stop $1
252 elif hostapif $1; then
253 /etc/rc.d/hostapd stop $1
258 /etc/rc.d/dhclient stop $1
263 ${IFCONFIG_CMD} $1 down
270 # get_if_var if var [default]
271 # Return the value of the pseudo-hash corresponding to $if where
272 # $var is a string containg the sub-string "IF" which will be
273 # replaced with $if after the characters defined in _punct are
274 # replaced with '_'. If the variable is unset, replace it with
278 local _if _punct _punct_c _var _default prefix suffix
280 if [ $# -ne 2 -a $# -ne 3 ]; then
281 err 3 'USAGE: get_if_var name var [default]'
286 ltr ${_if} "${_punct}" '_' _if
292 eval echo \${${prefix}${_if}${suffix}-${_default}}
295 # _ifconfig_getargs if [af]
296 # Prints the arguments for the supplied interface to stdout.
297 # Returns 1 if empty. In general, ifconfig_getargs should be used
305 if [ -z "$_ifn" ]; then
309 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
312 # ifconfig_getargs if [af]
313 # Takes the result from _ifconfig_getargs and removes pseudo
314 # args such as DHCP and WPA.
317 local _tmpargs _arg _args _vnet
318 _tmpargs=`_ifconfig_getargs $1 $2`
319 if [ $? -eq 1 ]; then
325 for _arg in $_tmpargs; do
327 [Dd][Hh][Cc][Pp]:0) ;;
328 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
329 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
330 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
332 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
345 # Returns 0 if the interface should be automatically configured at
346 # boot time and 1 otherwise.
350 _tmpargs=`_ifconfig_getargs $1`
352 for _arg in $_tmpargs; do
354 [Nn][Oo][Aa][Uu][Tt][Oo])
364 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
368 _tmpargs=`_ifconfig_getargs $1`
382 for _arg in $_tmpargs; do
387 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
390 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
400 # Returns 0 if the interface should be configured synchronously and
405 _tmpargs=`_ifconfig_getargs $1`
411 for _arg in $_tmpargs; do
413 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
416 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
422 checkyesno synchronous_dhclient
426 # Returns 0 if the interface is a WPA interface and 1 otherwise.
430 _tmpargs=`_ifconfig_getargs $1`
432 for _arg in $_tmpargs; do
444 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
448 _tmpargs=`_ifconfig_getargs $1`
450 for _arg in $_tmpargs; do
452 [Hh][Oo][Ss][Tt][Aa][Pp])
462 # Returns 0 and echo jail if "vnet" keyword is specified on the
463 # interface, and 1 otherwise.
466 local _tmpargs _arg _vnet
467 _tmpargs=`_ifconfig_getargs $1`
470 for _arg in $_tmpargs; do
473 *:1) echo $_arg; return 0 ;;
481 # Returns 0 if the address family is enabled in the kernel
490 check_kern_features ${_af}
493 if [ -x /sbin/atmconfig ]; then
494 /sbin/atmconfig diag list > /dev/null 2>&1
503 err 1 "afexists(): Unsupported address family: $_af"
509 # Returns 0 if the interface has no af configuration and 1 otherwise.
537 # Returns 0 if the interface should be configured for IPv6 and
544 if ! afexists inet6; then
548 # lo0 is always IPv6-enabled
555 case "${ipv6_network_interfaces}" in
556 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
557 # True if $ifconfig_IF_ipv6 is defined.
558 _tmpargs=`_ifconfig_getargs $_if ipv6`
559 if [ -n "${_tmpargs}" ]; then
563 # True if $ipv6_prefix_IF is defined.
564 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
565 if [ -n "${_tmpargs}" ]; then
569 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
570 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
571 if [ -n "${_tmpargs}" ]; then
581 # Returns 0 if the interface should be configured for IPv6 with
582 # Stateless Address Configuration; 1 otherwise.
585 local _if _tmpargs _arg
599 if ! ipv6if $_if; then
602 if checkyesno ipv6_gateway_enable; then
605 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
606 if [ -n "${_tmpargs}" ]; then
609 # backward compatibility: $ipv6_enable
611 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
612 if checkyesno ipv6_gateway_enable; then
617 # No accept_rtadv by default on if_bridge(4)
618 # to avoid a conflict with the member
629 _tmpargs=`_ifconfig_getargs $_if ipv6`
630 for _arg in $_tmpargs; do
638 # backward compatibility: $ipv6_ifconfig_IF
639 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
640 for _arg in $_tmpargs; do
652 # Returns 0 if the interface exists and 1 otherwise.
655 [ -z "$1" ] && return 1
656 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
660 # add IPv4 addresses to the interface $if
667 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
668 if [ "${_if}" = "lo0" ]; then
669 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
670 if [ -z "${ifconfig_args}" ]; then
671 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
674 ifalias ${_if} inet alias && _ret=0
680 # add IPv6 addresses to the interface $if
687 if ! ipv6if $_if; then
691 ifalias ${_if} inet6 alias && _ret=0
692 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
693 ipv6_accept_rtadv_up ${_if} && _ret=0
699 # remove IPv4 addresses from the interface $if
702 local _if _ifs _ret inetList oldifs _inet
707 ifalias ${_if} inet -alias && _ret=0
709 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
713 for _inet in $inetList ; do
714 # get rid of extraneous line
720 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
723 ${IFCONFIG_CMD} ${_if} ${_inet} delete
733 # remove IPv6 addresses from the interface $if
736 local _if _ifs _ret inetList oldifs _inet6
741 if ! ipv6if $_if; then
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
749 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
753 for _inet6 in $inetList ; do
754 # get rid of extraneous line
760 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
763 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
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.
782 afexists $2 || return $_ret
785 inet|inet6|link|ether)
786 ifalias_af_common $1 $2 $3 && _ret=0
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.
798 ifalias_expand_addr()
806 afexists $_af || return
807 ifalias_expand_addr_$_af $_action $*
810 # ifalias_expand_addr_inet action addr
811 # Helper function for ifalias_expand_addr(). Handles IPv4.
813 ifalias_expand_addr_inet()
815 local _action _arg _cidr _cidr_addr _exargs
816 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
824 case $_action:$_arg:$_exargs in
825 *:*--*) return ;; # invalid
826 tmp:*[0-9]-[0-9]*:*) # to be expanded
829 *:*[0-9]-[0-9]*:*) # to be expanded
831 tmp:*:*netmask*) # already expanded w/ netmask option
832 echo ${_arg%/[0-9]*} $_exargs && return
834 tmp:*:*) # already expanded w/o netmask option
835 echo $_arg $_exargs && return
837 *:*:*netmask*) # already expanded w/ netmask option
838 echo inet ${_arg%/[0-9]*} $_exargs && return
840 *:*:*) # already expanded w/o netmask option
841 echo inet $_arg $_exargs && return
845 for _cidr in $_arg; do
848 # When subnet prefix length is not specified, use /32.
850 $_ipaddr) _plen=32 ;; # "/" character not found
854 IFS=. set -- $_ipaddr
864 _iphead="${_iphead}${_iphead:+.}${_c}"
867 _iptail="${_iptail}${_iptail:+.}${_c}"
875 # clear netmask when removing aliases
876 if [ "$_action" = "-alias" ]; then
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."
887 _ipcount=$(($_ipcount + 1))
889 # Forcibly set /32 for remaining aliases.
894 for _c in $_retstr; do
895 ifalias_expand_addr_inet $_action $_c $_exargs
899 # ifalias_expand_addr_inet6 action addr
900 # Helper function for ifalias_expand_addr(). Handles IPv6.
902 ifalias_expand_addr_inet6()
904 local _action _arg _cidr _cidr_addr _exargs
905 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
914 case $_action:$_arg:$_exargs in
915 *:*--*:*) return ;; # invalid
916 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
919 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
921 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
922 echo ${_arg%/[0-9]*} $_exargs && return
924 tmp:*:*) # already expanded w/o prefixlen option
925 echo $_arg $_exargs && return
927 *:*:*prefixlen*) # already expanded w/ prefixlen option
928 echo inet6 ${_arg%/[0-9]*} $_exargs && return
930 *:*:*) # already expanded w/o prefixlen option
931 echo inet6 $_arg $_exargs && return
935 for _cidr in $_arg; do
936 _ipaddr="${_cidr%%/*}"
939 case $_action:$_ipaddr:$_cidr in
940 -alias:*:*) unset _plen ;;
941 *:$_cidr:$_ipaddr) unset _plen ;;
944 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
945 # Handle !v4mapped && !v4compat addresses.
947 # The default prefix length is 64.
948 case $_ipaddr:$_cidr in
949 $_cidr:$_ipaddr) _plen="64" ;;
951 _ipleft=${_ipaddr%-*}
952 _ipright=${_ipaddr#*-}
953 _iplow=${_ipleft##*:}
954 _iphigh=${_ipright%%:*}
955 _ipleft=${_ipleft%:*}
956 _ipright=${_ipright#*:}
958 if [ "$_iphigh" = "$_ipright" ]; then
964 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
965 _iplow=$((0x$_iplow))
966 _iphigh=$((0x$_iphigh))
968 while [ $_ipcount -le $_iphigh ]; do
969 _r=`printf "%s:%04x%s%s" \
970 $_ipleft $_ipcount $_ipright \
972 _retstr="$_retstr $_r"
973 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
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."
978 _ipcount=$(($_ipcount + 1))
982 _retstr="${_ipaddr}${_plen:+/}${_plen}"
985 for _c in $_retstr; do
986 ifalias_expand_addr_inet6 $_action $_c $_exargs
989 # v4mapped/v4compat should handle as an IPv4 alias
990 _ipv4part=${_ipaddr##*:}
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)) ;;
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
1008 # ifalias_af_common_handler if af action args
1009 # Helper function for ifalias_af_common().
1011 ifalias_af_common_handler()
1013 local _ret _if _af _action _args _c _tmpargs
1027 # link(ether) does not support address removal.
1028 case $_af:$_action in
1029 link:-alias|ether:-alias) return ;;
1033 for _c in $_args; do
1037 ${_af}\ *[0-9a-fA-F]-*)
1038 ifalias_af_common_handler $_if $_af $_action \
1039 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1042 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1048 _tmpargs="$_tmpargs $_c"
1052 # Process the last component if any.
1053 if [ -n "$_tmpargs}" ]; then
1055 ${_af}\ *[0-9a-fA-F]-*)
1056 ifalias_af_common_handler $_if $_af $_action \
1057 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1060 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1068 # ifalias_af_common if af action
1069 # Helper function for ifalias().
1073 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1074 local _vif _punct=".-/+"
1082 # Normalize $_if before using it in a pattern to list_vars()
1083 ltr "$_if" "$_punct" "_" _vif
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))`
1089 eval ifconfig_args=\"\$$alias\"
1091 case $ifconfig_args in
1092 inet\ *) _iaf=inet ;;
1093 inet6\ *) _iaf=inet6 ;;
1094 link\ *) _iaf=link ;;
1095 ether\ *) _iaf=ether ;;
1098 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1100 _aliasn="$_aliasn $ifconfig_args"
1106 _aliasn="$_aliasn inet $ifconfig_args"
1107 warn "\$${alias} needs leading" \
1108 "\"inet\" keyword for an IPv4 address."
1112 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1115 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1116 sort_lite -nk1.$((14+${#_vif}+7))`
1118 eval ifconfig_args=\"\$$alias\"
1119 case ${_action}:"${ifconfig_args}" in
1124 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1125 warn "\$${alias} is obsolete. " \
1126 "Use ifconfig_${_vif}_aliasN instead."
1132 # backward compatibility: ipv4_addrs_IF.
1133 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1134 _aliasn="$_aliasn inet $_tmpargs"
1137 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1139 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1141 inet|inet6|link|ether)
1144 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1150 _tmpargs="$_tmpargs $_c"
1153 # Process the last component
1156 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1163 # ipv6_prefix_hostid_addr_common if action
1164 # Add or remove IPv6 prefix + hostid addr on the interface $if
1166 ipv6_prefix_hostid_addr_common()
1168 local _if _action prefix j
1171 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1173 if [ -n "${prefix}" ]; then
1174 for j in ${prefix}; do
1175 # The default prefixlen is 64.
1178 $plen:$j) plen=64 ;;
1182 # Normalize the last part by removing ":"
1185 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1186 prefixlen $plen eui64 ${_action}
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
1198 # ipv6_accept_rtadv_up if
1199 # Enable accepting Router Advertisement and send Router
1200 # Solicitation message
1201 ipv6_accept_rtadv_up()
1203 if ipv6_autoconfif $1; then
1204 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1205 if ! checkyesno rtsold_enable; then
1206 rtsol ${rtsol_flags} $1
1211 # ipv6_accept_rtadv_down if
1212 # Disable accepting Router Advertisement
1213 ipv6_accept_rtadv_down()
1215 if ipv6_autoconfif $1; then
1216 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
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.
1227 if [ -r /etc/start_if.$1 ]; then
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.
1242 if [ -r /etc/stop_if.$1 ]; then
1251 # Create IEEE802.11 interfaces.
1255 local _list _iflist parent child_wlans child create_args debug_flags
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`"
1266 ""|$child|$child\ *|*\ $child\ *|*\ $child) ;;
1269 # Skip if ${child} already exists.
1270 if ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1273 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1274 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1275 if [ $? -eq 0 ]; then
1276 _list="$_list $child"
1278 if [ -n "${debug_flags}" ]; then
1279 wlandebug -i $child ${debug_flags}
1282 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1283 # XXXGL: wlandebug should accept any name
1284 if [ -n "${debug_flags}" ]; then
1285 wlandebug -i $i ${debug_flags}
1287 ${IFCONFIG_CMD} $i name $child && cfg=0
1288 if [ $? -eq 0 ]; then
1289 _list="$_list $child"
1294 if [ -n "${_list# }" ]; then
1295 echo "Created wlan(4) interfaces: ${_list# }."
1297 debug "Created wlan(4)s: ${_list# }"
1301 # Destroy IEEE802.11 interfaces.
1305 local _list _iflist parent child_wlans child
1309 # Parse wlans_$parent="$child ..."
1310 for parent in `set | sed -nE 's/wlans_([a-z]+[0-9]+)=.*/\1/p'`; do
1311 child_wlans=`get_if_var $parent wlans_IF`
1312 for child in ${child_wlans}; do
1314 ""|$child|$child\ *|*\ $child\ *|*\ $child) ;;
1317 # Skip if ${child} doesn't exists.
1318 if ! ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1321 ${IFCONFIG_CMD} -n ${child} destroy
1322 if [ $? -eq 0 ]; then
1323 _list="$_list $child"
1327 if [ -n "${_list# }" ]; then
1328 echo "Destroyed wlan(4) interfaces: ${_list# }."
1330 debug "Destroyed wlan(4)s: ${_list# }"
1334 # Create cloneable interfaces.
1338 local _list ifn ifopt _iflist _n tmpargs
1343 for ifn in ${cloned_interfaces}; do
1345 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1347 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1352 # epair(4) uses epair[0-9] for creation and
1353 # epair[0-9][ab] for configuration.
1355 # Skip if ${ifn}a or ${ifn}b already exist.
1356 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1358 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1361 ${IFCONFIG_CMD} ${ifn} create \
1362 `get_if_var ${ifn} create_args_IF`
1363 if [ $? -eq 0 ]; then
1364 _list="$_list ${ifn}a ${ifn}b"
1368 # Skip if ${ifn} already exists.
1369 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1372 ${IFCONFIG_CMD} ${ifn} create \
1373 `get_if_var ${ifn} create_args_IF`
1374 if [ $? -eq 0 ]; then
1379 if [ -n "$gif_interfaces" ]; then
1380 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1382 for ifn in ${gif_interfaces}; do
1384 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1386 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1389 # Skip if ifn already exists.
1390 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1395 ${IFCONFIG_CMD} $ifn create
1398 _n=$(${IFCONFIG_CMD} gif create)
1399 ${IFCONFIG_CMD} $_n name $ifn
1402 if [ $? -eq 0 ]; then
1405 tmpargs=$(get_if_var $ifn gifconfig_IF)
1406 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1408 if [ -n "${_list# }" ]; then
1409 echo "Created clone interfaces: ${_list# }."
1411 debug "Cloned: ${_list# }"
1415 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1420 local _list ifn _difn ifopt _iflist _sticky
1424 : ${cloned_interfaces_sticky:=NO}
1425 if checkyesno cloned_interfaces_sticky; then
1430 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1432 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1433 case $ifopt:$_sticky in
1434 sticky:*) continue ;; # :sticky => not destroy
1435 nosticky:*) ;; # :nosticky => destroy
1436 *:1) continue ;; # global sticky knob == 1
1439 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1444 # Note: epair(4) uses epair[0-9] for removal and
1445 # epair[0-9][ab] for configuration.
1447 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1448 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1450 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1455 ${IFCONFIG_CMD} -n $_difn destroy
1456 if [ $? -eq 0 ]; then
1457 _list="$_list ${ifn}a ${ifn}b"
1461 # Skip if ifn does not exist.
1462 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1465 ${IFCONFIG_CMD} -n ${ifn} destroy
1466 if [ $? -eq 0 ]; then
1472 if [ -n "${_list# }" ]; then
1473 echo "Destroyed clone interfaces: ${_list# }."
1475 debug "Destroyed clones: ${_list# }"
1479 # Create and configure child interfaces. Return 0 if child
1480 # interfaces are created.
1482 # XXXGL: the wlan code in this functions is superseded by wlan_up(),
1483 # and will go away soon.
1487 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1491 # Create wireless interfaces
1492 child_wlans=`get_if_var $ifn wlans_IF`
1494 for child in ${child_wlans}; do
1495 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1496 debug_flags="`get_if_var $child wlandebug_IF`"
1498 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1499 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1500 if [ -n "${debug_flags}" ]; then
1501 wlandebug -i $child ${debug_flags}
1504 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1505 if [ -n "${debug_flags}" ]; then
1506 wlandebug -i $i ${debug_flags}
1508 ${IFCONFIG_CMD} $i name $child && cfg=0
1510 if autoif $child; then
1515 # Create vlan interfaces
1516 child_vlans=`get_if_var $ifn vlans_IF`
1518 if [ -n "${child_vlans}" ]; then
1522 for child in ${child_vlans}; do
1523 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1524 child="${ifn}.${child}"
1525 create_args=`get_if_var $child create_args_IF`
1526 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1528 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1529 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1530 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1532 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1533 ${IFCONFIG_CMD} $i name $child && cfg=0
1536 if autoif $child; then
1545 # Destroy child interfaces.
1549 local cfg child child_vlans child_wlans ifn
1552 child_wlans=`get_if_var $ifn wlans_IF`
1553 for child in ${child_wlans}; do
1554 if ! ifexists $child; then
1557 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1560 child_vlans=`get_if_var $ifn vlans_IF`
1561 for child in ${child_vlans}; do
1562 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1563 child="${ifn}.${child}"
1565 if ! ifexists $child; then
1568 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1575 # Create netgraph nodes.
1579 ngctl -f - 2> /dev/null <<EOF
1586 # Create netgraph nodes.
1592 ng_mkpeer $* | while read line; do
1593 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1594 if [ -n "${t}" ]; then
1601 # ifnet_rename [ifname]
1602 # Rename interfaces if ifconfig_IF_name is defined.
1609 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1610 _ifname=`get_if_var $_if ifconfig_IF_name`
1611 if [ ! -z "$_ifname" ]; then
1612 ${IFCONFIG_CMD} $_if name $_ifname
1619 # list_net_interfaces type
1620 # List all network interfaces. The type of interface returned
1621 # can be controlled by the type argument. The type
1622 # argument can be any of the following:
1623 # nodhcp - all interfaces, excluding DHCP configured interfaces
1624 # dhcp - list only DHCP configured interfaces
1625 # noautoconf - all interfaces, excluding IPv6 Stateless
1626 # Address Autoconf configured interfaces
1627 # autoconf - list only IPv6 Stateless Address Autoconf
1628 # configured interfaces
1629 # If no argument is specified all network interfaces are output.
1630 # Note that the list will include cloned interfaces if applicable.
1631 # Cloned interfaces must already exist to have a chance to appear
1632 # in the list if ${network_interfaces} is set to `auto'.
1634 list_net_interfaces()
1636 local type _tmplist _list _autolist _lo _if
1639 # Get a list of ALL the interfaces and make lo0 first if it's there.
1642 case ${network_interfaces} in
1644 _autolist="`${IFCONFIG_CMD} -l`"
1646 for _if in ${_autolist} ; do
1647 if autoif $_if; then
1648 if [ "$_if" = "lo0" ]; then
1651 _tmplist="${_tmplist} ${_if}"
1655 _tmplist="${_lo}${_tmplist# }"
1658 for _if in ${network_interfaces} ${cloned_interfaces}; do
1659 # epair(4) uses epair[0-9] for creation and
1660 # epair[0-9][ab] for configuration.
1663 _tmplist="$_tmplist ${_if}a ${_if}b"
1666 _tmplist="$_tmplist $_if"
1671 # lo0 is effectively mandatory, so help prevent foot-shooting
1674 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1675 # This is fine, do nothing
1676 _tmplist="${_tmplist# }"
1679 _tmplist="lo0 ${_tmplist# }"
1688 for _if in ${_tmplist} ; do
1689 if ! dhcpif $_if && \
1690 [ -n "`_ifconfig_getargs $_if`" ]; then
1691 _list="${_list# } ${_if}"
1696 for _if in ${_tmplist} ; do
1697 if dhcpif $_if; then
1698 _list="${_list# } ${_if}"
1703 for _if in ${_tmplist} ; do
1704 if ! ipv6_autoconfif $_if && \
1705 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1706 _list="${_list# } ${_if}"
1711 for _if in ${_tmplist} ; do
1712 if ipv6_autoconfif $_if; then
1713 _list="${_list# } ${_if}"
1727 # get_default_if -address_family
1728 # Get the interface of the default route for the given address family.
1729 # The -address_family argument must be suitable passing to route(8).
1733 local routeget oldifs defif line
1738 for line in `route -n get $1 default 2>/dev/null`; do
1751 # Echo decimal number $arg (single digit) in hexadecimal format.
1758 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1764 is_wired_interface()
1768 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1769 *media:?Ethernet*) media=Ethernet ;;
1772 test "$media" = "Ethernet"
1775 # network6_getladdr if [flag]
1776 # Echo link-local address from $if if any.
1777 # If flag is defined, tentative ones will be excluded.
1780 local _if _flag proto addr rest
1784 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1785 case "${proto}/${addr}/${_flag}/${rest}" in
1789 inet6/fe80:://*tentative*) # w/o flag
1790 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1791 network6_getladdr $_if $_flags
1793 inet6/fe80::/*/*tentative*) # w/ flag