2 * IPv6 tunneling device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
10 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
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>
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>
50 #include <net/ip_tunnels.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.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;
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 };
100 for_each_possible_cpu(i) {
102 const struct pcpu_sw_netstats *tstats =
103 per_cpu_ptr(dev->tstats, i);
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;
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
131 * tunnel matching given end-points if found,
132 * else fallback tunnel if its device is up,
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);
144 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
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))
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))
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))
171 t = rcu_dereference(ip6n->tnls_wc[0]);
172 if (t && (t->dev->flags & IFF_UP))
179 * ip6_tnl_bucket - get head of list matching given tunnel parameters
180 * @p: parameters containing tunnel end-points
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;
197 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
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
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
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;
233 rcu_assign_pointer(*tp, t->next);
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);
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);
255 t = netdev_priv(dev);
257 dev->rtnl_link_ops = &ip6_link_ops;
258 err = register_netdevice(dev);
262 strcpy(t->parms.name, dev->name);
264 ip6_tnl_link(ip6n, t);
272 * ip6_tnl_create - create a new tunnel
273 * @p: tunnel parameters
274 * @pt: pointer to new tunnel
277 * Create tunnel matching given parameters.
280 * created tunnel or error pointer
283 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
285 struct net_device *dev;
291 if (!dev_valid_name(p->name))
293 strlcpy(name, p->name, IFNAMSIZ);
295 sprintf(name, "ip6tnl%%d");
298 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
303 dev_net_set(dev, net);
305 t = netdev_priv(dev);
307 t->net = dev_net(dev);
308 err = ip6_tnl_create2(dev);
321 * ip6_tnl_locate - find or create tunnel matching given parameters
322 * @p: tunnel parameters
323 * @create: != 0 if allowed to create new tunnel if no match found
326 * ip6_tnl_locate() first tries to locate an existing tunnel
327 * based on @parms. If this is unsuccessful, but @create is set a new
328 * tunnel device is created and registered for use.
331 * matching tunnel or error pointer
334 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
335 struct __ip6_tnl_parm *p, int create)
337 const struct in6_addr *remote = &p->raddr;
338 const struct in6_addr *local = &p->laddr;
339 struct ip6_tnl __rcu **tp;
341 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
343 for (tp = ip6_tnl_bucket(ip6n, p);
344 (t = rtnl_dereference(*tp)) != NULL;
346 if (ipv6_addr_equal(local, &t->parms.laddr) &&
347 ipv6_addr_equal(remote, &t->parms.raddr)) {
349 return ERR_PTR(-EEXIST);
355 return ERR_PTR(-ENODEV);
356 return ip6_tnl_create(net, p);
360 * ip6_tnl_dev_uninit - tunnel device uninitializer
361 * @dev: the device to be destroyed
364 * ip6_tnl_dev_uninit() removes tunnel from its list
368 ip6_tnl_dev_uninit(struct net_device *dev)
370 struct ip6_tnl *t = netdev_priv(dev);
371 struct net *net = t->net;
372 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
374 if (dev == ip6n->fb_tnl_dev)
375 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
377 ip6_tnl_unlink(ip6n, t);
378 dst_cache_reset(&t->dst_cache);
383 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
384 * @skb: received socket buffer
387 * 0 if none was found,
388 * else index to encapsulation limit
391 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
393 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
394 unsigned int nhoff = raw - skb->data;
395 unsigned int off = nhoff + sizeof(*ipv6h);
396 u8 next, nexthdr = ipv6h->nexthdr;
398 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
399 struct ipv6_opt_hdr *hdr;
402 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
405 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
406 if (nexthdr == NEXTHDR_FRAGMENT) {
407 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
408 if (frag_hdr->frag_off)
411 } else if (nexthdr == NEXTHDR_AUTH) {
412 optlen = (hdr->hdrlen + 2) << 2;
414 optlen = ipv6_optlen(hdr);
416 /* cache hdr->nexthdr, since pskb_may_pull() might
420 if (nexthdr == NEXTHDR_DEST) {
423 /* Remember : hdr is no longer valid at this point. */
424 if (!pskb_may_pull(skb, off + optlen))
428 struct ipv6_tlv_tnl_enc_lim *tel;
430 /* No more room for encapsulation limit */
431 if (i + sizeof(*tel) > optlen)
434 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
435 /* return index of option if found and valid */
436 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
438 return i + off - nhoff;
439 /* else jump to next option */
441 i += tel->length + 2;
451 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
454 * ip6_tnl_err - tunnel error handler
457 * ip6_tnl_err() should handle errors in the tunnel according
458 * to the specifications in RFC 2473.
462 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
463 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
465 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
468 u8 rel_type = ICMPV6_DEST_UNREACH;
469 u8 rel_code = ICMPV6_ADDR_UNREACH;
475 /* If the packet doesn't contain the original IPv6 header we are
476 in trouble since we might need the source address for further
477 processing of the error. */
480 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
484 tproto = ACCESS_ONCE(t->parms.proto);
485 if (tproto != ipproto && tproto != 0)
492 struct ipv6_tlv_tnl_enc_lim *tel;
494 case ICMPV6_DEST_UNREACH:
495 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
499 case ICMPV6_TIME_EXCEED:
500 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
501 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
506 case ICMPV6_PARAMPROB:
508 if ((*code) == ICMPV6_HDR_FIELD)
509 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
511 if (teli && teli == *info - 2) {
512 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
513 if (tel->encap_limit == 0) {
514 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
519 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
523 case ICMPV6_PKT_TOOBIG:
524 mtu = *info - offset;
525 if (mtu < IPV6_MIN_MTU)
529 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
531 rel_type = ICMPV6_PKT_TOOBIG;
550 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
551 u8 type, u8 code, int offset, __be32 info)
556 __u32 rel_info = ntohl(info);
558 struct sk_buff *skb2;
559 const struct iphdr *eiph;
563 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
564 &rel_msg, &rel_info, offset);
572 case ICMPV6_DEST_UNREACH:
573 if (rel_code != ICMPV6_ADDR_UNREACH)
575 rel_type = ICMP_DEST_UNREACH;
576 rel_code = ICMP_HOST_UNREACH;
578 case ICMPV6_PKT_TOOBIG:
581 rel_type = ICMP_DEST_UNREACH;
582 rel_code = ICMP_FRAG_NEEDED;
585 rel_type = ICMP_REDIRECT;
586 rel_code = ICMP_REDIR_HOST;
591 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
594 skb2 = skb_clone(skb, GFP_ATOMIC);
600 skb_pull(skb2, offset);
601 skb_reset_network_header(skb2);
604 /* Try to guess incoming interface */
605 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
608 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
612 skb2->dev = rt->dst.dev;
614 /* route "incoming" packet */
615 if (rt->rt_flags & RTCF_LOCAL) {
618 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
619 eiph->daddr, eiph->saddr,
622 RT_TOS(eiph->tos), 0);
624 rt->dst.dev->type != ARPHRD_TUNNEL6) {
629 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(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
645 if (rel_type == ICMP_REDIRECT)
646 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
648 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
656 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
657 u8 type, u8 code, int offset, __be32 info)
662 __u32 rel_info = ntohl(info);
665 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
666 &rel_msg, &rel_info, offset);
670 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
672 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
678 skb_pull(skb2, offset);
679 skb_reset_network_header(skb2);
681 /* Try to guess incoming interface */
682 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
685 if (rt && rt->dst.dev)
686 skb2->dev = rt->dst.dev;
688 icmpv6_send(skb2, rel_type, rel_code, rel_info);
698 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
699 const struct ipv6hdr *ipv6h,
702 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
704 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
705 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
707 return IP6_ECN_decapsulate(ipv6h, skb);
710 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
711 const struct ipv6hdr *ipv6h,
714 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
715 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
717 return IP6_ECN_decapsulate(ipv6h, skb);
720 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
721 const struct in6_addr *laddr,
722 const struct in6_addr *raddr)
724 struct __ip6_tnl_parm *p = &t->parms;
725 int ltype = ipv6_addr_type(laddr);
726 int rtype = ipv6_addr_type(raddr);
729 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
730 flags = IP6_TNL_F_CAP_PER_PACKET;
731 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
732 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
733 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
734 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
735 if (ltype&IPV6_ADDR_UNICAST)
736 flags |= IP6_TNL_F_CAP_XMIT;
737 if (rtype&IPV6_ADDR_UNICAST)
738 flags |= IP6_TNL_F_CAP_RCV;
742 EXPORT_SYMBOL(ip6_tnl_get_cap);
744 /* called with rcu_read_lock() */
745 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
746 const struct in6_addr *laddr,
747 const struct in6_addr *raddr)
749 struct __ip6_tnl_parm *p = &t->parms;
751 struct net *net = t->net;
753 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
754 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
755 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
756 struct net_device *ldev = NULL;
759 ldev = dev_get_by_index_rcu(net, p->link);
761 if ((ipv6_addr_is_multicast(laddr) ||
762 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
763 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
768 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
771 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
772 * @skb: received socket buffer
773 * @protocol: ethernet protocol ID
774 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
779 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
781 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
782 const struct ipv6hdr *ipv6h,
783 struct sk_buff *skb))
786 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
791 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
793 struct pcpu_sw_netstats *tstats;
795 tproto = ACCESS_ONCE(t->parms.proto);
796 if (tproto != ipproto && tproto != 0) {
801 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
806 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
807 t->dev->stats.rx_dropped++;
811 skb->mac_header = skb->network_header;
812 skb_reset_network_header(skb);
813 skb->protocol = htons(protocol);
814 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
816 __skb_tunnel_rx(skb, t->dev, t->net);
818 err = dscp_ecn_decapsulate(t, ipv6h, skb);
821 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
823 ipv6_get_dsfield(ipv6h));
825 ++t->dev->stats.rx_frame_errors;
826 ++t->dev->stats.rx_errors;
832 tstats = this_cpu_ptr(t->dev->tstats);
833 u64_stats_update_begin(&tstats->syncp);
834 tstats->rx_packets++;
835 tstats->rx_bytes += skb->len;
836 u64_stats_update_end(&tstats->syncp);
851 static int ip4ip6_rcv(struct sk_buff *skb)
853 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
854 ip4ip6_dscp_ecn_decapsulate);
857 static int ip6ip6_rcv(struct sk_buff *skb)
859 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
860 ip6ip6_dscp_ecn_decapsulate);
863 struct ipv6_tel_txoption {
864 struct ipv6_txoptions ops;
868 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
870 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
872 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
874 opt->dst_opt[4] = encap_limit;
875 opt->dst_opt[5] = IPV6_TLV_PADN;
878 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
879 opt->ops.opt_nflen = 8;
883 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
884 * @t: the outgoing tunnel device
885 * @hdr: IPv6 header from the incoming packet
888 * Avoid trivial tunneling loop by checking that tunnel exit-point
889 * doesn't match source of incoming packet.
897 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
899 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
902 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
903 const struct in6_addr *laddr,
904 const struct in6_addr *raddr)
906 struct __ip6_tnl_parm *p = &t->parms;
908 struct net *net = t->net;
910 if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
911 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
912 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
913 struct net_device *ldev = NULL;
917 ldev = dev_get_by_index_rcu(net, p->link);
919 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
920 pr_warn("%s xmit: Local address not yet configured!\n",
922 else if (!ipv6_addr_is_multicast(raddr) &&
923 unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
924 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
932 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
935 * ip6_tnl_xmit2 - encapsulate packet and send
936 * @skb: the outgoing socket buffer
937 * @dev: the outgoing tunnel device
938 * @dsfield: dscp code for outer header
939 * @fl: flow of tunneled packet
940 * @encap_limit: encapsulation limit
941 * @pmtu: Path MTU is stored if packet is too big
944 * Build new header and do some sanity checks on the packet before sending
950 * %-EMSGSIZE message too big. return mtu in this case.
953 static int ip6_tnl_xmit2(struct sk_buff *skb,
954 struct net_device *dev,
960 struct ip6_tnl *t = netdev_priv(dev);
961 struct net *net = t->net;
962 struct net_device_stats *stats = &t->dev->stats;
963 struct ipv6hdr *ipv6h;
964 struct ipv6_tel_txoption opt;
965 struct dst_entry *dst = NULL, *ndst = NULL;
966 struct net_device *tdev;
968 unsigned int max_headroom = sizeof(struct ipv6hdr);
973 if (ipv6_addr_any(&t->parms.raddr)) {
974 if (skb->protocol == htons(ETH_P_IPV6)) {
975 struct in6_addr *addr6;
976 struct neighbour *neigh;
980 goto tx_err_link_failure;
982 neigh = dst_neigh_lookup(skb_dst(skb),
983 &ipv6_hdr(skb)->daddr);
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);
996 } else if (!fl6->flowi6_mark)
997 dst = dst_cache_get(&t->dst_cache);
999 if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1000 goto tx_err_link_failure;
1003 dst = ip6_route_output(net, NULL, fl6);
1006 goto tx_err_link_failure;
1007 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1011 goto tx_err_link_failure;
1019 stats->collisions++;
1020 net_warn_ratelimited("%s: Local routing loop detected!\n",
1022 goto tx_err_dst_release;
1024 mtu = dst_mtu(dst) - sizeof(*ipv6h);
1025 if (encap_limit >= 0) {
1029 if (mtu < IPV6_MIN_MTU)
1032 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1033 if (skb->len > mtu) {
1036 goto tx_err_dst_release;
1039 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1042 * Okay, now see if we can stuff it in the buffer as-is.
1044 max_headroom += LL_RESERVED_SPACE(tdev);
1046 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1047 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1048 struct sk_buff *new_skb;
1050 new_skb = skb_realloc_headroom(skb, max_headroom);
1052 goto tx_err_dst_release;
1055 skb_set_owner_w(new_skb, skb->sk);
1060 if (!fl6->flowi6_mark && ndst)
1061 dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
1062 skb_dst_set(skb, dst);
1064 skb->transport_header = skb->network_header;
1066 proto = fl6->flowi6_proto;
1067 if (encap_limit >= 0) {
1068 init_tel_txopt(&opt, encap_limit);
1069 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1072 if (likely(!skb->encapsulation)) {
1073 skb_reset_inner_headers(skb);
1074 skb->encapsulation = 1;
1077 skb_push(skb, sizeof(struct ipv6hdr));
1078 skb_reset_network_header(skb);
1079 ipv6h = ipv6_hdr(skb);
1080 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1081 ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1082 ipv6h->hop_limit = t->parms.hop_limit;
1083 ipv6h->nexthdr = proto;
1084 ipv6h->saddr = fl6->saddr;
1085 ipv6h->daddr = fl6->daddr;
1086 ip6tunnel_xmit(NULL, skb, dev);
1088 tx_err_link_failure:
1089 stats->tx_carrier_errors++;
1090 dst_link_failure(skb);
1097 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1099 struct ip6_tnl *t = netdev_priv(dev);
1100 const struct iphdr *iph;
1101 int encap_limit = -1;
1108 /* ensure we can access the full inner ip header */
1109 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1113 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1115 tproto = ACCESS_ONCE(t->parms.proto);
1116 if (tproto != IPPROTO_IPIP && tproto != 0)
1119 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1120 encap_limit = t->parms.encap_limit;
1122 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1123 fl6.flowi6_proto = IPPROTO_IPIP;
1125 dsfield = ipv4_get_dsfield(iph);
1127 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1128 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1130 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1131 fl6.flowi6_mark = skb->mark;
1133 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1135 /* XXX: send ICMP error even if DF is not set. */
1136 if (err == -EMSGSIZE)
1137 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1146 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1148 struct ip6_tnl *t = netdev_priv(dev);
1149 struct ipv6hdr *ipv6h;
1150 int encap_limit = -1;
1158 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
1161 ipv6h = ipv6_hdr(skb);
1162 tproto = ACCESS_ONCE(t->parms.proto);
1163 if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1164 ip6_tnl_addr_conflict(t, ipv6h))
1167 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1169 struct ipv6_tlv_tnl_enc_lim *tel;
1170 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1171 if (tel->encap_limit == 0) {
1172 icmpv6_send(skb, ICMPV6_PARAMPROB,
1173 ICMPV6_HDR_FIELD, offset + 2);
1176 encap_limit = tel->encap_limit - 1;
1177 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1178 encap_limit = t->parms.encap_limit;
1180 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1181 fl6.flowi6_proto = IPPROTO_IPV6;
1183 dsfield = ipv6_get_dsfield(ipv6h);
1184 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1185 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1186 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1187 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1188 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1189 fl6.flowi6_mark = skb->mark;
1191 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1193 if (err == -EMSGSIZE)
1194 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1202 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1204 struct ip6_tnl *t = netdev_priv(dev);
1205 struct net_device_stats *stats = &t->dev->stats;
1208 switch (skb->protocol) {
1209 case htons(ETH_P_IP):
1210 ret = ip4ip6_tnl_xmit(skb, dev);
1212 case htons(ETH_P_IPV6):
1213 ret = ip6ip6_tnl_xmit(skb, dev);
1222 return NETDEV_TX_OK;
1226 stats->tx_dropped++;
1228 return NETDEV_TX_OK;
1231 static void ip6_tnl_link_config(struct ip6_tnl *t)
1233 struct net_device *dev = t->dev;
1234 struct __ip6_tnl_parm *p = &t->parms;
1235 struct flowi6 *fl6 = &t->fl.u.ip6;
1237 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1238 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1240 /* Set up flowi template */
1241 fl6->saddr = p->laddr;
1242 fl6->daddr = p->raddr;
1243 fl6->flowi6_oif = p->link;
1246 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1247 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1248 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1249 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1251 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1252 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1254 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1255 dev->flags |= IFF_POINTOPOINT;
1257 dev->flags &= ~IFF_POINTOPOINT;
1259 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1260 int strict = (ipv6_addr_type(&p->raddr) &
1261 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1263 struct rt6_info *rt = rt6_lookup(t->net,
1264 &p->raddr, &p->laddr,
1271 dev->hard_header_len = rt->dst.dev->hard_header_len +
1272 sizeof(struct ipv6hdr);
1274 dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1275 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1278 if (dev->mtu < IPV6_MIN_MTU)
1279 dev->mtu = IPV6_MIN_MTU;
1286 * ip6_tnl_change - update the tunnel parameters
1287 * @t: tunnel to be changed
1288 * @p: tunnel configuration parameters
1291 * ip6_tnl_change() updates the tunnel parameters
1295 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1297 t->parms.laddr = p->laddr;
1298 t->parms.raddr = p->raddr;
1299 t->parms.flags = p->flags;
1300 t->parms.hop_limit = p->hop_limit;
1301 t->parms.encap_limit = p->encap_limit;
1302 t->parms.flowinfo = p->flowinfo;
1303 t->parms.link = p->link;
1304 t->parms.proto = p->proto;
1305 dst_cache_reset(&t->dst_cache);
1306 ip6_tnl_link_config(t);
1310 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1312 struct net *net = t->net;
1313 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1316 ip6_tnl_unlink(ip6n, t);
1318 err = ip6_tnl_change(t, p);
1319 ip6_tnl_link(ip6n, t);
1320 netdev_state_change(t->dev);
1324 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1326 /* for default tnl0 device allow to change only the proto */
1327 t->parms.proto = p->proto;
1328 netdev_state_change(t->dev);
1333 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1335 p->laddr = u->laddr;
1336 p->raddr = u->raddr;
1337 p->flags = u->flags;
1338 p->hop_limit = u->hop_limit;
1339 p->encap_limit = u->encap_limit;
1340 p->flowinfo = u->flowinfo;
1342 p->proto = u->proto;
1343 memcpy(p->name, u->name, sizeof(u->name));
1347 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1349 u->laddr = p->laddr;
1350 u->raddr = p->raddr;
1351 u->flags = p->flags;
1352 u->hop_limit = p->hop_limit;
1353 u->encap_limit = p->encap_limit;
1354 u->flowinfo = p->flowinfo;
1356 u->proto = p->proto;
1357 memcpy(u->name, p->name, sizeof(u->name));
1361 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1362 * @dev: virtual device associated with tunnel
1363 * @ifr: parameters passed from userspace
1364 * @cmd: command to be performed
1367 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1370 * The possible commands are the following:
1371 * %SIOCGETTUNNEL: get tunnel parameters for device
1372 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1373 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1374 * %SIOCDELTUNNEL: delete tunnel
1376 * The fallback device "ip6tnl0", created during module
1377 * initialization, can be used for creating other tunnel devices.
1381 * %-EFAULT if unable to copy data to or from userspace,
1382 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1383 * %-EINVAL if passed tunnel parameters are invalid,
1384 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1385 * %-ENODEV if attempting to change or delete a nonexisting device
1389 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1392 struct ip6_tnl_parm p;
1393 struct __ip6_tnl_parm p1;
1394 struct ip6_tnl *t = netdev_priv(dev);
1395 struct net *net = t->net;
1396 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1400 if (dev == ip6n->fb_tnl_dev) {
1401 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1405 ip6_tnl_parm_from_user(&p1, &p);
1406 t = ip6_tnl_locate(net, &p1, 0);
1408 t = netdev_priv(dev);
1410 memset(&p, 0, sizeof(p));
1412 ip6_tnl_parm_to_user(&p, &t->parms);
1413 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1420 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1423 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1426 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1429 ip6_tnl_parm_from_user(&p1, &p);
1430 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1431 if (cmd == SIOCCHGTUNNEL) {
1433 if (t->dev != dev) {
1438 t = netdev_priv(dev);
1439 if (dev == ip6n->fb_tnl_dev)
1440 err = ip6_tnl0_update(t, &p1);
1442 err = ip6_tnl_update(t, &p1);
1446 ip6_tnl_parm_to_user(&p, &t->parms);
1447 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1456 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1459 if (dev == ip6n->fb_tnl_dev) {
1461 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1464 ip6_tnl_parm_from_user(&p1, &p);
1465 t = ip6_tnl_locate(net, &p1, 0);
1469 if (t->dev == ip6n->fb_tnl_dev)
1474 unregister_netdevice(dev);
1483 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1484 * @dev: virtual device associated with tunnel
1485 * @new_mtu: the new mtu
1489 * %-EINVAL if mtu too small
1493 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1495 struct ip6_tnl *tnl = netdev_priv(dev);
1497 if (tnl->parms.proto == IPPROTO_IPIP) {
1501 if (new_mtu < IPV6_MIN_MTU)
1504 if (new_mtu > 0xFFF8 - dev->hard_header_len)
1510 int ip6_tnl_get_iflink(const struct net_device *dev)
1512 struct ip6_tnl *t = netdev_priv(dev);
1514 return t->parms.link;
1516 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1518 static const struct net_device_ops ip6_tnl_netdev_ops = {
1519 .ndo_init = ip6_tnl_dev_init,
1520 .ndo_uninit = ip6_tnl_dev_uninit,
1521 .ndo_start_xmit = ip6_tnl_xmit,
1522 .ndo_do_ioctl = ip6_tnl_ioctl,
1523 .ndo_change_mtu = ip6_tnl_change_mtu,
1524 .ndo_get_stats = ip6_get_stats,
1525 .ndo_get_iflink = ip6_tnl_get_iflink,
1530 * ip6_tnl_dev_setup - setup virtual tunnel device
1531 * @dev: virtual device associated with tunnel
1534 * Initialize function pointers and device parameters
1537 static void ip6_tnl_dev_setup(struct net_device *dev)
1541 dev->netdev_ops = &ip6_tnl_netdev_ops;
1542 dev->destructor = ip6_dev_free;
1544 dev->type = ARPHRD_TUNNEL6;
1545 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1546 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1547 t = netdev_priv(dev);
1548 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1550 dev->flags |= IFF_NOARP;
1551 dev->addr_len = sizeof(struct in6_addr);
1552 netif_keep_dst(dev);
1553 /* This perm addr will be used as interface identifier by IPv6 */
1554 dev->addr_assign_type = NET_ADDR_RANDOM;
1555 eth_random_addr(dev->perm_addr);
1560 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1561 * @dev: virtual device associated with tunnel
1565 ip6_tnl_dev_init_gen(struct net_device *dev)
1567 struct ip6_tnl *t = netdev_priv(dev);
1571 t->net = dev_net(dev);
1572 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1576 ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
1578 free_percpu(dev->tstats);
1588 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1589 * @dev: virtual device associated with tunnel
1592 static int ip6_tnl_dev_init(struct net_device *dev)
1594 struct ip6_tnl *t = netdev_priv(dev);
1595 int err = ip6_tnl_dev_init_gen(dev);
1599 ip6_tnl_link_config(t);
1604 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1605 * @dev: fallback device
1610 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1612 struct ip6_tnl *t = netdev_priv(dev);
1613 struct net *net = dev_net(dev);
1614 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1616 t->parms.proto = IPPROTO_IPV6;
1618 rcu_assign_pointer(ip6n->tnls_wc[0], t);
1622 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1626 if (!data || !data[IFLA_IPTUN_PROTO])
1629 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1630 if (proto != IPPROTO_IPV6 &&
1631 proto != IPPROTO_IPIP &&
1638 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1639 struct __ip6_tnl_parm *parms)
1641 memset(parms, 0, sizeof(*parms));
1646 if (data[IFLA_IPTUN_LINK])
1647 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1649 if (data[IFLA_IPTUN_LOCAL])
1650 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1652 if (data[IFLA_IPTUN_REMOTE])
1653 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1655 if (data[IFLA_IPTUN_TTL])
1656 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1658 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1659 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1661 if (data[IFLA_IPTUN_FLOWINFO])
1662 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1664 if (data[IFLA_IPTUN_FLAGS])
1665 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1667 if (data[IFLA_IPTUN_PROTO])
1668 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1671 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1672 struct nlattr *tb[], struct nlattr *data[])
1674 struct net *net = dev_net(dev);
1675 struct ip6_tnl *nt, *t;
1677 nt = netdev_priv(dev);
1678 ip6_tnl_netlink_parms(data, &nt->parms);
1680 t = ip6_tnl_locate(net, &nt->parms, 0);
1684 return ip6_tnl_create2(dev);
1687 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1688 struct nlattr *data[])
1690 struct ip6_tnl *t = netdev_priv(dev);
1691 struct __ip6_tnl_parm p;
1692 struct net *net = t->net;
1693 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1695 if (dev == ip6n->fb_tnl_dev)
1698 ip6_tnl_netlink_parms(data, &p);
1700 t = ip6_tnl_locate(net, &p, 0);
1705 t = netdev_priv(dev);
1707 return ip6_tnl_update(t, &p);
1710 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1712 struct net *net = dev_net(dev);
1713 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1715 if (dev != ip6n->fb_tnl_dev)
1716 unregister_netdevice_queue(dev, head);
1719 static size_t ip6_tnl_get_size(const struct net_device *dev)
1722 /* IFLA_IPTUN_LINK */
1724 /* IFLA_IPTUN_LOCAL */
1725 nla_total_size(sizeof(struct in6_addr)) +
1726 /* IFLA_IPTUN_REMOTE */
1727 nla_total_size(sizeof(struct in6_addr)) +
1728 /* IFLA_IPTUN_TTL */
1730 /* IFLA_IPTUN_ENCAP_LIMIT */
1732 /* IFLA_IPTUN_FLOWINFO */
1734 /* IFLA_IPTUN_FLAGS */
1736 /* IFLA_IPTUN_PROTO */
1741 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1743 struct ip6_tnl *tunnel = netdev_priv(dev);
1744 struct __ip6_tnl_parm *parm = &tunnel->parms;
1746 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1747 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1748 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1749 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1750 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1751 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1752 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1753 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1754 goto nla_put_failure;
1761 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1763 struct ip6_tnl *tunnel = netdev_priv(dev);
1767 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1769 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1770 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1771 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1772 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1773 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1774 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1775 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1776 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1777 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1780 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1782 .maxtype = IFLA_IPTUN_MAX,
1783 .policy = ip6_tnl_policy,
1784 .priv_size = sizeof(struct ip6_tnl),
1785 .setup = ip6_tnl_dev_setup,
1786 .validate = ip6_tnl_validate,
1787 .newlink = ip6_tnl_newlink,
1788 .changelink = ip6_tnl_changelink,
1789 .dellink = ip6_tnl_dellink,
1790 .get_size = ip6_tnl_get_size,
1791 .fill_info = ip6_tnl_fill_info,
1792 .get_link_net = ip6_tnl_get_link_net,
1795 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1796 .handler = ip4ip6_rcv,
1797 .err_handler = ip4ip6_err,
1801 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1802 .handler = ip6ip6_rcv,
1803 .err_handler = ip6ip6_err,
1807 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1809 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1810 struct net_device *dev, *aux;
1815 for_each_netdev_safe(net, dev, aux)
1816 if (dev->rtnl_link_ops == &ip6_link_ops)
1817 unregister_netdevice_queue(dev, &list);
1819 for (h = 0; h < HASH_SIZE; h++) {
1820 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1822 /* If dev is in the same netns, it has already
1823 * been added to the list by the previous loop.
1825 if (!net_eq(dev_net(t->dev), net))
1826 unregister_netdevice_queue(t->dev, &list);
1827 t = rtnl_dereference(t->next);
1831 unregister_netdevice_many(&list);
1834 static int __net_init ip6_tnl_init_net(struct net *net)
1836 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1837 struct ip6_tnl *t = NULL;
1840 ip6n->tnls[0] = ip6n->tnls_wc;
1841 ip6n->tnls[1] = ip6n->tnls_r_l;
1844 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1845 NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1847 if (!ip6n->fb_tnl_dev)
1849 dev_net_set(ip6n->fb_tnl_dev, net);
1850 ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1851 /* FB netdevice is special: we have one, and only one per netns.
1852 * Allowing to move it to another netns is clearly unsafe.
1854 ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1856 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1860 err = register_netdev(ip6n->fb_tnl_dev);
1864 t = netdev_priv(ip6n->fb_tnl_dev);
1866 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1870 ip6_dev_free(ip6n->fb_tnl_dev);
1875 static void __net_exit ip6_tnl_exit_net(struct net *net)
1878 ip6_tnl_destroy_tunnels(net);
1882 static struct pernet_operations ip6_tnl_net_ops = {
1883 .init = ip6_tnl_init_net,
1884 .exit = ip6_tnl_exit_net,
1885 .id = &ip6_tnl_net_id,
1886 .size = sizeof(struct ip6_tnl_net),
1890 * ip6_tunnel_init - register protocol and reserve needed resources
1892 * Return: 0 on success
1895 static int __init ip6_tunnel_init(void)
1899 err = register_pernet_device(&ip6_tnl_net_ops);
1903 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1905 pr_err("%s: can't register ip4ip6\n", __func__);
1909 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1911 pr_err("%s: can't register ip6ip6\n", __func__);
1914 err = rtnl_link_register(&ip6_link_ops);
1916 goto rtnl_link_failed;
1921 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1923 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1925 unregister_pernet_device(&ip6_tnl_net_ops);
1931 * ip6_tunnel_cleanup - free resources and unregister protocol
1934 static void __exit ip6_tunnel_cleanup(void)
1936 rtnl_link_unregister(&ip6_link_ops);
1937 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1938 pr_info("%s: can't deregister ip4ip6\n", __func__);
1940 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1941 pr_info("%s: can't deregister ip6ip6\n", __func__);
1943 unregister_pernet_device(&ip6_tnl_net_ops);
1946 module_init(ip6_tunnel_init);
1947 module_exit(ip6_tunnel_cleanup);