2 # Copyright (c) 2012 OpenWrt.org
4 .
/lib
/functions
/service.sh
5 .
/lib
/functions
/network.sh
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
() {
57 network_get_device __cdevice
"$__section"
58 [ "$__cdevice" != "$__device" ] && return
60 eval "$__return"'="'"$__section"'"'
67 config_foreach resolve_network_add interface
"$__device" "$__return"
76 local addr
=$
(echo "$prefix" | cut
-d/ -f1)
77 local rem
=$
(echo "$prefix" | cut
-d/ -f2)
78 local length
=$
(echo "$rem" | cut
-d, -f1)
82 # If preferred / valid provided
83 [ "$rem" != "$length" ] && {
84 prefer
=$
(echo "$rem" | cut
-d, -f2)
85 valid
=$
(echo "$rem" | cut
-d, -f3)
88 local msg
='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
89 [ -n "$valid" ] && msg
="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
90 [ -z "$cmd" ] && cmd
=newprefix
92 ubus call
6distributed
"$cmd" "$msg}"
99 # Notify the address distribution daemon
100 ubus call
6distributed deliface
'{"network": "'"$network"'"}'
102 # Disable advertisement daemon
103 stop_service
/usr
/sbin
/6relayd
"/var/run/ipv6-router-$network.pid"
107 restart_relay_slave
() {
111 network_is_up
"$__section" ||
return
114 network_get_device __device
"$__section"
117 config_get __cmaster
"$__section" relay_master
119 [ "$__master" == "$__cmaster" ] && {
120 disable_interface
"$__section"
121 enable_interface
"$__section" "$__device"
132 network_is_up
"$__section" ||
return
136 network_get_device __device
"$__section"
138 # Match master network
140 config_get __cmaster
"$__section" relay_master
141 [ "$__master" == "$__cmaster" ] ||
return
145 config_get __cmode
"$__section" mode
146 [ "$__cmode" == "downstream" ] && __cmode
="router"
148 # Don't start fallback interfaces if we are in forced-relay mode
149 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] ||
return
151 # Don't make non-relay or non-router interfaces slaves
152 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] ||
return
154 # Disable any active distribution
155 [ "$__cmode" == "router" ] && disable_router
"$__section"
157 eval "$__return"'="$'"$__return"' '"$__device"'"'
163 local pid_fallback
="/var/run/ipv6-relay-fallback-$network.pid"
164 local pid_forced
="/var/run/ipv6-relay-forced-$network.pid"
165 local was_fallback
=""
167 stop_service
/usr
/sbin
/6relayd
"$pid_fallback" was_fallback
168 stop_service
/usr
/sbin
/6relayd
"$pid_forced"
170 # Reenable normal distribution on slave interfaces
171 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface
"$network"
175 detect_forced_relay_mode
() {
180 config_get __cmode
"$__section" mode
181 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
189 # Stop last active relay
190 stop_relay
"$network"
192 # Detect if we have a forced-relay
193 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
195 # Don't start without a mode
196 [ -z "$mode" ] && return
198 # Detect master device
200 network_get_device device
"$network"
202 # Generate command string
203 local cmd
="/usr/sbin/6relayd -A $device"
205 config_foreach add_relay_slave interface ifaces
"$network" "$mode"
208 local pid
="/var/run/ipv6-relay-$mode-$network.pid"
209 [ -n "$ifaces" ] && start_service
"$cmd $ifaces" "$pid"
211 # There are no slave interface, however indicate that we want to relay
212 [ -z "$ifaces" ] && touch "$pid"
216 restart_master_relay
() {
219 local pid_fallback
="/var/run/ipv6-relay-fallback-$network.pid"
220 local pid_forced
="/var/run/ipv6-relay-forced-$network.pid"
222 # Disable active relaying to this interface
223 config_get relay_master
"$network" relay_master
224 [ -z "$relay_master" ] && return
225 network_is_up
"$relay_master" ||
return
227 # Detect running mode
228 [ -z "$mode" && -f "$pid_fallback" ] && mode
="fallback"
229 [ -z "$mode" && -f "$pid_forced" ] && mode
="forced"
231 # Restart relay if running or start requested
232 [ -n "$mode" ] && restart_relay
"$relay_master" "$mode"
236 disable_interface
() {
239 # Delete all prefixes routed to this interface
240 ubus call
6distributed delprefix
'{"network": "'"$network"'"}'
243 restart_master_relay
"$network"
245 # Disable distribution
246 disable_router
"$network"
249 stop_relay
"$network"
251 # Disable DHCPv6 client if enabled, state script will take care
252 stop_service
/usr
/sbin
/odhcp6c
"/var/run/ipv6-dhcpv6-$network.pid"
260 # Enable global forwarding
262 conf_get global_forward all forwarding
263 [ "$global_forward" != "1" ] && conf_set all forwarding
1
266 conf_set
"$device" accept_ra
1
267 conf_set
"$device" forwarding
1
271 config_get ula_prefix
"$network" ula_prefix
273 # ULA auto configuration (first init)
274 [ "$ula_prefix" == "auto" ] && {
279 # Sometimes results are empty, therefore try until it works...
280 while [ -z "$r1" -o -z "$r2" -o -z "$r3" ]; do
281 r1
=$
(printf "%02x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 256)))
282 r2
=$
(printf "%01x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 65536)))
283 r3
=$
(printf "%01x" $
(($
(</dev
/urandom
tr -dc 0-9 |
dd bs
=9 count
=1) % 65536)))
286 ula_prefix
="fd$r1:$r2:$r3::/48"
288 # Save prefix so it will be preserved across reboots
289 uci
set network6.
$network.ula_prefix
=$ula_prefix
294 [ -n "$ula_prefix" ] && announce_prefix
$ula_prefix $network
296 # Announce all static prefixes
297 config_list_foreach
"$network" static_prefix announce_prefix
$network
299 # start relay if there are forced relay members
300 restart_relay
"$network"
310 config_get length
"$network" advertise_prefix
311 [ -z "$length" ] && length
=64
312 [ "$length" -ne "0" ] && ubus call
6distributed newiface
'{"network": "'"$network"'", "iface": "'"$device"'", "length": '"$length"'}'
314 # Start RD & DHCPv6 service
315 local pid
="/var/run/ipv6-router-$network.pid"
316 start_service
"/usr/sbin/6relayd -Rserver -Dserver . $device" "$pid"
318 # Try relaying if necessary
319 restart_master_relay
"$network"
328 conf_set
"$device" accept_ra
2
329 conf_set
"$device" forwarding
2
332 conf_set
"$device" disable_ipv6
1
333 conf_set
"$device" disable_ipv6
0
335 # Configure DHCPv6-client
336 local dhcp6_opts
="$device"
338 # Configure DHCPv6-client (e.g. requested prefix)
340 config_get request_prefix
"$network" request_prefix
341 [ -z "$request_prefix" ] && request_prefix
="auto"
342 [ "$request_prefix" != "no" ] && {
343 [ "$request_prefix" == "auto" ] && request_prefix
=0
344 dhcp6_opts
="-P$request_prefix $dhcp6_opts"
347 # Start DHCPv6 client
348 local pid
="/var/run/ipv6-dhcpv6-$network.pid"
349 start_service
"/usr/sbin/odhcp6c -s/lib/ipv6/dhcpv6.sh $dhcp6_opts" "$pid"
351 # Refresh RA on all interfaces
352 for pid
in /var
/run
/ipv6-router-
*.pid
; do
353 kill -SIGUSR1 $
(cat "$pid")
363 config_get mode
"$network" mode
365 # Compatibility with old mode names
366 [ "$mode" == "downstream" ] && mode
=router
367 [ "$mode" == "upstream" ] && mode
=dhcpv6
369 # Run mode startup code
370 [ "$mode" == "dhcpv6" -o "$mode" == "static" ] && enable_static
"$network" "$device"
371 [ "$mode" == "dhcpv6" ] && enable_dhcpv6
"$network" "$device"
372 [ "$mode" == "router" ] && enable_router
"$network" "$device"
373 [ "$mode" == "relay" ] && restart_master_relay
"$network" forced