ipv6-support: Add new IPv6-support meta-package
[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
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
56 local __cdevice
57 network_get_device __cdevice "$__section"
58 [ "$__cdevice" != "$__device" ] && return
59
60 eval "$__return"'="'"$__section"'"'
61 }
62
63
64 resolve_network() {
65 local __return="$1"
66 local __device="$2"
67 config_foreach resolve_network_add interface "$__device" "$__return"
68 }
69
70
71 announce_prefix() {
72 local prefix="$1"
73 local network="$2"
74 local cmd="$3"
75
76 local addr=$(echo "$prefix" | cut -d/ -f1)
77 local rem=$(echo "$prefix" | cut -d/ -f2)
78 local length=$(echo "$rem" | cut -d, -f1)
79 local prefer=""
80 local valid=""
81
82 # If preferred / valid provided
83 [ "$rem" != "$length" ] && {
84 prefer=$(echo "$rem" | cut -d, -f2)
85 valid=$(echo "$rem" | cut -d, -f3)
86 }
87
88 local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
89 [ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
90 [ -z "$cmd" ] && cmd=newprefix
91
92 ubus call 6distributed "$cmd" "$msg}"
93 }
94
95
96 disable_downstream() {
97 local network="$1"
98
99 # Notify the address distribution daemon
100 ubus call 6distributed deliface '{"network": "'"$network"'"}'
101
102 # Disable advertisement daemon
103 stop_service /usr/sbin/6relayd "/var/run/ipv6-downstream-$network.pid"
104 }
105
106
107 restart_relay_add() {
108 local __section="$1"
109 local __return="$2"
110 local __master="$3"
111 local __disable="$4"
112
113 network_is_up "$__section" || return
114
115 # Match master network
116 local __cmaster=""
117 config_get __cmaster "$__section" relay_master
118 [ "$__master" != "$__cmaster" ] && return
119
120 # Disable any active distribution
121 disable_downstream "$__section"
122
123 local __device=""
124 network_get_device __device "$__section"
125
126 # Coming from stop relay, reenable distribution
127 [ "$__disable" == "disable" ] && {
128 enable_downstream "$__section" "$__device"
129 return
130 }
131
132
133 eval "$__return"'="$'"$__return"' '"$__device"'"'
134 }
135
136
137 stop_relay() {
138 local network="$1"
139 local pid="/var/run/ipv6-relay-$network.pid"
140 local was_running=""
141
142 stop_service /usr/sbin/6relayd "$pid" was_running
143
144 # Reenable normal distribution on slave interfaces
145 [ -n "$was_running" ] && config_foreach restart_relay_add interface dummy "$network" disable
146 }
147
148
149 restart_relay() {
150 local network="$1"
151 local force="$2"
152 local pid="/var/run/ipv6-relay-$network.pid"
153
154 local not_running=0
155 [ -f "$pid" ] || not_running=1
156
157 # Don't start if not desired
158 [ "$force" != "1" ] && [ "$not_running" == "1" ] && return
159
160 # Kill current relay and distribution daemon
161 stop_relay "$network"
162
163 # Detect master device
164 local device=""
165 network_get_device device $network
166
167 # Generate command string
168 local cmd="/usr/sbin/6relayd -A $device "
169 config_foreach restart_relay_add interface cmd "$network"
170
171 # Start relay
172 start_service "$cmd" "$pid"
173 }
174
175
176 restart_master_relay() {
177 local network="$1"
178
179 # Disable active relaying to this interface
180 local relay_master
181 config_get relay_master "$network" relay_master
182 [ -n "$relay_master" ] && restart_relay "$relay_master"
183 }
184
185
186 disable_interface() {
187 local network="$1"
188
189 # Delete all prefixes routed to this interface
190 ubus call 6distributed delprefix '{"network": "'"$network"'"}'
191
192 # Restart Relay
193 restart_master_relay "$network"
194
195 # Disable distribution
196 disable_downstream "$network"
197
198 # Disable relay
199 stop_relay "$network"
200
201 # Disable DHCPv6 client if enabled, state script will take care
202 stop_service /usr/sbin/odhcp6c "/var/run/ipv6-upstream-$network.pid"
203 }
204
205
206 enable_static() {
207 local network="$1"
208 local device="$2"
209
210 # Enable global forwarding
211 local global_forward
212 conf_get global_forward all forwarding
213 [ "$global_forward" != "1" ] && conf_set all forwarding 1
214
215 # Configure device
216 conf_set "$device" accept_ra 1
217 conf_set "$device" forwarding 1
218
219 # ULA-integration
220 local ula_prefix=""
221 config_get ula_prefix "$network" ula_prefix
222
223 # ULA auto configuration (first init)
224 [ "$ula_prefix" == "auto" ] && {
225 local r1=""
226 local r2=""
227 local r3=""
228
229 # Sometimes results are empty, therefore try until it works...
230 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
231 r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
232 r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
233 r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
234 done
235
236 ula_prefix="fd$r1:$r2:$r3::/48"
237
238 # Save prefix so it will be preserved across reboots
239 uci set network6.$network.ula_prefix=$ula_prefix
240 uci commit network6
241 }
242
243 # Announce ULA
244 [ -n "$ula_prefix" ] && announce_prefix $ula_prefix $network
245
246 # Announce all static prefixes
247 config_list_foreach "$network" static_prefix announce_prefix $network
248 }
249
250
251 enable_downstream() {
252 local network="$1"
253 local device="$2"
254
255 # Get IPv6 prefixes
256 local length
257 config_get length "$network" advertise_prefix
258 [ -z "$length" ] && length=64
259 [ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
260
261 # Start RD & DHCPv6 service
262 local pid="/var/run/ipv6-downstream-$network.pid"
263 start_service "/usr/sbin/6relayd -Rserver -Dserver . $device" "$pid"
264
265 # Try relaying if necessary
266 restart_master_relay "$network"
267 }
268
269
270 enable_upstream() {
271 local network="$1"
272 local device="$2"
273
274 # Configure device
275 conf_set "$device" accept_ra 2
276 conf_set "$device" forwarding 2
277
278 # Trigger RS
279 conf_set "$device" disable_ipv6 1
280 conf_set "$device" disable_ipv6 0
281
282 # Configure DHCPv6-client
283 local dhcp6_opts="$device"
284
285 # Configure DHCPv6-client (e.g. requested prefix)
286 local request_prefix
287 config_get request_prefix "$network" request_prefix
288 [ -z "$request_prefix" ] && request_prefix="auto"
289 [ "$request_prefix" != "no" ] && {
290 [ "$request_prefix" == "auto" ] && request_prefix=0
291 dhcp6_opts="-P$request_prefix $dhcp6_opts"
292 }
293
294 # Start DHCPv6 client
295 local pid="/var/run/ipv6-upstream-$network.pid"
296 start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
297
298 # Refresh RA on all interfaces
299 for pid in /var/run/ipv6-downstream-*.pid; do
300 kill -SIGUSR1 $(cat "$pid")
301 done
302 }
303
304