blob: 2a8e3deeb2c54705fc4a56c99eb837c7bbcf5b46 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * IPv6 tunneling device
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
8 *
9 * Based on:
10 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
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 * Changes:
20 * Steven Barth <cyrus@openwrt.org>: MAP-E FMR support
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/capability.h>
27#include <linux/errno.h>
28#include <linux/types.h>
29#include <linux/sockios.h>
30#include <linux/icmp.h>
31#include <linux/if.h>
32#include <linux/in.h>
33#include <linux/ip.h>
34#include <linux/net.h>
35#include <linux/in6.h>
36#include <linux/netdevice.h>
37#include <linux/if_arp.h>
38#include <linux/icmpv6.h>
39#include <linux/init.h>
40#include <linux/route.h>
41#include <linux/rtnetlink.h>
42#include <linux/netfilter_ipv6.h>
43#include <linux/slab.h>
44#include <linux/hash.h>
45#include <linux/etherdevice.h>
46
47#include <linux/uaccess.h>
48#include <linux/atomic.h>
49
50#include <net/icmp.h>
51#include <net/ip.h>
52#include <net/ip_tunnels.h>
53#include <net/ipv6.h>
54#include <net/ip6_route.h>
55#include <net/addrconf.h>
56#include <net/ip6_tunnel.h>
57#include <net/xfrm.h>
58#include <net/dsfield.h>
59#include <net/inet_ecn.h>
60#include <net/net_namespace.h>
61#include <net/netns/generic.h>
62#include <net/dst_metadata.h>
63
64MODULE_AUTHOR("Ville Nuorvala");
65MODULE_DESCRIPTION("IPv6 tunneling device");
66MODULE_LICENSE("GPL");
67MODULE_ALIAS_RTNL_LINK("ip6tnl");
68MODULE_ALIAS_NETDEV("ip6tnl0");
69
70#define IP6_TUNNEL_HASH_SIZE_SHIFT 5
71#define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
72
73static bool log_ecn_error = true;
74module_param(log_ecn_error, bool, 0644);
75MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
76
77static u32 HASH(const struct in6_addr *addr)
78{
79 u32 hash = ipv6_addr_hash(addr);
80
81 return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
82}
83
84static int ip6_tnl_dev_init(struct net_device *dev);
85static void ip6_tnl_dev_setup(struct net_device *dev);
86static struct rtnl_link_ops ip6_link_ops __read_mostly;
87
88static unsigned int ip6_tnl_net_id __read_mostly;
89struct ip6_tnl_net {
90 /* the IPv6 tunnel fallback device */
91 struct net_device *fb_tnl_dev;
92 /* lists for storing tunnels in use */
93 struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
94 struct ip6_tnl __rcu *tnls_wc[1];
95 struct ip6_tnl __rcu **tnls[2];
96 struct ip6_tnl __rcu *collect_md_tun;
97};
98
99static struct net_device_stats *ip6_get_stats(struct net_device *dev)
100{
101 struct pcpu_sw_netstats tmp, sum = { 0 };
102 int i;
103
104 for_each_possible_cpu(i) {
105 unsigned int start;
106 const struct pcpu_sw_netstats *tstats =
107 per_cpu_ptr(dev->tstats, i);
108
109 do {
110 start = u64_stats_fetch_begin_irq(&tstats->syncp);
111 tmp.rx_packets = tstats->rx_packets;
112 tmp.rx_bytes = tstats->rx_bytes;
113 tmp.tx_packets = tstats->tx_packets;
114 tmp.tx_bytes = tstats->tx_bytes;
115 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
116
117 sum.rx_packets += tmp.rx_packets;
118 sum.rx_bytes += tmp.rx_bytes;
119 sum.tx_packets += tmp.tx_packets;
120 sum.tx_bytes += tmp.tx_bytes;
121 }
122 dev->stats.rx_packets = sum.rx_packets;
123 dev->stats.rx_bytes = sum.rx_bytes;
124 dev->stats.tx_packets = sum.tx_packets;
125 dev->stats.tx_bytes = sum.tx_bytes;
126 return &dev->stats;
127}
128
129/**
130 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
131 * @remote: the address of the tunnel exit-point
132 * @local: the address of the tunnel entry-point
133 *
134 * Return:
135 * tunnel matching given end-points if found,
136 * else fallback tunnel if its device is up,
137 * else %NULL
138 **/
139
140#define for_each_ip6_tunnel_rcu(start) \
141 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
142
143static struct ip6_tnl *
144ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
145{
146 unsigned int hash = HASH(local);
147 struct ip6_tnl *t;
148 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
149 struct in6_addr any;
150 struct __ip6_tnl_fmr *fmr;
151
152 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
153 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
154 !(t->dev->flags & IFF_UP))
155 continue;
156
157 if (ipv6_addr_equal(remote, &t->parms.raddr))
158 return t;
159
160 for (fmr = t->parms.fmrs; fmr; fmr = fmr->next) {
161 if (ipv6_prefix_equal(remote, &fmr->ip6_prefix,
162 fmr->ip6_prefix_len))
163 return t;
164 }
165 }
166
167 memset(&any, 0, sizeof(any));
168 hash = HASH(local);
169 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
170 if (ipv6_addr_equal(local, &t->parms.laddr) &&
171 ipv6_addr_any(&t->parms.raddr) &&
172 (t->dev->flags & IFF_UP))
173 return t;
174 }
175
176 hash = HASH(&any);
177 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
178 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
179 ipv6_addr_any(&t->parms.laddr) &&
180 (t->dev->flags & IFF_UP))
181 return t;
182 }
183
184 t = rcu_dereference(ip6n->collect_md_tun);
185 if (t && t->dev->flags & IFF_UP)
186 return t;
187
188 t = rcu_dereference(ip6n->tnls_wc[0]);
189 if (t && (t->dev->flags & IFF_UP))
190 return t;
191
192 return NULL;
193}
194
195/**
196 * ip6_tnl_bucket - get head of list matching given tunnel parameters
197 * @p: parameters containing tunnel end-points
198 *
199 * Description:
200 * ip6_tnl_bucket() returns the head of the list matching the
201 * &struct in6_addr entries laddr and raddr in @p.
202 *
203 * Return: head of IPv6 tunnel list
204 **/
205
206static struct ip6_tnl __rcu **
207ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
208{
209 const struct in6_addr *remote = &p->raddr;
210 const struct in6_addr *local = &p->laddr;
211 unsigned int h = 0;
212 int prio = 0;
213
214 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
215 prio = 1;
216 h = HASH(local);
217 }
218 return &ip6n->tnls[prio][h];
219}
220
221/**
222 * ip6_tnl_link - add tunnel to hash table
223 * @t: tunnel to be added
224 **/
225
226static void
227ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
228{
229 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
230
231 if (t->parms.collect_md)
232 rcu_assign_pointer(ip6n->collect_md_tun, t);
233 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
234 rcu_assign_pointer(*tp, t);
235}
236
237/**
238 * ip6_tnl_unlink - remove tunnel from hash table
239 * @t: tunnel to be removed
240 **/
241
242static void
243ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
244{
245 struct ip6_tnl __rcu **tp;
246 struct ip6_tnl *iter;
247
248 if (t->parms.collect_md)
249 rcu_assign_pointer(ip6n->collect_md_tun, NULL);
250
251 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
252 (iter = rtnl_dereference(*tp)) != NULL;
253 tp = &iter->next) {
254 if (t == iter) {
255 rcu_assign_pointer(*tp, t->next);
256 break;
257 }
258 }
259}
260
261static void ip6_dev_free(struct net_device *dev)
262{
263 struct ip6_tnl *t = netdev_priv(dev);
264
265 gro_cells_destroy(&t->gro_cells);
266 dst_cache_destroy(&t->dst_cache);
267 free_percpu(dev->tstats);
268}
269
270static int ip6_tnl_create2(struct net_device *dev)
271{
272 struct ip6_tnl *t = netdev_priv(dev);
273 struct net *net = dev_net(dev);
274 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
275 int err;
276
277 t = netdev_priv(dev);
278
279 dev->rtnl_link_ops = &ip6_link_ops;
280 err = register_netdevice(dev);
281 if (err < 0)
282 goto out;
283
284 strcpy(t->parms.name, dev->name);
285
286 dev_hold(dev);
287 ip6_tnl_link(ip6n, t);
288 return 0;
289
290out:
291 return err;
292}
293
294/**
295 * ip6_tnl_create - create a new tunnel
296 * @p: tunnel parameters
297 * @pt: pointer to new tunnel
298 *
299 * Description:
300 * Create tunnel matching given parameters.
301 *
302 * Return:
303 * created tunnel or error pointer
304 **/
305
306static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
307{
308 struct net_device *dev;
309 struct ip6_tnl *t;
310 char name[IFNAMSIZ];
311 int err = -E2BIG;
312
313 if (p->name[0]) {
314 if (!dev_valid_name(p->name))
315 goto failed;
316 strlcpy(name, p->name, IFNAMSIZ);
317 } else {
318 sprintf(name, "ip6tnl%%d");
319 }
320 err = -ENOMEM;
321 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
322 ip6_tnl_dev_setup);
323 if (!dev)
324 goto failed;
325
326 dev_net_set(dev, net);
327
328 t = netdev_priv(dev);
329 t->parms = *p;
330 t->net = dev_net(dev);
331 err = ip6_tnl_create2(dev);
332 if (err < 0)
333 goto failed_free;
334
335 return t;
336
337failed_free:
338 free_netdev(dev);
339failed:
340 return ERR_PTR(err);
341}
342
343/**
344 * ip6_tnl_locate - find or create tunnel matching given parameters
345 * @p: tunnel parameters
346 * @create: != 0 if allowed to create new tunnel if no match found
347 *
348 * Description:
349 * ip6_tnl_locate() first tries to locate an existing tunnel
350 * based on @parms. If this is unsuccessful, but @create is set a new
351 * tunnel device is created and registered for use.
352 *
353 * Return:
354 * matching tunnel or error pointer
355 **/
356
357static struct ip6_tnl *ip6_tnl_locate(struct net *net,
358 struct __ip6_tnl_parm *p, int create)
359{
360 const struct in6_addr *remote = &p->raddr;
361 const struct in6_addr *local = &p->laddr;
362 struct ip6_tnl __rcu **tp;
363 struct ip6_tnl *t;
364 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
365
366 for (tp = ip6_tnl_bucket(ip6n, p);
367 (t = rtnl_dereference(*tp)) != NULL;
368 tp = &t->next) {
369 if (ipv6_addr_equal(local, &t->parms.laddr) &&
370 ipv6_addr_equal(remote, &t->parms.raddr)) {
371 if (create)
372 return ERR_PTR(-EEXIST);
373
374 return t;
375 }
376 }
377 if (!create)
378 return ERR_PTR(-ENODEV);
379 return ip6_tnl_create(net, p);
380}
381
382/**
383 * ip6_tnl_dev_uninit - tunnel device uninitializer
384 * @dev: the device to be destroyed
385 *
386 * Description:
387 * ip6_tnl_dev_uninit() removes tunnel from its list
388 **/
389
390static void
391ip6_tnl_dev_uninit(struct net_device *dev)
392{
393 struct ip6_tnl *t = netdev_priv(dev);
394 struct net *net = t->net;
395 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
396
397 while (t->parms.fmrs) {
398 struct __ip6_tnl_fmr *next = t->parms.fmrs->next;
399 kfree(t->parms.fmrs);
400 t->parms.fmrs = next;
401 }
402
403 if (dev == ip6n->fb_tnl_dev)
404 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
405 else
406 ip6_tnl_unlink(ip6n, t);
407 dst_cache_reset(&t->dst_cache);
408 dev_put(dev);
409}
410
411/**
412 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
413 * @skb: received socket buffer
414 *
415 * Return:
416 * 0 if none was found,
417 * else index to encapsulation limit
418 **/
419
420__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
421{
422 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
423 unsigned int nhoff = raw - skb->data;
424 unsigned int off = nhoff + sizeof(*ipv6h);
425 u8 next, nexthdr = ipv6h->nexthdr;
426
427 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
428 struct ipv6_opt_hdr *hdr;
429 u16 optlen;
430
431 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
432 break;
433
434 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
435 if (nexthdr == NEXTHDR_FRAGMENT) {
436 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
437 if (frag_hdr->frag_off)
438 break;
439 optlen = 8;
440 } else if (nexthdr == NEXTHDR_AUTH) {
441 optlen = (hdr->hdrlen + 2) << 2;
442 } else {
443 optlen = ipv6_optlen(hdr);
444 }
445 /* cache hdr->nexthdr, since pskb_may_pull() might
446 * invalidate hdr
447 */
448 next = hdr->nexthdr;
449 if (nexthdr == NEXTHDR_DEST) {
450 u16 i = 2;
451
452 /* Remember : hdr is no longer valid at this point. */
453 if (!pskb_may_pull(skb, off + optlen))
454 break;
455
456 while (1) {
457 struct ipv6_tlv_tnl_enc_lim *tel;
458
459 /* No more room for encapsulation limit */
460 if (i + sizeof(*tel) > optlen)
461 break;
462
463 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
464 /* return index of option if found and valid */
465 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
466 tel->length == 1)
467 return i + off - nhoff;
468 /* else jump to next option */
469 if (tel->type)
470 i += tel->length + 2;
471 else
472 i++;
473 }
474 }
475 nexthdr = next;
476 off += optlen;
477 }
478 return 0;
479}
480EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
481
482/**
483 * ip6_tnl_err - tunnel error handler
484 *
485 * Description:
486 * ip6_tnl_err() should handle errors in the tunnel according
487 * to the specifications in RFC 2473.
488 **/
489
490static int
491ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
492 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
493{
494 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
495 struct net *net = dev_net(skb->dev);
496 u8 rel_type = ICMPV6_DEST_UNREACH;
497 u8 rel_code = ICMPV6_ADDR_UNREACH;
498 __u32 rel_info = 0;
499 struct ip6_tnl *t;
500 int err = -ENOENT;
501 int rel_msg = 0;
502 u8 tproto;
503 __u16 len;
504
505 /* If the packet doesn't contain the original IPv6 header we are
506 in trouble since we might need the source address for further
507 processing of the error. */
508
509 rcu_read_lock();
510 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
511 if (!t)
512 goto out;
513
514 tproto = READ_ONCE(t->parms.proto);
515 if (tproto != ipproto && tproto != 0)
516 goto out;
517
518 err = 0;
519
520 switch (*type) {
521 struct ipv6_tlv_tnl_enc_lim *tel;
522 __u32 mtu, teli;
523 case ICMPV6_DEST_UNREACH:
524 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
525 t->parms.name);
526 rel_msg = 1;
527 break;
528 case ICMPV6_TIME_EXCEED:
529 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
530 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
531 t->parms.name);
532 rel_msg = 1;
533 }
534 break;
535 case ICMPV6_PARAMPROB:
536 teli = 0;
537 if ((*code) == ICMPV6_HDR_FIELD)
538 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
539
540 if (teli && teli == *info - 2) {
541 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
542 if (tel->encap_limit == 0) {
543 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
544 t->parms.name);
545 rel_msg = 1;
546 }
547 } else {
548 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
549 t->parms.name);
550 }
551 break;
552 case ICMPV6_PKT_TOOBIG:
553 ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
554 sock_net_uid(net, NULL));
555 mtu = *info - offset;
556 if (mtu < IPV6_MIN_MTU)
557 mtu = IPV6_MIN_MTU;
558 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
559 if (len > mtu) {
560 rel_type = ICMPV6_PKT_TOOBIG;
561 rel_code = 0;
562 rel_info = mtu;
563 rel_msg = 1;
564 }
565 break;
566 case NDISC_REDIRECT:
567 ip6_redirect(skb, net, skb->dev->ifindex, 0,
568 sock_net_uid(net, NULL));
569 break;
570 }
571
572 *type = rel_type;
573 *code = rel_code;
574 *info = rel_info;
575 *msg = rel_msg;
576
577out:
578 rcu_read_unlock();
579 return err;
580}
581
582static int
583ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
584 u8 type, u8 code, int offset, __be32 info)
585{
586 __u32 rel_info = ntohl(info);
587 const struct iphdr *eiph;
588 struct sk_buff *skb2;
589 int err, rel_msg = 0;
590 u8 rel_type = type;
591 u8 rel_code = code;
592 struct rtable *rt;
593 struct flowi4 fl4;
594
595 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
596 &rel_msg, &rel_info, offset);
597 if (err < 0)
598 return err;
599
600 if (rel_msg == 0)
601 return 0;
602
603 switch (rel_type) {
604 case ICMPV6_DEST_UNREACH:
605 if (rel_code != ICMPV6_ADDR_UNREACH)
606 return 0;
607 rel_type = ICMP_DEST_UNREACH;
608 rel_code = ICMP_HOST_UNREACH;
609 break;
610 case ICMPV6_PKT_TOOBIG:
611 if (rel_code != 0)
612 return 0;
613 rel_type = ICMP_DEST_UNREACH;
614 rel_code = ICMP_FRAG_NEEDED;
615 break;
616 default:
617 return 0;
618 }
619
620 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
621 return 0;
622
623 skb2 = skb_clone(skb, GFP_ATOMIC);
624 if (!skb2)
625 return 0;
626
627 skb_dst_drop(skb2);
628
629 skb_pull(skb2, offset);
630 skb_reset_network_header(skb2);
631 eiph = ip_hdr(skb2);
632
633 /* Try to guess incoming interface */
634 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
635 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
636 if (IS_ERR(rt))
637 goto out;
638
639 skb2->dev = rt->dst.dev;
640 ip_rt_put(rt);
641
642 /* route "incoming" packet */
643 if (rt->rt_flags & RTCF_LOCAL) {
644 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
645 eiph->daddr, eiph->saddr, 0, 0,
646 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
647 if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) {
648 if (!IS_ERR(rt))
649 ip_rt_put(rt);
650 goto out;
651 }
652 skb_dst_set(skb2, &rt->dst);
653 } else {
654 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
655 skb2->dev) ||
656 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6)
657 goto out;
658 }
659
660 /* change mtu on this route */
661 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
662 if (rel_info > dst_mtu(skb_dst(skb2)))
663 goto out;
664
665 skb_dst_update_pmtu_no_confirm(skb2, rel_info);
666 }
667
668 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
669
670out:
671 kfree_skb(skb2);
672 return 0;
673}
674
675static int
676ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
677 u8 type, u8 code, int offset, __be32 info)
678{
679 __u32 rel_info = ntohl(info);
680 int err, rel_msg = 0;
681 u8 rel_type = type;
682 u8 rel_code = code;
683
684 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
685 &rel_msg, &rel_info, offset);
686 if (err < 0)
687 return err;
688
689 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
690 struct rt6_info *rt;
691 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
692
693 if (!skb2)
694 return 0;
695
696 skb_dst_drop(skb2);
697 skb_pull(skb2, offset);
698 skb_reset_network_header(skb2);
699
700 /* Try to guess incoming interface */
701 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
702 NULL, 0, skb2, 0);
703
704 if (rt && rt->dst.dev)
705 skb2->dev = rt->dst.dev;
706
707 icmpv6_send(skb2, rel_type, rel_code, rel_info);
708
709 ip6_rt_put(rt);
710
711 kfree_skb(skb2);
712 }
713
714 return 0;
715}
716
717static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
718 const struct ipv6hdr *ipv6h,
719 struct sk_buff *skb)
720{
721 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
722
723 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
724 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
725
726 return IP6_ECN_decapsulate(ipv6h, skb);
727}
728
729static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
730 const struct ipv6hdr *ipv6h,
731 struct sk_buff *skb)
732{
733 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
734 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
735
736 return IP6_ECN_decapsulate(ipv6h, skb);
737}
738
739__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
740 const struct in6_addr *laddr,
741 const struct in6_addr *raddr)
742{
743 struct __ip6_tnl_parm *p = &t->parms;
744 int ltype = ipv6_addr_type(laddr);
745 int rtype = ipv6_addr_type(raddr);
746 __u32 flags = 0;
747
748 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
749 flags = IP6_TNL_F_CAP_PER_PACKET;
750 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
751 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
752 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
753 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
754 if (ltype&IPV6_ADDR_UNICAST)
755 flags |= IP6_TNL_F_CAP_XMIT;
756 if (rtype&IPV6_ADDR_UNICAST)
757 flags |= IP6_TNL_F_CAP_RCV;
758 }
759 return flags;
760}
761EXPORT_SYMBOL(ip6_tnl_get_cap);
762
763/* called with rcu_read_lock() */
764int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
765 const struct in6_addr *laddr,
766 const struct in6_addr *raddr)
767{
768 struct __ip6_tnl_parm *p = &t->parms;
769 int ret = 0;
770 struct net *net = t->net;
771
772 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
773 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
774 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
775 struct net_device *ldev = NULL;
776
777 if (p->link)
778 ldev = dev_get_by_index_rcu(net, p->link);
779
780 if ((ipv6_addr_is_multicast(laddr) ||
781 likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
782 0, IFA_F_TENTATIVE))) &&
783 ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
784 likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
785 0, IFA_F_TENTATIVE))))
786 ret = 1;
787 }
788 return ret;
789}
790EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
791
792/**
793 * ip4ip6_fmr_calc - calculate target / source IPv6-address based on FMR
794 * @dest: destination IPv6 address buffer
795 * @skb: received socket buffer
796 * @fmr: MAP FMR
797 * @xmit: Calculate for xmit or rcv
798 **/
799static void ip4ip6_fmr_calc(struct in6_addr *dest,
800 const struct iphdr *iph, const uint8_t *end,
801 const struct __ip6_tnl_fmr *fmr, bool xmit)
802{
803 int psidlen = fmr->ea_len - (32 - fmr->ip4_prefix_len);
804 u8 *portp = NULL;
805 bool use_dest_addr;
806 const struct iphdr *dsth = iph;
807
808 if ((u8*)dsth >= end)
809 return;
810
811 /* find significant IP header */
812 if (iph->protocol == IPPROTO_ICMP) {
813 struct icmphdr *ih = (struct icmphdr*)(((u8*)dsth) + dsth->ihl * 4);
814 if (ih && ((u8*)&ih[1]) <= end && (
815 ih->type == ICMP_DEST_UNREACH ||
816 ih->type == ICMP_SOURCE_QUENCH ||
817 ih->type == ICMP_TIME_EXCEEDED ||
818 ih->type == ICMP_PARAMETERPROB ||
819 ih->type == ICMP_REDIRECT))
820 dsth = (const struct iphdr*)&ih[1];
821 }
822
823 /* in xmit-path use dest port by default and source port only if
824 this is an ICMP reply to something else; vice versa in rcv-path */
825 use_dest_addr = (xmit && dsth == iph) || (!xmit && dsth != iph);
826
827 /* get dst port */
828 if (((u8*)&dsth[1]) <= end && (
829 dsth->protocol == IPPROTO_UDP ||
830 dsth->protocol == IPPROTO_TCP ||
831 dsth->protocol == IPPROTO_SCTP ||
832 dsth->protocol == IPPROTO_DCCP)) {
833 /* for UDP, TCP, SCTP and DCCP source and dest port
834 follow IPv4 header directly */
835 portp = ((u8*)dsth) + dsth->ihl * 4;
836
837 if (use_dest_addr)
838 portp += sizeof(u16);
839 } else if (iph->protocol == IPPROTO_ICMP) {
840 struct icmphdr *ih = (struct icmphdr*)(((u8*)dsth) + dsth->ihl * 4);
841
842 /* use icmp identifier as port */
843 if (((u8*)&ih) <= end && (
844 (use_dest_addr && (
845 ih->type == ICMP_ECHOREPLY ||
846 ih->type == ICMP_TIMESTAMPREPLY ||
847 ih->type == ICMP_INFO_REPLY ||
848 ih->type == ICMP_ADDRESSREPLY)) ||
849 (!use_dest_addr && (
850 ih->type == ICMP_ECHO ||
851 ih->type == ICMP_TIMESTAMP ||
852 ih->type == ICMP_INFO_REQUEST ||
853 ih->type == ICMP_ADDRESS)
854 )))
855 portp = (u8*)&ih->un.echo.id;
856 }
857
858 if ((portp && &portp[2] <= end) || psidlen == 0) {
859 int frombyte = fmr->ip6_prefix_len / 8;
860 int fromrem = fmr->ip6_prefix_len % 8;
861 int bytes = sizeof(struct in6_addr) - frombyte;
862 const u32 *addr = (use_dest_addr) ? &iph->daddr : &iph->saddr;
863 u64 eabits = ((u64)ntohl(*addr)) << (32 + fmr->ip4_prefix_len);
864 u64 t = 0;
865
866 /* extract PSID from port and add it to eabits */
867 u16 psidbits = 0;
868 if (psidlen > 0) {
869 psidbits = ((u16)portp[0]) << 8 | ((u16)portp[1]);
870 psidbits >>= 16 - psidlen - fmr->offset;
871 psidbits = (u16)(psidbits << (16 - psidlen));
872 eabits |= ((u64)psidbits) << (48 - (fmr->ea_len - psidlen));
873 }
874
875 /* rewrite destination address */
876 *dest = fmr->ip6_prefix;
877 memcpy(&dest->s6_addr[10], addr, sizeof(*addr));
878 dest->s6_addr16[7] = htons(psidbits >> (16 - psidlen));
879
880 if (bytes > sizeof(u64))
881 bytes = sizeof(u64);
882
883 /* insert eabits */
884 memcpy(&t, &dest->s6_addr[frombyte], bytes);
885 t = be64_to_cpu(t) & ~(((((u64)1) << fmr->ea_len) - 1)
886 << (64 - fmr->ea_len - fromrem));
887 t = cpu_to_be64(t | (eabits >> fromrem));
888 memcpy(&dest->s6_addr[frombyte], &t, bytes);
889 }
890}
891
892
893static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
894 const struct tnl_ptk_info *tpi,
895 struct metadata_dst *tun_dst,
896 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
897 const struct ipv6hdr *ipv6h,
898 struct sk_buff *skb),
899 bool log_ecn_err)
900{
901 struct pcpu_sw_netstats *tstats;
902 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
903 int err;
904
905 if ((!(tpi->flags & TUNNEL_CSUM) &&
906 (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
907 ((tpi->flags & TUNNEL_CSUM) &&
908 !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
909 tunnel->dev->stats.rx_crc_errors++;
910 tunnel->dev->stats.rx_errors++;
911 goto drop;
912 }
913
914 if (tunnel->parms.i_flags & TUNNEL_SEQ) {
915 if (!(tpi->flags & TUNNEL_SEQ) ||
916 (tunnel->i_seqno &&
917 (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
918 tunnel->dev->stats.rx_fifo_errors++;
919 tunnel->dev->stats.rx_errors++;
920 goto drop;
921 }
922 tunnel->i_seqno = ntohl(tpi->seq) + 1;
923 }
924
925 skb->protocol = tpi->proto;
926
927 /* Warning: All skb pointers will be invalidated! */
928 if (tunnel->dev->type == ARPHRD_ETHER) {
929 if (!pskb_may_pull(skb, ETH_HLEN)) {
930 tunnel->dev->stats.rx_length_errors++;
931 tunnel->dev->stats.rx_errors++;
932 goto drop;
933 }
934
935 ipv6h = ipv6_hdr(skb);
936 skb->protocol = eth_type_trans(skb, tunnel->dev);
937 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
938 } else {
939 skb->dev = tunnel->dev;
940 }
941
942 skb_reset_network_header(skb);
943 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
944
945 if (tpi->proto == htons(ETH_P_IP) && tunnel->parms.fmrs &&
946 !ipv6_addr_equal(&ipv6h->saddr, &tunnel->parms.raddr)) {
947 /* Packet didn't come from BR, so lookup FMR */
948 struct __ip6_tnl_fmr *fmr;
949 struct in6_addr expected = tunnel->parms.raddr;
950 for (fmr = tunnel->parms.fmrs; fmr; fmr = fmr->next)
951 if (ipv6_prefix_equal(&ipv6h->saddr,
952 &fmr->ip6_prefix, fmr->ip6_prefix_len))
953 break;
954
955 /* Check that IPv6 matches IPv4 source to prevent spoofing */
956 if (fmr)
957 ip4ip6_fmr_calc(&expected, ip_hdr(skb),
958 skb_tail_pointer(skb), fmr, false);
959
960 if (!ipv6_addr_equal(&ipv6h->saddr, &expected)) {
961 rcu_read_unlock();
962 goto drop;
963 }
964 }
965
966 __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
967
968 err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
969 if (unlikely(err)) {
970 if (log_ecn_err)
971 net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
972 &ipv6h->saddr,
973 ipv6_get_dsfield(ipv6h));
974 if (err > 1) {
975 ++tunnel->dev->stats.rx_frame_errors;
976 ++tunnel->dev->stats.rx_errors;
977 goto drop;
978 }
979 }
980
981 tstats = this_cpu_ptr(tunnel->dev->tstats);
982 u64_stats_update_begin(&tstats->syncp);
983 tstats->rx_packets++;
984 tstats->rx_bytes += skb->len;
985 u64_stats_update_end(&tstats->syncp);
986
987 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
988
989 if (tun_dst)
990 skb_dst_set(skb, (struct dst_entry *)tun_dst);
991
992 gro_cells_receive(&tunnel->gro_cells, skb);
993 return 0;
994
995drop:
996 if (tun_dst)
997 dst_release((struct dst_entry *)tun_dst);
998 kfree_skb(skb);
999 return 0;
1000}
1001
1002int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
1003 const struct tnl_ptk_info *tpi,
1004 struct metadata_dst *tun_dst,
1005 bool log_ecn_err)
1006{
1007 return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate,
1008 log_ecn_err);
1009}
1010EXPORT_SYMBOL(ip6_tnl_rcv);
1011
1012static const struct tnl_ptk_info tpi_v6 = {
1013 /* no tunnel info required for ipxip6. */
1014 .proto = htons(ETH_P_IPV6),
1015};
1016
1017static const struct tnl_ptk_info tpi_v4 = {
1018 /* no tunnel info required for ipxip6. */
1019 .proto = htons(ETH_P_IP),
1020};
1021
1022static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
1023 const struct tnl_ptk_info *tpi,
1024 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
1025 const struct ipv6hdr *ipv6h,
1026 struct sk_buff *skb))
1027{
1028 struct ip6_tnl *t;
1029 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1030 struct metadata_dst *tun_dst = NULL;
1031 int ret = -1;
1032
1033 rcu_read_lock();
1034 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
1035
1036 if (t) {
1037 u8 tproto = READ_ONCE(t->parms.proto);
1038
1039 if (tproto != ipproto && tproto != 0)
1040 goto drop;
1041 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1042 goto drop;
1043 ipv6h = ipv6_hdr(skb);
1044 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
1045 goto drop;
1046 if (iptunnel_pull_header(skb, 0, tpi->proto, false))
1047 goto drop;
1048 if (t->parms.collect_md) {
1049 tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
1050 if (!tun_dst)
1051 goto drop;
1052 }
1053 ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
1054 log_ecn_error);
1055 }
1056
1057 rcu_read_unlock();
1058
1059 return ret;
1060
1061drop:
1062 rcu_read_unlock();
1063 kfree_skb(skb);
1064 return 0;
1065}
1066
1067static int ip4ip6_rcv(struct sk_buff *skb)
1068{
1069 return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
1070 ip4ip6_dscp_ecn_decapsulate);
1071}
1072
1073static int ip6ip6_rcv(struct sk_buff *skb)
1074{
1075 return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
1076 ip6ip6_dscp_ecn_decapsulate);
1077}
1078
1079struct ipv6_tel_txoption {
1080 struct ipv6_txoptions ops;
1081 __u8 dst_opt[8];
1082};
1083
1084static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
1085{
1086 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
1087
1088 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
1089 opt->dst_opt[3] = 1;
1090 opt->dst_opt[4] = encap_limit;
1091 opt->dst_opt[5] = IPV6_TLV_PADN;
1092 opt->dst_opt[6] = 1;
1093
1094 opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
1095 opt->ops.opt_nflen = 8;
1096}
1097
1098
1099/**
1100 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
1101 * @t: the outgoing tunnel device
1102 * @hdr: IPv6 header from the incoming packet
1103 *
1104 * Description:
1105 * Avoid trivial tunneling loop by checking that tunnel exit-point
1106 * doesn't match source of incoming packet.
1107 *
1108 * Return:
1109 * 1 if conflict,
1110 * 0 else
1111 **/
1112
1113static inline bool
1114ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
1115{
1116 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
1117}
1118
1119int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
1120 const struct in6_addr *laddr,
1121 const struct in6_addr *raddr)
1122{
1123 struct __ip6_tnl_parm *p = &t->parms;
1124 int ret = 0;
1125 struct net *net = t->net;
1126
1127 if (t->parms.collect_md)
1128 return 1;
1129
1130 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
1131 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
1132 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
1133 struct net_device *ldev = NULL;
1134
1135 rcu_read_lock();
1136 if (p->link)
1137 ldev = dev_get_by_index_rcu(net, p->link);
1138
1139 if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
1140 0, IFA_F_TENTATIVE)))
1141 pr_warn("%s xmit: Local address not yet configured!\n",
1142 p->name);
1143 else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
1144 !ipv6_addr_is_multicast(raddr) &&
1145 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
1146 true, 0, IFA_F_TENTATIVE)))
1147 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1148 p->name);
1149 else
1150 ret = 1;
1151 rcu_read_unlock();
1152 }
1153 return ret;
1154}
1155EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1156
1157/**
1158 * ip6_tnl_xmit - encapsulate packet and send
1159 * @skb: the outgoing socket buffer
1160 * @dev: the outgoing tunnel device
1161 * @dsfield: dscp code for outer header
1162 * @fl6: flow of tunneled packet
1163 * @encap_limit: encapsulation limit
1164 * @pmtu: Path MTU is stored if packet is too big
1165 * @proto: next header value
1166 *
1167 * Description:
1168 * Build new header and do some sanity checks on the packet before sending
1169 * it.
1170 *
1171 * Return:
1172 * 0 on success
1173 * -1 fail
1174 * %-EMSGSIZE message too big. return mtu in this case.
1175 **/
1176
1177int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
1178 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
1179 __u8 proto)
1180{
1181 struct ip6_tnl *t = netdev_priv(dev);
1182 struct net *net = t->net;
1183 struct net_device_stats *stats = &t->dev->stats;
1184 struct ipv6hdr *ipv6h;
1185 struct ipv6_tel_txoption opt;
1186 struct dst_entry *dst = NULL, *ndst = NULL;
1187 struct net_device *tdev;
1188 int mtu;
1189 unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
1190 unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
1191 unsigned int max_headroom = psh_hlen;
1192 bool use_cache = false;
1193 u8 hop_limit;
1194 int err = -1;
1195
1196 if (t->parms.collect_md) {
1197 hop_limit = skb_tunnel_info(skb)->key.ttl;
1198 goto route_lookup;
1199 } else {
1200 hop_limit = t->parms.hop_limit;
1201 }
1202
1203 /* NBMA tunnel */
1204 if (ipv6_addr_any(&t->parms.raddr)) {
1205 if (skb->protocol == htons(ETH_P_IPV6)) {
1206 struct in6_addr *addr6;
1207 struct neighbour *neigh;
1208 int addr_type;
1209
1210 if (!skb_dst(skb))
1211 goto tx_err_link_failure;
1212
1213 neigh = dst_neigh_lookup(skb_dst(skb),
1214 &ipv6_hdr(skb)->daddr);
1215 if (!neigh)
1216 goto tx_err_link_failure;
1217
1218 addr6 = (struct in6_addr *)&neigh->primary_key;
1219 addr_type = ipv6_addr_type(addr6);
1220
1221 if (addr_type == IPV6_ADDR_ANY)
1222 addr6 = &ipv6_hdr(skb)->daddr;
1223
1224 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1225 neigh_release(neigh);
1226 }
1227 } else if (t->parms.proto != 0 && !(t->parms.flags &
1228 (IP6_TNL_F_USE_ORIG_TCLASS |
1229 IP6_TNL_F_USE_ORIG_FWMARK))) {
1230 /* enable the cache only if neither the outer protocol nor the
1231 * routing decision depends on the current inner header value
1232 */
1233 use_cache = true;
1234 }
1235
1236 if (use_cache)
1237 dst = dst_cache_get(&t->dst_cache);
1238
1239 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1240 goto tx_err_link_failure;
1241
1242 if (!dst) {
1243route_lookup:
1244 /* add dsfield to flowlabel for route lookup */
1245 fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
1246
1247 dst = ip6_route_output(net, NULL, fl6);
1248
1249 if (dst->error)
1250 goto tx_err_link_failure;
1251 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1252 if (IS_ERR(dst)) {
1253 err = PTR_ERR(dst);
1254 dst = NULL;
1255 goto tx_err_link_failure;
1256 }
1257 if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) &&
1258 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
1259 &fl6->daddr, 0, &fl6->saddr))
1260 goto tx_err_link_failure;
1261 ndst = dst;
1262 }
1263
1264 tdev = dst->dev;
1265
1266 if (tdev == dev) {
1267 stats->collisions++;
1268 net_warn_ratelimited("%s: Local routing loop detected!\n",
1269 t->parms.name);
1270 goto tx_err_dst_release;
1271 }
1272 mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
1273 if (encap_limit >= 0) {
1274 max_headroom += 8;
1275 mtu -= 8;
1276 }
1277 mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ?
1278 IPV6_MIN_MTU : IPV4_MIN_MTU);
1279
1280 skb_dst_update_pmtu_no_confirm(skb, mtu);
1281 if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
1282 *pmtu = mtu;
1283 err = -EMSGSIZE;
1284 goto tx_err_dst_release;
1285 }
1286
1287 if (t->err_count > 0) {
1288 if (time_before(jiffies,
1289 t->err_time + IP6TUNNEL_ERR_TIMEO)) {
1290 t->err_count--;
1291
1292 dst_link_failure(skb);
1293 } else {
1294 t->err_count = 0;
1295 }
1296 }
1297
1298 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1299
1300 /*
1301 * Okay, now see if we can stuff it in the buffer as-is.
1302 */
1303 max_headroom += LL_RESERVED_SPACE(tdev);
1304
1305 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1306 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1307 struct sk_buff *new_skb;
1308
1309 new_skb = skb_realloc_headroom(skb, max_headroom);
1310 if (!new_skb)
1311 goto tx_err_dst_release;
1312
1313 if (skb->sk)
1314 skb_set_owner_w(new_skb, skb->sk);
1315 consume_skb(skb);
1316 skb = new_skb;
1317 }
1318
1319 if (t->parms.collect_md) {
1320 if (t->encap.type != TUNNEL_ENCAP_NONE)
1321 goto tx_err_dst_release;
1322 } else {
1323 if (use_cache && ndst)
1324 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1325 }
1326 skb_dst_set(skb, dst);
1327
1328 if (hop_limit == 0) {
1329 if (skb->protocol == htons(ETH_P_IP))
1330 hop_limit = ip_hdr(skb)->ttl;
1331 else if (skb->protocol == htons(ETH_P_IPV6))
1332 hop_limit = ipv6_hdr(skb)->hop_limit;
1333 else
1334 hop_limit = ip6_dst_hoplimit(dst);
1335 }
1336
1337 /* Calculate max headroom for all the headers and adjust
1338 * needed_headroom if necessary.
1339 */
1340 max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
1341 + dst->header_len + t->hlen;
1342 if (max_headroom > dev->needed_headroom)
1343 dev->needed_headroom = max_headroom;
1344
1345 err = ip6_tnl_encap(skb, t, &proto, fl6);
1346 if (err)
1347 return err;
1348
1349 if (encap_limit >= 0) {
1350 init_tel_txopt(&opt, encap_limit);
1351 ipv6_push_frag_opts(skb, &opt.ops, &proto);
1352 }
1353
1354 skb_push(skb, sizeof(struct ipv6hdr));
1355 skb_reset_network_header(skb);
1356 ipv6h = ipv6_hdr(skb);
1357 ip6_flow_hdr(ipv6h, dsfield,
1358 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1359 ipv6h->hop_limit = hop_limit;
1360 ipv6h->nexthdr = proto;
1361 ipv6h->saddr = fl6->saddr;
1362 ipv6h->daddr = fl6->daddr;
1363 ip6tunnel_xmit(NULL, skb, dev);
1364 return 0;
1365tx_err_link_failure:
1366 stats->tx_carrier_errors++;
1367 dst_link_failure(skb);
1368tx_err_dst_release:
1369 dst_release(dst);
1370 return err;
1371}
1372EXPORT_SYMBOL(ip6_tnl_xmit);
1373
1374static inline int
1375ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1376{
1377 struct ip6_tnl *t = netdev_priv(dev);
1378 const struct iphdr *iph;
1379 int encap_limit = -1;
1380 struct flowi6 fl6;
1381 __u8 dsfield;
1382 __u32 mtu;
1383 u8 tproto;
1384 int err;
1385
1386 iph = ip_hdr(skb);
1387 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1388
1389 tproto = READ_ONCE(t->parms.proto);
1390 if (tproto != IPPROTO_IPIP && tproto != 0)
1391 return -1;
1392
1393 if (t->parms.collect_md) {
1394 struct ip_tunnel_info *tun_info;
1395 const struct ip_tunnel_key *key;
1396
1397 tun_info = skb_tunnel_info(skb);
1398 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1399 ip_tunnel_info_af(tun_info) != AF_INET6))
1400 return -1;
1401 key = &tun_info->key;
1402 memset(&fl6, 0, sizeof(fl6));
1403 fl6.flowi6_proto = IPPROTO_IPIP;
1404 fl6.saddr = key->u.ipv6.src;
1405 fl6.daddr = key->u.ipv6.dst;
1406 fl6.flowlabel = key->label;
1407 dsfield = key->tos;
1408 } else {
1409 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1410 encap_limit = t->parms.encap_limit;
1411
1412 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1413 fl6.flowi6_proto = IPPROTO_IPIP;
1414
1415 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1416 dsfield = ipv4_get_dsfield(iph);
1417 else
1418 dsfield = ip6_tclass(t->parms.flowinfo);
1419 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1420 fl6.flowi6_mark = skb->mark;
1421 else
1422 fl6.flowi6_mark = t->parms.fwmark;
1423 }
1424
1425 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1426 dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
1427
1428 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1429 return -1;
1430
1431 skb_set_inner_ipproto(skb, IPPROTO_IPIP);
1432
1433 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1434 IPPROTO_IPIP);
1435 if (err != 0) {
1436 /* XXX: send ICMP error even if DF is not set. */
1437 if (err == -EMSGSIZE)
1438 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1439 htonl(mtu));
1440 return -1;
1441 }
1442
1443 return 0;
1444}
1445
1446static inline int
1447ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1448{
1449 struct ip6_tnl *t = netdev_priv(dev);
1450 struct ipv6hdr *ipv6h;
1451 struct __ip6_tnl_fmr *fmr;
1452 int encap_limit = -1;
1453 __u16 offset;
1454 struct flowi6 fl6;
1455 __u8 dsfield;
1456 __u32 mtu;
1457 u8 tproto;
1458 int err;
1459
1460 ipv6h = ipv6_hdr(skb);
1461 tproto = READ_ONCE(t->parms.proto);
1462 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1463 ip6_tnl_addr_conflict(t, ipv6h))
1464 return -1;
1465
1466 if (t->parms.collect_md) {
1467 struct ip_tunnel_info *tun_info;
1468 const struct ip_tunnel_key *key;
1469
1470 tun_info = skb_tunnel_info(skb);
1471 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1472 ip_tunnel_info_af(tun_info) != AF_INET6))
1473 return -1;
1474 key = &tun_info->key;
1475 memset(&fl6, 0, sizeof(fl6));
1476 fl6.flowi6_proto = IPPROTO_IPV6;
1477 fl6.saddr = key->u.ipv6.src;
1478 fl6.daddr = key->u.ipv6.dst;
1479 fl6.flowlabel = key->label;
1480 dsfield = key->tos;
1481 } else {
1482 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1483 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
1484 ipv6h = ipv6_hdr(skb);
1485 if (offset > 0) {
1486 struct ipv6_tlv_tnl_enc_lim *tel;
1487
1488 tel = (void *)&skb_network_header(skb)[offset];
1489 if (tel->encap_limit == 0) {
1490 icmpv6_send(skb, ICMPV6_PARAMPROB,
1491 ICMPV6_HDR_FIELD, offset + 2);
1492 return -1;
1493 }
1494 encap_limit = tel->encap_limit - 1;
1495 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
1496 encap_limit = t->parms.encap_limit;
1497 }
1498
1499 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1500 fl6.flowi6_proto = IPPROTO_IPV6;
1501
1502 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1503 dsfield = ipv6_get_dsfield(ipv6h);
1504 else
1505 dsfield = ip6_tclass(t->parms.flowinfo);
1506 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1507 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1508 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1509 fl6.flowi6_mark = skb->mark;
1510 else
1511 fl6.flowi6_mark = t->parms.fwmark;
1512 }
1513
1514 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1515 dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
1516
1517 /* try to find matching FMR */
1518 for (fmr = t->parms.fmrs; fmr; fmr = fmr->next) {
1519 unsigned mshift = 32 - fmr->ip4_prefix_len;
1520 if (ntohl(fmr->ip4_prefix.s_addr) >> mshift ==
1521 ntohl(ip_hdr(skb)->daddr) >> mshift)
1522 break;
1523 }
1524
1525 /* change dstaddr according to FMR */
1526 if (fmr)
1527 ip4ip6_fmr_calc(&fl6.daddr, ip_hdr(skb), skb_tail_pointer(skb), fmr, true);
1528
1529 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1530 return -1;
1531
1532 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1533
1534 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1535 IPPROTO_IPV6);
1536 if (err != 0) {
1537 if (err == -EMSGSIZE)
1538 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1539 return -1;
1540 }
1541
1542 return 0;
1543}
1544
1545static netdev_tx_t
1546ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
1547{
1548 struct ip6_tnl *t = netdev_priv(dev);
1549 struct net_device_stats *stats = &t->dev->stats;
1550 int ret;
1551
1552 if (!pskb_inet_may_pull(skb))
1553 goto tx_err;
1554
1555 switch (skb->protocol) {
1556 case htons(ETH_P_IP):
1557 ret = ip4ip6_tnl_xmit(skb, dev);
1558 break;
1559 case htons(ETH_P_IPV6):
1560 ret = ip6ip6_tnl_xmit(skb, dev);
1561 break;
1562 default:
1563 goto tx_err;
1564 }
1565
1566 if (ret < 0)
1567 goto tx_err;
1568
1569 return NETDEV_TX_OK;
1570
1571tx_err:
1572 stats->tx_errors++;
1573 stats->tx_dropped++;
1574 kfree_skb(skb);
1575 return NETDEV_TX_OK;
1576}
1577
1578static void ip6_tnl_link_config(struct ip6_tnl *t)
1579{
1580 struct net_device *dev = t->dev;
1581 struct __ip6_tnl_parm *p = &t->parms;
1582 struct flowi6 *fl6 = &t->fl.u.ip6;
1583 int t_hlen;
1584
1585 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1586 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1587
1588 /* Set up flowi template */
1589 fl6->saddr = p->laddr;
1590 fl6->daddr = p->raddr;
1591 fl6->flowi6_oif = p->link;
1592 fl6->flowlabel = 0;
1593
1594 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1595 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1596 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1597 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1598
1599 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1600 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1601
1602 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1603 dev->flags |= IFF_POINTOPOINT;
1604 else
1605 dev->flags &= ~IFF_POINTOPOINT;
1606
1607 t->tun_hlen = 0;
1608 t->hlen = t->encap_hlen + t->tun_hlen;
1609 t_hlen = t->hlen + sizeof(struct ipv6hdr);
1610
1611 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1612 int strict = (ipv6_addr_type(&p->raddr) &
1613 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1614
1615 struct rt6_info *rt = rt6_lookup(t->net,
1616 &p->raddr, &p->laddr,
1617 p->link, NULL, strict);
1618
1619 if (!rt)
1620 return;
1621
1622 if (rt->dst.dev) {
1623 dev->hard_header_len = rt->dst.dev->hard_header_len +
1624 t_hlen;
1625
1626 dev->mtu = rt->dst.dev->mtu - t_hlen;
1627 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1628 dev->mtu -= 8;
1629
1630 if (dev->mtu < IPV6_MIN_MTU)
1631 dev->mtu = IPV6_MIN_MTU;
1632 }
1633 ip6_rt_put(rt);
1634 }
1635}
1636
1637/**
1638 * ip6_tnl_change - update the tunnel parameters
1639 * @t: tunnel to be changed
1640 * @p: tunnel configuration parameters
1641 *
1642 * Description:
1643 * ip6_tnl_change() updates the tunnel parameters
1644 **/
1645
1646static int
1647ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1648{
1649 t->parms.laddr = p->laddr;
1650 t->parms.raddr = p->raddr;
1651 t->parms.flags = p->flags;
1652 t->parms.hop_limit = p->hop_limit;
1653 t->parms.encap_limit = p->encap_limit;
1654 t->parms.flowinfo = p->flowinfo;
1655 t->parms.link = p->link;
1656 t->parms.proto = p->proto;
1657 t->parms.fwmark = p->fwmark;
1658
1659 while (t->parms.fmrs) {
1660 struct __ip6_tnl_fmr *next = t->parms.fmrs->next;
1661 kfree(t->parms.fmrs);
1662 t->parms.fmrs = next;
1663 }
1664 t->parms.fmrs = p->fmrs;
1665
1666 dst_cache_reset(&t->dst_cache);
1667 ip6_tnl_link_config(t);
1668 return 0;
1669}
1670
1671static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1672{
1673 struct net *net = t->net;
1674 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1675 int err;
1676
1677 ip6_tnl_unlink(ip6n, t);
1678 synchronize_net();
1679 err = ip6_tnl_change(t, p);
1680 ip6_tnl_link(ip6n, t);
1681 netdev_state_change(t->dev);
1682 return err;
1683}
1684
1685static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1686{
1687 /* for default tnl0 device allow to change only the proto */
1688 t->parms.proto = p->proto;
1689 netdev_state_change(t->dev);
1690 return 0;
1691}
1692
1693static void
1694ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1695{
1696 p->laddr = u->laddr;
1697 p->raddr = u->raddr;
1698 p->flags = u->flags;
1699 p->hop_limit = u->hop_limit;
1700 p->encap_limit = u->encap_limit;
1701 p->flowinfo = u->flowinfo;
1702 p->link = u->link;
1703 p->proto = u->proto;
1704 p->fmrs = NULL;
1705 memcpy(p->name, u->name, sizeof(u->name));
1706}
1707
1708static void
1709ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1710{
1711 u->laddr = p->laddr;
1712 u->raddr = p->raddr;
1713 u->flags = p->flags;
1714 u->hop_limit = p->hop_limit;
1715 u->encap_limit = p->encap_limit;
1716 u->flowinfo = p->flowinfo;
1717 u->link = p->link;
1718 u->proto = p->proto;
1719 memcpy(u->name, p->name, sizeof(u->name));
1720}
1721
1722/**
1723 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1724 * @dev: virtual device associated with tunnel
1725 * @ifr: parameters passed from userspace
1726 * @cmd: command to be performed
1727 *
1728 * Description:
1729 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1730 * from userspace.
1731 *
1732 * The possible commands are the following:
1733 * %SIOCGETTUNNEL: get tunnel parameters for device
1734 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1735 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1736 * %SIOCDELTUNNEL: delete tunnel
1737 *
1738 * The fallback device "ip6tnl0", created during module
1739 * initialization, can be used for creating other tunnel devices.
1740 *
1741 * Return:
1742 * 0 on success,
1743 * %-EFAULT if unable to copy data to or from userspace,
1744 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1745 * %-EINVAL if passed tunnel parameters are invalid,
1746 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1747 * %-ENODEV if attempting to change or delete a nonexisting device
1748 **/
1749
1750static int
1751ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1752{
1753 int err = 0;
1754 struct ip6_tnl_parm p;
1755 struct __ip6_tnl_parm p1;
1756 struct ip6_tnl *t = netdev_priv(dev);
1757 struct net *net = t->net;
1758 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1759
1760 memset(&p1, 0, sizeof(p1));
1761
1762 switch (cmd) {
1763 case SIOCGETTUNNEL:
1764 if (dev == ip6n->fb_tnl_dev) {
1765 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1766 err = -EFAULT;
1767 break;
1768 }
1769 ip6_tnl_parm_from_user(&p1, &p);
1770 t = ip6_tnl_locate(net, &p1, 0);
1771 if (IS_ERR(t))
1772 t = netdev_priv(dev);
1773 } else {
1774 memset(&p, 0, sizeof(p));
1775 }
1776 ip6_tnl_parm_to_user(&p, &t->parms);
1777 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1778 err = -EFAULT;
1779 }
1780 break;
1781 case SIOCADDTUNNEL:
1782 case SIOCCHGTUNNEL:
1783 err = -EPERM;
1784 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1785 break;
1786 err = -EFAULT;
1787 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1788 break;
1789 err = -EINVAL;
1790 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1791 p.proto != 0)
1792 break;
1793 ip6_tnl_parm_from_user(&p1, &p);
1794 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1795 if (cmd == SIOCCHGTUNNEL) {
1796 if (!IS_ERR(t)) {
1797 if (t->dev != dev) {
1798 err = -EEXIST;
1799 break;
1800 }
1801 } else
1802 t = netdev_priv(dev);
1803 if (dev == ip6n->fb_tnl_dev)
1804 err = ip6_tnl0_update(t, &p1);
1805 else
1806 err = ip6_tnl_update(t, &p1);
1807 }
1808 if (!IS_ERR(t)) {
1809 err = 0;
1810 ip6_tnl_parm_to_user(&p, &t->parms);
1811 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1812 err = -EFAULT;
1813
1814 } else {
1815 err = PTR_ERR(t);
1816 }
1817 break;
1818 case SIOCDELTUNNEL:
1819 err = -EPERM;
1820 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1821 break;
1822
1823 if (dev == ip6n->fb_tnl_dev) {
1824 err = -EFAULT;
1825 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1826 break;
1827 err = -ENOENT;
1828 ip6_tnl_parm_from_user(&p1, &p);
1829 t = ip6_tnl_locate(net, &p1, 0);
1830 if (IS_ERR(t))
1831 break;
1832 err = -EPERM;
1833 if (t->dev == ip6n->fb_tnl_dev)
1834 break;
1835 dev = t->dev;
1836 }
1837 err = 0;
1838 unregister_netdevice(dev);
1839 break;
1840 default:
1841 err = -EINVAL;
1842 }
1843 return err;
1844}
1845
1846/**
1847 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1848 * @dev: virtual device associated with tunnel
1849 * @new_mtu: the new mtu
1850 *
1851 * Return:
1852 * 0 on success,
1853 * %-EINVAL if mtu too small
1854 **/
1855
1856int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1857{
1858 struct ip6_tnl *tnl = netdev_priv(dev);
1859
1860 if (tnl->parms.proto == IPPROTO_IPV6) {
1861 if (new_mtu < IPV6_MIN_MTU)
1862 return -EINVAL;
1863 } else {
1864 if (new_mtu < ETH_MIN_MTU)
1865 return -EINVAL;
1866 }
1867 if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
1868 if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
1869 return -EINVAL;
1870 } else {
1871 if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
1872 return -EINVAL;
1873 }
1874 dev->mtu = new_mtu;
1875 return 0;
1876}
1877EXPORT_SYMBOL(ip6_tnl_change_mtu);
1878
1879int ip6_tnl_get_iflink(const struct net_device *dev)
1880{
1881 struct ip6_tnl *t = netdev_priv(dev);
1882
1883 return t->parms.link;
1884}
1885EXPORT_SYMBOL(ip6_tnl_get_iflink);
1886
1887int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
1888 unsigned int num)
1889{
1890 if (num >= MAX_IPTUN_ENCAP_OPS)
1891 return -ERANGE;
1892
1893 return !cmpxchg((const struct ip6_tnl_encap_ops **)
1894 &ip6tun_encaps[num],
1895 NULL, ops) ? 0 : -1;
1896}
1897EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
1898
1899int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
1900 unsigned int num)
1901{
1902 int ret;
1903
1904 if (num >= MAX_IPTUN_ENCAP_OPS)
1905 return -ERANGE;
1906
1907 ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
1908 &ip6tun_encaps[num],
1909 ops, NULL) == ops) ? 0 : -1;
1910
1911 synchronize_net();
1912
1913 return ret;
1914}
1915EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
1916
1917int ip6_tnl_encap_setup(struct ip6_tnl *t,
1918 struct ip_tunnel_encap *ipencap)
1919{
1920 int hlen;
1921
1922 memset(&t->encap, 0, sizeof(t->encap));
1923
1924 hlen = ip6_encap_hlen(ipencap);
1925 if (hlen < 0)
1926 return hlen;
1927
1928 t->encap.type = ipencap->type;
1929 t->encap.sport = ipencap->sport;
1930 t->encap.dport = ipencap->dport;
1931 t->encap.flags = ipencap->flags;
1932
1933 t->encap_hlen = hlen;
1934 t->hlen = t->encap_hlen + t->tun_hlen;
1935
1936 return 0;
1937}
1938EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
1939
1940static const struct net_device_ops ip6_tnl_netdev_ops = {
1941 .ndo_init = ip6_tnl_dev_init,
1942 .ndo_uninit = ip6_tnl_dev_uninit,
1943 .ndo_start_xmit = ip6_tnl_start_xmit,
1944 .ndo_do_ioctl = ip6_tnl_ioctl,
1945 .ndo_change_mtu = ip6_tnl_change_mtu,
1946 .ndo_get_stats = ip6_get_stats,
1947 .ndo_get_iflink = ip6_tnl_get_iflink,
1948};
1949
1950#define IPXIPX_FEATURES (NETIF_F_SG | \
1951 NETIF_F_FRAGLIST | \
1952 NETIF_F_HIGHDMA | \
1953 NETIF_F_GSO_SOFTWARE | \
1954 NETIF_F_HW_CSUM)
1955
1956/**
1957 * ip6_tnl_dev_setup - setup virtual tunnel device
1958 * @dev: virtual device associated with tunnel
1959 *
1960 * Description:
1961 * Initialize function pointers and device parameters
1962 **/
1963
1964static void ip6_tnl_dev_setup(struct net_device *dev)
1965{
1966 dev->netdev_ops = &ip6_tnl_netdev_ops;
1967 dev->needs_free_netdev = true;
1968 dev->priv_destructor = ip6_dev_free;
1969
1970 dev->type = ARPHRD_TUNNEL6;
1971 dev->flags |= IFF_NOARP;
1972 dev->addr_len = sizeof(struct in6_addr);
1973 dev->features |= NETIF_F_LLTX;
1974 netif_keep_dst(dev);
1975
1976 dev->features |= IPXIPX_FEATURES;
1977 dev->hw_features |= IPXIPX_FEATURES;
1978
1979 /* This perm addr will be used as interface identifier by IPv6 */
1980 dev->addr_assign_type = NET_ADDR_RANDOM;
1981 eth_random_addr(dev->perm_addr);
1982}
1983
1984
1985/**
1986 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1987 * @dev: virtual device associated with tunnel
1988 **/
1989
1990static inline int
1991ip6_tnl_dev_init_gen(struct net_device *dev)
1992{
1993 struct ip6_tnl *t = netdev_priv(dev);
1994 int ret;
1995 int t_hlen;
1996
1997 t->dev = dev;
1998 t->net = dev_net(dev);
1999 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2000 if (!dev->tstats)
2001 return -ENOMEM;
2002
2003 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
2004 if (ret)
2005 goto free_stats;
2006
2007 ret = gro_cells_init(&t->gro_cells, dev);
2008 if (ret)
2009 goto destroy_dst;
2010
2011 t->tun_hlen = 0;
2012 t->hlen = t->encap_hlen + t->tun_hlen;
2013 t_hlen = t->hlen + sizeof(struct ipv6hdr);
2014
2015 dev->type = ARPHRD_TUNNEL6;
2016 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
2017 dev->mtu = ETH_DATA_LEN - t_hlen;
2018 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
2019 dev->mtu -= 8;
2020 dev->min_mtu = ETH_MIN_MTU;
2021 dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
2022
2023 return 0;
2024
2025destroy_dst:
2026 dst_cache_destroy(&t->dst_cache);
2027free_stats:
2028 free_percpu(dev->tstats);
2029 dev->tstats = NULL;
2030
2031 return ret;
2032}
2033
2034/**
2035 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
2036 * @dev: virtual device associated with tunnel
2037 **/
2038
2039static int ip6_tnl_dev_init(struct net_device *dev)
2040{
2041 struct ip6_tnl *t = netdev_priv(dev);
2042 int err = ip6_tnl_dev_init_gen(dev);
2043
2044 if (err)
2045 return err;
2046 ip6_tnl_link_config(t);
2047 if (t->parms.collect_md) {
2048 dev->features |= NETIF_F_NETNS_LOCAL;
2049 netif_keep_dst(dev);
2050 }
2051 return 0;
2052}
2053
2054/**
2055 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
2056 * @dev: fallback device
2057 *
2058 * Return: 0
2059 **/
2060
2061static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
2062{
2063 struct ip6_tnl *t = netdev_priv(dev);
2064 struct net *net = dev_net(dev);
2065 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2066
2067 t->parms.proto = IPPROTO_IPV6;
2068 dev_hold(dev);
2069
2070 rcu_assign_pointer(ip6n->tnls_wc[0], t);
2071 return 0;
2072}
2073
2074static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
2075 struct netlink_ext_ack *extack)
2076{
2077 u8 proto;
2078
2079 if (!data || !data[IFLA_IPTUN_PROTO])
2080 return 0;
2081
2082 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
2083 if (proto != IPPROTO_IPV6 &&
2084 proto != IPPROTO_IPIP &&
2085 proto != 0)
2086 return -EINVAL;
2087
2088 return 0;
2089}
2090
2091static const struct nla_policy ip6_tnl_fmr_policy[IFLA_IPTUN_FMR_MAX + 1] = {
2092 [IFLA_IPTUN_FMR_IP6_PREFIX] = { .len = sizeof(struct in6_addr) },
2093 [IFLA_IPTUN_FMR_IP4_PREFIX] = { .len = sizeof(struct in_addr) },
2094 [IFLA_IPTUN_FMR_IP6_PREFIX_LEN] = { .type = NLA_U8 },
2095 [IFLA_IPTUN_FMR_IP4_PREFIX_LEN] = { .type = NLA_U8 },
2096 [IFLA_IPTUN_FMR_EA_LEN] = { .type = NLA_U8 },
2097 [IFLA_IPTUN_FMR_OFFSET] = { .type = NLA_U8 }
2098};
2099
2100static void ip6_tnl_netlink_parms(struct nlattr *data[],
2101 struct __ip6_tnl_parm *parms)
2102{
2103 memset(parms, 0, sizeof(*parms));
2104
2105 if (!data)
2106 return;
2107
2108 if (data[IFLA_IPTUN_LINK])
2109 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
2110
2111 if (data[IFLA_IPTUN_LOCAL])
2112 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
2113
2114 if (data[IFLA_IPTUN_REMOTE])
2115 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
2116
2117 if (data[IFLA_IPTUN_TTL])
2118 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
2119
2120 if (data[IFLA_IPTUN_ENCAP_LIMIT])
2121 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
2122
2123 if (data[IFLA_IPTUN_FLOWINFO])
2124 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
2125
2126 if (data[IFLA_IPTUN_FLAGS])
2127 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
2128
2129 if (data[IFLA_IPTUN_PROTO])
2130 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
2131
2132 if (data[IFLA_IPTUN_COLLECT_METADATA])
2133 parms->collect_md = true;
2134
2135 if (data[IFLA_IPTUN_FWMARK])
2136 parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
2137
2138 if (data[IFLA_IPTUN_FMRS]) {
2139 unsigned rem;
2140 struct nlattr *fmr;
2141 nla_for_each_nested(fmr, data[IFLA_IPTUN_FMRS], rem) {
2142 struct nlattr *fmrd[IFLA_IPTUN_FMR_MAX + 1], *c;
2143 struct __ip6_tnl_fmr *nfmr;
2144
2145 nla_parse_nested(fmrd, IFLA_IPTUN_FMR_MAX,
2146 fmr, ip6_tnl_fmr_policy, NULL);
2147
2148 if (!(nfmr = kzalloc(sizeof(*nfmr), GFP_KERNEL)))
2149 continue;
2150
2151 nfmr->offset = 6;
2152
2153 if ((c = fmrd[IFLA_IPTUN_FMR_IP6_PREFIX]))
2154 nla_memcpy(&nfmr->ip6_prefix, fmrd[IFLA_IPTUN_FMR_IP6_PREFIX],
2155 sizeof(nfmr->ip6_prefix));
2156
2157 if ((c = fmrd[IFLA_IPTUN_FMR_IP4_PREFIX]))
2158 nla_memcpy(&nfmr->ip4_prefix, fmrd[IFLA_IPTUN_FMR_IP4_PREFIX],
2159 sizeof(nfmr->ip4_prefix));
2160
2161 if ((c = fmrd[IFLA_IPTUN_FMR_IP6_PREFIX_LEN]))
2162 nfmr->ip6_prefix_len = nla_get_u8(c);
2163
2164 if ((c = fmrd[IFLA_IPTUN_FMR_IP4_PREFIX_LEN]))
2165 nfmr->ip4_prefix_len = nla_get_u8(c);
2166
2167 if ((c = fmrd[IFLA_IPTUN_FMR_EA_LEN]))
2168 nfmr->ea_len = nla_get_u8(c);
2169
2170 if ((c = fmrd[IFLA_IPTUN_FMR_OFFSET]))
2171 nfmr->offset = nla_get_u8(c);
2172
2173 nfmr->next = parms->fmrs;
2174 parms->fmrs = nfmr;
2175 }
2176 }
2177}
2178
2179static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
2180 struct ip_tunnel_encap *ipencap)
2181{
2182 bool ret = false;
2183
2184 memset(ipencap, 0, sizeof(*ipencap));
2185
2186 if (!data)
2187 return ret;
2188
2189 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
2190 ret = true;
2191 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
2192 }
2193
2194 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
2195 ret = true;
2196 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
2197 }
2198
2199 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
2200 ret = true;
2201 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
2202 }
2203
2204 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
2205 ret = true;
2206 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
2207 }
2208
2209 return ret;
2210}
2211
2212static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
2213 struct nlattr *tb[], struct nlattr *data[],
2214 struct netlink_ext_ack *extack)
2215{
2216 struct net *net = dev_net(dev);
2217 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2218 struct ip_tunnel_encap ipencap;
2219 struct ip6_tnl *nt, *t;
2220 int err;
2221
2222 nt = netdev_priv(dev);
2223
2224 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2225 err = ip6_tnl_encap_setup(nt, &ipencap);
2226 if (err < 0)
2227 return err;
2228 }
2229
2230 ip6_tnl_netlink_parms(data, &nt->parms);
2231
2232 if (nt->parms.collect_md) {
2233 if (rtnl_dereference(ip6n->collect_md_tun))
2234 return -EEXIST;
2235 } else {
2236 t = ip6_tnl_locate(net, &nt->parms, 0);
2237 if (!IS_ERR(t))
2238 return -EEXIST;
2239 }
2240
2241 err = ip6_tnl_create2(dev);
2242 if (!err && tb[IFLA_MTU])
2243 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2244
2245 return err;
2246}
2247
2248static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
2249 struct nlattr *data[],
2250 struct netlink_ext_ack *extack)
2251{
2252 struct ip6_tnl *t = netdev_priv(dev);
2253 struct __ip6_tnl_parm p;
2254 struct net *net = t->net;
2255 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2256 struct ip_tunnel_encap ipencap;
2257
2258 if (dev == ip6n->fb_tnl_dev)
2259 return -EINVAL;
2260
2261 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2262 int err = ip6_tnl_encap_setup(t, &ipencap);
2263
2264 if (err < 0)
2265 return err;
2266 }
2267 ip6_tnl_netlink_parms(data, &p);
2268 if (p.collect_md)
2269 return -EINVAL;
2270
2271 t = ip6_tnl_locate(net, &p, 0);
2272 if (!IS_ERR(t)) {
2273 if (t->dev != dev)
2274 return -EEXIST;
2275 } else
2276 t = netdev_priv(dev);
2277
2278 return ip6_tnl_update(t, &p);
2279}
2280
2281static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
2282{
2283 struct net *net = dev_net(dev);
2284 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2285
2286 if (dev != ip6n->fb_tnl_dev)
2287 unregister_netdevice_queue(dev, head);
2288}
2289
2290static size_t ip6_tnl_get_size(const struct net_device *dev)
2291{
2292 const struct ip6_tnl *t = netdev_priv(dev);
2293 struct __ip6_tnl_fmr *c;
2294 int fmrs = 0;
2295 for (c = t->parms.fmrs; c; c = c->next)
2296 ++fmrs;
2297
2298 return
2299 /* IFLA_IPTUN_LINK */
2300 nla_total_size(4) +
2301 /* IFLA_IPTUN_LOCAL */
2302 nla_total_size(sizeof(struct in6_addr)) +
2303 /* IFLA_IPTUN_REMOTE */
2304 nla_total_size(sizeof(struct in6_addr)) +
2305 /* IFLA_IPTUN_TTL */
2306 nla_total_size(1) +
2307 /* IFLA_IPTUN_ENCAP_LIMIT */
2308 nla_total_size(1) +
2309 /* IFLA_IPTUN_FLOWINFO */
2310 nla_total_size(4) +
2311 /* IFLA_IPTUN_FLAGS */
2312 nla_total_size(4) +
2313 /* IFLA_IPTUN_PROTO */
2314 nla_total_size(1) +
2315 /* IFLA_IPTUN_ENCAP_TYPE */
2316 nla_total_size(2) +
2317 /* IFLA_IPTUN_ENCAP_FLAGS */
2318 nla_total_size(2) +
2319 /* IFLA_IPTUN_ENCAP_SPORT */
2320 nla_total_size(2) +
2321 /* IFLA_IPTUN_ENCAP_DPORT */
2322 nla_total_size(2) +
2323 /* IFLA_IPTUN_COLLECT_METADATA */
2324 nla_total_size(0) +
2325 /* IFLA_IPTUN_FWMARK */
2326 nla_total_size(4) +
2327 /* IFLA_IPTUN_FMRS */
2328 nla_total_size(0) +
2329 (
2330 /* nest */
2331 nla_total_size(0) +
2332 /* IFLA_IPTUN_FMR_IP6_PREFIX */
2333 nla_total_size(sizeof(struct in6_addr)) +
2334 /* IFLA_IPTUN_FMR_IP4_PREFIX */
2335 nla_total_size(sizeof(struct in_addr)) +
2336 /* IFLA_IPTUN_FMR_EA_LEN */
2337 nla_total_size(1) +
2338 /* IFLA_IPTUN_FMR_IP6_PREFIX_LEN */
2339 nla_total_size(1) +
2340 /* IFLA_IPTUN_FMR_IP4_PREFIX_LEN */
2341 nla_total_size(1) +
2342 /* IFLA_IPTUN_FMR_OFFSET */
2343 nla_total_size(1)
2344 ) * fmrs +
2345 0;
2346}
2347
2348static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
2349{
2350 struct ip6_tnl *tunnel = netdev_priv(dev);
2351 struct __ip6_tnl_parm *parm = &tunnel->parms;
2352 struct __ip6_tnl_fmr *c;
2353 int fmrcnt = 0;
2354 struct nlattr *fmrs;
2355
2356 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
2357 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
2358 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
2359 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
2360 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
2361 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
2362 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
2363 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
2364 nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark) ||
2365 !(fmrs = nla_nest_start(skb, IFLA_IPTUN_FMRS)))
2366 goto nla_put_failure;
2367
2368 for (c = parm->fmrs; c; c = c->next) {
2369 struct nlattr *fmr = nla_nest_start(skb, ++fmrcnt);
2370 if (!fmr ||
2371 nla_put(skb, IFLA_IPTUN_FMR_IP6_PREFIX,
2372 sizeof(c->ip6_prefix), &c->ip6_prefix) ||
2373 nla_put(skb, IFLA_IPTUN_FMR_IP4_PREFIX,
2374 sizeof(c->ip4_prefix), &c->ip4_prefix) ||
2375 nla_put_u8(skb, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, c->ip6_prefix_len) ||
2376 nla_put_u8(skb, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, c->ip4_prefix_len) ||
2377 nla_put_u8(skb, IFLA_IPTUN_FMR_EA_LEN, c->ea_len) ||
2378 nla_put_u8(skb, IFLA_IPTUN_FMR_OFFSET, c->offset))
2379 goto nla_put_failure;
2380
2381 nla_nest_end(skb, fmr);
2382 }
2383 nla_nest_end(skb, fmrs);
2384
2385 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
2386 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
2387 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
2388 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
2389 goto nla_put_failure;
2390
2391 if (parm->collect_md)
2392 if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
2393 goto nla_put_failure;
2394
2395 return 0;
2396
2397nla_put_failure:
2398 return -EMSGSIZE;
2399}
2400
2401struct net *ip6_tnl_get_link_net(const struct net_device *dev)
2402{
2403 struct ip6_tnl *tunnel = netdev_priv(dev);
2404
2405 return tunnel->net;
2406}
2407EXPORT_SYMBOL(ip6_tnl_get_link_net);
2408
2409static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
2410 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
2411 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
2412 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
2413 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
2414 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
2415 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
2416 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
2417 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
2418 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
2419 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
2420 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
2421 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
2422 [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
2423 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
2424 [IFLA_IPTUN_FMRS] = { .type = NLA_NESTED },
2425};
2426
2427static struct rtnl_link_ops ip6_link_ops __read_mostly = {
2428 .kind = "ip6tnl",
2429 .maxtype = IFLA_IPTUN_MAX,
2430 .policy = ip6_tnl_policy,
2431 .priv_size = sizeof(struct ip6_tnl),
2432 .setup = ip6_tnl_dev_setup,
2433 .validate = ip6_tnl_validate,
2434 .newlink = ip6_tnl_newlink,
2435 .changelink = ip6_tnl_changelink,
2436 .dellink = ip6_tnl_dellink,
2437 .get_size = ip6_tnl_get_size,
2438 .fill_info = ip6_tnl_fill_info,
2439 .get_link_net = ip6_tnl_get_link_net,
2440};
2441
2442static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
2443 .handler = ip4ip6_rcv,
2444 .err_handler = ip4ip6_err,
2445 .priority = 1,
2446};
2447
2448static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
2449 .handler = ip6ip6_rcv,
2450 .err_handler = ip6ip6_err,
2451 .priority = 1,
2452};
2453
2454static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
2455{
2456 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2457 struct net_device *dev, *aux;
2458 int h;
2459 struct ip6_tnl *t;
2460
2461 for_each_netdev_safe(net, dev, aux)
2462 if (dev->rtnl_link_ops == &ip6_link_ops)
2463 unregister_netdevice_queue(dev, list);
2464
2465 for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
2466 t = rtnl_dereference(ip6n->tnls_r_l[h]);
2467 while (t) {
2468 /* If dev is in the same netns, it has already
2469 * been added to the list by the previous loop.
2470 */
2471 if (!net_eq(dev_net(t->dev), net))
2472 unregister_netdevice_queue(t->dev, list);
2473 t = rtnl_dereference(t->next);
2474 }
2475 }
2476}
2477
2478static int __net_init ip6_tnl_init_net(struct net *net)
2479{
2480 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2481 struct ip6_tnl *t = NULL;
2482 int err;
2483
2484 ip6n->tnls[0] = ip6n->tnls_wc;
2485 ip6n->tnls[1] = ip6n->tnls_r_l;
2486
2487 if (!net_has_fallback_tunnels(net))
2488 return 0;
2489 err = -ENOMEM;
2490 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
2491 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
2492
2493 if (!ip6n->fb_tnl_dev)
2494 goto err_alloc_dev;
2495 dev_net_set(ip6n->fb_tnl_dev, net);
2496 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
2497 /* FB netdevice is special: we have one, and only one per netns.
2498 * Allowing to move it to another netns is clearly unsafe.
2499 */
2500 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
2501
2502 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
2503 if (err < 0)
2504 goto err_register;
2505
2506 err = register_netdev(ip6n->fb_tnl_dev);
2507 if (err < 0)
2508 goto err_register;
2509
2510 t = netdev_priv(ip6n->fb_tnl_dev);
2511
2512 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
2513 return 0;
2514
2515err_register:
2516 free_netdev(ip6n->fb_tnl_dev);
2517err_alloc_dev:
2518 return err;
2519}
2520
2521static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
2522{
2523 struct net *net;
2524 LIST_HEAD(list);
2525
2526 rtnl_lock();
2527 list_for_each_entry(net, net_list, exit_list)
2528 ip6_tnl_destroy_tunnels(net, &list);
2529 unregister_netdevice_many(&list);
2530 rtnl_unlock();
2531}
2532
2533static struct pernet_operations ip6_tnl_net_ops = {
2534 .init = ip6_tnl_init_net,
2535 .exit_batch = ip6_tnl_exit_batch_net,
2536 .id = &ip6_tnl_net_id,
2537 .size = sizeof(struct ip6_tnl_net),
2538};
2539
2540/**
2541 * ip6_tunnel_init - register protocol and reserve needed resources
2542 *
2543 * Return: 0 on success
2544 **/
2545
2546static int __init ip6_tunnel_init(void)
2547{
2548 int err;
2549
2550 if (!ipv6_mod_enabled())
2551 return -EOPNOTSUPP;
2552
2553 err = register_pernet_device(&ip6_tnl_net_ops);
2554 if (err < 0)
2555 goto out_pernet;
2556
2557 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
2558 if (err < 0) {
2559 pr_err("%s: can't register ip4ip6\n", __func__);
2560 goto out_ip4ip6;
2561 }
2562
2563 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
2564 if (err < 0) {
2565 pr_err("%s: can't register ip6ip6\n", __func__);
2566 goto out_ip6ip6;
2567 }
2568 err = rtnl_link_register(&ip6_link_ops);
2569 if (err < 0)
2570 goto rtnl_link_failed;
2571
2572 return 0;
2573
2574rtnl_link_failed:
2575 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
2576out_ip6ip6:
2577 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
2578out_ip4ip6:
2579 unregister_pernet_device(&ip6_tnl_net_ops);
2580out_pernet:
2581 return err;
2582}
2583
2584/**
2585 * ip6_tunnel_cleanup - free resources and unregister protocol
2586 **/
2587
2588static void __exit ip6_tunnel_cleanup(void)
2589{
2590 rtnl_link_unregister(&ip6_link_ops);
2591 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2592 pr_info("%s: can't deregister ip4ip6\n", __func__);
2593
2594 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2595 pr_info("%s: can't deregister ip6ip6\n", __func__);
2596
2597 unregister_pernet_device(&ip6_tnl_net_ops);
2598}
2599
2600module_init(ip6_tunnel_init);
2601module_exit(ip6_tunnel_cleanup);