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}"
96 disable_downstream
() {
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-downstream-$network.pid"
107 restart_relay_add
() {
113 network_is_up
"$__section" ||
return
115 # Match master network
117 config_get __cmaster
"$__section" relay_master
118 [ "$__master" != "$__cmaster" ] && return
120 # Disable any active distribution
121 disable_downstream
"$__section"
124 network_get_device __device
"$__section"
126 # Coming from stop relay, reenable distribution
127 [ "$__disable" == "disable" ] && {
128 enable_downstream
"$__section" "$__device"
133 eval "$__return"'="$'"$__return"' '"$__device"'"'
139 local pid
="/var/run/ipv6-relay-$network.pid"
142 stop_service
/usr
/sbin
/6relayd
"$pid" was_running
144 # Reenable normal distribution on slave interfaces
145 [ -n "$was_running" ] && config_foreach restart_relay_add interface dummy
"$network" disable
152 local pid
="/var/run/ipv6-relay-$network.pid"
155 [ -f "$pid" ] || not_running
=1
157 # Don't start if not desired
158 [ "$force" != "1" ] && [ "$not_running" == "1" ] && return
160 # Kill current relay and distribution daemon
161 stop_relay
"$network"
163 # Detect master device
165 network_get_device device
$network
167 # Generate command string
168 local cmd
="/usr/sbin/6relayd -A $device "
169 config_foreach restart_relay_add interface cmd
"$network"
172 start_service
"$cmd" "$pid"
176 restart_master_relay
() {
179 # Disable active relaying to this interface
181 config_get relay_master
"$network" relay_master
182 [ -n "$relay_master" ] && restart_relay
"$relay_master"
186 disable_interface
() {
189 # Delete all prefixes routed to this interface
190 ubus call
6distributed delprefix
'{"network": "'"$network"'"}'
193 restart_master_relay
"$network"
195 # Disable distribution
196 disable_downstream
"$network"
199 stop_relay
"$network"
201 # Disable DHCPv6 client if enabled, state script will take care
202 stop_service
/usr
/sbin
/odhcp6c
"/var/run/ipv6-upstream-$network.pid"
210 # Enable global forwarding
212 conf_get global_forward all forwarding
213 [ "$global_forward" != "1" ] && conf_set all forwarding
1
216 conf_set
"$device" accept_ra
1
217 conf_set
"$device" forwarding
1
221 config_get ula_prefix
"$network" ula_prefix
223 # ULA auto configuration (first init)
224 [ "$ula_prefix" == "auto" ] && {
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)))
236 ula_prefix
="fd$r1:$r2:$r3::/48"
238 # Save prefix so it will be preserved across reboots
239 uci
set network6.
$network.ula_prefix
=$ula_prefix
244 [ -n "$ula_prefix" ] && announce_prefix
$ula_prefix $network
246 # Announce all static prefixes
247 config_list_foreach
"$network" static_prefix announce_prefix
$network
251 enable_downstream
() {
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"'}'
261 # Start RD & DHCPv6 service
262 local pid
="/var/run/ipv6-downstream-$network.pid"
263 start_service
"/usr/sbin/6relayd -Rserver -Dserver . $device" "$pid"
265 # Try relaying if necessary
266 restart_master_relay
"$network"
275 conf_set
"$device" accept_ra
2
276 conf_set
"$device" forwarding
2
279 conf_set
"$device" disable_ipv6
1
280 conf_set
"$device" disable_ipv6
0
282 # Configure DHCPv6-client
283 local dhcp6_opts
="$device"
285 # Configure DHCPv6-client (e.g. requested 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"
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"
298 # Refresh RA on all interfaces
299 for pid
in /var
/run
/ipv6-downstream-
*.pid
; do
300 kill -SIGUSR1 $
(cat "$pid")