ipv6-support: next iteration * Introduce mode 6rd * Introduce mode 6to4 * Fix announc...
[openwrt/staging/yousong.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
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 announce_prefix() {
71 local prefix="$1"
72 local network="$2"
73 local cmd="$3"
74
75 local addr=$(echo "$prefix" | cut -d/ -f1)
76 local rem=$(echo "$prefix" | cut -d/ -f2)
77 local length=$(echo "$rem" | cut -d, -f1)
78 local prefer=""
79 local valid=""
80
81 # If preferred / valid provided
82 [ "$rem" != "$length" ] && {
83 prefer=$(echo "$rem" | cut -d, -f2)
84 valid=$(echo "$rem" | cut -d, -f3)
85 }
86
87 local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
88 [ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
89 [ -z "$cmd" ] && cmd=newprefix
90
91 ubus call 6distributed "$cmd" "$msg}"
92 }
93
94
95 disable_router() {
96 local network="$1"
97
98 # Notify the address distribution daemon
99 ubus call 6distributed deliface '{"network": "'"$network"'"}'
100
101 # Disable advertisement daemon
102 stop_service /usr/sbin/6relayd "/var/run/ipv6-router-$network.pid"
103 }
104
105
106 restart_relay_slave() {
107 local __section="$1"
108 local __master="$2"
109
110 network_is_up "$__section" || return
111
112 local __device=""
113 network_get_device __device "$__section"
114
115 local __cmaster=""
116 config_get __cmaster "$__section" relay_master
117
118 [ "$__master" == "$__cmaster" ] && {
119 disable_interface "$__section"
120 enable_interface "$__section" "$__device"
121 }
122 }
123
124
125 add_relay_slave() {
126 local __section="$1"
127 local __return="$2"
128 local __master="$3"
129 local __mode="$4"
130
131 network_is_up "$__section" || return
132
133 # Get device
134 local __device=""
135 network_get_device __device "$__section"
136
137 # Match master network
138 local __cmaster=""
139 config_get __cmaster "$__section" relay_master
140 [ "$__master" == "$__cmaster" ] || return
141
142 # Test slave mode
143 local __cmode=""
144 config_get __cmode "$__section" mode
145 [ "$__cmode" == "downstream" ] && __cmode="router"
146
147 # Don't start fallback interfaces if we are in forced-relay mode
148 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] || return
149
150 # Don't make non-relay or non-router interfaces slaves
151 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] || return
152
153 # Disable any active distribution
154 [ "$__cmode" == "router" ] && disable_router "$__section"
155
156 # Configure interface to accept RA and send RS
157 conf_set "$__device" accept_ra 2
158 conf_set "$__device" forwarding 2
159
160 eval "$__return"'="$'"$__return"' '"$__device"'"'
161 }
162
163
164 stop_relay() {
165 local network="$1"
166 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
167 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
168 local was_fallback=""
169
170 stop_service /usr/sbin/6relayd "$pid_fallback" was_fallback
171 stop_service /usr/sbin/6relayd "$pid_forced"
172
173 # Reenable normal distribution on slave interfaces
174 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface "$network"
175 }
176
177
178 detect_forced_relay_mode() {
179 local __section="$1"
180 local __mode="$2"
181
182 local __cmode
183 config_get __cmode "$__section" mode
184 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
185 }
186
187
188 restart_relay() {
189 local network="$1"
190 local mode="$2"
191
192 # Stop last active relay
193 stop_relay "$network"
194
195 # Detect if we have a forced-relay
196 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
197
198 # Don't start without a mode
199 [ -z "$mode" ] && return
200
201 # Detect master device
202 local device=""
203 network_get_device device "$network"
204
205 # Generate command string
206 local cmd="/usr/sbin/6relayd -A $device"
207 local ifaces=""
208 config_foreach add_relay_slave interface ifaces "$network" "$mode"
209
210 # Start relay
211 local pid="/var/run/ipv6-relay-$mode-$network.pid"
212 [ -n "$ifaces" ] && start_service "$cmd $ifaces" "$pid"
213
214 # There are no slave interface, however indicate that we want to relay
215 [ -z "$ifaces" ] && touch "$pid"
216 }
217
218
219 restart_master_relay() {
220 local network="$1"
221 local mode="$2"
222 local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
223 local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
224
225 # Disable active relaying to this interface
226 config_get relay_master "$network" relay_master
227 [ -z "$relay_master" ] && return
228 network_is_up "$relay_master" || return
229
230 # Detect running mode
231 [ -z "$mode" && -f "$pid_fallback" ] && mode="fallback"
232 [ -z "$mode" && -f "$pid_forced" ] && mode="forced"
233
234 # Restart relay if running or start requested
235 [ -n "$mode" ] && restart_relay "$relay_master" "$mode"
236 }
237
238
239 disable_interface() {
240 local network="$1"
241
242 # Delete all prefixes routed to this interface
243 ubus call 6distributed delprefix '{"network": "'"$network"'"}'
244
245 # Restart Relay
246 restart_master_relay "$network"
247
248 # Disable distribution
249 disable_router "$network"
250
251 # Disable relay
252 stop_relay "$network"
253
254 # Disable DHCPv6 client if enabled, state script will take care
255 stop_service /usr/sbin/odhcp6c "/var/run/ipv6-dhcpv6-$network.pid"
256 }
257
258
259 enable_static() {
260 local network="$1"
261 local device="$2"
262
263 # Enable global forwarding
264 local global_forward
265 conf_get global_forward all forwarding
266 [ "$global_forward" != "1" ] && conf_set all forwarding 1
267
268 # Configure device
269 conf_set "$device" accept_ra 1
270 conf_set "$device" forwarding 1
271
272 # ULA-integration
273 local ula_prefix=""
274 config_get ula_prefix "$network" ula_prefix
275
276 # ULA auto configuration (first init)
277 [ "$ula_prefix" == "auto" ] && {
278 local r1=""
279 local r2=""
280 local r3=""
281
282 # Sometimes results are empty, therefore try until it works...
283 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
284 r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
285 r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
286 r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
287 done
288
289 ula_prefix="fd$r1:$r2:$r3::/48"
290
291 # Save prefix so it will be preserved across reboots
292 uci_set network6 "$network" ula_prefix "$ula_prefix"
293 uci_commit network6
294 }
295
296 # Announce ULA
297 [ -n "$ula_prefix" ] && announce_prefix $ula_prefix $network
298
299 # Announce all static prefixes
300 config_list_foreach "$network" static_prefix announce_prefix $network
301
302 # start relay if there are forced relay members
303 restart_relay "$network"
304 }
305
306
307 enable_router() {
308 local network="$1"
309 local device="$2"
310
311 # Get IPv6 prefixes
312 local length
313 config_get length "$network" advertise_prefix
314 [ -z "$length" ] && length=64
315 [ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
316
317 # Start RD & DHCPv6 service
318 local pid="/var/run/ipv6-router-$network.pid"
319
320 # Start server
321 start_service "/usr/sbin/6relayd -S . $device" "$pid"
322
323 # Try relaying if necessary
324 restart_master_relay "$network"
325
326 # start relay if there are forced relay members
327 restart_relay "$network"
328 }
329
330
331 enable_dhcpv6() {
332 local network="$1"
333 local device="$2"
334
335 # Configure device
336 conf_set "$device" accept_ra 2
337 conf_set "$device" forwarding 2
338
339 # Trigger RS
340 conf_set "$device" disable_ipv6 1
341 conf_set "$device" disable_ipv6 0
342
343 # Configure DHCPv6-client
344 local dhcp6_opts="$device"
345
346 # Configure DHCPv6-client (e.g. requested prefix)
347 local request_prefix
348 config_get request_prefix "$network" request_prefix
349 [ -z "$request_prefix" ] && request_prefix="auto"
350 [ "$request_prefix" != "no" ] && {
351 [ "$request_prefix" == "auto" ] && request_prefix=0
352 dhcp6_opts="-P$request_prefix $dhcp6_opts"
353 }
354
355 # Start DHCPv6 client
356 local pid="/var/run/ipv6-dhcpv6-$network.pid"
357 start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
358
359 # Refresh RA on all interfaces
360 for pid in /var/run/ipv6-router-*.pid; do
361 kill -SIGUSR1 $(cat "$pid")
362 done
363 }
364
365
366 enable_6to4() {
367 local network="$1"
368 local device="$2"
369 local mode="$3"
370
371 local prefixlen="48"
372 [ "$mode" == "6rd" ] && {
373 local ip4prefix=$(uci_get network "$network" ip4prefixlen 0)
374 local ip6prefix=$(uci_get network "$network" ip6prefixlen 32)
375 prefixlen=$(($ip6prefix + 32 - $ip4prefix))
376 }
377
378 local prefix=""
379 network_get_ipaddr6 prefix "$network"
380
381 announce_prefix "$prefix/$prefixlen" "$network"
382 }
383
384
385 enable_interface()
386 {
387 local network="$1"
388 local device="$2"
389 local mode=""
390 config_get mode "$network" mode
391
392 # Compatibility with old mode names
393 [ "$mode" == "downstream" ] && mode=router
394 [ "$mode" == "upstream" ] && mode=dhcpv6
395
396 # Run mode startup code
397 [ "$mode" == "dhcpv6" -o "$mode" == "static" ] && enable_static "$network" "$device"
398 [ "$mode" == "dhcpv6" ] && enable_dhcpv6 "$network" "$device"
399 [ "$mode" == "router" ] && enable_router "$network" "$device"
400 [ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4 "$network" "$device" "$mode"
401 [ "$mode" == "relay" ] && restart_master_relay "$network" forced
402 }