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
() {
56 network_get_device __cdevice
"$__section"
57 [ "$__cdevice" != "$__device" ] && return
59 eval "$__return"'="'"$__section"'"'
66 config_foreach resolve_network_add interface
"$__device" "$__return"
75 local addr
=$
(echo "$prefix" | cut
-d/ -f1)
76 local rem
=$
(echo "$prefix" | cut
-d/ -f2)
77 local length
=$
(echo "$rem" | cut
-d, -f1)
81 # If preferred / valid provided
82 [ "$rem" != "$length" ] && {
83 prefer
=$
(echo "$rem" | cut
-d, -f2)
84 valid
=$
(echo "$rem" | cut
-d, -f3)
87 local msg
='{"network": "'"$network"'", "prefix": "'"$addr"'", "length": '"$length"
88 [ -n "$valid" ] && msg
="$msg"', "valid": '"$valid"', "preferred": '"$prefer"
89 [ -z "$cmd" ] && cmd
=newprefix
91 ubus call
6distributed
"$cmd" "$msg}"
98 # Notify the address distribution daemon
99 ubus call
6distributed deliface
'{"network": "'"$network"'"}'
101 # Disable advertisement daemon
102 stop_service
/usr
/sbin
/6relayd
"/var/run/ipv6-router-$network.pid"
106 restart_relay_slave
() {
110 network_is_up
"$__section" ||
return
113 network_get_device __device
"$__section"
116 config_get __cmaster
"$__section" relay_master
118 [ "$__master" == "$__cmaster" ] && {
119 disable_interface
"$__section"
120 enable_interface
"$__section" "$__device"
131 network_is_up
"$__section" ||
return
135 network_get_device __device
"$__section"
137 # Match master network
139 config_get __cmaster
"$__section" relay_master
140 [ "$__master" == "$__cmaster" ] ||
return
144 config_get __cmode
"$__section" mode
145 [ "$__cmode" == "downstream" ] && __cmode
="router"
147 # Don't start fallback interfaces if we are in forced-relay mode
148 [ "$__cmode" == "relay" -o "$__mode" == "fallback" ] ||
return
150 # Don't make non-relay or non-router interfaces slaves
151 [ "$__cmode" == "relay" -o "$__cmode" == "router" ] ||
return
153 # Disable any active distribution
154 [ "$__cmode" == "router" ] && disable_router
"$__section"
156 # Configure interface to accept RA and send RS
157 conf_set
"$__device" accept_ra
2
158 conf_set
"$__device" forwarding
2
160 eval "$__return"'="$'"$__return"' '"$__device"'"'
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
=""
170 stop_service
/usr
/sbin
/6relayd
"$pid_fallback" was_fallback
171 stop_service
/usr
/sbin
/6relayd
"$pid_forced"
173 # Reenable normal distribution on slave interfaces
174 [ -n "$was_fallback" ] && config_foreach restart_relay_slave interface
"$network"
178 detect_forced_relay_mode
() {
183 config_get __cmode
"$__section" mode
184 [ "$__cmode" == "relay" ] && eval "$__mode=forced"
192 # Stop last active relay
193 stop_relay
"$network"
195 # Detect if we have a forced-relay
196 [ -z "$mode" ] && config_foreach detect_forced_relay_mode interface mode
198 # Don't start without a mode
199 [ -z "$mode" ] && return
201 # Detect master device
203 network_get_device device
"$network"
205 # Generate command string
206 local cmd
="/usr/sbin/6relayd -A $device"
208 config_foreach add_relay_slave interface ifaces
"$network" "$mode"
211 local pid
="/var/run/ipv6-relay-$mode-$network.pid"
212 [ -n "$ifaces" ] && start_service
"$cmd $ifaces" "$pid"
214 # There are no slave interface, however indicate that we want to relay
215 [ -z "$ifaces" ] && touch "$pid"
219 restart_master_relay
() {
222 local pid_fallback
="/var/run/ipv6-relay-fallback-$network.pid"
223 local pid_forced
="/var/run/ipv6-relay-forced-$network.pid"
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
230 # Detect running mode
231 [ -z "$mode" && -f "$pid_fallback" ] && mode
="fallback"
232 [ -z "$mode" && -f "$pid_forced" ] && mode
="forced"
234 # Restart relay if running or start requested
235 [ -n "$mode" ] && restart_relay
"$relay_master" "$mode"
239 disable_interface
() {
242 # Delete all prefixes routed to this interface
243 ubus call
6distributed delprefix
'{"network": "'"$network"'"}'
246 restart_master_relay
"$network"
248 # Disable distribution
249 disable_router
"$network"
252 stop_relay
"$network"
254 # Disable DHCPv6 client if enabled, state script will take care
255 stop_service
/usr
/sbin
/odhcp6c
"/var/run/ipv6-dhcpv6-$network.pid"
263 # Enable global forwarding
265 conf_get global_forward all forwarding
266 [ "$global_forward" != "1" ] && conf_set all forwarding
1
269 conf_set
"$device" accept_ra
1
270 conf_set
"$device" forwarding
1
274 config_get ula_prefix
"$network" ula_prefix
276 # ULA auto configuration (first init)
277 [ "$ula_prefix" == "auto" ] && {
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)))
289 ula_prefix
="fd$r1:$r2:$r3::/48"
291 # Save prefix so it will be preserved across reboots
292 uci_set network6
"$network" ula_prefix
"$ula_prefix"
297 [ -n "$ula_prefix" ] && announce_prefix
$ula_prefix $network
299 # Announce all static prefixes
300 config_list_foreach
"$network" static_prefix announce_prefix
$network
302 # start relay if there are forced relay members
303 restart_relay
"$network"
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"'}'
317 # Start RD & DHCPv6 service
318 local pid
="/var/run/ipv6-router-$network.pid"
321 start_service
"/usr/sbin/6relayd -S . $device" "$pid"
323 # Try relaying if necessary
324 restart_master_relay
"$network"
326 # start relay if there are forced relay members
327 restart_relay
"$network"
336 conf_set
"$device" accept_ra
2
337 conf_set
"$device" forwarding
2
340 conf_set
"$device" disable_ipv6
1
341 conf_set
"$device" disable_ipv6
0
343 # Configure DHCPv6-client
344 local dhcp6_opts
="$device"
346 # Configure DHCPv6-client (e.g. requested 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"
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"
359 # Refresh RA on all interfaces
360 for pid
in /var
/run
/ipv6-router-
*.pid
; do
361 kill -SIGUSR1 $
(cat "$pid")
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))
379 network_get_ipaddr6 prefix
"$network"
381 announce_prefix
"$prefix/$prefixlen" "$network"
390 config_get mode
"$network" mode
392 # Compatibility with old mode names
393 [ "$mode" == "downstream" ] && mode
=router
394 [ "$mode" == "upstream" ] && mode
=dhcpv6
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