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 cloneable interfaces.
414 local _prefix _list ifn
418 for ifn in ${cloned_interfaces}; do
419 ifconfig ${ifn} create
420 if [ $? -eq 0 ]; then
421 _list="${_list}${_prefix}${ifn}"
422 [ -z "$_prefix" ] && _prefix=' '
425 if [ -n "${_list}" ]; then
426 echo "Created clone interfaces: ${_list}"
428 debug "Created clone interfaces: ${_list}"
432 # Destroy cloned interfaces.
436 local _prefix _list ifn
440 for ifn in ${cloned_interfaces}; do
441 ifconfig ${ifn} destroy
442 if [ $? -eq 0 ]; then
443 _list="${_list}${_prefix}${ifn}"
444 [ -z "$_prefix" ] && _prefix=' '
447 if [ -n "${_list}" ]; then
448 echo "Destroyed clone interfaces: ${_list}"
450 debug "Destroyed clone interfaces: ${_list}"
454 # Create IPv6<-->IPv4 tunnels
459 case ${gif_interfaces} in
465 for _if in ${gif_interfaces}; do
466 eval _peers=\$gifconfig_${_if}
472 ifconfig $_if create >/dev/null 2>&1
473 ifconfig $_if tunnel ${_peers}
481 # Rename all requested interfaces.
485 local _ifn_list _if _ifname
487 _ifn_list=$(ifconfig -l)
488 [ -z "$_ifn_list" ] && return 0
490 for _if in ${_ifn_list} ; do
491 _ifname=`get_if_var $_if ifconfig_IF_name`
492 if [ -n "$_ifname" ]; then
493 ifconfig $_if name $_ifname
499 # list_net_interfaces
500 # List all network interfaces.
501 # Note that the list will include cloned interfaces if applicable.
502 # Cloned interfaces must already exist to have a chance to appear
503 # in the list if ${network_interfaces} is set to `auto'.
505 list_net_interfaces()
507 local _tmplist _autolist _lo _if
509 case ${network_interfaces} in
511 _autolist="`ifconfig -l` `sysctl -in net.wlan.devices`"
513 for _if in ${_autolist} ; do
514 if [ "$_if" = "lo0" ]; then
517 _tmplist="${_tmplist} ${_if}"
520 _tmplist="${_lo} ${_tmplist}"
523 _tmplist="${network_interfaces} ${cloned_interfaces}"
532 if [ $1 -lt 10 ]; then
552 dig=`hexdigit $((${val} & 15))`
555 while [ ${val} -gt 0 ]; do
556 dig=`hexdigit $((${val} & 15))`
566 # IPv6-specific setup subroutines
569 # Setup the interfaces for IPv6
570 network6_interface_setup()
572 local interfaces rtsol_interfaces ipv6_ifconfig
573 local rtsol_available rtsol_interface
574 local prefix laddr hostid address
579 case ${ipv6_gateway_enable} in
587 for _if in $interfaces; do
589 prefix=`get_if_var $_if ipv6_prefix_IF`
590 if [ -n "${prefix}" ]; then
593 laddr=`network6_getladdr $_if`
594 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
595 for j in ${prefix}; do
596 address=$j\:${hostid}
597 ifconfig $_if inet6 ${address} prefixlen 64 alias
599 case ${ipv6_gateway_enable} in
601 # subnet-router anycast address
603 ifconfig $_if inet6 $j:: prefixlen 64 \
609 ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
610 if [ -n "${ipv6_ifconfig}" ]; then
613 ifconfig $_if inet6 ${ipv6_ifconfig} alias
616 if [ "${rtsol_available}" = "yes" -a \
617 "${rtsol_interface}" = "yes" ]; then
619 lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
622 rtsol_interfaces="${rtsol_interfaces} ${_if}"
630 if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
631 # Act as endhost - automatically configured.
632 # You can configure only single interface, as
633 # specification assumes that autoconfigured host has
634 # single interface only.
635 ${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
636 set ${rtsol_interfaces}
638 echo "Auto configuring interface $1 ..."
642 for _if in $interfaces; do
647 # Setup IPv6 to IPv4 mapping
650 local stf_prefixlen stf_interface_ipv6_ifid
651 local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
654 case ${stf_interface_ipv4addr} in
658 # assign IPv6 addr and interface route for 6to4 interface
659 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
662 set ${stf_interface_ipv4addr}
664 hexfrag1=`hexprint $(($1*256 + $2))`
665 hexfrag2=`hexprint $(($3*256 + $4))`
666 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
667 case ${stf_interface_ipv6_ifid} in
668 [Aa][Uu][Tt][Oo] | '')
669 for _if in ${ipv6_network_interfaces}; do
670 laddr=`network6_getladdr $_if`
679 stf_interface_ipv6_ifid=`expr "${laddr}" : \
680 'fe80::\(.*\)%\(.*\)'`
681 case ${stf_interface_ipv6_ifid} in
683 stf_interface_ipv6_ifid=0:0:0:1
688 ifconfig stf0 create >/dev/null 2>&1
689 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
690 prefixlen ${stf_prefixlen}
691 # disallow packets to malicious 6to4 prefix
692 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
693 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
694 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
695 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
700 # Setup static routes
701 network6_static_routes_setup()
705 # Set up any static routes.
706 case ${ipv6_defaultrouter} in
710 ipv6_static_routes="default ${ipv6_static_routes}"
711 ipv6_route_default="default ${ipv6_defaultrouter}"
714 case ${ipv6_static_routes} in
718 for _rt in ${ipv6_static_routes}; do
719 eval ipv6_route_args=\$ipv6_route_${_rt}
720 route add -inet6 ${ipv6_route_args}
726 # Install the "default interface" to kernel, which will be used
727 # as the default route when there's no router.
728 network6_default_interface_setup()
732 # Choose IPv6 default interface if it is not clearly specified.
733 case ${ipv6_default_interface} in
735 for _if in ${ipv6_network_interfaces}; do
736 if [ "${_if}" = "lo0" ]; then
740 laddr=`network6_getladdr $_if exclude_tentative`
745 ipv6_default_interface=$_if
753 # Disallow unicast packets without outgoing scope identifiers,
754 # or route such packets to a "default" interface, if it is specified.
755 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
756 case ${ipv6_default_interface} in
758 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
761 laddr=`network6_getladdr ${ipv6_default_interface}`
762 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
765 # Disable installing the default interface with the
766 # case net.inet6.ip6.forwarding=0 and
767 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
768 # between the default router list and the manual
769 # configured default route.
770 case ${ipv6_gateway_enable} in
774 if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
776 ndp -I ${ipv6_default_interface}
786 local proto addr rest
788 ifconfig $1 2>/dev/null | while read proto addr rest; do