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