2 # Copyright (c) 2012 OpenWrt.org
4 .
/lib
/functions
/service.sh
5 .
/lib
/functions
/network.sh
8 local NAT
="ip6tables -t nat"
14 local __value
=$
(cat "/proc/sys/net/ipv6/conf/$device/$option")
15 eval "$__return=$__value"
23 echo "$value" > "/proc/sys/net/ipv6/conf/$device/$option"
32 service_check
"$__exe" && {
34 [ -n "$__return" ] && eval "$__return=1"
36 rm -f "$SERVICE_PID_FILE"
46 SERVICE_PID_FILE
="$pidfile"
51 resolve_network_add
() {
56 network_get_device __cdevice
"$__section"
57 [ "$__cdevice" != "$__device" ] && return
59 eval "$__return"'="'"$__section"'"'
66 config_foreach resolve_network_add interface
"$__device" "$__return"
70 setup_masquerading
() {
72 local chain
="network6_masquerade_$2"
75 $NAT -D POSTROUTING
-j "$chain" 2>/dev
/null
&& {
76 $NAT -F "$chain" 2>/dev
/null
77 $NAT -X "$chain" 2>/dev
/null
80 [ "$cmd" != "stop" ] && {
82 $NAT -A "$chain" -o "$device" -j MASQUERADE
83 $NAT -A POSTROUTING
-j "$chain"
91 local chain
="network6_npt_$network"
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
101 [ "$cmd" != "stop" ] && {
102 $NAT -N "$chain" 2>/dev
/null
&& {
103 $NAT -A PREROUTING
-j "$chain"
104 $NAT -A POSTROUTING
-j "$chain"
115 local addr
=$
(echo "$prefix" | cut
-d/ -f1)
116 local rem
=$
(echo "$prefix" | cut
-d/ -f2)
117 local length
=$
(echo "$rem" | cut
-d, -f1)
121 # If preferred / valid provided
122 [ "$rem" != "$length" ] && {
123 prefer
=$
(echo "$rem" | cut
-d, -f2)
124 valid
=$
(echo "$rem" | cut
-d, -f3)
127 # Get prefix configuration
129 local prefix_action
=""
130 config_get ula global ula_prefix
131 config_get prefix_action
"$network" prefix_action
132 [ -z "$prefix_action" ] && prefix_action
="distribute"
134 # Always announce the ULA when doing NPT
135 [ "$prefix" == "$ula" -a "$prefix_action" == "npt" ] && prefix_action
="distribute"
137 [ "$prefix_action" == "distribute" ] && {
138 local msg
='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
139 [ -n "$valid" ] && msg
="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
140 [ -z "$cmd" ] && cmd
=newprefix
142 ubus call
6distributed
"$cmd" "$msg}"
145 [ "$prefix_action" == "npt" ] && {
146 local chain
="network6_npt_$network"
147 local ula_addr
=$
(echo "$ula" | cut
-d/ -f1)
148 local ula_rem
=$
(echo "$ula" | cut
-d/ -f2)
149 local ula_length
=$
(echo "$ula_rem" | cut
-d, -f1)
152 network_get_device device
"$network"
153 [ "$length" -lt "$ula_length" ] && length
="$ula_length"
154 [ "$cmd" == "delprefix" ] && cmd
="-D $chain" || cmd
="-A $chain"
156 local in="-i $device -d $addr/$length -j NETMAP --to $ula_addr/$ula_length"
157 local out
="-o $device -s $ula_addr/$ula_length -j NETMAP --to $addr/$length"
159 setup_npt_chain start
"$network"
169 # Notify the address distribution daemon
170 ubus call
6distributed deliface
'{"network": "'"$network"'"}'
172 # Disable advertisement daemon
173 stop_service
/usr
/sbin
/6relayd
"/var/run/ipv6-router-$network.pid"
177 restart_relay_slave
() {
181 network_is_up
"$__section" ||
return
184 network_get_device __device
"$__section"
187 config_get __cmaster
"$__section" relay_master
189 [ "$__master" == "$__cmaster" ] && {
190 disable_interface
"$__section"
191 enable_interface
"$__section" "$__device"
202 network_is_up
"$__section" ||
return
206 network_get_device __device
"$__section"
208 # Match master network
210 config_get __cmaster
"$__section" relay_master
211 [ "$__master" == "$__cmaster" ] ||
return
215 config_get __cmode
"$__section" mode
216 [ "$__cmode" == "downstream" ] && __cmode
="router"
218 # Don't start fallback interfaces if we are in forced-relay mode
219 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] ||
return
221 # Don't make non-relay or non-router interfaces slaves
222 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] ||
return
224 # Disable any active distribution
225 [ "$__cmode" == "router" ] && disable_router
"$__section"
227 # Configure interface to accept RA and send RS
228 conf_set
"$__device" accept_ra
2
229 conf_set
"$__device" forwarding
2
231 eval "$__return"'="$'"$__return"' '"$__device"'"'
237 local pid_fallback
="/var/run/ipv6-relay-fallback-$network.pid"
238 local pid_forced
="/var/run/ipv6-relay-forced-$network.pid"
239 local was_fallback
=""
241 stop_service
/usr
/sbin
/6relayd
"$pid_fallback" was_fallback
242 stop_service
/usr
/sbin
/6relayd
"$pid_forced"
244 # Reenable normal distribution on slave interfaces
245 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface
"$network"
249 detect_forced_relay_mode
() {
254 config_get __cmode
"$__section" mode
255 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
263 # Stop last active relay
264 stop_relay
"$network"
266 # Detect if we have a forced-relay
267 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
269 # Don't start without a mode
270 [ -z "$mode" ] && return
272 # Detect master device
274 network_get_device device
"$network"
276 # Generate command string
277 local cmd
="/usr/sbin/6relayd -A $device"
279 config_foreach add_relay_slave interface ifaces
"$network" "$mode"
282 local pid
="/var/run/ipv6-relay-$mode-$network.pid"
283 [ -n "$ifaces" ] && start_service
"$cmd $ifaces" "$pid"
285 # There are no slave interface, however indicate that we want to relay
286 [ -z "$ifaces" ] && touch "$pid"
290 setup_prefix_fallback
() {
295 stop_relay
"$network"
296 restart_relay
"$network"
298 setup_masquerading stop
"$network"
300 [ "$cmd" != "stop" ] && {
302 config_get fallback
"$network" prefix_fallback
304 [ "$fallback" == "relay" ] && restart_relay
"$network" fallback
305 [ "$fallback" == "masquerade" ] && setup_masquerading start
"$network" "$device"
310 restart_master_relay
() {
313 local pid_fallback
="/var/run/ipv6-relay-fallback-$network.pid"
314 local pid_forced
="/var/run/ipv6-relay-forced-$network.pid"
316 # Disable active relaying to this interface
317 config_get relay_master
"$network" relay_master
318 [ -z "$relay_master" ] && return
319 network_is_up
"$relay_master" ||
return
321 # Detect running mode
322 [ -z "$mode" && -f "$pid_fallback" ] && mode
="fallback"
323 [ -z "$mode" && -f "$pid_forced" ] && mode
="forced"
325 # Restart relay if running or start requested
326 [ -n "$mode" ] && restart_relay
"$relay_master" "$mode"
330 disable_interface
() {
333 # Delete all prefixes routed to this interface
334 ubus call
6distributed delprefix
'{"network": "'"$network"'"}'
337 restart_master_relay
"$network"
339 # Disable distribution
340 disable_router
"$network"
342 # Disable any active relays, masquerading rules and NPT rules
343 stop_relay
"$network"
344 setup_masquerading stop
"$network"
345 setup_npt_chain stop
"$network"
347 # Disable DHCPv6 client if enabled, state script will take care
348 stop_service
/usr
/sbin
/odhcp6c
"/var/run/ipv6-dhcpv6-$network.pid"
352 enable_ula_prefix
() {
355 [ -z "$ula" ] && ula
="global"
359 config_get ula_prefix
"$ula" ula_prefix
361 # ULA auto configuration (first init)
362 [ "$ula_prefix" == "auto" ] && {
367 # Sometimes results are empty, therefore try until it works...
368 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
369 r1
=$
(printf "%02x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 256)))
370 r2
=$
(printf "%01x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 65536)))
371 r3
=$
(printf "%01x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 65536)))
374 ula_prefix
="fd$r1:$r2:$r3::/48"
376 # Save prefix so it will be preserved across reboots
377 config_set
"$ula" ula_prefix
"$ula_prefix"
378 uci_set network6
"$ula" ula_prefix
"$ula_prefix"
383 [ -n "$ula_prefix" ] && announce_prefix
"$ula_prefix" "$network"
391 # Enable global forwarding
393 conf_get global_forward all forwarding
394 [ "$global_forward" != "1" ] && conf_set all forwarding
1
397 conf_set
"$device" accept_ra
1
398 conf_set
"$device" forwarding
1
401 enable_ula_prefix
"$network"
402 # Compatibility (deprecated)
403 enable_ula_prefix
"$network" "$network"
405 # Announce all static prefixes
406 config_list_foreach
"$network" static_prefix announce_prefix
$network
408 # start relay if there are forced relay members
409 restart_relay
"$network"
419 config_get length
"$network" advertise_prefix
420 [ -z "$length" ] && length
=64
421 [ "$length" -ne "0" ] && ubus call
6distributed newiface
'{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
423 # Start RD & DHCPv6 service
424 local pid
="/var/run/ipv6-router-$network.pid"
425 start_service
"/usr/sbin/6relayd -S . $device" "$pid"
427 # Try relaying if necessary
428 restart_master_relay
"$network"
437 conf_set
"$device" accept_ra
2
438 conf_set
"$device" forwarding
2
441 conf_set
"$device" disable_ipv6
1
442 conf_set
"$device" disable_ipv6
0
444 # Configure DHCPv6-client
445 local dhcp6_opts
="$device"
447 # Configure DHCPv6-client (e.g. requested prefix)
449 config_get request_prefix
"$network" request_prefix
450 [ -z "$request_prefix" ] && request_prefix
="auto"
451 [ "$request_prefix" != "no" ] && {
452 [ "$request_prefix" == "auto" ] && request_prefix
=0
453 dhcp6_opts
="-P$request_prefix $dhcp6_opts"
456 # Start DHCPv6 client
457 local pid
="/var/run/ipv6-dhcpv6-$network.pid"
458 start_service
"/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
460 # Refresh RA on all interfaces
461 for pid
in /var
/run
/ipv6-router-
*.pid
; do
462 kill -SIGUSR1 $
(cat "$pid")
473 [ "$mode" == "6rd" ] && {
474 local ip4prefix
=$
(uci_get network
"$network" ip4prefixlen
0)
475 local ip6prefix
=$
(uci_get network
"$network" ip6prefixlen
32)
476 prefixlen
=$
(($ip6prefix + 32 - $ip4prefix))
480 network_get_ipaddr6 prefix
"$network"
482 announce_prefix
"$prefix/$prefixlen" "$network"
492 config_get mode
"$network" mode
493 [ -n "$mode" -a "$mode" != "none" ] ||
return
495 # Compatibility with old mode names
496 [ "$mode" == "downstream" ] && mode
=router
497 [ "$mode" == "upstream" ] && mode
=dhcpv6
499 # Run mode startup code
500 enable_static
"$network" "$device"
501 [ "$mode" == "dhcpv6" ] && enable_dhcpv6
"$network" "$device"
502 [ "$mode" == "router" ] && enable_router
"$network" "$device"
503 [ "$mode" == "6to4" -o "$mode" == "6rd" ] && enable_6to4
"$network" "$device" "$mode"
504 [ "$mode" == "relay" ] && restart_master_relay
"$network" forced