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)
116 if ! checkyesno ipv6_gateway_enable; then
117 _ipv6_opts="${_ipv6_opts} accept_rtadv"
122 case $ipv6_cpe_wanif in
124 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
128 if [ -n "${_ipv6_opts}" ]; then
129 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
133 ifconfig_args=`ifconfig_getargs $1 ipv6`
134 if [ -n "${ifconfig_args}" ]; then
135 # backward compatibility: inet6 keyword
136 case "${ifconfig_args}" in
138 warn "\$ifconfig_$1_ipv6 needs " \
139 "\"inet6\" keyword for an IPv6 address."
140 ifconfig_args="inet6 ${ifconfig_args}"
143 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
144 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
148 # $ipv6_prefix_IF will be handled in
149 # ipv6_prefix_hostid_addr_common().
150 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
151 if [ -n "${ifconfig_args}" ]; then
152 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
156 # backward compatibility: $ipv6_ifconfig_IF
157 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
158 if [ -n "${ifconfig_args}" ]; then
159 warn "\$ipv6_ifconfig_$1 is obsolete." \
160 " Use ifconfig_$1_ipv6 instead."
161 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
162 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
167 if [ ${_cfg} -eq 0 ]; then
168 ${IFCONFIG_CMD} $1 up
172 /etc/rc.d/wpa_supplicant start $1
173 _cfg=0 # XXX: not sure this should count
174 elif hostapif $1; then
175 /etc/rc.d/hostapd start $1
180 if [ $_cfg -ne 0 ] ; then
181 ${IFCONFIG_CMD} $1 up
183 if syncdhcpif $1; then
184 /etc/rc.d/dhclient start $1
193 # returns 1 if wpa_supplicant or dhclient was stopped or
194 # the interface exists.
202 /etc/rc.d/wpa_supplicant stop $1
204 elif hostapif $1; then
205 /etc/rc.d/hostapd stop $1
210 /etc/rc.d/dhclient stop $1
215 ${IFCONFIG_CMD} $1 down
222 # get_if_var if var [default]
223 # Return the value of the pseudo-hash corresponding to $if where
224 # $var is a string containg the sub-string "IF" which will be
225 # replaced with $if after the characters defined in _punct are
226 # replaced with '_'. If the variable is unset, replace it with
230 local _if _punct _punct_c _var _default prefix suffix
232 if [ $# -ne 2 -a $# -ne 3 ]; then
233 err 3 'USAGE: get_if_var name var [default]'
238 for _punct_c in $_punct; do
239 _if=`ltr ${_if} ${_punct_c} '_'`
246 eval echo \${${prefix}${_if}${suffix}-${_default}}
249 # _ifconfig_getargs if [af]
250 # Prints the arguments for the supplied interface to stdout.
251 # Returns 1 if empty. In general, ifconfig_getargs should be used
259 if [ -z "$_ifn" ]; then
263 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
266 # ifconfig_getargs if [af]
267 # Takes the result from _ifconfig_getargs and removes pseudo
268 # args such as DHCP and WPA.
271 local _tmpargs _arg _args
272 _tmpargs=`_ifconfig_getargs $1 $2`
273 if [ $? -eq 1 ]; then
278 for _arg in $_tmpargs; do
281 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
282 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
283 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
285 [Hh][Oo][Ss][Tt][Aa][Pp]) ;;
296 # Returns 0 if the interface should be automatically configured at
297 # boot time and 1 otherwise.
301 _tmpargs=`_ifconfig_getargs $1`
303 for _arg in $_tmpargs; do
305 [Nn][Oo][Aa][Uu][Tt][Oo])
315 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
319 _tmpargs=`_ifconfig_getargs $1`
325 for _arg in $_tmpargs; do
330 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
333 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
343 # Returns 0 if the interface should be configured synchronously and
348 _tmpargs=`_ifconfig_getargs $1`
354 for _arg in $_tmpargs; do
356 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
359 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
365 checkyesno synchronous_dhclient
369 # Returns 0 if the interface is a WPA interface and 1 otherwise.
373 _tmpargs=`_ifconfig_getargs $1`
375 for _arg in $_tmpargs; do
387 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
391 _tmpargs=`_ifconfig_getargs $1`
393 for _arg in $_tmpargs; do
395 [Hh][Oo][Ss][Tt][Aa][Pp])
405 # Returns 0 if the address family is enabled in the kernel
414 check_kern_features ${_af}
417 ${SYSCTL_N} net.ipx > /dev/null 2>&1
420 if [ -x /sbin/atmconfig ]; then
421 /sbin/atmconfig diag list > /dev/null 2>&1
427 err 1 "afexists(): Unsupported address family: $_af"
433 # Returns 0 if the interface has no af configuration and 1 otherwise.
460 # Returns 0 if the interface should be configured for IPv6 and
467 if ! afexists inet6; then
471 # lo0 is always IPv6-enabled
478 case "${ipv6_network_interfaces}" in
479 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
480 # True if $ifconfig_IF_ipv6 is defined.
481 _tmpargs=`_ifconfig_getargs $_if ipv6`
482 if [ -n "${_tmpargs}" ]; then
486 # True if $ipv6_prefix_IF is defined.
487 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
488 if [ -n "${_tmpargs}" ]; then
492 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
493 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
494 if [ -n "${_tmpargs}" ]; then
504 # Returns 0 if the interface should be configured for IPv6 with
505 # Stateless Address Configuration; 1 otherwise.
508 local _if _tmpargs _arg
523 if ! ipv6if $_if; then
526 if checkyesno ipv6_gateway_enable; then
529 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
530 if [ -n "${_tmpargs}" ]; then
533 # backward compatibility: $ipv6_enable
535 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
536 if checkyesno ipv6_gateway_enable; then
544 _tmpargs=`_ifconfig_getargs $_if ipv6`
545 for _arg in $_tmpargs; do
553 # backward compatibility: $ipv6_ifconfig_IF
554 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
555 for _arg in $_tmpargs; do
567 # Returns 0 if the interface exists and 1 otherwise.
570 [ -z "$1" ] && return 1
571 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
575 # add IPv4 addresses to the interface $if
582 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
583 if [ "${_if}" = "lo0" ]; then
584 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
585 if [ -z "${ifconfig_args}" ]; then
586 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
589 ifalias ${_if} inet alias && _ret=0
595 # add IPv6 addresses to the interface $if
602 if ! ipv6if $_if; then
606 ifalias ${_if} inet6 alias && _ret=0
607 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
608 ipv6_accept_rtadv_up ${_if} && _ret=0
614 # remove IPv4 addresses from the interface $if
617 local _if _ifs _ret inetList oldifs _inet
622 ifalias ${_if} inet -alias && _ret=0
624 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
628 for _inet in $inetList ; do
629 # get rid of extraneous line
630 [ -z "$_inet" ] && break
632 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
635 ${IFCONFIG_CMD} ${_if} ${_inet} delete
645 # remove IPv6 addresses from the interface $if
648 local _if _ifs _ret inetList oldifs _inet6
653 if ! ipv6if $_if; then
657 ipv6_accept_rtadv_down ${_if} && _ret=0
658 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
659 ifalias ${_if} inet6 -alias && _ret=0
661 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
665 for _inet6 in $inetList ; do
666 # get rid of extraneous line
667 [ -z "$_inet6" ] && break
669 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
672 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
681 # ifalias if af action
682 # Configure or remove aliases for network interface $if.
683 # It returns 0 if at least one alias was configured or
684 # removed, or 1 if there were none.
691 afexists $2 || return $_ret
695 ifalias_af_common $1 $2 $3 && _ret=0
702 # ifalias_expand_addr af action addr
703 # Expand address range ("N-M") specification in addr.
704 # "addr" must not include an address-family keyword.
705 # The results will include an address-family keyword.
707 ifalias_expand_addr()
710 afexists $1 || return
711 ifalias_expand_addr_$1 $2 $3
714 # ifalias_expand_addr_inet action addr
715 # Helper function for ifalias_expand_addr(). Handles IPv4.
717 ifalias_expand_addr_inet()
719 local _action _arg _cidr _cidr_addr
720 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
726 case $_action:$_arg in
727 *:*--*) return ;; # invalid
728 tmp:*) echo $_arg && return ;; # already expanded
729 tmp:*-*) _action="alias" ;; # to be expanded
730 *:*-*) ;; # to be expanded
731 *:*) echo inet $_arg && return ;; # already expanded
734 for _cidr in $_arg; do
737 # When subnet prefix length is not specified, use /32.
739 $_ipaddr) _plen=32 ;; # "/" character not found
743 IFS=. set -- $_ipaddr
753 _iphead="${_iphead}${_iphead:+.}${_c}"
756 _iptail="${_iptail}${_iptail:+.}${_c}"
764 # clear netmask when removing aliases
765 if [ "$_action" = "-alias" ]; then
770 while [ "$_ipcount" -le "$_iphigh" ]; do
771 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
772 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
773 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."
776 _ipcount=$(($_ipcount + 1))
778 # Forcibly set /32 for remaining aliases.
783 for _c in $_retstr; do
784 ifalias_expand_addr_inet $_action $_c
788 # ifalias_expand_addr_inet6 action addr
789 # Helper function for ifalias_expand_addr(). Handles IPv6.
791 ifalias_expand_addr_inet6()
793 local _action _arg _cidr _cidr_addr
794 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
801 case $_action:$_arg in
802 *:*--*) return ;; # invalid
803 tmp:*) echo $_arg && return ;;
804 tmp:*-*) _action="alias" ;;
806 *:*) echo inet6 $_arg && return ;;
809 for _cidr in $_arg; do
810 _ipaddr="${_cidr%%/*}"
813 case $_action:$_ipaddr:$_cidr in
814 -alias:*:*) unset _plen ;;
815 *:$_cidr:$_ipaddr) unset _plen ;;
818 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
819 # Handle !v4mapped && !v4compat addresses.
821 # The default prefix length is 64.
822 case $_ipaddr:$_cidr in
823 $_cidr:$_ipaddr) _plen="64" ;;
825 _ipleft=${_ipaddr%-*}
826 _ipright=${_ipaddr#*-}
827 _iplow=${_ipleft##*:}
828 _iphigh=${_ipright%%:*}
829 _ipleft=${_ipleft%:*}
830 _ipright=${_ipright#*:}
832 if [ "$_iphigh" = "$_ipright" ]; then
838 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
839 _iplow=$((0x$_iplow))
840 _iphigh=$((0x$_iphigh))
842 while [ $_ipcount -le $_iphigh ]; do
843 _r=`printf "%s:%04x%s%s" \
844 $_ipleft $_ipcount $_ipright \
846 _retstr="$_retstr $_r"
847 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
849 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."
852 _ipcount=$(($_ipcount + 1))
856 _retstr="${_ipaddr}${_plen:+/}${_plen}"
859 for _c in $_retstr; do
860 ifalias_expand_addr_inet6 $_action $_c
863 # v4mapped/v4compat should handle as an IPv4 alias
864 _ipv4part=${_ipaddr##*:}
866 # Adjust prefix length if any. If not, set the
867 # default prefix length as 32.
868 case $_ipaddr:$_cidr in
869 $_cidr:$_ipaddr) _plen=32 ;;
870 *) _plen=$(($_plen - 96)) ;;
873 _retstr=`ifalias_expand_addr_inet \
874 tmp ${_ipv4part}${_plen:+/}${_plen}`
875 for _c in $_retstr; do
876 ifalias_expand_addr_inet $_action $_c
882 # ifalias_af_common_handler if af action args
883 # Helper function for ifalias_af_common().
885 ifalias_af_common_handler()
887 local _ret _if _af _action _args _c _tmpargs
907 ifalias_af_common_handler $_if $_af $_action \
908 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
911 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
917 _tmpargs="$_tmpargs $_c"
921 # Process the last component if any.
922 if [ -n "$_tmpargs}" ]; then
925 ifalias_af_common_handler $_if $_af $_action \
926 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
929 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
937 # ifalias_af_common if af action
938 # Helper function for ifalias().
942 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
950 # ifconfig_IF_aliasN which starts with $_af
953 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
955 case $ifconfig_args in
956 inet\ *) _iaf=inet ;;
957 inet6\ *) _iaf=inet6 ;;
961 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
963 _aliasn="$_aliasn $ifconfig_args"
969 _aliasn="$_aliasn inet $ifconfig_args"
970 warn "\$ifconfig_${_if}_alias${alias} needs " \
971 "\"inet\" keyword for an IPv4 address."
973 alias=$(($alias + 1))
976 # backward compatibility: ipv6_ifconfig_IF_aliasN.
981 ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
982 case ${_action}:"${ifconfig_args}" in
987 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
988 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
989 "is obsolete. Use ifconfig_$1_aliasN " \
993 alias=$(($alias + 1))
997 # backward compatibility: ipv4_addrs_IF.
998 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
999 _aliasn="$_aliasn inet $_tmpargs"
1002 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1004 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1009 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1015 _tmpargs="$_tmpargs $_c"
1018 # Process the last component
1021 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1028 # ipv6_prefix_hostid_addr_common if action
1029 # Add or remove IPv6 prefix + hostid addr on the interface $if
1031 ipv6_prefix_hostid_addr_common()
1033 local _if _action prefix laddr hostid j address
1036 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1038 if [ -n "${prefix}" ]; then
1039 laddr=`network6_getladdr ${_if}`
1040 hostid=${laddr#fe80::}
1041 hostid=${hostid%\%*}
1043 for j in ${prefix}; do
1044 # The default prefixlen is 64.
1047 $plen:$j) plen=64 ;;
1051 # Normalize the last part by removing ":"
1054 OIFS=$IFS; IFS=":"; set -- $j; nj=$#; IFS=$OIFS
1055 OIFS=$IFS; IFS=":"; set -- $hostid; nh=$#; IFS=$OIFS
1056 if [ $(($nj + $nh)) -eq 8 ]; then
1059 address=$j\::$hostid
1062 ${IFCONFIG_CMD} ${_if} inet6 ${address} \
1063 prefixlen $plen ${_action}
1065 # if I am a router, add subnet router
1066 # anycast address (RFC 2373).
1067 if checkyesno ipv6_gateway_enable; then
1068 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1069 prefixlen $plen ${_action} anycast
1075 # ipv6_accept_rtadv_up if
1076 # Enable accepting Router Advertisement and send Router
1077 # Solicitation message
1078 ipv6_accept_rtadv_up()
1080 if ipv6_autoconfif $1; then
1081 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1082 if ! checkyesno rtsold_enable; then
1083 rtsol ${rtsol_flags} $1
1088 # ipv6_accept_rtadv_down if
1089 # Disable accepting Router Advertisement
1090 ipv6_accept_rtadv_down()
1092 if ipv6_autoconfif $1; then
1093 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1098 # Evaluate a startup script for the $if interface.
1099 # It returns 0 if a script was found and processed or
1100 # 1 if no script was found.
1104 if [ -r /etc/start_if.$1 ]; then
1113 # Evaluate a shutdown script for the $if interface.
1114 # It returns 0 if a script was found and processed or
1115 # 1 if no script was found.
1119 if [ -r /etc/stop_if.$1 ]; then
1128 # Create cloneable interfaces.
1132 local _prefix _list ifn
1137 for ifn in ${cloned_interfaces}; do
1138 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1139 if [ $? -eq 0 ]; then
1140 _list="${_list}${_prefix}${ifn}"
1141 [ -z "$_prefix" ] && _prefix=' '
1144 debug "Cloned: ${_list}"
1148 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1153 local _prefix _list ifn
1157 for ifn in ${cloned_interfaces}; do
1158 ${IFCONFIG_CMD} -n ${ifn} destroy
1159 if [ $? -eq 0 ]; then
1160 _list="${_list}${_prefix}${ifn}"
1161 [ -z "$_prefix" ] && _prefix=' '
1164 debug "Destroyed clones: ${_list}"
1168 # Create and configure child interfaces. Return 0 if child
1169 # interfaces are created.
1173 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1177 # Create wireless interfaces
1178 child_wlans=`get_if_var $ifn wlans_IF`
1180 for child in ${child_wlans}; do
1181 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1182 debug_flags="`get_if_var $child wlandebug_IF`"
1184 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1185 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1186 if [ -n "${debug_flags}" ]; then
1187 wlandebug -i $child ${debug_flags}
1190 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1191 if [ -n "${debug_flags}" ]; then
1192 wlandebug -i $i ${debug_flags}
1194 ${IFCONFIG_CMD} $i name $child && cfg=0
1196 if autoif $child; then
1201 # Create vlan interfaces
1202 child_vlans=`get_if_var $ifn vlans_IF`
1204 if [ -n "${child_vlans}" ]; then
1208 for child in ${child_vlans}; do
1209 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1210 child="${ifn}.${child}"
1211 create_args=`get_if_var $child create_args_IF`
1212 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1214 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1215 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1216 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1218 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1219 ${IFCONFIG_CMD} $i name $child && cfg=0
1222 if autoif $child; then
1231 # Destroy child interfaces.
1235 local cfg child child_vlans child_wlans ifn
1238 child_wlans=`get_if_var $ifn wlans_IF`
1239 for child in ${child_wlans}; do
1240 if ! ifexists $child; then
1243 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1246 child_vlans=`get_if_var $ifn vlans_IF`
1247 for child in ${child_vlans}; do
1248 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1249 child="${ifn}.${child}"
1251 if ! ifexists $child; then
1254 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1261 # Create netgraph nodes.
1265 ngctl -f - 2> /dev/null <<EOF
1272 # Create netgraph nodes.
1278 ng_mkpeer $* | while read line; do
1279 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1280 if [ -n "${t}" ]; then
1288 # Create gif(4) tunnel interfaces.
1293 for i in ${gif_interfaces}; do
1294 peers=`get_if_var $i gifconfig_IF`
1300 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1301 ${IFCONFIG_CMD} $i create >/dev/null 2>&1
1303 gif=`${IFCONFIG_CMD} gif create`
1304 ${IFCONFIG_CMD} $gif name $i
1306 ${IFCONFIG_CMD} $i tunnel ${peers}
1307 ${IFCONFIG_CMD} $i up
1314 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1315 # FEC arguments were found and configured; returns !0 otherwise.
1318 local req_iface iface bogus
1321 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1325 iface=`ng_create_one fec dummy fec`
1326 if [ -z "${iface}" ]; then
1329 if [ "${iface}" = "${req_iface}" ]; then
1332 bogus="${bogus} ${iface}"
1335 for iface in ${bogus}; do
1336 ngctl shutdown ${iface}:
1341 # Create Fast EtherChannel interfaces.
1346 for i in ${fec_interfaces}; do
1348 for j in `get_if_var $i fecconfig_IF`; do
1354 ngctl msg ${i}: add_iface "\"${j}\""
1362 # Configure any IPX addresses for interface $ifn. Returns 0 if
1363 # IPX arguments were found and configured; returns 1 otherwise.
1371 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1372 if [ -n "${ifconfig_args}" ]; then
1373 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1381 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1382 # addresses were found and unconfigured. It returns 1, otherwise.
1386 local _if _ifs _ret ipxList oldifs _ipx
1390 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1394 for _ipx in $ipxList ; do
1395 # get rid of extraneous line
1396 [ -z "$_ipx" ] && break
1398 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1401 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1411 # Rename all requested interfaces.
1418 for _if in `${IFCONFIG_CMD} -l`; do
1419 _ifname=`get_if_var $_if ifconfig_IF_name`
1420 if [ ! -z "$_ifname" ]; then
1421 ${IFCONFIG_CMD} $_if name $_ifname
1428 # list_net_interfaces type
1429 # List all network interfaces. The type of interface returned
1430 # can be controlled by the type argument. The type
1431 # argument can be any of the following:
1432 # nodhcp - all interfaces, excluding DHCP configured interfaces
1433 # dhcp - list only DHCP configured interfaces
1434 # noautoconf - all interfaces, excluding IPv6 Stateless
1435 # Address Autoconf configured interfaces
1436 # autoconf - list only IPv6 Stateless Address Autoconf
1437 # configured interfaces
1438 # If no argument is specified all network interfaces are output.
1439 # Note that the list will include cloned interfaces if applicable.
1440 # Cloned interfaces must already exist to have a chance to appear
1441 # in the list if ${network_interfaces} is set to `auto'.
1443 list_net_interfaces()
1445 local type _tmplist _list _autolist _lo _if
1448 # Get a list of ALL the interfaces and make lo0 first if it's there.
1451 case ${network_interfaces} in
1453 _autolist="`${IFCONFIG_CMD} -l`"
1455 for _if in ${_autolist} ; do
1456 if autoif $_if; then
1457 if [ "$_if" = "lo0" ]; then
1460 _tmplist="${_tmplist} ${_if}"
1464 _tmplist="${_lo}${_tmplist# }"
1467 _tmplist="${network_interfaces} ${cloned_interfaces}"
1469 # lo0 is effectively mandatory, so help prevent foot-shooting
1472 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1473 *) _tmplist="lo0 ${_tmplist}" ;;
1481 for _if in ${_tmplist} ; do
1482 if ! dhcpif $_if && \
1483 [ -n "`_ifconfig_getargs $_if`" ]; then
1484 _list="${_list# } ${_if}"
1489 for _if in ${_tmplist} ; do
1490 if dhcpif $_if; then
1491 _list="${_list# } ${_if}"
1496 for _if in ${_tmplist} ; do
1497 if ! ipv6_autoconfif $_if && \
1498 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1499 _list="${_list# } ${_if}"
1504 for _if in ${_tmplist} ; do
1505 if ipv6_autoconfif $_if; then
1506 _list="${_list# } ${_if}"
1520 # get_default_if -address_family
1521 # Get the interface of the default route for the given address family.
1522 # The -address_family argument must be suitable passing to route(8).
1526 local routeget oldifs defif line
1531 for line in `route -n get $1 default 2>/dev/null`; do
1544 # Echo decimal number $arg (single digit) in hexadecimal format.
1551 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1557 is_wired_interface()
1561 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1562 *media:?Ethernet*) media=Ethernet ;;
1565 test "$media" = "Ethernet"
1568 # network6_getladdr if [flag]
1569 # Echo link-local address from $if if any.
1570 # If flag is defined, tentative ones will be excluded.
1573 local _if _flag proto addr rest
1577 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1578 case "${proto}/${addr}/${_flag}/${rest}" in
1582 inet6/fe80:://*tentative*) # w/o flag
1583 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1584 network6_getladdr $_if $_flags
1586 inet6/fe80::/*/*tentative*) # w/ flag