GNU Linux-libre 4.14.266-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                                 dev->mtu = rt->dst.dev->mtu - t_hlen;
775                                 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
776                                         dev->mtu -= 8;
777                                 if (dev->type == ARPHRD_ETHER)
778                                         dev->mtu -= ETH_HLEN;
779
780                                 if (dev->mtu < IPV6_MIN_MTU)
781                                         dev->mtu = IPV6_MIN_MTU;
782                         }
783                 }
784                 ip6_rt_put(rt);
785         }
786 }
787
788 static int ip6gre_tnl_change(struct ip6_tnl *t,
789         const struct __ip6_tnl_parm *p, int set_mtu)
790 {
791         t->parms.laddr = p->laddr;
792         t->parms.raddr = p->raddr;
793         t->parms.flags = p->flags;
794         t->parms.hop_limit = p->hop_limit;
795         t->parms.encap_limit = p->encap_limit;
796         t->parms.flowinfo = p->flowinfo;
797         t->parms.link = p->link;
798         t->parms.proto = p->proto;
799         t->parms.i_key = p->i_key;
800         t->parms.o_key = p->o_key;
801         t->parms.i_flags = p->i_flags;
802         t->parms.o_flags = p->o_flags;
803         t->parms.fwmark = p->fwmark;
804         dst_cache_reset(&t->dst_cache);
805         ip6gre_tnl_link_config(t, set_mtu);
806         return 0;
807 }
808
809 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
810         const struct ip6_tnl_parm2 *u)
811 {
812         p->laddr = u->laddr;
813         p->raddr = u->raddr;
814         p->flags = u->flags;
815         p->hop_limit = u->hop_limit;
816         p->encap_limit = u->encap_limit;
817         p->flowinfo = u->flowinfo;
818         p->link = u->link;
819         p->i_key = u->i_key;
820         p->o_key = u->o_key;
821         p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
822         p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
823         memcpy(p->name, u->name, sizeof(u->name));
824 }
825
826 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
827         const struct __ip6_tnl_parm *p)
828 {
829         u->proto = IPPROTO_GRE;
830         u->laddr = p->laddr;
831         u->raddr = p->raddr;
832         u->flags = p->flags;
833         u->hop_limit = p->hop_limit;
834         u->encap_limit = p->encap_limit;
835         u->flowinfo = p->flowinfo;
836         u->link = p->link;
837         u->i_key = p->i_key;
838         u->o_key = p->o_key;
839         u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
840         u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
841         memcpy(u->name, p->name, sizeof(u->name));
842 }
843
844 static int ip6gre_tunnel_ioctl(struct net_device *dev,
845         struct ifreq *ifr, int cmd)
846 {
847         int err = 0;
848         struct ip6_tnl_parm2 p;
849         struct __ip6_tnl_parm p1;
850         struct ip6_tnl *t = netdev_priv(dev);
851         struct net *net = t->net;
852         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
853
854         memset(&p1, 0, sizeof(p1));
855
856         switch (cmd) {
857         case SIOCGETTUNNEL:
858                 if (dev == ign->fb_tunnel_dev) {
859                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
860                                 err = -EFAULT;
861                                 break;
862                         }
863                         ip6gre_tnl_parm_from_user(&p1, &p);
864                         t = ip6gre_tunnel_locate(net, &p1, 0);
865                         if (!t)
866                                 t = netdev_priv(dev);
867                 }
868                 memset(&p, 0, sizeof(p));
869                 ip6gre_tnl_parm_to_user(&p, &t->parms);
870                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
871                         err = -EFAULT;
872                 break;
873
874         case SIOCADDTUNNEL:
875         case SIOCCHGTUNNEL:
876                 err = -EPERM;
877                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
878                         goto done;
879
880                 err = -EFAULT;
881                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
882                         goto done;
883
884                 err = -EINVAL;
885                 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
886                         goto done;
887
888                 if (!(p.i_flags&GRE_KEY))
889                         p.i_key = 0;
890                 if (!(p.o_flags&GRE_KEY))
891                         p.o_key = 0;
892
893                 ip6gre_tnl_parm_from_user(&p1, &p);
894                 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
895
896                 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
897                         if (t) {
898                                 if (t->dev != dev) {
899                                         err = -EEXIST;
900                                         break;
901                                 }
902                         } else {
903                                 t = netdev_priv(dev);
904
905                                 ip6gre_tunnel_unlink(ign, t);
906                                 synchronize_net();
907                                 ip6gre_tnl_change(t, &p1, 1);
908                                 ip6gre_tunnel_link(ign, t);
909                                 netdev_state_change(dev);
910                         }
911                 }
912
913                 if (t) {
914                         err = 0;
915
916                         memset(&p, 0, sizeof(p));
917                         ip6gre_tnl_parm_to_user(&p, &t->parms);
918                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
919                                 err = -EFAULT;
920                 } else
921                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
922                 break;
923
924         case SIOCDELTUNNEL:
925                 err = -EPERM;
926                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
927                         goto done;
928
929                 if (dev == ign->fb_tunnel_dev) {
930                         err = -EFAULT;
931                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
932                                 goto done;
933                         err = -ENOENT;
934                         ip6gre_tnl_parm_from_user(&p1, &p);
935                         t = ip6gre_tunnel_locate(net, &p1, 0);
936                         if (!t)
937                                 goto done;
938                         err = -EPERM;
939                         if (t == netdev_priv(ign->fb_tunnel_dev))
940                                 goto done;
941                         dev = t->dev;
942                 }
943                 unregister_netdevice(dev);
944                 err = 0;
945                 break;
946
947         default:
948                 err = -EINVAL;
949         }
950
951 done:
952         return err;
953 }
954
955 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
956                          unsigned short type, const void *daddr,
957                          const void *saddr, unsigned int len)
958 {
959         struct ip6_tnl *t = netdev_priv(dev);
960         struct ipv6hdr *ipv6h;
961         __be16 *p;
962
963         ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
964         ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
965                                                   t->fl.u.ip6.flowlabel,
966                                                   true, &t->fl.u.ip6));
967         ipv6h->hop_limit = t->parms.hop_limit;
968         ipv6h->nexthdr = NEXTHDR_GRE;
969         ipv6h->saddr = t->parms.laddr;
970         ipv6h->daddr = t->parms.raddr;
971
972         p = (__be16 *)(ipv6h + 1);
973         p[0] = t->parms.o_flags;
974         p[1] = htons(type);
975
976         /*
977          *      Set the source hardware address.
978          */
979
980         if (saddr)
981                 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
982         if (daddr)
983                 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
984         if (!ipv6_addr_any(&ipv6h->daddr))
985                 return t->hlen;
986
987         return -t->hlen;
988 }
989
990 static const struct header_ops ip6gre_header_ops = {
991         .create = ip6gre_header,
992 };
993
994 static const struct net_device_ops ip6gre_netdev_ops = {
995         .ndo_init               = ip6gre_tunnel_init,
996         .ndo_uninit             = ip6gre_tunnel_uninit,
997         .ndo_start_xmit         = ip6gre_tunnel_xmit,
998         .ndo_do_ioctl           = ip6gre_tunnel_ioctl,
999         .ndo_change_mtu         = ip6_tnl_change_mtu,
1000         .ndo_get_stats64        = ip_tunnel_get_stats64,
1001         .ndo_get_iflink         = ip6_tnl_get_iflink,
1002 };
1003
1004 static void ip6gre_dev_free(struct net_device *dev)
1005 {
1006         struct ip6_tnl *t = netdev_priv(dev);
1007
1008         dst_cache_destroy(&t->dst_cache);
1009         free_percpu(dev->tstats);
1010 }
1011
1012 static void ip6gre_tunnel_setup(struct net_device *dev)
1013 {
1014         dev->netdev_ops = &ip6gre_netdev_ops;
1015         dev->needs_free_netdev = true;
1016         dev->priv_destructor = ip6gre_dev_free;
1017
1018         dev->type = ARPHRD_IP6GRE;
1019
1020         dev->flags |= IFF_NOARP;
1021         dev->addr_len = sizeof(struct in6_addr);
1022         netif_keep_dst(dev);
1023         /* This perm addr will be used as interface identifier by IPv6 */
1024         dev->addr_assign_type = NET_ADDR_RANDOM;
1025         eth_random_addr(dev->perm_addr);
1026 }
1027
1028 #define GRE6_FEATURES (NETIF_F_SG |             \
1029                        NETIF_F_FRAGLIST |       \
1030                        NETIF_F_HIGHDMA |        \
1031                        NETIF_F_HW_CSUM)
1032
1033 static void ip6gre_tnl_init_features(struct net_device *dev)
1034 {
1035         struct ip6_tnl *nt = netdev_priv(dev);
1036
1037         dev->features           |= GRE6_FEATURES;
1038         dev->hw_features        |= GRE6_FEATURES;
1039
1040         if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1041                 /* TCP offload with GRE SEQ is not supported, nor
1042                  * can we support 2 levels of outer headers requiring
1043                  * an update.
1044                  */
1045                 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1046                     nt->encap.type == TUNNEL_ENCAP_NONE) {
1047                         dev->features    |= NETIF_F_GSO_SOFTWARE;
1048                         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1049                 }
1050
1051                 /* Can use a lockless transmit, unless we generate
1052                  * output sequences
1053                  */
1054                 dev->features |= NETIF_F_LLTX;
1055         }
1056 }
1057
1058 static int ip6gre_tunnel_init_common(struct net_device *dev)
1059 {
1060         struct ip6_tnl *tunnel;
1061         int ret;
1062         int t_hlen;
1063
1064         tunnel = netdev_priv(dev);
1065
1066         tunnel->dev = dev;
1067         tunnel->net = dev_net(dev);
1068         strcpy(tunnel->parms.name, dev->name);
1069
1070         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1071         if (!dev->tstats)
1072                 return -ENOMEM;
1073
1074         ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1075         if (ret) {
1076                 free_percpu(dev->tstats);
1077                 dev->tstats = NULL;
1078                 return ret;
1079         }
1080
1081         tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1082         tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1083         t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1084
1085         dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1086         dev->mtu = ETH_DATA_LEN - t_hlen;
1087         if (dev->type == ARPHRD_ETHER)
1088                 dev->mtu -= ETH_HLEN;
1089         if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1090                 dev->mtu -= 8;
1091
1092         ip6gre_tnl_init_features(dev);
1093
1094         return 0;
1095 }
1096
1097 static int ip6gre_tunnel_init(struct net_device *dev)
1098 {
1099         struct ip6_tnl *tunnel;
1100         int ret;
1101
1102         ret = ip6gre_tunnel_init_common(dev);
1103         if (ret)
1104                 return ret;
1105
1106         tunnel = netdev_priv(dev);
1107
1108         memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1109         memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1110
1111         if (ipv6_addr_any(&tunnel->parms.raddr))
1112                 dev->header_ops = &ip6gre_header_ops;
1113
1114         return 0;
1115 }
1116
1117 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1118 {
1119         struct ip6_tnl *tunnel = netdev_priv(dev);
1120
1121         tunnel->dev = dev;
1122         tunnel->net = dev_net(dev);
1123         strcpy(tunnel->parms.name, dev->name);
1124
1125         tunnel->hlen            = sizeof(struct ipv6hdr) + 4;
1126 }
1127
1128
1129 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1130         .handler     = gre_rcv,
1131         .err_handler = ip6gre_err,
1132         .flags       = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1133 };
1134
1135 static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
1136 {
1137         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1138         struct net_device *dev, *aux;
1139         int prio;
1140
1141         for_each_netdev_safe(net, dev, aux)
1142                 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1143                     dev->rtnl_link_ops == &ip6gre_tap_ops)
1144                         unregister_netdevice_queue(dev, head);
1145
1146         for (prio = 0; prio < 4; prio++) {
1147                 int h;
1148                 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1149                         struct ip6_tnl *t;
1150
1151                         t = rtnl_dereference(ign->tunnels[prio][h]);
1152
1153                         while (t) {
1154                                 /* If dev is in the same netns, it has already
1155                                  * been added to the list by the previous loop.
1156                                  */
1157                                 if (!net_eq(dev_net(t->dev), net))
1158                                         unregister_netdevice_queue(t->dev,
1159                                                                    head);
1160                                 t = rtnl_dereference(t->next);
1161                         }
1162                 }
1163         }
1164 }
1165
1166 static int __net_init ip6gre_init_net(struct net *net)
1167 {
1168         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1169         struct net_device *ndev;
1170         int err;
1171
1172         ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1173                             NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1174         if (!ndev) {
1175                 err = -ENOMEM;
1176                 goto err_alloc_dev;
1177         }
1178         ign->fb_tunnel_dev = ndev;
1179         dev_net_set(ign->fb_tunnel_dev, net);
1180         /* FB netdevice is special: we have one, and only one per netns.
1181          * Allowing to move it to another netns is clearly unsafe.
1182          */
1183         ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1184
1185
1186         ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1187         ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1188
1189         err = register_netdev(ign->fb_tunnel_dev);
1190         if (err)
1191                 goto err_reg_dev;
1192
1193         rcu_assign_pointer(ign->tunnels_wc[0],
1194                            netdev_priv(ign->fb_tunnel_dev));
1195         return 0;
1196
1197 err_reg_dev:
1198         free_netdev(ndev);
1199 err_alloc_dev:
1200         return err;
1201 }
1202
1203 static void __net_exit ip6gre_exit_net(struct net *net)
1204 {
1205         LIST_HEAD(list);
1206
1207         rtnl_lock();
1208         ip6gre_destroy_tunnels(net, &list);
1209         unregister_netdevice_many(&list);
1210         rtnl_unlock();
1211 }
1212
1213 static struct pernet_operations ip6gre_net_ops = {
1214         .init = ip6gre_init_net,
1215         .exit = ip6gre_exit_net,
1216         .id   = &ip6gre_net_id,
1217         .size = sizeof(struct ip6gre_net),
1218 };
1219
1220 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1221                                   struct netlink_ext_ack *extack)
1222 {
1223         __be16 flags;
1224
1225         if (!data)
1226                 return 0;
1227
1228         flags = 0;
1229         if (data[IFLA_GRE_IFLAGS])
1230                 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1231         if (data[IFLA_GRE_OFLAGS])
1232                 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1233         if (flags & (GRE_VERSION|GRE_ROUTING))
1234                 return -EINVAL;
1235
1236         return 0;
1237 }
1238
1239 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1240                                struct netlink_ext_ack *extack)
1241 {
1242         struct in6_addr daddr;
1243
1244         if (tb[IFLA_ADDRESS]) {
1245                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1246                         return -EINVAL;
1247                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1248                         return -EADDRNOTAVAIL;
1249         }
1250
1251         if (!data)
1252                 goto out;
1253
1254         if (data[IFLA_GRE_REMOTE]) {
1255                 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1256                 if (ipv6_addr_any(&daddr))
1257                         return -EINVAL;
1258         }
1259
1260 out:
1261         return ip6gre_tunnel_validate(tb, data, extack);
1262 }
1263
1264
1265 static void ip6gre_netlink_parms(struct nlattr *data[],
1266                                 struct __ip6_tnl_parm *parms)
1267 {
1268         memset(parms, 0, sizeof(*parms));
1269
1270         if (!data)
1271                 return;
1272
1273         if (data[IFLA_GRE_LINK])
1274                 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1275
1276         if (data[IFLA_GRE_IFLAGS])
1277                 parms->i_flags = gre_flags_to_tnl_flags(
1278                                 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1279
1280         if (data[IFLA_GRE_OFLAGS])
1281                 parms->o_flags = gre_flags_to_tnl_flags(
1282                                 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1283
1284         if (data[IFLA_GRE_IKEY])
1285                 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1286
1287         if (data[IFLA_GRE_OKEY])
1288                 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1289
1290         if (data[IFLA_GRE_LOCAL])
1291                 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1292
1293         if (data[IFLA_GRE_REMOTE])
1294                 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1295
1296         if (data[IFLA_GRE_TTL])
1297                 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1298
1299         if (data[IFLA_GRE_ENCAP_LIMIT])
1300                 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1301
1302         if (data[IFLA_GRE_FLOWINFO])
1303                 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1304
1305         if (data[IFLA_GRE_FLAGS])
1306                 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1307
1308         if (data[IFLA_GRE_FWMARK])
1309                 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1310 }
1311
1312 static int ip6gre_tap_init(struct net_device *dev)
1313 {
1314         int ret;
1315
1316         ret = ip6gre_tunnel_init_common(dev);
1317         if (ret)
1318                 return ret;
1319
1320         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1321
1322         return 0;
1323 }
1324
1325 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1326         .ndo_init = ip6gre_tap_init,
1327         .ndo_uninit = ip6gre_tunnel_uninit,
1328         .ndo_start_xmit = ip6gre_tunnel_xmit,
1329         .ndo_set_mac_address = eth_mac_addr,
1330         .ndo_validate_addr = eth_validate_addr,
1331         .ndo_change_mtu = ip6_tnl_change_mtu,
1332         .ndo_get_stats64 = ip_tunnel_get_stats64,
1333         .ndo_get_iflink = ip6_tnl_get_iflink,
1334 };
1335
1336 static void ip6gre_tap_setup(struct net_device *dev)
1337 {
1338
1339         ether_setup(dev);
1340
1341         dev->max_mtu = 0;
1342         dev->netdev_ops = &ip6gre_tap_netdev_ops;
1343         dev->needs_free_netdev = true;
1344         dev->priv_destructor = ip6gre_dev_free;
1345
1346         dev->features |= NETIF_F_NETNS_LOCAL;
1347         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1348         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1349         netif_keep_dst(dev);
1350 }
1351
1352 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1353                                        struct ip_tunnel_encap *ipencap)
1354 {
1355         bool ret = false;
1356
1357         memset(ipencap, 0, sizeof(*ipencap));
1358
1359         if (!data)
1360                 return ret;
1361
1362         if (data[IFLA_GRE_ENCAP_TYPE]) {
1363                 ret = true;
1364                 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1365         }
1366
1367         if (data[IFLA_GRE_ENCAP_FLAGS]) {
1368                 ret = true;
1369                 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1370         }
1371
1372         if (data[IFLA_GRE_ENCAP_SPORT]) {
1373                 ret = true;
1374                 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1375         }
1376
1377         if (data[IFLA_GRE_ENCAP_DPORT]) {
1378                 ret = true;
1379                 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1380         }
1381
1382         return ret;
1383 }
1384
1385 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1386                           struct nlattr *tb[], struct nlattr *data[],
1387                           struct netlink_ext_ack *extack)
1388 {
1389         struct ip6_tnl *nt;
1390         struct net *net = dev_net(dev);
1391         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1392         struct ip_tunnel_encap ipencap;
1393         int err;
1394
1395         nt = netdev_priv(dev);
1396
1397         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1398                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1399
1400                 if (err < 0)
1401                         return err;
1402         }
1403
1404         ip6gre_netlink_parms(data, &nt->parms);
1405
1406         if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1407                 return -EEXIST;
1408
1409         if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1410                 eth_hw_addr_random(dev);
1411
1412         nt->dev = dev;
1413         nt->net = dev_net(dev);
1414
1415         err = register_netdevice(dev);
1416         if (err)
1417                 goto out;
1418
1419         ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1420
1421         if (tb[IFLA_MTU])
1422                 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1423
1424         dev_hold(dev);
1425         ip6gre_tunnel_link(ign, nt);
1426
1427 out:
1428         return err;
1429 }
1430
1431 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1432                              struct nlattr *data[],
1433                              struct netlink_ext_ack *extack)
1434 {
1435         struct ip6_tnl *t, *nt = netdev_priv(dev);
1436         struct net *net = nt->net;
1437         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1438         struct __ip6_tnl_parm p;
1439         struct ip_tunnel_encap ipencap;
1440
1441         if (dev == ign->fb_tunnel_dev)
1442                 return -EINVAL;
1443
1444         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1445                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1446
1447                 if (err < 0)
1448                         return err;
1449         }
1450
1451         ip6gre_netlink_parms(data, &p);
1452
1453         t = ip6gre_tunnel_locate(net, &p, 0);
1454
1455         if (t) {
1456                 if (t->dev != dev)
1457                         return -EEXIST;
1458         } else {
1459                 t = nt;
1460         }
1461
1462         ip6gre_tunnel_unlink(ign, t);
1463         ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1464         ip6gre_tunnel_link(ign, t);
1465         return 0;
1466 }
1467
1468 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1469 {
1470         struct net *net = dev_net(dev);
1471         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1472
1473         if (dev != ign->fb_tunnel_dev)
1474                 unregister_netdevice_queue(dev, head);
1475 }
1476
1477 static size_t ip6gre_get_size(const struct net_device *dev)
1478 {
1479         return
1480                 /* IFLA_GRE_LINK */
1481                 nla_total_size(4) +
1482                 /* IFLA_GRE_IFLAGS */
1483                 nla_total_size(2) +
1484                 /* IFLA_GRE_OFLAGS */
1485                 nla_total_size(2) +
1486                 /* IFLA_GRE_IKEY */
1487                 nla_total_size(4) +
1488                 /* IFLA_GRE_OKEY */
1489                 nla_total_size(4) +
1490                 /* IFLA_GRE_LOCAL */
1491                 nla_total_size(sizeof(struct in6_addr)) +
1492                 /* IFLA_GRE_REMOTE */
1493                 nla_total_size(sizeof(struct in6_addr)) +
1494                 /* IFLA_GRE_TTL */
1495                 nla_total_size(1) +
1496                 /* IFLA_GRE_ENCAP_LIMIT */
1497                 nla_total_size(1) +
1498                 /* IFLA_GRE_FLOWINFO */
1499                 nla_total_size(4) +
1500                 /* IFLA_GRE_FLAGS */
1501                 nla_total_size(4) +
1502                 /* IFLA_GRE_ENCAP_TYPE */
1503                 nla_total_size(2) +
1504                 /* IFLA_GRE_ENCAP_FLAGS */
1505                 nla_total_size(2) +
1506                 /* IFLA_GRE_ENCAP_SPORT */
1507                 nla_total_size(2) +
1508                 /* IFLA_GRE_ENCAP_DPORT */
1509                 nla_total_size(2) +
1510                 /* IFLA_GRE_FWMARK */
1511                 nla_total_size(4) +
1512                 0;
1513 }
1514
1515 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1516 {
1517         struct ip6_tnl *t = netdev_priv(dev);
1518         struct __ip6_tnl_parm *p = &t->parms;
1519
1520         if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1521             nla_put_be16(skb, IFLA_GRE_IFLAGS,
1522                          gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1523             nla_put_be16(skb, IFLA_GRE_OFLAGS,
1524                          gre_tnl_flags_to_gre_flags(p->o_flags)) ||
1525             nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1526             nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1527             nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1528             nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
1529             nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1530             nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1531             nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1532             nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
1533             nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark))
1534                 goto nla_put_failure;
1535
1536         if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1537                         t->encap.type) ||
1538             nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1539                          t->encap.sport) ||
1540             nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1541                          t->encap.dport) ||
1542             nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1543                         t->encap.flags))
1544                 goto nla_put_failure;
1545
1546         return 0;
1547
1548 nla_put_failure:
1549         return -EMSGSIZE;
1550 }
1551
1552 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1553         [IFLA_GRE_LINK]        = { .type = NLA_U32 },
1554         [IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
1555         [IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
1556         [IFLA_GRE_IKEY]        = { .type = NLA_U32 },
1557         [IFLA_GRE_OKEY]        = { .type = NLA_U32 },
1558         [IFLA_GRE_LOCAL]       = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1559         [IFLA_GRE_REMOTE]      = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1560         [IFLA_GRE_TTL]         = { .type = NLA_U8 },
1561         [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1562         [IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
1563         [IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
1564         [IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
1565         [IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
1566         [IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
1567         [IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
1568         [IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
1569 };
1570
1571 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1572         .kind           = "ip6gre",
1573         .maxtype        = IFLA_GRE_MAX,
1574         .policy         = ip6gre_policy,
1575         .priv_size      = sizeof(struct ip6_tnl),
1576         .setup          = ip6gre_tunnel_setup,
1577         .validate       = ip6gre_tunnel_validate,
1578         .newlink        = ip6gre_newlink,
1579         .changelink     = ip6gre_changelink,
1580         .dellink        = ip6gre_dellink,
1581         .get_size       = ip6gre_get_size,
1582         .fill_info      = ip6gre_fill_info,
1583         .get_link_net   = ip6_tnl_get_link_net,
1584 };
1585
1586 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1587         .kind           = "ip6gretap",
1588         .maxtype        = IFLA_GRE_MAX,
1589         .policy         = ip6gre_policy,
1590         .priv_size      = sizeof(struct ip6_tnl),
1591         .setup          = ip6gre_tap_setup,
1592         .validate       = ip6gre_tap_validate,
1593         .newlink        = ip6gre_newlink,
1594         .changelink     = ip6gre_changelink,
1595         .get_size       = ip6gre_get_size,
1596         .fill_info      = ip6gre_fill_info,
1597         .get_link_net   = ip6_tnl_get_link_net,
1598 };
1599
1600 /*
1601  *      And now the modules code and kernel interface.
1602  */
1603
1604 static int __init ip6gre_init(void)
1605 {
1606         int err;
1607
1608         pr_info("GRE over IPv6 tunneling driver\n");
1609
1610         err = register_pernet_device(&ip6gre_net_ops);
1611         if (err < 0)
1612                 return err;
1613
1614         err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1615         if (err < 0) {
1616                 pr_info("%s: can't add protocol\n", __func__);
1617                 goto add_proto_failed;
1618         }
1619
1620         err = rtnl_link_register(&ip6gre_link_ops);
1621         if (err < 0)
1622                 goto rtnl_link_failed;
1623
1624         err = rtnl_link_register(&ip6gre_tap_ops);
1625         if (err < 0)
1626                 goto tap_ops_failed;
1627
1628 out:
1629         return err;
1630
1631 tap_ops_failed:
1632         rtnl_link_unregister(&ip6gre_link_ops);
1633 rtnl_link_failed:
1634         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1635 add_proto_failed:
1636         unregister_pernet_device(&ip6gre_net_ops);
1637         goto out;
1638 }
1639
1640 static void __exit ip6gre_fini(void)
1641 {
1642         rtnl_link_unregister(&ip6gre_tap_ops);
1643         rtnl_link_unregister(&ip6gre_link_ops);
1644         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1645         unregister_pernet_device(&ip6gre_net_ops);
1646 }
1647
1648 module_init(ip6gre_init);
1649 module_exit(ip6gre_fini);
1650 MODULE_LICENSE("GPL");
1651 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1652 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1653 MODULE_ALIAS_RTNL_LINK("ip6gre");
1654 MODULE_ALIAS_RTNL_LINK("ip6gretap");
1655 MODULE_ALIAS_NETDEV("ip6gre0");