~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/ip6_gre.c

Version: ~ [ linux-6.4-rc3 ] ~ [ linux-6.3.4 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.30 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.113 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.180 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.243 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.283 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.315 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp