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}
52 /etc/rc.d/wpa_supplicant start $1
53 _cfg=0 # XXX: not sure this should count
57 /etc/rc.d/dhcp_client start $1
65 # Remove all inet entries from the $if interface. It returns
66 # 0 if inet entries were found and removed. It returns 1 if
67 # no entries were found or they could not be removed.
71 local _cfg _ifs oldifs _inet inetList
73 [ -z "$1" ] && return 1
77 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
81 for _inet in $inetList ; do
82 # get rid of extraneous line
83 [ -z "$_inet" ] && break
85 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
88 ifconfig $1 ${_inet} delete
95 /etc/rc.d/wpa_supplicant stop $1
99 /etc/rc.d/dhcp_client stop $1
106 # get_if_var if var [default]
107 # Return the value of the pseudo-hash corresponding to $if where
108 # $var is a string containg the sub-string "IF" which will be
109 # replaced with $if after the characters defined in _punct are
110 # replaced with '_'. If the variable is unset, replace it with
115 local _if _punct_c _punct _var _default prefix suffix
117 if [ $# -ne 2 -a $# -ne 3 ]; then
118 err 3 'USAGE: get_if_var name var [default]'
123 for _punct_c in $_punct; do
124 _if=`ltr ${_if} ${_punct_c} '_'`
131 eval echo \${${prefix}${_if}${suffix}-${_default}}
134 # _ifconfig_getargs if [af]
135 # Echos the arguments for the supplied interface to stdout.
136 # Returns 1 if no interface is specified.
137 # In general, the ifconfig_getargs() below should be used outside
142 local _if _ifn _af _args
147 if [ -z "$_ifn" ]; then
151 _args=`get_if_var $_ifn ${_af}ifconfig_IF`
152 if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
153 for _if in ${removable_interfaces} ; do
154 if [ "$_if" = "$_ifn" ] ; then
155 _args=${pccard_ifconfig}
164 # ifconfig_getargs if [af]
165 # Takes the result from _ifconfig_getargs() and removes pseudo
166 # args such as DHCP and WPA.
170 local _tmpargs _arg _args is_optarg
172 _tmpargs=`_ifconfig_getargs $1 $2`
173 if [ $? -eq 1 ]; then
179 for _arg in $_tmpargs; do
180 if [ "$is_optarg" = "no" ]; then
205 # Returns 0 if the interface should be configured for IPv6 and
213 # lo0 is always IPv6-enabled
214 if [ "$_if" = "lo0" ]; then
218 case ${ipv6_enable} in
219 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
224 case "${ipv6_network_interfaces}" in
225 $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
226 # True if $ipv6_ifconfig_IF is defined.
227 _tmpargs=`_ifconfig_getargs $_if ipv6`
228 if [ -n "${_tmpargs}" ]; then
232 # True if $ipv6_prefix_IF is defined.
233 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
234 if [ -n "${_tmpargs}" ]; then
244 # dhcpif if [ipv4|ipv6]
245 # Returns 0 if the interface needs DHCP for IPv4/IPv6 and 1 otherwise.
246 # If the second argument is "ipv4" (or "ipv6"), then only IPv4 (or
247 # IPv6) is checked, otherwise both are checked.
251 local _tmpargs _arg _if _af
255 if [ -z "$_af" -o "$_af" = "ipv4" ]; then
256 _tmpargs=`_ifconfig_getargs $_if`
257 for _arg in $_tmpargs; do
266 if [ -z "$_af" -o "$_af" = "ipv6" ] && ipv6if $_if; then
267 _tmpargs=`_ifconfig_getargs $_if ipv6`
268 for _arg in $_tmpargs; do
281 # Returns 0 if the interface is a WPA interface and 1 otherwise.
285 local _tmpargs _arg is_optarg
287 _tmpargs=`_ifconfig_getargs $1`
289 for _arg in $_tmpargs; do
290 if [ "$is_optarg" = "no" ]; then
308 # Returns 0 if the interface exists and 1 otherwise.
312 [ -z "$1" ] && return 1
313 ifconfig -n $1 >/dev/null 2>&1
317 # Configure aliases for network interface $if.
318 # It returns 0 if at least one alias was configured or
319 # 1 if there were none.
323 local _ret alias ifconfig_args
328 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
329 if [ -n "${ifconfig_args}" ]; then
330 ifconfig $1 ${ifconfig_args} alias
331 alias=$((${alias} + 1))
341 # Remove aliases for network interface $if.
342 # It returns 0 if at least one alias was removed or
343 # 1 if there were none.
347 local _ret alias ifconfig_args
352 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
353 if [ -n "${ifconfig_args}" ]; then
354 ifconfig $1 ${ifconfig_args} -alias
355 alias=$((${alias} + 1))
365 # Evaluate a startup script for the $if interface.
366 # It returns 0 if a script was found and processed or
367 # 1 if no script was found.
371 if [ -r /etc/start_if.$1 ]; then
379 # Evaluate a shutdown script for the $if interface.
380 # It returns 0 if a script was found and processed or
381 # 1 if no script was found.
385 if [ -r /etc/stop_if.$1 ]; then
393 # Create cloneable interfaces.
397 local _prefix _list ifn
401 for ifn in ${cloned_interfaces}; do
402 ifconfig ${ifn} create
403 if [ $? -eq 0 ]; then
404 _list="${_list}${_prefix}${ifn}"
405 [ -z "$_prefix" ] && _prefix=' '
408 if [ -n "${_list}" ]; then
409 echo "Created clone interfaces: ${_list}"
411 debug "Created clone interfaces: ${_list}"
415 # Destroy cloned interfaces.
419 local _prefix _list ifn
423 for ifn in ${cloned_interfaces}; do
424 ifconfig ${ifn} destroy
425 if [ $? -eq 0 ]; then
426 _list="${_list}${_prefix}${ifn}"
427 [ -z "$_prefix" ] && _prefix=' '
430 if [ -n "${_list}" ]; then
431 echo "Destroyed clone interfaces: ${_list}"
433 debug "Destroyed clone interfaces: ${_list}"
437 # Create IPv6<-->IPv4 tunnels
442 case ${gif_interfaces} in
448 for _if in ${gif_interfaces}; do
449 eval _peers=\$gifconfig_${_if}
455 ifconfig $_if create >/dev/null 2>&1
456 ifconfig $_if tunnel ${_peers}
464 # Rename all requested interfaces.
468 local _ifn_list _if _ifname
470 _ifn_list=$(ifconfig -l)
471 [ -z "$_ifn_list" ] && return 0
473 for _if in ${_ifn_list} ; do
474 _ifname=`get_if_var $_if ifconfig_IF_name`
475 if [ -n "$_ifname" ]; then
476 ifconfig $_if name $_ifname
482 # list_net_interfaces type
483 # List all network interfaces. The type of interface returned
484 # can be controlled by the type argument. The type
485 # argument can be any of the following:
486 # nodhcp - all interfaces, excluding DHCP configured interfaces
487 # dhcp - list only DHCP configured interfaces
488 # If no argument is specified all network interfaces are output.
489 # Note that the list will include cloned interfaces if applicable.
490 # Cloned interfaces must already exist to have a chance to appear
491 # in the list if ${network_interfaces} is set to `auto'.
493 list_net_interfaces()
495 local type _tmplist _autolist _lo _if
496 local _aprefix _bprefix _ifarg _dhcplist _nodhcplist
499 # Get a list of ALL the interfaces.
500 # NOTE: cloned interfaces have already been configured so they should
501 # show up in the 'ifconfig -l' output.
503 case ${network_interfaces} in
505 _autolist="`ifconfig -l` `sysctl -in net.wlan.devices`"
507 for _if in ${_autolist} ; do
508 if [ "$_if" = "lo0" ]; then
511 _tmplist="${_tmplist} ${_if}"
514 _tmplist="${_lo} ${_tmplist}"
517 _tmplist="${network_interfaces} ${cloned_interfaces}"
521 if [ -z "$type" ]; then
526 # Separate out dhcp and non-dhcp interfaces
530 for _if in ${_tmplist} ; do
531 eval _ifarg="\$ifconfig_${_if}"
534 _dhcplist="${_dhcplist}${_aprefix}${_if}"
535 [ -z "$_aprefix" ] && _aprefix=' '
538 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
539 [ -z "$_bprefix" ] && _bprefix=' '
557 if [ $1 -lt 10 ]; then
577 dig=`hexdigit $((${val} & 15))`
580 while [ ${val} -gt 0 ]; do
581 dig=`hexdigit $((${val} & 15))`
591 # IPv6-specific setup subroutines
594 # Setup the interfaces for IPv6
595 network6_interface_setup()
597 local interfaces rtsol_interfaces ipv6_ifconfig
598 local rtsol_available rtsol_interface
599 local prefix laddr hostid address alias
604 case ${ipv6_gateway_enable} in
612 for _if in $interfaces; do
614 prefix=`get_if_var $_if ipv6_prefix_IF`
615 if [ -n "${prefix}" ]; then
618 laddr=`network6_getladdr $_if`
619 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
620 for j in ${prefix}; do
621 address=$j\:${hostid}
622 ifconfig $_if inet6 ${address} prefixlen 64 alias
624 case ${ipv6_gateway_enable} in
626 # subnet-router anycast address
628 ifconfig $_if inet6 $j:: prefixlen 64 \
634 ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
635 if [ -n "${ipv6_ifconfig}" ]; then
638 ifconfig $_if inet6 ${ipv6_ifconfig} alias
641 if [ "${rtsol_available}" = "yes" -a \
642 "${rtsol_interface}" = "yes" ]; then
644 lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
647 rtsol_interfaces="${rtsol_interfaces} ${_if}"
655 if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
656 # Act as endhost - automatically configured.
657 # You can configure only single interface, as
658 # specification assumes that autoconfigured host has
659 # single interface only.
660 ${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
661 set ${rtsol_interfaces}
663 echo "Auto configuring interface $1 ..."
667 for _if in $interfaces; do
670 ipv6_ifconfig=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
671 if [ -z "${ipv6_ifconfig}" ]; then
674 ifconfig $_if inet6 ${ipv6_ifconfig} alias
675 alias=$((${alias} + 1))
680 # Setup IPv6 to IPv4 mapping
683 local stf_prefixlen stf_interface_ipv6_ifid
684 local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
687 case ${stf_interface_ipv4addr} in
691 # assign IPv6 addr and interface route for 6to4 interface
692 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
695 set ${stf_interface_ipv4addr}
697 hexfrag1=`hexprint $(($1*256 + $2))`
698 hexfrag2=`hexprint $(($3*256 + $4))`
699 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
700 case ${stf_interface_ipv6_ifid} in
701 [Aa][Uu][Tt][Oo] | '')
702 for _if in ${ipv6_network_interfaces}; do
703 laddr=`network6_getladdr $_if`
712 stf_interface_ipv6_ifid=`expr "${laddr}" : \
713 'fe80::\(.*\)%\(.*\)'`
714 case ${stf_interface_ipv6_ifid} in
716 stf_interface_ipv6_ifid=0:0:0:1
721 ifconfig stf0 create >/dev/null 2>&1
722 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
723 prefixlen ${stf_prefixlen}
724 # disallow packets to malicious 6to4 prefix
725 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
726 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
727 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
728 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
733 # Setup static routes
734 network6_static_routes_setup()
738 # Set up any static routes.
739 case ${ipv6_defaultrouter} in
743 ipv6_static_routes="default ${ipv6_static_routes}"
744 ipv6_route_default="default ${ipv6_defaultrouter}"
747 case ${ipv6_static_routes} in
751 for _rt in ${ipv6_static_routes}; do
752 eval ipv6_route_args=\$ipv6_route_${_rt}
753 route add -inet6 ${ipv6_route_args}
759 # Install the "default interface" to kernel, which will be used
760 # as the default route when there's no router.
761 network6_default_interface_setup()
765 # Choose IPv6 default interface if it is not clearly specified.
766 case ${ipv6_default_interface} in
768 for _if in ${ipv6_network_interfaces}; do
769 if [ "${_if}" = "lo0" ]; then
773 laddr=`network6_getladdr $_if exclude_tentative`
778 ipv6_default_interface=$_if
786 # Disallow unicast packets without outgoing scope identifiers,
787 # or route such packets to a "default" interface, if it is specified.
788 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
789 case ${ipv6_default_interface} in
791 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
794 laddr=`network6_getladdr ${ipv6_default_interface}`
795 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
798 # Disable installing the default interface with the
799 # case net.inet6.ip6.forwarding=0 and
800 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
801 # between the default router list and the manual
802 # configured default route.
803 case ${ipv6_gateway_enable} in
807 if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
809 ndp -I ${ipv6_default_interface}
819 local proto addr rest
821 ifconfig $1 2>/dev/null | while read proto addr rest; do