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
41 eval ifconfig_args=\$ifconfig_$1
42 if [ -n "${ifconfig_args}" ]; then
43 ifconfig $1 ${ifconfig_args}
50 # Remove all inet entries from the $if interface. It returns
51 # 0 if inet entries were found and removed. It returns 1 if
52 # no entries were found or they could not be removed.
56 [ -z "$1" ] && return 1
60 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
64 for _inet in $inetList ; do
65 # get rid of extraneous line
66 [ -z "$_inet" ] && break
68 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
71 ifconfig $1 ${_inet} delete
81 # Configure aliases for network interface $if.
82 # It returns 0 if at least one alias was configured or
83 # 1 if there were none.
90 eval ifconfig_args=\$ifconfig_$1_alias${alias}
91 if [ -n "${ifconfig_args}" ]; then
92 ifconfig $1 ${ifconfig_args} alias
93 alias=$((${alias} + 1))
103 # Remove aliases for network interface $if.
104 # It returns 0 if at least one alias was removed or
105 # 1 if there were none.
112 eval ifconfig_args=\$ifconfig_$1_alias${alias}
113 if [ -n "${ifconfig_args}" ]; then
114 ifconfig $1 ${ifconfig_args} -alias
115 alias=$((${alias} + 1))
125 # Evaluate a startup script for the $if interface.
126 # It returns 0 if a script was found and processed or
127 # 1 if no script was found.
131 if [ -r /etc/start_if.$1 ]; then
139 # Evaluate a shutdown script for the $if interface.
140 # It returns 0 if a script was found and processed or
141 # 1 if no script was found.
145 if [ -r /etc/stop_if.$1 ]; then
152 # Create cloneable interfaces.
158 for ifn in ${cloned_interfaces}; do
159 ifconfig ${ifn} create
160 if [ $? -eq 0 ]; then
161 _list="${_list}${_prefix}${ifn}"
162 [ -z "$_prefix" ] && _prefix=' '
165 debug "Cloned: ${_list}"
168 # Destroy cloned interfaces. Destroyed interfaces are echoed
169 # to standard output.
175 for ifn in ${cloned_interfaces}; do
176 ifconfig ${ifn} destroy
177 if [ $? -eq 0 ]; then
178 _list="${_list}${_prefix}${ifn}"
179 [ -z "$_prefix" ] && _prefix=' '
182 debug "Destroyed clones: ${_list}"
186 case ${gif_interfaces} in
190 for i in ${gif_interfaces}; do
191 eval peers=\$gifconfig_$i
197 ifconfig $i create >/dev/null 2>&1
198 ifconfig $i tunnel ${peers}
209 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
210 # arguments were found and configured; returns 1 otherwise.
215 eval ifconfig_args=\$ifconfig_${ifn}_ipx
216 if [ -n "${ifconfig_args}" ]; then
217 ifconfig ${ifn} ${ifconfig_args}
224 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
225 # addresses were found and unconfigured. It returns 1, otherwise.
229 [ -z "$1" ] && return 1
233 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
237 for _ipx in $ipxList ; do
238 # get rid of extraneous line
239 [ -z "$_ipx" ] && break
241 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
244 ifconfig $1 ${_ipx} delete
254 # list_net_interfaces type
255 # List all network interfaces. The type of interface returned
256 # can be controlled by the type argument. The type
257 # argument can be any of the following:
258 # nodhcp - all interfaces, excluding DHCP configured interfaces
259 # dhcp - list only DHCP configured interfaces
260 # If no argument is specified all network interfaces are output.
261 # Note that the list always includes cloned interfaces.
263 list_net_interfaces()
267 # Get a list of ALL the interfaces
269 case ${network_interfaces} in
271 _tmplist="`ifconfig -l`"
274 _tmplist="${network_interfaces}"
277 _tmplist="${_tmplist} ${cloned_interfaces}"
279 if [ -z "$type" ]; then
284 # Separate out dhcp and non-dhcp intefraces
288 for _if in ${_tmplist} ; do
289 eval _ifarg="\$ifconfig_${_if}"
292 _dhcplist="${_dhcplist}${_aprefix}${_if}"
293 [ -z "$_aprefix" ] && _aprefix=' '
296 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
297 [ -z "$_bprefix" ] && _bprefix=' '
302 case ${pccard_ifconfig} in
304 for _if in ${removable_interfaces} ; do
305 _test_if=`ifconfig ${_if} 2>&1`
307 "ifconfig: interface $_if does not exist")
310 _dhcplist="${_dhcplist}${_aprefix}${_if}"
311 [ -z "$_aprefix" ] && _aprefix=' '
333 if [ $1 -lt 10 ]; then
352 dig=`hexdigit $((${val} & 15))`
355 while [ ${val} -gt 0 ]; do
356 dig=`hexdigit $((${val} & 15))`
364 # Setup the interfaces for IPv6
365 network6_interface_setup()
369 case ${ipv6_gateway_enable} in
377 for i in $interfaces; do
379 eval prefix=\$ipv6_prefix_$i
380 if [ -n "${prefix}" ]; then
383 laddr=`network6_getladdr $i`
384 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
385 for j in ${prefix}; do
386 address=$j\:${hostid}
387 ifconfig $i inet6 ${address} prefixlen 64 alias
389 case ${ipv6_gateway_enable} in
391 # subnet-router anycast address
393 ifconfig $i inet6 $j:: prefixlen 64 \
399 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
400 if [ -n "${ipv6_ifconfig}" ]; then
403 ifconfig $i inet6 ${ipv6_ifconfig} alias
406 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
409 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
412 rtsol_interfaces="${rtsol_interfaces} ${i}"
420 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
421 # Act as endhost - automatically configured.
422 # You can configure only single interface, as
423 # specification assumes that autoconfigured host has
424 # single interface only.
425 sysctl net.inet6.ip6.accept_rtadv=1
426 set ${rtsol_interfaces}
428 rtsol ${rtsol_flags} $1
431 for i in $interfaces; do
434 eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
435 if [ -z "${ipv6_ifconfig}" ]; then
438 ifconfig $i inet6 ${ipv6_ifconfig} alias
439 alias=$((${alias} + 1))
444 # Setup IPv6 to IPv4 mapping
447 case ${stf_interface_ipv4addr} in
451 # assign IPv6 addr and interface route for 6to4 interface
452 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
455 set ${stf_interface_ipv4addr}
457 hexfrag1=`hexprint $(($1*256 + $2))`
458 hexfrag2=`hexprint $(($3*256 + $4))`
459 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
460 case ${stf_interface_ipv6_ifid} in
461 [Aa][Uu][Tt][Oo] | '')
462 for i in ${ipv6_network_interfaces}; do
463 laddr=`network6_getladdr ${i}`
472 stf_interface_ipv6_ifid=`expr "${laddr}" : \
473 'fe80::\(.*\)%\(.*\)'`
474 case ${stf_interface_ipv6_ifid} in
476 stf_interface_ipv6_ifid=0:0:0:1
481 ifconfig stf0 create >/dev/null 2>&1
482 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
483 prefixlen ${stf_prefixlen}
484 # disallow packets to malicious 6to4 prefix
485 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
486 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
487 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
488 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
493 # Setup static routes
494 network6_static_routes_setup()
496 # Set up any static routes.
497 case ${ipv6_defaultrouter} in
501 ipv6_static_routes="default ${ipv6_static_routes}"
502 ipv6_route_default="default ${ipv6_defaultrouter}"
505 case ${ipv6_static_routes} in
509 for i in ${ipv6_static_routes}; do
510 eval ipv6_route_args=\$ipv6_route_${i}
511 route add -inet6 ${ipv6_route_args}
518 network6_faith_setup()
520 case ${ipv6_faith_prefix} in
524 sysctl net.inet6.ip6.keepfaith=1
525 ifconfig faith0 create >/dev/null 2>&1
527 for prefix in ${ipv6_faith_prefix}; do
528 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
534 prefix=`expr "${prefix}" : \
535 "\(.*\)/${prefixlen}"`
538 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
539 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
546 # Install the "default interface" to kernel, which will be used
547 # as the default route when there's no router.
548 network6_default_interface_setup()
550 # Choose IPv6 default interface if it is not clearly specified.
551 case ${ipv6_default_interface} in
553 for i in ${ipv6_network_interfaces}; do
559 laddr=`network6_getladdr $i exclude_tentative`
564 ipv6_default_interface=$i
572 # Disallow unicast packets without outgoing scope identifiers,
573 # or route such packets to a "default" interface, if it is specified.
574 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
575 case ${ipv6_default_interface} in
577 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
580 laddr=`network6_getladdr ${ipv6_default_interface}`
581 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
584 # Disable installing the default interface with the
585 # case net.inet6.ip6.forwarding=0 and
586 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
587 # between the default router list and the manual
588 # configured default route.
589 case ${ipv6_gateway_enable} in
593 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
595 ndp -I ${ipv6_default_interface}
605 ifconfig $1 2>/dev/null | while read proto addr rest; do