Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
c4d3efaf | 2 | * IPv6 tunneling device |
1da177e4 LT |
3 | * Linux INET6 implementation |
4 | * | |
5 | * Authors: | |
1ab1457c | 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
c4d3efaf | 7 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
1da177e4 | 8 | * |
1da177e4 | 9 | * Based on: |
c4d3efaf | 10 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
1da177e4 LT |
11 | * |
12 | * RFC 2473 | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License | |
16 | * as published by the Free Software Foundation; either version | |
17 | * 2 of the License, or (at your option) any later version. | |
18 | * | |
19 | */ | |
20 | ||
f3213831 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
1da177e4 | 23 | #include <linux/module.h> |
4fc268d2 | 24 | #include <linux/capability.h> |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/types.h> | |
27 | #include <linux/sockios.h> | |
c4d3efaf | 28 | #include <linux/icmp.h> |
1da177e4 LT |
29 | #include <linux/if.h> |
30 | #include <linux/in.h> | |
31 | #include <linux/ip.h> | |
1da177e4 LT |
32 | #include <linux/net.h> |
33 | #include <linux/in6.h> | |
34 | #include <linux/netdevice.h> | |
35 | #include <linux/if_arp.h> | |
36 | #include <linux/icmpv6.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/route.h> | |
39 | #include <linux/rtnetlink.h> | |
40 | #include <linux/netfilter_ipv6.h> | |
5a0e3ad6 | 41 | #include <linux/slab.h> |
ddbe5032 | 42 | #include <linux/hash.h> |
e837735e | 43 | #include <linux/etherdevice.h> |
1da177e4 LT |
44 | |
45 | #include <asm/uaccess.h> | |
60063497 | 46 | #include <linux/atomic.h> |
1da177e4 | 47 | |
c4d3efaf | 48 | #include <net/icmp.h> |
1da177e4 | 49 | #include <net/ip.h> |
c5441932 | 50 | #include <net/ip_tunnels.h> |
1da177e4 | 51 | #include <net/ipv6.h> |
1da177e4 LT |
52 | #include <net/ip6_route.h> |
53 | #include <net/addrconf.h> | |
54 | #include <net/ip6_tunnel.h> | |
55 | #include <net/xfrm.h> | |
56 | #include <net/dsfield.h> | |
57 | #include <net/inet_ecn.h> | |
13eeb8e9 PE |
58 | #include <net/net_namespace.h> |
59 | #include <net/netns/generic.h> | |
1da177e4 LT |
60 | |
61 | MODULE_AUTHOR("Ville Nuorvala"); | |
c4d3efaf | 62 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
1da177e4 | 63 | MODULE_LICENSE("GPL"); |
f98f89a0 | 64 | MODULE_ALIAS_RTNL_LINK("ip6tnl"); |
6dfbd87a | 65 | MODULE_ALIAS_NETDEV("ip6tnl0"); |
1da177e4 | 66 | |
ddbe5032 ED |
67 | #define HASH_SIZE_SHIFT 5 |
68 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) | |
1da177e4 | 69 | |
f4e0b4c5 ND |
70 | static bool log_ecn_error = true; |
71 | module_param(log_ecn_error, bool, 0644); | |
72 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
73 | ||
ddbe5032 ED |
74 | static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) |
75 | { | |
76 | u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); | |
77 | ||
78 | return hash_32(hash, HASH_SIZE_SHIFT); | |
79 | } | |
1da177e4 | 80 | |
8560f226 | 81 | static int ip6_tnl_dev_init(struct net_device *dev); |
3144581c | 82 | static void ip6_tnl_dev_setup(struct net_device *dev); |
c075b130 | 83 | static struct rtnl_link_ops ip6_link_ops __read_mostly; |
1da177e4 | 84 | |
f99189b1 | 85 | static int ip6_tnl_net_id __read_mostly; |
13eeb8e9 | 86 | struct ip6_tnl_net { |
15820e12 PE |
87 | /* the IPv6 tunnel fallback device */ |
88 | struct net_device *fb_tnl_dev; | |
3e6c9fb5 | 89 | /* lists for storing tunnels in use */ |
94767632 ED |
90 | struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE]; |
91 | struct ip6_tnl __rcu *tnls_wc[1]; | |
92 | struct ip6_tnl __rcu **tnls[2]; | |
13eeb8e9 PE |
93 | }; |
94 | ||
8560f226 ED |
95 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
96 | { | |
56a4342d | 97 | struct pcpu_sw_netstats tmp, sum = { 0 }; |
8560f226 ED |
98 | int i; |
99 | ||
100 | for_each_possible_cpu(i) { | |
abb6013c | 101 | unsigned int start; |
8f84985f LR |
102 | const struct pcpu_sw_netstats *tstats = |
103 | per_cpu_ptr(dev->tstats, i); | |
8560f226 | 104 | |
abb6013c | 105 | do { |
57a7744e | 106 | start = u64_stats_fetch_begin_irq(&tstats->syncp); |
abb6013c LR |
107 | tmp.rx_packets = tstats->rx_packets; |
108 | tmp.rx_bytes = tstats->rx_bytes; | |
109 | tmp.tx_packets = tstats->tx_packets; | |
110 | tmp.tx_bytes = tstats->tx_bytes; | |
57a7744e | 111 | } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); |
abb6013c LR |
112 | |
113 | sum.rx_packets += tmp.rx_packets; | |
114 | sum.rx_bytes += tmp.rx_bytes; | |
115 | sum.tx_packets += tmp.tx_packets; | |
116 | sum.tx_bytes += tmp.tx_bytes; | |
8560f226 ED |
117 | } |
118 | dev->stats.rx_packets = sum.rx_packets; | |
119 | dev->stats.rx_bytes = sum.rx_bytes; | |
120 | dev->stats.tx_packets = sum.tx_packets; | |
121 | dev->stats.tx_bytes = sum.tx_bytes; | |
122 | return &dev->stats; | |
123 | } | |
124 | ||
2922bc8a | 125 | /* |
94767632 | 126 | * Locking : hash tables are protected by RCU and RTNL |
2922bc8a | 127 | */ |
1da177e4 | 128 | |
cdf3464e MKL |
129 | static void __ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst, |
130 | struct dst_entry *dst) | |
1da177e4 | 131 | { |
cdf3464e MKL |
132 | dst_release(idst->dst); |
133 | if (dst) { | |
134 | dst_hold(dst); | |
135 | idst->cookie = rt6_get_cookie((struct rt6_info *)dst); | |
136 | } else { | |
137 | idst->cookie = 0; | |
1da177e4 | 138 | } |
cdf3464e MKL |
139 | idst->dst = dst; |
140 | } | |
141 | ||
142 | static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst, | |
143 | struct dst_entry *dst) | |
144 | { | |
1da177e4 | 145 | |
cdf3464e MKL |
146 | spin_lock_bh(&idst->lock); |
147 | __ip6_tnl_per_cpu_dst_set(idst, dst); | |
148 | spin_unlock_bh(&idst->lock); | |
149 | } | |
150 | ||
151 | struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t) | |
152 | { | |
153 | struct ip6_tnl_dst *idst; | |
154 | struct dst_entry *dst; | |
155 | ||
156 | idst = raw_cpu_ptr(t->dst_cache); | |
157 | spin_lock_bh(&idst->lock); | |
158 | dst = idst->dst; | |
159 | if (dst) { | |
160 | if (!dst->obsolete || dst->ops->check(dst, idst->cookie)) { | |
161 | dst_hold(idst->dst); | |
162 | } else { | |
163 | __ip6_tnl_per_cpu_dst_set(idst, NULL); | |
164 | dst = NULL; | |
165 | } | |
166 | } | |
167 | spin_unlock_bh(&idst->lock); | |
1da177e4 LT |
168 | return dst; |
169 | } | |
f230d1e8 | 170 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_get); |
1da177e4 | 171 | |
c12b395a | 172 | void ip6_tnl_dst_reset(struct ip6_tnl *t) |
1da177e4 | 173 | { |
cdf3464e MKL |
174 | int i; |
175 | ||
176 | for_each_possible_cpu(i) | |
177 | ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), NULL); | |
1da177e4 | 178 | } |
c12b395a | 179 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset); |
1da177e4 | 180 | |
f230d1e8 | 181 | void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst) |
1da177e4 | 182 | { |
cdf3464e MKL |
183 | ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst); |
184 | ||
1da177e4 | 185 | } |
f230d1e8 | 186 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_set); |
1da177e4 | 187 | |
cdf3464e MKL |
188 | void ip6_tnl_dst_destroy(struct ip6_tnl *t) |
189 | { | |
190 | if (!t->dst_cache) | |
191 | return; | |
192 | ||
193 | ip6_tnl_dst_reset(t); | |
194 | free_percpu(t->dst_cache); | |
195 | } | |
196 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy); | |
197 | ||
198 | int ip6_tnl_dst_init(struct ip6_tnl *t) | |
199 | { | |
200 | int i; | |
201 | ||
202 | t->dst_cache = alloc_percpu(struct ip6_tnl_dst); | |
203 | if (!t->dst_cache) | |
204 | return -ENOMEM; | |
205 | ||
206 | for_each_possible_cpu(i) | |
207 | spin_lock_init(&per_cpu_ptr(t->dst_cache, i)->lock); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_init); | |
212 | ||
1da177e4 | 213 | /** |
3144581c | 214 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
1ab1457c YH |
215 | * @remote: the address of the tunnel exit-point |
216 | * @local: the address of the tunnel entry-point | |
1da177e4 | 217 | * |
1ab1457c | 218 | * Return: |
1da177e4 | 219 | * tunnel matching given end-points if found, |
1ab1457c | 220 | * else fallback tunnel if its device is up, |
1da177e4 LT |
221 | * else %NULL |
222 | **/ | |
223 | ||
2922bc8a ED |
224 | #define for_each_ip6_tunnel_rcu(start) \ |
225 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) | |
226 | ||
1da177e4 | 227 | static struct ip6_tnl * |
b71d1d42 | 228 | ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local) |
1da177e4 | 229 | { |
ddbe5032 | 230 | unsigned int hash = HASH(remote, local); |
1da177e4 | 231 | struct ip6_tnl *t; |
3e6c9fb5 | 232 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
ea3dc960 | 233 | struct in6_addr any; |
1da177e4 | 234 | |
ddbe5032 | 235 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
1da177e4 LT |
236 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
237 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
238 | (t->dev->flags & IFF_UP)) | |
239 | return t; | |
240 | } | |
ea3dc960 SK |
241 | |
242 | memset(&any, 0, sizeof(any)); | |
243 | hash = HASH(&any, local); | |
244 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { | |
245 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
246 | (t->dev->flags & IFF_UP)) | |
247 | return t; | |
248 | } | |
249 | ||
250 | hash = HASH(remote, &any); | |
251 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { | |
252 | if (ipv6_addr_equal(remote, &t->parms.raddr) && | |
253 | (t->dev->flags & IFF_UP)) | |
254 | return t; | |
255 | } | |
256 | ||
2922bc8a ED |
257 | t = rcu_dereference(ip6n->tnls_wc[0]); |
258 | if (t && (t->dev->flags & IFF_UP)) | |
1da177e4 LT |
259 | return t; |
260 | ||
261 | return NULL; | |
262 | } | |
263 | ||
264 | /** | |
3144581c | 265 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
1ab1457c | 266 | * @p: parameters containing tunnel end-points |
1da177e4 LT |
267 | * |
268 | * Description: | |
3144581c | 269 | * ip6_tnl_bucket() returns the head of the list matching the |
1da177e4 LT |
270 | * &struct in6_addr entries laddr and raddr in @p. |
271 | * | |
1ab1457c | 272 | * Return: head of IPv6 tunnel list |
1da177e4 LT |
273 | **/ |
274 | ||
94767632 | 275 | static struct ip6_tnl __rcu ** |
c12b395a | 276 | ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) |
1da177e4 | 277 | { |
b71d1d42 ED |
278 | const struct in6_addr *remote = &p->raddr; |
279 | const struct in6_addr *local = &p->laddr; | |
95c96174 | 280 | unsigned int h = 0; |
1da177e4 LT |
281 | int prio = 0; |
282 | ||
283 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { | |
284 | prio = 1; | |
ddbe5032 | 285 | h = HASH(remote, local); |
1da177e4 | 286 | } |
3e6c9fb5 | 287 | return &ip6n->tnls[prio][h]; |
1da177e4 LT |
288 | } |
289 | ||
290 | /** | |
3144581c | 291 | * ip6_tnl_link - add tunnel to hash table |
1da177e4 LT |
292 | * @t: tunnel to be added |
293 | **/ | |
294 | ||
295 | static void | |
2dd02c89 | 296 | ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 297 | { |
94767632 | 298 | struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); |
1da177e4 | 299 | |
cf778b00 ED |
300 | rcu_assign_pointer(t->next , rtnl_dereference(*tp)); |
301 | rcu_assign_pointer(*tp, t); | |
1da177e4 LT |
302 | } |
303 | ||
304 | /** | |
3144581c | 305 | * ip6_tnl_unlink - remove tunnel from hash table |
1da177e4 LT |
306 | * @t: tunnel to be removed |
307 | **/ | |
308 | ||
309 | static void | |
2dd02c89 | 310 | ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 311 | { |
94767632 ED |
312 | struct ip6_tnl __rcu **tp; |
313 | struct ip6_tnl *iter; | |
314 | ||
315 | for (tp = ip6_tnl_bucket(ip6n, &t->parms); | |
316 | (iter = rtnl_dereference(*tp)) != NULL; | |
317 | tp = &iter->next) { | |
318 | if (t == iter) { | |
cf778b00 | 319 | rcu_assign_pointer(*tp, t->next); |
1da177e4 LT |
320 | break; |
321 | } | |
322 | } | |
323 | } | |
324 | ||
8560f226 ED |
325 | static void ip6_dev_free(struct net_device *dev) |
326 | { | |
cdf3464e MKL |
327 | struct ip6_tnl *t = netdev_priv(dev); |
328 | ||
329 | ip6_tnl_dst_destroy(t); | |
8560f226 ED |
330 | free_percpu(dev->tstats); |
331 | free_netdev(dev); | |
332 | } | |
333 | ||
0b112457 ND |
334 | static int ip6_tnl_create2(struct net_device *dev) |
335 | { | |
336 | struct ip6_tnl *t = netdev_priv(dev); | |
337 | struct net *net = dev_net(dev); | |
338 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
339 | int err; | |
340 | ||
341 | t = netdev_priv(dev); | |
0b112457 ND |
342 | |
343 | err = register_netdevice(dev); | |
344 | if (err < 0) | |
345 | goto out; | |
346 | ||
347 | strcpy(t->parms.name, dev->name); | |
348 | dev->rtnl_link_ops = &ip6_link_ops; | |
349 | ||
350 | dev_hold(dev); | |
351 | ip6_tnl_link(ip6n, t); | |
352 | return 0; | |
353 | ||
354 | out: | |
355 | return err; | |
356 | } | |
357 | ||
1da177e4 | 358 | /** |
2c53040f | 359 | * ip6_tnl_create - create a new tunnel |
1da177e4 LT |
360 | * @p: tunnel parameters |
361 | * @pt: pointer to new tunnel | |
362 | * | |
363 | * Description: | |
364 | * Create tunnel matching given parameters. | |
1ab1457c YH |
365 | * |
366 | * Return: | |
37355565 | 367 | * created tunnel or error pointer |
1da177e4 LT |
368 | **/ |
369 | ||
c12b395a | 370 | static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) |
1da177e4 LT |
371 | { |
372 | struct net_device *dev; | |
373 | struct ip6_tnl *t; | |
374 | char name[IFNAMSIZ]; | |
37355565 | 375 | int err = -ENOMEM; |
1da177e4 | 376 | |
34cc7ba6 | 377 | if (p->name[0]) |
1da177e4 | 378 | strlcpy(name, p->name, IFNAMSIZ); |
34cc7ba6 PE |
379 | else |
380 | sprintf(name, "ip6tnl%%d"); | |
381 | ||
c835a677 TG |
382 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
383 | ip6_tnl_dev_setup); | |
63159f29 | 384 | if (!dev) |
567131a7 | 385 | goto failed; |
1da177e4 | 386 | |
554eb277 PE |
387 | dev_net_set(dev, net); |
388 | ||
2941a486 | 389 | t = netdev_priv(dev); |
1da177e4 | 390 | t->parms = *p; |
0bd87628 | 391 | t->net = dev_net(dev); |
0b112457 | 392 | err = ip6_tnl_create2(dev); |
8560f226 ED |
393 | if (err < 0) |
394 | goto failed_free; | |
1da177e4 | 395 | |
567131a7 | 396 | return t; |
b37d428b PE |
397 | |
398 | failed_free: | |
8560f226 | 399 | ip6_dev_free(dev); |
567131a7 | 400 | failed: |
37355565 | 401 | return ERR_PTR(err); |
1da177e4 LT |
402 | } |
403 | ||
404 | /** | |
3144581c | 405 | * ip6_tnl_locate - find or create tunnel matching given parameters |
1ab1457c | 406 | * @p: tunnel parameters |
1da177e4 LT |
407 | * @create: != 0 if allowed to create new tunnel if no match found |
408 | * | |
409 | * Description: | |
3144581c | 410 | * ip6_tnl_locate() first tries to locate an existing tunnel |
1da177e4 LT |
411 | * based on @parms. If this is unsuccessful, but @create is set a new |
412 | * tunnel device is created and registered for use. | |
413 | * | |
414 | * Return: | |
37355565 | 415 | * matching tunnel or error pointer |
1da177e4 LT |
416 | **/ |
417 | ||
2dd02c89 | 418 | static struct ip6_tnl *ip6_tnl_locate(struct net *net, |
c12b395a | 419 | struct __ip6_tnl_parm *p, int create) |
1da177e4 | 420 | { |
b71d1d42 ED |
421 | const struct in6_addr *remote = &p->raddr; |
422 | const struct in6_addr *local = &p->laddr; | |
94767632 | 423 | struct ip6_tnl __rcu **tp; |
1da177e4 | 424 | struct ip6_tnl *t; |
2dd02c89 | 425 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 426 | |
94767632 ED |
427 | for (tp = ip6_tnl_bucket(ip6n, p); |
428 | (t = rtnl_dereference(*tp)) != NULL; | |
429 | tp = &t->next) { | |
1da177e4 | 430 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
2b0bb01b SK |
431 | ipv6_addr_equal(remote, &t->parms.raddr)) { |
432 | if (create) | |
37355565 | 433 | return ERR_PTR(-EEXIST); |
2b0bb01b | 434 | |
567131a7 | 435 | return t; |
2b0bb01b | 436 | } |
1da177e4 LT |
437 | } |
438 | if (!create) | |
37355565 | 439 | return ERR_PTR(-ENODEV); |
2dd02c89 | 440 | return ip6_tnl_create(net, p); |
1da177e4 LT |
441 | } |
442 | ||
443 | /** | |
3144581c | 444 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
1da177e4 | 445 | * @dev: the device to be destroyed |
1ab1457c | 446 | * |
1da177e4 | 447 | * Description: |
3144581c | 448 | * ip6_tnl_dev_uninit() removes tunnel from its list |
1da177e4 LT |
449 | **/ |
450 | ||
451 | static void | |
3144581c | 452 | ip6_tnl_dev_uninit(struct net_device *dev) |
1da177e4 | 453 | { |
2941a486 | 454 | struct ip6_tnl *t = netdev_priv(dev); |
0bd87628 | 455 | struct net *net = t->net; |
2dd02c89 | 456 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 457 | |
94767632 | 458 | if (dev == ip6n->fb_tnl_dev) |
a9b3cd7f | 459 | RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); |
94767632 | 460 | else |
2dd02c89 | 461 | ip6_tnl_unlink(ip6n, t); |
1da177e4 LT |
462 | ip6_tnl_dst_reset(t); |
463 | dev_put(dev); | |
464 | } | |
465 | ||
466 | /** | |
467 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option | |
468 | * @skb: received socket buffer | |
469 | * | |
1ab1457c YH |
470 | * Return: |
471 | * 0 if none was found, | |
1da177e4 LT |
472 | * else index to encapsulation limit |
473 | **/ | |
474 | ||
c12b395a | 475 | __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) |
1da177e4 | 476 | { |
b71d1d42 | 477 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw; |
1da177e4 | 478 | __u8 nexthdr = ipv6h->nexthdr; |
67ba4152 | 479 | __u16 off = sizeof(*ipv6h); |
1da177e4 LT |
480 | |
481 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { | |
482 | __u16 optlen = 0; | |
483 | struct ipv6_opt_hdr *hdr; | |
67ba4152 | 484 | if (raw + off + sizeof(*hdr) > skb->data && |
1da177e4 LT |
485 | !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) |
486 | break; | |
487 | ||
488 | hdr = (struct ipv6_opt_hdr *) (raw + off); | |
489 | if (nexthdr == NEXTHDR_FRAGMENT) { | |
490 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; | |
491 | if (frag_hdr->frag_off) | |
492 | break; | |
493 | optlen = 8; | |
494 | } else if (nexthdr == NEXTHDR_AUTH) { | |
495 | optlen = (hdr->hdrlen + 2) << 2; | |
496 | } else { | |
497 | optlen = ipv6_optlen(hdr); | |
498 | } | |
499 | if (nexthdr == NEXTHDR_DEST) { | |
500 | __u16 i = off + 2; | |
501 | while (1) { | |
502 | struct ipv6_tlv_tnl_enc_lim *tel; | |
503 | ||
504 | /* No more room for encapsulation limit */ | |
505 | if (i + sizeof (*tel) > off + optlen) | |
506 | break; | |
507 | ||
508 | tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; | |
509 | /* return index of option if found and valid */ | |
510 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && | |
511 | tel->length == 1) | |
512 | return i; | |
513 | /* else jump to next option */ | |
514 | if (tel->type) | |
515 | i += tel->length + 2; | |
516 | else | |
517 | i++; | |
518 | } | |
519 | } | |
520 | nexthdr = hdr->nexthdr; | |
521 | off += optlen; | |
522 | } | |
523 | return 0; | |
524 | } | |
c12b395a | 525 | EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); |
1da177e4 LT |
526 | |
527 | /** | |
e490d1d8 | 528 | * ip6_tnl_err - tunnel error handler |
1da177e4 LT |
529 | * |
530 | * Description: | |
e490d1d8 | 531 | * ip6_tnl_err() should handle errors in the tunnel according |
1da177e4 LT |
532 | * to the specifications in RFC 2473. |
533 | **/ | |
534 | ||
d2acc347 | 535 | static int |
502b0935 | 536 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
d5fdd6ba | 537 | u8 *type, u8 *code, int *msg, __u32 *info, int offset) |
1da177e4 | 538 | { |
b71d1d42 | 539 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data; |
1da177e4 LT |
540 | struct ip6_tnl *t; |
541 | int rel_msg = 0; | |
d5fdd6ba BH |
542 | u8 rel_type = ICMPV6_DEST_UNREACH; |
543 | u8 rel_code = ICMPV6_ADDR_UNREACH; | |
acf722f7 | 544 | u8 tproto; |
1da177e4 LT |
545 | __u32 rel_info = 0; |
546 | __u16 len; | |
d2acc347 | 547 | int err = -ENOENT; |
1da177e4 | 548 | |
1ab1457c YH |
549 | /* If the packet doesn't contain the original IPv6 header we are |
550 | in trouble since we might need the source address for further | |
1da177e4 LT |
551 | processing of the error. */ |
552 | ||
2922bc8a | 553 | rcu_read_lock(); |
e5d08d71 | 554 | t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr); |
63159f29 | 555 | if (!t) |
1da177e4 LT |
556 | goto out; |
557 | ||
acf722f7 AA |
558 | tproto = ACCESS_ONCE(t->parms.proto); |
559 | if (tproto != ipproto && tproto != 0) | |
502b0935 YK |
560 | goto out; |
561 | ||
d2acc347 HX |
562 | err = 0; |
563 | ||
e490d1d8 | 564 | switch (*type) { |
1da177e4 LT |
565 | __u32 teli; |
566 | struct ipv6_tlv_tnl_enc_lim *tel; | |
567 | __u32 mtu; | |
568 | case ICMPV6_DEST_UNREACH: | |
e87cc472 JP |
569 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", |
570 | t->parms.name); | |
1da177e4 LT |
571 | rel_msg = 1; |
572 | break; | |
573 | case ICMPV6_TIME_EXCEED: | |
e490d1d8 | 574 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
e87cc472 JP |
575 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
576 | t->parms.name); | |
1da177e4 LT |
577 | rel_msg = 1; |
578 | } | |
579 | break; | |
580 | case ICMPV6_PARAMPROB: | |
107a5fe6 | 581 | teli = 0; |
e490d1d8 | 582 | if ((*code) == ICMPV6_HDR_FIELD) |
c12b395a | 583 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
1da177e4 | 584 | |
704eae1f | 585 | if (teli && teli == *info - 2) { |
1da177e4 LT |
586 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
587 | if (tel->encap_limit == 0) { | |
e87cc472 JP |
588 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
589 | t->parms.name); | |
1da177e4 LT |
590 | rel_msg = 1; |
591 | } | |
e87cc472 JP |
592 | } else { |
593 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", | |
594 | t->parms.name); | |
1da177e4 LT |
595 | } |
596 | break; | |
597 | case ICMPV6_PKT_TOOBIG: | |
704eae1f | 598 | mtu = *info - offset; |
1da177e4 LT |
599 | if (mtu < IPV6_MIN_MTU) |
600 | mtu = IPV6_MIN_MTU; | |
601 | t->dev->mtu = mtu; | |
602 | ||
e5d08d71 IM |
603 | len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len); |
604 | if (len > mtu) { | |
1da177e4 LT |
605 | rel_type = ICMPV6_PKT_TOOBIG; |
606 | rel_code = 0; | |
607 | rel_info = mtu; | |
608 | rel_msg = 1; | |
609 | } | |
610 | break; | |
611 | } | |
e490d1d8 YK |
612 | |
613 | *type = rel_type; | |
614 | *code = rel_code; | |
615 | *info = rel_info; | |
616 | *msg = rel_msg; | |
617 | ||
618 | out: | |
2922bc8a | 619 | rcu_read_unlock(); |
e490d1d8 YK |
620 | return err; |
621 | } | |
622 | ||
c4d3efaf YK |
623 | static int |
624 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 625 | u8 type, u8 code, int offset, __be32 info) |
c4d3efaf YK |
626 | { |
627 | int rel_msg = 0; | |
d5fdd6ba BH |
628 | u8 rel_type = type; |
629 | u8 rel_code = code; | |
704eae1f | 630 | __u32 rel_info = ntohl(info); |
c4d3efaf YK |
631 | int err; |
632 | struct sk_buff *skb2; | |
b71d1d42 | 633 | const struct iphdr *eiph; |
c4d3efaf | 634 | struct rtable *rt; |
31e4543d | 635 | struct flowi4 fl4; |
c4d3efaf | 636 | |
502b0935 YK |
637 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
638 | &rel_msg, &rel_info, offset); | |
c4d3efaf YK |
639 | if (err < 0) |
640 | return err; | |
641 | ||
642 | if (rel_msg == 0) | |
643 | return 0; | |
644 | ||
645 | switch (rel_type) { | |
646 | case ICMPV6_DEST_UNREACH: | |
647 | if (rel_code != ICMPV6_ADDR_UNREACH) | |
648 | return 0; | |
649 | rel_type = ICMP_DEST_UNREACH; | |
650 | rel_code = ICMP_HOST_UNREACH; | |
651 | break; | |
652 | case ICMPV6_PKT_TOOBIG: | |
653 | if (rel_code != 0) | |
654 | return 0; | |
655 | rel_type = ICMP_DEST_UNREACH; | |
656 | rel_code = ICMP_FRAG_NEEDED; | |
657 | break; | |
ec18d9a2 DM |
658 | case NDISC_REDIRECT: |
659 | rel_type = ICMP_REDIRECT; | |
660 | rel_code = ICMP_REDIR_HOST; | |
c4d3efaf YK |
661 | default: |
662 | return 0; | |
663 | } | |
664 | ||
665 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) | |
666 | return 0; | |
667 | ||
668 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
669 | if (!skb2) | |
670 | return 0; | |
671 | ||
adf30907 ED |
672 | skb_dst_drop(skb2); |
673 | ||
c4d3efaf | 674 | skb_pull(skb2, offset); |
c1d2bbe1 | 675 | skb_reset_network_header(skb2); |
eddc9ec5 | 676 | eiph = ip_hdr(skb2); |
c4d3efaf YK |
677 | |
678 | /* Try to guess incoming interface */ | |
31e4543d | 679 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
78fbfd8a DM |
680 | eiph->saddr, 0, |
681 | 0, 0, | |
682 | IPPROTO_IPIP, RT_TOS(eiph->tos), 0); | |
b23dd4fe | 683 | if (IS_ERR(rt)) |
c4d3efaf YK |
684 | goto out; |
685 | ||
d8d1f30b | 686 | skb2->dev = rt->dst.dev; |
c4d3efaf YK |
687 | |
688 | /* route "incoming" packet */ | |
689 | if (rt->rt_flags & RTCF_LOCAL) { | |
690 | ip_rt_put(rt); | |
691 | rt = NULL; | |
31e4543d | 692 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
78fbfd8a DM |
693 | eiph->daddr, eiph->saddr, |
694 | 0, 0, | |
695 | IPPROTO_IPIP, | |
696 | RT_TOS(eiph->tos), 0); | |
b23dd4fe | 697 | if (IS_ERR(rt) || |
d8d1f30b | 698 | rt->dst.dev->type != ARPHRD_TUNNEL) { |
b23dd4fe DM |
699 | if (!IS_ERR(rt)) |
700 | ip_rt_put(rt); | |
c4d3efaf YK |
701 | goto out; |
702 | } | |
b23dd4fe | 703 | skb_dst_set(skb2, &rt->dst); |
c4d3efaf YK |
704 | } else { |
705 | ip_rt_put(rt); | |
706 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, | |
707 | skb2->dev) || | |
adf30907 | 708 | skb_dst(skb2)->dev->type != ARPHRD_TUNNEL) |
c4d3efaf YK |
709 | goto out; |
710 | } | |
711 | ||
712 | /* change mtu on this route */ | |
713 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { | |
adf30907 | 714 | if (rel_info > dst_mtu(skb_dst(skb2))) |
c4d3efaf YK |
715 | goto out; |
716 | ||
6700c270 | 717 | skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info); |
c4d3efaf | 718 | } |
1ed5c48f | 719 | if (rel_type == ICMP_REDIRECT) |
6700c270 | 720 | skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2); |
c4d3efaf | 721 | |
704eae1f | 722 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
c4d3efaf YK |
723 | |
724 | out: | |
725 | kfree_skb(skb2); | |
726 | return 0; | |
727 | } | |
728 | ||
e490d1d8 YK |
729 | static int |
730 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 731 | u8 type, u8 code, int offset, __be32 info) |
e490d1d8 YK |
732 | { |
733 | int rel_msg = 0; | |
d5fdd6ba BH |
734 | u8 rel_type = type; |
735 | u8 rel_code = code; | |
704eae1f | 736 | __u32 rel_info = ntohl(info); |
e490d1d8 YK |
737 | int err; |
738 | ||
502b0935 YK |
739 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
740 | &rel_msg, &rel_info, offset); | |
e490d1d8 YK |
741 | if (err < 0) |
742 | return err; | |
743 | ||
744 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { | |
1da177e4 LT |
745 | struct rt6_info *rt; |
746 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
305d4b3c | 747 | |
1da177e4 | 748 | if (!skb2) |
e490d1d8 | 749 | return 0; |
1da177e4 | 750 | |
adf30907 | 751 | skb_dst_drop(skb2); |
1da177e4 | 752 | skb_pull(skb2, offset); |
c1d2bbe1 | 753 | skb_reset_network_header(skb2); |
1da177e4 LT |
754 | |
755 | /* Try to guess incoming interface */ | |
2f7f54b7 PE |
756 | rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, |
757 | NULL, 0, 0); | |
1da177e4 | 758 | |
d1918542 DM |
759 | if (rt && rt->dst.dev) |
760 | skb2->dev = rt->dst.dev; | |
1da177e4 | 761 | |
3ffe533c | 762 | icmpv6_send(skb2, rel_type, rel_code, rel_info); |
1da177e4 | 763 | |
94e187c0 | 764 | ip6_rt_put(rt); |
1da177e4 LT |
765 | |
766 | kfree_skb(skb2); | |
767 | } | |
e490d1d8 YK |
768 | |
769 | return 0; | |
1da177e4 LT |
770 | } |
771 | ||
f4e0b4c5 ND |
772 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
773 | const struct ipv6hdr *ipv6h, | |
774 | struct sk_buff *skb) | |
c4d3efaf YK |
775 | { |
776 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; | |
777 | ||
778 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) | |
eddc9ec5 | 779 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
c4d3efaf | 780 | |
f4e0b4c5 | 781 | return IP6_ECN_decapsulate(ipv6h, skb); |
c4d3efaf YK |
782 | } |
783 | ||
f4e0b4c5 ND |
784 | static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
785 | const struct ipv6hdr *ipv6h, | |
786 | struct sk_buff *skb) | |
1da177e4 | 787 | { |
8359925b | 788 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
29bb43b4 | 789 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
1da177e4 | 790 | |
f4e0b4c5 | 791 | return IP6_ECN_decapsulate(ipv6h, skb); |
1da177e4 | 792 | } |
8359925b | 793 | |
c12b395a | 794 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
d0087b29 VN |
795 | const struct in6_addr *laddr, |
796 | const struct in6_addr *raddr) | |
797 | { | |
c12b395a | 798 | struct __ip6_tnl_parm *p = &t->parms; |
d0087b29 VN |
799 | int ltype = ipv6_addr_type(laddr); |
800 | int rtype = ipv6_addr_type(raddr); | |
801 | __u32 flags = 0; | |
802 | ||
803 | if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { | |
804 | flags = IP6_TNL_F_CAP_PER_PACKET; | |
805 | } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
806 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
807 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && | |
808 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { | |
809 | if (ltype&IPV6_ADDR_UNICAST) | |
810 | flags |= IP6_TNL_F_CAP_XMIT; | |
811 | if (rtype&IPV6_ADDR_UNICAST) | |
812 | flags |= IP6_TNL_F_CAP_RCV; | |
813 | } | |
814 | return flags; | |
815 | } | |
c12b395a | 816 | EXPORT_SYMBOL(ip6_tnl_get_cap); |
d0087b29 | 817 | |
f1a28eab | 818 | /* called with rcu_read_lock() */ |
c12b395a | 819 | int ip6_tnl_rcv_ctl(struct ip6_tnl *t, |
d0087b29 VN |
820 | const struct in6_addr *laddr, |
821 | const struct in6_addr *raddr) | |
09c6bbf0 | 822 | { |
c12b395a | 823 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 824 | int ret = 0; |
0bd87628 | 825 | struct net *net = t->net; |
09c6bbf0 | 826 | |
d0087b29 VN |
827 | if ((p->flags & IP6_TNL_F_CAP_RCV) || |
828 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && | |
829 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { | |
1ab1457c | 830 | struct net_device *ldev = NULL; |
09c6bbf0 VN |
831 | |
832 | if (p->link) | |
f1a28eab | 833 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 834 | |
d0087b29 VN |
835 | if ((ipv6_addr_is_multicast(laddr) || |
836 | likely(ipv6_chk_addr(net, laddr, ldev, 0))) && | |
837 | likely(!ipv6_chk_addr(net, raddr, NULL, 0))) | |
09c6bbf0 | 838 | ret = 1; |
09c6bbf0 VN |
839 | } |
840 | return ret; | |
841 | } | |
c12b395a | 842 | EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); |
1da177e4 LT |
843 | |
844 | /** | |
3144581c | 845 | * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally |
1da177e4 | 846 | * @skb: received socket buffer |
8359925b YK |
847 | * @protocol: ethernet protocol ID |
848 | * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN | |
1da177e4 LT |
849 | * |
850 | * Return: 0 | |
851 | **/ | |
852 | ||
8359925b | 853 | static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol, |
502b0935 | 854 | __u8 ipproto, |
f4e0b4c5 ND |
855 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, |
856 | const struct ipv6hdr *ipv6h, | |
857 | struct sk_buff *skb)) | |
1da177e4 | 858 | { |
1da177e4 | 859 | struct ip6_tnl *t; |
b71d1d42 | 860 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
acf722f7 | 861 | u8 tproto; |
f4e0b4c5 | 862 | int err; |
1da177e4 | 863 | |
2922bc8a | 864 | rcu_read_lock(); |
e5d08d71 | 865 | t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr); |
53b24b8f | 866 | if (t) { |
8f84985f | 867 | struct pcpu_sw_netstats *tstats; |
8560f226 | 868 | |
acf722f7 AA |
869 | tproto = ACCESS_ONCE(t->parms.proto); |
870 | if (tproto != ipproto && tproto != 0) { | |
2922bc8a | 871 | rcu_read_unlock(); |
502b0935 YK |
872 | goto discard; |
873 | } | |
874 | ||
1da177e4 | 875 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
2922bc8a | 876 | rcu_read_unlock(); |
50fba2aa | 877 | goto discard; |
1da177e4 LT |
878 | } |
879 | ||
d0087b29 | 880 | if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) { |
3dca02af | 881 | t->dev->stats.rx_dropped++; |
2922bc8a | 882 | rcu_read_unlock(); |
1da177e4 LT |
883 | goto discard; |
884 | } | |
b0e380b1 | 885 | skb->mac_header = skb->network_header; |
c1d2bbe1 | 886 | skb_reset_network_header(skb); |
8359925b | 887 | skb->protocol = htons(protocol); |
1da177e4 | 888 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); |
8359925b | 889 | |
ea23192e | 890 | __skb_tunnel_rx(skb, t->dev, t->net); |
f4e0b4c5 ND |
891 | |
892 | err = dscp_ecn_decapsulate(t, ipv6h, skb); | |
893 | if (unlikely(err)) { | |
894 | if (log_ecn_error) | |
895 | net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n", | |
896 | &ipv6h->saddr, | |
897 | ipv6_get_dsfield(ipv6h)); | |
898 | if (err > 1) { | |
899 | ++t->dev->stats.rx_frame_errors; | |
900 | ++t->dev->stats.rx_errors; | |
901 | rcu_read_unlock(); | |
902 | goto discard; | |
903 | } | |
904 | } | |
905 | ||
8560f226 | 906 | tstats = this_cpu_ptr(t->dev->tstats); |
abb6013c | 907 | u64_stats_update_begin(&tstats->syncp); |
8560f226 ED |
908 | tstats->rx_packets++; |
909 | tstats->rx_bytes += skb->len; | |
abb6013c | 910 | u64_stats_update_end(&tstats->syncp); |
8560f226 | 911 | |
caf586e5 | 912 | netif_rx(skb); |
8990f468 | 913 | |
2922bc8a | 914 | rcu_read_unlock(); |
1da177e4 LT |
915 | return 0; |
916 | } | |
2922bc8a | 917 | rcu_read_unlock(); |
1da177e4 | 918 | return 1; |
50fba2aa HX |
919 | |
920 | discard: | |
921 | kfree_skb(skb); | |
922 | return 0; | |
1da177e4 LT |
923 | } |
924 | ||
c4d3efaf YK |
925 | static int ip4ip6_rcv(struct sk_buff *skb) |
926 | { | |
502b0935 YK |
927 | return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP, |
928 | ip4ip6_dscp_ecn_decapsulate); | |
c4d3efaf YK |
929 | } |
930 | ||
8359925b YK |
931 | static int ip6ip6_rcv(struct sk_buff *skb) |
932 | { | |
502b0935 YK |
933 | return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6, |
934 | ip6ip6_dscp_ecn_decapsulate); | |
8359925b YK |
935 | } |
936 | ||
6fb32dde VN |
937 | struct ipv6_tel_txoption { |
938 | struct ipv6_txoptions ops; | |
939 | __u8 dst_opt[8]; | |
940 | }; | |
1da177e4 | 941 | |
6fb32dde VN |
942 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
943 | { | |
944 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); | |
1da177e4 | 945 | |
6fb32dde VN |
946 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
947 | opt->dst_opt[3] = 1; | |
948 | opt->dst_opt[4] = encap_limit; | |
949 | opt->dst_opt[5] = IPV6_TLV_PADN; | |
950 | opt->dst_opt[6] = 1; | |
1da177e4 | 951 | |
6fb32dde VN |
952 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
953 | opt->ops.opt_nflen = 8; | |
1da177e4 LT |
954 | } |
955 | ||
956 | /** | |
3144581c | 957 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
1da177e4 | 958 | * @t: the outgoing tunnel device |
1ab1457c | 959 | * @hdr: IPv6 header from the incoming packet |
1da177e4 LT |
960 | * |
961 | * Description: | |
1ab1457c | 962 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
1da177e4 LT |
963 | * doesn't match source of incoming packet. |
964 | * | |
1ab1457c | 965 | * Return: |
1da177e4 LT |
966 | * 1 if conflict, |
967 | * 0 else | |
968 | **/ | |
969 | ||
92113bfd | 970 | static inline bool |
b71d1d42 | 971 | ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) |
1da177e4 LT |
972 | { |
973 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
974 | } | |
975 | ||
d5005140 SK |
976 | int ip6_tnl_xmit_ctl(struct ip6_tnl *t, |
977 | const struct in6_addr *laddr, | |
978 | const struct in6_addr *raddr) | |
09c6bbf0 | 979 | { |
c12b395a | 980 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 981 | int ret = 0; |
0bd87628 | 982 | struct net *net = t->net; |
09c6bbf0 | 983 | |
d5005140 SK |
984 | if ((p->flags & IP6_TNL_F_CAP_XMIT) || |
985 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && | |
986 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) { | |
09c6bbf0 VN |
987 | struct net_device *ldev = NULL; |
988 | ||
f1a28eab | 989 | rcu_read_lock(); |
09c6bbf0 | 990 | if (p->link) |
f1a28eab | 991 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 992 | |
d5005140 | 993 | if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0))) |
f3213831 JP |
994 | pr_warn("%s xmit: Local address not yet configured!\n", |
995 | p->name); | |
d5005140 SK |
996 | else if (!ipv6_addr_is_multicast(raddr) && |
997 | unlikely(ipv6_chk_addr(net, raddr, NULL, 0))) | |
f3213831 JP |
998 | pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", |
999 | p->name); | |
09c6bbf0 VN |
1000 | else |
1001 | ret = 1; | |
f1a28eab | 1002 | rcu_read_unlock(); |
09c6bbf0 VN |
1003 | } |
1004 | return ret; | |
1005 | } | |
c12b395a | 1006 | EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); |
1007 | ||
1da177e4 | 1008 | /** |
61ec2aec | 1009 | * ip6_tnl_xmit2 - encapsulate packet and send |
1da177e4 | 1010 | * @skb: the outgoing socket buffer |
1ab1457c | 1011 | * @dev: the outgoing tunnel device |
61ec2aec YK |
1012 | * @dsfield: dscp code for outer header |
1013 | * @fl: flow of tunneled packet | |
1014 | * @encap_limit: encapsulation limit | |
1015 | * @pmtu: Path MTU is stored if packet is too big | |
1da177e4 LT |
1016 | * |
1017 | * Description: | |
1018 | * Build new header and do some sanity checks on the packet before sending | |
1019 | * it. | |
1020 | * | |
1ab1457c | 1021 | * Return: |
c4d3efaf | 1022 | * 0 on success |
61ec2aec YK |
1023 | * -1 fail |
1024 | * %-EMSGSIZE message too big. return mtu in this case. | |
1da177e4 LT |
1025 | **/ |
1026 | ||
61ec2aec YK |
1027 | static int ip6_tnl_xmit2(struct sk_buff *skb, |
1028 | struct net_device *dev, | |
1029 | __u8 dsfield, | |
4c9483b2 | 1030 | struct flowi6 *fl6, |
61ec2aec YK |
1031 | int encap_limit, |
1032 | __u32 *pmtu) | |
1da177e4 | 1033 | { |
2941a486 | 1034 | struct ip6_tnl *t = netdev_priv(dev); |
0bd87628 | 1035 | struct net *net = t->net; |
3dca02af | 1036 | struct net_device_stats *stats = &t->dev->stats; |
0660e03f | 1037 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
6fb32dde | 1038 | struct ipv6_tel_txoption opt; |
d24f22f3 | 1039 | struct dst_entry *dst = NULL, *ndst = NULL; |
1da177e4 LT |
1040 | struct net_device *tdev; |
1041 | int mtu; | |
c2636b4d | 1042 | unsigned int max_headroom = sizeof(struct ipv6hdr); |
1da177e4 | 1043 | u8 proto; |
61ec2aec | 1044 | int err = -1; |
1da177e4 | 1045 | |
ea3dc960 SK |
1046 | /* NBMA tunnel */ |
1047 | if (ipv6_addr_any(&t->parms.raddr)) { | |
1048 | struct in6_addr *addr6; | |
1049 | struct neighbour *neigh; | |
1050 | int addr_type; | |
1051 | ||
1052 | if (!skb_dst(skb)) | |
1053 | goto tx_err_link_failure; | |
1054 | ||
1055 | neigh = dst_neigh_lookup(skb_dst(skb), | |
1056 | &ipv6_hdr(skb)->daddr); | |
1057 | if (!neigh) | |
1058 | goto tx_err_link_failure; | |
1059 | ||
1060 | addr6 = (struct in6_addr *)&neigh->primary_key; | |
1061 | addr_type = ipv6_addr_type(addr6); | |
1062 | ||
1063 | if (addr_type == IPV6_ADDR_ANY) | |
1064 | addr6 = &ipv6_hdr(skb)->daddr; | |
1065 | ||
1066 | memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr)); | |
1067 | neigh_release(neigh); | |
1068 | } else if (!fl6->flowi6_mark) | |
f230d1e8 | 1069 | dst = ip6_tnl_dst_get(t); |
d5005140 SK |
1070 | |
1071 | if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr)) | |
1072 | goto tx_err_link_failure; | |
1073 | ||
89b02126 | 1074 | if (!dst) { |
cdf3464e | 1075 | dst = ip6_route_output(net, NULL, fl6); |
1da177e4 | 1076 | |
cdf3464e | 1077 | if (dst->error) |
a57ebc90 | 1078 | goto tx_err_link_failure; |
cdf3464e MKL |
1079 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0); |
1080 | if (IS_ERR(dst)) { | |
1081 | err = PTR_ERR(dst); | |
1082 | dst = NULL; | |
452edd59 DM |
1083 | goto tx_err_link_failure; |
1084 | } | |
cdf3464e | 1085 | ndst = dst; |
a57ebc90 | 1086 | } |
1da177e4 LT |
1087 | |
1088 | tdev = dst->dev; | |
1089 | ||
1090 | if (tdev == dev) { | |
1091 | stats->collisions++; | |
e87cc472 JP |
1092 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
1093 | t->parms.name); | |
1da177e4 LT |
1094 | goto tx_err_dst_release; |
1095 | } | |
67ba4152 | 1096 | mtu = dst_mtu(dst) - sizeof(*ipv6h); |
6fb32dde | 1097 | if (encap_limit >= 0) { |
1da177e4 LT |
1098 | max_headroom += 8; |
1099 | mtu -= 8; | |
1100 | } | |
1101 | if (mtu < IPV6_MIN_MTU) | |
1102 | mtu = IPV6_MIN_MTU; | |
adf30907 | 1103 | if (skb_dst(skb)) |
6700c270 | 1104 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
1da177e4 | 1105 | if (skb->len > mtu) { |
61ec2aec YK |
1106 | *pmtu = mtu; |
1107 | err = -EMSGSIZE; | |
1da177e4 LT |
1108 | goto tx_err_dst_release; |
1109 | } | |
1110 | ||
963a88b3 | 1111 | skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev))); |
0bd87628 | 1112 | |
1da177e4 LT |
1113 | /* |
1114 | * Okay, now see if we can stuff it in the buffer as-is. | |
1115 | */ | |
1116 | max_headroom += LL_RESERVED_SPACE(tdev); | |
1ab1457c | 1117 | |
cfbba49d PM |
1118 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
1119 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | |
1da177e4 | 1120 | struct sk_buff *new_skb; |
1ab1457c | 1121 | |
e5d08d71 IM |
1122 | new_skb = skb_realloc_headroom(skb, max_headroom); |
1123 | if (!new_skb) | |
1da177e4 LT |
1124 | goto tx_err_dst_release; |
1125 | ||
1126 | if (skb->sk) | |
1127 | skb_set_owner_w(new_skb, skb->sk); | |
9ff26449 | 1128 | consume_skb(skb); |
1da177e4 LT |
1129 | skb = new_skb; |
1130 | } | |
cdf3464e MKL |
1131 | |
1132 | if (!fl6->flowi6_mark && ndst) | |
1133 | ip6_tnl_dst_set(t, ndst); | |
1134 | skb_dst_set(skb, dst); | |
1135 | ||
b0e380b1 | 1136 | skb->transport_header = skb->network_header; |
1da177e4 | 1137 | |
4c9483b2 | 1138 | proto = fl6->flowi6_proto; |
6fb32dde VN |
1139 | if (encap_limit >= 0) { |
1140 | init_tel_txopt(&opt, encap_limit); | |
1141 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); | |
1142 | } | |
3d483058 HFS |
1143 | |
1144 | if (likely(!skb->encapsulation)) { | |
1145 | skb_reset_inner_headers(skb); | |
1146 | skb->encapsulation = 1; | |
1147 | } | |
1148 | ||
e2d1bca7 ACM |
1149 | skb_push(skb, sizeof(struct ipv6hdr)); |
1150 | skb_reset_network_header(skb); | |
0660e03f | 1151 | ipv6h = ipv6_hdr(skb); |
cb1ce2ef | 1152 | ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), |
42240901 | 1153 | ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6)); |
1da177e4 LT |
1154 | ipv6h->hop_limit = t->parms.hop_limit; |
1155 | ipv6h->nexthdr = proto; | |
4e3fd7a0 AD |
1156 | ipv6h->saddr = fl6->saddr; |
1157 | ipv6h->daddr = fl6->daddr; | |
79b16aad | 1158 | ip6tunnel_xmit(NULL, skb, dev); |
1da177e4 LT |
1159 | return 0; |
1160 | tx_err_link_failure: | |
1161 | stats->tx_carrier_errors++; | |
1162 | dst_link_failure(skb); | |
1163 | tx_err_dst_release: | |
cdf3464e | 1164 | dst_release(dst); |
61ec2aec YK |
1165 | return err; |
1166 | } | |
1167 | ||
c4d3efaf YK |
1168 | static inline int |
1169 | ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) | |
1170 | { | |
1171 | struct ip6_tnl *t = netdev_priv(dev); | |
b71d1d42 | 1172 | const struct iphdr *iph = ip_hdr(skb); |
c4d3efaf | 1173 | int encap_limit = -1; |
4c9483b2 | 1174 | struct flowi6 fl6; |
c4d3efaf YK |
1175 | __u8 dsfield; |
1176 | __u32 mtu; | |
acf722f7 | 1177 | u8 tproto; |
c4d3efaf YK |
1178 | int err; |
1179 | ||
acf722f7 | 1180 | tproto = ACCESS_ONCE(t->parms.proto); |
d5005140 | 1181 | if (tproto != IPPROTO_IPIP && tproto != 0) |
c4d3efaf YK |
1182 | return -1; |
1183 | ||
1184 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1185 | encap_limit = t->parms.encap_limit; | |
1186 | ||
67ba4152 | 1187 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
4c9483b2 | 1188 | fl6.flowi6_proto = IPPROTO_IPIP; |
c4d3efaf YK |
1189 | |
1190 | dsfield = ipv4_get_dsfield(iph); | |
1191 | ||
d24f22f3 | 1192 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
4c9483b2 | 1193 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
b77f2fa6 | 1194 | & IPV6_TCLASS_MASK; |
d24f22f3 ED |
1195 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
1196 | fl6.flowi6_mark = skb->mark; | |
c4d3efaf | 1197 | |
4c9483b2 | 1198 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
c4d3efaf YK |
1199 | if (err != 0) { |
1200 | /* XXX: send ICMP error even if DF is not set. */ | |
1201 | if (err == -EMSGSIZE) | |
1202 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, | |
1203 | htonl(mtu)); | |
1204 | return -1; | |
1205 | } | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
61ec2aec YK |
1210 | static inline int |
1211 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) | |
1212 | { | |
1213 | struct ip6_tnl *t = netdev_priv(dev); | |
0660e03f | 1214 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
61ec2aec YK |
1215 | int encap_limit = -1; |
1216 | __u16 offset; | |
4c9483b2 | 1217 | struct flowi6 fl6; |
61ec2aec YK |
1218 | __u8 dsfield; |
1219 | __u32 mtu; | |
acf722f7 | 1220 | u8 tproto; |
61ec2aec YK |
1221 | int err; |
1222 | ||
acf722f7 AA |
1223 | tproto = ACCESS_ONCE(t->parms.proto); |
1224 | if ((tproto != IPPROTO_IPV6 && tproto != 0) || | |
d5005140 | 1225 | ip6_tnl_addr_conflict(t, ipv6h)) |
61ec2aec YK |
1226 | return -1; |
1227 | ||
c12b395a | 1228 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
d56f90a7 | 1229 | if (offset > 0) { |
61ec2aec | 1230 | struct ipv6_tlv_tnl_enc_lim *tel; |
d56f90a7 | 1231 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
61ec2aec YK |
1232 | if (tel->encap_limit == 0) { |
1233 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
3ffe533c | 1234 | ICMPV6_HDR_FIELD, offset + 2); |
61ec2aec YK |
1235 | return -1; |
1236 | } | |
1237 | encap_limit = tel->encap_limit - 1; | |
1238 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1239 | encap_limit = t->parms.encap_limit; | |
1240 | ||
67ba4152 | 1241 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
4c9483b2 | 1242 | fl6.flowi6_proto = IPPROTO_IPV6; |
61ec2aec YK |
1243 | |
1244 | dsfield = ipv6_get_dsfield(ipv6h); | |
d24f22f3 | 1245 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
4c9483b2 | 1246 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
d24f22f3 | 1247 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
3308de2b | 1248 | fl6.flowlabel |= ip6_flowlabel(ipv6h); |
d24f22f3 ED |
1249 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
1250 | fl6.flowi6_mark = skb->mark; | |
61ec2aec | 1251 | |
4c9483b2 | 1252 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
61ec2aec YK |
1253 | if (err != 0) { |
1254 | if (err == -EMSGSIZE) | |
3ffe533c | 1255 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
61ec2aec YK |
1256 | return -1; |
1257 | } | |
1258 | ||
1259 | return 0; | |
1260 | } | |
1261 | ||
6fef4c0c | 1262 | static netdev_tx_t |
61ec2aec YK |
1263 | ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
1264 | { | |
1265 | struct ip6_tnl *t = netdev_priv(dev); | |
3dca02af | 1266 | struct net_device_stats *stats = &t->dev->stats; |
61ec2aec YK |
1267 | int ret; |
1268 | ||
61ec2aec | 1269 | switch (skb->protocol) { |
60678040 | 1270 | case htons(ETH_P_IP): |
c4d3efaf YK |
1271 | ret = ip4ip6_tnl_xmit(skb, dev); |
1272 | break; | |
60678040 | 1273 | case htons(ETH_P_IPV6): |
61ec2aec YK |
1274 | ret = ip6ip6_tnl_xmit(skb, dev); |
1275 | break; | |
1276 | default: | |
1277 | goto tx_err; | |
1278 | } | |
1279 | ||
1280 | if (ret < 0) | |
1281 | goto tx_err; | |
1282 | ||
6ed10654 | 1283 | return NETDEV_TX_OK; |
61ec2aec | 1284 | |
1da177e4 LT |
1285 | tx_err: |
1286 | stats->tx_errors++; | |
1287 | stats->tx_dropped++; | |
1288 | kfree_skb(skb); | |
6ed10654 | 1289 | return NETDEV_TX_OK; |
1da177e4 LT |
1290 | } |
1291 | ||
3144581c | 1292 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
1da177e4 LT |
1293 | { |
1294 | struct net_device *dev = t->dev; | |
c12b395a | 1295 | struct __ip6_tnl_parm *p = &t->parms; |
4c9483b2 | 1296 | struct flowi6 *fl6 = &t->fl.u.ip6; |
1da177e4 | 1297 | |
3a6d54c5 JP |
1298 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
1299 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
1da177e4 LT |
1300 | |
1301 | /* Set up flowi template */ | |
4e3fd7a0 AD |
1302 | fl6->saddr = p->laddr; |
1303 | fl6->daddr = p->raddr; | |
4c9483b2 DM |
1304 | fl6->flowi6_oif = p->link; |
1305 | fl6->flowlabel = 0; | |
1da177e4 LT |
1306 | |
1307 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
4c9483b2 | 1308 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
1da177e4 | 1309 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
4c9483b2 | 1310 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
1da177e4 | 1311 | |
d0087b29 VN |
1312 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
1313 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
1da177e4 LT |
1314 | |
1315 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) | |
1316 | dev->flags |= IFF_POINTOPOINT; | |
1317 | else | |
1318 | dev->flags &= ~IFF_POINTOPOINT; | |
1319 | ||
1da177e4 | 1320 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
305d4b3c VN |
1321 | int strict = (ipv6_addr_type(&p->raddr) & |
1322 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
1323 | ||
0bd87628 | 1324 | struct rt6_info *rt = rt6_lookup(t->net, |
2f7f54b7 | 1325 | &p->raddr, &p->laddr, |
305d4b3c | 1326 | p->link, strict); |
1da177e4 | 1327 | |
63159f29 | 1328 | if (!rt) |
1da177e4 LT |
1329 | return; |
1330 | ||
d1918542 DM |
1331 | if (rt->dst.dev) { |
1332 | dev->hard_header_len = rt->dst.dev->hard_header_len + | |
67ba4152 | 1333 | sizeof(struct ipv6hdr); |
1da177e4 | 1334 | |
67ba4152 | 1335 | dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr); |
381601e5 | 1336 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
67ba4152 | 1337 | dev->mtu -= 8; |
1da177e4 LT |
1338 | |
1339 | if (dev->mtu < IPV6_MIN_MTU) | |
1340 | dev->mtu = IPV6_MIN_MTU; | |
1341 | } | |
94e187c0 | 1342 | ip6_rt_put(rt); |
1da177e4 LT |
1343 | } |
1344 | } | |
1345 | ||
1346 | /** | |
3144581c | 1347 | * ip6_tnl_change - update the tunnel parameters |
1da177e4 LT |
1348 | * @t: tunnel to be changed |
1349 | * @p: tunnel configuration parameters | |
1da177e4 LT |
1350 | * |
1351 | * Description: | |
3144581c | 1352 | * ip6_tnl_change() updates the tunnel parameters |
1da177e4 LT |
1353 | **/ |
1354 | ||
1355 | static int | |
c12b395a | 1356 | ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) |
1da177e4 | 1357 | { |
4e3fd7a0 AD |
1358 | t->parms.laddr = p->laddr; |
1359 | t->parms.raddr = p->raddr; | |
1da177e4 LT |
1360 | t->parms.flags = p->flags; |
1361 | t->parms.hop_limit = p->hop_limit; | |
1362 | t->parms.encap_limit = p->encap_limit; | |
1363 | t->parms.flowinfo = p->flowinfo; | |
8181b8c1 | 1364 | t->parms.link = p->link; |
502b0935 | 1365 | t->parms.proto = p->proto; |
0c088890 | 1366 | ip6_tnl_dst_reset(t); |
3144581c | 1367 | ip6_tnl_link_config(t); |
1da177e4 LT |
1368 | return 0; |
1369 | } | |
1370 | ||
0b112457 ND |
1371 | static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
1372 | { | |
0bd87628 | 1373 | struct net *net = t->net; |
0b112457 ND |
1374 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1375 | int err; | |
1376 | ||
1377 | ip6_tnl_unlink(ip6n, t); | |
1378 | synchronize_net(); | |
1379 | err = ip6_tnl_change(t, p); | |
1380 | ip6_tnl_link(ip6n, t); | |
1381 | netdev_state_change(t->dev); | |
1382 | return err; | |
1383 | } | |
1384 | ||
acf722f7 AA |
1385 | static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
1386 | { | |
1387 | /* for default tnl0 device allow to change only the proto */ | |
1388 | t->parms.proto = p->proto; | |
1389 | netdev_state_change(t->dev); | |
1390 | return 0; | |
1391 | } | |
1392 | ||
c12b395a | 1393 | static void |
1394 | ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) | |
1395 | { | |
1396 | p->laddr = u->laddr; | |
1397 | p->raddr = u->raddr; | |
1398 | p->flags = u->flags; | |
1399 | p->hop_limit = u->hop_limit; | |
1400 | p->encap_limit = u->encap_limit; | |
1401 | p->flowinfo = u->flowinfo; | |
1402 | p->link = u->link; | |
1403 | p->proto = u->proto; | |
1404 | memcpy(p->name, u->name, sizeof(u->name)); | |
1405 | } | |
1406 | ||
1407 | static void | |
1408 | ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) | |
1409 | { | |
1410 | u->laddr = p->laddr; | |
1411 | u->raddr = p->raddr; | |
1412 | u->flags = p->flags; | |
1413 | u->hop_limit = p->hop_limit; | |
1414 | u->encap_limit = p->encap_limit; | |
1415 | u->flowinfo = p->flowinfo; | |
1416 | u->link = p->link; | |
1417 | u->proto = p->proto; | |
1418 | memcpy(u->name, p->name, sizeof(u->name)); | |
1419 | } | |
1420 | ||
1da177e4 | 1421 | /** |
3144581c | 1422 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
1da177e4 LT |
1423 | * @dev: virtual device associated with tunnel |
1424 | * @ifr: parameters passed from userspace | |
1425 | * @cmd: command to be performed | |
1426 | * | |
1427 | * Description: | |
3144581c | 1428 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
1ab1457c | 1429 | * from userspace. |
1da177e4 LT |
1430 | * |
1431 | * The possible commands are the following: | |
1432 | * %SIOCGETTUNNEL: get tunnel parameters for device | |
1433 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters | |
1434 | * %SIOCCHGTUNNEL: change tunnel parameters to those given | |
1435 | * %SIOCDELTUNNEL: delete tunnel | |
1436 | * | |
1ab1457c | 1437 | * The fallback device "ip6tnl0", created during module |
1da177e4 LT |
1438 | * initialization, can be used for creating other tunnel devices. |
1439 | * | |
1440 | * Return: | |
1441 | * 0 on success, | |
1442 | * %-EFAULT if unable to copy data to or from userspace, | |
1443 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set | |
1444 | * %-EINVAL if passed tunnel parameters are invalid, | |
1445 | * %-EEXIST if changing a tunnel's parameters would cause a conflict | |
1446 | * %-ENODEV if attempting to change or delete a nonexisting device | |
1447 | **/ | |
1448 | ||
1449 | static int | |
3144581c | 1450 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
1da177e4 LT |
1451 | { |
1452 | int err = 0; | |
1da177e4 | 1453 | struct ip6_tnl_parm p; |
c12b395a | 1454 | struct __ip6_tnl_parm p1; |
74462f0d ND |
1455 | struct ip6_tnl *t = netdev_priv(dev); |
1456 | struct net *net = t->net; | |
2dd02c89 | 1457 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 LT |
1458 | |
1459 | switch (cmd) { | |
1460 | case SIOCGETTUNNEL: | |
15820e12 | 1461 | if (dev == ip6n->fb_tnl_dev) { |
67ba4152 | 1462 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
1da177e4 LT |
1463 | err = -EFAULT; |
1464 | break; | |
1465 | } | |
c12b395a | 1466 | ip6_tnl_parm_from_user(&p1, &p); |
1467 | t = ip6_tnl_locate(net, &p1, 0); | |
37355565 | 1468 | if (IS_ERR(t)) |
74462f0d | 1469 | t = netdev_priv(dev); |
5ef5d6c5 DC |
1470 | } else { |
1471 | memset(&p, 0, sizeof(p)); | |
567131a7 | 1472 | } |
c12b395a | 1473 | ip6_tnl_parm_to_user(&p, &t->parms); |
67ba4152 | 1474 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) { |
1da177e4 LT |
1475 | err = -EFAULT; |
1476 | } | |
1477 | break; | |
1478 | case SIOCADDTUNNEL: | |
1479 | case SIOCCHGTUNNEL: | |
1480 | err = -EPERM; | |
af31f412 | 1481 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 1482 | break; |
567131a7 | 1483 | err = -EFAULT; |
67ba4152 | 1484 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
1da177e4 | 1485 | break; |
567131a7 | 1486 | err = -EINVAL; |
502b0935 YK |
1487 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
1488 | p.proto != 0) | |
1da177e4 | 1489 | break; |
c12b395a | 1490 | ip6_tnl_parm_from_user(&p1, &p); |
1491 | t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
acf722f7 | 1492 | if (cmd == SIOCCHGTUNNEL) { |
37355565 | 1493 | if (!IS_ERR(t)) { |
567131a7 VN |
1494 | if (t->dev != dev) { |
1495 | err = -EEXIST; | |
1496 | break; | |
1497 | } | |
1498 | } else | |
1499 | t = netdev_priv(dev); | |
acf722f7 AA |
1500 | if (dev == ip6n->fb_tnl_dev) |
1501 | err = ip6_tnl0_update(t, &p1); | |
1502 | else | |
1503 | err = ip6_tnl_update(t, &p1); | |
1da177e4 | 1504 | } |
37355565 | 1505 | if (!IS_ERR(t)) { |
1da177e4 | 1506 | err = 0; |
c12b395a | 1507 | ip6_tnl_parm_to_user(&p, &t->parms); |
1508 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
567131a7 VN |
1509 | err = -EFAULT; |
1510 | ||
37355565 ND |
1511 | } else { |
1512 | err = PTR_ERR(t); | |
1513 | } | |
1da177e4 LT |
1514 | break; |
1515 | case SIOCDELTUNNEL: | |
1516 | err = -EPERM; | |
af31f412 | 1517 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1518 | break; |
1519 | ||
15820e12 | 1520 | if (dev == ip6n->fb_tnl_dev) { |
567131a7 | 1521 | err = -EFAULT; |
67ba4152 | 1522 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
1da177e4 | 1523 | break; |
567131a7 | 1524 | err = -ENOENT; |
c12b395a | 1525 | ip6_tnl_parm_from_user(&p1, &p); |
1526 | t = ip6_tnl_locate(net, &p1, 0); | |
37355565 | 1527 | if (IS_ERR(t)) |
1da177e4 | 1528 | break; |
567131a7 | 1529 | err = -EPERM; |
15820e12 | 1530 | if (t->dev == ip6n->fb_tnl_dev) |
1da177e4 | 1531 | break; |
567131a7 | 1532 | dev = t->dev; |
1da177e4 | 1533 | } |
22f8cde5 SH |
1534 | err = 0; |
1535 | unregister_netdevice(dev); | |
1da177e4 LT |
1536 | break; |
1537 | default: | |
1538 | err = -EINVAL; | |
1539 | } | |
1540 | return err; | |
1541 | } | |
1542 | ||
1da177e4 | 1543 | /** |
3144581c | 1544 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
1da177e4 LT |
1545 | * @dev: virtual device associated with tunnel |
1546 | * @new_mtu: the new mtu | |
1547 | * | |
1548 | * Return: | |
1549 | * 0 on success, | |
1550 | * %-EINVAL if mtu too small | |
1551 | **/ | |
1552 | ||
1553 | static int | |
3144581c | 1554 | ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
1da177e4 | 1555 | { |
582442d6 OG |
1556 | struct ip6_tnl *tnl = netdev_priv(dev); |
1557 | ||
1558 | if (tnl->parms.proto == IPPROTO_IPIP) { | |
1559 | if (new_mtu < 68) | |
1560 | return -EINVAL; | |
1561 | } else { | |
1562 | if (new_mtu < IPV6_MIN_MTU) | |
1563 | return -EINVAL; | |
1da177e4 | 1564 | } |
582442d6 OG |
1565 | if (new_mtu > 0xFFF8 - dev->hard_header_len) |
1566 | return -EINVAL; | |
1da177e4 LT |
1567 | dev->mtu = new_mtu; |
1568 | return 0; | |
1569 | } | |
1570 | ||
ecf2c06a ND |
1571 | int ip6_tnl_get_iflink(const struct net_device *dev) |
1572 | { | |
1573 | struct ip6_tnl *t = netdev_priv(dev); | |
1574 | ||
1575 | return t->parms.link; | |
1576 | } | |
1577 | EXPORT_SYMBOL(ip6_tnl_get_iflink); | |
1326c3d5 SH |
1578 | |
1579 | static const struct net_device_ops ip6_tnl_netdev_ops = { | |
6c6151da | 1580 | .ndo_init = ip6_tnl_dev_init, |
8560f226 | 1581 | .ndo_uninit = ip6_tnl_dev_uninit, |
1326c3d5 | 1582 | .ndo_start_xmit = ip6_tnl_xmit, |
8560f226 | 1583 | .ndo_do_ioctl = ip6_tnl_ioctl, |
1326c3d5 | 1584 | .ndo_change_mtu = ip6_tnl_change_mtu, |
8560f226 | 1585 | .ndo_get_stats = ip6_get_stats, |
ecf2c06a | 1586 | .ndo_get_iflink = ip6_tnl_get_iflink, |
1326c3d5 SH |
1587 | }; |
1588 | ||
8560f226 | 1589 | |
1da177e4 | 1590 | /** |
3144581c | 1591 | * ip6_tnl_dev_setup - setup virtual tunnel device |
1da177e4 LT |
1592 | * @dev: virtual device associated with tunnel |
1593 | * | |
1594 | * Description: | |
1595 | * Initialize function pointers and device parameters | |
1596 | **/ | |
1597 | ||
3144581c | 1598 | static void ip6_tnl_dev_setup(struct net_device *dev) |
1da177e4 | 1599 | { |
381601e5 AF |
1600 | struct ip6_tnl *t; |
1601 | ||
1326c3d5 | 1602 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
8560f226 | 1603 | dev->destructor = ip6_dev_free; |
1da177e4 LT |
1604 | |
1605 | dev->type = ARPHRD_TUNNEL6; | |
67ba4152 IM |
1606 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr); |
1607 | dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr); | |
381601e5 AF |
1608 | t = netdev_priv(dev); |
1609 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
67ba4152 | 1610 | dev->mtu -= 8; |
1da177e4 LT |
1611 | dev->flags |= IFF_NOARP; |
1612 | dev->addr_len = sizeof(struct in6_addr); | |
02875878 | 1613 | netif_keep_dst(dev); |
e837735e ND |
1614 | /* This perm addr will be used as interface identifier by IPv6 */ |
1615 | dev->addr_assign_type = NET_ADDR_RANDOM; | |
1616 | eth_random_addr(dev->perm_addr); | |
1da177e4 LT |
1617 | } |
1618 | ||
1619 | ||
1620 | /** | |
3144581c | 1621 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
1da177e4 LT |
1622 | * @dev: virtual device associated with tunnel |
1623 | **/ | |
1624 | ||
8560f226 | 1625 | static inline int |
3144581c | 1626 | ip6_tnl_dev_init_gen(struct net_device *dev) |
1da177e4 | 1627 | { |
2941a486 | 1628 | struct ip6_tnl *t = netdev_priv(dev); |
cdf3464e | 1629 | int ret; |
8560f226 | 1630 | |
1da177e4 | 1631 | t->dev = dev; |
0bd87628 | 1632 | t->net = dev_net(dev); |
1c213bd2 | 1633 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
8560f226 ED |
1634 | if (!dev->tstats) |
1635 | return -ENOMEM; | |
cdf3464e MKL |
1636 | |
1637 | ret = ip6_tnl_dst_init(t); | |
1638 | if (ret) { | |
1639 | free_percpu(dev->tstats); | |
1640 | dev->tstats = NULL; | |
1641 | return ret; | |
1642 | } | |
1643 | ||
8560f226 | 1644 | return 0; |
1da177e4 LT |
1645 | } |
1646 | ||
1647 | /** | |
3144581c | 1648 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
1da177e4 LT |
1649 | * @dev: virtual device associated with tunnel |
1650 | **/ | |
1651 | ||
8560f226 | 1652 | static int ip6_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1653 | { |
2941a486 | 1654 | struct ip6_tnl *t = netdev_priv(dev); |
8560f226 ED |
1655 | int err = ip6_tnl_dev_init_gen(dev); |
1656 | ||
1657 | if (err) | |
1658 | return err; | |
3144581c | 1659 | ip6_tnl_link_config(t); |
8560f226 | 1660 | return 0; |
1da177e4 LT |
1661 | } |
1662 | ||
1663 | /** | |
3144581c | 1664 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
1da177e4 LT |
1665 | * @dev: fallback device |
1666 | * | |
1667 | * Return: 0 | |
1668 | **/ | |
1669 | ||
8560f226 | 1670 | static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1671 | { |
2941a486 | 1672 | struct ip6_tnl *t = netdev_priv(dev); |
3e6c9fb5 PE |
1673 | struct net *net = dev_net(dev); |
1674 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1675 | ||
502b0935 | 1676 | t->parms.proto = IPPROTO_IPV6; |
1da177e4 | 1677 | dev_hold(dev); |
d0087b29 | 1678 | |
cf778b00 | 1679 | rcu_assign_pointer(ip6n->tnls_wc[0], t); |
8560f226 | 1680 | return 0; |
1da177e4 LT |
1681 | } |
1682 | ||
0b112457 ND |
1683 | static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[]) |
1684 | { | |
1685 | u8 proto; | |
1686 | ||
c8965932 | 1687 | if (!data || !data[IFLA_IPTUN_PROTO]) |
0b112457 ND |
1688 | return 0; |
1689 | ||
1690 | proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
1691 | if (proto != IPPROTO_IPV6 && | |
1692 | proto != IPPROTO_IPIP && | |
1693 | proto != 0) | |
1694 | return -EINVAL; | |
1695 | ||
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | static void ip6_tnl_netlink_parms(struct nlattr *data[], | |
1700 | struct __ip6_tnl_parm *parms) | |
1701 | { | |
1702 | memset(parms, 0, sizeof(*parms)); | |
1703 | ||
1704 | if (!data) | |
1705 | return; | |
1706 | ||
1707 | if (data[IFLA_IPTUN_LINK]) | |
1708 | parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); | |
1709 | ||
1710 | if (data[IFLA_IPTUN_LOCAL]) | |
67b61f6c | 1711 | parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); |
0b112457 ND |
1712 | |
1713 | if (data[IFLA_IPTUN_REMOTE]) | |
67b61f6c | 1714 | parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); |
0b112457 ND |
1715 | |
1716 | if (data[IFLA_IPTUN_TTL]) | |
1717 | parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); | |
1718 | ||
1719 | if (data[IFLA_IPTUN_ENCAP_LIMIT]) | |
1720 | parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); | |
1721 | ||
1722 | if (data[IFLA_IPTUN_FLOWINFO]) | |
1ff05fb7 | 1723 | parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); |
0b112457 ND |
1724 | |
1725 | if (data[IFLA_IPTUN_FLAGS]) | |
1726 | parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); | |
1727 | ||
1728 | if (data[IFLA_IPTUN_PROTO]) | |
1729 | parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
1730 | } | |
1731 | ||
1732 | static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, | |
1733 | struct nlattr *tb[], struct nlattr *data[]) | |
1734 | { | |
1735 | struct net *net = dev_net(dev); | |
37355565 | 1736 | struct ip6_tnl *nt, *t; |
0b112457 ND |
1737 | |
1738 | nt = netdev_priv(dev); | |
1739 | ip6_tnl_netlink_parms(data, &nt->parms); | |
1740 | ||
37355565 ND |
1741 | t = ip6_tnl_locate(net, &nt->parms, 0); |
1742 | if (!IS_ERR(t)) | |
0b112457 ND |
1743 | return -EEXIST; |
1744 | ||
1745 | return ip6_tnl_create2(dev); | |
1746 | } | |
1747 | ||
1748 | static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], | |
1749 | struct nlattr *data[]) | |
1750 | { | |
0bd87628 | 1751 | struct ip6_tnl *t = netdev_priv(dev); |
0b112457 | 1752 | struct __ip6_tnl_parm p; |
0bd87628 | 1753 | struct net *net = t->net; |
0b112457 ND |
1754 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1755 | ||
1756 | if (dev == ip6n->fb_tnl_dev) | |
1757 | return -EINVAL; | |
1758 | ||
1759 | ip6_tnl_netlink_parms(data, &p); | |
1760 | ||
1761 | t = ip6_tnl_locate(net, &p, 0); | |
37355565 | 1762 | if (!IS_ERR(t)) { |
0b112457 ND |
1763 | if (t->dev != dev) |
1764 | return -EEXIST; | |
1765 | } else | |
1766 | t = netdev_priv(dev); | |
1767 | ||
1768 | return ip6_tnl_update(t, &p); | |
1769 | } | |
1770 | ||
1e9f3d6f ND |
1771 | static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) |
1772 | { | |
1773 | struct net *net = dev_net(dev); | |
1774 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1775 | ||
1776 | if (dev != ip6n->fb_tnl_dev) | |
1777 | unregister_netdevice_queue(dev, head); | |
1778 | } | |
1779 | ||
b58d731a | 1780 | static size_t ip6_tnl_get_size(const struct net_device *dev) |
c075b130 ND |
1781 | { |
1782 | return | |
1783 | /* IFLA_IPTUN_LINK */ | |
1784 | nla_total_size(4) + | |
1785 | /* IFLA_IPTUN_LOCAL */ | |
1786 | nla_total_size(sizeof(struct in6_addr)) + | |
1787 | /* IFLA_IPTUN_REMOTE */ | |
1788 | nla_total_size(sizeof(struct in6_addr)) + | |
1789 | /* IFLA_IPTUN_TTL */ | |
1790 | nla_total_size(1) + | |
1791 | /* IFLA_IPTUN_ENCAP_LIMIT */ | |
1792 | nla_total_size(1) + | |
1793 | /* IFLA_IPTUN_FLOWINFO */ | |
1794 | nla_total_size(4) + | |
1795 | /* IFLA_IPTUN_FLAGS */ | |
1796 | nla_total_size(4) + | |
cfa323b6 ND |
1797 | /* IFLA_IPTUN_PROTO */ |
1798 | nla_total_size(1) + | |
c075b130 ND |
1799 | 0; |
1800 | } | |
1801 | ||
b58d731a | 1802 | static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) |
c075b130 ND |
1803 | { |
1804 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1805 | struct __ip6_tnl_parm *parm = &tunnel->parms; | |
1806 | ||
1807 | if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || | |
930345ea JB |
1808 | nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) || |
1809 | nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) || | |
c075b130 ND |
1810 | nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || |
1811 | nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || | |
1812 | nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || | |
cfa323b6 ND |
1813 | nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || |
1814 | nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto)) | |
c075b130 ND |
1815 | goto nla_put_failure; |
1816 | return 0; | |
1817 | ||
1818 | nla_put_failure: | |
1819 | return -EMSGSIZE; | |
1820 | } | |
1821 | ||
1728d4fa ND |
1822 | struct net *ip6_tnl_get_link_net(const struct net_device *dev) |
1823 | { | |
1824 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1825 | ||
1826 | return tunnel->net; | |
1827 | } | |
1828 | EXPORT_SYMBOL(ip6_tnl_get_link_net); | |
1829 | ||
0b112457 ND |
1830 | static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { |
1831 | [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, | |
1832 | [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, | |
1833 | [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, | |
1834 | [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, | |
1835 | [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
1836 | [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, | |
1837 | [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, | |
1838 | [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, | |
1839 | }; | |
1840 | ||
c075b130 ND |
1841 | static struct rtnl_link_ops ip6_link_ops __read_mostly = { |
1842 | .kind = "ip6tnl", | |
1843 | .maxtype = IFLA_IPTUN_MAX, | |
0b112457 | 1844 | .policy = ip6_tnl_policy, |
c075b130 | 1845 | .priv_size = sizeof(struct ip6_tnl), |
0b112457 ND |
1846 | .setup = ip6_tnl_dev_setup, |
1847 | .validate = ip6_tnl_validate, | |
1848 | .newlink = ip6_tnl_newlink, | |
1849 | .changelink = ip6_tnl_changelink, | |
1e9f3d6f | 1850 | .dellink = ip6_tnl_dellink, |
b58d731a ND |
1851 | .get_size = ip6_tnl_get_size, |
1852 | .fill_info = ip6_tnl_fill_info, | |
1728d4fa | 1853 | .get_link_net = ip6_tnl_get_link_net, |
c075b130 ND |
1854 | }; |
1855 | ||
3ff2cfa5 | 1856 | static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { |
c4d3efaf YK |
1857 | .handler = ip4ip6_rcv, |
1858 | .err_handler = ip4ip6_err, | |
1859 | .priority = 1, | |
1860 | }; | |
1861 | ||
3ff2cfa5 | 1862 | static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { |
0303770d PM |
1863 | .handler = ip6ip6_rcv, |
1864 | .err_handler = ip6ip6_err, | |
d2acc347 | 1865 | .priority = 1, |
1da177e4 LT |
1866 | }; |
1867 | ||
1e9f3d6f | 1868 | static void __net_exit ip6_tnl_destroy_tunnels(struct net *net) |
3e6c9fb5 | 1869 | { |
1e9f3d6f | 1870 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
0bd87628 | 1871 | struct net_device *dev, *aux; |
3e6c9fb5 PE |
1872 | int h; |
1873 | struct ip6_tnl *t; | |
cf4432f5 | 1874 | LIST_HEAD(list); |
3e6c9fb5 | 1875 | |
0bd87628 ND |
1876 | for_each_netdev_safe(net, dev, aux) |
1877 | if (dev->rtnl_link_ops == &ip6_link_ops) | |
1878 | unregister_netdevice_queue(dev, &list); | |
1879 | ||
3e6c9fb5 | 1880 | for (h = 0; h < HASH_SIZE; h++) { |
94767632 | 1881 | t = rtnl_dereference(ip6n->tnls_r_l[h]); |
53b24b8f | 1882 | while (t) { |
0bd87628 ND |
1883 | /* If dev is in the same netns, it has already |
1884 | * been added to the list by the previous loop. | |
1885 | */ | |
1886 | if (!net_eq(dev_net(t->dev), net)) | |
1887 | unregister_netdevice_queue(t->dev, &list); | |
94767632 | 1888 | t = rtnl_dereference(t->next); |
cf4432f5 | 1889 | } |
3e6c9fb5 PE |
1890 | } |
1891 | ||
cf4432f5 | 1892 | unregister_netdevice_many(&list); |
3e6c9fb5 PE |
1893 | } |
1894 | ||
2c8c1e72 | 1895 | static int __net_init ip6_tnl_init_net(struct net *net) |
13eeb8e9 | 1896 | { |
ac31cd3c | 1897 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
731abb9c | 1898 | struct ip6_tnl *t = NULL; |
13eeb8e9 | 1899 | int err; |
13eeb8e9 | 1900 | |
3e6c9fb5 PE |
1901 | ip6n->tnls[0] = ip6n->tnls_wc; |
1902 | ip6n->tnls[1] = ip6n->tnls_r_l; | |
1903 | ||
15820e12 PE |
1904 | err = -ENOMEM; |
1905 | ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", | |
c835a677 | 1906 | NET_NAME_UNKNOWN, ip6_tnl_dev_setup); |
15820e12 PE |
1907 | |
1908 | if (!ip6n->fb_tnl_dev) | |
1909 | goto err_alloc_dev; | |
be77e593 | 1910 | dev_net_set(ip6n->fb_tnl_dev, net); |
bb814094 | 1911 | ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; |
0bd87628 ND |
1912 | /* FB netdevice is special: we have one, and only one per netns. |
1913 | * Allowing to move it to another netns is clearly unsafe. | |
1914 | */ | |
1915 | ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL; | |
15820e12 | 1916 | |
8560f226 ED |
1917 | err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); |
1918 | if (err < 0) | |
1919 | goto err_register; | |
15820e12 PE |
1920 | |
1921 | err = register_netdev(ip6n->fb_tnl_dev); | |
1922 | if (err < 0) | |
1923 | goto err_register; | |
731abb9c JB |
1924 | |
1925 | t = netdev_priv(ip6n->fb_tnl_dev); | |
1926 | ||
1927 | strcpy(t->parms.name, ip6n->fb_tnl_dev->name); | |
13eeb8e9 PE |
1928 | return 0; |
1929 | ||
15820e12 | 1930 | err_register: |
8560f226 | 1931 | ip6_dev_free(ip6n->fb_tnl_dev); |
15820e12 | 1932 | err_alloc_dev: |
13eeb8e9 PE |
1933 | return err; |
1934 | } | |
1935 | ||
2c8c1e72 | 1936 | static void __net_exit ip6_tnl_exit_net(struct net *net) |
13eeb8e9 | 1937 | { |
3e6c9fb5 | 1938 | rtnl_lock(); |
1e9f3d6f | 1939 | ip6_tnl_destroy_tunnels(net); |
3e6c9fb5 | 1940 | rtnl_unlock(); |
13eeb8e9 PE |
1941 | } |
1942 | ||
1943 | static struct pernet_operations ip6_tnl_net_ops = { | |
1944 | .init = ip6_tnl_init_net, | |
1945 | .exit = ip6_tnl_exit_net, | |
ac31cd3c EB |
1946 | .id = &ip6_tnl_net_id, |
1947 | .size = sizeof(struct ip6_tnl_net), | |
13eeb8e9 PE |
1948 | }; |
1949 | ||
1da177e4 LT |
1950 | /** |
1951 | * ip6_tunnel_init - register protocol and reserve needed resources | |
1952 | * | |
1953 | * Return: 0 on success | |
1954 | **/ | |
1955 | ||
1956 | static int __init ip6_tunnel_init(void) | |
1957 | { | |
1958 | int err; | |
1959 | ||
d5aa407f AD |
1960 | err = register_pernet_device(&ip6_tnl_net_ops); |
1961 | if (err < 0) | |
1962 | goto out_pernet; | |
1963 | ||
1964 | err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); | |
1965 | if (err < 0) { | |
f3213831 | 1966 | pr_err("%s: can't register ip4ip6\n", __func__); |
d5aa407f | 1967 | goto out_ip4ip6; |
c4d3efaf YK |
1968 | } |
1969 | ||
d5aa407f AD |
1970 | err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); |
1971 | if (err < 0) { | |
f3213831 | 1972 | pr_err("%s: can't register ip6ip6\n", __func__); |
d5aa407f | 1973 | goto out_ip6ip6; |
1da177e4 | 1974 | } |
c075b130 ND |
1975 | err = rtnl_link_register(&ip6_link_ops); |
1976 | if (err < 0) | |
1977 | goto rtnl_link_failed; | |
13eeb8e9 | 1978 | |
1da177e4 | 1979 | return 0; |
d5aa407f | 1980 | |
c075b130 ND |
1981 | rtnl_link_failed: |
1982 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); | |
d5aa407f | 1983 | out_ip6ip6: |
c4d3efaf | 1984 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
d5aa407f AD |
1985 | out_ip4ip6: |
1986 | unregister_pernet_device(&ip6_tnl_net_ops); | |
1987 | out_pernet: | |
1da177e4 LT |
1988 | return err; |
1989 | } | |
1990 | ||
1991 | /** | |
1992 | * ip6_tunnel_cleanup - free resources and unregister protocol | |
1993 | **/ | |
1994 | ||
1995 | static void __exit ip6_tunnel_cleanup(void) | |
1996 | { | |
c075b130 | 1997 | rtnl_link_unregister(&ip6_link_ops); |
c4d3efaf | 1998 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
f3213831 | 1999 | pr_info("%s: can't deregister ip4ip6\n", __func__); |
c4d3efaf | 2000 | |
73d605d1 | 2001 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
f3213831 | 2002 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
1da177e4 | 2003 | |
ac31cd3c | 2004 | unregister_pernet_device(&ip6_tnl_net_ops); |
1da177e4 LT |
2005 | } |
2006 | ||
2007 | module_init(ip6_tunnel_init); | |
2008 | module_exit(ip6_tunnel_cleanup); |