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
() {
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}
407 inetcalc
-6 -e "${addr1}" "${addr2}" \
408 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
417 inetcalc
-6 -g "${addr1}" "${addr2}" \
418 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
425 address
="$(ipv6_format "${address}")"
426 echo "${address//:/}"
433 ipv6_6rd_format_address
() {
434 local isp_prefix
="${1}"
435 assert ipv6_net_is_valid
"${isp_prefix}"
437 local client_address
="${2}"
438 assert ipv4_is_valid
"${client_address}"
440 local prefix
="$(ipv6_get_prefix "${isp_prefix}")"
441 isp_prefix
="$(ipv6_split_prefix "${isp_prefix}")"
443 # This only works for prefix lengths up to 32 bit.
444 assert
[ "${prefix}" -le 32 ]
445 assert
[ "${prefix}" -gt 0 ]
447 # Explode the address and throw away the second 32 bit.
450 for segment
in ${isp_prefix//:/ }; do
451 while [[ ${#segment} -lt 4 ]]; do
452 segment
="0${segment}"
454 list_append address
"${segment}"
456 address
="$(list_join ":" ${address})"
458 client_address
="$(ipv6_6rd_format_client_address ${client_address})"
459 assert isset client_address
461 local block1
="0x${address:0:4}"
462 local block2
="0x${address:5:4}"
463 local block3
="0x${address:10:4}"
464 local block4
="0x${address:15:4}"
466 address
="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))"
467 assert
[ "${address}" -gt 0 ]
469 block1
="0x${client_address:0:4}"
470 block2
="0x${client_address:5:4}"
472 client_address
="$(( (${block1} << 48) + (${block2} << 32) ))"
474 # Fix for numbers that are interpreted by bash as negative
475 # numbers and therefore filled up with ones when shifted to
477 if [ "${client_address}" -gt 0 ]; then
478 client_address
="$(( ${client_address} >> ${prefix} ))"
480 local bitmask
="$(( 1 << 63 ))"
481 client_address
="$(( ${client_address} >> 1 ))"
482 client_address
="$(( ${client_address} ^ ${bitmask} ))"
483 client_address
="$(( ${client_address} >> $(( ${prefix} - 1 )) ))"
485 assert
[ "${client_address}" -gt 0 ]
487 # XOR everything together
488 address
="$(( ${address} ^ ${client_address} ))"
489 prefix
="$(( ${prefix} + 32 ))"
491 local block formatted_address
=":"
492 while [ ${address} -gt 0 ]; do
493 printf -v block
"%x" "$(( ${address} & 0xffff ))"
494 formatted_address
="${block}:${formatted_address}"
496 address
="$(( ${address} >> 16 ))"
499 assert ipv6_is_valid
"${formatted_address}"
501 # Implode the output IP address.
502 formatted_address
="$(ipv6_format "${formatted_address}")"
504 print
"${formatted_address}/${prefix}"
507 ipv6_6rd_format_client_address
() {
511 print
"%02x%02x:%02x%02x" ${address//\./ }