ipv6-support: Add support for NPT status tracking
[openwrt/svn-archive/archive.git] / package / network / ipv6 / ipv6-support / files / support.sh
1 #!/bin/sh
2 # Copyright (c) 2012 OpenWrt.org
3 . /lib/functions.sh
4 . /lib/functions/service.sh
5 . /lib/functions/network.sh
6
7 config_load network6
8 local NAT="ip6tables -t nat"
9
10 conf_get() {
11 local __return="$1"
12 local __device="$2"
13 local __option="$3"
14 local __value=$(cat "/proc/sys/net/ipv6/conf/$device/$option")
15 eval "$__return=$__value"
16 }
17
18
19 conf_set() {
20 local device="$1"
21 local option="$2"
22 local value="$3"
23 echo "$value" > "/proc/sys/net/ipv6/conf/$device/$option"
24 }
25
26
27 stop_service() {
28 local __exe="$1"
29 SERVICE_PID_FILE="$2"
30 local __return="$3"
31
32 service_check "$__exe" && {
33 service_stop "$__exe"
34 [ -n "$__return" ] && eval "$__return=1"
35 }
36 rm -f "$SERVICE_PID_FILE"
37 }
38
39
40 start_service() {
41 local cmd="$1"
42 local pidfile="$2"
43
44 SERVICE_DAEMONIZE=1
45 SERVICE_WRITE_PID=1
46 SERVICE_PID_FILE="$pidfile"
47 service_start $cmd
48 }
49
50
51 resolve_network_add() {
52 local __section="$1"
53 local __device="$2"
54 local __return="$3"
55 local __cdevice
56 network_get_device __cdevice "$__section"
57 [ "$__cdevice" != "$__device" ] && return
58
59 eval "$__return"'="'"$__section"'"'
60 }
61
62
63 resolve_network() {
64 local __return="$1"
65 local __device="$2"
66 config_foreach resolve_network_add interface "$__device" "$__return"
67 }
68
69
70 setup_masquerading() {
71 local cmd="$1"
72 local chain="network6_masquerade_$2"
73 local device="$3"
74
75 $NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
76 $NAT -F "$chain" 2>/dev/null
77 $NAT -X "$chain" 2>/dev/null
78 }
79
80 [ "$cmd" != "stop" ] && {
81 $NAT -N "$chain"
82 $NAT -A "$chain" -o "$device" -j MASQUERADE
83 $NAT -A POSTROUTING -j "$chain"
84 }
85 }
86
87
88 setup_npt_chain() {
89 local cmd="$1"
90 local network="$2"
91 local chain="network6_npt_$network"
92
93 [ "$cmd" != "start" ] && {
94 $NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
95 $NAT -D PREROUTING -j "$chain" 2>/dev/null
96 $NAT -F "$chain" 2>/dev/null
97 $NAT -X "$chain" 2>/dev/null
98 }
99 }
100
101 [ "$cmd" != "stop" ] && {
102 $NAT -N "$chain" 2>/dev/null && {
103 $NAT -A PREROUTING -j "$chain"
104 $NAT -A POSTROUTING -j "$chain"
105 }
106 }
107 }
108
109
110 announce_prefix() {
111 local prefix="$1"
112 local network="$2"
113 local cmd="$3"
114
115 local addr=$(echo "$prefix" | cut -d/ -f1)
116 local rem=$(echo "$prefix" | cut -d/ -f2)
117 local length=$(echo "$rem" | cut -d, -f1)
118 local prefer=""
119 local valid=""
120
121 # If preferred / valid provided
122 [ "$rem" != "$length" ] && {
123 prefer=$(echo "$rem" | cut -d, -f2)
124 valid=$(echo "$rem" | cut -d, -f3)
125 }
126
127 # Get prefix configuration
128 local ula=""
129 local prefix_action=""
130 config_get ula global ula_prefix
131 config_get prefix_action "$network" prefix_action
132 [ -z "$prefix_action" ] && prefix_action="distribute"
133
134 # Always announce the ULA when doing NPT
135 [ "$prefix" == "$ula" -a "$prefix_action" == "npt" ] && prefix_action="distribute"
136
137 [ "$prefix_action" == "distribute" -o "$prefix_action" == "npt" ] && {
138 local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
139 [ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
140 [ -z "$cmd" ] && cmd=newprefix
141
142 [ "$prefix_action" == "npt" ] && msg="$msg"', "npt": 1'
143 ubus call 6distributed "$cmd" "$msg}"
144 }
145
146 [ "$prefix_action" == "npt" ] && {
147 local chain="network6_npt_$network"
148 local ula_addr=$(echo "$ula" | cut -d/ -f1)
149 local ula_rem=$(echo "$ula" | cut -d/ -f2)
150 local ula_length=$(echo "$ula_rem" | cut -d, -f1)
151 local device=""
152
153 network_get_device device "$network"
154 [ "$length" -lt "$ula_length" ] && length="$ula_length"
155 [ "$cmd" == "delprefix" ] && cmd="-D $chain" || cmd="-A $chain"
156
157 local in="-i $device -d $addr/$length -j NETMAP --to $ula_addr/$ula_length"
158 local out="-o $device -s $ula_addr/$ula_length -j NETMAP --to $addr/$length"
159
160 setup_npt_chain start "$network"
161 $NAT $cmd $in
162 $NAT $cmd $out
163 }
164 }
165
166
167 disable_router() {
168 local network="$1"
169
170 # Notify the address distribution daemon
171 ubus call 6distributed deliface '{"network": "'"$network"'"}'
172
173 # Disable advertisement daemon
174 stop_service /usr/sbin/6relayd "/var/run/ipv6-router-$network.pid"
175 }
176
177
178 restart_relay_slave() {
179 local __section="$1"
180 local __master="$2"
181
182 network_is_up "$__section" || return
183
184 local __device=""
185 network_get_device __device "$__section"
186
187 local __cmaster=""
188 config_get __cmaster "$__section" relay_master
189
190 [ "$__master" == "$__cmaster" ] && {
191 disable_interface "$__section"
192 enable_interface "$__section" "$__device"
193 }
194 }
195
196
197 add_relay_slave() {
198 local __section="$1"
199 local __return="$2"
200 local __master="$3"
201 local __mode="$4"
202
203 network_is_up "$__section" || return
204
205 # Get device
206 local __device=""
207 network_get_device __device "$__section"
208
209 # Match master network
210 local __cmaster=""
211 config_get __cmaster "$__section" relay_master
212 [ "$__master" == "$__cmaster" ] || return
213
214 # Test slave mode
215 local __cmode=""
216 config_get __cmode "$__section" mode
217 [ "$__cmode" == "downstream" ] && __cmode="router"
218
219 # Don't start fallback interfaces if we are in forced-relay mode
220 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] || return
221
222 # Don't make non-relay or non-router interfaces slaves
223 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] || return
224
225 # Disable any active distribution
226 [ "$__cmode" == "router" ] && disable_router "$__section"
227
228 # Configure interface to accept RA and send RS
229 conf_set "$__device" accept_ra 2
230 conf_set "$__device" forwarding 2
231
232 eval "$__return"'="$'"$__return"' '"$__device"'"'
233 }
234
235
236 stop_relay() {
237 local network="$1"
238 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
239 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
240 local was_fallback=""
241
242 stop_service /usr/sbin/6relayd "$pid_fallback" was_fallback
243 stop_service /usr/sbin/6relayd "$pid_forced"
244
245 # Reenable normal distribution on slave interfaces
246 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface "$network"
247 }
248
249
250 detect_forced_relay_mode() {
251 local __section="$1"
252 local __mode="$2"
253
254 local __cmode
255 config_get __cmode "$__section" mode
256 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
257 }
258
259
260 restart_relay() {
261 local network="$1"
262 local mode="$2"
263
264 # Stop last active relay
265 stop_relay "$network"
266
267 # Detect if we have a forced-relay
268 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
269
270 # Don't start without a mode
271 [ -z "$mode" ] && return
272
273 # Detect master device
274 local device=""
275 network_get_device device "$network"
276
277 # Generate command string
278 local cmd="/usr/sbin/6relayd -A $device"
279 local ifaces=""
280 config_foreach add_relay_slave interface ifaces "$network" "$mode"
281
282 # Start relay
283 local pid="/var/run/ipv6-relay-$mode-$network.pid"
284 [ -n "$ifaces" ] && start_service "$cmd $ifaces" "$pid"
285
286 # There are no slave interface, however indicate that we want to relay
287 [ -z "$ifaces" ] && touch "$pid"
288 }
289
290
291 setup_prefix_fallback() {
292 local cmd="$1"
293 local network="$2"
294 local device="$3"
295
296 stop_relay "$network"
297 restart_relay "$network"
298
299 setup_masquerading stop "$network"
300
301 [ "$cmd" != "stop" ] && {
302 local fallback=""
303 config_get fallback "$network" prefix_fallback
304
305 [ "$fallback" == "relay" ] && restart_relay "$network" fallback
306 [ "$fallback" == "masquerade" ] && setup_masquerading start "$network" "$device"
307 }
308 }
309
310
311 restart_master_relay() {
312 local network="$1"
313 local mode="$2"
314 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
315 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
316
317 # Disable active relaying to this interface
318 config_get relay_master "$network" relay_master
319 [ -z "$relay_master" ] && return
320 network_is_up "$relay_master" || return
321
322 # Detect running mode
323 [ -z "$mode" && -f "$pid_fallback" ] && mode="fallback"
324 [ -z "$mode" && -f "$pid_forced" ] && mode="forced"
325
326 # Restart relay if running or start requested
327 [ -n "$mode" ] && restart_relay "$relay_master" "$mode"
328 }
329
330
331 disable_interface() {
332 local network="$1"
333
334 # Delete all prefixes routed to this interface
335 ubus call 6distributed delprefix '{"network": "'"$network"'"}'
336
337 # Restart Relay
338 restart_master_relay "$network"
339
340 # Disable distribution
341 disable_router "$network"
342
343 # Disable any active relays, masquerading rules and NPT rules
344 stop_relay "$network"
345 setup_masquerading stop "$network"
346 setup_npt_chain stop "$network"
347
348 # Disable DHCPv6 client if enabled, state script will take care
349 stop_service /usr/sbin/odhcp6c "/var/run/ipv6-dhcpv6-$network.pid"
350 }
351
352
353 enable_ula_prefix() {
354 local network="$1"
355 local ula="$2"
356 [ -z "$ula" ] && ula="global"
357
358 # ULA-integration
359 local ula_prefix=""
360 config_get ula_prefix "$ula" ula_prefix
361
362 # ULA auto configuration (first init)
363 [ "$ula_prefix" == "auto" ] && {
364 local r1=""
365 local r2=""
366 local r3=""
367
368 # Sometimes results are empty, therefore try until it works...
369 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
370 r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
371 r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
372 r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
373 done
374
375 ula_prefix="fd$r1:$r2:$r3::/48"
376
377 # Save prefix so it will be preserved across reboots
378 config_set "$ula" ula_prefix "$ula_prefix"
379 uci_set network6 "$ula" ula_prefix "$ula_prefix"
380 uci_commit network6
381 }
382
383 # Announce ULA
384 [ -n "$ula_prefix" ] && announce_prefix "$ula_prefix" "$network"
385 }
386
387
388 enable_static() {
389 local network="$1"
390 local device="$2"
391
392 # Enable global forwarding
393 local global_forward
394 conf_get global_forward all forwarding
395 [ "$global_forward" != "1" ] && conf_set all forwarding 1
396
397 # Configure device
398 conf_set "$device" accept_ra 1
399 conf_set "$device" forwarding 1
400
401 # Enable ULA
402 enable_ula_prefix "$network"
403 # Compatibility (deprecated)
404 enable_ula_prefix "$network" "$network"
405
406 # Announce all static prefixes
407 config_list_foreach "$network" static_prefix announce_prefix $network
408
409 # start relay if there are forced relay members
410 restart_relay "$network"
411 }
412
413
414 enable_router() {
415 local network="$1"
416 local device="$2"
417
418 # Get IPv6 prefixes
419 local length
420 config_get length "$network" advertise_prefix
421 [ -z "$length" ] && length=64
422 [ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
423
424 # Start RD & DHCPv6 service
425 local pid="/var/run/ipv6-router-$network.pid"
426 start_service "/usr/sbin/6relayd -S . $device" "$pid"
427
428 # Try relaying if necessary
429 restart_master_relay "$network"
430 }
431
432
433 enable_dhcpv6() {
434 local network="$1"
435 local device="$2"
436
437 # Configure device
438 conf_set "$device" accept_ra 2
439 conf_set "$device" forwarding 2
440
441 # Trigger RS
442 conf_set "$device" disable_ipv6 1
443 conf_set "$device" disable_ipv6 0
444
445 # Configure DHCPv6-client
446 local dhcp6_opts="$device"
447
448 # Configure DHCPv6-client (e.g. requested prefix)
449 local request_prefix
450 config_get request_prefix "$network" request_prefix
451 [ -z "$request_prefix" ] && request_prefix="auto"
452 [ "$request_prefix" != "no" ] && {
453 [ "$request_prefix" == "auto" ] && request_prefix=0
454 dhcp6_opts="-P$request_prefix $dhcp6_opts"
455 }
456
457 # Start DHCPv6 client
458 local pid="/var/run/ipv6-dhcpv6-$network.pid"
459 start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
460
461 # Refresh RA on all interfaces
462 for pid in /var/run/ipv6-router-*.pid; do
463 kill -SIGUSR1 $(cat "$pid")
464 done
465 }
466
467
468 enable_6to4() {
469 local network="$1"
470 local device="$2"
471 local mode="$3"
472
473 local prefixlen="48"
474 [ "$mode" == "6rd" ] && {
475 local ip4prefix=$(uci_get network "$network" ip4prefixlen 0)
476 local ip6prefix=$(uci_get network "$network" ip6prefixlen 32)
477 prefixlen=$(($ip6prefix + 32 - $ip4prefix))
478 }
479
480 local prefix=""
481 network_get_ipaddr6 prefix "$network"
482
483 announce_prefix "$prefix/$prefixlen" "$network"
484 }
485
486
487 enable_interface()
488 {
489 local network="$1"
490 local device="$2"
491 local mode=""
492
493 config_get mode "$network" mode
494 [ -n "$mode" -a "$mode" != "none" ] || return
495
496 # Compatibility with old mode names
497 [ "$mode" == "downstream" ] && mode=router
498 [ "$mode" == "upstream" ] && mode=dhcpv6
499
500 # Run mode startup code
501 enable_static "$network" "$device"
502 [ "$mode" == "dhcpv6" ] && enable_dhcpv6 "$network" "$device"
503 [ "$mode" == "router" ] && enable_router "$network" "$device"
504 [ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4 "$network" "$device" "$mode"
505 [ "$mode" == "relay" ] && restart_master_relay "$network" forced
506 }