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 " \
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 for _punct_c in $_punct; do
251 _if=`ltr ${_if} ${_punct_c} '_'`
258 eval echo \${${prefix}${_if}${suffix}-${_default}}
261 # _ifconfig_getargs if [af]
262 # Prints the arguments for the supplied interface to stdout.
263 # Returns 1 if empty. In general, ifconfig_getargs should be used
271 if [ -z "$_ifn" ]; then
275 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
278 # ifconfig_getargs if [af]
279 # Takes the result from _ifconfig_getargs and removes pseudo
280 # args such as DHCP and WPA.
283 local _tmpargs _arg _args
284 _tmpargs=`_ifconfig_getargs $1 $2`
285 if [ $? -eq 1 ]; then
290 for _arg in $_tmpargs; do
293 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
294 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
295 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
297 [Hh][Oo][Ss][Tt][Aa][Pp]) ;;
308 # Returns 0 if the interface should be automatically configured at
309 # boot time and 1 otherwise.
313 _tmpargs=`_ifconfig_getargs $1`
315 for _arg in $_tmpargs; do
317 [Nn][Oo][Aa][Uu][Tt][Oo])
327 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
331 _tmpargs=`_ifconfig_getargs $1`
346 for _arg in $_tmpargs; do
351 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
354 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
364 # Returns 0 if the interface should be configured synchronously and
369 _tmpargs=`_ifconfig_getargs $1`
375 for _arg in $_tmpargs; do
377 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
380 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
386 checkyesno synchronous_dhclient
390 # Returns 0 if the interface is a WPA interface and 1 otherwise.
394 _tmpargs=`_ifconfig_getargs $1`
396 for _arg in $_tmpargs; do
408 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
412 _tmpargs=`_ifconfig_getargs $1`
414 for _arg in $_tmpargs; do
416 [Hh][Oo][Ss][Tt][Aa][Pp])
426 # Returns 0 if the address family is enabled in the kernel
435 check_kern_features ${_af}
438 ${SYSCTL_N} net.ipx > /dev/null 2>&1
441 if [ -x /sbin/atmconfig ]; then
442 /sbin/atmconfig diag list > /dev/null 2>&1
451 err 1 "afexists(): Unsupported address family: $_af"
457 # Returns 0 if the interface has no af configuration and 1 otherwise.
484 # Returns 0 if the interface should be configured for IPv6 and
491 if ! afexists inet6; then
495 # lo0 is always IPv6-enabled
502 case "${ipv6_network_interfaces}" in
503 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
504 # True if $ifconfig_IF_ipv6 is defined.
505 _tmpargs=`_ifconfig_getargs $_if ipv6`
506 if [ -n "${_tmpargs}" ]; then
510 # True if $ipv6_prefix_IF is defined.
511 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
512 if [ -n "${_tmpargs}" ]; then
516 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
517 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
518 if [ -n "${_tmpargs}" ]; then
528 # Returns 0 if the interface should be configured for IPv6 with
529 # Stateless Address Configuration; 1 otherwise.
532 local _if _tmpargs _arg
547 if ! ipv6if $_if; then
550 if checkyesno ipv6_gateway_enable; then
553 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
554 if [ -n "${_tmpargs}" ]; then
557 # backward compatibility: $ipv6_enable
559 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
560 if checkyesno ipv6_gateway_enable; then
565 # No accept_rtadv by default on if_bridge(4)
566 # to avoid a conflict with the member
577 _tmpargs=`_ifconfig_getargs $_if ipv6`
578 for _arg in $_tmpargs; do
586 # backward compatibility: $ipv6_ifconfig_IF
587 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
588 for _arg in $_tmpargs; do
600 # Returns 0 if the interface exists and 1 otherwise.
603 [ -z "$1" ] && return 1
604 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
608 # add IPv4 addresses to the interface $if
615 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
616 if [ "${_if}" = "lo0" ]; then
617 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
618 if [ -z "${ifconfig_args}" ]; then
619 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
622 ifalias ${_if} inet alias && _ret=0
628 # add IPv6 addresses to the interface $if
635 if ! ipv6if $_if; then
639 ifalias ${_if} inet6 alias && _ret=0
640 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
641 ipv6_accept_rtadv_up ${_if} && _ret=0
647 # remove IPv4 addresses from the interface $if
650 local _if _ifs _ret inetList oldifs _inet
655 ifalias ${_if} inet -alias && _ret=0
657 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
661 for _inet in $inetList ; do
662 # get rid of extraneous line
668 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
671 ${IFCONFIG_CMD} ${_if} ${_inet} delete
681 # remove IPv6 addresses from the interface $if
684 local _if _ifs _ret inetList oldifs _inet6
689 if ! ipv6if $_if; then
693 ipv6_accept_rtadv_down ${_if} && _ret=0
694 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
695 ifalias ${_if} inet6 -alias && _ret=0
697 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
701 for _inet6 in $inetList ; do
702 # get rid of extraneous line
708 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
711 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
720 # ifalias if af action
721 # Configure or remove aliases for network interface $if.
722 # It returns 0 if at least one alias was configured or
723 # removed, or 1 if there were none.
730 afexists $2 || return $_ret
733 inet|inet6|link|ether)
734 ifalias_af_common $1 $2 $3 && _ret=0
741 # ifalias_expand_addr af action addr
742 # Expand address range ("N-M") specification in addr.
743 # "addr" must not include an address-family keyword.
744 # The results will include an address-family keyword.
746 ifalias_expand_addr()
754 afexists $_af || return
755 ifalias_expand_addr_$_af $_action $*
758 # ifalias_expand_addr_inet action addr
759 # Helper function for ifalias_expand_addr(). Handles IPv4.
761 ifalias_expand_addr_inet()
763 local _action _arg _cidr _cidr_addr _exargs
764 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
772 case $_action:$_arg:$_exargs in
773 *:*--*) return ;; # invalid
774 tmp:*[0-9]-[0-9]*:*) # to be expanded
777 *:*[0-9]-[0-9]*:*) # to be expanded
779 tmp:*:*netmask*) # already expanded w/ netmask option
780 echo ${_arg%/[0-9]*} $_exargs && return
782 tmp:*:*) # already expanded w/o netmask option
783 echo $_arg $_exargs && return
785 *:*:*netmask*) # already expanded w/ netmask option
786 echo inet ${_arg%/[0-9]*} $_exargs && return
788 *:*:*) # already expanded w/o netmask option
789 echo inet $_arg $_exargs && return
793 for _cidr in $_arg; do
796 # When subnet prefix length is not specified, use /32.
798 $_ipaddr) _plen=32 ;; # "/" character not found
802 IFS=. set -- $_ipaddr
812 _iphead="${_iphead}${_iphead:+.}${_c}"
815 _iptail="${_iptail}${_iptail:+.}${_c}"
823 # clear netmask when removing aliases
824 if [ "$_action" = "-alias" ]; then
829 while [ "$_ipcount" -le "$_iphigh" ]; do
830 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
831 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
832 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."
835 _ipcount=$(($_ipcount + 1))
837 # Forcibly set /32 for remaining aliases.
842 for _c in $_retstr; do
843 ifalias_expand_addr_inet $_action $_c $_exargs
847 # ifalias_expand_addr_inet6 action addr
848 # Helper function for ifalias_expand_addr(). Handles IPv6.
850 ifalias_expand_addr_inet6()
852 local _action _arg _cidr _cidr_addr _exargs
853 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
862 case $_action:$_arg:$_exargs in
863 *:*--*:*) return ;; # invalid
864 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
867 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
869 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
870 echo ${_arg%/[0-9]*} $_exargs && return
872 tmp:*:*) # already expanded w/o prefixlen option
873 echo $_arg $_exargs && return
875 *:*:*prefixlen*) # already expanded w/ prefixlen option
876 echo inet6 ${_arg%/[0-9]*} $_exargs && return
878 *:*:*) # already expanded w/o prefixlen option
879 echo inet6 $_arg $_exargs && return
883 for _cidr in $_arg; do
884 _ipaddr="${_cidr%%/*}"
887 case $_action:$_ipaddr:$_cidr in
888 -alias:*:*) unset _plen ;;
889 *:$_cidr:$_ipaddr) unset _plen ;;
892 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
893 # Handle !v4mapped && !v4compat addresses.
895 # The default prefix length is 64.
896 case $_ipaddr:$_cidr in
897 $_cidr:$_ipaddr) _plen="64" ;;
899 _ipleft=${_ipaddr%-*}
900 _ipright=${_ipaddr#*-}
901 _iplow=${_ipleft##*:}
902 _iphigh=${_ipright%%:*}
903 _ipleft=${_ipleft%:*}
904 _ipright=${_ipright#*:}
906 if [ "$_iphigh" = "$_ipright" ]; then
912 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
913 _iplow=$((0x$_iplow))
914 _iphigh=$((0x$_iphigh))
916 while [ $_ipcount -le $_iphigh ]; do
917 _r=`printf "%s:%04x%s%s" \
918 $_ipleft $_ipcount $_ipright \
920 _retstr="$_retstr $_r"
921 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
923 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."
926 _ipcount=$(($_ipcount + 1))
930 _retstr="${_ipaddr}${_plen:+/}${_plen}"
933 for _c in $_retstr; do
934 ifalias_expand_addr_inet6 $_action $_c $_exargs
937 # v4mapped/v4compat should handle as an IPv4 alias
938 _ipv4part=${_ipaddr##*:}
940 # Adjust prefix length if any. If not, set the
941 # default prefix length as 32.
942 case $_ipaddr:$_cidr in
943 $_cidr:$_ipaddr) _plen=32 ;;
944 *) _plen=$(($_plen - 96)) ;;
947 _retstr=`ifalias_expand_addr_inet \
948 tmp ${_ipv4part}${_plen:+/}${_plen}`
949 for _c in $_retstr; do
950 ifalias_expand_addr_inet $_action $_c $_exargs
956 # ifalias_af_common_handler if af action args
957 # Helper function for ifalias_af_common().
959 ifalias_af_common_handler()
961 local _ret _if _af _action _args _c _tmpargs
975 # link(ether) does not support address removal.
976 case $_af:$_action in
977 link:-alias|ether:-alias) return ;;
986 ifalias_af_common_handler $_if $_af $_action \
987 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
990 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
996 _tmpargs="$_tmpargs $_c"
1000 # Process the last component if any.
1001 if [ -n "$_tmpargs}" ]; then
1004 ifalias_af_common_handler $_if $_af $_action \
1005 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1008 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1016 # ifalias_af_common if af action
1017 # Helper function for ifalias().
1021 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1029 # ifconfig_IF_aliasN which starts with $_af
1032 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1034 case $ifconfig_args in
1035 inet\ *) _iaf=inet ;;
1036 inet6\ *) _iaf=inet6 ;;
1038 link\ *) _iaf=link ;;
1039 ether\ *) _iaf=ether ;;
1042 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1044 _aliasn="$_aliasn $ifconfig_args"
1050 _aliasn="$_aliasn inet $ifconfig_args"
1051 warn "\$ifconfig_${_if}_alias${alias} needs " \
1052 "\"inet\" keyword for an IPv4 address."
1054 alias=$(($alias + 1))
1057 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1062 ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1063 case ${_action}:"${ifconfig_args}" in
1068 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1069 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1070 "is obsolete. Use ifconfig_$1_aliasN " \
1074 alias=$(($alias + 1))
1078 # backward compatibility: ipv4_addrs_IF.
1079 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1080 _aliasn="$_aliasn inet $_tmpargs"
1083 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1085 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1087 inet|inet6|ipx|link|ether)
1090 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1096 _tmpargs="$_tmpargs $_c"
1099 # Process the last component
1102 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1109 # ipv6_prefix_hostid_addr_common if action
1110 # Add or remove IPv6 prefix + hostid addr on the interface $if
1112 ipv6_prefix_hostid_addr_common()
1114 local _if _action prefix j
1117 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1119 if [ -n "${prefix}" ]; then
1120 for j in ${prefix}; do
1121 # The default prefixlen is 64.
1124 $plen:$j) plen=64 ;;
1128 # Normalize the last part by removing ":"
1131 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1132 prefixlen $plen eui64 ${_action}
1134 # if I am a router, add subnet router
1135 # anycast address (RFC 2373).
1136 if checkyesno ipv6_gateway_enable; then
1137 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1138 prefixlen $plen ${_action} anycast
1144 # ipv6_accept_rtadv_up if
1145 # Enable accepting Router Advertisement and send Router
1146 # Solicitation message
1147 ipv6_accept_rtadv_up()
1149 if ipv6_autoconfif $1; then
1150 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1151 if ! checkyesno rtsold_enable; then
1152 rtsol ${rtsol_flags} $1
1157 # ipv6_accept_rtadv_down if
1158 # Disable accepting Router Advertisement
1159 ipv6_accept_rtadv_down()
1161 if ipv6_autoconfif $1; then
1162 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1167 # Evaluate a startup script for the $if interface.
1168 # It returns 0 if a script was found and processed or
1169 # 1 if no script was found.
1173 if [ -r /etc/start_if.$1 ]; then
1182 # Evaluate a shutdown script for the $if interface.
1183 # It returns 0 if a script was found and processed or
1184 # 1 if no script was found.
1188 if [ -r /etc/stop_if.$1 ]; then
1197 # Create cloneable interfaces.
1201 local _prefix _list ifn ifopt _iflist _n tmpargs
1207 for ifn in ${cloned_interfaces}; do
1209 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1211 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1214 # Skip if ifn already exists.
1215 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1218 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1219 if [ $? -eq 0 ]; then
1220 _list="${_list}${_prefix}${ifn}"
1221 [ -z "$_prefix" ] && _prefix=' '
1224 if [ -n "$gif_interfaces" ]; then
1225 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1227 for ifn in ${gif_interfaces}; do
1229 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1231 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1234 # Skip if ifn already exists.
1235 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1240 ${IFCONFIG_CMD} $ifn create
1243 _n=$(${IFCONFIG_CMD} gif create)
1244 ${IFCONFIG_CMD} $_n name $ifn
1247 if [ $? -eq 0 ]; then
1248 _list="${_list}${_prefix}${ifn}"
1249 [ -z "$_prefix" ] && _prefix=' '
1251 tmpargs=$(get_if_var $ifn gifconfig_IF)
1252 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1254 if [ -n "${_list}" ]; then
1255 echo "Created clone interfaces: ${_list}."
1257 debug "Cloned: ${_list}"
1261 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1266 local _prefix _list ifn ifopt _iflist _sticky
1271 : ${cloned_interfaces_sticky:=NO}
1272 if checkyesno cloned_interfaces_sticky; then
1277 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1279 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1280 case $ifopt:$_sticky in
1281 sticky:*) continue ;; # :sticky => not destroy
1282 nosticky:*) ;; # :nosticky => destroy
1283 *:1) continue ;; # global sticky knob == 1
1286 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1289 # Skip if ifn does not exist.
1290 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1293 ${IFCONFIG_CMD} -n ${ifn} destroy
1294 if [ $? -eq 0 ]; then
1295 _list="${_list}${_prefix}${ifn}"
1296 [ -z "$_prefix" ] && _prefix=' '
1299 if [ -n "${_list}" ]; then
1300 echo "Destroyed clone interfaces: ${_list}."
1302 debug "Destroyed clones: ${_list}"
1306 # Create and configure child interfaces. Return 0 if child
1307 # interfaces are created.
1311 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1315 # Create wireless interfaces
1316 child_wlans=`get_if_var $ifn wlans_IF`
1318 for child in ${child_wlans}; do
1319 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1320 debug_flags="`get_if_var $child wlandebug_IF`"
1322 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1323 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1324 if [ -n "${debug_flags}" ]; then
1325 wlandebug -i $child ${debug_flags}
1328 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1329 if [ -n "${debug_flags}" ]; then
1330 wlandebug -i $i ${debug_flags}
1332 ${IFCONFIG_CMD} $i name $child && cfg=0
1334 if autoif $child; then
1339 # Create vlan interfaces
1340 child_vlans=`get_if_var $ifn vlans_IF`
1342 if [ -n "${child_vlans}" ]; then
1346 for child in ${child_vlans}; do
1347 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1348 child="${ifn}.${child}"
1349 create_args=`get_if_var $child create_args_IF`
1350 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1352 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1353 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1354 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1356 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1357 ${IFCONFIG_CMD} $i name $child && cfg=0
1360 if autoif $child; then
1369 # Destroy child interfaces.
1373 local cfg child child_vlans child_wlans ifn
1376 child_wlans=`get_if_var $ifn wlans_IF`
1377 for child in ${child_wlans}; do
1378 if ! ifexists $child; then
1381 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1384 child_vlans=`get_if_var $ifn vlans_IF`
1385 for child in ${child_vlans}; do
1386 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1387 child="${ifn}.${child}"
1389 if ! ifexists $child; then
1392 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1399 # Create netgraph nodes.
1403 ngctl -f - 2> /dev/null <<EOF
1410 # Create netgraph nodes.
1416 ng_mkpeer $* | while read line; do
1417 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1418 if [ -n "${t}" ]; then
1426 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1427 # FEC arguments were found and configured; returns !0 otherwise.
1430 local req_iface iface bogus
1433 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1437 iface=`ng_create_one fec dummy fec`
1438 if [ -z "${iface}" ]; then
1441 if [ "${iface}" = "${req_iface}" ]; then
1444 bogus="${bogus} ${iface}"
1447 for iface in ${bogus}; do
1448 ngctl shutdown ${iface}:
1453 # Create Fast EtherChannel interfaces.
1458 for i in ${fec_interfaces}; do
1460 for j in `get_if_var $i fecconfig_IF`; do
1466 ngctl msg ${i}: add_iface "\"${j}\""
1474 # Configure any IPX addresses for interface $ifn. Returns 0 if
1475 # IPX arguments were found and configured; returns 1 otherwise.
1483 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1484 if [ -n "${ifconfig_args}" ]; then
1485 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1493 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1494 # addresses were found and unconfigured. It returns 1, otherwise.
1498 local _if _ifs _ret ipxList oldifs _ipx
1502 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1506 for _ipx in $ipxList ; do
1507 # get rid of extraneous line
1508 [ -z "$_ipx" ] && break
1510 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1513 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1522 # ifnet_rename [ifname]
1523 # Rename interfaces if ifconfig_IF_name is defined.
1530 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1531 _ifname=`get_if_var $_if ifconfig_IF_name`
1532 if [ ! -z "$_ifname" ]; then
1533 ${IFCONFIG_CMD} $_if name $_ifname
1540 # list_net_interfaces type
1541 # List all network interfaces. The type of interface returned
1542 # can be controlled by the type argument. The type
1543 # argument can be any of the following:
1544 # nodhcp - all interfaces, excluding DHCP configured interfaces
1545 # dhcp - list only DHCP configured interfaces
1546 # noautoconf - all interfaces, excluding IPv6 Stateless
1547 # Address Autoconf configured interfaces
1548 # autoconf - list only IPv6 Stateless Address Autoconf
1549 # configured interfaces
1550 # If no argument is specified all network interfaces are output.
1551 # Note that the list will include cloned interfaces if applicable.
1552 # Cloned interfaces must already exist to have a chance to appear
1553 # in the list if ${network_interfaces} is set to `auto'.
1555 list_net_interfaces()
1557 local type _tmplist _list _autolist _lo _if
1560 # Get a list of ALL the interfaces and make lo0 first if it's there.
1563 case ${network_interfaces} in
1565 _autolist="`${IFCONFIG_CMD} -l`"
1567 for _if in ${_autolist} ; do
1568 if autoif $_if; then
1569 if [ "$_if" = "lo0" ]; then
1572 _tmplist="${_tmplist} ${_if}"
1576 _tmplist="${_lo}${_tmplist# }"
1579 _tmplist="${network_interfaces} ${cloned_interfaces}"
1581 # lo0 is effectively mandatory, so help prevent foot-shooting
1584 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1585 *) _tmplist="lo0 ${_tmplist}" ;;
1593 for _if in ${_tmplist} ; do
1594 if ! dhcpif $_if && \
1595 [ -n "`_ifconfig_getargs $_if`" ]; then
1596 _list="${_list# } ${_if}"
1601 for _if in ${_tmplist} ; do
1602 if dhcpif $_if; then
1603 _list="${_list# } ${_if}"
1608 for _if in ${_tmplist} ; do
1609 if ! ipv6_autoconfif $_if && \
1610 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1611 _list="${_list# } ${_if}"
1616 for _if in ${_tmplist} ; do
1617 if ipv6_autoconfif $_if; then
1618 _list="${_list# } ${_if}"
1632 # get_default_if -address_family
1633 # Get the interface of the default route for the given address family.
1634 # The -address_family argument must be suitable passing to route(8).
1638 local routeget oldifs defif line
1643 for line in `route -n get $1 default 2>/dev/null`; do
1656 # Echo decimal number $arg (single digit) in hexadecimal format.
1663 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1669 is_wired_interface()
1673 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1674 *media:?Ethernet*) media=Ethernet ;;
1677 test "$media" = "Ethernet"
1680 # network6_getladdr if [flag]
1681 # Echo link-local address from $if if any.
1682 # If flag is defined, tentative ones will be excluded.
1685 local _if _flag proto addr rest
1689 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1690 case "${proto}/${addr}/${_flag}/${rest}" in
1694 inet6/fe80:://*tentative*) # w/o flag
1695 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1696 network6_getladdr $_if $_flags
1698 inet6/fe80::/*/*tentative*) # w/ flag