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 /etc/rc.d/wpa_supplicant start $1
53 _cfg=0 # XXX: not sure this should count
57 if [ $_cfg -ne 0 ] ; then
60 if syncdhcpif $1; then
61 /etc/rc.d/dhclient start $1
70 # returns 1 if wpa_supplicant or dhclient was stopped or
71 # the interface exists.
75 [ -z "$1" ] && return 1
79 /etc/rc.d/wpa_supplicant stop $1
84 /etc/rc.d/dhclient stop $1
96 # get_if_var if var [default]
97 # Return the value of the pseudo-hash corresponding to $if where
98 # $var is a string containg the sub-string "IF" which will be
99 # replaced with $if after the characters defined in _punct are
100 # replaced with '_'. If the variable is unset, replace it with
104 if [ $# -ne 2 -a $# -ne 3 ]; then
105 err 3 'USAGE: get_if_var name var [default]'
110 for _punct_c in $_punct; do
111 _if=`ltr ${_if} ${_punct_c} '_'`
118 eval echo \${${prefix}${_if}${suffix}-${_default}}
121 # _ifconfig_getargs if
122 # Echos the arguments for the supplied interface to stdout.
123 # returns 1 if empty. In general, ifconfig_getargs should be used
128 if [ -z "$_ifn" ]; then
132 get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
135 # ifconfig_getargs if
136 # Takes the result from _ifconfig_getargs and removes pseudo
137 # args such as DHCP and WPA.
140 _tmpargs=`_ifconfig_getargs $1`
141 if [ $? -eq 1 ]; then
146 for _arg in $_tmpargs; do
149 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
150 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
151 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
163 # Returns 0 if the interface should be automaticly configured at
164 # boot time and 1 otherwise.
167 _tmpargs=`_ifconfig_getargs $1`
168 for _arg in $_tmpargs; do
170 [Nn][Oo][Aa][Uu][Tt][Oo])
179 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
182 _tmpargs=`_ifconfig_getargs $1`
183 for _arg in $_tmpargs; do
188 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
191 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
200 # Returns 0 if the interface should be configured synchronously and
204 _tmpargs=`_ifconfig_getargs $1`
205 for _arg in $_tmpargs; do
207 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
210 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
215 if checkyesno synchronous_dhclient; then
223 # Returns 0 if the interface is a WPA interface and 1 otherwise.
226 _tmpargs=`_ifconfig_getargs $1`
227 for _arg in $_tmpargs; do
238 # Returns 0 if the interface should be configured for IPv6 and
242 if ! checkyesno ipv6_enable; then
245 case "${ipv6_network_interfaces}" in
253 for v6if in ${ipv6_network_interfaces}; do
254 if [ "${v6if}" = "${1}" ]; then
262 # Returns 0 if the interface exists and 1 otherwise.
265 ifconfig -n $1 > /dev/null 2>&1
269 # add IPv4 addresses to the interface $if
274 ipv4_addrs_common ${_if} alias
278 # remove IPv4 addresses from the interface $if
285 ifexists ${_if} || return 1
287 inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
291 for _inet in $inetList ; do
292 # get rid of extraneous line
293 [ -z "$_inet" ] && break
295 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
298 ifconfig ${_if} ${_inet} delete
304 ifalias_down ${_if} && _ret=0
305 ipv4_addrs_common ${_if} -alias && _ret=0
310 # ipv4_addrs_common if action
311 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
312 # and use $action to add or remove IPv4 addresses from $if.
320 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
322 for _cidr in ${cidr_addr}; do
324 _netmask="/"${_cidr##*/}
325 _range=${_ipaddr##*.}
330 # clear netmask when removing aliases
331 if [ "${_action}" = "-alias" ]; then
336 while [ "${_ipcount}" -le "${_iphigh}" ]; do
337 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
338 _ipcount=$((${_ipcount}+1))
341 # only the first ipaddr in a subnet need the real netmask
342 if [ "${_action}" != "-alias" ]; then
351 # Configure aliases for network interface $if.
352 # It returns 0 if at least one alias was configured or
353 # 1 if there were none.
360 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
361 if [ -n "${ifconfig_args}" ]; then
362 ifconfig $1 ${ifconfig_args} alias
363 alias=$((${alias} + 1))
373 # Remove aliases for network interface $if.
374 # It returns 0 if at least one alias was removed or
375 # 1 if there were none.
382 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
383 if [ -n "${ifconfig_args}" ]; then
384 ifconfig $1 ${ifconfig_args} -alias
385 alias=$((${alias} + 1))
395 # Evaluate a startup script for the $if interface.
396 # It returns 0 if a script was found and processed or
397 # 1 if no script was found.
401 if [ -r /etc/start_if.$1 ]; then
409 # Evaluate a shutdown script for the $if interface.
410 # It returns 0 if a script was found and processed or
411 # 1 if no script was found.
415 if [ -r /etc/stop_if.$1 ]; then
422 # Create cloneable interfaces.
428 for ifn in ${cloned_interfaces}; do
429 ifconfig ${ifn} create
430 if [ $? -eq 0 ]; then
431 _list="${_list}${_prefix}${ifn}"
432 [ -z "$_prefix" ] && _prefix=' '
435 debug "Cloned: ${_list}"
438 # Destroy cloned interfaces. Destroyed interfaces are echoed
439 # to standard output.
445 for ifn in ${cloned_interfaces}; do
446 ifconfig ${ifn} destroy
447 if [ $? -eq 0 ]; then
448 _list="${_list}${_prefix}${ifn}"
449 [ -z "$_prefix" ] && _prefix=' '
452 debug "Destroyed clones: ${_list}"
455 # Create netgraph nodes.
458 ngctl -f - 2> /dev/null <<EOF
465 ng_mkpeer $* | while read line; do
466 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
467 if [ -n "${t}" ]; then
475 # The following must be removed once RELENG_7 is branched.
476 case ${gif_interfaces} in
478 warn "gif_interfaces=\"NO\" is deprecated, use gif_interfaces=\"\" instead."
483 for i in ${gif_interfaces}; do
484 peers=`get_if_var $i gifconfig_IF`
490 ifconfig $i create >/dev/null 2>&1
491 ifconfig $i tunnel ${peers}
499 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
500 # arguments were found and configured; returns !0 otherwise.
502 local req_iface iface bogus
505 ngctl shutdown ${req_iface}: > /dev/null 2>&1
509 iface=`ng_create_one fec dummy fec`
510 if [ -z "${iface}" ]; then
513 if [ "${iface}" = "${req_iface}" ]; then
516 bogus="${bogus} ${iface}"
519 for iface in ${bogus}; do
520 ngctl shutdown ${iface}:
525 for i in ${fec_interfaces}; do
527 for j in `get_if_var $i fecconfig_IF`; do
533 ngctl msg ${i}: add_iface "\"${j}\""
542 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
543 # arguments were found and configured; returns 1 otherwise.
548 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
549 if [ -n "${ifconfig_args}" ]; then
550 ifconfig ${ifn} ${ifconfig_args}
557 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
558 # addresses were found and unconfigured. It returns 1, otherwise.
562 [ -z "$1" ] && return 1
566 ifexists $1 || return 1
568 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
572 for _ipx in $ipxList ; do
573 # get rid of extraneous line
574 [ -z "$_ipx" ] && break
576 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
579 ifconfig $1 ${_ipx} delete
589 # Rename all requested interfaces.
594 _ifn_list="`ifconfig -l`"
595 [ -z "$_ifn_list" ] && return 0
596 for _if in ${_ifn_list} ; do
597 _ifname=`get_if_var $_if ifconfig_IF_name`
598 if [ ! -z "$_ifname" ]; then
599 ifconfig $_if name $_ifname
606 # list_net_interfaces type
607 # List all network interfaces. The type of interface returned
608 # can be controlled by the type argument. The type
609 # argument can be any of the following:
610 # nodhcp - all interfaces, excluding DHCP configured interfaces
611 # dhcp - list only DHCP configured interfaces
612 # If no argument is specified all network interfaces are output.
613 # Note that the list will include cloned interfaces if applicable.
614 # Cloned interfaces must already exist to have a chance to appear
615 # in the list if ${network_interfaces} is set to `auto'.
617 list_net_interfaces()
621 # Get a list of ALL the interfaces and make lo0 first if it's there.
623 case ${network_interfaces} in
626 _autolist="`ifconfig -l`"
628 for _if in ${_autolist} ; do
630 if [ "$_if" = "lo0" ]; then
633 _tmplist="${_tmplist}${_prefix}${_if}"
634 [ -z "$_prefix" ] && _prefix=' '
638 _tmplist="${_lo}${_tmplist}"
641 if [ -z "$type" ]; then
642 warn "Values of network_interfaces other than" \
643 "AUTO are deprecated"
645 _tmplist="${network_interfaces} ${cloned_interfaces}"
649 if [ -z "$type" ]; then
654 # Separate out dhcp and non-dhcp interfaces
658 for _if in ${_tmplist} ; do
660 _dhcplist="${_dhcplist}${_aprefix}${_if}"
661 [ -z "$_aprefix" ] && _aprefix=' '
662 elif [ -n "`_ifconfig_getargs $_if`" ]; then
663 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
664 [ -z "$_bprefix" ] && _bprefix=' '
681 if [ $1 -lt 10 ]; then
700 dig=`hexdigit $((${val} & 15))`
703 while [ ${val} -gt 0 ]; do
704 dig=`hexdigit $((${val} & 15))`
712 # Setup the interfaces for IPv6
713 network6_interface_setup()
717 case ${ipv6_gateway_enable} in
725 for i in $interfaces; do
727 prefix=`get_if_var $i ipv6_prefix_IF`
728 if [ -n "${prefix}" ]; then
731 laddr=`network6_getladdr $i`
732 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
733 for j in ${prefix}; do
734 address=$j\:${hostid}
735 ifconfig $i inet6 ${address} prefixlen 64 alias
737 case ${ipv6_gateway_enable} in
739 # subnet-router anycast address
741 ifconfig $i inet6 $j:: prefixlen 64 \
747 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
748 if [ -n "${ipv6_ifconfig}" ]; then
751 ifconfig $i inet6 ${ipv6_ifconfig} alias
754 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
757 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]*)
760 rtsol_interfaces="${rtsol_interfaces} ${i}"
768 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
769 # Act as endhost - automatically configured.
770 # You can configure only single interface, as
771 # specification assumes that autoconfigured host has
772 # single interface only.
773 sysctl net.inet6.ip6.accept_rtadv=1
774 set ${rtsol_interfaces}
776 rtsol ${rtsol_flags} $1
779 for i in $interfaces; do
782 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
783 if [ -z "${ipv6_ifconfig}" ]; then
786 ifconfig $i inet6 ${ipv6_ifconfig} alias
787 alias=$((${alias} + 1))
792 # Setup IPv6 to IPv4 mapping
795 case ${stf_interface_ipv4addr} in
799 # assign IPv6 addr and interface route for 6to4 interface
800 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
803 set ${stf_interface_ipv4addr}
805 hexfrag1=`hexprint $(($1*256 + $2))`
806 hexfrag2=`hexprint $(($3*256 + $4))`
807 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
808 case ${stf_interface_ipv6_ifid} in
809 [Aa][Uu][Tt][Oo] | '')
810 for i in ${ipv6_network_interfaces}; do
811 laddr=`network6_getladdr ${i}`
820 stf_interface_ipv6_ifid=`expr "${laddr}" : \
821 'fe80::\(.*\)%\(.*\)'`
822 case ${stf_interface_ipv6_ifid} in
824 stf_interface_ipv6_ifid=0:0:0:1
829 ifconfig stf0 create >/dev/null 2>&1
830 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
831 prefixlen ${stf_prefixlen}
832 # disallow packets to malicious 6to4 prefix
833 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
834 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
835 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
836 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
841 # Setup static routes
842 network6_static_routes_setup()
844 # Set up any static routes.
845 case ${ipv6_defaultrouter} in
849 ipv6_static_routes="default ${ipv6_static_routes}"
850 ipv6_route_default="default ${ipv6_defaultrouter}"
853 case ${ipv6_static_routes} in
857 for i in ${ipv6_static_routes}; do
858 ipv6_route_args=`get_if_var $i ipv6_route_IF`
859 route add -inet6 ${ipv6_route_args}
866 network6_faith_setup()
868 case ${ipv6_faith_prefix} in
872 sysctl net.inet6.ip6.keepfaith=1
873 ifconfig faith0 create >/dev/null 2>&1
875 for prefix in ${ipv6_faith_prefix}; do
876 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
882 prefix=`expr "${prefix}" : \
883 "\(.*\)/${prefixlen}"`
886 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
887 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
894 # Install the "default interface" to kernel, which will be used
895 # as the default route when there's no router.
896 network6_default_interface_setup()
898 # Choose IPv6 default interface if it is not clearly specified.
899 case ${ipv6_default_interface} in
901 for i in ${ipv6_network_interfaces}; do
907 laddr=`network6_getladdr $i exclude_tentative`
912 ipv6_default_interface=$i
920 # Disallow unicast packets without outgoing scope identifiers,
921 # or route such packets to a "default" interface, if it is specified.
922 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
923 case ${ipv6_default_interface} in
925 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
928 laddr=`network6_getladdr ${ipv6_default_interface}`
929 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
932 # Disable installing the default interface with the
933 # case net.inet6.ip6.forwarding=0 and
934 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
935 # between the default router list and the manual
936 # configured default route.
937 case ${ipv6_gateway_enable} in
941 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
943 ndp -I ${ipv6_default_interface}
953 ifconfig $1 2>/dev/null | while read proto addr rest; do