Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / net / ipv6 / ip6_tunnel.c
1 /*
2  *      IPv6 tunneling device
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Ville Nuorvala          <vnuorval@tcs.hut.fi>
7  *      Yasuyuki Kozakai        <kozakai@linux-ipv6.org>
8  *
9  *      Based on:
10  *      linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
11  *
12  *      RFC 2473
13  *
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.
18  *
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.h>
29 #include <linux/if.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
41 #include <linux/slab.h>
42 #include <linux/hash.h>
43 #include <linux/etherdevice.h>
44
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
47
48 #include <net/icmp.h>
49 #include <net/ip.h>
50 #include <net/ip_tunnels.h>
51 #include <net/ipv6.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.h>
55 #include <net/xfrm.h>
56 #include <net/dsfield.h>
57 #include <net/inet_ecn.h>
58 #include <net/net_namespace.h>
59 #include <net/netns/generic.h>
60
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");
66
67 #define HASH_SIZE_SHIFT  5
68 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
69
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");
73
74 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
75 {
76         u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
77
78         return hash_32(hash, HASH_SIZE_SHIFT);
79 }
80
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;
84
85 static int ip6_tnl_net_id __read_mostly;
86 struct ip6_tnl_net {
87         /* the IPv6 tunnel fallback device */
88         struct net_device *fb_tnl_dev;
89         /* lists for storing tunnels in use */
90         struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
91         struct ip6_tnl __rcu *tnls_wc[1];
92         struct ip6_tnl __rcu **tnls[2];
93 };
94
95 static struct net_device_stats *ip6_get_stats(struct net_device *dev)
96 {
97         struct pcpu_sw_netstats tmp, sum = { 0 };
98         int i;
99
100         for_each_possible_cpu(i) {
101                 unsigned int start;
102                 const struct pcpu_sw_netstats *tstats =
103                                                    per_cpu_ptr(dev->tstats, i);
104
105                 do {
106                         start = u64_stats_fetch_begin_irq(&tstats->syncp);
107                         tmp.rx_packets = tstats->rx_packets;
108                         tmp.rx_bytes = tstats->rx_bytes;
109                         tmp.tx_packets = tstats->tx_packets;
110                         tmp.tx_bytes =  tstats->tx_bytes;
111                 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
112
113                 sum.rx_packets += tmp.rx_packets;
114                 sum.rx_bytes   += tmp.rx_bytes;
115                 sum.tx_packets += tmp.tx_packets;
116                 sum.tx_bytes   += tmp.tx_bytes;
117         }
118         dev->stats.rx_packets = sum.rx_packets;
119         dev->stats.rx_bytes   = sum.rx_bytes;
120         dev->stats.tx_packets = sum.tx_packets;
121         dev->stats.tx_bytes   = sum.tx_bytes;
122         return &dev->stats;
123 }
124
125 /*
126  * Locking : hash tables are protected by RCU and RTNL
127  */
128
129 static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst,
130                                     struct dst_entry *dst)
131 {
132         write_seqlock_bh(&idst->lock);
133         dst_release(rcu_dereference_protected(
134                             idst->dst,
135                             lockdep_is_held(&idst->lock.lock)));
136         if (dst) {
137                 dst_hold(dst);
138                 idst->cookie = rt6_get_cookie((struct rt6_info *)dst);
139         } else {
140                 idst->cookie = 0;
141         }
142         rcu_assign_pointer(idst->dst, dst);
143         write_sequnlock_bh(&idst->lock);
144 }
145
146 struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t)
147 {
148         struct ip6_tnl_dst *idst;
149         struct dst_entry *dst;
150         unsigned int seq;
151         u32 cookie;
152
153         idst = raw_cpu_ptr(t->dst_cache);
154
155         rcu_read_lock();
156         do {
157                 seq = read_seqbegin(&idst->lock);
158                 dst = rcu_dereference(idst->dst);
159                 cookie = idst->cookie;
160         } while (read_seqretry(&idst->lock, seq));
161
162         if (dst && !atomic_inc_not_zero(&dst->__refcnt))
163                 dst = NULL;
164         rcu_read_unlock();
165
166         if (dst && dst->obsolete && !dst->ops->check(dst, cookie)) {
167                 ip6_tnl_per_cpu_dst_set(idst, NULL);
168                 dst_release(dst);
169                 dst = NULL;
170         }
171         return dst;
172 }
173 EXPORT_SYMBOL_GPL(ip6_tnl_dst_get);
174
175 void ip6_tnl_dst_reset(struct ip6_tnl *t)
176 {
177         int i;
178
179         for_each_possible_cpu(i)
180                 ip6_tnl_per_cpu_dst_set(per_cpu_ptr(t->dst_cache, i), NULL);
181 }
182 EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
183
184 void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst)
185 {
186         ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst);
187
188 }
189 EXPORT_SYMBOL_GPL(ip6_tnl_dst_set);
190
191 void ip6_tnl_dst_destroy(struct ip6_tnl *t)
192 {
193         if (!t->dst_cache)
194                 return;
195
196         ip6_tnl_dst_reset(t);
197         free_percpu(t->dst_cache);
198 }
199 EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy);
200
201 int ip6_tnl_dst_init(struct ip6_tnl *t)
202 {
203         int i;
204
205         t->dst_cache = alloc_percpu(struct ip6_tnl_dst);
206         if (!t->dst_cache)
207                 return -ENOMEM;
208
209         for_each_possible_cpu(i)
210                 seqlock_init(&per_cpu_ptr(t->dst_cache, i)->lock);
211
212         return 0;
213 }
214 EXPORT_SYMBOL_GPL(ip6_tnl_dst_init);
215
216 /**
217  * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
218  *   @remote: the address of the tunnel exit-point
219  *   @local: the address of the tunnel entry-point
220  *
221  * Return:
222  *   tunnel matching given end-points if found,
223  *   else fallback tunnel if its device is up,
224  *   else %NULL
225  **/
226
227 #define for_each_ip6_tunnel_rcu(start) \
228         for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
229
230 static struct ip6_tnl *
231 ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
232 {
233         unsigned int hash = HASH(remote, local);
234         struct ip6_tnl *t;
235         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
236         struct in6_addr any;
237
238         for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
239                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
240                     ipv6_addr_equal(remote, &t->parms.raddr) &&
241                     (t->dev->flags & IFF_UP))
242                         return t;
243         }
244
245         memset(&any, 0, sizeof(any));
246         hash = HASH(&any, local);
247         for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
248                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
249                     ipv6_addr_any(&t->parms.raddr) &&
250                     (t->dev->flags & IFF_UP))
251                         return t;
252         }
253
254         hash = HASH(remote, &any);
255         for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
256                 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
257                     ipv6_addr_any(&t->parms.laddr) &&
258                     (t->dev->flags & IFF_UP))
259                         return t;
260         }
261
262         t = rcu_dereference(ip6n->tnls_wc[0]);
263         if (t && (t->dev->flags & IFF_UP))
264                 return t;
265
266         return NULL;
267 }
268
269 /**
270  * ip6_tnl_bucket - get head of list matching given tunnel parameters
271  *   @p: parameters containing tunnel end-points
272  *
273  * Description:
274  *   ip6_tnl_bucket() returns the head of the list matching the
275  *   &struct in6_addr entries laddr and raddr in @p.
276  *
277  * Return: head of IPv6 tunnel list
278  **/
279
280 static struct ip6_tnl __rcu **
281 ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
282 {
283         const struct in6_addr *remote = &p->raddr;
284         const struct in6_addr *local = &p->laddr;
285         unsigned int h = 0;
286         int prio = 0;
287
288         if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
289                 prio = 1;
290                 h = HASH(remote, local);
291         }
292         return &ip6n->tnls[prio][h];
293 }
294
295 /**
296  * ip6_tnl_link - add tunnel to hash table
297  *   @t: tunnel to be added
298  **/
299
300 static void
301 ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
302 {
303         struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
304
305         rcu_assign_pointer(t->next , rtnl_dereference(*tp));
306         rcu_assign_pointer(*tp, t);
307 }
308
309 /**
310  * ip6_tnl_unlink - remove tunnel from hash table
311  *   @t: tunnel to be removed
312  **/
313
314 static void
315 ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
316 {
317         struct ip6_tnl __rcu **tp;
318         struct ip6_tnl *iter;
319
320         for (tp = ip6_tnl_bucket(ip6n, &t->parms);
321              (iter = rtnl_dereference(*tp)) != NULL;
322              tp = &iter->next) {
323                 if (t == iter) {
324                         rcu_assign_pointer(*tp, t->next);
325                         break;
326                 }
327         }
328 }
329
330 static void ip6_dev_free(struct net_device *dev)
331 {
332         struct ip6_tnl *t = netdev_priv(dev);
333
334         ip6_tnl_dst_destroy(t);
335         free_percpu(dev->tstats);
336         free_netdev(dev);
337 }
338
339 static int ip6_tnl_create2(struct net_device *dev)
340 {
341         struct ip6_tnl *t = netdev_priv(dev);
342         struct net *net = dev_net(dev);
343         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
344         int err;
345
346         t = netdev_priv(dev);
347
348         dev->rtnl_link_ops = &ip6_link_ops;
349         err = register_netdevice(dev);
350         if (err < 0)
351                 goto out;
352
353         strcpy(t->parms.name, dev->name);
354
355         dev_hold(dev);
356         ip6_tnl_link(ip6n, t);
357         return 0;
358
359 out:
360         return err;
361 }
362
363 /**
364  * ip6_tnl_create - create a new tunnel
365  *   @p: tunnel parameters
366  *   @pt: pointer to new tunnel
367  *
368  * Description:
369  *   Create tunnel matching given parameters.
370  *
371  * Return:
372  *   created tunnel or error pointer
373  **/
374
375 static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
376 {
377         struct net_device *dev;
378         struct ip6_tnl *t;
379         char name[IFNAMSIZ];
380         int err = -ENOMEM;
381
382         if (p->name[0])
383                 strlcpy(name, p->name, IFNAMSIZ);
384         else
385                 sprintf(name, "ip6tnl%%d");
386
387         dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
388                            ip6_tnl_dev_setup);
389         if (!dev)
390                 goto failed;
391
392         dev_net_set(dev, net);
393
394         t = netdev_priv(dev);
395         t->parms = *p;
396         t->net = dev_net(dev);
397         err = ip6_tnl_create2(dev);
398         if (err < 0)
399                 goto failed_free;
400
401         return t;
402
403 failed_free:
404         ip6_dev_free(dev);
405 failed:
406         return ERR_PTR(err);
407 }
408
409 /**
410  * ip6_tnl_locate - find or create tunnel matching given parameters
411  *   @p: tunnel parameters
412  *   @create: != 0 if allowed to create new tunnel if no match found
413  *
414  * Description:
415  *   ip6_tnl_locate() first tries to locate an existing tunnel
416  *   based on @parms. If this is unsuccessful, but @create is set a new
417  *   tunnel device is created and registered for use.
418  *
419  * Return:
420  *   matching tunnel or error pointer
421  **/
422
423 static struct ip6_tnl *ip6_tnl_locate(struct net *net,
424                 struct __ip6_tnl_parm *p, int create)
425 {
426         const struct in6_addr *remote = &p->raddr;
427         const struct in6_addr *local = &p->laddr;
428         struct ip6_tnl __rcu **tp;
429         struct ip6_tnl *t;
430         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
431
432         for (tp = ip6_tnl_bucket(ip6n, p);
433              (t = rtnl_dereference(*tp)) != NULL;
434              tp = &t->next) {
435                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
436                     ipv6_addr_equal(remote, &t->parms.raddr)) {
437                         if (create)
438                                 return ERR_PTR(-EEXIST);
439
440                         return t;
441                 }
442         }
443         if (!create)
444                 return ERR_PTR(-ENODEV);
445         return ip6_tnl_create(net, p);
446 }
447
448 /**
449  * ip6_tnl_dev_uninit - tunnel device uninitializer
450  *   @dev: the device to be destroyed
451  *
452  * Description:
453  *   ip6_tnl_dev_uninit() removes tunnel from its list
454  **/
455
456 static void
457 ip6_tnl_dev_uninit(struct net_device *dev)
458 {
459         struct ip6_tnl *t = netdev_priv(dev);
460         struct net *net = t->net;
461         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
462
463         if (dev == ip6n->fb_tnl_dev)
464                 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
465         else
466                 ip6_tnl_unlink(ip6n, t);
467         ip6_tnl_dst_reset(t);
468         dev_put(dev);
469 }
470
471 /**
472  * parse_tvl_tnl_enc_lim - handle encapsulation limit option
473  *   @skb: received socket buffer
474  *
475  * Return:
476  *   0 if none was found,
477  *   else index to encapsulation limit
478  **/
479
480 __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
481 {
482         const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
483         unsigned int nhoff = raw - skb->data;
484         unsigned int off = nhoff + sizeof(*ipv6h);
485         u8 next, nexthdr = ipv6h->nexthdr;
486
487         while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
488                 struct ipv6_opt_hdr *hdr;
489                 u16 optlen;
490
491                 if (!pskb_may_pull(skb, off + sizeof(*hdr)))
492                         break;
493
494                 hdr = (struct ipv6_opt_hdr *)(skb->data + off);
495                 if (nexthdr == NEXTHDR_FRAGMENT) {
496                         struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
497                         if (frag_hdr->frag_off)
498                                 break;
499                         optlen = 8;
500                 } else if (nexthdr == NEXTHDR_AUTH) {
501                         optlen = (hdr->hdrlen + 2) << 2;
502                 } else {
503                         optlen = ipv6_optlen(hdr);
504                 }
505                 /* cache hdr->nexthdr, since pskb_may_pull() might
506                  * invalidate hdr
507                  */
508                 next = hdr->nexthdr;
509                 if (nexthdr == NEXTHDR_DEST) {
510                         u16 i = 2;
511
512                         /* Remember : hdr is no longer valid at this point. */
513                         if (!pskb_may_pull(skb, off + optlen))
514                                 break;
515
516                         while (1) {
517                                 struct ipv6_tlv_tnl_enc_lim *tel;
518
519                                 /* No more room for encapsulation limit */
520                                 if (i + sizeof(*tel) > optlen)
521                                         break;
522
523                                 tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
524                                 /* return index of option if found and valid */
525                                 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
526                                     tel->length == 1)
527                                         return i + off - nhoff;
528                                 /* else jump to next option */
529                                 if (tel->type)
530                                         i += tel->length + 2;
531                                 else
532                                         i++;
533                         }
534                 }
535                 nexthdr = next;
536                 off += optlen;
537         }
538         return 0;
539 }
540 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
541
542 /**
543  * ip6_tnl_err - tunnel error handler
544  *
545  * Description:
546  *   ip6_tnl_err() should handle errors in the tunnel according
547  *   to the specifications in RFC 2473.
548  **/
549
550 static int
551 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
552             u8 *type, u8 *code, int *msg, __u32 *info, int offset)
553 {
554         const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
555         struct ip6_tnl *t;
556         int rel_msg = 0;
557         u8 rel_type = ICMPV6_DEST_UNREACH;
558         u8 rel_code = ICMPV6_ADDR_UNREACH;
559         u8 tproto;
560         __u32 rel_info = 0;
561         __u16 len;
562         int err = -ENOENT;
563
564         /* If the packet doesn't contain the original IPv6 header we are
565            in trouble since we might need the source address for further
566            processing of the error. */
567
568         rcu_read_lock();
569         t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
570         if (!t)
571                 goto out;
572
573         tproto = ACCESS_ONCE(t->parms.proto);
574         if (tproto != ipproto && tproto != 0)
575                 goto out;
576
577         err = 0;
578
579         switch (*type) {
580                 __u32 teli;
581                 struct ipv6_tlv_tnl_enc_lim *tel;
582                 __u32 mtu;
583         case ICMPV6_DEST_UNREACH:
584                 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
585                                     t->parms.name);
586                 rel_msg = 1;
587                 break;
588         case ICMPV6_TIME_EXCEED:
589                 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
590                         net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
591                                             t->parms.name);
592                         rel_msg = 1;
593                 }
594                 break;
595         case ICMPV6_PARAMPROB:
596                 teli = 0;
597                 if ((*code) == ICMPV6_HDR_FIELD)
598                         teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
599
600                 if (teli && teli == *info - 2) {
601                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
602                         if (tel->encap_limit == 0) {
603                                 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
604                                                     t->parms.name);
605                                 rel_msg = 1;
606                         }
607                 } else {
608                         net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
609                                             t->parms.name);
610                 }
611                 break;
612         case ICMPV6_PKT_TOOBIG:
613                 mtu = *info - offset;
614                 if (mtu < IPV6_MIN_MTU)
615                         mtu = IPV6_MIN_MTU;
616                 t->dev->mtu = mtu;
617
618                 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
619                 if (len > mtu) {
620                         rel_type = ICMPV6_PKT_TOOBIG;
621                         rel_code = 0;
622                         rel_info = mtu;
623                         rel_msg = 1;
624                 }
625                 break;
626         }
627
628         *type = rel_type;
629         *code = rel_code;
630         *info = rel_info;
631         *msg = rel_msg;
632
633 out:
634         rcu_read_unlock();
635         return err;
636 }
637
638 static int
639 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
640            u8 type, u8 code, int offset, __be32 info)
641 {
642         int rel_msg = 0;
643         u8 rel_type = type;
644         u8 rel_code = code;
645         __u32 rel_info = ntohl(info);
646         int err;
647         struct sk_buff *skb2;
648         const struct iphdr *eiph;
649         struct rtable *rt;
650         struct flowi4 fl4;
651
652         err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
653                           &rel_msg, &rel_info, offset);
654         if (err < 0)
655                 return err;
656
657         if (rel_msg == 0)
658                 return 0;
659
660         switch (rel_type) {
661         case ICMPV6_DEST_UNREACH:
662                 if (rel_code != ICMPV6_ADDR_UNREACH)
663                         return 0;
664                 rel_type = ICMP_DEST_UNREACH;
665                 rel_code = ICMP_HOST_UNREACH;
666                 break;
667         case ICMPV6_PKT_TOOBIG:
668                 if (rel_code != 0)
669                         return 0;
670                 rel_type = ICMP_DEST_UNREACH;
671                 rel_code = ICMP_FRAG_NEEDED;
672                 break;
673         case NDISC_REDIRECT:
674                 rel_type = ICMP_REDIRECT;
675                 rel_code = ICMP_REDIR_HOST;
676         default:
677                 return 0;
678         }
679
680         if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
681                 return 0;
682
683         skb2 = skb_clone(skb, GFP_ATOMIC);
684         if (!skb2)
685                 return 0;
686
687         skb_dst_drop(skb2);
688
689         skb_pull(skb2, offset);
690         skb_reset_network_header(skb2);
691         eiph = ip_hdr(skb2);
692
693         /* Try to guess incoming interface */
694         rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
695                                    eiph->saddr, 0,
696                                    0, 0,
697                                    IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
698         if (IS_ERR(rt))
699                 goto out;
700
701         skb2->dev = rt->dst.dev;
702
703         /* route "incoming" packet */
704         if (rt->rt_flags & RTCF_LOCAL) {
705                 ip_rt_put(rt);
706                 rt = NULL;
707                 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
708                                            eiph->daddr, eiph->saddr,
709                                            0, 0,
710                                            IPPROTO_IPIP,
711                                            RT_TOS(eiph->tos), 0);
712                 if (IS_ERR(rt) ||
713                     rt->dst.dev->type != ARPHRD_TUNNEL) {
714                         if (!IS_ERR(rt))
715                                 ip_rt_put(rt);
716                         goto out;
717                 }
718                 skb_dst_set(skb2, &rt->dst);
719         } else {
720                 ip_rt_put(rt);
721                 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
722                                    skb2->dev) ||
723                     skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
724                         goto out;
725         }
726
727         /* change mtu on this route */
728         if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
729                 if (rel_info > dst_mtu(skb_dst(skb2)))
730                         goto out;
731
732                 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
733         }
734         if (rel_type == ICMP_REDIRECT)
735                 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
736
737         icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
738
739 out:
740         kfree_skb(skb2);
741         return 0;
742 }
743
744 static int
745 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
746            u8 type, u8 code, int offset, __be32 info)
747 {
748         int rel_msg = 0;
749         u8 rel_type = type;
750         u8 rel_code = code;
751         __u32 rel_info = ntohl(info);
752         int err;
753
754         err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
755                           &rel_msg, &rel_info, offset);
756         if (err < 0)
757                 return err;
758
759         if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
760                 struct rt6_info *rt;
761                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
762
763                 if (!skb2)
764                         return 0;
765
766                 skb_dst_drop(skb2);
767                 skb_pull(skb2, offset);
768                 skb_reset_network_header(skb2);
769
770                 /* Try to guess incoming interface */
771                 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
772                                 NULL, 0, 0);
773
774                 if (rt && rt->dst.dev)
775                         skb2->dev = rt->dst.dev;
776
777                 icmpv6_send(skb2, rel_type, rel_code, rel_info);
778
779                 ip6_rt_put(rt);
780
781                 kfree_skb(skb2);
782         }
783
784         return 0;
785 }
786
787 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
788                                        const struct ipv6hdr *ipv6h,
789                                        struct sk_buff *skb)
790 {
791         __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
792
793         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
794                 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
795
796         return IP6_ECN_decapsulate(ipv6h, skb);
797 }
798
799 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
800                                        const struct ipv6hdr *ipv6h,
801                                        struct sk_buff *skb)
802 {
803         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
804                 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
805
806         return IP6_ECN_decapsulate(ipv6h, skb);
807 }
808
809 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
810                              const struct in6_addr *laddr,
811                              const struct in6_addr *raddr)
812 {
813         struct __ip6_tnl_parm *p = &t->parms;
814         int ltype = ipv6_addr_type(laddr);
815         int rtype = ipv6_addr_type(raddr);
816         __u32 flags = 0;
817
818         if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
819                 flags = IP6_TNL_F_CAP_PER_PACKET;
820         } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
821                    rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
822                    !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
823                    (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
824                 if (ltype&IPV6_ADDR_UNICAST)
825                         flags |= IP6_TNL_F_CAP_XMIT;
826                 if (rtype&IPV6_ADDR_UNICAST)
827                         flags |= IP6_TNL_F_CAP_RCV;
828         }
829         return flags;
830 }
831 EXPORT_SYMBOL(ip6_tnl_get_cap);
832
833 /* called with rcu_read_lock() */
834 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
835                                   const struct in6_addr *laddr,
836                                   const struct in6_addr *raddr)
837 {
838         struct __ip6_tnl_parm *p = &t->parms;
839         int ret = 0;
840         struct net *net = t->net;
841
842         if ((p->flags & IP6_TNL_F_CAP_RCV) ||
843             ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
844              (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
845                 struct net_device *ldev = NULL;
846
847                 if (p->link)
848                         ldev = dev_get_by_index_rcu(net, p->link);
849
850                 if ((ipv6_addr_is_multicast(laddr) ||
851                      likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
852                     likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
853                         ret = 1;
854         }
855         return ret;
856 }
857 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
858
859 /**
860  * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
861  *   @skb: received socket buffer
862  *   @protocol: ethernet protocol ID
863  *   @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
864  *
865  * Return: 0
866  **/
867
868 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
869                        __u8 ipproto,
870                        int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
871                                                    const struct ipv6hdr *ipv6h,
872                                                    struct sk_buff *skb))
873 {
874         struct ip6_tnl *t;
875         const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
876         u8 tproto;
877         int err;
878
879         rcu_read_lock();
880         t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
881         if (t) {
882                 struct pcpu_sw_netstats *tstats;
883
884                 tproto = ACCESS_ONCE(t->parms.proto);
885                 if (tproto != ipproto && tproto != 0) {
886                         rcu_read_unlock();
887                         goto discard;
888                 }
889
890                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
891                         rcu_read_unlock();
892                         goto discard;
893                 }
894
895                 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
896                         t->dev->stats.rx_dropped++;
897                         rcu_read_unlock();
898                         goto discard;
899                 }
900                 skb->mac_header = skb->network_header;
901                 skb_reset_network_header(skb);
902                 skb->protocol = htons(protocol);
903                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
904
905                 __skb_tunnel_rx(skb, t->dev, t->net);
906
907                 err = dscp_ecn_decapsulate(t, ipv6h, skb);
908                 if (unlikely(err)) {
909                         if (log_ecn_error)
910                                 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
911                                                      &ipv6h->saddr,
912                                                      ipv6_get_dsfield(ipv6h));
913                         if (err > 1) {
914                                 ++t->dev->stats.rx_frame_errors;
915                                 ++t->dev->stats.rx_errors;
916                                 rcu_read_unlock();
917                                 goto discard;
918                         }
919                 }
920
921                 tstats = this_cpu_ptr(t->dev->tstats);
922                 u64_stats_update_begin(&tstats->syncp);
923                 tstats->rx_packets++;
924                 tstats->rx_bytes += skb->len;
925                 u64_stats_update_end(&tstats->syncp);
926
927                 netif_rx(skb);
928
929                 rcu_read_unlock();
930                 return 0;
931         }
932         rcu_read_unlock();
933         return 1;
934
935 discard:
936         kfree_skb(skb);
937         return 0;
938 }
939
940 static int ip4ip6_rcv(struct sk_buff *skb)
941 {
942         return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
943                            ip4ip6_dscp_ecn_decapsulate);
944 }
945
946 static int ip6ip6_rcv(struct sk_buff *skb)
947 {
948         return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
949                            ip6ip6_dscp_ecn_decapsulate);
950 }
951
952 struct ipv6_tel_txoption {
953         struct ipv6_txoptions ops;
954         __u8 dst_opt[8];
955 };
956
957 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
958 {
959         memset(opt, 0, sizeof(struct ipv6_tel_txoption));
960
961         opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
962         opt->dst_opt[3] = 1;
963         opt->dst_opt[4] = encap_limit;
964         opt->dst_opt[5] = IPV6_TLV_PADN;
965         opt->dst_opt[6] = 1;
966
967         opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
968         opt->ops.opt_nflen = 8;
969 }
970
971 /**
972  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
973  *   @t: the outgoing tunnel device
974  *   @hdr: IPv6 header from the incoming packet
975  *
976  * Description:
977  *   Avoid trivial tunneling loop by checking that tunnel exit-point
978  *   doesn't match source of incoming packet.
979  *
980  * Return:
981  *   1 if conflict,
982  *   0 else
983  **/
984
985 static inline bool
986 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
987 {
988         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
989 }
990
991 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
992                      const struct in6_addr *laddr,
993                      const struct in6_addr *raddr)
994 {
995         struct __ip6_tnl_parm *p = &t->parms;
996         int ret = 0;
997         struct net *net = t->net;
998
999         if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
1000             ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
1001              (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
1002                 struct net_device *ldev = NULL;
1003
1004                 rcu_read_lock();
1005                 if (p->link)
1006                         ldev = dev_get_by_index_rcu(net, p->link);
1007
1008                 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
1009                         pr_warn("%s xmit: Local address not yet configured!\n",
1010                                 p->name);
1011                 else if (!ipv6_addr_is_multicast(raddr) &&
1012                          unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
1013                         pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1014                                 p->name);
1015                 else
1016                         ret = 1;
1017                 rcu_read_unlock();
1018         }
1019         return ret;
1020 }
1021 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1022
1023 /**
1024  * ip6_tnl_xmit2 - encapsulate packet and send
1025  *   @skb: the outgoing socket buffer
1026  *   @dev: the outgoing tunnel device
1027  *   @dsfield: dscp code for outer header
1028  *   @fl: flow of tunneled packet
1029  *   @encap_limit: encapsulation limit
1030  *   @pmtu: Path MTU is stored if packet is too big
1031  *
1032  * Description:
1033  *   Build new header and do some sanity checks on the packet before sending
1034  *   it.
1035  *
1036  * Return:
1037  *   0 on success
1038  *   -1 fail
1039  *   %-EMSGSIZE message too big. return mtu in this case.
1040  **/
1041
1042 static int ip6_tnl_xmit2(struct sk_buff *skb,
1043                          struct net_device *dev,
1044                          __u8 dsfield,
1045                          struct flowi6 *fl6,
1046                          int encap_limit,
1047                          __u32 *pmtu)
1048 {
1049         struct ip6_tnl *t = netdev_priv(dev);
1050         struct net *net = t->net;
1051         struct net_device_stats *stats = &t->dev->stats;
1052         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1053         struct ipv6_tel_txoption opt;
1054         struct dst_entry *dst = NULL, *ndst = NULL;
1055         struct net_device *tdev;
1056         bool use_cache = false;
1057         int mtu;
1058         unsigned int max_headroom = sizeof(struct ipv6hdr);
1059         u8 proto;
1060         int err = -1;
1061
1062         /* NBMA tunnel */
1063         if (ipv6_addr_any(&t->parms.raddr)) {
1064                 struct in6_addr *addr6;
1065                 struct neighbour *neigh;
1066                 int addr_type;
1067
1068                 if (!skb_dst(skb))
1069                         goto tx_err_link_failure;
1070
1071                 neigh = dst_neigh_lookup(skb_dst(skb),
1072                                          &ipv6_hdr(skb)->daddr);
1073                 if (!neigh)
1074                         goto tx_err_link_failure;
1075
1076                 addr6 = (struct in6_addr *)&neigh->primary_key;
1077                 addr_type = ipv6_addr_type(addr6);
1078
1079                 if (addr_type == IPV6_ADDR_ANY)
1080                         addr6 = &ipv6_hdr(skb)->daddr;
1081
1082                 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1083                 neigh_release(neigh);
1084         } else if (!(t->parms.flags &
1085                      (IP6_TNL_F_USE_ORIG_TCLASS | IP6_TNL_F_USE_ORIG_FWMARK))) {
1086                 /* enable the cache only only if the routing decision does
1087                  * not depend on the current inner header value
1088                  */
1089                 use_cache = true;
1090         }
1091
1092         if (use_cache)
1093                 dst = ip6_tnl_dst_get(t);
1094
1095         if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1096                 goto tx_err_link_failure;
1097
1098         if (!dst) {
1099                 dst = ip6_route_output(net, NULL, fl6);
1100
1101                 if (dst->error)
1102                         goto tx_err_link_failure;
1103                 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1104                 if (IS_ERR(dst)) {
1105                         err = PTR_ERR(dst);
1106                         dst = NULL;
1107                         goto tx_err_link_failure;
1108                 }
1109                 ndst = dst;
1110         }
1111
1112         tdev = dst->dev;
1113
1114         if (tdev == dev) {
1115                 stats->collisions++;
1116                 net_warn_ratelimited("%s: Local routing loop detected!\n",
1117                                      t->parms.name);
1118                 goto tx_err_dst_release;
1119         }
1120         mtu = dst_mtu(dst) - sizeof(*ipv6h);
1121         if (encap_limit >= 0) {
1122                 max_headroom += 8;
1123                 mtu -= 8;
1124         }
1125         if (mtu < IPV6_MIN_MTU)
1126                 mtu = IPV6_MIN_MTU;
1127         if (skb_dst(skb))
1128                 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1129         if (skb->len > mtu) {
1130                 *pmtu = mtu;
1131                 err = -EMSGSIZE;
1132                 goto tx_err_dst_release;
1133         }
1134
1135         skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1136
1137         /*
1138          * Okay, now see if we can stuff it in the buffer as-is.
1139          */
1140         max_headroom += LL_RESERVED_SPACE(tdev);
1141
1142         if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1143             (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1144                 struct sk_buff *new_skb;
1145
1146                 new_skb = skb_realloc_headroom(skb, max_headroom);
1147                 if (!new_skb)
1148                         goto tx_err_dst_release;
1149
1150                 if (skb->sk)
1151                         skb_set_owner_w(new_skb, skb->sk);
1152                 consume_skb(skb);
1153                 skb = new_skb;
1154         }
1155
1156         if (use_cache && ndst)
1157                 ip6_tnl_dst_set(t, ndst);
1158         skb_dst_set(skb, dst);
1159
1160         skb->transport_header = skb->network_header;
1161
1162         proto = fl6->flowi6_proto;
1163         if (encap_limit >= 0) {
1164                 init_tel_txopt(&opt, encap_limit);
1165                 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1166         }
1167
1168         if (likely(!skb->encapsulation)) {
1169                 skb_reset_inner_headers(skb);
1170                 skb->encapsulation = 1;
1171         }
1172
1173         skb_push(skb, sizeof(struct ipv6hdr));
1174         skb_reset_network_header(skb);
1175         ipv6h = ipv6_hdr(skb);
1176         ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1177                      ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1178         ipv6h->hop_limit = t->parms.hop_limit;
1179         ipv6h->nexthdr = proto;
1180         ipv6h->saddr = fl6->saddr;
1181         ipv6h->daddr = fl6->daddr;
1182         ip6tunnel_xmit(NULL, skb, dev);
1183         return 0;
1184 tx_err_link_failure:
1185         stats->tx_carrier_errors++;
1186         dst_link_failure(skb);
1187 tx_err_dst_release:
1188         dst_release(dst);
1189         return err;
1190 }
1191
1192 static inline int
1193 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1194 {
1195         struct ip6_tnl *t = netdev_priv(dev);
1196         const struct iphdr  *iph = ip_hdr(skb);
1197         int encap_limit = -1;
1198         struct flowi6 fl6;
1199         __u8 dsfield;
1200         __u32 mtu;
1201         u8 tproto;
1202         int err;
1203
1204         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1205
1206         tproto = ACCESS_ONCE(t->parms.proto);
1207         if (tproto != IPPROTO_IPIP && tproto != 0)
1208                 return -1;
1209
1210         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1211                 encap_limit = t->parms.encap_limit;
1212
1213         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1214         fl6.flowi6_proto = IPPROTO_IPIP;
1215
1216         dsfield = ipv4_get_dsfield(iph);
1217
1218         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1219                 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1220                                           & IPV6_TCLASS_MASK;
1221         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1222                 fl6.flowi6_mark = skb->mark;
1223
1224         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1225         if (err != 0) {
1226                 /* XXX: send ICMP error even if DF is not set. */
1227                 if (err == -EMSGSIZE)
1228                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1229                                   htonl(mtu));
1230                 return -1;
1231         }
1232
1233         return 0;
1234 }
1235
1236 static inline int
1237 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1238 {
1239         struct ip6_tnl *t = netdev_priv(dev);
1240         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1241         int encap_limit = -1;
1242         __u16 offset;
1243         struct flowi6 fl6;
1244         __u8 dsfield;
1245         __u32 mtu;
1246         u8 tproto;
1247         int err;
1248
1249         tproto = ACCESS_ONCE(t->parms.proto);
1250         if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1251             ip6_tnl_addr_conflict(t, ipv6h))
1252                 return -1;
1253
1254         offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1255         if (offset > 0) {
1256                 struct ipv6_tlv_tnl_enc_lim *tel;
1257                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1258                 if (tel->encap_limit == 0) {
1259                         icmpv6_send(skb, ICMPV6_PARAMPROB,
1260                                     ICMPV6_HDR_FIELD, offset + 2);
1261                         return -1;
1262                 }
1263                 encap_limit = tel->encap_limit - 1;
1264         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1265                 encap_limit = t->parms.encap_limit;
1266
1267         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1268         fl6.flowi6_proto = IPPROTO_IPV6;
1269
1270         dsfield = ipv6_get_dsfield(ipv6h);
1271         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1272                 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1273         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1274                 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1275         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1276                 fl6.flowi6_mark = skb->mark;
1277
1278         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1279         if (err != 0) {
1280                 if (err == -EMSGSIZE)
1281                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1282                 return -1;
1283         }
1284
1285         return 0;
1286 }
1287
1288 static netdev_tx_t
1289 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1290 {
1291         struct ip6_tnl *t = netdev_priv(dev);
1292         struct net_device_stats *stats = &t->dev->stats;
1293         int ret;
1294
1295         switch (skb->protocol) {
1296         case htons(ETH_P_IP):
1297                 ret = ip4ip6_tnl_xmit(skb, dev);
1298                 break;
1299         case htons(ETH_P_IPV6):
1300                 ret = ip6ip6_tnl_xmit(skb, dev);
1301                 break;
1302         default:
1303                 goto tx_err;
1304         }
1305
1306         if (ret < 0)
1307                 goto tx_err;
1308
1309         return NETDEV_TX_OK;
1310
1311 tx_err:
1312         stats->tx_errors++;
1313         stats->tx_dropped++;
1314         kfree_skb(skb);
1315         return NETDEV_TX_OK;
1316 }
1317
1318 static void ip6_tnl_link_config(struct ip6_tnl *t)
1319 {
1320         struct net_device *dev = t->dev;
1321         struct __ip6_tnl_parm *p = &t->parms;
1322         struct flowi6 *fl6 = &t->fl.u.ip6;
1323
1324         memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1325         memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1326
1327         /* Set up flowi template */
1328         fl6->saddr = p->laddr;
1329         fl6->daddr = p->raddr;
1330         fl6->flowi6_oif = p->link;
1331         fl6->flowlabel = 0;
1332
1333         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1334                 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1335         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1336                 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1337
1338         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1339         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1340
1341         if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1342                 dev->flags |= IFF_POINTOPOINT;
1343         else
1344                 dev->flags &= ~IFF_POINTOPOINT;
1345
1346         if (p->flags & IP6_TNL_F_CAP_XMIT) {
1347                 int strict = (ipv6_addr_type(&p->raddr) &
1348                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1349
1350                 struct rt6_info *rt = rt6_lookup(t->net,
1351                                                  &p->raddr, &p->laddr,
1352                                                  p->link, strict);
1353
1354                 if (!rt)
1355                         return;
1356
1357                 if (rt->dst.dev) {
1358                         dev->hard_header_len = rt->dst.dev->hard_header_len +
1359                                 sizeof(struct ipv6hdr);
1360
1361                         dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1362                         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1363                                 dev->mtu -= 8;
1364
1365                         if (dev->mtu < IPV6_MIN_MTU)
1366                                 dev->mtu = IPV6_MIN_MTU;
1367                 }
1368                 ip6_rt_put(rt);
1369         }
1370 }
1371
1372 /**
1373  * ip6_tnl_change - update the tunnel parameters
1374  *   @t: tunnel to be changed
1375  *   @p: tunnel configuration parameters
1376  *
1377  * Description:
1378  *   ip6_tnl_change() updates the tunnel parameters
1379  **/
1380
1381 static int
1382 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1383 {
1384         t->parms.laddr = p->laddr;
1385         t->parms.raddr = p->raddr;
1386         t->parms.flags = p->flags;
1387         t->parms.hop_limit = p->hop_limit;
1388         t->parms.encap_limit = p->encap_limit;
1389         t->parms.flowinfo = p->flowinfo;
1390         t->parms.link = p->link;
1391         t->parms.proto = p->proto;
1392         ip6_tnl_dst_reset(t);
1393         ip6_tnl_link_config(t);
1394         return 0;
1395 }
1396
1397 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1398 {
1399         struct net *net = t->net;
1400         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1401         int err;
1402
1403         ip6_tnl_unlink(ip6n, t);
1404         synchronize_net();
1405         err = ip6_tnl_change(t, p);
1406         ip6_tnl_link(ip6n, t);
1407         netdev_state_change(t->dev);
1408         return err;
1409 }
1410
1411 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1412 {
1413         /* for default tnl0 device allow to change only the proto */
1414         t->parms.proto = p->proto;
1415         netdev_state_change(t->dev);
1416         return 0;
1417 }
1418
1419 static void
1420 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1421 {
1422         p->laddr = u->laddr;
1423         p->raddr = u->raddr;
1424         p->flags = u->flags;
1425         p->hop_limit = u->hop_limit;
1426         p->encap_limit = u->encap_limit;
1427         p->flowinfo = u->flowinfo;
1428         p->link = u->link;
1429         p->proto = u->proto;
1430         memcpy(p->name, u->name, sizeof(u->name));
1431 }
1432
1433 static void
1434 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1435 {
1436         u->laddr = p->laddr;
1437         u->raddr = p->raddr;
1438         u->flags = p->flags;
1439         u->hop_limit = p->hop_limit;
1440         u->encap_limit = p->encap_limit;
1441         u->flowinfo = p->flowinfo;
1442         u->link = p->link;
1443         u->proto = p->proto;
1444         memcpy(u->name, p->name, sizeof(u->name));
1445 }
1446
1447 /**
1448  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1449  *   @dev: virtual device associated with tunnel
1450  *   @ifr: parameters passed from userspace
1451  *   @cmd: command to be performed
1452  *
1453  * Description:
1454  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1455  *   from userspace.
1456  *
1457  *   The possible commands are the following:
1458  *     %SIOCGETTUNNEL: get tunnel parameters for device
1459  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1460  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
1461  *     %SIOCDELTUNNEL: delete tunnel
1462  *
1463  *   The fallback device "ip6tnl0", created during module
1464  *   initialization, can be used for creating other tunnel devices.
1465  *
1466  * Return:
1467  *   0 on success,
1468  *   %-EFAULT if unable to copy data to or from userspace,
1469  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
1470  *   %-EINVAL if passed tunnel parameters are invalid,
1471  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
1472  *   %-ENODEV if attempting to change or delete a nonexisting device
1473  **/
1474
1475 static int
1476 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1477 {
1478         int err = 0;
1479         struct ip6_tnl_parm p;
1480         struct __ip6_tnl_parm p1;
1481         struct ip6_tnl *t = netdev_priv(dev);
1482         struct net *net = t->net;
1483         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1484
1485         switch (cmd) {
1486         case SIOCGETTUNNEL:
1487                 if (dev == ip6n->fb_tnl_dev) {
1488                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1489                                 err = -EFAULT;
1490                                 break;
1491                         }
1492                         ip6_tnl_parm_from_user(&p1, &p);
1493                         t = ip6_tnl_locate(net, &p1, 0);
1494                         if (IS_ERR(t))
1495                                 t = netdev_priv(dev);
1496                 } else {
1497                         memset(&p, 0, sizeof(p));
1498                 }
1499                 ip6_tnl_parm_to_user(&p, &t->parms);
1500                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1501                         err = -EFAULT;
1502                 }
1503                 break;
1504         case SIOCADDTUNNEL:
1505         case SIOCCHGTUNNEL:
1506                 err = -EPERM;
1507                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1508                         break;
1509                 err = -EFAULT;
1510                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1511                         break;
1512                 err = -EINVAL;
1513                 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1514                     p.proto != 0)
1515                         break;
1516                 ip6_tnl_parm_from_user(&p1, &p);
1517                 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1518                 if (cmd == SIOCCHGTUNNEL) {
1519                         if (!IS_ERR(t)) {
1520                                 if (t->dev != dev) {
1521                                         err = -EEXIST;
1522                                         break;
1523                                 }
1524                         } else
1525                                 t = netdev_priv(dev);
1526                         if (dev == ip6n->fb_tnl_dev)
1527                                 err = ip6_tnl0_update(t, &p1);
1528                         else
1529                                 err = ip6_tnl_update(t, &p1);
1530                 }
1531                 if (!IS_ERR(t)) {
1532                         err = 0;
1533                         ip6_tnl_parm_to_user(&p, &t->parms);
1534                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1535                                 err = -EFAULT;
1536
1537                 } else {
1538                         err = PTR_ERR(t);
1539                 }
1540                 break;
1541         case SIOCDELTUNNEL:
1542                 err = -EPERM;
1543                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1544                         break;
1545
1546                 if (dev == ip6n->fb_tnl_dev) {
1547                         err = -EFAULT;
1548                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1549                                 break;
1550                         err = -ENOENT;
1551                         ip6_tnl_parm_from_user(&p1, &p);
1552                         t = ip6_tnl_locate(net, &p1, 0);
1553                         if (IS_ERR(t))
1554                                 break;
1555                         err = -EPERM;
1556                         if (t->dev == ip6n->fb_tnl_dev)
1557                                 break;
1558                         dev = t->dev;
1559                 }
1560                 err = 0;
1561                 unregister_netdevice(dev);
1562                 break;
1563         default:
1564                 err = -EINVAL;
1565         }
1566         return err;
1567 }
1568
1569 /**
1570  * ip6_tnl_change_mtu - change mtu manually for tunnel device
1571  *   @dev: virtual device associated with tunnel
1572  *   @new_mtu: the new mtu
1573  *
1574  * Return:
1575  *   0 on success,
1576  *   %-EINVAL if mtu too small
1577  **/
1578
1579 static int
1580 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1581 {
1582         struct ip6_tnl *tnl = netdev_priv(dev);
1583
1584         if (tnl->parms.proto == IPPROTO_IPIP) {
1585                 if (new_mtu < 68)
1586                         return -EINVAL;
1587         } else {
1588                 if (new_mtu < IPV6_MIN_MTU)
1589                         return -EINVAL;
1590         }
1591         if (new_mtu > 0xFFF8 - dev->hard_header_len)
1592                 return -EINVAL;
1593         dev->mtu = new_mtu;
1594         return 0;
1595 }
1596
1597 int ip6_tnl_get_iflink(const struct net_device *dev)
1598 {
1599         struct ip6_tnl *t = netdev_priv(dev);
1600
1601         return t->parms.link;
1602 }
1603 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1604
1605 static const struct net_device_ops ip6_tnl_netdev_ops = {
1606         .ndo_init       = ip6_tnl_dev_init,
1607         .ndo_uninit     = ip6_tnl_dev_uninit,
1608         .ndo_start_xmit = ip6_tnl_xmit,
1609         .ndo_do_ioctl   = ip6_tnl_ioctl,
1610         .ndo_change_mtu = ip6_tnl_change_mtu,
1611         .ndo_get_stats  = ip6_get_stats,
1612         .ndo_get_iflink = ip6_tnl_get_iflink,
1613 };
1614
1615
1616 /**
1617  * ip6_tnl_dev_setup - setup virtual tunnel device
1618  *   @dev: virtual device associated with tunnel
1619  *
1620  * Description:
1621  *   Initialize function pointers and device parameters
1622  **/
1623
1624 static void ip6_tnl_dev_setup(struct net_device *dev)
1625 {
1626         struct ip6_tnl *t;
1627
1628         dev->netdev_ops = &ip6_tnl_netdev_ops;
1629         dev->destructor = ip6_dev_free;
1630
1631         dev->type = ARPHRD_TUNNEL6;
1632         dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1633         dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1634         t = netdev_priv(dev);
1635         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1636                 dev->mtu -= 8;
1637         dev->flags |= IFF_NOARP;
1638         dev->addr_len = sizeof(struct in6_addr);
1639         netif_keep_dst(dev);
1640         /* This perm addr will be used as interface identifier by IPv6 */
1641         dev->addr_assign_type = NET_ADDR_RANDOM;
1642         eth_random_addr(dev->perm_addr);
1643 }
1644
1645
1646 /**
1647  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1648  *   @dev: virtual device associated with tunnel
1649  **/
1650
1651 static inline int
1652 ip6_tnl_dev_init_gen(struct net_device *dev)
1653 {
1654         struct ip6_tnl *t = netdev_priv(dev);
1655         int ret;
1656
1657         t->dev = dev;
1658         t->net = dev_net(dev);
1659         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1660         if (!dev->tstats)
1661                 return -ENOMEM;
1662
1663         ret = ip6_tnl_dst_init(t);
1664         if (ret) {
1665                 free_percpu(dev->tstats);
1666                 dev->tstats = NULL;
1667                 return ret;
1668         }
1669
1670         return 0;
1671 }
1672
1673 /**
1674  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1675  *   @dev: virtual device associated with tunnel
1676  **/
1677
1678 static int ip6_tnl_dev_init(struct net_device *dev)
1679 {
1680         struct ip6_tnl *t = netdev_priv(dev);
1681         int err = ip6_tnl_dev_init_gen(dev);
1682
1683         if (err)
1684                 return err;
1685         ip6_tnl_link_config(t);
1686         return 0;
1687 }
1688
1689 /**
1690  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1691  *   @dev: fallback device
1692  *
1693  * Return: 0
1694  **/
1695
1696 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1697 {
1698         struct ip6_tnl *t = netdev_priv(dev);
1699         struct net *net = dev_net(dev);
1700         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1701
1702         t->parms.proto = IPPROTO_IPV6;
1703         dev_hold(dev);
1704
1705         rcu_assign_pointer(ip6n->tnls_wc[0], t);
1706         return 0;
1707 }
1708
1709 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1710 {
1711         u8 proto;
1712
1713         if (!data || !data[IFLA_IPTUN_PROTO])
1714                 return 0;
1715
1716         proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1717         if (proto != IPPROTO_IPV6 &&
1718             proto != IPPROTO_IPIP &&
1719             proto != 0)
1720                 return -EINVAL;
1721
1722         return 0;
1723 }
1724
1725 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1726                                   struct __ip6_tnl_parm *parms)
1727 {
1728         memset(parms, 0, sizeof(*parms));
1729
1730         if (!data)
1731                 return;
1732
1733         if (data[IFLA_IPTUN_LINK])
1734                 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1735
1736         if (data[IFLA_IPTUN_LOCAL])
1737                 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1738
1739         if (data[IFLA_IPTUN_REMOTE])
1740                 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1741
1742         if (data[IFLA_IPTUN_TTL])
1743                 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1744
1745         if (data[IFLA_IPTUN_ENCAP_LIMIT])
1746                 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1747
1748         if (data[IFLA_IPTUN_FLOWINFO])
1749                 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1750
1751         if (data[IFLA_IPTUN_FLAGS])
1752                 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1753
1754         if (data[IFLA_IPTUN_PROTO])
1755                 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1756 }
1757
1758 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1759                            struct nlattr *tb[], struct nlattr *data[])
1760 {
1761         struct net *net = dev_net(dev);
1762         struct ip6_tnl *nt, *t;
1763
1764         nt = netdev_priv(dev);
1765         ip6_tnl_netlink_parms(data, &nt->parms);
1766
1767         t = ip6_tnl_locate(net, &nt->parms, 0);
1768         if (!IS_ERR(t))
1769                 return -EEXIST;
1770
1771         return ip6_tnl_create2(dev);
1772 }
1773
1774 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1775                               struct nlattr *data[])
1776 {
1777         struct ip6_tnl *t = netdev_priv(dev);
1778         struct __ip6_tnl_parm p;
1779         struct net *net = t->net;
1780         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1781
1782         if (dev == ip6n->fb_tnl_dev)
1783                 return -EINVAL;
1784
1785         ip6_tnl_netlink_parms(data, &p);
1786
1787         t = ip6_tnl_locate(net, &p, 0);
1788         if (!IS_ERR(t)) {
1789                 if (t->dev != dev)
1790                         return -EEXIST;
1791         } else
1792                 t = netdev_priv(dev);
1793
1794         return ip6_tnl_update(t, &p);
1795 }
1796
1797 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1798 {
1799         struct net *net = dev_net(dev);
1800         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1801
1802         if (dev != ip6n->fb_tnl_dev)
1803                 unregister_netdevice_queue(dev, head);
1804 }
1805
1806 static size_t ip6_tnl_get_size(const struct net_device *dev)
1807 {
1808         return
1809                 /* IFLA_IPTUN_LINK */
1810                 nla_total_size(4) +
1811                 /* IFLA_IPTUN_LOCAL */
1812                 nla_total_size(sizeof(struct in6_addr)) +
1813                 /* IFLA_IPTUN_REMOTE */
1814                 nla_total_size(sizeof(struct in6_addr)) +
1815                 /* IFLA_IPTUN_TTL */
1816                 nla_total_size(1) +
1817                 /* IFLA_IPTUN_ENCAP_LIMIT */
1818                 nla_total_size(1) +
1819                 /* IFLA_IPTUN_FLOWINFO */
1820                 nla_total_size(4) +
1821                 /* IFLA_IPTUN_FLAGS */
1822                 nla_total_size(4) +
1823                 /* IFLA_IPTUN_PROTO */
1824                 nla_total_size(1) +
1825                 0;
1826 }
1827
1828 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1829 {
1830         struct ip6_tnl *tunnel = netdev_priv(dev);
1831         struct __ip6_tnl_parm *parm = &tunnel->parms;
1832
1833         if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1834             nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1835             nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1836             nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1837             nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1838             nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1839             nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1840             nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1841                 goto nla_put_failure;
1842         return 0;
1843
1844 nla_put_failure:
1845         return -EMSGSIZE;
1846 }
1847
1848 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1849 {
1850         struct ip6_tnl *tunnel = netdev_priv(dev);
1851
1852         return tunnel->net;
1853 }
1854 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1855
1856 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1857         [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
1858         [IFLA_IPTUN_LOCAL]              = { .len = sizeof(struct in6_addr) },
1859         [IFLA_IPTUN_REMOTE]             = { .len = sizeof(struct in6_addr) },
1860         [IFLA_IPTUN_TTL]                = { .type = NLA_U8 },
1861         [IFLA_IPTUN_ENCAP_LIMIT]        = { .type = NLA_U8 },
1862         [IFLA_IPTUN_FLOWINFO]           = { .type = NLA_U32 },
1863         [IFLA_IPTUN_FLAGS]              = { .type = NLA_U32 },
1864         [IFLA_IPTUN_PROTO]              = { .type = NLA_U8 },
1865 };
1866
1867 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1868         .kind           = "ip6tnl",
1869         .maxtype        = IFLA_IPTUN_MAX,
1870         .policy         = ip6_tnl_policy,
1871         .priv_size      = sizeof(struct ip6_tnl),
1872         .setup          = ip6_tnl_dev_setup,
1873         .validate       = ip6_tnl_validate,
1874         .newlink        = ip6_tnl_newlink,
1875         .changelink     = ip6_tnl_changelink,
1876         .dellink        = ip6_tnl_dellink,
1877         .get_size       = ip6_tnl_get_size,
1878         .fill_info      = ip6_tnl_fill_info,
1879         .get_link_net   = ip6_tnl_get_link_net,
1880 };
1881
1882 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1883         .handler        = ip4ip6_rcv,
1884         .err_handler    = ip4ip6_err,
1885         .priority       =       1,
1886 };
1887
1888 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1889         .handler        = ip6ip6_rcv,
1890         .err_handler    = ip6ip6_err,
1891         .priority       =       1,
1892 };
1893
1894 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1895 {
1896         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1897         struct net_device *dev, *aux;
1898         int h;
1899         struct ip6_tnl *t;
1900         LIST_HEAD(list);
1901
1902         for_each_netdev_safe(net, dev, aux)
1903                 if (dev->rtnl_link_ops == &ip6_link_ops)
1904                         unregister_netdevice_queue(dev, &list);
1905
1906         for (h = 0; h < HASH_SIZE; h++) {
1907                 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1908                 while (t) {
1909                         /* If dev is in the same netns, it has already
1910                          * been added to the list by the previous loop.
1911                          */
1912                         if (!net_eq(dev_net(t->dev), net))
1913                                 unregister_netdevice_queue(t->dev, &list);
1914                         t = rtnl_dereference(t->next);
1915                 }
1916         }
1917
1918         unregister_netdevice_many(&list);
1919 }
1920
1921 static int __net_init ip6_tnl_init_net(struct net *net)
1922 {
1923         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1924         struct ip6_tnl *t = NULL;
1925         int err;
1926
1927         ip6n->tnls[0] = ip6n->tnls_wc;
1928         ip6n->tnls[1] = ip6n->tnls_r_l;
1929
1930         err = -ENOMEM;
1931         ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1932                                         NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1933
1934         if (!ip6n->fb_tnl_dev)
1935                 goto err_alloc_dev;
1936         dev_net_set(ip6n->fb_tnl_dev, net);
1937         ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1938         /* FB netdevice is special: we have one, and only one per netns.
1939          * Allowing to move it to another netns is clearly unsafe.
1940          */
1941         ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1942
1943         err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1944         if (err < 0)
1945                 goto err_register;
1946
1947         err = register_netdev(ip6n->fb_tnl_dev);
1948         if (err < 0)
1949                 goto err_register;
1950
1951         t = netdev_priv(ip6n->fb_tnl_dev);
1952
1953         strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1954         return 0;
1955
1956 err_register:
1957         ip6_dev_free(ip6n->fb_tnl_dev);
1958 err_alloc_dev:
1959         return err;
1960 }
1961
1962 static void __net_exit ip6_tnl_exit_net(struct net *net)
1963 {
1964         rtnl_lock();
1965         ip6_tnl_destroy_tunnels(net);
1966         rtnl_unlock();
1967 }
1968
1969 static struct pernet_operations ip6_tnl_net_ops = {
1970         .init = ip6_tnl_init_net,
1971         .exit = ip6_tnl_exit_net,
1972         .id   = &ip6_tnl_net_id,
1973         .size = sizeof(struct ip6_tnl_net),
1974 };
1975
1976 /**
1977  * ip6_tunnel_init - register protocol and reserve needed resources
1978  *
1979  * Return: 0 on success
1980  **/
1981
1982 static int __init ip6_tunnel_init(void)
1983 {
1984         int  err;
1985
1986         err = register_pernet_device(&ip6_tnl_net_ops);
1987         if (err < 0)
1988                 goto out_pernet;
1989
1990         err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1991         if (err < 0) {
1992                 pr_err("%s: can't register ip4ip6\n", __func__);
1993                 goto out_ip4ip6;
1994         }
1995
1996         err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1997         if (err < 0) {
1998                 pr_err("%s: can't register ip6ip6\n", __func__);
1999                 goto out_ip6ip6;
2000         }
2001         err = rtnl_link_register(&ip6_link_ops);
2002         if (err < 0)
2003                 goto rtnl_link_failed;
2004
2005         return 0;
2006
2007 rtnl_link_failed:
2008         xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
2009 out_ip6ip6:
2010         xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
2011 out_ip4ip6:
2012         unregister_pernet_device(&ip6_tnl_net_ops);
2013 out_pernet:
2014         return err;
2015 }
2016
2017 /**
2018  * ip6_tunnel_cleanup - free resources and unregister protocol
2019  **/
2020
2021 static void __exit ip6_tunnel_cleanup(void)
2022 {
2023         rtnl_link_unregister(&ip6_link_ops);
2024         if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2025                 pr_info("%s: can't deregister ip4ip6\n", __func__);
2026
2027         if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2028                 pr_info("%s: can't deregister ip6ip6\n", __func__);
2029
2030         unregister_pernet_device(&ip6_tnl_net_ops);
2031 }
2032
2033 module_init(ip6_tunnel_init);
2034 module_exit(ip6_tunnel_cleanup);