2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 IP_SUPPORTED_PROTOCOLS
="${IP_SUPPORTED_PROTOCOLS} ipv6"
24 ipv6_device_autoconf_enable
() {
26 assert device_exists
"${device}"
28 sysctl_set
"net.ipv6.conf.${device}.accept_ra" 1
29 sysctl_set
"net.ipv6.conf.${device}.autoconf" 1
31 log INFO
"Enabled IPv6 auto-configuration on '${device}'"
33 # Disable IPv6 forwarding which cannot be used when the
34 # device is using IPv6 auto-configuration.
35 ipv6_device_forwarding_disable
"${device}"
38 ipv6_device_autoconf_disable
() {
40 assert device_exists
"${device}"
42 sysctl_set
"net.ipv6.conf.${device}.accept_ra" 0
43 sysctl_set
"net.ipv6.conf.${device}.autoconf" 0
45 log INFO
"Disabled IPv6 auto-configuration on '${device}'"
47 # Enable IPv6 forwarding again
48 ipv6_device_forwarding_enable
"${device}"
50 # Automatically disable privacy extensions
51 ipv6_device_privacy_extensions_disable
"${device}"
54 ipv6_device_forwarding_enable
() {
69 sysctl_set
"net.ipv6.conf.${device}.forwarding" 1
71 log INFO
"Enabled IPv6 forwarding on '${device}'"
73 # If forwarding is enabled, the kernel won't process
74 # any router advertisements any more, which is not good
75 # when we still want a default route when running in
76 # DHCP client mode on an uplink zone.
77 if [ ${accept_ra} -gt 0 ]; then
78 log INFO
" and accepting router advertisements"
80 sysctl_set
"net.ipv6.conf.${device}.accept_ra" 2
84 ipv6_device_forwarding_disable
() {
87 sysctl_set
"net.ipv6.conf.${device}.forwarding" 0
89 log INFO
"Disabled IPv6 forwarding on '${device}'"
92 # Enable IPv6 RFC3041 privacy extensions if desired
93 ipv6_device_privacy_extensions_enable
() {
95 assert device_exists
"${device}"
97 sysctl_set
"net.ipv6.conf.${device}.use_tempaddr" 2
100 ipv6_device_privacy_extensions_disable
() {
102 assert device_exists
"${device}"
104 sysctl_set
"net.ipv6.conf.${device}.use_tempaddr" 0
108 ipcalc
--ipv6 -c $@
>/dev
/null
2>&1
120 ipv6_prefix_is_valid
() {
124 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
125 [ ${prefix} -gt 128 ] && return ${EXIT_FALSE}
134 ipv6_split_prefix
() {
143 assert device_exists
"${device}"
147 local preferred_lft valid_lft
149 # Enable to wait until DAD has finished and return
150 # an error if it has failed
151 local wait_for_dad
="true"
156 --preferred-lifetime=*)
157 preferred_lft
="$(cli_get_val "${arg}")"
160 valid_lft
="$(cli_get_val "${arg}")"
166 done <<< "$(args $@)"
168 local cmd
="ip addr add ${address} dev ${device} scope ${scope}"
171 if isinteger preferred_lft
; then
172 list_append cmd
"preferred_lft ${preferred_lft}"
176 if isinteger valid_lft
; then
177 list_append cmd
"valid_lft ${valid_lft}"
180 cmd_quiet
"${cmd}" ||
return ${EXIT_ERROR}
182 if enabled wait_for_dad
; then
183 log DEBUG
"Waiting for DAD to complete..."
185 ipv6_wait_for_dad
"${address}" "${device}"
191 log DEBUG
"DAD successfully completed"
197 log ERROR
"DAD failed"
199 # Remove the IP address again
200 ipv6_address_del
"${address}" "${device}"
207 log ERROR
"DAD failed with unhandled error: ${ret}"
220 ip_address_del
"${device}" "${address}"
223 ipv6_address_flush
() {
227 log DEBUG
"Flushing all IPv6 addresses on ${device}"
229 # Remove any stale addresses from aborted clients
230 cmd_quiet ip
-6 addr flush dev
"${device}" scope global permanent
231 cmd_quiet ip
-6 addr flush dev
"${device}" scope global dynamic
234 ipv6_address_change_lifetime
() {
239 assert device_exists
"${device}"
248 --preferred-lifetime=*)
249 preferred_lft
="$(cli_get_val "${arg}")"
252 valid_lft
="$(cli_get_val "${arg}")"
255 done <<< "$(args $@)"
257 local cmd
="ip -6 addr change ${address} dev ${device} scope global"
259 if isinteger preferred_lft
; then
260 list_append cmd
"preferred_lft" "${preferred_lft}"
263 if isinteger valid_lft
; then
264 list_append cmd
"valid_lft" "${valid_lft}"
267 if ! cmd_quiet
"${cmd}"; then
268 log ERROR
"Could not change lifetimes of ${address} (${device})"
272 log DEBUG
"Changed lifetimes of ${address} (${device}) to:"
273 if isset preferred_lft
; then
274 log DEBUG
" preferred: ${preferred_lft}"
277 if isset valid_lft
; then
278 log DEBUG
" valid: ${valid_lft}"
284 ipv6_get_dad_status
() {
291 # Strip prefix from address
292 address
="$(ipv6_split_prefix "${address}")"
294 local output
="$(ip -o addr show dev "${device}" to "${address}")"
295 if ! isset output
; then
299 # Abort if DAD failed
300 if [[ ${output} =~
"dadfailed" ]]; then
301 return ${EXIT_DAD_FAILED}
304 # Wait a little more if DAD is still in progress
305 if [[ ${output} =~
"tentative" ]]; then
306 return ${EXIT_DAD_TENTATIVE}
309 # DAD has successfully completed
310 return ${EXIT_DAD_OK}
313 ipv6_wait_for_dad
() {
320 # Strip prefix from address
321 address
="$(ipv6_split_prefix "${address}")"
326 ipv6_get_dad_status
"${address}" "${interface}"
330 # DAD is still in progress. Give it a moment to settle...
331 ${EXIT_DAD_TENTATIVE})
336 # Raise all other error codes
337 ${EXIT_DAD_OK}|
${EXIT_DAD_FAILED}|
*)
346 ipv6_device_get_addresses
() {
357 scope
="$(cli_get_val "${arg}")"
360 done <<< "$(args $@)"
362 local cmd
="ip -o addr show dev ${device}"
364 assert isoneof scope global dynamic link
365 list_append cmd
"scope ${scope}"
374 for (( i
=0; i
< ${#args[@]} - 1; i
++ )); do
375 if [ "${args[${i}]}" = "inet6" ]; then
376 list_append_one addresses
"${args[$(( ${i} + 1 ))]}"
382 list_sort
${addresses}
390 eval $
(ipcalc
-6 -i ${address} 2>/dev
/null
)
391 assert isset ADDRESS6_IMPL
393 print
"${ADDRESS6_IMPL}"
400 # Nothing to do if the length of the address is 39.
401 if [ ${#address} -eq 39 ]; then
407 eval $
(ipcalc
-6 -e ${address} 2>/dev
/null
)
408 assert isset ADDRESS6_EXPL
410 print
"${ADDRESS6_EXPL}"
421 for addr
in addr1 addr2
; do
422 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
425 [[ "${addr1}" = "${addr2}" ]] \
426 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
437 for addr
in addr1 addr2
; do
438 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
441 local i addr1_oct addr2_oct
442 for i
in 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30; do
443 addr1_oct
="0x${addr1:${i}:2}"
444 addr2_oct
="0x${addr2:${i}:2}"
446 [[ ${addr1_oct} -gt ${addr2_oct} ]] && return ${EXIT_TRUE}
458 address
=$
(ipv6_explode
${address})
460 echo "${address//:/}"
467 # Check if a prefix (e.g. /64) is provided.
468 local prefix
=$
(ip_get_prefix
${addr})
469 assert ipv6_prefix_is_valid
${prefix}
472 eval $
(ipcalc
--ipv6 -p ${addr})
475 print
"${PREFIX6}/${prefix}"
478 ipv6_6rd_format_address
() {
479 local isp_prefix
="${1}"
480 assert ipv6_is_valid
"${isp_prefix}"
482 local client_address
="${2}"
483 assert ipv4_is_valid
"${client_address}"
485 local prefix
="$(ipv6_get_prefix "${isp_prefix}")"
486 isp_prefix
="$(ipv6_split_prefix "${isp_prefix}")"
488 # This only works for prefix lengths up to 32 bit.
489 assert
[ "${prefix}" -le 32 ]
490 assert
[ "${prefix}" -gt 0 ]
492 # Explode the address and throw away the second 32 bit.
493 local address
="$(ipv6_explode "${isp_prefix}")"
495 client_address
="$(ipv6_6rd_format_client_address ${client_address})"
496 assert isset client_address
498 local block1
="0x${address:0:4}"
499 local block2
="0x${address:5:4}"
500 local block3
="0x${address:10:4}"
501 local block4
="0x${address:15:4}"
503 address
="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))"
504 assert
[ "${address}" -gt 0 ]
506 block1
="0x${client_address:0:4}"
507 block2
="0x${client_address:5:4}"
509 client_address
="$(( (${block1} << 48) + (${block2} << 32) ))"
511 # Fix for numbers that are interpreted by bash as negative
512 # numbers and therefore filled up with ones when shifted to
514 if [ "${client_address}" -gt 0 ]; then
515 client_address
="$(( ${client_address} >> ${prefix} ))"
517 local bitmask
="$(( 1 << 63 ))"
518 client_address
="$(( ${client_address} >> 1 ))"
519 client_address
="$(( ${client_address} ^ ${bitmask} ))"
520 client_address
="$(( ${client_address} >> $(( ${prefix} - 1 )) ))"
522 assert
[ "${client_address}" -gt 0 ]
524 # XOR everything together
525 address
="$(( ${address} ^ ${client_address} ))"
526 prefix
="$(( ${prefix} + 32 ))"
528 local block formatted_address
=":"
529 while [ ${address} -gt 0 ]; do
530 printf -v block
"%x" "$(( ${address} & 0xffff ))"
531 formatted_address
="${block}:${formatted_address}"
533 address
="$(( ${address} >> 16 ))"
536 assert ipv6_is_valid
"${formatted_address}"
538 # Implode the output IP address.
539 formatted_address
="$(ipv6_implode "${formatted_address}")"
541 print
"${formatted_address}/${prefix}"
544 ipv6_6rd_format_client_address
() {
548 print
"%02x%02x:%02x%02x" ${address//\./ }