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 afexists inet && ipv4_up ${ifn} && cfg=0
52 afexists inet6 && ipv6_up ${ifn} && cfg=0
53 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 afexists ipx && ipx_down ${ifn} && cfg=0
72 afexists inet6 && ipv6_down ${ifn} && cfg=0
73 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 # Evaluate ifconfig(8) arguments for interface $if and
83 # run ifconfig(8) with those arguments. It returns 0 if
84 # arguments were found and executed or 1 if the interface
85 # had no arguments. Pseudo arguments DHCP and WPA are handled
90 local _cfg _ipv6_opts ifconfig_args
93 # Make sure lo0 always comes up.
94 if [ "$1" = "lo0" ]; then
99 ifconfig_args=`ifconfig_getargs $1`
100 if [ -n "${ifconfig_args}" ]; then
101 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
106 if afexists inet6; then
107 if checkyesno ipv6_activate_all_interfaces; then
108 _ipv6_opts="-ifdisabled"
109 elif [ "$1" != "lo0" ]; then
110 _ipv6_opts="ifdisabled"
113 # backward compatibility: $ipv6_enable
115 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
118 # No accept_rtadv by default on if_bridge(4)
119 # to avoid a conflict with the member
123 if ! checkyesno ipv6_gateway_enable; then
124 _ipv6_opts="${_ipv6_opts} accept_rtadv"
131 case $ipv6_cpe_wanif in
133 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
137 if [ -n "${_ipv6_opts}" ]; then
138 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
142 ifconfig_args=`ifconfig_getargs $1 ipv6`
143 if [ -n "${ifconfig_args}" ]; then
144 # backward compatibility: inet6 keyword
145 case "${ifconfig_args}" in
147 warn "\$ifconfig_$1_ipv6 needs leading" \
148 "\"inet6\" keyword for an IPv6 address."
149 ifconfig_args="inet6 ${ifconfig_args}"
152 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
153 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
157 # $ipv6_prefix_IF will be handled in
158 # ipv6_prefix_hostid_addr_common().
159 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
160 if [ -n "${ifconfig_args}" ]; then
161 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
165 # backward compatibility: $ipv6_ifconfig_IF
166 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
167 if [ -n "${ifconfig_args}" ]; then
168 warn "\$ipv6_ifconfig_$1 is obsolete." \
169 " Use ifconfig_$1_ipv6 instead."
170 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
171 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
176 ifalias $1 link alias
177 ifalias $1 ether alias
179 if [ ${_cfg} -eq 0 ]; then
180 ${IFCONFIG_CMD} $1 up
184 /etc/rc.d/wpa_supplicant start $1
185 _cfg=0 # XXX: not sure this should count
186 elif hostapif $1; then
187 /etc/rc.d/hostapd start $1
192 if [ $_cfg -ne 0 ] ; then
193 ${IFCONFIG_CMD} $1 up
195 if syncdhcpif $1; then
196 /etc/rc.d/dhclient start $1
205 # returns 1 if wpa_supplicant or dhclient was stopped or
206 # the interface exists.
214 /etc/rc.d/wpa_supplicant stop $1
216 elif hostapif $1; then
217 /etc/rc.d/hostapd stop $1
222 /etc/rc.d/dhclient stop $1
227 ${IFCONFIG_CMD} $1 down
234 # get_if_var if var [default]
235 # Return the value of the pseudo-hash corresponding to $if where
236 # $var is a string containg the sub-string "IF" which will be
237 # replaced with $if after the characters defined in _punct are
238 # replaced with '_'. If the variable is unset, replace it with
242 local _if _punct _punct_c _var _default prefix suffix
244 if [ $# -ne 2 -a $# -ne 3 ]; then
245 err 3 'USAGE: get_if_var name var [default]'
250 ltr ${_if} "${_punct}" '_' _if
256 eval echo \${${prefix}${_if}${suffix}-${_default}}
259 # _ifconfig_getargs if [af]
260 # Prints the arguments for the supplied interface to stdout.
261 # Returns 1 if empty. In general, ifconfig_getargs should be used
269 if [ -z "$_ifn" ]; then
273 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
276 # ifconfig_getargs if [af]
277 # Takes the result from _ifconfig_getargs and removes pseudo
278 # args such as DHCP and WPA.
281 local _tmpargs _arg _args
282 _tmpargs=`_ifconfig_getargs $1 $2`
283 if [ $? -eq 1 ]; then
288 for _arg in $_tmpargs; do
291 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
292 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
293 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
295 [Hh][Oo][Ss][Tt][Aa][Pp]) ;;
306 # Returns 0 if the interface should be automatically configured at
307 # boot time and 1 otherwise.
311 _tmpargs=`_ifconfig_getargs $1`
313 for _arg in $_tmpargs; do
315 [Nn][Oo][Aa][Uu][Tt][Oo])
325 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
329 _tmpargs=`_ifconfig_getargs $1`
344 for _arg in $_tmpargs; do
349 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
352 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
362 # Returns 0 if the interface should be configured synchronously and
367 _tmpargs=`_ifconfig_getargs $1`
373 for _arg in $_tmpargs; do
375 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
378 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
384 checkyesno synchronous_dhclient
388 # Returns 0 if the interface is a WPA interface and 1 otherwise.
392 _tmpargs=`_ifconfig_getargs $1`
394 for _arg in $_tmpargs; do
406 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
410 _tmpargs=`_ifconfig_getargs $1`
412 for _arg in $_tmpargs; do
414 [Hh][Oo][Ss][Tt][Aa][Pp])
424 # Returns 0 if the address family is enabled in the kernel
433 check_kern_features ${_af}
436 ${SYSCTL_N} net.ipx > /dev/null 2>&1
439 if [ -x /sbin/atmconfig ]; then
440 /sbin/atmconfig diag list > /dev/null 2>&1
449 err 1 "afexists(): Unsupported address family: $_af"
455 # Returns 0 if the interface has no af configuration and 1 otherwise.
482 # Returns 0 if the interface should be configured for IPv6 and
489 if ! afexists inet6; then
493 # lo0 is always IPv6-enabled
500 case "${ipv6_network_interfaces}" in
501 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
502 # True if $ifconfig_IF_ipv6 is defined.
503 _tmpargs=`_ifconfig_getargs $_if ipv6`
504 if [ -n "${_tmpargs}" ]; then
508 # True if $ipv6_prefix_IF is defined.
509 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
510 if [ -n "${_tmpargs}" ]; then
514 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
515 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
516 if [ -n "${_tmpargs}" ]; then
526 # Returns 0 if the interface should be configured for IPv6 with
527 # Stateless Address Configuration; 1 otherwise.
530 local _if _tmpargs _arg
545 if ! ipv6if $_if; then
548 if checkyesno ipv6_gateway_enable; then
551 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
552 if [ -n "${_tmpargs}" ]; then
555 # backward compatibility: $ipv6_enable
557 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
558 if checkyesno ipv6_gateway_enable; then
563 # No accept_rtadv by default on if_bridge(4)
564 # to avoid a conflict with the member
575 _tmpargs=`_ifconfig_getargs $_if ipv6`
576 for _arg in $_tmpargs; do
584 # backward compatibility: $ipv6_ifconfig_IF
585 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
586 for _arg in $_tmpargs; do
598 # Returns 0 if the interface exists and 1 otherwise.
601 [ -z "$1" ] && return 1
602 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
606 # add IPv4 addresses to the interface $if
613 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
614 if [ "${_if}" = "lo0" ]; then
615 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
616 if [ -z "${ifconfig_args}" ]; then
617 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
620 ifalias ${_if} inet alias && _ret=0
626 # add IPv6 addresses to the interface $if
633 if ! ipv6if $_if; then
637 ifalias ${_if} inet6 alias && _ret=0
638 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
639 ipv6_accept_rtadv_up ${_if} && _ret=0
645 # remove IPv4 addresses from the interface $if
648 local _if _ifs _ret inetList oldifs _inet
653 ifalias ${_if} inet -alias && _ret=0
655 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
659 for _inet in $inetList ; do
660 # get rid of extraneous line
661 [ -z "$_inet" ] && break
663 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
666 ${IFCONFIG_CMD} ${_if} ${_inet} delete
676 # remove IPv6 addresses from the interface $if
679 local _if _ifs _ret inetList oldifs _inet6
684 if ! ipv6if $_if; then
688 ipv6_accept_rtadv_down ${_if} && _ret=0
689 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
690 ifalias ${_if} inet6 -alias && _ret=0
692 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
696 for _inet6 in $inetList ; do
697 # get rid of extraneous line
698 [ -z "$_inet6" ] && break
700 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
703 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
712 # ifalias if af action
713 # Configure or remove aliases for network interface $if.
714 # It returns 0 if at least one alias was configured or
715 # removed, or 1 if there were none.
722 afexists $2 || return $_ret
725 inet|inet6|link|ether)
726 ifalias_af_common $1 $2 $3 && _ret=0
733 # ifalias_expand_addr af action addr
734 # Expand address range ("N-M") specification in addr.
735 # "addr" must not include an address-family keyword.
736 # The results will include an address-family keyword.
738 ifalias_expand_addr()
746 afexists $_af || return
747 ifalias_expand_addr_$_af $_action $*
750 # ifalias_expand_addr_inet action addr
751 # Helper function for ifalias_expand_addr(). Handles IPv4.
753 ifalias_expand_addr_inet()
755 local _action _arg _cidr _cidr_addr _exargs
756 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
764 case $_action:$_arg:$_exargs in
765 *:*--*) return ;; # invalid
766 tmp:*[0-9]-[0-9]*:*) # to be expanded
769 *:*[0-9]-[0-9]*:*) # to be expanded
771 tmp:*:*netmask*) # already expanded w/ netmask option
772 echo ${_arg%/[0-9]*} $_exargs && return
774 tmp:*:*) # already expanded w/o netmask option
775 echo $_arg $_exargs && return
777 *:*:*netmask*) # already expanded w/ netmask option
778 echo inet ${_arg%/[0-9]*} $_exargs && return
780 *:*:*) # already expanded w/o netmask option
781 echo inet $_arg $_exargs && return
785 for _cidr in $_arg; do
788 # When subnet prefix length is not specified, use /32.
790 $_ipaddr) _plen=32 ;; # "/" character not found
794 IFS=. set -- $_ipaddr
804 _iphead="${_iphead}${_iphead:+.}${_c}"
807 _iptail="${_iptail}${_iptail:+.}${_c}"
815 # clear netmask when removing aliases
816 if [ "$_action" = "-alias" ]; then
821 while [ "$_ipcount" -le "$_iphigh" ]; do
822 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
823 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
824 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."
827 _ipcount=$(($_ipcount + 1))
829 # Forcibly set /32 for remaining aliases.
834 for _c in $_retstr; do
835 ifalias_expand_addr_inet $_action $_c $_exargs
839 # ifalias_expand_addr_inet6 action addr
840 # Helper function for ifalias_expand_addr(). Handles IPv6.
842 ifalias_expand_addr_inet6()
844 local _action _arg _cidr _cidr_addr _exargs
845 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
854 case $_action:$_arg:$_exargs in
855 *:*--*:*) return ;; # invalid
856 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
859 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
861 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
862 echo ${_arg%/[0-9]*} $_exargs && return
864 tmp:*:*) # already expanded w/o prefixlen option
865 echo $_arg $_exargs && return
867 *:*:*prefixlen*) # already expanded w/ prefixlen option
868 echo inet6 ${_arg%/[0-9]*} $_exargs && return
870 *:*:*) # already expanded w/o prefixlen option
871 echo inet6 $_arg $_exargs && return
875 for _cidr in $_arg; do
876 _ipaddr="${_cidr%%/*}"
879 case $_action:$_ipaddr:$_cidr in
880 -alias:*:*) unset _plen ;;
881 *:$_cidr:$_ipaddr) unset _plen ;;
884 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
885 # Handle !v4mapped && !v4compat addresses.
887 # The default prefix length is 64.
888 case $_ipaddr:$_cidr in
889 $_cidr:$_ipaddr) _plen="64" ;;
891 _ipleft=${_ipaddr%-*}
892 _ipright=${_ipaddr#*-}
893 _iplow=${_ipleft##*:}
894 _iphigh=${_ipright%%:*}
895 _ipleft=${_ipleft%:*}
896 _ipright=${_ipright#*:}
898 if [ "$_iphigh" = "$_ipright" ]; then
904 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
905 _iplow=$((0x$_iplow))
906 _iphigh=$((0x$_iphigh))
908 while [ $_ipcount -le $_iphigh ]; do
909 _r=`printf "%s:%04x%s%s" \
910 $_ipleft $_ipcount $_ipright \
912 _retstr="$_retstr $_r"
913 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
915 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."
918 _ipcount=$(($_ipcount + 1))
922 _retstr="${_ipaddr}${_plen:+/}${_plen}"
925 for _c in $_retstr; do
926 ifalias_expand_addr_inet6 $_action $_c $_exargs
929 # v4mapped/v4compat should handle as an IPv4 alias
930 _ipv4part=${_ipaddr##*:}
932 # Adjust prefix length if any. If not, set the
933 # default prefix length as 32.
934 case $_ipaddr:$_cidr in
935 $_cidr:$_ipaddr) _plen=32 ;;
936 *) _plen=$(($_plen - 96)) ;;
939 _retstr=`ifalias_expand_addr_inet \
940 tmp ${_ipv4part}${_plen:+/}${_plen}`
941 for _c in $_retstr; do
942 ifalias_expand_addr_inet $_action $_c $_exargs
948 # ifalias_af_common_handler if af action args
949 # Helper function for ifalias_af_common().
951 ifalias_af_common_handler()
953 local _ret _if _af _action _args _c _tmpargs
967 # link(ether) does not support address removal.
968 case $_af:$_action in
969 link:-alias|ether:-alias) return ;;
978 ifalias_af_common_handler $_if $_af $_action \
979 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
982 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
988 _tmpargs="$_tmpargs $_c"
992 # Process the last component if any.
993 if [ -n "$_tmpargs}" ]; then
996 ifalias_af_common_handler $_if $_af $_action \
997 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1000 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1008 # ifalias_af_common if af action
1009 # Helper function for ifalias().
1013 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1014 local _vif _punct=".-/+"
1022 # Normalize $_if before using it in a pattern to list_vars()
1023 ltr "$_if" "$_punct" "_" _vif
1025 # ifconfig_IF_aliasN which starts with $_af
1026 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1027 sort_lite -nk1.$((9+${#_vif}+7))`
1029 eval ifconfig_args=\"\$$alias\"
1031 case $ifconfig_args in
1032 inet\ *) _iaf=inet ;;
1033 inet6\ *) _iaf=inet6 ;;
1035 link\ *) _iaf=link ;;
1036 ether\ *) _iaf=ether ;;
1039 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1041 _aliasn="$_aliasn $ifconfig_args"
1047 _aliasn="$_aliasn inet $ifconfig_args"
1048 warn "\$${alias} needs leading" \
1049 "\"inet\" keyword for an IPv4 address."
1053 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1056 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1057 sort_lite -nk1.$((14+${#_vif}+7))`
1059 eval ifconfig_args=\"\$$alias\"
1060 case ${_action}:"${ifconfig_args}" in
1065 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1066 warn "\$${alias} is obsolete. " \
1067 "Use ifconfig_${_vif}_aliasN instead."
1073 # backward compatibility: ipv4_addrs_IF.
1074 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1075 _aliasn="$_aliasn inet $_tmpargs"
1078 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1080 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1082 inet|inet6|ipx|link|ether)
1085 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1091 _tmpargs="$_tmpargs $_c"
1094 # Process the last component
1097 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1104 # ipv6_prefix_hostid_addr_common if action
1105 # Add or remove IPv6 prefix + hostid addr on the interface $if
1107 ipv6_prefix_hostid_addr_common()
1109 local _if _action prefix j
1112 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1114 if [ -n "${prefix}" ]; then
1115 for j in ${prefix}; do
1116 # The default prefixlen is 64.
1119 $plen:$j) plen=64 ;;
1123 # Normalize the last part by removing ":"
1126 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1127 prefixlen $plen eui64 ${_action}
1129 # if I am a router, add subnet router
1130 # anycast address (RFC 2373).
1131 if checkyesno ipv6_gateway_enable; then
1132 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1133 prefixlen $plen ${_action} anycast
1139 # ipv6_accept_rtadv_up if
1140 # Enable accepting Router Advertisement and send Router
1141 # Solicitation message
1142 ipv6_accept_rtadv_up()
1144 if ipv6_autoconfif $1; then
1145 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1146 if ! checkyesno rtsold_enable; then
1147 rtsol ${rtsol_flags} $1
1152 # ipv6_accept_rtadv_down if
1153 # Disable accepting Router Advertisement
1154 ipv6_accept_rtadv_down()
1156 if ipv6_autoconfif $1; then
1157 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1162 # Evaluate a startup script for the $if interface.
1163 # It returns 0 if a script was found and processed or
1164 # 1 if no script was found.
1168 if [ -r /etc/start_if.$1 ]; then
1177 # Evaluate a shutdown script for the $if interface.
1178 # It returns 0 if a script was found and processed or
1179 # 1 if no script was found.
1183 if [ -r /etc/stop_if.$1 ]; then
1192 # Create cloneable interfaces.
1196 local _prefix _list ifn
1201 for ifn in ${cloned_interfaces}; do
1202 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1203 if [ $? -eq 0 ]; then
1204 _list="${_list}${_prefix}${ifn}"
1205 [ -z "$_prefix" ] && _prefix=' '
1208 debug "Cloned: ${_list}"
1212 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1217 local _prefix _list ifn
1221 for ifn in ${cloned_interfaces}; do
1222 ${IFCONFIG_CMD} -n ${ifn} destroy
1223 if [ $? -eq 0 ]; then
1224 _list="${_list}${_prefix}${ifn}"
1225 [ -z "$_prefix" ] && _prefix=' '
1228 debug "Destroyed clones: ${_list}"
1232 # Create and configure child interfaces. Return 0 if child
1233 # interfaces are created.
1237 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1241 # Create wireless interfaces
1242 child_wlans=`get_if_var $ifn wlans_IF`
1244 for child in ${child_wlans}; do
1245 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1246 debug_flags="`get_if_var $child wlandebug_IF`"
1248 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1249 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1250 if [ -n "${debug_flags}" ]; then
1251 wlandebug -i $child ${debug_flags}
1254 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1255 if [ -n "${debug_flags}" ]; then
1256 wlandebug -i $i ${debug_flags}
1258 ${IFCONFIG_CMD} $i name $child && cfg=0
1260 if autoif $child; then
1265 # Create vlan interfaces
1266 child_vlans=`get_if_var $ifn vlans_IF`
1268 if [ -n "${child_vlans}" ]; then
1272 for child in ${child_vlans}; do
1273 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1274 child="${ifn}.${child}"
1275 create_args=`get_if_var $child create_args_IF`
1276 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1278 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1279 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1280 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1282 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1283 ${IFCONFIG_CMD} $i name $child && cfg=0
1286 if autoif $child; then
1295 # Destroy child interfaces.
1299 local cfg child child_vlans child_wlans ifn
1302 child_wlans=`get_if_var $ifn wlans_IF`
1303 for child in ${child_wlans}; do
1304 if ! ifexists $child; then
1307 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1310 child_vlans=`get_if_var $ifn vlans_IF`
1311 for child in ${child_vlans}; do
1312 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1313 child="${ifn}.${child}"
1315 if ! ifexists $child; then
1318 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1325 # Create netgraph nodes.
1329 ngctl -f - 2> /dev/null <<EOF
1336 # Create netgraph nodes.
1342 ng_mkpeer $* | while read line; do
1343 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1344 if [ -n "${t}" ]; then
1352 # Create gif(4) tunnel interfaces.
1357 for i in ${gif_interfaces}; do
1358 peers=`get_if_var $i gifconfig_IF`
1364 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1365 ${IFCONFIG_CMD} $i create >/dev/null 2>&1
1367 gif=`${IFCONFIG_CMD} gif create`
1368 ${IFCONFIG_CMD} $gif name $i
1370 ${IFCONFIG_CMD} $i tunnel ${peers}
1371 ${IFCONFIG_CMD} $i up
1378 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1379 # FEC arguments were found and configured; returns !0 otherwise.
1382 local req_iface iface bogus
1385 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1389 iface=`ng_create_one fec dummy fec`
1390 if [ -z "${iface}" ]; then
1393 if [ "${iface}" = "${req_iface}" ]; then
1396 bogus="${bogus} ${iface}"
1399 for iface in ${bogus}; do
1400 ngctl shutdown ${iface}:
1405 # Create Fast EtherChannel interfaces.
1410 for i in ${fec_interfaces}; do
1412 for j in `get_if_var $i fecconfig_IF`; do
1418 ngctl msg ${i}: add_iface "\"${j}\""
1426 # Configure any IPX addresses for interface $ifn. Returns 0 if
1427 # IPX arguments were found and configured; returns 1 otherwise.
1435 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1436 if [ -n "${ifconfig_args}" ]; then
1437 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1445 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1446 # addresses were found and unconfigured. It returns 1, otherwise.
1450 local _if _ifs _ret ipxList oldifs _ipx
1454 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1458 for _ipx in $ipxList ; do
1459 # get rid of extraneous line
1460 [ -z "$_ipx" ] && break
1462 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1465 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1475 # Rename all requested interfaces.
1482 for _if in `${IFCONFIG_CMD} -l`; do
1483 _ifname=`get_if_var $_if ifconfig_IF_name`
1484 if [ ! -z "$_ifname" ]; then
1485 ${IFCONFIG_CMD} $_if name $_ifname
1492 # list_net_interfaces type
1493 # List all network interfaces. The type of interface returned
1494 # can be controlled by the type argument. The type
1495 # argument can be any of the following:
1496 # nodhcp - all interfaces, excluding DHCP configured interfaces
1497 # dhcp - list only DHCP configured interfaces
1498 # noautoconf - all interfaces, excluding IPv6 Stateless
1499 # Address Autoconf configured interfaces
1500 # autoconf - list only IPv6 Stateless Address Autoconf
1501 # configured interfaces
1502 # If no argument is specified all network interfaces are output.
1503 # Note that the list will include cloned interfaces if applicable.
1504 # Cloned interfaces must already exist to have a chance to appear
1505 # in the list if ${network_interfaces} is set to `auto'.
1507 list_net_interfaces()
1509 local type _tmplist _list _autolist _lo _if
1512 # Get a list of ALL the interfaces and make lo0 first if it's there.
1515 case ${network_interfaces} in
1517 _autolist="`${IFCONFIG_CMD} -l`"
1519 for _if in ${_autolist} ; do
1520 if autoif $_if; then
1521 if [ "$_if" = "lo0" ]; then
1524 _tmplist="${_tmplist} ${_if}"
1528 _tmplist="${_lo}${_tmplist# }"
1531 _tmplist="${network_interfaces} ${cloned_interfaces}"
1533 # lo0 is effectively mandatory, so help prevent foot-shooting
1536 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1537 *) _tmplist="lo0 ${_tmplist}" ;;
1545 for _if in ${_tmplist} ; do
1546 if ! dhcpif $_if && \
1547 [ -n "`_ifconfig_getargs $_if`" ]; then
1548 _list="${_list# } ${_if}"
1553 for _if in ${_tmplist} ; do
1554 if dhcpif $_if; then
1555 _list="${_list# } ${_if}"
1560 for _if in ${_tmplist} ; do
1561 if ! ipv6_autoconfif $_if && \
1562 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1563 _list="${_list# } ${_if}"
1568 for _if in ${_tmplist} ; do
1569 if ipv6_autoconfif $_if; then
1570 _list="${_list# } ${_if}"
1584 # get_default_if -address_family
1585 # Get the interface of the default route for the given address family.
1586 # The -address_family argument must be suitable passing to route(8).
1590 local routeget oldifs defif line
1595 for line in `route -n get $1 default 2>/dev/null`; do
1608 # Echo decimal number $arg (single digit) in hexadecimal format.
1615 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1621 is_wired_interface()
1625 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1626 *media:?Ethernet*) media=Ethernet ;;
1629 test "$media" = "Ethernet"
1632 # network6_getladdr if [flag]
1633 # Echo link-local address from $if if any.
1634 # If flag is defined, tentative ones will be excluded.
1637 local _if _flag proto addr rest
1641 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1642 case "${proto}/${addr}/${_flag}/${rest}" in
1646 inet6/fe80:://*tentative*) # w/o flag
1647 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1648 network6_getladdr $_if $_flags
1650 inet6/fe80::/*/*tentative*) # w/ flag