2 # 6to4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] ||
{
12 local if=$
(ip
-4 r l e
0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
13 [ -n "$if" ] && grep -qs "^ *$if:" /proc
/net
/dev
&& echo "$if"
17 local ip
=$
(ip
-4 a s dev
"$1"); ip
="${ip#*inet }"
18 echo "${ip%%[^0-9.]*}"
23 local oIFS
="$IFS"; IFS
="."; set -- $ip4; IFS
="$oIFS"
25 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
30 local oIFS
="$IFS"; IFS
="."; set -- $1; IFS
="$oIFS"
31 [ $1 -eq 10 ] && return 0
32 [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
33 [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
36 [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0
41 set_6to4_radvd_interface
() {
43 local lanif
="${2:-lan}"
44 local ifmtu
="${3:-1280}"
50 config_get net
"$cfg" interface
52 [ "$net" = "$lanif" ] && {
58 config_foreach find_ifsection interface
60 [ -z "$ifsection" ] && {
61 ifsection
="iface_$sid"
62 uci_set_state radvd
"$ifsection" "" interface
63 uci_set_state radvd
"$ifsection" interface
"$lanif"
66 uci_set_state radvd
"$ifsection" ignore
0
67 uci_set_state radvd
"$ifsection" IgnoreIfMissing
1
68 uci_set_state radvd
"$ifsection" AdvSendAdvert
1
69 uci_set_state radvd
"$ifsection" MaxRtrAdvInterval
30
70 uci_set_state radvd
"$ifsection" AdvLinkMTU
"$ifmtu"
73 set_6to4_radvd_prefix
() {
75 local lanif
="${2:-lan}"
76 local wanif
="${3:-wan}"
77 local prefix
="${4:-0:0:0:1::/64}"
85 config_get net
"$cfg" interface
86 config_get base
"$cfg" Base6to4Interface
88 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
94 config_foreach find_pfxsection prefix
96 [ -z "$pfxsection" ] && {
97 pfxsection
="prefix_${sid}_${lanif}"
98 uci_set_state radvd
"$pfxsection" "" prefix
99 uci_set_state radvd
"$pfxsection" ignore
0
100 uci_set_state radvd
"$pfxsection" interface
"$lanif"
101 uci_set_state radvd
"$pfxsection" prefix
"$prefix"
102 uci_set_state radvd
"$pfxsection" AdvOnLink
1
103 uci_set_state radvd
"$pfxsection" AdvAutonomous
1
104 uci_set_state radvd
"$pfxsection" AdvValidLifetime
"$vlt"
105 uci_set_state radvd
"$pfxsection" AdvPreferredLifetime
"$plt"
106 uci_set_state radvd
"$pfxsection" Base6to4Interface
"$wanif"
111 local cfg
="$1"; shift;
113 [ -n "$1" ] && proto_notify_error
"$cfg" "$@"
114 proto_block_restart
"$cfg"
120 local link
="6to4-$cfg"
124 json_get_var local4 ipaddr
126 json_get_var adv_subnet adv_subnet
127 json_get_var adv_interface adv_interface
128 json_get_var adv_valid_lifetime adv_valid_lifetime
129 json_get_var adv_preferred_lifetime adv_preferred_lifetime
131 local wanif
=$
(find_6to4_wanif
)
133 tun_error
"$cfg" "NO_WAN_LINK"
137 .
/lib
/network
/config.sh
138 local wancfg
="$(find_config "$wanif")"
139 [ -z "$wancfg" ] && {
140 tun_error
"$cfg" "NO_WAN_LINK"
144 # If local4 is unset, guess local IPv4 address from the
145 # interface used by the default route.
146 [ -z "$local4" ] && {
147 [ -n "$wanif" ] && local4
=$
(find_6to4_wanip
"$wanif")
150 [ -z "$local4" ] && {
151 tun_error
"$cfg" "NO_WAN_LINK"
155 test_6to4_rfc1918
"$local4" && {
156 tun_error
"$cfg" "INVALID_LOCAL_ADDRESS"
160 # find our local prefix
161 local prefix6
=$
(find_6to4_prefix
"$local4")
162 local local6
="$prefix6::1"
164 proto_init_update
"$link" 1
165 proto_add_ipv6_address
"$local6" 16
166 proto_add_ipv6_route
"::" 0 "::192.88.99.1"
169 json_add_string mode sit
170 json_add_int mtu
"${mtu:-1280}"
171 json_add_int ttl
"${ttl:-64}"
172 json_add_string
local "$local4"
175 proto_send_update
"$cfg"
177 [ -f /etc
/config
/radvd
] && /etc
/init.d
/radvd enabled
&& {
178 local sid
="6to4_$cfg"
180 uci_revert_state radvd
183 adv_subnet
=$
((0x
${adv_subnet:-1}))
187 for adv_interface
in ${adv_interface:-lan}; do
189 config_get adv_ifname
"${adv_interface:-lan}" ifname
191 grep -qs "^ *$adv_ifname:" /proc
/net
/dev
&& {
192 local subnet6
="$(printf "%s
:%x
::1/64" "$prefix6" $adv_subnet)"
194 logger
-t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
195 ip
-6 addr add
$subnet6 dev
$adv_ifname
197 set_6to4_radvd_interface
"$sid" "$adv_interface" "$mtu"
198 set_6to4_radvd_prefix
"$sid" "$adv_interface" \
199 "$wancfg" "$(printf "0:0:0:%x
::/64" $adv_subnet)" \
200 "$adv_valid_lifetime" "$adv_preferred_lifetime"
202 adv_subnets
="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
203 adv_subnet
=$
(($adv_subnet + 1))
207 uci_set_state network
"$cfg" adv_subnets
"$adv_subnets"
209 /etc
/init.d
/radvd restart
213 proto_6to4_teardown
() {
215 local link
="6to4-$cfg"
217 local adv_subnets
=$
(uci_get_state network
"$cfg" adv_subnets
)
219 grep -qs "^ *$link:" /proc
/net
/dev
&& {
220 [ -n "$adv_subnets" ] && {
221 uci_revert_state radvd
222 /etc
/init.d
/radvd enabled
&& /etc
/init.d
/radvd restart
227 proto_6to4_init_config
() {
231 proto_config_add_string
"ipaddr"
232 proto_config_add_int
"mtu"
233 proto_config_add_int
"ttl"
234 proto_config_add_string
"adv_interface"
235 proto_config_add_string
"adv_subnet"
236 proto_config_add_int
"adv_valid_lifetime"
237 proto_config_add_int
"adv_preferred_lifetime"
240 [ -n "$INCLUDE_ONLY" ] ||
{