]> git.ipfire.org Git - people/stevee/network.git/blob - functions.ipv6
Make versioning of IP protocols more modular.
[people/stevee/network.git] / functions.ipv6
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv6"
23
24 function ipv6_init() {
25 log INFO "Initializing IPv6 networking."
26
27 # Enable forwarding on all devices
28 ipv6_device_forwarding_disable all
29 ipv6_device_forwarding_disable default
30
31 # Disable autoconfiguration on all devices per default
32 ipv6_device_autoconf_disable all
33 ipv6_device_autoconf_disable default
34
35 # XXX do we need this?
36 #local device
37 #for device in $(devices_get_all); do
38 # ipv6_device_forwarding_disable ${device}
39 # ipv6_device_autoconf_disable ${device}
40 #done
41 }
42
43 init_register ipv6_init
44
45 function ipv6_device_autoconf_enable() {
46 local device=${1}
47
48 assert isset device
49
50 # Allow setting default and all settings
51 if ! isoneof device all default; then
52 assert device_exists ${device}
53 fi
54
55 local val
56 for val in accept_ra accept_redirects; do
57 echo 1 > /proc/sys/net/ipv6/conf/${device}/${val}
58 done
59 }
60
61 function ipv6_device_autoconf_disable() {
62 local device=${1}
63
64 assert isset device
65
66 # Allow setting default and all settings
67 if ! isoneof device all default; then
68 assert device_exists ${device}
69 fi
70
71 local val
72 for val in accept_ra accept_redirects; do
73 echo 0 > /proc/sys/net/ipv6/conf/${device}/${val}
74 done
75 }
76
77 function ipv6_device_forwarding_enable() {
78 local device=${1}
79
80 assert isset device
81
82 # Allow setting default and all settings
83 if ! isoneof device all default; then
84 assert device_exists ${device}
85 fi
86
87 echo 1 > /proc/sys/net/ipv6/conf/${device}/forwarding
88 }
89
90 function ipv6_device_forwarding_disable() {
91 local device=${1}
92
93 assert isset device
94
95 # Allow setting default and all settings
96 if ! isoneof device all default; then
97 assert device_exists ${device}
98 fi
99
100 echo 0 > /proc/sys/net/ipv6/conf/${device}/forwarding
101 }
102
103 # Enable IPv6 RFC3041 privacy extensions if desired
104 function ipv6_device_privacy_extensions_enable() {
105 local device=${1}
106 local type=${2}
107
108 assert isset device
109 assert device_exists ${device}
110
111 # Default value is rfc3041
112 if [ -z "${type}" ]; then
113 type="rfc3041"
114 fi
115
116 assert isset type
117
118 case "${type}" in
119 rfc3041)
120 echo 2 > /proc/sys/net/ipv6/conf/${device}/use_tempaddr
121 ;;
122 *)
123 error_log "Given type '${type}' is not supported."
124 return ${EXIT_ERROR}
125 ;;
126 esac
127
128 return ${EXIT_OK}
129 }
130
131 function ipv6_device_privacy_extensions_disable() {
132 local device=${1}
133
134 assert isset device
135 assert device_exists ${device}
136
137 echo 0 > /proc/sys/net/ipv6/conf/${device}/use_tempaddr
138 }
139
140 function ipv6_is_valid() {
141 local address=${1}
142
143 assert isset address
144
145 # Check length
146 [ ${#address} -gt 39 ] && return ${EXIT_ERROR}
147
148 # XXX find :: twice?
149 # XXX check for documentation prefix?
150
151 # Check for bad characters
152 local char
153 for char in 0 1 2 3 4 5 6 7 8 9 a b c d e f :; do
154 address=${address//${char}/}
155 done
156 [ -n "${address}" ] && return ${EXIT_ERROR}
157
158 return ${EXIT_OK}
159 }
160
161 function ipv6_implode() {
162 local address=${1}
163
164 assert isset address
165
166 if ! ipv6_is_valid ${address}; then
167 error "IPv6 address is invalid: ${address}"
168 return ${EXIT_ERROR}
169 fi
170
171 # Make proper address in exploded format
172 address=$(ipv6_explode ${address})
173
174 local block
175 local char
176 local i
177
178 local address_new
179 local block_new
180
181 for block in ${address//:/\ }; do
182 block_new=
183 for i in $(seq 0 ${#block}); do
184 char="${block:${i}:1}"
185
186 [ -z "${char}" ] && continue
187
188 if [ -z "${block_new}" ] && [ "${char}" = "0" ]; then
189 continue
190 fi
191
192 block_new="${block_new}${char}"
193 done
194
195 [ -z "${block_new}" ] && block_new="0"
196
197 address_new="${address_new}:${block_new}"
198 done
199
200 # Cut first colon (:)
201 address="${address_new:1:${#address_new}}"
202
203 local match
204 local matches=()
205 local pattern
206 local pos_start
207 local pos_next
208 for pos_start in $(seq 0 ${#address}); do
209 matches["${pos_start}"]=0
210
211 for pos_next in $(seq ${pos_start} 2 ${#address}); do
212 case "${pos_start}" in
213 0)
214 match="${address:${pos_next}:2}"
215 pattern="0:"
216 ;;
217 *)
218 match="${address:${pos_next}:2}"
219 pattern=":0"
220 ;;
221 esac
222
223 [ -z "${match}" ] && continue
224
225 if [ "${match}" = "${pattern}" ]; then
226 matches[${pos_start}]=$(( matches[${pos_start}] + 1))
227 else
228 break
229 fi
230 done
231 done
232
233 local pos_best
234 local pos_best_val=0
235 for i in $(seq 0 ${#matches[@]}); do
236 [ -z "${matches[${i}]}" ] && continue
237
238 if [ ${matches[${i}]} -gt ${pos_best_val} ]; then
239 pos_best=${i}
240 pos_best_val=${matches[${i}]}
241 fi
242 done
243
244 if [ -n "${pos_best}" ]; then
245 address_new="${address:0:${pos_best}}::"
246
247 local pos_end=$(( ${pos_best_val} * 2 + ${pos_best} + 1))
248
249 if [ "${pos_best}" = "0" ]; then
250 pos_end=$(( ${pos_end} - 1 ))
251 fi
252
253 address="${address_new}${address:${pos_end}:${#address}}"
254 fi
255
256 assert ipv6_is_valid ${address}
257
258 echo "${address}"
259 }
260
261 function ipv6_explode() {
262 local address=${1}
263
264 assert isset address
265
266 if [ ${#address} -eq 39 ]; then
267 echo "${address}"
268 return ${EXIT_OK}
269 fi
270
271 address=${address//::/:X:}
272
273 local block
274 local block_count=0
275 local block_id
276 local block_max=8
277 local blocks=()
278
279 for block in ${address//:/\ }; do
280 blocks[${block_count}]=${block}
281
282 block_count=$(( ${block_count} + 1 ))
283 done
284
285 if [ ${#blocks[@]} -lt ${block_max} ]; then
286 for block_id in $(seq ${#blocks[@]} -1 0); do
287 block=${blocks[${block_id}]}
288
289 [ -z "${block}" ] && continue
290
291 if [ "${block}" = "X" ]; then
292 blocks[${block_id}]="0000"
293 break
294 fi
295
296 blocks[$(( ${block_max} - ${block_count} + ${block_id} ))]=${block}
297 blocks[${block_id}]="0000"
298 done
299 fi
300
301 for block_id in $(seq 0 ${#blocks[@]}); do
302 block=${blocks[${block_id}]}
303
304 [ -z "${block}" ] && block="0000"
305
306 while [ "${#block}" -lt 4 ]; do
307 block="0${block}"
308 done
309
310 blocks[${block_id}]=${block}
311 done
312
313 address=
314 for block in ${blocks[@]}; do
315 address="${address}:${block}"
316 done
317 address=${address:1:39}
318
319 assert ipv6_is_valid ${address}
320
321 echo "${address}"
322 }
323
324 function ipv6_hash() {
325 local address=${1}
326
327 assert isset address
328
329 # Explode address
330 address=$(ipv6_explode ${address})
331
332 echo "${address//:/}"
333 }