26d71f2f06f6d4f6d1c5c569f2b5ca8cc23138fa
[dragonfly.git] / etc / network.subr
1 #
2 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3 #
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.
12 #
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.
24 #
25 # $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
26 #
27
28 #
29 # Subroutines commonly used from network startup scripts.
30 # Requires that /etc/rc.subr be loaded first.
31 #
32
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.
39 #
40 ifconfig_up()
41 {
42         local _cfg ifconfig_args
43         _cfg=1
44
45         ifconfig_args=`ifconfig_getargs $1`
46         if [ -n "${ifconfig_args}" ]; then
47                 ifconfig $1 ${ifconfig_args}
48                 ifconfig $1 up
49                 _cfg=0
50         fi
51
52         if wpaif $1; then
53                 ifconfig $1 up
54                 /etc/rc.d/wpa_supplicant start $1
55                 _cfg=0          # XXX: not sure this should count
56         fi
57
58         if dhcpif $1; then
59                 /etc/rc.d/dhcp_client start $1
60                 _cfg=0
61         fi
62
63         return $_cfg
64 }
65
66 # ifconfig_down if
67 #       Remove all inet entries from the $if interface. It returns
68 #       0 if inet entries were found and removed. It returns 1 if
69 #       no entries were found or they could not be removed.
70 #
71 ifconfig_down()
72 {
73         local _cfg _ifs oldifs _inet inetList
74
75         [ -z "$1" ] && return 1
76         _ifs="^"
77         _cfg=1
78
79         inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
80
81         oldifs="$IFS"
82         IFS="$_ifs"
83         for _inet in $inetList ; do
84                 # get rid of extraneous line
85                 [ -z "$_inet" ] && break
86
87                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
88
89                 IFS="$oldifs"
90                 ifconfig $1 ${_inet} delete
91                 IFS="$_ifs"
92                 _cfg=0
93         done
94         IFS="$oldifs"
95
96         if wpaif $1; then
97                 /etc/rc.d/wpa_supplicant stop $1
98         fi
99
100         if dhcpif $1; then
101                 /etc/rc.d/dhcp_client stop $1
102                 _cfg=0
103         fi
104
105         if ifexists $1; then
106                 ifconfig $1 down
107                 _cfg=0
108         fi
109
110         return $_cfg
111 }
112
113 # get_if_var if var [default]
114 #       Return the value of the pseudo-hash corresponding to $if where
115 #       $var is a string containg the sub-string "IF" which will be
116 #       replaced with $if after the characters defined in _punct are
117 #       replaced with '_'. If the variable is unset, replace it with
118 #       $default if given.
119 #
120 get_if_var()
121 {
122         local _if _punct_c _punct _var _default prefix suffix
123
124         if [ $# -ne 2 -a $# -ne 3 ]; then
125                 err 3 'USAGE: get_if_var name var [default]'
126         fi
127
128         _if=$1
129         _punct=". - / +"
130         for _punct_c in $_punct; do
131                 _if=`ltr ${_if} ${_punct_c} '_'`
132         done
133         _var=$2
134         _default=$3
135
136         prefix=${_var%%IF*}
137         suffix=${_var##*IF}
138         eval echo \${${prefix}${_if}${suffix}-${_default}}
139 }
140
141 # _ifconfig_getargs if [af]
142 #       Echos the arguments for the supplied interface to stdout.
143 #       Returns 1 if no interface is specified.
144 #       In general, the ifconfig_getargs() below should be used outside
145 #       this file.
146 #
147 _ifconfig_getargs()
148 {
149         local _if _ifn _af _args
150
151         _ifn=$1
152         _af=${2:+${2}_}
153
154         if [ -z "$_ifn" ]; then
155                 return 1
156         fi
157
158         _args=`get_if_var $_ifn ${_af}ifconfig_IF`
159         if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
160                 for _if in ${removable_interfaces} ; do
161                         if [ "$_if" = "$_ifn" ] ; then
162                                 _args=${pccard_ifconfig}
163                                 break
164                         fi
165                 done
166         fi
167
168         echo $_args
169 }
170
171 # ifconfig_getargs if [af]
172 #       Takes the result from _ifconfig_getargs() and removes pseudo
173 #       args such as DHCP and WPA.
174 #
175 ifconfig_getargs()
176 {
177         local _tmpargs _arg _args is_optarg
178
179         _tmpargs=`_ifconfig_getargs $1 $2`
180         if [ $? -eq 1 ]; then
181                 return 1
182         fi
183         _args=
184
185         is_optarg=no
186         for _arg in $_tmpargs; do
187                 if [ "$is_optarg" = "no" ]; then
188                         case $_arg in
189                         [Dd][Hh][Cc][Pp])
190                                 ;;
191                         [Ww][Pp][Aa])
192                                 ;;
193                         *)
194                                 _args="$_args $_arg"
195                                 case $_arg in
196                                 authmode)
197                                         is_optarg=yes
198                                         ;;
199                                 esac
200                                 ;;
201                         esac
202                 else
203                         _args="$_args $_arg"
204                         is_optarg=no
205                 fi
206         done
207
208         echo $_args
209 }
210
211 # ipv6if if
212 #       Returns 0 if the interface should be configured for IPv6 and
213 #       1 otherwise.
214 #
215 ipv6if()
216 {
217         local _if _tmpargs
218         _if=$1
219
220         # lo0 is always IPv6-enabled
221         if [ "$_if" = "lo0" ]; then
222                 return 0
223         fi
224
225         case ${ipv6_enable} in
226         [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
227                 return 1
228                 ;;
229         esac
230
231         case "${ipv6_network_interfaces}" in
232         $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
233                 # True if $ipv6_ifconfig_IF is defined.
234                 _tmpargs=`_ifconfig_getargs $_if ipv6`
235                 if [ -n "${_tmpargs}" ]; then
236                         return 0
237                 fi
238
239                 # True if $ipv6_prefix_IF is defined.
240                 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
241                 if [ -n "${_tmpargs}" ]; then
242                         return 0
243                 fi
244
245                 ;;
246         esac
247
248         return 1
249 }
250
251 # dhcpif if [ipv4|ipv6]
252 #       Returns 0 if the interface needs DHCP for IPv4/IPv6 and 1 otherwise.
253 #       If the second argument is "ipv4" (or "ipv6"), then only IPv4 (or
254 #       IPv6) is checked, otherwise both are checked.
255 #
256 dhcpif()
257 {
258         local _tmpargs _arg _if _af
259         _if=$1
260         _af=$2
261
262         if [ -z "$_af" -o "$_af" = "ipv4" ]; then
263                 _tmpargs=`_ifconfig_getargs $_if`
264                 for _arg in $_tmpargs; do
265                         case $_arg in
266                         [Dd][Hh][Cc][Pp])
267                                 return 0
268                                 ;;
269                         esac
270                 done
271         fi
272
273         if [ -z "$_af" -o "$_af" = "ipv6" ] && ipv6if $_if; then
274                 _tmpargs=`_ifconfig_getargs $_if ipv6`
275                 for _arg in $_tmpargs; do
276                         case $_arg in
277                         [Dd][Hh][Cc][Pp])
278                                 return 0
279                                 ;;
280                         esac
281                 done
282         fi
283
284         return 1
285 }
286
287 # wpaif if
288 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
289 #
290 wpaif()
291 {
292         local _tmpargs _arg is_optarg
293
294         _tmpargs=`_ifconfig_getargs $1`
295         is_optarg=no
296         for _arg in $_tmpargs; do
297                 if [ "$is_optarg" = "no" ]; then
298                         case $_arg in
299                         [Ww][Pp][Aa])
300                                 return 0
301                                 ;;
302                         authmode)
303                                 is_optarg=yes
304                                 ;;
305                         esac
306                 else
307                         is_optarg=no
308                 fi
309         done
310
311         return 1
312 }
313
314 # ifexists if
315 #       Returns 0 if the interface exists and 1 otherwise.
316 #
317 ifexists()
318 {
319         [ -z "$1" ] && return 1
320         ifconfig -n $1 >/dev/null 2>&1
321 }
322
323 # ifalias_common if action [ipv6]
324 #       Helper function for ifalias_up() and ifalias_down().
325 #       The $action argument can be either "alias" (to add an
326 #       alias) or "-alias" (to remove an alias).
327 #       Returns 0 if at least one alias was added/removed or
328 #       1 if there were none.
329 #
330 ifalias_common()
331 {
332         local _if _action _af _af2 _ret _var _args _alias
333         _if=$1
334         _action=$2
335         _af=$3
336
337         _ret=1
338         _alias=0
339         while : ; do
340                 if [ "${_af}" = "ipv6" ]; then
341                         _af2="inet6"
342                         _var="ipv6_ifconfig_IF_alias${_alias}"
343                 else
344                         _af2="inet"
345                         _var="ifconfig_IF_alias${_alias}"
346                 fi
347                 _args=`get_if_var $_if $_var`
348                 _args="${_args#${_af2} }"
349                 if [ -z "${_args}" ]; then
350                         break
351                 fi
352                 ifconfig $_if $_af2 $_args $_action
353                 _alias=$((${_alias} + 1))
354                 _ret=0
355         done
356         return $_ret
357 }
358
359 # ifalias_up if [ipv6]
360 #       Configure IPv4 aliases for network interface $if or
361 #       IPv6 aliases if the second argument is "ipv6".
362 #       It returns 0 if at least one alias was configured or
363 #       1 if there were none.
364 #
365 ifalias_up()
366 {
367         ifalias_common $1 alias $2
368 }
369
370 # ifalias_down if [ipv6]
371 #       Remove IPv4 aliases for network interface $if or
372 #       IPv6 aliases if the second argument is "ipv6".
373 #       It returns 0 if at least one alias was removed or
374 #       1 if there were none.
375 #
376 ifalias_down()
377 {
378         ifalias_common $1 -alias $2
379 }
380
381 # ifscript_up if
382 #       Evaluate a startup script for the $if interface.
383 #       It returns 0 if a script was found and processed or
384 #       1 if no script was found.
385 #
386 ifscript_up()
387 {
388         if [ -r /etc/start_if.$1 ]; then
389                 . /etc/start_if.$1
390                 return 0
391         fi
392         return 1
393 }
394
395 # ifscript_down if
396 #       Evaluate a shutdown script for the $if interface.
397 #       It returns 0 if a script was found and processed or
398 #       1 if no script was found.
399 #
400 ifscript_down()
401 {
402         if [ -r /etc/stop_if.$1 ]; then
403                 . /etc/stop_if.$1
404                 return 0
405         fi
406         return 1
407 }
408
409 # wlan_up
410 #       Create IEEE 802.11 interfaces.
411 #
412 wlan_up()
413 {
414         local _prefix _list parent child child_wlans create_args debug_flags
415         _prefix=
416         _list=
417
418         for parent in `${SYSCTL_N} -q net.wlan.devices`; do
419                 # Parse wlans_$parent="$child ..."
420                 child_wlans=`get_if_var $parent wlans_IF`
421                 for child in ${child_wlans}; do
422                         if ifexists $child; then
423                                 continue
424                         fi
425
426                         create_args="wlandev $parent `get_if_var $child create_args_IF`"
427                         debug_flags="`get_if_var $child wlandebug_IF`"
428                         if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
429                                 ifconfig $child create ${create_args}
430                         else
431                                 ifconfig wlan create ${create_args} name $child
432                         fi
433                         if [ $? -eq 0 ]; then
434                                 _list="${_list}${_prefix}${child}"
435                                 [ -z "$_prefix" ] && _prefix=' '
436                         fi
437                         if [ -n "${debug_flags}" ]; then
438                                 wlandebug -i $child ${debug_flags}
439                         fi
440                 done
441         done
442
443         if [ -n "${_list}" ]; then
444                 echo "Created wlan interfaces: ${_list}"
445         fi
446         debug "Created wlan interfaces: ${_list}"
447 }
448
449 # wlan_down
450 #       Destroy IEEE 802.11 interfaces.
451 #
452 wlan_down()
453 {
454         local _prefix _list parent child child_wlans
455         _prefix=
456         _list=
457
458         for parent in `${SYSCTL_N} -q net.wlan.devices`; do
459                 child_wlans=`get_if_var $parent wlans_IF`
460                 for child in ${child_wlans}; do
461                         if ! ifexists $child; then
462                                 continue
463                         fi
464
465                         ifconfig -n $child destroy
466                         if [ $? -eq 0 ]; then
467                                 _list="${_list}${_prefix}${child}"
468                                 [ -z "$_prefix" ] && _prefix=' '
469                         fi
470                 done
471         done
472
473         if [ -n "${_list}" ]; then
474                 echo "Destroyed wlan interfaces: ${_list}"
475         fi
476         debug "Destroyed wlan interfaces: ${_list}"
477 }
478
479 # clone_up
480 #       Create cloneable interfaces.
481 #
482 clone_up()
483 {
484         local _prefix _list ifn
485         _prefix=
486         _list=
487
488         for ifn in ${cloned_interfaces}; do
489                 ifconfig ${ifn} create
490                 if [ $? -eq 0 ]; then
491                         _list="${_list}${_prefix}${ifn}"
492                         [ -z "$_prefix" ] && _prefix=' '
493                 fi
494         done
495         if [ -n "${_list}" ]; then
496                 echo "Created clone interfaces: ${_list}"
497         fi
498         debug "Created clone interfaces: ${_list}"
499 }
500
501 # clone_down
502 #       Destroy cloned interfaces.
503 #
504 clone_down()
505 {
506         local _prefix _list ifn
507         _prefix=
508         _list=
509
510         for ifn in ${cloned_interfaces}; do
511                 ifconfig ${ifn} destroy
512                 if [ $? -eq 0 ]; then
513                         _list="${_list}${_prefix}${ifn}"
514                         [ -z "$_prefix" ] && _prefix=' '
515                 fi
516         done
517         if [ -n "${_list}" ]; then
518                 echo "Destroyed clone interfaces: ${_list}"
519         fi
520         debug "Destroyed clone interfaces: ${_list}"
521 }
522
523 # gif_up
524 #       Create IPv6<-->IPv4 tunnels
525 #
526 gif_up() {
527         local _if _peers
528
529         case ${gif_interfaces} in
530         [Nn][Oo] | '')
531                 return
532                 ;;
533         esac
534
535         for _if in ${gif_interfaces}; do
536                 eval _peers=\$gifconfig_${_if}
537                 case ${_peers} in
538                 '')
539                         continue
540                         ;;
541                 *)
542                         ifconfig $_if create >/dev/null 2>&1
543                         ifconfig $_if tunnel ${_peers}
544                         ifconfig $_if up
545                         ;;
546                 esac
547         done
548 }
549
550 # ifnet_rename
551 #       Rename all requested interfaces.
552 #
553 ifnet_rename()
554 {
555         local _ifn_list _if _ifname
556
557         _ifn_list=$(ifconfig -l)
558         [ -z "$_ifn_list" ] && return 0
559
560         for _if in ${_ifn_list} ; do
561                 _ifname=`get_if_var $_if ifconfig_IF_name`
562                 if [ -n "$_ifname" ]; then
563                         ifconfig $_if name $_ifname
564                 fi
565         done
566         return 0
567 }
568
569 # list_net_interfaces
570 #       List all network interfaces.
571 #       Note that the list will include cloned interfaces if applicable.
572 #       Cloned interfaces must already exist to have a chance to appear
573 #       in the list if ${network_interfaces} is set to `auto'.
574 #
575 list_net_interfaces()
576 {
577         local _tmplist _autolist _lo _if
578
579         case ${network_interfaces} in
580         [Aa][Uu][Tt][Oo])
581                 _autolist=$(ifconfig -l)
582                 _lo=
583                 for _if in ${_autolist} ; do
584                         if [ "$_if" = "lo0" ]; then
585                                 _lo="lo0"
586                         else
587                                 _tmplist="${_tmplist} ${_if}"
588                         fi
589                 done
590                 _tmplist="${_lo} ${_tmplist}"
591                 ;;
592         *)
593                 _tmplist="${network_interfaces} ${cloned_interfaces}"
594                 ;;
595         esac
596
597         echo $_tmplist
598 }
599
600 hexdigit()
601 {
602         if [ $1 -lt 10 ]; then
603                 echo $1
604         else
605                 case $1 in
606                 10)     echo a ;;
607                 11)     echo b ;;
608                 12)     echo c ;;
609                 13)     echo d ;;
610                 14)     echo e ;;
611                 15)     echo f ;;
612                 esac
613         fi
614 }
615
616 hexprint()
617 {
618         local val str dig
619         val=$1
620         str=''
621
622         dig=`hexdigit $((${val} & 15))`
623         str=${dig}${str}
624         val=$((${val} >> 4))
625         while [ ${val} -gt 0 ]; do
626                 dig=`hexdigit $((${val} & 15))`
627                 str=${dig}${str}
628                 val=$((${val} >> 4))
629         done
630
631         echo ${str}
632 }
633
634
635 #
636 # IPv6-specific setup subroutines
637 #
638
639 # Setup the interfaces for IPv6
640 network6_interface_setup()
641 {
642         local interfaces rtsol_interfaces ipv6_ifconfig
643         local rtsol_available rtsol_interface
644         local prefix laddr hostid address
645         local _if j
646
647         interfaces=$*
648         rtsol_interfaces=''
649         case ${ipv6_gateway_enable} in
650         [Yy][Ee][Ss])
651                 rtsol_available=no
652                 ;;
653         *)
654                 rtsol_available=yes
655                 ;;
656         esac
657         for _if in $interfaces; do
658                 rtsol_interface=yes
659                 prefix=`get_if_var $_if ipv6_prefix_IF`
660                 if [ -n "${prefix}" ]; then
661                         rtsol_available=no
662                         rtsol_interface=no
663                         laddr=`network6_getladdr $_if`
664                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
665                         for j in ${prefix}; do
666                                 address=$j\:${hostid}
667                                 ifconfig $_if inet6 ${address} prefixlen 64 alias
668
669                                 case ${ipv6_gateway_enable} in
670                                 [Yy][Ee][Ss])
671                                         # subnet-router anycast address
672                                         # (rfc2373)
673                                         ifconfig $_if inet6 $j:: prefixlen 64 \
674                                                 alias anycast
675                                         ;;
676                                 esac
677                         done
678                 fi
679                 ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
680                 if [ -n "${ipv6_ifconfig}" ]; then
681                         rtsol_available=no
682                         rtsol_interface=no
683                         ifconfig $_if inet6 ${ipv6_ifconfig} alias
684                 fi
685
686                 if [ "${rtsol_available}" = "yes" -a \
687                      "${rtsol_interface}" = "yes" ]; then
688                         case ${i} in
689                         lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
690                                 ;;
691                         *)
692                                 rtsol_interfaces="${rtsol_interfaces} ${_if}"
693                                 ;;
694                         esac
695                 else
696                         ifconfig $_if inet6
697                 fi
698         done
699
700         if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
701                 # Act as endhost - automatically configured.
702                 # You can configure only single interface, as
703                 # specification assumes that autoconfigured host has
704                 # single interface only.
705                 ${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
706                 set ${rtsol_interfaces}
707                 ifconfig $1 up
708                 echo "Auto configuring interface $1 ..."
709                 rtsol $1
710         fi
711
712         for _if in $interfaces; do
713                 ifalias_up $_if ipv6
714         done
715 }
716
717 # Setup IPv6 to IPv4 mapping
718 network6_stf_setup()
719 {
720         local stf_prefixlen stf_interface_ipv6_ifid
721         local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
722         local _if OIFS
723
724         case ${stf_interface_ipv4addr} in
725         [Nn][Oo] | '')
726                 ;;
727         *)
728                 # assign IPv6 addr and interface route for 6to4 interface
729                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
730                 OIFS="$IFS"
731                 IFS=".$IFS"
732                 set ${stf_interface_ipv4addr}
733                 IFS="$OIFS"
734                 hexfrag1=`hexprint $(($1*256 + $2))`
735                 hexfrag2=`hexprint $(($3*256 + $4))`
736                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
737                 case ${stf_interface_ipv6_ifid} in
738                 [Aa][Uu][Tt][Oo] | '')
739                         for _if in ${ipv6_network_interfaces}; do
740                                 laddr=`network6_getladdr $_if`
741                                 case ${laddr} in
742                                 '')
743                                         ;;
744                                 *)
745                                         break
746                                         ;;
747                                 esac
748                         done
749                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
750                                                       'fe80::\(.*\)%\(.*\)'`
751                         case ${stf_interface_ipv6_ifid} in
752                         '')
753                                 stf_interface_ipv6_ifid=0:0:0:1
754                                 ;;
755                         esac
756                         ;;
757                 esac
758                 ifconfig stf0 create >/dev/null 2>&1
759                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
760                         prefixlen ${stf_prefixlen}
761                 # disallow packets to malicious 6to4 prefix
762                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
763                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
764                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
765                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
766                 ;;
767         esac
768 }
769
770 # Setup static routes
771 network6_static_routes_setup()
772 {
773         local _rt
774
775         # Set up any static routes.
776         case ${ipv6_defaultrouter} in
777         [Nn][Oo] | '')
778                 ;;
779         *)
780                 ipv6_static_routes="default ${ipv6_static_routes}"
781                 ipv6_route_default="default ${ipv6_defaultrouter}"
782                 ;;
783         esac
784         case ${ipv6_static_routes} in
785         [Nn][Oo] | '')
786                 ;;
787         *)
788                 for _rt in ${ipv6_static_routes}; do
789                         eval ipv6_route_args=\$ipv6_route_${_rt}
790                         route add -inet6 ${ipv6_route_args}
791                 done
792                 ;;
793         esac
794 }
795
796 # Install the "default interface" to kernel, which will be used
797 # as the default route when there's no router.
798 network6_default_interface_setup()
799 {
800         local _if laddr
801
802         # Choose IPv6 default interface if it is not clearly specified.
803         case ${ipv6_default_interface} in
804         '')
805                 for _if in ${ipv6_network_interfaces}; do
806                         if [ "${_if}" = "lo0" ]; then
807                                 continue
808                         fi
809
810                         laddr=`network6_getladdr $_if exclude_tentative`
811                         case ${laddr} in
812                         '')
813                                 ;;
814                         *)
815                                 ipv6_default_interface=$_if
816                                 break
817                                 ;;
818                         esac
819                 done
820                 ;;
821         esac
822
823         # Disallow unicast packets without outgoing scope identifiers,
824         # or route such packets to a "default" interface, if it is specified.
825         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
826         case ${ipv6_default_interface} in
827         [Nn][Oo] | '')
828                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
829                 ;;
830         *)
831                 laddr=`network6_getladdr ${ipv6_default_interface}`
832                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
833                         -cloning
834
835                 # Disable installing the default interface with the
836                 # case net.inet6.ip6.forwarding=0 and
837                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
838                 # between the default router list and the manual
839                 # configured default route.
840                 case ${ipv6_gateway_enable} in
841                 [Yy][Ee][Ss])
842                         ;;
843                 *)
844                         if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
845                         then
846                                 ndp -I ${ipv6_default_interface}
847                         fi
848                         ;;
849                 esac
850                 ;;
851         esac
852 }
853
854 network6_getladdr()
855 {
856         local proto addr rest
857
858         ifconfig $1 2>/dev/null | while read proto addr rest; do
859                 case ${proto} in
860                 inet6)
861                         case ${addr} in
862                         fe80::*)
863                                 if [ -z "$2" ]; then
864                                         echo ${addr}
865                                         return
866                                 fi
867                                 case ${rest} in
868                                 *tentative*)
869                                         continue
870                                         ;;
871                                 *)
872                                         echo ${addr}
873                                         return
874                                 esac
875                         esac
876                 esac
877         done
878 }