dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / ipv6 / ip6_tunnel.c
blobe8f21dd520b25e9fbbae85b10cbd714113ae561f
1 /*
2 * IPv6 tunneling device
3 * Linux INET6 implementation
5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
9 * Based on:
10 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
12 * RFC 2473
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
41 #include <linux/slab.h>
42 #include <linux/hash.h>
43 #include <linux/etherdevice.h>
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
48 #include <net/icmp.h>
49 #include <net/ip.h>
50 #include <net/ip_tunnels.h>
51 #include <net/ipv6.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.h>
55 #include <net/xfrm.h>
56 #include <net/dsfield.h>
57 #include <net/inet_ecn.h>
58 #include <net/net_namespace.h>
59 #include <net/netns/generic.h>
61 MODULE_AUTHOR("Ville Nuorvala");
62 MODULE_DESCRIPTION("IPv6 tunneling device");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_RTNL_LINK("ip6tnl");
65 MODULE_ALIAS_NETDEV("ip6tnl0");
67 #define HASH_SIZE_SHIFT 5
68 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
70 static bool log_ecn_error = true;
71 module_param(log_ecn_error, bool, 0644);
72 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
74 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
76 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
78 return hash_32(hash, HASH_SIZE_SHIFT);
81 static int ip6_tnl_dev_init(struct net_device *dev);
82 static void ip6_tnl_dev_setup(struct net_device *dev);
83 static struct rtnl_link_ops ip6_link_ops __read_mostly;
85 static int ip6_tnl_net_id __read_mostly;
86 struct ip6_tnl_net {
87 /* the IPv6 tunnel fallback device */
88 struct net_device *fb_tnl_dev;
89 /* lists for storing tunnels in use */
90 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
91 struct ip6_tnl __rcu *tnls_wc[1];
92 struct ip6_tnl __rcu **tnls[2];
95 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
97 struct pcpu_sw_netstats tmp, sum = { 0 };
98 int i;
100 for_each_possible_cpu(i) {
101 unsigned int start;
102 const struct pcpu_sw_netstats *tstats =
103 per_cpu_ptr(dev->tstats, i);
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));
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;
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;
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
130 * Return:
131 * tunnel matching given end-points if found,
132 * else fallback tunnel if its device is up,
133 * else %NULL
136 #define for_each_ip6_tunnel_rcu(start) \
137 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
139 static struct ip6_tnl *
140 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
142 unsigned int hash = HASH(remote, local);
143 struct ip6_tnl *t;
144 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
145 struct in6_addr any;
147 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
148 if (ipv6_addr_equal(local, &t->parms.laddr) &&
149 ipv6_addr_equal(remote, &t->parms.raddr) &&
150 (t->dev->flags & IFF_UP))
151 return t;
154 memset(&any, 0, sizeof(any));
155 hash = HASH(&any, local);
156 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
157 if (ipv6_addr_equal(local, &t->parms.laddr) &&
158 ipv6_addr_any(&t->parms.raddr) &&
159 (t->dev->flags & IFF_UP))
160 return t;
163 hash = HASH(remote, &any);
164 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
165 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
166 ipv6_addr_any(&t->parms.laddr) &&
167 (t->dev->flags & IFF_UP))
168 return t;
171 t = rcu_dereference(ip6n->tnls_wc[0]);
172 if (t && (t->dev->flags & IFF_UP))
173 return t;
175 return NULL;
179 * ip6_tnl_bucket - get head of list matching given tunnel parameters
180 * @p: parameters containing tunnel end-points
182 * Description:
183 * ip6_tnl_bucket() returns the head of the list matching the
184 * &struct in6_addr entries laddr and raddr in @p.
186 * Return: head of IPv6 tunnel list
189 static struct ip6_tnl __rcu **
190 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
192 const struct in6_addr *remote = &p->raddr;
193 const struct in6_addr *local = &p->laddr;
194 unsigned int h = 0;
195 int prio = 0;
197 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
198 prio = 1;
199 h = HASH(remote, local);
201 return &ip6n->tnls[prio][h];
205 * ip6_tnl_link - add tunnel to hash table
206 * @t: tunnel to be added
209 static void
210 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
212 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
214 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
215 rcu_assign_pointer(*tp, t);
219 * ip6_tnl_unlink - remove tunnel from hash table
220 * @t: tunnel to be removed
223 static void
224 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
226 struct ip6_tnl __rcu **tp;
227 struct ip6_tnl *iter;
229 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
230 (iter = rtnl_dereference(*tp)) != NULL;
231 tp = &iter->next) {
232 if (t == iter) {
233 rcu_assign_pointer(*tp, t->next);
234 break;
239 static void ip6_dev_free(struct net_device *dev)
241 struct ip6_tnl *t = netdev_priv(dev);
243 dst_cache_destroy(&t->dst_cache);
244 free_percpu(dev->tstats);
245 free_netdev(dev);
248 static int ip6_tnl_create2(struct net_device *dev)
250 struct ip6_tnl *t = netdev_priv(dev);
251 struct net *net = dev_net(dev);
252 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
253 int err;
255 t = netdev_priv(dev);
257 dev->rtnl_link_ops = &ip6_link_ops;
258 err = register_netdevice(dev);
259 if (err < 0)
260 goto out;
262 strcpy(t->parms.name, dev->name);
264 dev_hold(dev);
265 ip6_tnl_link(ip6n, t);
266 return 0;
268 out:
269 return err;
273 * ip6_tnl_create - create a new tunnel
274 * @p: tunnel parameters
275 * @pt: pointer to new tunnel
277 * Description:
278 * Create tunnel matching given parameters.
280 * Return:
281 * created tunnel or error pointer
284 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
286 struct net_device *dev;
287 struct ip6_tnl *t;
288 char name[IFNAMSIZ];
289 int err = -E2BIG;
291 if (p->name[0]) {
292 if (!dev_valid_name(p->name))
293 goto failed;
294 strlcpy(name, p->name, IFNAMSIZ);
295 } else {
296 sprintf(name, "ip6tnl%%d");
298 err = -ENOMEM;
299 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
300 ip6_tnl_dev_setup);
301 if (!dev)
302 goto failed;
304 dev_net_set(dev, net);
306 t = netdev_priv(dev);
307 t->parms = *p;
308 t->net = dev_net(dev);
309 err = ip6_tnl_create2(dev);
310 if (err < 0)
311 goto failed_free;
313 return t;
315 failed_free:
316 ip6_dev_free(dev);
317 failed:
318 return ERR_PTR(err);
322 * ip6_tnl_locate - find or create tunnel matching given parameters
323 * @p: tunnel parameters
324 * @create: != 0 if allowed to create new tunnel if no match found
326 * Description:
327 * ip6_tnl_locate() first tries to locate an existing tunnel
328 * based on @parms. If this is unsuccessful, but @create is set a new
329 * tunnel device is created and registered for use.
331 * Return:
332 * matching tunnel or error pointer
335 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
336 struct __ip6_tnl_parm *p, int create)
338 const struct in6_addr *remote = &p->raddr;
339 const struct in6_addr *local = &p->laddr;
340 struct ip6_tnl __rcu **tp;
341 struct ip6_tnl *t;
342 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
344 for (tp = ip6_tnl_bucket(ip6n, p);
345 (t = rtnl_dereference(*tp)) != NULL;
346 tp = &t->next) {
347 if (ipv6_addr_equal(local, &t->parms.laddr) &&
348 ipv6_addr_equal(remote, &t->parms.raddr)) {
349 if (create)
350 return ERR_PTR(-EEXIST);
352 return t;
355 if (!create)
356 return ERR_PTR(-ENODEV);
357 return ip6_tnl_create(net, p);
361 * ip6_tnl_dev_uninit - tunnel device uninitializer
362 * @dev: the device to be destroyed
364 * Description:
365 * ip6_tnl_dev_uninit() removes tunnel from its list
368 static void
369 ip6_tnl_dev_uninit(struct net_device *dev)
371 struct ip6_tnl *t = netdev_priv(dev);
372 struct net *net = t->net;
373 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
375 if (dev == ip6n->fb_tnl_dev)
376 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
377 else
378 ip6_tnl_unlink(ip6n, t);
379 dst_cache_reset(&t->dst_cache);
380 dev_put(dev);
384 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
385 * @skb: received socket buffer
387 * Return:
388 * 0 if none was found,
389 * else index to encapsulation limit
392 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
394 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
395 unsigned int nhoff = raw - skb->data;
396 unsigned int off = nhoff + sizeof(*ipv6h);
397 u8 next, nexthdr = ipv6h->nexthdr;
399 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
400 struct ipv6_opt_hdr *hdr;
401 u16 optlen;
403 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
404 break;
406 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
407 if (nexthdr == NEXTHDR_FRAGMENT) {
408 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
409 if (frag_hdr->frag_off)
410 break;
411 optlen = 8;
412 } else if (nexthdr == NEXTHDR_AUTH) {
413 optlen = (hdr->hdrlen + 2) << 2;
414 } else {
415 optlen = ipv6_optlen(hdr);
417 /* cache hdr->nexthdr, since pskb_may_pull() might
418 * invalidate hdr
420 next = hdr->nexthdr;
421 if (nexthdr == NEXTHDR_DEST) {
422 u16 i = 2;
424 /* Remember : hdr is no longer valid at this point. */
425 if (!pskb_may_pull(skb, off + optlen))
426 break;
428 while (1) {
429 struct ipv6_tlv_tnl_enc_lim *tel;
431 /* No more room for encapsulation limit */
432 if (i + sizeof(*tel) > optlen)
433 break;
435 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
436 /* return index of option if found and valid */
437 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
438 tel->length == 1)
439 return i + off - nhoff;
440 /* else jump to next option */
441 if (tel->type)
442 i += tel->length + 2;
443 else
444 i++;
447 nexthdr = next;
448 off += optlen;
450 return 0;
452 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
455 * ip6_tnl_err - tunnel error handler
457 * Description:
458 * ip6_tnl_err() should handle errors in the tunnel according
459 * to the specifications in RFC 2473.
462 static int
463 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
464 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
466 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
467 struct ip6_tnl *t;
468 int rel_msg = 0;
469 u8 rel_type = ICMPV6_DEST_UNREACH;
470 u8 rel_code = ICMPV6_ADDR_UNREACH;
471 u8 tproto;
472 __u32 rel_info = 0;
473 __u16 len;
474 int err = -ENOENT;
476 /* If the packet doesn't contain the original IPv6 header we are
477 in trouble since we might need the source address for further
478 processing of the error. */
480 rcu_read_lock();
481 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
482 if (!t)
483 goto out;
485 tproto = ACCESS_ONCE(t->parms.proto);
486 if (tproto != ipproto && tproto != 0)
487 goto out;
489 err = 0;
491 switch (*type) {
492 __u32 teli;
493 struct ipv6_tlv_tnl_enc_lim *tel;
494 __u32 mtu;
495 case ICMPV6_DEST_UNREACH:
496 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
497 t->parms.name);
498 rel_msg = 1;
499 break;
500 case ICMPV6_TIME_EXCEED:
501 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
502 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
503 t->parms.name);
504 rel_msg = 1;
506 break;
507 case ICMPV6_PARAMPROB:
508 teli = 0;
509 if ((*code) == ICMPV6_HDR_FIELD)
510 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
512 if (teli && teli == *info - 2) {
513 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
514 if (tel->encap_limit == 0) {
515 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
516 t->parms.name);
517 rel_msg = 1;
519 } else {
520 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
521 t->parms.name);
523 break;
524 case ICMPV6_PKT_TOOBIG:
525 mtu = *info - offset;
526 if (mtu < IPV6_MIN_MTU)
527 mtu = IPV6_MIN_MTU;
528 t->dev->mtu = mtu;
530 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
531 if (len > mtu) {
532 rel_type = ICMPV6_PKT_TOOBIG;
533 rel_code = 0;
534 rel_info = mtu;
535 rel_msg = 1;
537 break;
540 *type = rel_type;
541 *code = rel_code;
542 *info = rel_info;
543 *msg = rel_msg;
545 out:
546 rcu_read_unlock();
547 return err;
550 static int
551 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
552 u8 type, u8 code, int offset, __be32 info)
554 int rel_msg = 0;
555 u8 rel_type = type;
556 u8 rel_code = code;
557 __u32 rel_info = ntohl(info);
558 int err;
559 struct sk_buff *skb2;
560 const struct iphdr *eiph;
561 struct rtable *rt;
562 struct flowi4 fl4;
564 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
565 &rel_msg, &rel_info, offset);
566 if (err < 0)
567 return err;
569 if (rel_msg == 0)
570 return 0;
572 switch (rel_type) {
573 case ICMPV6_DEST_UNREACH:
574 if (rel_code != ICMPV6_ADDR_UNREACH)
575 return 0;
576 rel_type = ICMP_DEST_UNREACH;
577 rel_code = ICMP_HOST_UNREACH;
578 break;
579 case ICMPV6_PKT_TOOBIG:
580 if (rel_code != 0)
581 return 0;
582 rel_type = ICMP_DEST_UNREACH;
583 rel_code = ICMP_FRAG_NEEDED;
584 break;
585 case NDISC_REDIRECT:
586 rel_type = ICMP_REDIRECT;
587 rel_code = ICMP_REDIR_HOST;
588 default:
589 return 0;
592 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
593 return 0;
595 skb2 = skb_clone(skb, GFP_ATOMIC);
596 if (!skb2)
597 return 0;
599 skb_dst_drop(skb2);
601 skb_pull(skb2, offset);
602 skb_reset_network_header(skb2);
603 eiph = ip_hdr(skb2);
605 /* Try to guess incoming interface */
606 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
607 eiph->saddr, 0,
608 0, 0,
609 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
610 if (IS_ERR(rt))
611 goto out;
613 skb2->dev = rt->dst.dev;
615 /* route "incoming" packet */
616 if (rt->rt_flags & RTCF_LOCAL) {
617 ip_rt_put(rt);
618 rt = NULL;
619 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
620 eiph->daddr, eiph->saddr,
621 0, 0,
622 IPPROTO_IPIP,
623 RT_TOS(eiph->tos), 0);
624 if (IS_ERR(rt) ||
625 rt->dst.dev->type != ARPHRD_TUNNEL) {
626 if (!IS_ERR(rt))
627 ip_rt_put(rt);
628 goto out;
630 skb_dst_set(skb2, &rt->dst);
631 } else {
632 ip_rt_put(rt);
633 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
634 skb2->dev) ||
635 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
636 goto out;
639 /* change mtu on this route */
640 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
641 if (rel_info > dst_mtu(skb_dst(skb2)))
642 goto out;
644 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
646 if (rel_type == ICMP_REDIRECT)
647 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
649 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
651 out:
652 kfree_skb(skb2);
653 return 0;
656 static int
657 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
658 u8 type, u8 code, int offset, __be32 info)
660 int rel_msg = 0;
661 u8 rel_type = type;
662 u8 rel_code = code;
663 __u32 rel_info = ntohl(info);
664 int err;
666 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
667 &rel_msg, &rel_info, offset);
668 if (err < 0)
669 return err;
671 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
672 struct rt6_info *rt;
673 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
675 if (!skb2)
676 return 0;
678 skb_dst_drop(skb2);
679 skb_pull(skb2, offset);
680 skb_reset_network_header(skb2);
682 /* Try to guess incoming interface */
683 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
684 NULL, 0, 0);
686 if (rt && rt->dst.dev)
687 skb2->dev = rt->dst.dev;
689 icmpv6_send(skb2, rel_type, rel_code, rel_info);
691 ip6_rt_put(rt);
693 kfree_skb(skb2);
696 return 0;
699 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
700 const struct ipv6hdr *ipv6h,
701 struct sk_buff *skb)
703 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
705 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
706 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
708 return IP6_ECN_decapsulate(ipv6h, skb);
711 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
712 const struct ipv6hdr *ipv6h,
713 struct sk_buff *skb)
715 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
716 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
718 return IP6_ECN_decapsulate(ipv6h, skb);
721 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
722 const struct in6_addr *laddr,
723 const struct in6_addr *raddr)
725 struct __ip6_tnl_parm *p = &t->parms;
726 int ltype = ipv6_addr_type(laddr);
727 int rtype = ipv6_addr_type(raddr);
728 __u32 flags = 0;
730 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
731 flags = IP6_TNL_F_CAP_PER_PACKET;
732 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
733 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
734 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
735 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
736 if (ltype&IPV6_ADDR_UNICAST)
737 flags |= IP6_TNL_F_CAP_XMIT;
738 if (rtype&IPV6_ADDR_UNICAST)
739 flags |= IP6_TNL_F_CAP_RCV;
741 return flags;
743 EXPORT_SYMBOL(ip6_tnl_get_cap);
745 /* called with rcu_read_lock() */
746 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
747 const struct in6_addr *laddr,
748 const struct in6_addr *raddr)
750 struct __ip6_tnl_parm *p = &t->parms;
751 int ret = 0;
752 struct net *net = t->net;
754 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
755 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
756 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
757 struct net_device *ldev = NULL;
759 if (p->link)
760 ldev = dev_get_by_index_rcu(net, p->link);
762 if ((ipv6_addr_is_multicast(laddr) ||
763 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
764 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
765 ret = 1;
767 return ret;
769 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
772 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
773 * @skb: received socket buffer
774 * @protocol: ethernet protocol ID
775 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
777 * Return: 0
780 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
781 __u8 ipproto,
782 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
783 const struct ipv6hdr *ipv6h,
784 struct sk_buff *skb))
786 struct ip6_tnl *t;
787 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
788 u8 tproto;
789 int err;
791 rcu_read_lock();
792 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
793 if (t) {
794 struct pcpu_sw_netstats *tstats;
796 tproto = ACCESS_ONCE(t->parms.proto);
797 if (tproto != ipproto && tproto != 0) {
798 rcu_read_unlock();
799 goto discard;
802 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
803 rcu_read_unlock();
804 goto discard;
807 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
808 t->dev->stats.rx_dropped++;
809 rcu_read_unlock();
810 goto discard;
812 skb->mac_header = skb->network_header;
813 skb_reset_network_header(skb);
814 skb->protocol = htons(protocol);
815 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
817 __skb_tunnel_rx(skb, t->dev, t->net);
819 err = dscp_ecn_decapsulate(t, ipv6h, skb);
820 if (unlikely(err)) {
821 if (log_ecn_error)
822 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
823 &ipv6h->saddr,
824 ipv6_get_dsfield(ipv6h));
825 if (err > 1) {
826 ++t->dev->stats.rx_frame_errors;
827 ++t->dev->stats.rx_errors;
828 rcu_read_unlock();
829 goto discard;
833 tstats = this_cpu_ptr(t->dev->tstats);
834 u64_stats_update_begin(&tstats->syncp);
835 tstats->rx_packets++;
836 tstats->rx_bytes += skb->len;
837 u64_stats_update_end(&tstats->syncp);
839 netif_rx(skb);
841 rcu_read_unlock();
842 return 0;
844 rcu_read_unlock();
845 return 1;
847 discard:
848 kfree_skb(skb);
849 return 0;
852 static int ip4ip6_rcv(struct sk_buff *skb)
854 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
855 ip4ip6_dscp_ecn_decapsulate);
858 static int ip6ip6_rcv(struct sk_buff *skb)
860 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
861 ip6ip6_dscp_ecn_decapsulate);
864 struct ipv6_tel_txoption {
865 struct ipv6_txoptions ops;
866 __u8 dst_opt[8];
869 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
871 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
873 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
874 opt->dst_opt[3] = 1;
875 opt->dst_opt[4] = encap_limit;
876 opt->dst_opt[5] = IPV6_TLV_PADN;
877 opt->dst_opt[6] = 1;
879 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
880 opt->ops.opt_nflen = 8;
884 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
885 * @t: the outgoing tunnel device
886 * @hdr: IPv6 header from the incoming packet
888 * Description:
889 * Avoid trivial tunneling loop by checking that tunnel exit-point
890 * doesn't match source of incoming packet.
892 * Return:
893 * 1 if conflict,
894 * 0 else
897 static inline bool
898 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
900 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
903 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
904 const struct in6_addr *laddr,
905 const struct in6_addr *raddr)
907 struct __ip6_tnl_parm *p = &t->parms;
908 int ret = 0;
909 struct net *net = t->net;
911 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
912 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
913 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
914 struct net_device *ldev = NULL;
916 rcu_read_lock();
917 if (p->link)
918 ldev = dev_get_by_index_rcu(net, p->link);
920 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
921 pr_warn("%s xmit: Local address not yet configured!\n",
922 p->name);
923 else if (!ipv6_addr_is_multicast(raddr) &&
924 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
925 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
926 p->name);
927 else
928 ret = 1;
929 rcu_read_unlock();
931 return ret;
933 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
936 * ip6_tnl_xmit2 - encapsulate packet and send
937 * @skb: the outgoing socket buffer
938 * @dev: the outgoing tunnel device
939 * @dsfield: dscp code for outer header
940 * @fl: flow of tunneled packet
941 * @encap_limit: encapsulation limit
942 * @pmtu: Path MTU is stored if packet is too big
944 * Description:
945 * Build new header and do some sanity checks on the packet before sending
946 * it.
948 * Return:
949 * 0 on success
950 * -1 fail
951 * %-EMSGSIZE message too big. return mtu in this case.
954 static int ip6_tnl_xmit2(struct sk_buff *skb,
955 struct net_device *dev,
956 __u8 dsfield,
957 struct flowi6 *fl6,
958 int encap_limit,
959 __u32 *pmtu)
961 struct ip6_tnl *t = netdev_priv(dev);
962 struct net *net = t->net;
963 struct net_device_stats *stats = &t->dev->stats;
964 struct ipv6hdr *ipv6h;
965 struct ipv6_tel_txoption opt;
966 struct dst_entry *dst = NULL, *ndst = NULL;
967 struct net_device *tdev;
968 int mtu;
969 unsigned int max_headroom = sizeof(struct ipv6hdr);
970 u8 proto;
971 int err = -1;
973 /* NBMA tunnel */
974 if (ipv6_addr_any(&t->parms.raddr)) {
975 struct in6_addr *addr6;
976 struct neighbour *neigh;
977 int addr_type;
979 if (!skb_dst(skb))
980 goto tx_err_link_failure;
982 neigh = dst_neigh_lookup(skb_dst(skb),
983 &ipv6_hdr(skb)->daddr);
984 if (!neigh)
985 goto tx_err_link_failure;
987 addr6 = (struct in6_addr *)&neigh->primary_key;
988 addr_type = ipv6_addr_type(addr6);
990 if (addr_type == IPV6_ADDR_ANY)
991 addr6 = &ipv6_hdr(skb)->daddr;
993 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
994 neigh_release(neigh);
995 } else if (!fl6->flowi6_mark)
996 dst = dst_cache_get(&t->dst_cache);
998 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
999 goto tx_err_link_failure;
1001 if (!dst) {
1002 dst = ip6_route_output(net, NULL, fl6);
1004 if (dst->error)
1005 goto tx_err_link_failure;
1006 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1007 if (IS_ERR(dst)) {
1008 err = PTR_ERR(dst);
1009 dst = NULL;
1010 goto tx_err_link_failure;
1012 ndst = dst;
1015 tdev = dst->dev;
1017 if (tdev == dev) {
1018 stats->collisions++;
1019 net_warn_ratelimited("%s: Local routing loop detected!\n",
1020 t->parms.name);
1021 goto tx_err_dst_release;
1023 mtu = dst_mtu(dst) - sizeof(*ipv6h);
1024 if (encap_limit >= 0) {
1025 max_headroom += 8;
1026 mtu -= 8;
1028 if (mtu < IPV6_MIN_MTU)
1029 mtu = IPV6_MIN_MTU;
1030 if (skb_dst(skb))
1031 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1032 if (skb->len > mtu) {
1033 *pmtu = mtu;
1034 err = -EMSGSIZE;
1035 goto tx_err_dst_release;
1038 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1041 * Okay, now see if we can stuff it in the buffer as-is.
1043 max_headroom += LL_RESERVED_SPACE(tdev);
1045 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1046 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1047 struct sk_buff *new_skb;
1049 new_skb = skb_realloc_headroom(skb, max_headroom);
1050 if (!new_skb)
1051 goto tx_err_dst_release;
1053 if (skb->sk)
1054 skb_set_owner_w(new_skb, skb->sk);
1055 consume_skb(skb);
1056 skb = new_skb;
1059 if (!fl6->flowi6_mark && ndst)
1060 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1061 skb_dst_set(skb, dst);
1063 skb->transport_header = skb->network_header;
1065 proto = fl6->flowi6_proto;
1066 if (encap_limit >= 0) {
1067 init_tel_txopt(&opt, encap_limit);
1068 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1071 if (likely(!skb->encapsulation)) {
1072 skb_reset_inner_headers(skb);
1073 skb->encapsulation = 1;
1076 skb_push(skb, sizeof(struct ipv6hdr));
1077 skb_reset_network_header(skb);
1078 ipv6h = ipv6_hdr(skb);
1079 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1080 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1081 ipv6h->hop_limit = t->parms.hop_limit;
1082 ipv6h->nexthdr = proto;
1083 ipv6h->saddr = fl6->saddr;
1084 ipv6h->daddr = fl6->daddr;
1085 ip6tunnel_xmit(NULL, skb, dev);
1086 return 0;
1087 tx_err_link_failure:
1088 stats->tx_carrier_errors++;
1089 dst_link_failure(skb);
1090 tx_err_dst_release:
1091 dst_release(dst);
1092 return err;
1095 static inline int
1096 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1098 struct ip6_tnl *t = netdev_priv(dev);
1099 const struct iphdr *iph = ip_hdr(skb);
1100 int encap_limit = -1;
1101 struct flowi6 fl6;
1102 __u8 dsfield;
1103 __u32 mtu;
1104 u8 tproto;
1105 int err;
1107 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1109 tproto = ACCESS_ONCE(t->parms.proto);
1110 if (tproto != IPPROTO_IPIP && tproto != 0)
1111 return -1;
1113 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1114 encap_limit = t->parms.encap_limit;
1116 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1117 fl6.flowi6_proto = IPPROTO_IPIP;
1119 dsfield = ipv4_get_dsfield(iph);
1121 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1122 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1123 & IPV6_TCLASS_MASK;
1124 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1125 fl6.flowi6_mark = skb->mark;
1127 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1128 if (err != 0) {
1129 /* XXX: send ICMP error even if DF is not set. */
1130 if (err == -EMSGSIZE)
1131 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1132 htonl(mtu));
1133 return -1;
1136 return 0;
1139 static inline int
1140 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1142 struct ip6_tnl *t = netdev_priv(dev);
1143 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1144 int encap_limit = -1;
1145 __u16 offset;
1146 struct flowi6 fl6;
1147 __u8 dsfield;
1148 __u32 mtu;
1149 u8 tproto;
1150 int err;
1152 tproto = ACCESS_ONCE(t->parms.proto);
1153 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1154 ip6_tnl_addr_conflict(t, ipv6h))
1155 return -1;
1157 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1158 if (offset > 0) {
1159 struct ipv6_tlv_tnl_enc_lim *tel;
1160 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1161 if (tel->encap_limit == 0) {
1162 icmpv6_send(skb, ICMPV6_PARAMPROB,
1163 ICMPV6_HDR_FIELD, offset + 2);
1164 return -1;
1166 encap_limit = tel->encap_limit - 1;
1167 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1168 encap_limit = t->parms.encap_limit;
1170 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1171 fl6.flowi6_proto = IPPROTO_IPV6;
1173 dsfield = ipv6_get_dsfield(ipv6h);
1174 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1175 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1176 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1177 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1178 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1179 fl6.flowi6_mark = skb->mark;
1181 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1182 if (err != 0) {
1183 if (err == -EMSGSIZE)
1184 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1185 return -1;
1188 return 0;
1191 static netdev_tx_t
1192 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1194 struct ip6_tnl *t = netdev_priv(dev);
1195 struct net_device_stats *stats = &t->dev->stats;
1196 int ret;
1198 switch (skb->protocol) {
1199 case htons(ETH_P_IP):
1200 ret = ip4ip6_tnl_xmit(skb, dev);
1201 break;
1202 case htons(ETH_P_IPV6):
1203 ret = ip6ip6_tnl_xmit(skb, dev);
1204 break;
1205 default:
1206 goto tx_err;
1209 if (ret < 0)
1210 goto tx_err;
1212 return NETDEV_TX_OK;
1214 tx_err:
1215 stats->tx_errors++;
1216 stats->tx_dropped++;
1217 kfree_skb(skb);
1218 return NETDEV_TX_OK;
1221 static void ip6_tnl_link_config(struct ip6_tnl *t)
1223 struct net_device *dev = t->dev;
1224 struct __ip6_tnl_parm *p = &t->parms;
1225 struct flowi6 *fl6 = &t->fl.u.ip6;
1227 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1228 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1230 /* Set up flowi template */
1231 fl6->saddr = p->laddr;
1232 fl6->daddr = p->raddr;
1233 fl6->flowi6_oif = p->link;
1234 fl6->flowlabel = 0;
1236 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1237 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1238 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1239 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1241 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1242 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1244 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1245 dev->flags |= IFF_POINTOPOINT;
1246 else
1247 dev->flags &= ~IFF_POINTOPOINT;
1249 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1250 int strict = (ipv6_addr_type(&p->raddr) &
1251 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1253 struct rt6_info *rt = rt6_lookup(t->net,
1254 &p->raddr, &p->laddr,
1255 p->link, strict);
1257 if (!rt)
1258 return;
1260 if (rt->dst.dev) {
1261 dev->hard_header_len = rt->dst.dev->hard_header_len +
1262 sizeof(struct ipv6hdr);
1264 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1265 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1266 dev->mtu -= 8;
1268 if (dev->mtu < IPV6_MIN_MTU)
1269 dev->mtu = IPV6_MIN_MTU;
1271 ip6_rt_put(rt);
1276 * ip6_tnl_change - update the tunnel parameters
1277 * @t: tunnel to be changed
1278 * @p: tunnel configuration parameters
1280 * Description:
1281 * ip6_tnl_change() updates the tunnel parameters
1284 static int
1285 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1287 t->parms.laddr = p->laddr;
1288 t->parms.raddr = p->raddr;
1289 t->parms.flags = p->flags;
1290 t->parms.hop_limit = p->hop_limit;
1291 t->parms.encap_limit = p->encap_limit;
1292 t->parms.flowinfo = p->flowinfo;
1293 t->parms.link = p->link;
1294 t->parms.proto = p->proto;
1295 dst_cache_reset(&t->dst_cache);
1296 ip6_tnl_link_config(t);
1297 return 0;
1300 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1302 struct net *net = t->net;
1303 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1304 int err;
1306 ip6_tnl_unlink(ip6n, t);
1307 synchronize_net();
1308 err = ip6_tnl_change(t, p);
1309 ip6_tnl_link(ip6n, t);
1310 netdev_state_change(t->dev);
1311 return err;
1314 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1316 /* for default tnl0 device allow to change only the proto */
1317 t->parms.proto = p->proto;
1318 netdev_state_change(t->dev);
1319 return 0;
1322 static void
1323 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1325 p->laddr = u->laddr;
1326 p->raddr = u->raddr;
1327 p->flags = u->flags;
1328 p->hop_limit = u->hop_limit;
1329 p->encap_limit = u->encap_limit;
1330 p->flowinfo = u->flowinfo;
1331 p->link = u->link;
1332 p->proto = u->proto;
1333 memcpy(p->name, u->name, sizeof(u->name));
1336 static void
1337 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1339 u->laddr = p->laddr;
1340 u->raddr = p->raddr;
1341 u->flags = p->flags;
1342 u->hop_limit = p->hop_limit;
1343 u->encap_limit = p->encap_limit;
1344 u->flowinfo = p->flowinfo;
1345 u->link = p->link;
1346 u->proto = p->proto;
1347 memcpy(u->name, p->name, sizeof(u->name));
1351 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1352 * @dev: virtual device associated with tunnel
1353 * @ifr: parameters passed from userspace
1354 * @cmd: command to be performed
1356 * Description:
1357 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1358 * from userspace.
1360 * The possible commands are the following:
1361 * %SIOCGETTUNNEL: get tunnel parameters for device
1362 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1363 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1364 * %SIOCDELTUNNEL: delete tunnel
1366 * The fallback device "ip6tnl0", created during module
1367 * initialization, can be used for creating other tunnel devices.
1369 * Return:
1370 * 0 on success,
1371 * %-EFAULT if unable to copy data to or from userspace,
1372 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1373 * %-EINVAL if passed tunnel parameters are invalid,
1374 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1375 * %-ENODEV if attempting to change or delete a nonexisting device
1378 static int
1379 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1381 int err = 0;
1382 struct ip6_tnl_parm p;
1383 struct __ip6_tnl_parm p1;
1384 struct ip6_tnl *t = netdev_priv(dev);
1385 struct net *net = t->net;
1386 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1388 switch (cmd) {
1389 case SIOCGETTUNNEL:
1390 if (dev == ip6n->fb_tnl_dev) {
1391 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1392 err = -EFAULT;
1393 break;
1395 ip6_tnl_parm_from_user(&p1, &p);
1396 t = ip6_tnl_locate(net, &p1, 0);
1397 if (IS_ERR(t))
1398 t = netdev_priv(dev);
1399 } else {
1400 memset(&p, 0, sizeof(p));
1402 ip6_tnl_parm_to_user(&p, &t->parms);
1403 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1404 err = -EFAULT;
1406 break;
1407 case SIOCADDTUNNEL:
1408 case SIOCCHGTUNNEL:
1409 err = -EPERM;
1410 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1411 break;
1412 err = -EFAULT;
1413 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1414 break;
1415 err = -EINVAL;
1416 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1417 p.proto != 0)
1418 break;
1419 ip6_tnl_parm_from_user(&p1, &p);
1420 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1421 if (cmd == SIOCCHGTUNNEL) {
1422 if (!IS_ERR(t)) {
1423 if (t->dev != dev) {
1424 err = -EEXIST;
1425 break;
1427 } else
1428 t = netdev_priv(dev);
1429 if (dev == ip6n->fb_tnl_dev)
1430 err = ip6_tnl0_update(t, &p1);
1431 else
1432 err = ip6_tnl_update(t, &p1);
1434 if (!IS_ERR(t)) {
1435 err = 0;
1436 ip6_tnl_parm_to_user(&p, &t->parms);
1437 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1438 err = -EFAULT;
1440 } else {
1441 err = PTR_ERR(t);
1443 break;
1444 case SIOCDELTUNNEL:
1445 err = -EPERM;
1446 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1447 break;
1449 if (dev == ip6n->fb_tnl_dev) {
1450 err = -EFAULT;
1451 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1452 break;
1453 err = -ENOENT;
1454 ip6_tnl_parm_from_user(&p1, &p);
1455 t = ip6_tnl_locate(net, &p1, 0);
1456 if (IS_ERR(t))
1457 break;
1458 err = -EPERM;
1459 if (t->dev == ip6n->fb_tnl_dev)
1460 break;
1461 dev = t->dev;
1463 err = 0;
1464 unregister_netdevice(dev);
1465 break;
1466 default:
1467 err = -EINVAL;
1469 return err;
1473 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1474 * @dev: virtual device associated with tunnel
1475 * @new_mtu: the new mtu
1477 * Return:
1478 * 0 on success,
1479 * %-EINVAL if mtu too small
1482 static int
1483 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1485 struct ip6_tnl *tnl = netdev_priv(dev);
1487 if (tnl->parms.proto == IPPROTO_IPIP) {
1488 if (new_mtu < 68)
1489 return -EINVAL;
1490 } else {
1491 if (new_mtu < IPV6_MIN_MTU)
1492 return -EINVAL;
1494 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1495 return -EINVAL;
1496 dev->mtu = new_mtu;
1497 return 0;
1500 int ip6_tnl_get_iflink(const struct net_device *dev)
1502 struct ip6_tnl *t = netdev_priv(dev);
1504 return t->parms.link;
1506 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1508 static const struct net_device_ops ip6_tnl_netdev_ops = {
1509 .ndo_init = ip6_tnl_dev_init,
1510 .ndo_uninit = ip6_tnl_dev_uninit,
1511 .ndo_start_xmit = ip6_tnl_xmit,
1512 .ndo_do_ioctl = ip6_tnl_ioctl,
1513 .ndo_change_mtu = ip6_tnl_change_mtu,
1514 .ndo_get_stats = ip6_get_stats,
1515 .ndo_get_iflink = ip6_tnl_get_iflink,
1520 * ip6_tnl_dev_setup - setup virtual tunnel device
1521 * @dev: virtual device associated with tunnel
1523 * Description:
1524 * Initialize function pointers and device parameters
1527 static void ip6_tnl_dev_setup(struct net_device *dev)
1529 struct ip6_tnl *t;
1531 dev->netdev_ops = &ip6_tnl_netdev_ops;
1532 dev->destructor = ip6_dev_free;
1534 dev->type = ARPHRD_TUNNEL6;
1535 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1536 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1537 t = netdev_priv(dev);
1538 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1539 dev->mtu -= 8;
1540 dev->flags |= IFF_NOARP;
1541 dev->addr_len = sizeof(struct in6_addr);
1542 netif_keep_dst(dev);
1543 /* This perm addr will be used as interface identifier by IPv6 */
1544 dev->addr_assign_type = NET_ADDR_RANDOM;
1545 eth_random_addr(dev->perm_addr);
1550 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1551 * @dev: virtual device associated with tunnel
1554 static inline int
1555 ip6_tnl_dev_init_gen(struct net_device *dev)
1557 struct ip6_tnl *t = netdev_priv(dev);
1558 int ret;
1560 t->dev = dev;
1561 t->net = dev_net(dev);
1562 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1563 if (!dev->tstats)
1564 return -ENOMEM;
1566 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
1567 if (ret) {
1568 free_percpu(dev->tstats);
1569 dev->tstats = NULL;
1570 return ret;
1573 return 0;
1577 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1578 * @dev: virtual device associated with tunnel
1581 static int ip6_tnl_dev_init(struct net_device *dev)
1583 struct ip6_tnl *t = netdev_priv(dev);
1584 int err = ip6_tnl_dev_init_gen(dev);
1586 if (err)
1587 return err;
1588 ip6_tnl_link_config(t);
1589 return 0;
1593 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1594 * @dev: fallback device
1596 * Return: 0
1599 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1601 struct ip6_tnl *t = netdev_priv(dev);
1602 struct net *net = dev_net(dev);
1603 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1605 t->parms.proto = IPPROTO_IPV6;
1606 dev_hold(dev);
1608 rcu_assign_pointer(ip6n->tnls_wc[0], t);
1609 return 0;
1612 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1614 u8 proto;
1616 if (!data || !data[IFLA_IPTUN_PROTO])
1617 return 0;
1619 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1620 if (proto != IPPROTO_IPV6 &&
1621 proto != IPPROTO_IPIP &&
1622 proto != 0)
1623 return -EINVAL;
1625 return 0;
1628 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1629 struct __ip6_tnl_parm *parms)
1631 memset(parms, 0, sizeof(*parms));
1633 if (!data)
1634 return;
1636 if (data[IFLA_IPTUN_LINK])
1637 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1639 if (data[IFLA_IPTUN_LOCAL])
1640 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1642 if (data[IFLA_IPTUN_REMOTE])
1643 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1645 if (data[IFLA_IPTUN_TTL])
1646 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1648 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1649 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1651 if (data[IFLA_IPTUN_FLOWINFO])
1652 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1654 if (data[IFLA_IPTUN_FLAGS])
1655 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1657 if (data[IFLA_IPTUN_PROTO])
1658 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1661 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1662 struct nlattr *tb[], struct nlattr *data[])
1664 struct net *net = dev_net(dev);
1665 struct ip6_tnl *nt, *t;
1667 nt = netdev_priv(dev);
1668 ip6_tnl_netlink_parms(data, &nt->parms);
1670 t = ip6_tnl_locate(net, &nt->parms, 0);
1671 if (!IS_ERR(t))
1672 return -EEXIST;
1674 return ip6_tnl_create2(dev);
1677 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1678 struct nlattr *data[])
1680 struct ip6_tnl *t = netdev_priv(dev);
1681 struct __ip6_tnl_parm p;
1682 struct net *net = t->net;
1683 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1685 if (dev == ip6n->fb_tnl_dev)
1686 return -EINVAL;
1688 ip6_tnl_netlink_parms(data, &p);
1690 t = ip6_tnl_locate(net, &p, 0);
1691 if (!IS_ERR(t)) {
1692 if (t->dev != dev)
1693 return -EEXIST;
1694 } else
1695 t = netdev_priv(dev);
1697 return ip6_tnl_update(t, &p);
1700 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1702 struct net *net = dev_net(dev);
1703 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1705 if (dev != ip6n->fb_tnl_dev)
1706 unregister_netdevice_queue(dev, head);
1709 static size_t ip6_tnl_get_size(const struct net_device *dev)
1711 return
1712 /* IFLA_IPTUN_LINK */
1713 nla_total_size(4) +
1714 /* IFLA_IPTUN_LOCAL */
1715 nla_total_size(sizeof(struct in6_addr)) +
1716 /* IFLA_IPTUN_REMOTE */
1717 nla_total_size(sizeof(struct in6_addr)) +
1718 /* IFLA_IPTUN_TTL */
1719 nla_total_size(1) +
1720 /* IFLA_IPTUN_ENCAP_LIMIT */
1721 nla_total_size(1) +
1722 /* IFLA_IPTUN_FLOWINFO */
1723 nla_total_size(4) +
1724 /* IFLA_IPTUN_FLAGS */
1725 nla_total_size(4) +
1726 /* IFLA_IPTUN_PROTO */
1727 nla_total_size(1) +
1731 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1733 struct ip6_tnl *tunnel = netdev_priv(dev);
1734 struct __ip6_tnl_parm *parm = &tunnel->parms;
1736 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1737 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1738 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1739 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1740 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1741 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1742 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1743 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1744 goto nla_put_failure;
1745 return 0;
1747 nla_put_failure:
1748 return -EMSGSIZE;
1751 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1753 struct ip6_tnl *tunnel = netdev_priv(dev);
1755 return tunnel->net;
1757 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1759 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1760 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1761 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1762 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1763 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1764 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1765 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1766 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1767 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1770 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1771 .kind = "ip6tnl",
1772 .maxtype = IFLA_IPTUN_MAX,
1773 .policy = ip6_tnl_policy,
1774 .priv_size = sizeof(struct ip6_tnl),
1775 .setup = ip6_tnl_dev_setup,
1776 .validate = ip6_tnl_validate,
1777 .newlink = ip6_tnl_newlink,
1778 .changelink = ip6_tnl_changelink,
1779 .dellink = ip6_tnl_dellink,
1780 .get_size = ip6_tnl_get_size,
1781 .fill_info = ip6_tnl_fill_info,
1782 .get_link_net = ip6_tnl_get_link_net,
1785 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1786 .handler = ip4ip6_rcv,
1787 .err_handler = ip4ip6_err,
1788 .priority = 1,
1791 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1792 .handler = ip6ip6_rcv,
1793 .err_handler = ip6ip6_err,
1794 .priority = 1,
1797 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1799 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1800 struct net_device *dev, *aux;
1801 int h;
1802 struct ip6_tnl *t;
1803 LIST_HEAD(list);
1805 for_each_netdev_safe(net, dev, aux)
1806 if (dev->rtnl_link_ops == &ip6_link_ops)
1807 unregister_netdevice_queue(dev, &list);
1809 for (h = 0; h < HASH_SIZE; h++) {
1810 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1811 while (t) {
1812 /* If dev is in the same netns, it has already
1813 * been added to the list by the previous loop.
1815 if (!net_eq(dev_net(t->dev), net))
1816 unregister_netdevice_queue(t->dev, &list);
1817 t = rtnl_dereference(t->next);
1821 unregister_netdevice_many(&list);
1824 static int __net_init ip6_tnl_init_net(struct net *net)
1826 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1827 struct ip6_tnl *t = NULL;
1828 int err;
1830 ip6n->tnls[0] = ip6n->tnls_wc;
1831 ip6n->tnls[1] = ip6n->tnls_r_l;
1833 err = -ENOMEM;
1834 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1835 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1837 if (!ip6n->fb_tnl_dev)
1838 goto err_alloc_dev;
1839 dev_net_set(ip6n->fb_tnl_dev, net);
1840 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1841 /* FB netdevice is special: we have one, and only one per netns.
1842 * Allowing to move it to another netns is clearly unsafe.
1844 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1846 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1847 if (err < 0)
1848 goto err_register;
1850 err = register_netdev(ip6n->fb_tnl_dev);
1851 if (err < 0)
1852 goto err_register;
1854 t = netdev_priv(ip6n->fb_tnl_dev);
1856 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1857 return 0;
1859 err_register:
1860 ip6_dev_free(ip6n->fb_tnl_dev);
1861 err_alloc_dev:
1862 return err;
1865 static void __net_exit ip6_tnl_exit_net(struct net *net)
1867 rtnl_lock();
1868 ip6_tnl_destroy_tunnels(net);
1869 rtnl_unlock();
1872 static struct pernet_operations ip6_tnl_net_ops = {
1873 .init = ip6_tnl_init_net,
1874 .exit = ip6_tnl_exit_net,
1875 .id = &ip6_tnl_net_id,
1876 .size = sizeof(struct ip6_tnl_net),
1880 * ip6_tunnel_init - register protocol and reserve needed resources
1882 * Return: 0 on success
1885 static int __init ip6_tunnel_init(void)
1887 int err;
1889 err = register_pernet_device(&ip6_tnl_net_ops);
1890 if (err < 0)
1891 goto out_pernet;
1893 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1894 if (err < 0) {
1895 pr_err("%s: can't register ip4ip6\n", __func__);
1896 goto out_ip4ip6;
1899 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1900 if (err < 0) {
1901 pr_err("%s: can't register ip6ip6\n", __func__);
1902 goto out_ip6ip6;
1904 err = rtnl_link_register(&ip6_link_ops);
1905 if (err < 0)
1906 goto rtnl_link_failed;
1908 return 0;
1910 rtnl_link_failed:
1911 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1912 out_ip6ip6:
1913 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1914 out_ip4ip6:
1915 unregister_pernet_device(&ip6_tnl_net_ops);
1916 out_pernet:
1917 return err;
1921 * ip6_tunnel_cleanup - free resources and unregister protocol
1924 static void __exit ip6_tunnel_cleanup(void)
1926 rtnl_link_unregister(&ip6_link_ops);
1927 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1928 pr_info("%s: can't deregister ip4ip6\n", __func__);
1930 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1931 pr_info("%s: can't deregister ip6ip6\n", __func__);
1933 unregister_pernet_device(&ip6_tnl_net_ops);
1936 module_init(ip6_tunnel_init);
1937 module_exit(ip6_tunnel_cleanup);