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