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