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 ifconfig_args=`ifconfig_getargs $1`
137 if [ -n "${ifconfig_args}" ]; then
138 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
143 if ! noafif $1 && afexists inet6; then
144 if checkyesno ipv6_activate_all_interfaces; then
145 _ipv6_opts="-ifdisabled"
146 elif [ "$1" != "lo0" ]; then
147 _ipv6_opts="ifdisabled"
150 # backward compatibility: $ipv6_enable
152 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
155 # No accept_rtadv by default on if_bridge(4)
156 # to avoid a conflict with the member
160 if ! checkyesno ipv6_gateway_enable; then
161 _ipv6_opts="${_ipv6_opts} accept_rtadv"
168 case $ipv6_cpe_wanif in
170 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
174 if [ -n "${_ipv6_opts}" ]; then
175 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
179 ifconfig_args=`ifconfig_getargs $1 ipv6`
180 if [ -n "${ifconfig_args}" ]; then
181 # backward compatibility: inet6 keyword
182 case "${ifconfig_args}" in
184 warn "\$ifconfig_$1_ipv6 needs leading" \
185 "\"inet6\" keyword for an IPv6 address."
186 ifconfig_args="inet6 ${ifconfig_args}"
189 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
190 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
194 # $ipv6_prefix_IF will be handled in
195 # ipv6_prefix_hostid_addr_common().
196 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
197 if [ -n "${ifconfig_args}" ]; then
198 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
202 # backward compatibility: $ipv6_ifconfig_IF
203 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
204 if [ -n "${ifconfig_args}" ]; then
205 warn "\$ipv6_ifconfig_$1 is obsolete." \
206 " Use ifconfig_$1_ipv6 instead."
207 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
208 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
213 ifalias $1 link alias
214 ifalias $1 ether alias
216 if [ ${_cfg} -eq 0 ]; then
217 ${IFCONFIG_CMD} $1 up
221 /etc/rc.d/wpa_supplicant start $1
222 _cfg=0 # XXX: not sure this should count
223 elif hostapif $1; then
224 /etc/rc.d/hostapd start $1
229 if [ $_cfg -ne 0 ] ; then
230 ${IFCONFIG_CMD} $1 up
232 if syncdhcpif $1; then
233 /etc/rc.d/dhclient start $1
242 # returns 1 if wpa_supplicant or dhclient was stopped or
243 # the interface exists.
251 /etc/rc.d/wpa_supplicant stop $1
253 elif hostapif $1; then
254 /etc/rc.d/hostapd stop $1
259 /etc/rc.d/dhclient stop $1
264 ${IFCONFIG_CMD} $1 down
271 # get_if_var if var [default]
272 # Return the value of the pseudo-hash corresponding to $if where
273 # $var is a string containg the sub-string "IF" which will be
274 # replaced with $if after the characters defined in _punct are
275 # replaced with '_'. If the variable is unset, replace it with
279 local _if _punct _punct_c _var _default prefix suffix
281 if [ $# -ne 2 -a $# -ne 3 ]; then
282 err 3 'USAGE: get_if_var name var [default]'
287 ltr ${_if} "${_punct}" '_' _if
293 eval echo \${${prefix}${_if}${suffix}-${_default}}
296 # _ifconfig_getargs if [af]
297 # Prints the arguments for the supplied interface to stdout.
298 # Returns 1 if empty. In general, ifconfig_getargs should be used
306 if [ -z "$_ifn" ]; then
310 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
313 # ifconfig_getargs if [af]
314 # Takes the result from _ifconfig_getargs and removes pseudo
315 # args such as DHCP and WPA.
318 local _tmpargs _arg _args _vnet
319 _tmpargs=`_ifconfig_getargs $1 $2`
320 if [ $? -eq 1 ]; then
326 for _arg in $_tmpargs; do
328 [Dd][Hh][Cc][Pp]:0) ;;
329 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
330 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
331 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
333 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
346 # Returns 0 if the interface should be automatically configured at
347 # boot time and 1 otherwise.
351 _tmpargs=`_ifconfig_getargs $1`
353 for _arg in $_tmpargs; do
355 [Nn][Oo][Aa][Uu][Tt][Oo])
365 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
369 _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 ${SYSCTL_N} net.ipx > /dev/null 2>&1
498 if [ -x /sbin/atmconfig ]; then
499 /sbin/atmconfig diag list > /dev/null 2>&1
508 err 1 "afexists(): Unsupported address family: $_af"
514 # Returns 0 if the interface has no af configuration and 1 otherwise.
542 # Returns 0 if the interface should be configured for IPv6 and
549 if ! afexists inet6; then
553 # lo0 is always IPv6-enabled
560 case "${ipv6_network_interfaces}" in
561 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
562 # True if $ifconfig_IF_ipv6 is defined.
563 _tmpargs=`_ifconfig_getargs $_if ipv6`
564 if [ -n "${_tmpargs}" ]; then
568 # True if $ipv6_prefix_IF is defined.
569 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
570 if [ -n "${_tmpargs}" ]; then
574 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
575 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
576 if [ -n "${_tmpargs}" ]; then
586 # Returns 0 if the interface should be configured for IPv6 with
587 # Stateless Address Configuration; 1 otherwise.
590 local _if _tmpargs _arg
605 if ! ipv6if $_if; then
608 if checkyesno ipv6_gateway_enable; then
611 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
612 if [ -n "${_tmpargs}" ]; then
615 # backward compatibility: $ipv6_enable
617 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
618 if checkyesno ipv6_gateway_enable; then
623 # No accept_rtadv by default on if_bridge(4)
624 # to avoid a conflict with the member
635 _tmpargs=`_ifconfig_getargs $_if ipv6`
636 for _arg in $_tmpargs; do
644 # backward compatibility: $ipv6_ifconfig_IF
645 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
646 for _arg in $_tmpargs; do
658 # Returns 0 if the interface exists and 1 otherwise.
661 [ -z "$1" ] && return 1
662 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
666 # add IPv4 addresses to the interface $if
673 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
674 if [ "${_if}" = "lo0" ]; then
675 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
676 if [ -z "${ifconfig_args}" ]; then
677 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
680 ifalias ${_if} inet alias && _ret=0
686 # add IPv6 addresses to the interface $if
693 if ! ipv6if $_if; then
697 ifalias ${_if} inet6 alias && _ret=0
698 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
699 ipv6_accept_rtadv_up ${_if} && _ret=0
705 # remove IPv4 addresses from the interface $if
708 local _if _ifs _ret inetList oldifs _inet
713 ifalias ${_if} inet -alias && _ret=0
715 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
719 for _inet in $inetList ; do
720 # get rid of extraneous line
726 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
729 ${IFCONFIG_CMD} ${_if} ${_inet} delete
739 # remove IPv6 addresses from the interface $if
742 local _if _ifs _ret inetList oldifs _inet6
747 if ! ipv6if $_if; then
751 ipv6_accept_rtadv_down ${_if} && _ret=0
752 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
753 ifalias ${_if} inet6 -alias && _ret=0
755 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
759 for _inet6 in $inetList ; do
760 # get rid of extraneous line
766 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
769 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
778 # ifalias if af action
779 # Configure or remove aliases for network interface $if.
780 # It returns 0 if at least one alias was configured or
781 # removed, or 1 if there were none.
788 afexists $2 || return $_ret
791 inet|inet6|link|ether)
792 ifalias_af_common $1 $2 $3 && _ret=0
799 # ifalias_expand_addr af action addr
800 # Expand address range ("N-M") specification in addr.
801 # "addr" must not include an address-family keyword.
802 # The results will include an address-family keyword.
804 ifalias_expand_addr()
812 afexists $_af || return
813 ifalias_expand_addr_$_af $_action $*
816 # ifalias_expand_addr_inet action addr
817 # Helper function for ifalias_expand_addr(). Handles IPv4.
819 ifalias_expand_addr_inet()
821 local _action _arg _cidr _cidr_addr _exargs
822 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
830 case $_action:$_arg:$_exargs in
831 *:*--*) return ;; # invalid
832 tmp:*[0-9]-[0-9]*:*) # to be expanded
835 *:*[0-9]-[0-9]*:*) # to be expanded
837 tmp:*:*netmask*) # already expanded w/ netmask option
838 echo ${_arg%/[0-9]*} $_exargs && return
840 tmp:*:*) # already expanded w/o netmask option
841 echo $_arg $_exargs && return
843 *:*:*netmask*) # already expanded w/ netmask option
844 echo inet ${_arg%/[0-9]*} $_exargs && return
846 *:*:*) # already expanded w/o netmask option
847 echo inet $_arg $_exargs && return
851 for _cidr in $_arg; do
854 # When subnet prefix length is not specified, use /32.
856 $_ipaddr) _plen=32 ;; # "/" character not found
860 IFS=. set -- $_ipaddr
870 _iphead="${_iphead}${_iphead:+.}${_c}"
873 _iptail="${_iptail}${_iptail:+.}${_c}"
881 # clear netmask when removing aliases
882 if [ "$_action" = "-alias" ]; then
887 while [ "$_ipcount" -le "$_iphigh" ]; do
888 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
889 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
890 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."
893 _ipcount=$(($_ipcount + 1))
895 # Forcibly set /32 for remaining aliases.
900 for _c in $_retstr; do
901 ifalias_expand_addr_inet $_action $_c $_exargs
905 # ifalias_expand_addr_inet6 action addr
906 # Helper function for ifalias_expand_addr(). Handles IPv6.
908 ifalias_expand_addr_inet6()
910 local _action _arg _cidr _cidr_addr _exargs
911 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
920 case $_action:$_arg:$_exargs in
921 *:*--*:*) return ;; # invalid
922 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
925 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
927 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
928 echo ${_arg%/[0-9]*} $_exargs && return
930 tmp:*:*) # already expanded w/o prefixlen option
931 echo $_arg $_exargs && return
933 *:*:*prefixlen*) # already expanded w/ prefixlen option
934 echo inet6 ${_arg%/[0-9]*} $_exargs && return
936 *:*:*) # already expanded w/o prefixlen option
937 echo inet6 $_arg $_exargs && return
941 for _cidr in $_arg; do
942 _ipaddr="${_cidr%%/*}"
945 case $_action:$_ipaddr:$_cidr in
946 -alias:*:*) unset _plen ;;
947 *:$_cidr:$_ipaddr) unset _plen ;;
950 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
951 # Handle !v4mapped && !v4compat addresses.
953 # The default prefix length is 64.
954 case $_ipaddr:$_cidr in
955 $_cidr:$_ipaddr) _plen="64" ;;
957 _ipleft=${_ipaddr%-*}
958 _ipright=${_ipaddr#*-}
959 _iplow=${_ipleft##*:}
960 _iphigh=${_ipright%%:*}
961 _ipleft=${_ipleft%:*}
962 _ipright=${_ipright#*:}
964 if [ "$_iphigh" = "$_ipright" ]; then
970 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
971 _iplow=$((0x$_iplow))
972 _iphigh=$((0x$_iphigh))
974 while [ $_ipcount -le $_iphigh ]; do
975 _r=`printf "%s:%04x%s%s" \
976 $_ipleft $_ipcount $_ipright \
978 _retstr="$_retstr $_r"
979 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
981 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."
984 _ipcount=$(($_ipcount + 1))
988 _retstr="${_ipaddr}${_plen:+/}${_plen}"
991 for _c in $_retstr; do
992 ifalias_expand_addr_inet6 $_action $_c $_exargs
995 # v4mapped/v4compat should handle as an IPv4 alias
996 _ipv4part=${_ipaddr##*:}
998 # Adjust prefix length if any. If not, set the
999 # default prefix length as 32.
1000 case $_ipaddr:$_cidr in
1001 $_cidr:$_ipaddr) _plen=32 ;;
1002 *) _plen=$(($_plen - 96)) ;;
1005 _retstr=`ifalias_expand_addr_inet \
1006 tmp ${_ipv4part}${_plen:+/}${_plen}`
1007 for _c in $_retstr; do
1008 ifalias_expand_addr_inet $_action $_c $_exargs
1014 # ifalias_af_common_handler if af action args
1015 # Helper function for ifalias_af_common().
1017 ifalias_af_common_handler()
1019 local _ret _if _af _action _args _c _tmpargs
1033 # link(ether) does not support address removal.
1034 case $_af:$_action in
1035 link:-alias|ether:-alias) return ;;
1039 for _c in $_args; do
1044 ifalias_af_common_handler $_if $_af $_action \
1045 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1048 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1054 _tmpargs="$_tmpargs $_c"
1058 # Process the last component if any.
1059 if [ -n "$_tmpargs}" ]; then
1062 ifalias_af_common_handler $_if $_af $_action \
1063 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1066 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1074 # ifalias_af_common if af action
1075 # Helper function for ifalias().
1079 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1080 local _vif _punct=".-/+"
1088 # Normalize $_if before using it in a pattern to list_vars()
1089 ltr "$_if" "$_punct" "_" _vif
1091 # ifconfig_IF_aliasN which starts with $_af
1092 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1093 sort_lite -nk1.$((9+${#_vif}+7))`
1095 eval ifconfig_args=\"\$$alias\"
1097 case $ifconfig_args in
1098 inet\ *) _iaf=inet ;;
1099 inet6\ *) _iaf=inet6 ;;
1101 link\ *) _iaf=link ;;
1102 ether\ *) _iaf=ether ;;
1105 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1107 _aliasn="$_aliasn $ifconfig_args"
1113 _aliasn="$_aliasn inet $ifconfig_args"
1114 warn "\$${alias} needs leading" \
1115 "\"inet\" keyword for an IPv4 address."
1119 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1122 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1123 sort_lite -nk1.$((14+${#_vif}+7))`
1125 eval ifconfig_args=\"\$$alias\"
1126 case ${_action}:"${ifconfig_args}" in
1131 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1132 warn "\$${alias} is obsolete. " \
1133 "Use ifconfig_${_vif}_aliasN instead."
1139 # backward compatibility: ipv4_addrs_IF.
1140 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1141 _aliasn="$_aliasn inet $_tmpargs"
1144 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1146 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1148 inet|inet6|ipx|link|ether)
1151 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1157 _tmpargs="$_tmpargs $_c"
1160 # Process the last component
1163 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1170 # ipv6_prefix_hostid_addr_common if action
1171 # Add or remove IPv6 prefix + hostid addr on the interface $if
1173 ipv6_prefix_hostid_addr_common()
1175 local _if _action prefix j
1178 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1180 if [ -n "${prefix}" ]; then
1181 for j in ${prefix}; do
1182 # The default prefixlen is 64.
1185 $plen:$j) plen=64 ;;
1189 # Normalize the last part by removing ":"
1192 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1193 prefixlen $plen eui64 ${_action}
1195 # if I am a router, add subnet router
1196 # anycast address (RFC 2373).
1197 if checkyesno ipv6_gateway_enable; then
1198 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1199 prefixlen $plen ${_action} anycast
1205 # ipv6_accept_rtadv_up if
1206 # Enable accepting Router Advertisement and send Router
1207 # Solicitation message
1208 ipv6_accept_rtadv_up()
1210 if ipv6_autoconfif $1; then
1211 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1212 if ! checkyesno rtsold_enable; then
1213 rtsol ${rtsol_flags} $1
1218 # ipv6_accept_rtadv_down if
1219 # Disable accepting Router Advertisement
1220 ipv6_accept_rtadv_down()
1222 if ipv6_autoconfif $1; then
1223 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1228 # Evaluate a startup script for the $if interface.
1229 # It returns 0 if a script was found and processed or
1230 # 1 if no script was found.
1234 if [ -r /etc/start_if.$1 ]; then
1243 # Evaluate a shutdown script for the $if interface.
1244 # It returns 0 if a script was found and processed or
1245 # 1 if no script was found.
1249 if [ -r /etc/stop_if.$1 ]; then
1258 # Create cloneable interfaces.
1262 local _list ifn ifopt _iflist _n tmpargs
1267 for ifn in ${cloned_interfaces}; do
1269 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1271 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1276 # epair(4) uses epair[0-9] for creation and
1277 # epair[0-9][ab] for configuration.
1279 # Skip if ${ifn}a or ${ifn}b already exist.
1280 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1282 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1285 ${IFCONFIG_CMD} ${ifn} create \
1286 `get_if_var ${ifn} create_args_IF`
1287 if [ $? -eq 0 ]; then
1288 _list="$_list ${ifn}a ${ifn}b"
1292 # Skip if ${ifn} already exists.
1293 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1296 ${IFCONFIG_CMD} ${ifn} create \
1297 `get_if_var ${ifn} create_args_IF`
1298 if [ $? -eq 0 ]; then
1303 if [ -n "$gif_interfaces" ]; then
1304 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1306 for ifn in ${gif_interfaces}; do
1308 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1310 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1313 # Skip if ifn already exists.
1314 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1319 ${IFCONFIG_CMD} $ifn create
1322 _n=$(${IFCONFIG_CMD} gif create)
1323 ${IFCONFIG_CMD} $_n name $ifn
1326 if [ $? -eq 0 ]; then
1329 tmpargs=$(get_if_var $ifn gifconfig_IF)
1330 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1332 if [ -n "${_list# }" ]; then
1333 echo "Created clone interfaces: ${_list# }."
1335 debug "Cloned: ${_list# }"
1339 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1344 local _list ifn _difn ifopt _iflist _sticky
1348 : ${cloned_interfaces_sticky:=NO}
1349 if checkyesno cloned_interfaces_sticky; then
1354 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1356 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1357 case $ifopt:$_sticky in
1358 sticky:*) continue ;; # :sticky => not destroy
1359 nosticky:*) ;; # :nosticky => destroy
1360 *:1) continue ;; # global sticky knob == 1
1363 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1368 # Note: epair(4) uses epair[0-9] for removal and
1369 # epair[0-9][ab] for configuration.
1371 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1372 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1374 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1379 ${IFCONFIG_CMD} -n $_difn destroy
1380 if [ $? -eq 0 ]; then
1381 _list="$_list ${ifn}a ${ifn}b"
1385 # Skip if ifn does not exist.
1386 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1389 ${IFCONFIG_CMD} -n ${ifn} destroy
1390 if [ $? -eq 0 ]; then
1396 if [ -n "${_list# }" ]; then
1397 echo "Destroyed clone interfaces: ${_list# }."
1399 debug "Destroyed clones: ${_list# }"
1403 # Create and configure child interfaces. Return 0 if child
1404 # interfaces are created.
1408 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1412 # Create wireless interfaces
1413 child_wlans=`get_if_var $ifn wlans_IF`
1415 for child in ${child_wlans}; do
1416 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1417 debug_flags="`get_if_var $child wlandebug_IF`"
1419 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1420 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1421 if [ -n "${debug_flags}" ]; then
1422 wlandebug -i $child ${debug_flags}
1425 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1426 if [ -n "${debug_flags}" ]; then
1427 wlandebug -i $i ${debug_flags}
1429 ${IFCONFIG_CMD} $i name $child && cfg=0
1431 if autoif $child; then
1436 # Create vlan interfaces
1437 child_vlans=`get_if_var $ifn vlans_IF`
1439 if [ -n "${child_vlans}" ]; then
1443 for child in ${child_vlans}; do
1444 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1445 child="${ifn}.${child}"
1446 create_args=`get_if_var $child create_args_IF`
1447 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1449 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1450 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1451 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1453 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1454 ${IFCONFIG_CMD} $i name $child && cfg=0
1457 if autoif $child; then
1466 # Destroy child interfaces.
1470 local cfg child child_vlans child_wlans ifn
1473 child_wlans=`get_if_var $ifn wlans_IF`
1474 for child in ${child_wlans}; do
1475 if ! ifexists $child; then
1478 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1481 child_vlans=`get_if_var $ifn vlans_IF`
1482 for child in ${child_vlans}; do
1483 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1484 child="${ifn}.${child}"
1486 if ! ifexists $child; then
1489 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1496 # Create netgraph nodes.
1500 ngctl -f - 2> /dev/null <<EOF
1507 # Create netgraph nodes.
1513 ng_mkpeer $* | while read line; do
1514 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1515 if [ -n "${t}" ]; then
1523 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1524 # FEC arguments were found and configured; returns !0 otherwise.
1527 local req_iface iface bogus
1530 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1534 iface=`ng_create_one fec dummy fec`
1535 if [ -z "${iface}" ]; then
1538 if [ "${iface}" = "${req_iface}" ]; then
1541 bogus="${bogus} ${iface}"
1544 for iface in ${bogus}; do
1545 ngctl shutdown ${iface}:
1550 # Create Fast EtherChannel interfaces.
1555 for i in ${fec_interfaces}; do
1557 for j in `get_if_var $i fecconfig_IF`; do
1563 ngctl msg ${i}: add_iface "\"${j}\""
1571 # Configure any IPX addresses for interface $ifn. Returns 0 if
1572 # IPX arguments were found and configured; returns 1 otherwise.
1580 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1581 if [ -n "${ifconfig_args}" ]; then
1582 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1590 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1591 # addresses were found and unconfigured. It returns 1, otherwise.
1595 local _if _ifs _ret ipxList oldifs _ipx
1599 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1603 for _ipx in $ipxList ; do
1604 # get rid of extraneous line
1605 [ -z "$_ipx" ] && break
1607 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1610 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1619 # ifnet_rename [ifname]
1620 # Rename interfaces if ifconfig_IF_name is defined.
1627 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1628 _ifname=`get_if_var $_if ifconfig_IF_name`
1629 if [ ! -z "$_ifname" ]; then
1630 ${IFCONFIG_CMD} $_if name $_ifname
1637 # list_net_interfaces type
1638 # List all network interfaces. The type of interface returned
1639 # can be controlled by the type argument. The type
1640 # argument can be any of the following:
1641 # nodhcp - all interfaces, excluding DHCP configured interfaces
1642 # dhcp - list only DHCP configured interfaces
1643 # noautoconf - all interfaces, excluding IPv6 Stateless
1644 # Address Autoconf configured interfaces
1645 # autoconf - list only IPv6 Stateless Address Autoconf
1646 # configured interfaces
1647 # If no argument is specified all network interfaces are output.
1648 # Note that the list will include cloned interfaces if applicable.
1649 # Cloned interfaces must already exist to have a chance to appear
1650 # in the list if ${network_interfaces} is set to `auto'.
1652 list_net_interfaces()
1654 local type _tmplist _list _autolist _lo _if
1657 # Get a list of ALL the interfaces and make lo0 first if it's there.
1660 case ${network_interfaces} in
1662 _autolist="`${IFCONFIG_CMD} -l`"
1664 for _if in ${_autolist} ; do
1665 if autoif $_if; then
1666 if [ "$_if" = "lo0" ]; then
1669 _tmplist="${_tmplist} ${_if}"
1673 _tmplist="${_lo}${_tmplist# }"
1676 for _if in ${network_interfaces} ${cloned_interfaces}; do
1677 # epair(4) uses epair[0-9] for creation and
1678 # epair[0-9][ab] for configuration.
1681 _tmplist="$_tmplist ${_if}a ${_if}b"
1684 _tmplist="$_tmplist $_if"
1689 # lo0 is effectively mandatory, so help prevent foot-shooting
1692 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1693 # This is fine, do nothing
1694 _tmplist="${_tmplist# }"
1697 _tmplist="lo0 ${_tmplist# }"
1706 for _if in ${_tmplist} ; do
1707 if ! dhcpif $_if && \
1708 [ -n "`_ifconfig_getargs $_if`" ]; then
1709 _list="${_list# } ${_if}"
1714 for _if in ${_tmplist} ; do
1715 if dhcpif $_if; then
1716 _list="${_list# } ${_if}"
1721 for _if in ${_tmplist} ; do
1722 if ! ipv6_autoconfif $_if && \
1723 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1724 _list="${_list# } ${_if}"
1729 for _if in ${_tmplist} ; do
1730 if ipv6_autoconfif $_if; then
1731 _list="${_list# } ${_if}"
1745 # get_default_if -address_family
1746 # Get the interface of the default route for the given address family.
1747 # The -address_family argument must be suitable passing to route(8).
1751 local routeget oldifs defif line
1756 for line in `route -n get $1 default 2>/dev/null`; do
1769 # Echo decimal number $arg (single digit) in hexadecimal format.
1776 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1782 is_wired_interface()
1786 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1787 *media:?Ethernet*) media=Ethernet ;;
1790 test "$media" = "Ethernet"
1793 # network6_getladdr if [flag]
1794 # Echo link-local address from $if if any.
1795 # If flag is defined, tentative ones will be excluded.
1798 local _if _flag proto addr rest
1802 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1803 case "${proto}/${addr}/${_flag}/${rest}" in
1807 inet6/fe80:://*tentative*) # w/o flag
1808 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1809 network6_getladdr $_if $_flags
1811 inet6/fe80::/*/*tentative*) # w/ flag