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 _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
224 /etc/rc.d/wpa_supplicant start $1
225 _cfg=0 # XXX: not sure this should count
226 elif hostapif $1; then
227 /etc/rc.d/hostapd start $1
232 if [ $_cfg -ne 0 ] ; then
233 ${IFCONFIG_CMD} $1 up
235 if syncdhcpif $1; then
236 /etc/rc.d/dhclient start $1
245 # returns 1 if wpa_supplicant or dhclient was stopped or
246 # the interface exists.
254 /etc/rc.d/wpa_supplicant stop $1
256 elif hostapif $1; then
257 /etc/rc.d/hostapd stop $1
262 /etc/rc.d/dhclient stop $1
267 ${IFCONFIG_CMD} $1 down
274 # get_if_var if var [default]
275 # Return the value of the pseudo-hash corresponding to $if where
276 # $var is a string containg the sub-string "IF" which will be
277 # replaced with $if after the characters defined in _punct are
278 # replaced with '_'. If the variable is unset, replace it with
282 local _if _punct _punct_c _var _default prefix suffix
284 if [ $# -ne 2 -a $# -ne 3 ]; then
285 err 3 'USAGE: get_if_var name var [default]'
290 ltr ${_if} "${_punct}" '_' _if
296 eval echo \${${prefix}${_if}${suffix}-${_default}}
299 # _ifconfig_getargs if [af]
300 # Prints the arguments for the supplied interface to stdout.
301 # Returns 1 if empty. In general, ifconfig_getargs should be used
309 if [ -z "$_ifn" ]; then
313 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
316 # ifconfig_getargs if [af]
317 # Takes the result from _ifconfig_getargs and removes pseudo
318 # args such as DHCP and WPA.
321 local _tmpargs _arg _args _vnet
322 _tmpargs=`_ifconfig_getargs $1 $2`
323 if [ $? -eq 1 ]; then
329 for _arg in $_tmpargs; do
331 [Dd][Hh][Cc][Pp]:0) ;;
332 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
333 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
334 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
336 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
349 # Returns 0 if the interface should be automatically configured at
350 # boot time and 1 otherwise.
354 _tmpargs=`_ifconfig_getargs $1`
356 for _arg in $_tmpargs; do
358 [Nn][Oo][Aa][Uu][Tt][Oo])
368 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
372 _tmpargs=`_ifconfig_getargs $1`
387 for _arg in $_tmpargs; do
392 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
395 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
405 # Returns 0 if the interface should be configured synchronously and
410 _tmpargs=`_ifconfig_getargs $1`
416 for _arg in $_tmpargs; do
418 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
421 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
427 checkyesno synchronous_dhclient
431 # Returns 0 if the interface is a WPA interface and 1 otherwise.
435 _tmpargs=`_ifconfig_getargs $1`
437 for _arg in $_tmpargs; do
449 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
453 _tmpargs=`_ifconfig_getargs $1`
455 for _arg in $_tmpargs; do
457 [Hh][Oo][Ss][Tt][Aa][Pp])
467 # Returns 0 and echo jail if "vnet" keyword is specified on the
468 # interface, and 1 otherwise.
471 local _tmpargs _arg _vnet
472 _tmpargs=`_ifconfig_getargs $1`
475 for _arg in $_tmpargs; do
478 *:1) echo $_arg; return 0 ;;
486 # Returns 0 if the address family is enabled in the kernel
495 check_kern_features ${_af}
498 ${SYSCTL_N} net.ipx > /dev/null 2>&1
501 if [ -x /sbin/atmconfig ]; then
502 /sbin/atmconfig diag list > /dev/null 2>&1
511 err 1 "afexists(): Unsupported address family: $_af"
517 # Returns 0 if the interface has no af configuration and 1 otherwise.
545 # Returns 0 if the interface should be configured for IPv6 and
552 if ! afexists inet6; then
556 # lo0 is always IPv6-enabled
563 case "${ipv6_network_interfaces}" in
564 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
565 # True if $ifconfig_IF_ipv6 is defined.
566 _tmpargs=`_ifconfig_getargs $_if ipv6`
567 if [ -n "${_tmpargs}" ]; then
571 # True if $ipv6_prefix_IF is defined.
572 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
573 if [ -n "${_tmpargs}" ]; then
577 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
578 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
579 if [ -n "${_tmpargs}" ]; then
589 # Returns 0 if the interface should be configured for IPv6 with
590 # Stateless Address Configuration; 1 otherwise.
593 local _if _tmpargs _arg
608 if ! ipv6if $_if; then
611 if checkyesno ipv6_gateway_enable; then
614 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
615 if [ -n "${_tmpargs}" ]; then
618 # backward compatibility: $ipv6_enable
620 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
621 if checkyesno ipv6_gateway_enable; then
626 # No accept_rtadv by default on if_bridge(4)
627 # to avoid a conflict with the member
638 _tmpargs=`_ifconfig_getargs $_if ipv6`
639 for _arg in $_tmpargs; do
647 # backward compatibility: $ipv6_ifconfig_IF
648 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
649 for _arg in $_tmpargs; do
661 # Returns 0 if the interface exists and 1 otherwise.
664 [ -z "$1" ] && return 1
665 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
669 # add IPv4 addresses to the interface $if
676 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
677 if [ "${_if}" = "lo0" ]; then
678 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
679 if [ -z "${ifconfig_args}" ]; then
680 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
683 ifalias ${_if} inet alias && _ret=0
689 # add IPv6 addresses to the interface $if
696 if ! ipv6if $_if; then
700 ifalias ${_if} inet6 alias && _ret=0
701 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
702 ipv6_accept_rtadv_up ${_if} && _ret=0
708 # remove IPv4 addresses from the interface $if
711 local _if _ifs _ret inetList oldifs _inet
716 ifalias ${_if} inet -alias && _ret=0
718 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
722 for _inet in $inetList ; do
723 # get rid of extraneous line
729 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
732 ${IFCONFIG_CMD} ${_if} ${_inet} delete
742 # remove IPv6 addresses from the interface $if
745 local _if _ifs _ret inetList oldifs _inet6
750 if ! ipv6if $_if; then
754 ipv6_accept_rtadv_down ${_if} && _ret=0
755 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
756 ifalias ${_if} inet6 -alias && _ret=0
758 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
762 for _inet6 in $inetList ; do
763 # get rid of extraneous line
769 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
772 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
781 # ifalias if af action
782 # Configure or remove aliases for network interface $if.
783 # It returns 0 if at least one alias was configured or
784 # removed, or 1 if there were none.
791 afexists $2 || return $_ret
794 inet|inet6|link|ether)
795 ifalias_af_common $1 $2 $3 && _ret=0
802 # ifalias_expand_addr af action addr
803 # Expand address range ("N-M") specification in addr.
804 # "addr" must not include an address-family keyword.
805 # The results will include an address-family keyword.
807 ifalias_expand_addr()
815 afexists $_af || return
816 ifalias_expand_addr_$_af $_action $*
819 # ifalias_expand_addr_inet action addr
820 # Helper function for ifalias_expand_addr(). Handles IPv4.
822 ifalias_expand_addr_inet()
824 local _action _arg _cidr _cidr_addr _exargs
825 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
833 case $_action:$_arg:$_exargs in
834 *:*--*) return ;; # invalid
835 tmp:*[0-9]-[0-9]*:*) # to be expanded
838 *:*[0-9]-[0-9]*:*) # to be expanded
840 tmp:*:*netmask*) # already expanded w/ netmask option
841 echo ${_arg%/[0-9]*} $_exargs && return
843 tmp:*:*) # already expanded w/o netmask option
844 echo $_arg $_exargs && return
846 *:*:*netmask*) # already expanded w/ netmask option
847 echo inet ${_arg%/[0-9]*} $_exargs && return
849 *:*:*) # already expanded w/o netmask option
850 echo inet $_arg $_exargs && return
854 for _cidr in $_arg; do
857 # When subnet prefix length is not specified, use /32.
859 $_ipaddr) _plen=32 ;; # "/" character not found
863 IFS=. set -- $_ipaddr
873 _iphead="${_iphead}${_iphead:+.}${_c}"
876 _iptail="${_iptail}${_iptail:+.}${_c}"
884 # clear netmask when removing aliases
885 if [ "$_action" = "-alias" ]; then
890 while [ "$_ipcount" -le "$_iphigh" ]; do
891 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
892 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
893 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."
896 _ipcount=$(($_ipcount + 1))
898 # Forcibly set /32 for remaining aliases.
903 for _c in $_retstr; do
904 ifalias_expand_addr_inet $_action $_c $_exargs
908 # ifalias_expand_addr_inet6 action addr
909 # Helper function for ifalias_expand_addr(). Handles IPv6.
911 ifalias_expand_addr_inet6()
913 local _action _arg _cidr _cidr_addr _exargs
914 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
923 case $_action:$_arg:$_exargs in
924 *:*--*:*) return ;; # invalid
925 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
928 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
930 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
931 echo ${_arg%/[0-9]*} $_exargs && return
933 tmp:*:*) # already expanded w/o prefixlen option
934 echo $_arg $_exargs && return
936 *:*:*prefixlen*) # already expanded w/ prefixlen option
937 echo inet6 ${_arg%/[0-9]*} $_exargs && return
939 *:*:*) # already expanded w/o prefixlen option
940 echo inet6 $_arg $_exargs && return
944 for _cidr in $_arg; do
945 _ipaddr="${_cidr%%/*}"
948 case $_action:$_ipaddr:$_cidr in
949 -alias:*:*) unset _plen ;;
950 *:$_cidr:$_ipaddr) unset _plen ;;
953 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
954 # Handle !v4mapped && !v4compat addresses.
956 # The default prefix length is 64.
957 case $_ipaddr:$_cidr in
958 $_cidr:$_ipaddr) _plen="64" ;;
960 _ipleft=${_ipaddr%-*}
961 _ipright=${_ipaddr#*-}
962 _iplow=${_ipleft##*:}
963 _iphigh=${_ipright%%:*}
964 _ipleft=${_ipleft%:*}
965 _ipright=${_ipright#*:}
967 if [ "$_iphigh" = "$_ipright" ]; then
973 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
974 _iplow=$((0x$_iplow))
975 _iphigh=$((0x$_iphigh))
977 while [ $_ipcount -le $_iphigh ]; do
978 _r=`printf "%s:%04x%s%s" \
979 $_ipleft $_ipcount $_ipright \
981 _retstr="$_retstr $_r"
982 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
984 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."
987 _ipcount=$(($_ipcount + 1))
991 _retstr="${_ipaddr}${_plen:+/}${_plen}"
994 for _c in $_retstr; do
995 ifalias_expand_addr_inet6 $_action $_c $_exargs
998 # v4mapped/v4compat should handle as an IPv4 alias
999 _ipv4part=${_ipaddr##*:}
1001 # Adjust prefix length if any. If not, set the
1002 # default prefix length as 32.
1003 case $_ipaddr:$_cidr in
1004 $_cidr:$_ipaddr) _plen=32 ;;
1005 *) _plen=$(($_plen - 96)) ;;
1008 _retstr=`ifalias_expand_addr_inet \
1009 tmp ${_ipv4part}${_plen:+/}${_plen}`
1010 for _c in $_retstr; do
1011 ifalias_expand_addr_inet $_action $_c $_exargs
1017 # ifalias_af_common_handler if af action args
1018 # Helper function for ifalias_af_common().
1020 ifalias_af_common_handler()
1022 local _ret _if _af _action _args _c _tmpargs
1036 # link(ether) does not support address removal.
1037 case $_af:$_action in
1038 link:-alias|ether:-alias) return ;;
1042 for _c in $_args; do
1046 ${_af}\ *[0-9a-fA-F]-*)
1047 ifalias_af_common_handler $_if $_af $_action \
1048 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1051 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1057 _tmpargs="$_tmpargs $_c"
1061 # Process the last component if any.
1062 if [ -n "$_tmpargs}" ]; then
1064 ${_af}\ *[0-9a-fA-F]-*)
1065 ifalias_af_common_handler $_if $_af $_action \
1066 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1069 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1077 # ifalias_af_common if af action
1078 # Helper function for ifalias().
1082 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1083 local _vif _punct=".-/+"
1091 # Normalize $_if before using it in a pattern to list_vars()
1092 ltr "$_if" "$_punct" "_" _vif
1094 # ifconfig_IF_aliasN which starts with $_af
1095 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1096 sort_lite -nk1.$((9+${#_vif}+7))`
1098 eval ifconfig_args=\"\$$alias\"
1100 case $ifconfig_args in
1101 inet\ *) _iaf=inet ;;
1102 inet6\ *) _iaf=inet6 ;;
1104 link\ *) _iaf=link ;;
1105 ether\ *) _iaf=ether ;;
1108 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1110 _aliasn="$_aliasn $ifconfig_args"
1116 _aliasn="$_aliasn inet $ifconfig_args"
1117 warn "\$${alias} needs leading" \
1118 "\"inet\" keyword for an IPv4 address."
1122 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1125 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1126 sort_lite -nk1.$((14+${#_vif}+7))`
1128 eval ifconfig_args=\"\$$alias\"
1129 case ${_action}:"${ifconfig_args}" in
1134 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1135 warn "\$${alias} is obsolete. " \
1136 "Use ifconfig_${_vif}_aliasN instead."
1142 # backward compatibility: ipv4_addrs_IF.
1143 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1144 _aliasn="$_aliasn inet $_tmpargs"
1147 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1149 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1151 inet|inet6|ipx|link|ether)
1154 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1160 _tmpargs="$_tmpargs $_c"
1163 # Process the last component
1166 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1173 # ipv6_prefix_hostid_addr_common if action
1174 # Add or remove IPv6 prefix + hostid addr on the interface $if
1176 ipv6_prefix_hostid_addr_common()
1178 local _if _action prefix j
1181 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1183 if [ -n "${prefix}" ]; then
1184 for j in ${prefix}; do
1185 # The default prefixlen is 64.
1188 $plen:$j) plen=64 ;;
1192 # Normalize the last part by removing ":"
1195 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1196 prefixlen $plen eui64 ${_action}
1198 # if I am a router, add subnet router
1199 # anycast address (RFC 2373).
1200 if checkyesno ipv6_gateway_enable; then
1201 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1202 prefixlen $plen ${_action} anycast
1208 # ipv6_accept_rtadv_up if
1209 # Enable accepting Router Advertisement and send Router
1210 # Solicitation message
1211 ipv6_accept_rtadv_up()
1213 if ipv6_autoconfif $1; then
1214 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1215 if ! checkyesno rtsold_enable; then
1216 rtsol ${rtsol_flags} $1
1221 # ipv6_accept_rtadv_down if
1222 # Disable accepting Router Advertisement
1223 ipv6_accept_rtadv_down()
1225 if ipv6_autoconfif $1; then
1226 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1231 # Evaluate a startup script for the $if interface.
1232 # It returns 0 if a script was found and processed or
1233 # 1 if no script was found.
1237 if [ -r /etc/start_if.$1 ]; then
1246 # Evaluate a shutdown script for the $if interface.
1247 # It returns 0 if a script was found and processed or
1248 # 1 if no script was found.
1252 if [ -r /etc/stop_if.$1 ]; then
1261 # Create cloneable interfaces.
1265 local _list ifn ifopt _iflist _n tmpargs
1270 for ifn in ${cloned_interfaces}; do
1272 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1274 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1279 # epair(4) uses epair[0-9] for creation and
1280 # epair[0-9][ab] for configuration.
1282 # Skip if ${ifn}a or ${ifn}b already exist.
1283 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1285 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1288 ${IFCONFIG_CMD} ${ifn} create \
1289 `get_if_var ${ifn} create_args_IF`
1290 if [ $? -eq 0 ]; then
1291 _list="$_list ${ifn}a ${ifn}b"
1295 # Skip if ${ifn} already exists.
1296 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1299 ${IFCONFIG_CMD} ${ifn} create \
1300 `get_if_var ${ifn} create_args_IF`
1301 if [ $? -eq 0 ]; then
1306 if [ -n "$gif_interfaces" ]; then
1307 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1309 for ifn in ${gif_interfaces}; do
1311 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1313 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1316 # Skip if ifn already exists.
1317 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1322 ${IFCONFIG_CMD} $ifn create
1325 _n=$(${IFCONFIG_CMD} gif create)
1326 ${IFCONFIG_CMD} $_n name $ifn
1329 if [ $? -eq 0 ]; then
1332 tmpargs=$(get_if_var $ifn gifconfig_IF)
1333 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1335 if [ -n "${_list# }" ]; then
1336 echo "Created clone interfaces: ${_list# }."
1338 debug "Cloned: ${_list# }"
1342 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1347 local _list ifn _difn ifopt _iflist _sticky
1351 : ${cloned_interfaces_sticky:=NO}
1352 if checkyesno cloned_interfaces_sticky; then
1357 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1359 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1360 case $ifopt:$_sticky in
1361 sticky:*) continue ;; # :sticky => not destroy
1362 nosticky:*) ;; # :nosticky => destroy
1363 *:1) continue ;; # global sticky knob == 1
1366 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1371 # Note: epair(4) uses epair[0-9] for removal and
1372 # epair[0-9][ab] for configuration.
1374 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1375 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1377 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1382 ${IFCONFIG_CMD} -n $_difn destroy
1383 if [ $? -eq 0 ]; then
1384 _list="$_list ${ifn}a ${ifn}b"
1388 # Skip if ifn does not exist.
1389 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1392 ${IFCONFIG_CMD} -n ${ifn} destroy
1393 if [ $? -eq 0 ]; then
1399 if [ -n "${_list# }" ]; then
1400 echo "Destroyed clone interfaces: ${_list# }."
1402 debug "Destroyed clones: ${_list# }"
1406 # Create and configure child interfaces. Return 0 if child
1407 # interfaces are created.
1411 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1415 # Create wireless interfaces
1416 child_wlans=`get_if_var $ifn wlans_IF`
1418 for child in ${child_wlans}; do
1419 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1420 debug_flags="`get_if_var $child wlandebug_IF`"
1422 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1423 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1424 if [ -n "${debug_flags}" ]; then
1425 wlandebug -i $child ${debug_flags}
1428 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1429 if [ -n "${debug_flags}" ]; then
1430 wlandebug -i $i ${debug_flags}
1432 ${IFCONFIG_CMD} $i name $child && cfg=0
1434 if autoif $child; then
1439 # Create vlan interfaces
1440 child_vlans=`get_if_var $ifn vlans_IF`
1442 if [ -n "${child_vlans}" ]; then
1446 for child in ${child_vlans}; do
1447 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1448 child="${ifn}.${child}"
1449 create_args=`get_if_var $child create_args_IF`
1450 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1452 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1453 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1454 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1456 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1457 ${IFCONFIG_CMD} $i name $child && cfg=0
1460 if autoif $child; then
1469 # Destroy child interfaces.
1473 local cfg child child_vlans child_wlans ifn
1476 child_wlans=`get_if_var $ifn wlans_IF`
1477 for child in ${child_wlans}; do
1478 if ! ifexists $child; then
1481 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1484 child_vlans=`get_if_var $ifn vlans_IF`
1485 for child in ${child_vlans}; do
1486 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1487 child="${ifn}.${child}"
1489 if ! ifexists $child; then
1492 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1499 # Create netgraph nodes.
1503 ngctl -f - 2> /dev/null <<EOF
1510 # Create netgraph nodes.
1516 ng_mkpeer $* | while read line; do
1517 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1518 if [ -n "${t}" ]; then
1526 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1527 # FEC arguments were found and configured; returns !0 otherwise.
1530 local req_iface iface bogus
1533 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1537 iface=`ng_create_one fec dummy fec`
1538 if [ -z "${iface}" ]; then
1541 if [ "${iface}" = "${req_iface}" ]; then
1544 bogus="${bogus} ${iface}"
1547 for iface in ${bogus}; do
1548 ngctl shutdown ${iface}:
1553 # Create Fast EtherChannel interfaces.
1558 for i in ${fec_interfaces}; do
1560 for j in `get_if_var $i fecconfig_IF`; do
1566 ngctl msg ${i}: add_iface "\"${j}\""
1574 # Configure any IPX addresses for interface $ifn. Returns 0 if
1575 # IPX arguments were found and configured; returns 1 otherwise.
1583 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1584 if [ -n "${ifconfig_args}" ]; then
1585 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1593 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1594 # addresses were found and unconfigured. It returns 1, otherwise.
1598 local _if _ifs _ret ipxList oldifs _ipx
1602 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1606 for _ipx in $ipxList ; do
1607 # get rid of extraneous line
1608 [ -z "$_ipx" ] && break
1610 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1613 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1622 # ifnet_rename [ifname]
1623 # Rename interfaces if ifconfig_IF_name is defined.
1630 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1631 _ifname=`get_if_var $_if ifconfig_IF_name`
1632 if [ ! -z "$_ifname" ]; then
1633 ${IFCONFIG_CMD} $_if name $_ifname
1640 # list_net_interfaces type
1641 # List all network interfaces. The type of interface returned
1642 # can be controlled by the type argument. The type
1643 # argument can be any of the following:
1644 # nodhcp - all interfaces, excluding DHCP configured interfaces
1645 # dhcp - list only DHCP configured interfaces
1646 # noautoconf - all interfaces, excluding IPv6 Stateless
1647 # Address Autoconf configured interfaces
1648 # autoconf - list only IPv6 Stateless Address Autoconf
1649 # configured interfaces
1650 # If no argument is specified all network interfaces are output.
1651 # Note that the list will include cloned interfaces if applicable.
1652 # Cloned interfaces must already exist to have a chance to appear
1653 # in the list if ${network_interfaces} is set to `auto'.
1655 list_net_interfaces()
1657 local type _tmplist _list _autolist _lo _if
1660 # Get a list of ALL the interfaces and make lo0 first if it's there.
1663 case ${network_interfaces} in
1665 _autolist="`${IFCONFIG_CMD} -l`"
1667 for _if in ${_autolist} ; do
1668 if autoif $_if; then
1669 if [ "$_if" = "lo0" ]; then
1672 _tmplist="${_tmplist} ${_if}"
1676 _tmplist="${_lo}${_tmplist# }"
1679 for _if in ${network_interfaces} ${cloned_interfaces}; do
1680 # epair(4) uses epair[0-9] for creation and
1681 # epair[0-9][ab] for configuration.
1684 _tmplist="$_tmplist ${_if}a ${_if}b"
1687 _tmplist="$_tmplist $_if"
1692 # lo0 is effectively mandatory, so help prevent foot-shooting
1695 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1696 # This is fine, do nothing
1697 _tmplist="${_tmplist# }"
1700 _tmplist="lo0 ${_tmplist# }"
1709 for _if in ${_tmplist} ; do
1710 if ! dhcpif $_if && \
1711 [ -n "`_ifconfig_getargs $_if`" ]; then
1712 _list="${_list# } ${_if}"
1717 for _if in ${_tmplist} ; do
1718 if dhcpif $_if; then
1719 _list="${_list# } ${_if}"
1724 for _if in ${_tmplist} ; do
1725 if ! ipv6_autoconfif $_if && \
1726 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1727 _list="${_list# } ${_if}"
1732 for _if in ${_tmplist} ; do
1733 if ipv6_autoconfif $_if; then
1734 _list="${_list# } ${_if}"
1748 # get_default_if -address_family
1749 # Get the interface of the default route for the given address family.
1750 # The -address_family argument must be suitable passing to route(8).
1754 local routeget oldifs defif line
1759 for line in `route -n get $1 default 2>/dev/null`; do
1772 # Echo decimal number $arg (single digit) in hexadecimal format.
1779 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1785 is_wired_interface()
1789 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1790 *media:?Ethernet*) media=Ethernet ;;
1793 test "$media" = "Ethernet"
1796 # network6_getladdr if [flag]
1797 # Echo link-local address from $if if any.
1798 # If flag is defined, tentative ones will be excluded.
1801 local _if _flag proto addr rest
1805 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1806 case "${proto}/${addr}/${_flag}/${rest}" in
1810 inet6/fe80:://*tentative*) # w/o flag
1811 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1812 network6_getladdr $_if $_flags
1814 inet6/fe80::/*/*tentative*) # w/ flag