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 " \
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 for _punct_c in $_punct; do
290 _if=`ltr ${_if} ${_punct_c} '_'`
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`
388 for _arg in $_tmpargs; do
393 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
396 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
406 # Returns 0 if the interface should be configured synchronously and
411 _tmpargs=`_ifconfig_getargs $1`
417 for _arg in $_tmpargs; do
419 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
422 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
428 checkyesno synchronous_dhclient
432 # Returns 0 if the interface is a WPA interface and 1 otherwise.
436 _tmpargs=`_ifconfig_getargs $1`
438 for _arg in $_tmpargs; do
450 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
454 _tmpargs=`_ifconfig_getargs $1`
456 for _arg in $_tmpargs; do
458 [Hh][Oo][Ss][Tt][Aa][Pp])
468 # Returns 0 and echo jail if "vnet" keyword is specified on the
469 # interface, and 1 otherwise.
472 local _tmpargs _arg _vnet
473 _tmpargs=`_ifconfig_getargs $1`
476 for _arg in $_tmpargs; do
479 *:1) echo $_arg; return 0 ;;
487 # Returns 0 if the address family is enabled in the kernel
496 check_kern_features ${_af}
499 ${SYSCTL_N} net.ipx > /dev/null 2>&1
502 if [ -x /sbin/atmconfig ]; then
503 /sbin/atmconfig diag list > /dev/null 2>&1
512 err 1 "afexists(): Unsupported address family: $_af"
518 # Returns 0 if the interface has no af configuration and 1 otherwise.
546 # Returns 0 if the interface should be configured for IPv6 and
553 if ! afexists inet6; then
557 # lo0 is always IPv6-enabled
564 case "${ipv6_network_interfaces}" in
565 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
566 # True if $ifconfig_IF_ipv6 is defined.
567 _tmpargs=`_ifconfig_getargs $_if ipv6`
568 if [ -n "${_tmpargs}" ]; then
572 # True if $ipv6_prefix_IF is defined.
573 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
574 if [ -n "${_tmpargs}" ]; then
578 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
579 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
580 if [ -n "${_tmpargs}" ]; then
590 # Returns 0 if the interface should be configured for IPv6 with
591 # Stateless Address Configuration; 1 otherwise.
594 local _if _tmpargs _arg
609 if ! ipv6if $_if; then
612 if checkyesno ipv6_gateway_enable; then
615 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
616 if [ -n "${_tmpargs}" ]; then
619 # backward compatibility: $ipv6_enable
621 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
622 if checkyesno ipv6_gateway_enable; then
627 # No accept_rtadv by default on if_bridge(4)
628 # to avoid a conflict with the member
639 _tmpargs=`_ifconfig_getargs $_if ipv6`
640 for _arg in $_tmpargs; do
648 # backward compatibility: $ipv6_ifconfig_IF
649 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
650 for _arg in $_tmpargs; do
662 # Returns 0 if the interface exists and 1 otherwise.
665 [ -z "$1" ] && return 1
666 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
670 # add IPv4 addresses to the interface $if
677 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
678 if [ "${_if}" = "lo0" ]; then
679 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
680 if [ -z "${ifconfig_args}" ]; then
681 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
684 ifalias ${_if} inet alias && _ret=0
690 # add IPv6 addresses to the interface $if
697 if ! ipv6if $_if; then
701 ifalias ${_if} inet6 alias && _ret=0
702 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
703 ipv6_accept_rtadv_up ${_if} && _ret=0
709 # remove IPv4 addresses from the interface $if
712 local _if _ifs _ret inetList oldifs _inet
717 ifalias ${_if} inet -alias && _ret=0
719 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
723 for _inet in $inetList ; do
724 # get rid of extraneous line
730 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
733 ${IFCONFIG_CMD} ${_if} ${_inet} delete
743 # remove IPv6 addresses from the interface $if
746 local _if _ifs _ret inetList oldifs _inet6
751 if ! ipv6if $_if; then
755 ipv6_accept_rtadv_down ${_if} && _ret=0
756 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
757 ifalias ${_if} inet6 -alias && _ret=0
759 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
763 for _inet6 in $inetList ; do
764 # get rid of extraneous line
770 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
773 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
782 # ifalias if af action
783 # Configure or remove aliases for network interface $if.
784 # It returns 0 if at least one alias was configured or
785 # removed, or 1 if there were none.
792 afexists $2 || return $_ret
795 inet|inet6|link|ether)
796 ifalias_af_common $1 $2 $3 && _ret=0
803 # ifalias_expand_addr af action addr
804 # Expand address range ("N-M") specification in addr.
805 # "addr" must not include an address-family keyword.
806 # The results will include an address-family keyword.
808 ifalias_expand_addr()
816 afexists $_af || return
817 ifalias_expand_addr_$_af $_action $*
820 # ifalias_expand_addr_inet action addr
821 # Helper function for ifalias_expand_addr(). Handles IPv4.
823 ifalias_expand_addr_inet()
825 local _action _arg _cidr _cidr_addr _exargs
826 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
834 case $_action:$_arg:$_exargs in
835 *:*--*) return ;; # invalid
836 tmp:*[0-9]-[0-9]*:*) # to be expanded
839 *:*[0-9]-[0-9]*:*) # to be expanded
841 tmp:*:*netmask*) # already expanded w/ netmask option
842 echo ${_arg%/[0-9]*} $_exargs && return
844 tmp:*:*) # already expanded w/o netmask option
845 echo $_arg $_exargs && return
847 *:*:*netmask*) # already expanded w/ netmask option
848 echo inet ${_arg%/[0-9]*} $_exargs && return
850 *:*:*) # already expanded w/o netmask option
851 echo inet $_arg $_exargs && return
855 for _cidr in $_arg; do
858 # When subnet prefix length is not specified, use /32.
860 $_ipaddr) _plen=32 ;; # "/" character not found
864 IFS=. set -- $_ipaddr
874 _iphead="${_iphead}${_iphead:+.}${_c}"
877 _iptail="${_iptail}${_iptail:+.}${_c}"
885 # clear netmask when removing aliases
886 if [ "$_action" = "-alias" ]; then
891 while [ "$_ipcount" -le "$_iphigh" ]; do
892 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
893 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
894 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."
897 _ipcount=$(($_ipcount + 1))
899 # Forcibly set /32 for remaining aliases.
904 for _c in $_retstr; do
905 ifalias_expand_addr_inet $_action $_c $_exargs
909 # ifalias_expand_addr_inet6 action addr
910 # Helper function for ifalias_expand_addr(). Handles IPv6.
912 ifalias_expand_addr_inet6()
914 local _action _arg _cidr _cidr_addr _exargs
915 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
924 case $_action:$_arg:$_exargs in
925 *:*--*:*) return ;; # invalid
926 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
929 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
931 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
932 echo ${_arg%/[0-9]*} $_exargs && return
934 tmp:*:*) # already expanded w/o prefixlen option
935 echo $_arg $_exargs && return
937 *:*:*prefixlen*) # already expanded w/ prefixlen option
938 echo inet6 ${_arg%/[0-9]*} $_exargs && return
940 *:*:*) # already expanded w/o prefixlen option
941 echo inet6 $_arg $_exargs && return
945 for _cidr in $_arg; do
946 _ipaddr="${_cidr%%/*}"
949 case $_action:$_ipaddr:$_cidr in
950 -alias:*:*) unset _plen ;;
951 *:$_cidr:$_ipaddr) unset _plen ;;
954 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
955 # Handle !v4mapped && !v4compat addresses.
957 # The default prefix length is 64.
958 case $_ipaddr:$_cidr in
959 $_cidr:$_ipaddr) _plen="64" ;;
961 _ipleft=${_ipaddr%-*}
962 _ipright=${_ipaddr#*-}
963 _iplow=${_ipleft##*:}
964 _iphigh=${_ipright%%:*}
965 _ipleft=${_ipleft%:*}
966 _ipright=${_ipright#*:}
968 if [ "$_iphigh" = "$_ipright" ]; then
974 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
975 _iplow=$((0x$_iplow))
976 _iphigh=$((0x$_iphigh))
978 while [ $_ipcount -le $_iphigh ]; do
979 _r=`printf "%s:%04x%s%s" \
980 $_ipleft $_ipcount $_ipright \
982 _retstr="$_retstr $_r"
983 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
985 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."
988 _ipcount=$(($_ipcount + 1))
992 _retstr="${_ipaddr}${_plen:+/}${_plen}"
995 for _c in $_retstr; do
996 ifalias_expand_addr_inet6 $_action $_c $_exargs
999 # v4mapped/v4compat should handle as an IPv4 alias
1000 _ipv4part=${_ipaddr##*:}
1002 # Adjust prefix length if any. If not, set the
1003 # default prefix length as 32.
1004 case $_ipaddr:$_cidr in
1005 $_cidr:$_ipaddr) _plen=32 ;;
1006 *) _plen=$(($_plen - 96)) ;;
1009 _retstr=`ifalias_expand_addr_inet \
1010 tmp ${_ipv4part}${_plen:+/}${_plen}`
1011 for _c in $_retstr; do
1012 ifalias_expand_addr_inet $_action $_c $_exargs
1018 # ifalias_af_common_handler if af action args
1019 # Helper function for ifalias_af_common().
1021 ifalias_af_common_handler()
1023 local _ret _if _af _action _args _c _tmpargs
1037 # link(ether) does not support address removal.
1038 case $_af:$_action in
1039 link:-alias|ether:-alias) return ;;
1043 for _c in $_args; do
1048 ifalias_af_common_handler $_if $_af $_action \
1049 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1052 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1058 _tmpargs="$_tmpargs $_c"
1062 # Process the last component if any.
1063 if [ -n "$_tmpargs}" ]; then
1066 ifalias_af_common_handler $_if $_af $_action \
1067 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1070 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1078 # ifalias_af_common if af action
1079 # Helper function for ifalias().
1083 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1091 # ifconfig_IF_aliasN which starts with $_af
1094 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1096 case $ifconfig_args in
1097 inet\ *) _iaf=inet ;;
1098 inet6\ *) _iaf=inet6 ;;
1100 link\ *) _iaf=link ;;
1101 ether\ *) _iaf=ether ;;
1104 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1106 _aliasn="$_aliasn $ifconfig_args"
1112 _aliasn="$_aliasn inet $ifconfig_args"
1113 warn "\$ifconfig_${_if}_alias${alias} needs " \
1114 "\"inet\" keyword for an IPv4 address."
1116 alias=$(($alias + 1))
1119 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1124 ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1125 case ${_action}:"${ifconfig_args}" in
1130 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1131 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1132 "is obsolete. Use ifconfig_$1_aliasN " \
1136 alias=$(($alias + 1))
1140 # backward compatibility: ipv4_addrs_IF.
1141 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1142 _aliasn="$_aliasn inet $_tmpargs"
1145 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1147 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1149 inet|inet6|ipx|link|ether)
1152 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1158 _tmpargs="$_tmpargs $_c"
1161 # Process the last component
1164 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1171 # ipv6_prefix_hostid_addr_common if action
1172 # Add or remove IPv6 prefix + hostid addr on the interface $if
1174 ipv6_prefix_hostid_addr_common()
1176 local _if _action prefix j
1179 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1181 if [ -n "${prefix}" ]; then
1182 for j in ${prefix}; do
1183 # The default prefixlen is 64.
1186 $plen:$j) plen=64 ;;
1190 # Normalize the last part by removing ":"
1193 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1194 prefixlen $plen eui64 ${_action}
1196 # if I am a router, add subnet router
1197 # anycast address (RFC 2373).
1198 if checkyesno ipv6_gateway_enable; then
1199 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1200 prefixlen $plen ${_action} anycast
1206 # ipv6_accept_rtadv_up if
1207 # Enable accepting Router Advertisement and send Router
1208 # Solicitation message
1209 ipv6_accept_rtadv_up()
1211 if ipv6_autoconfif $1; then
1212 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1213 if ! checkyesno rtsold_enable; then
1214 rtsol ${rtsol_flags} $1
1219 # ipv6_accept_rtadv_down if
1220 # Disable accepting Router Advertisement
1221 ipv6_accept_rtadv_down()
1223 if ipv6_autoconfif $1; then
1224 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1229 # Evaluate a startup script for the $if interface.
1230 # It returns 0 if a script was found and processed or
1231 # 1 if no script was found.
1235 if [ -r /etc/start_if.$1 ]; then
1244 # Evaluate a shutdown script for the $if interface.
1245 # It returns 0 if a script was found and processed or
1246 # 1 if no script was found.
1250 if [ -r /etc/stop_if.$1 ]; then
1259 # Create cloneable interfaces.
1263 local _list ifn ifopt _iflist _n tmpargs
1268 for ifn in ${cloned_interfaces}; do
1270 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1272 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1277 # epair(4) uses epair[0-9] for creation and
1278 # epair[0-9][ab] for configuration.
1280 # Skip if ${ifn}a or ${ifn}b already exist.
1281 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1283 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1286 ${IFCONFIG_CMD} ${ifn} create \
1287 `get_if_var ${ifn} create_args_IF`
1288 if [ $? -eq 0 ]; then
1289 _list="$_list ${ifn}a ${ifn}b"
1293 # Skip if ${ifn} already exists.
1294 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1297 ${IFCONFIG_CMD} ${ifn} create \
1298 `get_if_var ${ifn} create_args_IF`
1299 if [ $? -eq 0 ]; then
1304 if [ -n "$gif_interfaces" ]; then
1305 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1307 for ifn in ${gif_interfaces}; do
1309 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1311 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1314 # Skip if ifn already exists.
1315 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1320 ${IFCONFIG_CMD} $ifn create
1323 _n=$(${IFCONFIG_CMD} gif create)
1324 ${IFCONFIG_CMD} $_n name $ifn
1327 if [ $? -eq 0 ]; then
1330 tmpargs=$(get_if_var $ifn gifconfig_IF)
1331 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1333 if [ -n "${_list# }" ]; then
1334 echo "Created clone interfaces: ${_list# }."
1336 debug "Cloned: ${_list# }"
1340 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1345 local _list ifn _difn ifopt _iflist _sticky
1349 : ${cloned_interfaces_sticky:=NO}
1350 if checkyesno cloned_interfaces_sticky; then
1355 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1357 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1358 case $ifopt:$_sticky in
1359 sticky:*) continue ;; # :sticky => not destroy
1360 nosticky:*) ;; # :nosticky => destroy
1361 *:1) continue ;; # global sticky knob == 1
1364 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1369 # Note: epair(4) uses epair[0-9] for removal and
1370 # epair[0-9][ab] for configuration.
1372 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1373 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1375 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1380 ${IFCONFIG_CMD} -n $_difn destroy
1381 if [ $? -eq 0 ]; then
1382 _list="$_list ${ifn}a ${ifn}b"
1386 # Skip if ifn does not exist.
1387 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1390 ${IFCONFIG_CMD} -n ${ifn} destroy
1391 if [ $? -eq 0 ]; then
1397 if [ -n "${_list# }" ]; then
1398 echo "Destroyed clone interfaces: ${_list# }."
1400 debug "Destroyed clones: ${_list# }"
1404 # Create and configure child interfaces. Return 0 if child
1405 # interfaces are created.
1409 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1413 # Create wireless interfaces
1414 child_wlans=`get_if_var $ifn wlans_IF`
1416 for child in ${child_wlans}; do
1417 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1418 debug_flags="`get_if_var $child wlandebug_IF`"
1420 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1421 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1422 if [ -n "${debug_flags}" ]; then
1423 wlandebug -i $child ${debug_flags}
1426 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1427 if [ -n "${debug_flags}" ]; then
1428 wlandebug -i $i ${debug_flags}
1430 ${IFCONFIG_CMD} $i name $child && cfg=0
1432 if autoif $child; then
1437 # Create vlan interfaces
1438 child_vlans=`get_if_var $ifn vlans_IF`
1440 if [ -n "${child_vlans}" ]; then
1444 for child in ${child_vlans}; do
1445 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1446 child="${ifn}.${child}"
1447 create_args=`get_if_var $child create_args_IF`
1448 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1450 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1451 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1452 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1454 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1455 ${IFCONFIG_CMD} $i name $child && cfg=0
1458 if autoif $child; then
1467 # Destroy child interfaces.
1471 local cfg child child_vlans child_wlans ifn
1474 child_wlans=`get_if_var $ifn wlans_IF`
1475 for child in ${child_wlans}; do
1476 if ! ifexists $child; then
1479 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1482 child_vlans=`get_if_var $ifn vlans_IF`
1483 for child in ${child_vlans}; do
1484 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1485 child="${ifn}.${child}"
1487 if ! ifexists $child; then
1490 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1497 # Create netgraph nodes.
1501 ngctl -f - 2> /dev/null <<EOF
1508 # Create netgraph nodes.
1514 ng_mkpeer $* | while read line; do
1515 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1516 if [ -n "${t}" ]; then
1524 # Configure any IPX addresses for interface $ifn. Returns 0 if
1525 # IPX arguments were found and configured; returns 1 otherwise.
1533 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1534 if [ -n "${ifconfig_args}" ]; then
1535 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1543 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1544 # addresses were found and unconfigured. It returns 1, otherwise.
1548 local _if _ifs _ret ipxList oldifs _ipx
1552 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1556 for _ipx in $ipxList ; do
1557 # get rid of extraneous line
1558 [ -z "$_ipx" ] && break
1560 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1563 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1572 # ifnet_rename [ifname]
1573 # Rename interfaces if ifconfig_IF_name is defined.
1580 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1581 _ifname=`get_if_var $_if ifconfig_IF_name`
1582 if [ ! -z "$_ifname" ]; then
1583 ${IFCONFIG_CMD} $_if name $_ifname
1590 # list_net_interfaces type
1591 # List all network interfaces. The type of interface returned
1592 # can be controlled by the type argument. The type
1593 # argument can be any of the following:
1594 # nodhcp - all interfaces, excluding DHCP configured interfaces
1595 # dhcp - list only DHCP configured interfaces
1596 # noautoconf - all interfaces, excluding IPv6 Stateless
1597 # Address Autoconf configured interfaces
1598 # autoconf - list only IPv6 Stateless Address Autoconf
1599 # configured interfaces
1600 # If no argument is specified all network interfaces are output.
1601 # Note that the list will include cloned interfaces if applicable.
1602 # Cloned interfaces must already exist to have a chance to appear
1603 # in the list if ${network_interfaces} is set to `auto'.
1605 list_net_interfaces()
1607 local type _tmplist _list _autolist _lo _if
1610 # Get a list of ALL the interfaces and make lo0 first if it's there.
1613 case ${network_interfaces} in
1615 _autolist="`${IFCONFIG_CMD} -l`"
1617 for _if in ${_autolist} ; do
1618 if autoif $_if; then
1619 if [ "$_if" = "lo0" ]; then
1622 _tmplist="${_tmplist} ${_if}"
1626 _tmplist="${_lo}${_tmplist# }"
1629 for _if in ${network_interfaces} ${cloned_interfaces}; do
1630 # epair(4) uses epair[0-9] for creation and
1631 # epair[0-9][ab] for configuration.
1634 _tmplist="$_tmplist ${_if}a ${_if}b"
1637 _tmplist="$_tmplist $_if"
1642 # lo0 is effectively mandatory, so help prevent foot-shooting
1645 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1646 # This is fine, do nothing
1647 _tmplist="${_tmplist# }"
1650 _tmplist="lo0 ${_tmplist# }"
1659 for _if in ${_tmplist} ; do
1660 if ! dhcpif $_if && \
1661 [ -n "`_ifconfig_getargs $_if`" ]; then
1662 _list="${_list# } ${_if}"
1667 for _if in ${_tmplist} ; do
1668 if dhcpif $_if; then
1669 _list="${_list# } ${_if}"
1674 for _if in ${_tmplist} ; do
1675 if ! ipv6_autoconfif $_if && \
1676 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1677 _list="${_list# } ${_if}"
1682 for _if in ${_tmplist} ; do
1683 if ipv6_autoconfif $_if; then
1684 _list="${_list# } ${_if}"
1698 # get_default_if -address_family
1699 # Get the interface of the default route for the given address family.
1700 # The -address_family argument must be suitable passing to route(8).
1704 local routeget oldifs defif line
1709 for line in `route -n get $1 default 2>/dev/null`; do
1722 # Echo decimal number $arg (single digit) in hexadecimal format.
1729 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1735 is_wired_interface()
1739 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1740 *media:?Ethernet*) media=Ethernet ;;
1743 test "$media" = "Ethernet"
1746 # network6_getladdr if [flag]
1747 # Echo link-local address from $if if any.
1748 # If flag is defined, tentative ones will be excluded.
1751 local _if _flag proto addr rest
1755 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1756 case "${proto}/${addr}/${_flag}/${rest}" in
1760 inet6/fe80:://*tentative*) # w/o flag
1761 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1762 network6_getladdr $_if $_flags
1764 inet6/fe80::/*/*tentative*) # w/ flag