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 function 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
32 function ipv6_device_autoconf_disable
() {
34 assert device_exists
"${device}"
36 sysctl_set
"net.ipv6.conf.${device}.accept_ra" 0
37 sysctl_set
"net.ipv6.conf.${device}.autoconf" 0
40 # Enable IPv6 RFC3041 privacy extensions if desired
41 function ipv6_device_privacy_extensions_enable
() {
43 assert device_exists
"${device}"
45 sysctl_set
"net.ipv6.conf.${device}.use_tempaddr" 2
48 function ipv6_device_privacy_extensions_disable
() {
50 assert device_exists
"${device}"
52 sysctl_set
"net.ipv6.conf.${device}.use_tempaddr" 0
55 function ipv6_is_valid
() {
56 ipcalc
--ipv6 -c $@
>/dev
/null
2>&1
68 function ipv6_prefix_is_valid
() {
72 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
73 [ ${prefix} -gt 128 ] && return ${EXIT_FALSE}
78 function ipv6_get_prefix
() {
82 function ipv6_split_prefix
() {
86 function ipv6_address_add
() {
91 assert device_exists
"${device}"
95 local preferred_lft valid_lft
97 # Enable to wait until DAD has finished and return
98 # an error if it has failed
99 local wait_for_dad
="true"
104 --preferred-lifetime=*)
105 preferred_lft
="$(cli_get_val "${arg}")"
108 valid_lft
="$(cli_get_val "${arg}")"
114 done <<< "$(args $@)"
116 local cmd
="ip addr add ${address} dev ${device} scope ${scope}"
119 if isinteger preferred_lft
; then
120 list_append cmd
"preferred_lft ${preferred_lft}"
124 if isinteger valid_lft
; then
125 list_append cmd
"valid_lft ${valid_lft}"
128 cmd_quiet
"${cmd}" ||
return ${EXIT_ERROR}
130 if enabled wait_for_dad
; then
131 log DEBUG
"Waiting for DAD to complete..."
133 ipv6_wait_for_dad
"${address}" "${device}"
139 log DEBUG
"DAD successfully completed"
145 log ERROR
"DAD failed"
147 # Remove the IP address again
148 ipv6_address_del
"${address}" "${device}"
155 log ERROR
"DAD failed with unhandled error: ${ret}"
164 function ipv6_address_del
() {
168 ip_address_del
"${device}" "${address}"
171 function ipv6_address_flush
() {
175 log DEBUG
"Flushing all IPv6 addresses on ${device}"
177 # Remove any stale addresses from aborted clients
178 cmd_quiet ip
-6 addr flush dev
"${device}" scope global permanent
181 function ipv6_address_change_lifetime
() {
186 assert device_exists
"${device}"
195 --preferred-lifetime=*)
196 preferred_lft
="$(cli_get_val "${arg}")"
199 valid_lft
="$(cli_get_val "${arg}")"
202 done <<< "$(args $@)"
204 local cmd
="ip -6 addr change ${address} dev ${device} scope global"
206 if isinteger preferred_lft
; then
207 list_append cmd
"preferred_lft" "${preferred_lft}"
210 if isinteger valid_lft
; then
211 list_append cmd
"valid_lft" "${valid_lft}"
214 if ! cmd_quiet
"${cmd}"; then
215 log ERROR
"Could not change lifetimes of ${address} (${device})"
219 log DEBUG
"Changed lifetimes of ${address} (${device}) to:"
220 if isset preferred_lft
; then
221 log DEBUG
" preferred: ${preferred_lft}"
224 if isset valid_lft
; then
225 log DEBUG
" valid: ${valid_lft}"
231 function ipv6_get_dad_status
() {
238 # Strip prefix from address
239 address
="$(ipv6_split_prefix "${address}")"
241 local output
="$(ip -o addr show dev "${device}" to "${address}")"
242 if ! isset output
; then
246 # Abort if DAD failed
247 if [[ ${output} =~
"dadfailed" ]]; then
248 return ${EXIT_DAD_FAILED}
251 # Wait a little more if DAD is still in progress
252 if [[ ${output} =~
"tentative" ]]; then
253 return ${EXIT_DAD_TENTATIVE}
256 # DAD has successfully completed
257 return ${EXIT_DAD_OK}
260 function ipv6_wait_for_dad
() {
267 # Strip prefix from address
268 address
="$(ipv6_split_prefix "${address}")"
273 ipv6_get_dad_status
"${address}" "${interface}"
277 # DAD is still in progress. Give it a moment to settle...
278 ${EXIT_DAD_TENTATIVE})
283 # Raise all other error codes
284 ${EXIT_DAD_OK}|
${EXIT_DAD_FAILED}|
*)
293 function ipv6_device_get_addresses
() {
304 scope
="$(cli_get_val "${arg}")"
307 done <<< "$(args $@)"
309 local cmd
="ip -o addr show dev ${device}"
311 assert isoneof scope global dynamic link
312 list_append cmd
"scope ${scope}"
321 for (( i
=0; i
< ${#args[@]} - 1; i
++ )); do
322 if [ "${args[${i}]}" = "inet6" ]; then
323 list_append_one addresses
"${args[$(( ${i} + 1 ))]}"
329 list_sort
${addresses}
332 function ipv6_implode
() {
337 eval $
(ipcalc
-6 -i ${address} 2>/dev
/null
)
338 assert isset ADDRESS6_IMPL
340 print
"${ADDRESS6_IMPL}"
343 function ipv6_explode
() {
347 # Nothing to do if the length of the address is 39.
348 if [ ${#address} -eq 39 ]; then
354 eval $
(ipcalc
-6 -e ${address} 2>/dev
/null
)
355 assert isset ADDRESS6_EXPL
357 print
"${ADDRESS6_EXPL}"
360 function ipv6_addr_eq
() {
368 for addr
in addr1 addr2
; do
369 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
372 [[ "${addr1}" = "${addr2}" ]] \
373 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
376 function ipv6_addr_gt
() {
384 for addr
in addr1 addr2
; do
385 printf -v ${addr} "%s" $
(ipv6_explode
${!addr})
388 local i addr1_oct addr2_oct
389 for i
in 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30; do
390 addr1_oct
="0x${addr1:${i}:2}"
391 addr2_oct
="0x${addr2:${i}:2}"
393 [[ ${addr1_oct} -gt ${addr2_oct} ]] && return ${EXIT_TRUE}
399 function ipv6_hash
() {
405 address
=$
(ipv6_explode
${address})
407 echo "${address//:/}"
410 function ipv6_get_network
() {
414 # Check if a prefix (e.g. /64) is provided.
415 local prefix
=$
(ip_get_prefix
${addr})
416 assert ipv6_prefix_is_valid
${prefix}
419 eval $
(ipcalc
--ipv6 -p ${addr})
422 print
"${PREFIX6}/${prefix}"
425 function ipv6_6rd_format_address
() {
426 local isp_prefix
="${1}"
427 assert ipv6_is_valid
"${isp_prefix}"
429 local client_address
="${2}"
430 assert ipv4_is_valid
"${client_address}"
432 local prefix
="$(ipv6_get_prefix "${isp_prefix}")"
433 isp_prefix
="$(ipv6_split_prefix "${isp_prefix}")"
435 # This only works for prefix lengths up to 32 bit.
436 assert
[ "${prefix}" -le 32 ]
437 assert
[ "${prefix}" -gt 0 ]
439 # Explode the address and throw away the second 32 bit.
440 local address
="$(ipv6_explode "${isp_prefix}")"
442 client_address
="$(ipv6_6rd_format_client_address ${client_address})"
443 assert isset client_address
445 local block1
="0x${address:0:4}"
446 local block2
="0x${address:5:4}"
447 local block3
="0x${address:10:4}"
448 local block4
="0x${address:15:4}"
450 address
="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))"
451 assert
[ "${address}" -gt 0 ]
453 block1
="0x${client_address:0:4}"
454 block2
="0x${client_address:5:4}"
456 client_address
="$(( (${block1} << 48) + (${block2} << 32) ))"
458 # Fix for numbers that are interpreted by bash as negative
459 # numbers and therefore filled up with ones when shifted to
461 if [ "${client_address}" -gt 0 ]; then
462 client_address
="$(( ${client_address} >> ${prefix} ))"
464 local bitmask
="$(( 1 << 63 ))"
465 client_address
="$(( ${client_address} >> 1 ))"
466 client_address
="$(( ${client_address} ^ ${bitmask} ))"
467 client_address
="$(( ${client_address} >> $(( ${prefix} - 1 )) ))"
469 assert
[ "${client_address}" -gt 0 ]
471 # XOR everything together
472 address
="$(( ${address} ^ ${client_address} ))"
473 prefix
="$(( ${prefix} + 32 ))"
475 local block formatted_address
=":"
476 while [ ${address} -gt 0 ]; do
477 printf -v block
"%x" "$(( ${address} & 0xffff ))"
478 formatted_address
="${block}:${formatted_address}"
480 address
="$(( ${address} >> 16 ))"
483 assert ipv6_is_valid
"${formatted_address}"
485 # Implode the output IP address.
486 formatted_address
="$(ipv6_implode "${formatted_address}")"
488 print
"${formatted_address}/${prefix}"
491 function ipv6_6rd_format_client_address
() {
495 print
"%02x%02x:%02x%02x" ${address//\./ }