[package] 6to4: don't start if detected local IPv4 address is RFC1918, allows people...
[openwrt.git] / package / 6to4 / files / 6to4.sh
1 # 6to4.sh - IPv6-in-IPv4 tunnel backend
2 # Copyright (c) 2010-2011 OpenWrt.org
3
4 find_6to4_wanif() {
5 local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
6 [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
7 }
8
9 find_6to4_wanip() {
10 local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
11 echo "${ip%%[^0-9.]*}"
12 }
13
14 find_6to4_prefix() {
15 local ip4="$1"
16 local oIFS="$IFS"; IFS="."; set -- $ip4; IFS="$oIFS"
17
18 printf "2002:%02x%02x:%02x%02x\n" $1 $2 $3 $4
19 }
20
21 test_6to4_rfc1918()
22 {
23 local oIFS="$IFS"; IFS="."; set -- $1; IFS="$oIFS"
24 [ $1 -eq 10 ] && return 0
25 [ $1 -eq 192 ] && [ $2 -eq 168 ] && return 0
26 [ $1 -eq 172 ] && [ $2 -ge 16 ] && [ $2 -le 31 ] && return 0
27 return 1
28 }
29
30 set_6to4_radvd_interface() {
31 local cfgid="$1"
32 local lanif="${2:-lan}"
33 local ifsection=""
34
35 find_ifsection() {
36 local net
37 local cfg="$1"
38 config_get net "$cfg" interface
39
40 [ "$net" = "$lanif" ] && {
41 ifsection="$cfg"
42 return 1
43 }
44 }
45
46 config_foreach find_ifsection interface
47
48 [ -z "$ifsection" ] && {
49 ifsection="iface_$sid"
50 uci_set_state radvd "$ifsection" "" interface
51 uci_set_state radvd "$ifsection" interface "$lanif"
52 }
53
54 uci_set_state radvd "$ifsection" ignore 0
55 uci_set_state radvd "$ifsection" IgnoreIfMissing 1
56 uci_set_state radvd "$ifsection" AdvSendAdvert 1
57 uci_set_state radvd "$ifsection" MaxRtrAdvInterval 30
58 }
59
60 set_6to4_radvd_prefix() {
61 local cfgid="$1"
62 local lanif="${2:-lan}"
63 local wanif="${3:-wan}"
64 local prefix="${4:-0:0:0:1::/64}"
65 local pfxsection=""
66
67 find_pfxsection() {
68 local net base
69 local cfg="$1"
70 config_get net "$cfg" interface
71 config_get base "$cfg" Base6to4Interface
72
73 [ "$net" = "$lanif" ] && [ "$base" = "$wanif" ] && {
74 pfxsection="$cfg"
75 return 1
76 }
77 }
78
79 config_foreach find_pfxsection prefix
80
81 [ -z "$pfxsection" ] && {
82 pfxsection="prefix_${sid}_${lanif}"
83 uci_set_state radvd "$pfxsection" "" prefix
84 uci_set_state radvd "$pfxsection" ignore 0
85 uci_set_state radvd "$pfxsection" interface "$lanif"
86 uci_set_state radvd "$pfxsection" prefix "$prefix"
87 uci_set_state radvd "$pfxsection" AdvOnLink 1
88 uci_set_state radvd "$pfxsection" AdvAutonomous 1
89 uci_set_state radvd "$pfxsection" AdvValidLifetime 300
90 uci_set_state radvd "$pfxsection" AdvPreferredLifetime 120
91 uci_set_state radvd "$pfxsection" Base6to4Interface "$wanif"
92 }
93 }
94
95
96 # Hook into scan_interfaces() to synthesize a .device option
97 # This is needed for /sbin/ifup to properly dispatch control
98 # to setup_interface_6to4() even if no .ifname is set in
99 # the configuration.
100 scan_6to4() {
101 config_set "$1" device "6to4-$1"
102 }
103
104 coldplug_interface_6to4() {
105 setup_interface_6to4 "6to4-$1" "$1"
106 }
107
108 setup_interface_6to4() {
109 local iface="$1"
110 local cfg="$2"
111 local link="6to4-$cfg"
112
113 local local4=$(uci_get network "$cfg" ipaddr)
114
115 local mtu
116 config_get mtu "$cfg" mtu
117
118 local ttl
119 config_get ttl "$cfg" ttl
120
121 local metric
122 config_get metric "$cfg" metric
123
124 local defaultroute
125 config_get_bool defaultroute "$cfg" defaultroute 1
126
127 local wanif=$(find_6to4_wanif)
128 [ -z "$wanif" ] && {
129 logger -t "$link" "Cannot find wan interface - aborting"
130 return
131 }
132
133 local wancfg=$(find_config "$wanif")
134 [ -z "$wancfg" ] && {
135 logger -t "$link" "Cannot find wan network - aborting"
136 return
137 }
138
139 # If local4 is unset, guess local IPv4 address from the
140 # interface used by the default route.
141 [ -z "$local4" ] && {
142 [ -n "$wanif" ] && {
143 local4=$(find_6to4_wanip "$wanif")
144 uci_set_state network "$cfg" wan_device "$wanif"
145 }
146 }
147
148 test_6to4_rfc1918 "$local4" && {
149 logger -t "$link" "Local wan ip $local4 is private - aborting"
150 return
151 }
152
153 [ -n "$local4" ] && {
154 logger -t "$link" "Starting ..."
155
156 # creating the tunnel below will trigger a net subsystem event
157 # prevent it from touching or iface by disabling .auto here
158 uci_set_state network "$cfg" ifname $link
159 uci_set_state network "$cfg" auto 0
160
161 # find our local prefix
162 local prefix6=$(find_6to4_prefix "$local4")
163 local local6="$prefix6::1/16"
164
165 logger -t "$link" " * IPv4 address is $local4"
166 logger -t "$link" " * IPv6 address is $local6"
167 ip tunnel add $link mode sit remote any local $local4 ttl ${ttl:-64}
168 ip link set $link up
169 ip link set mtu ${mtu:-1280} dev $link
170 ip addr add $local6 dev $link
171
172 uci_set_state network "$cfg" ipaddr $local4
173 uci_set_state network "$cfg" ip6addr $local6
174
175 [ "$defaultroute" = 1 ] && {
176 logger -t "$link" " * Adding default route"
177 ip -6 route add ::/0 via ::192.88.99.1 metric ${metric:-1} dev $link
178 uci_set_state network "$cfg" defaultroute 1
179 }
180
181 [ -f /etc/config/radvd ] && /etc/init.d/radvd enabled && {
182 local sid="6to4_$cfg"
183
184 uci_revert_state radvd
185 config_load radvd
186
187 # find delegation target
188 local adv_interface
189 config_get adv_interface "$cfg" adv_interface
190
191 local adv_subnet=$(uci_get network "$cfg" adv_subnet)
192 adv_subnet=$((0x${adv_subnet:-1}))
193
194 local adv_subnets=""
195
196 for adv_interface in ${adv_interface:-lan}; do
197 local adv_ifname
198 config_get adv_ifname "${adv_interface:-lan}" ifname
199
200 grep -qs "^ *$adv_ifname:" /proc/net/dev && {
201 local subnet6="$(printf "%s:%x::1/64" "$prefix6" $adv_subnet)"
202
203 logger -t "$link" " * Advertising IPv6 subnet $subnet6 on ${adv_interface:-lan} ($adv_ifname)"
204 ip -6 addr add $subnet6 dev $adv_ifname
205
206 set_6to4_radvd_interface "$sid" "$adv_interface"
207 set_6to4_radvd_prefix "$sid" "$adv_interface" \
208 "$wancfg" "$(printf "0:0:0:%x::/64" $adv_subnet)"
209
210 adv_subnets="${adv_subnets:+$adv_subnets }$adv_ifname:$subnet6"
211 adv_subnet=$(($adv_subnet + 1))
212 }
213 done
214
215 uci_set_state network "$cfg" adv_subnets "$adv_subnets"
216
217 /etc/init.d/radvd restart
218 }
219
220 logger -t "$link" "... started"
221
222 env -i ACTION="ifup" INTERFACE="$cfg" DEVICE="$link" PROTO=6to4 /sbin/hotplug-call "iface" &
223 } || {
224 echo "Cannot determine local IPv4 address for 6to4 tunnel $cfg - skipping"
225 }
226 }
227
228 stop_interface_6to4() {
229 local cfg="$1"
230 local link="6to4-$cfg"
231
232 local local6=$(uci_get_state network "$cfg" ip6addr)
233 local defaultroute=$(uci_get_state network "$cfg" defaultroute)
234
235 local adv_subnets=$(uci_get_state network "$cfg" adv_subnets)
236
237 grep -qs "^ *$link:" /proc/net/dev && {
238 logger -t "$link" "Shutting down ..."
239 env -i ACTION="ifdown" INTERFACE="$cfg" DEVICE="$link" PROTO=6to4 /sbin/hotplug-call "iface" &
240
241 [ -n "$adv_subnets" ] && {
242 uci_revert_state radvd
243 /etc/init.d/radvd enabled && /etc/init.d/radvd restart
244
245 local adv_subnet
246 for adv_subnet in $adv_subnets; do
247 local ifname="${adv_subnet%%:*}"
248 local subnet="${adv_subnet#*:}"
249
250 logger -t "$link" " * Removing IPv6 subnet $subnet from interface $ifname"
251 ip -6 addr del $subnet dev $ifname
252 done
253 }
254
255 [ "$defaultroute" = "1" ] && {
256 ip -6 route del ::/0 via ::192.88.99.1 dev $link metric 1
257 }
258
259 ip addr del $local6 dev $link
260 ip link set $link down
261 ip tunnel del $link
262
263 logger -t "$link" "... stopped"
264 }
265 }
This page took 0.073596 seconds and 5 git commands to generate.