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
54 afexists ipx && ipx_up ${ifn} && cfg=0
56 childif_create ${ifn} && cfg=0
62 # Shutdown and de-configure an interface. If action is taken,
63 # print the interface name.
71 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
73 if ! noafif $ifn; then
74 afexists ipx && ipx_down ${ifn} && cfg=0
75 afexists inet6 && ipv6_down ${ifn} && cfg=0
76 afexists inet && ipv4_down ${ifn} && cfg=0
78 ifconfig_down ${ifn} && cfg=0
79 ifscript_down ${ifn} && cfg=0
80 childif_destroy ${ifn} && cfg=0
86 # Move ifn to the specified vnet jail.
95 # Reclaim ifn from the specified vnet jail.
103 # ifn_vnet0 ifn action
104 # Helper function for ifn_vnetup and ifn_vnetdown.
108 local _ifn _cfg _action _vnet
113 if _vnet=$(vnetif $_ifn); then
114 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
121 # Evaluate ifconfig(8) arguments for interface $if and
122 # run ifconfig(8) with those arguments. It returns 0 if
123 # arguments were found and executed or 1 if the interface
124 # had no arguments. Pseudo arguments DHCP and WPA are handled
129 local _cfg _ipv6_opts ifconfig_args
132 # Make sure lo0 always comes up.
133 if [ "$1" = "lo0" ]; then
138 ifconfig_args=`ifconfig_getargs $1`
139 if [ -n "${ifconfig_args}" ]; then
140 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
145 if ! noafif $1 && afexists inet6; then
146 if checkyesno ipv6_activate_all_interfaces; then
147 _ipv6_opts="-ifdisabled"
148 elif [ "$1" != "lo0" ]; then
149 _ipv6_opts="ifdisabled"
152 # backward compatibility: $ipv6_enable
154 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
157 # No accept_rtadv by default on if_bridge(4)
158 # to avoid a conflict with the member
162 if ! checkyesno ipv6_gateway_enable; then
163 _ipv6_opts="${_ipv6_opts} accept_rtadv"
170 case $ipv6_cpe_wanif in
172 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
176 if [ -n "${_ipv6_opts}" ]; then
177 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
181 ifconfig_args=`ifconfig_getargs $1 ipv6`
182 if [ -n "${ifconfig_args}" ]; then
183 # backward compatibility: inet6 keyword
184 case "${ifconfig_args}" in
186 warn "\$ifconfig_$1_ipv6 needs leading" \
187 "\"inet6\" keyword for an IPv6 address."
188 ifconfig_args="inet6 ${ifconfig_args}"
191 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
192 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
196 # $ipv6_prefix_IF will be handled in
197 # ipv6_prefix_hostid_addr_common().
198 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
199 if [ -n "${ifconfig_args}" ]; then
200 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
204 # backward compatibility: $ipv6_ifconfig_IF
205 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
206 if [ -n "${ifconfig_args}" ]; then
207 warn "\$ipv6_ifconfig_$1 is obsolete." \
208 " Use ifconfig_$1_ipv6 instead."
209 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
210 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
215 ifalias $1 link alias
216 ifalias $1 ether alias
218 if [ ${_cfg} -eq 0 ]; then
219 ${IFCONFIG_CMD} $1 up
223 /etc/rc.d/wpa_supplicant start $1
224 _cfg=0 # XXX: not sure this should count
225 elif hostapif $1; then
226 /etc/rc.d/hostapd start $1
231 if [ $_cfg -ne 0 ] ; then
232 ${IFCONFIG_CMD} $1 up
234 if syncdhcpif $1; then
235 /etc/rc.d/dhclient start $1
244 # returns 1 if wpa_supplicant or dhclient was stopped or
245 # the interface exists.
253 /etc/rc.d/wpa_supplicant stop $1
255 elif hostapif $1; then
256 /etc/rc.d/hostapd stop $1
261 /etc/rc.d/dhclient stop $1
266 ${IFCONFIG_CMD} $1 down
273 # get_if_var if var [default]
274 # Return the value of the pseudo-hash corresponding to $if where
275 # $var is a string containg the sub-string "IF" which will be
276 # replaced with $if after the characters defined in _punct are
277 # replaced with '_'. If the variable is unset, replace it with
281 local _if _punct _punct_c _var _default prefix suffix
283 if [ $# -ne 2 -a $# -ne 3 ]; then
284 err 3 'USAGE: get_if_var name var [default]'
289 ltr ${_if} "${_punct}" '_' _if
295 eval echo \${${prefix}${_if}${suffix}-${_default}}
298 # _ifconfig_getargs if [af]
299 # Prints the arguments for the supplied interface to stdout.
300 # Returns 1 if empty. In general, ifconfig_getargs should be used
308 if [ -z "$_ifn" ]; then
312 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
315 # ifconfig_getargs if [af]
316 # Takes the result from _ifconfig_getargs and removes pseudo
317 # args such as DHCP and WPA.
320 local _tmpargs _arg _args _vnet
321 _tmpargs=`_ifconfig_getargs $1 $2`
322 if [ $? -eq 1 ]; then
328 for _arg in $_tmpargs; do
330 [Dd][Hh][Cc][Pp]:0) ;;
331 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
332 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
333 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
335 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
348 # Returns 0 if the interface should be automatically configured at
349 # boot time and 1 otherwise.
353 _tmpargs=`_ifconfig_getargs $1`
355 for _arg in $_tmpargs; do
357 [Nn][Oo][Aa][Uu][Tt][Oo])
367 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
371 _tmpargs=`_ifconfig_getargs $1`
386 for _arg in $_tmpargs; do
391 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
394 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
404 # Returns 0 if the interface should be configured synchronously and
409 _tmpargs=`_ifconfig_getargs $1`
415 for _arg in $_tmpargs; do
417 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
420 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
426 checkyesno synchronous_dhclient
430 # Returns 0 if the interface is a WPA interface and 1 otherwise.
434 _tmpargs=`_ifconfig_getargs $1`
436 for _arg in $_tmpargs; do
448 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
452 _tmpargs=`_ifconfig_getargs $1`
454 for _arg in $_tmpargs; do
456 [Hh][Oo][Ss][Tt][Aa][Pp])
466 # Returns 0 and echo jail if "vnet" keyword is specified on the
467 # interface, and 1 otherwise.
470 local _tmpargs _arg _vnet
471 _tmpargs=`_ifconfig_getargs $1`
474 for _arg in $_tmpargs; do
477 *:1) echo $_arg; return 0 ;;
485 # Returns 0 if the address family is enabled in the kernel
494 check_kern_features ${_af}
497 ${SYSCTL_N} net.ipx > /dev/null 2>&1
500 if [ -x /sbin/atmconfig ]; then
501 /sbin/atmconfig diag list > /dev/null 2>&1
510 err 1 "afexists(): Unsupported address family: $_af"
516 # Returns 0 if the interface has no af configuration and 1 otherwise.
544 # Returns 0 if the interface should be configured for IPv6 and
551 if ! afexists inet6; then
555 # lo0 is always IPv6-enabled
562 case "${ipv6_network_interfaces}" in
563 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
564 # True if $ifconfig_IF_ipv6 is defined.
565 _tmpargs=`_ifconfig_getargs $_if ipv6`
566 if [ -n "${_tmpargs}" ]; then
570 # True if $ipv6_prefix_IF is defined.
571 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
572 if [ -n "${_tmpargs}" ]; then
576 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
577 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
578 if [ -n "${_tmpargs}" ]; then
588 # Returns 0 if the interface should be configured for IPv6 with
589 # Stateless Address Configuration; 1 otherwise.
592 local _if _tmpargs _arg
607 if ! ipv6if $_if; then
610 if checkyesno ipv6_gateway_enable; then
613 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
614 if [ -n "${_tmpargs}" ]; then
617 # backward compatibility: $ipv6_enable
619 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
620 if checkyesno ipv6_gateway_enable; then
625 # No accept_rtadv by default on if_bridge(4)
626 # to avoid a conflict with the member
637 _tmpargs=`_ifconfig_getargs $_if ipv6`
638 for _arg in $_tmpargs; do
646 # backward compatibility: $ipv6_ifconfig_IF
647 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
648 for _arg in $_tmpargs; do
660 # Returns 0 if the interface exists and 1 otherwise.
663 [ -z "$1" ] && return 1
664 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
668 # add IPv4 addresses to the interface $if
675 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
676 if [ "${_if}" = "lo0" ]; then
677 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
678 if [ -z "${ifconfig_args}" ]; then
679 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
682 ifalias ${_if} inet alias && _ret=0
688 # add IPv6 addresses to the interface $if
695 if ! ipv6if $_if; then
699 ifalias ${_if} inet6 alias && _ret=0
700 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
701 ipv6_accept_rtadv_up ${_if} && _ret=0
707 # remove IPv4 addresses from the interface $if
710 local _if _ifs _ret inetList oldifs _inet
715 ifalias ${_if} inet -alias && _ret=0
717 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
721 for _inet in $inetList ; do
722 # get rid of extraneous line
728 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
731 ${IFCONFIG_CMD} ${_if} ${_inet} delete
741 # remove IPv6 addresses from the interface $if
744 local _if _ifs _ret inetList oldifs _inet6
749 if ! ipv6if $_if; then
753 ipv6_accept_rtadv_down ${_if} && _ret=0
754 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
755 ifalias ${_if} inet6 -alias && _ret=0
757 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
761 for _inet6 in $inetList ; do
762 # get rid of extraneous line
768 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
771 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
780 # ifalias if af action
781 # Configure or remove aliases for network interface $if.
782 # It returns 0 if at least one alias was configured or
783 # removed, or 1 if there were none.
790 afexists $2 || return $_ret
793 inet|inet6|link|ether)
794 ifalias_af_common $1 $2 $3 && _ret=0
801 # ifalias_expand_addr af action addr
802 # Expand address range ("N-M") specification in addr.
803 # "addr" must not include an address-family keyword.
804 # The results will include an address-family keyword.
806 ifalias_expand_addr()
814 afexists $_af || return
815 ifalias_expand_addr_$_af $_action $*
818 # ifalias_expand_addr_inet action addr
819 # Helper function for ifalias_expand_addr(). Handles IPv4.
821 ifalias_expand_addr_inet()
823 local _action _arg _cidr _cidr_addr _exargs
824 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
832 case $_action:$_arg:$_exargs in
833 *:*--*) return ;; # invalid
834 tmp:*[0-9]-[0-9]*:*) # to be expanded
837 *:*[0-9]-[0-9]*:*) # to be expanded
839 tmp:*:*netmask*) # already expanded w/ netmask option
840 echo ${_arg%/[0-9]*} $_exargs && return
842 tmp:*:*) # already expanded w/o netmask option
843 echo $_arg $_exargs && return
845 *:*:*netmask*) # already expanded w/ netmask option
846 echo inet ${_arg%/[0-9]*} $_exargs && return
848 *:*:*) # already expanded w/o netmask option
849 echo inet $_arg $_exargs && return
853 for _cidr in $_arg; do
856 # When subnet prefix length is not specified, use /32.
858 $_ipaddr) _plen=32 ;; # "/" character not found
862 IFS=. set -- $_ipaddr
872 _iphead="${_iphead}${_iphead:+.}${_c}"
875 _iptail="${_iptail}${_iptail:+.}${_c}"
883 # clear netmask when removing aliases
884 if [ "$_action" = "-alias" ]; then
889 while [ "$_ipcount" -le "$_iphigh" ]; do
890 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
891 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
892 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."
895 _ipcount=$(($_ipcount + 1))
897 # Forcibly set /32 for remaining aliases.
902 for _c in $_retstr; do
903 ifalias_expand_addr_inet $_action $_c $_exargs
907 # ifalias_expand_addr_inet6 action addr
908 # Helper function for ifalias_expand_addr(). Handles IPv6.
910 ifalias_expand_addr_inet6()
912 local _action _arg _cidr _cidr_addr _exargs
913 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
922 case $_action:$_arg:$_exargs in
923 *:*--*:*) return ;; # invalid
924 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
927 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
929 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
930 echo ${_arg%/[0-9]*} $_exargs && return
932 tmp:*:*) # already expanded w/o prefixlen option
933 echo $_arg $_exargs && return
935 *:*:*prefixlen*) # already expanded w/ prefixlen option
936 echo inet6 ${_arg%/[0-9]*} $_exargs && return
938 *:*:*) # already expanded w/o prefixlen option
939 echo inet6 $_arg $_exargs && return
943 for _cidr in $_arg; do
944 _ipaddr="${_cidr%%/*}"
947 case $_action:$_ipaddr:$_cidr in
948 -alias:*:*) unset _plen ;;
949 *:$_cidr:$_ipaddr) unset _plen ;;
952 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
953 # Handle !v4mapped && !v4compat addresses.
955 # The default prefix length is 64.
956 case $_ipaddr:$_cidr in
957 $_cidr:$_ipaddr) _plen="64" ;;
959 _ipleft=${_ipaddr%-*}
960 _ipright=${_ipaddr#*-}
961 _iplow=${_ipleft##*:}
962 _iphigh=${_ipright%%:*}
963 _ipleft=${_ipleft%:*}
964 _ipright=${_ipright#*:}
966 if [ "$_iphigh" = "$_ipright" ]; then
972 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
973 _iplow=$((0x$_iplow))
974 _iphigh=$((0x$_iphigh))
976 while [ $_ipcount -le $_iphigh ]; do
977 _r=`printf "%s:%04x%s%s" \
978 $_ipleft $_ipcount $_ipright \
980 _retstr="$_retstr $_r"
981 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
983 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."
986 _ipcount=$(($_ipcount + 1))
990 _retstr="${_ipaddr}${_plen:+/}${_plen}"
993 for _c in $_retstr; do
994 ifalias_expand_addr_inet6 $_action $_c $_exargs
997 # v4mapped/v4compat should handle as an IPv4 alias
998 _ipv4part=${_ipaddr##*:}
1000 # Adjust prefix length if any. If not, set the
1001 # default prefix length as 32.
1002 case $_ipaddr:$_cidr in
1003 $_cidr:$_ipaddr) _plen=32 ;;
1004 *) _plen=$(($_plen - 96)) ;;
1007 _retstr=`ifalias_expand_addr_inet \
1008 tmp ${_ipv4part}${_plen:+/}${_plen}`
1009 for _c in $_retstr; do
1010 ifalias_expand_addr_inet $_action $_c $_exargs
1016 # ifalias_af_common_handler if af action args
1017 # Helper function for ifalias_af_common().
1019 ifalias_af_common_handler()
1021 local _ret _if _af _action _args _c _tmpargs
1035 # link(ether) does not support address removal.
1036 case $_af:$_action in
1037 link:-alias|ether:-alias) return ;;
1041 for _c in $_args; do
1046 ifalias_af_common_handler $_if $_af $_action \
1047 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1050 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1056 _tmpargs="$_tmpargs $_c"
1060 # Process the last component if any.
1061 if [ -n "$_tmpargs}" ]; then
1064 ifalias_af_common_handler $_if $_af $_action \
1065 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1068 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1076 # ifalias_af_common if af action
1077 # Helper function for ifalias().
1081 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1082 local _vif _punct=".-/+"
1090 # Normalize $_if before using it in a pattern to list_vars()
1091 ltr "$_if" "$_punct" "_" _vif
1093 # ifconfig_IF_aliasN which starts with $_af
1094 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1095 sort_lite -nk1.$((9+${#_vif}+7))`
1097 eval ifconfig_args=\"\$$alias\"
1099 case $ifconfig_args in
1100 inet\ *) _iaf=inet ;;
1101 inet6\ *) _iaf=inet6 ;;
1103 link\ *) _iaf=link ;;
1104 ether\ *) _iaf=ether ;;
1107 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1109 _aliasn="$_aliasn $ifconfig_args"
1115 _aliasn="$_aliasn inet $ifconfig_args"
1116 warn "\$${alias} needs leading" \
1117 "\"inet\" keyword for an IPv4 address."
1121 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1124 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1125 sort_lite -nk1.$((14+${#_vif}+7))`
1127 eval ifconfig_args=\"\$$alias\"
1128 case ${_action}:"${ifconfig_args}" in
1133 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1134 warn "\$${alias} is obsolete. " \
1135 "Use ifconfig_${_vif}_aliasN instead."
1141 # backward compatibility: ipv4_addrs_IF.
1142 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1143 _aliasn="$_aliasn inet $_tmpargs"
1146 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1148 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1150 inet|inet6|ipx|link|ether)
1153 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1159 _tmpargs="$_tmpargs $_c"
1162 # Process the last component
1165 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1172 # ipv6_prefix_hostid_addr_common if action
1173 # Add or remove IPv6 prefix + hostid addr on the interface $if
1175 ipv6_prefix_hostid_addr_common()
1177 local _if _action prefix j
1180 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1182 if [ -n "${prefix}" ]; then
1183 for j in ${prefix}; do
1184 # The default prefixlen is 64.
1187 $plen:$j) plen=64 ;;
1191 # Normalize the last part by removing ":"
1194 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1195 prefixlen $plen eui64 ${_action}
1197 # if I am a router, add subnet router
1198 # anycast address (RFC 2373).
1199 if checkyesno ipv6_gateway_enable; then
1200 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1201 prefixlen $plen ${_action} anycast
1207 # ipv6_accept_rtadv_up if
1208 # Enable accepting Router Advertisement and send Router
1209 # Solicitation message
1210 ipv6_accept_rtadv_up()
1212 if ipv6_autoconfif $1; then
1213 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1214 if ! checkyesno rtsold_enable; then
1215 rtsol ${rtsol_flags} $1
1220 # ipv6_accept_rtadv_down if
1221 # Disable accepting Router Advertisement
1222 ipv6_accept_rtadv_down()
1224 if ipv6_autoconfif $1; then
1225 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1230 # Evaluate a startup script for the $if interface.
1231 # It returns 0 if a script was found and processed or
1232 # 1 if no script was found.
1236 if [ -r /etc/start_if.$1 ]; then
1245 # Evaluate a shutdown script for the $if interface.
1246 # It returns 0 if a script was found and processed or
1247 # 1 if no script was found.
1251 if [ -r /etc/stop_if.$1 ]; then
1260 # Create cloneable interfaces.
1264 local _list ifn ifopt _iflist _n tmpargs
1269 for ifn in ${cloned_interfaces}; do
1271 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1273 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1278 # epair(4) uses epair[0-9] for creation and
1279 # epair[0-9][ab] for configuration.
1281 # Skip if ${ifn}a or ${ifn}b already exist.
1282 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1284 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1287 ${IFCONFIG_CMD} ${ifn} create \
1288 `get_if_var ${ifn} create_args_IF`
1289 if [ $? -eq 0 ]; then
1290 _list="$_list ${ifn}a ${ifn}b"
1294 # Skip if ${ifn} already exists.
1295 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1298 ${IFCONFIG_CMD} ${ifn} create \
1299 `get_if_var ${ifn} create_args_IF`
1300 if [ $? -eq 0 ]; then
1305 if [ -n "$gif_interfaces" ]; then
1306 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1308 for ifn in ${gif_interfaces}; do
1310 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1312 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1315 # Skip if ifn already exists.
1316 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1321 ${IFCONFIG_CMD} $ifn create
1324 _n=$(${IFCONFIG_CMD} gif create)
1325 ${IFCONFIG_CMD} $_n name $ifn
1328 if [ $? -eq 0 ]; then
1331 tmpargs=$(get_if_var $ifn gifconfig_IF)
1332 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1334 if [ -n "${_list# }" ]; then
1335 echo "Created clone interfaces: ${_list# }."
1337 debug "Cloned: ${_list# }"
1341 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1346 local _list ifn _difn ifopt _iflist _sticky
1350 : ${cloned_interfaces_sticky:=NO}
1351 if checkyesno cloned_interfaces_sticky; then
1356 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1358 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1359 case $ifopt:$_sticky in
1360 sticky:*) continue ;; # :sticky => not destroy
1361 nosticky:*) ;; # :nosticky => destroy
1362 *:1) continue ;; # global sticky knob == 1
1365 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1370 # Note: epair(4) uses epair[0-9] for removal and
1371 # epair[0-9][ab] for configuration.
1373 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1374 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1376 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1381 ${IFCONFIG_CMD} -n $_difn destroy
1382 if [ $? -eq 0 ]; then
1383 _list="$_list ${ifn}a ${ifn}b"
1387 # Skip if ifn does not exist.
1388 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1391 ${IFCONFIG_CMD} -n ${ifn} destroy
1392 if [ $? -eq 0 ]; then
1398 if [ -n "${_list# }" ]; then
1399 echo "Destroyed clone interfaces: ${_list# }."
1401 debug "Destroyed clones: ${_list# }"
1405 # Create and configure child interfaces. Return 0 if child
1406 # interfaces are created.
1410 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1414 # Create wireless interfaces
1415 child_wlans=`get_if_var $ifn wlans_IF`
1417 for child in ${child_wlans}; do
1418 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1419 debug_flags="`get_if_var $child wlandebug_IF`"
1421 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1422 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1423 if [ -n "${debug_flags}" ]; then
1424 wlandebug -i $child ${debug_flags}
1427 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1428 if [ -n "${debug_flags}" ]; then
1429 wlandebug -i $i ${debug_flags}
1431 ${IFCONFIG_CMD} $i name $child && cfg=0
1433 if autoif $child; then
1438 # Create vlan interfaces
1439 child_vlans=`get_if_var $ifn vlans_IF`
1441 if [ -n "${child_vlans}" ]; then
1445 for child in ${child_vlans}; do
1446 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1447 child="${ifn}.${child}"
1448 create_args=`get_if_var $child create_args_IF`
1449 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1451 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1452 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1453 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1455 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1456 ${IFCONFIG_CMD} $i name $child && cfg=0
1459 if autoif $child; then
1468 # Destroy child interfaces.
1472 local cfg child child_vlans child_wlans ifn
1475 child_wlans=`get_if_var $ifn wlans_IF`
1476 for child in ${child_wlans}; do
1477 if ! ifexists $child; then
1480 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1483 child_vlans=`get_if_var $ifn vlans_IF`
1484 for child in ${child_vlans}; do
1485 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1486 child="${ifn}.${child}"
1488 if ! ifexists $child; then
1491 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1498 # Create netgraph nodes.
1502 ngctl -f - 2> /dev/null <<EOF
1509 # Create netgraph nodes.
1515 ng_mkpeer $* | while read line; do
1516 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1517 if [ -n "${t}" ]; then
1525 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1526 # FEC arguments were found and configured; returns !0 otherwise.
1529 local req_iface iface bogus
1532 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1536 iface=`ng_create_one fec dummy fec`
1537 if [ -z "${iface}" ]; then
1540 if [ "${iface}" = "${req_iface}" ]; then
1543 bogus="${bogus} ${iface}"
1546 for iface in ${bogus}; do
1547 ngctl shutdown ${iface}:
1552 # Create Fast EtherChannel interfaces.
1557 for i in ${fec_interfaces}; do
1559 for j in `get_if_var $i fecconfig_IF`; do
1565 ngctl msg ${i}: add_iface "\"${j}\""
1573 # Configure any IPX addresses for interface $ifn. Returns 0 if
1574 # IPX arguments were found and configured; returns 1 otherwise.
1582 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1583 if [ -n "${ifconfig_args}" ]; then
1584 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1592 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1593 # addresses were found and unconfigured. It returns 1, otherwise.
1597 local _if _ifs _ret ipxList oldifs _ipx
1601 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1605 for _ipx in $ipxList ; do
1606 # get rid of extraneous line
1607 [ -z "$_ipx" ] && break
1609 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1612 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1621 # ifnet_rename [ifname]
1622 # Rename interfaces if ifconfig_IF_name is defined.
1629 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1630 _ifname=`get_if_var $_if ifconfig_IF_name`
1631 if [ ! -z "$_ifname" ]; then
1632 ${IFCONFIG_CMD} $_if name $_ifname
1639 # list_net_interfaces type
1640 # List all network interfaces. The type of interface returned
1641 # can be controlled by the type argument. The type
1642 # argument can be any of the following:
1643 # nodhcp - all interfaces, excluding DHCP configured interfaces
1644 # dhcp - list only DHCP configured interfaces
1645 # noautoconf - all interfaces, excluding IPv6 Stateless
1646 # Address Autoconf configured interfaces
1647 # autoconf - list only IPv6 Stateless Address Autoconf
1648 # configured interfaces
1649 # If no argument is specified all network interfaces are output.
1650 # Note that the list will include cloned interfaces if applicable.
1651 # Cloned interfaces must already exist to have a chance to appear
1652 # in the list if ${network_interfaces} is set to `auto'.
1654 list_net_interfaces()
1656 local type _tmplist _list _autolist _lo _if
1659 # Get a list of ALL the interfaces and make lo0 first if it's there.
1662 case ${network_interfaces} in
1664 _autolist="`${IFCONFIG_CMD} -l`"
1666 for _if in ${_autolist} ; do
1667 if autoif $_if; then
1668 if [ "$_if" = "lo0" ]; then
1671 _tmplist="${_tmplist} ${_if}"
1675 _tmplist="${_lo}${_tmplist# }"
1678 for _if in ${network_interfaces} ${cloned_interfaces}; do
1679 # epair(4) uses epair[0-9] for creation and
1680 # epair[0-9][ab] for configuration.
1683 _tmplist="$_tmplist ${_if}a ${_if}b"
1686 _tmplist="$_tmplist $_if"
1691 # lo0 is effectively mandatory, so help prevent foot-shooting
1694 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1695 # This is fine, do nothing
1696 _tmplist="${_tmplist# }"
1699 _tmplist="lo0 ${_tmplist# }"
1708 for _if in ${_tmplist} ; do
1709 if ! dhcpif $_if && \
1710 [ -n "`_ifconfig_getargs $_if`" ]; then
1711 _list="${_list# } ${_if}"
1716 for _if in ${_tmplist} ; do
1717 if dhcpif $_if; then
1718 _list="${_list# } ${_if}"
1723 for _if in ${_tmplist} ; do
1724 if ! ipv6_autoconfif $_if && \
1725 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1726 _list="${_list# } ${_if}"
1731 for _if in ${_tmplist} ; do
1732 if ipv6_autoconfif $_if; then
1733 _list="${_list# } ${_if}"
1747 # get_default_if -address_family
1748 # Get the interface of the default route for the given address family.
1749 # The -address_family argument must be suitable passing to route(8).
1753 local routeget oldifs defif line
1758 for line in `route -n get $1 default 2>/dev/null`; do
1771 # Echo decimal number $arg (single digit) in hexadecimal format.
1778 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1784 is_wired_interface()
1788 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1789 *media:?Ethernet*) media=Ethernet ;;
1792 test "$media" = "Ethernet"
1795 # network6_getladdr if [flag]
1796 # Echo link-local address from $if if any.
1797 # If flag is defined, tentative ones will be excluded.
1800 local _if _flag proto addr rest
1804 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1805 case "${proto}/${addr}/${_flag}/${rest}" in
1809 inet6/fe80:://*tentative*) # w/o flag
1810 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1811 network6_getladdr $_if $_flags
1813 inet6/fe80::/*/*tentative*) # w/ flag