ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / ipv6 / ip6_tunnel.c
blob7c7a74ea2b0d9a3b1ad1768722b22ec8d801cba7
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;
1100 int encap_limit = -1;
1101 struct flowi6 fl6;
1102 __u8 dsfield;
1103 __u32 mtu;
1104 u8 tproto;
1105 int err;
1107 /* ensure we can access the full inner ip header */
1108 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1109 return -1;
1111 iph = ip_hdr(skb);
1112 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1114 tproto = ACCESS_ONCE(t->parms.proto);
1115 if (tproto != IPPROTO_IPIP && tproto != 0)
1116 return -1;
1118 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1119 encap_limit = t->parms.encap_limit;
1121 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1122 fl6.flowi6_proto = IPPROTO_IPIP;
1124 dsfield = ipv4_get_dsfield(iph);
1126 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1127 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1128 & IPV6_TCLASS_MASK;
1129 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1130 fl6.flowi6_mark = skb->mark;
1132 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1133 if (err != 0) {
1134 /* XXX: send ICMP error even if DF is not set. */
1135 if (err == -EMSGSIZE)
1136 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1137 htonl(mtu));
1138 return -1;
1141 return 0;
1144 static inline int
1145 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1147 struct ip6_tnl *t = netdev_priv(dev);
1148 struct ipv6hdr *ipv6h;
1149 int encap_limit = -1;
1150 __u16 offset;
1151 struct flowi6 fl6;
1152 __u8 dsfield;
1153 __u32 mtu;
1154 u8 tproto;
1155 int err;
1157 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
1158 return -1;
1160 ipv6h = ipv6_hdr(skb);
1161 tproto = ACCESS_ONCE(t->parms.proto);
1162 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1163 ip6_tnl_addr_conflict(t, ipv6h))
1164 return -1;
1166 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1167 if (offset > 0) {
1168 struct ipv6_tlv_tnl_enc_lim *tel;
1169 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1170 if (tel->encap_limit == 0) {
1171 icmpv6_send(skb, ICMPV6_PARAMPROB,
1172 ICMPV6_HDR_FIELD, offset + 2);
1173 return -1;
1175 encap_limit = tel->encap_limit - 1;
1176 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1177 encap_limit = t->parms.encap_limit;
1179 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1180 fl6.flowi6_proto = IPPROTO_IPV6;
1182 dsfield = ipv6_get_dsfield(ipv6h);
1183 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1184 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1185 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1186 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1187 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1188 fl6.flowi6_mark = skb->mark;
1190 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1191 if (err != 0) {
1192 if (err == -EMSGSIZE)
1193 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1194 return -1;
1197 return 0;
1200 static netdev_tx_t
1201 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1203 struct ip6_tnl *t = netdev_priv(dev);
1204 struct net_device_stats *stats = &t->dev->stats;
1205 int ret;
1207 switch (skb->protocol) {
1208 case htons(ETH_P_IP):
1209 ret = ip4ip6_tnl_xmit(skb, dev);
1210 break;
1211 case htons(ETH_P_IPV6):
1212 ret = ip6ip6_tnl_xmit(skb, dev);
1213 break;
1214 default:
1215 goto tx_err;
1218 if (ret < 0)
1219 goto tx_err;
1221 return NETDEV_TX_OK;
1223 tx_err:
1224 stats->tx_errors++;
1225 stats->tx_dropped++;
1226 kfree_skb(skb);
1227 return NETDEV_TX_OK;
1230 static void ip6_tnl_link_config(struct ip6_tnl *t)
1232 struct net_device *dev = t->dev;
1233 struct __ip6_tnl_parm *p = &t->parms;
1234 struct flowi6 *fl6 = &t->fl.u.ip6;
1236 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1237 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1239 /* Set up flowi template */
1240 fl6->saddr = p->laddr;
1241 fl6->daddr = p->raddr;
1242 fl6->flowi6_oif = p->link;
1243 fl6->flowlabel = 0;
1245 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1246 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1247 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1248 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1250 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1251 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1253 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1254 dev->flags |= IFF_POINTOPOINT;
1255 else
1256 dev->flags &= ~IFF_POINTOPOINT;
1258 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1259 int strict = (ipv6_addr_type(&p->raddr) &
1260 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1262 struct rt6_info *rt = rt6_lookup(t->net,
1263 &p->raddr, &p->laddr,
1264 p->link, strict);
1266 if (!rt)
1267 return;
1269 if (rt->dst.dev) {
1270 dev->hard_header_len = rt->dst.dev->hard_header_len +
1271 sizeof(struct ipv6hdr);
1273 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1274 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1275 dev->mtu -= 8;
1277 if (dev->mtu < IPV6_MIN_MTU)
1278 dev->mtu = IPV6_MIN_MTU;
1280 ip6_rt_put(rt);
1285 * ip6_tnl_change - update the tunnel parameters
1286 * @t: tunnel to be changed
1287 * @p: tunnel configuration parameters
1289 * Description:
1290 * ip6_tnl_change() updates the tunnel parameters
1293 static int
1294 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1296 t->parms.laddr = p->laddr;
1297 t->parms.raddr = p->raddr;
1298 t->parms.flags = p->flags;
1299 t->parms.hop_limit = p->hop_limit;
1300 t->parms.encap_limit = p->encap_limit;
1301 t->parms.flowinfo = p->flowinfo;
1302 t->parms.link = p->link;
1303 t->parms.proto = p->proto;
1304 dst_cache_reset(&t->dst_cache);
1305 ip6_tnl_link_config(t);
1306 return 0;
1309 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1311 struct net *net = t->net;
1312 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1313 int err;
1315 ip6_tnl_unlink(ip6n, t);
1316 synchronize_net();
1317 err = ip6_tnl_change(t, p);
1318 ip6_tnl_link(ip6n, t);
1319 netdev_state_change(t->dev);
1320 return err;
1323 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1325 /* for default tnl0 device allow to change only the proto */
1326 t->parms.proto = p->proto;
1327 netdev_state_change(t->dev);
1328 return 0;
1331 static void
1332 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1334 p->laddr = u->laddr;
1335 p->raddr = u->raddr;
1336 p->flags = u->flags;
1337 p->hop_limit = u->hop_limit;
1338 p->encap_limit = u->encap_limit;
1339 p->flowinfo = u->flowinfo;
1340 p->link = u->link;
1341 p->proto = u->proto;
1342 memcpy(p->name, u->name, sizeof(u->name));
1345 static void
1346 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1348 u->laddr = p->laddr;
1349 u->raddr = p->raddr;
1350 u->flags = p->flags;
1351 u->hop_limit = p->hop_limit;
1352 u->encap_limit = p->encap_limit;
1353 u->flowinfo = p->flowinfo;
1354 u->link = p->link;
1355 u->proto = p->proto;
1356 memcpy(u->name, p->name, sizeof(u->name));
1360 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1361 * @dev: virtual device associated with tunnel
1362 * @ifr: parameters passed from userspace
1363 * @cmd: command to be performed
1365 * Description:
1366 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1367 * from userspace.
1369 * The possible commands are the following:
1370 * %SIOCGETTUNNEL: get tunnel parameters for device
1371 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1372 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1373 * %SIOCDELTUNNEL: delete tunnel
1375 * The fallback device "ip6tnl0", created during module
1376 * initialization, can be used for creating other tunnel devices.
1378 * Return:
1379 * 0 on success,
1380 * %-EFAULT if unable to copy data to or from userspace,
1381 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1382 * %-EINVAL if passed tunnel parameters are invalid,
1383 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1384 * %-ENODEV if attempting to change or delete a nonexisting device
1387 static int
1388 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1390 int err = 0;
1391 struct ip6_tnl_parm p;
1392 struct __ip6_tnl_parm p1;
1393 struct ip6_tnl *t = netdev_priv(dev);
1394 struct net *net = t->net;
1395 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1397 switch (cmd) {
1398 case SIOCGETTUNNEL:
1399 if (dev == ip6n->fb_tnl_dev) {
1400 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1401 err = -EFAULT;
1402 break;
1404 ip6_tnl_parm_from_user(&p1, &p);
1405 t = ip6_tnl_locate(net, &p1, 0);
1406 if (IS_ERR(t))
1407 t = netdev_priv(dev);
1408 } else {
1409 memset(&p, 0, sizeof(p));
1411 ip6_tnl_parm_to_user(&p, &t->parms);
1412 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1413 err = -EFAULT;
1415 break;
1416 case SIOCADDTUNNEL:
1417 case SIOCCHGTUNNEL:
1418 err = -EPERM;
1419 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1420 break;
1421 err = -EFAULT;
1422 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1423 break;
1424 err = -EINVAL;
1425 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1426 p.proto != 0)
1427 break;
1428 ip6_tnl_parm_from_user(&p1, &p);
1429 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1430 if (cmd == SIOCCHGTUNNEL) {
1431 if (!IS_ERR(t)) {
1432 if (t->dev != dev) {
1433 err = -EEXIST;
1434 break;
1436 } else
1437 t = netdev_priv(dev);
1438 if (dev == ip6n->fb_tnl_dev)
1439 err = ip6_tnl0_update(t, &p1);
1440 else
1441 err = ip6_tnl_update(t, &p1);
1443 if (!IS_ERR(t)) {
1444 err = 0;
1445 ip6_tnl_parm_to_user(&p, &t->parms);
1446 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1447 err = -EFAULT;
1449 } else {
1450 err = PTR_ERR(t);
1452 break;
1453 case SIOCDELTUNNEL:
1454 err = -EPERM;
1455 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1456 break;
1458 if (dev == ip6n->fb_tnl_dev) {
1459 err = -EFAULT;
1460 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1461 break;
1462 err = -ENOENT;
1463 ip6_tnl_parm_from_user(&p1, &p);
1464 t = ip6_tnl_locate(net, &p1, 0);
1465 if (IS_ERR(t))
1466 break;
1467 err = -EPERM;
1468 if (t->dev == ip6n->fb_tnl_dev)
1469 break;
1470 dev = t->dev;
1472 err = 0;
1473 unregister_netdevice(dev);
1474 break;
1475 default:
1476 err = -EINVAL;
1478 return err;
1482 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1483 * @dev: virtual device associated with tunnel
1484 * @new_mtu: the new mtu
1486 * Return:
1487 * 0 on success,
1488 * %-EINVAL if mtu too small
1491 static int
1492 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1494 struct ip6_tnl *tnl = netdev_priv(dev);
1496 if (tnl->parms.proto == IPPROTO_IPIP) {
1497 if (new_mtu < 68)
1498 return -EINVAL;
1499 } else {
1500 if (new_mtu < IPV6_MIN_MTU)
1501 return -EINVAL;
1503 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1504 return -EINVAL;
1505 dev->mtu = new_mtu;
1506 return 0;
1509 int ip6_tnl_get_iflink(const struct net_device *dev)
1511 struct ip6_tnl *t = netdev_priv(dev);
1513 return t->parms.link;
1515 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1517 static const struct net_device_ops ip6_tnl_netdev_ops = {
1518 .ndo_init = ip6_tnl_dev_init,
1519 .ndo_uninit = ip6_tnl_dev_uninit,
1520 .ndo_start_xmit = ip6_tnl_xmit,
1521 .ndo_do_ioctl = ip6_tnl_ioctl,
1522 .ndo_change_mtu = ip6_tnl_change_mtu,
1523 .ndo_get_stats = ip6_get_stats,
1524 .ndo_get_iflink = ip6_tnl_get_iflink,
1529 * ip6_tnl_dev_setup - setup virtual tunnel device
1530 * @dev: virtual device associated with tunnel
1532 * Description:
1533 * Initialize function pointers and device parameters
1536 static void ip6_tnl_dev_setup(struct net_device *dev)
1538 struct ip6_tnl *t;
1540 dev->netdev_ops = &ip6_tnl_netdev_ops;
1541 dev->destructor = ip6_dev_free;
1543 dev->type = ARPHRD_TUNNEL6;
1544 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1545 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1546 t = netdev_priv(dev);
1547 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1548 dev->mtu -= 8;
1549 dev->flags |= IFF_NOARP;
1550 dev->addr_len = sizeof(struct in6_addr);
1551 netif_keep_dst(dev);
1552 /* This perm addr will be used as interface identifier by IPv6 */
1553 dev->addr_assign_type = NET_ADDR_RANDOM;
1554 eth_random_addr(dev->perm_addr);
1559 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1560 * @dev: virtual device associated with tunnel
1563 static inline int
1564 ip6_tnl_dev_init_gen(struct net_device *dev)
1566 struct ip6_tnl *t = netdev_priv(dev);
1567 int ret;
1569 t->dev = dev;
1570 t->net = dev_net(dev);
1571 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1572 if (!dev->tstats)
1573 return -ENOMEM;
1575 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
1576 if (ret) {
1577 free_percpu(dev->tstats);
1578 dev->tstats = NULL;
1579 return ret;
1582 return 0;
1586 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1587 * @dev: virtual device associated with tunnel
1590 static int ip6_tnl_dev_init(struct net_device *dev)
1592 struct ip6_tnl *t = netdev_priv(dev);
1593 int err = ip6_tnl_dev_init_gen(dev);
1595 if (err)
1596 return err;
1597 ip6_tnl_link_config(t);
1598 return 0;
1602 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1603 * @dev: fallback device
1605 * Return: 0
1608 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1610 struct ip6_tnl *t = netdev_priv(dev);
1611 struct net *net = dev_net(dev);
1612 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1614 t->parms.proto = IPPROTO_IPV6;
1615 dev_hold(dev);
1617 rcu_assign_pointer(ip6n->tnls_wc[0], t);
1618 return 0;
1621 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1623 u8 proto;
1625 if (!data || !data[IFLA_IPTUN_PROTO])
1626 return 0;
1628 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1629 if (proto != IPPROTO_IPV6 &&
1630 proto != IPPROTO_IPIP &&
1631 proto != 0)
1632 return -EINVAL;
1634 return 0;
1637 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1638 struct __ip6_tnl_parm *parms)
1640 memset(parms, 0, sizeof(*parms));
1642 if (!data)
1643 return;
1645 if (data[IFLA_IPTUN_LINK])
1646 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1648 if (data[IFLA_IPTUN_LOCAL])
1649 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1651 if (data[IFLA_IPTUN_REMOTE])
1652 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1654 if (data[IFLA_IPTUN_TTL])
1655 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1657 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1658 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1660 if (data[IFLA_IPTUN_FLOWINFO])
1661 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1663 if (data[IFLA_IPTUN_FLAGS])
1664 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1666 if (data[IFLA_IPTUN_PROTO])
1667 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1670 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1671 struct nlattr *tb[], struct nlattr *data[])
1673 struct net *net = dev_net(dev);
1674 struct ip6_tnl *nt, *t;
1676 nt = netdev_priv(dev);
1677 ip6_tnl_netlink_parms(data, &nt->parms);
1679 t = ip6_tnl_locate(net, &nt->parms, 0);
1680 if (!IS_ERR(t))
1681 return -EEXIST;
1683 return ip6_tnl_create2(dev);
1686 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1687 struct nlattr *data[])
1689 struct ip6_tnl *t = netdev_priv(dev);
1690 struct __ip6_tnl_parm p;
1691 struct net *net = t->net;
1692 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1694 if (dev == ip6n->fb_tnl_dev)
1695 return -EINVAL;
1697 ip6_tnl_netlink_parms(data, &p);
1699 t = ip6_tnl_locate(net, &p, 0);
1700 if (!IS_ERR(t)) {
1701 if (t->dev != dev)
1702 return -EEXIST;
1703 } else
1704 t = netdev_priv(dev);
1706 return ip6_tnl_update(t, &p);
1709 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1711 struct net *net = dev_net(dev);
1712 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1714 if (dev != ip6n->fb_tnl_dev)
1715 unregister_netdevice_queue(dev, head);
1718 static size_t ip6_tnl_get_size(const struct net_device *dev)
1720 return
1721 /* IFLA_IPTUN_LINK */
1722 nla_total_size(4) +
1723 /* IFLA_IPTUN_LOCAL */
1724 nla_total_size(sizeof(struct in6_addr)) +
1725 /* IFLA_IPTUN_REMOTE */
1726 nla_total_size(sizeof(struct in6_addr)) +
1727 /* IFLA_IPTUN_TTL */
1728 nla_total_size(1) +
1729 /* IFLA_IPTUN_ENCAP_LIMIT */
1730 nla_total_size(1) +
1731 /* IFLA_IPTUN_FLOWINFO */
1732 nla_total_size(4) +
1733 /* IFLA_IPTUN_FLAGS */
1734 nla_total_size(4) +
1735 /* IFLA_IPTUN_PROTO */
1736 nla_total_size(1) +
1740 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1742 struct ip6_tnl *tunnel = netdev_priv(dev);
1743 struct __ip6_tnl_parm *parm = &tunnel->parms;
1745 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1746 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1747 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1748 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1749 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1750 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1751 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1752 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1753 goto nla_put_failure;
1754 return 0;
1756 nla_put_failure:
1757 return -EMSGSIZE;
1760 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1762 struct ip6_tnl *tunnel = netdev_priv(dev);
1764 return tunnel->net;
1766 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1768 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1769 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1770 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1771 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1772 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1773 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1774 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1775 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1776 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1779 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1780 .kind = "ip6tnl",
1781 .maxtype = IFLA_IPTUN_MAX,
1782 .policy = ip6_tnl_policy,
1783 .priv_size = sizeof(struct ip6_tnl),
1784 .setup = ip6_tnl_dev_setup,
1785 .validate = ip6_tnl_validate,
1786 .newlink = ip6_tnl_newlink,
1787 .changelink = ip6_tnl_changelink,
1788 .dellink = ip6_tnl_dellink,
1789 .get_size = ip6_tnl_get_size,
1790 .fill_info = ip6_tnl_fill_info,
1791 .get_link_net = ip6_tnl_get_link_net,
1794 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1795 .handler = ip4ip6_rcv,
1796 .err_handler = ip4ip6_err,
1797 .priority = 1,
1800 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1801 .handler = ip6ip6_rcv,
1802 .err_handler = ip6ip6_err,
1803 .priority = 1,
1806 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1808 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1809 struct net_device *dev, *aux;
1810 int h;
1811 struct ip6_tnl *t;
1812 LIST_HEAD(list);
1814 for_each_netdev_safe(net, dev, aux)
1815 if (dev->rtnl_link_ops == &ip6_link_ops)
1816 unregister_netdevice_queue(dev, &list);
1818 for (h = 0; h < HASH_SIZE; h++) {
1819 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1820 while (t) {
1821 /* If dev is in the same netns, it has already
1822 * been added to the list by the previous loop.
1824 if (!net_eq(dev_net(t->dev), net))
1825 unregister_netdevice_queue(t->dev, &list);
1826 t = rtnl_dereference(t->next);
1830 unregister_netdevice_many(&list);
1833 static int __net_init ip6_tnl_init_net(struct net *net)
1835 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1836 struct ip6_tnl *t = NULL;
1837 int err;
1839 ip6n->tnls[0] = ip6n->tnls_wc;
1840 ip6n->tnls[1] = ip6n->tnls_r_l;
1842 err = -ENOMEM;
1843 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1844 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1846 if (!ip6n->fb_tnl_dev)
1847 goto err_alloc_dev;
1848 dev_net_set(ip6n->fb_tnl_dev, net);
1849 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1850 /* FB netdevice is special: we have one, and only one per netns.
1851 * Allowing to move it to another netns is clearly unsafe.
1853 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1855 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1856 if (err < 0)
1857 goto err_register;
1859 err = register_netdev(ip6n->fb_tnl_dev);
1860 if (err < 0)
1861 goto err_register;
1863 t = netdev_priv(ip6n->fb_tnl_dev);
1865 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1866 return 0;
1868 err_register:
1869 ip6_dev_free(ip6n->fb_tnl_dev);
1870 err_alloc_dev:
1871 return err;
1874 static void __net_exit ip6_tnl_exit_net(struct net *net)
1876 rtnl_lock();
1877 ip6_tnl_destroy_tunnels(net);
1878 rtnl_unlock();
1881 static struct pernet_operations ip6_tnl_net_ops = {
1882 .init = ip6_tnl_init_net,
1883 .exit = ip6_tnl_exit_net,
1884 .id = &ip6_tnl_net_id,
1885 .size = sizeof(struct ip6_tnl_net),
1889 * ip6_tunnel_init - register protocol and reserve needed resources
1891 * Return: 0 on success
1894 static int __init ip6_tunnel_init(void)
1896 int err;
1898 err = register_pernet_device(&ip6_tnl_net_ops);
1899 if (err < 0)
1900 goto out_pernet;
1902 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1903 if (err < 0) {
1904 pr_err("%s: can't register ip4ip6\n", __func__);
1905 goto out_ip4ip6;
1908 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1909 if (err < 0) {
1910 pr_err("%s: can't register ip6ip6\n", __func__);
1911 goto out_ip6ip6;
1913 err = rtnl_link_register(&ip6_link_ops);
1914 if (err < 0)
1915 goto rtnl_link_failed;
1917 return 0;
1919 rtnl_link_failed:
1920 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1921 out_ip6ip6:
1922 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1923 out_ip4ip6:
1924 unregister_pernet_device(&ip6_tnl_net_ops);
1925 out_pernet:
1926 return err;
1930 * ip6_tunnel_cleanup - free resources and unregister protocol
1933 static void __exit ip6_tunnel_cleanup(void)
1935 rtnl_link_unregister(&ip6_link_ops);
1936 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1937 pr_info("%s: can't deregister ip4ip6\n", __func__);
1939 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1940 pr_info("%s: can't deregister ip6ip6\n", __func__);
1942 unregister_pernet_device(&ip6_tnl_net_ops);
1945 module_init(ip6_tunnel_init);
1946 module_exit(ip6_tunnel_cleanup);