summaryrefslogtreecommitdiff
path: root/package/network/ipv6/ipv6-support/files/support.sh
blob: 8a0e5404319e958c829c15e34613cf29c10fd0d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/bin/sh
# Copyright (c) 2012 OpenWrt.org
. /lib/functions.sh
. /lib/functions/service.sh
. /lib/functions/network.sh

config_load network6
local NAT="ip6tables -t nat"

conf_get() {
	local __return="$1"
	local __device="$2"
	local __option="$3"
	local __value=$(cat "/proc/sys/net/ipv6/conf/$device/$option")
	eval "$__return=$__value"
}


conf_set() {
	local device="$1"
	local option="$2"
	local value="$3"
	echo "$value" > "/proc/sys/net/ipv6/conf/$device/$option"
}


stop_service() {
	local __exe="$1"
	SERVICE_PID_FILE="$2"
	local __return="$3"

	service_check "$__exe" && {
		service_stop "$__exe"
		[ -n "$__return" ] && eval "$__return=1"
	}
	rm -f "$SERVICE_PID_FILE"
}


start_service() {
	local cmd="$1"
	local pidfile="$2"

	SERVICE_DAEMONIZE=1
	SERVICE_WRITE_PID=1
	SERVICE_PID_FILE="$pidfile"
	service_start $cmd
}


resolve_network_add() {
	local __section="$1"
	local __device="$2"
	local __return="$3"
	local __cdevice
	network_get_device __cdevice "$__section"
	[ "$__cdevice" != "$__device" ] && return
	
	eval "$__return"'="'"$__section"'"'
}


resolve_network() {
	local __return="$1"
	local __device="$2"
	config_foreach resolve_network_add interface "$__device" "$__return"
}


setup_masquerading() {
	local cmd="$1"
	local chain="network6_masquerade_$2"
	local device="$3"

	$NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
		$NAT -F "$chain" 2>/dev/null
		$NAT -X "$chain" 2>/dev/null
	}

	[ "$cmd" != "stop" ] && {
		$NAT -N "$chain"
		$NAT -A "$chain" -o "$device" -j MASQUERADE
		$NAT -A POSTROUTING -j "$chain"
	}
}


setup_npt_chain() {
	local cmd="$1"
	local network="$2"
	local chain="network6_npt_$network"

	[ "$cmd" != "start" ] && {
		$NAT -D POSTROUTING -j "$chain" 2>/dev/null && {
			$NAT -D PREROUTING -j "$chain" 2>/dev/null
			$NAT -F "$chain" 2>/dev/null
			$NAT -X "$chain" 2>/dev/null
		}
	}

	[ "$cmd" != "stop" ] && {
		$NAT -N "$chain" 2>/dev/null && {
			$NAT -A PREROUTING -j "$chain"
			$NAT -A POSTROUTING -j "$chain"
		}
	}
}


announce_prefix() {
	local prefix="$1"
	local network="$2"
	local cmd="$3"

	local addr=$(echo "$prefix" | cut -d/ -f1)
	local rem=$(echo "$prefix" | cut -d/ -f2)
	local length=$(echo "$rem" | cut -d, -f1)
	local prefer=""
	local valid=""

	# If preferred / valid provided
	[ "$rem" != "$length" ] && {
		prefer=$(echo "$rem" | cut -d, -f2)
		valid=$(echo "$rem" | cut -d, -f3)
	}

	# Get prefix configuration
	local ula=""
	local prefix_action=""
	config_get ula global ula_prefix
	config_get prefix_action "$network" prefix_action
	[ -z "$prefix_action" ] && prefix_action="distribute"

	# Always announce the ULA when doing NPT
	[ "$prefix" == "$ula" -a "$prefix_action" == "npt" ] && prefix_action="distribute"

	[ "$prefix_action" == "distribute" ] && {
		local msg='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
		[ -n "$valid" ] && msg="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
		[ -z "$cmd" ] && cmd=newprefix
	
		ubus call 6distributed "$cmd" "$msg}"
	}

	[ "$prefix_action" == "npt" ] && {
		local chain="network6_npt_$network"
		local ula_addr=$(echo "$ula" | cut -d/ -f1)
		local ula_rem=$(echo "$ula" | cut -d/ -f2)
		local ula_length=$(echo "$ula_rem" | cut -d, -f1)
		local device=""

		network_get_device device "$network"
		[ "$length" -lt "$ula_length" ] && length="$ula_length"
		[ "$cmd" == "delprefix" ] && cmd="-D $chain" || cmd="-A $chain"

		local in="-i $device -d $addr/$length -j NETMAP --to $ula_addr/$ula_length"
		local out="-o $device -s $ula_addr/$ula_length -j NETMAP --to $addr/$length"

		setup_npt_chain start "$network"
		$NAT $cmd $in
		$NAT $cmd $out
	}
}


