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 function ipv6_device_autoconf_enable
() {
25 if ! device_exists
${device}; then
26 error
"Device '${device}' does not exist."
30 echo 1 > /proc
/sys
/net
/ipv
6/conf
/${device}/autoconf
33 function ipv6_device_autoconf_disable
() {
36 if ! device_exists
${device}; then
37 error
"Device '${device}' does not exist."
41 echo 0 > /proc
/sys
/net
/ipv
6/conf
/${device}/autoconf
44 function ipv6_is_valid
() {
48 [ ${#address} -gt 39 ] && return ${EXIT_ERROR}
51 # XXX check for documentation prefix?
53 # Check for bad characters
55 for char
in 0 1 2 3 4 5 6 7 8 9 a b c d e f
:; do
56 address
=${address//${char}/}
58 [ -n "${address}" ] && return ${EXIT_ERROR}
63 function ipv6_implode
() {
66 if ! ipv6_is_valid
${address}; then
67 error
"IPv6 address is invalid: ${address}"
71 # Make proper address in exploded format
72 address
=$
(ipv6_explode
${address})
81 for block
in ${address//:/\ }; do
83 for i
in $
(seq 0 ${#block}); do
84 char
="${block:${i}:1}"
86 [ -z "${char}" ] && continue
88 if [ -z "${block_new}" ] && [ "${char}" = "0" ]; then
92 block_new
="${block_new}${char}"
95 [ -z "${block_new}" ] && block_new
="0"
97 address_new
="${address_new}:${block_new}"
100 # Cut first colon (:)
101 address
="${address_new:1:${#address_new}}"
108 for pos_start
in $
(seq 0 ${#address}); do
109 matches
["${pos_start}"]=0
111 for pos_next
in $
(seq ${pos_start} 2 ${#address}); do
112 case "${pos_start}" in
114 match
="${address:${pos_next}:2}"
118 match
="${address:${pos_next}:2}"
123 [ -z "${match}" ] && continue
125 if [ "${match}" = "${pattern}" ]; then
126 matches
[${pos_start}]=$
(( matches
[${pos_start}] + 1))
135 for i
in $
(seq 0 ${#matches[@]}); do
136 [ -z "${matches[${i}]}" ] && continue
138 if [ ${matches[${i}]} -gt ${pos_best_val} ]; then
140 pos_best_val
=${matches[${i}]}
144 if [ -n "${pos_best}" ]; then
145 address_new
="${address:0:${pos_best}}::"
147 local pos_end
=$
(( ${pos_best_val} * 2 + ${pos_best} + 1))
149 if [ "${pos_best}" = "0" ]; then
150 pos_end
=$
(( ${pos_end} - 1 ))
153 address
="${address_new}${address:${pos_end}:${#address}}"
156 assert ipv6_is_valid
${address}
161 function ipv6_explode
() {
164 if [ ${#address} -eq 39 ]; then
169 address
=${address//::/:X:}
177 for block
in ${address//:/\ }; do
178 blocks
[${block_count}]=${block}
180 block_count
=$
(( ${block_count} + 1 ))
183 if [ ${#blocks[@]} -lt ${block_max} ]; then
184 for block_id
in $
(seq ${#blocks[@]} -1 0); do
185 block
=${blocks[${block_id}]}
187 [ -z "${block}" ] && continue
189 if [ "${block}" = "X" ]; then
190 blocks
[${block_id}]="0000"
194 blocks
[$
(( ${block_max} - ${block_count} + ${block_id} ))]=${block}
195 blocks
[${block_id}]="0000"
199 for block_id
in $
(seq 0 ${#blocks[@]}); do
200 block
=${blocks[${block_id}]}
202 [ -z "${block}" ] && block
="0000"
204 while [ "${#block}" -lt 4 ]; do
208 blocks
[${block_id}]=${block}
212 for block
in ${blocks[@]}; do
213 address
="${address}:${block}"
215 address
=${address:1:39}
217 assert ipv6_is_valid
${address}
222 function ipv6_hash
() {
226 address
=$
(ipv6_explode
${address})
228 echo "${address//:/}"