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