GNU Linux-libre 4.14.313-gnu1
[releases.git] / net / ipv6 / ip6_gre.c
1 /*
2  *      GRE over IPv6 protocol decoder.
3  *
4  *      Authors: Dmitry Kozlov (xeb@mail.ru)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  *
11  */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/capability.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/in.h>
24 #include <linux/tcp.h>
25 #include <linux/udp.h>
26 #include <linux/if_arp.h>
27 #include <linux/init.h>
28 #include <linux/in6.h>
29 #include <linux/inetdevice.h>
30 #include <linux/igmp.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/etherdevice.h>
33 #include <linux/if_ether.h>
34 #include <linux/hash.h>
35 #include <linux/if_tunnel.h>
36 #include <linux/ip6_tunnel.h>
37
38 #include <net/sock.h>
39 #include <net/ip.h>
40 #include <net/ip_tunnels.h>
41 #include <net/icmp.h>
42 #include <net/protocol.h>
43 #include <net/addrconf.h>
44 #include <net/arp.h>
45 #include <net/checksum.h>
46 #include <net/dsfield.h>
47 #include <net/inet_ecn.h>
48 #include <net/xfrm.h>
49 #include <net/net_namespace.h>
50 #include <net/netns/generic.h>
51 #include <net/rtnetlink.h>
52
53 #include <net/ipv6.h>
54 #include <net/ip6_fib.h>
55 #include <net/ip6_route.h>
56 #include <net/ip6_tunnel.h>
57 #include <net/gre.h>
58
59
60 static bool log_ecn_error = true;
61 module_param(log_ecn_error, bool, 0644);
62 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
63
64 #define IP6_GRE_HASH_SIZE_SHIFT  5
65 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
66
67 static unsigned int ip6gre_net_id __read_mostly;
68 struct ip6gre_net {
69         struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
70
71         struct net_device *fb_tunnel_dev;
72 };
73
74 static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
75 static struct rtnl_link_ops ip6gre_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
81 /* Tunnel hash table */
82
83 /*
84    4 hash tables:
85
86    3: (remote,local)
87    2: (remote,*)
88    1: (*,local)
89    0: (*,*)
90
91    We require exact key match i.e. if a key is present in packet
92    it will match only tunnel with the same key; if it is not present,
93    it will match only keyless tunnel.
94
95    All keysless packets, if not matched configured keyless tunnels
96    will match fallback tunnel.
97  */
98
99 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
100 static u32 HASH_ADDR(const struct in6_addr *addr)
101 {
102         u32 hash = ipv6_addr_hash(addr);
103
104         return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
105 }
106
107 #define tunnels_r_l     tunnels[3]
108 #define tunnels_r       tunnels[2]
109 #define tunnels_l       tunnels[1]
110 #define tunnels_wc      tunnels[0]
111
112 /* Given src, dst and key, find appropriate for input tunnel. */
113
114 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
115                 const struct in6_addr *remote, const struct in6_addr *local,
116                 __be32 key, __be16 gre_proto)
117 {
118         struct net *net = dev_net(dev);
119         int link = dev->ifindex;
120         unsigned int h0 = HASH_ADDR(remote);
121         unsigned int h1 = HASH_KEY(key);
122         struct ip6_tnl *t, *cand = NULL;
123         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
124         int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
125                        ARPHRD_ETHER : ARPHRD_IP6GRE;
126         int score, cand_score = 4;
127         struct net_device *ndev;
128
129         for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
130                 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
131                     !ipv6_addr_equal(remote, &t->parms.raddr) ||
132                     key != t->parms.i_key ||
133                     !(t->dev->flags & IFF_UP))
134                         continue;
135
136                 if (t->dev->type != ARPHRD_IP6GRE &&
137                     t->dev->type != dev_type)
138                         continue;
139
140                 score = 0;
141                 if (t->parms.link != link)
142                         score |= 1;
143                 if (t->dev->type != dev_type)
144                         score |= 2;
145                 if (score == 0)
146                         return t;
147
148                 if (score < cand_score) {
149                         cand = t;
150                         cand_score = score;
151                 }
152         }
153
154         for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
155                 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
156                     key != t->parms.i_key ||
157                     !(t->dev->flags & IFF_UP))
158                         continue;
159
160                 if (t->dev->type != ARPHRD_IP6GRE &&
161                     t->dev->type != dev_type)
162                         continue;
163
164                 score = 0;
165                 if (t->parms.link != link)
166                         score |= 1;
167                 if (t->dev->type != dev_type)
168                         score |= 2;
169                 if (score == 0)
170                         return t;
171
172                 if (score < cand_score) {
173                         cand = t;
174                         cand_score = score;
175                 }
176         }
177
178         for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
179                 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
180                           (!ipv6_addr_equal(local, &t->parms.raddr) ||
181                                  !ipv6_addr_is_multicast(local))) ||
182                     key != t->parms.i_key ||
183                     !(t->dev->flags & IFF_UP))
184                         continue;
185
186                 if (t->dev->type != ARPHRD_IP6GRE &&
187                     t->dev->type != dev_type)
188                         continue;
189
190                 score = 0;
191                 if (t->parms.link != link)
192                         score |= 1;
193                 if (t->dev->type != dev_type)
194                         score |= 2;
195                 if (score == 0)
196                         return t;
197
198                 if (score < cand_score) {
199                         cand = t;
200                         cand_score = score;
201                 }
202         }
203
204         for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
205                 if (t->parms.i_key != key ||
206                     !(t->dev->flags & IFF_UP))
207                         continue;
208
209                 if (t->dev->type != ARPHRD_IP6GRE &&
210                     t->dev->type != dev_type)
211                         continue;
212
213                 score = 0;
214                 if (t->parms.link != link)
215                         score |= 1;
216                 if (t->dev->type != dev_type)
217                         score |= 2;
218                 if (score == 0)
219                         return t;
220
221                 if (score < cand_score) {
222                         cand = t;
223                         cand_score = score;
224                 }
225         }
226
227         if (cand)
228                 return cand;
229
230         ndev = READ_ONCE(ign->fb_tunnel_dev);
231         if (ndev && ndev->flags & IFF_UP)
232                 return netdev_priv(ndev);
233
234         return NULL;
235 }
236
237 static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
238                 const struct __ip6_tnl_parm *p)
239 {
240         const struct in6_addr *remote = &p->raddr;
241         const struct in6_addr *local = &p->laddr;
242         unsigned int h = HASH_KEY(p->i_key);
243         int prio = 0;
244
245         if (!ipv6_addr_any(local))
246                 prio |= 1;
247         if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
248                 prio |= 2;
249                 h ^= HASH_ADDR(remote);
250         }
251
252         return &ign->tunnels[prio][h];
253 }
254
255 static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
256                 const struct ip6_tnl *t)
257 {
258         return __ip6gre_bucket(ign, &t->parms);
259 }
260
261 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
262 {
263         struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
264
265         rcu_assign_pointer(t->next, rtnl_dereference(*tp));
266         rcu_assign_pointer(*tp, t);
267 }
268
269 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
270 {
271         struct ip6_tnl __rcu **tp;
272         struct ip6_tnl *iter;
273
274         for (tp = ip6gre_bucket(ign, t);
275              (iter = rtnl_dereference(*tp)) != NULL;
276              tp = &iter->next) {
277                 if (t == iter) {
278                         rcu_assign_pointer(*tp, t->next);
279                         break;
280                 }
281         }
282 }
283
284 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
285                                            const struct __ip6_tnl_parm *parms,
286                                            int type)
287 {
288         const struct in6_addr *remote = &parms->raddr;
289         const struct in6_addr *local = &parms->laddr;
290         __be32 key = parms->i_key;
291         int link = parms->link;
292         struct ip6_tnl *t;
293         struct ip6_tnl __rcu **tp;
294         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
295
296         for (tp = __ip6gre_bucket(ign, parms);
297              (t = rtnl_dereference(*tp)) != NULL;
298              tp = &t->next)
299                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
300                     ipv6_addr_equal(remote, &t->parms.raddr) &&
301                     key == t->parms.i_key &&
302                     link == t->parms.link &&
303                     type == t->dev->type)
304                         break;
305
306         return t;
307 }
308
309 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
310                 const struct __ip6_tnl_parm *parms, int create)
311 {
312         struct ip6_tnl *t, *nt;
313         struct net_device *dev;
314         char name[IFNAMSIZ];
315         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
316
317         t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
318         if (t && create)
319                 return NULL;
320         if (t || !create)
321                 return t;
322
323         if (parms->name[0]) {
324                 if (!dev_valid_name(parms->name))
325                         return NULL;
326                 strlcpy(name, parms->name, IFNAMSIZ);
327         } else {
328                 strcpy(name, "ip6gre%d");
329         }
330         dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
331                            ip6gre_tunnel_setup);
332         if (!dev)
333                 return NULL;
334
335         dev_net_set(dev, net);
336
337         nt = netdev_priv(dev);
338         nt->parms = *parms;
339         dev->rtnl_link_ops = &ip6gre_link_ops;
340
341         nt->dev = dev;
342         nt->net = dev_net(dev);
343
344         if (register_netdevice(dev) < 0)
345                 goto failed_free;
346
347         ip6gre_tnl_link_config(nt, 1);
348
349         /* Can use a lockless transmit, unless we generate output sequences */
350         if (!(nt->parms.o_flags & TUNNEL_SEQ))
351                 dev->features |= NETIF_F_LLTX;
352
353         ip6gre_tunnel_link(ign, nt);
354         return nt;
355
356 failed_free:
357         free_netdev(dev);
358         return NULL;
359 }
360
361 static void ip6gre_tunnel_uninit(struct net_device *dev)
362 {
363         struct ip6_tnl *t = netdev_priv(dev);
364         struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
365
366         ip6gre_tunnel_unlink(ign, t);
367         if (ign->fb_tunnel_dev == dev)
368                 WRITE_ONCE(ign->fb_tunnel_dev, NULL);
369         dst_cache_reset(&t->dst_cache);
370         dev_put(dev);
371 }
372
373
374 static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
375                        u8 type, u8 code, int offset, __be32 info)
376 {
377         const struct gre_base_hdr *greh;
378         const struct ipv6hdr *ipv6h;
379         int grehlen = sizeof(*greh);
380         struct ip6_tnl *t;
381         int key_off = 0;
382         __be16 flags;
383         __be32 key;
384
385         if (!pskb_may_pull(skb, offset + grehlen))
386                 return;
387         greh = (const struct gre_base_hdr *)(skb->data + offset);
388         flags = greh->flags;
389         if (flags & (GRE_VERSION | GRE_ROUTING))
390                 return;
391         if (flags & GRE_CSUM)
392                 grehlen += 4;
393         if (flags & GRE_KEY) {
394                 key_off = grehlen + offset;
395                 grehlen += 4;
396         }
397
398         if (!pskb_may_pull(skb, offset + grehlen))
399                 return;
400         ipv6h = (const struct ipv6hdr *)skb->data;
401         greh = (const struct gre_base_hdr *)(skb->data + offset);
402         key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
403
404         t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
405                                  key, greh->protocol);
406         if (!t)
407                 return;
408
409         switch (type) {
410                 __u32 teli;
411                 struct ipv6_tlv_tnl_enc_lim *tel;
412                 __u32 mtu;
413         case ICMPV6_DEST_UNREACH:
414                 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
415                                     t->parms.name);
416                 if (code != ICMPV6_PORT_UNREACH)
417                         break;
418                 return;
419         case ICMPV6_TIME_EXCEED:
420                 if (code == ICMPV6_EXC_HOPLIMIT) {
421                         net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
422                                             t->parms.name);
423                         break;
424                 }
425                 return;
426         case ICMPV6_PARAMPROB:
427                 teli = 0;
428                 if (code == ICMPV6_HDR_FIELD)
429                         teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
430
431                 if (teli && teli == be32_to_cpu(info) - 2) {
432                         tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
433                         if (tel->encap_limit == 0) {
434                                 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
435                                                     t->parms.name);
436                         }
437                 } else {
438                         net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
439                                             t->parms.name);
440                 }
441                 return;
442         case ICMPV6_PKT_TOOBIG:
443                 mtu = be32_to_cpu(info) - offset - t->tun_hlen;
444                 if (t->dev->type == ARPHRD_ETHER)
445                         mtu -= ETH_HLEN;
446                 if (mtu < IPV6_MIN_MTU)
447                         mtu = IPV6_MIN_MTU;
448                 t->dev->mtu = mtu;
449                 return;
450         }
451
452         if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
453                 t->err_count++;
454         else
455                 t->err_count = 1;
456         t->err_time = jiffies;
457 }
458
459 static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
460 {
461         const struct ipv6hdr *ipv6h;
462         struct ip6_tnl *tunnel;
463
464         ipv6h = ipv6_hdr(skb);
465         tunnel = ip6gre_tunnel_lookup(skb->dev,
466                                       &ipv6h->saddr, &ipv6h->daddr, tpi->key,
467                                       tpi->proto);
468         if (tunnel) {
469                 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
470
471                 return PACKET_RCVD;
472         }
473
474         return PACKET_REJECT;
475 }
476
477 static int gre_rcv(struct sk_buff *skb)
478 {
479         struct tnl_ptk_info tpi;
480         bool csum_err = false;
481         int hdr_len;
482
483         hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
484         if (hdr_len < 0)
485                 goto drop;
486
487         if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
488                 goto drop;
489
490         if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
491                 return 0;
492
493         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
494 drop:
495         kfree_skb(skb);
496         return 0;
497 }
498
499 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
500 {
501         return iptunnel_handle_offloads(skb,
502                                         csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
503 }
504
505 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
506                                struct net_device *dev, __u8 dsfield,
507                                struct flowi6 *fl6, int encap_limit,
508                                __u32 *pmtu, __be16 proto)
509 {
510         struct ip6_tnl *tunnel = netdev_priv(dev);
511         struct dst_entry *dst = skb_dst(skb);
512         __be16 protocol;
513
514         if (dev->type == ARPHRD_ETHER)
515                 IPCB(skb)->flags = 0;
516
517         if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
518                 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
519         else
520                 fl6->daddr = tunnel->parms.raddr;
521
522         if (tunnel->parms.o_flags & TUNNEL_SEQ)
523                 tunnel->o_seqno++;
524
525         /* Push GRE header. */
526         protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
527         gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
528                          protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
529
530         /* TooBig packet may have updated dst->dev's mtu */
531         if (dst && dst_mtu(dst) > dst->dev->mtu)
532                 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu, false);
533
534         return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
535                             NEXTHDR_GRE);
536 }
537
538 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
539 {
540         struct ip6_tnl *t = netdev_priv(dev);
541         const struct iphdr  *iph = ip_hdr(skb);
542         int encap_limit = -1;
543         struct flowi6 fl6;
544         __u8 dsfield;
545         __u32 mtu;
546         int err;
547
548         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
549
550         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
551                 encap_limit = t->parms.encap_limit;
552
553         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
554
555         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
556                 dsfield = ipv4_get_dsfield(iph);
557         else
558                 dsfield = ip6_tclass(t->parms.flowinfo);
559         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
560                 fl6.flowi6_mark = skb->mark;
561         else
562                 fl6.flowi6_mark = t->parms.fwmark;
563
564         fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
565
566         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
567         if (err)
568                 return -1;
569
570         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
571                           skb->protocol);
572         if (err != 0) {
573                 /* XXX: send ICMP error even if DF is not set. */
574                 if (err == -EMSGSIZE)
575                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
576                                   htonl(mtu));
577                 return -1;
578         }
579
580         return 0;
581 }
582
583 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
584 {
585         struct ip6_tnl *t = netdev_priv(dev);
586         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
587         int encap_limit = -1;
588         __u16 offset;
589         struct flowi6 fl6;
590         __u8 dsfield;
591         __u32 mtu;
592         int err;
593
594         if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
595                 return -1;
596
597         offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
598         /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
599         ipv6h = ipv6_hdr(skb);
600
601         if (offset > 0) {
602                 struct ipv6_tlv_tnl_enc_lim *tel;
603                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
604                 if (tel->encap_limit == 0) {
605                         icmpv6_send(skb, ICMPV6_PARAMPROB,
606                                     ICMPV6_HDR_FIELD, offset + 2);
607                         return -1;
608                 }
609                 encap_limit = tel->encap_limit - 1;
610         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
611                 encap_limit = t->parms.encap_limit;
612
613         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
614
615         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
616                 dsfield = ipv6_get_dsfield(ipv6h);
617         else
618                 dsfield = ip6_tclass(t->parms.flowinfo);
619
620         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
621                 fl6.flowlabel |= ip6_flowlabel(ipv6h);
622         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
623                 fl6.flowi6_mark = skb->mark;
624         else
625                 fl6.flowi6_mark = t->parms.fwmark;
626
627         fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
628
629         if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
630                 return -1;
631
632         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
633                           &mtu, skb->protocol);
634         if (err != 0) {
635                 if (err == -EMSGSIZE)
636                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
637                 return -1;
638         }
639
640         return 0;
641 }
642
643 /**
644  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
645  *   @t: the outgoing tunnel device
646  *   @hdr: IPv6 header from the incoming packet
647  *
648  * Description:
649  *   Avoid trivial tunneling loop by checking that tunnel exit-point
650  *   doesn't match source of incoming packet.
651  *
652  * Return:
653  *   1 if conflict,
654  *   0 else
655  **/
656
657 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
658         const struct ipv6hdr *hdr)
659 {
660         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
661 }
662
663 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
664 {
665         struct ip6_tnl *t = netdev_priv(dev);
666         int encap_limit = -1;
667         struct flowi6 fl6;
668         __u32 mtu;
669         int err;
670
671         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
672                 encap_limit = t->parms.encap_limit;
673
674         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
675
676         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
677         if (err)
678                 return err;
679
680         err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
681
682         return err;
683 }
684
685 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
686         struct net_device *dev)
687 {
688         struct ip6_tnl *t = netdev_priv(dev);
689         struct net_device_stats *stats = &t->dev->stats;
690         int ret;
691
692         if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
693                 goto tx_err;
694
695         switch (skb->protocol) {
696         case htons(ETH_P_IP):
697                 ret = ip6gre_xmit_ipv4(skb, dev);
698                 break;
699         case htons(ETH_P_IPV6):
700                 ret = ip6gre_xmit_ipv6(skb, dev);
701                 break;
702         default:
703                 ret = ip6gre_xmit_other(skb, dev);
704                 break;
705         }
706
707         if (ret < 0)
708                 goto tx_err;
709
710         return NETDEV_TX_OK;
711
712 tx_err:
713         stats->tx_errors++;
714         stats->tx_dropped++;
715         kfree_skb(skb);
716         return NETDEV_TX_OK;
717 }
718
719 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
720 {
721         struct net_device *dev = t->dev;
722         struct __ip6_tnl_parm *p = &t->parms;
723         struct flowi6 *fl6 = &t->fl.u.ip6;
724         int t_hlen;
725
726         if (dev->type != ARPHRD_ETHER) {
727                 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
728                 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
729         }
730
731         /* Set up flowi template */
732         fl6->saddr = p->laddr;
733         fl6->daddr = p->raddr;
734         fl6->flowi6_oif = p->link;
735         fl6->flowlabel = 0;
736         fl6->flowi6_proto = IPPROTO_GRE;
737
738         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
739                 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
740         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
741                 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
742
743         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
744         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
745
746         if (p->flags&IP6_TNL_F_CAP_XMIT &&
747                         p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
748                 dev->flags |= IFF_POINTOPOINT;
749         else
750                 dev->flags &= ~IFF_POINTOPOINT;
751
752         t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
753
754         t->hlen = t->encap_hlen + t->tun_hlen;
755
756         t_hlen = t->hlen + sizeof(struct ipv6hdr);
757
758         if (p->flags & IP6_TNL_F_CAP_XMIT) {
759                 int strict = (ipv6_addr_type(&p->raddr) &
760                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
761
762                 struct rt6_info *rt = rt6_lookup(t->net,
763                                                  &p->raddr, &p->laddr,
764                                                  p->link, strict);
765
766                 if (!rt)
767                         return;
768
769                 if (rt->dst.dev) {
770                         dev->hard_header_len = rt->dst.dev->hard_header_len +
771                                                t_hlen;
772
773                         if (set_mtu) {
774                                 int mtu = rt->dst.dev->mtu - t_hlen;
775
776                                 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
777                                         mtu -= 8;
778                                 if (dev->type == ARPHRD_ETHER)
779                                         mtu -= ETH_HLEN;
780
781                                 if (mtu < IPV6_MIN_MTU)
782                                         mtu = IPV6_MIN_MTU;
783                                 WRITE_ONCE(dev->mtu, mtu);
784                         }
785                 }
786                 ip6_rt_put(rt);
787         }
788 }
789
790 static int ip6gre_tnl_change(struct ip6_tnl *t,
791         const struct __ip6_tnl_parm *p, int set_mtu)
792 {
793         t->parms.laddr = p->laddr;
794         t->parms.raddr = p->raddr;
795         t->parms.flags = p->flags;
796         t->parms.hop_limit = p->hop_limit;
797         t->parms.encap_limit = p->encap_limit;
798         t->parms.flowinfo = p->flowinfo;
799         t->parms.link = p->link;
800         t->parms.proto = p->proto;
801         t->parms.i_key = p->i_key;
802         t->parms.o_key = p->o_key;
803         t->parms.i_flags = p->i_flags;
804         t->parms.o_flags = p->o_flags;
805         t->parms.fwmark = p->fwmark;
806         dst_cache_reset(&t->dst_cache);
807         ip6gre_tnl_link_config(t, set_mtu);
808         return 0;
809 }
810
811 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
812         const struct ip6_tnl_parm2 *u)
813 {
814         p->laddr = u->laddr;
815         p->raddr = u->raddr;
816         p->flags = u->flags;
817         p->hop_limit = u->hop_limit;
818         p->encap_limit = u->encap_limit;
819         p->flowinfo = u->flowinfo;
820         p->link = u->link;
821         p->i_key = u->i_key;
822         p->o_key = u->o_key;
823         p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
824         p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
825         memcpy(p->name, u->name, sizeof(u->name));
826 }
827
828 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
829         const struct __ip6_tnl_parm *p)
830 {
831         u->proto = IPPROTO_GRE;
832         u->laddr = p->laddr;
833         u->raddr = p->raddr;
834         u->flags = p->flags;
835         u->hop_limit = p->hop_limit;
836         u->encap_limit = p->encap_limit;
837         u->flowinfo = p->flowinfo;
838         u->link = p->link;
839         u->i_key = p->i_key;
840         u->o_key = p->o_key;
841         u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
842         u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
843         memcpy(u->name, p->name, sizeof(u->name));
844 }
845
846 static int ip6gre_tunnel_ioctl(struct net_device *dev,
847         struct ifreq *ifr, int cmd)
848 {
849         int err = 0;
850         struct ip6_tnl_parm2 p;
851         struct __ip6_tnl_parm p1;
852         struct ip6_tnl *t = netdev_priv(dev);
853         struct net *net = t->net;
854         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
855
856         memset(&p1, 0, sizeof(p1));
857
858         switch (cmd) {
859         case SIOCGETTUNNEL:
860                 if (dev == ign->fb_tunnel_dev) {
861                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
862                                 err = -EFAULT;
863                                 break;
864                         }
865                         ip6gre_tnl_parm_from_user(&p1, &p);
866                         t = ip6gre_tunnel_locate(net, &p1, 0);
867                         if (!t)
868                                 t = netdev_priv(dev);
869                 }
870                 memset(&p, 0, sizeof(p));
871                 ip6gre_tnl_parm_to_user(&p, &t->parms);
872                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
873                         err = -EFAULT;
874                 break;
875
876         case SIOCADDTUNNEL:
877         case SIOCCHGTUNNEL:
878                 err = -EPERM;
879                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
880                         goto done;
881
882                 err = -EFAULT;
883                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
884                         goto done;
885
886                 err = -EINVAL;
887                 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
888                         goto done;
889
890                 if (!(p.i_flags&GRE_KEY))
891                         p.i_key = 0;
892                 if (!(p.o_flags&GRE_KEY))
893                         p.o_key = 0;
894
895                 ip6gre_tnl_parm_from_user(&p1, &p);
896                 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
897
898                 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
899                         if (t) {
900                                 if (t->dev != dev) {
901                                         err = -EEXIST;
902                                         break;
903                                 }
904                         } else {
905                                 t = netdev_priv(dev);
906
907                                 ip6gre_tunnel_unlink(ign, t);
908                                 synchronize_net();
909                                 ip6gre_tnl_change(t, &p1, 1);
910                                 ip6gre_tunnel_link(ign, t);
911                                 netdev_state_change(dev);
912                         }
913                 }
914
915                 if (t) {
916                         err = 0;
917
918                         memset(&p, 0, sizeof(p));
919                         ip6gre_tnl_parm_to_user(&p, &t->parms);
920                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
921                                 err = -EFAULT;
922                 } else
923                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
924                 break;
925
926         case SIOCDELTUNNEL:
927                 err = -EPERM;
928                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
929                         goto done;
930
931                 if (dev == ign->fb_tunnel_dev) {
932                         err = -EFAULT;
933                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
934                                 goto done;
935                         err = -ENOENT;
936                         ip6gre_tnl_parm_from_user(&p1, &p);
937                         t = ip6gre_tunnel_locate(net, &p1, 0);
938                         if (!t)
939                                 goto done;
940                         err = -EPERM;
941                         if (t == netdev_priv(ign->fb_tunnel_dev))
942                                 goto done;
943                         dev = t->dev;
944                 }
945                 unregister_netdevice(dev);
946                 err = 0;
947                 break;
948
949         default:
950                 err = -EINVAL;
951         }
952
953 done:
954         return err;
955 }
956
957 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
958                          unsigned short type, const void *daddr,
959                          const void *saddr, unsigned int len)
960 {
961         struct ip6_tnl *t = netdev_priv(dev);
962         struct ipv6hdr *ipv6h;
963         __be16 *p;
964
965         ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
966         ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
967                                                   t->fl.u.ip6.flowlabel,
968                                                   true, &t->fl.u.ip6));
969         ipv6h->hop_limit = t->parms.hop_limit;
970         ipv6h->nexthdr = NEXTHDR_GRE;
971         ipv6h->saddr = t->parms.laddr;
972         ipv6h->daddr = t->parms.raddr;
973
974         p = (__be16 *)(ipv6h + 1);
975         p[0] = t->parms.o_flags;
976         p[1] = htons(type);
977
978         /*
979          *      Set the source hardware address.
980          */
981
982         if (saddr)
983                 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
984         if (daddr)
985                 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
986         if (!ipv6_addr_any(&ipv6h->daddr))
987                 return t->hlen;
988
989         return -t->hlen;
990 }
991
992 static const struct header_ops ip6gre_header_ops = {
993         .create = ip6gre_header,
994 };
995
996 static const struct net_device_ops ip6gre_netdev_ops = {
997         .ndo_init               = ip6gre_tunnel_init,
998         .ndo_uninit             = ip6gre_tunnel_uninit,
999         .ndo_start_xmit         = ip6gre_tunnel_xmit,
1000         .ndo_do_ioctl           = ip6gre_tunnel_ioctl,
1001         .ndo_change_mtu         = ip6_tnl_change_mtu,
1002         .ndo_get_stats64        = ip_tunnel_get_stats64,
1003         .ndo_get_iflink         = ip6_tnl_get_iflink,
1004 };
1005
1006 static void ip6gre_dev_free(struct net_device *dev)
1007 {
1008         struct ip6_tnl *t = netdev_priv(dev);
1009
1010         dst_cache_destroy(&t->dst_cache);
1011         free_percpu(dev->tstats);
1012 }
1013
1014 static void ip6gre_tunnel_setup(struct net_device *dev)
1015 {
1016         dev->netdev_ops = &ip6gre_netdev_ops;
1017         dev->needs_free_netdev = true;
1018         dev->priv_destructor = ip6gre_dev_free;
1019
1020         dev->type = ARPHRD_IP6GRE;
1021
1022         dev->flags |= IFF_NOARP;
1023         dev->addr_len = sizeof(struct in6_addr);
1024         netif_keep_dst(dev);
1025         /* This perm addr will be used as interface identifier by IPv6 */
1026         dev->addr_assign_type = NET_ADDR_RANDOM;
1027         eth_random_addr(dev->perm_addr);
1028 }
1029
1030 #define GRE6_FEATURES (NETIF_F_SG |             \
1031                        NETIF_F_FRAGLIST |       \
1032                        NETIF_F_HIGHDMA |        \
1033                        NETIF_F_HW_CSUM)
1034
1035 static void ip6gre_tnl_init_features(struct net_device *dev)
1036 {
1037         struct ip6_tnl *nt = netdev_priv(dev);
1038
1039         dev->features           |= GRE6_FEATURES;
1040         dev->hw_features        |= GRE6_FEATURES;
1041
1042         if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1043                 /* TCP offload with GRE SEQ is not supported, nor
1044                  * can we support 2 levels of outer headers requiring
1045                  * an update.
1046                  */
1047                 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1048                     nt->encap.type == TUNNEL_ENCAP_NONE) {
1049                         dev->features    |= NETIF_F_GSO_SOFTWARE;
1050                         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1051                 }
1052
1053                 /* Can use a lockless transmit, unless we generate
1054                  * output sequences
1055                  */
1056                 dev->features |= NETIF_F_LLTX;
1057         }
1058 }
1059
1060 static int ip6gre_tunnel_init_common(struct net_device *dev)
1061 {
1062         struct ip6_tnl *tunnel;
1063         int ret;
1064         int t_hlen;
1065
1066         tunnel = netdev_priv(dev);
1067
1068         tunnel->dev = dev;
1069         tunnel->net = dev_net(dev);
1070         strcpy(tunnel->parms.name, dev->name);
1071
1072         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1073         if (!dev->tstats)
1074                 return -ENOMEM;
1075
1076         ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1077         if (ret) {
1078                 free_percpu(dev->tstats);
1079                 dev->tstats = NULL;
1080                 return ret;
1081         }
1082
1083         tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1084         tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1085         t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1086
1087         dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1088         dev->mtu = ETH_DATA_LEN - t_hlen;
1089         if (dev->type == ARPHRD_ETHER)
1090                 dev->mtu -= ETH_HLEN;
1091         if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1092                 dev->mtu -= 8;
1093
1094         ip6gre_tnl_init_features(dev);
1095
1096         return 0;
1097 }
1098
1099 static int ip6gre_tunnel_init(struct net_device *dev)
1100 {
1101         struct ip6_tnl *tunnel;
1102         int ret;
1103
1104         ret = ip6gre_tunnel_init_common(dev);
1105         if (ret)
1106                 return ret;
1107
1108         tunnel = netdev_priv(dev);
1109
1110         memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1111         memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1112
1113         if (ipv6_addr_any(&tunnel->parms.raddr))
1114                 dev->header_ops = &ip6gre_header_ops;
1115
1116         return 0;
1117 }
1118
1119 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1120 {
1121         struct ip6_tnl *tunnel = netdev_priv(dev);
1122
1123         tunnel->dev = dev;
1124         tunnel->net = dev_net(dev);
1125         strcpy(tunnel->parms.name, dev->name);
1126
1127         tunnel->hlen            = sizeof(struct ipv6hdr) + 4;
1128 }
1129
1130
1131 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1132         .handler     = gre_rcv,
1133         .err_handler = ip6gre_err,
1134         .flags       = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1135 };
1136
1137 static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
1138 {
1139         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1140         struct net_device *dev, *aux;
1141         int prio;
1142
1143         for_each_netdev_safe(net, dev, aux)
1144                 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1145                     dev->rtnl_link_ops == &ip6gre_tap_ops)
1146                         unregister_netdevice_queue(dev, head);
1147
1148         for (prio = 0; prio < 4; prio++) {
1149                 int h;
1150                 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1151                         struct ip6_tnl *t;
1152
1153                         t = rtnl_dereference(ign->tunnels[prio][h]);
1154
1155                         while (t) {
1156                                 /* If dev is in the same netns, it has already
1157                                  * been added to the list by the previous loop.
1158                                  */
1159                                 if (!net_eq(dev_net(t->dev), net))
1160                                         unregister_netdevice_queue(t->dev,
1161                                                                    head);
1162                                 t = rtnl_dereference(t->next);
1163                         }
1164                 }
1165         }
1166 }
1167
1168 static int __net_init ip6gre_init_net(struct net *net)
1169 {
1170         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1171         struct net_device *ndev;
1172         int err;
1173
1174         ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1175                             NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1176         if (!ndev) {
1177                 err = -ENOMEM;
1178                 goto err_alloc_dev;
1179         }
1180         ign->fb_tunnel_dev = ndev;
1181         dev_net_set(ign->fb_tunnel_dev, net);
1182         /* FB netdevice is special: we have one, and only one per netns.
1183          * Allowing to move it to another netns is clearly unsafe.
1184          */
1185         ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1186
1187
1188         ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1189         ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1190
1191         err = register_netdev(ign->fb_tunnel_dev);
1192         if (err)
1193                 goto err_reg_dev;
1194
1195         rcu_assign_pointer(ign->tunnels_wc[0],
1196                            netdev_priv(ign->fb_tunnel_dev));
1197         return 0;
1198
1199 err_reg_dev:
1200         free_netdev(ndev);
1201 err_alloc_dev:
1202         return err;
1203 }
1204
1205 static void __net_exit ip6gre_exit_net(struct net *net)
1206 {
1207         LIST_HEAD(list);
1208
1209         rtnl_lock();
1210         ip6gre_destroy_tunnels(net, &list);
1211         unregister_netdevice_many(&list);
1212         rtnl_unlock();
1213 }
1214
1215 static struct pernet_operations ip6gre_net_ops = {
1216         .init = ip6gre_init_net,
1217         .exit = ip6gre_exit_net,
1218         .id   = &ip6gre_net_id,
1219         .size = sizeof(struct ip6gre_net),
1220 };
1221
1222 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1223                                   struct netlink_ext_ack *extack)
1224 {
1225         __be16 flags;
1226
1227         if (!data)
1228                 return 0;
1229
1230         flags = 0;
1231         if (data[IFLA_GRE_IFLAGS])
1232                 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1233         if (data[IFLA_GRE_OFLAGS])
1234                 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1235         if (flags & (GRE_VERSION|GRE_ROUTING))
1236                 return -EINVAL;
1237
1238         return 0;
1239 }
1240
1241 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1242                                struct netlink_ext_ack *extack)
1243 {
1244         struct in6_addr daddr;
1245
1246         if (tb[IFLA_ADDRESS]) {
1247                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1248                         return -EINVAL;
1249                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1250                         return -EADDRNOTAVAIL;
1251         }
1252
1253         if (!data)
1254                 goto out;
1255
1256         if (data[IFLA_GRE_REMOTE]) {
1257                 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1258                 if (ipv6_addr_any(&daddr))
1259                         return -EINVAL;
1260         }
1261
1262 out:
1263         return ip6gre_tunnel_validate(tb, data, extack);
1264 }
1265
1266
1267 static void ip6gre_netlink_parms(struct nlattr *data[],
1268                                 struct __ip6_tnl_parm *parms)
1269 {
1270         memset(parms, 0, sizeof(*parms));
1271
1272         if (!data)
1273                 return;
1274
1275         if (data[IFLA_GRE_LINK])
1276                 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1277
1278         if (data[IFLA_GRE_IFLAGS])
1279                 parms->i_flags = gre_flags_to_tnl_flags(
1280                                 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1281
1282         if (data[IFLA_GRE_OFLAGS])
1283                 parms->o_flags = gre_flags_to_tnl_flags(
1284                                 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1285
1286         if (data[IFLA_GRE_IKEY])
1287                 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1288
1289         if (data[IFLA_GRE_OKEY])
1290                 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1291
1292         if (data[IFLA_GRE_LOCAL])
1293                 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1294
1295         if (data[IFLA_GRE_REMOTE])
1296                 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1297
1298         if (data[IFLA_GRE_TTL])
1299                 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1300
1301         if (data[IFLA_GRE_ENCAP_LIMIT])
1302                 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1303
1304         if (data[IFLA_GRE_FLOWINFO])
1305                 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1306
1307         if (data[IFLA_GRE_FLAGS])
1308                 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1309
1310         if (data[IFLA_GRE_FWMARK])
1311                 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1312 }
1313
1314 static int ip6gre_tap_init(struct net_device *dev)
1315 {
1316         int ret;
1317
1318         ret = ip6gre_tunnel_init_common(dev);
1319         if (ret)
1320                 return ret;
1321
1322         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1323
1324         return 0;
1325 }
1326
1327 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1328         .ndo_init = ip6gre_tap_init,
1329         .ndo_uninit = ip6gre_tunnel_uninit,
1330         .ndo_start_xmit = ip6gre_tunnel_xmit,
1331         .ndo_set_mac_address = eth_mac_addr,
1332         .ndo_validate_addr = eth_validate_addr,
1333         .ndo_change_mtu = ip6_tnl_change_mtu,
1334         .ndo_get_stats64 = ip_tunnel_get_stats64,
1335         .ndo_get_iflink = ip6_tnl_get_iflink,
1336 };
1337
1338 static void ip6gre_tap_setup(struct net_device *dev)
1339 {
1340
1341         ether_setup(dev);
1342
1343         dev->max_mtu = 0;
1344         dev->netdev_ops = &ip6gre_tap_netdev_ops;
1345         dev->needs_free_netdev = true;
1346         dev->priv_destructor = ip6gre_dev_free;
1347
1348         dev->features |= NETIF_F_NETNS_LOCAL;
1349         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1350         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1351         netif_keep_dst(dev);
1352 }
1353
1354 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1355                                        struct ip_tunnel_encap *ipencap)
1356 {
1357         bool ret = false;
1358
1359         memset(ipencap, 0, sizeof(*ipencap));
1360
1361         if (!data)
1362                 return ret;
1363
1364         if (data[IFLA_GRE_ENCAP_TYPE]) {
1365                 ret = true;
1366                 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1367         }
1368
1369         if (data[IFLA_GRE_ENCAP_FLAGS]) {
1370                 ret = true;
1371                 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1372         }
1373
1374         if (data[IFLA_GRE_ENCAP_SPORT]) {
1375                 ret = true;
1376                 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1377         }
1378
1379         if (data[IFLA_GRE_ENCAP_DPORT]) {
1380                 ret = true;
1381                 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1382         }
1383
1384         return ret;
1385 }
1386
1387 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1388                           struct nlattr *tb[], struct nlattr *data[],
1389                           struct netlink_ext_ack *extack)
1390 {
1391         struct ip6_tnl *nt;
1392         struct net *net = dev_net(dev);
1393         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1394         struct ip_tunnel_encap ipencap;
1395         int err;
1396
1397         nt = netdev_priv(dev);
1398
1399         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1400                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1401
1402                 if (err < 0)
1403                         return err;
1404         }
1405
1406         ip6gre_netlink_parms(data, &nt->parms);
1407
1408         if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1409                 return -EEXIST;
1410
1411         if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1412                 eth_hw_addr_random(dev);
1413
1414         nt->dev = dev;
1415         nt->net = dev_net(dev);
1416
1417         err = register_netdevice(dev);
1418         if (err)
1419                 goto out;
1420
1421         ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1422
1423         if (tb[IFLA_MTU])
1424                 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1425
1426         dev_hold(dev);
1427         ip6gre_tunnel_link(ign, nt);
1428
1429 out:
1430         return err;
1431 }
1432
1433 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1434                              struct nlattr *data[],
1435                              struct netlink_ext_ack *extack)
1436 {
1437         struct ip6_tnl *t, *nt = netdev_priv(dev);
1438         struct net *net = nt->net;
1439         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1440         struct __ip6_tnl_parm p;
1441         struct ip_tunnel_encap ipencap;
1442
1443         if (dev == ign->fb_tunnel_dev)
1444                 return -EINVAL;
1445
1446         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1447                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1448
1449                 if (err < 0)
1450                         return err;
1451         }
1452
1453         ip6gre_netlink_parms(data, &p);
1454
1455         t = ip6gre_tunnel_locate(net, &p, 0);
1456
1457         if (t) {
1458                 if (t->dev != dev)
1459                         return -EEXIST;
1460         } else {
1461                 t = nt;
1462         }
1463
1464         ip6gre_tunnel_unlink(ign, t);
1465         ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1466         ip6gre_tunnel_link(ign, t);
1467         return 0;
1468 }
1469
1470 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1471 {
1472         struct net *net = dev_net(dev);
1473         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1474
1475         if (dev != ign->fb_tunnel_dev)
1476                 unregister_netdevice_queue(dev, head);
1477 }
1478
1479 static size_t ip6gre_get_size(const struct net_device *dev)
1480 {
1481         return
1482                 /* IFLA_GRE_LINK */
1483                 nla_total_size(4) +
1484                 /* IFLA_GRE_IFLAGS */
1485                 nla_total_size(2) +
1486                 /* IFLA_GRE_OFLAGS */
1487                 nla_total_size(2) +
1488                 /* IFLA_GRE_IKEY */
1489                 nla_total_size(4) +
1490                 /* IFLA_GRE_OKEY */
1491                 nla_total_size(4) +
1492                 /* IFLA_GRE_LOCAL */
1493                 nla_total_size(sizeof(struct in6_addr)) +
1494                 /* IFLA_GRE_REMOTE */
1495                 nla_total_size(sizeof(struct in6_addr)) +
1496                 /* IFLA_GRE_TTL */
1497                 nla_total_size(1) +
1498                 /* IFLA_GRE_ENCAP_LIMIT */
1499                 nla_total_size(1) +
1500                 /* IFLA_GRE_FLOWINFO */
1501                 nla_total_size(4) +
1502                 /* IFLA_GRE_FLAGS */
1503                 nla_total_size(4) +
1504                 /* IFLA_GRE_ENCAP_TYPE */
1505                 nla_total_size(2) +
1506                 /* IFLA_GRE_ENCAP_FLAGS */
1507                 nla_total_size(2) +
1508                 /* IFLA_GRE_ENCAP_SPORT */
1509                 nla_total_size(2) +
1510                 /* IFLA_GRE_ENCAP_DPORT */
1511                 nla_total_size(2) +
1512                 /* IFLA_GRE_FWMARK */
1513                 nla_total_size(4) +
1514                 0;
1515 }
1516
1517 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1518 {
1519         struct ip6_tnl *t = netdev_priv(dev);
1520         struct __ip6_tnl_parm *p = &t->parms;
1521
1522         if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1523             nla_put_be16(skb, IFLA_GRE_IFLAGS,
1524                          gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1525             nla_put_be16(skb, IFLA_GRE_OFLAGS,
1526                          gre_tnl_flags_to_gre_flags(p->o_flags)) ||
1527             nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1528             nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1529             nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1530             nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
1531             nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1532             nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1533             nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1534             nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
1535             nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
1536                 goto nla_put_failure;
1537
1538         if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1539                         t->encap.type) ||
1540             nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1541                          t->encap.sport) ||
1542             nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1543                          t->encap.dport) ||
1544             nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1545                         t->encap.flags))
1546                 goto nla_put_failure;
1547
1548         return 0;
1549
1550 nla_put_failure:
1551         return -EMSGSIZE;
1552 }
1553
1554 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1555         [IFLA_GRE_LINK]        = { .type = NLA_U32 },
1556         [IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
1557         [IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
1558         [IFLA_GRE_IKEY]        = { .type = NLA_U32 },
1559         [IFLA_GRE_OKEY]        = { .type = NLA_U32 },
1560         [IFLA_GRE_LOCAL]       = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1561         [IFLA_GRE_REMOTE]      = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1562         [IFLA_GRE_TTL]         = { .type = NLA_U8 },
1563         [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1564         [IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
1565         [IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
1566         [IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
1567         [IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
1568         [IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
1569         [IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
1570         [IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
1571 };
1572
1573 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1574         .kind           = "ip6gre",
1575         .maxtype        = IFLA_GRE_MAX,
1576         .policy         = ip6gre_policy,
1577         .priv_size      = sizeof(struct ip6_tnl),
1578         .setup          = ip6gre_tunnel_setup,
1579         .validate       = ip6gre_tunnel_validate,
1580         .newlink        = ip6gre_newlink,
1581         .changelink     = ip6gre_changelink,
1582         .dellink        = ip6gre_dellink,
1583         .get_size       = ip6gre_get_size,
1584         .fill_info      = ip6gre_fill_info,
1585         .get_link_net   = ip6_tnl_get_link_net,
1586 };
1587
1588 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1589         .kind           = "ip6gretap",
1590         .maxtype        = IFLA_GRE_MAX,
1591         .policy         = ip6gre_policy,
1592         .priv_size      = sizeof(struct ip6_tnl),
1593         .setup          = ip6gre_tap_setup,
1594         .validate       = ip6gre_tap_validate,
1595         .newlink        = ip6gre_newlink,
1596         .changelink     = ip6gre_changelink,
1597         .get_size       = ip6gre_get_size,
1598         .fill_info      = ip6gre_fill_info,
1599         .get_link_net   = ip6_tnl_get_link_net,
1600 };
1601
1602 /*
1603  *      And now the modules code and kernel interface.
1604  */
1605
1606 static int __init ip6gre_init(void)
1607 {
1608         int err;
1609
1610         pr_info("GRE over IPv6 tunneling driver\n");
1611
1612         err = register_pernet_device(&ip6gre_net_ops);
1613         if (err < 0)
1614                 return err;
1615
1616         err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1617         if (err < 0) {
1618                 pr_info("%s: can't add protocol\n", __func__);
1619                 goto add_proto_failed;
1620         }
1621
1622         err = rtnl_link_register(&ip6gre_link_ops);
1623         if (err < 0)
1624                 goto rtnl_link_failed;
1625
1626         err = rtnl_link_register(&ip6gre_tap_ops);
1627         if (err < 0)
1628                 goto tap_ops_failed;
1629
1630 out:
1631         return err;
1632
1633 tap_ops_failed:
1634         rtnl_link_unregister(&ip6gre_link_ops);
1635 rtnl_link_failed:
1636         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1637 add_proto_failed:
1638         unregister_pernet_device(&ip6gre_net_ops);
1639         goto out;
1640 }
1641
1642 static void __exit ip6gre_fini(void)
1643 {
1644         rtnl_link_unregister(&ip6gre_tap_ops);
1645         rtnl_link_unregister(&ip6gre_link_ops);
1646         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1647         unregister_pernet_device(&ip6gre_net_ops);
1648 }
1649
1650 module_init(ip6gre_init);
1651 module_exit(ip6gre_fini);
1652 MODULE_LICENSE("GPL");
1653 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1654 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1655 MODULE_ALIAS_RTNL_LINK("ip6gre");
1656 MODULE_ALIAS_RTNL_LINK("ip6gretap");
1657 MODULE_ALIAS_NETDEV("ip6gre0");