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 `get_if_var ${ifn} create_args_IF`
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_wlans create_args ifn i
515 # Create wireless interfaces
516 child_wlans=`get_if_var $ifn wlans_IF`
517 if [ -z "${child_wlans}" ]; then
518 child_wlans=`get_if_var $ifn vaps_IF`
519 if [ -n "${child_wlans}" ]; then
520 warn "soon to be deleted vaps_$ifn variable defined use wlans_$ifn"
524 for child in ${child_wlans}; do
525 create_args="wlandev $ifn `get_if_var $child create_args_IF` `get_if_var $child vap_create_IF`"
526 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
527 ifconfig $child create ${create_args} && cfg=0
529 i=`ifconfig wlan create ${create_args}`
530 ifconfig $i name $child && cfg=0
538 # Destroy child interfaces.
542 local cfg child child_wlans ifn
544 child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
545 for child in ${child_wlans}; do
546 ifconfig $child destroy && cfg=0
550 # Create netgraph nodes.
553 ngctl -f - 2> /dev/null <<EOF
560 ng_mkpeer $* | while read line; do
561 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
562 if [ -n "${t}" ]; then
570 for i in ${gif_interfaces}; do
571 peers=`get_if_var $i gifconfig_IF`
577 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
578 ifconfig $i create >/dev/null 2>&1
580 gif=`ifconfig gif create`
581 ifconfig $gif name $i
583 ifconfig $i tunnel ${peers}
591 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
592 # arguments were found and configured; returns !0 otherwise.
594 local req_iface iface bogus
597 ngctl shutdown ${req_iface}: > /dev/null 2>&1
601 iface=`ng_create_one fec dummy fec`
602 if [ -z "${iface}" ]; then
605 if [ "${iface}" = "${req_iface}" ]; then
608 bogus="${bogus} ${iface}"
611 for iface in ${bogus}; do
612 ngctl shutdown ${iface}:
617 for i in ${fec_interfaces}; do
619 for j in `get_if_var $i fecconfig_IF`; do
625 ngctl msg ${i}: add_iface "\"${j}\""
634 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
635 # arguments were found and configured; returns 1 otherwise.
640 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
641 if [ -n "${ifconfig_args}" ]; then
642 ifconfig ${ifn} ${ifconfig_args}
649 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
650 # addresses were found and unconfigured. It returns 1, otherwise.
654 [ -z "$1" ] && return 1
658 ifexists $1 || return 1
660 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
664 for _ipx in $ipxList ; do
665 # get rid of extraneous line
666 [ -z "$_ipx" ] && break
668 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
671 ifconfig $1 ${_ipx} delete
681 # Rename all requested interfaces.
686 _ifn_list="`ifconfig -l`"
687 [ -z "$_ifn_list" ] && return 0
688 for _if in ${_ifn_list} ; do
689 _ifname=`get_if_var $_if ifconfig_IF_name`
690 if [ ! -z "$_ifname" ]; then
691 ifconfig $_if name $_ifname
698 # list_net_interfaces type
699 # List all network interfaces. The type of interface returned
700 # can be controlled by the type argument. The type
701 # argument can be any of the following:
702 # nodhcp - all interfaces, excluding DHCP configured interfaces
703 # dhcp - list only DHCP configured interfaces
704 # If no argument is specified all network interfaces are output.
705 # Note that the list will include cloned interfaces if applicable.
706 # Cloned interfaces must already exist to have a chance to appear
707 # in the list if ${network_interfaces} is set to `auto'.
709 list_net_interfaces()
713 # Get a list of ALL the interfaces and make lo0 first if it's there.
715 case ${network_interfaces} in
718 _autolist="`ifconfig -l`"
720 for _if in ${_autolist} ; do
722 if [ "$_if" = "lo0" ]; then
725 _tmplist="${_tmplist}${_prefix}${_if}"
726 [ -z "$_prefix" ] && _prefix=' '
730 _tmplist="${_lo}${_tmplist}"
733 if [ -z "$type"]; then
734 warn "Values of network_interfaces other than" \
735 "AUTO are deprecated"
737 _tmplist="${network_interfaces} ${cloned_interfaces}"
741 if [ -z "$type" ]; then
746 # Separate out dhcp and non-dhcp interfaces
750 for _if in ${_tmplist} ; do
752 _dhcplist="${_dhcplist}${_aprefix}${_if}"
753 [ -z "$_aprefix" ] && _aprefix=' '
754 elif [ -n "`_ifconfig_getargs $_if`" ]; then
755 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
756 [ -z "$_bprefix" ] && _bprefix=' '
773 if [ $1 -lt 10 ]; then
792 dig=`hexdigit $((${val} & 15))`
795 while [ ${val} -gt 0 ]; do
796 dig=`hexdigit $((${val} & 15))`
804 # Setup the interfaces for IPv6
805 network6_interface_setup()
809 case ${ipv6_gateway_enable} in
817 for i in $interfaces; do
819 prefix=`get_if_var $i ipv6_prefix_IF`
820 if [ -n "${prefix}" ]; then
823 laddr=`network6_getladdr $i`
824 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
825 for j in ${prefix}; do
826 address=$j\:${hostid}
827 ifconfig $i inet6 ${address} prefixlen 64 alias
829 case ${ipv6_gateway_enable} in
831 # subnet-router anycast address
833 ifconfig $i inet6 $j:: prefixlen 64 \
839 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
840 if [ -n "${ipv6_ifconfig}" ]; then
843 ifconfig $i inet6 ${ipv6_ifconfig} alias
846 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
849 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]*)
852 rtsol_interfaces="${rtsol_interfaces} ${i}"
860 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
861 # Act as endhost - automatically configured.
862 # You can configure only single interface, as
863 # specification assumes that autoconfigured host has
864 # single interface only.
865 sysctl net.inet6.ip6.accept_rtadv=1
866 set ${rtsol_interfaces}
868 rtsol ${rtsol_flags} $1
871 for i in $interfaces; do
874 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
875 if [ -z "${ipv6_ifconfig}" ]; then
878 ifconfig $i inet6 ${ipv6_ifconfig} alias
879 alias=$((${alias} + 1))
884 # Setup IPv6 to IPv4 mapping
887 case ${stf_interface_ipv4addr} in
891 # assign IPv6 addr and interface route for 6to4 interface
892 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
895 set ${stf_interface_ipv4addr}
897 hexfrag1=`hexprint $(($1*256 + $2))`
898 hexfrag2=`hexprint $(($3*256 + $4))`
899 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
900 case ${stf_interface_ipv6_ifid} in
901 [Aa][Uu][Tt][Oo] | '')
902 for i in ${ipv6_network_interfaces}; do
903 laddr=`network6_getladdr ${i}`
912 stf_interface_ipv6_ifid=`expr "${laddr}" : \
913 'fe80::\(.*\)%\(.*\)'`
914 case ${stf_interface_ipv6_ifid} in
916 stf_interface_ipv6_ifid=0:0:0:1
921 ifconfig stf0 create >/dev/null 2>&1
922 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
923 prefixlen ${stf_prefixlen}
924 # disallow packets to malicious 6to4 prefix
925 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
926 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
927 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
928 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
933 # Setup static routes
934 network6_static_routes_setup()
936 # Set up any static routes.
937 case ${ipv6_defaultrouter} in
941 ipv6_static_routes="default ${ipv6_static_routes}"
942 ipv6_route_default="default ${ipv6_defaultrouter}"
945 case ${ipv6_static_routes} in
949 for i in ${ipv6_static_routes}; do
950 ipv6_route_args=`get_if_var $i ipv6_route_IF`
951 route add -inet6 ${ipv6_route_args}
958 network6_faith_setup()
960 case ${ipv6_faith_prefix} in
964 sysctl net.inet6.ip6.keepfaith=1
965 ifconfig faith0 create >/dev/null 2>&1
967 for prefix in ${ipv6_faith_prefix}; do
968 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
974 prefix=`expr "${prefix}" : \
975 "\(.*\)/${prefixlen}"`
978 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
979 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
986 # Install the "default interface" to kernel, which will be used
987 # as the default route when there's no router.
988 network6_default_interface_setup()
990 # Choose IPv6 default interface if it is not clearly specified.
991 case ${ipv6_default_interface} in
993 for i in ${ipv6_network_interfaces}; do
999 laddr=`network6_getladdr $i exclude_tentative`
1004 ipv6_default_interface=$i
1012 # Disallow unicast packets without outgoing scope identifiers,
1013 # or route such packets to a "default" interface, if it is specified.
1014 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1015 case ${ipv6_default_interface} in
1017 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1020 laddr=`network6_getladdr ${ipv6_default_interface}`
1021 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1024 # Disable installing the default interface with the
1025 # case net.inet6.ip6.forwarding=0 and
1026 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1027 # between the default router list and the manual
1028 # configured default route.
1029 case ${ipv6_gateway_enable} in
1033 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1035 ndp -I ${ipv6_default_interface}
1045 ifconfig $1 2>/dev/null | while read proto addr rest; do
1050 if [ -z "$2" ]; then