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 $
26 # $DragonFly: src/etc/network.subr,v 1.5 2007/07/10 22:36:01 swildner 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
38 # had no arguments. Pseudo arguments DHCP and WPA are handled
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/dhclient 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 [ -z "$1" ] && return 1
75 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
79 for _inet in $inetList ; do
80 # get rid of extraneous line
81 [ -z "$_inet" ] && break
83 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
86 ifconfig $1 ${_inet} delete
93 /etc/rc.d/wpa_supplicant stop $1
97 /etc/rc.d/dhclient stop $1
104 # _ifconfig_getargs if
105 # Echos the arguments for the supplied interface to stdout.
106 # returns 1 if empty. In general, ifconfig_getargs should be used
111 if [ -z "$_ifn" ]; then
115 eval _args=\$ifconfig_$1
116 if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
117 for _if in ${removable_interfaces} ; do
118 if [ "$_if" = "$_ifn" ] ; then
119 _args=${pccard_ifconfig}
128 # ifconfig_getargs if
129 # Takes the result from _ifconfig_getargs and removes pseudo
130 # args such as DHCP and WPA.
133 _tmpargs=`_ifconfig_getargs $1`
134 if [ $? -eq 1 ]; then
139 for _arg in $_tmpargs; do
155 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
158 _tmpargs=`_ifconfig_getargs $1`
159 for _arg in $_tmpargs; do
170 # Returns 0 if the interface is a WPA interface and 1 otherwise.
173 _tmpargs=`_ifconfig_getargs $1`
174 for _arg in $_tmpargs; do
185 # Configure aliases for network interface $if.
186 # It returns 0 if at least one alias was configured or
187 # 1 if there were none.
194 eval ifconfig_args=\$ifconfig_$1_alias${alias}
195 if [ -n "${ifconfig_args}" ]; then
196 ifconfig $1 ${ifconfig_args} alias
197 alias=$((${alias} + 1))
207 # Remove aliases for network interface $if.
208 # It returns 0 if at least one alias was removed or
209 # 1 if there were none.
216 eval ifconfig_args=\$ifconfig_$1_alias${alias}
217 if [ -n "${ifconfig_args}" ]; then
218 ifconfig $1 ${ifconfig_args} -alias
219 alias=$((${alias} + 1))
229 # Evaluate a startup script for the $if interface.
230 # It returns 0 if a script was found and processed or
231 # 1 if no script was found.
235 if [ -r /etc/start_if.$1 ]; then
243 # Evaluate a shutdown script for the $if interface.
244 # It returns 0 if a script was found and processed or
245 # 1 if no script was found.
249 if [ -r /etc/stop_if.$1 ]; then
256 # Create cloneable interfaces.
262 for ifn in ${cloned_interfaces}; do
263 ifconfig ${ifn} create
264 if [ $? -eq 0 ]; then
265 _list="${_list}${_prefix}${ifn}"
266 [ -z "$_prefix" ] && _prefix=' '
269 debug "Cloned: ${_list}"
272 # Destroy cloned interfaces. Destroyed interfaces are echoed
273 # to standard output.
279 for ifn in ${cloned_interfaces}; do
280 ifconfig ${ifn} destroy
281 if [ $? -eq 0 ]; then
282 _list="${_list}${_prefix}${ifn}"
283 [ -z "$_prefix" ] && _prefix=' '
286 debug "Destroyed clones: ${_list}"
290 case ${gif_interfaces} in
294 for i in ${gif_interfaces}; do
295 eval peers=\$gifconfig_$i
301 ifconfig $i create >/dev/null 2>&1
302 ifconfig $i tunnel ${peers}
313 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
314 # arguments were found and configured; returns 1 otherwise.
319 eval ifconfig_args=\$ifconfig_${ifn}_ipx
320 if [ -n "${ifconfig_args}" ]; then
321 ifconfig ${ifn} ${ifconfig_args}
328 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
329 # addresses were found and unconfigured. It returns 1, otherwise.
333 [ -z "$1" ] && return 1
337 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
341 for _ipx in $ipxList ; do
342 # get rid of extraneous line
343 [ -z "$_ipx" ] && break
345 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
348 ifconfig $1 ${_ipx} delete
358 # Rename all requested interfaces.
363 _ifn_list="`ifconfig -l`"
364 [ -z "$_ifn_list" ] && return 0
365 for _if in ${_ifn_list} ; do
366 eval _ifname=\$ifconfig_${_if}_name
367 if [ ! -z "$_ifname" ]; then
368 ifconfig $_if name $_ifname
375 # list_net_interfaces type
376 # List all network interfaces. The type of interface returned
377 # can be controlled by the type argument. The type
378 # argument can be any of the following:
379 # nodhcp - all interfaces, excluding DHCP configured interfaces
380 # dhcp - list only DHCP configured interfaces
381 # If no argument is specified all network interfaces are output.
382 # Note that the list will include cloned interfaces if applicable.
383 # Cloned interfaces must already exist to have a chance to appear
384 # in the list if ${network_interfaces} is set to `auto'.
386 list_net_interfaces()
390 # Get a list of ALL the interfaces. NOTE: cloned interfaces
391 # have already been configured so they should show up in the
392 # ifconfig -l output.
394 case ${network_interfaces} in
396 _tmplist="`ifconfig -l`"
399 _tmplist="${network_interfaces} ${cloned_interfaces}"
403 if [ -z "$type" ]; then
408 # Separate out dhcp and non-dhcp interfaces
412 for _if in ${_tmplist} ; do
413 eval _ifarg="\$ifconfig_${_if}"
416 _dhcplist="${_dhcplist}${_aprefix}${_if}"
417 [ -z "$_aprefix" ] && _aprefix=' '
420 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
421 [ -z "$_bprefix" ] && _bprefix=' '
439 if [ $1 -lt 10 ]; then
458 dig=`hexdigit $((${val} & 15))`
461 while [ ${val} -gt 0 ]; do
462 dig=`hexdigit $((${val} & 15))`
470 # Setup the interfaces for IPv6
471 network6_interface_setup()
475 case ${ipv6_gateway_enable} in
483 for i in $interfaces; do
485 eval prefix=\$ipv6_prefix_$i
486 if [ -n "${prefix}" ]; then
489 laddr=`network6_getladdr $i`
490 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
491 for j in ${prefix}; do
492 address=$j\:${hostid}
493 ifconfig $i inet6 ${address} prefixlen 64 alias
495 case ${ipv6_gateway_enable} in
497 # subnet-router anycast address
499 ifconfig $i inet6 $j:: prefixlen 64 \
505 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
506 if [ -n "${ipv6_ifconfig}" ]; then
509 ifconfig $i inet6 ${ipv6_ifconfig} alias
512 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
515 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
518 rtsol_interfaces="${rtsol_interfaces} ${i}"
526 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
527 # Act as endhost - automatically configured.
528 # You can configure only single interface, as
529 # specification assumes that autoconfigured host has
530 # single interface only.
531 sysctl net.inet6.ip6.accept_rtadv=1
532 set ${rtsol_interfaces}
537 for i in $interfaces; do
540 eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
541 if [ -z "${ipv6_ifconfig}" ]; then
544 ifconfig $i inet6 ${ipv6_ifconfig} alias
545 alias=$((${alias} + 1))
550 # Setup IPv6 to IPv4 mapping
553 case ${stf_interface_ipv4addr} in
557 # assign IPv6 addr and interface route for 6to4 interface
558 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
561 set ${stf_interface_ipv4addr}
563 hexfrag1=`hexprint $(($1*256 + $2))`
564 hexfrag2=`hexprint $(($3*256 + $4))`
565 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
566 case ${stf_interface_ipv6_ifid} in
567 [Aa][Uu][Tt][Oo] | '')
568 for i in ${ipv6_network_interfaces}; do
569 laddr=`network6_getladdr ${i}`
578 stf_interface_ipv6_ifid=`expr "${laddr}" : \
579 'fe80::\(.*\)%\(.*\)'`
580 case ${stf_interface_ipv6_ifid} in
582 stf_interface_ipv6_ifid=0:0:0:1
587 ifconfig stf0 create >/dev/null 2>&1
588 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
589 prefixlen ${stf_prefixlen}
590 # disallow packets to malicious 6to4 prefix
591 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
592 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
593 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
594 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
599 # Setup static routes
600 network6_static_routes_setup()
602 # Set up any static routes.
603 case ${ipv6_defaultrouter} in
607 ipv6_static_routes="default ${ipv6_static_routes}"
608 ipv6_route_default="default ${ipv6_defaultrouter}"
611 case ${ipv6_static_routes} in
615 for i in ${ipv6_static_routes}; do
616 eval ipv6_route_args=\$ipv6_route_${i}
617 route add -inet6 ${ipv6_route_args}
624 network6_faith_setup()
626 case ${ipv6_faith_prefix} in
630 sysctl net.inet6.ip6.keepfaith=1
631 ifconfig faith0 create >/dev/null 2>&1
633 for prefix in ${ipv6_faith_prefix}; do
634 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
640 prefix=`expr "${prefix}" : \
641 "\(.*\)/${prefixlen}"`
644 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
645 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
652 # Install the "default interface" to kernel, which will be used
653 # as the default route when there's no router.
654 network6_default_interface_setup()
656 # Choose IPv6 default interface if it is not clearly specified.
657 case ${ipv6_default_interface} in
659 for i in ${ipv6_network_interfaces}; do
665 laddr=`network6_getladdr $i exclude_tentative`
670 ipv6_default_interface=$i
678 # Disallow unicast packets without outgoing scope identifiers,
679 # or route such packets to a "default" interface, if it is specified.
680 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
681 case ${ipv6_default_interface} in
683 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
686 laddr=`network6_getladdr ${ipv6_default_interface}`
687 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
690 # Disable installing the default interface with the
691 # case net.inet6.ip6.forwarding=0 and
692 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
693 # between the default router list and the manual
694 # configured default route.
695 case ${ipv6_gateway_enable} in
699 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
701 ndp -I ${ipv6_default_interface}
711 ifconfig $1 2>/dev/null | while read proto addr rest; do