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