]> git.ipfire.org Git - people/ms/network.git/blob - functions.ipv6
Create a basic version of an ISDN dial-in server.
[people/ms/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 ipcalc --ipv6 -c $@ >/dev/null 2>&1
142
143 case "$?" in
144 0)
145 return ${EXIT_OK}
146 ;;
147 *)
148 return ${EXIT_ERROR}
149 ;;
150 esac
151 }
152
153 function ipv6_implode() {
154 local address=${1}
155
156 assert isset address
157
158 if ! ipv6_is_valid ${address}; then
159 error "IPv6 address is invalid: ${address}"
160 return ${EXIT_ERROR}
161 fi
162
163 # Save prefix
164 local prefix=$(ip_get_prefix ${address})
165 address=$(ip_split_prefix ${address})
166
167 # Make proper address in exploded format
168 address=$(ipv6_explode ${address})
169
170 local block
171 local char
172 local i
173
174 local address_new
175 local block_new
176
177 for block in ${address//:/\ }; do
178 block_new=
179 for i in $(seq 0 ${#block}); do
180 char="${block:${i}:1}"
181
182 [ -z "${char}" ] && continue
183
184 if [ -z "${block_new}" ] && [ "${char}" = "0" ]; then
185 continue
186 fi
187
188 block_new="${block_new}${char}"
189 done
190
191 [ -z "${block_new}" ] && block_new="0"
192
193 address_new="${address_new}:${block_new}"
194 done
195
196 # Cut first colon (:)
197 address="${address_new:1:${#address_new}}"
198
199 local match
200 local matches=()
201 local pattern
202 local pos_start
203 local pos_next
204 for pos_start in $(seq 0 ${#address}); do
205 matches["${pos_start}"]=0
206
207 for pos_next in $(seq ${pos_start} 2 ${#address}); do
208 case "${pos_start}" in
209 0)
210 match="${address:${pos_next}:2}"
211 pattern="0:"
212 ;;
213 *)
214 match="${address:${pos_next}:2}"
215 pattern=":0"
216 ;;
217 esac
218
219 [ -z "${match}" ] && continue
220
221 if [ "${match}" = "${pattern}" ]; then
222 matches[${pos_start}]=$(( matches[${pos_start}] + 1))
223 else
224 break
225 fi
226 done
227 done
228
229 local pos_best
230 local pos_best_val=0
231 for i in $(seq 0 ${#matches[@]}); do
232 [ -z "${matches[${i}]}" ] && continue
233
234 if [ ${matches[${i}]} -gt ${pos_best_val} ]; then
235 pos_best=${i}
236 pos_best_val=${matches[${i}]}
237 fi
238 done
239
240 if [ -n "${pos_best}" ]; then
241 address_new="${address:0:${pos_best}}::"
242
243 local pos_end=$(( ${pos_best_val} * 2 + ${pos_best} + 1))
244
245 if [ "${pos_best}" = "0" ]; then
246 pos_end=$(( ${pos_end} - 1 ))
247 fi
248
249 address="${address_new}${address:${pos_end}:${#address}}"
250 fi
251
252 # If a prefix was provided we append it in the end
253 [ -n "${prefix}" ] && address="${address}/${prefix}"
254
255 assert ipv6_is_valid ${address}
256
257 echo "${address}"
258 }
259
260 function ipv6_explode() {
261 local address=${1}
262
263 assert isset address
264
265 local prefix=$(ip_get_prefix ${address})
266 address=$(ip_split_prefix ${address})
267
268 if [ ${#address} -eq 39 ]; then
269 echo "${address}$([ -n "${prefix}" ] && echo "/${prefix}")"
270 return ${EXIT_OK}
271 fi
272
273 address=${address//::/:X:}
274
275 local block
276 local block_count=0
277 local block_id
278 local block_max=8
279 local blocks=()
280
281 for block in ${address//:/\ }; do
282 blocks[${block_count}]=${block}
283
284 block_count=$(( ${block_count} + 1 ))
285 done
286
287 if [ ${#blocks[@]} -lt ${block_max} ]; then
288 for block_id in $(seq ${#blocks[@]} -1 0); do
289 block=${blocks[${block_id}]}
290
291 [ -z "${block}" ] && continue
292
293 if [ "${block}" = "X" ]; then
294 blocks[${block_id}]="0000"
295 break
296 fi
297
298 blocks[$(( ${block_max} - ${block_count} + ${block_id} ))]=${block}
299 blocks[${block_id}]="0000"
300 done
301 fi
302
303 for block_id in $(seq 0 ${#blocks[@]}); do
304 block=${blocks[${block_id}]}
305
306 [ -z "${block}" ] && block="0000"
307
308 while [ "${#block}" -lt 4 ]; do
309 block="0${block}"
310 done
311
312 blocks[${block_id}]=${block}
313 done
314
315 address=
316 for block in ${blocks[@]}; do
317 address="${address}:${block}"
318 done
319 address=${address:1:39}
320
321 # If a prefix was provided we append it in the end again
322 [ -n "${prefix}" ] && address="${address}/${prefix}"
323
324 assert ipv6_is_valid ${address}
325
326 echo "${address}"
327 }
328
329 function ipv6_hash() {
330 local address=${1}
331
332 assert isset address
333
334 # Explode address
335 address=$(ipv6_explode ${address})
336
337 echo "${address//:/}"
338 }