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_init
() {
25 log INFO
"Initializing IPv6 networking."
27 # Enable forwarding on all devices
28 #ipv6_device_forwarding_disable all
29 #ipv6_device_forwarding_disable default
31 # Disable autoconfiguration on all devices per default
32 #ipv6_device_autoconf_disable all
33 #ipv6_device_autoconf_disable default
35 # XXX do we need this?
37 #for device in $(devices_get_all); do
38 # ipv6_device_forwarding_disable ${device}
39 # ipv6_device_autoconf_disable ${device}
43 init_register ipv6_init
45 function ipv6_device_autoconf_enable
() {
50 # Allow setting default and all settings
51 if ! isoneof device all default
; then
52 assert device_exists
${device}
56 for val
in accept_ra accept_redirects
; do
57 echo 1 > /proc
/sys
/net
/ipv
6/conf
/${device}/${val}
61 function ipv6_device_autoconf_disable
() {
66 # Allow setting default and all settings
67 if ! isoneof device all default
; then
68 assert device_exists
${device}
72 for val
in accept_ra accept_redirects
; do
73 echo 0 > /proc
/sys
/net
/ipv
6/conf
/${device}/${val}
77 function ipv6_device_forwarding_enable
() {
82 # Allow setting default and all settings
83 if ! isoneof device all default
; then
84 assert device_exists
${device}
87 echo 1 > /proc
/sys
/net
/ipv
6/conf
/${device}/forwarding
90 function ipv6_device_forwarding_disable
() {
95 # Allow setting default and all settings
96 if ! isoneof device all default
; then
97 assert device_exists
${device}
100 echo 0 > /proc
/sys
/net
/ipv
6/conf
/${device}/forwarding
103 # Enable IPv6 RFC3041 privacy extensions if desired
104 function ipv6_device_privacy_extensions_enable
() {
109 assert device_exists
${device}
111 # Default value is rfc3041
112 if [ -z "${type}" ]; then
120 echo 2 > /proc
/sys
/net
/ipv
6/conf
/${device}/use_tempaddr
123 error_log
"Given type '${type}' is not supported."
131 function ipv6_device_privacy_extensions_disable
() {
135 assert device_exists
${device}
137 echo 0 > /proc
/sys
/net
/ipv
6/conf
/${device}/use_tempaddr
140 function ipv6_is_valid
() {
141 ipcalc
--ipv6 -c $@
>/dev
/null
2>&1
153 function ipv6_prefix_is_valid
() {
157 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
158 [ ${prefix} -gt 128 ] && return ${EXIT_FALSE}
163 function ipv6_implode
() {
168 if ! ipv6_is_valid
${address}; then
169 error
"IPv6 address is invalid: ${address}"
174 local prefix
=$
(ip_get_prefix
${address})
175 address
=$
(ip_split_prefix
${address})
177 # Make proper address in exploded format
178 address
=$
(ipv6_explode
${address})
187 for block
in ${address//:/\ }; do
189 for i
in $
(seq 0 ${#block}); do
190 char
="${block:${i}:1}"
192 [ -z "${char}" ] && continue
194 if [ -z "${block_new}" ] && [ "${char}" = "0" ]; then
198 block_new
="${block_new}${char}"
201 [ -z "${block_new}" ] && block_new
="0"
203 address_new
="${address_new}:${block_new}"
206 # Cut first colon (:)
207 address
="${address_new:1:${#address_new}}"
214 for pos_start
in $
(seq 0 ${#address}); do
215 matches
["${pos_start}"]=0
217 for pos_next
in $
(seq ${pos_start} 2 ${#address}); do
218 case "${pos_start}" in
220 match
="${address:${pos_next}:2}"
224 match
="${address:${pos_next}:2}"
229 [ -z "${match}" ] && continue
231 if [ "${match}" = "${pattern}" ]; then
232 matches
[${pos_start}]=$
(( matches
[${pos_start}] + 1))
241 for i
in $
(seq 0 ${#matches[@]}); do
242 [ -z "${matches[${i}]}" ] && continue
244 if [ ${matches[${i}]} -gt ${pos_best_val} ]; then
246 pos_best_val
=${matches[${i}]}
250 if [ -n "${pos_best}" ]; then
251 address_new
="${address:0:${pos_best}}::"
253 local pos_end
=$
(( ${pos_best_val} * 2 + ${pos_best} + 1))
255 if [ "${pos_best}" = "0" ]; then
256 pos_end
=$
(( ${pos_end} - 1 ))
259 address
="${address_new}${address:${pos_end}:${#address}}"
262 # If a prefix was provided we append it in the end
263 [ -n "${prefix}" ] && address="${address}/${prefix}"
265 assert ipv6_is_valid
${address}
270 function ipv6_explode
() {
275 local prefix
=$
(ip_get_prefix
${address})
276 address
=$
(ip_split_prefix
${address})
278 if [ ${#address} -eq 39 ]; then
279 echo "${address}$([ -n "${prefix}" ] && echo "/${prefix}")"
283 address=${address//::/:X:}
291 for block in ${address//:/\ }; do
292 blocks[${block_count}]=${block}
294 block_count=$(( ${block_count} + 1 ))
297 if [ ${#blocks[@]} -lt ${block_max} ]; then
298 for block_id in $(seq ${#blocks[@]} -1 0); do
299 block=${blocks[${block_id}]}
301 [ -z "${block}" ] && continue
303 if [ "${block}" = "X
" ]; then
304 blocks[${block_id}]="0000"
308 blocks[$(( ${block_max} - ${block_count} + ${block_id} ))]=${block}
309 blocks[${block_id}]="0000"
313 for block_id in $(seq 0 ${#blocks[@]}); do
314 block=${blocks[${block_id}]}
316 [ -z "${block}" ] && block="0000"
318 while [ "${#block}" -lt 4 ]; do
322 blocks[${block_id}]=${block}
326 for block in ${blocks[@]}; do
327 address="${address}:${block}"
329 address=${address:1:39}
331 # If a prefix was provided we append it in the end again
332 [ -n "${prefix}" ] && address="${address}/${prefix}"
334 assert ipv6_is_valid ${address}
339 function ipv6_hash() {
345 address=$(ipv6_explode ${address})
347 echo "${address//:/}"