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 if ! noafif $ifn; then
50 afexists inet && ipv4_up ${ifn} && cfg=0
51 afexists inet6 && ipv6_up ${ifn} && cfg=0
53 childif_create ${ifn} && cfg=0
59 # Shutdown and de-configure an interface. If action is taken,
60 # print the interface name.
68 [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
70 if ! noafif $ifn; then
71 afexists inet6 && ipv6_down ${ifn} && cfg=0
72 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 # Move ifn to the specified vnet jail.
91 # Reclaim ifn from the specified vnet jail.
99 # ifn_vnet0 ifn action
100 # Helper function for ifn_vnetup and ifn_vnetdown.
104 local _ifn _cfg _action _vnet
109 if _vnet=$(vnetif $_ifn); then
110 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
117 # Evaluate ifconfig(8) arguments for interface $if and
118 # run ifconfig(8) with those arguments. It returns 0 if
119 # arguments were found and executed or 1 if the interface
120 # had no arguments. Pseudo arguments DHCP and WPA are handled
125 local _cfg _ifconfig_descr _ipv6_opts ifconfig_args
128 # Make sure lo0 always comes up.
129 if [ "$1" = "lo0" ]; then
134 if ! noafif $1 && afexists inet6; then
135 if checkyesno ipv6_activate_all_interfaces; then
136 _ipv6_opts="-ifdisabled"
139 # backward compatibility: $ipv6_enable
141 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
144 # No accept_rtadv by default on if_bridge(4)
145 # to avoid a conflict with the member
149 if ! checkyesno ipv6_gateway_enable; then
150 _ipv6_opts="${_ipv6_opts} accept_rtadv"
157 case $ipv6_cpe_wanif in
159 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
163 if [ -n "${_ipv6_opts}" ]; then
164 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
169 ifconfig_args=`ifconfig_getargs $1`
170 if [ -n "${ifconfig_args}" ]; then
171 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
176 if ! noafif $1 && afexists inet6; then
178 ifconfig_args=`ifconfig_getargs $1 ipv6`
179 if [ -n "${ifconfig_args}" ]; then
180 # backward compatibility: inet6 keyword
181 case "${ifconfig_args}" in
183 warn "\$ifconfig_$1_ipv6 needs leading" \
184 "\"inet6\" keyword for an IPv6 address."
185 ifconfig_args="inet6 ${ifconfig_args}"
188 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
189 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
193 # $ipv6_prefix_IF will be handled in
194 # ipv6_prefix_hostid_addr_common().
195 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
196 if [ -n "${ifconfig_args}" ]; then
197 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
201 # backward compatibility: $ipv6_ifconfig_IF
202 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
203 if [ -n "${ifconfig_args}" ]; then
204 warn "\$ipv6_ifconfig_$1 is obsolete." \
205 " Use ifconfig_$1_ipv6 instead."
206 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
207 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
212 ifalias $1 link alias
213 ifalias $1 ether alias
215 _ifconfig_descr=`get_if_var $1 ifconfig_IF_descr`
216 if [ -n "${_ifconfig_descr}" ]; then
217 ${IFCONFIG_CMD} $1 description "${_ifconfig_descr}"
221 /etc/rc.d/wpa_supplicant start $1
222 _cfg=0 # XXX: not sure this should count
223 elif hostapif $1; then
224 /etc/rc.d/hostapd start $1
226 elif [ ${_cfg} -eq 0 ]; then
227 ${IFCONFIG_CMD} $1 up
230 if ! noafif $1 && afexists inet6; then
231 ipv6_accept_rtadv_up $1 && _cfg=0
235 if [ $_cfg -ne 0 ] ; then
236 ${IFCONFIG_CMD} $1 up
238 if syncdhcpif $1; then
239 /etc/rc.d/dhclient start $1
248 # returns 1 if wpa_supplicant or dhclient was stopped or
249 # the interface exists.
257 /etc/rc.d/wpa_supplicant stop $1
259 elif hostapif $1; then
260 /etc/rc.d/hostapd stop $1
263 /etc/rc.d/dhclient stop $1
268 ${IFCONFIG_CMD} $1 down
275 # get_if_var if var [default]
276 # Return the value of the pseudo-hash corresponding to $if where
277 # $var is a string containg the sub-string "IF" which will be
278 # replaced with $if after the characters defined in _punct are
279 # replaced with '_'. If the variable is unset, replace it with
283 local _if _punct _punct_c _var _default prefix suffix
285 if [ $# -ne 2 -a $# -ne 3 ]; then
286 err 3 'USAGE: get_if_var name var [default]'
291 ltr ${_if} "${_punct}" '_' _if
297 eval echo \${${prefix}${_if}${suffix}-${_default}}
300 # _ifconfig_getargs if [af]
301 # Prints the arguments for the supplied interface to stdout.
302 # Returns 1 if empty. In general, ifconfig_getargs should be used
310 if [ -z "$_ifn" ]; then
314 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
317 # ifconfig_getargs if [af]
318 # Takes the result from _ifconfig_getargs and removes pseudo
319 # args such as DHCP and WPA.
322 local _tmpargs _arg _args _vnet
323 _tmpargs=`_ifconfig_getargs $1 $2`
324 if [ $? -eq 1 ]; then
330 for _arg in $_tmpargs; do
332 [Dd][Hh][Cc][Pp]:0) ;;
333 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
334 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
335 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
337 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
350 # Returns 0 if the interface should be automatically configured at
351 # boot time and 1 otherwise.
355 _tmpargs=`_ifconfig_getargs $1`
357 for _arg in $_tmpargs; do
359 [Nn][Oo][Aa][Uu][Tt][Oo])
369 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
373 _tmpargs=`_ifconfig_getargs $1`
387 for _arg in $_tmpargs; do
392 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
395 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
405 # Returns 0 if the interface should be configured synchronously and
410 _tmpargs=`_ifconfig_getargs $1`
416 for _arg in $_tmpargs; do
418 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
421 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
427 checkyesno synchronous_dhclient
431 # Returns 0 if the interface is a WPA interface and 1 otherwise.
435 _tmpargs=`_ifconfig_getargs $1`
437 for _arg in $_tmpargs; do
449 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
453 _tmpargs=`_ifconfig_getargs $1`
455 for _arg in $_tmpargs; do
457 [Hh][Oo][Ss][Tt][Aa][Pp])
467 # Returns 0 and echo jail if "vnet" keyword is specified on the
468 # interface, and 1 otherwise.
471 local _tmpargs _arg _vnet
472 _tmpargs=`_ifconfig_getargs $1`
475 for _arg in $_tmpargs; do
478 *:1) echo $_arg; return 0 ;;
486 # Returns 0 if the address family is enabled in the kernel
495 check_kern_features ${_af}
501 err 1 "afexists(): Unsupported address family: $_af"
507 # Returns 0 if the interface has no af configuration and 1 otherwise.
535 # Returns 0 if the interface should be configured for IPv6 and
542 if ! afexists inet6; then
546 # lo0 is always IPv6-enabled
553 case "${ipv6_network_interfaces}" in
554 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
555 # True if $ifconfig_IF_ipv6 is defined.
556 _tmpargs=`_ifconfig_getargs $_if ipv6`
557 if [ -n "${_tmpargs}" ]; then
561 # True if $ipv6_prefix_IF is defined.
562 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
563 if [ -n "${_tmpargs}" ]; then
567 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
568 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
569 if [ -n "${_tmpargs}" ]; then
579 # Returns 0 if the interface should be configured for IPv6 with
580 # Stateless Address Configuration; 1 otherwise.
583 local _if _tmpargs _arg
597 if ! ipv6if $_if; then
600 if checkyesno ipv6_gateway_enable; then
603 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
604 if [ -n "${_tmpargs}" ]; then
607 # backward compatibility: $ipv6_enable
609 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
610 if checkyesno ipv6_gateway_enable; then
615 # No accept_rtadv by default on if_bridge(4)
616 # to avoid a conflict with the member
627 _tmpargs=`_ifconfig_getargs $_if ipv6`
628 for _arg in $_tmpargs; do
636 # backward compatibility: $ipv6_ifconfig_IF
637 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
638 for _arg in $_tmpargs; do
650 # Returns 0 if the interface exists and 1 otherwise.
653 [ -z "$1" ] && return 1
654 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
658 # add IPv4 addresses to the interface $if
665 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
666 if [ "${_if}" = "lo0" ]; then
667 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
668 if [ -z "${ifconfig_args}" ]; then
669 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
672 ifalias ${_if} inet alias && _ret=0
678 # add IPv6 addresses to the interface $if
685 if ! ipv6if $_if; then
689 ifalias ${_if} inet6 alias && _ret=0
690 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
696 # remove IPv4 addresses from the interface $if
699 local _if _ifs _ret inetList oldifs _inet
704 ifalias ${_if} inet -alias && _ret=0
706 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
710 for _inet in $inetList ; do
711 # get rid of extraneous line
717 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
720 ${IFCONFIG_CMD} ${_if} ${_inet} delete
730 # remove IPv6 addresses from the interface $if
733 local _if _ifs _ret inetList oldifs _inet6
738 if ! ipv6if $_if; then
742 ipv6_accept_rtadv_down ${_if} && _ret=0
743 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
744 ifalias ${_if} inet6 -alias && _ret=0
746 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
750 for _inet6 in $inetList ; do
751 # get rid of extraneous line
757 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
760 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
769 # ifalias if af action
770 # Configure or remove aliases for network interface $if.
771 # It returns 0 if at least one alias was configured or
772 # removed, or 1 if there were none.
779 afexists $2 || return $_ret
782 inet|inet6|link|ether)
783 ifalias_af_common $1 $2 $3 && _ret=0
790 # ifalias_expand_addr af action addr
791 # Expand address range ("N-M") specification in addr.
792 # "addr" must not include an address-family keyword.
793 # The results will include an address-family keyword.
795 ifalias_expand_addr()
803 afexists $_af || return
804 ifalias_expand_addr_$_af $_action $*
807 # ifalias_expand_addr_inet action addr
808 # Helper function for ifalias_expand_addr(). Handles IPv4.
810 ifalias_expand_addr_inet()
812 local _action _arg _cidr _cidr_addr _exargs
813 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
821 case $_action:$_arg:$_exargs in
822 *:*--*) return ;; # invalid
823 tmp:*[0-9]-[0-9]*:*) # to be expanded
826 *:*[0-9]-[0-9]*:*) # to be expanded
828 tmp:*:*netmask*) # already expanded w/ netmask option
829 echo ${_arg%/[0-9]*} $_exargs && return
831 tmp:*:*) # already expanded w/o netmask option
832 echo $_arg $_exargs && return
834 *:*:*netmask*) # already expanded w/ netmask option
835 echo inet ${_arg%/[0-9]*} $_exargs && return
837 *:*:*) # already expanded w/o netmask option
838 echo inet $_arg $_exargs && return
842 for _cidr in $_arg; do
845 # When subnet prefix length is not specified, use /32.
847 $_ipaddr) _plen=32 ;; # "/" character not found
851 IFS=. set -- $_ipaddr
861 _iphead="${_iphead}${_iphead:+.}${_c}"
864 _iptail="${_iptail}${_iptail:+.}${_c}"
872 # clear netmask when removing aliases
873 if [ "$_action" = "-alias" ]; then
878 while [ "$_ipcount" -le "$_iphigh" ]; do
879 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
880 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
881 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."
884 _ipcount=$(($_ipcount + 1))
886 # Forcibly set /32 for remaining aliases.
891 for _c in $_retstr; do
892 ifalias_expand_addr_inet $_action $_c $_exargs
896 # ifalias_expand_addr_inet6 action addr
897 # Helper function for ifalias_expand_addr(). Handles IPv6.
899 ifalias_expand_addr_inet6()
901 local _action _arg _cidr _cidr_addr _exargs
902 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
911 case $_action:$_arg:$_exargs in
912 *:*--*:*) return ;; # invalid
913 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
916 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
918 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
919 echo ${_arg%/[0-9]*} $_exargs && return
921 tmp:*:*) # already expanded w/o prefixlen option
922 echo $_arg $_exargs && return
924 *:*:*prefixlen*) # already expanded w/ prefixlen option
925 echo inet6 ${_arg%/[0-9]*} $_exargs && return
927 *:*:*) # already expanded w/o prefixlen option
928 echo inet6 $_arg $_exargs && return
932 for _cidr in $_arg; do
933 _ipaddr="${_cidr%%/*}"
936 case $_action:$_ipaddr:$_cidr in
937 -alias:*:*) unset _plen ;;
938 *:$_cidr:$_ipaddr) unset _plen ;;
941 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
942 # Handle !v4mapped && !v4compat addresses.
944 # The default prefix length is 64.
945 case $_ipaddr:$_cidr in
946 $_cidr:$_ipaddr) _plen="64" ;;
948 _ipleft=${_ipaddr%-*}
949 _ipright=${_ipaddr#*-}
950 _iplow=${_ipleft##*:}
951 _iphigh=${_ipright%%:*}
952 _ipleft=${_ipleft%:*}
953 _ipright=${_ipright#*:}
955 if [ "$_iphigh" = "$_ipright" ]; then
961 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
962 _iplow=$((0x$_iplow))
963 _iphigh=$((0x$_iphigh))
965 while [ $_ipcount -le $_iphigh ]; do
966 _r=`printf "%s:%04x%s%s" \
967 $_ipleft $_ipcount $_ipright \
969 _retstr="$_retstr $_r"
970 if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
972 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."
975 _ipcount=$(($_ipcount + 1))
979 _retstr="${_ipaddr}${_plen:+/}${_plen}"
982 for _c in $_retstr; do
983 ifalias_expand_addr_inet6 $_action $_c $_exargs
986 # v4mapped/v4compat should handle as an IPv4 alias
987 _ipv4part=${_ipaddr##*:}
989 # Adjust prefix length if any. If not, set the
990 # default prefix length as 32.
991 case $_ipaddr:$_cidr in
992 $_cidr:$_ipaddr) _plen=32 ;;
993 *) _plen=$(($_plen - 96)) ;;
996 _retstr=`ifalias_expand_addr_inet \
997 tmp ${_ipv4part}${_plen:+/}${_plen}`
998 for _c in $_retstr; do
999 ifalias_expand_addr_inet $_action $_c $_exargs
1005 # ifalias_af_common_handler if af action args
1006 # Helper function for ifalias_af_common().
1008 ifalias_af_common_handler()
1010 local _ret _if _af _action _args _c _tmpargs
1024 # link(ether) does not support address removal.
1025 case $_af:$_action in
1026 link:-alias|ether:-alias) return ;;
1030 for _c in $_args; do
1034 ${_af}\ *[0-9a-fA-F]-*)
1035 ifalias_af_common_handler $_if $_af $_action \
1036 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1039 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1045 _tmpargs="$_tmpargs $_c"
1049 # Process the last component if any.
1050 if [ -n "$_tmpargs}" ]; then
1052 ${_af}\ *[0-9a-fA-F]-*)
1053 ifalias_af_common_handler $_if $_af $_action \
1054 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1057 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1065 # ifalias_af_common if af action
1066 # Helper function for ifalias().
1070 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1071 local _vif _punct=".-/+"
1079 # Normalize $_if before using it in a pattern to list_vars()
1080 ltr "$_if" "$_punct" "_" _vif
1082 # ifconfig_IF_aliasN which starts with $_af
1083 for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1084 sort_lite -nk1.$((9+${#_vif}+7))`
1086 eval ifconfig_args=\"\$$alias\"
1088 case $ifconfig_args in
1089 inet\ *) _iaf=inet ;;
1090 inet6\ *) _iaf=inet6 ;;
1091 link\ *) _iaf=link ;;
1092 ether\ *) _iaf=ether ;;
1095 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1097 _aliasn="$_aliasn $ifconfig_args"
1103 _aliasn="$_aliasn inet $ifconfig_args"
1104 warn "\$${alias} needs leading" \
1105 "\"inet\" keyword for an IPv4 address."
1109 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1112 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1113 sort_lite -nk1.$((14+${#_vif}+7))`
1115 eval ifconfig_args=\"\$$alias\"
1116 case ${_action}:"${ifconfig_args}" in
1121 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1122 warn "\$${alias} is obsolete. " \
1123 "Use ifconfig_${_vif}_aliasN instead."
1129 # backward compatibility: ipv4_addrs_IF.
1130 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1131 _aliasn="$_aliasn inet $_tmpargs"
1134 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1136 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1138 inet|inet6|link|ether)
1141 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1147 _tmpargs="$_tmpargs $_c"
1150 # Process the last component
1153 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1160 # ipv6_prefix_hostid_addr_common if action
1161 # Add or remove IPv6 prefix + hostid addr on the interface $if
1163 ipv6_prefix_hostid_addr_common()
1165 local _if _action prefix j
1168 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1170 if [ -n "${prefix}" ]; then
1171 for j in ${prefix}; do
1172 # The default prefixlen is 64.
1175 $plen:$j) plen=64 ;;
1179 # Normalize the last part by removing ":"
1182 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1183 prefixlen $plen eui64 ${_action}
1185 # if I am a router, add subnet router
1186 # anycast address (RFC 2373).
1187 if checkyesno ipv6_gateway_enable; then
1188 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1189 prefixlen $plen ${_action} anycast
1195 # ipv6_accept_rtadv_up if
1196 # Enable accepting Router Advertisement and send Router
1197 # Solicitation message
1198 ipv6_accept_rtadv_up()
1200 if ipv6_autoconfif $1; then
1201 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1202 if [ -x /sbin/rtsol ]; then
1203 /sbin/rtsol ${rtsol_flags} $1
1210 # ipv6_accept_rtadv_down if
1211 # Disable accepting Router Advertisement
1212 ipv6_accept_rtadv_down()
1214 if ipv6_autoconfif $1; then
1215 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1220 # Evaluate a startup script for the $if interface.
1221 # It returns 0 if a script was found and processed or
1222 # 1 if no script was found.
1226 if [ -r /etc/start_if.$1 ]; then
1235 # Evaluate a shutdown script for the $if interface.
1236 # It returns 0 if a script was found and processed or
1237 # 1 if no script was found.
1241 if [ -r /etc/stop_if.$1 ]; then
1250 # Create IEEE802.11 interfaces.
1254 local _list _iflist parent child_wlans child create_args debug_flags
1258 # Parse wlans_$parent="$child ..."
1259 for parent in `set | sed -nE 's/wlans_([a-z]+[0-9]+)=.*/\1/p'`; do
1260 child_wlans=`get_if_var $parent wlans_IF`
1261 for child in ${child_wlans}; do
1262 create_args="wlandev $parent `get_if_var $child create_args_IF`"
1263 debug_flags="`get_if_var $child wlandebug_IF`"
1265 ""|$child|$child\ *|*\ $child\ *|*\ $child) ;;
1268 # Skip if ${child} already exists.
1269 if ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1272 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1273 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1275 ${IFCONFIG_CMD} wlan create ${create_args} name $child && cfg=0
1277 if [ $? -eq 0 ]; then
1278 _list="$_list $child"
1280 if [ -n "${debug_flags}" ]; then
1281 wlandebug -i $child ${debug_flags}
1285 if [ -n "${_list# }" ]; then
1286 echo "Created wlan(4) interfaces: ${_list# }."
1288 debug "Created wlan(4)s: ${_list# }"
1292 # Destroy IEEE802.11 interfaces.
1296 local _list _iflist parent child_wlans child
1300 # Parse wlans_$parent="$child ..."
1301 for parent in `set | sed -nE 's/wlans_([a-z]+[0-9]+)=.*/\1/p'`; do
1302 child_wlans=`get_if_var $parent wlans_IF`
1303 for child in ${child_wlans}; do
1305 ""|$child|$child\ *|*\ $child\ *|*\ $child) ;;
1308 # Skip if ${child} doesn't exists.
1309 if ! ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1312 ${IFCONFIG_CMD} -n ${child} destroy
1313 if [ $? -eq 0 ]; then
1314 _list="$_list $child"
1318 if [ -n "${_list# }" ]; then
1319 echo "Destroyed wlan(4) interfaces: ${_list# }."
1321 debug "Destroyed wlan(4)s: ${_list# }"
1325 # Create cloneable interfaces.
1329 local _list ifn ifopt _iflist _inet6 _n tmpargs
1334 for ifn in ${cloned_interfaces}; do
1336 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1338 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1343 # epair(4) uses epair[0-9] for creation and
1344 # epair[0-9][ab] for configuration.
1346 # Skip if ${ifn}a or ${ifn}b already exist.
1347 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1349 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1352 ${IFCONFIG_CMD} ${ifn} create \
1353 `get_if_var ${ifn} create_args_IF`
1354 if [ $? -eq 0 ]; then
1355 _list="$_list ${ifn}a ${ifn}b"
1359 # Skip if ${ifn} already exists.
1360 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1363 ${IFCONFIG_CMD} ${ifn} create \
1364 `get_if_var ${ifn} create_args_IF`
1365 if [ $? -eq 0 ]; then
1370 for ifn in ${gif_interfaces}; do
1372 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1374 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1377 # Skip if ifn already exists.
1378 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1383 ${IFCONFIG_CMD} $ifn create
1386 _n=$(${IFCONFIG_CMD} gif create)
1387 ${IFCONFIG_CMD} $_n name $ifn
1390 if [ $? -eq 0 ]; then
1392 tmpargs=$(get_if_var $ifn gifconfig_IF)
1398 tmpargs=${tmpargs#inet6}
1403 ${IFCONFIG_CMD} $ifn $_inet6 tunnel $tmpargs
1408 if [ -n "${_list# }" ]; then
1409 echo "Created clone interfaces: ${_list# }."
1411 debug "Cloned: ${_list# }"
1415 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1420 local _list ifn _difn ifopt _iflist _sticky
1424 : ${cloned_interfaces_sticky:=NO}
1425 if checkyesno cloned_interfaces_sticky; then
1430 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1432 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1433 case $ifopt:$_sticky in
1434 sticky:*) continue ;; # :sticky => not destroy
1435 nosticky:*) ;; # :nosticky => destroy
1436 *:1) continue ;; # global sticky knob == 1
1439 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1444 # Note: epair(4) uses epair[0-9] for removal and
1445 # epair[0-9][ab] for configuration.
1447 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1448 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1450 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1455 ${IFCONFIG_CMD} -n $_difn destroy
1456 if [ $? -eq 0 ]; then
1457 _list="$_list ${ifn}a ${ifn}b"
1461 # Skip if ifn does not exist.
1462 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1465 ${IFCONFIG_CMD} -n ${ifn} destroy
1466 if [ $? -eq 0 ]; then
1472 if [ -n "${_list# }" ]; then
1473 echo "Destroyed clone interfaces: ${_list# }."
1475 debug "Destroyed clones: ${_list# }"
1479 # Create and configure child interfaces. Return 0 if child
1480 # interfaces are created.
1484 local cfg child child_vlans create_args debug_flags ifn i
1488 # Create vlan interfaces
1489 child_vlans=`get_if_var $ifn vlans_IF`
1491 if [ -n "${child_vlans}" ]; then
1495 for child in ${child_vlans}; do
1496 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1497 child="${ifn}.${child}"
1498 create_args=`get_if_var $child create_args_IF`
1499 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1501 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1502 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1503 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1505 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1506 ${IFCONFIG_CMD} $i name $child && cfg=0
1509 if autoif $child; then
1518 # Destroy child interfaces.
1522 local cfg child child_vlans ifn
1525 child_vlans=`get_if_var $ifn vlans_IF`
1526 for child in ${child_vlans}; do
1527 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1528 child="${ifn}.${child}"
1530 if ! ifexists $child; then
1533 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1540 # Create netgraph nodes.
1544 ngctl -f - 2> /dev/null <<EOF
1551 # Create netgraph nodes.
1557 ng_mkpeer $* | while read line; do
1558 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1559 if [ -n "${t}" ]; then
1566 # ifnet_rename [ifname]
1567 # Rename interfaces if ifconfig_IF_name is defined.
1574 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1575 _ifname=`get_if_var $_if ifconfig_IF_name`
1576 if [ ! -z "$_ifname" ]; then
1577 ${IFCONFIG_CMD} $_if name $_ifname
1584 # list_net_interfaces type
1585 # List all network interfaces. The type of interface returned
1586 # can be controlled by the type argument. The type
1587 # argument can be any of the following:
1588 # nodhcp - all interfaces, excluding DHCP configured interfaces
1589 # dhcp - list only DHCP configured interfaces
1590 # noautoconf - all interfaces, excluding IPv6 Stateless
1591 # Address Autoconf configured interfaces
1592 # autoconf - list only IPv6 Stateless Address Autoconf
1593 # configured interfaces
1594 # If no argument is specified all network interfaces are output.
1595 # Note that the list will include cloned interfaces if applicable.
1596 # Cloned interfaces must already exist to have a chance to appear
1597 # in the list if ${network_interfaces} is set to `auto'.
1599 list_net_interfaces()
1601 local type _tmplist _list _autolist _lo _if
1604 # Get a list of ALL the interfaces and make lo0 first if it's there.
1607 case ${network_interfaces} in
1609 _autolist="`${IFCONFIG_CMD} -l`"
1611 for _if in ${_autolist} ; do
1612 if autoif $_if; then
1613 if [ "$_if" = "lo0" ]; then
1616 _tmplist="${_tmplist} ${_if}"
1620 _tmplist="${_lo}${_tmplist# }"
1623 for _if in ${network_interfaces} ${cloned_interfaces}; do
1624 # epair(4) uses epair[0-9] for creation and
1625 # epair[0-9][ab] for configuration.
1628 _tmplist="$_tmplist ${_if}a ${_if}b"
1631 _tmplist="$_tmplist $_if"
1636 # lo0 is effectively mandatory, so help prevent foot-shooting
1639 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1640 # This is fine, do nothing
1641 _tmplist="${_tmplist# }"
1644 _tmplist="lo0 ${_tmplist# }"
1653 for _if in ${_tmplist} ; do
1654 if ! dhcpif $_if && \
1655 [ -n "`_ifconfig_getargs $_if`" ]; then
1656 _list="${_list# } ${_if}"
1661 for _if in ${_tmplist} ; do
1662 if dhcpif $_if; then
1663 _list="${_list# } ${_if}"
1668 for _if in ${_tmplist} ; do
1669 if ! ipv6_autoconfif $_if && \
1670 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1671 _list="${_list# } ${_if}"
1676 for _if in ${_tmplist} ; do
1677 if ipv6_autoconfif $_if; then
1678 _list="${_list# } ${_if}"
1692 # get_default_if -address_family
1693 # Get the interface of the default route for the given address family.
1694 # The -address_family argument must be suitable passing to route(8).
1698 local routeget oldifs defif line
1703 for line in `route -n get $1 default 2>/dev/null`; do
1716 # Echo decimal number $arg (single digit) in hexadecimal format.
1723 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1729 is_wired_interface()
1733 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1734 *media:?Ethernet*) media=Ethernet ;;
1737 test "$media" = "Ethernet"
1740 # network6_getladdr if [flag]
1741 # Echo link-local address from $if if any.
1742 # If flag is defined, tentative ones will be excluded.
1745 local _if _flag proto addr rest
1749 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1750 case "${proto}/${addr}/${_flag}/${rest}" in
1754 inet6/fe80:://*tentative*) # w/o flag
1755 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1756 network6_getladdr $_if $_flags
1758 inet6/fe80::/*/*tentative*) # w/ flag