2 # 6to4.sh - IPv6-in-IPv4 tunnel backend
3 # Copyright (c) 2010-2012 OpenWrt.org
5 [ -n "$INCLUDE_ONLY" ] ||
{
7 .
/lib
/functions
/network.sh
14 local oIFS
="$IFS"; IFS
="."; set -- $ip4; IFS
="$oIFS"
16 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
21 local oIFS
="$IFS"; IFS
="."; set -- $1; IFS
="$oIFS"
22 [ $1 -eq 10 ] && return 0
23 [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
24 [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
27 [ $1 -eq 100 ] && [ $2 -ge 64 ] && [ $2 -le 127 ] && return 0
32 set_6to4_radvd_interface
() {
34 local lanif
="${2:-lan}"
35 local ifmtu
="${3:-1280}"
41 config_get net
"$cfg" interface
43 [ "$net" = "$lanif" ] && {
49 config_foreach find_ifsection interface
51 [ -z "$ifsection" ] && {
52 ifsection
="iface_$sid"
53 uci_set_state radvd
"$ifsection" "" interface
54 uci_set_state radvd
"$ifsection" interface
"$lanif"
57 uci_set_state radvd
"$ifsection" ignore
0
58 uci_set_state radvd
"$ifsection" IgnoreIfMissing
1
59 uci_set_state radvd
"$ifsection" AdvSendAdvert
1
60 uci_set_state radvd
"$ifsection" MaxRtrAdvInterval
30
61 uci_set_state radvd
"$ifsection" AdvLinkMTU
"$ifmtu"
64 set_6to4_radvd_prefix
() {
66 local lanif
="${2:-lan}"
67 local wanif
="${3:-wan}"
68 local prefix
="${4:-0:0:0:1::/64}"
76 config_get net
"$cfg" interface
77 config_get base
"$cfg" Base6to4Interface
79 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
85 config_foreach find_pfxsection prefix
87 [ -z "$pfxsection" ] && {
88 pfxsection
="prefix_${sid}_${lanif}"
89 uci_set_state radvd
"$pfxsection" "" prefix
90 uci_set_state radvd
"$pfxsection" ignore
0
91 uci_set_state radvd
"$pfxsection" interface
"$lanif"
92 uci_set_state radvd
"$pfxsection" prefix
"$prefix"
93 uci_set_state radvd
"$pfxsection" AdvOnLink
1
94 uci_set_state radvd
"$pfxsection" AdvAutonomous
1
95 uci_set_state radvd
"$pfxsection" AdvValidLifetime
"$vlt"
96 uci_set_state radvd
"$pfxsection" AdvPreferredLifetime
"$plt"
97 uci_set_state radvd
"$pfxsection" Base6to4Interface
"$wanif"
104 local link
="6to4-$cfg"
106 local mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
107 json_get_vars mtu ttl ipaddr adv_subnet adv_interface adv_valid_lifetime adv_preferred_lifetime
109 ( proto_add_host_dependency
"$cfg" 0.0.0.0 )
111 [ -z "$ipaddr" ] && {
113 if ! network_find_wan wanif ||
! network_get_ipaddr ipaddr
"$wanif"; then
114 proto_notify_error
"$cfg" "NO_WAN_LINK"
119 test_6to4_rfc1918
"$ipaddr" && {
120 proto_notify_error
"$cfg" "INVALID_LOCAL_ADDRESS"
124 # find our local prefix
125 local prefix6
=$
(find_6to4_prefix
"$ipaddr")
126 local local6
="$prefix6::1"
128 proto_init_update
"$link" 1
129 proto_add_ipv6_address
"$local6" 16
130 proto_add_ipv6_route
"::" 0 "::192.88.99.1"
133 json_add_string mode sit
134 json_add_int mtu
"${mtu:-1280}"
135 json_add_int ttl
"${ttl:-64}"
136 json_add_string
local "$ipaddr"
139 proto_send_update
"$cfg"
141 [ -f /etc
/config
/radvd
] && /etc
/init.d
/radvd enabled
&& {
142 local sid
="6to4_$cfg"
144 uci_revert_state radvd
147 adv_subnet
=$
((0x
${adv_subnet:-1}))
151 for adv_interface
in ${adv_interface:-lan}; do
153 network_get_device adv_ifname
"${adv_interface:-lan}" ||
continue
155 local subnet6
="$(printf "%s
:%x
::1/64" "$prefix6" $adv_subnet)"
157 logger
-t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
158 ip
-6 addr add
$subnet6 dev
$adv_ifname
160 set_6to4_radvd_interface
"$sid" "$adv_interface" "$mtu"
161 set_6to4_radvd_prefix
"$sid" "$adv_interface" \
162 "$wancfg" "$(printf "0:0:0:%x
::/64" $adv_subnet)" \
163 "$adv_valid_lifetime" "$adv_preferred_lifetime"
165 adv_subnets
="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
166 adv_subnet
=$
(($adv_subnet + 1))
169 uci_set_state network
"$cfg" adv_subnets
"$adv_subnets"
171 /etc
/init.d
/radvd restart
175 proto_6to4_teardown
() {
177 local link
="6to4-$cfg"
179 local adv_subnets
=$
(uci_get_state network
"$cfg" adv_subnets
)
181 grep -qs "^ *$link:" /proc
/net
/dev
&& {
182 [ -n "$adv_subnets" ] && {
183 uci_revert_state radvd
184 /etc
/init.d
/radvd enabled
&& /etc
/init.d
/radvd restart
189 proto_6to4_init_config
() {
193 proto_config_add_string
"ipaddr"
194 proto_config_add_int
"mtu"
195 proto_config_add_int
"ttl"
196 proto_config_add_string
"adv_interface"
197 proto_config_add_string
"adv_subnet"
198 proto_config_add_int
"adv_valid_lifetime"
199 proto_config_add_int
"adv_preferred_lifetime"
202 [ -n "$INCLUDE_ONLY" ] ||
{