~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/ip6_tunnel.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp