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 # Bring up and configure an interface. If some configuration is applied
35 # print the interface configuration.
43 [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
45 ifscript_up ${ifn} && cfg=0
46 ifconfig_up ${ifn} && cfg=0
47 ipv4_up ${ifn} && cfg=0
48 ipx_up ${ifn} && cfg=0
49 childif_create ${ifn} && cfg=0
51 if [ "$cfg" -eq 0 ]; then
59 # Shutdown and de-configure an interface. If action is taken print the
68 [ -z "$ifn" ] && return 1
70 ipx_down ${ifn} && cfg=0
71 ipv4_down ${ifn} && cfg=0
72 ifconfig_down ${ifn} && cfg=0
73 ifscript_down ${ifn} && cfg=0
74 childif_destroy ${ifn} && cfg=0
76 if [ "$cfg" -eq 0 ]; then
84 # Evaluate ifconfig(8) arguments for interface $if and
85 # run ifconfig(8) with those arguments. It returns 0 if
86 # arguments were found and executed or 1 if the interface
87 # had no arguments. Pseudo arguments DHCP and WPA are handled
94 ifconfig_args=`ifconfig_getargs $1`
95 if [ -n "${ifconfig_args}" ]; then
96 ifconfig $1 ${ifconfig_args}
102 /etc/rc.d/wpa_supplicant start $1
103 _cfg=0 # XXX: not sure this should count
107 if [ $_cfg -ne 0 ] ; then
110 if syncdhcpif $1; then
111 /etc/rc.d/dhclient start $1
120 # returns 1 if wpa_supplicant or dhclient was stopped or
121 # the interface exists.
125 [ -z "$1" ] && return 1
129 /etc/rc.d/wpa_supplicant stop $1
134 /etc/rc.d/dhclient stop $1
146 # get_if_var if var [default]
147 # Return the value of the pseudo-hash corresponding to $if where
148 # $var is a string containg the sub-string "IF" which will be
149 # replaced with $if after the characters defined in _punct are
150 # replaced with '_'. If the variable is unset, replace it with
154 if [ $# -ne 2 -a $# -ne 3 ]; then
155 err 3 'USAGE: get_if_var name var [default]'
160 for _punct_c in $_punct; do
161 _if=`ltr ${_if} ${_punct_c} '_'`
168 eval echo \${${prefix}${_if}${suffix}-${_default}}
171 # _ifconfig_getargs if
172 # Echos the arguments for the supplied interface to stdout.
173 # returns 1 if empty. In general, ifconfig_getargs should be used
178 if [ -z "$_ifn" ]; then
182 get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
185 # ifconfig_getargs if
186 # Takes the result from _ifconfig_getargs and removes pseudo
187 # args such as DHCP and WPA.
190 _tmpargs=`_ifconfig_getargs $1`
191 if [ $? -eq 1 ]; then
196 for _arg in $_tmpargs; do
199 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
200 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
201 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
213 # Returns 0 if the interface should be automaticly configured at
214 # boot time and 1 otherwise.
217 _tmpargs=`_ifconfig_getargs $1`
218 for _arg in $_tmpargs; do
220 [Nn][Oo][Aa][Uu][Tt][Oo])
229 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
232 _tmpargs=`_ifconfig_getargs $1`
233 for _arg in $_tmpargs; do
238 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
241 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
250 # Returns 0 if the interface should be configured synchronously and
254 _tmpargs=`_ifconfig_getargs $1`
255 for _arg in $_tmpargs; do
257 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
260 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
265 if checkyesno synchronous_dhclient; then
273 # Returns 0 if the interface is a WPA interface and 1 otherwise.
276 _tmpargs=`_ifconfig_getargs $1`
277 for _arg in $_tmpargs; do
288 # Returns 0 if the interface should be configured for IPv6 and
292 if ! checkyesno ipv6_enable; then
295 case "${ipv6_network_interfaces}" in
303 for v6if in ${ipv6_network_interfaces}; do
304 if [ "${v6if}" = "${1}" ]; then
312 # Returns 0 if the interface exists and 1 otherwise.
315 ifconfig -n $1 > /dev/null 2>&1
319 # add IPv4 addresses to the interface $if
324 ipv4_addrs_common ${_if} alias
328 # remove IPv4 addresses from the interface $if
335 ifexists ${_if} || return 1
337 inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
341 for _inet in $inetList ; do
342 # get rid of extraneous line
343 [ -z "$_inet" ] && break
345 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
348 ifconfig ${_if} ${_inet} delete
354 ifalias_down ${_if} && _ret=0
355 ipv4_addrs_common ${_if} -alias && _ret=0
360 # ipv4_addrs_common if action
361 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
362 # and use $action to add or remove IPv4 addresses from $if.
370 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
372 for _cidr in ${cidr_addr}; do
374 _netmask="/"${_cidr##*/}
375 _range=${_ipaddr##*.}
380 # clear netmask when removing aliases
381 if [ "${_action}" = "-alias" ]; then
386 while [ "${_ipcount}" -le "${_iphigh}" ]; do
387 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
388 _ipcount=$((${_ipcount}+1))
391 # only the first ipaddr in a subnet need the real netmask
392 if [ "${_action}" != "-alias" ]; then
401 # Configure aliases for network interface $if.
402 # It returns 0 if at least one alias was configured or
403 # 1 if there were none.
410 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
411 if [ -n "${ifconfig_args}" ]; then
412 ifconfig $1 ${ifconfig_args} alias
413 alias=$((${alias} + 1))
423 # Remove aliases for network interface $if.
424 # It returns 0 if at least one alias was removed or
425 # 1 if there were none.
432 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
433 if [ -n "${ifconfig_args}" ]; then
434 ifconfig $1 ${ifconfig_args} -alias
435 alias=$((${alias} + 1))
445 # Evaluate a startup script for the $if interface.
446 # It returns 0 if a script was found and processed or
447 # 1 if no script was found.
451 if [ -r /etc/start_if.$1 ]; then
459 # Evaluate a shutdown script for the $if interface.
460 # It returns 0 if a script was found and processed or
461 # 1 if no script was found.
465 if [ -r /etc/stop_if.$1 ]; then
472 # Create cloneable interfaces.
478 for ifn in ${cloned_interfaces}; do
479 ifconfig ${ifn} create
480 if [ $? -eq 0 ]; then
481 _list="${_list}${_prefix}${ifn}"
482 [ -z "$_prefix" ] && _prefix=' '
485 debug "Cloned: ${_list}"
488 # Destroy cloned interfaces. Destroyed interfaces are echoed
489 # to standard output.
495 for ifn in ${cloned_interfaces}; do
496 ifconfig ${ifn} destroy
497 if [ $? -eq 0 ]; then
498 _list="${_list}${_prefix}${ifn}"
499 [ -z "$_prefix" ] && _prefix=' '
502 debug "Destroyed clones: ${_list}"
505 # Create and configure child interfaces.
506 # Return 0 if child interfaces are created.
510 local cfg child child_vaps create_args ifn i
516 child_vaps=`get_if_var $ifn vaps_IF`
517 for child in ${child_vaps}; do
518 create_args="wlandev $ifn `get_if_var $child vap_create_IF`"
519 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
520 ifconfig $child create ${create_args} && cfg=0
522 i=`ifconfig wlan create ${create_args}`
523 ifconfig $i name $child && cfg=0
531 # Destroy child interfaces.
535 local cfg child child_vaps ifn
537 child_vaps=`get_if_var $ifn vaps_IF`
538 for child in ${child_vaps}; do
539 ifconfig $child destroy && cfg=0
543 # Create netgraph nodes.
546 ngctl -f - 2> /dev/null <<EOF
553 ng_mkpeer $* | while read line; do
554 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
555 if [ -n "${t}" ]; then
563 for i in ${gif_interfaces}; do
564 peers=`get_if_var $i gifconfig_IF`
570 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
571 ifconfig $i create >/dev/null 2>&1
573 gif=`ifconfig gif create`
574 ifconfig $gif name $i
576 ifconfig $i tunnel ${peers}
584 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
585 # arguments were found and configured; returns !0 otherwise.
587 local req_iface iface bogus
590 ngctl shutdown ${req_iface}: > /dev/null 2>&1
594 iface=`ng_create_one fec dummy fec`
595 if [ -z "${iface}" ]; then
598 if [ "${iface}" = "${req_iface}" ]; then
601 bogus="${bogus} ${iface}"
604 for iface in ${bogus}; do
605 ngctl shutdown ${iface}:
610 for i in ${fec_interfaces}; do
612 for j in `get_if_var $i fecconfig_IF`; do
618 ngctl msg ${i}: add_iface "\"${j}\""
627 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
628 # arguments were found and configured; returns 1 otherwise.
633 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
634 if [ -n "${ifconfig_args}" ]; then
635 ifconfig ${ifn} ${ifconfig_args}
642 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
643 # addresses were found and unconfigured. It returns 1, otherwise.
647 [ -z "$1" ] && return 1
651 ifexists $1 || return 1
653 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
657 for _ipx in $ipxList ; do
658 # get rid of extraneous line
659 [ -z "$_ipx" ] && break
661 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
664 ifconfig $1 ${_ipx} delete
674 # Rename all requested interfaces.
679 _ifn_list="`ifconfig -l`"
680 [ -z "$_ifn_list" ] && return 0
681 for _if in ${_ifn_list} ; do
682 _ifname=`get_if_var $_if ifconfig_IF_name`
683 if [ ! -z "$_ifname" ]; then
684 ifconfig $_if name $_ifname
691 # list_net_interfaces type
692 # List all network interfaces. The type of interface returned
693 # can be controlled by the type argument. The type
694 # argument can be any of the following:
695 # nodhcp - all interfaces, excluding DHCP configured interfaces
696 # dhcp - list only DHCP configured interfaces
697 # If no argument is specified all network interfaces are output.
698 # Note that the list will include cloned interfaces if applicable.
699 # Cloned interfaces must already exist to have a chance to appear
700 # in the list if ${network_interfaces} is set to `auto'.
702 list_net_interfaces()
706 # Get a list of ALL the interfaces and make lo0 first if it's there.
708 case ${network_interfaces} in
711 _autolist="`ifconfig -l`"
713 for _if in ${_autolist} ; do
715 if [ "$_if" = "lo0" ]; then
718 _tmplist="${_tmplist}${_prefix}${_if}"
719 [ -z "$_prefix" ] && _prefix=' '
723 _tmplist="${_lo}${_tmplist}"
726 _tmplist="${network_interfaces} ${cloned_interfaces}"
730 if [ -z "$type" ]; then
735 # Separate out dhcp and non-dhcp interfaces
739 for _if in ${_tmplist} ; do
741 _dhcplist="${_dhcplist}${_aprefix}${_if}"
742 [ -z "$_aprefix" ] && _aprefix=' '
743 elif [ -n "`_ifconfig_getargs $_if`" ]; then
744 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
745 [ -z "$_bprefix" ] && _bprefix=' '
762 if [ $1 -lt 10 ]; then
781 dig=`hexdigit $((${val} & 15))`
784 while [ ${val} -gt 0 ]; do
785 dig=`hexdigit $((${val} & 15))`
793 # Setup the interfaces for IPv6
794 network6_interface_setup()
798 case ${ipv6_gateway_enable} in
806 for i in $interfaces; do
808 prefix=`get_if_var $i ipv6_prefix_IF`
809 if [ -n "${prefix}" ]; then
812 laddr=`network6_getladdr $i`
813 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
814 for j in ${prefix}; do
815 address=$j\:${hostid}
816 ifconfig $i inet6 ${address} prefixlen 64 alias
818 case ${ipv6_gateway_enable} in
820 # subnet-router anycast address
822 ifconfig $i inet6 $j:: prefixlen 64 \
828 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
829 if [ -n "${ipv6_ifconfig}" ]; then
832 ifconfig $i inet6 ${ipv6_ifconfig} alias
835 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
838 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]*)
841 rtsol_interfaces="${rtsol_interfaces} ${i}"
849 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
850 # Act as endhost - automatically configured.
851 # You can configure only single interface, as
852 # specification assumes that autoconfigured host has
853 # single interface only.
854 sysctl net.inet6.ip6.accept_rtadv=1
855 set ${rtsol_interfaces}
857 rtsol ${rtsol_flags} $1
860 for i in $interfaces; do
863 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
864 if [ -z "${ipv6_ifconfig}" ]; then
867 ifconfig $i inet6 ${ipv6_ifconfig} alias
868 alias=$((${alias} + 1))
873 # Setup IPv6 to IPv4 mapping
876 case ${stf_interface_ipv4addr} in
880 # assign IPv6 addr and interface route for 6to4 interface
881 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
884 set ${stf_interface_ipv4addr}
886 hexfrag1=`hexprint $(($1*256 + $2))`
887 hexfrag2=`hexprint $(($3*256 + $4))`
888 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
889 case ${stf_interface_ipv6_ifid} in
890 [Aa][Uu][Tt][Oo] | '')
891 for i in ${ipv6_network_interfaces}; do
892 laddr=`network6_getladdr ${i}`
901 stf_interface_ipv6_ifid=`expr "${laddr}" : \
902 'fe80::\(.*\)%\(.*\)'`
903 case ${stf_interface_ipv6_ifid} in
905 stf_interface_ipv6_ifid=0:0:0:1
910 ifconfig stf0 create >/dev/null 2>&1
911 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
912 prefixlen ${stf_prefixlen}
913 # disallow packets to malicious 6to4 prefix
914 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
915 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
916 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
917 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
922 # Setup static routes
923 network6_static_routes_setup()
925 # Set up any static routes.
926 case ${ipv6_defaultrouter} in
930 ipv6_static_routes="default ${ipv6_static_routes}"
931 ipv6_route_default="default ${ipv6_defaultrouter}"
934 case ${ipv6_static_routes} in
938 for i in ${ipv6_static_routes}; do
939 ipv6_route_args=`get_if_var $i ipv6_route_IF`
940 route add -inet6 ${ipv6_route_args}
947 network6_faith_setup()
949 case ${ipv6_faith_prefix} in
953 sysctl net.inet6.ip6.keepfaith=1
954 ifconfig faith0 create >/dev/null 2>&1
956 for prefix in ${ipv6_faith_prefix}; do
957 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
963 prefix=`expr "${prefix}" : \
964 "\(.*\)/${prefixlen}"`
967 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
968 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
975 # Install the "default interface" to kernel, which will be used
976 # as the default route when there's no router.
977 network6_default_interface_setup()
979 # Choose IPv6 default interface if it is not clearly specified.
980 case ${ipv6_default_interface} in
982 for i in ${ipv6_network_interfaces}; do
988 laddr=`network6_getladdr $i exclude_tentative`
993 ipv6_default_interface=$i
1001 # Disallow unicast packets without outgoing scope identifiers,
1002 # or route such packets to a "default" interface, if it is specified.
1003 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1004 case ${ipv6_default_interface} in
1006 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1009 laddr=`network6_getladdr ${ipv6_default_interface}`
1010 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1013 # Disable installing the default interface with the
1014 # case net.inet6.ip6.forwarding=0 and
1015 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1016 # between the default router list and the manual
1017 # configured default route.
1018 case ${ipv6_gateway_enable} in
1022 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1024 ndp -I ${ipv6_default_interface}
1034 ifconfig $1 2>/dev/null | while read proto addr rest; do
1039 if [ -z "$2" ]; then