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 eval "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 /etc/rc.d/dhclient start $1
71 # Remove all inet entries from the $if interface. It returns
72 # 0 if inet entries were found and removed. It returns 1 if
73 # no entries were found or they could not be removed.
77 [ -z "$1" ] && return 1
81 inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
85 for _inet in $inetList ; do
86 # get rid of extraneous line
87 [ -z "$_inet" ] && break
89 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
92 ifconfig $1 ${_inet} delete
99 /etc/rc.d/wpa_supplicant stop $1
104 /etc/rc.d/dhclient stop $1
111 # _ifconfig_getargs if
112 # Echos the arguments for the supplied interface to stdout.
113 # returns 1 if empty. In general, ifconfig_getargs should be used
118 if [ -z "$_ifn" ]; then
122 eval _args=\${ifconfig_$1-$ifconfig_DEFAULT}
127 # ifconfig_getargs if
128 # Takes the result from _ifconfig_getargs and removes pseudo
129 # args such as DHCP and WPA.
132 _tmpargs=`_ifconfig_getargs $1`
133 if [ $? -eq 1 ]; then
138 for _arg in $_tmpargs; do
142 [Nn][Oo][Aa][Uu][Tt][Oo])
156 # Returns 0 if the interface should be automaticly configured at
157 # boot time and 1 otherwise.
160 _tmpargs=`_ifconfig_getargs $1`
161 for _arg in $_tmpargs; do
163 [Nn][Oo][Aa][Uu][Tt][Oo])
172 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
175 _tmpargs=`_ifconfig_getargs $1`
176 for _arg in $_tmpargs; do
187 # Returns 0 if the interface is a WPA interface and 1 otherwise.
190 _tmpargs=`_ifconfig_getargs $1`
191 for _arg in $_tmpargs; do
202 # add IPv4 addresses to the interface $if
207 ipv4_addrs_common ${_if} alias
211 # remove IPv4 addresses from the interface $if
216 ipv4_addrs_common ${_if} -alias
219 # ipv4_addrs_common if action
220 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
221 # and use $action to add or remove IPv4 addresses from $if.
229 eval cidr_addr=\${ipv4_addrs_${_if}}
231 for _cidr in ${cidr_addr}; do
233 _netmask="/"${_cidr##*/}
234 _range=${_ipaddr##*.}
239 # clear netmask when removing aliases
240 if [ "${_action}" = "-alias" ]; then
245 while [ "${_ipcount}" -le "${_iphigh}" ]; do
246 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
247 _ipcount=$((${_ipcount}+1))
250 # only the first ipaddr in a subnet need the real netmask
251 if [ "${_action}" != "-alias" ]; then
260 # Configure aliases for network interface $if.
261 # It returns 0 if at least one alias was configured or
262 # 1 if there were none.
269 eval ifconfig_args=\$ifconfig_$1_alias${alias}
270 if [ -n "${ifconfig_args}" ]; then
271 ifconfig $1 ${ifconfig_args} alias
272 alias=$((${alias} + 1))
282 # Remove aliases for network interface $if.
283 # It returns 0 if at least one alias was removed or
284 # 1 if there were none.
291 eval ifconfig_args=\$ifconfig_$1_alias${alias}
292 if [ -n "${ifconfig_args}" ]; then
293 ifconfig $1 ${ifconfig_args} -alias
294 alias=$((${alias} + 1))
304 # Evaluate a startup script for the $if interface.
305 # It returns 0 if a script was found and processed or
306 # 1 if no script was found.
310 if [ -r /etc/start_if.$1 ]; then
318 # Evaluate a shutdown script for the $if interface.
319 # It returns 0 if a script was found and processed or
320 # 1 if no script was found.
324 if [ -r /etc/stop_if.$1 ]; then
331 # Create cloneable interfaces.
337 for ifn in ${cloned_interfaces}; do
338 ifconfig ${ifn} create
339 if [ $? -eq 0 ]; then
340 _list="${_list}${_prefix}${ifn}"
341 [ -z "$_prefix" ] && _prefix=' '
344 debug "Cloned: ${_list}"
347 # Destroy cloned interfaces. Destroyed interfaces are echoed
348 # to standard output.
354 for ifn in ${cloned_interfaces}; do
355 ifconfig ${ifn} destroy
356 if [ $? -eq 0 ]; then
357 _list="${_list}${_prefix}${ifn}"
358 [ -z "$_prefix" ] && _prefix=' '
361 debug "Destroyed clones: ${_list}"
365 case ${gif_interfaces} in
369 for i in ${gif_interfaces}; do
370 eval peers=\$gifconfig_$i
376 ifconfig $i create >/dev/null 2>&1
377 ifconfig $i tunnel ${peers}
388 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
389 # arguments were found and configured; returns 1 otherwise.
394 eval ifconfig_args=\$ifconfig_${ifn}_ipx
395 if [ -n "${ifconfig_args}" ]; then
396 ifconfig ${ifn} ${ifconfig_args}
403 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
404 # addresses were found and unconfigured. It returns 1, otherwise.
408 [ -z "$1" ] && return 1
412 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
416 for _ipx in $ipxList ; do
417 # get rid of extraneous line
418 [ -z "$_ipx" ] && break
420 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
423 ifconfig $1 ${_ipx} delete
433 # Rename all requested interfaces.
438 _ifn_list="`ifconfig -l`"
439 [ -z "$_ifn_list" ] && return 0
440 for _if in ${_ifn_list} ; do
441 eval _ifname=\$ifconfig_${_if}_name
442 if [ ! -z "$_ifname" ]; then
443 ifconfig $_if name $_ifname
450 # list_net_interfaces type
451 # List all network interfaces. The type of interface returned
452 # can be controlled by the type argument. The type
453 # argument can be any of the following:
454 # nodhcp - all interfaces, excluding DHCP configured interfaces
455 # dhcp - list only DHCP configured interfaces
456 # If no argument is specified all network interfaces are output.
457 # Note that the list will include cloned interfaces if applicable.
458 # Cloned interfaces must already exist to have a chance to appear
459 # in the list if ${network_interfaces} is set to `auto'.
461 list_net_interfaces()
465 # Get a list of ALL the interfaces and make lo0 first if it's there.
467 case ${network_interfaces} in
470 _autolist="`ifconfig -l`"
472 for _if in ${_autolist} ; do
474 if [ "$_if" = "lo0" ]; then
477 _tmplist="${_tmplist}${_prefix}${_if}"
478 [ -z "$_prefix" ] && _prefix=' '
482 _tmplist="${_lo}${_tmplist}"
485 _tmplist="${network_interfaces} ${cloned_interfaces}"
489 if [ -z "$type" ]; then
494 # Separate out dhcp and non-dhcp interfaces
498 for _if in ${_tmplist} ; do
500 _dhcplist="${_dhcplist}${_aprefix}${_if}"
501 [ -z "$_aprefix" ] && _aprefix=' '
502 elif [ -n "`_ifconfig_getargs $if`" ]; then
503 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
504 [ -z "$_bprefix" ] && _bprefix=' '
521 if [ $1 -lt 10 ]; then
540 dig=`hexdigit $((${val} & 15))`
543 while [ ${val} -gt 0 ]; do
544 dig=`hexdigit $((${val} & 15))`
552 # Setup the interfaces for IPv6
553 network6_interface_setup()
557 case ${ipv6_gateway_enable} in
565 for i in $interfaces; do
567 eval prefix=\$ipv6_prefix_$i
568 if [ -n "${prefix}" ]; then
571 laddr=`network6_getladdr $i`
572 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
573 for j in ${prefix}; do
574 address=$j\:${hostid}
575 ifconfig $i inet6 ${address} prefixlen 64 alias
577 case ${ipv6_gateway_enable} in
579 # subnet-router anycast address
581 ifconfig $i inet6 $j:: prefixlen 64 \
587 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
588 if [ -n "${ipv6_ifconfig}" ]; then
591 ifconfig $i inet6 ${ipv6_ifconfig} alias
594 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
597 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
600 rtsol_interfaces="${rtsol_interfaces} ${i}"
608 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
609 # Act as endhost - automatically configured.
610 # You can configure only single interface, as
611 # specification assumes that autoconfigured host has
612 # single interface only.
613 sysctl net.inet6.ip6.accept_rtadv=1
614 set ${rtsol_interfaces}
616 rtsol ${rtsol_flags} $1
619 for i in $interfaces; do
622 eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
623 if [ -z "${ipv6_ifconfig}" ]; then
626 ifconfig $i inet6 ${ipv6_ifconfig} alias
627 alias=$((${alias} + 1))
632 # Setup IPv6 to IPv4 mapping
635 case ${stf_interface_ipv4addr} in
639 # assign IPv6 addr and interface route for 6to4 interface
640 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
643 set ${stf_interface_ipv4addr}
645 hexfrag1=`hexprint $(($1*256 + $2))`
646 hexfrag2=`hexprint $(($3*256 + $4))`
647 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
648 case ${stf_interface_ipv6_ifid} in
649 [Aa][Uu][Tt][Oo] | '')
650 for i in ${ipv6_network_interfaces}; do
651 laddr=`network6_getladdr ${i}`
660 stf_interface_ipv6_ifid=`expr "${laddr}" : \
661 'fe80::\(.*\)%\(.*\)'`
662 case ${stf_interface_ipv6_ifid} in
664 stf_interface_ipv6_ifid=0:0:0:1
669 ifconfig stf0 create >/dev/null 2>&1
670 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
671 prefixlen ${stf_prefixlen}
672 # disallow packets to malicious 6to4 prefix
673 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
674 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
675 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
676 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
681 # Setup static routes
682 network6_static_routes_setup()
684 # Set up any static routes.
685 case ${ipv6_defaultrouter} in
689 ipv6_static_routes="default ${ipv6_static_routes}"
690 ipv6_route_default="default ${ipv6_defaultrouter}"
693 case ${ipv6_static_routes} in
697 for i in ${ipv6_static_routes}; do
698 eval ipv6_route_args=\$ipv6_route_${i}
699 route add -inet6 ${ipv6_route_args}
706 network6_faith_setup()
708 case ${ipv6_faith_prefix} in
712 sysctl net.inet6.ip6.keepfaith=1
713 ifconfig faith0 create >/dev/null 2>&1
715 for prefix in ${ipv6_faith_prefix}; do
716 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
722 prefix=`expr "${prefix}" : \
723 "\(.*\)/${prefixlen}"`
726 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
727 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
734 # Install the "default interface" to kernel, which will be used
735 # as the default route when there's no router.
736 network6_default_interface_setup()
738 # Choose IPv6 default interface if it is not clearly specified.
739 case ${ipv6_default_interface} in
741 for i in ${ipv6_network_interfaces}; do
747 laddr=`network6_getladdr $i exclude_tentative`
752 ipv6_default_interface=$i
760 # Disallow unicast packets without outgoing scope identifiers,
761 # or route such packets to a "default" interface, if it is specified.
762 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
763 case ${ipv6_default_interface} in
765 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
768 laddr=`network6_getladdr ${ipv6_default_interface}`
769 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
772 # Disable installing the default interface with the
773 # case net.inet6.ip6.forwarding=0 and
774 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
775 # between the default router list and the manual
776 # configured default route.
777 case ${ipv6_gateway_enable} in
781 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
783 ndp -I ${ipv6_default_interface}
793 ifconfig $1 2>/dev/null | while read proto addr rest; do