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
29 # Subroutines commonly used from network startup scripts.
30 # Requires that rc.conf 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
44 ifconfig_args=`ifconfig_getargs $1`
45 if [ -n "${ifconfig_args}" ]; then
47 ifconfig $1 ${ifconfig_args}
52 if [ $_cfg -ne 0 ] ; then
55 /etc/rc.d/wpa_supplicant start $1
56 _cfg=0 # XXX: not sure this should count
60 if [ $_cfg -ne 0 ] ; then
63 if syncdhcpif $1; then
64 /etc/rc.d/dhclient start $1
73 # Remove all inet entries from the $if interface. It returns
74 # 0 if inet entries were found and removed. It returns 1 if
75 # no entries were found or they could not be removed.
77 # XXX: should not be only inet
81 [ -z "$1" ] && return 1
85 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
89 for _inet in $inetList ; do
90 # get rid of extraneous line
91 [ -z "$_inet" ] && break
93 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
96 ifconfig $1 ${_inet} delete
103 /etc/rc.d/wpa_supplicant stop $1
108 /etc/rc.d/dhclient stop $1
117 # get_if_var if var [default]
118 # Return the value of the pseudo-hash corresponding to $if where
119 # $var is a string containg the sub-string "IF" which will be
120 # replaced with $if after the characters defined in _punct are
121 # replaced with '_'. If the variable is unset, replace it with
125 if [ $# -ne 2 -a $# -ne 3 ]; then
126 err 3 'USAGE: get_if_var name var [default]'
131 for _punct_c in $_punct; do
132 _if=`ltr ${_if} ${_punct_c} '_'`
139 eval echo \${${prefix}${_if}${suffix}-${_default}}
142 # _ifconfig_getargs if
143 # Echos the arguments for the supplied interface to stdout.
144 # returns 1 if empty. In general, ifconfig_getargs should be used
149 if [ -z "$_ifn" ]; then
153 get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
156 # ifconfig_getargs if
157 # Takes the result from _ifconfig_getargs and removes pseudo
158 # args such as DHCP and WPA.
161 _tmpargs=`_ifconfig_getargs $1`
162 if [ $? -eq 1 ]; then
167 for _arg in $_tmpargs; do
170 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
171 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
172 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
184 # Returns 0 if the interface should be automaticly configured at
185 # boot time and 1 otherwise.
188 _tmpargs=`_ifconfig_getargs $1`
189 for _arg in $_tmpargs; do
191 [Nn][Oo][Aa][Uu][Tt][Oo])
200 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
203 _tmpargs=`_ifconfig_getargs $1`
204 for _arg in $_tmpargs; do
209 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
212 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
221 # Returns 0 if the interface should be configured synchronously and
225 _tmpargs=`_ifconfig_getargs $1`
226 for _arg in $_tmpargs; do
228 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
231 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
236 if checkyesno synchronous_dhclient; then
244 # Returns 0 if the interface is a WPA interface and 1 otherwise.
247 _tmpargs=`_ifconfig_getargs $1`
248 for _arg in $_tmpargs; do
259 # add IPv4 addresses to the interface $if
264 ipv4_addrs_common ${_if} alias
268 # remove IPv4 addresses from the interface $if
273 ipv4_addrs_common ${_if} -alias
276 # ipv4_addrs_common if action
277 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
278 # and use $action to add or remove IPv4 addresses from $if.
286 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
288 for _cidr in ${cidr_addr}; do
290 _netmask="/"${_cidr##*/}
291 _range=${_ipaddr##*.}
296 # clear netmask when removing aliases
297 if [ "${_action}" = "-alias" ]; then
302 while [ "${_ipcount}" -le "${_iphigh}" ]; do
303 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
304 _ipcount=$((${_ipcount}+1))
307 # only the first ipaddr in a subnet need the real netmask
308 if [ "${_action}" != "-alias" ]; then
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.
326 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
327 if [ -n "${ifconfig_args}" ]; then
328 ifconfig $1 ${ifconfig_args} alias
329 alias=$((${alias} + 1))
339 # Remove aliases for network interface $if.
340 # It returns 0 if at least one alias was removed or
341 # 1 if there were none.
348 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
349 if [ -n "${ifconfig_args}" ]; then
350 ifconfig $1 ${ifconfig_args} -alias
351 alias=$((${alias} + 1))
361 # Evaluate a startup script for the $if interface.
362 # It returns 0 if a script was found and processed or
363 # 1 if no script was found.
367 if [ -r /etc/start_if.$1 ]; then
375 # Evaluate a shutdown script for the $if interface.
376 # It returns 0 if a script was found and processed or
377 # 1 if no script was found.
381 if [ -r /etc/stop_if.$1 ]; then
388 # Create cloneable interfaces.
394 for ifn in ${cloned_interfaces}; do
395 ifconfig ${ifn} create
396 if [ $? -eq 0 ]; then
397 _list="${_list}${_prefix}${ifn}"
398 [ -z "$_prefix" ] && _prefix=' '
401 debug "Cloned: ${_list}"
404 # Destroy cloned interfaces. Destroyed interfaces are echoed
405 # to standard output.
411 for ifn in ${cloned_interfaces}; do
412 ifconfig ${ifn} destroy
413 if [ $? -eq 0 ]; then
414 _list="${_list}${_prefix}${ifn}"
415 [ -z "$_prefix" ] && _prefix=' '
418 debug "Destroyed clones: ${_list}"
422 case ${gif_interfaces} in
426 for i in ${gif_interfaces}; do
427 peers=`get_if_var $i gifconfig_IF`
433 ifconfig $i create >/dev/null 2>&1
434 ifconfig $i tunnel ${peers}
445 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
446 # arguments were found and configured; returns 1 otherwise.
451 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
452 if [ -n "${ifconfig_args}" ]; then
453 ifconfig ${ifn} ${ifconfig_args}
460 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
461 # addresses were found and unconfigured. It returns 1, otherwise.
465 [ -z "$1" ] && return 1
469 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
473 for _ipx in $ipxList ; do
474 # get rid of extraneous line
475 [ -z "$_ipx" ] && break
477 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
480 ifconfig $1 ${_ipx} delete
490 # Rename all requested interfaces.
495 _ifn_list="`ifconfig -l`"
496 [ -z "$_ifn_list" ] && return 0
497 for _if in ${_ifn_list} ; do
498 _ifname=`get_if_var $_if ifconfig_IF_name`
499 if [ ! -z "$_ifname" ]; then
500 ifconfig $_if name $_ifname
507 # list_net_interfaces type
508 # List all network interfaces. The type of interface returned
509 # can be controlled by the type argument. The type
510 # argument can be any of the following:
511 # nodhcp - all interfaces, excluding DHCP configured interfaces
512 # dhcp - list only DHCP configured interfaces
513 # If no argument is specified all network interfaces are output.
514 # Note that the list will include cloned interfaces if applicable.
515 # Cloned interfaces must already exist to have a chance to appear
516 # in the list if ${network_interfaces} is set to `auto'.
518 list_net_interfaces()
522 # Get a list of ALL the interfaces and make lo0 first if it's there.
524 case ${network_interfaces} in
527 _autolist="`ifconfig -l`"
529 for _if in ${_autolist} ; do
531 if [ "$_if" = "lo0" ]; then
534 _tmplist="${_tmplist}${_prefix}${_if}"
535 [ -z "$_prefix" ] && _prefix=' '
539 _tmplist="${_lo}${_tmplist}"
542 _tmplist="${network_interfaces} ${cloned_interfaces}"
546 if [ -z "$type" ]; then
551 # Separate out dhcp and non-dhcp interfaces
555 for _if in ${_tmplist} ; do
557 _dhcplist="${_dhcplist}${_aprefix}${_if}"
558 [ -z "$_aprefix" ] && _aprefix=' '
559 elif [ -n "`_ifconfig_getargs $_if`" ]; then
560 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
561 [ -z "$_bprefix" ] && _bprefix=' '
578 if [ $1 -lt 10 ]; then
597 dig=`hexdigit $((${val} & 15))`
600 while [ ${val} -gt 0 ]; do
601 dig=`hexdigit $((${val} & 15))`
609 # Setup the interfaces for IPv6
610 network6_interface_setup()
614 case ${ipv6_gateway_enable} in
622 for i in $interfaces; do
624 prefix=`get_if_var $i ipv6_prefix_IF`
625 if [ -n "${prefix}" ]; then
628 laddr=`network6_getladdr $i`
629 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
630 for j in ${prefix}; do
631 address=$j\:${hostid}
632 ifconfig $i inet6 ${address} prefixlen 64 alias
634 case ${ipv6_gateway_enable} in
636 # subnet-router anycast address
638 ifconfig $i inet6 $j:: prefixlen 64 \
644 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
645 if [ -n "${ipv6_ifconfig}" ]; then
648 ifconfig $i inet6 ${ipv6_ifconfig} alias
651 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
654 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
657 rtsol_interfaces="${rtsol_interfaces} ${i}"
665 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
666 # Act as endhost - automatically configured.
667 # You can configure only single interface, as
668 # specification assumes that autoconfigured host has
669 # single interface only.
670 sysctl net.inet6.ip6.accept_rtadv=1
671 set ${rtsol_interfaces}
673 rtsol ${rtsol_flags} $1
676 for i in $interfaces; do
679 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
680 if [ -z "${ipv6_ifconfig}" ]; then
683 ifconfig $i inet6 ${ipv6_ifconfig} alias
684 alias=$((${alias} + 1))
689 # Setup IPv6 to IPv4 mapping
692 case ${stf_interface_ipv4addr} in
696 # assign IPv6 addr and interface route for 6to4 interface
697 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
700 set ${stf_interface_ipv4addr}
702 hexfrag1=`hexprint $(($1*256 + $2))`
703 hexfrag2=`hexprint $(($3*256 + $4))`
704 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
705 case ${stf_interface_ipv6_ifid} in
706 [Aa][Uu][Tt][Oo] | '')
707 for i in ${ipv6_network_interfaces}; do
708 laddr=`network6_getladdr ${i}`
717 stf_interface_ipv6_ifid=`expr "${laddr}" : \
718 'fe80::\(.*\)%\(.*\)'`
719 case ${stf_interface_ipv6_ifid} in
721 stf_interface_ipv6_ifid=0:0:0:1
726 ifconfig stf0 create >/dev/null 2>&1
727 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
728 prefixlen ${stf_prefixlen}
729 # disallow packets to malicious 6to4 prefix
730 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
731 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
732 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
733 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
738 # Setup static routes
739 network6_static_routes_setup()
741 # Set up any static routes.
742 case ${ipv6_defaultrouter} in
746 ipv6_static_routes="default ${ipv6_static_routes}"
747 ipv6_route_default="default ${ipv6_defaultrouter}"
750 case ${ipv6_static_routes} in
754 for i in ${ipv6_static_routes}; do
755 ipv6_route_args=`get_if_var $i ipv6_route_IF`
756 route add -inet6 ${ipv6_route_args}
763 network6_faith_setup()
765 case ${ipv6_faith_prefix} in
769 sysctl net.inet6.ip6.keepfaith=1
770 ifconfig faith0 create >/dev/null 2>&1
772 for prefix in ${ipv6_faith_prefix}; do
773 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
779 prefix=`expr "${prefix}" : \
780 "\(.*\)/${prefixlen}"`
783 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
784 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
791 # Install the "default interface" to kernel, which will be used
792 # as the default route when there's no router.
793 network6_default_interface_setup()
795 # Choose IPv6 default interface if it is not clearly specified.
796 case ${ipv6_default_interface} in
798 for i in ${ipv6_network_interfaces}; do
804 laddr=`network6_getladdr $i exclude_tentative`
809 ipv6_default_interface=$i
817 # Disallow unicast packets without outgoing scope identifiers,
818 # or route such packets to a "default" interface, if it is specified.
819 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
820 case ${ipv6_default_interface} in
822 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
825 laddr=`network6_getladdr ${ipv6_default_interface}`
826 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
829 # Disable installing the default interface with the
830 # case net.inet6.ip6.forwarding=0 and
831 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
832 # between the default router list and the manual
833 # configured default route.
834 case ${ipv6_gateway_enable} in
838 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
840 ndp -I ${ipv6_default_interface}
850 ifconfig $1 2>/dev/null | while read proto addr rest; do