8cd044f99004a7e68e11d47ca186b324b1bfa9ea
[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 local type="$4"
115
116 local addr=$(echo "$prefix" | cut -d/ -f1)
117 local rem=$(echo "$prefix" | cut -d/ -f2)
118 local length=$(echo "$rem" | cut -d, -f1)
119 local prefer=""
120 local valid=""
121
122 # If preferred / valid provided
123 [ "$rem" != "$length" ] && {
124 prefer=$(echo "$rem" | cut -d, -f2)
125 valid=$(echo "$rem" | cut -d, -f3)
126 }
127
128 # Get prefix configuration
129 local ula=""
130 local prefix_action=""
131 config_get ula global ula_prefix
132 config_get prefix_action "$network" prefix_action
133 [ -z "$prefix_action" ] && prefix_action="distribute"
134
135 # Always announce the ULA when doing NPT
136 [ "$prefix" == "$ula" -a "$prefix_action" == "npt" ] && prefix_action="distribute"
137
138 [ "$prefix_action" == "distribute" -o "$prefix_action" == "npt" ] && {
139 local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
140 [ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
141 [ -z "$cmd" ] && cmd=newprefix
142
143 [ "$prefix_action" == "npt" ] && msg="$msg"', "npt": 1'
144 [ "$type" == "secondary" ] && msg="$msg"', "secondary": 1'
145 ubus call 6distributed "$cmd" "$msg}"
146 }
147
148 [ "$prefix_action" == "npt" ] && {
149 local chain="network6_npt_$network"
150 local ula_addr=$(echo "$ula" | cut -d/ -f1)
151 local ula_rem=$(echo "$ula" | cut -d/ -f2)
152 local ula_length=$(echo "$ula_rem" | cut -d, -f1)
153 local device=""
154
155 network_get_device device "$network"
156 [ "$length" -lt "$ula_length" ] && length="$ula_length"
157 [ "$cmd" == "delprefix" ] && cmd="-D $chain" || cmd="-A $chain"
158
159 local in="-i $device -d $addr/$length -j NETMAP --to $ula_addr/$ula_length"
160 local out="-o $device -s $ula_addr/$ula_length -j NETMAP --to $addr/$length"
161
162 setup_npt_chain start "$network"
163 $NAT $cmd $in
164 $NAT $cmd $out
165 }
166 }
167
168
169 disable_router() {
170 local network="$1"
171
172 # Notify the address distribution daemon
173 ubus call 6distributed deliface '{"network": "'"$network"'"}'
174
175
176 # Start RD & DHCPv6 service
177 local router_service
178 config_get router_service global router_service
179
180 if [ "$router_service" == "dnsmasq" ]; then
181 rm -f "/var/etc/dnsmasq.d/ipv6-router-$network.conf"
182 /etc/init.d/dnsmasq restart
183 else
184 stop_service /usr/sbin/6relayd "/var/run/ipv6-router-$network.pid"
185 fi
186 }
187
188
189 restart_relay_slave() {
190 local __section="$1"
191 local __master="$2"
192
193 network_is_up "$__section" || return
194
195 local __device=""
196 network_get_device __device "$__section"
197
198 local __cmaster=""
199 config_get __cmaster "$__section" relay_master
200
201 [ "$__master" == "$__cmaster" ] && {
202 disable_interface "$__section"
203 enable_interface "$__section" "$__device"
204 }
205 }
206
207
208 add_relay_slave() {
209 local __section="$1"
210 local __return="$2"
211 local __master="$3"
212 local __mode="$4"
213
214 network_is_up "$__section" || return
215
216 # Get device
217 local __device=""
218 network_get_device __device "$__section"
219
220 # Match master network
221 local __cmaster=""
222 config_get __cmaster "$__section" relay_master
223 [ "$__master" == "$__cmaster" ] || return
224
225 # Test slave mode
226 local __cmode=""
227 config_get __cmode "$__section" mode
228 [ "$__cmode" == "downstream" ] && __cmode="router"
229
230 # Don't start fallback interfaces if we are in forced-relay mode
231 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] || return
232
233 # Don't make non-relay or non-router interfaces slaves
234 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] || return
235
236 # Disable any active distribution
237 [ "$__cmode" == "router" ] && disable_router "$__section"
238
239 # Configure interface to accept RA and send RS
240 conf_set "$__device" accept_ra 2
241 conf_set "$__device" forwarding 2
242
243 eval "$__return"'="$'"$__return"' '"$__device"'"'
244 }
245
246
247 stop_relay() {
248 local network="$1"
249 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
250 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
251 local was_fallback=""
252
253 stop_service /usr/sbin/6relayd "$pid_fallback" was_fallback
254 stop_service /usr/sbin/6relayd "$pid_forced"
255
256 # Reenable normal distribution on slave interfaces
257 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface "$network"
258 }
259
260
261 detect_forced_relay_mode() {
262 local __section="$1"
263 local __mode="$2"
264
265 local __cmode
266 config_get __cmode "$__section" mode
267 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
268 }
269
270
271 restart_relay() {
272 local network="$1"
273 local mode="$2"
274
275 # Stop last active relay
276 stop_relay "$network"
277
278 # Detect if we have a forced-relay
279 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
280
281 # Don't start without a mode
282 [ -z "$mode" ] && return
283
284 # Detect master device
285 local device=""
286 network_get_device device "$network"
287
288 # Generate command string
289 local cmd="/usr/sbin/6relayd -A $device"
290 local ifaces=""
291 config_foreach add_relay_slave interface ifaces "$network" "$mode"
292
293 # Start relay
294 local pid="/var/run/ipv6-relay-$mode-$network.pid"
295 [ -n "$ifaces" ] && start_service "$cmd $ifaces" "$pid"
296
297 # There are no slave interface, however indicate that we want to relay
298 [ -z "$ifaces" ] && touch "$pid"
299 }
300
301
302 setup_prefix_fallback() {
303 local cmd="$1"
304 local network="$2"
305 local device="$3"
306
307 stop_relay "$network"
308 restart_relay "$network"
309
310 setup_masquerading stop "$network"
311
312 [ "$cmd" != "stop" ] && {
313 local fallback=""
314 config_get fallback "$network" prefix_fallback
315
316 [ "$fallback" == "relay" ] && restart_relay "$network" fallback
317 [ "$fallback" == "masquerade" ] && setup_masquerading start "$network" "$device"
318 }
319 }
320
321
322 restart_master_relay() {
323 local network="$1"
324 local mode="$2"
325 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
326 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
327
328 # Disable active relaying to this interface
329 config_get relay_master "$network" relay_master
330 [ -z "$relay_master" ] && return
331 network_is_up "$relay_master" || return
332
333 # Detect running mode
334 [ -z "$mode" && -f "$pid_fallback" ] && mode="fallback"
335 [ -z "$mode" && -f "$pid_forced" ] && mode="forced"
336
337 # Restart relay if running or start requested
338 [ -n "$mode" ] && restart_relay "$relay_master" "$mode"
339 }
340
341
342 disable_interface() {
343 local network="$1"
344
345 # Delete all prefixes routed to this interface
346 ubus call 6distributed delprefix '{"network": "'"$network"'"}'
347
348 # Restart Relay
349 restart_master_relay "$network"
350
351 # Disable distribution
352 disable_router "$network"
353
354 # Disable any active relays, masquerading rules and NPT rules
355 stop_relay "$network"
356 setup_masquerading stop "$network"
357 setup_npt_chain stop "$network"
358
359 # Disable DHCPv6 client if enabled, state script will take care
360 stop_service /usr/sbin/odhcp6c "/var/run/ipv6-dhcpv6-$network.pid"
361 }
362
363
364 enable_ula_prefix() {
365 local network="$1"
366 local ula="$2"
367 [ -z "$ula" ] && ula="global"
368
369 # ULA-integration
370 local ula_prefix=""
371 config_get ula_prefix "$ula" ula_prefix
372
373 # ULA auto configuration (first init)
374 [ "$ula_prefix" == "auto" ] && {
375 local r1=""
376 local r2=""
377 local r3=""
378
379 # Sometimes results are empty, therefore try until it works...
380 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
381 r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
382 r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
383 r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
384 done
385
386 ula_prefix="fd$r1:$r2:$r3::/48"
387
388 # Save prefix so it will be preserved across reboots
389 config_set "$ula" ula_prefix "$ula_prefix"
390 uci_set network6 "$ula" ula_prefix "$ula_prefix"
391 uci_commit network6
392 }
393
394 # Announce ULA
395 [ -n "$ula_prefix" ] && announce_prefix "$ula_prefix" "$network" newprefix secondary
396 }
397
398
399 enable_static() {
400 local network="$1"
401 local device="$2"
402
403 # Enable global forwarding
404 local global_forward
405 conf_get global_forward all forwarding
406 [ "$global_forward" != "1" ] && conf_set all forwarding 1
407
408 # Configure device
409 conf_set "$device" accept_ra 1
410 conf_set "$device" forwarding 1
411
412 # Enable ULA
413 enable_ula_prefix "$network"
414 # Compatibility (deprecated)
415 enable_ula_prefix "$network" "$network"
416
417 # Announce all static prefixes
418 config_list_foreach "$network" static_prefix announce_prefix $network
419
420 # start relay if there are forced relay members
421 restart_relay "$network"
422 }
423
424
425 enable_router() {
426 local network="$1"
427 local device="$2"
428
429 # Get IPv6 prefixes
430 local length
431 config_get length "$network" advertise_prefix
432 [ -z "$length" ] && length=64
433 [ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
434
435 # Start RD & DHCPv6 service
436 local router_service
437 config_get router_service global router_service
438
439 if [ "$router_service" == "dnsmasq" ]; then
440 local dnsmasq_opts
441 config_get dnsmasq_opts global dnsmasq_opts
442 [ -z "$dnsmasq_opts" ] && dnsmasq_opts="ra-stateful,ra-names"
443
444 local conf="/var/etc/dnsmasq.d/ipv6-router-$network.conf"
445 mkdir -p $(dirname $conf)
446 echo "dhcp-range=::1,constructor:$device,$dnsmasq_opts" > $conf
447 echo "enable-ra" >> $conf
448 /etc/init.d/dnsmasq restart
449 else
450 local pid="/var/run/ipv6-router-$network.pid"
451 start_service "/usr/sbin/6relayd -S . $device" "$pid"
452 fi
453
454 # Try relaying if necessary
455 restart_master_relay "$network"
456 }
457
458
459 enable_dhcpv6() {
460 local network="$1"
461 local device="$2"
462
463 # Configure device
464 conf_set "$device" accept_ra 2
465 conf_set "$device" forwarding 2
466
467 # Trigger RS
468 conf_set "$device" disable_ipv6 1
469 conf_set "$device" disable_ipv6 0
470
471 # Configure DHCPv6-client
472 local dhcp6_opts="$device"
473
474 # Configure DHCPv6-client (e.g. requested prefix)
475 local request_prefix
476 config_get request_prefix "$network" request_prefix
477 [ -z "$request_prefix" ] && request_prefix="auto"
478 [ "$request_prefix" != "no" ] && {
479 [ "$request_prefix" == "auto" ] && request_prefix=0
480 dhcp6_opts="-P$request_prefix $dhcp6_opts"
481 }
482
483 # Start DHCPv6 client
484 local pid="/var/run/ipv6-dhcpv6-$network.pid"
485 start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
486
487 # Refresh RA on all interfaces
488 for pid in /var/run/ipv6-router-*.pid; do
489 kill -SIGUSR1 $(cat "$pid")
490 done
491 }
492
493
494 enable_6to4() {
495 local network="$1"
496 local device="$2"
497 local mode="$3"
498
499 local prefixlen="48"
500 [ "$mode" == "6rd" ] && {
501 local ip4prefix=$(uci_get network "$network" ip4prefixlen 0)
502 local ip6prefix=$(uci_get network "$network" ip6prefixlen 32)
503 prefixlen=$(($ip6prefix + 32 - $ip4prefix))
504 }
505
506 local prefix=""
507 network_get_ipaddr6 prefix "$network"
508
509 announce_prefix "$prefix/$prefixlen" "$network"
510 }
511
512
513 enable_interface()
514 {
515 local network="$1"
516 local device="$2"
517 local mode=""
518
519 config_get mode "$network" mode
520 [ -n "$mode" -a "$mode" != "none" ] || return
521
522 # Compatibility with old mode names
523 [ "$mode" == "downstream" ] && mode=router
524 [ "$mode" == "upstream" ] && mode=dhcpv6
525
526 # Run mode startup code
527 enable_static "$network" "$device"
528 [ "$mode" == "dhcpv6" ] && enable_dhcpv6 "$network" "$device"
529 [ "$mode" == "router" ] && enable_router "$network" "$device"
530 [ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4 "$network" "$device" "$mode"
531 [ "$mode" == "relay" ] && restart_master_relay "$network" forced
532 }