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
25 # $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
29 # Subroutines commonly used from network startup scripts.
30 # Requires that /etc/rc.subr be loaded first.
34 # Evaluate ifconfig(8) arguments for interface $if and
35 # run ifconfig(8) with those arguments. It returns 0 if
36 # arguments were found and executed or 1 if the interface
37 # had no arguments. Pseudo arguments DHCP and WPA are handled
42 local _cfg ifconfig_args
45 ifconfig_args=`ifconfig_getargs $1`
46 if [ -n "${ifconfig_args}" ]; then
47 ifconfig $1 ${ifconfig_args}
54 /etc/rc.d/wpa_supplicant start $1
55 _cfg=0 # XXX: not sure this should count
59 /etc/rc.d/dhcp_client start $1
67 # Remove all inet entries from the $if interface. It returns
68 # 0 if inet entries were found and removed. It returns 1 if
69 # no entries were found or they could not be removed.
73 local _cfg _ifs oldifs _inet inetList
75 [ -z "$1" ] && return 1
79 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
83 for _inet in $inetList ; do
84 # get rid of extraneous line
85 [ -z "$_inet" ] && break
87 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
90 ifconfig $1 ${_inet} delete
97 /etc/rc.d/wpa_supplicant stop $1
101 /etc/rc.d/dhcp_client stop $1
113 # get_if_var if var [default]
114 # Return the value of the pseudo-hash corresponding to $if where
115 # $var is a string containg the sub-string "IF" which will be
116 # replaced with $if after the characters defined in _punct are
117 # replaced with '_'. If the variable is unset, replace it with
122 local _if _punct_c _punct _var _default prefix suffix
124 if [ $# -ne 2 -a $# -ne 3 ]; then
125 err 3 'USAGE: get_if_var name var [default]'
130 for _punct_c in $_punct; do
131 _if=`ltr ${_if} ${_punct_c} '_'`
138 eval echo \${${prefix}${_if}${suffix}-${_default}}
141 # _ifconfig_getargs if [af]
142 # Echos the arguments for the supplied interface to stdout.
143 # Returns 1 if no interface is specified.
144 # In general, the ifconfig_getargs() below should be used outside
149 local _if _ifn _af _args
154 if [ -z "$_ifn" ]; then
158 _args=`get_if_var $_ifn ${_af}ifconfig_IF`
159 if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
160 for _if in ${removable_interfaces} ; do
161 if [ "$_if" = "$_ifn" ] ; then
162 _args=${pccard_ifconfig}
171 # ifconfig_getargs if [af]
172 # Takes the result from _ifconfig_getargs() and removes pseudo
173 # args such as DHCP and WPA.
177 local _tmpargs _arg _args is_optarg
179 _tmpargs=`_ifconfig_getargs $1 $2`
180 if [ $? -eq 1 ]; then
186 for _arg in $_tmpargs; do
187 if [ "$is_optarg" = "no" ]; then
212 # Returns 0 if the interface should be configured for IPv6 and
220 # lo0 is always IPv6-enabled
221 if [ "$_if" = "lo0" ]; then
225 case ${ipv6_enable} in
226 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
231 case "${ipv6_network_interfaces}" in
232 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
233 # True if $ipv6_ifconfig_IF is defined.
234 _tmpargs=`_ifconfig_getargs $_if ipv6`
235 if [ -n "${_tmpargs}" ]; then
239 # True if $ipv6_prefix_IF is defined.
240 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
241 if [ -n "${_tmpargs}" ]; then
251 # dhcpif if [ipv4|ipv6]
252 # Returns 0 if the interface needs DHCP for IPv4/IPv6 and 1 otherwise.
253 # If the second argument is "ipv4" (or "ipv6"), then only IPv4 (or
254 # IPv6) is checked, otherwise both are checked.
258 local _tmpargs _arg _if _af
262 if [ -z "$_af" -o "$_af" = "ipv4" ]; then
263 _tmpargs=`_ifconfig_getargs $_if`
264 for _arg in $_tmpargs; do
273 if [ -z "$_af" -o "$_af" = "ipv6" ] && ipv6if $_if; then
274 _tmpargs=`_ifconfig_getargs $_if ipv6`
275 for _arg in $_tmpargs; do
288 # Returns 0 if the interface is a WPA interface and 1 otherwise.
292 local _tmpargs _arg is_optarg
294 _tmpargs=`_ifconfig_getargs $1`
296 for _arg in $_tmpargs; do
297 if [ "$is_optarg" = "no" ]; then
315 # Returns 0 if the interface exists and 1 otherwise.
319 [ -z "$1" ] && return 1
320 ifconfig -n $1 >/dev/null 2>&1
323 # ifalias_common if action [ipv6]
324 # Helper function for ifalias_up() and ifalias_down().
325 # The $action argument can be either "alias" (to add an
326 # alias) or "-alias" (to remove an alias).
327 # Returns 0 if at least one alias was added/removed or
328 # 1 if there were none.
332 local _if _action _af _af2 _ret _var _args _alias
340 if [ "${_af}" = "ipv6" ]; then
342 _var="ipv6_ifconfig_IF_alias${_alias}"
345 _var="ifconfig_IF_alias${_alias}"
347 _args=`get_if_var $_if $_var`
348 _args="${_args#${_af2} }"
349 if [ -z "${_args}" ]; then
352 ifconfig $_if $_af2 $_args $_action
353 _alias=$((${_alias} + 1))
359 # ifalias_up if [ipv6]
360 # Configure IPv4 aliases for network interface $if or
361 # IPv6 aliases if the second argument is "ipv6".
362 # It returns 0 if at least one alias was configured or
363 # 1 if there were none.
367 ifalias_common $1 alias $2
370 # ifalias_down if [ipv6]
371 # Remove IPv4 aliases for network interface $if or
372 # IPv6 aliases if the second argument is "ipv6".
373 # It returns 0 if at least one alias was removed or
374 # 1 if there were none.
378 ifalias_common $1 -alias $2
382 # Evaluate a startup script for the $if interface.
383 # It returns 0 if a script was found and processed or
384 # 1 if no script was found.
388 if [ -r /etc/start_if.$1 ]; then
396 # Evaluate a shutdown script for the $if interface.
397 # It returns 0 if a script was found and processed or
398 # 1 if no script was found.
402 if [ -r /etc/stop_if.$1 ]; then
410 # Create IEEE 802.11 interfaces.
414 local _prefix _list parent child child_wlans create_args debug_flags
418 for parent in `${SYSCTL_N} -q net.wlan.devices`; do
419 # Parse wlans_$parent="$child ..."
420 child_wlans=`get_if_var $parent wlans_IF`
421 for child in ${child_wlans}; do
422 if ifexists $child; then
426 create_args="wlandev $parent `get_if_var $child create_args_IF`"
427 debug_flags="`get_if_var $child wlandebug_IF`"
428 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
429 ifconfig $child create ${create_args}
431 ifconfig wlan create ${create_args} name $child
433 if [ $? -eq 0 ]; then
434 _list="${_list}${_prefix}${child}"
435 [ -z "$_prefix" ] && _prefix=' '
437 if [ -n "${debug_flags}" ]; then
438 wlandebug -i $child ${debug_flags}
443 if [ -n "${_list}" ]; then
444 echo "Created wlan interfaces: ${_list}"
446 debug "Created wlan interfaces: ${_list}"
450 # Destroy IEEE 802.11 interfaces.
454 local _prefix _list parent child child_wlans
458 for parent in `${SYSCTL_N} -q net.wlan.devices`; do
459 child_wlans=`get_if_var $parent wlans_IF`
460 for child in ${child_wlans}; do
461 if ! ifexists $child; then
465 ifconfig -n $child destroy
466 if [ $? -eq 0 ]; then
467 _list="${_list}${_prefix}${child}"
468 [ -z "$_prefix" ] && _prefix=' '
473 if [ -n "${_list}" ]; then
474 echo "Destroyed wlan interfaces: ${_list}"
476 debug "Destroyed wlan interfaces: ${_list}"
480 # Create cloneable interfaces.
484 local _prefix _list ifn
488 for ifn in ${cloned_interfaces}; do
489 ifconfig ${ifn} create
490 if [ $? -eq 0 ]; then
491 _list="${_list}${_prefix}${ifn}"
492 [ -z "$_prefix" ] && _prefix=' '
495 if [ -n "${_list}" ]; then
496 echo "Created clone interfaces: ${_list}"
498 debug "Created clone interfaces: ${_list}"
502 # Destroy cloned interfaces.
506 local _prefix _list ifn
510 for ifn in ${cloned_interfaces}; do
511 ifconfig ${ifn} destroy
512 if [ $? -eq 0 ]; then
513 _list="${_list}${_prefix}${ifn}"
514 [ -z "$_prefix" ] && _prefix=' '
517 if [ -n "${_list}" ]; then
518 echo "Destroyed clone interfaces: ${_list}"
520 debug "Destroyed clone interfaces: ${_list}"
524 # Create IPv6<-->IPv4 tunnels
529 case ${gif_interfaces} in
535 for _if in ${gif_interfaces}; do
536 eval _peers=\$gifconfig_${_if}
542 ifconfig $_if create >/dev/null 2>&1
543 ifconfig $_if tunnel ${_peers}
551 # Rename all requested interfaces.
555 local _ifn_list _if _ifname
557 _ifn_list=$(ifconfig -l)
558 [ -z "$_ifn_list" ] && return 0
560 for _if in ${_ifn_list} ; do
561 _ifname=`get_if_var $_if ifconfig_IF_name`
562 if [ -n "$_ifname" ]; then
563 ifconfig $_if name $_ifname
569 # list_net_interfaces
570 # List all network interfaces.
571 # Note that the list will include cloned interfaces if applicable.
572 # Cloned interfaces must already exist to have a chance to appear
573 # in the list if ${network_interfaces} is set to `auto'.
575 list_net_interfaces()
577 local _tmplist _autolist _lo _if
579 case ${network_interfaces} in
581 _autolist=$(ifconfig -l)
583 for _if in ${_autolist} ; do
584 if [ "$_if" = "lo0" ]; then
587 _tmplist="${_tmplist} ${_if}"
590 _tmplist="${_lo} ${_tmplist}"
593 _tmplist="${network_interfaces} ${cloned_interfaces}"
602 if [ $1 -lt 10 ]; then
622 dig=`hexdigit $((${val} & 15))`
625 while [ ${val} -gt 0 ]; do
626 dig=`hexdigit $((${val} & 15))`
636 # IPv6-specific setup subroutines
639 # Setup the interfaces for IPv6
640 network6_interface_setup()
642 local interfaces rtsol_interfaces ipv6_ifconfig
643 local rtsol_available rtsol_interface
644 local prefix laddr hostid address
649 case ${ipv6_gateway_enable} in
657 for _if in $interfaces; do
659 prefix=`get_if_var $_if ipv6_prefix_IF`
660 if [ -n "${prefix}" ]; then
663 laddr=`network6_getladdr $_if`
664 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
665 for j in ${prefix}; do
666 address=$j\:${hostid}
667 ifconfig $_if inet6 ${address} prefixlen 64 alias
669 case ${ipv6_gateway_enable} in
671 # subnet-router anycast address
673 ifconfig $_if inet6 $j:: prefixlen 64 \
679 ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
680 if [ -n "${ipv6_ifconfig}" ]; then
683 ifconfig $_if inet6 ${ipv6_ifconfig} alias
686 if [ "${rtsol_available}" = "yes" -a \
687 "${rtsol_interface}" = "yes" ]; then
689 lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
692 rtsol_interfaces="${rtsol_interfaces} ${_if}"
700 if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
701 # Act as endhost - automatically configured.
702 # You can configure only single interface, as
703 # specification assumes that autoconfigured host has
704 # single interface only.
705 ${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
706 set ${rtsol_interfaces}
708 echo "Auto configuring interface $1 ..."
712 for _if in $interfaces; do
717 # Setup IPv6 to IPv4 mapping
720 local stf_prefixlen stf_interface_ipv6_ifid
721 local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
724 case ${stf_interface_ipv4addr} in
728 # assign IPv6 addr and interface route for 6to4 interface
729 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
732 set ${stf_interface_ipv4addr}
734 hexfrag1=`hexprint $(($1*256 + $2))`
735 hexfrag2=`hexprint $(($3*256 + $4))`
736 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
737 case ${stf_interface_ipv6_ifid} in
738 [Aa][Uu][Tt][Oo] | '')
739 for _if in ${ipv6_network_interfaces}; do
740 laddr=`network6_getladdr $_if`
749 stf_interface_ipv6_ifid=`expr "${laddr}" : \
750 'fe80::\(.*\)%\(.*\)'`
751 case ${stf_interface_ipv6_ifid} in
753 stf_interface_ipv6_ifid=0:0:0:1
758 ifconfig stf0 create >/dev/null 2>&1
759 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
760 prefixlen ${stf_prefixlen}
761 # disallow packets to malicious 6to4 prefix
762 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
763 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
764 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
765 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
770 # Setup static routes
771 network6_static_routes_setup()
775 # Set up any static routes.
776 case ${ipv6_defaultrouter} in
780 ipv6_static_routes="default ${ipv6_static_routes}"
781 ipv6_route_default="default ${ipv6_defaultrouter}"
784 case ${ipv6_static_routes} in
788 for _rt in ${ipv6_static_routes}; do
789 eval ipv6_route_args=\$ipv6_route_${_rt}
790 route add -inet6 ${ipv6_route_args}
796 # Install the "default interface" to kernel, which will be used
797 # as the default route when there's no router.
798 network6_default_interface_setup()
802 # Choose IPv6 default interface if it is not clearly specified.
803 case ${ipv6_default_interface} in
805 for _if in ${ipv6_network_interfaces}; do
806 if [ "${_if}" = "lo0" ]; then
810 laddr=`network6_getladdr $_if exclude_tentative`
815 ipv6_default_interface=$_if
823 # Disallow unicast packets without outgoing scope identifiers,
824 # or route such packets to a "default" interface, if it is specified.
825 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
826 case ${ipv6_default_interface} in
828 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
831 laddr=`network6_getladdr ${ipv6_default_interface}`
832 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
835 # Disable installing the default interface with the
836 # case net.inet6.ip6.forwarding=0 and
837 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
838 # between the default router list and the manual
839 # configured default route.
840 case ${ipv6_gateway_enable} in
844 if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
846 ndp -I ${ipv6_default_interface}
856 local proto addr rest
858 ifconfig $1 2>/dev/null | while read proto addr rest; do