Allow optional "inet6" keyword in ipv6_ifconfig_<ifname>
[dragonfly.git] / etc / network.subr
CommitLineData
9c600e7d
MD
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#
f26c267a 25# $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
9c600e7d
MD
26#
27
28#
29# Subroutines commonly used from network startup scripts.
e4b0f1d1 30# Requires that /etc/rc.subr be loaded first.
9c600e7d
MD
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
f26c267a
SW
37# had no arguments. Pseudo arguments DHCP and WPA are handled
38# here.
9c600e7d
MD
39#
40ifconfig_up()
41{
e4b0f1d1 42 local _cfg ifconfig_args
f26c267a
SW
43 _cfg=1
44
45 ifconfig_args=`ifconfig_getargs $1`
9c600e7d
MD
46 if [ -n "${ifconfig_args}" ]; then
47 ifconfig $1 ${ifconfig_args}
5ad36ff1 48 ifconfig $1 up
f26c267a 49 _cfg=0
9c600e7d 50 fi
f26c267a
SW
51
52 if wpaif $1; then
5ad36ff1 53 ifconfig $1 up
f26c267a
SW
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
817cf585 59 /etc/rc.d/dhcp_client start $1
f26c267a
SW
60 _cfg=0
61 fi
62
63 return $_cfg
9c600e7d
MD
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#
71ifconfig_down()
72{
e4b0f1d1
AL
73 local _cfg _ifs oldifs _inet inetList
74
9c600e7d
MD
75 [ -z "$1" ] && return 1
76 _ifs="^"
f26c267a 77 _cfg=1
9c600e7d
MD
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"
f26c267a 92 _cfg=0
9c600e7d
MD
93 done
94 IFS="$oldifs"
95
f26c267a
SW
96 if wpaif $1; then
97 /etc/rc.d/wpa_supplicant stop $1
98 fi
99
100 if dhcpif $1; then
817cf585 101 /etc/rc.d/dhcp_client stop $1
f26c267a
SW
102 _cfg=0
103 fi
104
5ad36ff1
AL
105 if ifexists $1; then
106 ifconfig $1 down
107 _cfg=0
108 fi
109
f26c267a
SW
110 return $_cfg
111}
112
d86e211f
AH
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.
e4b0f1d1 119#
d86e211f
AH
120get_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
8cd2e074 141# _ifconfig_getargs if [af]
f26c267a 142# Echos the arguments for the supplied interface to stdout.
e4b0f1d1
AL
143# Returns 1 if no interface is specified.
144# In general, the ifconfig_getargs() below should be used outside
145# this file.
146#
f26c267a
SW
147_ifconfig_getargs()
148{
e4b0f1d1
AL
149 local _if _ifn _af _args
150
f26c267a 151 _ifn=$1
8cd2e074
AL
152 _af=${2:+${2}_}
153
f26c267a
SW
154 if [ -z "$_ifn" ]; then
155 return 1
156 fi
157
8cd2e074 158 _args=`get_if_var $_ifn ${_af}ifconfig_IF`
f26c267a
SW
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
8cd2e074 171# ifconfig_getargs if [af]
e4b0f1d1 172# Takes the result from _ifconfig_getargs() and removes pseudo
f26c267a 173# args such as DHCP and WPA.
e4b0f1d1 174#
f26c267a
SW
175ifconfig_getargs()
176{
e4b0f1d1
AL
177 local _tmpargs _arg _args is_optarg
178
8cd2e074 179 _tmpargs=`_ifconfig_getargs $1 $2`
f26c267a
SW
180 if [ $? -eq 1 ]; then
181 return 1
182 fi
183 _args=
184
1f03f592 185 is_optarg=no
f26c267a 186 for _arg in $_tmpargs; do
1f03f592
SW
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
f26c267a 203 _args="$_args $_arg"
1f03f592
SW
204 is_optarg=no
205 fi
f26c267a
SW
206 done
207
208 echo $_args
209}
210
da4dd514
AL
211# ipv6if if
212# Returns 0 if the interface should be configured for IPv6 and
213# 1 otherwise.
e4b0f1d1 214#
da4dd514
AL
215ipv6if()
216{
217 local _if _tmpargs
218 _if=$1
219
220 # lo0 is always IPv6-enabled
e4b0f1d1 221 if [ "$_if" = "lo0" ]; then
da4dd514 222 return 0
e4b0f1d1 223 fi
da4dd514
AL
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
329743de
AL
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.
e4b0f1d1 255#
f26c267a
SW
256dhcpif()
257{
329743de
AL
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
f26c267a
SW
284 return 1
285}
286
287# wpaif if
288# Returns 0 if the interface is a WPA interface and 1 otherwise.
e4b0f1d1 289#
f26c267a
SW
290wpaif()
291{
e4b0f1d1
AL
292 local _tmpargs _arg is_optarg
293
f26c267a 294 _tmpargs=`_ifconfig_getargs $1`
1f03f592 295 is_optarg=no
f26c267a 296 for _arg in $_tmpargs; do
1f03f592
SW
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
f26c267a 309 done
e4b0f1d1 310
f26c267a 311 return 1
9c600e7d
MD
312}
313
cc419283
AL
314# ifexists if
315# Returns 0 if the interface exists and 1 otherwise.
e4b0f1d1 316#
cc419283
AL
317ifexists()
318{
319 [ -z "$1" ] && return 1
e4b0f1d1 320 ifconfig -n $1 >/dev/null 2>&1
cc419283
AL
321}
322
3b4e0f2a
AL
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
9c600e7d
MD
328# 1 if there were none.
329#
3b4e0f2a 330ifalias_common()
9c600e7d 331{
3b4e0f2a
AL
332 local _if _action _af _af2 _ret _var _args _alias
333 _if=$1
334 _action=$2
335 _af=$3
e4b0f1d1 336
9c600e7d 337 _ret=1
3b4e0f2a 338 _alias=0
9c600e7d 339 while : ; do
3b4e0f2a
AL
340 if [ "${_af}" = "ipv6" ]; then
341 _af2="inet6"
342 _var="ipv6_ifconfig_IF_alias${_alias}"
9c600e7d 343 else
3b4e0f2a
AL
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
9c600e7d
MD
350 break
351 fi
3b4e0f2a
AL
352 ifconfig $_if $_af2 $_args $_action
353 _alias=$((${_alias} + 1))
354 _ret=0
9c600e7d
MD
355 done
356 return $_ret
357}
358
3b4e0f2a
AL
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#
365ifalias_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".
9c600e7d
MD
373# It returns 0 if at least one alias was removed or
374# 1 if there were none.
375#
376ifalias_down()
377{
3b4e0f2a 378 ifalias_common $1 -alias $2
9c600e7d
MD
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#
386ifscript_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#
400ifscript_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
3928a593
AL
409# wlan_up
410# Create IEEE 802.11 interfaces.
411#
412wlan_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#
452wlan_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
e4b0f1d1
AL
479# clone_up
480# Create cloneable interfaces.
9c600e7d
MD
481#
482clone_up()
483{
e4b0f1d1 484 local _prefix _list ifn
9c600e7d
MD
485 _prefix=
486 _list=
e4b0f1d1 487
9c600e7d
MD
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
e4b0f1d1
AL
495 if [ -n "${_list}" ]; then
496 echo "Created clone interfaces: ${_list}"
497 fi
498 debug "Created clone interfaces: ${_list}"
9c600e7d
MD
499}
500
e4b0f1d1
AL
501# clone_down
502# Destroy cloned interfaces.
9c600e7d
MD
503#
504clone_down()
505{
e4b0f1d1 506 local _prefix _list ifn
9c600e7d
MD
507 _prefix=
508 _list=
e4b0f1d1 509
9c600e7d
MD
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
e4b0f1d1
AL
517 if [ -n "${_list}" ]; then
518 echo "Destroyed clone interfaces: ${_list}"
519 fi
520 debug "Destroyed clone interfaces: ${_list}"
9c600e7d
MD
521}
522
e4b0f1d1
AL
523# gif_up
524# Create IPv6<-->IPv4 tunnels
525#
9c600e7d 526gif_up() {
e4b0f1d1
AL
527 local _if _peers
528
9c600e7d
MD
529 case ${gif_interfaces} in
530 [Nn][Oo] | '')
e4b0f1d1 531 return
9c600e7d
MD
532 ;;
533 esac
e4b0f1d1
AL
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
9c600e7d
MD
548}
549
f26c267a
SW
550# ifnet_rename
551# Rename all requested interfaces.
552#
553ifnet_rename()
554{
e4b0f1d1 555 local _ifn_list _if _ifname
f26c267a 556
e4b0f1d1 557 _ifn_list=$(ifconfig -l)
f26c267a 558 [ -z "$_ifn_list" ] && return 0
e4b0f1d1 559
f26c267a 560 for _if in ${_ifn_list} ; do
a939f930 561 _ifname=`get_if_var $_if ifconfig_IF_name`
e4b0f1d1 562 if [ -n "$_ifname" ]; then
f26c267a
SW
563 ifconfig $_if name $_ifname
564 fi
565 done
566 return 0
567}
568
0e387345
AL
569# list_net_interfaces
570# List all network interfaces.
acba6ed7
SS
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'.
9c600e7d
MD
574#
575list_net_interfaces()
576{
0e387345
AL
577 local _tmplist _autolist _lo _if
578
9c600e7d
MD
579 case ${network_interfaces} in
580 [Aa][Uu][Tt][Oo])
3928a593 581 _autolist=$(ifconfig -l)
667713fe
HT
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}"
9c600e7d
MD
591 ;;
592 *)
acba6ed7 593 _tmplist="${network_interfaces} ${cloned_interfaces}"
9c600e7d
MD
594 ;;
595 esac
9c600e7d 596
0e387345 597 echo $_tmplist
9c600e7d
MD
598}
599
600hexdigit()
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
616hexprint()
617{
e4b0f1d1 618 local val str dig
9c600e7d
MD
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
e4b0f1d1
AL
634
635#
636# IPv6-specific setup subroutines
637#
638
9c600e7d
MD
639# Setup the interfaces for IPv6
640network6_interface_setup()
641{
e4b0f1d1
AL
642 local interfaces rtsol_interfaces ipv6_ifconfig
643 local rtsol_available rtsol_interface
3b4e0f2a 644 local prefix laddr hostid address
e4b0f1d1
AL
645 local _if j
646
9c600e7d
MD
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
e4b0f1d1 657 for _if in $interfaces; do
9c600e7d 658 rtsol_interface=yes
e4b0f1d1 659 prefix=`get_if_var $_if ipv6_prefix_IF`
9c600e7d
MD
660 if [ -n "${prefix}" ]; then
661 rtsol_available=no
662 rtsol_interface=no
e4b0f1d1 663 laddr=`network6_getladdr $_if`
9c600e7d
MD
664 hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
665 for j in ${prefix}; do
666 address=$j\:${hostid}
e4b0f1d1 667 ifconfig $_if inet6 ${address} prefixlen 64 alias
9c600e7d
MD
668
669 case ${ipv6_gateway_enable} in
670 [Yy][Ee][Ss])
671 # subnet-router anycast address
672 # (rfc2373)
e4b0f1d1 673 ifconfig $_if inet6 $j:: prefixlen 64 \
9c600e7d
MD
674 alias anycast
675 ;;
676 esac
677 done
678 fi
e4b0f1d1 679 ipv6_ifconfig=`ifconfig_getargs $_if ipv6`
203bea78 680 ipv6_ifconfig="${ipv6_ifconfig#inet6 }"
9c600e7d
MD
681 if [ -n "${ipv6_ifconfig}" ]; then
682 rtsol_available=no
683 rtsol_interface=no
e4b0f1d1 684 ifconfig $_if inet6 ${ipv6_ifconfig} alias
9c600e7d
MD
685 fi
686
e4b0f1d1
AL
687 if [ "${rtsol_available}" = "yes" -a \
688 "${rtsol_interface}" = "yes" ]; then
9c600e7d 689 case ${i} in
06937ef9 690 lo0|gif[0-9]*|stf[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
9c600e7d
MD
691 ;;
692 *)
e4b0f1d1 693 rtsol_interfaces="${rtsol_interfaces} ${_if}"
9c600e7d
MD
694 ;;
695 esac
696 else
e4b0f1d1 697 ifconfig $_if inet6
9c600e7d
MD
698 fi
699 done
700
e4b0f1d1 701 if [ "${rtsol_available}" = "yes" -a -n "${rtsol_interfaces}" ]; then
9c600e7d
MD
702 # Act as endhost - automatically configured.
703 # You can configure only single interface, as
704 # specification assumes that autoconfigured host has
705 # single interface only.
b0a4258d 706 ${SYSCTL_W} net.inet6.ip6.accept_rtadv=1
9c600e7d
MD
707 set ${rtsol_interfaces}
708 ifconfig $1 up
e4b0f1d1 709 echo "Auto configuring interface $1 ..."
9c600e7d
MD
710 rtsol $1
711 fi
712
e4b0f1d1 713 for _if in $interfaces; do
3b4e0f2a 714 ifalias_up $_if ipv6
9c600e7d
MD
715 done
716}
717
718# Setup IPv6 to IPv4 mapping
719network6_stf_setup()
720{
e4b0f1d1
AL
721 local stf_prefixlen stf_interface_ipv6_ifid
722 local hexfrag1 hexfrag2 ipv4_in_hexformat laddr
723 local _if OIFS
724
9c600e7d
MD
725 case ${stf_interface_ipv4addr} in
726 [Nn][Oo] | '')
727 ;;
728 *)
729 # assign IPv6 addr and interface route for 6to4 interface
730 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
731 OIFS="$IFS"
732 IFS=".$IFS"
733 set ${stf_interface_ipv4addr}
734 IFS="$OIFS"
735 hexfrag1=`hexprint $(($1*256 + $2))`
736 hexfrag2=`hexprint $(($3*256 + $4))`
737 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
738 case ${stf_interface_ipv6_ifid} in
739 [Aa][Uu][Tt][Oo] | '')
e4b0f1d1
AL
740 for _if in ${ipv6_network_interfaces}; do
741 laddr=`network6_getladdr $_if`
9c600e7d
MD
742 case ${laddr} in
743 '')
744 ;;
745 *)
746 break
747 ;;
748 esac
749 done
750 stf_interface_ipv6_ifid=`expr "${laddr}" : \
751 'fe80::\(.*\)%\(.*\)'`
752 case ${stf_interface_ipv6_ifid} in
753 '')
754 stf_interface_ipv6_ifid=0:0:0:1
755 ;;
756 esac
757 ;;
758 esac
759 ifconfig stf0 create >/dev/null 2>&1
760 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
761 prefixlen ${stf_prefixlen}
762 # disallow packets to malicious 6to4 prefix
763 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
764 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
765 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
766 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
767 ;;
768 esac
769}
770
771# Setup static routes
772network6_static_routes_setup()
773{
e4b0f1d1
AL
774 local _rt
775
9c600e7d
MD
776 # Set up any static routes.
777 case ${ipv6_defaultrouter} in
778 [Nn][Oo] | '')
779 ;;
780 *)
781 ipv6_static_routes="default ${ipv6_static_routes}"
782 ipv6_route_default="default ${ipv6_defaultrouter}"
783 ;;
784 esac
785 case ${ipv6_static_routes} in
786 [Nn][Oo] | '')
787 ;;
788 *)
e4b0f1d1
AL
789 for _rt in ${ipv6_static_routes}; do
790 eval ipv6_route_args=\$ipv6_route_${_rt}
9c600e7d
MD
791 route add -inet6 ${ipv6_route_args}
792 done
793 ;;
794 esac
795}
796
9c600e7d
MD
797# Install the "default interface" to kernel, which will be used
798# as the default route when there's no router.
799network6_default_interface_setup()
800{
e4b0f1d1
AL
801 local _if laddr
802
9c600e7d
MD
803 # Choose IPv6 default interface if it is not clearly specified.
804 case ${ipv6_default_interface} in
805 '')
e4b0f1d1
AL
806 for _if in ${ipv6_network_interfaces}; do
807 if [ "${_if}" = "lo0" ]; then
9c600e7d 808 continue
e4b0f1d1
AL
809 fi
810
811 laddr=`network6_getladdr $_if exclude_tentative`
9c600e7d
MD
812 case ${laddr} in
813 '')
814 ;;
815 *)
e4b0f1d1 816 ipv6_default_interface=$_if
9c600e7d
MD
817 break
818 ;;
819 esac
820 done
821 ;;
822 esac
823
824 # Disallow unicast packets without outgoing scope identifiers,
825 # or route such packets to a "default" interface, if it is specified.
826 route add -inet6 fe80:: -prefixlen 10 ::1 -reject
827 case ${ipv6_default_interface} in
828 [Nn][Oo] | '')
829 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
830 ;;
831 *)
832 laddr=`network6_getladdr ${ipv6_default_interface}`
833 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
834 -cloning
835
836 # Disable installing the default interface with the
837 # case net.inet6.ip6.forwarding=0 and
838 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
839 # between the default router list and the manual
840 # configured default route.
841 case ${ipv6_gateway_enable} in
842 [Yy][Ee][Ss])
843 ;;
844 *)
b0a4258d 845 if [ `${SYSCTL_N} net.inet6.ip6.accept_rtadv` -eq 1 ]
9c600e7d
MD
846 then
847 ndp -I ${ipv6_default_interface}
848 fi
849 ;;
850 esac
851 ;;
852 esac
853}
854
855network6_getladdr()
856{
e4b0f1d1
AL
857 local proto addr rest
858
9c600e7d
MD
859 ifconfig $1 2>/dev/null | while read proto addr rest; do
860 case ${proto} in
861 inet6)
862 case ${addr} in
863 fe80::*)
864 if [ -z "$2" ]; then
865 echo ${addr}
866 return
867 fi
868 case ${rest} in
869 *tentative*)
870 continue
871 ;;
872 *)
873 echo ${addr}
874 return
875 esac
876 esac
877 esac
878 done
879}