disable_router() {
	local network="$1"

	# Notify the address distribution daemon
	ubus call 6distributed deliface '{"network": "'"$network"'"}'

	# Disable advertisement daemon
	stop_service /usr/sbin/6relayd "/var/run/ipv6-router-$network.pid"
}


restart_relay_slave() {
	local __section="$1"
	local __master="$2"

	network_is_up "$__section" || return

	local __device=""
	network_get_device __device "$__section"

	local __cmaster=""
	config_get __cmaster "$__section" relay_master

	[ "$__master" == "$__cmaster" ] && {
		disable_interface "$__section"
		enable_interface "$__section" "$__device"
	}
}


add_relay_slave() {
	local __section="$1"
	local __return="$2"
	local __master="$3"
	local __mode="$4"

	network_is_up "$__section" || return

	# Get device
	local __device=""
	network_get_device __device "$__section"

	# Match master network
	local __cmaster=""
	config_get __cmaster "$__section" relay_master
	[ "$__master" == "$__cmaster" ] || return
	
	# Test slave  mode
	local __cmode=""
	config_get __cmode "$__section" mode
	[ "$__cmode" == "downstream" ] && __cmode="router"

	# Don't start fallback interfaces if we are in forced-relay mode
	[ "$__cmode" == "relay" -o "$__mode" == "fallback" ] || return

	# Don't make non-relay or non-router interfaces slaves
	[ "$__cmode" == "relay" -o "$__cmode" == "router" ] || return

	# Disable any active distribution
	[ "$__cmode" == "router" ] && disable_router "$__section"

	# Configure interface to accept RA and send RS
	conf_set "$__device" accept_ra 2
	conf_set "$__device" forwarding 2

	eval "$__return"'="$'"$__return"' '"$__device"'"'
}


stop_relay() {
	local network="$1"
	local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
	local pid_forced="/var/run/ipv6-relay-forced-$network.pid"
	local was_fallback=""
	
	stop_service /usr/sbin/6relayd "$pid_fallback" was_fallback
	stop_service /usr/sbin/6relayd "$pid_forced"

	# Reenable normal distribution on slave interfaces	
	[ -n "$was_fallback" ] && config_foreach restart_relay_slave interface "$network"
}


detect_forced_relay_mode() {
	local __section="$1"
	local __mode="$2"

	local __cmode
	config_get __cmode "$__section" mode
	[ "$__cmode" == "relay" ] && eval "$__mode=forced"
}


