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