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 # returns 1 if wpa_supplicant or dhclient was stopped or
74 # the interface exists.
78 [ -z "$1" ] && return 1
82 /etc/rc.d/wpa_supplicant stop $1
87 /etc/rc.d/dhclient stop $1
99 # get_if_var if var [default]
100 # Return the value of the pseudo-hash corresponding to $if where
101 # $var is a string containg the sub-string "IF" which will be
102 # replaced with $if after the characters defined in _punct are
103 # replaced with '_'. If the variable is unset, replace it with
107 if [ $# -ne 2 -a $# -ne 3 ]; then
108 err 3 'USAGE: get_if_var name var [default]'
113 for _punct_c in $_punct; do
114 _if=`ltr ${_if} ${_punct_c} '_'`
121 eval echo \${${prefix}${_if}${suffix}-${_default}}
124 # _ifconfig_getargs if
125 # Echos the arguments for the supplied interface to stdout.
126 # returns 1 if empty. In general, ifconfig_getargs should be used
131 if [ -z "$_ifn" ]; then
135 get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
138 # ifconfig_getargs if
139 # Takes the result from _ifconfig_getargs and removes pseudo
140 # args such as DHCP and WPA.
143 _tmpargs=`_ifconfig_getargs $1`
144 if [ $? -eq 1 ]; then
149 for _arg in $_tmpargs; do
152 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
153 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
154 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
166 # Returns 0 if the interface should be automaticly configured at
167 # boot time and 1 otherwise.
170 _tmpargs=`_ifconfig_getargs $1`
171 for _arg in $_tmpargs; do
173 [Nn][Oo][Aa][Uu][Tt][Oo])
182 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
185 _tmpargs=`_ifconfig_getargs $1`
186 for _arg in $_tmpargs; do
191 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
194 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
203 # Returns 0 if the interface should be configured synchronously and
207 _tmpargs=`_ifconfig_getargs $1`
208 for _arg in $_tmpargs; do
210 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
213 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
218 if checkyesno synchronous_dhclient; then
226 # Returns 0 if the interface is a WPA interface and 1 otherwise.
229 _tmpargs=`_ifconfig_getargs $1`
230 for _arg in $_tmpargs; do
241 # Returns 0 if the interface should be configured for IPv6 and
245 if ! checkyesno ipv6_enable; then
248 case "${ipv6_network_interfaces}" in
256 for v6if in ${ipv6_network_interfaces}; do
257 if [ "${v6if}" = "${1}" ]; then
265 # Returns 0 if the interface exists and 1 otherwise.
268 ifconfig -n $1 > /dev/null 2>&1
272 # add IPv4 addresses to the interface $if
277 ipv4_addrs_common ${_if} alias
281 # remove IPv4 addresses from the interface $if
288 ifexists ${_if} || return 1
290 inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
294 for _inet in $inetList ; do
295 # get rid of extraneous line
296 [ -z "$_inet" ] && break
298 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
301 ifconfig ${_if} ${_inet} delete
307 ifalias_down ${_if} && _ret=0
308 ipv4_addrs_common ${_if} -alias && _ret=0
313 # ipv4_addrs_common if action
314 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
315 # and use $action to add or remove IPv4 addresses from $if.
323 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
325 for _cidr in ${cidr_addr}; do
327 _netmask="/"${_cidr##*/}
328 _range=${_ipaddr##*.}
333 # clear netmask when removing aliases
334 if [ "${_action}" = "-alias" ]; then
339 while [ "${_ipcount}" -le "${_iphigh}" ]; do
340 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
341 _ipcount=$((${_ipcount}+1))
344 # only the first ipaddr in a subnet need the real netmask
345 if [ "${_action}" != "-alias" ]; then
354 # Configure aliases for network interface $if.
355 # It returns 0 if at least one alias was configured or
356 # 1 if there were none.
363 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
364 if [ -n "${ifconfig_args}" ]; then
365 ifconfig $1 ${ifconfig_args} alias
366 alias=$((${alias} + 1))
376 # Remove aliases for network interface $if.
377 # It returns 0 if at least one alias was removed or
378 # 1 if there were none.
385 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
386 if [ -n "${ifconfig_args}" ]; then
387 ifconfig $1 ${ifconfig_args} -alias
388 alias=$((${alias} + 1))
398 # Evaluate a startup script for the $if interface.
399 # It returns 0 if a script was found and processed or
400 # 1 if no script was found.
404 if [ -r /etc/start_if.$1 ]; then
412 # Evaluate a shutdown script for the $if interface.
413 # It returns 0 if a script was found and processed or
414 # 1 if no script was found.
418 if [ -r /etc/stop_if.$1 ]; then
425 # Create cloneable interfaces.
431 for ifn in ${cloned_interfaces}; do
432 ifconfig ${ifn} create
433 if [ $? -eq 0 ]; then
434 _list="${_list}${_prefix}${ifn}"
435 [ -z "$_prefix" ] && _prefix=' '
438 debug "Cloned: ${_list}"
441 # Destroy cloned interfaces. Destroyed interfaces are echoed
442 # to standard output.
448 for ifn in ${cloned_interfaces}; do
449 ifconfig ${ifn} destroy
450 if [ $? -eq 0 ]; then
451 _list="${_list}${_prefix}${ifn}"
452 [ -z "$_prefix" ] && _prefix=' '
455 debug "Destroyed clones: ${_list}"
458 # Create netgraph nodes.
461 ngctl -f - 2> /dev/null <<EOF
468 ng_mkpeer $* | while read line; do
469 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
470 if [ -n "${t}" ]; then
478 # The following must be removed once RELENG_7 is branched.
479 case ${gif_interfaces} in
481 warn "gif_interfaces=\"NO\" is deprecated, use gif_interfaces=\"\" instead."
486 for i in ${gif_interfaces}; do
487 peers=`get_if_var $i gifconfig_IF`
493 ifconfig $i create >/dev/null 2>&1
494 ifconfig $i tunnel ${peers}
502 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
503 # arguments were found and configured; returns !0 otherwise.
505 local req_iface iface bogus
508 ngctl shutdown ${req_iface}: > /dev/null 2>&1
512 iface=`ng_create_one fec dummy fec`
513 if [ -z "${iface}" ]; then
516 if [ "${iface}" = "${req_iface}" ]; then
519 bogus="${bogus} ${iface}"
522 for iface in ${bogus}; do
523 ngctl shutdown ${iface}:
528 for i in ${fec_interfaces}; do
530 for j in `get_if_var $i fecconfig_IF`; do
536 ngctl msg ${i}: add_iface "\"${j}\""
545 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
546 # arguments were found and configured; returns 1 otherwise.
551 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
552 if [ -n "${ifconfig_args}" ]; then
553 ifconfig ${ifn} ${ifconfig_args}
560 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
561 # addresses were found and unconfigured. It returns 1, otherwise.
565 [ -z "$1" ] && return 1
569 ifexists $1 || return 1
571 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
575 for _ipx in $ipxList ; do
576 # get rid of extraneous line
577 [ -z "$_ipx" ] && break
579 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
582 ifconfig $1 ${_ipx} delete
592 # Rename all requested interfaces.
597 _ifn_list="`ifconfig -l`"
598 [ -z "$_ifn_list" ] && return 0
599 for _if in ${_ifn_list} ; do
600 _ifname=`get_if_var $_if ifconfig_IF_name`
601 if [ ! -z "$_ifname" ]; then
602 ifconfig $_if name $_ifname
609 # list_net_interfaces type
610 # List all network interfaces. The type of interface returned
611 # can be controlled by the type argument. The type
612 # argument can be any of the following:
613 # nodhcp - all interfaces, excluding DHCP configured interfaces
614 # dhcp - list only DHCP configured interfaces
615 # If no argument is specified all network interfaces are output.
616 # Note that the list will include cloned interfaces if applicable.
617 # Cloned interfaces must already exist to have a chance to appear
618 # in the list if ${network_interfaces} is set to `auto'.
620 list_net_interfaces()
624 # Get a list of ALL the interfaces and make lo0 first if it's there.
626 case ${network_interfaces} in
629 _autolist="`ifconfig -l`"
631 for _if in ${_autolist} ; do
633 if [ "$_if" = "lo0" ]; then
636 _tmplist="${_tmplist}${_prefix}${_if}"
637 [ -z "$_prefix" ] && _prefix=' '
641 _tmplist="${_lo}${_tmplist}"
644 _tmplist="${network_interfaces} ${cloned_interfaces}"
648 if [ -z "$type" ]; then
653 # Separate out dhcp and non-dhcp interfaces
657 for _if in ${_tmplist} ; do
659 _dhcplist="${_dhcplist}${_aprefix}${_if}"
660 [ -z "$_aprefix" ] && _aprefix=' '
661 elif [ -n "`_ifconfig_getargs $_if`" ]; then
662 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
663 [ -z "$_bprefix" ] && _bprefix=' '
680 if [ $1 -lt 10 ]; then
699 dig=`hexdigit $((${val} & 15))`
702 while [ ${val} -gt 0 ]; do
703 dig=`hexdigit $((${val} & 15))`
711 # Setup the interfaces for IPv6
712 network6_interface_setup()
716 case ${ipv6_gateway_enable} in
724 for i in $interfaces; do
726 prefix=`get_if_var $i ipv6_prefix_IF`
727 if [ -n "${prefix}" ]; then
730 laddr=`network6_getladdr $i`
731 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
732 for j in ${prefix}; do
733 address=$j\:${hostid}
734 ifconfig $i inet6 ${address} prefixlen 64 alias
736 case ${ipv6_gateway_enable} in
738 # subnet-router anycast address
740 ifconfig $i inet6 $j:: prefixlen 64 \
746 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
747 if [ -n "${ipv6_ifconfig}" ]; then
750 ifconfig $i inet6 ${ipv6_ifconfig} alias
753 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
756 lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*|pflog[0-9]*|pfsync[0-9]*)
759 rtsol_interfaces="${rtsol_interfaces} ${i}"
767 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
768 # Act as endhost - automatically configured.
769 # You can configure only single interface, as
770 # specification assumes that autoconfigured host has
771 # single interface only.
772 sysctl net.inet6.ip6.accept_rtadv=1
773 set ${rtsol_interfaces}
775 rtsol ${rtsol_flags} $1
778 for i in $interfaces; do
781 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
782 if [ -z "${ipv6_ifconfig}" ]; then
785 ifconfig $i inet6 ${ipv6_ifconfig} alias
786 alias=$((${alias} + 1))
791 # Setup IPv6 to IPv4 mapping
794 case ${stf_interface_ipv4addr} in
798 # assign IPv6 addr and interface route for 6to4 interface
799 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
802 set ${stf_interface_ipv4addr}
804 hexfrag1=`hexprint $(($1*256 + $2))`
805 hexfrag2=`hexprint $(($3*256 + $4))`
806 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
807 case ${stf_interface_ipv6_ifid} in
808 [Aa][Uu][Tt][Oo] | '')
809 for i in ${ipv6_network_interfaces}; do
810 laddr=`network6_getladdr ${i}`
819 stf_interface_ipv6_ifid=`expr "${laddr}" : \
820 'fe80::\(.*\)%\(.*\)'`
821 case ${stf_interface_ipv6_ifid} in
823 stf_interface_ipv6_ifid=0:0:0:1
828 ifconfig stf0 create >/dev/null 2>&1
829 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
830 prefixlen ${stf_prefixlen}
831 # disallow packets to malicious 6to4 prefix
832 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
833 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
834 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
835 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
840 # Setup static routes
841 network6_static_routes_setup()
843 # Set up any static routes.
844 case ${ipv6_defaultrouter} in
848 ipv6_static_routes="default ${ipv6_static_routes}"
849 ipv6_route_default="default ${ipv6_defaultrouter}"
852 case ${ipv6_static_routes} in
856 for i in ${ipv6_static_routes}; do
857 ipv6_route_args=`get_if_var $i ipv6_route_IF`
858 route add -inet6 ${ipv6_route_args}
865 network6_faith_setup()
867 case ${ipv6_faith_prefix} in
871 sysctl net.inet6.ip6.keepfaith=1
872 ifconfig faith0 create >/dev/null 2>&1
874 for prefix in ${ipv6_faith_prefix}; do
875 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
881 prefix=`expr "${prefix}" : \
882 "\(.*\)/${prefixlen}"`
885 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
886 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
893 # Install the "default interface" to kernel, which will be used
894 # as the default route when there's no router.
895 network6_default_interface_setup()
897 # Choose IPv6 default interface if it is not clearly specified.
898 case ${ipv6_default_interface} in
900 for i in ${ipv6_network_interfaces}; do
906 laddr=`network6_getladdr $i exclude_tentative`
911 ipv6_default_interface=$i
919 # Disallow unicast packets without outgoing scope identifiers,
920 # or route such packets to a "default" interface, if it is specified.
921 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
922 case ${ipv6_default_interface} in
924 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
927 laddr=`network6_getladdr ${ipv6_default_interface}`
928 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
931 # Disable installing the default interface with the
932 # case net.inet6.ip6.forwarding=0 and
933 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
934 # between the default router list and the manual
935 # configured default route.
936 case ${ipv6_gateway_enable} in
940 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
942 ndp -I ${ipv6_default_interface}
952 ifconfig $1 2>/dev/null | while read proto addr rest; do