ipv6: sit: better validate user provided tunnel names
[linux-block.git] / net / ipv6 / ip6_gre.c
CommitLineData
c12b395a 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>
c12b395a 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>
c5441932 40#include <net/ip_tunnels.h>
c12b395a 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>
308edfdf 57#include <net/gre.h>
5a963eb6 58#include <net/erspan.h>
6712abc1 59#include <net/dst_metadata.h>
c12b395a 60
61
eccc1bb8 62static bool log_ecn_error = true;
63module_param(log_ecn_error, bool, 0644);
64MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
65
e87a8f24
JK
66#define IP6_GRE_HASH_SIZE_SHIFT 5
67#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
c12b395a 68
c7d03a00 69static unsigned int ip6gre_net_id __read_mostly;
c12b395a 70struct ip6gre_net {
e87a8f24 71 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
c12b395a 72
6712abc1 73 struct ip6_tnl __rcu *collect_md_tun;
c12b395a 74 struct net_device *fb_tunnel_dev;
75};
76
77static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
22f08069 78static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
5a963eb6 79static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
c12b395a 80static int ip6gre_tunnel_init(struct net_device *dev);
81static void ip6gre_tunnel_setup(struct net_device *dev);
82static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
83static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
84
85/* Tunnel hash table */
86
87/*
88 4 hash tables:
89
90 3: (remote,local)
91 2: (remote,*)
92 1: (*,local)
93 0: (*,*)
94
95 We require exact key match i.e. if a key is present in packet
96 it will match only tunnel with the same key; if it is not present,
97 it will match only keyless tunnel.
98
99 All keysless packets, if not matched configured keyless tunnels
100 will match fallback tunnel.
101 */
102
e87a8f24 103#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
c12b395a 104static u32 HASH_ADDR(const struct in6_addr *addr)
105{
106 u32 hash = ipv6_addr_hash(addr);
107
e87a8f24 108 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
c12b395a 109}
110
111#define tunnels_r_l tunnels[3]
112#define tunnels_r tunnels[2]
113#define tunnels_l tunnels[1]
114#define tunnels_wc tunnels[0]
c12b395a 115
c12b395a 116/* Given src, dst and key, find appropriate for input tunnel. */
117
118static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
119 const struct in6_addr *remote, const struct in6_addr *local,
120 __be32 key, __be16 gre_proto)
121{
122 struct net *net = dev_net(dev);
123 int link = dev->ifindex;
124 unsigned int h0 = HASH_ADDR(remote);
125 unsigned int h1 = HASH_KEY(key);
126 struct ip6_tnl *t, *cand = NULL;
127 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
5a963eb6 128 int dev_type = (gre_proto == htons(ETH_P_TEB) ||
3b04caab
WT
129 gre_proto == htons(ETH_P_ERSPAN) ||
130 gre_proto == htons(ETH_P_ERSPAN2)) ?
c12b395a 131 ARPHRD_ETHER : ARPHRD_IP6GRE;
132 int score, cand_score = 4;
133
e086cadc 134 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
c12b395a 135 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
136 !ipv6_addr_equal(remote, &t->parms.raddr) ||
137 key != t->parms.i_key ||
138 !(t->dev->flags & IFF_UP))
139 continue;
140
141 if (t->dev->type != ARPHRD_IP6GRE &&
142 t->dev->type != dev_type)
143 continue;
144
145 score = 0;
146 if (t->parms.link != link)
147 score |= 1;
148 if (t->dev->type != dev_type)
149 score |= 2;
150 if (score == 0)
151 return t;
152
153 if (score < cand_score) {
154 cand = t;
155 cand_score = score;
156 }
157 }
158
e086cadc 159 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
c12b395a 160 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
161 key != t->parms.i_key ||
162 !(t->dev->flags & IFF_UP))
163 continue;
164
165 if (t->dev->type != ARPHRD_IP6GRE &&
166 t->dev->type != dev_type)
167 continue;
168
169 score = 0;
170 if (t->parms.link != link)
171 score |= 1;
172 if (t->dev->type != dev_type)
173 score |= 2;
174 if (score == 0)
175 return t;
176
177 if (score < cand_score) {
178 cand = t;
179 cand_score = score;
180 }
181 }
182
e086cadc 183 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
c12b395a 184 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
185 (!ipv6_addr_equal(local, &t->parms.raddr) ||
186 !ipv6_addr_is_multicast(local))) ||
187 key != t->parms.i_key ||
188 !(t->dev->flags & IFF_UP))
189 continue;
190
191 if (t->dev->type != ARPHRD_IP6GRE &&
192 t->dev->type != dev_type)
193 continue;
194
195 score = 0;
196 if (t->parms.link != link)
197 score |= 1;
198 if (t->dev->type != dev_type)
199 score |= 2;
200 if (score == 0)
201 return t;
202
203 if (score < cand_score) {
204 cand = t;
205 cand_score = score;
206 }
207 }
208
e086cadc 209 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
c12b395a 210 if (t->parms.i_key != key ||
211 !(t->dev->flags & IFF_UP))
212 continue;
213
214 if (t->dev->type != ARPHRD_IP6GRE &&
215 t->dev->type != dev_type)
216 continue;
217
218 score = 0;
219 if (t->parms.link != link)
220 score |= 1;
221 if (t->dev->type != dev_type)
222 score |= 2;
223 if (score == 0)
224 return t;
225
226 if (score < cand_score) {
227 cand = t;
228 cand_score = score;
229 }
230 }
231
53b24b8f 232 if (cand)
c12b395a 233 return cand;
234
6712abc1
WT
235 t = rcu_dereference(ign->collect_md_tun);
236 if (t && t->dev->flags & IFF_UP)
237 return t;
238
c12b395a 239 dev = ign->fb_tunnel_dev;
79134e6c 240 if (dev && dev->flags & IFF_UP)
c12b395a 241 return netdev_priv(dev);
242
243 return NULL;
244}
245
246static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
247 const struct __ip6_tnl_parm *p)
248{
249 const struct in6_addr *remote = &p->raddr;
250 const struct in6_addr *local = &p->laddr;
251 unsigned int h = HASH_KEY(p->i_key);
252 int prio = 0;
253
254 if (!ipv6_addr_any(local))
255 prio |= 1;
256 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
257 prio |= 2;
258 h ^= HASH_ADDR(remote);
259 }
260
261 return &ign->tunnels[prio][h];
262}
263
264static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
265 const struct ip6_tnl *t)
266{
267 return __ip6gre_bucket(ign, &t->parms);
268}
269
270static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
271{
272 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
273
6712abc1
WT
274 if (t->parms.collect_md)
275 rcu_assign_pointer(ign->collect_md_tun, t);
276
c12b395a 277 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
278 rcu_assign_pointer(*tp, t);
279}
280
281static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
282{
283 struct ip6_tnl __rcu **tp;
284 struct ip6_tnl *iter;
285
6712abc1
WT
286 if (t->parms.collect_md)
287 rcu_assign_pointer(ign->collect_md_tun, NULL);
288
c12b395a 289 for (tp = ip6gre_bucket(ign, t);
290 (iter = rtnl_dereference(*tp)) != NULL;
291 tp = &iter->next) {
292 if (t == iter) {
293 rcu_assign_pointer(*tp, t->next);
294 break;
295 }
296 }
297}
298
299static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
300 const struct __ip6_tnl_parm *parms,
301 int type)
302{
303 const struct in6_addr *remote = &parms->raddr;
304 const struct in6_addr *local = &parms->laddr;
305 __be32 key = parms->i_key;
306 int link = parms->link;
307 struct ip6_tnl *t;
308 struct ip6_tnl __rcu **tp;
309 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
310
311 for (tp = __ip6gre_bucket(ign, parms);
312 (t = rtnl_dereference(*tp)) != NULL;
313 tp = &t->next)
314 if (ipv6_addr_equal(local, &t->parms.laddr) &&
315 ipv6_addr_equal(remote, &t->parms.raddr) &&
316 key == t->parms.i_key &&
317 link == t->parms.link &&
318 type == t->dev->type)
319 break;
320
321 return t;
322}
323
324static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
325 const struct __ip6_tnl_parm *parms, int create)
326{
327 struct ip6_tnl *t, *nt;
328 struct net_device *dev;
329 char name[IFNAMSIZ];
330 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
331
332 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
cd0a0bd9
SK
333 if (t && create)
334 return NULL;
c12b395a 335 if (t || !create)
336 return t;
337
338 if (parms->name[0])
339 strlcpy(name, parms->name, IFNAMSIZ);
340 else
341 strcpy(name, "ip6gre%d");
342
c835a677
TG
343 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
344 ip6gre_tunnel_setup);
c12b395a 345 if (!dev)
346 return NULL;
347
348 dev_net_set(dev, net);
349
350 nt = netdev_priv(dev);
351 nt->parms = *parms;
352 dev->rtnl_link_ops = &ip6gre_link_ops;
353
354 nt->dev = dev;
0bd87628 355 nt->net = dev_net(dev);
c12b395a 356
357 if (register_netdevice(dev) < 0)
358 goto failed_free;
359
128bb975
AK
360 ip6gre_tnl_link_config(nt, 1);
361
c12b395a 362 /* Can use a lockless transmit, unless we generate output sequences */
b45bd1d7 363 if (!(nt->parms.o_flags & TUNNEL_SEQ))
c12b395a 364 dev->features |= NETIF_F_LLTX;
365
366 dev_hold(dev);
367 ip6gre_tunnel_link(ign, nt);
368 return nt;
369
370failed_free:
371 free_netdev(dev);
372 return NULL;
373}
374
375static void ip6gre_tunnel_uninit(struct net_device *dev)
376{
22f08069
ND
377 struct ip6_tnl *t = netdev_priv(dev);
378 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
c12b395a 379
22f08069 380 ip6gre_tunnel_unlink(ign, t);
607f725f 381 dst_cache_reset(&t->dst_cache);
c12b395a 382 dev_put(dev);
383}
384
385
386static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
7892032c 387 u8 type, u8 code, int offset, __be32 info)
c12b395a 388{
929fc032 389 struct net *net = dev_net(skb->dev);
7892032c
ED
390 const struct gre_base_hdr *greh;
391 const struct ipv6hdr *ipv6h;
392 int grehlen = sizeof(*greh);
c12b395a 393 struct ip6_tnl *t;
7892032c 394 int key_off = 0;
c12b395a 395 __be16 flags;
7892032c 396 __be32 key;
c12b395a 397
7892032c
ED
398 if (!pskb_may_pull(skb, offset + grehlen))
399 return;
400 greh = (const struct gre_base_hdr *)(skb->data + offset);
401 flags = greh->flags;
402 if (flags & (GRE_VERSION | GRE_ROUTING))
403 return;
404 if (flags & GRE_CSUM)
405 grehlen += 4;
406 if (flags & GRE_KEY) {
407 key_off = grehlen + offset;
408 grehlen += 4;
c12b395a 409 }
410
7892032c 411 if (!pskb_may_pull(skb, offset + grehlen))
c12b395a 412 return;
b87fb39e 413 ipv6h = (const struct ipv6hdr *)skb->data;
7892032c
ED
414 greh = (const struct gre_base_hdr *)(skb->data + offset);
415 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
c12b395a 416
c12b395a 417 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
7892032c 418 key, greh->protocol);
63159f29 419 if (!t)
0c5794a6 420 return;
c12b395a 421
422 switch (type) {
c12b395a 423 struct ipv6_tlv_tnl_enc_lim *tel;
fe1a4ca0 424 __u32 teli;
c12b395a 425 case ICMPV6_DEST_UNREACH:
a46496ce
MB
426 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
427 t->parms.name);
f8d20b46
XL
428 if (code != ICMPV6_PORT_UNREACH)
429 break;
430 return;
c12b395a 431 case ICMPV6_TIME_EXCEED:
432 if (code == ICMPV6_EXC_HOPLIMIT) {
a46496ce
MB
433 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
434 t->parms.name);
f8d20b46 435 break;
c12b395a 436 }
f8d20b46 437 return;
c12b395a 438 case ICMPV6_PARAMPROB:
439 teli = 0;
440 if (code == ICMPV6_HDR_FIELD)
441 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
442
d1e158e2 443 if (teli && teli == be32_to_cpu(info) - 2) {
c12b395a 444 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
445 if (tel->encap_limit == 0) {
a46496ce
MB
446 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
447 t->parms.name);
c12b395a 448 }
449 } else {
a46496ce
MB
450 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
451 t->parms.name);
c12b395a 452 }
f8d20b46 453 return;
c12b395a 454 case ICMPV6_PKT_TOOBIG:
fe1a4ca0 455 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
f8d20b46 456 return;
929fc032
XL
457 case NDISC_REDIRECT:
458 ip6_redirect(skb, net, skb->dev->ifindex, 0,
459 sock_net_uid(net, NULL));
460 return;
c12b395a 461 }
462
463 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
464 t->err_count++;
465 else
466 t->err_count = 1;
467 t->err_time = jiffies;
c12b395a 468}
469
308edfdf 470static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
c12b395a 471{
472 const struct ipv6hdr *ipv6h;
c12b395a 473 struct ip6_tnl *tunnel;
c12b395a 474
475 ipv6h = ipv6_hdr(skb);
c12b395a 476 tunnel = ip6gre_tunnel_lookup(skb->dev,
308edfdf
TH
477 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
478 tpi->proto);
c12b395a 479 if (tunnel) {
6712abc1
WT
480 if (tunnel->parms.collect_md) {
481 struct metadata_dst *tun_dst;
482 __be64 tun_id;
483 __be16 flags;
484
485 flags = tpi->flags;
486 tun_id = key32_to_tunnel_id(tpi->key);
487
488 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
489 if (!tun_dst)
490 return PACKET_REJECT;
491
492 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
493 } else {
494 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
495 }
c12b395a 496
308edfdf
TH
497 return PACKET_RCVD;
498 }
eccc1bb8 499
308edfdf
TH
500 return PACKET_REJECT;
501}
eccc1bb8 502
5a963eb6
WT
503static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
504 struct tnl_ptk_info *tpi)
505{
1d7e2ed2
WT
506 struct erspan_base_hdr *ershdr;
507 struct erspan_metadata *pkt_md;
5a963eb6 508 const struct ipv6hdr *ipv6h;
3df19283 509 struct erspan_md2 *md2;
5a963eb6 510 struct ip6_tnl *tunnel;
1d7e2ed2 511 u8 ver;
5a963eb6 512
5a963eb6
WT
513 if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
514 return PACKET_REJECT;
515
293a1991
HY
516 ipv6h = ipv6_hdr(skb);
517 ershdr = (struct erspan_base_hdr *)skb->data;
c69de58b
WT
518 ver = ershdr->ver;
519 tpi->key = cpu_to_be32(get_session_id(ershdr));
5a963eb6
WT
520
521 tunnel = ip6gre_tunnel_lookup(skb->dev,
522 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
523 tpi->proto);
524 if (tunnel) {
1d7e2ed2
WT
525 int len = erspan_hdr_len(ver);
526
527 if (unlikely(!pskb_may_pull(skb, len)))
ae3e1337 528 return PACKET_REJECT;
1d7e2ed2 529
d91e8db5
WT
530 ershdr = (struct erspan_base_hdr *)skb->data;
531 pkt_md = (struct erspan_metadata *)(ershdr + 1);
532
1d7e2ed2 533 if (__iptunnel_pull_header(skb, len,
5a963eb6
WT
534 htons(ETH_P_TEB),
535 false, false) < 0)
536 return PACKET_REJECT;
537
ef7baf5e
WT
538 if (tunnel->parms.collect_md) {
539 struct metadata_dst *tun_dst;
540 struct ip_tunnel_info *info;
541 struct erspan_metadata *md;
542 __be64 tun_id;
543 __be16 flags;
544
545 tpi->flags |= TUNNEL_KEY;
546 flags = tpi->flags;
547 tun_id = key32_to_tunnel_id(tpi->key);
548
549 tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
550 sizeof(*md));
551 if (!tun_dst)
552 return PACKET_REJECT;
553
554 info = &tun_dst->u.tun_info;
555 md = ip_tunnel_info_opts(info);
94d7d8f2 556 md->version = ver;
3df19283
WT
557 md2 = &md->u.md2;
558 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
559 ERSPAN_V2_MDSIZE);
ef7baf5e
WT
560 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
561 info->options_len = sizeof(*md);
562
563 ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
564
565 } else {
ef7baf5e
WT
566 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
567 }
5a963eb6
WT
568
569 return PACKET_RCVD;
570 }
571
572 return PACKET_REJECT;
573}
574
308edfdf
TH
575static int gre_rcv(struct sk_buff *skb)
576{
577 struct tnl_ptk_info tpi;
578 bool csum_err = false;
579 int hdr_len;
eccc1bb8 580
e582615a 581 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
f132ae7c 582 if (hdr_len < 0)
308edfdf 583 goto drop;
c12b395a 584
308edfdf
TH
585 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
586 goto drop;
c12b395a 587
94d7d8f2
WT
588 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
589 tpi.proto == htons(ETH_P_ERSPAN2))) {
5a963eb6
WT
590 if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
591 return 0;
a7343211 592 goto out;
5a963eb6
WT
593 }
594
308edfdf 595 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
c12b395a 596 return 0;
c12b395a 597
a7343211 598out:
308edfdf 599 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
c12b395a 600drop:
c12b395a 601 kfree_skb(skb);
602 return 0;
603}
604
b05229f4 605static int gre_handle_offloads(struct sk_buff *skb, bool csum)
c12b395a 606{
b05229f4
TH
607 return iptunnel_handle_offloads(skb,
608 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
c12b395a 609}
610
898b2979
WT
611static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
612 struct net_device *dev,
613 struct flowi6 *fl6, __u8 *dsfield,
614 int *encap_limit)
615{
616 const struct iphdr *iph = ip_hdr(skb);
617 struct ip6_tnl *t = netdev_priv(dev);
618
619 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
620 *encap_limit = t->parms.encap_limit;
621
622 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
623
624 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
625 *dsfield = ipv4_get_dsfield(iph);
626 else
627 *dsfield = ip6_tclass(t->parms.flowinfo);
628
629 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
630 fl6->flowi6_mark = skb->mark;
631 else
632 fl6->flowi6_mark = t->parms.fwmark;
633
634 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
635}
636
637static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
638 struct net_device *dev,
639 struct flowi6 *fl6, __u8 *dsfield,
640 int *encap_limit)
641{
642 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
643 struct ip6_tnl *t = netdev_priv(dev);
644 __u16 offset;
645
646 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
647 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
648
649 if (offset > 0) {
650 struct ipv6_tlv_tnl_enc_lim *tel;
651
652 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
653 if (tel->encap_limit == 0) {
654 icmpv6_send(skb, ICMPV6_PARAMPROB,
655 ICMPV6_HDR_FIELD, offset + 2);
656 return -1;
657 }
658 *encap_limit = tel->encap_limit - 1;
659 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
660 *encap_limit = t->parms.encap_limit;
661 }
662
663 memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));
664
665 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
666 *dsfield = ipv6_get_dsfield(ipv6h);
667 else
668 *dsfield = ip6_tclass(t->parms.flowinfo);
669
670 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
671 fl6->flowlabel |= ip6_flowlabel(ipv6h);
672
673 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
674 fl6->flowi6_mark = skb->mark;
675 else
676 fl6->flowi6_mark = t->parms.fwmark;
677
678 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
679
680 return 0;
681}
682
b05229f4
TH
683static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
684 struct net_device *dev, __u8 dsfield,
685 struct flowi6 *fl6, int encap_limit,
686 __u32 *pmtu, __be16 proto)
c12b395a 687{
c12b395a 688 struct ip6_tnl *tunnel = netdev_priv(dev);
8aec4959 689 __be16 protocol;
c12b395a 690
691 if (dev->type == ARPHRD_ETHER)
692 IPCB(skb)->flags = 0;
693
b05229f4
TH
694 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
695 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
696 else
c12b395a 697 fl6->daddr = tunnel->parms.raddr;
c12b395a 698
b05229f4 699 /* Push GRE header. */
8aec4959 700 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
6712abc1
WT
701
702 if (tunnel->parms.collect_md) {
703 struct ip_tunnel_info *tun_info;
704 const struct ip_tunnel_key *key;
705 __be16 flags;
706
707 tun_info = skb_tunnel_info(skb);
708 if (unlikely(!tun_info ||
709 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
710 ip_tunnel_info_af(tun_info) != AF_INET6))
711 return -EINVAL;
712
713 key = &tun_info->key;
714 memset(fl6, 0, sizeof(*fl6));
715 fl6->flowi6_proto = IPPROTO_GRE;
716 fl6->daddr = key->u.ipv6.dst;
717 fl6->flowlabel = key->label;
718 fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
719
720 dsfield = key->tos;
77a5196a
WT
721 flags = key->tun_flags &
722 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
6712abc1
WT
723 tunnel->tun_hlen = gre_calc_hlen(flags);
724
725 gre_build_header(skb, tunnel->tun_hlen,
726 flags, protocol,
77a5196a 727 tunnel_id_to_key32(tun_info->key.tun_id),
15746394 728 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
77a5196a 729 : 0);
6712abc1
WT
730
731 } else {
77a5196a
WT
732 if (tunnel->parms.o_flags & TUNNEL_SEQ)
733 tunnel->o_seqno++;
734
6712abc1
WT
735 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
736 protocol, tunnel->parms.o_key,
737 htonl(tunnel->o_seqno));
738 }
c12b395a 739
b05229f4
TH
740 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
741 NEXTHDR_GRE);
c12b395a 742}
743
744static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
745{
746 struct ip6_tnl *t = netdev_priv(dev);
c12b395a 747 int encap_limit = -1;
748 struct flowi6 fl6;
6712abc1 749 __u8 dsfield = 0;
c12b395a 750 __u32 mtu;
751 int err;
752
5146d1f1
BH
753 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
754
6712abc1
WT
755 if (!t->parms.collect_md)
756 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
757 &dsfield, &encap_limit);
e2d118a1 758
b05229f4
TH
759 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
760 if (err)
761 return -1;
762
763 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
764 skb->protocol);
c12b395a 765 if (err != 0) {
766 /* XXX: send ICMP error even if DF is not set. */
767 if (err == -EMSGSIZE)
768 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
769 htonl(mtu));
770 return -1;
771 }
772
773 return 0;
774}
775
776static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
777{
778 struct ip6_tnl *t = netdev_priv(dev);
779 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
780 int encap_limit = -1;
c12b395a 781 struct flowi6 fl6;
6712abc1 782 __u8 dsfield = 0;
c12b395a 783 __u32 mtu;
784 int err;
785
786 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
787 return -1;
788
6712abc1
WT
789 if (!t->parms.collect_md &&
790 prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
898b2979 791 return -1;
e2d118a1 792
b05229f4
TH
793 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
794 return -1;
795
796 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
797 &mtu, skb->protocol);
c12b395a 798 if (err != 0) {
799 if (err == -EMSGSIZE)
800 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
801 return -1;
802 }
803
804 return 0;
805}
806
807/**
808 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
809 * @t: the outgoing tunnel device
810 * @hdr: IPv6 header from the incoming packet
811 *
812 * Description:
813 * Avoid trivial tunneling loop by checking that tunnel exit-point
814 * doesn't match source of incoming packet.
815 *
816 * Return:
817 * 1 if conflict,
818 * 0 else
819 **/
820
821static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
822 const struct ipv6hdr *hdr)
823{
824 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
825}
826
827static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
828{
829 struct ip6_tnl *t = netdev_priv(dev);
830 int encap_limit = -1;
831 struct flowi6 fl6;
832 __u32 mtu;
833 int err;
834
835 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
836 encap_limit = t->parms.encap_limit;
837
6712abc1
WT
838 if (!t->parms.collect_md)
839 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a 840
b05229f4
TH
841 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
842 if (err)
843 return err;
844
845 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
c12b395a 846
847 return err;
848}
849
850static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
851 struct net_device *dev)
852{
853 struct ip6_tnl *t = netdev_priv(dev);
854 struct net_device_stats *stats = &t->dev->stats;
855 int ret;
856
d5005140 857 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
41ab3e31 858 goto tx_err;
c12b395a 859
860 switch (skb->protocol) {
861 case htons(ETH_P_IP):
862 ret = ip6gre_xmit_ipv4(skb, dev);
863 break;
864 case htons(ETH_P_IPV6):
865 ret = ip6gre_xmit_ipv6(skb, dev);
866 break;
867 default:
868 ret = ip6gre_xmit_other(skb, dev);
869 break;
870 }
871
872 if (ret < 0)
873 goto tx_err;
874
875 return NETDEV_TX_OK;
876
877tx_err:
878 stats->tx_errors++;
879 stats->tx_dropped++;
880 kfree_skb(skb);
881 return NETDEV_TX_OK;
882}
883
5a963eb6
WT
884static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
885 struct net_device *dev)
886{
887 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
888 struct ip6_tnl *t = netdev_priv(dev);
889 struct dst_entry *dst = skb_dst(skb);
890 struct net_device_stats *stats;
891 bool truncate = false;
892 int encap_limit = -1;
893 __u8 dsfield = false;
894 struct flowi6 fl6;
895 int err = -EINVAL;
896 __u32 mtu;
897
898 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
899 goto tx_err;
900
901 if (gre_handle_offloads(skb, false))
902 goto tx_err;
903
5a963eb6
WT
904 if (skb->len > dev->mtu + dev->hard_header_len) {
905 pskb_trim(skb, dev->mtu + dev->hard_header_len);
906 truncate = true;
907 }
908
e41c7c68
WT
909 if (skb_cow_head(skb, dev->needed_headroom))
910 goto tx_err;
911
5a963eb6 912 t->parms.o_flags &= ~TUNNEL_KEY;
5a963eb6 913 IPCB(skb)->flags = 0;
ef7baf5e
WT
914
915 /* For collect_md mode, derive fl6 from the tunnel key,
916 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
917 */
918 if (t->parms.collect_md) {
919 struct ip_tunnel_info *tun_info;
920 const struct ip_tunnel_key *key;
921 struct erspan_metadata *md;
c69de58b 922 __be32 tun_id;
ef7baf5e
WT
923
924 tun_info = skb_tunnel_info(skb);
925 if (unlikely(!tun_info ||
926 !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
927 ip_tunnel_info_af(tun_info) != AF_INET6))
928 return -EINVAL;
929
930 key = &tun_info->key;
931 memset(&fl6, 0, sizeof(fl6));
932 fl6.flowi6_proto = IPPROTO_GRE;
933 fl6.daddr = key->u.ipv6.dst;
934 fl6.flowlabel = key->label;
935 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
936
937 dsfield = key->tos;
938 md = ip_tunnel_info_opts(tun_info);
939 if (!md)
940 goto tx_err;
941
c69de58b 942 tun_id = tunnel_id_to_key32(key->tun_id);
94d7d8f2
WT
943 if (md->version == 1) {
944 erspan_build_header(skb,
c69de58b 945 ntohl(tun_id),
94d7d8f2
WT
946 ntohl(md->u.index), truncate,
947 false);
948 } else if (md->version == 2) {
94d7d8f2 949 erspan_build_header_v2(skb,
c69de58b
WT
950 ntohl(tun_id),
951 md->u.md2.dir,
952 get_hwid(&md->u.md2),
953 truncate, false);
d6aa7119
WT
954 } else {
955 goto tx_err;
94d7d8f2 956 }
ef7baf5e
WT
957 } else {
958 switch (skb->protocol) {
959 case htons(ETH_P_IP):
960 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
961 prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
962 &dsfield, &encap_limit);
963 break;
964 case htons(ETH_P_IPV6):
965 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
966 goto tx_err;
967 if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
968 &dsfield, &encap_limit))
969 goto tx_err;
970 break;
971 default:
972 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
973 break;
974 }
975
94d7d8f2 976 if (t->parms.erspan_ver == 1)
c69de58b 977 erspan_build_header(skb, ntohl(t->parms.o_key),
94d7d8f2
WT
978 t->parms.index,
979 truncate, false);
980 else
c69de58b 981 erspan_build_header_v2(skb, ntohl(t->parms.o_key),
94d7d8f2
WT
982 t->parms.dir,
983 t->parms.hwid,
984 truncate, false);
ef7baf5e
WT
985 fl6.daddr = t->parms.raddr;
986 }
5a963eb6
WT
987
988 /* Push GRE header. */
989 gre_build_header(skb, 8, TUNNEL_SEQ,
990 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));
991
992 /* TooBig packet may have updated dst->dev's mtu */
ef7baf5e 993 if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
5a963eb6
WT
994 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
995
996 err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
997 NEXTHDR_GRE);
998 if (err != 0) {
999 /* XXX: send ICMP error even if DF is not set. */
1000 if (err == -EMSGSIZE) {
1001 if (skb->protocol == htons(ETH_P_IP))
1002 icmp_send(skb, ICMP_DEST_UNREACH,
1003 ICMP_FRAG_NEEDED, htonl(mtu));
1004 else
1005 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
1006 }
1007
1008 goto tx_err;
1009 }
1010 return NETDEV_TX_OK;
1011
1012tx_err:
1013 stats = &t->dev->stats;
1014 stats->tx_errors++;
1015 stats->tx_dropped++;
1016 kfree_skb(skb);
1017 return NETDEV_TX_OK;
1018}
1019
c12b395a 1020static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1021{
1022 struct net_device *dev = t->dev;
1023 struct __ip6_tnl_parm *p = &t->parms;
1024 struct flowi6 *fl6 = &t->fl.u.ip6;
db2ec95d 1025 int t_hlen;
c12b395a 1026
1027 if (dev->type != ARPHRD_ETHER) {
1028 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1029 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1030 }
1031
1032 /* Set up flowi template */
1033 fl6->saddr = p->laddr;
1034 fl6->daddr = p->raddr;
1035 fl6->flowi6_oif = p->link;
1036 fl6->flowlabel = 0;
252f3f5a 1037 fl6->flowi6_proto = IPPROTO_GRE;
c12b395a 1038
1039 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1040 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1041 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1042 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1043
1044 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1045 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1046
1047 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1048 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1049 dev->flags |= IFF_POINTOPOINT;
1050 else
1051 dev->flags &= ~IFF_POINTOPOINT;
1052
db2ec95d
TH
1053 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1054
1faf3d9f 1055 t->hlen = t->encap_hlen + t->tun_hlen;
db2ec95d
TH
1056
1057 t_hlen = t->hlen + sizeof(struct ipv6hdr);
c12b395a 1058
1059 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1060 int strict = (ipv6_addr_type(&p->raddr) &
1061 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1062
22f08069 1063 struct rt6_info *rt = rt6_lookup(t->net,
c12b395a 1064 &p->raddr, &p->laddr,
b75cc8f9 1065 p->link, NULL, strict);
c12b395a 1066
63159f29 1067 if (!rt)
c12b395a 1068 return;
1069
1070 if (rt->dst.dev) {
db2ec95d
TH
1071 dev->hard_header_len = rt->dst.dev->hard_header_len +
1072 t_hlen;
c12b395a 1073
1074 if (set_mtu) {
db2ec95d 1075 dev->mtu = rt->dst.dev->mtu - t_hlen;
c12b395a 1076 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1077 dev->mtu -= 8;
a9e242ca
AD
1078 if (dev->type == ARPHRD_ETHER)
1079 dev->mtu -= ETH_HLEN;
c12b395a 1080
1081 if (dev->mtu < IPV6_MIN_MTU)
1082 dev->mtu = IPV6_MIN_MTU;
1083 }
1084 }
94e187c0 1085 ip6_rt_put(rt);
c12b395a 1086 }
c12b395a 1087}
1088
1089static int ip6gre_tnl_change(struct ip6_tnl *t,
1090 const struct __ip6_tnl_parm *p, int set_mtu)
1091{
1092 t->parms.laddr = p->laddr;
1093 t->parms.raddr = p->raddr;
1094 t->parms.flags = p->flags;
1095 t->parms.hop_limit = p->hop_limit;
1096 t->parms.encap_limit = p->encap_limit;
1097 t->parms.flowinfo = p->flowinfo;
1098 t->parms.link = p->link;
1099 t->parms.proto = p->proto;
1100 t->parms.i_key = p->i_key;
1101 t->parms.o_key = p->o_key;
1102 t->parms.i_flags = p->i_flags;
1103 t->parms.o_flags = p->o_flags;
0a473b82 1104 t->parms.fwmark = p->fwmark;
607f725f 1105 dst_cache_reset(&t->dst_cache);
c12b395a 1106 ip6gre_tnl_link_config(t, set_mtu);
1107 return 0;
1108}
1109
1110static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1111 const struct ip6_tnl_parm2 *u)
1112{
1113 p->laddr = u->laddr;
1114 p->raddr = u->raddr;
1115 p->flags = u->flags;
1116 p->hop_limit = u->hop_limit;
1117 p->encap_limit = u->encap_limit;
1118 p->flowinfo = u->flowinfo;
1119 p->link = u->link;
1120 p->i_key = u->i_key;
1121 p->o_key = u->o_key;
f41fe3c2
TH
1122 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
1123 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
c12b395a 1124 memcpy(p->name, u->name, sizeof(u->name));
1125}
1126
1127static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1128 const struct __ip6_tnl_parm *p)
1129{
1130 u->proto = IPPROTO_GRE;
1131 u->laddr = p->laddr;
1132 u->raddr = p->raddr;
1133 u->flags = p->flags;
1134 u->hop_limit = p->hop_limit;
1135 u->encap_limit = p->encap_limit;
1136 u->flowinfo = p->flowinfo;
1137 u->link = p->link;
1138 u->i_key = p->i_key;
1139 u->o_key = p->o_key;
f41fe3c2
TH
1140 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
1141 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
c12b395a 1142 memcpy(u->name, p->name, sizeof(u->name));
1143}
1144
1145static int ip6gre_tunnel_ioctl(struct net_device *dev,
1146 struct ifreq *ifr, int cmd)
1147{
1148 int err = 0;
1149 struct ip6_tnl_parm2 p;
1150 struct __ip6_tnl_parm p1;
22f08069
ND
1151 struct ip6_tnl *t = netdev_priv(dev);
1152 struct net *net = t->net;
c12b395a 1153 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1154
308edfdf
TH
1155 memset(&p1, 0, sizeof(p1));
1156
c12b395a 1157 switch (cmd) {
1158 case SIOCGETTUNNEL:
c12b395a 1159 if (dev == ign->fb_tunnel_dev) {
1160 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1161 err = -EFAULT;
1162 break;
1163 }
1164 ip6gre_tnl_parm_from_user(&p1, &p);
1165 t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29 1166 if (!t)
22f08069 1167 t = netdev_priv(dev);
c12b395a 1168 }
5dbd5068 1169 memset(&p, 0, sizeof(p));
c12b395a 1170 ip6gre_tnl_parm_to_user(&p, &t->parms);
1171 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1172 err = -EFAULT;
1173 break;
1174
1175 case SIOCADDTUNNEL:
1176 case SIOCCHGTUNNEL:
1177 err = -EPERM;
af31f412 1178 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a 1179 goto done;
1180
1181 err = -EFAULT;
1182 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1183 goto done;
1184
1185 err = -EINVAL;
1186 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1187 goto done;
1188
1189 if (!(p.i_flags&GRE_KEY))
1190 p.i_key = 0;
1191 if (!(p.o_flags&GRE_KEY))
1192 p.o_key = 0;
1193
1194 ip6gre_tnl_parm_from_user(&p1, &p);
1195 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1196
1197 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
53b24b8f 1198 if (t) {
c12b395a 1199 if (t->dev != dev) {
1200 err = -EEXIST;
1201 break;
1202 }
1203 } else {
1204 t = netdev_priv(dev);
1205
1206 ip6gre_tunnel_unlink(ign, t);
1207 synchronize_net();
1208 ip6gre_tnl_change(t, &p1, 1);
1209 ip6gre_tunnel_link(ign, t);
1210 netdev_state_change(dev);
1211 }
1212 }
1213
1214 if (t) {
1215 err = 0;
1216
5dbd5068 1217 memset(&p, 0, sizeof(p));
c12b395a 1218 ip6gre_tnl_parm_to_user(&p, &t->parms);
1219 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1220 err = -EFAULT;
1221 } else
1222 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1223 break;
1224
1225 case SIOCDELTUNNEL:
1226 err = -EPERM;
af31f412 1227 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a 1228 goto done;
1229
1230 if (dev == ign->fb_tunnel_dev) {
1231 err = -EFAULT;
1232 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1233 goto done;
1234 err = -ENOENT;
1235 ip6gre_tnl_parm_from_user(&p1, &p);
1236 t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29 1237 if (!t)
c12b395a 1238 goto done;
1239 err = -EPERM;
1240 if (t == netdev_priv(ign->fb_tunnel_dev))
1241 goto done;
1242 dev = t->dev;
1243 }
1244 unregister_netdevice(dev);
1245 err = 0;
1246 break;
1247
1248 default:
1249 err = -EINVAL;
1250 }
1251
1252done:
1253 return err;
1254}
1255
c12b395a 1256static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
76cc0d32
XL
1257 unsigned short type, const void *daddr,
1258 const void *saddr, unsigned int len)
c12b395a 1259{
1260 struct ip6_tnl *t = netdev_priv(dev);
76cc0d32
XL
1261 struct ipv6hdr *ipv6h;
1262 __be16 *p;
c12b395a 1263
76cc0d32
XL
1264 ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
1265 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
1266 t->fl.u.ip6.flowlabel,
1267 true, &t->fl.u.ip6));
c12b395a 1268 ipv6h->hop_limit = t->parms.hop_limit;
1269 ipv6h->nexthdr = NEXTHDR_GRE;
1270 ipv6h->saddr = t->parms.laddr;
1271 ipv6h->daddr = t->parms.raddr;
1272
76cc0d32
XL
1273 p = (__be16 *)(ipv6h + 1);
1274 p[0] = t->parms.o_flags;
1275 p[1] = htons(type);
c12b395a 1276
1277 /*
1278 * Set the source hardware address.
1279 */
1280
1281 if (saddr)
1282 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1283 if (daddr)
1284 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1285 if (!ipv6_addr_any(&ipv6h->daddr))
1286 return t->hlen;
1287
1288 return -t->hlen;
1289}
1290
c12b395a 1291static const struct header_ops ip6gre_header_ops = {
1292 .create = ip6gre_header,
c12b395a 1293};
1294
1295static const struct net_device_ops ip6gre_netdev_ops = {
1296 .ndo_init = ip6gre_tunnel_init,
1297 .ndo_uninit = ip6gre_tunnel_uninit,
1298 .ndo_start_xmit = ip6gre_tunnel_xmit,
1299 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
b05229f4 1300 .ndo_change_mtu = ip6_tnl_change_mtu,
f61dd388 1301 .ndo_get_stats64 = ip_tunnel_get_stats64,
ecf2c06a 1302 .ndo_get_iflink = ip6_tnl_get_iflink,
c12b395a 1303};
1304
1305static void ip6gre_dev_free(struct net_device *dev)
1306{
cdf3464e
MKL
1307 struct ip6_tnl *t = netdev_priv(dev);
1308
607f725f 1309 dst_cache_destroy(&t->dst_cache);
c12b395a 1310 free_percpu(dev->tstats);
c12b395a 1311}
1312
1313static void ip6gre_tunnel_setup(struct net_device *dev)
1314{
c12b395a 1315 dev->netdev_ops = &ip6gre_netdev_ops;
cf124db5
DM
1316 dev->needs_free_netdev = true;
1317 dev->priv_destructor = ip6gre_dev_free;
c12b395a 1318
1319 dev->type = ARPHRD_IP6GRE;
b05229f4 1320
c12b395a 1321 dev->flags |= IFF_NOARP;
c12b395a 1322 dev->addr_len = sizeof(struct in6_addr);
02875878 1323 netif_keep_dst(dev);
45ce0fd1
FJ
1324 /* This perm addr will be used as interface identifier by IPv6 */
1325 dev->addr_assign_type = NET_ADDR_RANDOM;
1326 eth_random_addr(dev->perm_addr);
c12b395a 1327}
1328
e5a9336a
AK
1329#define GRE6_FEATURES (NETIF_F_SG | \
1330 NETIF_F_FRAGLIST | \
1331 NETIF_F_HIGHDMA | \
1332 NETIF_F_HW_CSUM)
1333
1334static void ip6gre_tnl_init_features(struct net_device *dev)
1335{
1336 struct ip6_tnl *nt = netdev_priv(dev);
1337
1338 dev->features |= GRE6_FEATURES;
1339 dev->hw_features |= GRE6_FEATURES;
1340
1341 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
1342 /* TCP offload with GRE SEQ is not supported, nor
1343 * can we support 2 levels of outer headers requiring
1344 * an update.
1345 */
1346 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1347 nt->encap.type == TUNNEL_ENCAP_NONE) {
1348 dev->features |= NETIF_F_GSO_SOFTWARE;
1349 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1350 }
1351
1352 /* Can use a lockless transmit, unless we generate
1353 * output sequences
1354 */
1355 dev->features |= NETIF_F_LLTX;
1356 }
1357}
1358
a3c119d3 1359static int ip6gre_tunnel_init_common(struct net_device *dev)
c12b395a 1360{
1361 struct ip6_tnl *tunnel;
cdf3464e 1362 int ret;
b05229f4 1363 int t_hlen;
c12b395a 1364
1365 tunnel = netdev_priv(dev);
1366
1367 tunnel->dev = dev;
0bd87628 1368 tunnel->net = dev_net(dev);
c12b395a 1369 strcpy(tunnel->parms.name, dev->name);
1370
a3c119d3
MKL
1371 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1372 if (!dev->tstats)
1373 return -ENOMEM;
1374
607f725f 1375 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
cdf3464e
MKL
1376 if (ret) {
1377 free_percpu(dev->tstats);
1378 dev->tstats = NULL;
1379 return ret;
1380 }
1381
b05229f4 1382 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1faf3d9f 1383 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
db2ec95d 1384 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
b05229f4 1385
db2ec95d
TH
1386 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1387 dev->mtu = ETH_DATA_LEN - t_hlen;
1b227e53
HY
1388 if (dev->type == ARPHRD_ETHER)
1389 dev->mtu -= ETH_HLEN;
b05229f4
TH
1390 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1391 dev->mtu -= 8;
1392
6712abc1
WT
1393 if (tunnel->parms.collect_md) {
1394 dev->features |= NETIF_F_NETNS_LOCAL;
1395 netif_keep_dst(dev);
1396 }
e5a9336a 1397 ip6gre_tnl_init_features(dev);
6712abc1 1398
a3c119d3
MKL
1399 return 0;
1400}
1401
1402static int ip6gre_tunnel_init(struct net_device *dev)
1403{
1404 struct ip6_tnl *tunnel;
1405 int ret;
1406
1407 ret = ip6gre_tunnel_init_common(dev);
1408 if (ret)
1409 return ret;
1410
1411 tunnel = netdev_priv(dev);
1412
6712abc1
WT
1413 if (tunnel->parms.collect_md)
1414 return 0;
1415
c12b395a 1416 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1417 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1418
1419 if (ipv6_addr_any(&tunnel->parms.raddr))
1420 dev->header_ops = &ip6gre_header_ops;
1421
c12b395a 1422 return 0;
1423}
1424
1425static void ip6gre_fb_tunnel_init(struct net_device *dev)
1426{
1427 struct ip6_tnl *tunnel = netdev_priv(dev);
1428
1429 tunnel->dev = dev;
0bd87628 1430 tunnel->net = dev_net(dev);
c12b395a 1431 strcpy(tunnel->parms.name, dev->name);
1432
1433 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1434
1435 dev_hold(dev);
1436}
1437
c12b395a 1438static struct inet6_protocol ip6gre_protocol __read_mostly = {
308edfdf 1439 .handler = gre_rcv,
c12b395a 1440 .err_handler = ip6gre_err,
1441 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1442};
1443
22f08069 1444static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
c12b395a 1445{
22f08069
ND
1446 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1447 struct net_device *dev, *aux;
c12b395a 1448 int prio;
1449
22f08069
ND
1450 for_each_netdev_safe(net, dev, aux)
1451 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
5a963eb6
WT
1452 dev->rtnl_link_ops == &ip6gre_tap_ops ||
1453 dev->rtnl_link_ops == &ip6erspan_tap_ops)
22f08069
ND
1454 unregister_netdevice_queue(dev, head);
1455
c12b395a 1456 for (prio = 0; prio < 4; prio++) {
1457 int h;
e87a8f24 1458 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
c12b395a 1459 struct ip6_tnl *t;
1460
1461 t = rtnl_dereference(ign->tunnels[prio][h]);
1462
53b24b8f 1463 while (t) {
22f08069
ND
1464 /* If dev is in the same netns, it has already
1465 * been added to the list by the previous loop.
1466 */
1467 if (!net_eq(dev_net(t->dev), net))
1468 unregister_netdevice_queue(t->dev,
1469 head);
c12b395a 1470 t = rtnl_dereference(t->next);
1471 }
1472 }
1473 }
1474}
1475
1476static int __net_init ip6gre_init_net(struct net *net)
1477{
1478 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1479 int err;
1480
79134e6c
ED
1481 if (!net_has_fallback_tunnels(net))
1482 return 0;
c12b395a 1483 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
c835a677
TG
1484 NET_NAME_UNKNOWN,
1485 ip6gre_tunnel_setup);
c12b395a 1486 if (!ign->fb_tunnel_dev) {
1487 err = -ENOMEM;
1488 goto err_alloc_dev;
1489 }
1490 dev_net_set(ign->fb_tunnel_dev, net);
22f08069
ND
1491 /* FB netdevice is special: we have one, and only one per netns.
1492 * Allowing to move it to another netns is clearly unsafe.
1493 */
1494 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1495
c12b395a 1496
1497 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1498 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1499
1500 err = register_netdev(ign->fb_tunnel_dev);
1501 if (err)
1502 goto err_reg_dev;
1503
1504 rcu_assign_pointer(ign->tunnels_wc[0],
1505 netdev_priv(ign->fb_tunnel_dev));
1506 return 0;
1507
1508err_reg_dev:
cf124db5 1509 free_netdev(ign->fb_tunnel_dev);
c12b395a 1510err_alloc_dev:
1511 return err;
1512}
1513
bb401cae 1514static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
c12b395a 1515{
bb401cae 1516 struct net *net;
c12b395a 1517 LIST_HEAD(list);
1518
c12b395a 1519 rtnl_lock();
bb401cae
ED
1520 list_for_each_entry(net, net_list, exit_list)
1521 ip6gre_destroy_tunnels(net, &list);
c12b395a 1522 unregister_netdevice_many(&list);
1523 rtnl_unlock();
1524}
1525
1526static struct pernet_operations ip6gre_net_ops = {
1527 .init = ip6gre_init_net,
bb401cae 1528 .exit_batch = ip6gre_exit_batch_net,
c12b395a 1529 .id = &ip6gre_net_id,
1530 .size = sizeof(struct ip6gre_net),
1531};
1532
a8b8a889
MS
1533static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1534 struct netlink_ext_ack *extack)
c12b395a 1535{
1536 __be16 flags;
1537
1538 if (!data)
1539 return 0;
1540
1541 flags = 0;
1542 if (data[IFLA_GRE_IFLAGS])
1543 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1544 if (data[IFLA_GRE_OFLAGS])
1545 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1546 if (flags & (GRE_VERSION|GRE_ROUTING))
1547 return -EINVAL;
1548
1549 return 0;
1550}
1551
a8b8a889
MS
1552static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1553 struct netlink_ext_ack *extack)
c12b395a 1554{
1555 struct in6_addr daddr;
1556
1557 if (tb[IFLA_ADDRESS]) {
1558 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1559 return -EINVAL;
1560 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1561 return -EADDRNOTAVAIL;
1562 }
1563
1564 if (!data)
1565 goto out;
1566
1567 if (data[IFLA_GRE_REMOTE]) {
67b61f6c 1568 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a 1569 if (ipv6_addr_any(&daddr))
1570 return -EINVAL;
1571 }
1572
1573out:
a8b8a889 1574 return ip6gre_tunnel_validate(tb, data, extack);
c12b395a 1575}
1576
5a963eb6
WT
1577static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1578 struct netlink_ext_ack *extack)
1579{
1580 __be16 flags = 0;
94d7d8f2 1581 int ret, ver = 0;
5a963eb6
WT
1582
1583 if (!data)
1584 return 0;
1585
1586 ret = ip6gre_tap_validate(tb, data, extack);
1587 if (ret)
1588 return ret;
1589
1590 /* ERSPAN should only have GRE sequence and key flag */
1591 if (data[IFLA_GRE_OFLAGS])
1592 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1593 if (data[IFLA_GRE_IFLAGS])
1594 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1595 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1596 flags != (GRE_SEQ | GRE_KEY))
1597 return -EINVAL;
1598
1599 /* ERSPAN Session ID only has 10-bit. Since we reuse
1600 * 32-bit key field as ID, check it's range.
1601 */
1602 if (data[IFLA_GRE_IKEY] &&
1603 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1604 return -EINVAL;
1605
1606 if (data[IFLA_GRE_OKEY] &&
1607 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1608 return -EINVAL;
1609
94d7d8f2
WT
1610 if (data[IFLA_GRE_ERSPAN_VER]) {
1611 ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1612 if (ver != 1 && ver != 2)
5a963eb6
WT
1613 return -EINVAL;
1614 }
94d7d8f2
WT
1615
1616 if (ver == 1) {
1617 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1618 u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1619
1620 if (index & ~INDEX_MASK)
1621 return -EINVAL;
1622 }
1623 } else if (ver == 2) {
1624 if (data[IFLA_GRE_ERSPAN_DIR]) {
1625 u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1626
1627 if (dir & ~(DIR_MASK >> DIR_OFFSET))
1628 return -EINVAL;
1629 }
1630
1631 if (data[IFLA_GRE_ERSPAN_HWID]) {
1632 u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1633
1634 if (hwid & ~(HWID_MASK >> HWID_OFFSET))
1635 return -EINVAL;
1636 }
1637 }
1638
5a963eb6
WT
1639 return 0;
1640}
c12b395a 1641
1642static void ip6gre_netlink_parms(struct nlattr *data[],
1643 struct __ip6_tnl_parm *parms)
1644{
1645 memset(parms, 0, sizeof(*parms));
1646
1647 if (!data)
1648 return;
1649
1650 if (data[IFLA_GRE_LINK])
1651 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1652
1653 if (data[IFLA_GRE_IFLAGS])
f41fe3c2
TH
1654 parms->i_flags = gre_flags_to_tnl_flags(
1655 nla_get_be16(data[IFLA_GRE_IFLAGS]));
c12b395a 1656
1657 if (data[IFLA_GRE_OFLAGS])
f41fe3c2
TH
1658 parms->o_flags = gre_flags_to_tnl_flags(
1659 nla_get_be16(data[IFLA_GRE_OFLAGS]));
c12b395a 1660
1661 if (data[IFLA_GRE_IKEY])
1662 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1663
1664 if (data[IFLA_GRE_OKEY])
1665 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1666
1667 if (data[IFLA_GRE_LOCAL])
67b61f6c 1668 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
c12b395a 1669
1670 if (data[IFLA_GRE_REMOTE])
67b61f6c 1671 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a 1672
1673 if (data[IFLA_GRE_TTL])
1674 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1675
1676 if (data[IFLA_GRE_ENCAP_LIMIT])
1677 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1678
1679 if (data[IFLA_GRE_FLOWINFO])
c2675de4 1680 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
c12b395a 1681
1682 if (data[IFLA_GRE_FLAGS])
1683 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
0a473b82
CG
1684
1685 if (data[IFLA_GRE_FWMARK])
1686 parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
5a963eb6 1687
6712abc1
WT
1688 if (data[IFLA_GRE_COLLECT_METADATA])
1689 parms->collect_md = true;
94d7d8f2
WT
1690
1691 if (data[IFLA_GRE_ERSPAN_VER])
1692 parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1693
1694 if (parms->erspan_ver == 1) {
1695 if (data[IFLA_GRE_ERSPAN_INDEX])
1696 parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1697 } else if (parms->erspan_ver == 2) {
1698 if (data[IFLA_GRE_ERSPAN_DIR])
1699 parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1700 if (data[IFLA_GRE_ERSPAN_HWID])
1701 parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1702 }
c12b395a 1703}
1704
1705static int ip6gre_tap_init(struct net_device *dev)
1706{
a3c119d3 1707 int ret;
c12b395a 1708
a3c119d3
MKL
1709 ret = ip6gre_tunnel_init_common(dev);
1710 if (ret)
1711 return ret;
c12b395a 1712
0a46baaf
SC
1713 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1714
c12b395a 1715 return 0;
1716}
1717
1718static const struct net_device_ops ip6gre_tap_netdev_ops = {
1719 .ndo_init = ip6gre_tap_init,
1720 .ndo_uninit = ip6gre_tunnel_uninit,
1721 .ndo_start_xmit = ip6gre_tunnel_xmit,
1722 .ndo_set_mac_address = eth_mac_addr,
1723 .ndo_validate_addr = eth_validate_addr,
b05229f4 1724 .ndo_change_mtu = ip6_tnl_change_mtu,
f61dd388 1725 .ndo_get_stats64 = ip_tunnel_get_stats64,
ecf2c06a 1726 .ndo_get_iflink = ip6_tnl_get_iflink,
c12b395a 1727};
1728
5a963eb6
WT
1729static int ip6erspan_tap_init(struct net_device *dev)
1730{
1731 struct ip6_tnl *tunnel;
1732 int t_hlen;
1733 int ret;
1734
1735 tunnel = netdev_priv(dev);
1736
1737 tunnel->dev = dev;
1738 tunnel->net = dev_net(dev);
1739 strcpy(tunnel->parms.name, dev->name);
1740
1741 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1742 if (!dev->tstats)
1743 return -ENOMEM;
1744
1745 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1746 if (ret) {
1747 free_percpu(dev->tstats);
1748 dev->tstats = NULL;
1749 return ret;
1750 }
1751
1752 tunnel->tun_hlen = 8;
1753 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
94d7d8f2 1754 erspan_hdr_len(tunnel->parms.erspan_ver);
5a963eb6
WT
1755 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1756
1757 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1758 dev->mtu = ETH_DATA_LEN - t_hlen;
1759 if (dev->type == ARPHRD_ETHER)
1760 dev->mtu -= ETH_HLEN;
1761 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1762 dev->mtu -= 8;
1763
1764 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5a963eb6
WT
1765 ip6gre_tnl_link_config(tunnel, 1);
1766
1767 return 0;
1768}
1769
1770static const struct net_device_ops ip6erspan_netdev_ops = {
1771 .ndo_init = ip6erspan_tap_init,
1772 .ndo_uninit = ip6gre_tunnel_uninit,
1773 .ndo_start_xmit = ip6erspan_tunnel_xmit,
1774 .ndo_set_mac_address = eth_mac_addr,
1775 .ndo_validate_addr = eth_validate_addr,
1776 .ndo_change_mtu = ip6_tnl_change_mtu,
1777 .ndo_get_stats64 = ip_tunnel_get_stats64,
1778 .ndo_get_iflink = ip6_tnl_get_iflink,
1779};
1780
c12b395a 1781static void ip6gre_tap_setup(struct net_device *dev)
1782{
1783
1784 ether_setup(dev);
1785
2c52129a 1786 dev->max_mtu = 0;
c12b395a 1787 dev->netdev_ops = &ip6gre_tap_netdev_ops;
cf124db5
DM
1788 dev->needs_free_netdev = true;
1789 dev->priv_destructor = ip6gre_dev_free;
c12b395a 1790
c12b395a 1791 dev->features |= NETIF_F_NETNS_LOCAL;
d13b161c 1792 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0a46baaf 1793 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2d40557c 1794 netif_keep_dst(dev);
c12b395a 1795}
1796
d1b2a6c4
PM
1797bool is_ip6gretap_dev(const struct net_device *dev)
1798{
1799 return dev->netdev_ops == &ip6gre_tap_netdev_ops;
1800}
1801EXPORT_SYMBOL_GPL(is_ip6gretap_dev);
1802
1faf3d9f
TH
1803static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1804 struct ip_tunnel_encap *ipencap)
1805{
1806 bool ret = false;
1807
1808 memset(ipencap, 0, sizeof(*ipencap));
1809
1810 if (!data)
1811 return ret;
1812
1813 if (data[IFLA_GRE_ENCAP_TYPE]) {
1814 ret = true;
1815 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1816 }
1817
1818 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1819 ret = true;
1820 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1821 }
1822
1823 if (data[IFLA_GRE_ENCAP_SPORT]) {
1824 ret = true;
1825 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1826 }
1827
1828 if (data[IFLA_GRE_ENCAP_DPORT]) {
1829 ret = true;
1830 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1831 }
1832
1833 return ret;
1834}
1835
c12b395a 1836static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
7a3f4a18
MS
1837 struct nlattr *tb[], struct nlattr *data[],
1838 struct netlink_ext_ack *extack)
c12b395a 1839{
1840 struct ip6_tnl *nt;
1841 struct net *net = dev_net(dev);
1842 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1faf3d9f 1843 struct ip_tunnel_encap ipencap;
c12b395a 1844 int err;
1845
1846 nt = netdev_priv(dev);
1faf3d9f
TH
1847
1848 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1849 int err = ip6_tnl_encap_setup(nt, &ipencap);
1850
1851 if (err < 0)
1852 return err;
1853 }
1854
c12b395a 1855 ip6gre_netlink_parms(data, &nt->parms);
1856
6712abc1
WT
1857 if (nt->parms.collect_md) {
1858 if (rtnl_dereference(ign->collect_md_tun))
1859 return -EEXIST;
1860 } else {
1861 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1862 return -EEXIST;
1863 }
c12b395a 1864
1865 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1866 eth_hw_addr_random(dev);
1867
1868 nt->dev = dev;
0bd87628 1869 nt->net = dev_net(dev);
c12b395a 1870
c12b395a 1871 err = register_netdevice(dev);
1872 if (err)
1873 goto out;
1874
128bb975
AK
1875 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1876
1877 if (tb[IFLA_MTU])
1878 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1879
c12b395a 1880 dev_hold(dev);
1881 ip6gre_tunnel_link(ign, nt);
1882
1883out:
1884 return err;
1885}
1886
1887static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
ad744b22
MS
1888 struct nlattr *data[],
1889 struct netlink_ext_ack *extack)
c12b395a 1890{
22f08069
ND
1891 struct ip6_tnl *t, *nt = netdev_priv(dev);
1892 struct net *net = nt->net;
c12b395a 1893 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1894 struct __ip6_tnl_parm p;
1faf3d9f 1895 struct ip_tunnel_encap ipencap;
c12b395a 1896
1897 if (dev == ign->fb_tunnel_dev)
1898 return -EINVAL;
1899
1faf3d9f
TH
1900 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1901 int err = ip6_tnl_encap_setup(nt, &ipencap);
1902
1903 if (err < 0)
1904 return err;
1905 }
1906
c12b395a 1907 ip6gre_netlink_parms(data, &p);
1908
1909 t = ip6gre_tunnel_locate(net, &p, 0);
1910
1911 if (t) {
1912 if (t->dev != dev)
1913 return -EEXIST;
1914 } else {
1915 t = nt;
c12b395a 1916 }
1917
6a61d4db
ND
1918 ip6gre_tunnel_unlink(ign, t);
1919 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1920 ip6gre_tunnel_link(ign, t);
c12b395a 1921 return 0;
1922}
1923
54d63f78
ND
1924static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1925{
1926 struct net *net = dev_net(dev);
1927 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1928
1929 if (dev != ign->fb_tunnel_dev)
1930 unregister_netdevice_queue(dev, head);
1931}
1932
c12b395a 1933static size_t ip6gre_get_size(const struct net_device *dev)
1934{
1935 return
1936 /* IFLA_GRE_LINK */
1937 nla_total_size(4) +
1938 /* IFLA_GRE_IFLAGS */
1939 nla_total_size(2) +
1940 /* IFLA_GRE_OFLAGS */
1941 nla_total_size(2) +
1942 /* IFLA_GRE_IKEY */
1943 nla_total_size(4) +
1944 /* IFLA_GRE_OKEY */
1945 nla_total_size(4) +
1946 /* IFLA_GRE_LOCAL */
a3754133 1947 nla_total_size(sizeof(struct in6_addr)) +
c12b395a 1948 /* IFLA_GRE_REMOTE */
a3754133 1949 nla_total_size(sizeof(struct in6_addr)) +
c12b395a 1950 /* IFLA_GRE_TTL */
1951 nla_total_size(1) +
c12b395a 1952 /* IFLA_GRE_ENCAP_LIMIT */
1953 nla_total_size(1) +
1954 /* IFLA_GRE_FLOWINFO */
1955 nla_total_size(4) +
1956 /* IFLA_GRE_FLAGS */
1957 nla_total_size(4) +
1faf3d9f
TH
1958 /* IFLA_GRE_ENCAP_TYPE */
1959 nla_total_size(2) +
1960 /* IFLA_GRE_ENCAP_FLAGS */
1961 nla_total_size(2) +
1962 /* IFLA_GRE_ENCAP_SPORT */
1963 nla_total_size(2) +
1964 /* IFLA_GRE_ENCAP_DPORT */
1965 nla_total_size(2) +
6712abc1
WT
1966 /* IFLA_GRE_COLLECT_METADATA */
1967 nla_total_size(0) +
0a473b82
CG
1968 /* IFLA_GRE_FWMARK */
1969 nla_total_size(4) +
5a963eb6
WT
1970 /* IFLA_GRE_ERSPAN_INDEX */
1971 nla_total_size(4) +
c12b395a 1972 0;
1973}
1974
1975static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1976{
1977 struct ip6_tnl *t = netdev_priv(dev);
1978 struct __ip6_tnl_parm *p = &t->parms;
1979
1980 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
f41fe3c2
TH
1981 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1982 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1983 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1984 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
c12b395a 1985 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1986 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
930345ea
JB
1987 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1988 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
c12b395a 1989 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
c12b395a 1990 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1991 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
0a473b82 1992 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
5a963eb6
WT
1993 nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
1994 nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
c12b395a 1995 goto nla_put_failure;
1faf3d9f
TH
1996
1997 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1998 t->encap.type) ||
1999 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
2000 t->encap.sport) ||
2001 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
2002 t->encap.dport) ||
2003 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
2004 t->encap.flags))
2005 goto nla_put_failure;
2006
6712abc1
WT
2007 if (p->collect_md) {
2008 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
2009 goto nla_put_failure;
2010 }
2011
94d7d8f2
WT
2012 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
2013 goto nla_put_failure;
2014
2015 if (p->erspan_ver == 1) {
2016 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
2017 goto nla_put_failure;
2018 } else if (p->erspan_ver == 2) {
2019 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
2020 goto nla_put_failure;
2021 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
2022 goto nla_put_failure;
2023 }
2024
c12b395a 2025 return 0;
2026
2027nla_put_failure:
2028 return -EMSGSIZE;
2029}
2030
2031static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
2032 [IFLA_GRE_LINK] = { .type = NLA_U32 },
2033 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
2034 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
2035 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
2036 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
2037 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
2038 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
2039 [IFLA_GRE_TTL] = { .type = NLA_U8 },
2040 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
2041 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
2042 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
1faf3d9f
TH
2043 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
2044 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
2045 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
2046 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
6712abc1 2047 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
0a473b82 2048 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
5a963eb6 2049 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
94d7d8f2
WT
2050 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
2051 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
2052 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
c12b395a 2053};
2054
5a963eb6
WT
2055static void ip6erspan_tap_setup(struct net_device *dev)
2056{
2057 ether_setup(dev);
2058
2059 dev->netdev_ops = &ip6erspan_netdev_ops;
2060 dev->needs_free_netdev = true;
2061 dev->priv_destructor = ip6gre_dev_free;
2062
2063 dev->features |= NETIF_F_NETNS_LOCAL;
2064 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2065 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2066 netif_keep_dst(dev);
2067}
2068
c12b395a 2069static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
2070 .kind = "ip6gre",
2071 .maxtype = IFLA_GRE_MAX,
2072 .policy = ip6gre_policy,
2073 .priv_size = sizeof(struct ip6_tnl),
2074 .setup = ip6gre_tunnel_setup,
2075 .validate = ip6gre_tunnel_validate,
2076 .newlink = ip6gre_newlink,
2077 .changelink = ip6gre_changelink,
54d63f78 2078 .dellink = ip6gre_dellink,
c12b395a 2079 .get_size = ip6gre_get_size,
2080 .fill_info = ip6gre_fill_info,
1728d4fa 2081 .get_link_net = ip6_tnl_get_link_net,
c12b395a 2082};
2083
2084static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
2085 .kind = "ip6gretap",
2086 .maxtype = IFLA_GRE_MAX,
2087 .policy = ip6gre_policy,
2088 .priv_size = sizeof(struct ip6_tnl),
2089 .setup = ip6gre_tap_setup,
2090 .validate = ip6gre_tap_validate,
2091 .newlink = ip6gre_newlink,
2092 .changelink = ip6gre_changelink,
2093 .get_size = ip6gre_get_size,
2094 .fill_info = ip6gre_fill_info,
3390e397 2095 .get_link_net = ip6_tnl_get_link_net,
c12b395a 2096};
2097
5a963eb6
WT
2098static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
2099 .kind = "ip6erspan",
2100 .maxtype = IFLA_GRE_MAX,
2101 .policy = ip6gre_policy,
2102 .priv_size = sizeof(struct ip6_tnl),
2103 .setup = ip6erspan_tap_setup,
2104 .validate = ip6erspan_tap_validate,
2105 .newlink = ip6gre_newlink,
2106 .changelink = ip6gre_changelink,
2107 .get_size = ip6gre_get_size,
2108 .fill_info = ip6gre_fill_info,
2109 .get_link_net = ip6_tnl_get_link_net,
2110};
2111
c12b395a 2112/*
2113 * And now the modules code and kernel interface.
2114 */
2115
2116static int __init ip6gre_init(void)
2117{
2118 int err;
2119
2120 pr_info("GRE over IPv6 tunneling driver\n");
2121
2122 err = register_pernet_device(&ip6gre_net_ops);
2123 if (err < 0)
2124 return err;
2125
2126 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
2127 if (err < 0) {
2128 pr_info("%s: can't add protocol\n", __func__);
2129 goto add_proto_failed;
2130 }
2131
2132 err = rtnl_link_register(&ip6gre_link_ops);
2133 if (err < 0)
2134 goto rtnl_link_failed;
2135
2136 err = rtnl_link_register(&ip6gre_tap_ops);
2137 if (err < 0)
2138 goto tap_ops_failed;
2139
5a963eb6
WT
2140 err = rtnl_link_register(&ip6erspan_tap_ops);
2141 if (err < 0)
2142 goto erspan_link_failed;
2143
c12b395a 2144out:
2145 return err;
2146
5a963eb6
WT
2147erspan_link_failed:
2148 rtnl_link_unregister(&ip6gre_tap_ops);
c12b395a 2149tap_ops_failed:
2150 rtnl_link_unregister(&ip6gre_link_ops);
2151rtnl_link_failed:
2152 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2153add_proto_failed:
2154 unregister_pernet_device(&ip6gre_net_ops);
2155 goto out;
2156}
2157
2158static void __exit ip6gre_fini(void)
2159{
2160 rtnl_link_unregister(&ip6gre_tap_ops);
2161 rtnl_link_unregister(&ip6gre_link_ops);
5a963eb6 2162 rtnl_link_unregister(&ip6erspan_tap_ops);
c12b395a 2163 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
2164 unregister_pernet_device(&ip6gre_net_ops);
2165}
2166
2167module_init(ip6gre_init);
2168module_exit(ip6gre_fini);
2169MODULE_LICENSE("GPL");
2170MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
2171MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
2172MODULE_ALIAS_RTNL_LINK("ip6gre");
5a4ee9a9 2173MODULE_ALIAS_RTNL_LINK("ip6gretap");
94d7d8f2 2174MODULE_ALIAS_RTNL_LINK("ip6erspan");
c12b395a 2175MODULE_ALIAS_NETDEV("ip6gre0");