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"
29 # Maximum number of addresses expanded from a address range specification.
33 # Subroutines commonly used from network startup scripts.
34 # Requires that rc.conf be loaded first.
38 # Bring up and configure an interface. If some configuration is
39 # applied, print the interface configuration.
47 [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
49 ifscript_up ${ifn} && cfg=0
50 ifconfig_up ${ifn} && cfg=0
51 if ! noafif $ifn; then
52 afexists inet && ipv4_up ${ifn} && cfg=0
53 afexists inet6 && ipv6_up ${ifn} && cfg=0
55 childif_create ${ifn} && cfg=0
61 # Shutdown and de-configure an interface. If action is taken,
62 # print the interface name.
70 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
72 if ! noafif $ifn; then
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 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 + $_IPEXPANDMAX)) ]; 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."
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 + $_IPEXPANDMAX)) ]
981 warn "Range specification is too large $(printf '(%s:%04x%s-%s:%04x%s)' $_ipleft $_iplow $_ipright $_ipleft $_iphigh $_ipright). $(printf '%s:%04x%s-%s:%04x%s' $_ipleft $_iplow $_ipright $_ipleft $_ipcount $_ipright) was processed."
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
1043 ${_af}\ *[0-9a-fA-F]-*)
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
1061 ${_af}\ *[0-9][a-f][A-F]-*)
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
1088 # Normalize $_if before using it in a pattern to list_vars()
1089 ltr "$_if" "$_punct" "_" _if
1091 # ifconfig_IF_aliasN which starts with $_af
1092 for alias in `list_vars ifconfig_${_if}_alias[0-9]\* |
1093 sort_lite -nk1.$((9+${#_if}+7))`
1095 eval ifconfig_args=\"\$$alias\"
1097 case $ifconfig_args in
1098 inet\ *) _iaf=inet ;;
1099 inet6\ *) _iaf=inet6 ;;
1100 link\ *) _iaf=link ;;
1101 ether\ *) _iaf=ether ;;
1104 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1106 _aliasn="$_aliasn $ifconfig_args"
1112 _aliasn="$_aliasn inet $ifconfig_args"
1113 warn "\$${alias} needs leading" \
1114 "\"inet\" keyword for an IPv4 address."
1118 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1121 for alias in `list_vars ipv6_ifconfig_${_if}_alias[0-9]\* |
1122 sort_lite -nk1.$((14+${#_if}+7))`
1124 eval ifconfig_args=\"\$$alias\"
1125 case ${_action}:"${ifconfig_args}" in
1130 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1131 warn "\$${alias} is obsolete. " \
1132 "Use ifconfig_$1_aliasN instead."
1138 # backward compatibility: ipv4_addrs_IF.
1139 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1140 _aliasn="$_aliasn inet $_tmpargs"
1143 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1145 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1147 inet|inet6|link|ether)
1150 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1156 _tmpargs="$_tmpargs $_c"
1159 # Process the last component
1162 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1169 # ipv6_prefix_hostid_addr_common if action
1170 # Add or remove IPv6 prefix + hostid addr on the interface $if
1172 ipv6_prefix_hostid_addr_common()
1174 local _if _action prefix j
1177 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1179 if [ -n "${prefix}" ]; then
1180 for j in ${prefix}; do
1181 # The default prefixlen is 64.
1184 $plen:$j) plen=64 ;;
1188 # Normalize the last part by removing ":"
1191 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1192 prefixlen $plen eui64 ${_action}
1194 # if I am a router, add subnet router
1195 # anycast address (RFC 2373).
1196 if checkyesno ipv6_gateway_enable; then
1197 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1198 prefixlen $plen ${_action} anycast
1204 # ipv6_accept_rtadv_up if
1205 # Enable accepting Router Advertisement and send Router
1206 # Solicitation message
1207 ipv6_accept_rtadv_up()
1209 if ipv6_autoconfif $1; then
1210 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1211 if ! checkyesno rtsold_enable; then
1212 rtsol ${rtsol_flags} $1
1217 # ipv6_accept_rtadv_down if
1218 # Disable accepting Router Advertisement
1219 ipv6_accept_rtadv_down()
1221 if ipv6_autoconfif $1; then
1222 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1227 # Evaluate a startup script for the $if interface.
1228 # It returns 0 if a script was found and processed or
1229 # 1 if no script was found.
1233 if [ -r /etc/start_if.$1 ]; then
1242 # Evaluate a shutdown script for the $if interface.
1243 # It returns 0 if a script was found and processed or
1244 # 1 if no script was found.
1248 if [ -r /etc/stop_if.$1 ]; then
1257 # Create cloneable interfaces.
1261 local _list ifn ifopt _iflist _n tmpargs
1266 for ifn in ${cloned_interfaces}; do
1268 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1270 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1275 # epair(4) uses epair[0-9] for creation and
1276 # epair[0-9][ab] for configuration.
1278 # Skip if ${ifn}a or ${ifn}b already exist.
1279 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1281 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1284 ${IFCONFIG_CMD} ${ifn} create \
1285 `get_if_var ${ifn} create_args_IF`
1286 if [ $? -eq 0 ]; then
1287 _list="$_list ${ifn}a ${ifn}b"
1291 # Skip if ${ifn} already exists.
1292 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1295 ${IFCONFIG_CMD} ${ifn} create \
1296 `get_if_var ${ifn} create_args_IF`
1297 if [ $? -eq 0 ]; then
1302 if [ -n "$gif_interfaces" ]; then
1303 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1305 for ifn in ${gif_interfaces}; do
1307 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1309 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1312 # Skip if ifn already exists.
1313 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1318 ${IFCONFIG_CMD} $ifn create
1321 _n=$(${IFCONFIG_CMD} gif create)
1322 ${IFCONFIG_CMD} $_n name $ifn
1325 if [ $? -eq 0 ]; then
1328 tmpargs=$(get_if_var $ifn gifconfig_IF)
1329 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1331 if [ -n "${_list# }" ]; then
1332 echo "Created clone interfaces: ${_list# }."
1334 debug "Cloned: ${_list# }"
1338 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1343 local _list ifn _difn ifopt _iflist _sticky
1347 : ${cloned_interfaces_sticky:=NO}
1348 if checkyesno cloned_interfaces_sticky; then
1353 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1355 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1356 case $ifopt:$_sticky in
1357 sticky:*) continue ;; # :sticky => not destroy
1358 nosticky:*) ;; # :nosticky => destroy
1359 *:1) continue ;; # global sticky knob == 1
1362 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1367 # Note: epair(4) uses epair[0-9] for removal and
1368 # epair[0-9][ab] for configuration.
1370 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1371 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1373 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1378 ${IFCONFIG_CMD} -n $_difn destroy
1379 if [ $? -eq 0 ]; then
1380 _list="$_list ${ifn}a ${ifn}b"
1384 # Skip if ifn does not exist.
1385 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1388 ${IFCONFIG_CMD} -n ${ifn} destroy
1389 if [ $? -eq 0 ]; then
1395 if [ -n "${_list# }" ]; then
1396 echo "Destroyed clone interfaces: ${_list# }."
1398 debug "Destroyed clones: ${_list# }"
1402 # Create and configure child interfaces. Return 0 if child
1403 # interfaces are created.
1407 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1411 # Create wireless interfaces
1412 child_wlans=`get_if_var $ifn wlans_IF`
1414 for child in ${child_wlans}; do
1415 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1416 debug_flags="`get_if_var $child wlandebug_IF`"
1418 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1419 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1420 if [ -n "${debug_flags}" ]; then
1421 wlandebug -i $child ${debug_flags}
1424 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1425 if [ -n "${debug_flags}" ]; then
1426 wlandebug -i $i ${debug_flags}
1428 ${IFCONFIG_CMD} $i name $child && cfg=0
1430 if autoif $child; then
1435 # Create vlan interfaces
1436 child_vlans=`get_if_var $ifn vlans_IF`
1438 if [ -n "${child_vlans}" ]; then
1442 for child in ${child_vlans}; do
1443 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1444 child="${ifn}.${child}"
1445 create_args=`get_if_var $child create_args_IF`
1446 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1448 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1449 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1450 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1452 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1453 ${IFCONFIG_CMD} $i name $child && cfg=0
1456 if autoif $child; then
1465 # Destroy child interfaces.
1469 local cfg child child_vlans child_wlans ifn
1472 child_wlans=`get_if_var $ifn wlans_IF`
1473 for child in ${child_wlans}; do
1474 if ! ifexists $child; then
1477 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1480 child_vlans=`get_if_var $ifn vlans_IF`
1481 for child in ${child_vlans}; do
1482 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1483 child="${ifn}.${child}"
1485 if ! ifexists $child; then
1488 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1495 # Create netgraph nodes.
1499 ngctl -f - 2> /dev/null <<EOF
1506 # Create netgraph nodes.
1512 ng_mkpeer $* | while read line; do
1513 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1514 if [ -n "${t}" ]; then
1521 # ifnet_rename [ifname]
1522 # Rename interfaces if ifconfig_IF_name is defined.
1529 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1530 _ifname=`get_if_var $_if ifconfig_IF_name`
1531 if [ ! -z "$_ifname" ]; then
1532 ${IFCONFIG_CMD} $_if name $_ifname
1539 # list_net_interfaces type
1540 # List all network interfaces. The type of interface returned
1541 # can be controlled by the type argument. The type
1542 # argument can be any of the following:
1543 # nodhcp - all interfaces, excluding DHCP configured interfaces
1544 # dhcp - list only DHCP configured interfaces
1545 # noautoconf - all interfaces, excluding IPv6 Stateless
1546 # Address Autoconf configured interfaces
1547 # autoconf - list only IPv6 Stateless Address Autoconf
1548 # configured interfaces
1549 # If no argument is specified all network interfaces are output.
1550 # Note that the list will include cloned interfaces if applicable.
1551 # Cloned interfaces must already exist to have a chance to appear
1552 # in the list if ${network_interfaces} is set to `auto'.
1554 list_net_interfaces()
1556 local type _tmplist _list _autolist _lo _if
1559 # Get a list of ALL the interfaces and make lo0 first if it's there.
1562 case ${network_interfaces} in
1564 _autolist="`${IFCONFIG_CMD} -l`"
1566 for _if in ${_autolist} ; do
1567 if autoif $_if; then
1568 if [ "$_if" = "lo0" ]; then
1571 _tmplist="${_tmplist} ${_if}"
1575 _tmplist="${_lo}${_tmplist# }"
1578 for _if in ${network_interfaces} ${cloned_interfaces}; do
1579 # epair(4) uses epair[0-9] for creation and
1580 # epair[0-9][ab] for configuration.
1583 _tmplist="$_tmplist ${_if}a ${_if}b"
1586 _tmplist="$_tmplist $_if"
1591 # lo0 is effectively mandatory, so help prevent foot-shooting
1594 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1595 # This is fine, do nothing
1596 _tmplist="${_tmplist# }"
1599 _tmplist="lo0 ${_tmplist# }"
1608 for _if in ${_tmplist} ; do
1609 if ! dhcpif $_if && \
1610 [ -n "`_ifconfig_getargs $_if`" ]; then
1611 _list="${_list# } ${_if}"
1616 for _if in ${_tmplist} ; do
1617 if dhcpif $_if; then
1618 _list="${_list# } ${_if}"
1623 for _if in ${_tmplist} ; do
1624 if ! ipv6_autoconfif $_if && \
1625 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1626 _list="${_list# } ${_if}"
1631 for _if in ${_tmplist} ; do
1632 if ipv6_autoconfif $_if; then
1633 _list="${_list# } ${_if}"
1647 # get_default_if -address_family
1648 # Get the interface of the default route for the given address family.
1649 # The -address_family argument must be suitable passing to route(8).
1653 local routeget oldifs defif line
1658 for line in `route -n get $1 default 2>/dev/null`; do
1671 # Echo decimal number $arg (single digit) in hexadecimal format.
1678 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1684 is_wired_interface()
1688 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1689 *media:?Ethernet*) media=Ethernet ;;
1692 test "$media" = "Ethernet"
1695 # network6_getladdr if [flag]
1696 # Echo link-local address from $if if any.
1697 # If flag is defined, tentative ones will be excluded.
1700 local _if _flag proto addr rest
1704 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1705 case "${proto}/${addr}/${_flag}/${rest}" in
1709 inet6/fe80:://*tentative*) # w/o flag
1710 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1711 network6_getladdr $_if $_flags
1713 inet6/fe80::/*/*tentative*) # w/ flag