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
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} && cfg=0
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 eval 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 # Prints 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 automatically 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 address family is enabled in the kernel
289 ${SYSCTL_N} net.inet > /dev/null 2>&1
292 ${SYSCTL_N} net.inet6 > /dev/null 2>&1
295 ${SYSCTL_N} net.ipx > /dev/null 2>&1
298 if [ -x /sbin/atmconfig ]; then
299 /sbin/atmconfig diag list > /dev/null 2>&1
305 err 1 "afexists(): Unsupported address family: $_af"
311 # Returns 0 if the interface should be configured for IPv6 and
315 if ! checkyesno ipv6_enable; then
318 case "${ipv6_network_interfaces}" in
326 for v6if in ${ipv6_network_interfaces}; do
327 if [ "${v6if}" = "${1}" ]; then
335 # Returns 0 if the interface exists and 1 otherwise.
338 ifconfig -n $1 > /dev/null 2>&1
342 # add IPv4 addresses to the interface $if
347 ipv4_addrs_common ${_if} alias
351 # remove IPv4 addresses from the interface $if
358 ifexists ${_if} || return 1
360 inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
364 for _inet in $inetList ; do
365 # get rid of extraneous line
366 [ -z "$_inet" ] && break
368 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
371 ifconfig ${_if} ${_inet} delete
377 ifalias_down ${_if} && _ret=0
378 ipv4_addrs_common ${_if} -alias && _ret=0
383 # ipv4_addrs_common if action
384 # Evaluate the ifconfig_if_ipv4 arguments for interface $if
385 # and use $action to add or remove IPv4 addresses from $if.
393 cidr_addr=`get_if_var $_if ipv4_addrs_IF`
395 for _cidr in ${cidr_addr}; do
397 _netmask="/"${_cidr##*/}
398 _range=${_ipaddr##*.}
403 # clear netmask when removing aliases
404 if [ "${_action}" = "-alias" ]; then
409 while [ "${_ipcount}" -le "${_iphigh}" ]; do
410 eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
411 _ipcount=$((${_ipcount}+1))
414 # only the first ipaddr in a subnet need the real netmask
415 if [ "${_action}" != "-alias" ]; then
424 # Configure aliases for network interface $if.
425 # It returns 0 if at least one alias was configured or
426 # 1 if there were none.
433 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
434 if [ -n "${ifconfig_args}" ]; then
435 ifconfig $1 ${ifconfig_args} alias
436 alias=$((${alias} + 1))
446 # Remove aliases for network interface $if.
447 # It returns 0 if at least one alias was removed or
448 # 1 if there were none.
455 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
456 if [ -n "${ifconfig_args}" ]; then
457 ifconfig $1 ${ifconfig_args} -alias
458 alias=$((${alias} + 1))
468 # Evaluate a startup script for the $if interface.
469 # It returns 0 if a script was found and processed or
470 # 1 if no script was found.
474 if [ -r /etc/start_if.$1 ]; then
482 # Evaluate a shutdown script for the $if interface.
483 # It returns 0 if a script was found and processed or
484 # 1 if no script was found.
488 if [ -r /etc/stop_if.$1 ]; then
495 # Create cloneable interfaces.
501 for ifn in ${cloned_interfaces}; do
502 ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
503 if [ $? -eq 0 ]; then
504 _list="${_list}${_prefix}${ifn}"
505 [ -z "$_prefix" ] && _prefix=' '
508 debug "Cloned: ${_list}"
511 # Destroy cloned interfaces. Destroyed interfaces are echoed
512 # to standard output.
518 for ifn in ${cloned_interfaces}; do
519 ifconfig -n ${ifn} destroy
520 if [ $? -eq 0 ]; then
521 _list="${_list}${_prefix}${ifn}"
522 [ -z "$_prefix" ] && _prefix=' '
525 debug "Destroyed clones: ${_list}"
528 # Create and configure child interfaces.
529 # Return 0 if child interfaces are created.
533 local cfg child child_vlans child_wlans create_args debug_flags ifn i
538 # Create wireless interfaces
539 child_wlans=`get_if_var $ifn wlans_IF`
541 for child in ${child_wlans}; do
542 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
543 debug_flags="`get_if_var $child wlandebug_IF`"
545 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
546 ifconfig $child create ${create_args} && cfg=0
547 if [ -n "${debug_flags}" ]; then
548 wlandebug -i $child ${debug_flags}
551 i=`ifconfig wlan create ${create_args}`
552 if [ -n "${debug_flags}" ]; then
553 wlandebug -i $i ${debug_flags}
555 ifconfig $i name $child && cfg=0
557 if autoif $child; then
562 # Create vlan interfaces
563 child_vlans=`get_if_var $ifn vlans_IF`
565 if [ -n "${child_vlans}" ]; then
569 for child in ${child_vlans}; do
570 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
571 child="${ifn}.${child}"
572 create_args=`get_if_var $child create_args_IF`
573 ifconfig $child create ${create_args} && cfg=0
575 create_args="vlandev $ifn `get_if_var $child create_args_IF`"
576 if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
577 ifconfig $child create ${create_args} && cfg=0
579 i=`ifconfig vlan create ${create_args}`
580 ifconfig $i name $child && cfg=0
583 if autoif $child; then
591 # Destroy child interfaces.
595 local cfg child child_vlans child_wlans ifn
597 child_wlans=`get_if_var $ifn wlans_IF`
598 for child in ${child_wlans}; do
599 if ! ifexists $child; then
602 ifconfig -n $child destroy && cfg=0
605 child_vlans=`get_if_var $ifn vlans_IF`
606 for child in ${child_vlans}; do
607 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
608 child="${ifn}.${child}"
610 if ! ifexists $child; then
613 ifconfig -n $child destroy && cfg=0
619 # Create netgraph nodes.
622 ngctl -f - 2> /dev/null <<EOF
629 ng_mkpeer $* | while read line; do
630 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
631 if [ -n "${t}" ]; then
639 for i in ${gif_interfaces}; do
640 peers=`get_if_var $i gifconfig_IF`
646 if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
647 ifconfig $i create >/dev/null 2>&1
649 gif=`ifconfig gif create`
650 ifconfig $gif name $i
652 ifconfig $i tunnel ${peers}
660 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
661 # arguments were found and configured; returns !0 otherwise.
663 local req_iface iface bogus
666 ngctl shutdown ${req_iface}: > /dev/null 2>&1
670 iface=`ng_create_one fec dummy fec`
671 if [ -z "${iface}" ]; then
674 if [ "${iface}" = "${req_iface}" ]; then
677 bogus="${bogus} ${iface}"
680 for iface in ${bogus}; do
681 ngctl shutdown ${iface}:
686 for i in ${fec_interfaces}; do
688 for j in `get_if_var $i fecconfig_IF`; do
694 ngctl msg ${i}: add_iface "\"${j}\""
703 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
704 # arguments were found and configured; returns 1 otherwise.
709 ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
710 if [ -n "${ifconfig_args}" ]; then
711 ifconfig ${ifn} ${ifconfig_args}
718 # Remove IPX addresses for interface $ifn. Returns 0 if IPX
719 # addresses were found and unconfigured. It returns 1, otherwise.
723 [ -z "$1" ] && return 1
727 ifexists $1 || return 1
729 ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
733 for _ipx in $ipxList ; do
734 # get rid of extraneous line
735 [ -z "$_ipx" ] && break
737 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
740 ifconfig $1 ${_ipx} delete
750 # Rename all requested interfaces.
755 _ifn_list="`ifconfig -l`"
756 [ -z "$_ifn_list" ] && return 0
757 for _if in ${_ifn_list} ; do
758 _ifname=`get_if_var $_if ifconfig_IF_name`
759 if [ ! -z "$_ifname" ]; then
760 ifconfig $_if name $_ifname
767 # list_net_interfaces type
768 # List all network interfaces. The type of interface returned
769 # can be controlled by the type argument. The type
770 # argument can be any of the following:
771 # nodhcp - all interfaces, excluding DHCP configured interfaces
772 # dhcp - list only DHCP configured interfaces
773 # If no argument is specified all network interfaces are output.
774 # Note that the list will include cloned interfaces if applicable.
775 # Cloned interfaces must already exist to have a chance to appear
776 # in the list if ${network_interfaces} is set to `auto'.
778 list_net_interfaces()
782 # Get a list of ALL the interfaces and make lo0 first if it's there.
784 case ${network_interfaces} in
787 _autolist="`ifconfig -l`"
789 for _if in ${_autolist} ; do
791 if [ "$_if" = "lo0" ]; then
794 _tmplist="${_tmplist}${_prefix}${_if}"
795 [ -z "$_prefix" ] && _prefix=' '
799 _tmplist="${_lo}${_tmplist}"
802 _tmplist="${network_interfaces} ${cloned_interfaces}"
804 # lo0 is effectively mandatory, so help prevent foot-shooting
807 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
808 *) _tmplist="lo0 ${_tmplist}" ;;
813 if [ -z "$type" ]; then
818 # Separate out dhcp and non-dhcp interfaces
822 for _if in ${_tmplist} ; do
824 _dhcplist="${_dhcplist}${_aprefix}${_if}"
825 [ -z "$_aprefix" ] && _aprefix=' '
826 elif [ -n "`_ifconfig_getargs $_if`" ]; then
827 _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
828 [ -z "$_bprefix" ] && _bprefix=' '
843 # get_default_if -address_family
844 # Get the interface of the default route for the given address family.
845 # The -address_family argument must be suitable passing to route(8).
849 routeget="`route -n get $1 default 2>/dev/null`"
854 for line in $routeget ; do
868 if [ $1 -lt 10 ]; then
887 dig=`hexdigit $((${val} & 15))`
890 while [ ${val} -gt 0 ]; do
891 dig=`hexdigit $((${val} & 15))`
903 case `ifconfig $1 2>/dev/null` in
904 *media:?Ethernet*) media=Ethernet ;;
907 test "$media" = "Ethernet"
910 # Setup the interfaces for IPv6
911 network6_interface_setup()
915 case ${ipv6_gateway_enable} in
923 for i in $interfaces; do
925 prefix=`get_if_var $i ipv6_prefix_IF`
926 if [ -n "${prefix}" ]; then
929 laddr=`network6_getladdr $i`
930 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
931 for j in ${prefix}; do
932 address=$j\:${hostid}
933 ifconfig $i inet6 ${address} prefixlen 64 alias
935 case ${ipv6_gateway_enable} in
937 # subnet-router anycast address
939 ifconfig $i inet6 $j:: prefixlen 64 \
945 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
946 if [ -n "${ipv6_ifconfig}" ]; then
949 ifconfig $i inet6 ${ipv6_ifconfig} alias
952 # Wireless NIC cards are virtualized through the wlan interface
953 if ! is_wired_interface ${i}; then
955 wlan*) rtsol_interface=yes ;;
956 *) rtsol_interface=no ;;
960 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
963 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]*)
966 rtsol_interfaces="${rtsol_interfaces} ${i}"
974 if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
975 # Act as endhost - automatically configured.
976 # You can configure only single interface, as
977 # specification assumes that autoconfigured host has
978 # single interface only.
979 sysctl net.inet6.ip6.accept_rtadv=1
980 set ${rtsol_interfaces}
982 if ! checkyesno rtsold_enable; then
983 rtsol ${rtsol_flags} $1
987 for i in $interfaces; do
990 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
991 if [ -z "${ipv6_ifconfig}" ]; then
994 ifconfig $i inet6 ${ipv6_ifconfig} alias
995 alias=$((${alias} + 1))
1000 # Setup IPv6 to IPv4 mapping
1001 network6_stf_setup()
1003 case ${stf_interface_ipv4addr} in
1007 # assign IPv6 addr and interface route for 6to4 interface
1008 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
1011 set ${stf_interface_ipv4addr}
1013 hexfrag1=`hexprint $(($1*256 + $2))`
1014 hexfrag2=`hexprint $(($3*256 + $4))`
1015 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
1016 case ${stf_interface_ipv6_ifid} in
1017 [Aa][Uu][Tt][Oo] | '')
1018 for i in ${ipv6_network_interfaces}; do
1019 laddr=`network6_getladdr ${i}`
1028 stf_interface_ipv6_ifid=`expr "${laddr}" : \
1029 'fe80::\(.*\)%\(.*\)'`
1030 case ${stf_interface_ipv6_ifid} in
1032 stf_interface_ipv6_ifid=0:0:0:1
1037 ifconfig stf0 create >/dev/null 2>&1
1038 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
1039 prefixlen ${stf_prefixlen}
1040 # disallow packets to malicious 6to4 prefix
1041 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
1042 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
1043 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1044 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1049 # Setup static routes
1050 network6_static_routes_setup()
1052 # Set up any static routes.
1053 case ${ipv6_defaultrouter} in
1057 ipv6_static_routes="default ${ipv6_static_routes}"
1058 ipv6_route_default="default ${ipv6_defaultrouter}"
1061 case ${ipv6_static_routes} in
1065 for i in ${ipv6_static_routes}; do
1066 ipv6_route_args=`get_if_var $i ipv6_route_IF`
1067 route add -inet6 ${ipv6_route_args}
1074 network6_faith_setup()
1076 case ${ipv6_faith_prefix} in
1080 sysctl net.inet6.ip6.keepfaith=1
1081 ifconfig faith0 create >/dev/null 2>&1
1083 for prefix in ${ipv6_faith_prefix}; do
1084 prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
1085 case ${prefixlen} in
1090 prefix=`expr "${prefix}" : \
1091 "\(.*\)/${prefixlen}"`
1094 route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
1095 route change -inet6 ${prefix} -prefixlen ${prefixlen} \
1102 # Install the "default interface" to kernel, which will be used
1103 # as the default route when there's no router.
1104 network6_default_interface_setup()
1106 # Choose IPv6 default interface if it is not clearly specified.
1107 case ${ipv6_default_interface} in
1109 for i in ${ipv6_network_interfaces}; do
1115 laddr=`network6_getladdr $i exclude_tentative`
1120 ipv6_default_interface=$i
1128 # Get the number of FIBs supported.
1129 fibs=`sysctl -n net.fibs`
1132 # Disallow unicast packets without outgoing scope identifiers,
1133 # or route such packets to a "default" interface, if it is specified.
1134 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1135 case ${ipv6_default_interface} in
1137 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1139 if test ${i} -lt ${fibs}; then
1140 printf "Also installing reject routes for FIBs"
1141 while test ${i} -lt ${fibs}; do
1142 setfib -F ${i} route add -inet6 \
1143 ff02:: -prefixlen 16 ::1 -reject
1151 laddr=`network6_getladdr ${ipv6_default_interface}`
1152 # Only add the laddr route to the default FIB and a reject
1153 # route to all others.
1154 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface
1156 if test ${i} -lt ${fibs}; then
1157 printf "Also installing reject routes for FIBs"
1158 while test ${i} -lt ${fibs}; do
1159 setfib -F ${i} route add -inet6 \
1160 ff02:: -prefixlen 16 ::1 -reject
1167 # Disable installing the default interface with the
1168 # case net.inet6.ip6.forwarding=0 and
1169 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1170 # between the default router list and the manual
1171 # configured default route.
1172 case ${ipv6_gateway_enable} in
1176 if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1178 ndp -I ${ipv6_default_interface}
1188 ifconfig $1 2>/dev/null | while read proto addr rest; do
1193 if [ -z "$2" ]; then