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