GNU Linux-libre 5.15.137-gnu
[releases.git] / net / ipv6 / ip6_gre.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      GRE over IPv6 protocol decoder.
4  *
5  *      Authors: Dmitry Kozlov (xeb@mail.ru)
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/in.h>
19 #include <linux/tcp.h>
20 #include <linux/udp.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <linux/in6.h>
24 #include <linux/inetdevice.h>
25 #include <linux/igmp.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/etherdevice.h>
28 #include <linux/if_ether.h>
29 #include <linux/hash.h>
30 #include <linux/if_tunnel.h>
31 #include <linux/ip6_tunnel.h>
32
33 #include <net/sock.h>
34 #include <net/ip.h>
35 #include <net/ip_tunnels.h>
36 #include <net/icmp.h>
37 #include <net/protocol.h>
38 #include <net/addrconf.h>
39 #include <net/arp.h>
40 #include <net/checksum.h>
41 #include <net/dsfield.h>
42 #include <net/inet_ecn.h>
43 #include <net/xfrm.h>
44 #include <net/net_namespace.h>
45 #include <net/netns/generic.h>
46 #include <net/rtnetlink.h>
47
48 #include <net/ipv6.h>
49 #include <net/ip6_fib.h>
50 #include <net/ip6_route.h>
51 #include <net/ip6_tunnel.h>
52 #include <net/gre.h>
53 #include <net/erspan.h>
54 #include <net/dst_metadata.h>
55
56
57 static bool log_ecn_error = true;
58 module_param(log_ecn_error, bool, 0644);
59 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
60
61 #define IP6_GRE_HASH_SIZE_SHIFT  5
62 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
63
64 static unsigned int ip6gre_net_id __read_mostly;
65 struct ip6gre_net {
66         struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
67
68         struct ip6_tnl __rcu *collect_md_tun;
69         struct ip6_tnl __rcu *collect_md_tun_erspan;
70         struct net_device *fb_tunnel_dev;
71 };
72
73 static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
74 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
75 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
76 static int ip6gre_tunnel_init(struct net_device *dev);
77 static void ip6gre_tunnel_setup(struct net_device *dev);
78 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
79 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
80 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
81
82 /* Tunnel hash table */
83
84 /*
85    4 hash tables:
86
87    3: (remote,local)
88    2: (remote,*)
89    1: (*,local)
90    0: (*,*)
91
92    We require exact key match i.e. if a key is present in packet
93    it will match only tunnel with the same key; if it is not present,
94    it will match only keyless tunnel.
95
96    All keysless packets, if not matched configured keyless tunnels
97    will match fallback tunnel.
98  */
99
100 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
101 static u32 HASH_ADDR(const struct in6_addr *addr)
102 {
103         u32 hash = ipv6_addr_hash(addr);
104
105         return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
106 }
107
108 #define tunnels_r_l     tunnels[3]
109 #define tunnels_r       tunnels[2]
110 #define tunnels_l       tunnels[1]
111 #define tunnels_wc      tunnels[0]
112
113 /* Given src, dst and key, find appropriate for input tunnel. */
114
115 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
116                 const struct in6_addr *remote, const struct in6_addr *local,
117                 __be32 key, __be16 gre_proto)
118 {
119         struct net *net = dev_net(dev);
120         int link = dev->ifindex;
121         unsigned int h0 = HASH_ADDR(remote);
122         unsigned int h1 = HASH_KEY(key);
123         struct ip6_tnl *t, *cand = NULL;
124         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
125         int dev_type = (gre_proto == htons(ETH_P_TEB) ||
126                         gre_proto == htons(ETH_P_ERSPAN) ||
127                         gre_proto == htons(ETH_P_ERSPAN2)) ?
128                        ARPHRD_ETHER : ARPHRD_IP6GRE;
129         int score, cand_score = 4;
130         struct net_device *ndev;
131
132         for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
133                 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
134                     !ipv6_addr_equal(remote, &t->parms.raddr) ||
135                     key != t->parms.i_key ||
136                     !(t->dev->flags & IFF_UP))
137                         continue;
138
139                 if (t->dev->type != ARPHRD_IP6GRE &&
140                     t->dev->type != dev_type)
141                         continue;
142
143                 score = 0;
144                 if (t->parms.link != link)
145                         score |= 1;
146                 if (t->dev->type != dev_type)
147                         score |= 2;
148                 if (score == 0)
149                         return t;
150
151                 if (score < cand_score) {
152                         cand = t;
153                         cand_score = score;
154                 }
155         }
156
157         for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
158                 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
159                     key != t->parms.i_key ||
160                     !(t->dev->flags & IFF_UP))
161                         continue;
162
163                 if (t->dev->type != ARPHRD_IP6GRE &&
164                     t->dev->type != dev_type)
165                         continue;
166
167                 score = 0;
168                 if (t->parms.link != link)
169                         score |= 1;
170                 if (t->dev->type != dev_type)
171                         score |= 2;
172                 if (score == 0)
173                         return t;
174
175                 if (score < cand_score) {
176                         cand = t;
177                         cand_score = score;
178                 }
179         }
180
181         for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
182                 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
183                           (!ipv6_addr_equal(local, &t->parms.raddr) ||
184                                  !ipv6_addr_is_multicast(local))) ||
185                     key != t->parms.i_key ||
186                     !(t->dev->flags & IFF_UP))
187                         continue;
188
189                 if (t->dev->type != ARPHRD_IP6GRE &&
190                     t->dev->type != dev_type)
191                         continue;
192
193                 score = 0;
194                 if (t->parms.link != link)
195                         score |= 1;
196                 if (t->dev->type != dev_type)
197                         score |= 2;
198                 if (score == 0)
199                         return t;
200
201                 if (score < cand_score) {
202                         cand = t;
203                         cand_score = score;
204                 }
205         }
206
207         for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
208                 if (t->parms.i_key != key ||
209                     !(t->dev->flags & IFF_UP))
210                         continue;
211
212                 if (t->dev->type != ARPHRD_IP6GRE &&
213                     t->dev->type != dev_type)
214                         continue;
215
216                 score = 0;
217                 if (t->parms.link != link)
218                         score |= 1;
219                 if (t->dev->type != dev_type)
220                         score |= 2;
221                 if (score == 0)
222                         return t;
223
224                 if (score < cand_score) {
225                         cand = t;
226                         cand_score = score;
227                 }
228         }
229
230         if (cand)
231                 return cand;
232
233         if (gre_proto == htons(ETH_P_ERSPAN) ||
234             gre_proto == htons(ETH_P_ERSPAN2))
235                 t = rcu_dereference(ign->collect_md_tun_erspan);
236         else
237                 t = rcu_dereference(ign->collect_md_tun);
238
239         if (t && t->dev->flags & IFF_UP)
240                 return t;
241
242         ndev = READ_ONCE(ign->fb_tunnel_dev);
243         if (ndev && ndev->flags & IFF_UP)
244                 return netdev_priv(ndev);
245
246         return NULL;
247 }
248
249 static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
250                 const struct __ip6_tnl_parm *p)
251 {
252         const struct in6_addr *remote = &p->raddr;
253         const struct in6_addr *local = &p->laddr;
254         unsigned int h = HASH_KEY(p->i_key);
255         int prio = 0;
256
257         if (!ipv6_addr_any(local))
258                 prio |= 1;
259         if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
260                 prio |= 2;
261                 h ^= HASH_ADDR(remote);
262         }
263
264         return &ign->tunnels[prio][h];
265 }
266
267 static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
268 {
269         if (t->parms.collect_md)
270                 rcu_assign_pointer(ign->collect_md_tun, t);
271 }
272
273 static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
274 {
275         if (t->parms.collect_md)
276                 rcu_assign_pointer(ign->collect_md_tun_erspan, t);
277 }
278
279 static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
280 {
281         if (t->parms.collect_md)
282                 rcu_assign_pointer(ign->collect_md_tun, NULL);
283 }
284
285 static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
286                                        struct ip6_tnl *t)
287 {
288         if (t->parms.collect_md)
289                 rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
290 }
291
292 static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
293                 const struct ip6_tnl *t)
294 {
295         return __ip6gre_bucket(ign, &t->parms);
296 }
297
298 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
299 {
300         struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
301
302         rcu_assign_pointer(t->next, rtnl_dereference(*tp));
303         rcu_assign_pointer(*tp, t);
304 }
305
306 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
307 {
308         struct ip6_tnl __rcu **tp;
309         struct ip6_tnl *iter;
310
311         for (tp = ip6gre_bucket(ign, t);
312              (iter = rtnl_dereference(*tp)) != NULL;
313              tp = &iter->next) {
314                 if (t == iter) {
315                         rcu_assign_pointer(*tp, t->next);
316                         break;
317                 }
318         }
319 }
320
321 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
322                                            const struct __ip6_tnl_parm *parms,
323                                            int type)
324 {
325         const struct in6_addr *remote = &parms->raddr;
326         const struct in6_addr *local = &parms->laddr;
327         __be32 key = parms->i_key;
328         int link = parms->link;
329         struct ip6_tnl *t;
330         struct ip6_tnl __rcu **tp;
331         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
332
333         for (tp = __ip6gre_bucket(ign, parms);
334              (t = rtnl_dereference(*tp)) != NULL;
335              tp = &t->next)
336                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
337                     ipv6_addr_equal(remote, &t->parms.raddr) &&
338                     key == t->parms.i_key &&
339                     link == t->parms.link &&
340                     type == t->dev->type)
341                         break;
342
343         return t;
344 }
345
346 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
347                 const struct __ip6_tnl_parm *parms, int create)
348 {
349         struct ip6_tnl *t, *nt;
350         struct net_device *dev;
351         char name[IFNAMSIZ];
352         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
353
354         t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
355         if (t && create)
356                 return NULL;
357         if (t || !create)
358                 return t;
359
360         if (parms->name[0]) {
361                 if (!dev_valid_name(parms->name))
362                         return NULL;
363                 strlcpy(name, parms->name, IFNAMSIZ);
364         } else {
365                 strcpy(name, "ip6gre%d");
366         }
367         dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
368                            ip6gre_tunnel_setup);
369         if (!dev)
370                 return NULL;
371
372         dev_net_set(dev, net);
373
374         nt = netdev_priv(dev);
375         nt->parms = *parms;
376         dev->rtnl_link_ops = &ip6gre_link_ops;
377
378         nt->dev = dev;
379         nt->net = dev_net(dev);
380
381         if (register_netdevice(dev) < 0)
382                 goto failed_free;
383
384         ip6gre_tnl_link_config(nt, 1);
385
386         /* Can use a lockless transmit, unless we generate output sequences */
387         if (!(nt->parms.o_flags & TUNNEL_SEQ))
388                 dev->features |= NETIF_F_LLTX;
389
390         ip6gre_tunnel_link(ign, nt);
391         return nt;
392
393 failed_free:
394         free_netdev(dev);
395         return NULL;
396 }
397
398 static void ip6erspan_tunnel_uninit(struct net_device *dev)
399 {
400         struct ip6_tnl *t = netdev_priv(dev);
401         struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
402
403         ip6erspan_tunnel_unlink_md(ign, t);
404         ip6gre_tunnel_unlink(ign, t);
405         dst_cache_reset(&t->dst_cache);
406         dev_put(dev);
407 }
408
409 static void ip6gre_tunnel_uninit(struct net_device *dev)
410 {
411         struct ip6_tnl *t = netdev_priv(dev);
412         struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
413
414         ip6gre_tunnel_unlink_md(ign, t);
415         ip6gre_tunnel_unlink(ign, t);
416         if (ign->fb_tunnel_dev == dev)
417                 WRITE_ONCE(ign->fb_tunnel_dev, NULL);
418         dst_cache_reset(&t->dst_cache);
419         dev_put(dev);
420 }
421
422
423 static int ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
424                        u8 type, u8 code, int offset, __be32 info)
425 {
426         struct net *net = dev_net(skb->dev);
427         const struct ipv6hdr *ipv6h;
428         struct tnl_ptk_info tpi;
429         struct ip6_tnl *t;
430
431         if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IPV6),
432                              offset) < 0)
433                 return -EINVAL;
434
435         ipv6h = (const struct ipv6hdr *)skb->data;
436         t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
437                                  tpi.key, tpi.proto);
438         if (!t)
439                 return -ENOENT;
440
441         switch (type) {
442         case ICMPV6_DEST_UNREACH:
443                 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
444                                     t->parms.name);
445                 if (code != ICMPV6_PORT_UNREACH)
446                         break;
447                 return 0;
448         case ICMPV6_TIME_EXCEED:
449                 if (code == ICMPV6_EXC_HOPLIMIT) {
450                         net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
451                                             t->parms.name);
452                         break;
453                 }
454                 return 0;
455         case ICMPV6_PARAMPROB: {
456                 struct ipv6_tlv_tnl_enc_lim *tel;
457                 __u32 teli;
458
459                 teli = 0;
460                 if (code == ICMPV6_HDR_FIELD)
461                         teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
462
463                 if (teli && teli == be32_to_cpu(info) - 2) {
464                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
465                         if (tel->encap_limit == 0) {
466                                 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
467                                                     t->parms.name);
468                         }
469                 } else {
470                         net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
471                                             t->parms.name);
472                 }
473                 return 0;
474         }
475         case ICMPV6_PKT_TOOBIG:
476                 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
477                 return 0;
478         case NDISC_REDIRECT:
479                 ip6_redirect(skb, net, skb->dev->ifindex, 0,
480                              sock_net_uid(net, NULL));
481                 return 0;
482         }
483
484         if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
485                 t->err_count++;
486         else
487                 t->err_count = 1;
488         t->err_time = jiffies;
489
490         return 0;
491 }
492
493 static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
494 {
495         const struct ipv6hdr *ipv6h;
496         struct ip6_tnl *tunnel;
497
498         ipv6h = ipv6_hdr(skb);
499         tunnel = ip6gre_tunnel_lookup(skb->dev,
500                                       &ipv6h->saddr, &ipv6h->daddr, tpi->key,
501                                       tpi->proto);
502         if (tunnel) {
503                 if (tunnel->parms.collect_md) {
504                         struct metadata_dst *tun_dst;
505                         __be64 tun_id;
506                         __be16 flags;
507
508                         flags = tpi->flags;
509                         tun_id = key32_to_tunnel_id(tpi->key);
510
511                         tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
512                         if (!tun_dst)
513                                 return PACKET_REJECT;
514
515                         ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
516                 } else {
517                         ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
518                 }
519
520                 return PACKET_RCVD;
521         }
522
523         return PACKET_REJECT;
524 }
525
526 static int ip6erspan_rcv(struct sk_buff *skb,
527                          struct tnl_ptk_info *tpi,
528                          int gre_hdr_len)
529 {
530         struct erspan_base_hdr *ershdr;
531         const struct ipv6hdr *ipv6h;
532         struct erspan_md2 *md2;
533         struct ip6_tnl *tunnel;
534         u8 ver;
535
536         ipv6h = ipv6_hdr(skb);
537         ershdr = (struct erspan_base_hdr *)skb->data;
538         ver = ershdr->ver;
539
540         tunnel = ip6gre_tunnel_lookup(skb->dev,
541                                       &ipv6h->saddr, &ipv6h->daddr, tpi->key,
542                                       tpi->proto);
543         if (tunnel) {
544                 int len = erspan_hdr_len(ver);
545
546                 if (unlikely(!pskb_may_pull(skb, len)))
547                         return PACKET_REJECT;
548
549                 if (__iptunnel_pull_header(skb, len,
550                                            htons(ETH_P_TEB),
551                                            false, false) < 0)
552                         return PACKET_REJECT;
553
554                 if (tunnel->parms.collect_md) {
555                         struct erspan_metadata *pkt_md, *md;
556                         struct metadata_dst *tun_dst;
557                         struct ip_tunnel_info *info;
558                         unsigned char *gh;
559                         __be64 tun_id;
560                         __be16 flags;
561
562                         tpi->flags |= TUNNEL_KEY;
563                         flags = tpi->flags;
564                         tun_id = key32_to_tunnel_id(tpi->key);
565
566                         tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
567                                                   sizeof(*md));
568                         if (!tun_dst)
569                                 return PACKET_REJECT;
570
571                         /* skb can be uncloned in __iptunnel_pull_header, so
572                          * old pkt_md is no longer valid and we need to reset
573                          * it
574                          */
575                         gh = skb_network_header(skb) +
576                              skb_network_header_len(skb);
577                         pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
578                                                             sizeof(*ershdr));
579                         info = &tun_dst->u.tun_info;
580                         md = ip_tunnel_info_opts(info);
581                         md->version = ver;
582                         md2 = &md->u.md2;
583                         memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
584                                                        ERSPAN_V2_MDSIZE);
585                         info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
586                         info->options_len = sizeof(*md);
587
588                         ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
589
590                 } else {
591                         ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
592                 }
593
594                 return PACKET_RCVD;
595         }
596
597         return PACKET_REJECT;
598 }
599
600 static int gre_rcv(struct sk_buff *skb)
601 {
602         struct tnl_ptk_info tpi;
603         bool csum_err = false;
604         int hdr_len;
605
606         hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
607         if (hdr_len < 0)
608                 goto drop;
609
610         if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
611                 goto drop;
612
613         if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
614                      tpi.proto == htons(ETH_P_ERSPAN2))) {
615                 if (ip6erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
616                         return 0;
617                 goto out;
618         }
619
620         if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
621                 return 0;
622
623 out:
624         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
625 drop:
626         kfree_skb(skb);
627         return 0;
628 }
629
630 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
631 {
632         return iptunnel_handle_offloads(skb,
633                                         csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
634 }
635
636 static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
637                                      struct net_device *dev,
638                                      struct flowi6 *fl6, __u8 *dsfield,
639                                      int *encap_limit)
640 {
641         const struct iphdr *iph = ip_hdr(skb);
642         struct ip6_tnl *t = netdev_priv(dev);
643
644         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
645                 *encap_limit = t->parms.encap_limit;
646
647         memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
648
649         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
650                 *dsfield = ipv4_get_dsfield(iph);
651         else
652                 *dsfield = ip6_tclass(t->parms.flowinfo);
653
654         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
655                 fl6->flowi6_mark = skb->mark;
656         else
657                 fl6->flowi6_mark = t->parms.fwmark;
658
659         fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
660 }
661
662 static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
663                                     struct net_device *dev,
664                                     struct flowi6 *fl6, __u8 *dsfield,
665                                     int *encap_limit)
666 {
667         struct ipv6hdr *ipv6h;
668         struct ip6_tnl *t = netdev_priv(dev);
669         __u16 offset;
670
671         offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
672         /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
673         ipv6h = ipv6_hdr(skb);
674
675         if (offset > 0) {
676                 struct ipv6_tlv_tnl_enc_lim *tel;
677
678                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
679                 if (tel->encap_limit == 0) {
680                         icmpv6_ndo_send(skb, ICMPV6_PARAMPROB,
681                                         ICMPV6_HDR_FIELD, offset + 2);
682                         return -1;
683                 }
684                 *encap_limit = tel->encap_limit - 1;
685         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
686                 *encap_limit = t->parms.encap_limit;
687         }
688
689         memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
690
691         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
692                 *dsfield = ipv6_get_dsfield(ipv6h);
693         else
694                 *dsfield = ip6_tclass(t->parms.flowinfo);
695
696         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
697                 fl6->flowlabel |= ip6_flowlabel(ipv6h);
698
699         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
700                 fl6->flowi6_mark = skb->mark;
701         else
702                 fl6->flowi6_mark = t->parms.fwmark;
703
704         fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
705
706         return 0;
707 }
708
709 static struct ip_tunnel_info *skb_tunnel_info_txcheck(struct sk_buff *skb)
710 {
711         struct ip_tunnel_info *tun_info;
712
713         tun_info = skb_tunnel_info(skb);
714         if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX)))
715                 return ERR_PTR(-EINVAL);
716
717         return tun_info;
718 }
719
720 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
721                                struct net_device *dev, __u8 dsfield,
722                                struct flowi6 *fl6, int encap_limit,
723                                __u32 *pmtu, __be16 proto)
724 {
725         struct ip6_tnl *tunnel = netdev_priv(dev);
726         __be16 protocol;
727         __be16 flags;
728
729         if (dev->type == ARPHRD_ETHER)
730                 IPCB(skb)->flags = 0;
731
732         if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
733                 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
734         else
735                 fl6->daddr = tunnel->parms.raddr;
736
737         /* Push GRE header. */
738         protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
739
740         if (tunnel->parms.collect_md) {
741                 struct ip_tunnel_info *tun_info;
742                 const struct ip_tunnel_key *key;
743                 int tun_hlen;
744
745                 tun_info = skb_tunnel_info_txcheck(skb);
746                 if (IS_ERR(tun_info) ||
747                     unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
748                         return -EINVAL;
749
750                 key = &tun_info->key;
751                 memset(fl6, 0, sizeof(*fl6));
752                 fl6->flowi6_proto = IPPROTO_GRE;
753                 fl6->daddr = key->u.ipv6.dst;
754                 fl6->flowlabel = key->label;
755                 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
756                 fl6->fl6_gre_key = tunnel_id_to_key32(key->tun_id);
757
758                 dsfield = key->tos;
759                 flags = key->tun_flags &
760                         (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
761                 tun_hlen = gre_calc_hlen(flags);
762
763                 if (skb_cow_head(skb, dev->needed_headroom ?: tun_hlen + tunnel->encap_hlen))
764                         return -ENOMEM;
765
766                 gre_build_header(skb, tun_hlen,
767                                  flags, protocol,
768                                  tunnel_id_to_key32(tun_info->key.tun_id),
769                                  (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno))
770                                                       : 0);
771
772         } else {
773                 if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
774                         return -ENOMEM;
775
776                 flags = tunnel->parms.o_flags;
777
778                 gre_build_header(skb, tunnel->tun_hlen, flags,
779                                  protocol, tunnel->parms.o_key,
780                                  (flags & TUNNEL_SEQ) ? htonl(atomic_fetch_inc(&tunnel->o_seqno))
781                                                       : 0);
782         }
783
784         return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
785                             NEXTHDR_GRE);
786 }
787
788 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
789 {
790         struct ip6_tnl *t = netdev_priv(dev);
791         int encap_limit = -1;
792         struct flowi6 fl6;
793         __u8 dsfield = 0;
794         __u32 mtu;
795         int err;
796
797         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
798
799         if (!t->parms.collect_md)
800                 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
801                                          &dsfield, &encap_limit);
802
803         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
804         if (err)
805                 return -1;
806
807         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
808                           skb->protocol);
809         if (err != 0) {
810                 /* XXX: send ICMP error even if DF is not set. */
811                 if (err == -EMSGSIZE)
812                         icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
813                                       htonl(mtu));
814                 return -1;
815         }
816
817         return 0;
818 }
819
820 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
821 {
822         struct ip6_tnl *t = netdev_priv(dev);
823         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
824         int encap_limit = -1;
825         struct flowi6 fl6;
826         __u8 dsfield = 0;
827         __u32 mtu;
828         int err;
829
830         if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
831                 return -1;
832
833         if (!t->parms.collect_md &&
834             prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
835                 return -1;
836
837         if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
838                 return -1;
839
840         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
841                           &mtu, skb->protocol);
842         if (err != 0) {
843                 if (err == -EMSGSIZE)
844                         icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
845                 return -1;
846         }
847
848         return 0;
849 }
850
851 /**
852  * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own
853  *   @t: the outgoing tunnel device
854  *   @hdr: IPv6 header from the incoming packet
855  *
856  * Description:
857  *   Avoid trivial tunneling loop by checking that tunnel exit-point
858  *   doesn't match source of incoming packet.
859  *
860  * Return:
861  *   1 if conflict,
862  *   0 else
863  **/
864
865 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
866         const struct ipv6hdr *hdr)
867 {
868         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
869 }
870
871 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
872 {
873         struct ip6_tnl *t = netdev_priv(dev);
874         int encap_limit = -1;
875         struct flowi6 fl6;
876         __u32 mtu;
877         int err;
878
879         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
880                 encap_limit = t->parms.encap_limit;
881
882         if (!t->parms.collect_md)
883                 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
884
885         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
886         if (err)
887                 return err;
888
889         err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
890
891         return err;
892 }
893
894 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
895         struct net_device *dev)
896 {
897         struct ip6_tnl *t = netdev_priv(dev);
898         struct net_device_stats *stats = &t->dev->stats;
899         int ret;
900
901         if (!pskb_inet_may_pull(skb))
902                 goto tx_err;
903
904         if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
905                 goto tx_err;
906
907         switch (skb->protocol) {
908         case htons(ETH_P_IP):
909                 ret = ip6gre_xmit_ipv4(skb, dev);
910                 break;
911         case htons(ETH_P_IPV6):
912                 ret = ip6gre_xmit_ipv6(skb, dev);
913                 break;
914         default:
915                 ret = ip6gre_xmit_other(skb, dev);
916                 break;
917         }
918
919         if (ret < 0)
920                 goto tx_err;
921
922         return NETDEV_TX_OK;
923
924 tx_err:
925         if (!t->parms.collect_md || !IS_ERR(skb_tunnel_info_txcheck(skb)))
926                 stats->tx_errors++;
927         stats->tx_dropped++;
928         kfree_skb(skb);
929         return NETDEV_TX_OK;
930 }
931
932 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
933                                          struct net_device *dev)
934 {
935         struct ip_tunnel_info *tun_info = NULL;
936         struct ip6_tnl *t = netdev_priv(dev);
937         struct dst_entry *dst = skb_dst(skb);
938         struct net_device_stats *stats;
939         bool truncate = false;
940         int encap_limit = -1;
941         __u8 dsfield = false;
942         struct flowi6 fl6;
943         int err = -EINVAL;
944         __be16 proto;
945         __u32 mtu;
946         int nhoff;
947
948         if (!pskb_inet_may_pull(skb))
949                 goto tx_err;
950
951         if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
952                 goto tx_err;
953
954         if (gre_handle_offloads(skb, false))
955                 goto tx_err;
956
957         if (skb->len > dev->mtu + dev->hard_header_len) {
958                 if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
959                         goto tx_err;
960                 truncate = true;
961         }
962
963         nhoff = skb_network_offset(skb);
964         if (skb->protocol == htons(ETH_P_IP) &&
965             (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
966                 truncate = true;
967
968         if (skb->protocol == htons(ETH_P_IPV6)) {
969                 int thoff;
970
971                 if (skb_transport_header_was_set(skb))
972                         thoff = skb_transport_offset(skb);
973                 else
974                         thoff = nhoff + sizeof(struct ipv6hdr);
975                 if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
976                         truncate = true;
977         }
978
979         if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
980                 goto tx_err;
981
982         t->parms.o_flags &= ~TUNNEL_KEY;
983         IPCB(skb)->flags = 0;
984
985         /* For collect_md mode, derive fl6 from the tunnel key,
986          * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
987          */
988         if (t->parms.collect_md) {
989                 const struct ip_tunnel_key *key;
990                 struct erspan_metadata *md;
991                 __be32 tun_id;
992
993                 tun_info = skb_tunnel_info_txcheck(skb);
994                 if (IS_ERR(tun_info) ||
995                     unlikely(ip_tunnel_info_af(tun_info) != AF_INET6))
996                         goto tx_err;
997
998                 key = &tun_info->key;
999                 memset(&fl6, 0, sizeof(fl6));
1000                 fl6.flowi6_proto = IPPROTO_GRE;
1001                 fl6.daddr = key->u.ipv6.dst;
1002                 fl6.flowlabel = key->label;
1003                 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1004                 fl6.fl6_gre_key = tunnel_id_to_key32(key->tun_id);
1005
1006                 dsfield = key->tos;
1007                 if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
1008                         goto tx_err;
1009                 if (tun_info->options_len < sizeof(*md))
1010                         goto tx_err;
1011                 md = ip_tunnel_info_opts(tun_info);
1012
1013                 tun_id = tunnel_id_to_key32(key->tun_id);
1014                 if (md->version == 1) {
1015                         erspan_build_header(skb,
1016                                             ntohl(tun_id),
1017                                             ntohl(md->u.index), truncate,
1018                                             false);
1019                         proto = htons(ETH_P_ERSPAN);
1020                 } else if (md->version == 2) {
1021                         erspan_build_header_v2(skb,
1022                                                ntohl(tun_id),
1023                                                md->u.md2.dir,
1024                                                get_hwid(&md->u.md2),
1025                                                truncate, false);
1026                         proto = htons(ETH_P_ERSPAN2);
1027                 } else {
1028                         goto tx_err;
1029                 }
1030         } else {
1031                 switch (skb->protocol) {
1032                 case htons(ETH_P_IP):
1033                         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1034                         prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
1035                                                  &dsfield, &encap_limit);
1036                         break;
1037                 case htons(ETH_P_IPV6):
1038                         if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
1039                                 goto tx_err;
1040                         if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
1041                                                      &dsfield, &encap_limit))
1042                                 goto tx_err;
1043                         break;
1044                 default:
1045                         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
1046                         break;
1047                 }
1048
1049                 if (t->parms.erspan_ver == 1) {
1050                         erspan_build_header(skb, ntohl(t->parms.o_key),
1051                                             t->parms.index,
1052                                             truncate, false);
1053                         proto = htons(ETH_P_ERSPAN);
1054                 } else if (t->parms.erspan_ver == 2) {
1055                         erspan_build_header_v2(skb, ntohl(t->parms.o_key),
1056                                                t->parms.dir,
1057                                                t->parms.hwid,
1058                                                truncate, false);
1059                         proto = htons(ETH_P_ERSPAN2);
1060                 } else {
1061                         goto tx_err;
1062                 }
1063
1064                 fl6.daddr = t->parms.raddr;
1065         }
1066
1067         /* Push GRE header. */
1068         gre_build_header(skb, 8, TUNNEL_SEQ, proto, 0, htonl(atomic_fetch_inc(&t->o_seqno)));
1069
1070         /* TooBig packet may have updated dst->dev's mtu */
1071         if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
1072                 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu, false);
1073
1074         err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
1075                            NEXTHDR_GRE);
1076         if (err != 0) {
1077                 /* XXX: send ICMP error even if DF is not set. */
1078                 if (err == -EMSGSIZE) {
1079                         if (skb->protocol == htons(ETH_P_IP))
1080                                 icmp_ndo_send(skb, ICMP_DEST_UNREACH,
1081                                               ICMP_FRAG_NEEDED, htonl(mtu));
1082                         else
1083                                 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1084                 }
1085
1086                 goto tx_err;
1087         }
1088         return NETDEV_TX_OK;
1089
1090 tx_err:
1091         stats = &t->dev->stats;
1092         if (!IS_ERR(tun_info))
1093                 stats->tx_errors++;
1094         stats->tx_dropped++;
1095         kfree_skb(skb);
1096         return NETDEV_TX_OK;
1097 }
1098
1099 static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
1100 {
1101         struct net_device *dev = t->dev;
1102         struct __ip6_tnl_parm *p = &t->parms;
1103         struct flowi6 *fl6 = &t->fl.u.ip6;
1104
1105         if (dev->type != ARPHRD_ETHER) {
1106                 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1107                 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1108         }
1109
1110         /* Set up flowi template */
1111         fl6->saddr = p->laddr;
1112         fl6->daddr = p->raddr;
1113         fl6->flowi6_oif = p->link;
1114         fl6->flowlabel = 0;
1115         fl6->flowi6_proto = IPPROTO_GRE;
1116         fl6->fl6_gre_key = t->parms.o_key;
1117
1118         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1119                 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1120         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1121                 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1122
1123         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1124         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1125
1126         if (p->flags&IP6_TNL_F_CAP_XMIT &&
1127                         p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1128                 dev->flags |= IFF_POINTOPOINT;
1129         else
1130                 dev->flags &= ~IFF_POINTOPOINT;
1131 }
1132
1133 static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
1134                                          int t_hlen)
1135 {
1136         const struct __ip6_tnl_parm *p = &t->parms;
1137         struct net_device *dev = t->dev;
1138
1139         if (p->flags & IP6_TNL_F_CAP_XMIT) {
1140                 int strict = (ipv6_addr_type(&p->raddr) &
1141                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1142
1143                 struct rt6_info *rt = rt6_lookup(t->net,
1144                                                  &p->raddr, &p->laddr,
1145                                                  p->link, NULL, strict);
1146
1147                 if (!rt)
1148                         return;
1149
1150                 if (rt->dst.dev) {
1151                         unsigned short dst_len = rt->dst.dev->hard_header_len +
1152                                                  t_hlen;
1153
1154                         if (t->dev->header_ops)
1155                                 dev->hard_header_len = dst_len;
1156                         else
1157                                 dev->needed_headroom = dst_len;
1158
1159                         if (set_mtu) {
1160                                 int mtu = rt->dst.dev->mtu - t_hlen;
1161
1162                                 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1163                                         mtu -= 8;
1164                                 if (dev->type == ARPHRD_ETHER)
1165                                         mtu -= ETH_HLEN;
1166
1167                                 if (mtu < IPV6_MIN_MTU)
1168                                         mtu = IPV6_MIN_MTU;
1169                                 WRITE_ONCE(dev->mtu, mtu);
1170                         }
1171                 }
1172                 ip6_rt_put(rt);
1173         }
1174 }
1175
1176 static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
1177 {
1178         int t_hlen;
1179
1180         tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1181         tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1182
1183         t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1184
1185         if (tunnel->dev->header_ops)
1186                 tunnel->dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1187         else
1188                 tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1189
1190         return t_hlen;
1191 }
1192
1193 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1194 {
1195         ip6gre_tnl_link_config_common(t);
1196         ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
1197 }
1198
1199 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
1200                                      const struct __ip6_tnl_parm *p)
1201 {
1202         t->parms.laddr = p->laddr;
1203         t->parms.raddr = p->raddr;
1204         t->parms.flags = p->flags;
1205         t->parms.hop_limit = p->hop_limit;
1206         t->parms.encap_limit = p->encap_limit;
1207         t->parms.flowinfo = p->flowinfo;
1208         t->parms.link = p->link;
1209         t->parms.proto = p->proto;
1210         t->parms.i_key = p->i_key;
1211         t->parms.o_key = p->o_key;
1212         t->parms.i_flags = p->i_flags;
1213         t->parms.o_flags = p->o_flags;
1214         t->parms.fwmark = p->fwmark;
1215         t->parms.erspan_ver = p->erspan_ver;
1216         t->parms.index = p->index;
1217         t->parms.dir = p->dir;
1218         t->parms.hwid = p->hwid;
1219         dst_cache_reset(&t->dst_cache);
1220 }
1221
1222 static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
1223                              int set_mtu)
1224 {
1225         ip6gre_tnl_copy_tnl_parm(t, p);
1226         ip6gre_tnl_link_config(t, set_mtu);
1227         return 0;
1228 }
1229
1230 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1231         const struct ip6_tnl_parm2 *u)
1232 {
1233         p->laddr = u->laddr;
1234         p->raddr = u->raddr;
1235         p->flags = u->flags;
1236         p->hop_limit = u->hop_limit;
1237         p->encap_limit = u->encap_limit;
1238         p->flowinfo = u->flowinfo;
1239         p->link = u->link;
1240         p->i_key = u->i_key;
1241         p->o_key = u->o_key;
1242         p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1243         p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
1244         memcpy(p->name, u->name, sizeof(u->name));
1245 }
1246
1247 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1248         const struct __ip6_tnl_parm *p)
1249 {
1250         u->proto = IPPROTO_GRE;
1251         u->laddr = p->laddr;
1252         u->raddr = p->raddr;
1253         u->flags = p->flags;
1254         u->hop_limit = p->hop_limit;
1255         u->encap_limit = p->encap_limit;
1256         u->flowinfo = p->flowinfo;
1257         u->link = p->link;
1258         u->i_key = p->i_key;
1259         u->o_key = p->o_key;
1260         u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1261         u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
1262         memcpy(u->name, p->name, sizeof(u->name));
1263 }
1264
1265 static int ip6gre_tunnel_siocdevprivate(struct net_device *dev,
1266                                         struct ifreq *ifr, void __user *data,
1267                                         int cmd)
1268 {
1269         int err = 0;
1270         struct ip6_tnl_parm2 p;
1271         struct __ip6_tnl_parm p1;
1272         struct ip6_tnl *t = netdev_priv(dev);
1273         struct net *net = t->net;
1274         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1275
1276         memset(&p1, 0, sizeof(p1));
1277
1278         switch (cmd) {
1279         case SIOCGETTUNNEL:
1280                 if (dev == ign->fb_tunnel_dev) {
1281                         if (copy_from_user(&p, data, sizeof(p))) {
1282                                 err = -EFAULT;
1283                                 break;
1284                         }
1285                         ip6gre_tnl_parm_from_user(&p1, &p);
1286                         t = ip6gre_tunnel_locate(net, &p1, 0);
1287                         if (!t)
1288                                 t = netdev_priv(dev);
1289                 }
1290                 memset(&p, 0, sizeof(p));
1291                 ip6gre_tnl_parm_to_user(&p, &t->parms);
1292                 if (copy_to_user(data, &p, sizeof(p)))
1293                         err = -EFAULT;
1294                 break;
1295
1296         case SIOCADDTUNNEL:
1297         case SIOCCHGTUNNEL:
1298                 err = -EPERM;
1299                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1300                         goto done;
1301
1302                 err = -EFAULT;
1303                 if (copy_from_user(&p, data, sizeof(p)))
1304                         goto done;
1305
1306                 err = -EINVAL;
1307                 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1308                         goto done;
1309
1310                 if (!(p.i_flags&GRE_KEY))
1311                         p.i_key = 0;
1312                 if (!(p.o_flags&GRE_KEY))
1313                         p.o_key = 0;
1314
1315                 ip6gre_tnl_parm_from_user(&p1, &p);
1316                 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1317
1318                 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1319                         if (t) {
1320                                 if (t->dev != dev) {
1321                                         err = -EEXIST;
1322                                         break;
1323                                 }
1324                         } else {
1325                                 t = netdev_priv(dev);
1326
1327                                 ip6gre_tunnel_unlink(ign, t);
1328                                 synchronize_net();
1329                                 ip6gre_tnl_change(t, &p1, 1);
1330                                 ip6gre_tunnel_link(ign, t);
1331                                 netdev_state_change(dev);
1332                         }
1333                 }
1334
1335                 if (t) {
1336                         err = 0;
1337
1338                         memset(&p, 0, sizeof(p));
1339                         ip6gre_tnl_parm_to_user(&p, &t->parms);
1340                         if (copy_to_user(data, &p, sizeof(p)))
1341                                 err = -EFAULT;
1342                 } else
1343                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1344                 break;
1345
1346         case SIOCDELTUNNEL:
1347                 err = -EPERM;
1348                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1349                         goto done;
1350
1351                 if (dev == ign->fb_tunnel_dev) {
1352                         err = -EFAULT;
1353                         if (copy_from_user(&p, data, sizeof(p)))
1354                                 goto done;
1355                         err = -ENOENT;
1356                         ip6gre_tnl_parm_from_user(&p1, &p);
1357                         t = ip6gre_tunnel_locate(net, &p1, 0);
1358                         if (!t)
1359                                 goto done;
1360                         err = -EPERM;
1361                         if (t == netdev_priv(ign->fb_tunnel_dev))
1362                                 goto done;
1363                         dev = t->dev;
1364                 }
1365                 unregister_netdevice(dev);
1366                 err = 0;
1367                 break;
1368
1369         default:
1370                 err = -EINVAL;
1371         }
1372
1373 done:
1374         return err;
1375 }
1376
1377 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1378                          unsigned short type, const void *daddr,
1379                          const void *saddr, unsigned int len)
1380 {
1381         struct ip6_tnl *t = netdev_priv(dev);
1382         struct ipv6hdr *ipv6h;
1383         __be16 *p;
1384
1385         ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1386         ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1387                                                   t->fl.u.ip6.flowlabel,
1388                                                   true, &t->fl.u.ip6));
1389         ipv6h->hop_limit = t->parms.hop_limit;
1390         ipv6h->nexthdr = NEXTHDR_GRE;
1391         ipv6h->saddr = t->parms.laddr;
1392         ipv6h->daddr = t->parms.raddr;
1393
1394         p = (__be16 *)(ipv6h + 1);
1395         p[0] = t->parms.o_flags;
1396         p[1] = htons(type);
1397
1398         /*
1399          *      Set the source hardware address.
1400          */
1401
1402         if (saddr)
1403                 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1404         if (daddr)
1405                 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1406         if (!ipv6_addr_any(&ipv6h->daddr))
1407                 return t->hlen;
1408
1409         return -t->hlen;
1410 }
1411
1412 static const struct header_ops ip6gre_header_ops = {
1413         .create = ip6gre_header,
1414 };
1415
1416 static const struct net_device_ops ip6gre_netdev_ops = {
1417         .ndo_init               = ip6gre_tunnel_init,
1418         .ndo_uninit             = ip6gre_tunnel_uninit,
1419         .ndo_start_xmit         = ip6gre_tunnel_xmit,
1420         .ndo_siocdevprivate     = ip6gre_tunnel_siocdevprivate,
1421         .ndo_change_mtu         = ip6_tnl_change_mtu,
1422         .ndo_get_stats64        = dev_get_tstats64,
1423         .ndo_get_iflink         = ip6_tnl_get_iflink,
1424 };
1425
1426 static void ip6gre_dev_free(struct net_device *dev)
1427 {
1428         struct ip6_tnl *t = netdev_priv(dev);
1429
1430         gro_cells_destroy(&t->gro_cells);
1431         dst_cache_destroy(&t->dst_cache);
1432         free_percpu(dev->tstats);
1433 }
1434
1435 static void ip6gre_tunnel_setup(struct net_device *dev)
1436 {
1437         dev->netdev_ops = &ip6gre_netdev_ops;
1438         dev->needs_free_netdev = true;
1439         dev->priv_destructor = ip6gre_dev_free;
1440
1441         dev->type = ARPHRD_IP6GRE;
1442
1443         dev->flags |= IFF_NOARP;
1444         dev->addr_len = sizeof(struct in6_addr);
1445         netif_keep_dst(dev);
1446         /* This perm addr will be used as interface identifier by IPv6 */
1447         dev->addr_assign_type = NET_ADDR_RANDOM;
1448         eth_random_addr(dev->perm_addr);
1449 }
1450
1451 #define GRE6_FEATURES (NETIF_F_SG |             \
1452                        NETIF_F_FRAGLIST |       \
1453                        NETIF_F_HIGHDMA |        \
1454                        NETIF_F_HW_CSUM)
1455
1456 static void ip6gre_tnl_init_features(struct net_device *dev)
1457 {
1458         struct ip6_tnl *nt = netdev_priv(dev);
1459
1460         dev->features           |= GRE6_FEATURES;
1461         dev->hw_features        |= GRE6_FEATURES;
1462
1463         if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1464                 /* TCP offload with GRE SEQ is not supported, nor
1465                  * can we support 2 levels of outer headers requiring
1466                  * an update.
1467                  */
1468                 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1469                     nt->encap.type == TUNNEL_ENCAP_NONE) {
1470                         dev->features    |= NETIF_F_GSO_SOFTWARE;
1471                         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1472                 }
1473
1474                 /* Can use a lockless transmit, unless we generate
1475                  * output sequences
1476                  */
1477                 dev->features |= NETIF_F_LLTX;
1478         }
1479 }
1480
1481 static int ip6gre_tunnel_init_common(struct net_device *dev)
1482 {
1483         struct ip6_tnl *tunnel;
1484         int ret;
1485         int t_hlen;
1486
1487         tunnel = netdev_priv(dev);
1488
1489         tunnel->dev = dev;
1490         tunnel->net = dev_net(dev);
1491         strcpy(tunnel->parms.name, dev->name);
1492
1493         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1494         if (!dev->tstats)
1495                 return -ENOMEM;
1496
1497         ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1498         if (ret)
1499                 goto cleanup_alloc_pcpu_stats;
1500
1501         ret = gro_cells_init(&tunnel->gro_cells, dev);
1502         if (ret)
1503                 goto cleanup_dst_cache_init;
1504
1505         t_hlen = ip6gre_calc_hlen(tunnel);
1506         dev->mtu = ETH_DATA_LEN - t_hlen;
1507         if (dev->type == ARPHRD_ETHER)
1508                 dev->mtu -= ETH_HLEN;
1509         if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1510                 dev->mtu -= 8;
1511
1512         if (tunnel->parms.collect_md) {
1513                 netif_keep_dst(dev);
1514         }
1515         ip6gre_tnl_init_features(dev);
1516
1517         dev_hold(dev);
1518         return 0;
1519
1520 cleanup_dst_cache_init:
1521         dst_cache_destroy(&tunnel->dst_cache);
1522 cleanup_alloc_pcpu_stats:
1523         free_percpu(dev->tstats);
1524         dev->tstats = NULL;
1525         return ret;
1526 }
1527
1528 static int ip6gre_tunnel_init(struct net_device *dev)
1529 {
1530         struct ip6_tnl *tunnel;
1531         int ret;
1532
1533         ret = ip6gre_tunnel_init_common(dev);
1534         if (ret)
1535                 return ret;
1536
1537         tunnel = netdev_priv(dev);
1538
1539         if (tunnel->parms.collect_md)
1540                 return 0;
1541
1542         memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1543         memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1544
1545         if (ipv6_addr_any(&tunnel->parms.raddr))
1546                 dev->header_ops = &ip6gre_header_ops;
1547
1548         return 0;
1549 }
1550
1551 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1552 {
1553         struct ip6_tnl *tunnel = netdev_priv(dev);
1554
1555         tunnel->dev = dev;
1556         tunnel->net = dev_net(dev);
1557         strcpy(tunnel->parms.name, dev->name);
1558
1559         tunnel->hlen            = sizeof(struct ipv6hdr) + 4;
1560 }
1561
1562 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1563         .handler     = gre_rcv,
1564         .err_handler = ip6gre_err,
1565         .flags       = INET6_PROTO_FINAL,
1566 };
1567
1568 static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
1569 {
1570         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1571         struct net_device *dev, *aux;
1572         int prio;
1573
1574         for_each_netdev_safe(net, dev, aux)
1575                 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1576                     dev->rtnl_link_ops == &ip6gre_tap_ops ||
1577                     dev->rtnl_link_ops == &ip6erspan_tap_ops)
1578                         unregister_netdevice_queue(dev, head);
1579
1580         for (prio = 0; prio < 4; prio++) {
1581                 int h;
1582                 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1583                         struct ip6_tnl *t;
1584
1585                         t = rtnl_dereference(ign->tunnels[prio][h]);
1586
1587                         while (t) {
1588                                 /* If dev is in the same netns, it has already
1589                                  * been added to the list by the previous loop.
1590                                  */
1591                                 if (!net_eq(dev_net(t->dev), net))
1592                                         unregister_netdevice_queue(t->dev,
1593                                                                    head);
1594                                 t = rtnl_dereference(t->next);
1595                         }
1596                 }
1597         }
1598 }
1599
1600 static int __net_init ip6gre_init_net(struct net *net)
1601 {
1602         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1603         struct net_device *ndev;
1604         int err;
1605
1606         if (!net_has_fallback_tunnels(net))
1607                 return 0;
1608         ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1609                             NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1610         if (!ndev) {
1611                 err = -ENOMEM;
1612                 goto err_alloc_dev;
1613         }
1614         ign->fb_tunnel_dev = ndev;
1615         dev_net_set(ign->fb_tunnel_dev, net);
1616         /* FB netdevice is special: we have one, and only one per netns.
1617          * Allowing to move it to another netns is clearly unsafe.
1618          */
1619         ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1620
1621
1622         ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1623         ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1624
1625         err = register_netdev(ign->fb_tunnel_dev);
1626         if (err)
1627                 goto err_reg_dev;
1628
1629         rcu_assign_pointer(ign->tunnels_wc[0],
1630                            netdev_priv(ign->fb_tunnel_dev));
1631         return 0;
1632
1633 err_reg_dev:
1634         free_netdev(ndev);
1635 err_alloc_dev:
1636         return err;
1637 }
1638
1639 static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
1640 {
1641         struct net *net;
1642         LIST_HEAD(list);
1643
1644         rtnl_lock();
1645         list_for_each_entry(net, net_list, exit_list)
1646                 ip6gre_destroy_tunnels(net, &list);
1647         unregister_netdevice_many(&list);
1648         rtnl_unlock();
1649 }
1650
1651 static struct pernet_operations ip6gre_net_ops = {
1652         .init = ip6gre_init_net,
1653         .exit_batch = ip6gre_exit_batch_net,
1654         .id   = &ip6gre_net_id,
1655         .size = sizeof(struct ip6gre_net),
1656 };
1657
1658 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1659                                   struct netlink_ext_ack *extack)
1660 {
1661         __be16 flags;
1662
1663         if (!data)
1664                 return 0;
1665
1666         flags = 0;
1667         if (data[IFLA_GRE_IFLAGS])
1668                 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1669         if (data[IFLA_GRE_OFLAGS])
1670                 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1671         if (flags & (GRE_VERSION|GRE_ROUTING))
1672                 return -EINVAL;
1673
1674         return 0;
1675 }
1676
1677 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1678                                struct netlink_ext_ack *extack)
1679 {
1680         struct in6_addr daddr;
1681
1682         if (tb[IFLA_ADDRESS]) {
1683                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1684                         return -EINVAL;
1685                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1686                         return -EADDRNOTAVAIL;
1687         }
1688
1689         if (!data)
1690                 goto out;
1691
1692         if (data[IFLA_GRE_REMOTE]) {
1693                 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1694                 if (ipv6_addr_any(&daddr))
1695                         return -EINVAL;
1696         }
1697
1698 out:
1699         return ip6gre_tunnel_validate(tb, data, extack);
1700 }
1701
1702 static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1703                                   struct netlink_ext_ack *extack)
1704 {
1705         __be16 flags = 0;
1706         int ret, ver = 0;
1707
1708         if (!data)
1709                 return 0;
1710
1711         ret = ip6gre_tap_validate(tb, data, extack);
1712         if (ret)
1713                 return ret;
1714
1715         /* ERSPAN should only have GRE sequence and key flag */
1716         if (data[IFLA_GRE_OFLAGS])
1717                 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1718         if (data[IFLA_GRE_IFLAGS])
1719                 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1720         if (!data[IFLA_GRE_COLLECT_METADATA] &&
1721             flags != (GRE_SEQ | GRE_KEY))
1722                 return -EINVAL;
1723
1724         /* ERSPAN Session ID only has 10-bit. Since we reuse
1725          * 32-bit key field as ID, check it's range.
1726          */
1727         if (data[IFLA_GRE_IKEY] &&
1728             (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1729                 return -EINVAL;
1730
1731         if (data[IFLA_GRE_OKEY] &&
1732             (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1733                 return -EINVAL;
1734
1735         if (data[IFLA_GRE_ERSPAN_VER]) {
1736                 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1737                 if (ver != 1 && ver != 2)
1738                         return -EINVAL;
1739         }
1740
1741         if (ver == 1) {
1742                 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1743                         u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1744
1745                         if (index & ~INDEX_MASK)
1746                                 return -EINVAL;
1747                 }
1748         } else if (ver == 2) {
1749                 if (data[IFLA_GRE_ERSPAN_DIR]) {
1750                         u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1751
1752                         if (dir & ~(DIR_MASK >> DIR_OFFSET))
1753                                 return -EINVAL;
1754                 }
1755
1756                 if (data[IFLA_GRE_ERSPAN_HWID]) {
1757                         u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1758
1759                         if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1760                                 return -EINVAL;
1761                 }
1762         }
1763
1764         return 0;
1765 }
1766
1767 static void ip6erspan_set_version(struct nlattr *data[],
1768                                   struct __ip6_tnl_parm *parms)
1769 {
1770         if (!data)
1771                 return;
1772
1773         parms->erspan_ver = 1;
1774         if (data[IFLA_GRE_ERSPAN_VER])
1775                 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1776
1777         if (parms->erspan_ver == 1) {
1778                 if (data[IFLA_GRE_ERSPAN_INDEX])
1779                         parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1780         } else if (parms->erspan_ver == 2) {
1781                 if (data[IFLA_GRE_ERSPAN_DIR])
1782                         parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1783                 if (data[IFLA_GRE_ERSPAN_HWID])
1784                         parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1785         }
1786 }
1787
1788 static void ip6gre_netlink_parms(struct nlattr *data[],
1789                                 struct __ip6_tnl_parm *parms)
1790 {
1791         memset(parms, 0, sizeof(*parms));
1792
1793         if (!data)
1794                 return;
1795
1796         if (data[IFLA_GRE_LINK])
1797                 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1798
1799         if (data[IFLA_GRE_IFLAGS])
1800                 parms->i_flags = gre_flags_to_tnl_flags(
1801                                 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1802
1803         if (data[IFLA_GRE_OFLAGS])
1804                 parms->o_flags = gre_flags_to_tnl_flags(
1805                                 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1806
1807         if (data[IFLA_GRE_IKEY])
1808                 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1809
1810         if (data[IFLA_GRE_OKEY])
1811                 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1812
1813         if (data[IFLA_GRE_LOCAL])
1814                 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1815
1816         if (data[IFLA_GRE_REMOTE])
1817                 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1818
1819         if (data[IFLA_GRE_TTL])
1820                 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1821
1822         if (data[IFLA_GRE_ENCAP_LIMIT])
1823                 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1824
1825         if (data[IFLA_GRE_FLOWINFO])
1826                 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1827
1828         if (data[IFLA_GRE_FLAGS])
1829                 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1830
1831         if (data[IFLA_GRE_FWMARK])
1832                 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1833
1834         if (data[IFLA_GRE_COLLECT_METADATA])
1835                 parms->collect_md = true;
1836 }
1837
1838 static int ip6gre_tap_init(struct net_device *dev)
1839 {
1840         int ret;
1841
1842         ret = ip6gre_tunnel_init_common(dev);
1843         if (ret)
1844                 return ret;
1845
1846         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1847
1848         return 0;
1849 }
1850
1851 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1852         .ndo_init = ip6gre_tap_init,
1853         .ndo_uninit = ip6gre_tunnel_uninit,
1854         .ndo_start_xmit = ip6gre_tunnel_xmit,
1855         .ndo_set_mac_address = eth_mac_addr,
1856         .ndo_validate_addr = eth_validate_addr,
1857         .ndo_change_mtu = ip6_tnl_change_mtu,
1858         .ndo_get_stats64 = dev_get_tstats64,
1859         .ndo_get_iflink = ip6_tnl_get_iflink,
1860 };
1861
1862 static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
1863 {
1864         int t_hlen;
1865
1866         tunnel->tun_hlen = 8;
1867         tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1868                        erspan_hdr_len(tunnel->parms.erspan_ver);
1869
1870         t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1871         tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1872         return t_hlen;
1873 }
1874
1875 static int ip6erspan_tap_init(struct net_device *dev)
1876 {
1877         struct ip6_tnl *tunnel;
1878         int t_hlen;
1879         int ret;
1880
1881         tunnel = netdev_priv(dev);
1882
1883         tunnel->dev = dev;
1884         tunnel->net = dev_net(dev);
1885         strcpy(tunnel->parms.name, dev->name);
1886
1887         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1888         if (!dev->tstats)
1889                 return -ENOMEM;
1890
1891         ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1892         if (ret)
1893                 goto cleanup_alloc_pcpu_stats;
1894
1895         ret = gro_cells_init(&tunnel->gro_cells, dev);
1896         if (ret)
1897                 goto cleanup_dst_cache_init;
1898
1899         t_hlen = ip6erspan_calc_hlen(tunnel);
1900         dev->mtu = ETH_DATA_LEN - t_hlen;
1901         if (dev->type == ARPHRD_ETHER)
1902                 dev->mtu -= ETH_HLEN;
1903         if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1904                 dev->mtu -= 8;
1905
1906         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1907         ip6erspan_tnl_link_config(tunnel, 1);
1908
1909         dev_hold(dev);
1910         return 0;
1911
1912 cleanup_dst_cache_init:
1913         dst_cache_destroy(&tunnel->dst_cache);
1914 cleanup_alloc_pcpu_stats:
1915         free_percpu(dev->tstats);
1916         dev->tstats = NULL;
1917         return ret;
1918 }
1919
1920 static const struct net_device_ops ip6erspan_netdev_ops = {
1921         .ndo_init =             ip6erspan_tap_init,
1922         .ndo_uninit =           ip6erspan_tunnel_uninit,
1923         .ndo_start_xmit =       ip6erspan_tunnel_xmit,
1924         .ndo_set_mac_address =  eth_mac_addr,
1925         .ndo_validate_addr =    eth_validate_addr,
1926         .ndo_change_mtu =       ip6_tnl_change_mtu,
1927         .ndo_get_stats64 =      dev_get_tstats64,
1928         .ndo_get_iflink =       ip6_tnl_get_iflink,
1929 };
1930
1931 static void ip6gre_tap_setup(struct net_device *dev)
1932 {
1933
1934         ether_setup(dev);
1935
1936         dev->max_mtu = 0;
1937         dev->netdev_ops = &ip6gre_tap_netdev_ops;
1938         dev->needs_free_netdev = true;
1939         dev->priv_destructor = ip6gre_dev_free;
1940
1941         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1942         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1943         netif_keep_dst(dev);
1944 }
1945
1946 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1947                                        struct ip_tunnel_encap *ipencap)
1948 {
1949         bool ret = false;
1950
1951         memset(ipencap, 0, sizeof(*ipencap));
1952
1953         if (!data)
1954                 return ret;
1955
1956         if (data[IFLA_GRE_ENCAP_TYPE]) {
1957                 ret = true;
1958                 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1959         }
1960
1961         if (data[IFLA_GRE_ENCAP_FLAGS]) {
1962                 ret = true;
1963                 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1964         }
1965
1966         if (data[IFLA_GRE_ENCAP_SPORT]) {
1967                 ret = true;
1968                 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1969         }
1970
1971         if (data[IFLA_GRE_ENCAP_DPORT]) {
1972                 ret = true;
1973                 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1974         }
1975
1976         return ret;
1977 }
1978
1979 static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
1980                                  struct nlattr *tb[], struct nlattr *data[],
1981                                  struct netlink_ext_ack *extack)
1982 {
1983         struct ip6_tnl *nt;
1984         struct ip_tunnel_encap ipencap;
1985         int err;
1986
1987         nt = netdev_priv(dev);
1988
1989         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1990                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1991
1992                 if (err < 0)
1993                         return err;
1994         }
1995
1996         if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1997                 eth_hw_addr_random(dev);
1998
1999         nt->dev = dev;
2000         nt->net = dev_net(dev);
2001
2002         err = register_netdevice(dev);
2003         if (err)
2004                 goto out;
2005
2006         if (tb[IFLA_MTU])
2007                 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2008
2009 out:
2010         return err;
2011 }
2012
2013 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
2014                           struct nlattr *tb[], struct nlattr *data[],
2015                           struct netlink_ext_ack *extack)
2016 {
2017         struct ip6_tnl *nt = netdev_priv(dev);
2018         struct net *net = dev_net(dev);
2019         struct ip6gre_net *ign;
2020         int err;
2021
2022         ip6gre_netlink_parms(data, &nt->parms);
2023         ign = net_generic(net, ip6gre_net_id);
2024
2025         if (nt->parms.collect_md) {
2026                 if (rtnl_dereference(ign->collect_md_tun))
2027                         return -EEXIST;
2028         } else {
2029                 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2030                         return -EEXIST;
2031         }
2032
2033         err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
2034         if (!err) {
2035                 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
2036                 ip6gre_tunnel_link_md(ign, nt);
2037                 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2038         }
2039         return err;
2040 }
2041
2042 static struct ip6_tnl *
2043 ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
2044                          struct nlattr *data[], struct __ip6_tnl_parm *p_p,
2045                          struct netlink_ext_ack *extack)
2046 {
2047         struct ip6_tnl *t, *nt = netdev_priv(dev);
2048         struct net *net = nt->net;
2049         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2050         struct ip_tunnel_encap ipencap;
2051
2052         if (dev == ign->fb_tunnel_dev)
2053                 return ERR_PTR(-EINVAL);
2054
2055         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
2056                 int err = ip6_tnl_encap_setup(nt, &ipencap);
2057
2058                 if (err < 0)
2059                         return ERR_PTR(err);
2060         }
2061
2062         ip6gre_netlink_parms(data, p_p);
2063
2064         t = ip6gre_tunnel_locate(net, p_p, 0);
2065
2066         if (t) {
2067                 if (t->dev != dev)
2068                         return ERR_PTR(-EEXIST);
2069         } else {
2070                 t = nt;
2071         }
2072
2073         return t;
2074 }
2075
2076 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
2077                              struct nlattr *data[],
2078                              struct netlink_ext_ack *extack)
2079 {
2080         struct ip6_tnl *t = netdev_priv(dev);
2081         struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
2082         struct __ip6_tnl_parm p;
2083
2084         t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2085         if (IS_ERR(t))
2086                 return PTR_ERR(t);
2087
2088         ip6gre_tunnel_unlink_md(ign, t);
2089         ip6gre_tunnel_unlink(ign, t);
2090         ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
2091         ip6gre_tunnel_link_md(ign, t);
2092         ip6gre_tunnel_link(ign, t);
2093         return 0;
2094 }
2095
2096 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
2097 {
2098         struct net *net = dev_net(dev);
2099         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2100
2101         if (dev != ign->fb_tunnel_dev)
2102                 unregister_netdevice_queue(dev, head);
2103 }
2104
2105 static size_t ip6gre_get_size(const struct net_device *dev)
2106 {
2107         return
2108                 /* IFLA_GRE_LINK */
2109                 nla_total_size(4) +
2110                 /* IFLA_GRE_IFLAGS */
2111                 nla_total_size(2) +
2112                 /* IFLA_GRE_OFLAGS */
2113                 nla_total_size(2) +
2114                 /* IFLA_GRE_IKEY */
2115                 nla_total_size(4) +
2116                 /* IFLA_GRE_OKEY */
2117                 nla_total_size(4) +
2118                 /* IFLA_GRE_LOCAL */
2119                 nla_total_size(sizeof(struct in6_addr)) +
2120                 /* IFLA_GRE_REMOTE */
2121                 nla_total_size(sizeof(struct in6_addr)) +
2122                 /* IFLA_GRE_TTL */
2123                 nla_total_size(1) +
2124                 /* IFLA_GRE_ENCAP_LIMIT */
2125                 nla_total_size(1) +
2126                 /* IFLA_GRE_FLOWINFO */
2127                 nla_total_size(4) +
2128                 /* IFLA_GRE_FLAGS */
2129                 nla_total_size(4) +
2130                 /* IFLA_GRE_ENCAP_TYPE */
2131                 nla_total_size(2) +
2132                 /* IFLA_GRE_ENCAP_FLAGS */
2133                 nla_total_size(2) +
2134                 /* IFLA_GRE_ENCAP_SPORT */
2135                 nla_total_size(2) +
2136                 /* IFLA_GRE_ENCAP_DPORT */
2137                 nla_total_size(2) +
2138                 /* IFLA_GRE_COLLECT_METADATA */
2139                 nla_total_size(0) +
2140                 /* IFLA_GRE_FWMARK */
2141                 nla_total_size(4) +
2142                 /* IFLA_GRE_ERSPAN_INDEX */
2143                 nla_total_size(4) +
2144                 0;
2145 }
2146
2147 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
2148 {
2149         struct ip6_tnl *t = netdev_priv(dev);
2150         struct __ip6_tnl_parm *p = &t->parms;
2151         __be16 o_flags = p->o_flags;
2152
2153         if (p->erspan_ver == 1 || p->erspan_ver == 2) {
2154                 if (!p->collect_md)
2155                         o_flags |= TUNNEL_KEY;
2156
2157                 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2158                         goto nla_put_failure;
2159
2160                 if (p->erspan_ver == 1) {
2161                         if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2162                                 goto nla_put_failure;
2163                 } else {
2164                         if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2165                                 goto nla_put_failure;
2166                         if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2167                                 goto nla_put_failure;
2168                 }
2169         }
2170
2171         if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
2172             nla_put_be16(skb, IFLA_GRE_IFLAGS,
2173                          gre_tnl_flags_to_gre_flags(p->i_flags)) ||
2174             nla_put_be16(skb, IFLA_GRE_OFLAGS,
2175                          gre_tnl_flags_to_gre_flags(o_flags)) ||
2176             nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
2177             nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
2178             nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
2179             nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
2180             nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
2181             nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
2182             nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
2183             nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
2184             nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
2185                 goto nla_put_failure;
2186
2187         if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
2188                         t->encap.type) ||
2189             nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2190                          t->encap.sport) ||
2191             nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2192                          t->encap.dport) ||
2193             nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2194                         t->encap.flags))
2195                 goto nla_put_failure;
2196
2197         if (p->collect_md) {
2198                 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2199                         goto nla_put_failure;
2200         }
2201
2202         return 0;
2203
2204 nla_put_failure:
2205         return -EMSGSIZE;
2206 }
2207
2208 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2209         [IFLA_GRE_LINK]        = { .type = NLA_U32 },
2210         [IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
2211         [IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
2212         [IFLA_GRE_IKEY]        = { .type = NLA_U32 },
2213         [IFLA_GRE_OKEY]        = { .type = NLA_U32 },
2214         [IFLA_GRE_LOCAL]       = { .len = sizeof_field(struct ipv6hdr, saddr) },
2215         [IFLA_GRE_REMOTE]      = { .len = sizeof_field(struct ipv6hdr, daddr) },
2216         [IFLA_GRE_TTL]         = { .type = NLA_U8 },
2217         [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2218         [IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
2219         [IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
2220         [IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
2221         [IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
2222         [IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
2223         [IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
2224         [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
2225         [IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
2226         [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
2227         [IFLA_GRE_ERSPAN_VER]   = { .type = NLA_U8 },
2228         [IFLA_GRE_ERSPAN_DIR]   = { .type = NLA_U8 },
2229         [IFLA_GRE_ERSPAN_HWID]  = { .type = NLA_U16 },
2230 };
2231
2232 static void ip6erspan_tap_setup(struct net_device *dev)
2233 {
2234         ether_setup(dev);
2235
2236         dev->max_mtu = 0;
2237         dev->netdev_ops = &ip6erspan_netdev_ops;
2238         dev->needs_free_netdev = true;
2239         dev->priv_destructor = ip6gre_dev_free;
2240
2241         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2242         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2243         netif_keep_dst(dev);
2244 }
2245
2246 static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,
2247                              struct nlattr *tb[], struct nlattr *data[],
2248                              struct netlink_ext_ack *extack)
2249 {
2250         struct ip6_tnl *nt = netdev_priv(dev);
2251         struct net *net = dev_net(dev);
2252         struct ip6gre_net *ign;
2253         int err;
2254
2255         ip6gre_netlink_parms(data, &nt->parms);
2256         ip6erspan_set_version(data, &nt->parms);
2257         ign = net_generic(net, ip6gre_net_id);
2258
2259         if (nt->parms.collect_md) {
2260                 if (rtnl_dereference(ign->collect_md_tun_erspan))
2261                         return -EEXIST;
2262         } else {
2263                 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
2264                         return -EEXIST;
2265         }
2266
2267         err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
2268         if (!err) {
2269                 ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2270                 ip6erspan_tunnel_link_md(ign, nt);
2271                 ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
2272         }
2273         return err;
2274 }
2275
2276 static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
2277 {
2278         ip6gre_tnl_link_config_common(t);
2279         ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
2280 }
2281
2282 static int ip6erspan_tnl_change(struct ip6_tnl *t,
2283                                 const struct __ip6_tnl_parm *p, int set_mtu)
2284 {
2285         ip6gre_tnl_copy_tnl_parm(t, p);
2286         ip6erspan_tnl_link_config(t, set_mtu);
2287         return 0;
2288 }
2289
2290 static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
2291                                 struct nlattr *data[],
2292                                 struct netlink_ext_ack *extack)
2293 {
2294         struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
2295         struct __ip6_tnl_parm p;
2296         struct ip6_tnl *t;
2297
2298         t = ip6gre_changelink_common(dev, tb, data, &p, extack);
2299         if (IS_ERR(t))
2300                 return PTR_ERR(t);
2301
2302         ip6erspan_set_version(data, &p);
2303         ip6gre_tunnel_unlink_md(ign, t);
2304         ip6gre_tunnel_unlink(ign, t);
2305         ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2306         ip6erspan_tunnel_link_md(ign, t);
2307         ip6gre_tunnel_link(ign, t);
2308         return 0;
2309 }
2310
2311 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2312         .kind           = "ip6gre",
2313         .maxtype        = IFLA_GRE_MAX,
2314         .policy         = ip6gre_policy,
2315         .priv_size      = sizeof(struct ip6_tnl),
2316         .setup          = ip6gre_tunnel_setup,
2317         .validate       = ip6gre_tunnel_validate,
2318         .newlink        = ip6gre_newlink,
2319         .changelink     = ip6gre_changelink,
2320         .dellink        = ip6gre_dellink,
2321         .get_size       = ip6gre_get_size,
2322         .fill_info      = ip6gre_fill_info,
2323         .get_link_net   = ip6_tnl_get_link_net,
2324 };
2325
2326 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2327         .kind           = "ip6gretap",
2328         .maxtype        = IFLA_GRE_MAX,
2329         .policy         = ip6gre_policy,
2330         .priv_size      = sizeof(struct ip6_tnl),
2331         .setup          = ip6gre_tap_setup,
2332         .validate       = ip6gre_tap_validate,
2333         .newlink        = ip6gre_newlink,
2334         .changelink     = ip6gre_changelink,
2335         .get_size       = ip6gre_get_size,
2336         .fill_info      = ip6gre_fill_info,
2337         .get_link_net   = ip6_tnl_get_link_net,
2338 };
2339
2340 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2341         .kind           = "ip6erspan",
2342         .maxtype        = IFLA_GRE_MAX,
2343         .policy         = ip6gre_policy,
2344         .priv_size      = sizeof(struct ip6_tnl),
2345         .setup          = ip6erspan_tap_setup,
2346         .validate       = ip6erspan_tap_validate,
2347         .newlink        = ip6erspan_newlink,
2348         .changelink     = ip6erspan_changelink,
2349         .get_size       = ip6gre_get_size,
2350         .fill_info      = ip6gre_fill_info,
2351         .get_link_net   = ip6_tnl_get_link_net,
2352 };
2353
2354 /*
2355  *      And now the modules code and kernel interface.
2356  */
2357
2358 static int __init ip6gre_init(void)
2359 {
2360         int err;
2361
2362         pr_info("GRE over IPv6 tunneling driver\n");
2363
2364         err = register_pernet_device(&ip6gre_net_ops);
2365         if (err < 0)
2366                 return err;
2367
2368         err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2369         if (err < 0) {
2370                 pr_info("%s: can't add protocol\n", __func__);
2371                 goto add_proto_failed;
2372         }
2373
2374         err = rtnl_link_register(&ip6gre_link_ops);
2375         if (err < 0)
2376                 goto rtnl_link_failed;
2377
2378         err = rtnl_link_register(&ip6gre_tap_ops);
2379         if (err < 0)
2380                 goto tap_ops_failed;
2381
2382         err = rtnl_link_register(&ip6erspan_tap_ops);
2383         if (err < 0)
2384                 goto erspan_link_failed;
2385
2386 out:
2387         return err;
2388
2389 erspan_link_failed:
2390         rtnl_link_unregister(&ip6gre_tap_ops);
2391 tap_ops_failed:
2392         rtnl_link_unregister(&ip6gre_link_ops);
2393 rtnl_link_failed:
2394         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2395 add_proto_failed:
2396         unregister_pernet_device(&ip6gre_net_ops);
2397         goto out;
2398 }
2399
2400 static void __exit ip6gre_fini(void)
2401 {
2402         rtnl_link_unregister(&ip6gre_tap_ops);
2403         rtnl_link_unregister(&ip6gre_link_ops);
2404         rtnl_link_unregister(&ip6erspan_tap_ops);
2405         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2406         unregister_pernet_device(&ip6gre_net_ops);
2407 }
2408
2409 module_init(ip6gre_init);
2410 module_exit(ip6gre_fini);
2411 MODULE_LICENSE("GPL");
2412 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2413 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2414 MODULE_ALIAS_RTNL_LINK("ip6gre");
2415 MODULE_ALIAS_RTNL_LINK("ip6gretap");
2416 MODULE_ALIAS_RTNL_LINK("ip6erspan");
2417 MODULE_ALIAS_NETDEV("ip6gre0");