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