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.151 2003/06/24 03:55:21 kuriyama Exp $
26 # $DragonFly: src/etc/network.subr,v 1.1 2003/07/24 06:35:32 dillon Exp $
30 # Subroutines commonly used from network startup scripts.
31 # Requires that rc.conf be loaded first.
35 # Evaluate ifconfig(8) arguments for interface $if and
36 # run ifconfig(8) with those arguments. It returns 0 if
37 # arguments were found and executed or 1 if the interface
42 eval ifconfig_args=\$ifconfig_$1
43 if [ -n "${ifconfig_args}" ]; then
44 ifconfig $1 ${ifconfig_args}
51 # Remove all inet entries from the $if interface. It returns
52 # 0 if inet entries were found and removed. It returns 1 if
53 # no entries were found or they could not be removed.
57 [ -z "$1" ] && return 1
61 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
65 for _inet in $inetList ; do
66 # get rid of extraneous line
67 [ -z "$_inet" ] && break
69 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
72 ifconfig $1 ${_inet} delete
82 # Configure aliases for network interface $if.
83 # It returns 0 if at least one alias was configured or
84 # 1 if there were none.
91 eval ifconfig_args=\$ifconfig_$1_alias${alias}
92 if [ -n "${ifconfig_args}" ]; then
93 ifconfig $1 ${ifconfig_args} alias
94 alias=$((${alias} + 1))
104 # Remove aliases for network interface $if.
105 # It returns 0 if at least one alias was removed or
106 # 1 if there were none.
113 eval ifconfig_args=\$ifconfig_$1_alias${alias}
114 if [ -n "${ifconfig_args}" ]; then
115 ifconfig $1 ${ifconfig_args} -alias
116 alias=$((${alias} + 1))
126 # Evaluate a startup script for the $if interface.
127 # It returns 0 if a script was found and processed or
128 # 1 if no script was found.
132 if [ -r /etc/start_if.$1 ]; then
140 # Evaluate a shutdown script for the $if interface.
141 # It returns 0 if a script was found and processed or
142 # 1 if no script was found.
146 if [ -r /etc/stop_if.$1 ]; then
153 # Create cloneable interfaces.
159 for ifn in ${cloned_interfaces}; do
160 ifconfig ${ifn} create
161 if [ $? -eq 0 ]; then
162 _list="${_list}${_prefix}${ifn}"
163 [ -z "$_prefix" ] && _prefix=' '
166 debug "Cloned: ${_list}"
169 # Destroy cloned interfaces. Destroyed interfaces are echoed
170 # to standard output.
176 for ifn in ${cloned_interfaces}; do
177 ifconfig ${ifn} destroy
178 if [ $? -eq 0 ]; then
179 _list="${_list}${_prefix}${ifn}"
180 [ -z "$_prefix" ] && _prefix=' '
183 debug "Destroyed clones: ${_list}"
187 case ${gif_interfaces} in
191 for i in ${gif_interfaces}; do
192 eval peers=\$gifconfig_$i
198 ifconfig $i create >/dev/null 2>&1
199 ifconfig $i tunnel ${peers}
210 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
211 # arguments were found and configured; returns 1 otherwise.
216 eval ifconfig_args=\$ifconfig_${ifn}_ipx
217 if [ -n "${ifconfig_args}" ]; then
218 ifconfig ${ifn} ${ifconfig_args}
225 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
226 # addresses were found and unconfigured. It returns 1, otherwise.
230 [ -z "$1" ] && return 1
234 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
238 for _ipx in $ipxList ; do
239 # get rid of extraneous line
240 [ -z "$_ipx" ] && break
242 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
245 ifconfig $1 ${_ipx} delete
255 # list_net_interfaces type
256 # List all network interfaces. The type of interface returned
257 # can be controlled by the type argument. The type
258 # argument can be any of the following:
259 # nodhcp - all interfaces, excluding DHCP configured interfaces
260 # dhcp - list only DHCP configured interfaces
261 # If no argument is specified all network interfaces are output.
262 # Note that the list always includes cloned interfaces.
264 list_net_interfaces()
268 # Get a list of ALL the interfaces
270 case ${network_interfaces} in
272 _tmplist="`ifconfig -l`"
275 _tmplist="${network_interfaces}"
278 _tmplist="${_tmplist} ${cloned_interfaces}"
280 if [ -z "$type" ]; then
285 # Separate out dhcp and non-dhcp intefraces
289 for _if in ${_tmplist} ; do
290 eval _ifarg="\$ifconfig_${_if}"
293 _dhcplist="${_dhcplist}${_aprefix}${_if}"
294 [ -z "$_aprefix" ] && _aprefix=' '
297 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
298 [ -z "$_bprefix" ] && _bprefix=' '
316 if [ $1 -lt 10 ]; then
335 dig=`hexdigit $((${val} & 15))`
338 while [ ${val} -gt 0 ]; do
339 dig=`hexdigit $((${val} & 15))`
347 # Setup the interfaces for IPv6
348 network6_interface_setup()
352 case ${ipv6_gateway_enable} in
360 for i in $interfaces; do
362 eval prefix=\$ipv6_prefix_$i
363 if [ -n "${prefix}" ]; then
366 laddr=`network6_getladdr $i`
367 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
368 for j in ${prefix}; do
369 address=$j\:${hostid}
370 ifconfig $i inet6 ${address} prefixlen 64 alias
372 case ${ipv6_gateway_enable} in
374 # subnet-router anycast address
376 ifconfig $i inet6 $j:: prefixlen 64 \
382 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
383 if [ -n "${ipv6_ifconfig}" ]; then
386 ifconfig $i inet6 ${ipv6_ifconfig} alias
389 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
392 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
395 rtsol_interfaces="${rtsol_interfaces} ${i}"
403 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
404 # Act as endhost - automatically configured.
405 # You can configure only single interface, as
406 # specification assumes that autoconfigured host has
407 # single interface only.
408 sysctl net.inet6.ip6.accept_rtadv=1
409 set ${rtsol_interfaces}
414 for i in $interfaces; do
417 eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
418 if [ -z "${ipv6_ifconfig}" ]; then
421 ifconfig $i inet6 ${ipv6_ifconfig} alias
422 alias=$((${alias} + 1))
427 # Setup IPv6 to IPv4 mapping
430 case ${stf_interface_ipv4addr} in
434 # assign IPv6 addr and interface route for 6to4 interface
435 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
438 set ${stf_interface_ipv4addr}
440 hexfrag1=`hexprint $(($1*256 + $2))`
441 hexfrag2=`hexprint $(($3*256 + $4))`
442 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
443 case ${stf_interface_ipv6_ifid} in
444 [Aa][Uu][Tt][Oo] | '')
445 for i in ${ipv6_network_interfaces}; do
446 laddr=`network6_getladdr ${i}`
455 stf_interface_ipv6_ifid=`expr "${laddr}" : \
456 'fe80::\(.*\)%\(.*\)'`
457 case ${stf_interface_ipv6_ifid} in
459 stf_interface_ipv6_ifid=0:0:0:1
464 ifconfig stf0 create >/dev/null 2>&1
465 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
466 prefixlen ${stf_prefixlen}
467 # disallow packets to malicious 6to4 prefix
468 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
469 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
470 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
471 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
476 # Setup static routes
477 network6_static_routes_setup()
479 # Set up any static routes.
480 case ${ipv6_defaultrouter} in
484 ipv6_static_routes="default ${ipv6_static_routes}"
485 ipv6_route_default="default ${ipv6_defaultrouter}"
488 case ${ipv6_static_routes} in
492 for i in ${ipv6_static_routes}; do
493 eval ipv6_route_args=\$ipv6_route_${i}
494 route add -inet6 ${ipv6_route_args}
501 network6_faith_setup()
503 case ${ipv6_faith_prefix} in
507 sysctl net.inet6.ip6.keepfaith=1
508 ifconfig faith0 create >/dev/null 2>&1
510 for prefix in ${ipv6_faith_prefix}; do
511 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
517 prefix=`expr "${prefix}" : \
518 "\(.*\)/${prefixlen}"`
521 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
522 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
529 # Install the "default interface" to kernel, which will be used
530 # as the default route when there's no router.
531 network6_default_interface_setup()
533 # Choose IPv6 default interface if it is not clearly specified.
534 case ${ipv6_default_interface} in
536 for i in ${ipv6_network_interfaces}; do
542 laddr=`network6_getladdr $i exclude_tentative`
547 ipv6_default_interface=$i
555 # Disallow unicast packets without outgoing scope identifiers,
556 # or route such packets to a "default" interface, if it is specified.
557 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
558 case ${ipv6_default_interface} in
560 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
563 laddr=`network6_getladdr ${ipv6_default_interface}`
564 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
567 # Disable installing the default interface with the
568 # case net.inet6.ip6.forwarding=0 and
569 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
570 # between the default router list and the manual
571 # configured default route.
572 case ${ipv6_gateway_enable} in
576 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
578 ndp -I ${ipv6_default_interface}
588 ifconfig $1 2>/dev/null | while read proto addr rest; do