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}
130 ipv6_prefix_size_is_valid_for_delegation
() {
131 local prefix_size
="${1}"
132 assert isinteger prefix_size
134 # For prefix delegation, the prefix must be between /48 and /64
135 # (RFC3769, section 3.1)
136 [[ ${prefix_size} -lt 48 ]] && return ${EXIT_FALSE}
137 [[ ${prefix_size} -gt 64 ]] && return ${EXIT_FALSE}
146 ipv6_split_prefix
() {
155 assert device_exists
"${device}"
159 local preferred_lft valid_lft
161 # Enable to wait until DAD has finished and return
162 # an error if it has failed
163 local wait_for_dad
="true"
168 --preferred-lifetime=*)
169 preferred_lft
="$(cli_get_val "${arg}")"
172 valid_lft
="$(cli_get_val "${arg}")"
178 done <<< "$(args $@)"
180 local cmd
="ip addr add ${address} dev ${device} scope ${scope}"
183 if isinteger preferred_lft
; then
184 list_append cmd
"preferred_lft ${preferred_lft}"
188 if isinteger valid_lft
; then
189 list_append cmd
"valid_lft ${valid_lft}"
192 cmd_quiet
"${cmd}" ||
return ${EXIT_ERROR}
194 if enabled wait_for_dad
; then
195 log DEBUG
"Waiting for DAD to complete..."
197 ipv6_wait_for_dad
"${address}" "${device}"
203 log DEBUG
"DAD successfully completed"
209 log ERROR
"DAD failed"
211 # Remove the IP address again
212 ipv6_address_del
"${address}" "${device}"
219 log ERROR
"DAD failed with unhandled error: ${ret}"
232 ip_address_del
"${device}" "${address}"
235 ipv6_address_flush
() {
239 log DEBUG
"Flushing all IPv6 addresses on ${device}"
241 # Remove any stale addresses from aborted clients
242 cmd_quiet ip
-6 addr flush dev
"${device}" scope global permanent
243 cmd_quiet ip
-6 addr flush dev
"${device}" scope global dynamic
246 ipv6_address_change_lifetime
() {
251 assert device_exists
"${device}"
260 --preferred-lifetime=*)
261 preferred_lft
="$(cli_get_val "${arg}")"
264 valid_lft
="$(cli_get_val "${arg}")"
267 done <<< "$(args $@)"
269 local cmd
="ip -6 addr change ${address} dev ${device} scope global"
271 if isinteger preferred_lft
; then
272 list_append cmd
"preferred_lft" "${preferred_lft}"
275 if isinteger valid_lft
; then
276 list_append cmd
"valid_lft" "${valid_lft}"
279 if ! cmd_quiet
"${cmd}"; then
280 log ERROR
"Could not change lifetimes of ${address} (${device})"
284 log DEBUG
"Changed lifetimes of ${address} (${device}) to:"
285 if isset preferred_lft
; then
286 log DEBUG
" preferred: ${preferred_lft}"
289 if isset valid_lft
; then
290 log DEBUG
" valid: ${valid_lft}"
296 ipv6_get_dad_status
() {
303 # Strip prefix from address
304 address
="$(ipv6_split_prefix "${address}")"
306 local output
="$(ip -o addr show dev "${device}" to "${address}")"
307 if ! isset output
; then
311 # Abort if DAD failed
312 if [[ ${output} =~
"dadfailed" ]]; then
313 return ${EXIT_DAD_FAILED}
316 # Wait a little more if DAD is still in progress
317 if [[ ${output} =~
"tentative" ]]; then
318 return ${EXIT_DAD_TENTATIVE}
321 # DAD has successfully completed
322 return ${EXIT_DAD_OK}
325 ipv6_wait_for_dad
() {
332 # Strip prefix from address
333 address
="$(ipv6_split_prefix "${address}")"
338 ipv6_get_dad_status
"${address}" "${interface}"
342 # DAD is still in progress. Give it a moment to settle...
343 ${EXIT_DAD_TENTATIVE})
348 # Raise all other error codes
349 ${EXIT_DAD_OK}|
${EXIT_DAD_FAILED}|
*)
358 ipv6_device_get_addresses
() {
369 scope
="$(cli_get_val "${arg}")"
372 done <<< "$(args $@)"
374 local cmd
="ip -o addr show dev ${device}"
376 assert isoneof scope global dynamic link
377 list_append cmd
"scope ${scope}"
386 for (( i
=0; i
< ${#args[@]} - 1; i
++ )); do
387 if [ "${args[${i}]}" = "inet6" ]; then
388 list_append_one addresses
"${args[$(( ${i} + 1 ))]}"
394 list_sort
${addresses}
402 eval $
(ipcalc
-6 -i ${address} 2>/dev
/null
)
403 assert isset ADDRESS6_IMPL
405 print
"${ADDRESS6_IMPL}"
412 # Nothing to do if the length of the address is 39.
413 if [ ${#address} -eq 39 ]; then
419 eval $
(ipcalc
-6 -e ${address} 2>/dev
/null
)
420 assert isset ADDRESS6_EXPL
422 print
"${ADDRESS6_EXPL}"
433 for addr
in addr1 addr2
; do
434 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
437 [[ "${addr1}" = "${addr2}" ]] \
438 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
449 for addr
in addr1 addr2
; do
450 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
453 printf -v ${addr} "${!addr//:/}"
456 local i addr1_oct addr2_oct
457 for i
in 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30; do
458 addr1_oct
="0x${addr1:${i}:2}"
459 addr2_oct
="0x${addr2:${i}:2}"
461 [[ ${addr1_oct} -gt ${addr2_oct} ]] && return ${EXIT_TRUE}
473 address
=$
(ipv6_explode
${address})
475 echo "${address//:/}"
482 # Check if a prefix (e.g. /64) is provided.
483 local prefix
=$
(ip_get_prefix
${addr})
484 assert ipv6_prefix_is_valid
${prefix}
487 eval $
(ipcalc
--ipv6 -p ${addr})
490 print
"${PREFIX6}/${prefix}"
493 ipv6_6rd_format_address
() {
494 local isp_prefix
="${1}"
495 assert ipv6_is_valid
"${isp_prefix}"
497 local client_address
="${2}"
498 assert ipv4_is_valid
"${client_address}"
500 local prefix
="$(ipv6_get_prefix "${isp_prefix}")"
501 isp_prefix
="$(ipv6_split_prefix "${isp_prefix}")"
503 # This only works for prefix lengths up to 32 bit.
504 assert
[ "${prefix}" -le 32 ]
505 assert
[ "${prefix}" -gt 0 ]
507 # Explode the address and throw away the second 32 bit.
508 local address
="$(ipv6_explode "${isp_prefix}")"
510 client_address
="$(ipv6_6rd_format_client_address ${client_address})"
511 assert isset client_address
513 local block1
="0x${address:0:4}"
514 local block2
="0x${address:5:4}"
515 local block3
="0x${address:10:4}"
516 local block4
="0x${address:15:4}"
518 address
="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))"
519 assert
[ "${address}" -gt 0 ]
521 block1
="0x${client_address:0:4}"
522 block2
="0x${client_address:5:4}"
524 client_address
="$(( (${block1} << 48) + (${block2} << 32) ))"
526 # Fix for numbers that are interpreted by bash as negative
527 # numbers and therefore filled up with ones when shifted to
529 if [ "${client_address}" -gt 0 ]; then
530 client_address
="$(( ${client_address} >> ${prefix} ))"
532 local bitmask
="$(( 1 << 63 ))"
533 client_address
="$(( ${client_address} >> 1 ))"
534 client_address
="$(( ${client_address} ^ ${bitmask} ))"
535 client_address
="$(( ${client_address} >> $(( ${prefix} - 1 )) ))"
537 assert
[ "${client_address}" -gt 0 ]
539 # XOR everything together
540 address
="$(( ${address} ^ ${client_address} ))"
541 prefix
="$(( ${prefix} + 32 ))"
543 local block formatted_address
=":"
544 while [ ${address} -gt 0 ]; do
545 printf -v block
"%x" "$(( ${address} & 0xffff ))"
546 formatted_address
="${block}:${formatted_address}"
548 address
="$(( ${address} >> 16 ))"
551 assert ipv6_is_valid
"${formatted_address}"
553 # Implode the output IP address.
554 formatted_address
="$(ipv6_implode "${formatted_address}")"
556 print
"${formatted_address}/${prefix}"
559 ipv6_6rd_format_client_address
() {
563 print
"%02x%02x:%02x%02x" ${address//\./ }