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 afexists inet && ipv4_up ${ifn} && cfg=0
50 afexists inet6 && ipv6_up ${ifn} && cfg=0
51 afexists ipx && ipx_up ${ifn} && cfg=0
52 childif_create ${ifn} && cfg=0
58 # Shutdown and de-configure an interface. If action is taken,
59 # print the interface name.
67 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
69 afexists ipx && ipx_down ${ifn} && cfg=0
70 afexists inet6 && ipv6_down ${ifn} && cfg=0
71 afexists inet && ipv4_down ${ifn} && cfg=0
72 ifconfig_down ${ifn} && cfg=0
73 ifscript_down ${ifn} && cfg=0
74 childif_destroy ${ifn} && cfg=0
80 # Evaluate ifconfig(8) arguments for interface $if and
81 # run ifconfig(8) with those arguments. It returns 0 if
82 # arguments were found and executed or 1 if the interface
83 # had no arguments. Pseudo arguments DHCP and WPA are handled
88 local _cfg _ipv6_opts ifconfig_args
91 # Make sure lo0 always comes up.
92 if [ "$1" = "lo0" ]; then
97 ifconfig_args=`ifconfig_getargs $1`
98 if [ -n "${ifconfig_args}" ]; then
99 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
104 if afexists inet6; then
105 if checkyesno ipv6_activate_all_interfaces; then
106 _ipv6_opts="-ifdisabled"
107 elif [ "$1" != "lo0" ]; then
108 _ipv6_opts="ifdisabled"
111 # backward compatibility: $ipv6_enable
113 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
116 # No accept_rtadv by default on if_bridge(4)
117 # to avoid a conflict with the member
121 if ! checkyesno ipv6_gateway_enable; then
122 _ipv6_opts="${_ipv6_opts} accept_rtadv"
129 case $ipv6_cpe_wanif in
131 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
135 if [ -n "${_ipv6_opts}" ]; then
136 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
140 ifconfig_args=`ifconfig_getargs $1 ipv6`
141 if [ -n "${ifconfig_args}" ]; then
142 # backward compatibility: inet6 keyword
143 case "${ifconfig_args}" in
145 warn "\$ifconfig_$1_ipv6 needs leading" \
146 "\"inet6\" keyword for an IPv6 address."
147 ifconfig_args="inet6 ${ifconfig_args}"
150 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
151 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
155 # $ipv6_prefix_IF will be handled in
156 # ipv6_prefix_hostid_addr_common().
157 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
158 if [ -n "${ifconfig_args}" ]; then
159 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
163 # backward compatibility: $ipv6_ifconfig_IF
164 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
165 if [ -n "${ifconfig_args}" ]; then
166 warn "\$ipv6_ifconfig_$1 is obsolete." \
167 " Use ifconfig_$1_ipv6 instead."
168 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
169 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
174 ifalias $1 link alias
175 ifalias $1 ether alias
177 if [ ${_cfg} -eq 0 ]; then
178 ${IFCONFIG_CMD} $1 up
182 /etc/rc.d/wpa_supplicant start $1
183 _cfg=0 # XXX: not sure this should count
184 elif hostapif $1; then
185 /etc/rc.d/hostapd start $1
190 if [ $_cfg -ne 0 ] ; then
191 ${IFCONFIG_CMD} $1 up
193 if syncdhcpif $1; then
194 /etc/rc.d/dhclient start $1
203 # returns 1 if wpa_supplicant or dhclient was stopped or
204 # the interface exists.
212 /etc/rc.d/wpa_supplicant stop $1
214 elif hostapif $1; then
215 /etc/rc.d/hostapd stop $1
220 /etc/rc.d/dhclient stop $1
225 ${IFCONFIG_CMD} $1 down
232 # get_if_var if var [default]
233 # Return the value of the pseudo-hash corresponding to $if where
234 # $var is a string containg the sub-string "IF" which will be
235 # replaced with $if after the characters defined in _punct are
236 # replaced with '_'. If the variable is unset, replace it with
240 local _if _punct _punct_c _var _default prefix suffix
242 if [ $# -ne 2 -a $# -ne 3 ]; then
243 err 3 'USAGE: get_if_var name var [default]'
248 ltr ${_if} "${_punct}" '_' _if
254 eval echo \${${prefix}${_if}${suffix}-${_default}}
257 # _ifconfig_getargs if [af]
258 # Prints the arguments for the supplied interface to stdout.
259 # Returns 1 if empty. In general, ifconfig_getargs should be used
267 if [ -z "$_ifn" ]; then
271 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
274 # ifconfig_getargs if [af]
275 # Takes the result from _ifconfig_getargs and removes pseudo
276 # args such as DHCP and WPA.
279 local _tmpargs _arg _args
280 _tmpargs=`_ifconfig_getargs $1 $2`
281 if [ $? -eq 1 ]; then
286 for _arg in $_tmpargs; do
289 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
290 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
291 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
293 [Hh][Oo][Ss][Tt][Aa][Pp]) ;;
304 # Returns 0 if the interface should be automatically configured at
305 # boot time and 1 otherwise.
309 _tmpargs=`_ifconfig_getargs $1`
311 for _arg in $_tmpargs; do
313 [Nn][Oo][Aa][Uu][Tt][Oo])
323 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
327 _tmpargs=`_ifconfig_getargs $1`
342 for _arg in $_tmpargs; do
347 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
350 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
360 # Returns 0 if the interface should be configured synchronously and
365 _tmpargs=`_ifconfig_getargs $1`
371 for _arg in $_tmpargs; do
373 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
376 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
382 checkyesno synchronous_dhclient
386 # Returns 0 if the interface is a WPA interface and 1 otherwise.
390 _tmpargs=`_ifconfig_getargs $1`
392 for _arg in $_tmpargs; do
404 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
408 _tmpargs=`_ifconfig_getargs $1`
410 for _arg in $_tmpargs; do
412 [Hh][Oo][Ss][Tt][Aa][Pp])
422 # Returns 0 if the address family is enabled in the kernel
431 check_kern_features ${_af}
434 ${SYSCTL_N} net.ipx > /dev/null 2>&1
437 if [ -x /sbin/atmconfig ]; then
438 /sbin/atmconfig diag list > /dev/null 2>&1
447 err 1 "afexists(): Unsupported address family: $_af"
453 # Returns 0 if the interface has no af configuration and 1 otherwise.
480 # Returns 0 if the interface should be configured for IPv6 and
487 if ! afexists inet6; then
491 # lo0 is always IPv6-enabled
498 case "${ipv6_network_interfaces}" in
499 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
500 # True if $ifconfig_IF_ipv6 is defined.
501 _tmpargs=`_ifconfig_getargs $_if ipv6`
502 if [ -n "${_tmpargs}" ]; then
506 # True if $ipv6_prefix_IF is defined.
507 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
508 if [ -n "${_tmpargs}" ]; then
512 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
513 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
514 if [ -n "${_tmpargs}" ]; then
524 # Returns 0 if the interface should be configured for IPv6 with
525 # Stateless Address Configuration; 1 otherwise.
528 local _if _tmpargs _arg
543 if ! ipv6if $_if; then
546 if checkyesno ipv6_gateway_enable; then
549 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
550 if [ -n "${_tmpargs}" ]; then
553 # backward compatibility: $ipv6_enable
555 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
556 if checkyesno ipv6_gateway_enable; then
561 # No accept_rtadv by default on if_bridge(4)
562 # to avoid a conflict with the member
573 _tmpargs=`_ifconfig_getargs $_if ipv6`
574 for _arg in $_tmpargs; do
582 # backward compatibility: $ipv6_ifconfig_IF
583 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
584 for _arg in $_tmpargs; do
596 # Returns 0 if the interface exists and 1 otherwise.
599 [ -z "$1" ] && return 1
600 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
604 # add IPv4 addresses to the interface $if
611 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
612 if [ "${_if}" = "lo0" ]; then
613 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
614 if [ -z "${ifconfig_args}" ]; then
615 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
618 ifalias ${_if} inet alias && _ret=0
624 # add IPv6 addresses to the interface $if
631 if ! ipv6if $_if; then
635 ifalias ${_if} inet6 alias && _ret=0
636 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
637 ipv6_accept_rtadv_up ${_if} && _ret=0
643 # remove IPv4 addresses from the interface $if
646 local _if _ifs _ret inetList oldifs _inet
651 ifalias ${_if} inet -alias && _ret=0
653 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
657 for _inet in $inetList ; do
658 # get rid of extraneous line
659 [ -z "$_inet" ] && break
661 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
664 ${IFCONFIG_CMD} ${_if} ${_inet} delete
674 # remove IPv6 addresses from the interface $if
677 local _if _ifs _ret inetList oldifs _inet6
682 if ! ipv6if $_if; then
686 ipv6_accept_rtadv_down ${_if} && _ret=0
687 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
688 ifalias ${_if} inet6 -alias && _ret=0
690 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
694 for _inet6 in $inetList ; do
695 # get rid of extraneous line
696 [ -z "$_inet6" ] && break
698 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
701 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
710 # ifalias if af action
711 # Configure or remove aliases for network interface $if.
712 # It returns 0 if at least one alias was configured or
713 # removed, or 1 if there were none.
720 afexists $2 || return $_ret
723 inet|inet6|link|ether)
724 ifalias_af_common $1 $2 $3 && _ret=0
731 # ifalias_expand_addr af action addr
732 # Expand address range ("N-M") specification in addr.
733 # "addr" must not include an address-family keyword.
734 # The results will include an address-family keyword.
736 ifalias_expand_addr()
744 afexists $_af || return
745 ifalias_expand_addr_$_af $_action $*
748 # ifalias_expand_addr_inet action addr
749 # Helper function for ifalias_expand_addr(). Handles IPv4.
751 ifalias_expand_addr_inet()
753 local _action _arg _cidr _cidr_addr _exargs
754 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
762 case $_action:$_arg:$_exargs in
763 *:*--*) return ;; # invalid
764 tmp:*[0-9]-[0-9]*:*) # to be expanded
767 *:*[0-9]-[0-9]*:*) # to be expanded
769 tmp:*:*netmask*) # already expanded w/ netmask option
770 echo ${_arg%/[0-9]*} $_exargs && return
772 tmp:*:*) # already expanded w/o netmask option
773 echo $_arg $_exargs && return
775 *:*:*netmask*) # already expanded w/ netmask option
776 echo inet ${_arg%/[0-9]*} $_exargs && return
778 *:*:*) # already expanded w/o netmask option
779 echo inet $_arg $_exargs && return
783 for _cidr in $_arg; do
786 # When subnet prefix length is not specified, use /32.
788 $_ipaddr) _plen=32 ;; # "/" character not found
792 IFS=. set -- $_ipaddr
802 _iphead="${_iphead}${_iphead:+.}${_c}"
805 _iptail="${_iptail}${_iptail:+.}${_c}"
813 # clear netmask when removing aliases
814 if [ "$_action" = "-alias" ]; then
819 while [ "$_ipcount" -le "$_iphigh" ]; do
820 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
821 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
822 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."
825 _ipcount=$(($_ipcount + 1))
827 # Forcibly set /32 for remaining aliases.
832 for _c in $_retstr; do
833 ifalias_expand_addr_inet $_action $_c $_exargs
837 # ifalias_expand_addr_inet6 action addr
838 # Helper function for ifalias_expand_addr(). Handles IPv6.
840 ifalias_expand_addr_inet6()
842 local _action _arg _cidr _cidr_addr _exargs
843 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
852 case $_action:$_arg:$_exargs in
853 *:*--*:*) return ;; # invalid
854 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
857 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
859 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
860 echo ${_arg%/[0-9]*} $_exargs && return
862 tmp:*:*) # already expanded w/o prefixlen option
863 echo $_arg $_exargs && return
865 *:*:*prefixlen*) # already expanded w/ prefixlen option
866 echo inet6 ${_arg%/[0-9]*} $_exargs && return
868 *:*:*) # already expanded w/o prefixlen option
869 echo inet6 $_arg $_exargs && return
873 for _cidr in $_arg; do
874 _ipaddr="${_cidr%%/*}"
877 case $_action:$_ipaddr:$_cidr in
878 -alias:*:*) unset _plen ;;
879 *:$_cidr:$_ipaddr) unset _plen ;;
882 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
883 # Handle !v4mapped && !v4compat addresses.
885 # The default prefix length is 64.
886 case $_ipaddr:$_cidr in
887 $_cidr:$_ipaddr) _plen="64" ;;
889 _ipleft=${_ipaddr%-*}
890 _ipright=${_ipaddr#*-}
891 _iplow=${_ipleft##*:}
892 _iphigh=${_ipright%%:*}
893 _ipleft=${_ipleft%:*}
894 _ipright=${_ipright#*:}
896 if [ "$_iphigh" = "$_ipright" ]; then
902 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
903 _iplow=$((0x$_iplow))
904 _iphigh=$((0x$_iphigh))
906 while [ $_ipcount -le $_iphigh ]; do
907 _r=`printf "%s:%04x%s%s" \
908 $_ipleft $_ipcount $_ipright \
910 _retstr="$_retstr $_r"
911 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
913 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."
916 _ipcount=$(($_ipcount + 1))
920 _retstr="${_ipaddr}${_plen:+/}${_plen}"
923 for _c in $_retstr; do
924 ifalias_expand_addr_inet6 $_action $_c $_exargs
927 # v4mapped/v4compat should handle as an IPv4 alias
928 _ipv4part=${_ipaddr##*:}
930 # Adjust prefix length if any. If not, set the
931 # default prefix length as 32.
932 case $_ipaddr:$_cidr in
933 $_cidr:$_ipaddr) _plen=32 ;;
934 *) _plen=$(($_plen - 96)) ;;
937 _retstr=`ifalias_expand_addr_inet \
938 tmp ${_ipv4part}${_plen:+/}${_plen}`
939 for _c in $_retstr; do
940 ifalias_expand_addr_inet $_action $_c $_exargs
946 # ifalias_af_common_handler if af action args
947 # Helper function for ifalias_af_common().
949 ifalias_af_common_handler()
951 local _ret _if _af _action _args _c _tmpargs
965 # link(ether) does not support address removal.
966 case $_af:$_action in
967 link:-alias|ether:-alias) return ;;
976 ifalias_af_common_handler $_if $_af $_action \
977 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
980 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
986 _tmpargs="$_tmpargs $_c"
990 # Process the last component if any.
991 if [ -n "$_tmpargs}" ]; then
994 ifalias_af_common_handler $_if $_af $_action \
995 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
998 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1006 # ifalias_af_common if af action
1007 # Helper function for ifalias().
1011 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1012 local _vif _punct=".-/+"
1020 # Normalize $_if before using it in a pattern to list_vars()
1021 ltr "$_if" "$_punct" "_" _vif
1023 # ifconfig_IF_aliasN which starts with $_af
1024 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1025 sort_lite -nk1.$((9+${#_vif}+7))`
1027 eval ifconfig_args=\"\$$alias\"
1029 case $ifconfig_args in
1030 inet\ *) _iaf=inet ;;
1031 inet6\ *) _iaf=inet6 ;;
1033 link\ *) _iaf=link ;;
1034 ether\ *) _iaf=ether ;;
1037 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1039 _aliasn="$_aliasn $ifconfig_args"
1045 _aliasn="$_aliasn inet $ifconfig_args"
1046 warn "\$${alias} needs leading" \
1047 "\"inet\" keyword for an IPv4 address."
1051 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1054 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1055 sort_lite -nk1.$((14+${#_vif}+7))`
1057 eval ifconfig_args=\"\$$alias\"
1058 case ${_action}:"${ifconfig_args}" in
1063 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1064 warn "\$${alias} is obsolete. " \
1065 "Use ifconfig_${_vif}_aliasN instead."
1071 # backward compatibility: ipv4_addrs_IF.
1072 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1073 _aliasn="$_aliasn inet $_tmpargs"
1076 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1078 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1080 inet|inet6|ipx|link|ether)
1083 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1089 _tmpargs="$_tmpargs $_c"
1092 # Process the last component
1095 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1102 # ipv6_prefix_hostid_addr_common if action
1103 # Add or remove IPv6 prefix + hostid addr on the interface $if
1105 ipv6_prefix_hostid_addr_common()
1107 local _if _action prefix j
1110 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1112 if [ -n "${prefix}" ]; then
1113 for j in ${prefix}; do
1114 # The default prefixlen is 64.
1117 $plen:$j) plen=64 ;;
1121 # Normalize the last part by removing ":"
1124 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1125 prefixlen $plen eui64 ${_action}
1127 # if I am a router, add subnet router
1128 # anycast address (RFC 2373).
1129 if checkyesno ipv6_gateway_enable; then
1130 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1131 prefixlen $plen ${_action} anycast
1137 # ipv6_accept_rtadv_up if
1138 # Enable accepting Router Advertisement and send Router
1139 # Solicitation message
1140 ipv6_accept_rtadv_up()
1142 if ipv6_autoconfif $1; then
1143 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1144 if ! checkyesno rtsold_enable; then
1145 rtsol ${rtsol_flags} $1
1150 # ipv6_accept_rtadv_down if
1151 # Disable accepting Router Advertisement
1152 ipv6_accept_rtadv_down()
1154 if ipv6_autoconfif $1; then
1155 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1160 # Evaluate a startup script for the $if interface.
1161 # It returns 0 if a script was found and processed or
1162 # 1 if no script was found.
1166 if [ -r /etc/start_if.$1 ]; then
1175 # Evaluate a shutdown script for the $if interface.
1176 # It returns 0 if a script was found and processed or
1177 # 1 if no script was found.
1181 if [ -r /etc/stop_if.$1 ]; then
1190 # Create cloneable interfaces.
1194 local _prefix _list ifn
1199 for ifn in ${cloned_interfaces}; do
1200 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1201 if [ $? -eq 0 ]; then
1202 _list="${_list}${_prefix}${ifn}"
1203 [ -z "$_prefix" ] && _prefix=' '
1206 debug "Cloned: ${_list}"
1210 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1215 local _prefix _list ifn
1219 for ifn in ${cloned_interfaces}; do
1220 ${IFCONFIG_CMD} -n ${ifn} destroy
1221 if [ $? -eq 0 ]; then
1222 _list="${_list}${_prefix}${ifn}"
1223 [ -z "$_prefix" ] && _prefix=' '
1226 debug "Destroyed clones: ${_list}"
1230 # Create and configure child interfaces. Return 0 if child
1231 # interfaces are created.
1235 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1239 # Create wireless interfaces
1240 child_wlans=`get_if_var $ifn wlans_IF`
1242 for child in ${child_wlans}; do
1243 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1244 debug_flags="`get_if_var $child wlandebug_IF`"
1246 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1247 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1248 if [ -n "${debug_flags}" ]; then
1249 wlandebug -i $child ${debug_flags}
1252 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1253 if [ -n "${debug_flags}" ]; then
1254 wlandebug -i $i ${debug_flags}
1256 ${IFCONFIG_CMD} $i name $child && cfg=0
1258 if autoif $child; then
1263 # Create vlan interfaces
1264 child_vlans=`get_if_var $ifn vlans_IF`
1266 if [ -n "${child_vlans}" ]; then
1270 for child in ${child_vlans}; do
1271 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1272 child="${ifn}.${child}"
1273 create_args=`get_if_var $child create_args_IF`
1274 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1276 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1277 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1278 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1280 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1281 ${IFCONFIG_CMD} $i name $child && cfg=0
1284 if autoif $child; then
1293 # Destroy child interfaces.
1297 local cfg child child_vlans child_wlans ifn
1300 child_wlans=`get_if_var $ifn wlans_IF`
1301 for child in ${child_wlans}; do
1302 if ! ifexists $child; then
1305 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1308 child_vlans=`get_if_var $ifn vlans_IF`
1309 for child in ${child_vlans}; do
1310 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1311 child="${ifn}.${child}"
1313 if ! ifexists $child; then
1316 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1323 # Create netgraph nodes.
1327 ngctl -f - 2> /dev/null <<EOF
1334 # Create netgraph nodes.
1340 ng_mkpeer $* | while read line; do
1341 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1342 if [ -n "${t}" ]; then
1350 # Create gif(4) tunnel interfaces.
1355 for i in ${gif_interfaces}; do
1356 peers=`get_if_var $i gifconfig_IF`
1362 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1363 ${IFCONFIG_CMD} $i create >/dev/null 2>&1
1365 gif=`${IFCONFIG_CMD} gif create`
1366 ${IFCONFIG_CMD} $gif name $i
1368 ${IFCONFIG_CMD} $i tunnel ${peers}
1369 ${IFCONFIG_CMD} $i up
1376 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1377 # FEC arguments were found and configured; returns !0 otherwise.
1380 local req_iface iface bogus
1383 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1387 iface=`ng_create_one fec dummy fec`
1388 if [ -z "${iface}" ]; then
1391 if [ "${iface}" = "${req_iface}" ]; then
1394 bogus="${bogus} ${iface}"
1397 for iface in ${bogus}; do
1398 ngctl shutdown ${iface}:
1403 # Create Fast EtherChannel interfaces.
1408 for i in ${fec_interfaces}; do
1410 for j in `get_if_var $i fecconfig_IF`; do
1416 ngctl msg ${i}: add_iface "\"${j}\""
1424 # Configure any IPX addresses for interface $ifn. Returns 0 if
1425 # IPX arguments were found and configured; returns 1 otherwise.
1433 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1434 if [ -n "${ifconfig_args}" ]; then
1435 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1443 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1444 # addresses were found and unconfigured. It returns 1, otherwise.
1448 local _if _ifs _ret ipxList oldifs _ipx
1452 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1456 for _ipx in $ipxList ; do
1457 # get rid of extraneous line
1458 [ -z "$_ipx" ] && break
1460 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1463 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1473 # Rename all requested interfaces.
1480 for _if in `${IFCONFIG_CMD} -l`; do
1481 _ifname=`get_if_var $_if ifconfig_IF_name`
1482 if [ ! -z "$_ifname" ]; then
1483 ${IFCONFIG_CMD} $_if name $_ifname
1490 # list_net_interfaces type
1491 # List all network interfaces. The type of interface returned
1492 # can be controlled by the type argument. The type
1493 # argument can be any of the following:
1494 # nodhcp - all interfaces, excluding DHCP configured interfaces
1495 # dhcp - list only DHCP configured interfaces
1496 # noautoconf - all interfaces, excluding IPv6 Stateless
1497 # Address Autoconf configured interfaces
1498 # autoconf - list only IPv6 Stateless Address Autoconf
1499 # configured interfaces
1500 # If no argument is specified all network interfaces are output.
1501 # Note that the list will include cloned interfaces if applicable.
1502 # Cloned interfaces must already exist to have a chance to appear
1503 # in the list if ${network_interfaces} is set to `auto'.
1505 list_net_interfaces()
1507 local type _tmplist _list _autolist _lo _if
1510 # Get a list of ALL the interfaces and make lo0 first if it's there.
1513 case ${network_interfaces} in
1515 _autolist="`${IFCONFIG_CMD} -l`"
1517 for _if in ${_autolist} ; do
1518 if autoif $_if; then
1519 if [ "$_if" = "lo0" ]; then
1522 _tmplist="${_tmplist} ${_if}"
1526 _tmplist="${_lo}${_tmplist# }"
1529 _tmplist="${network_interfaces} ${cloned_interfaces}"
1531 # lo0 is effectively mandatory, so help prevent foot-shooting
1534 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1535 *) _tmplist="lo0 ${_tmplist}" ;;
1543 for _if in ${_tmplist} ; do
1544 if ! dhcpif $_if && \
1545 [ -n "`_ifconfig_getargs $_if`" ]; then
1546 _list="${_list# } ${_if}"
1551 for _if in ${_tmplist} ; do
1552 if dhcpif $_if; then
1553 _list="${_list# } ${_if}"
1558 for _if in ${_tmplist} ; do
1559 if ! ipv6_autoconfif $_if && \
1560 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1561 _list="${_list# } ${_if}"
1566 for _if in ${_tmplist} ; do
1567 if ipv6_autoconfif $_if; then
1568 _list="${_list# } ${_if}"
1582 # get_default_if -address_family
1583 # Get the interface of the default route for the given address family.
1584 # The -address_family argument must be suitable passing to route(8).
1588 local routeget oldifs defif line
1593 for line in `route -n get $1 default 2>/dev/null`; do
1606 # Echo decimal number $arg (single digit) in hexadecimal format.
1613 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1619 is_wired_interface()
1623 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1624 *media:?Ethernet*) media=Ethernet ;;
1627 test "$media" = "Ethernet"
1630 # network6_getladdr if [flag]
1631 # Echo link-local address from $if if any.
1632 # If flag is defined, tentative ones will be excluded.
1635 local _if _flag proto addr rest
1639 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1640 case "${proto}/${addr}/${_flag}/${rest}" in
1644 inet6/fe80:://*tentative*) # w/o flag
1645 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1646 network6_getladdr $_if $_flags
1648 inet6/fe80::/*/*tentative*) # w/ flag