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