kernel - Make the wdog.h and gpio.h includes conditional in kern_shutdown.c.
[dragonfly.git] / etc / network.subr
blob8e5db363884a674bd4ce9d7f8859e71a8b80527c
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
6 # are met:
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
23 # SUCH DAMAGE.
25 # $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
29 # Subroutines commonly used from network startup scripts.
30 # Requires that rc.conf be loaded first.
33 # ifconfig_up if
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
38 #       here.
40 ifconfig_up()
42         _cfg=1
44         ifconfig_args=`ifconfig_getargs $1`
45         if [ -n "${ifconfig_args}" ]; then
46                 ifconfig $1 ${ifconfig_args}
47                 _cfg=0
48         fi
50         if wpaif $1; then
51                 /etc/rc.d/wpa_supplicant start $1
52                 _cfg=0          # XXX: not sure this should count
53         fi
55         if dhcpif $1; then
56                 /etc/rc.d/dhclient start $1
57                 _cfg=0
58         fi
60         return $_cfg
63 # ifconfig_down if
64 #       Remove all inet entries from the $if interface. It returns
65 #       0 if inet entries were found and removed. It returns 1 if
66 #       no entries were found or they could not be removed.
68 ifconfig_down()
70         [ -z "$1" ] && return 1
71         _ifs="^"
72         _cfg=1
74         inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
76         oldifs="$IFS"
77         IFS="$_ifs"
78         for _inet in $inetList ; do
79                 # get rid of extraneous line
80                 [ -z "$_inet" ] && break
82                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
84                 IFS="$oldifs"
85                 ifconfig $1 ${_inet} delete
86                 IFS="$_ifs"
87                 _cfg=0
88         done
89         IFS="$oldifs"
91         if wpaif $1; then
92                 /etc/rc.d/wpa_supplicant stop $1
93         fi
95         if dhcpif $1; then
96                 /etc/rc.d/dhclient stop $1
97                 _cfg=0
98         fi
100         return $_cfg
103 # get_if_var if var [default]
104 #       Return the value of the pseudo-hash corresponding to $if where
105 #       $var is a string containg the sub-string "IF" which will be
106 #       replaced with $if after the characters defined in _punct are
107 #       replaced with '_'. If the variable is unset, replace it with
108 #       $default if given.
109 get_if_var()
111         local _if _punct_c _punct _var _default prefix suffix
113         if [ $# -ne 2 -a $# -ne 3 ]; then
114                 err 3 'USAGE: get_if_var name var [default]'
115         fi
117         _if=$1
118         _punct=". - / +"
119         for _punct_c in $_punct; do
120                 _if=`ltr ${_if} ${_punct_c} '_'`
121         done
122         _var=$2
123         _default=$3
125         prefix=${_var%%IF*}
126         suffix=${_var##*IF}
127         eval echo \${${prefix}${_if}${suffix}-${_default}}
130 # _ifconfig_getargs if
131 #       Echos the arguments for the supplied interface to stdout.
132 #       returns 1 if empty.  In general, ifconfig_getargs should be used
133 #       outside this file.
134 _ifconfig_getargs()
136         _ifn=$1
137         if [ -z "$_ifn" ]; then
138                 return 1
139         fi
141         _args=`get_if_var $_ifn ifconfig_IF`
142         if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
143                 for _if in ${removable_interfaces} ; do
144                         if [ "$_if" = "$_ifn" ] ; then
145                                 _args=${pccard_ifconfig}
146                                 break
147                         fi
148                 done
149         fi
151         echo $_args
154 # ifconfig_getargs if
155 #       Takes the result from _ifconfig_getargs and removes pseudo
156 #       args such as DHCP and WPA.
157 ifconfig_getargs()
159         _tmpargs=`_ifconfig_getargs $1`
160         if [ $? -eq 1 ]; then
161                 return 1
162         fi
163         _args=
165         is_optarg=no
166         for _arg in $_tmpargs; do
167                 if [ "$is_optarg" = "no" ]; then
168                         case $_arg in
169                         [Dd][Hh][Cc][Pp])
170                                 ;;
171                         [Ww][Pp][Aa])
172                                 ;;
173                         *)
174                                 _args="$_args $_arg"
175                                 case $_arg in
176                                 authmode)
177                                         is_optarg=yes
178                                         ;;
179                                 esac
180                                 ;;
181                         esac
182                 else
183                         _args="$_args $_arg"
184                         is_optarg=no
185                 fi
186         done
188         echo $_args
191 # dhcpif if
192 #       Returns 0 if the interface is a DHCP interface and 1 otherwise.
193 dhcpif()
195         _tmpargs=`_ifconfig_getargs $1`
196         for _arg in $_tmpargs; do
197                 case $_arg in
198                 [Dd][Hh][Cc][Pp])
199                         return 0
200                         ;;
201                 esac
202         done
203         return 1
206 # wpaif if
207 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
208 wpaif()
210         _tmpargs=`_ifconfig_getargs $1`
211         is_optarg=no
212         for _arg in $_tmpargs; do
213                 if [ "$is_optarg" = "no" ]; then
214                         case $_arg in
215                         [Ww][Pp][Aa])
216                                 return 0
217                                 ;;
218                         authmode)
219                                 is_optarg=yes
220                                 ;;
221                         esac
222                 else
223                         is_optarg=no
224                 fi
225         done
226         return 1
229 # ifexists if
230 #       Returns 0 if the interface exists and 1 otherwise.
231 ifexists()
233         [ -z "$1" ] && return 1
234         ifconfig $1 > /dev/null 2>&1
237 # ifalias_up if
238 #       Configure aliases for network interface $if.
239 #       It returns 0 if at least one alias was configured or
240 #       1 if there were none.
242 ifalias_up()
244         _ret=1
245         alias=0
246         while : ; do
247                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
248                 if [ -n "${ifconfig_args}" ]; then
249                         ifconfig $1 ${ifconfig_args} alias
250                         alias=$((${alias} + 1))
251                         _ret=0
252                 else
253                         break
254                 fi
255         done
256         return $_ret
259 #ifalias_down if
260 #       Remove aliases for network interface $if.
261 #       It returns 0 if at least one alias was removed or
262 #       1 if there were none.
264 ifalias_down()
266         _ret=1
267         alias=0
268         while : ; do
269                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
270                 if [ -n "${ifconfig_args}" ]; then
271                         ifconfig $1 ${ifconfig_args} -alias
272                         alias=$((${alias} + 1))
273                         _ret=0
274                 else
275                         break
276                 fi
277         done
278         return $_ret
281 # ifscript_up if
282 #       Evaluate a startup script for the $if interface.
283 #       It returns 0 if a script was found and processed or
284 #       1 if no script was found.
286 ifscript_up()
288         if [ -r /etc/start_if.$1 ]; then
289                 . /etc/start_if.$1
290                 return 0
291         fi
292         return 1
295 # ifscript_down if
296 #       Evaluate a shutdown script for the $if interface.
297 #       It returns 0 if a script was found and processed or
298 #       1 if no script was found.
300 ifscript_down()
302         if [ -r /etc/stop_if.$1 ]; then
303                 . /etc/stop_if.$1
304                 return 0
305         fi
306         return 1
309 # Create cloneable interfaces.
311 clone_up()
313         _prefix=
314         _list=
315         for ifn in ${cloned_interfaces}; do
316                 ifconfig ${ifn} create
317                 if [ $? -eq 0 ]; then
318                         _list="${_list}${_prefix}${ifn}"
319                         [ -z "$_prefix" ] && _prefix=' '
320                 fi
321         done
322         debug "Cloned: ${_list}"
325 # Destroy cloned interfaces. Destroyed interfaces are echoed
326 # to standard output.
328 clone_down()
330         _prefix=
331         _list=
332         for ifn in ${cloned_interfaces}; do
333                 ifconfig ${ifn} destroy
334                 if [ $? -eq 0 ]; then
335                         _list="${_list}${_prefix}${ifn}"
336                         [ -z "$_prefix" ] && _prefix=' '
337                 fi
338         done
339         debug "Destroyed clones: ${_list}"
342 gif_up() {
343         case ${gif_interfaces} in
344         [Nn][Oo] | '')
345                 ;;
346         *)
347                 for i in ${gif_interfaces}; do
348                         eval peers=\$gifconfig_$i
349                         case ${peers} in
350                         '')
351                                 continue
352                                 ;;
353                         *)
354                                 ifconfig $i create >/dev/null 2>&1
355                                 ifconfig $i tunnel ${peers}
356                                 ifconfig $i up
357                                 ;;
358                         esac
359                 done
360                 ;;
361         esac
364 # ifnet_rename
365 #       Rename all requested interfaces.
367 ifnet_rename()
370         _ifn_list="`ifconfig -l`"
371         [ -z "$_ifn_list" ] && return 0
372         for _if in ${_ifn_list} ; do
373                 _ifname=`get_if_var $_if ifconfig_IF_name`
374                 if [ ! -z "$_ifname" ]; then
375                         ifconfig $_if name $_ifname
376                 fi
377         done
378         return 0
382 # list_net_interfaces type
383 #       List all network interfaces. The type of interface returned
384 #       can be controlled by the type argument. The type
385 #       argument can be any of the following:
386 #               nodhcp - all interfaces, excluding DHCP configured interfaces
387 #               dhcp   - list only DHCP configured interfaces
388 #       If no argument is specified all network interfaces are output.
389 #       Note that the list will include cloned interfaces if applicable.
390 #       Cloned interfaces must already exist to have a chance to appear
391 #       in the list if ${network_interfaces} is set to `auto'.
393 list_net_interfaces()
395         type=$1
397         # Get a list of ALL the interfaces.  NOTE: cloned interfaces
398         # have already been configured so they should show up in the
399         # ifconfig -l output.
400         #
401         case ${network_interfaces} in
402         [Aa][Uu][Tt][Oo])
403                 _autolist="`ifconfig -l` `sysctl -in net.wlan.devices`"
404                 _lo=
405                 for _if in ${_autolist} ; do
406                         if [ "$_if" = "lo0" ]; then
407                                 _lo="lo0"
408                         else
409                                 _tmplist="${_tmplist} ${_if}"
410                         fi
411                 done
412                 _tmplist="${_lo} ${_tmplist}"
413                 ;;
414         *)
415                 _tmplist="${network_interfaces} ${cloned_interfaces}"
416                 ;;
417         esac
419         if [ -z "$type" ]; then
420                 echo $_tmplist
421                 return 0
422         fi
424         # Separate out dhcp and non-dhcp interfaces
425         #
426         _aprefix=
427         _bprefix=
428         for _if in ${_tmplist} ; do
429                 eval _ifarg="\$ifconfig_${_if}"
430                 case "$_ifarg" in
431                 [Dd][Hh][Cc][Pp])
432                         _dhcplist="${_dhcplist}${_aprefix}${_if}"
433                         [ -z "$_aprefix" ] && _aprefix=' '
434                         ;;
435                 ''|*)
436                         _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
437                         [ -z "$_bprefix" ] && _bprefix=' '
438                         ;;
439                 esac
440         done
442         case "$type" in
443         nodhcp)
444                 echo $_nodhcplist
445                 ;;
446         dhcp)
447                 echo $_dhcplist
448                 ;;
449         esac
450         return 0
453 hexdigit()
455         if [ $1 -lt 10 ]; then
456                 echo $1
457         else
458                 case $1 in
459                 10)     echo a ;;
460                 11)     echo b ;;
461                 12)     echo c ;;
462                 13)     echo d ;;
463                 14)     echo e ;;
464                 15)     echo f ;;
465                 esac
466         fi
469 hexprint()
471         val=$1
472         str=''
474         dig=`hexdigit $((${val} & 15))`
475         str=${dig}${str}
476         val=$((${val} >> 4))
477         while [ ${val} -gt 0 ]; do
478                 dig=`hexdigit $((${val} & 15))`
479                 str=${dig}${str}
480                 val=$((${val} >> 4))
481         done
483         echo ${str}
486 # Setup the interfaces for IPv6
487 network6_interface_setup()
489         interfaces=$*
490         rtsol_interfaces=''
491         case ${ipv6_gateway_enable} in
492         [Yy][Ee][Ss])
493                 rtsol_available=no
494                 ;;
495         *)
496                 rtsol_available=yes
497                 ;;
498         esac
499         for i in $interfaces; do
500                 rtsol_interface=yes
501                 prefix=`get_if_var $i ipv6_prefix_IF`
502                 if [ -n "${prefix}" ]; then
503                         rtsol_available=no
504                         rtsol_interface=no
505                         laddr=`network6_getladdr $i`
506                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
507                         for j in ${prefix}; do
508                                 address=$j\:${hostid}
509                                 ifconfig $i inet6 ${address} prefixlen 64 alias
511                                 case ${ipv6_gateway_enable} in
512                                 [Yy][Ee][Ss])
513                                         # subnet-router anycast address
514                                         # (rfc2373)
515                                         ifconfig $i inet6 $j:: prefixlen 64 \
516                                                 alias anycast
517                                         ;;
518                                 esac
519                         done
520                 fi
521                 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
522                 if [ -n "${ipv6_ifconfig}" ]; then
523                         rtsol_available=no
524                         rtsol_interface=no
525                         ifconfig $i inet6 ${ipv6_ifconfig} alias
526                 fi
528                 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
529                 then
530                         case ${i} in
531                         lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
532                                 ;;
533                         *)
534                                 rtsol_interfaces="${rtsol_interfaces} ${i}"
535                                 ;;
536                         esac
537                 else
538                         ifconfig $i inet6
539                 fi
540         done
542         if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
543                 # Act as endhost - automatically configured.
544                 # You can configure only single interface, as
545                 # specification assumes that autoconfigured host has
546                 # single interface only.
547                 sysctl net.inet6.ip6.accept_rtadv=1
548                 set ${rtsol_interfaces}
549                 ifconfig $1 up
550                 rtsol $1
551         fi
553         for i in $interfaces; do
554                 alias=0
555                 while : ; do
556                         ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
557                         if [ -z "${ipv6_ifconfig}" ]; then
558                                 break;
559                         fi
560                         ifconfig $i inet6 ${ipv6_ifconfig} alias
561                         alias=$((${alias} + 1))
562                 done
563         done
566 # Setup IPv6 to IPv4 mapping
567 network6_stf_setup()
569         case ${stf_interface_ipv4addr} in
570         [Nn][Oo] | '')
571                 ;;
572         *)
573                 # assign IPv6 addr and interface route for 6to4 interface
574                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
575                 OIFS="$IFS"
576                 IFS=".$IFS"
577                 set ${stf_interface_ipv4addr}
578                 IFS="$OIFS"
579                 hexfrag1=`hexprint $(($1*256 + $2))`
580                 hexfrag2=`hexprint $(($3*256 + $4))`
581                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
582                 case ${stf_interface_ipv6_ifid} in
583                 [Aa][Uu][Tt][Oo] | '')
584                         for i in ${ipv6_network_interfaces}; do
585                                 laddr=`network6_getladdr ${i}`
586                                 case ${laddr} in
587                                 '')
588                                         ;;
589                                 *)
590                                         break
591                                         ;;
592                                 esac
593                         done
594                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
595                                                       'fe80::\(.*\)%\(.*\)'`
596                         case ${stf_interface_ipv6_ifid} in
597                         '')
598                                 stf_interface_ipv6_ifid=0:0:0:1
599                                 ;;
600                         esac
601                         ;;
602                 esac
603                 ifconfig stf0 create >/dev/null 2>&1
604                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
605                         prefixlen ${stf_prefixlen}
606                 # disallow packets to malicious 6to4 prefix
607                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
608                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
609                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
610                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
611                 ;;
612         esac
615 # Setup static routes
616 network6_static_routes_setup()
618         # Set up any static routes.
619         case ${ipv6_defaultrouter} in
620         [Nn][Oo] | '')
621                 ;;
622         *)
623                 ipv6_static_routes="default ${ipv6_static_routes}"
624                 ipv6_route_default="default ${ipv6_defaultrouter}"
625                 ;;
626         esac
627         case ${ipv6_static_routes} in
628         [Nn][Oo] | '')
629                 ;;
630         *)
631                 for i in ${ipv6_static_routes}; do
632                         eval ipv6_route_args=\$ipv6_route_${i}
633                         route add -inet6 ${ipv6_route_args}
634                 done
635                 ;;
636         esac
639 # Install the "default interface" to kernel, which will be used
640 # as the default route when there's no router.
641 network6_default_interface_setup()
643         # Choose IPv6 default interface if it is not clearly specified.
644         case ${ipv6_default_interface} in
645         '')
646                 for i in ${ipv6_network_interfaces}; do
647                         case $i in
648                         lo0)
649                                 continue
650                                 ;;
651                         esac
652                         laddr=`network6_getladdr $i exclude_tentative`
653                         case ${laddr} in
654                         '')
655                                 ;;
656                         *)
657                                 ipv6_default_interface=$i
658                                 break
659                                 ;;
660                         esac
661                 done
662                 ;;
663         esac
665         # Disallow unicast packets without outgoing scope identifiers,
666         # or route such packets to a "default" interface, if it is specified.
667         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
668         case ${ipv6_default_interface} in
669         [Nn][Oo] | '')
670                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
671                 ;;
672         *)
673                 laddr=`network6_getladdr ${ipv6_default_interface}`
674                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
675                         -cloning
677                 # Disable installing the default interface with the
678                 # case net.inet6.ip6.forwarding=0 and
679                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
680                 # between the default router list and the manual
681                 # configured default route.
682                 case ${ipv6_gateway_enable} in
683                 [Yy][Ee][Ss])
684                         ;;
685                 *)
686                         if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
687                         then
688                                 ndp -I ${ipv6_default_interface}
689                         fi
690                         ;;
691                 esac
692                 ;;
693         esac
696 network6_getladdr()
698         ifconfig $1 2>/dev/null | while read proto addr rest; do
699                 case ${proto} in
700                 inet6)
701                         case ${addr} in
702                         fe80::*)
703                                 if [ -z "$2" ]; then
704                                         echo ${addr}
705                                         return
706                                 fi
707                                 case ${rest} in
708                                 *tentative*)
709                                         continue
710                                         ;;
711                                 *)
712                                         echo ${addr}
713                                         return
714                                 esac
715                         esac
716                 esac
717         done