restart_relay() {
	local network="$1"
	local mode="$2"

	# Stop last active relay
	stop_relay "$network"

	# Detect if we have a forced-relay
	[ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode

	# Don't start without a mode
	[ -z "$mode" ] && return

	# Detect master device
	local device=""
	network_get_device device "$network"

	# Generate command string
	local cmd="/usr/sbin/6relayd -A $device"
	local ifaces=""
	config_foreach add_relay_slave interface ifaces "$network" "$mode"

	# Start relay
	local pid="/var/run/ipv6-relay-$mode-$network.pid"
	[ -n "$ifaces" ] && start_service "$cmd $ifaces" "$pid"

	# There are no slave interface, however indicate that we want to relay
	[ -z "$ifaces" ] && touch "$pid"
}


setup_prefix_fallback() {
	local cmd="$1"
	local network="$2"
	local device="$3"

	stop_relay "$network"
	restart_relay "$network"

	setup_masquerading stop "$network"

	[ "$cmd" != "stop" ] && {
		local fallback=""
		config_get fallback "$network" prefix_fallback

		[ "$fallback" == "relay" ] && restart_relay "$network" fallback
		[ "$fallback" == "masquerade" ] && setup_masquerading start "$network" "$device"
	}
}


restart_master_relay() {
	local network="$1"
	local mode="$2"
	local pid_fallback="/var/run/ipv6-relay-fallback-$network.pid"
	local pid_forced="/var/run/ipv6-relay-forced-$network.pid"

	# Disable active relaying to this interface
	config_get relay_master "$network" relay_master
	[ -z "$relay_master" ] && return
	network_is_up "$relay_master" || return

	# Detect running mode
	[ -z "$mode" && -f "$pid_fallback" ] && mode="fallback"
	[ -z "$mode" && -f "$pid_forced" ] && mode="forced"

	# Restart relay if running or start requested
	[ -n "$mode" ] && restart_relay "$relay_master" "$mode"
}


disable_interface() {
	local network="$1"

	# Delete all prefixes routed to this interface
	ubus call 6distributed delprefix '{"network": "'"$network"'"}'

	# Restart Relay
	restart_master_relay "$network"

	# Disable distribution
	disable_router "$network"

	# Disable any active relays, masquerading rules and NPT rules
	stop_relay "$network"
	setup_masquerading stop "$network"
	setup_npt_chain stop "$network"

	# Disable DHCPv6 client if enabled, state script will take care
	stop_service /usr/sbin/odhcp6c "/var/run/ipv6-dhcpv6-$network.pid"
}


enable_ula_prefix() {
	local network="$1"
	local ula="$2"
	[ -z "$ula" ] && ula="global"

	# ULA-integration
	local ula_prefix=""
	config_get ula_prefix "$ula" ula_prefix

	# ULA auto configuration (first init)
	[ "$ula_prefix" == "auto" ] && {
		local r1=""
		local r2=""
		local r3=""

		# Sometimes results are empty, therefore try until it works...		
		while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
			r1=$(printf "%02x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 256)))
			r2=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
			r3=$(printf "%01x" $(($(</dev/urandom tr -dc 0-9 | dd bs=9 count=1) % 65536)))
		done
		
		ula_prefix="fd$r1:$r2:$r3::/48"

		# Save prefix so it will be preserved across reboots
		config_set "$ula" ula_prefix "$ula_prefix"
		uci_set network6 "$ula" ula_prefix "$ula_prefix"
		uci_commit network6
	}

	# Announce ULA
	[ -n "$ula_prefix" ] && announce_prefix "$ula_prefix" "$network"
}


enable_static() {
	local network="$1"
	local device="$2"

	# Enable global forwarding
	local global_forward
	conf_get global_forward all forwarding
	[ "$global_forward" != "1" ] && conf_set all forwarding 1

	# Configure device
	conf_set "$device" accept_ra 1
	conf_set "$device" forwarding 1

	# Enable ULA
	enable_ula_prefix "$network"
	# Compatibility (deprecated)
	enable_ula_prefix "$network" "$network"

	# Announce all static prefixes
	config_list_foreach "$network" static_prefix announce_prefix $network

	# start relay if there are forced relay members
	restart_relay "$network"
}


enable_router() {
	local network="$1"
	local device="$2"

	# Get IPv6 prefixes
	local length
	config_get length "$network" advertise_prefix
	[ -z "$length" ] && length=64
	[ "$length" -ne "0" ] && ubus call 6distributed newiface '{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'

	# Start RD & DHCPv6 service
	local pid="/var/run/ipv6-router-$network.pid"
	start_service "/usr/sbin/6relayd -S . $device" "$pid"

	# Try relaying if necessary
	restart_master_relay "$network"
}


enable_dhcpv6() {
	local network="$1"
	local device="$2"
	
	# Configure device
	conf_set "$device" accept_ra 2
	conf_set "$device" forwarding 2
	
	# Trigger RS
	conf_set "$device" disable_ipv6 1
	conf_set "$device" disable_ipv6 0

	# Configure DHCPv6-client
	local dhcp6_opts="$device"

	# Configure DHCPv6-client (e.g. requested prefix)
	local request_prefix
	config_get request_prefix "$network" request_prefix
	[ -z "$request_prefix" ] && request_prefix="auto"
	[ "$request_prefix" != "no" ] && {
		[ "$request_prefix" == "auto" ] && request_prefix=0
		dhcp6_opts="-P$request_prefix $dhcp6_opts"
	}
	
	# Start DHCPv6 client
	local pid="/var/run/ipv6-dhcpv6-$network.pid"
	start_service "/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"

	# Refresh RA on all interfaces
	for pid in /var/run/ipv6-router-*.pid; do
		kill -SIGUSR1 $(cat "$pid")
	done
}


enable_6to4() {
	local network="$1"
	local device="$2"
	local mode="$3"

	local prefixlen="48"
	[ "$mode" == "6rd" ] && {
		local ip4prefix=$(uci_get network "$network" ip4prefixlen 0)
		local ip6prefix=$(uci_get network "$network" ip6prefixlen 32)
		prefixlen=$(($ip6prefix + 32 - $ip4prefix))
	}

	local prefix=""
	network_get_ipaddr6 prefix "$network"

	announce_prefix "$prefix/$prefixlen" "$network"
}


enable_interface()
{
	local network="$1"
	local device="$2"
	local mode=""

	config_get mode "$network" mode
	[ -n "$mode" -a "$mode" != "none" ] || return

	# Compatibility with old mode names
	[ "$mode" == "downstream" ] && mode=router
	[ "$mode" == "upstream" ] && mode=dhcpv6

	# Run mode startup code
	enable_static "$network" "$device"
	[ "$mode" == "dhcpv6" ] && enable_dhcpv6 "$network" "$device"
	[ "$mode" == "router" ] && enable_router "$network" "$device"
	[ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4 "$network" "$device" "$mode"
	[ "$mode" == "relay" ] && restart_master_relay "$network" forced
}