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 inetcalc
-6 -c $@
&& return ${EXIT_OK} ||
return ${EXIT_ERROR}
111 ipv6_net_is_valid
() {
114 local prefix
="$(ip_get_prefix "${net}")"
115 local addr
="$(ip_split_prefix "${net}")"
117 ipv6_prefix_is_valid
"${prefix}" && ipv6_is_valid
"${addr}"
120 ipv6_prefix_is_valid
() {
123 # Check if prefix is a number
124 isinteger prefix ||
return ${EXIT_FALSE}
126 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
127 [ ${prefix} -gt 128 ] && return ${EXIT_FALSE}
132 ipv6_prefix_size_is_valid_for_delegation
() {
133 local prefix_size
="${1}"
134 assert isinteger prefix_size
136 # For prefix delegation, the prefix must be between /48 and /64
137 # (RFC3769, section 3.1)
138 [[ ${prefix_size} -lt 48 ]] && return ${EXIT_FALSE}
139 [[ ${prefix_size} -gt 64 ]] && return ${EXIT_FALSE}
148 ipv6_split_prefix
() {
157 assert device_exists
"${device}"
161 local preferred_lft valid_lft
163 # Enable to wait until DAD has finished and return
164 # an error if it has failed
165 local wait_for_dad
="true"
170 --preferred-lifetime=*)
171 preferred_lft
="$(cli_get_val "${arg}")"
174 valid_lft
="$(cli_get_val "${arg}")"
180 done <<< "$(args $@)"
182 local cmd
="ip addr add ${address} dev ${device} scope ${scope}"
185 if isinteger preferred_lft
; then
186 list_append cmd
"preferred_lft ${preferred_lft}"
190 if isinteger valid_lft
; then
191 list_append cmd
"valid_lft ${valid_lft}"
194 cmd_quiet
"${cmd}" ||
return ${EXIT_ERROR}
196 if enabled wait_for_dad
; then
197 log DEBUG
"Waiting for DAD to complete..."
199 ipv6_wait_for_dad
"${address}" "${device}"
205 log DEBUG
"DAD successfully completed"
211 log ERROR
"DAD failed"
213 # Remove the IP address again
214 ipv6_address_del
"${address}" "${device}"
221 log ERROR
"DAD failed with unhandled error: ${ret}"
234 ip_address_del
"${device}" "${address}"
237 ipv6_address_flush
() {
241 log DEBUG
"Flushing all IPv6 addresses on ${device}"
243 # Remove any stale addresses from aborted clients
244 cmd_quiet ip
-6 addr flush dev
"${device}" scope global permanent
245 cmd_quiet ip
-6 addr flush dev
"${device}" scope global dynamic
248 ipv6_address_change_lifetime
() {
253 assert device_exists
"${device}"
262 --preferred-lifetime=*)
263 preferred_lft
="$(cli_get_val "${arg}")"
266 valid_lft
="$(cli_get_val "${arg}")"
269 done <<< "$(args $@)"
271 local cmd
="ip -6 addr change ${address} dev ${device} scope global"
273 if isinteger preferred_lft
; then
274 list_append cmd
"preferred_lft" "${preferred_lft}"
277 if isinteger valid_lft
; then
278 list_append cmd
"valid_lft" "${valid_lft}"
281 if ! cmd_quiet
"${cmd}"; then
282 log ERROR
"Could not change lifetimes of ${address} (${device})"
286 log DEBUG
"Changed lifetimes of ${address} (${device}) to:"
287 if isset preferred_lft
; then
288 log DEBUG
" preferred: ${preferred_lft}"
291 if isset valid_lft
; then
292 log DEBUG
" valid: ${valid_lft}"
298 ipv6_get_dad_status
() {
305 # Strip prefix from address
306 address
="$(ipv6_split_prefix "${address}")"
308 local output
="$(ip -o addr show dev "${device}" to "${address}")"
309 if ! isset output
; then
313 # Abort if DAD failed
314 if [[ ${output} =~
"dadfailed" ]]; then
315 return ${EXIT_DAD_FAILED}
318 # Wait a little more if DAD is still in progress
319 if [[ ${output} =~
"tentative" ]]; then
320 return ${EXIT_DAD_TENTATIVE}
323 # DAD has successfully completed
324 return ${EXIT_DAD_OK}
327 ipv6_wait_for_dad
() {
334 # Strip prefix from address
335 address
="$(ipv6_split_prefix "${address}")"
340 ipv6_get_dad_status
"${address}" "${interface}"
344 # DAD is still in progress. Give it a moment to settle...
345 ${EXIT_DAD_TENTATIVE})
350 # Raise all other error codes
351 ${EXIT_DAD_OK}|
${EXIT_DAD_FAILED}|
*)
360 ipv6_device_get_addresses
() {
371 scope
="$(cli_get_val "${arg}")"
374 done <<< "$(args $@)"
376 local cmd
="ip -o addr show dev ${device}"
378 assert isoneof scope global dynamic link
379 list_append cmd
"scope ${scope}"
388 for (( i
=0; i
< ${#args[@]} - 1; i
++ )); do
389 if [ "${args[${i}]}" = "inet6" ]; then
390 list_append_one addresses
"${args[$(( ${i} + 1 ))]}"
396 list_sort
${addresses}
409 inetcalc
-6 -e "${addr1}" "${addr2}" \
410 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
419 inetcalc
-6 -g "${addr1}" "${addr2}" \
420 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
424 ipv6_addr_eq $@ || ipv6_addr_gt $@
428 ! ipv6_addr_eq $@
&& ! ipv6_addr_gt $@
432 ipv6_addr_eq $@ ||
! ipv6_addr_gt $@
439 address
="$(ipv6_format "${address}")"
440 echo "${address//:/}"
447 ipv6_6rd_format_address
() {
448 local isp_prefix
="${1}"
449 assert ipv6_net_is_valid
"${isp_prefix}"
451 local client_address
="${2}"
452 assert ipv4_is_valid
"${client_address}"
454 local prefix
="$(ipv6_get_prefix "${isp_prefix}")"
455 isp_prefix
="$(ipv6_split_prefix "${isp_prefix}")"
457 # This only works for prefix lengths up to 32 bit.
458 assert
[ "${prefix}" -le 32 ]
459 assert
[ "${prefix}" -gt 0 ]
461 # Explode the address and throw away the second 32 bit.
464 for segment
in ${isp_prefix//:/ }; do
465 while [[ ${#segment} -lt 4 ]]; do
466 segment
="0${segment}"
468 list_append address
"${segment}"
470 address
="$(list_join ":" ${address})"
472 client_address
="$(ipv6_6rd_format_client_address ${client_address})"
473 assert isset client_address
475 local block1
="0x${address:0:4}"
476 local block2
="0x${address:5:4}"
477 local block3
="0x${address:10:4}"
478 local block4
="0x${address:15:4}"
480 address
="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))"
481 assert
[ "${address}" -gt 0 ]
483 block1
="0x${client_address:0:4}"
484 block2
="0x${client_address:5:4}"
486 client_address
="$(( (${block1} << 48) + (${block2} << 32) ))"
488 # Fix for numbers that are interpreted by bash as negative
489 # numbers and therefore filled up with ones when shifted to
491 if [ "${client_address}" -gt 0 ]; then
492 client_address
="$(( ${client_address} >> ${prefix} ))"
494 local bitmask
="$(( 1 << 63 ))"
495 client_address
="$(( ${client_address} >> 1 ))"
496 client_address
="$(( ${client_address} ^ ${bitmask} ))"
497 client_address
="$(( ${client_address} >> $(( ${prefix} - 1 )) ))"
499 assert
[ "${client_address}" -gt 0 ]
501 # XOR everything together
502 address
="$(( ${address} ^ ${client_address} ))"
503 prefix
="$(( ${prefix} + 32 ))"
505 local block formatted_address
=":"
506 while [ ${address} -gt 0 ]; do
507 printf -v block
"%x" "$(( ${address} & 0xffff ))"
508 formatted_address
="${block}:${formatted_address}"
510 address
="$(( ${address} >> 16 ))"
513 assert ipv6_is_valid
"${formatted_address}"
515 # Implode the output IP address.
516 formatted_address
="$(ipv6_format "${formatted_address}")"
518 print
"${formatted_address}/${prefix}"
521 ipv6_6rd_format_client_address
() {
525 print
"%02x%02x:%02x%02x" ${address//\./ }