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