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

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

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

~ [ 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