GNU Linux-libre 4.9.294-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 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 struct ipv6_tel_txoption {
500         struct ipv6_txoptions ops;
501         __u8 dst_opt[8];
502 };
503
504 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
505 {
506         return iptunnel_handle_offloads(skb,
507                                         csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
508 }
509
510 static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
511                                struct net_device *dev, __u8 dsfield,
512                                struct flowi6 *fl6, int encap_limit,
513                                __u32 *pmtu, __be16 proto)
514 {
515         struct ip6_tnl *tunnel = netdev_priv(dev);
516         struct dst_entry *dst = skb_dst(skb);
517         __be16 protocol;
518
519         if (dev->type == ARPHRD_ETHER)
520                 IPCB(skb)->flags = 0;
521
522         if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
523                 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
524         else
525                 fl6->daddr = tunnel->parms.raddr;
526
527         if (tunnel->parms.o_flags & TUNNEL_SEQ)
528                 tunnel->o_seqno++;
529
530         /* Push GRE header. */
531         protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
532         gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
533                          protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
534
535         /* TooBig packet may have updated dst->dev's mtu */
536         if (dst && dst_mtu(dst) > dst->dev->mtu)
537                 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
538
539         return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
540                             NEXTHDR_GRE);
541 }
542
543 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
544 {
545         struct ip6_tnl *t = netdev_priv(dev);
546         const struct iphdr  *iph = ip_hdr(skb);
547         int encap_limit = -1;
548         struct flowi6 fl6;
549         __u8 dsfield;
550         __u32 mtu;
551         int err;
552
553         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
554
555         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
556                 encap_limit = t->parms.encap_limit;
557
558         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
559
560         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
561                 dsfield = ipv4_get_dsfield(iph);
562         else
563                 dsfield = ip6_tclass(t->parms.flowinfo);
564         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
565                 fl6.flowi6_mark = skb->mark;
566
567         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
568         if (err)
569                 return -1;
570
571         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
572                           skb->protocol);
573         if (err != 0) {
574                 /* XXX: send ICMP error even if DF is not set. */
575                 if (err == -EMSGSIZE)
576                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
577                                   htonl(mtu));
578                 return -1;
579         }
580
581         return 0;
582 }
583
584 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
585 {
586         struct ip6_tnl *t = netdev_priv(dev);
587         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
588         int encap_limit = -1;
589         __u16 offset;
590         struct flowi6 fl6;
591         __u8 dsfield;
592         __u32 mtu;
593         int err;
594
595         if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
596                 return -1;
597
598         offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
599         /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
600         ipv6h = ipv6_hdr(skb);
601
602         if (offset > 0) {
603                 struct ipv6_tlv_tnl_enc_lim *tel;
604                 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
605                 if (tel->encap_limit == 0) {
606                         icmpv6_send(skb, ICMPV6_PARAMPROB,
607                                     ICMPV6_HDR_FIELD, offset + 2);
608                         return -1;
609                 }
610                 encap_limit = tel->encap_limit - 1;
611         } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
612                 encap_limit = t->parms.encap_limit;
613
614         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
615
616         if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
617                 dsfield = ipv6_get_dsfield(ipv6h);
618         else
619                 dsfield = ip6_tclass(t->parms.flowinfo);
620
621         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
622                 fl6.flowlabel |= ip6_flowlabel(ipv6h);
623         if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
624                 fl6.flowi6_mark = skb->mark;
625
626         if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
627                 return -1;
628
629         err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
630                           &mtu, skb->protocol);
631         if (err != 0) {
632                 if (err == -EMSGSIZE)
633                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
634                 return -1;
635         }
636
637         return 0;
638 }
639
640 /**
641  * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
642  *   @t: the outgoing tunnel device
643  *   @hdr: IPv6 header from the incoming packet
644  *
645  * Description:
646  *   Avoid trivial tunneling loop by checking that tunnel exit-point
647  *   doesn't match source of incoming packet.
648  *
649  * Return:
650  *   1 if conflict,
651  *   0 else
652  **/
653
654 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
655         const struct ipv6hdr *hdr)
656 {
657         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
658 }
659
660 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
661 {
662         struct ip6_tnl *t = netdev_priv(dev);
663         int encap_limit = -1;
664         struct flowi6 fl6;
665         __u32 mtu;
666         int err;
667
668         if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
669                 encap_limit = t->parms.encap_limit;
670
671         memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
672
673         err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
674         if (err)
675                 return err;
676
677         err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
678
679         return err;
680 }
681
682 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
683         struct net_device *dev)
684 {
685         struct ip6_tnl *t = netdev_priv(dev);
686         struct net_device_stats *stats = &t->dev->stats;
687         int ret;
688
689         if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
690                 goto tx_err;
691
692         switch (skb->protocol) {
693         case htons(ETH_P_IP):
694                 ret = ip6gre_xmit_ipv4(skb, dev);
695                 break;
696         case htons(ETH_P_IPV6):
697                 ret = ip6gre_xmit_ipv6(skb, dev);
698                 break;
699         default:
700                 ret = ip6gre_xmit_other(skb, dev);
701                 break;
702         }
703
704         if (ret < 0)
705                 goto tx_err;
706
707         return NETDEV_TX_OK;
708
709 tx_err:
710         stats->tx_errors++;
711         stats->tx_dropped++;
712         kfree_skb(skb);
713         return NETDEV_TX_OK;
714 }
715
716 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
717 {
718         struct net_device *dev = t->dev;
719         struct __ip6_tnl_parm *p = &t->parms;
720         struct flowi6 *fl6 = &t->fl.u.ip6;
721         int t_hlen;
722
723         if (dev->type != ARPHRD_ETHER) {
724                 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
725                 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
726         }
727
728         /* Set up flowi template */
729         fl6->saddr = p->laddr;
730         fl6->daddr = p->raddr;
731         fl6->flowi6_oif = p->link;
732         fl6->flowlabel = 0;
733         fl6->flowi6_proto = IPPROTO_GRE;
734
735         if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
736                 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
737         if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
738                 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
739
740         p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
741         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
742
743         if (p->flags&IP6_TNL_F_CAP_XMIT &&
744                         p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
745                 dev->flags |= IFF_POINTOPOINT;
746         else
747                 dev->flags &= ~IFF_POINTOPOINT;
748
749         t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
750
751         t->hlen = t->encap_hlen + t->tun_hlen;
752
753         t_hlen = t->hlen + sizeof(struct ipv6hdr);
754
755         if (p->flags & IP6_TNL_F_CAP_XMIT) {
756                 int strict = (ipv6_addr_type(&p->raddr) &
757                               (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
758
759                 struct rt6_info *rt = rt6_lookup(t->net,
760                                                  &p->raddr, &p->laddr,
761                                                  p->link, strict);
762
763                 if (!rt)
764                         return;
765
766                 if (rt->dst.dev) {
767                         dev->hard_header_len = rt->dst.dev->hard_header_len +
768                                                t_hlen;
769
770                         if (set_mtu) {
771                                 dev->mtu = rt->dst.dev->mtu - t_hlen;
772                                 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
773                                         dev->mtu -= 8;
774                                 if (dev->type == ARPHRD_ETHER)
775                                         dev->mtu -= ETH_HLEN;
776
777                                 if (dev->mtu < IPV6_MIN_MTU)
778                                         dev->mtu = IPV6_MIN_MTU;
779                         }
780                 }
781                 ip6_rt_put(rt);
782         }
783 }
784
785 static int ip6gre_tnl_change(struct ip6_tnl *t,
786         const struct __ip6_tnl_parm *p, int set_mtu)
787 {
788         t->parms.laddr = p->laddr;
789         t->parms.raddr = p->raddr;
790         t->parms.flags = p->flags;
791         t->parms.hop_limit = p->hop_limit;
792         t->parms.encap_limit = p->encap_limit;
793         t->parms.flowinfo = p->flowinfo;
794         t->parms.link = p->link;
795         t->parms.proto = p->proto;
796         t->parms.i_key = p->i_key;
797         t->parms.o_key = p->o_key;
798         t->parms.i_flags = p->i_flags;
799         t->parms.o_flags = p->o_flags;
800         dst_cache_reset(&t->dst_cache);
801         ip6gre_tnl_link_config(t, set_mtu);
802         return 0;
803 }
804
805 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
806         const struct ip6_tnl_parm2 *u)
807 {
808         p->laddr = u->laddr;
809         p->raddr = u->raddr;
810         p->flags = u->flags;
811         p->hop_limit = u->hop_limit;
812         p->encap_limit = u->encap_limit;
813         p->flowinfo = u->flowinfo;
814         p->link = u->link;
815         p->i_key = u->i_key;
816         p->o_key = u->o_key;
817         p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
818         p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
819         memcpy(p->name, u->name, sizeof(u->name));
820 }
821
822 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
823         const struct __ip6_tnl_parm *p)
824 {
825         u->proto = IPPROTO_GRE;
826         u->laddr = p->laddr;
827         u->raddr = p->raddr;
828         u->flags = p->flags;
829         u->hop_limit = p->hop_limit;
830         u->encap_limit = p->encap_limit;
831         u->flowinfo = p->flowinfo;
832         u->link = p->link;
833         u->i_key = p->i_key;
834         u->o_key = p->o_key;
835         u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
836         u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
837         memcpy(u->name, p->name, sizeof(u->name));
838 }
839
840 static int ip6gre_tunnel_ioctl(struct net_device *dev,
841         struct ifreq *ifr, int cmd)
842 {
843         int err = 0;
844         struct ip6_tnl_parm2 p;
845         struct __ip6_tnl_parm p1;
846         struct ip6_tnl *t = netdev_priv(dev);
847         struct net *net = t->net;
848         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
849
850         memset(&p1, 0, sizeof(p1));
851
852         switch (cmd) {
853         case SIOCGETTUNNEL:
854                 if (dev == ign->fb_tunnel_dev) {
855                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
856                                 err = -EFAULT;
857                                 break;
858                         }
859                         ip6gre_tnl_parm_from_user(&p1, &p);
860                         t = ip6gre_tunnel_locate(net, &p1, 0);
861                         if (!t)
862                                 t = netdev_priv(dev);
863                 }
864                 memset(&p, 0, sizeof(p));
865                 ip6gre_tnl_parm_to_user(&p, &t->parms);
866                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
867                         err = -EFAULT;
868                 break;
869
870         case SIOCADDTUNNEL:
871         case SIOCCHGTUNNEL:
872                 err = -EPERM;
873                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
874                         goto done;
875
876                 err = -EFAULT;
877                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
878                         goto done;
879
880                 err = -EINVAL;
881                 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
882                         goto done;
883
884                 if (!(p.i_flags&GRE_KEY))
885                         p.i_key = 0;
886                 if (!(p.o_flags&GRE_KEY))
887                         p.o_key = 0;
888
889                 ip6gre_tnl_parm_from_user(&p1, &p);
890                 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
891
892                 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
893                         if (t) {
894                                 if (t->dev != dev) {
895                                         err = -EEXIST;
896                                         break;
897                                 }
898                         } else {
899                                 t = netdev_priv(dev);
900
901                                 ip6gre_tunnel_unlink(ign, t);
902                                 synchronize_net();
903                                 ip6gre_tnl_change(t, &p1, 1);
904                                 ip6gre_tunnel_link(ign, t);
905                                 netdev_state_change(dev);
906                         }
907                 }
908
909                 if (t) {
910                         err = 0;
911
912                         memset(&p, 0, sizeof(p));
913                         ip6gre_tnl_parm_to_user(&p, &t->parms);
914                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
915                                 err = -EFAULT;
916                 } else
917                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
918                 break;
919
920         case SIOCDELTUNNEL:
921                 err = -EPERM;
922                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
923                         goto done;
924
925                 if (dev == ign->fb_tunnel_dev) {
926                         err = -EFAULT;
927                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
928                                 goto done;
929                         err = -ENOENT;
930                         ip6gre_tnl_parm_from_user(&p1, &p);
931                         t = ip6gre_tunnel_locate(net, &p1, 0);
932                         if (!t)
933                                 goto done;
934                         err = -EPERM;
935                         if (t == netdev_priv(ign->fb_tunnel_dev))
936                                 goto done;
937                         dev = t->dev;
938                 }
939                 unregister_netdevice(dev);
940                 err = 0;
941                 break;
942
943         default:
944                 err = -EINVAL;
945         }
946
947 done:
948         return err;
949 }
950
951 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
952                          unsigned short type, const void *daddr,
953                          const void *saddr, unsigned int len)
954 {
955         struct ip6_tnl *t = netdev_priv(dev);
956         struct ipv6hdr *ipv6h;
957         __be16 *p;
958
959         ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen + sizeof(*ipv6h));
960         ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
961                                                   t->fl.u.ip6.flowlabel,
962                                                   true, &t->fl.u.ip6));
963         ipv6h->hop_limit = t->parms.hop_limit;
964         ipv6h->nexthdr = NEXTHDR_GRE;
965         ipv6h->saddr = t->parms.laddr;
966         ipv6h->daddr = t->parms.raddr;
967
968         p = (__be16 *)(ipv6h + 1);
969         p[0] = t->parms.o_flags;
970         p[1] = htons(type);
971
972         /*
973          *      Set the source hardware address.
974          */
975
976         if (saddr)
977                 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
978         if (daddr)
979                 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
980         if (!ipv6_addr_any(&ipv6h->daddr))
981                 return t->hlen;
982
983         return -t->hlen;
984 }
985
986 static const struct header_ops ip6gre_header_ops = {
987         .create = ip6gre_header,
988 };
989
990 static const struct net_device_ops ip6gre_netdev_ops = {
991         .ndo_init               = ip6gre_tunnel_init,
992         .ndo_uninit             = ip6gre_tunnel_uninit,
993         .ndo_start_xmit         = ip6gre_tunnel_xmit,
994         .ndo_do_ioctl           = ip6gre_tunnel_ioctl,
995         .ndo_change_mtu         = ip6_tnl_change_mtu,
996         .ndo_get_stats64        = ip_tunnel_get_stats64,
997         .ndo_get_iflink         = ip6_tnl_get_iflink,
998 };
999
1000 static void ip6gre_dev_free(struct net_device *dev)
1001 {
1002         struct ip6_tnl *t = netdev_priv(dev);
1003
1004         dst_cache_destroy(&t->dst_cache);
1005         free_percpu(dev->tstats);
1006         free_netdev(dev);
1007 }
1008
1009 static void ip6gre_tunnel_setup(struct net_device *dev)
1010 {
1011         dev->netdev_ops = &ip6gre_netdev_ops;
1012         dev->destructor = ip6gre_dev_free;
1013
1014         dev->type = ARPHRD_IP6GRE;
1015
1016         dev->flags |= IFF_NOARP;
1017         dev->addr_len = sizeof(struct in6_addr);
1018         netif_keep_dst(dev);
1019 }
1020
1021 static int ip6gre_tunnel_init_common(struct net_device *dev)
1022 {
1023         struct ip6_tnl *tunnel;
1024         int ret;
1025         int t_hlen;
1026
1027         tunnel = netdev_priv(dev);
1028
1029         tunnel->dev = dev;
1030         tunnel->net = dev_net(dev);
1031         strcpy(tunnel->parms.name, dev->name);
1032
1033         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1034         if (!dev->tstats)
1035                 return -ENOMEM;
1036
1037         ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1038         if (ret) {
1039                 free_percpu(dev->tstats);
1040                 dev->tstats = NULL;
1041                 return ret;
1042         }
1043
1044         tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1045         tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
1046         t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1047
1048         dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1049         dev->mtu = ETH_DATA_LEN - t_hlen;
1050         if (dev->type == ARPHRD_ETHER)
1051                 dev->mtu -= ETH_HLEN;
1052         if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1053                 dev->mtu -= 8;
1054
1055         return 0;
1056 }
1057
1058 static int ip6gre_tunnel_init(struct net_device *dev)
1059 {
1060         struct ip6_tnl *tunnel;
1061         int ret;
1062
1063         ret = ip6gre_tunnel_init_common(dev);
1064         if (ret)
1065                 return ret;
1066
1067         tunnel = netdev_priv(dev);
1068
1069         memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1070         memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1071
1072         if (ipv6_addr_any(&tunnel->parms.raddr))
1073                 dev->header_ops = &ip6gre_header_ops;
1074
1075         return 0;
1076 }
1077
1078 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1079 {
1080         struct ip6_tnl *tunnel = netdev_priv(dev);
1081
1082         tunnel->dev = dev;
1083         tunnel->net = dev_net(dev);
1084         strcpy(tunnel->parms.name, dev->name);
1085
1086         tunnel->hlen            = sizeof(struct ipv6hdr) + 4;
1087 }
1088
1089
1090 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1091         .handler     = gre_rcv,
1092         .err_handler = ip6gre_err,
1093         .flags       = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1094 };
1095
1096 static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
1097 {
1098         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1099         struct net_device *dev, *aux;
1100         int prio;
1101
1102         for_each_netdev_safe(net, dev, aux)
1103                 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1104                     dev->rtnl_link_ops == &ip6gre_tap_ops)
1105                         unregister_netdevice_queue(dev, head);
1106
1107         for (prio = 0; prio < 4; prio++) {
1108                 int h;
1109                 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
1110                         struct ip6_tnl *t;
1111
1112                         t = rtnl_dereference(ign->tunnels[prio][h]);
1113
1114                         while (t) {
1115                                 /* If dev is in the same netns, it has already
1116                                  * been added to the list by the previous loop.
1117                                  */
1118                                 if (!net_eq(dev_net(t->dev), net))
1119                                         unregister_netdevice_queue(t->dev,
1120                                                                    head);
1121                                 t = rtnl_dereference(t->next);
1122                         }
1123                 }
1124         }
1125 }
1126
1127 static int __net_init ip6gre_init_net(struct net *net)
1128 {
1129         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1130         struct net_device *ndev;
1131         int err;
1132
1133         ndev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1134                             NET_NAME_UNKNOWN, ip6gre_tunnel_setup);
1135         if (!ndev) {
1136                 err = -ENOMEM;
1137                 goto err_alloc_dev;
1138         }
1139         ign->fb_tunnel_dev = ndev;
1140         dev_net_set(ign->fb_tunnel_dev, net);
1141         /* FB netdevice is special: we have one, and only one per netns.
1142          * Allowing to move it to another netns is clearly unsafe.
1143          */
1144         ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1145
1146
1147         ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1148         ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1149
1150         err = register_netdev(ign->fb_tunnel_dev);
1151         if (err)
1152                 goto err_reg_dev;
1153
1154         rcu_assign_pointer(ign->tunnels_wc[0],
1155                            netdev_priv(ign->fb_tunnel_dev));
1156         return 0;
1157
1158 err_reg_dev:
1159         ip6gre_dev_free(ndev);
1160 err_alloc_dev:
1161         return err;
1162 }
1163
1164 static void __net_exit ip6gre_exit_net(struct net *net)
1165 {
1166         LIST_HEAD(list);
1167
1168         rtnl_lock();
1169         ip6gre_destroy_tunnels(net, &list);
1170         unregister_netdevice_many(&list);
1171         rtnl_unlock();
1172 }
1173
1174 static struct pernet_operations ip6gre_net_ops = {
1175         .init = ip6gre_init_net,
1176         .exit = ip6gre_exit_net,
1177         .id   = &ip6gre_net_id,
1178         .size = sizeof(struct ip6gre_net),
1179 };
1180
1181 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1182 {
1183         __be16 flags;
1184
1185         if (!data)
1186                 return 0;
1187
1188         flags = 0;
1189         if (data[IFLA_GRE_IFLAGS])
1190                 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1191         if (data[IFLA_GRE_OFLAGS])
1192                 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1193         if (flags & (GRE_VERSION|GRE_ROUTING))
1194                 return -EINVAL;
1195
1196         return 0;
1197 }
1198
1199 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1200 {
1201         struct in6_addr daddr;
1202
1203         if (tb[IFLA_ADDRESS]) {
1204                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1205                         return -EINVAL;
1206                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1207                         return -EADDRNOTAVAIL;
1208         }
1209
1210         if (!data)
1211                 goto out;
1212
1213         if (data[IFLA_GRE_REMOTE]) {
1214                 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1215                 if (ipv6_addr_any(&daddr))
1216                         return -EINVAL;
1217         }
1218
1219 out:
1220         return ip6gre_tunnel_validate(tb, data);
1221 }
1222
1223
1224 static void ip6gre_netlink_parms(struct nlattr *data[],
1225                                 struct __ip6_tnl_parm *parms)
1226 {
1227         memset(parms, 0, sizeof(*parms));
1228
1229         if (!data)
1230                 return;
1231
1232         if (data[IFLA_GRE_LINK])
1233                 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1234
1235         if (data[IFLA_GRE_IFLAGS])
1236                 parms->i_flags = gre_flags_to_tnl_flags(
1237                                 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1238
1239         if (data[IFLA_GRE_OFLAGS])
1240                 parms->o_flags = gre_flags_to_tnl_flags(
1241                                 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1242
1243         if (data[IFLA_GRE_IKEY])
1244                 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1245
1246         if (data[IFLA_GRE_OKEY])
1247                 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1248
1249         if (data[IFLA_GRE_LOCAL])
1250                 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1251
1252         if (data[IFLA_GRE_REMOTE])
1253                 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1254
1255         if (data[IFLA_GRE_TTL])
1256                 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1257
1258         if (data[IFLA_GRE_ENCAP_LIMIT])
1259                 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1260
1261         if (data[IFLA_GRE_FLOWINFO])
1262                 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
1263
1264         if (data[IFLA_GRE_FLAGS])
1265                 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1266 }
1267
1268 static int ip6gre_tap_init(struct net_device *dev)
1269 {
1270         int ret;
1271
1272         ret = ip6gre_tunnel_init_common(dev);
1273         if (ret)
1274                 return ret;
1275
1276         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1277
1278         return 0;
1279 }
1280
1281 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1282         .ndo_init = ip6gre_tap_init,
1283         .ndo_uninit = ip6gre_tunnel_uninit,
1284         .ndo_start_xmit = ip6gre_tunnel_xmit,
1285         .ndo_set_mac_address = eth_mac_addr,
1286         .ndo_validate_addr = eth_validate_addr,
1287         .ndo_change_mtu = ip6_tnl_change_mtu,
1288         .ndo_get_stats64 = ip_tunnel_get_stats64,
1289         .ndo_get_iflink = ip6_tnl_get_iflink,
1290 };
1291
1292 #define GRE6_FEATURES (NETIF_F_SG |             \
1293                        NETIF_F_FRAGLIST |       \
1294                        NETIF_F_HIGHDMA |                \
1295                        NETIF_F_HW_CSUM)
1296
1297 static void ip6gre_tap_setup(struct net_device *dev)
1298 {
1299
1300         ether_setup(dev);
1301
1302         dev->netdev_ops = &ip6gre_tap_netdev_ops;
1303         dev->destructor = ip6gre_dev_free;
1304
1305         dev->features |= NETIF_F_NETNS_LOCAL;
1306         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1307         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1308         netif_keep_dst(dev);
1309 }
1310
1311 static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1312                                        struct ip_tunnel_encap *ipencap)
1313 {
1314         bool ret = false;
1315
1316         memset(ipencap, 0, sizeof(*ipencap));
1317
1318         if (!data)
1319                 return ret;
1320
1321         if (data[IFLA_GRE_ENCAP_TYPE]) {
1322                 ret = true;
1323                 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1324         }
1325
1326         if (data[IFLA_GRE_ENCAP_FLAGS]) {
1327                 ret = true;
1328                 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1329         }
1330
1331         if (data[IFLA_GRE_ENCAP_SPORT]) {
1332                 ret = true;
1333                 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1334         }
1335
1336         if (data[IFLA_GRE_ENCAP_DPORT]) {
1337                 ret = true;
1338                 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1339         }
1340
1341         return ret;
1342 }
1343
1344 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1345         struct nlattr *tb[], struct nlattr *data[])
1346 {
1347         struct ip6_tnl *nt;
1348         struct net *net = dev_net(dev);
1349         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1350         struct ip_tunnel_encap ipencap;
1351         int err;
1352
1353         nt = netdev_priv(dev);
1354
1355         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1356                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1357
1358                 if (err < 0)
1359                         return err;
1360         }
1361
1362         ip6gre_netlink_parms(data, &nt->parms);
1363
1364         if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1365                 return -EEXIST;
1366
1367         if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1368                 eth_hw_addr_random(dev);
1369
1370         nt->dev = dev;
1371         nt->net = dev_net(dev);
1372
1373         dev->features           |= GRE6_FEATURES;
1374         dev->hw_features        |= GRE6_FEATURES;
1375
1376         if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1377                 /* TCP offload with GRE SEQ is not supported, nor
1378                  * can we support 2 levels of outer headers requiring
1379                  * an update.
1380                  */
1381                 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1382                     (nt->encap.type == TUNNEL_ENCAP_NONE)) {
1383                         dev->features    |= NETIF_F_GSO_SOFTWARE;
1384                         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1385                 }
1386
1387                 /* Can use a lockless transmit, unless we generate
1388                  * output sequences
1389                  */
1390                 dev->features |= NETIF_F_LLTX;
1391         }
1392
1393         err = register_netdevice(dev);
1394         if (err)
1395                 goto out;
1396
1397         ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1398
1399         if (tb[IFLA_MTU])
1400                 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1401
1402         dev_hold(dev);
1403         ip6gre_tunnel_link(ign, nt);
1404
1405 out:
1406         return err;
1407 }
1408
1409 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1410                             struct nlattr *data[])
1411 {
1412         struct ip6_tnl *t, *nt = netdev_priv(dev);
1413         struct net *net = nt->net;
1414         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1415         struct __ip6_tnl_parm p;
1416         struct ip_tunnel_encap ipencap;
1417
1418         if (dev == ign->fb_tunnel_dev)
1419                 return -EINVAL;
1420
1421         if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1422                 int err = ip6_tnl_encap_setup(nt, &ipencap);
1423
1424                 if (err < 0)
1425                         return err;
1426         }
1427
1428         ip6gre_netlink_parms(data, &p);
1429
1430         t = ip6gre_tunnel_locate(net, &p, 0);
1431
1432         if (t) {
1433                 if (t->dev != dev)
1434                         return -EEXIST;
1435         } else {
1436                 t = nt;
1437         }
1438
1439         ip6gre_tunnel_unlink(ign, t);
1440         ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1441         ip6gre_tunnel_link(ign, t);
1442         return 0;
1443 }
1444
1445 static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1446 {
1447         struct net *net = dev_net(dev);
1448         struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1449
1450         if (dev != ign->fb_tunnel_dev)
1451                 unregister_netdevice_queue(dev, head);
1452 }
1453
1454 static size_t ip6gre_get_size(const struct net_device *dev)
1455 {
1456         return
1457                 /* IFLA_GRE_LINK */
1458                 nla_total_size(4) +
1459                 /* IFLA_GRE_IFLAGS */
1460                 nla_total_size(2) +
1461                 /* IFLA_GRE_OFLAGS */
1462                 nla_total_size(2) +
1463                 /* IFLA_GRE_IKEY */
1464                 nla_total_size(4) +
1465                 /* IFLA_GRE_OKEY */
1466                 nla_total_size(4) +
1467                 /* IFLA_GRE_LOCAL */
1468                 nla_total_size(sizeof(struct in6_addr)) +
1469                 /* IFLA_GRE_REMOTE */
1470                 nla_total_size(sizeof(struct in6_addr)) +
1471                 /* IFLA_GRE_TTL */
1472                 nla_total_size(1) +
1473                 /* IFLA_GRE_ENCAP_LIMIT */
1474                 nla_total_size(1) +
1475                 /* IFLA_GRE_FLOWINFO */
1476                 nla_total_size(4) +
1477                 /* IFLA_GRE_FLAGS */
1478                 nla_total_size(4) +
1479                 /* IFLA_GRE_ENCAP_TYPE */
1480                 nla_total_size(2) +
1481                 /* IFLA_GRE_ENCAP_FLAGS */
1482                 nla_total_size(2) +
1483                 /* IFLA_GRE_ENCAP_SPORT */
1484                 nla_total_size(2) +
1485                 /* IFLA_GRE_ENCAP_DPORT */
1486                 nla_total_size(2) +
1487                 0;
1488 }
1489
1490 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1491 {
1492         struct ip6_tnl *t = netdev_priv(dev);
1493         struct __ip6_tnl_parm *p = &t->parms;
1494
1495         if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1496             nla_put_be16(skb, IFLA_GRE_IFLAGS,
1497                          gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1498             nla_put_be16(skb, IFLA_GRE_OFLAGS,
1499                          gre_tnl_flags_to_gre_flags(p->o_flags)) ||
1500             nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1501             nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1502             nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1503             nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
1504             nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1505             nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1506             nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1507             nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
1508                 goto nla_put_failure;
1509
1510         if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1511                         t->encap.type) ||
1512             nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1513                          t->encap.sport) ||
1514             nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1515                          t->encap.dport) ||
1516             nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1517                         t->encap.flags))
1518                 goto nla_put_failure;
1519
1520         return 0;
1521
1522 nla_put_failure:
1523         return -EMSGSIZE;
1524 }
1525
1526 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1527         [IFLA_GRE_LINK]        = { .type = NLA_U32 },
1528         [IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
1529         [IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
1530         [IFLA_GRE_IKEY]        = { .type = NLA_U32 },
1531         [IFLA_GRE_OKEY]        = { .type = NLA_U32 },
1532         [IFLA_GRE_LOCAL]       = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1533         [IFLA_GRE_REMOTE]      = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1534         [IFLA_GRE_TTL]         = { .type = NLA_U8 },
1535         [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1536         [IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
1537         [IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
1538         [IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
1539         [IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
1540         [IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
1541         [IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
1542 };
1543
1544 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1545         .kind           = "ip6gre",
1546         .maxtype        = IFLA_GRE_MAX,
1547         .policy         = ip6gre_policy,
1548         .priv_size      = sizeof(struct ip6_tnl),
1549         .setup          = ip6gre_tunnel_setup,
1550         .validate       = ip6gre_tunnel_validate,
1551         .newlink        = ip6gre_newlink,
1552         .changelink     = ip6gre_changelink,
1553         .dellink        = ip6gre_dellink,
1554         .get_size       = ip6gre_get_size,
1555         .fill_info      = ip6gre_fill_info,
1556         .get_link_net   = ip6_tnl_get_link_net,
1557 };
1558
1559 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1560         .kind           = "ip6gretap",
1561         .maxtype        = IFLA_GRE_MAX,
1562         .policy         = ip6gre_policy,
1563         .priv_size      = sizeof(struct ip6_tnl),
1564         .setup          = ip6gre_tap_setup,
1565         .validate       = ip6gre_tap_validate,
1566         .newlink        = ip6gre_newlink,
1567         .changelink     = ip6gre_changelink,
1568         .get_size       = ip6gre_get_size,
1569         .fill_info      = ip6gre_fill_info,
1570         .get_link_net   = ip6_tnl_get_link_net,
1571 };
1572
1573 /*
1574  *      And now the modules code and kernel interface.
1575  */
1576
1577 static int __init ip6gre_init(void)
1578 {
1579         int err;
1580
1581         pr_info("GRE over IPv6 tunneling driver\n");
1582
1583         err = register_pernet_device(&ip6gre_net_ops);
1584         if (err < 0)
1585                 return err;
1586
1587         err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1588         if (err < 0) {
1589                 pr_info("%s: can't add protocol\n", __func__);
1590                 goto add_proto_failed;
1591         }
1592
1593         err = rtnl_link_register(&ip6gre_link_ops);
1594         if (err < 0)
1595                 goto rtnl_link_failed;
1596
1597         err = rtnl_link_register(&ip6gre_tap_ops);
1598         if (err < 0)
1599                 goto tap_ops_failed;
1600
1601 out:
1602         return err;
1603
1604 tap_ops_failed:
1605         rtnl_link_unregister(&ip6gre_link_ops);
1606 rtnl_link_failed:
1607         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1608 add_proto_failed:
1609         unregister_pernet_device(&ip6gre_net_ops);
1610         goto out;
1611 }
1612
1613 static void __exit ip6gre_fini(void)
1614 {
1615         rtnl_link_unregister(&ip6gre_tap_ops);
1616         rtnl_link_unregister(&ip6gre_link_ops);
1617         inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1618         unregister_pernet_device(&ip6gre_net_ops);
1619 }
1620
1621 module_init(ip6gre_init);
1622 module_exit(ip6gre_fini);
1623 MODULE_LICENSE("GPL");
1624 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1625 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1626 MODULE_ALIAS_RTNL_LINK("ip6gre");
1627 MODULE_ALIAS_RTNL_LINK("ip6gretap");
1628 MODULE_ALIAS_NETDEV("ip6gre0");