Merge branch 'linux-linaro-lsk-v4.4-android' of git://git.linaro.org/kernel/linux...
[firefly-linux-kernel-4.4.55.git] / 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         __u8 nexthdr = ipv6h->nexthdr;
484         __u16 off = sizeof(*ipv6h);
485
486         while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
487                 __u16 optlen = 0;
488                 struct ipv6_opt_hdr *hdr;
489                 if (raw + off + sizeof(*hdr) > skb->data &&
490                     !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
491                         break;
492
493                 hdr = (struct ipv6_opt_hdr *) (raw + off);
494                 if (nexthdr == NEXTHDR_FRAGMENT) {
495                         struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
496                         if (frag_hdr->frag_off)
497                                 break;
498                         optlen = 8;
499                 } else if (nexthdr == NEXTHDR_AUTH) {
500                         optlen = (hdr->hdrlen + 2) << 2;
501                 } else {
502                         optlen = ipv6_optlen(hdr);
503                 }
504                 if (nexthdr == NEXTHDR_DEST) {
505                         __u16 i = off + 2;
506                         while (1) {
507                                 struct ipv6_tlv_tnl_enc_lim *tel;
508
509                                 /* No more room for encapsulation limit */
510                                 if (i + sizeof (*tel) > off + optlen)
511                                         break;
512
513                                 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
514                                 /* return index of option if found and valid */
515                                 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
516                                     tel->length == 1)
517                                         return i;
518                                 /* else jump to next option */
519                                 if (tel->type)
520                                         i += tel->length + 2;
521                                 else
522                                         i++;
523                         }
524                 }
525                 nexthdr = hdr->nexthdr;
526                 off += optlen;
527         }
528         return 0;
529 }
530 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
531
532 /**
533  * ip6_tnl_err - tunnel error handler
534  *
535  * Description:
536  *   ip6_tnl_err() should handle errors in the tunnel according
537  *   to the specifications in RFC 2473.
538  **/
539
540 static int
541 ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
542             u8 *type, u8 *code, int *msg, __u32 *info, int offset)
543 {
544         const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
545         struct ip6_tnl *t;
546         int rel_msg = 0;
547         u8 rel_type = ICMPV6_DEST_UNREACH;
548         u8 rel_code = ICMPV6_ADDR_UNREACH;
549         u8 tproto;
550         __u32 rel_info = 0;
551         __u16 len;
552         int err = -ENOENT;
553
554         /* If the packet doesn't contain the original IPv6 header we are
555            in trouble since we might need the source address for further
556            processing of the error. */
557
558         rcu_read_lock();
559         t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
560         if (!t)
561                 goto out;
562
563         tproto = ACCESS_ONCE(t->parms.proto);
564         if (tproto != ipproto && tproto != 0)
565                 goto out;
566
567         err = 0;
568
569         switch (*type) {
570                 __u32 teli;
571                 struct ipv6_tlv_tnl_enc_lim *tel;
572                 __u32 mtu;
573         case ICMPV6_DEST_UNREACH:
574                 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
575                                     t->parms.name);
576                 rel_msg = 1;
577                 break;
578         case ICMPV6_TIME_EXCEED:
579                 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
580                         net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
581                                             t->parms.name);
582                         rel_msg = 1;
583                 }
584                 break;
585         case ICMPV6_PARAMPROB:
586                 teli = 0;
587                 if ((*code) == ICMPV6_HDR_FIELD)
588                         teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
589
590                 if (teli && teli == *info - 2) {
591                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
592                         if (tel->encap_limit == 0) {
593                                 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
594                                                     t->parms.name);
595                                 rel_msg = 1;
596                         }
597                 } else {
598                         net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
599                                             t->parms.name);
600                 }
601                 break;
602         case ICMPV6_PKT_TOOBIG:
603                 mtu = *info - offset;
604                 if (mtu < IPV6_MIN_MTU)
605                         mtu = IPV6_MIN_MTU;
606                 t->dev->mtu = mtu;
607
608                 len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
609                 if (len > mtu) {
610                         rel_type = ICMPV6_PKT_TOOBIG;
611                         rel_code = 0;
612                         rel_info = mtu;
613                         rel_msg = 1;
614                 }
615                 break;
616         }
617
618         *type = rel_type;
619         *code = rel_code;
620         *info = rel_info;
621         *msg = rel_msg;
622
623 out:
624         rcu_read_unlock();
625         return err;
626 }
627
628 static int
629 ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
630            u8 type, u8 code, int offset, __be32 info)
631 {
632         int rel_msg = 0;
633         u8 rel_type = type;
634         u8 rel_code = code;
635         __u32 rel_info = ntohl(info);
636         int err;
637         struct sk_buff *skb2;
638         const struct iphdr *eiph;
639         struct rtable *rt;
640         struct flowi4 fl4;
641
642         err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
643                           &rel_msg, &rel_info, offset);
644         if (err < 0)
645                 return err;
646
647         if (rel_msg == 0)
648                 return 0;
649
650         switch (rel_type) {
651         case ICMPV6_DEST_UNREACH:
652                 if (rel_code != ICMPV6_ADDR_UNREACH)
653                         return 0;
654                 rel_type = ICMP_DEST_UNREACH;
655                 rel_code = ICMP_HOST_UNREACH;
656                 break;
657         case ICMPV6_PKT_TOOBIG:
658                 if (rel_code != 0)
659                         return 0;
660                 rel_type = ICMP_DEST_UNREACH;
661                 rel_code = ICMP_FRAG_NEEDED;
662                 break;
663         case NDISC_REDIRECT:
664                 rel_type = ICMP_REDIRECT;
665                 rel_code = ICMP_REDIR_HOST;
666         default:
667                 return 0;
668         }
669
670         if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
671                 return 0;
672
673         skb2 = skb_clone(skb, GFP_ATOMIC);
674         if (!skb2)
675                 return 0;
676
677         skb_dst_drop(skb2);
678
679         skb_pull(skb2, offset);
680         skb_reset_network_header(skb2);
681         eiph = ip_hdr(skb2);
682
683         /* Try to guess incoming interface */
684         rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
685                                    eiph->saddr, 0,
686                                    0, 0,
687                                    IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
688         if (IS_ERR(rt))
689                 goto out;
690
691         skb2->dev = rt->dst.dev;
692
693         /* route "incoming" packet */
694         if (rt->rt_flags & RTCF_LOCAL) {
695                 ip_rt_put(rt);
696                 rt = NULL;
697                 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
698                                            eiph->daddr, eiph->saddr,
699                                            0, 0,
700                                            IPPROTO_IPIP,
701                                            RT_TOS(eiph->tos), 0);
702                 if (IS_ERR(rt) ||
703                     rt->dst.dev->type != ARPHRD_TUNNEL) {
704                         if (!IS_ERR(rt))
705                                 ip_rt_put(rt);
706                         goto out;
707                 }
708                 skb_dst_set(skb2, &rt->dst);
709         } else {
710                 ip_rt_put(rt);
711                 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
712                                    skb2->dev) ||
713                     skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
714                         goto out;
715         }
716
717         /* change mtu on this route */
718         if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
719                 if (rel_info > dst_mtu(skb_dst(skb2)))
720                         goto out;
721
722                 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
723         }
724         if (rel_type == ICMP_REDIRECT)
725                 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
726
727         icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
728
729 out:
730         kfree_skb(skb2);
731         return 0;
732 }
733
734 static int
735 ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
736            u8 type, u8 code, int offset, __be32 info)
737 {
738         int rel_msg = 0;
739         u8 rel_type = type;
740         u8 rel_code = code;
741         __u32 rel_info = ntohl(info);
742         int err;
743
744         err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
745                           &rel_msg, &rel_info, offset);
746         if (err < 0)
747                 return err;
748
749         if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
750                 struct rt6_info *rt;
751                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
752
753                 if (!skb2)
754                         return 0;
755
756                 skb_dst_drop(skb2);
757                 skb_pull(skb2, offset);
758                 skb_reset_network_header(skb2);
759
760                 /* Try to guess incoming interface */
761                 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
762                                 NULL, 0, 0);
763
764                 if (rt && rt->dst.dev)
765                         skb2->dev = rt->dst.dev;
766
767                 icmpv6_send(skb2, rel_type, rel_code, rel_info);
768
769                 ip6_rt_put(rt);
770
771                 kfree_skb(skb2);
772         }
773
774         return 0;
775 }
776
777 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
778                                        const struct ipv6hdr *ipv6h,
779                                        struct sk_buff *skb)
780 {
781         __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
782
783         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
784                 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
785
786         return IP6_ECN_decapsulate(ipv6h, skb);
787 }
788
789 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
790                                        const struct ipv6hdr *ipv6h,
791                                        struct sk_buff *skb)
792 {
793         if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
794                 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
795
796         return IP6_ECN_decapsulate(ipv6h, skb);
797 }
798
799 __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
800                              const struct in6_addr *laddr,
801                              const struct in6_addr *raddr)
802 {
803         struct __ip6_tnl_parm *p = &t->parms;
804         int ltype = ipv6_addr_type(laddr);
805         int rtype = ipv6_addr_type(raddr);
806         __u32 flags = 0;
807
808         if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
809                 flags = IP6_TNL_F_CAP_PER_PACKET;
810         } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
811                    rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
812                    !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
813                    (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
814                 if (ltype&IPV6_ADDR_UNICAST)
815                         flags |= IP6_TNL_F_CAP_XMIT;
816                 if (rtype&IPV6_ADDR_UNICAST)
817                         flags |= IP6_TNL_F_CAP_RCV;
818         }
819         return flags;
820 }
821 EXPORT_SYMBOL(ip6_tnl_get_cap);
822
823 /* called with rcu_read_lock() */
824 int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
825                                   const struct in6_addr *laddr,
826                                   const struct in6_addr *raddr)
827 {
828         struct __ip6_tnl_parm *p = &t->parms;
829         int ret = 0;
830         struct net *net = t->net;
831
832         if ((p->flags & IP6_TNL_F_CAP_RCV) ||
833             ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
834              (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
835                 struct net_device *ldev = NULL;
836
837                 if (p->link)
838                         ldev = dev_get_by_index_rcu(net, p->link);
839
840                 if ((ipv6_addr_is_multicast(laddr) ||
841                      likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
842                     likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
843                         ret = 1;
844         }
845         return ret;
846 }
847 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
848
849 /**
850  * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
851  *   @skb: received socket buffer
852  *   @protocol: ethernet protocol ID
853  *   @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
854  *
855  * Return: 0
856  **/
857
858 static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
859                        __u8 ipproto,
860                        int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
861                                                    const struct ipv6hdr *ipv6h,
862                                                    struct sk_buff *skb))
863 {
864         struct ip6_tnl *t;
865         const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
866         u8 tproto;
867         int err;
868
869         rcu_read_lock();
870         t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
871         if (t) {
872                 struct pcpu_sw_netstats *tstats;
873
874                 tproto = ACCESS_ONCE(t->parms.proto);
875                 if (tproto != ipproto && tproto != 0) {
876                         rcu_read_unlock();
877                         goto discard;
878                 }
879
880                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
881                         rcu_read_unlock();
882                         goto discard;
883                 }
884
885                 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
886                         t->dev->stats.rx_dropped++;
887                         rcu_read_unlock();
888                         goto discard;
889                 }
890                 skb->mac_header = skb->network_header;
891                 skb_reset_network_header(skb);
892                 skb->protocol = htons(protocol);
893                 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
894
895                 __skb_tunnel_rx(skb, t->dev, t->net);
896
897                 err = dscp_ecn_decapsulate(t, ipv6h, skb);
898                 if (unlikely(err)) {
899                         if (log_ecn_error)
900                                 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
901                                                      &ipv6h->saddr,
902                                                      ipv6_get_dsfield(ipv6h));
903                         if (err > 1) {
904                                 ++t->dev->stats.rx_frame_errors;
905                                 ++t->dev->stats.rx_errors;
906                                 rcu_read_unlock();
907                                 goto discard;
908                         }
909                 }
910
911                 tstats = this_cpu_ptr(t->dev->tstats);
912                 u64_stats_update_begin(&tstats->syncp);
913                 tstats->rx_packets++;
914                 tstats->rx_bytes += skb->len;
915                 u64_stats_update_end(&tstats->syncp);
916
917                 netif_rx(skb);
918
919                 rcu_read_unlock();
920                 return 0;
921         }
922         rcu_read_unlock();
923         return 1;
924
925 discard:
926         kfree_skb(skb);
927         return 0;
928 }
929
930 static int ip4ip6_rcv(struct sk_buff *skb)
931 {
932         return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
933                            ip4ip6_dscp_ecn_decapsulate);
934 }
935
936 static int ip6ip6_rcv(struct sk_buff *skb)
937 {
938         return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
939                            ip6ip6_dscp_ecn_decapsulate);
940 }
941
942 struct ipv6_tel_txoption {
943         struct ipv6_txoptions ops;
944         __u8 dst_opt[8];
945 };
946
947 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
948 {
949         memset(opt, 0, sizeof(struct ipv6_tel_txoption));
950
951         opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
952         opt->dst_opt[3] = 1;
953         opt->dst_opt[4] = encap_limit;
954         opt->dst_opt[5] = IPV6_TLV_PADN;
955         opt->dst_opt[6] = 1;
956
957         opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
958         opt->ops.opt_nflen = 8;
959 }
960
961 /**
962  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
963  *   @t: the outgoing tunnel device
964  *   @hdr: IPv6 header from the incoming packet
965  *
966  * Description:
967  *   Avoid trivial tunneling loop by checking that tunnel exit-point
968  *   doesn't match source of incoming packet.
969  *
970  * Return:
971  *   1 if conflict,
972  *   0 else
973  **/
974
975 static inline bool
976 ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
977 {
978         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
979 }
980
981 int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
982                      const struct in6_addr *laddr,
983                      const struct in6_addr *raddr)
984 {
985         struct __ip6_tnl_parm *p = &t->parms;
986         int ret = 0;
987         struct net *net = t->net;
988
989         if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
990             ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
991              (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
992                 struct net_device *ldev = NULL;
993
994                 rcu_read_lock();
995                 if (p->link)
996                         ldev = dev_get_by_index_rcu(net, p->link);
997
998                 if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
999                         pr_warn("%s xmit: Local address not yet configured!\n",
1000                                 p->name);
1001                 else if (!ipv6_addr_is_multicast(raddr) &&
1002                          unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
1003                         pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1004                                 p->name);
1005                 else
1006                         ret = 1;
1007                 rcu_read_unlock();
1008         }
1009         return ret;
1010 }
1011 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
1012
1013 /**
1014  * ip6_tnl_xmit2 - encapsulate packet and send
1015  *   @skb: the outgoing socket buffer
1016  *   @dev: the outgoing tunnel device
1017  *   @dsfield: dscp code for outer header
1018  *   @fl: flow of tunneled packet
1019  *   @encap_limit: encapsulation limit
1020  *   @pmtu: Path MTU is stored if packet is too big
1021  *
1022  * Description:
1023  *   Build new header and do some sanity checks on the packet before sending
1024  *   it.
1025  *
1026  * Return:
1027  *   0 on success
1028  *   -1 fail
1029  *   %-EMSGSIZE message too big. return mtu in this case.
1030  **/
1031
1032 static int ip6_tnl_xmit2(struct sk_buff *skb,
1033                          struct net_device *dev,
1034                          __u8 dsfield,
1035                          struct flowi6 *fl6,
1036                          int encap_limit,
1037                          __u32 *pmtu)
1038 {
1039         struct ip6_tnl *t = netdev_priv(dev);
1040         struct net *net = t->net;
1041         struct net_device_stats *stats = &t->dev->stats;
1042         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1043         struct ipv6_tel_txoption opt;
1044         struct dst_entry *dst = NULL, *ndst = NULL;
1045         struct net_device *tdev;
1046         bool use_cache = false;
1047         int mtu;
1048         unsigned int max_headroom = sizeof(struct ipv6hdr);
1049         u8 proto;
1050         int err = -1;
1051
1052         /* NBMA tunnel */
1053         if (ipv6_addr_any(&t->parms.raddr)) {
1054                 struct in6_addr *addr6;
1055                 struct neighbour *neigh;
1056                 int addr_type;
1057
1058                 if (!skb_dst(skb))
1059                         goto tx_err_link_failure;
1060
1061                 neigh = dst_neigh_lookup(skb_dst(skb),
1062                                          &ipv6_hdr(skb)->daddr);
1063                 if (!neigh)
1064                         goto tx_err_link_failure;
1065
1066                 addr6 = (struct in6_addr *)&neigh->primary_key;
1067                 addr_type = ipv6_addr_type(addr6);
1068
1069                 if (addr_type == IPV6_ADDR_ANY)
1070                         addr6 = &ipv6_hdr(skb)->daddr;
1071
1072                 memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
1073                 neigh_release(neigh);
1074         } else if (!(t->parms.flags &
1075                      (IP6_TNL_F_USE_ORIG_TCLASS | IP6_TNL_F_USE_ORIG_FWMARK))) {
1076                 /* enable the cache only only if the routing decision does
1077                  * not depend on the current inner header value
1078                  */
1079                 use_cache = true;
1080         }
1081
1082         if (use_cache)
1083                 dst = ip6_tnl_dst_get(t);
1084
1085         if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
1086                 goto tx_err_link_failure;
1087
1088         if (!dst) {
1089                 dst = ip6_route_output(net, NULL, fl6);
1090
1091                 if (dst->error)
1092                         goto tx_err_link_failure;
1093                 dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
1094                 if (IS_ERR(dst)) {
1095                         err = PTR_ERR(dst);
1096                         dst = NULL;
1097                         goto tx_err_link_failure;
1098                 }
1099                 ndst = dst;
1100         }
1101
1102         tdev = dst->dev;
1103
1104         if (tdev == dev) {
1105                 stats->collisions++;
1106                 net_warn_ratelimited("%s: Local routing loop detected!\n",
1107                                      t->parms.name);
1108                 goto tx_err_dst_release;
1109         }
1110         mtu = dst_mtu(dst) - sizeof(*ipv6h);
1111         if (encap_limit >= 0) {
1112                 max_headroom += 8;
1113                 mtu -= 8;
1114         }
1115         if (mtu < IPV6_MIN_MTU)
1116                 mtu = IPV6_MIN_MTU;
1117         if (skb_dst(skb))
1118                 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
1119         if (skb->len > mtu) {
1120                 *pmtu = mtu;
1121                 err = -EMSGSIZE;
1122                 goto tx_err_dst_release;
1123         }
1124
1125         skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
1126
1127         /*
1128          * Okay, now see if we can stuff it in the buffer as-is.
1129          */
1130         max_headroom += LL_RESERVED_SPACE(tdev);
1131
1132         if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1133             (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1134                 struct sk_buff *new_skb;
1135
1136                 new_skb = skb_realloc_headroom(skb, max_headroom);
1137                 if (!new_skb)
1138                         goto tx_err_dst_release;
1139
1140                 if (skb->sk)
1141                         skb_set_owner_w(new_skb, skb->sk);
1142                 consume_skb(skb);
1143                 skb = new_skb;
1144         }
1145
1146         if (use_cache && ndst)
1147                 ip6_tnl_dst_set(t, ndst);
1148         skb_dst_set(skb, dst);
1149
1150         skb->transport_header = skb->network_header;
1151
1152         proto = fl6->flowi6_proto;
1153         if (encap_limit >= 0) {
1154                 init_tel_txopt(&opt, encap_limit);
1155                 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1156         }
1157
1158         if (likely(!skb->encapsulation)) {
1159                 skb_reset_inner_headers(skb);
1160                 skb->encapsulation = 1;
1161         }
1162
1163         skb_push(skb, sizeof(struct ipv6hdr));
1164         skb_reset_network_header(skb);
1165         ipv6h = ipv6_hdr(skb);
1166         ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield),
1167                      ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
1168         ipv6h->hop_limit = t->parms.hop_limit;
1169         ipv6h->nexthdr = proto;
1170         ipv6h->saddr = fl6->saddr;
1171         ipv6h->daddr = fl6->daddr;
1172         ip6tunnel_xmit(NULL, skb, dev);
1173         return 0;
1174 tx_err_link_failure:
1175         stats->tx_carrier_errors++;
1176         dst_link_failure(skb);
1177 tx_err_dst_release:
1178         dst_release(dst);
1179         return err;
1180 }
1181
1182 static inline int
1183 ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1184 {
1185         struct ip6_tnl *t = netdev_priv(dev);
1186         const struct iphdr  *iph = ip_hdr(skb);
1187         int encap_limit = -1;
1188         struct flowi6 fl6;
1189         __u8 dsfield;
1190         __u32 mtu;
1191         u8 tproto;
1192         int err;
1193
1194         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1195
1196         tproto = ACCESS_ONCE(t->parms.proto);
1197         if (tproto != IPPROTO_IPIP && tproto != 0)
1198                 return -1;
1199
1200         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1201                 encap_limit = t->parms.encap_limit;
1202
1203         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1204         fl6.flowi6_proto = IPPROTO_IPIP;
1205
1206         fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1207
1208         dsfield = ipv4_get_dsfield(iph);
1209
1210         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1211                 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
1212                                           & IPV6_TCLASS_MASK;
1213         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1214                 fl6.flowi6_mark = skb->mark;
1215
1216         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1217         if (err != 0) {
1218                 /* XXX: send ICMP error even if DF is not set. */
1219                 if (err == -EMSGSIZE)
1220                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1221                                   htonl(mtu));
1222                 return -1;
1223         }
1224
1225         return 0;
1226 }
1227
1228 static inline int
1229 ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1230 {
1231         struct ip6_tnl *t = netdev_priv(dev);
1232         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1233         int encap_limit = -1;
1234         __u16 offset;
1235         struct flowi6 fl6;
1236         __u8 dsfield;
1237         __u32 mtu;
1238         u8 tproto;
1239         int err;
1240
1241         tproto = ACCESS_ONCE(t->parms.proto);
1242         if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
1243             ip6_tnl_addr_conflict(t, ipv6h))
1244                 return -1;
1245
1246         offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
1247         if (offset > 0) {
1248                 struct ipv6_tlv_tnl_enc_lim *tel;
1249                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1250                 if (tel->encap_limit == 0) {
1251                         icmpv6_send(skb, ICMPV6_PARAMPROB,
1252                                     ICMPV6_HDR_FIELD, offset + 2);
1253                         return -1;
1254                 }
1255                 encap_limit = tel->encap_limit - 1;
1256         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1257                 encap_limit = t->parms.encap_limit;
1258
1259         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1260         fl6.flowi6_proto = IPPROTO_IPV6;
1261         fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1262
1263         dsfield = ipv6_get_dsfield(ipv6h);
1264         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
1265                 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1266         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
1267                 fl6.flowlabel |= ip6_flowlabel(ipv6h);
1268         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1269                 fl6.flowi6_mark = skb->mark;
1270
1271         err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
1272         if (err != 0) {
1273                 if (err == -EMSGSIZE)
1274                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1275                 return -1;
1276         }
1277
1278         return 0;
1279 }
1280
1281 static netdev_tx_t
1282 ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1283 {
1284         struct ip6_tnl *t = netdev_priv(dev);
1285         struct net_device_stats *stats = &t->dev->stats;
1286         int ret;
1287
1288         switch (skb->protocol) {
1289         case htons(ETH_P_IP):
1290                 ret = ip4ip6_tnl_xmit(skb, dev);
1291                 break;
1292         case htons(ETH_P_IPV6):
1293                 ret = ip6ip6_tnl_xmit(skb, dev);
1294                 break;
1295         default:
1296                 goto tx_err;
1297         }
1298
1299         if (ret < 0)
1300                 goto tx_err;
1301
1302         return NETDEV_TX_OK;
1303
1304 tx_err:
1305         stats->tx_errors++;
1306         stats->tx_dropped++;
1307         kfree_skb(skb);
1308         return NETDEV_TX_OK;
1309 }
1310
1311 static void ip6_tnl_link_config(struct ip6_tnl *t)
1312 {
1313         struct net_device *dev = t->dev;
1314         struct __ip6_tnl_parm *p = &t->parms;
1315         struct flowi6 *fl6 = &t->fl.u.ip6;
1316
1317         memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1318         memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1319
1320         /* Set up flowi template */
1321         fl6->saddr = p->laddr;
1322         fl6->daddr = p->raddr;
1323         fl6->flowi6_oif = p->link;
1324         fl6->flowlabel = 0;
1325
1326         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1327                 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1328         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1329                 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1330
1331         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1332         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1333
1334         if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1335                 dev->flags |= IFF_POINTOPOINT;
1336         else
1337                 dev->flags &= ~IFF_POINTOPOINT;
1338
1339         if (p->flags & IP6_TNL_F_CAP_XMIT) {
1340                 int strict = (ipv6_addr_type(&p->raddr) &
1341                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1342
1343                 struct rt6_info *rt = rt6_lookup(t->net,
1344                                                  &p->raddr, &p->laddr,
1345                                                  p->link, strict);
1346
1347                 if (!rt)
1348                         return;
1349
1350                 if (rt->dst.dev) {
1351                         dev->hard_header_len = rt->dst.dev->hard_header_len +
1352                                 sizeof(struct ipv6hdr);
1353
1354                         dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr);
1355                         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1356                                 dev->mtu -= 8;
1357
1358                         if (dev->mtu < IPV6_MIN_MTU)
1359                                 dev->mtu = IPV6_MIN_MTU;
1360                 }
1361                 ip6_rt_put(rt);
1362         }
1363 }
1364
1365 /**
1366  * ip6_tnl_change - update the tunnel parameters
1367  *   @t: tunnel to be changed
1368  *   @p: tunnel configuration parameters
1369  *
1370  * Description:
1371  *   ip6_tnl_change() updates the tunnel parameters
1372  **/
1373
1374 static int
1375 ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
1376 {
1377         t->parms.laddr = p->laddr;
1378         t->parms.raddr = p->raddr;
1379         t->parms.flags = p->flags;
1380         t->parms.hop_limit = p->hop_limit;
1381         t->parms.encap_limit = p->encap_limit;
1382         t->parms.flowinfo = p->flowinfo;
1383         t->parms.link = p->link;
1384         t->parms.proto = p->proto;
1385         ip6_tnl_dst_reset(t);
1386         ip6_tnl_link_config(t);
1387         return 0;
1388 }
1389
1390 static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1391 {
1392         struct net *net = t->net;
1393         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1394         int err;
1395
1396         ip6_tnl_unlink(ip6n, t);
1397         synchronize_net();
1398         err = ip6_tnl_change(t, p);
1399         ip6_tnl_link(ip6n, t);
1400         netdev_state_change(t->dev);
1401         return err;
1402 }
1403
1404 static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1405 {
1406         /* for default tnl0 device allow to change only the proto */
1407         t->parms.proto = p->proto;
1408         netdev_state_change(t->dev);
1409         return 0;
1410 }
1411
1412 static void
1413 ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1414 {
1415         p->laddr = u->laddr;
1416         p->raddr = u->raddr;
1417         p->flags = u->flags;
1418         p->hop_limit = u->hop_limit;
1419         p->encap_limit = u->encap_limit;
1420         p->flowinfo = u->flowinfo;
1421         p->link = u->link;
1422         p->proto = u->proto;
1423         memcpy(p->name, u->name, sizeof(u->name));
1424 }
1425
1426 static void
1427 ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1428 {
1429         u->laddr = p->laddr;
1430         u->raddr = p->raddr;
1431         u->flags = p->flags;
1432         u->hop_limit = p->hop_limit;
1433         u->encap_limit = p->encap_limit;
1434         u->flowinfo = p->flowinfo;
1435         u->link = p->link;
1436         u->proto = p->proto;
1437         memcpy(u->name, p->name, sizeof(u->name));
1438 }
1439
1440 /**
1441  * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1442  *   @dev: virtual device associated with tunnel
1443  *   @ifr: parameters passed from userspace
1444  *   @cmd: command to be performed
1445  *
1446  * Description:
1447  *   ip6_tnl_ioctl() is used for managing IPv6 tunnels
1448  *   from userspace.
1449  *
1450  *   The possible commands are the following:
1451  *     %SIOCGETTUNNEL: get tunnel parameters for device
1452  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1453  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
1454  *     %SIOCDELTUNNEL: delete tunnel
1455  *
1456  *   The fallback device "ip6tnl0", created during module
1457  *   initialization, can be used for creating other tunnel devices.
1458  *
1459  * Return:
1460  *   0 on success,
1461  *   %-EFAULT if unable to copy data to or from userspace,
1462  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
1463  *   %-EINVAL if passed tunnel parameters are invalid,
1464  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
1465  *   %-ENODEV if attempting to change or delete a nonexisting device
1466  **/
1467
1468 static int
1469 ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1470 {
1471         int err = 0;
1472         struct ip6_tnl_parm p;
1473         struct __ip6_tnl_parm p1;
1474         struct ip6_tnl *t = netdev_priv(dev);
1475         struct net *net = t->net;
1476         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1477
1478         switch (cmd) {
1479         case SIOCGETTUNNEL:
1480                 if (dev == ip6n->fb_tnl_dev) {
1481                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1482                                 err = -EFAULT;
1483                                 break;
1484                         }
1485                         ip6_tnl_parm_from_user(&p1, &p);
1486                         t = ip6_tnl_locate(net, &p1, 0);
1487                         if (IS_ERR(t))
1488                                 t = netdev_priv(dev);
1489                 } else {
1490                         memset(&p, 0, sizeof(p));
1491                 }
1492                 ip6_tnl_parm_to_user(&p, &t->parms);
1493                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
1494                         err = -EFAULT;
1495                 }
1496                 break;
1497         case SIOCADDTUNNEL:
1498         case SIOCCHGTUNNEL:
1499                 err = -EPERM;
1500                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1501                         break;
1502                 err = -EFAULT;
1503                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1504                         break;
1505                 err = -EINVAL;
1506                 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1507                     p.proto != 0)
1508                         break;
1509                 ip6_tnl_parm_from_user(&p1, &p);
1510                 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
1511                 if (cmd == SIOCCHGTUNNEL) {
1512                         if (!IS_ERR(t)) {
1513                                 if (t->dev != dev) {
1514                                         err = -EEXIST;
1515                                         break;
1516                                 }
1517                         } else
1518                                 t = netdev_priv(dev);
1519                         if (dev == ip6n->fb_tnl_dev)
1520                                 err = ip6_tnl0_update(t, &p1);
1521                         else
1522                                 err = ip6_tnl_update(t, &p1);
1523                 }
1524                 if (!IS_ERR(t)) {
1525                         err = 0;
1526                         ip6_tnl_parm_to_user(&p, &t->parms);
1527                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1528                                 err = -EFAULT;
1529
1530                 } else {
1531                         err = PTR_ERR(t);
1532                 }
1533                 break;
1534         case SIOCDELTUNNEL:
1535                 err = -EPERM;
1536                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1537                         break;
1538
1539                 if (dev == ip6n->fb_tnl_dev) {
1540                         err = -EFAULT;
1541                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1542                                 break;
1543                         err = -ENOENT;
1544                         ip6_tnl_parm_from_user(&p1, &p);
1545                         t = ip6_tnl_locate(net, &p1, 0);
1546                         if (IS_ERR(t))
1547                                 break;
1548                         err = -EPERM;
1549                         if (t->dev == ip6n->fb_tnl_dev)
1550                                 break;
1551                         dev = t->dev;
1552                 }
1553                 err = 0;
1554                 unregister_netdevice(dev);
1555                 break;
1556         default:
1557                 err = -EINVAL;
1558         }
1559         return err;
1560 }
1561
1562 /**
1563  * ip6_tnl_change_mtu - change mtu manually for tunnel device
1564  *   @dev: virtual device associated with tunnel
1565  *   @new_mtu: the new mtu
1566  *
1567  * Return:
1568  *   0 on success,
1569  *   %-EINVAL if mtu too small
1570  **/
1571
1572 static int
1573 ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1574 {
1575         struct ip6_tnl *tnl = netdev_priv(dev);
1576
1577         if (tnl->parms.proto == IPPROTO_IPIP) {
1578                 if (new_mtu < 68)
1579                         return -EINVAL;
1580         } else {
1581                 if (new_mtu < IPV6_MIN_MTU)
1582                         return -EINVAL;
1583         }
1584         if (new_mtu > 0xFFF8 - dev->hard_header_len)
1585                 return -EINVAL;
1586         dev->mtu = new_mtu;
1587         return 0;
1588 }
1589
1590 int ip6_tnl_get_iflink(const struct net_device *dev)
1591 {
1592         struct ip6_tnl *t = netdev_priv(dev);
1593
1594         return t->parms.link;
1595 }
1596 EXPORT_SYMBOL(ip6_tnl_get_iflink);
1597
1598 static const struct net_device_ops ip6_tnl_netdev_ops = {
1599         .ndo_init       = ip6_tnl_dev_init,
1600         .ndo_uninit     = ip6_tnl_dev_uninit,
1601         .ndo_start_xmit = ip6_tnl_xmit,
1602         .ndo_do_ioctl   = ip6_tnl_ioctl,
1603         .ndo_change_mtu = ip6_tnl_change_mtu,
1604         .ndo_get_stats  = ip6_get_stats,
1605         .ndo_get_iflink = ip6_tnl_get_iflink,
1606 };
1607
1608
1609 /**
1610  * ip6_tnl_dev_setup - setup virtual tunnel device
1611  *   @dev: virtual device associated with tunnel
1612  *
1613  * Description:
1614  *   Initialize function pointers and device parameters
1615  **/
1616
1617 static void ip6_tnl_dev_setup(struct net_device *dev)
1618 {
1619         struct ip6_tnl *t;
1620
1621         dev->netdev_ops = &ip6_tnl_netdev_ops;
1622         dev->destructor = ip6_dev_free;
1623
1624         dev->type = ARPHRD_TUNNEL6;
1625         dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
1626         dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr);
1627         t = netdev_priv(dev);
1628         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1629                 dev->mtu -= 8;
1630         dev->flags |= IFF_NOARP;
1631         dev->addr_len = sizeof(struct in6_addr);
1632         netif_keep_dst(dev);
1633         /* This perm addr will be used as interface identifier by IPv6 */
1634         dev->addr_assign_type = NET_ADDR_RANDOM;
1635         eth_random_addr(dev->perm_addr);
1636 }
1637
1638
1639 /**
1640  * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1641  *   @dev: virtual device associated with tunnel
1642  **/
1643
1644 static inline int
1645 ip6_tnl_dev_init_gen(struct net_device *dev)
1646 {
1647         struct ip6_tnl *t = netdev_priv(dev);
1648         int ret;
1649
1650         t->dev = dev;
1651         t->net = dev_net(dev);
1652         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1653         if (!dev->tstats)
1654                 return -ENOMEM;
1655
1656         ret = ip6_tnl_dst_init(t);
1657         if (ret) {
1658                 free_percpu(dev->tstats);
1659                 dev->tstats = NULL;
1660                 return ret;
1661         }
1662
1663         return 0;
1664 }
1665
1666 /**
1667  * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1668  *   @dev: virtual device associated with tunnel
1669  **/
1670
1671 static int ip6_tnl_dev_init(struct net_device *dev)
1672 {
1673         struct ip6_tnl *t = netdev_priv(dev);
1674         int err = ip6_tnl_dev_init_gen(dev);
1675
1676         if (err)
1677                 return err;
1678         ip6_tnl_link_config(t);
1679         return 0;
1680 }
1681
1682 /**
1683  * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1684  *   @dev: fallback device
1685  *
1686  * Return: 0
1687  **/
1688
1689 static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
1690 {
1691         struct ip6_tnl *t = netdev_priv(dev);
1692         struct net *net = dev_net(dev);
1693         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1694
1695         t->parms.proto = IPPROTO_IPV6;
1696         dev_hold(dev);
1697
1698         rcu_assign_pointer(ip6n->tnls_wc[0], t);
1699         return 0;
1700 }
1701
1702 static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1703 {
1704         u8 proto;
1705
1706         if (!data || !data[IFLA_IPTUN_PROTO])
1707                 return 0;
1708
1709         proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1710         if (proto != IPPROTO_IPV6 &&
1711             proto != IPPROTO_IPIP &&
1712             proto != 0)
1713                 return -EINVAL;
1714
1715         return 0;
1716 }
1717
1718 static void ip6_tnl_netlink_parms(struct nlattr *data[],
1719                                   struct __ip6_tnl_parm *parms)
1720 {
1721         memset(parms, 0, sizeof(*parms));
1722
1723         if (!data)
1724                 return;
1725
1726         if (data[IFLA_IPTUN_LINK])
1727                 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1728
1729         if (data[IFLA_IPTUN_LOCAL])
1730                 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1731
1732         if (data[IFLA_IPTUN_REMOTE])
1733                 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1734
1735         if (data[IFLA_IPTUN_TTL])
1736                 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1737
1738         if (data[IFLA_IPTUN_ENCAP_LIMIT])
1739                 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1740
1741         if (data[IFLA_IPTUN_FLOWINFO])
1742                 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
1743
1744         if (data[IFLA_IPTUN_FLAGS])
1745                 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1746
1747         if (data[IFLA_IPTUN_PROTO])
1748                 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1749 }
1750
1751 static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1752                            struct nlattr *tb[], struct nlattr *data[])
1753 {
1754         struct net *net = dev_net(dev);
1755         struct ip6_tnl *nt, *t;
1756
1757         nt = netdev_priv(dev);
1758         ip6_tnl_netlink_parms(data, &nt->parms);
1759
1760         t = ip6_tnl_locate(net, &nt->parms, 0);
1761         if (!IS_ERR(t))
1762                 return -EEXIST;
1763
1764         return ip6_tnl_create2(dev);
1765 }
1766
1767 static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1768                               struct nlattr *data[])
1769 {
1770         struct ip6_tnl *t = netdev_priv(dev);
1771         struct __ip6_tnl_parm p;
1772         struct net *net = t->net;
1773         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1774
1775         if (dev == ip6n->fb_tnl_dev)
1776                 return -EINVAL;
1777
1778         ip6_tnl_netlink_parms(data, &p);
1779
1780         t = ip6_tnl_locate(net, &p, 0);
1781         if (!IS_ERR(t)) {
1782                 if (t->dev != dev)
1783                         return -EEXIST;
1784         } else
1785                 t = netdev_priv(dev);
1786
1787         return ip6_tnl_update(t, &p);
1788 }
1789
1790 static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1791 {
1792         struct net *net = dev_net(dev);
1793         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1794
1795         if (dev != ip6n->fb_tnl_dev)
1796                 unregister_netdevice_queue(dev, head);
1797 }
1798
1799 static size_t ip6_tnl_get_size(const struct net_device *dev)
1800 {
1801         return
1802                 /* IFLA_IPTUN_LINK */
1803                 nla_total_size(4) +
1804                 /* IFLA_IPTUN_LOCAL */
1805                 nla_total_size(sizeof(struct in6_addr)) +
1806                 /* IFLA_IPTUN_REMOTE */
1807                 nla_total_size(sizeof(struct in6_addr)) +
1808                 /* IFLA_IPTUN_TTL */
1809                 nla_total_size(1) +
1810                 /* IFLA_IPTUN_ENCAP_LIMIT */
1811                 nla_total_size(1) +
1812                 /* IFLA_IPTUN_FLOWINFO */
1813                 nla_total_size(4) +
1814                 /* IFLA_IPTUN_FLAGS */
1815                 nla_total_size(4) +
1816                 /* IFLA_IPTUN_PROTO */
1817                 nla_total_size(1) +
1818                 0;
1819 }
1820
1821 static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1822 {
1823         struct ip6_tnl *tunnel = netdev_priv(dev);
1824         struct __ip6_tnl_parm *parm = &tunnel->parms;
1825
1826         if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1827             nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1828             nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1829             nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1830             nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1831             nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
1832             nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1833             nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
1834                 goto nla_put_failure;
1835         return 0;
1836
1837 nla_put_failure:
1838         return -EMSGSIZE;
1839 }
1840
1841 struct net *ip6_tnl_get_link_net(const struct net_device *dev)
1842 {
1843         struct ip6_tnl *tunnel = netdev_priv(dev);
1844
1845         return tunnel->net;
1846 }
1847 EXPORT_SYMBOL(ip6_tnl_get_link_net);
1848
1849 static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1850         [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
1851         [IFLA_IPTUN_LOCAL]              = { .len = sizeof(struct in6_addr) },
1852         [IFLA_IPTUN_REMOTE]             = { .len = sizeof(struct in6_addr) },
1853         [IFLA_IPTUN_TTL]                = { .type = NLA_U8 },
1854         [IFLA_IPTUN_ENCAP_LIMIT]        = { .type = NLA_U8 },
1855         [IFLA_IPTUN_FLOWINFO]           = { .type = NLA_U32 },
1856         [IFLA_IPTUN_FLAGS]              = { .type = NLA_U32 },
1857         [IFLA_IPTUN_PROTO]              = { .type = NLA_U8 },
1858 };
1859
1860 static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1861         .kind           = "ip6tnl",
1862         .maxtype        = IFLA_IPTUN_MAX,
1863         .policy         = ip6_tnl_policy,
1864         .priv_size      = sizeof(struct ip6_tnl),
1865         .setup          = ip6_tnl_dev_setup,
1866         .validate       = ip6_tnl_validate,
1867         .newlink        = ip6_tnl_newlink,
1868         .changelink     = ip6_tnl_changelink,
1869         .dellink        = ip6_tnl_dellink,
1870         .get_size       = ip6_tnl_get_size,
1871         .fill_info      = ip6_tnl_fill_info,
1872         .get_link_net   = ip6_tnl_get_link_net,
1873 };
1874
1875 static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
1876         .handler        = ip4ip6_rcv,
1877         .err_handler    = ip4ip6_err,
1878         .priority       =       1,
1879 };
1880
1881 static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1882         .handler        = ip6ip6_rcv,
1883         .err_handler    = ip6ip6_err,
1884         .priority       =       1,
1885 };
1886
1887 static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1888 {
1889         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1890         struct net_device *dev, *aux;
1891         int h;
1892         struct ip6_tnl *t;
1893         LIST_HEAD(list);
1894
1895         for_each_netdev_safe(net, dev, aux)
1896                 if (dev->rtnl_link_ops == &ip6_link_ops)
1897                         unregister_netdevice_queue(dev, &list);
1898
1899         for (h = 0; h < HASH_SIZE; h++) {
1900                 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1901                 while (t) {
1902                         /* If dev is in the same netns, it has already
1903                          * been added to the list by the previous loop.
1904                          */
1905                         if (!net_eq(dev_net(t->dev), net))
1906                                 unregister_netdevice_queue(t->dev, &list);
1907                         t = rtnl_dereference(t->next);
1908                 }
1909         }
1910
1911         unregister_netdevice_many(&list);
1912 }
1913
1914 static int __net_init ip6_tnl_init_net(struct net *net)
1915 {
1916         struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1917         struct ip6_tnl *t = NULL;
1918         int err;
1919
1920         ip6n->tnls[0] = ip6n->tnls_wc;
1921         ip6n->tnls[1] = ip6n->tnls_r_l;
1922
1923         err = -ENOMEM;
1924         ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1925                                         NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
1926
1927         if (!ip6n->fb_tnl_dev)
1928                 goto err_alloc_dev;
1929         dev_net_set(ip6n->fb_tnl_dev, net);
1930         ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
1931         /* FB netdevice is special: we have one, and only one per netns.
1932          * Allowing to move it to another netns is clearly unsafe.
1933          */
1934         ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
1935
1936         err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1937         if (err < 0)
1938                 goto err_register;
1939
1940         err = register_netdev(ip6n->fb_tnl_dev);
1941         if (err < 0)
1942                 goto err_register;
1943
1944         t = netdev_priv(ip6n->fb_tnl_dev);
1945
1946         strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1947         return 0;
1948
1949 err_register:
1950         ip6_dev_free(ip6n->fb_tnl_dev);
1951 err_alloc_dev:
1952         return err;
1953 }
1954
1955 static void __net_exit ip6_tnl_exit_net(struct net *net)
1956 {
1957         rtnl_lock();
1958         ip6_tnl_destroy_tunnels(net);
1959         rtnl_unlock();
1960 }
1961
1962 static struct pernet_operations ip6_tnl_net_ops = {
1963         .init = ip6_tnl_init_net,
1964         .exit = ip6_tnl_exit_net,
1965         .id   = &ip6_tnl_net_id,
1966         .size = sizeof(struct ip6_tnl_net),
1967 };
1968
1969 /**
1970  * ip6_tunnel_init - register protocol and reserve needed resources
1971  *
1972  * Return: 0 on success
1973  **/
1974
1975 static int __init ip6_tunnel_init(void)
1976 {
1977         int  err;
1978
1979         err = register_pernet_device(&ip6_tnl_net_ops);
1980         if (err < 0)
1981                 goto out_pernet;
1982
1983         err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1984         if (err < 0) {
1985                 pr_err("%s: can't register ip4ip6\n", __func__);
1986                 goto out_ip4ip6;
1987         }
1988
1989         err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1990         if (err < 0) {
1991                 pr_err("%s: can't register ip6ip6\n", __func__);
1992                 goto out_ip6ip6;
1993         }
1994         err = rtnl_link_register(&ip6_link_ops);
1995         if (err < 0)
1996                 goto rtnl_link_failed;
1997
1998         return 0;
1999
2000 rtnl_link_failed:
2001         xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
2002 out_ip6ip6:
2003         xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
2004 out_ip4ip6:
2005         unregister_pernet_device(&ip6_tnl_net_ops);
2006 out_pernet:
2007         return err;
2008 }
2009
2010 /**
2011  * ip6_tunnel_cleanup - free resources and unregister protocol
2012  **/
2013
2014 static void __exit ip6_tunnel_cleanup(void)
2015 {
2016         rtnl_link_unregister(&ip6_link_ops);
2017         if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
2018                 pr_info("%s: can't deregister ip4ip6\n", __func__);
2019
2020         if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
2021                 pr_info("%s: can't deregister ip6ip6\n", __func__);
2022
2023         unregister_pernet_device(&ip6_tnl_net_ops);
2024 }
2025
2026 module_init(ip6_tunnel_init);
2027 module_exit(ip6_tunnel_cleanup);