1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 tunneling device
4 * Linux INET6 implementation
7 * Ville Nuorvala <vnuorval@tcs.hut.fi>
8 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
11 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/capability.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/sockios.h>
23 #include <linux/icmp.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/init.h>
33 #include <linux/route.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/netfilter_ipv6.h>
36 #include <linux/slab.h>
37 #include <linux/hash.h>
38 #include <linux/etherdevice.h>
40 #include <linux/uaccess.h>
41 #include <linux/atomic.h>
45 #include <net/ip_tunnels.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
51 #include <net/dsfield.h>
52 #include <net/inet_ecn.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
55 #include <net/dst_metadata.h>
57 MODULE_AUTHOR("Ville Nuorvala");
58 MODULE_DESCRIPTION("IPv6 tunneling device");
59 MODULE_LICENSE("GPL");
60 MODULE_ALIAS_RTNL_LINK("ip6tnl");
61 MODULE_ALIAS_NETDEV("ip6tnl0");
63 #define IP6_TUNNEL_HASH_SIZE_SHIFT 5
64 #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
66 static bool log_ecn_error = true;
67 module_param(log_ecn_error, bool, 0644);
68 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
70 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
72 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
74 return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
77 static int ip6_tnl_dev_init(struct net_device *dev);
78 static void ip6_tnl_dev_setup(struct net_device *dev);
79 static struct rtnl_link_ops ip6_link_ops __read_mostly;
81 static unsigned int ip6_tnl_net_id __read_mostly;
83 /* the IPv6 tunnel fallback device */
84 struct net_device *fb_tnl_dev;
85 /* lists for storing tunnels in use */
86 struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
87 struct ip6_tnl __rcu *tnls_wc[1];
88 struct ip6_tnl __rcu **tnls[2];
89 struct ip6_tnl __rcu *collect_md_tun;
92 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
94 struct pcpu_sw_netstats tmp, sum = { 0 };
97 for_each_possible_cpu(i) {
99 const struct pcpu_sw_netstats *tstats =
100 per_cpu_ptr(dev->tstats, i);
103 start = u64_stats_fetch_begin_irq(&tstats->syncp);
104 tmp.rx_packets = tstats->rx_packets;
105 tmp.rx_bytes = tstats->rx_bytes;
106 tmp.tx_packets = tstats->tx_packets;
107 tmp.tx_bytes = tstats->tx_bytes;
108 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
110 sum.rx_packets += tmp.rx_packets;
111 sum.rx_bytes += tmp.rx_bytes;
112 sum.tx_packets += tmp.tx_packets;
113 sum.tx_bytes += tmp.tx_bytes;
115 dev->stats.rx_packets = sum.rx_packets;
116 dev->stats.rx_bytes = sum.rx_bytes;
117 dev->stats.tx_packets = sum.tx_packets;
118 dev->stats.tx_bytes = sum.tx_bytes;
123 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
124 * @remote: the address of the tunnel exit-point
125 * @local: the address of the tunnel entry-point
128 * tunnel matching given end-points if found,
129 * else fallback tunnel if its device is up,
133 #define for_each_ip6_tunnel_rcu(start) \
134 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
136 static struct ip6_tnl *
137 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
139 unsigned int hash = HASH(remote, local);
141 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
144 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
145 if (ipv6_addr_equal(local, &t->parms.laddr) &&
146 ipv6_addr_equal(remote, &t->parms.raddr) &&
147 (t->dev->flags & IFF_UP))
151 memset(&any, 0, sizeof(any));
152 hash = HASH(&any, local);
153 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
154 if (ipv6_addr_equal(local, &t->parms.laddr) &&
155 ipv6_addr_any(&t->parms.raddr) &&
156 (t->dev->flags & IFF_UP))
160 hash = HASH(remote, &any);
161 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
162 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
163 ipv6_addr_any(&t->parms.laddr) &&
164 (t->dev->flags & IFF_UP))
168 t = rcu_dereference(ip6n->collect_md_tun);
169 if (t && t->dev->flags & IFF_UP)
172 t = rcu_dereference(ip6n->tnls_wc[0]);
173 if (t && (t->dev->flags & IFF_UP))
180 * ip6_tnl_bucket - get head of list matching given tunnel parameters
181 * @p: parameters containing tunnel end-points
184 * ip6_tnl_bucket() returns the head of the list matching the
185 * &struct in6_addr entries laddr and raddr in @p.
187 * Return: head of IPv6 tunnel list
190 static struct ip6_tnl __rcu **
191 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
193 const struct in6_addr *remote = &p->raddr;
194 const struct in6_addr *local = &p->laddr;
198 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
200 h = HASH(remote, local);
202 return &ip6n->tnls[prio][h];
206 * ip6_tnl_link - add tunnel to hash table
207 * @t: tunnel to be added
211 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
213 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
215 if (t->parms.collect_md)
216 rcu_assign_pointer(ip6n->collect_md_tun, t);
217 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
218 rcu_assign_pointer(*tp, t);
222 * ip6_tnl_unlink - remove tunnel from hash table
223 * @t: tunnel to be removed
227 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
229 struct ip6_tnl __rcu **tp;
230 struct ip6_tnl *iter;
232 if (t->parms.collect_md)
233 rcu_assign_pointer(ip6n->collect_md_tun, NULL);
235 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
236 (iter = rtnl_dereference(*tp)) != NULL;
239 rcu_assign_pointer(*tp, t->next);
245 static void ip6_dev_free(struct net_device *dev)
247 struct ip6_tnl *t = netdev_priv(dev);
249 gro_cells_destroy(&t->gro_cells);
250 dst_cache_destroy(&t->dst_cache);
251 free_percpu(dev->tstats);
254 static int ip6_tnl_create2(struct net_device *dev)
256 struct ip6_tnl *t = netdev_priv(dev);
257 struct net *net = dev_net(dev);
258 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
261 t = netdev_priv(dev);
263 dev->rtnl_link_ops = &ip6_link_ops;
264 err = register_netdevice(dev);
268 strcpy(t->parms.name, dev->name);
271 ip6_tnl_link(ip6n, t);
279 * ip6_tnl_create - create a new tunnel
280 * @p: tunnel parameters
281 * @pt: pointer to new tunnel
284 * Create tunnel matching given parameters.
287 * created tunnel or error pointer
290 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
292 struct net_device *dev;
298 if (!dev_valid_name(p->name))
300 strlcpy(name, p->name, IFNAMSIZ);
302 sprintf(name, "ip6tnl%%d");
305 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
310 dev_net_set(dev, net);
312 t = netdev_priv(dev);
314 t->net = dev_net(dev);
315 err = ip6_tnl_create2(dev);
328 * ip6_tnl_locate - find or create tunnel matching given parameters
329 * @p: tunnel parameters
330 * @create: != 0 if allowed to create new tunnel if no match found
333 * ip6_tnl_locate() first tries to locate an existing tunnel
334 * based on @parms. If this is unsuccessful, but @create is set a new
335 * tunnel device is created and registered for use.
338 * matching tunnel or error pointer
341 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
342 struct __ip6_tnl_parm *p, int create)
344 const struct in6_addr *remote = &p->raddr;
345 const struct in6_addr *local = &p->laddr;
346 struct ip6_tnl __rcu **tp;
348 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
350 for (tp = ip6_tnl_bucket(ip6n, p);
351 (t = rtnl_dereference(*tp)) != NULL;
353 if (ipv6_addr_equal(local, &t->parms.laddr) &&
354 ipv6_addr_equal(remote, &t->parms.raddr)) {
356 return ERR_PTR(-EEXIST);
362 return ERR_PTR(-ENODEV);
363 return ip6_tnl_create(net, p);
367 * ip6_tnl_dev_uninit - tunnel device uninitializer
368 * @dev: the device to be destroyed
371 * ip6_tnl_dev_uninit() removes tunnel from its list
375 ip6_tnl_dev_uninit(struct net_device *dev)
377 struct ip6_tnl *t = netdev_priv(dev);
378 struct net *net = t->net;
379 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
381 if (dev == ip6n->fb_tnl_dev)
382 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
384 ip6_tnl_unlink(ip6n, t);
385 dst_cache_reset(&t->dst_cache);
390 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
391 * @skb: received socket buffer
394 * 0 if none was found,
395 * else index to encapsulation limit
398 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
400 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
401 unsigned int nhoff = raw - skb->data;
402 unsigned int off = nhoff + sizeof(*ipv6h);
403 u8 next, nexthdr = ipv6h->nexthdr;
405 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
406 struct ipv6_opt_hdr *hdr;
409 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
412 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
413 if (nexthdr == NEXTHDR_FRAGMENT) {
414 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
415 if (frag_hdr->frag_off)
418 } else if (nexthdr == NEXTHDR_AUTH) {
419 optlen = (hdr->hdrlen + 2) << 2;
421 optlen = ipv6_optlen(hdr);
423 /* cache hdr->nexthdr, since pskb_may_pull() might
427 if (nexthdr == NEXTHDR_DEST) {
430 /* Remember : hdr is no longer valid at this point. */
431 if (!pskb_may_pull(skb, off + optlen))
435 struct ipv6_tlv_tnl_enc_lim *tel;
437 /* No more room for encapsulation limit */
438 if (i + sizeof(*tel) > optlen)
441 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
442 /* return index of option if found and valid */
443 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
445 return i + off - nhoff;
446 /* else jump to next option */
448 i += tel->length + 2;
458 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
461 * ip6_tnl_err - tunnel error handler
464 * ip6_tnl_err() should handle errors in the tunnel according
465 * to the specifications in RFC 2473.
469 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
470 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
472 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
473 struct net *net = dev_net(skb->dev);
474 u8 rel_type = ICMPV6_DEST_UNREACH;
475 u8 rel_code = ICMPV6_ADDR_UNREACH;
483 /* If the packet doesn't contain the original IPv6 header we are
484 in trouble since we might need the source address for further
485 processing of the error. */
488 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
492 tproto = READ_ONCE(t->parms.proto);
493 if (tproto != ipproto && tproto != 0)
499 struct ipv6_tlv_tnl_enc_lim *tel;
501 case ICMPV6_DEST_UNREACH:
502 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
506 case ICMPV6_TIME_EXCEED:
507 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
508 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
513 case ICMPV6_PARAMPROB:
515 if ((*code) == ICMPV6_HDR_FIELD)
516 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
518 if (teli && teli == *info - 2) {
519 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
520 if (tel->encap_limit == 0) {
521 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
526 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
530 case ICMPV6_PKT_TOOBIG:
531 ip6_update_pmtu(skb, net, htonl(*info), 0, 0,
532 sock_net_uid(net, NULL));
533 mtu = *info - offset;
534 if (mtu < IPV6_MIN_MTU)
536 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
538 rel_type = ICMPV6_PKT_TOOBIG;
545 ip6_redirect(skb, net, skb->dev->ifindex, 0,
546 sock_net_uid(net, NULL));
561 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
562 u8 type, u8 code, int offset, __be32 info)
564 __u32 rel_info = ntohl(info);
565 const struct iphdr *eiph;
566 struct sk_buff *skb2;
567 int err, rel_msg = 0;
573 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
574 &rel_msg, &rel_info, offset);
582 case ICMPV6_DEST_UNREACH:
583 if (rel_code != ICMPV6_ADDR_UNREACH)
585 rel_type = ICMP_DEST_UNREACH;
586 rel_code = ICMP_HOST_UNREACH;
588 case ICMPV6_PKT_TOOBIG:
591 rel_type = ICMP_DEST_UNREACH;
592 rel_code = ICMP_FRAG_NEEDED;
598 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
601 skb2 = skb_clone(skb, GFP_ATOMIC);
607 skb_pull(skb2, offset);
608 skb_reset_network_header(skb2);
611 /* Try to guess incoming interface */
612 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr,
613 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
617 skb2->dev = rt->dst.dev;
620 /* route "incoming" packet */
621 if (rt->rt_flags & RTCF_LOCAL) {
622 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
623 eiph->daddr, eiph->saddr, 0, 0,
624 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
625 if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) {
630 skb_dst_set(skb2, &rt->dst);
632 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
634 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6)
638 /* change mtu on this route */
639 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
640 if (rel_info > dst_mtu(skb_dst(skb2)))
643 skb_dst_update_pmtu(skb2, rel_info);
646 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
654 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
655 u8 type, u8 code, int offset, __be32 info)
657 __u32 rel_info = ntohl(info);
658 int err, rel_msg = 0;
662 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
663 &rel_msg, &rel_info, offset);
667 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
669 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
675 skb_pull(skb2, offset);
676 skb_reset_network_header(skb2);
678 /* Try to guess incoming interface */
679 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
682 if (rt && rt->dst.dev)
683 skb2->dev = rt->dst.dev;
685 icmpv6_send(skb2, rel_type, rel_code, rel_info);
695 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
696 const struct ipv6hdr *ipv6h,
699 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
701 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
702 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
704 return IP6_ECN_decapsulate(ipv6h, skb);
707 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
708 const struct ipv6hdr *ipv6h,
711 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
712 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
714 return IP6_ECN_decapsulate(ipv6h, skb);
717 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
718 const struct in6_addr *laddr,
719 const struct in6_addr *raddr)
721 struct __ip6_tnl_parm *p = &t->parms;
722 int ltype = ipv6_addr_type(laddr);
723 int rtype = ipv6_addr_type(raddr);
726 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
727 flags = IP6_TNL_F_CAP_PER_PACKET;
728 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
729 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
730 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
731 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
732 if (ltype&IPV6_ADDR_UNICAST)
733 flags |= IP6_TNL_F_CAP_XMIT;
734 if (rtype&IPV6_ADDR_UNICAST)
735 flags |= IP6_TNL_F_CAP_RCV;
739 EXPORT_SYMBOL(ip6_tnl_get_cap);
741 /* called with rcu_read_lock() */
742 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
743 const struct in6_addr *laddr,
744 const struct in6_addr *raddr)
746 struct __ip6_tnl_parm *p = &t->parms;
748 struct net *net = t->net;
750 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
751 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
752 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
753 struct net_device *ldev = NULL;
756 ldev = dev_get_by_index_rcu(net, p->link);
758 if ((ipv6_addr_is_multicast(laddr) ||
759 likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false,
760 0, IFA_F_TENTATIVE))) &&
761 ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) ||
762 likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true,
763 0, IFA_F_TENTATIVE))))
768 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
770 static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
771 const struct tnl_ptk_info *tpi,
772 struct metadata_dst *tun_dst,
773 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
774 const struct ipv6hdr *ipv6h,
775 struct sk_buff *skb),
778 struct pcpu_sw_netstats *tstats;
779 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
782 if ((!(tpi->flags & TUNNEL_CSUM) &&
783 (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
784 ((tpi->flags & TUNNEL_CSUM) &&
785 !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
786 tunnel->dev->stats.rx_crc_errors++;
787 tunnel->dev->stats.rx_errors++;
791 if (tunnel->parms.i_flags & TUNNEL_SEQ) {
792 if (!(tpi->flags & TUNNEL_SEQ) ||
794 (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
795 tunnel->dev->stats.rx_fifo_errors++;
796 tunnel->dev->stats.rx_errors++;
799 tunnel->i_seqno = ntohl(tpi->seq) + 1;
802 skb->protocol = tpi->proto;
804 /* Warning: All skb pointers will be invalidated! */
805 if (tunnel->dev->type == ARPHRD_ETHER) {
806 if (!pskb_may_pull(skb, ETH_HLEN)) {
807 tunnel->dev->stats.rx_length_errors++;
808 tunnel->dev->stats.rx_errors++;
812 ipv6h = ipv6_hdr(skb);
813 skb->protocol = eth_type_trans(skb, tunnel->dev);
814 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
816 skb->dev = tunnel->dev;
819 skb_reset_network_header(skb);
820 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
822 __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
824 err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
827 net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
829 ipv6_get_dsfield(ipv6h));
831 ++tunnel->dev->stats.rx_frame_errors;
832 ++tunnel->dev->stats.rx_errors;
837 tstats = this_cpu_ptr(tunnel->dev->tstats);
838 u64_stats_update_begin(&tstats->syncp);
839 tstats->rx_packets++;
840 tstats->rx_bytes += skb->len;
841 u64_stats_update_end(&tstats->syncp);
843 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
846 skb_dst_set(skb, (struct dst_entry *)tun_dst);
848 gro_cells_receive(&tunnel->gro_cells, skb);
853 dst_release((struct dst_entry *)tun_dst);
858 int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
859 const struct tnl_ptk_info *tpi,
860 struct metadata_dst *tun_dst,
863 return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate,
866 EXPORT_SYMBOL(ip6_tnl_rcv);
868 static const struct tnl_ptk_info tpi_v6 = {
869 /* no tunnel info required for ipxip6. */
870 .proto = htons(ETH_P_IPV6),
873 static const struct tnl_ptk_info tpi_v4 = {
874 /* no tunnel info required for ipxip6. */
875 .proto = htons(ETH_P_IP),
878 static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
879 const struct tnl_ptk_info *tpi,
880 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
881 const struct ipv6hdr *ipv6h,
882 struct sk_buff *skb))
885 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
886 struct metadata_dst *tun_dst = NULL;
890 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
893 u8 tproto = READ_ONCE(t->parms.proto);
895 if (tproto != ipproto && tproto != 0)
897 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
899 ipv6h = ipv6_hdr(skb);
900 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
902 if (iptunnel_pull_header(skb, 0, tpi->proto, false))
904 if (t->parms.collect_md) {
905 tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
909 ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
923 static int ip4ip6_rcv(struct sk_buff *skb)
925 return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
926 ip4ip6_dscp_ecn_decapsulate);
929 static int ip6ip6_rcv(struct sk_buff *skb)
931 return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
932 ip6ip6_dscp_ecn_decapsulate);
935 struct ipv6_tel_txoption {
936 struct ipv6_txoptions ops;
940 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
942 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
944 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
946 opt->dst_opt[4] = encap_limit;
947 opt->dst_opt[5] = IPV6_TLV_PADN;
950 opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt;
951 opt->ops.opt_nflen = 8;
955 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
956 * @t: the outgoing tunnel device
957 * @hdr: IPv6 header from the incoming packet
960 * Avoid trivial tunneling loop by checking that tunnel exit-point
961 * doesn't match source of incoming packet.
969 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
971 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
974 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
975 const struct in6_addr *laddr,
976 const struct in6_addr *raddr)
978 struct __ip6_tnl_parm *p = &t->parms;
980 struct net *net = t->net;
982 if (t->parms.collect_md)
985 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
986 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
987 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
988 struct net_device *ldev = NULL;
992 ldev = dev_get_by_index_rcu(net, p->link);
994 if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false,
995 0, IFA_F_TENTATIVE)))
996 pr_warn("%s xmit: Local address not yet configured!\n",
998 else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) &&
999 !ipv6_addr_is_multicast(raddr) &&
1000 unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev,
1001 true, 0, IFA_F_TENTATIVE)))
1002 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1010 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1013 * ip6_tnl_xmit - encapsulate packet and send
1014 * @skb: the outgoing socket buffer
1015 * @dev: the outgoing tunnel device
1016 * @dsfield: dscp code for outer header
1017 * @fl6: flow of tunneled packet
1018 * @encap_limit: encapsulation limit
1019 * @pmtu: Path MTU is stored if packet is too big
1020 * @proto: next header value
1023 * Build new header and do some sanity checks on the packet before sending
1029 * %-EMSGSIZE message too big. return mtu in this case.
1032 int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
1033 struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
1036 struct ip6_tnl *t = netdev_priv(dev);
1037 struct net *net = t->net;
1038 struct net_device_stats *stats = &t->dev->stats;
1039 struct ipv6hdr *ipv6h;
1040 struct ipv6_tel_txoption opt;
1041 struct dst_entry *dst = NULL, *ndst = NULL;
1042 struct net_device *tdev;
1044 unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
1045 unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
1046 unsigned int max_headroom = psh_hlen;
1047 bool use_cache = false;
1051 if (t->parms.collect_md) {
1052 hop_limit = skb_tunnel_info(skb)->key.ttl;
1055 hop_limit = t->parms.hop_limit;
1059 if (ipv6_addr_any(&t->parms.raddr)) {
1060 if (skb->protocol == htons(ETH_P_IPV6)) {
1061 struct in6_addr *addr6;
1062 struct neighbour *neigh;
1066 goto tx_err_link_failure;
1068 neigh = dst_neigh_lookup(skb_dst(skb),
1069 &ipv6_hdr(skb)->daddr);
1071 goto tx_err_link_failure;
1073 addr6 = (struct in6_addr *)&neigh->primary_key;
1074 addr_type = ipv6_addr_type(addr6);
1076 if (addr_type == IPV6_ADDR_ANY)
1077 addr6 = &ipv6_hdr(skb)->daddr;
1079 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1080 neigh_release(neigh);
1082 } else if (t->parms.proto != 0 && !(t->parms.flags &
1083 (IP6_TNL_F_USE_ORIG_TCLASS |
1084 IP6_TNL_F_USE_ORIG_FWMARK))) {
1085 /* enable the cache only if neither the outer protocol nor the
1086 * routing decision depends on the current inner header value
1092 dst = dst_cache_get(&t->dst_cache);
1094 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1095 goto tx_err_link_failure;
1099 /* add dsfield to flowlabel for route lookup */
1100 fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
1102 dst = ip6_route_output(net, NULL, fl6);
1105 goto tx_err_link_failure;
1106 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1110 goto tx_err_link_failure;
1112 if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) &&
1113 ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
1114 &fl6->daddr, 0, &fl6->saddr))
1115 goto tx_err_link_failure;
1122 stats->collisions++;
1123 net_warn_ratelimited("%s: Local routing loop detected!\n",
1125 goto tx_err_dst_release;
1127 mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
1128 if (encap_limit >= 0) {
1132 mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ?
1133 IPV6_MIN_MTU : IPV4_MIN_MTU);
1135 skb_dst_update_pmtu(skb, mtu);
1136 if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
1139 goto tx_err_dst_release;
1142 if (t->err_count > 0) {
1143 if (time_before(jiffies,
1144 t->err_time + IP6TUNNEL_ERR_TIMEO)) {
1147 dst_link_failure(skb);
1153 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1156 * Okay, now see if we can stuff it in the buffer as-is.
1158 max_headroom += LL_RESERVED_SPACE(tdev);
1160 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1161 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1162 struct sk_buff *new_skb;
1164 new_skb = skb_realloc_headroom(skb, max_headroom);
1166 goto tx_err_dst_release;
1169 skb_set_owner_w(new_skb, skb->sk);
1174 if (t->parms.collect_md) {
1175 if (t->encap.type != TUNNEL_ENCAP_NONE)
1176 goto tx_err_dst_release;
1178 if (use_cache && ndst)
1179 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1181 skb_dst_set(skb, dst);
1183 if (hop_limit == 0) {
1184 if (skb->protocol == htons(ETH_P_IP))
1185 hop_limit = ip_hdr(skb)->ttl;
1186 else if (skb->protocol == htons(ETH_P_IPV6))
1187 hop_limit = ipv6_hdr(skb)->hop_limit;
1189 hop_limit = ip6_dst_hoplimit(dst);
1192 /* Calculate max headroom for all the headers and adjust
1193 * needed_headroom if necessary.
1195 max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
1196 + dst->header_len + t->hlen;
1197 if (max_headroom > dev->needed_headroom)
1198 dev->needed_headroom = max_headroom;
1200 err = ip6_tnl_encap(skb, t, &proto, fl6);
1204 if (encap_limit >= 0) {
1205 init_tel_txopt(&opt, encap_limit);
1206 ipv6_push_frag_opts(skb, &opt.ops, &proto);
1209 skb_push(skb, sizeof(struct ipv6hdr));
1210 skb_reset_network_header(skb);
1211 ipv6h = ipv6_hdr(skb);
1212 ip6_flow_hdr(ipv6h, dsfield,
1213 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1214 ipv6h->hop_limit = hop_limit;
1215 ipv6h->nexthdr = proto;
1216 ipv6h->saddr = fl6->saddr;
1217 ipv6h->daddr = fl6->daddr;
1218 ip6tunnel_xmit(NULL, skb, dev);
1220 tx_err_link_failure:
1221 stats->tx_carrier_errors++;
1222 dst_link_failure(skb);
1227 EXPORT_SYMBOL(ip6_tnl_xmit);
1230 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1232 struct ip6_tnl *t = netdev_priv(dev);
1233 const struct iphdr *iph;
1234 int encap_limit = -1;
1242 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1244 tproto = READ_ONCE(t->parms.proto);
1245 if (tproto != IPPROTO_IPIP && tproto != 0)
1248 if (t->parms.collect_md) {
1249 struct ip_tunnel_info *tun_info;
1250 const struct ip_tunnel_key *key;
1252 tun_info = skb_tunnel_info(skb);
1253 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1254 ip_tunnel_info_af(tun_info) != AF_INET6))
1256 key = &tun_info->key;
1257 memset(&fl6, 0, sizeof(fl6));
1258 fl6.flowi6_proto = IPPROTO_IPIP;
1259 fl6.saddr = key->u.ipv6.src;
1260 fl6.daddr = key->u.ipv6.dst;
1261 fl6.flowlabel = key->label;
1264 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1265 encap_limit = t->parms.encap_limit;
1267 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1268 fl6.flowi6_proto = IPPROTO_IPIP;
1270 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1271 dsfield = ipv4_get_dsfield(iph);
1273 dsfield = ip6_tclass(t->parms.flowinfo);
1274 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1275 fl6.flowi6_mark = skb->mark;
1277 fl6.flowi6_mark = t->parms.fwmark;
1280 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1282 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1285 dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
1287 skb_set_inner_ipproto(skb, IPPROTO_IPIP);
1289 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1292 /* XXX: send ICMP error even if DF is not set. */
1293 if (err == -EMSGSIZE)
1294 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1303 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1305 struct ip6_tnl *t = netdev_priv(dev);
1306 struct ipv6hdr *ipv6h;
1307 int encap_limit = -1;
1315 ipv6h = ipv6_hdr(skb);
1316 tproto = READ_ONCE(t->parms.proto);
1317 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1318 ip6_tnl_addr_conflict(t, ipv6h))
1321 if (t->parms.collect_md) {
1322 struct ip_tunnel_info *tun_info;
1323 const struct ip_tunnel_key *key;
1325 tun_info = skb_tunnel_info(skb);
1326 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
1327 ip_tunnel_info_af(tun_info) != AF_INET6))
1329 key = &tun_info->key;
1330 memset(&fl6, 0, sizeof(fl6));
1331 fl6.flowi6_proto = IPPROTO_IPV6;
1332 fl6.saddr = key->u.ipv6.src;
1333 fl6.daddr = key->u.ipv6.dst;
1334 fl6.flowlabel = key->label;
1337 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1338 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
1339 ipv6h = ipv6_hdr(skb);
1341 struct ipv6_tlv_tnl_enc_lim *tel;
1343 tel = (void *)&skb_network_header(skb)[offset];
1344 if (tel->encap_limit == 0) {
1345 icmpv6_send(skb, ICMPV6_PARAMPROB,
1346 ICMPV6_HDR_FIELD, offset + 2);
1349 encap_limit = tel->encap_limit - 1;
1350 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
1351 encap_limit = t->parms.encap_limit;
1354 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1355 fl6.flowi6_proto = IPPROTO_IPV6;
1357 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1358 dsfield = ipv6_get_dsfield(ipv6h);
1360 dsfield = ip6_tclass(t->parms.flowinfo);
1361 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1362 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1363 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1364 fl6.flowi6_mark = skb->mark;
1366 fl6.flowi6_mark = t->parms.fwmark;
1369 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1371 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
1374 dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
1376 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
1378 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1381 if (err == -EMSGSIZE)
1382 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1390 ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
1392 struct ip6_tnl *t = netdev_priv(dev);
1393 struct net_device_stats *stats = &t->dev->stats;
1396 if (!pskb_inet_may_pull(skb))
1399 switch (skb->protocol) {
1400 case htons(ETH_P_IP):
1401 ret = ip4ip6_tnl_xmit(skb, dev);
1403 case htons(ETH_P_IPV6):
1404 ret = ip6ip6_tnl_xmit(skb, dev);
1413 return NETDEV_TX_OK;
1417 stats->tx_dropped++;
1419 return NETDEV_TX_OK;
1422 static void ip6_tnl_link_config(struct ip6_tnl *t)
1424 struct net_device *dev = t->dev;
1425 struct __ip6_tnl_parm *p = &t->parms;
1426 struct flowi6 *fl6 = &t->fl.u.ip6;
1429 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1430 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1432 /* Set up flowi template */
1433 fl6->saddr = p->laddr;
1434 fl6->daddr = p->raddr;
1435 fl6->flowi6_oif = p->link;
1438 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1439 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1440 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1441 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1443 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1444 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1446 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1447 dev->flags |= IFF_POINTOPOINT;
1449 dev->flags &= ~IFF_POINTOPOINT;
1452 t->hlen = t->encap_hlen + t->tun_hlen;
1453 t_hlen = t->hlen + sizeof(struct ipv6hdr);
1455 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1456 int strict = (ipv6_addr_type(&p->raddr) &
1457 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1459 struct rt6_info *rt = rt6_lookup(t->net,
1460 &p->raddr, &p->laddr,
1461 p->link, NULL, strict);
1467 dev->hard_header_len = rt->dst.dev->hard_header_len +
1470 dev->mtu = rt->dst.dev->mtu - t_hlen;
1471 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1474 if (dev->mtu < IPV6_MIN_MTU)
1475 dev->mtu = IPV6_MIN_MTU;
1482 * ip6_tnl_change - update the tunnel parameters
1483 * @t: tunnel to be changed
1484 * @p: tunnel configuration parameters
1487 * ip6_tnl_change() updates the tunnel parameters
1491 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1493 t->parms.laddr = p->laddr;
1494 t->parms.raddr = p->raddr;
1495 t->parms.flags = p->flags;
1496 t->parms.hop_limit = p->hop_limit;
1497 t->parms.encap_limit = p->encap_limit;
1498 t->parms.flowinfo = p->flowinfo;
1499 t->parms.link = p->link;
1500 t->parms.proto = p->proto;
1501 t->parms.fwmark = p->fwmark;
1502 dst_cache_reset(&t->dst_cache);
1503 ip6_tnl_link_config(t);
1507 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1509 struct net *net = t->net;
1510 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1513 ip6_tnl_unlink(ip6n, t);
1515 err = ip6_tnl_change(t, p);
1516 ip6_tnl_link(ip6n, t);
1517 netdev_state_change(t->dev);
1521 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1523 /* for default tnl0 device allow to change only the proto */
1524 t->parms.proto = p->proto;
1525 netdev_state_change(t->dev);
1530 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1532 p->laddr = u->laddr;
1533 p->raddr = u->raddr;
1534 p->flags = u->flags;
1535 p->hop_limit = u->hop_limit;
1536 p->encap_limit = u->encap_limit;
1537 p->flowinfo = u->flowinfo;
1539 p->proto = u->proto;
1540 memcpy(p->name, u->name, sizeof(u->name));
1544 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1546 u->laddr = p->laddr;
1547 u->raddr = p->raddr;
1548 u->flags = p->flags;
1549 u->hop_limit = p->hop_limit;
1550 u->encap_limit = p->encap_limit;
1551 u->flowinfo = p->flowinfo;
1553 u->proto = p->proto;
1554 memcpy(u->name, p->name, sizeof(u->name));
1558 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1559 * @dev: virtual device associated with tunnel
1560 * @ifr: parameters passed from userspace
1561 * @cmd: command to be performed
1564 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1567 * The possible commands are the following:
1568 * %SIOCGETTUNNEL: get tunnel parameters for device
1569 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1570 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1571 * %SIOCDELTUNNEL: delete tunnel
1573 * The fallback device "ip6tnl0", created during module
1574 * initialization, can be used for creating other tunnel devices.
1578 * %-EFAULT if unable to copy data to or from userspace,
1579 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1580 * %-EINVAL if passed tunnel parameters are invalid,
1581 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1582 * %-ENODEV if attempting to change or delete a nonexisting device
1586 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1589 struct ip6_tnl_parm p;
1590 struct __ip6_tnl_parm p1;
1591 struct ip6_tnl *t = netdev_priv(dev);
1592 struct net *net = t->net;
1593 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1595 memset(&p1, 0, sizeof(p1));
1599 if (dev == ip6n->fb_tnl_dev) {
1600 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1604 ip6_tnl_parm_from_user(&p1, &p);
1605 t = ip6_tnl_locate(net, &p1, 0);
1607 t = netdev_priv(dev);
1609 memset(&p, 0, sizeof(p));
1611 ip6_tnl_parm_to_user(&p, &t->parms);
1612 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1619 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1622 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1625 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1628 ip6_tnl_parm_from_user(&p1, &p);
1629 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1630 if (cmd == SIOCCHGTUNNEL) {
1632 if (t->dev != dev) {
1637 t = netdev_priv(dev);
1638 if (dev == ip6n->fb_tnl_dev)
1639 err = ip6_tnl0_update(t, &p1);
1641 err = ip6_tnl_update(t, &p1);
1645 ip6_tnl_parm_to_user(&p, &t->parms);
1646 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1655 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1658 if (dev == ip6n->fb_tnl_dev) {
1660 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1663 ip6_tnl_parm_from_user(&p1, &p);
1664 t = ip6_tnl_locate(net, &p1, 0);
1668 if (t->dev == ip6n->fb_tnl_dev)
1673 unregister_netdevice(dev);
1682 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1683 * @dev: virtual device associated with tunnel
1684 * @new_mtu: the new mtu
1688 * %-EINVAL if mtu too small
1691 int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1693 struct ip6_tnl *tnl = netdev_priv(dev);
1695 if (tnl->parms.proto == IPPROTO_IPV6) {
1696 if (new_mtu < IPV6_MIN_MTU)
1699 if (new_mtu < ETH_MIN_MTU)
1702 if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) {
1703 if (new_mtu > IP6_MAX_MTU - dev->hard_header_len)
1706 if (new_mtu > IP_MAX_MTU - dev->hard_header_len)
1712 EXPORT_SYMBOL(ip6_tnl_change_mtu);
1714 int ip6_tnl_get_iflink(const struct net_device *dev)
1716 struct ip6_tnl *t = netdev_priv(dev);
1718 return t->parms.link;
1720 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1722 int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
1725 if (num >= MAX_IPTUN_ENCAP_OPS)
1728 return !cmpxchg((const struct ip6_tnl_encap_ops **)
1729 &ip6tun_encaps[num],
1730 NULL, ops) ? 0 : -1;
1732 EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
1734 int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
1739 if (num >= MAX_IPTUN_ENCAP_OPS)
1742 ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
1743 &ip6tun_encaps[num],
1744 ops, NULL) == ops) ? 0 : -1;
1750 EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
1752 int ip6_tnl_encap_setup(struct ip6_tnl *t,
1753 struct ip_tunnel_encap *ipencap)
1757 memset(&t->encap, 0, sizeof(t->encap));
1759 hlen = ip6_encap_hlen(ipencap);
1763 t->encap.type = ipencap->type;
1764 t->encap.sport = ipencap->sport;
1765 t->encap.dport = ipencap->dport;
1766 t->encap.flags = ipencap->flags;
1768 t->encap_hlen = hlen;
1769 t->hlen = t->encap_hlen + t->tun_hlen;
1773 EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
1775 static const struct net_device_ops ip6_tnl_netdev_ops = {
1776 .ndo_init = ip6_tnl_dev_init,
1777 .ndo_uninit = ip6_tnl_dev_uninit,
1778 .ndo_start_xmit = ip6_tnl_start_xmit,
1779 .ndo_do_ioctl = ip6_tnl_ioctl,
1780 .ndo_change_mtu = ip6_tnl_change_mtu,
1781 .ndo_get_stats = ip6_get_stats,
1782 .ndo_get_iflink = ip6_tnl_get_iflink,
1785 #define IPXIPX_FEATURES (NETIF_F_SG | \
1786 NETIF_F_FRAGLIST | \
1788 NETIF_F_GSO_SOFTWARE | \
1792 * ip6_tnl_dev_setup - setup virtual tunnel device
1793 * @dev: virtual device associated with tunnel
1796 * Initialize function pointers and device parameters
1799 static void ip6_tnl_dev_setup(struct net_device *dev)
1801 dev->netdev_ops = &ip6_tnl_netdev_ops;
1802 dev->needs_free_netdev = true;
1803 dev->priv_destructor = ip6_dev_free;
1805 dev->type = ARPHRD_TUNNEL6;
1806 dev->flags |= IFF_NOARP;
1807 dev->addr_len = sizeof(struct in6_addr);
1808 dev->features |= NETIF_F_LLTX;
1809 netif_keep_dst(dev);
1811 dev->features |= IPXIPX_FEATURES;
1812 dev->hw_features |= IPXIPX_FEATURES;
1814 /* This perm addr will be used as interface identifier by IPv6 */
1815 dev->addr_assign_type = NET_ADDR_RANDOM;
1816 eth_random_addr(dev->perm_addr);
1821 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1822 * @dev: virtual device associated with tunnel
1826 ip6_tnl_dev_init_gen(struct net_device *dev)
1828 struct ip6_tnl *t = netdev_priv(dev);
1833 t->net = dev_net(dev);
1834 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1838 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
1842 ret = gro_cells_init(&t->gro_cells, dev);
1847 t->hlen = t->encap_hlen + t->tun_hlen;
1848 t_hlen = t->hlen + sizeof(struct ipv6hdr);
1850 dev->type = ARPHRD_TUNNEL6;
1851 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1852 dev->mtu = ETH_DATA_LEN - t_hlen;
1853 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1855 dev->min_mtu = ETH_MIN_MTU;
1856 dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len;
1861 dst_cache_destroy(&t->dst_cache);
1863 free_percpu(dev->tstats);
1870 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1871 * @dev: virtual device associated with tunnel
1874 static int ip6_tnl_dev_init(struct net_device *dev)
1876 struct ip6_tnl *t = netdev_priv(dev);
1877 int err = ip6_tnl_dev_init_gen(dev);
1881 ip6_tnl_link_config(t);
1882 if (t->parms.collect_md) {
1883 dev->features |= NETIF_F_NETNS_LOCAL;
1884 netif_keep_dst(dev);
1890 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1891 * @dev: fallback device
1896 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1898 struct ip6_tnl *t = netdev_priv(dev);
1899 struct net *net = dev_net(dev);
1900 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1902 t->parms.proto = IPPROTO_IPV6;
1905 rcu_assign_pointer(ip6n->tnls_wc[0], t);
1909 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[],
1910 struct netlink_ext_ack *extack)
1914 if (!data || !data[IFLA_IPTUN_PROTO])
1917 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1918 if (proto != IPPROTO_IPV6 &&
1919 proto != IPPROTO_IPIP &&
1926 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1927 struct __ip6_tnl_parm *parms)
1929 memset(parms, 0, sizeof(*parms));
1934 if (data[IFLA_IPTUN_LINK])
1935 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1937 if (data[IFLA_IPTUN_LOCAL])
1938 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1940 if (data[IFLA_IPTUN_REMOTE])
1941 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1943 if (data[IFLA_IPTUN_TTL])
1944 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1946 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1947 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1949 if (data[IFLA_IPTUN_FLOWINFO])
1950 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1952 if (data[IFLA_IPTUN_FLAGS])
1953 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1955 if (data[IFLA_IPTUN_PROTO])
1956 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1958 if (data[IFLA_IPTUN_COLLECT_METADATA])
1959 parms->collect_md = true;
1961 if (data[IFLA_IPTUN_FWMARK])
1962 parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
1965 static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
1966 struct ip_tunnel_encap *ipencap)
1970 memset(ipencap, 0, sizeof(*ipencap));
1975 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1977 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1980 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1982 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1985 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1987 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
1990 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1992 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
1998 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1999 struct nlattr *tb[], struct nlattr *data[],
2000 struct netlink_ext_ack *extack)
2002 struct net *net = dev_net(dev);
2003 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2004 struct ip_tunnel_encap ipencap;
2005 struct ip6_tnl *nt, *t;
2008 nt = netdev_priv(dev);
2010 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2011 err = ip6_tnl_encap_setup(nt, &ipencap);
2016 ip6_tnl_netlink_parms(data, &nt->parms);
2018 if (nt->parms.collect_md) {
2019 if (rtnl_dereference(ip6n->collect_md_tun))
2022 t = ip6_tnl_locate(net, &nt->parms, 0);
2027 err = ip6_tnl_create2(dev);
2028 if (!err && tb[IFLA_MTU])
2029 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2034 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
2035 struct nlattr *data[],
2036 struct netlink_ext_ack *extack)
2038 struct ip6_tnl *t = netdev_priv(dev);
2039 struct __ip6_tnl_parm p;
2040 struct net *net = t->net;
2041 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2042 struct ip_tunnel_encap ipencap;
2044 if (dev == ip6n->fb_tnl_dev)
2047 if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
2048 int err = ip6_tnl_encap_setup(t, &ipencap);
2053 ip6_tnl_netlink_parms(data, &p);
2057 t = ip6_tnl_locate(net, &p, 0);
2062 t = netdev_priv(dev);
2064 return ip6_tnl_update(t, &p);
2067 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
2069 struct net *net = dev_net(dev);
2070 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2072 if (dev != ip6n->fb_tnl_dev)
2073 unregister_netdevice_queue(dev, head);
2076 static size_t ip6_tnl_get_size(const struct net_device *dev)
2079 /* IFLA_IPTUN_LINK */
2081 /* IFLA_IPTUN_LOCAL */
2082 nla_total_size(sizeof(struct in6_addr)) +
2083 /* IFLA_IPTUN_REMOTE */
2084 nla_total_size(sizeof(struct in6_addr)) +
2085 /* IFLA_IPTUN_TTL */
2087 /* IFLA_IPTUN_ENCAP_LIMIT */
2089 /* IFLA_IPTUN_FLOWINFO */
2091 /* IFLA_IPTUN_FLAGS */
2093 /* IFLA_IPTUN_PROTO */
2095 /* IFLA_IPTUN_ENCAP_TYPE */
2097 /* IFLA_IPTUN_ENCAP_FLAGS */
2099 /* IFLA_IPTUN_ENCAP_SPORT */
2101 /* IFLA_IPTUN_ENCAP_DPORT */
2103 /* IFLA_IPTUN_COLLECT_METADATA */
2105 /* IFLA_IPTUN_FWMARK */
2110 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
2112 struct ip6_tnl *tunnel = netdev_priv(dev);
2113 struct __ip6_tnl_parm *parm = &tunnel->parms;
2115 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
2116 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
2117 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
2118 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
2119 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
2120 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
2121 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
2122 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) ||
2123 nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark))
2124 goto nla_put_failure;
2126 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
2127 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
2128 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
2129 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
2130 goto nla_put_failure;
2132 if (parm->collect_md)
2133 if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
2134 goto nla_put_failure;
2142 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
2144 struct ip6_tnl *tunnel = netdev_priv(dev);
2148 EXPORT_SYMBOL(ip6_tnl_get_link_net);
2150 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
2151 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
2152 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
2153 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
2154 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
2155 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
2156 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
2157 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
2158 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
2159 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
2160 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
2161 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
2162 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
2163 [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
2164 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
2167 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
2169 .maxtype = IFLA_IPTUN_MAX,
2170 .policy = ip6_tnl_policy,
2171 .priv_size = sizeof(struct ip6_tnl),
2172 .setup = ip6_tnl_dev_setup,
2173 .validate = ip6_tnl_validate,
2174 .newlink = ip6_tnl_newlink,
2175 .changelink = ip6_tnl_changelink,
2176 .dellink = ip6_tnl_dellink,
2177 .get_size = ip6_tnl_get_size,
2178 .fill_info = ip6_tnl_fill_info,
2179 .get_link_net = ip6_tnl_get_link_net,
2182 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
2183 .handler = ip4ip6_rcv,
2184 .err_handler = ip4ip6_err,
2188 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
2189 .handler = ip6ip6_rcv,
2190 .err_handler = ip6ip6_err,
2194 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list)
2196 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2197 struct net_device *dev, *aux;
2201 for_each_netdev_safe(net, dev, aux)
2202 if (dev->rtnl_link_ops == &ip6_link_ops)
2203 unregister_netdevice_queue(dev, list);
2205 for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
2206 t = rtnl_dereference(ip6n->tnls_r_l[h]);
2208 /* If dev is in the same netns, it has already
2209 * been added to the list by the previous loop.
2211 if (!net_eq(dev_net(t->dev), net))
2212 unregister_netdevice_queue(t->dev, list);
2213 t = rtnl_dereference(t->next);
2218 static int __net_init ip6_tnl_init_net(struct net *net)
2220 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
2221 struct ip6_tnl *t = NULL;
2224 ip6n->tnls[0] = ip6n->tnls_wc;
2225 ip6n->tnls[1] = ip6n->tnls_r_l;
2227 if (!net_has_fallback_tunnels(net))
2230 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
2231 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
2233 if (!ip6n->fb_tnl_dev)
2235 dev_net_set(ip6n->fb_tnl_dev, net);
2236 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
2237 /* FB netdevice is special: we have one, and only one per netns.
2238 * Allowing to move it to another netns is clearly unsafe.
2240 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
2242 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
2246 err = register_netdev(ip6n->fb_tnl_dev);
2250 t = netdev_priv(ip6n->fb_tnl_dev);
2252 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
2256 free_netdev(ip6n->fb_tnl_dev);
2261 static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list)
2267 list_for_each_entry(net, net_list, exit_list)
2268 ip6_tnl_destroy_tunnels(net, &list);
2269 unregister_netdevice_many(&list);
2273 static struct pernet_operations ip6_tnl_net_ops = {
2274 .init = ip6_tnl_init_net,
2275 .exit_batch = ip6_tnl_exit_batch_net,
2276 .id = &ip6_tnl_net_id,
2277 .size = sizeof(struct ip6_tnl_net),
2281 * ip6_tunnel_init - register protocol and reserve needed resources
2283 * Return: 0 on success
2286 static int __init ip6_tunnel_init(void)
2290 if (!ipv6_mod_enabled())
2293 err = register_pernet_device(&ip6_tnl_net_ops);
2297 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
2299 pr_err("%s: can't register ip4ip6\n", __func__);
2303 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
2305 pr_err("%s: can't register ip6ip6\n", __func__);
2308 err = rtnl_link_register(&ip6_link_ops);
2310 goto rtnl_link_failed;
2315 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
2317 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
2319 unregister_pernet_device(&ip6_tnl_net_ops);
2325 * ip6_tunnel_cleanup - free resources and unregister protocol
2328 static void __exit ip6_tunnel_cleanup(void)
2330 rtnl_link_unregister(&ip6_link_ops);
2331 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2332 pr_info("%s: can't deregister ip4ip6\n", __func__);
2334 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2335 pr_info("%s: can't deregister ip6ip6\n", __func__);
2337 unregister_pernet_device(&ip6_tnl_net_ops);
2340 module_init(ip6_tunnel_init);
2341 module_exit(ip6_tunnel_cleanup);