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
55 # Shutdown and de-configure an interface. If action is taken print the
64 [ -z "$ifn" ] && return 1
66 ipx_down ${ifn} && cfg=0
67 ipv4_down ${ifn} && cfg=0
68 ifconfig_down ${ifn} && cfg=0
69 ifscript_down ${ifn} && cfg=0
70 childif_destroy ${ifn}
76 # Evaluate ifconfig(8) arguments for interface $if and
77 # run ifconfig(8) with those arguments. It returns 0 if
78 # arguments were found and executed or 1 if the interface
79 # had no arguments. Pseudo arguments DHCP and WPA are handled
86 ifconfig_args=`ifconfig_getargs $1`
87 if [ -n "${ifconfig_args}" ]; then
88 ifconfig $1 ${ifconfig_args}
94 /etc/rc.d/wpa_supplicant start $1
95 _cfg=0 # XXX: not sure this should count
99 if [ $_cfg -ne 0 ] ; then
102 if syncdhcpif $1; then
103 /etc/rc.d/dhclient start $1
112 # returns 1 if wpa_supplicant or dhclient was stopped or
113 # the interface exists.
117 [ -z "$1" ] && return 1
121 /etc/rc.d/wpa_supplicant stop $1
126 /etc/rc.d/dhclient stop $1
138 # get_if_var if var [default]
139 # Return the value of the pseudo-hash corresponding to $if where
140 # $var is a string containg the sub-string "IF" which will be
141 # replaced with $if after the characters defined in _punct are
142 # replaced with '_'. If the variable is unset, replace it with
146 if [ $# -ne 2 -a $# -ne 3 ]; then
147 err 3 'USAGE: get_if_var name var [default]'
152 for _punct_c in $_punct; do
153 _if=`ltr ${_if} ${_punct_c} '_'`
160 eval echo \${${prefix}${_if}${suffix}-${_default}}
163 # _ifconfig_getargs if
164 # Echos the arguments for the supplied interface to stdout.
165 # returns 1 if empty. In general, ifconfig_getargs should be used
170 if [ -z "$_ifn" ]; then
174 get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
177 # ifconfig_getargs if
178 # Takes the result from _ifconfig_getargs and removes pseudo
179 # args such as DHCP and WPA.
182 _tmpargs=`_ifconfig_getargs $1`
183 if [ $? -eq 1 ]; then
188 for _arg in $_tmpargs; do
191 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
192 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
193 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
205 # Returns 0 if the interface should be automaticly configured at
206 # boot time and 1 otherwise.
209 _tmpargs=`_ifconfig_getargs $1`
210 for _arg in $_tmpargs; do
212 [Nn][Oo][Aa][Uu][Tt][Oo])
221 # Returns 0 if the interface is a DHCP interface and 1 otherwise.
224 _tmpargs=`_ifconfig_getargs $1`
225 for _arg in $_tmpargs; do
230 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
233 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
242 # Returns 0 if the interface should be configured synchronously and
246 _tmpargs=`_ifconfig_getargs $1`
247 for _arg in $_tmpargs; do
249 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
252 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
257 if checkyesno synchronous_dhclient; then
265 # Returns 0 if the interface is a WPA interface and 1 otherwise.
268 _tmpargs=`_ifconfig_getargs $1`
269 for _arg in $_tmpargs; do
280 # Returns 0 if the interface should be configured for IPv6 and
284 if ! checkyesno ipv6_enable; then
287 case "${ipv6_network_interfaces}" in
295 for v6if in ${ipv6_network_interfaces}; do
296 if [ "${v6if}" = "${1}" ]; then
304 # Returns 0 if the interface exists and 1 otherwise.
307 ifconfig -n $1 > /dev/null 2>&1
311 # add IPv4 addresses to the interface $if
316 ipv4_addrs_common ${_if} alias
320 # remove IPv4 addresses from the interface $if
327 ifexists ${_if} || return 1
329 inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
333 for _inet in $inetList ; do
334 # get rid of extraneous line
335 [ -z "$_inet" ] && break
337 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
340 ifconfig ${_if} ${_inet} delete
346 ifalias_down ${_if} && _ret=0
347 ipv4_addrs_common ${_if} -alias && _ret=0
352 # ipv4_addrs_common if action
353 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
354 # and use $action to add or remove IPv4 addresses from $if.
362 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
364 for _cidr in ${cidr_addr}; do
366 _netmask="/"${_cidr##*/}
367 _range=${_ipaddr##*.}
372 # clear netmask when removing aliases
373 if [ "${_action}" = "-alias" ]; then
378 while [ "${_ipcount}" -le "${_iphigh}" ]; do
379 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
380 _ipcount=$((${_ipcount}+1))
383 # only the first ipaddr in a subnet need the real netmask
384 if [ "${_action}" != "-alias" ]; then
393 # Configure aliases for network interface $if.
394 # It returns 0 if at least one alias was configured or
395 # 1 if there were none.
402 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
403 if [ -n "${ifconfig_args}" ]; then
404 ifconfig $1 ${ifconfig_args} alias
405 alias=$((${alias} + 1))
415 # Remove aliases for network interface $if.
416 # It returns 0 if at least one alias was removed or
417 # 1 if there were none.
424 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
425 if [ -n "${ifconfig_args}" ]; then
426 ifconfig $1 ${ifconfig_args} -alias
427 alias=$((${alias} + 1))
437 # Evaluate a startup script for the $if interface.
438 # It returns 0 if a script was found and processed or
439 # 1 if no script was found.
443 if [ -r /etc/start_if.$1 ]; then
451 # Evaluate a shutdown script for the $if interface.
452 # It returns 0 if a script was found and processed or
453 # 1 if no script was found.
457 if [ -r /etc/stop_if.$1 ]; then
464 # Create cloneable interfaces.
470 for ifn in ${cloned_interfaces}; do
471 ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
472 if [ $? -eq 0 ]; then
473 _list="${_list}${_prefix}${ifn}"
474 [ -z "$_prefix" ] && _prefix=' '
477 debug "Cloned: ${_list}"
480 # Destroy cloned interfaces. Destroyed interfaces are echoed
481 # to standard output.
487 for ifn in ${cloned_interfaces}; do
488 ifconfig ${ifn} destroy
489 if [ $? -eq 0 ]; then
490 _list="${_list}${_prefix}${ifn}"
491 [ -z "$_prefix" ] && _prefix=' '
494 debug "Destroyed clones: ${_list}"
497 # Create and configure child interfaces.
498 # Return 0 if child interfaces are created.
502 local cfg child child_wlans create_args ifn i
507 # Create wireless interfaces
508 child_wlans=`get_if_var $ifn wlans_IF`
509 if [ -z "${child_wlans}" ]; then
510 child_wlans=`get_if_var $ifn vaps_IF`
511 if [ -n "${child_wlans}" ]; then
512 warn "soon to be deleted vaps_$ifn variable defined use wlans_$ifn"
516 for child in ${child_wlans}; do
517 create_args="wlandev $ifn `get_if_var $child create_args_IF` `get_if_var $child vap_create_IF`"
518 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
519 ifconfig $child create ${create_args} && cfg=0
521 i=`ifconfig wlan create ${create_args}`
522 ifconfig $i name $child && cfg=0
530 # Destroy child interfaces.
534 local cfg child child_wlans ifn
536 child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
537 for child in ${child_wlans}; do
538 ifconfig $child destroy && cfg=0
542 # Create netgraph nodes.
545 ngctl -f - 2> /dev/null <<EOF
552 ng_mkpeer $* | while read line; do
553 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
554 if [ -n "${t}" ]; then
562 for i in ${gif_interfaces}; do
563 peers=`get_if_var $i gifconfig_IF`
569 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
570 ifconfig $i create >/dev/null 2>&1
572 gif=`ifconfig gif create`
573 ifconfig $gif name $i
575 ifconfig $i tunnel ${peers}
583 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
584 # arguments were found and configured; returns !0 otherwise.
586 local req_iface iface bogus
589 ngctl shutdown ${req_iface}: > /dev/null 2>&1
593 iface=`ng_create_one fec dummy fec`
594 if [ -z "${iface}" ]; then
597 if [ "${iface}" = "${req_iface}" ]; then
600 bogus="${bogus} ${iface}"
603 for iface in ${bogus}; do
604 ngctl shutdown ${iface}:
609 for i in ${fec_interfaces}; do
611 for j in `get_if_var $i fecconfig_IF`; do
617 ngctl msg ${i}: add_iface "\"${j}\""
626 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
627 # arguments were found and configured; returns 1 otherwise.
632 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
633 if [ -n "${ifconfig_args}" ]; then
634 ifconfig ${ifn} ${ifconfig_args}
641 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
642 # addresses were found and unconfigured. It returns 1, otherwise.
646 [ -z "$1" ] && return 1
650 ifexists $1 || return 1
652 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
656 for _ipx in $ipxList ; do
657 # get rid of extraneous line
658 [ -z "$_ipx" ] && break
660 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
663 ifconfig $1 ${_ipx} delete
673 # Rename all requested interfaces.
678 _ifn_list="`ifconfig -l`"
679 [ -z "$_ifn_list" ] && return 0
680 for _if in ${_ifn_list} ; do
681 _ifname=`get_if_var $_if ifconfig_IF_name`
682 if [ ! -z "$_ifname" ]; then
683 ifconfig $_if name $_ifname
690 # list_net_interfaces type
691 # List all network interfaces. The type of interface returned
692 # can be controlled by the type argument. The type
693 # argument can be any of the following:
694 # nodhcp - all interfaces, excluding DHCP configured interfaces
695 # dhcp - list only DHCP configured interfaces
696 # If no argument is specified all network interfaces are output.
697 # Note that the list will include cloned interfaces if applicable.
698 # Cloned interfaces must already exist to have a chance to appear
699 # in the list if ${network_interfaces} is set to `auto'.
701 list_net_interfaces()
705 # Get a list of ALL the interfaces and make lo0 first if it's there.
707 case ${network_interfaces} in
710 _autolist="`ifconfig -l`"
712 for _if in ${_autolist} ; do
714 if [ "$_if" = "lo0" ]; then
717 _tmplist="${_tmplist}${_prefix}${_if}"
718 [ -z "$_prefix" ] && _prefix=' '
722 _tmplist="${_lo}${_tmplist}"
725 if [ -z "$type" ]; then
726 warn "Values of network_interfaces other than" \
727 "AUTO are deprecated"
729 _tmplist="${network_interfaces} ${cloned_interfaces}"
733 if [ -z "$type" ]; then
738 # Separate out dhcp and non-dhcp interfaces
742 for _if in ${_tmplist} ; do
744 _dhcplist="${_dhcplist}${_aprefix}${_if}"
745 [ -z "$_aprefix" ] && _aprefix=' '
746 elif [ -n "`_ifconfig_getargs $_if`" ]; then
747 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
748 [ -z "$_bprefix" ] && _bprefix=' '
763 # get_default_if -address_family
764 # Get the interface of the default route for the given address family.
765 # The -address_family argument must be suitable passing to route(8).
769 routeget="`route -n get $1 default 2>/dev/null`"
774 for line in $routeget ; do
788 if [ $1 -lt 10 ]; then
807 dig=`hexdigit $((${val} & 15))`
810 while [ ${val} -gt 0 ]; do
811 dig=`hexdigit $((${val} & 15))`
819 # Setup the interfaces for IPv6
820 network6_interface_setup()
824 case ${ipv6_gateway_enable} in
832 for i in $interfaces; do
834 prefix=`get_if_var $i ipv6_prefix_IF`
835 if [ -n "${prefix}" ]; then
838 laddr=`network6_getladdr $i`
839 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
840 for j in ${prefix}; do
841 address=$j\:${hostid}
842 ifconfig $i inet6 ${address} prefixlen 64 alias
844 case ${ipv6_gateway_enable} in
846 # subnet-router anycast address
848 ifconfig $i inet6 $j:: prefixlen 64 \
854 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
855 if [ -n "${ipv6_ifconfig}" ]; then
858 ifconfig $i inet6 ${ipv6_ifconfig} alias
861 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
864 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]*)
867 rtsol_interfaces="${rtsol_interfaces} ${i}"
875 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
876 # Act as endhost - automatically configured.
877 # You can configure only single interface, as
878 # specification assumes that autoconfigured host has
879 # single interface only.
880 sysctl net.inet6.ip6.accept_rtadv=1
881 set ${rtsol_interfaces}
883 rtsol ${rtsol_flags} $1
886 for i in $interfaces; do
889 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
890 if [ -z "${ipv6_ifconfig}" ]; then
893 ifconfig $i inet6 ${ipv6_ifconfig} alias
894 alias=$((${alias} + 1))
899 # Setup IPv6 to IPv4 mapping
902 case ${stf_interface_ipv4addr} in
906 # assign IPv6 addr and interface route for 6to4 interface
907 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
910 set ${stf_interface_ipv4addr}
912 hexfrag1=`hexprint $(($1*256 + $2))`
913 hexfrag2=`hexprint $(($3*256 + $4))`
914 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
915 case ${stf_interface_ipv6_ifid} in
916 [Aa][Uu][Tt][Oo] | '')
917 for i in ${ipv6_network_interfaces}; do
918 laddr=`network6_getladdr ${i}`
927 stf_interface_ipv6_ifid=`expr "${laddr}" : \
928 'fe80::\(.*\)%\(.*\)'`
929 case ${stf_interface_ipv6_ifid} in
931 stf_interface_ipv6_ifid=0:0:0:1
936 ifconfig stf0 create >/dev/null 2>&1
937 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
938 prefixlen ${stf_prefixlen}
939 # disallow packets to malicious 6to4 prefix
940 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
941 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
942 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
943 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
948 # Setup static routes
949 network6_static_routes_setup()
951 # Set up any static routes.
952 case ${ipv6_defaultrouter} in
956 ipv6_static_routes="default ${ipv6_static_routes}"
957 ipv6_route_default="default ${ipv6_defaultrouter}"
960 case ${ipv6_static_routes} in
964 for i in ${ipv6_static_routes}; do
965 ipv6_route_args=`get_if_var $i ipv6_route_IF`
966 route add -inet6 ${ipv6_route_args}
973 network6_faith_setup()
975 case ${ipv6_faith_prefix} in
979 sysctl net.inet6.ip6.keepfaith=1
980 ifconfig faith0 create >/dev/null 2>&1
982 for prefix in ${ipv6_faith_prefix}; do
983 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
989 prefix=`expr "${prefix}" : \
990 "\(.*\)/${prefixlen}"`
993 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
994 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
1001 # Install the "default interface" to kernel, which will be used
1002 # as the default route when there's no router.
1003 network6_default_interface_setup()
1005 # Choose IPv6 default interface if it is not clearly specified.
1006 case ${ipv6_default_interface} in
1008 for i in ${ipv6_network_interfaces}; do
1014 laddr=`network6_getladdr $i exclude_tentative`
1019 ipv6_default_interface=$i
1027 # Disallow unicast packets without outgoing scope identifiers,
1028 # or route such packets to a "default" interface, if it is specified.
1029 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1030 case ${ipv6_default_interface} in
1032 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1035 laddr=`network6_getladdr ${ipv6_default_interface}`
1036 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1039 # Disable installing the default interface with the
1040 # case net.inet6.ip6.forwarding=0 and
1041 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1042 # between the default router list and the manual
1043 # configured default route.
1044 case ${ipv6_gateway_enable} in
1048 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1050 ndp -I ${ipv6_default_interface}
1060 ifconfig $1 2>/dev/null | while read proto addr rest; do
1065 if [ -z "$2" ]; then