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 # Evaluate ifconfig(8) arguments for interface $if and
87 # run ifconfig(8) with those arguments. It returns 0 if
88 # arguments were found and executed or 1 if the interface
89 # had no arguments. Pseudo arguments DHCP and WPA are handled
94 local _cfg _ipv6_opts ifconfig_args
97 # Make sure lo0 always comes up.
98 if [ "$1" = "lo0" ]; then
103 ifconfig_args=`ifconfig_getargs $1`
104 if [ -n "${ifconfig_args}" ]; then
105 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
110 if ! noafif $1 && afexists inet6; then
111 if checkyesno ipv6_activate_all_interfaces; then
112 _ipv6_opts="-ifdisabled"
113 elif [ "$1" != "lo0" ]; then
114 _ipv6_opts="ifdisabled"
117 # backward compatibility: $ipv6_enable
119 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
122 # No accept_rtadv by default on if_bridge(4)
123 # to avoid a conflict with the member
127 if ! checkyesno ipv6_gateway_enable; then
128 _ipv6_opts="${_ipv6_opts} accept_rtadv"
135 case $ipv6_cpe_wanif in
137 _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
141 if [ -n "${_ipv6_opts}" ]; then
142 ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
146 ifconfig_args=`ifconfig_getargs $1 ipv6`
147 if [ -n "${ifconfig_args}" ]; then
148 # backward compatibility: inet6 keyword
149 case "${ifconfig_args}" in
151 warn "\$ifconfig_$1_ipv6 needs " \
152 "\"inet6\" keyword for an IPv6 address."
153 ifconfig_args="inet6 ${ifconfig_args}"
156 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
157 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
161 # $ipv6_prefix_IF will be handled in
162 # ipv6_prefix_hostid_addr_common().
163 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
164 if [ -n "${ifconfig_args}" ]; then
165 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
169 # backward compatibility: $ipv6_ifconfig_IF
170 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
171 if [ -n "${ifconfig_args}" ]; then
172 warn "\$ipv6_ifconfig_$1 is obsolete." \
173 " Use ifconfig_$1_ipv6 instead."
174 ${IFCONFIG_CMD} $1 inet6 -ifdisabled
175 eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
180 ifalias $1 link alias
181 ifalias $1 ether alias
183 if [ ${_cfg} -eq 0 ]; then
184 ${IFCONFIG_CMD} $1 up
188 /etc/rc.d/wpa_supplicant start $1
189 _cfg=0 # XXX: not sure this should count
190 elif hostapif $1; then
191 /etc/rc.d/hostapd start $1
196 if [ $_cfg -ne 0 ] ; then
197 ${IFCONFIG_CMD} $1 up
199 if syncdhcpif $1; then
200 /etc/rc.d/dhclient start $1
209 # returns 1 if wpa_supplicant or dhclient was stopped or
210 # the interface exists.
218 /etc/rc.d/wpa_supplicant stop $1
220 elif hostapif $1; then
221 /etc/rc.d/hostapd stop $1
226 /etc/rc.d/dhclient stop $1
231 ${IFCONFIG_CMD} $1 down
238 # get_if_var if var [default]
239 # Return the value of the pseudo-hash corresponding to $if where
240 # $var is a string containg the sub-string "IF" which will be
241 # replaced with $if after the characters defined in _punct are
242 # replaced with '_'. If the variable is unset, replace it with
246 local _if _punct _punct_c _var _default prefix suffix
248 if [ $# -ne 2 -a $# -ne 3 ]; then
249 err 3 'USAGE: get_if_var name var [default]'
254 for _punct_c in $_punct; do
255 _if=`ltr ${_if} ${_punct_c} '_'`
262 eval echo \${${prefix}${_if}${suffix}-${_default}}
265 # _ifconfig_getargs if [af]
266 # Prints the arguments for the supplied interface to stdout.
267 # Returns 1 if empty. In general, ifconfig_getargs should be used
275 if [ -z "$_ifn" ]; then
279 get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
282 # ifconfig_getargs if [af]
283 # Takes the result from _ifconfig_getargs and removes pseudo
284 # args such as DHCP and WPA.
287 local _tmpargs _arg _args
288 _tmpargs=`_ifconfig_getargs $1 $2`
289 if [ $? -eq 1 ]; then
294 for _arg in $_tmpargs; do
297 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
298 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
299 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
301 [Hh][Oo][Ss][Tt][Aa][Pp]) ;;
312 # Returns 0 if the interface should be automatically configured at
313 # boot time and 1 otherwise.
317 _tmpargs=`_ifconfig_getargs $1`
319 for _arg in $_tmpargs; do
321 [Nn][Oo][Aa][Uu][Tt][Oo])
331 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
335 _tmpargs=`_ifconfig_getargs $1`
350 for _arg in $_tmpargs; do
355 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
358 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
368 # Returns 0 if the interface should be configured synchronously and
373 _tmpargs=`_ifconfig_getargs $1`
379 for _arg in $_tmpargs; do
381 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
384 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
390 checkyesno synchronous_dhclient
394 # Returns 0 if the interface is a WPA interface and 1 otherwise.
398 _tmpargs=`_ifconfig_getargs $1`
400 for _arg in $_tmpargs; do
412 # Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
416 _tmpargs=`_ifconfig_getargs $1`
418 for _arg in $_tmpargs; do
420 [Hh][Oo][Ss][Tt][Aa][Pp])
430 # Returns 0 if the address family is enabled in the kernel
439 check_kern_features ${_af}
442 ${SYSCTL_N} net.ipx > /dev/null 2>&1
445 if [ -x /sbin/atmconfig ]; then
446 /sbin/atmconfig diag list > /dev/null 2>&1
455 err 1 "afexists(): Unsupported address family: $_af"
461 # Returns 0 if the interface has no af configuration and 1 otherwise.
489 # Returns 0 if the interface should be configured for IPv6 and
496 if ! afexists inet6; then
500 # lo0 is always IPv6-enabled
507 case "${ipv6_network_interfaces}" in
508 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
509 # True if $ifconfig_IF_ipv6 is defined.
510 _tmpargs=`_ifconfig_getargs $_if ipv6`
511 if [ -n "${_tmpargs}" ]; then
515 # True if $ipv6_prefix_IF is defined.
516 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
517 if [ -n "${_tmpargs}" ]; then
521 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
522 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
523 if [ -n "${_tmpargs}" ]; then
533 # Returns 0 if the interface should be configured for IPv6 with
534 # Stateless Address Configuration; 1 otherwise.
537 local _if _tmpargs _arg
552 if ! ipv6if $_if; then
555 if checkyesno ipv6_gateway_enable; then
558 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
559 if [ -n "${_tmpargs}" ]; then
562 # backward compatibility: $ipv6_enable
564 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
565 if checkyesno ipv6_gateway_enable; then
570 # No accept_rtadv by default on if_bridge(4)
571 # to avoid a conflict with the member
582 _tmpargs=`_ifconfig_getargs $_if ipv6`
583 for _arg in $_tmpargs; do
591 # backward compatibility: $ipv6_ifconfig_IF
592 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
593 for _arg in $_tmpargs; do
605 # Returns 0 if the interface exists and 1 otherwise.
608 [ -z "$1" ] && return 1
609 ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
613 # add IPv4 addresses to the interface $if
620 # Add 127.0.0.1/8 to lo0 unless otherwise specified.
621 if [ "${_if}" = "lo0" ]; then
622 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
623 if [ -z "${ifconfig_args}" ]; then
624 ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
627 ifalias ${_if} inet alias && _ret=0
633 # add IPv6 addresses to the interface $if
640 if ! ipv6if $_if; then
644 ifalias ${_if} inet6 alias && _ret=0
645 ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
646 ipv6_accept_rtadv_up ${_if} && _ret=0
652 # remove IPv4 addresses from the interface $if
655 local _if _ifs _ret inetList oldifs _inet
660 ifalias ${_if} inet -alias && _ret=0
662 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
666 for _inet in $inetList ; do
667 # get rid of extraneous line
673 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
676 ${IFCONFIG_CMD} ${_if} ${_inet} delete
686 # remove IPv6 addresses from the interface $if
689 local _if _ifs _ret inetList oldifs _inet6
694 if ! ipv6if $_if; then
698 ipv6_accept_rtadv_down ${_if} && _ret=0
699 ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
700 ifalias ${_if} inet6 -alias && _ret=0
702 inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
706 for _inet6 in $inetList ; do
707 # get rid of extraneous line
713 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
716 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
725 # ifalias if af action
726 # Configure or remove aliases for network interface $if.
727 # It returns 0 if at least one alias was configured or
728 # removed, or 1 if there were none.
735 afexists $2 || return $_ret
738 inet|inet6|link|ether)
739 ifalias_af_common $1 $2 $3 && _ret=0
746 # ifalias_expand_addr af action addr
747 # Expand address range ("N-M") specification in addr.
748 # "addr" must not include an address-family keyword.
749 # The results will include an address-family keyword.
751 ifalias_expand_addr()
759 afexists $_af || return
760 ifalias_expand_addr_$_af $_action $*
763 # ifalias_expand_addr_inet action addr
764 # Helper function for ifalias_expand_addr(). Handles IPv4.
766 ifalias_expand_addr_inet()
768 local _action _arg _cidr _cidr_addr _exargs
769 local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
777 case $_action:$_arg:$_exargs in
778 *:*--*) return ;; # invalid
779 tmp:*[0-9]-[0-9]*:*) # to be expanded
782 *:*[0-9]-[0-9]*:*) # to be expanded
784 tmp:*:*netmask*) # already expanded w/ netmask option
785 echo ${_arg%/[0-9]*} $_exargs && return
787 tmp:*:*) # already expanded w/o netmask option
788 echo $_arg $_exargs && return
790 *:*:*netmask*) # already expanded w/ netmask option
791 echo inet ${_arg%/[0-9]*} $_exargs && return
793 *:*:*) # already expanded w/o netmask option
794 echo inet $_arg $_exargs && return
798 for _cidr in $_arg; do
801 # When subnet prefix length is not specified, use /32.
803 $_ipaddr) _plen=32 ;; # "/" character not found
807 IFS=. set -- $_ipaddr
817 _iphead="${_iphead}${_iphead:+.}${_c}"
820 _iptail="${_iptail}${_iptail:+.}${_c}"
828 # clear netmask when removing aliases
829 if [ "$_action" = "-alias" ]; then
834 while [ "$_ipcount" -le "$_iphigh" ]; do
835 _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
836 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
837 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."
840 _ipcount=$(($_ipcount + 1))
842 # Forcibly set /32 for remaining aliases.
847 for _c in $_retstr; do
848 ifalias_expand_addr_inet $_action $_c $_exargs
852 # ifalias_expand_addr_inet6 action addr
853 # Helper function for ifalias_expand_addr(). Handles IPv6.
855 ifalias_expand_addr_inet6()
857 local _action _arg _cidr _cidr_addr _exargs
858 local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
867 case $_action:$_arg:$_exargs in
868 *:*--*:*) return ;; # invalid
869 tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
872 *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*) # to be expanded
874 tmp:*:*prefixlen*) # already expanded w/ prefixlen option
875 echo ${_arg%/[0-9]*} $_exargs && return
877 tmp:*:*) # already expanded w/o prefixlen option
878 echo $_arg $_exargs && return
880 *:*:*prefixlen*) # already expanded w/ prefixlen option
881 echo inet6 ${_arg%/[0-9]*} $_exargs && return
883 *:*:*) # already expanded w/o prefixlen option
884 echo inet6 $_arg $_exargs && return
888 for _cidr in $_arg; do
889 _ipaddr="${_cidr%%/*}"
892 case $_action:$_ipaddr:$_cidr in
893 -alias:*:*) unset _plen ;;
894 *:$_cidr:$_ipaddr) unset _plen ;;
897 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
898 # Handle !v4mapped && !v4compat addresses.
900 # The default prefix length is 64.
901 case $_ipaddr:$_cidr in
902 $_cidr:$_ipaddr) _plen="64" ;;
904 _ipleft=${_ipaddr%-*}
905 _ipright=${_ipaddr#*-}
906 _iplow=${_ipleft##*:}
907 _iphigh=${_ipright%%:*}
908 _ipleft=${_ipleft%:*}
909 _ipright=${_ipright#*:}
911 if [ "$_iphigh" = "$_ipright" ]; then
917 if [ -n "$_iplow" -a -n "$_iphigh" ]; then
918 _iplow=$((0x$_iplow))
919 _iphigh=$((0x$_iphigh))
921 while [ $_ipcount -le $_iphigh ]; do
922 _r=`printf "%s:%04x%s%s" \
923 $_ipleft $_ipcount $_ipright \
925 _retstr="$_retstr $_r"
926 if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
928 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."
931 _ipcount=$(($_ipcount + 1))
935 _retstr="${_ipaddr}${_plen:+/}${_plen}"
938 for _c in $_retstr; do
939 ifalias_expand_addr_inet6 $_action $_c $_exargs
942 # v4mapped/v4compat should handle as an IPv4 alias
943 _ipv4part=${_ipaddr##*:}
945 # Adjust prefix length if any. If not, set the
946 # default prefix length as 32.
947 case $_ipaddr:$_cidr in
948 $_cidr:$_ipaddr) _plen=32 ;;
949 *) _plen=$(($_plen - 96)) ;;
952 _retstr=`ifalias_expand_addr_inet \
953 tmp ${_ipv4part}${_plen:+/}${_plen}`
954 for _c in $_retstr; do
955 ifalias_expand_addr_inet $_action $_c $_exargs
961 # ifalias_af_common_handler if af action args
962 # Helper function for ifalias_af_common().
964 ifalias_af_common_handler()
966 local _ret _if _af _action _args _c _tmpargs
980 # link(ether) does not support address removal.
981 case $_af:$_action in
982 link:-alias|ether:-alias) return ;;
991 ifalias_af_common_handler $_if $_af $_action \
992 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
995 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1001 _tmpargs="$_tmpargs $_c"
1005 # Process the last component if any.
1006 if [ -n "$_tmpargs}" ]; then
1009 ifalias_af_common_handler $_if $_af $_action \
1010 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1013 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1021 # ifalias_af_common if af action
1022 # Helper function for ifalias().
1026 local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1034 # ifconfig_IF_aliasN which starts with $_af
1037 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1039 case $ifconfig_args in
1040 inet\ *) _iaf=inet ;;
1041 inet6\ *) _iaf=inet6 ;;
1043 link\ *) _iaf=link ;;
1044 ether\ *) _iaf=ether ;;
1047 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1049 _aliasn="$_aliasn $ifconfig_args"
1055 _aliasn="$_aliasn inet $ifconfig_args"
1056 warn "\$ifconfig_${_if}_alias${alias} needs " \
1057 "\"inet\" keyword for an IPv4 address."
1059 alias=$(($alias + 1))
1062 # backward compatibility: ipv6_ifconfig_IF_aliasN.
1067 ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1068 case ${_action}:"${ifconfig_args}" in
1073 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1074 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1075 "is obsolete. Use ifconfig_$1_aliasN " \
1079 alias=$(($alias + 1))
1083 # backward compatibility: ipv4_addrs_IF.
1084 for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1085 _aliasn="$_aliasn inet $_tmpargs"
1088 # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1090 for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1092 inet|inet6|ipx|link|ether)
1095 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1101 _tmpargs="$_tmpargs $_c"
1104 # Process the last component
1107 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1114 # ipv6_prefix_hostid_addr_common if action
1115 # Add or remove IPv6 prefix + hostid addr on the interface $if
1117 ipv6_prefix_hostid_addr_common()
1119 local _if _action prefix j
1122 prefix=`get_if_var ${_if} ipv6_prefix_IF`
1124 if [ -n "${prefix}" ]; then
1125 for j in ${prefix}; do
1126 # The default prefixlen is 64.
1129 $plen:$j) plen=64 ;;
1133 # Normalize the last part by removing ":"
1136 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1137 prefixlen $plen eui64 ${_action}
1139 # if I am a router, add subnet router
1140 # anycast address (RFC 2373).
1141 if checkyesno ipv6_gateway_enable; then
1142 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1143 prefixlen $plen ${_action} anycast
1149 # ipv6_accept_rtadv_up if
1150 # Enable accepting Router Advertisement and send Router
1151 # Solicitation message
1152 ipv6_accept_rtadv_up()
1154 if ipv6_autoconfif $1; then
1155 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1156 if ! checkyesno rtsold_enable; then
1157 rtsol ${rtsol_flags} $1
1162 # ipv6_accept_rtadv_down if
1163 # Disable accepting Router Advertisement
1164 ipv6_accept_rtadv_down()
1166 if ipv6_autoconfif $1; then
1167 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1172 # Evaluate a startup script for the $if interface.
1173 # It returns 0 if a script was found and processed or
1174 # 1 if no script was found.
1178 if [ -r /etc/start_if.$1 ]; then
1187 # Evaluate a shutdown script for the $if interface.
1188 # It returns 0 if a script was found and processed or
1189 # 1 if no script was found.
1193 if [ -r /etc/stop_if.$1 ]; then
1202 # Create cloneable interfaces.
1206 local _list ifn ifopt _iflist _n tmpargs
1211 for ifn in ${cloned_interfaces}; do
1213 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1215 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1220 # epair(4) uses epair[0-9] for creation and
1221 # epair[0-9][ab] for configuration.
1223 # Skip if ${ifn}a or ${ifn}b already exist.
1224 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1226 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1229 ${IFCONFIG_CMD} ${ifn} create \
1230 `get_if_var ${ifn} create_args_IF`
1231 if [ $? -eq 0 ]; then
1232 _list="$_list ${ifn}a ${ifn}b"
1236 # Skip if ${ifn} already exists.
1237 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1240 ${IFCONFIG_CMD} ${ifn} create \
1241 `get_if_var ${ifn} create_args_IF`
1242 if [ $? -eq 0 ]; then
1247 if [ -n "$gif_interfaces" ]; then
1248 warn "\$gif_interfaces is obsolete. Use \$cloned_interfaces instead."
1250 for ifn in ${gif_interfaces}; do
1252 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1254 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1257 # Skip if ifn already exists.
1258 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1263 ${IFCONFIG_CMD} $ifn create
1266 _n=$(${IFCONFIG_CMD} gif create)
1267 ${IFCONFIG_CMD} $_n name $ifn
1270 if [ $? -eq 0 ]; then
1273 tmpargs=$(get_if_var $ifn gifconfig_IF)
1274 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1276 if [ -n "${_list# }" ]; then
1277 echo "Created clone interfaces: ${_list# }."
1279 debug "Cloned: ${_list# }"
1283 # Destroy cloned interfaces. Destroyed interfaces are echoed to
1288 local _list ifn _difn ifopt _iflist _sticky
1292 : ${cloned_interfaces_sticky:=NO}
1293 if checkyesno cloned_interfaces_sticky; then
1298 for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1300 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1301 case $ifopt:$_sticky in
1302 sticky:*) continue ;; # :sticky => not destroy
1303 nosticky:*) ;; # :nosticky => destroy
1304 *:1) continue ;; # global sticky knob == 1
1307 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn) ;;
1312 # Note: epair(4) uses epair[0-9] for removal and
1313 # epair[0-9][ab] for configuration.
1315 # Skip if both of ${ifn}a and ${ifn}b do not exist.
1316 if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1318 elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1323 ${IFCONFIG_CMD} -n $_difn destroy
1324 if [ $? -eq 0 ]; then
1325 _list="$_list ${ifn}a ${ifn}b"
1329 # Skip if ifn does not exist.
1330 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1333 ${IFCONFIG_CMD} -n ${ifn} destroy
1334 if [ $? -eq 0 ]; then
1340 if [ -n "${_list# }" ]; then
1341 echo "Destroyed clone interfaces: ${_list# }."
1343 debug "Destroyed clones: ${_list# }"
1347 # Create and configure child interfaces. Return 0 if child
1348 # interfaces are created.
1352 local cfg child child_vlans child_wlans create_args debug_flags ifn i
1356 # Create wireless interfaces
1357 child_wlans=`get_if_var $ifn wlans_IF`
1359 for child in ${child_wlans}; do
1360 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1361 debug_flags="`get_if_var $child wlandebug_IF`"
1363 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1364 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1365 if [ -n "${debug_flags}" ]; then
1366 wlandebug -i $child ${debug_flags}
1369 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1370 if [ -n "${debug_flags}" ]; then
1371 wlandebug -i $i ${debug_flags}
1373 ${IFCONFIG_CMD} $i name $child && cfg=0
1375 if autoif $child; then
1380 # Create vlan interfaces
1381 child_vlans=`get_if_var $ifn vlans_IF`
1383 if [ -n "${child_vlans}" ]; then
1387 for child in ${child_vlans}; do
1388 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1389 child="${ifn}.${child}"
1390 create_args=`get_if_var $child create_args_IF`
1391 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1393 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1394 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1395 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1397 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1398 ${IFCONFIG_CMD} $i name $child && cfg=0
1401 if autoif $child; then
1410 # Destroy child interfaces.
1414 local cfg child child_vlans child_wlans ifn
1417 child_wlans=`get_if_var $ifn wlans_IF`
1418 for child in ${child_wlans}; do
1419 if ! ifexists $child; then
1422 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1425 child_vlans=`get_if_var $ifn vlans_IF`
1426 for child in ${child_vlans}; do
1427 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1428 child="${ifn}.${child}"
1430 if ! ifexists $child; then
1433 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1440 # Create netgraph nodes.
1444 ngctl -f - 2> /dev/null <<EOF
1451 # Create netgraph nodes.
1457 ng_mkpeer $* | while read line; do
1458 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1459 if [ -n "${t}" ]; then
1467 # Configure Fast EtherChannel for interface $ifn. Returns 0 if
1468 # FEC arguments were found and configured; returns !0 otherwise.
1471 local req_iface iface bogus
1474 ngctl shutdown ${req_iface}: > /dev/null 2>&1
1478 iface=`ng_create_one fec dummy fec`
1479 if [ -z "${iface}" ]; then
1482 if [ "${iface}" = "${req_iface}" ]; then
1485 bogus="${bogus} ${iface}"
1488 for iface in ${bogus}; do
1489 ngctl shutdown ${iface}:
1494 # Create Fast EtherChannel interfaces.
1499 for i in ${fec_interfaces}; do
1501 for j in `get_if_var $i fecconfig_IF`; do
1507 ngctl msg ${i}: add_iface "\"${j}\""
1515 # Configure any IPX addresses for interface $ifn. Returns 0 if
1516 # IPX arguments were found and configured; returns 1 otherwise.
1524 ifconfig_args=`_ifconfig_getargs $ifn ipx`
1525 if [ -n "${ifconfig_args}" ]; then
1526 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1534 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
1535 # addresses were found and unconfigured. It returns 1, otherwise.
1539 local _if _ifs _ret ipxList oldifs _ipx
1543 ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1547 for _ipx in $ipxList ; do
1548 # get rid of extraneous line
1549 [ -z "$_ipx" ] && break
1551 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1554 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1563 # ifnet_rename [ifname]
1564 # Rename interfaces if ifconfig_IF_name is defined.
1571 for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1572 _ifname=`get_if_var $_if ifconfig_IF_name`
1573 if [ ! -z "$_ifname" ]; then
1574 ${IFCONFIG_CMD} $_if name $_ifname
1581 # list_net_interfaces type
1582 # List all network interfaces. The type of interface returned
1583 # can be controlled by the type argument. The type
1584 # argument can be any of the following:
1585 # nodhcp - all interfaces, excluding DHCP configured interfaces
1586 # dhcp - list only DHCP configured interfaces
1587 # noautoconf - all interfaces, excluding IPv6 Stateless
1588 # Address Autoconf configured interfaces
1589 # autoconf - list only IPv6 Stateless Address Autoconf
1590 # configured interfaces
1591 # If no argument is specified all network interfaces are output.
1592 # Note that the list will include cloned interfaces if applicable.
1593 # Cloned interfaces must already exist to have a chance to appear
1594 # in the list if ${network_interfaces} is set to `auto'.
1596 list_net_interfaces()
1598 local type _tmplist _list _autolist _lo _if
1601 # Get a list of ALL the interfaces and make lo0 first if it's there.
1604 case ${network_interfaces} in
1606 _autolist="`${IFCONFIG_CMD} -l`"
1608 for _if in ${_autolist} ; do
1609 if autoif $_if; then
1610 if [ "$_if" = "lo0" ]; then
1613 _tmplist="${_tmplist} ${_if}"
1617 _tmplist="${_lo}${_tmplist# }"
1620 for _if in ${network_interfaces} ${cloned_interfaces}; do
1621 # epair(4) uses epair[0-9] for creation and
1622 # epair[0-9][ab] for configuration.
1625 _tmplist="$_tmplist ${_if}a ${_if}b"
1628 _tmplist="$_tmplist $_if"
1633 # lo0 is effectively mandatory, so help prevent foot-shooting
1636 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1637 # This is fine, do nothing
1638 _tmplist="${_tmplist# }"
1641 _tmplist="lo0 ${_tmplist# }"
1650 for _if in ${_tmplist} ; do
1651 if ! dhcpif $_if && \
1652 [ -n "`_ifconfig_getargs $_if`" ]; then
1653 _list="${_list# } ${_if}"
1658 for _if in ${_tmplist} ; do
1659 if dhcpif $_if; then
1660 _list="${_list# } ${_if}"
1665 for _if in ${_tmplist} ; do
1666 if ! ipv6_autoconfif $_if && \
1667 [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1668 _list="${_list# } ${_if}"
1673 for _if in ${_tmplist} ; do
1674 if ipv6_autoconfif $_if; then
1675 _list="${_list# } ${_if}"
1689 # get_default_if -address_family
1690 # Get the interface of the default route for the given address family.
1691 # The -address_family argument must be suitable passing to route(8).
1695 local routeget oldifs defif line
1700 for line in `route -n get $1 default 2>/dev/null`; do
1713 # Echo decimal number $arg (single digit) in hexadecimal format.
1720 # Echo decimal number $arg (multiple digits) in hexadecimal format.
1726 is_wired_interface()
1730 case `${IFCONFIG_CMD} $1 2>/dev/null` in
1731 *media:?Ethernet*) media=Ethernet ;;
1734 test "$media" = "Ethernet"
1737 # network6_getladdr if [flag]
1738 # Echo link-local address from $if if any.
1739 # If flag is defined, tentative ones will be excluded.
1742 local _if _flag proto addr rest
1746 ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1747 case "${proto}/${addr}/${_flag}/${rest}" in
1751 inet6/fe80:://*tentative*) # w/o flag
1752 sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1753 network6_getladdr $_if $_flags
1755 inet6/fe80::/*/*tentative*) # w/ flag