ip6_gre.c 57.9 KB
Newer Older
X
xeb@mail.ru 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 *	GRE over IPv6 protocol decoder.
 *
 *	Authors: Dmitry Kozlov (xeb@mail.ru)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/capability.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/in6.h>
#include <linux/inetdevice.h>
#include <linux/igmp.h>
#include <linux/netfilter_ipv4.h>
#include <linux/etherdevice.h>
#include <linux/if_ether.h>
#include <linux/hash.h>
#include <linux/if_tunnel.h>
#include <linux/ip6_tunnel.h>

#include <net/sock.h>
#include <net/ip.h>
40
#include <net/ip_tunnels.h>
X
xeb@mail.ru 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <net/icmp.h>
#include <net/protocol.h>
#include <net/addrconf.h>
#include <net/arp.h>
#include <net/checksum.h>
#include <net/dsfield.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/rtnetlink.h>

#include <net/ipv6.h>
#include <net/ip6_fib.h>
#include <net/ip6_route.h>
#include <net/ip6_tunnel.h>
57
#include <net/gre.h>
58
#include <net/erspan.h>
59
#include <net/dst_metadata.h>
X
xeb@mail.ru 已提交
60 61


62 63 64 65
static bool log_ecn_error = true;
module_param(log_ecn_error, bool, 0644);
MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");

66 67
#define IP6_GRE_HASH_SIZE_SHIFT  5
#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
X
xeb@mail.ru 已提交
68

69
static unsigned int ip6gre_net_id __read_mostly;
X
xeb@mail.ru 已提交
70
struct ip6gre_net {
71
	struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
X
xeb@mail.ru 已提交
72

73
	struct ip6_tnl __rcu *collect_md_tun;
74
	struct ip6_tnl __rcu *collect_md_tun_erspan;
X
xeb@mail.ru 已提交
75 76 77 78
	struct net_device *fb_tunnel_dev;
};

static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
N
Nicolas Dichtel 已提交
79
static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
80
static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly;
X
xeb@mail.ru 已提交
81 82 83 84
static int ip6gre_tunnel_init(struct net_device *dev);
static void ip6gre_tunnel_setup(struct net_device *dev);
static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
85
static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu);
X
xeb@mail.ru 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

/* Tunnel hash table */

/*
   4 hash tables:

   3: (remote,local)
   2: (remote,*)
   1: (*,local)
   0: (*,*)

   We require exact key match i.e. if a key is present in packet
   it will match only tunnel with the same key; if it is not present,
   it will match only keyless tunnel.

   All keysless packets, if not matched configured keyless tunnels
   will match fallback tunnel.
 */

105
#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
X
xeb@mail.ru 已提交
106 107 108 109
static u32 HASH_ADDR(const struct in6_addr *addr)
{
	u32 hash = ipv6_addr_hash(addr);

110
	return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
X
xeb@mail.ru 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

#define tunnels_r_l	tunnels[3]
#define tunnels_r	tunnels[2]
#define tunnels_l	tunnels[1]
#define tunnels_wc	tunnels[0]

/* Given src, dst and key, find appropriate for input tunnel. */

static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
		const struct in6_addr *remote, const struct in6_addr *local,
		__be32 key, __be16 gre_proto)
{
	struct net *net = dev_net(dev);
	int link = dev->ifindex;
	unsigned int h0 = HASH_ADDR(remote);
	unsigned int h1 = HASH_KEY(key);
	struct ip6_tnl *t, *cand = NULL;
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
130
	int dev_type = (gre_proto == htons(ETH_P_TEB) ||
131 132
			gre_proto == htons(ETH_P_ERSPAN) ||
			gre_proto == htons(ETH_P_ERSPAN2)) ?
X
xeb@mail.ru 已提交
133 134 135
		       ARPHRD_ETHER : ARPHRD_IP6GRE;
	int score, cand_score = 4;

A
Amerigo Wang 已提交
136
	for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
X
xeb@mail.ru 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
		    !ipv6_addr_equal(remote, &t->parms.raddr) ||
		    key != t->parms.i_key ||
		    !(t->dev->flags & IFF_UP))
			continue;

		if (t->dev->type != ARPHRD_IP6GRE &&
		    t->dev->type != dev_type)
			continue;

		score = 0;
		if (t->parms.link != link)
			score |= 1;
		if (t->dev->type != dev_type)
			score |= 2;
		if (score == 0)
			return t;

		if (score < cand_score) {
			cand = t;
			cand_score = score;
		}
	}

A
Amerigo Wang 已提交
161
	for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
X
xeb@mail.ru 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
		    key != t->parms.i_key ||
		    !(t->dev->flags & IFF_UP))
			continue;

		if (t->dev->type != ARPHRD_IP6GRE &&
		    t->dev->type != dev_type)
			continue;

		score = 0;
		if (t->parms.link != link)
			score |= 1;
		if (t->dev->type != dev_type)
			score |= 2;
		if (score == 0)
			return t;

		if (score < cand_score) {
			cand = t;
			cand_score = score;
		}
	}

A
Amerigo Wang 已提交
185
	for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
X
xeb@mail.ru 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
			  (!ipv6_addr_equal(local, &t->parms.raddr) ||
				 !ipv6_addr_is_multicast(local))) ||
		    key != t->parms.i_key ||
		    !(t->dev->flags & IFF_UP))
			continue;

		if (t->dev->type != ARPHRD_IP6GRE &&
		    t->dev->type != dev_type)
			continue;

		score = 0;
		if (t->parms.link != link)
			score |= 1;
		if (t->dev->type != dev_type)
			score |= 2;
		if (score == 0)
			return t;

		if (score < cand_score) {
			cand = t;
			cand_score = score;
		}
	}

A
Amerigo Wang 已提交
211
	for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
X
xeb@mail.ru 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		if (t->parms.i_key != key ||
		    !(t->dev->flags & IFF_UP))
			continue;

		if (t->dev->type != ARPHRD_IP6GRE &&
		    t->dev->type != dev_type)
			continue;

		score = 0;
		if (t->parms.link != link)
			score |= 1;
		if (t->dev->type != dev_type)
			score |= 2;
		if (score == 0)
			return t;

		if (score < cand_score) {
			cand = t;
			cand_score = score;
		}
	}

234
	if (cand)
X
xeb@mail.ru 已提交
235 236
		return cand;

237 238 239 240 241 242
	if (gre_proto == htons(ETH_P_ERSPAN) ||
	    gre_proto == htons(ETH_P_ERSPAN2))
		t = rcu_dereference(ign->collect_md_tun_erspan);
	else
		t = rcu_dereference(ign->collect_md_tun);

243 244 245
	if (t && t->dev->flags & IFF_UP)
		return t;

X
xeb@mail.ru 已提交
246
	dev = ign->fb_tunnel_dev;
247
	if (dev && dev->flags & IFF_UP)
X
xeb@mail.ru 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		return netdev_priv(dev);

	return NULL;
}

static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
		const struct __ip6_tnl_parm *p)
{
	const struct in6_addr *remote = &p->raddr;
	const struct in6_addr *local = &p->laddr;
	unsigned int h = HASH_KEY(p->i_key);
	int prio = 0;

	if (!ipv6_addr_any(local))
		prio |= 1;
	if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
		prio |= 2;
		h ^= HASH_ADDR(remote);
	}

	return &ign->tunnels[prio][h];
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static void ip6gre_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
{
	if (t->parms.collect_md)
		rcu_assign_pointer(ign->collect_md_tun, t);
}

static void ip6erspan_tunnel_link_md(struct ip6gre_net *ign, struct ip6_tnl *t)
{
	if (t->parms.collect_md)
		rcu_assign_pointer(ign->collect_md_tun_erspan, t);
}

static void ip6gre_tunnel_unlink_md(struct ip6gre_net *ign, struct ip6_tnl *t)
{
	if (t->parms.collect_md)
		rcu_assign_pointer(ign->collect_md_tun, NULL);
}

static void ip6erspan_tunnel_unlink_md(struct ip6gre_net *ign,
				       struct ip6_tnl *t)
{
	if (t->parms.collect_md)
		rcu_assign_pointer(ign->collect_md_tun_erspan, NULL);
}

X
xeb@mail.ru 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
		const struct ip6_tnl *t)
{
	return __ip6gre_bucket(ign, &t->parms);
}

static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
{
	struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);

	rcu_assign_pointer(t->next, rtnl_dereference(*tp));
	rcu_assign_pointer(*tp, t);
}

static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
{
	struct ip6_tnl __rcu **tp;
	struct ip6_tnl *iter;

	for (tp = ip6gre_bucket(ign, t);
	     (iter = rtnl_dereference(*tp)) != NULL;
	     tp = &iter->next) {
		if (t == iter) {
			rcu_assign_pointer(*tp, t->next);
			break;
		}
	}
}

static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
					   const struct __ip6_tnl_parm *parms,
					   int type)
{
	const struct in6_addr *remote = &parms->raddr;
	const struct in6_addr *local = &parms->laddr;
	__be32 key = parms->i_key;
	int link = parms->link;
	struct ip6_tnl *t;
	struct ip6_tnl __rcu **tp;
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);

	for (tp = __ip6gre_bucket(ign, parms);
	     (t = rtnl_dereference(*tp)) != NULL;
	     tp = &t->next)
		if (ipv6_addr_equal(local, &t->parms.laddr) &&
		    ipv6_addr_equal(remote, &t->parms.raddr) &&
		    key == t->parms.i_key &&
		    link == t->parms.link &&
		    type == t->dev->type)
			break;

	return t;
}

static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
		const struct __ip6_tnl_parm *parms, int create)
{
	struct ip6_tnl *t, *nt;
	struct net_device *dev;
	char name[IFNAMSIZ];
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);

	t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
359 360
	if (t && create)
		return NULL;
X
xeb@mail.ru 已提交
361 362 363
	if (t || !create)
		return t;

364 365 366
	if (parms->name[0]) {
		if (!dev_valid_name(parms->name))
			return NULL;
X
xeb@mail.ru 已提交
367
		strlcpy(name, parms->name, IFNAMSIZ);
368
	} else {
X
xeb@mail.ru 已提交
369
		strcpy(name, "ip6gre%d");
370
	}
371 372
	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
			   ip6gre_tunnel_setup);
X
xeb@mail.ru 已提交
373 374 375 376 377 378 379 380 381 382
	if (!dev)
		return NULL;

	dev_net_set(dev, net);

	nt = netdev_priv(dev);
	nt->parms = *parms;
	dev->rtnl_link_ops = &ip6gre_link_ops;

	nt->dev = dev;
N
Nicolas Dichtel 已提交
383
	nt->net = dev_net(dev);
X
xeb@mail.ru 已提交
384 385 386 387

	if (register_netdevice(dev) < 0)
		goto failed_free;

388 389
	ip6gre_tnl_link_config(nt, 1);

X
xeb@mail.ru 已提交
390
	/* Can use a lockless transmit, unless we generate output sequences */
391
	if (!(nt->parms.o_flags & TUNNEL_SEQ))
X
xeb@mail.ru 已提交
392 393 394 395 396 397 398 399 400 401 402
		dev->features |= NETIF_F_LLTX;

	dev_hold(dev);
	ip6gre_tunnel_link(ign, nt);
	return nt;

failed_free:
	free_netdev(dev);
	return NULL;
}

403 404 405 406 407 408 409 410 411 412 413
static void ip6erspan_tunnel_uninit(struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);

	ip6erspan_tunnel_unlink_md(ign, t);
	ip6gre_tunnel_unlink(ign, t);
	dst_cache_reset(&t->dst_cache);
	dev_put(dev);
}

X
xeb@mail.ru 已提交
414 415
static void ip6gre_tunnel_uninit(struct net_device *dev)
{
N
Nicolas Dichtel 已提交
416 417
	struct ip6_tnl *t = netdev_priv(dev);
	struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
X
xeb@mail.ru 已提交
418

419
	ip6gre_tunnel_unlink_md(ign, t);
N
Nicolas Dichtel 已提交
420
	ip6gre_tunnel_unlink(ign, t);
421
	dst_cache_reset(&t->dst_cache);
X
xeb@mail.ru 已提交
422 423 424 425 426
	dev_put(dev);
}


static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
427
		       u8 type, u8 code, int offset, __be32 info)
X
xeb@mail.ru 已提交
428
{
429
	struct net *net = dev_net(skb->dev);
430 431 432
	const struct gre_base_hdr *greh;
	const struct ipv6hdr *ipv6h;
	int grehlen = sizeof(*greh);
X
xeb@mail.ru 已提交
433
	struct ip6_tnl *t;
434
	int key_off = 0;
X
xeb@mail.ru 已提交
435
	__be16 flags;
436
	__be32 key;
X
xeb@mail.ru 已提交
437

438 439 440 441 442 443 444 445 446 447 448
	if (!pskb_may_pull(skb, offset + grehlen))
		return;
	greh = (const struct gre_base_hdr *)(skb->data + offset);
	flags = greh->flags;
	if (flags & (GRE_VERSION | GRE_ROUTING))
		return;
	if (flags & GRE_CSUM)
		grehlen += 4;
	if (flags & GRE_KEY) {
		key_off = grehlen + offset;
		grehlen += 4;
X
xeb@mail.ru 已提交
449 450
	}

451
	if (!pskb_may_pull(skb, offset + grehlen))
X
xeb@mail.ru 已提交
452
		return;
E
Eric Dumazet 已提交
453
	ipv6h = (const struct ipv6hdr *)skb->data;
454 455
	greh = (const struct gre_base_hdr *)(skb->data + offset);
	key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
X
xeb@mail.ru 已提交
456 457

	t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
458
				 key, greh->protocol);
459
	if (!t)
460
		return;
X
xeb@mail.ru 已提交
461 462 463

	switch (type) {
		struct ipv6_tlv_tnl_enc_lim *tel;
X
Xin Long 已提交
464
		__u32 teli;
X
xeb@mail.ru 已提交
465
	case ICMPV6_DEST_UNREACH:
466 467
		net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
				    t->parms.name);
468 469 470
		if (code != ICMPV6_PORT_UNREACH)
			break;
		return;
X
xeb@mail.ru 已提交
471 472
	case ICMPV6_TIME_EXCEED:
		if (code == ICMPV6_EXC_HOPLIMIT) {
473 474
			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
					    t->parms.name);
475
			break;
X
xeb@mail.ru 已提交
476
		}
477
		return;
X
xeb@mail.ru 已提交
478 479 480 481 482
	case ICMPV6_PARAMPROB:
		teli = 0;
		if (code == ICMPV6_HDR_FIELD)
			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);

483
		if (teli && teli == be32_to_cpu(info) - 2) {
X
xeb@mail.ru 已提交
484 485
			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
			if (tel->encap_limit == 0) {
486 487
				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
						    t->parms.name);
X
xeb@mail.ru 已提交
488 489
			}
		} else {
490 491
			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
					    t->parms.name);
X
xeb@mail.ru 已提交
492
		}
493
		return;
X
xeb@mail.ru 已提交
494
	case ICMPV6_PKT_TOOBIG:
X
Xin Long 已提交
495
		ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
496
		return;
497 498 499 500
	case NDISC_REDIRECT:
		ip6_redirect(skb, net, skb->dev->ifindex, 0,
			     sock_net_uid(net, NULL));
		return;
X
xeb@mail.ru 已提交
501 502 503 504 505 506 507 508 509
	}

	if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
		t->err_count++;
	else
		t->err_count = 1;
	t->err_time = jiffies;
}

510
static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
X
xeb@mail.ru 已提交
511 512 513 514 515 516
{
	const struct ipv6hdr *ipv6h;
	struct ip6_tnl *tunnel;

	ipv6h = ipv6_hdr(skb);
	tunnel = ip6gre_tunnel_lookup(skb->dev,
517 518
				      &ipv6h->saddr, &ipv6h->daddr, tpi->key,
				      tpi->proto);
X
xeb@mail.ru 已提交
519
	if (tunnel) {
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		if (tunnel->parms.collect_md) {
			struct metadata_dst *tun_dst;
			__be64 tun_id;
			__be16 flags;

			flags = tpi->flags;
			tun_id = key32_to_tunnel_id(tpi->key);

			tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0);
			if (!tun_dst)
				return PACKET_REJECT;

			ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
		} else {
			ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
		}
X
xeb@mail.ru 已提交
536

537 538
		return PACKET_RCVD;
	}
539

540 541
	return PACKET_REJECT;
}
542

543 544 545
static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len,
			 struct tnl_ptk_info *tpi)
{
546 547
	struct erspan_base_hdr *ershdr;
	struct erspan_metadata *pkt_md;
548
	const struct ipv6hdr *ipv6h;
549
	struct erspan_md2 *md2;
550
	struct ip6_tnl *tunnel;
551
	u8 ver;
552 553 554 555

	if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr))))
		return PACKET_REJECT;

556 557
	ipv6h = ipv6_hdr(skb);
	ershdr = (struct erspan_base_hdr *)skb->data;
558 559
	ver = ershdr->ver;
	tpi->key = cpu_to_be32(get_session_id(ershdr));
560 561 562 563 564

	tunnel = ip6gre_tunnel_lookup(skb->dev,
				      &ipv6h->saddr, &ipv6h->daddr, tpi->key,
				      tpi->proto);
	if (tunnel) {
565 566 567
		int len = erspan_hdr_len(ver);

		if (unlikely(!pskb_may_pull(skb, len)))
W
William Tu 已提交
568
			return PACKET_REJECT;
569

570 571 572
		ershdr = (struct erspan_base_hdr *)skb->data;
		pkt_md = (struct erspan_metadata *)(ershdr + 1);

573
		if (__iptunnel_pull_header(skb, len,
574 575 576 577
					   htons(ETH_P_TEB),
					   false, false) < 0)
			return PACKET_REJECT;

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		if (tunnel->parms.collect_md) {
			struct metadata_dst *tun_dst;
			struct ip_tunnel_info *info;
			struct erspan_metadata *md;
			__be64 tun_id;
			__be16 flags;

			tpi->flags |= TUNNEL_KEY;
			flags = tpi->flags;
			tun_id = key32_to_tunnel_id(tpi->key);

			tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id,
						  sizeof(*md));
			if (!tun_dst)
				return PACKET_REJECT;

			info = &tun_dst->u.tun_info;
			md = ip_tunnel_info_opts(info);
W
William Tu 已提交
596
			md->version = ver;
597 598 599
			md2 = &md->u.md2;
			memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
						       ERSPAN_V2_MDSIZE);
600 601 602 603 604 605 606 607
			info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
			info->options_len = sizeof(*md);

			ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);

		} else {
			ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
		}
608 609 610 611 612 613 614

		return PACKET_RCVD;
	}

	return PACKET_REJECT;
}

615 616 617 618 619
static int gre_rcv(struct sk_buff *skb)
{
	struct tnl_ptk_info tpi;
	bool csum_err = false;
	int hdr_len;
620

E
Eric Dumazet 已提交
621
	hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
622
	if (hdr_len < 0)
623
		goto drop;
X
xeb@mail.ru 已提交
624

625 626
	if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
		goto drop;
X
xeb@mail.ru 已提交
627

W
William Tu 已提交
628 629
	if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
		     tpi.proto == htons(ETH_P_ERSPAN2))) {
630 631
		if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD)
			return 0;
632
		goto out;
633 634
	}

635
	if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
X
xeb@mail.ru 已提交
636 637
		return 0;

638
out:
639
	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
X
xeb@mail.ru 已提交
640 641 642 643 644
drop:
	kfree_skb(skb);
	return 0;
}

645
static int gre_handle_offloads(struct sk_buff *skb, bool csum)
X
xeb@mail.ru 已提交
646
{
647 648
	return iptunnel_handle_offloads(skb,
					csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
X
xeb@mail.ru 已提交
649 650
}

W
William Tu 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb,
				     struct net_device *dev,
				     struct flowi6 *fl6, __u8 *dsfield,
				     int *encap_limit)
{
	const struct iphdr *iph = ip_hdr(skb);
	struct ip6_tnl *t = netdev_priv(dev);

	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		*encap_limit = t->parms.encap_limit;

	memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));

	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
		*dsfield = ipv4_get_dsfield(iph);
	else
		*dsfield = ip6_tclass(t->parms.flowinfo);

	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
		fl6->flowi6_mark = skb->mark;
	else
		fl6->flowi6_mark = t->parms.fwmark;

	fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
}

static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb,
				    struct net_device *dev,
				    struct flowi6 *fl6, __u8 *dsfield,
				    int *encap_limit)
{
	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
	struct ip6_tnl *t = netdev_priv(dev);
	__u16 offset;

	offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
	/* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */

	if (offset > 0) {
		struct ipv6_tlv_tnl_enc_lim *tel;

		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
		if (tel->encap_limit == 0) {
			icmpv6_send(skb, ICMPV6_PARAMPROB,
				    ICMPV6_HDR_FIELD, offset + 2);
			return -1;
		}
		*encap_limit = tel->encap_limit - 1;
	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
		*encap_limit = t->parms.encap_limit;
	}

	memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6));

	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
		*dsfield = ipv6_get_dsfield(ipv6h);
	else
		*dsfield = ip6_tclass(t->parms.flowinfo);

	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
		fl6->flowlabel |= ip6_flowlabel(ipv6h);

	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
		fl6->flowi6_mark = skb->mark;
	else
		fl6->flowi6_mark = t->parms.fwmark;

	fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);

	return 0;
}

723 724 725 726
static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
			       struct net_device *dev, __u8 dsfield,
			       struct flowi6 *fl6, int encap_limit,
			       __u32 *pmtu, __be16 proto)
X
xeb@mail.ru 已提交
727 728
{
	struct ip6_tnl *tunnel = netdev_priv(dev);
729
	__be16 protocol;
X
xeb@mail.ru 已提交
730 731 732 733

	if (dev->type == ARPHRD_ETHER)
		IPCB(skb)->flags = 0;

734 735 736
	if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
		fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
	else
X
xeb@mail.ru 已提交
737 738
		fl6->daddr = tunnel->parms.raddr;

739 740 741
	if (skb_cow_head(skb, dev->needed_headroom ?: tunnel->hlen))
		return -ENOMEM;

742
	/* Push GRE header. */
743
	protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

	if (tunnel->parms.collect_md) {
		struct ip_tunnel_info *tun_info;
		const struct ip_tunnel_key *key;
		__be16 flags;

		tun_info = skb_tunnel_info(skb);
		if (unlikely(!tun_info ||
			     !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
			     ip_tunnel_info_af(tun_info) != AF_INET6))
			return -EINVAL;

		key = &tun_info->key;
		memset(fl6, 0, sizeof(*fl6));
		fl6->flowi6_proto = IPPROTO_GRE;
		fl6->daddr = key->u.ipv6.dst;
		fl6->flowlabel = key->label;
		fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);

		dsfield = key->tos;
764 765
		flags = key->tun_flags &
			(TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
766 767 768 769
		tunnel->tun_hlen = gre_calc_hlen(flags);

		gre_build_header(skb, tunnel->tun_hlen,
				 flags, protocol,
770
				 tunnel_id_to_key32(tun_info->key.tun_id),
771
				 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++)
772
						      : 0);
773 774

	} else {
775 776 777
		if (tunnel->parms.o_flags & TUNNEL_SEQ)
			tunnel->o_seqno++;

778 779 780 781
		gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
				 protocol, tunnel->parms.o_key,
				 htonl(tunnel->o_seqno));
	}
X
xeb@mail.ru 已提交
782

783 784
	return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
			    NEXTHDR_GRE);
X
xeb@mail.ru 已提交
785 786 787 788 789 790 791
}

static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	int encap_limit = -1;
	struct flowi6 fl6;
792
	__u8 dsfield = 0;
X
xeb@mail.ru 已提交
793 794 795
	__u32 mtu;
	int err;

796 797
	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));

798 799 800
	if (!t->parms.collect_md)
		prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
					 &dsfield, &encap_limit);
801

802 803 804 805 806 807
	err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
	if (err)
		return -1;

	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
			  skb->protocol);
X
xeb@mail.ru 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	if (err != 0) {
		/* XXX: send ICMP error even if DF is not set. */
		if (err == -EMSGSIZE)
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
				  htonl(mtu));
		return -1;
	}

	return 0;
}

static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
	int encap_limit = -1;
	struct flowi6 fl6;
825
	__u8 dsfield = 0;
X
xeb@mail.ru 已提交
826 827 828 829 830 831
	__u32 mtu;
	int err;

	if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
		return -1;

832 833
	if (!t->parms.collect_md &&
	    prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit))
W
William Tu 已提交
834
		return -1;
835

836 837 838 839 840
	if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
		return -1;

	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
			  &mtu, skb->protocol);
X
xeb@mail.ru 已提交
841 842 843 844 845 846 847 848 849 850
	if (err != 0) {
		if (err == -EMSGSIZE)
			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
		return -1;
	}

	return 0;
}

/**
851
 * ip6gre_tnl_addr_conflict - compare packet addresses to tunnel's own
X
xeb@mail.ru 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
 *   @t: the outgoing tunnel device
 *   @hdr: IPv6 header from the incoming packet
 *
 * Description:
 *   Avoid trivial tunneling loop by checking that tunnel exit-point
 *   doesn't match source of incoming packet.
 *
 * Return:
 *   1 if conflict,
 *   0 else
 **/

static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
	const struct ipv6hdr *hdr)
{
	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
}

static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	int encap_limit = -1;
	struct flowi6 fl6;
	__u32 mtu;
	int err;

	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		encap_limit = t->parms.encap_limit;

881 882
	if (!t->parms.collect_md)
		memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
X
xeb@mail.ru 已提交
883

884 885 886 887 888
	err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
	if (err)
		return err;

	err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
X
xeb@mail.ru 已提交
889 890 891 892 893 894 895 896 897 898 899

	return err;
}

static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
	struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	struct net_device_stats *stats = &t->dev->stats;
	int ret;

900 901 902
	if (!pskb_inet_may_pull(skb))
		goto tx_err;

903
	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
904
		goto tx_err;
X
xeb@mail.ru 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

	switch (skb->protocol) {
	case htons(ETH_P_IP):
		ret = ip6gre_xmit_ipv4(skb, dev);
		break;
	case htons(ETH_P_IPV6):
		ret = ip6gre_xmit_ipv6(skb, dev);
		break;
	default:
		ret = ip6gre_xmit_other(skb, dev);
		break;
	}

	if (ret < 0)
		goto tx_err;

	return NETDEV_TX_OK;

tx_err:
	stats->tx_errors++;
	stats->tx_dropped++;
	kfree_skb(skb);
	return NETDEV_TX_OK;
}

930 931 932 933 934 935 936 937 938 939 940 941
static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb,
					 struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	struct dst_entry *dst = skb_dst(skb);
	struct net_device_stats *stats;
	bool truncate = false;
	int encap_limit = -1;
	__u8 dsfield = false;
	struct flowi6 fl6;
	int err = -EINVAL;
	__u32 mtu;
942
	int nhoff;
943
	int thoff;
944

945 946 947
	if (!pskb_inet_may_pull(skb))
		goto tx_err;

948 949 950 951 952 953 954 955 956 957 958
	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
		goto tx_err;

	if (gre_handle_offloads(skb, false))
		goto tx_err;

	if (skb->len > dev->mtu + dev->hard_header_len) {
		pskb_trim(skb, dev->mtu + dev->hard_header_len);
		truncate = true;
	}

959 960 961 962 963
	nhoff = skb_network_header(skb) - skb_mac_header(skb);
	if (skb->protocol == htons(ETH_P_IP) &&
	    (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
		truncate = true;

964 965 966 967 968
	thoff = skb_transport_header(skb) - skb_mac_header(skb);
	if (skb->protocol == htons(ETH_P_IPV6) &&
	    (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
		truncate = true;

969
	if (skb_cow_head(skb, dev->needed_headroom ?: t->hlen))
970 971
		goto tx_err;

972 973
	t->parms.o_flags &= ~TUNNEL_KEY;
	IPCB(skb)->flags = 0;
974 975 976 977 978 979 980 981

	/* For collect_md mode, derive fl6 from the tunnel key,
	 * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}.
	 */
	if (t->parms.collect_md) {
		struct ip_tunnel_info *tun_info;
		const struct ip_tunnel_key *key;
		struct erspan_metadata *md;
982
		__be32 tun_id;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997

		tun_info = skb_tunnel_info(skb);
		if (unlikely(!tun_info ||
			     !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
			     ip_tunnel_info_af(tun_info) != AF_INET6))
			return -EINVAL;

		key = &tun_info->key;
		memset(&fl6, 0, sizeof(fl6));
		fl6.flowi6_proto = IPPROTO_GRE;
		fl6.daddr = key->u.ipv6.dst;
		fl6.flowlabel = key->label;
		fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);

		dsfield = key->tos;
998 999
		if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
			goto tx_err;
1000 1001 1002 1003
		md = ip_tunnel_info_opts(tun_info);
		if (!md)
			goto tx_err;

1004
		tun_id = tunnel_id_to_key32(key->tun_id);
W
William Tu 已提交
1005 1006
		if (md->version == 1) {
			erspan_build_header(skb,
1007
					    ntohl(tun_id),
W
William Tu 已提交
1008 1009 1010 1011
					    ntohl(md->u.index), truncate,
					    false);
		} else if (md->version == 2) {
			erspan_build_header_v2(skb,
1012 1013 1014 1015
					       ntohl(tun_id),
					       md->u.md2.dir,
					       get_hwid(&md->u.md2),
					       truncate, false);
1016 1017
		} else {
			goto tx_err;
W
William Tu 已提交
1018
		}
1019 1020 1021 1022 1023 1024 1025 1026
	} else {
		switch (skb->protocol) {
		case htons(ETH_P_IP):
			memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
			prepare_ip6gre_xmit_ipv4(skb, dev, &fl6,
						 &dsfield, &encap_limit);
			break;
		case htons(ETH_P_IPV6):
1027
			if (ipv6_addr_equal(&t->parms.raddr, &ipv6_hdr(skb)->saddr))
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
				goto tx_err;
			if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6,
						     &dsfield, &encap_limit))
				goto tx_err;
			break;
		default:
			memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
			break;
		}

W
William Tu 已提交
1038
		if (t->parms.erspan_ver == 1)
1039
			erspan_build_header(skb, ntohl(t->parms.o_key),
W
William Tu 已提交
1040 1041
					    t->parms.index,
					    truncate, false);
W
William Tu 已提交
1042
		else if (t->parms.erspan_ver == 2)
1043
			erspan_build_header_v2(skb, ntohl(t->parms.o_key),
W
William Tu 已提交
1044 1045 1046
					       t->parms.dir,
					       t->parms.hwid,
					       truncate, false);
W
William Tu 已提交
1047 1048 1049
		else
			goto tx_err;

1050 1051
		fl6.daddr = t->parms.raddr;
	}
1052 1053 1054 1055 1056 1057

	/* Push GRE header. */
	gre_build_header(skb, 8, TUNNEL_SEQ,
			 htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++));

	/* TooBig packet may have updated dst->dev's mtu */
1058
	if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu)
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);

	err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
			   NEXTHDR_GRE);
	if (err != 0) {
		/* XXX: send ICMP error even if DF is not set. */
		if (err == -EMSGSIZE) {
			if (skb->protocol == htons(ETH_P_IP))
				icmp_send(skb, ICMP_DEST_UNREACH,
					  ICMP_FRAG_NEEDED, htonl(mtu));
			else
				icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
		}

		goto tx_err;
	}
	return NETDEV_TX_OK;

tx_err:
	stats = &t->dev->stats;
	stats->tx_errors++;
	stats->tx_dropped++;
	kfree_skb(skb);
	return NETDEV_TX_OK;
}

1085
static void ip6gre_tnl_link_config_common(struct ip6_tnl *t)
X
xeb@mail.ru 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
{
	struct net_device *dev = t->dev;
	struct __ip6_tnl_parm *p = &t->parms;
	struct flowi6 *fl6 = &t->fl.u.ip6;

	if (dev->type != ARPHRD_ETHER) {
		memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
		memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
	}

	/* Set up flowi template */
	fl6->saddr = p->laddr;
	fl6->daddr = p->raddr;
	fl6->flowi6_oif = p->link;
	fl6->flowlabel = 0;
1101
	fl6->flowi6_proto = IPPROTO_GRE;
X
xeb@mail.ru 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;

	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);

	if (p->flags&IP6_TNL_F_CAP_XMIT &&
			p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
		dev->flags |= IFF_POINTOPOINT;
	else
		dev->flags &= ~IFF_POINTOPOINT;
1116
}
X
xeb@mail.ru 已提交
1117

1118 1119 1120 1121 1122
static void ip6gre_tnl_link_config_route(struct ip6_tnl *t, int set_mtu,
					 int t_hlen)
{
	const struct __ip6_tnl_parm *p = &t->parms;
	struct net_device *dev = t->dev;
X
xeb@mail.ru 已提交
1123 1124 1125 1126 1127

	if (p->flags & IP6_TNL_F_CAP_XMIT) {
		int strict = (ipv6_addr_type(&p->raddr) &
			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));

N
Nicolas Dichtel 已提交
1128
		struct rt6_info *rt = rt6_lookup(t->net,
X
xeb@mail.ru 已提交
1129
						 &p->raddr, &p->laddr,
D
David Ahern 已提交
1130
						 p->link, NULL, strict);
X
xeb@mail.ru 已提交
1131

1132
		if (!rt)
X
xeb@mail.ru 已提交
1133 1134 1135
			return;

		if (rt->dst.dev) {
1136
			dev->needed_headroom = rt->dst.dev->hard_header_len +
T
Tom Herbert 已提交
1137
					       t_hlen;
X
xeb@mail.ru 已提交
1138 1139

			if (set_mtu) {
T
Tom Herbert 已提交
1140
				dev->mtu = rt->dst.dev->mtu - t_hlen;
X
xeb@mail.ru 已提交
1141 1142
				if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
					dev->mtu -= 8;
1143 1144
				if (dev->type == ARPHRD_ETHER)
					dev->mtu -= ETH_HLEN;
X
xeb@mail.ru 已提交
1145 1146 1147 1148 1149

				if (dev->mtu < IPV6_MIN_MTU)
					dev->mtu = IPV6_MIN_MTU;
			}
		}
A
Amerigo Wang 已提交
1150
		ip6_rt_put(rt);
X
xeb@mail.ru 已提交
1151 1152 1153
	}
}

1154 1155 1156 1157 1158 1159 1160 1161
static int ip6gre_calc_hlen(struct ip6_tnl *tunnel)
{
	int t_hlen;

	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;

	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1162
	tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1163 1164 1165 1166 1167 1168 1169 1170 1171
	return t_hlen;
}

static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
{
	ip6gre_tnl_link_config_common(t);
	ip6gre_tnl_link_config_route(t, set_mtu, ip6gre_calc_hlen(t));
}

1172 1173
static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl *t,
				     const struct __ip6_tnl_parm *p)
X
xeb@mail.ru 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
{
	t->parms.laddr = p->laddr;
	t->parms.raddr = p->raddr;
	t->parms.flags = p->flags;
	t->parms.hop_limit = p->hop_limit;
	t->parms.encap_limit = p->encap_limit;
	t->parms.flowinfo = p->flowinfo;
	t->parms.link = p->link;
	t->parms.proto = p->proto;
	t->parms.i_key = p->i_key;
	t->parms.o_key = p->o_key;
	t->parms.i_flags = p->i_flags;
	t->parms.o_flags = p->o_flags;
1187
	t->parms.fwmark = p->fwmark;
1188
	dst_cache_reset(&t->dst_cache);
1189 1190 1191 1192 1193 1194
}

static int ip6gre_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
			     int set_mtu)
{
	ip6gre_tnl_copy_tnl_parm(t, p);
X
xeb@mail.ru 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	ip6gre_tnl_link_config(t, set_mtu);
	return 0;
}

static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
	const struct ip6_tnl_parm2 *u)
{
	p->laddr = u->laddr;
	p->raddr = u->raddr;
	p->flags = u->flags;
	p->hop_limit = u->hop_limit;
	p->encap_limit = u->encap_limit;
	p->flowinfo = u->flowinfo;
	p->link = u->link;
	p->i_key = u->i_key;
	p->o_key = u->o_key;
T
Tom Herbert 已提交
1211 1212
	p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
	p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
X
xeb@mail.ru 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
	memcpy(p->name, u->name, sizeof(u->name));
}

static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
	const struct __ip6_tnl_parm *p)
{
	u->proto = IPPROTO_GRE;
	u->laddr = p->laddr;
	u->raddr = p->raddr;
	u->flags = p->flags;
	u->hop_limit = p->hop_limit;
	u->encap_limit = p->encap_limit;
	u->flowinfo = p->flowinfo;
	u->link = p->link;
	u->i_key = p->i_key;
	u->o_key = p->o_key;
T
Tom Herbert 已提交
1229 1230
	u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
	u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
X
xeb@mail.ru 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239
	memcpy(u->name, p->name, sizeof(u->name));
}

static int ip6gre_tunnel_ioctl(struct net_device *dev,
	struct ifreq *ifr, int cmd)
{
	int err = 0;
	struct ip6_tnl_parm2 p;
	struct __ip6_tnl_parm p1;
N
Nicolas Dichtel 已提交
1240 1241
	struct ip6_tnl *t = netdev_priv(dev);
	struct net *net = t->net;
X
xeb@mail.ru 已提交
1242 1243
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);

1244 1245
	memset(&p1, 0, sizeof(p1));

X
xeb@mail.ru 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254
	switch (cmd) {
	case SIOCGETTUNNEL:
		if (dev == ign->fb_tunnel_dev) {
			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
				err = -EFAULT;
				break;
			}
			ip6gre_tnl_parm_from_user(&p1, &p);
			t = ip6gre_tunnel_locate(net, &p1, 0);
1255
			if (!t)
N
Nicolas Dichtel 已提交
1256
				t = netdev_priv(dev);
X
xeb@mail.ru 已提交
1257
		}
1258
		memset(&p, 0, sizeof(p));
X
xeb@mail.ru 已提交
1259 1260 1261 1262 1263 1264 1265 1266
		ip6gre_tnl_parm_to_user(&p, &t->parms);
		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
			err = -EFAULT;
		break;

	case SIOCADDTUNNEL:
	case SIOCCHGTUNNEL:
		err = -EPERM;
1267
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
X
xeb@mail.ru 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
			goto done;

		err = -EFAULT;
		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
			goto done;

		err = -EINVAL;
		if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
			goto done;

		if (!(p.i_flags&GRE_KEY))
			p.i_key = 0;
		if (!(p.o_flags&GRE_KEY))
			p.o_key = 0;

		ip6gre_tnl_parm_from_user(&p1, &p);
		t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);

		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1287
			if (t) {
X
xeb@mail.ru 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
				if (t->dev != dev) {
					err = -EEXIST;
					break;
				}
			} else {
				t = netdev_priv(dev);

				ip6gre_tunnel_unlink(ign, t);
				synchronize_net();
				ip6gre_tnl_change(t, &p1, 1);
				ip6gre_tunnel_link(ign, t);
				netdev_state_change(dev);
			}
		}

		if (t) {
			err = 0;

1306
			memset(&p, 0, sizeof(p));
X
xeb@mail.ru 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315
			ip6gre_tnl_parm_to_user(&p, &t->parms);
			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
				err = -EFAULT;
		} else
			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
		break;

	case SIOCDELTUNNEL:
		err = -EPERM;
1316
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
X
xeb@mail.ru 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325
			goto done;

		if (dev == ign->fb_tunnel_dev) {
			err = -EFAULT;
			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
				goto done;
			err = -ENOENT;
			ip6gre_tnl_parm_from_user(&p1, &p);
			t = ip6gre_tunnel_locate(net, &p1, 0);
1326
			if (!t)
X
xeb@mail.ru 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
				goto done;
			err = -EPERM;
			if (t == netdev_priv(ign->fb_tunnel_dev))
				goto done;
			dev = t->dev;
		}
		unregister_netdevice(dev);
		err = 0;
		break;

	default:
		err = -EINVAL;
	}

done:
	return err;
}

static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1346 1347
			 unsigned short type, const void *daddr,
			 const void *saddr, unsigned int len)
X
xeb@mail.ru 已提交
1348 1349
{
	struct ip6_tnl *t = netdev_priv(dev);
1350 1351
	struct ipv6hdr *ipv6h;
	__be16 *p;
X
xeb@mail.ru 已提交
1352

1353 1354 1355 1356
	ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h));
	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
						  t->fl.u.ip6.flowlabel,
						  true, &t->fl.u.ip6));
X
xeb@mail.ru 已提交
1357 1358 1359 1360 1361
	ipv6h->hop_limit = t->parms.hop_limit;
	ipv6h->nexthdr = NEXTHDR_GRE;
	ipv6h->saddr = t->parms.laddr;
	ipv6h->daddr = t->parms.raddr;

1362 1363 1364
	p = (__be16 *)(ipv6h + 1);
	p[0] = t->parms.o_flags;
	p[1] = htons(type);
X
xeb@mail.ru 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

	/*
	 *	Set the source hardware address.
	 */

	if (saddr)
		memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
	if (daddr)
		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
	if (!ipv6_addr_any(&ipv6h->daddr))
		return t->hlen;

	return -t->hlen;
}

static const struct header_ops ip6gre_header_ops = {
	.create	= ip6gre_header,
};

static const struct net_device_ops ip6gre_netdev_ops = {
	.ndo_init		= ip6gre_tunnel_init,
	.ndo_uninit		= ip6gre_tunnel_uninit,
	.ndo_start_xmit		= ip6gre_tunnel_xmit,
	.ndo_do_ioctl		= ip6gre_tunnel_ioctl,
1389
	.ndo_change_mtu		= ip6_tnl_change_mtu,
1390
	.ndo_get_stats64	= ip_tunnel_get_stats64,
1391
	.ndo_get_iflink		= ip6_tnl_get_iflink,
X
xeb@mail.ru 已提交
1392 1393 1394 1395
};

static void ip6gre_dev_free(struct net_device *dev)
{
1396 1397
	struct ip6_tnl *t = netdev_priv(dev);

E
Eran Ben Elisha 已提交
1398
	gro_cells_destroy(&t->gro_cells);
1399
	dst_cache_destroy(&t->dst_cache);
X
xeb@mail.ru 已提交
1400 1401 1402 1403 1404 1405
	free_percpu(dev->tstats);
}

static void ip6gre_tunnel_setup(struct net_device *dev)
{
	dev->netdev_ops = &ip6gre_netdev_ops;
1406 1407
	dev->needs_free_netdev = true;
	dev->priv_destructor = ip6gre_dev_free;
X
xeb@mail.ru 已提交
1408 1409

	dev->type = ARPHRD_IP6GRE;
1410

X
xeb@mail.ru 已提交
1411 1412
	dev->flags |= IFF_NOARP;
	dev->addr_len = sizeof(struct in6_addr);
1413
	netif_keep_dst(dev);
1414 1415 1416
	/* This perm addr will be used as interface identifier by IPv6 */
	dev->addr_assign_type = NET_ADDR_RANDOM;
	eth_random_addr(dev->perm_addr);
X
xeb@mail.ru 已提交
1417 1418
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
#define GRE6_FEATURES (NETIF_F_SG |		\
		       NETIF_F_FRAGLIST |	\
		       NETIF_F_HIGHDMA |	\
		       NETIF_F_HW_CSUM)

static void ip6gre_tnl_init_features(struct net_device *dev)
{
	struct ip6_tnl *nt = netdev_priv(dev);

	dev->features		|= GRE6_FEATURES;
	dev->hw_features	|= GRE6_FEATURES;

	if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
		/* TCP offload with GRE SEQ is not supported, nor
		 * can we support 2 levels of outer headers requiring
		 * an update.
		 */
		if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
		    nt->encap.type == TUNNEL_ENCAP_NONE) {
			dev->features    |= NETIF_F_GSO_SOFTWARE;
			dev->hw_features |= NETIF_F_GSO_SOFTWARE;
		}

		/* Can use a lockless transmit, unless we generate
		 * output sequences
		 */
		dev->features |= NETIF_F_LLTX;
	}
}

1449
static int ip6gre_tunnel_init_common(struct net_device *dev)
X
xeb@mail.ru 已提交
1450 1451
{
	struct ip6_tnl *tunnel;
1452
	int ret;
1453
	int t_hlen;
X
xeb@mail.ru 已提交
1454 1455 1456 1457

	tunnel = netdev_priv(dev);

	tunnel->dev = dev;
N
Nicolas Dichtel 已提交
1458
	tunnel->net = dev_net(dev);
X
xeb@mail.ru 已提交
1459 1460
	strcpy(tunnel->parms.name, dev->name);

1461 1462 1463 1464
	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
	if (!dev->tstats)
		return -ENOMEM;

1465
	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
E
Eran Ben Elisha 已提交
1466 1467 1468 1469 1470 1471
	if (ret)
		goto cleanup_alloc_pcpu_stats;

	ret = gro_cells_init(&tunnel->gro_cells, dev);
	if (ret)
		goto cleanup_dst_cache_init;
1472

1473
	t_hlen = ip6gre_calc_hlen(tunnel);
T
Tom Herbert 已提交
1474
	dev->mtu = ETH_DATA_LEN - t_hlen;
1475 1476
	if (dev->type == ARPHRD_ETHER)
		dev->mtu -= ETH_HLEN;
1477 1478 1479
	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		dev->mtu -= 8;

1480 1481 1482 1483
	if (tunnel->parms.collect_md) {
		dev->features |= NETIF_F_NETNS_LOCAL;
		netif_keep_dst(dev);
	}
1484
	ip6gre_tnl_init_features(dev);
1485

1486
	return 0;
E
Eran Ben Elisha 已提交
1487 1488 1489 1490 1491 1492 1493

cleanup_dst_cache_init:
	dst_cache_destroy(&tunnel->dst_cache);
cleanup_alloc_pcpu_stats:
	free_percpu(dev->tstats);
	dev->tstats = NULL;
	return ret;
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
}

static int ip6gre_tunnel_init(struct net_device *dev)
{
	struct ip6_tnl *tunnel;
	int ret;

	ret = ip6gre_tunnel_init_common(dev);
	if (ret)
		return ret;

	tunnel = netdev_priv(dev);

1507 1508 1509
	if (tunnel->parms.collect_md)
		return 0;

X
xeb@mail.ru 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
	memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));

	if (ipv6_addr_any(&tunnel->parms.raddr))
		dev->header_ops = &ip6gre_header_ops;

	return 0;
}

static void ip6gre_fb_tunnel_init(struct net_device *dev)
{
	struct ip6_tnl *tunnel = netdev_priv(dev);

	tunnel->dev = dev;
N
Nicolas Dichtel 已提交
1524
	tunnel->net = dev_net(dev);
X
xeb@mail.ru 已提交
1525 1526 1527 1528 1529 1530 1531 1532
	strcpy(tunnel->parms.name, dev->name);

	tunnel->hlen		= sizeof(struct ipv6hdr) + 4;

	dev_hold(dev);
}

static struct inet6_protocol ip6gre_protocol __read_mostly = {
1533
	.handler     = gre_rcv,
X
xeb@mail.ru 已提交
1534 1535 1536 1537
	.err_handler = ip6gre_err,
	.flags       = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
};

N
Nicolas Dichtel 已提交
1538
static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
X
xeb@mail.ru 已提交
1539
{
N
Nicolas Dichtel 已提交
1540 1541
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
	struct net_device *dev, *aux;
X
xeb@mail.ru 已提交
1542 1543
	int prio;

N
Nicolas Dichtel 已提交
1544 1545
	for_each_netdev_safe(net, dev, aux)
		if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1546 1547
		    dev->rtnl_link_ops == &ip6gre_tap_ops ||
		    dev->rtnl_link_ops == &ip6erspan_tap_ops)
N
Nicolas Dichtel 已提交
1548 1549
			unregister_netdevice_queue(dev, head);

X
xeb@mail.ru 已提交
1550 1551
	for (prio = 0; prio < 4; prio++) {
		int h;
1552
		for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
X
xeb@mail.ru 已提交
1553 1554 1555 1556
			struct ip6_tnl *t;

			t = rtnl_dereference(ign->tunnels[prio][h]);

1557
			while (t) {
N
Nicolas Dichtel 已提交
1558 1559 1560 1561 1562 1563
				/* If dev is in the same netns, it has already
				 * been added to the list by the previous loop.
				 */
				if (!net_eq(dev_net(t->dev), net))
					unregister_netdevice_queue(t->dev,
								   head);
X
xeb@mail.ru 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
				t = rtnl_dereference(t->next);
			}
		}
	}
}

static int __net_init ip6gre_init_net(struct net *net)
{
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
	int err;

1575 1576
	if (!net_has_fallback_tunnels(net))
		return 0;
X
xeb@mail.ru 已提交
1577
	ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1578 1579
					  NET_NAME_UNKNOWN,
					  ip6gre_tunnel_setup);
X
xeb@mail.ru 已提交
1580 1581 1582 1583 1584
	if (!ign->fb_tunnel_dev) {
		err = -ENOMEM;
		goto err_alloc_dev;
	}
	dev_net_set(ign->fb_tunnel_dev, net);
N
Nicolas Dichtel 已提交
1585 1586 1587 1588 1589
	/* FB netdevice is special: we have one, and only one per netns.
	 * Allowing to move it to another netns is clearly unsafe.
	 */
	ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;

X
xeb@mail.ru 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

	ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
	ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;

	err = register_netdev(ign->fb_tunnel_dev);
	if (err)
		goto err_reg_dev;

	rcu_assign_pointer(ign->tunnels_wc[0],
			   netdev_priv(ign->fb_tunnel_dev));
	return 0;

err_reg_dev:
1603
	free_netdev(ign->fb_tunnel_dev);
X
xeb@mail.ru 已提交
1604 1605 1606 1607
err_alloc_dev:
	return err;
}

1608
static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list)
X
xeb@mail.ru 已提交
1609
{
1610
	struct net *net;
X
xeb@mail.ru 已提交
1611 1612 1613
	LIST_HEAD(list);

	rtnl_lock();
1614 1615
	list_for_each_entry(net, net_list, exit_list)
		ip6gre_destroy_tunnels(net, &list);
X
xeb@mail.ru 已提交
1616 1617 1618 1619 1620 1621
	unregister_netdevice_many(&list);
	rtnl_unlock();
}

static struct pernet_operations ip6gre_net_ops = {
	.init = ip6gre_init_net,
1622
	.exit_batch = ip6gre_exit_batch_net,
X
xeb@mail.ru 已提交
1623 1624 1625 1626
	.id   = &ip6gre_net_id,
	.size = sizeof(struct ip6gre_net),
};

1627 1628
static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
				  struct netlink_ext_ack *extack)
X
xeb@mail.ru 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
{
	__be16 flags;

	if (!data)
		return 0;

	flags = 0;
	if (data[IFLA_GRE_IFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
	if (data[IFLA_GRE_OFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
	if (flags & (GRE_VERSION|GRE_ROUTING))
		return -EINVAL;

	return 0;
}

1646 1647
static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
			       struct netlink_ext_ack *extack)
X
xeb@mail.ru 已提交
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
{
	struct in6_addr daddr;

	if (tb[IFLA_ADDRESS]) {
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
			return -EINVAL;
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
			return -EADDRNOTAVAIL;
	}

	if (!data)
		goto out;

	if (data[IFLA_GRE_REMOTE]) {
1662
		daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
X
xeb@mail.ru 已提交
1663 1664 1665 1666 1667
		if (ipv6_addr_any(&daddr))
			return -EINVAL;
	}

out:
1668
	return ip6gre_tunnel_validate(tb, data, extack);
X
xeb@mail.ru 已提交
1669 1670
}

1671 1672 1673 1674
static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[],
				  struct netlink_ext_ack *extack)
{
	__be16 flags = 0;
W
William Tu 已提交
1675
	int ret, ver = 0;
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

	if (!data)
		return 0;

	ret = ip6gre_tap_validate(tb, data, extack);
	if (ret)
		return ret;

	/* ERSPAN should only have GRE sequence and key flag */
	if (data[IFLA_GRE_OFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
	if (data[IFLA_GRE_IFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
	if (!data[IFLA_GRE_COLLECT_METADATA] &&
	    flags != (GRE_SEQ | GRE_KEY))
		return -EINVAL;

	/* ERSPAN Session ID only has 10-bit. Since we reuse
	 * 32-bit key field as ID, check it's range.
	 */
	if (data[IFLA_GRE_IKEY] &&
	    (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
		return -EINVAL;

	if (data[IFLA_GRE_OKEY] &&
	    (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
		return -EINVAL;

W
William Tu 已提交
1704 1705 1706
	if (data[IFLA_GRE_ERSPAN_VER]) {
		ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
		if (ver != 1 && ver != 2)
1707 1708
			return -EINVAL;
	}
W
William Tu 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732

	if (ver == 1) {
		if (data[IFLA_GRE_ERSPAN_INDEX]) {
			u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);

			if (index & ~INDEX_MASK)
				return -EINVAL;
		}
	} else if (ver == 2) {
		if (data[IFLA_GRE_ERSPAN_DIR]) {
			u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);

			if (dir & ~(DIR_MASK >> DIR_OFFSET))
				return -EINVAL;
		}

		if (data[IFLA_GRE_ERSPAN_HWID]) {
			u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);

			if (hwid & ~(HWID_MASK >> HWID_OFFSET))
				return -EINVAL;
		}
	}

1733 1734
	return 0;
}
X
xeb@mail.ru 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747

static void ip6gre_netlink_parms(struct nlattr *data[],
				struct __ip6_tnl_parm *parms)
{
	memset(parms, 0, sizeof(*parms));

	if (!data)
		return;

	if (data[IFLA_GRE_LINK])
		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);

	if (data[IFLA_GRE_IFLAGS])
T
Tom Herbert 已提交
1748 1749
		parms->i_flags = gre_flags_to_tnl_flags(
				nla_get_be16(data[IFLA_GRE_IFLAGS]));
X
xeb@mail.ru 已提交
1750 1751

	if (data[IFLA_GRE_OFLAGS])
T
Tom Herbert 已提交
1752 1753
		parms->o_flags = gre_flags_to_tnl_flags(
				nla_get_be16(data[IFLA_GRE_OFLAGS]));
X
xeb@mail.ru 已提交
1754 1755 1756 1757 1758 1759 1760 1761

	if (data[IFLA_GRE_IKEY])
		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);

	if (data[IFLA_GRE_OKEY])
		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);

	if (data[IFLA_GRE_LOCAL])
1762
		parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
X
xeb@mail.ru 已提交
1763 1764

	if (data[IFLA_GRE_REMOTE])
1765
		parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
X
xeb@mail.ru 已提交
1766 1767 1768 1769 1770 1771 1772 1773

	if (data[IFLA_GRE_TTL])
		parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);

	if (data[IFLA_GRE_ENCAP_LIMIT])
		parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);

	if (data[IFLA_GRE_FLOWINFO])
1774
		parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
X
xeb@mail.ru 已提交
1775 1776 1777

	if (data[IFLA_GRE_FLAGS])
		parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1778 1779 1780

	if (data[IFLA_GRE_FWMARK])
		parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1781

1782 1783
	if (data[IFLA_GRE_COLLECT_METADATA])
		parms->collect_md = true;
W
William Tu 已提交
1784

1785
	parms->erspan_ver = 1;
W
William Tu 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
	if (data[IFLA_GRE_ERSPAN_VER])
		parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);

	if (parms->erspan_ver == 1) {
		if (data[IFLA_GRE_ERSPAN_INDEX])
			parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
	} else if (parms->erspan_ver == 2) {
		if (data[IFLA_GRE_ERSPAN_DIR])
			parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
		if (data[IFLA_GRE_ERSPAN_HWID])
			parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
	}
X
xeb@mail.ru 已提交
1798 1799 1800 1801
}

static int ip6gre_tap_init(struct net_device *dev)
{
1802
	int ret;
X
xeb@mail.ru 已提交
1803

1804 1805 1806
	ret = ip6gre_tunnel_init_common(dev);
	if (ret)
		return ret;
X
xeb@mail.ru 已提交
1807

1808 1809
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;

X
xeb@mail.ru 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818
	return 0;
}

static const struct net_device_ops ip6gre_tap_netdev_ops = {
	.ndo_init = ip6gre_tap_init,
	.ndo_uninit = ip6gre_tunnel_uninit,
	.ndo_start_xmit = ip6gre_tunnel_xmit,
	.ndo_set_mac_address = eth_mac_addr,
	.ndo_validate_addr = eth_validate_addr,
1819
	.ndo_change_mtu = ip6_tnl_change_mtu,
1820
	.ndo_get_stats64 = ip_tunnel_get_stats64,
1821
	.ndo_get_iflink = ip6_tnl_get_iflink,
X
xeb@mail.ru 已提交
1822 1823
};

1824 1825 1826 1827 1828 1829 1830 1831 1832
static int ip6erspan_calc_hlen(struct ip6_tnl *tunnel)
{
	int t_hlen;

	tunnel->tun_hlen = 8;
	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
		       erspan_hdr_len(tunnel->parms.erspan_ver);

	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1833
	tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
1834 1835 1836
	return t_hlen;
}

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
static int ip6erspan_tap_init(struct net_device *dev)
{
	struct ip6_tnl *tunnel;
	int t_hlen;
	int ret;

	tunnel = netdev_priv(dev);

	tunnel->dev = dev;
	tunnel->net = dev_net(dev);
	strcpy(tunnel->parms.name, dev->name);

	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
	if (!dev->tstats)
		return -ENOMEM;

	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
E
Eran Ben Elisha 已提交
1854 1855 1856 1857 1858 1859
	if (ret)
		goto cleanup_alloc_pcpu_stats;

	ret = gro_cells_init(&tunnel->gro_cells, dev);
	if (ret)
		goto cleanup_dst_cache_init;
1860

1861
	t_hlen = ip6erspan_calc_hlen(tunnel);
1862 1863 1864 1865 1866 1867 1868
	dev->mtu = ETH_DATA_LEN - t_hlen;
	if (dev->type == ARPHRD_ETHER)
		dev->mtu -= ETH_HLEN;
	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
		dev->mtu -= 8;

	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1869
	ip6erspan_tnl_link_config(tunnel, 1);
1870 1871

	return 0;
E
Eran Ben Elisha 已提交
1872 1873 1874 1875 1876 1877 1878

cleanup_dst_cache_init:
	dst_cache_destroy(&tunnel->dst_cache);
cleanup_alloc_pcpu_stats:
	free_percpu(dev->tstats);
	dev->tstats = NULL;
	return ret;
1879 1880 1881 1882
}

static const struct net_device_ops ip6erspan_netdev_ops = {
	.ndo_init =		ip6erspan_tap_init,
1883
	.ndo_uninit =		ip6erspan_tunnel_uninit,
1884 1885 1886 1887 1888 1889 1890 1891
	.ndo_start_xmit =	ip6erspan_tunnel_xmit,
	.ndo_set_mac_address =	eth_mac_addr,
	.ndo_validate_addr =	eth_validate_addr,
	.ndo_change_mtu =	ip6_tnl_change_mtu,
	.ndo_get_stats64 =	ip_tunnel_get_stats64,
	.ndo_get_iflink =	ip6_tnl_get_iflink,
};

X
xeb@mail.ru 已提交
1892 1893 1894 1895 1896
static void ip6gre_tap_setup(struct net_device *dev)
{

	ether_setup(dev);

1897
	dev->max_mtu = 0;
X
xeb@mail.ru 已提交
1898
	dev->netdev_ops = &ip6gre_tap_netdev_ops;
1899 1900
	dev->needs_free_netdev = true;
	dev->priv_destructor = ip6gre_dev_free;
X
xeb@mail.ru 已提交
1901 1902

	dev->features |= NETIF_F_NETNS_LOCAL;
J
Jiri Benc 已提交
1903
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1904
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1905
	netif_keep_dst(dev);
X
xeb@mail.ru 已提交
1906 1907
}

1908 1909 1910 1911 1912 1913
bool is_ip6gretap_dev(const struct net_device *dev)
{
	return dev->netdev_ops == &ip6gre_tap_netdev_ops;
}
EXPORT_SYMBOL_GPL(is_ip6gretap_dev);

1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
				       struct ip_tunnel_encap *ipencap)
{
	bool ret = false;

	memset(ipencap, 0, sizeof(*ipencap));

	if (!data)
		return ret;

	if (data[IFLA_GRE_ENCAP_TYPE]) {
		ret = true;
		ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
	}

	if (data[IFLA_GRE_ENCAP_FLAGS]) {
		ret = true;
		ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
	}

	if (data[IFLA_GRE_ENCAP_SPORT]) {
		ret = true;
		ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
	}

	if (data[IFLA_GRE_ENCAP_DPORT]) {
		ret = true;
		ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
	}

	return ret;
}

1947 1948 1949
static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,
				 struct nlattr *tb[], struct nlattr *data[],
				 struct netlink_ext_ack *extack)
X
xeb@mail.ru 已提交
1950 1951
{
	struct ip6_tnl *nt;
1952
	struct ip_tunnel_encap ipencap;
X
xeb@mail.ru 已提交
1953 1954 1955
	int err;

	nt = netdev_priv(dev);
1956 1957 1958 1959 1960 1961 1962 1963

	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
		int err = ip6_tnl_encap_setup(nt, &ipencap);

		if (err < 0)
			return err;
	}

X
xeb@mail.ru 已提交
1964 1965 1966 1967
	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
		eth_hw_addr_random(dev);

	nt->dev = dev;
N
Nicolas Dichtel 已提交
1968
	nt->net = dev_net(dev);
X
xeb@mail.ru 已提交
1969 1970 1971 1972 1973

	err = register_netdevice(dev);
	if (err)
		goto out;

1974 1975 1976
	if (tb[IFLA_MTU])
		ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));

X
xeb@mail.ru 已提交
1977 1978 1979 1980 1981 1982
	dev_hold(dev);

out:
	return err;
}

1983 1984 1985 1986 1987 1988
static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
			  struct nlattr *tb[], struct nlattr *data[],
			  struct netlink_ext_ack *extack)
{
	struct ip6_tnl *nt = netdev_priv(dev);
	struct net *net = dev_net(dev);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	struct ip6gre_net *ign;
	int err;

	ip6gre_netlink_parms(data, &nt->parms);
	ign = net_generic(net, ip6gre_net_id);

	if (nt->parms.collect_md) {
		if (rtnl_dereference(ign->collect_md_tun))
			return -EEXIST;
	} else {
		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
			return -EEXIST;
	}
2002

2003
	err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
2004 2005
	if (!err) {
		ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
2006
		ip6gre_tunnel_link_md(ign, nt);
2007 2008 2009 2010 2011
		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
	}
	return err;
}

2012 2013 2014 2015
static struct ip6_tnl *
ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
			 struct nlattr *data[], struct __ip6_tnl_parm *p_p,
			 struct netlink_ext_ack *extack)
X
xeb@mail.ru 已提交
2016
{
N
Nicolas Dichtel 已提交
2017 2018
	struct ip6_tnl *t, *nt = netdev_priv(dev);
	struct net *net = nt->net;
X
xeb@mail.ru 已提交
2019
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
2020
	struct ip_tunnel_encap ipencap;
X
xeb@mail.ru 已提交
2021 2022

	if (dev == ign->fb_tunnel_dev)
2023
		return ERR_PTR(-EINVAL);
X
xeb@mail.ru 已提交
2024

2025 2026 2027 2028
	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
		int err = ip6_tnl_encap_setup(nt, &ipencap);

		if (err < 0)
2029
			return ERR_PTR(err);
2030 2031
	}

2032
	ip6gre_netlink_parms(data, p_p);
X
xeb@mail.ru 已提交
2033

2034
	t = ip6gre_tunnel_locate(net, p_p, 0);
X
xeb@mail.ru 已提交
2035 2036 2037

	if (t) {
		if (t->dev != dev)
2038
			return ERR_PTR(-EEXIST);
X
xeb@mail.ru 已提交
2039 2040 2041 2042
	} else {
		t = nt;
	}

2043 2044 2045 2046 2047 2048 2049
	return t;
}

static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
			     struct nlattr *data[],
			     struct netlink_ext_ack *extack)
{
2050 2051
	struct ip6_tnl *t = netdev_priv(dev);
	struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
2052 2053 2054 2055 2056 2057
	struct __ip6_tnl_parm p;

	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
	if (IS_ERR(t))
		return PTR_ERR(t);

2058
	ip6gre_tunnel_unlink_md(ign, t);
2059 2060
	ip6gre_tunnel_unlink(ign, t);
	ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
2061
	ip6gre_tunnel_link_md(ign, t);
2062
	ip6gre_tunnel_link(ign, t);
X
xeb@mail.ru 已提交
2063 2064 2065
	return 0;
}

2066 2067 2068 2069 2070 2071 2072 2073 2074
static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
{
	struct net *net = dev_net(dev);
	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);

	if (dev != ign->fb_tunnel_dev)
		unregister_netdevice_queue(dev, head);
}

X
xeb@mail.ru 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
static size_t ip6gre_get_size(const struct net_device *dev)
{
	return
		/* IFLA_GRE_LINK */
		nla_total_size(4) +
		/* IFLA_GRE_IFLAGS */
		nla_total_size(2) +
		/* IFLA_GRE_OFLAGS */
		nla_total_size(2) +
		/* IFLA_GRE_IKEY */
		nla_total_size(4) +
		/* IFLA_GRE_OKEY */
		nla_total_size(4) +
		/* IFLA_GRE_LOCAL */
N
Nicolas Dichtel 已提交
2089
		nla_total_size(sizeof(struct in6_addr)) +
X
xeb@mail.ru 已提交
2090
		/* IFLA_GRE_REMOTE */
N
Nicolas Dichtel 已提交
2091
		nla_total_size(sizeof(struct in6_addr)) +
X
xeb@mail.ru 已提交
2092 2093 2094 2095 2096 2097 2098 2099
		/* IFLA_GRE_TTL */
		nla_total_size(1) +
		/* IFLA_GRE_ENCAP_LIMIT */
		nla_total_size(1) +
		/* IFLA_GRE_FLOWINFO */
		nla_total_size(4) +
		/* IFLA_GRE_FLAGS */
		nla_total_size(4) +
2100 2101 2102 2103 2104 2105 2106 2107
		/* IFLA_GRE_ENCAP_TYPE */
		nla_total_size(2) +
		/* IFLA_GRE_ENCAP_FLAGS */
		nla_total_size(2) +
		/* IFLA_GRE_ENCAP_SPORT */
		nla_total_size(2) +
		/* IFLA_GRE_ENCAP_DPORT */
		nla_total_size(2) +
2108 2109
		/* IFLA_GRE_COLLECT_METADATA */
		nla_total_size(0) +
2110 2111
		/* IFLA_GRE_FWMARK */
		nla_total_size(4) +
2112 2113
		/* IFLA_GRE_ERSPAN_INDEX */
		nla_total_size(4) +
X
xeb@mail.ru 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122
		0;
}

static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
	struct ip6_tnl *t = netdev_priv(dev);
	struct __ip6_tnl_parm *p = &t->parms;

	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
T
Tom Herbert 已提交
2123 2124 2125 2126
	    nla_put_be16(skb, IFLA_GRE_IFLAGS,
			 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
	    nla_put_be16(skb, IFLA_GRE_OFLAGS,
			 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
X
xeb@mail.ru 已提交
2127 2128
	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
2129 2130
	    nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
	    nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
X
xeb@mail.ru 已提交
2131 2132 2133
	    nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
	    nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
	    nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
2134
	    nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) ||
2135 2136
	    nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) ||
	    nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
X
xeb@mail.ru 已提交
2137
		goto nla_put_failure;
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148

	if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
			t->encap.type) ||
	    nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
			 t->encap.sport) ||
	    nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
			 t->encap.dport) ||
	    nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
			t->encap.flags))
		goto nla_put_failure;

2149 2150 2151 2152 2153
	if (p->collect_md) {
		if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
			goto nla_put_failure;
	}

W
William Tu 已提交
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
	if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver))
		goto nla_put_failure;

	if (p->erspan_ver == 1) {
		if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index))
			goto nla_put_failure;
	} else if (p->erspan_ver == 2) {
		if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir))
			goto nla_put_failure;
		if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid))
			goto nla_put_failure;
	}

X
xeb@mail.ru 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
	[IFLA_GRE_LINK]        = { .type = NLA_U32 },
	[IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
	[IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
	[IFLA_GRE_IKEY]        = { .type = NLA_U32 },
	[IFLA_GRE_OKEY]        = { .type = NLA_U32 },
	[IFLA_GRE_LOCAL]       = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
	[IFLA_GRE_REMOTE]      = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
	[IFLA_GRE_TTL]         = { .type = NLA_U8 },
	[IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
	[IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
	[IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
2185 2186 2187 2188
	[IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
	[IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
	[IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
	[IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
2189
	[IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
2190
	[IFLA_GRE_FWMARK]       = { .type = NLA_U32 },
2191
	[IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
W
William Tu 已提交
2192 2193 2194
	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
	[IFLA_GRE_ERSPAN_HWID]	= { .type = NLA_U16 },
X
xeb@mail.ru 已提交
2195 2196
};

2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
static void ip6erspan_tap_setup(struct net_device *dev)
{
	ether_setup(dev);

	dev->netdev_ops = &ip6erspan_netdev_ops;
	dev->needs_free_netdev = true;
	dev->priv_destructor = ip6gre_dev_free;

	dev->features |= NETIF_F_NETNS_LOCAL;
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
	netif_keep_dst(dev);
}

2211 2212 2213 2214 2215 2216
static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,
			     struct nlattr *tb[], struct nlattr *data[],
			     struct netlink_ext_ack *extack)
{
	struct ip6_tnl *nt = netdev_priv(dev);
	struct net *net = dev_net(dev);
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
	struct ip6gre_net *ign;
	int err;

	ip6gre_netlink_parms(data, &nt->parms);
	ign = net_generic(net, ip6gre_net_id);

	if (nt->parms.collect_md) {
		if (rtnl_dereference(ign->collect_md_tun_erspan))
			return -EEXIST;
	} else {
		if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
			return -EEXIST;
	}
2230

2231
	err = ip6gre_newlink_common(src_net, dev, tb, data, extack);
2232 2233
	if (!err) {
		ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]);
2234
		ip6erspan_tunnel_link_md(ign, nt);
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
		ip6gre_tunnel_link(net_generic(net, ip6gre_net_id), nt);
	}
	return err;
}

static void ip6erspan_tnl_link_config(struct ip6_tnl *t, int set_mtu)
{
	ip6gre_tnl_link_config_common(t);
	ip6gre_tnl_link_config_route(t, set_mtu, ip6erspan_calc_hlen(t));
}

static int ip6erspan_tnl_change(struct ip6_tnl *t,
				const struct __ip6_tnl_parm *p, int set_mtu)
{
	ip6gre_tnl_copy_tnl_parm(t, p);
	ip6erspan_tnl_link_config(t, set_mtu);
	return 0;
}

static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
				struct nlattr *data[],
				struct netlink_ext_ack *extack)
{
	struct ip6gre_net *ign = net_generic(dev_net(dev), ip6gre_net_id);
	struct __ip6_tnl_parm p;
	struct ip6_tnl *t;

	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
	if (IS_ERR(t))
		return PTR_ERR(t);

2266
	ip6gre_tunnel_unlink_md(ign, t);
2267 2268
	ip6gre_tunnel_unlink(ign, t);
	ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
2269
	ip6erspan_tunnel_link_md(ign, t);
2270 2271 2272 2273
	ip6gre_tunnel_link(ign, t);
	return 0;
}

X
xeb@mail.ru 已提交
2274 2275 2276 2277 2278 2279 2280 2281 2282
static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
	.kind		= "ip6gre",
	.maxtype	= IFLA_GRE_MAX,
	.policy		= ip6gre_policy,
	.priv_size	= sizeof(struct ip6_tnl),
	.setup		= ip6gre_tunnel_setup,
	.validate	= ip6gre_tunnel_validate,
	.newlink	= ip6gre_newlink,
	.changelink	= ip6gre_changelink,
2283
	.dellink	= ip6gre_dellink,
X
xeb@mail.ru 已提交
2284 2285
	.get_size	= ip6gre_get_size,
	.fill_info	= ip6gre_fill_info,
2286
	.get_link_net	= ip6_tnl_get_link_net,
X
xeb@mail.ru 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
};

static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
	.kind		= "ip6gretap",
	.maxtype	= IFLA_GRE_MAX,
	.policy		= ip6gre_policy,
	.priv_size	= sizeof(struct ip6_tnl),
	.setup		= ip6gre_tap_setup,
	.validate	= ip6gre_tap_validate,
	.newlink	= ip6gre_newlink,
	.changelink	= ip6gre_changelink,
	.get_size	= ip6gre_get_size,
	.fill_info	= ip6gre_fill_info,
2300
	.get_link_net	= ip6_tnl_get_link_net,
X
xeb@mail.ru 已提交
2301 2302
};

2303 2304 2305 2306 2307 2308 2309
static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = {
	.kind		= "ip6erspan",
	.maxtype	= IFLA_GRE_MAX,
	.policy		= ip6gre_policy,
	.priv_size	= sizeof(struct ip6_tnl),
	.setup		= ip6erspan_tap_setup,
	.validate	= ip6erspan_tap_validate,
2310 2311
	.newlink	= ip6erspan_newlink,
	.changelink	= ip6erspan_changelink,
2312 2313 2314 2315 2316
	.get_size	= ip6gre_get_size,
	.fill_info	= ip6gre_fill_info,
	.get_link_net	= ip6_tnl_get_link_net,
};

X
xeb@mail.ru 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
/*
 *	And now the modules code and kernel interface.
 */

static int __init ip6gre_init(void)
{
	int err;

	pr_info("GRE over IPv6 tunneling driver\n");

	err = register_pernet_device(&ip6gre_net_ops);
	if (err < 0)
		return err;

	err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
	if (err < 0) {
		pr_info("%s: can't add protocol\n", __func__);
		goto add_proto_failed;
	}

	err = rtnl_link_register(&ip6gre_link_ops);
	if (err < 0)
		goto rtnl_link_failed;

	err = rtnl_link_register(&ip6gre_tap_ops);
	if (err < 0)
		goto tap_ops_failed;

2345 2346 2347 2348
	err = rtnl_link_register(&ip6erspan_tap_ops);
	if (err < 0)
		goto erspan_link_failed;

X
xeb@mail.ru 已提交
2349 2350 2351
out:
	return err;

2352 2353
erspan_link_failed:
	rtnl_link_unregister(&ip6gre_tap_ops);
X
xeb@mail.ru 已提交
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
tap_ops_failed:
	rtnl_link_unregister(&ip6gre_link_ops);
rtnl_link_failed:
	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
add_proto_failed:
	unregister_pernet_device(&ip6gre_net_ops);
	goto out;
}

static void __exit ip6gre_fini(void)
{
	rtnl_link_unregister(&ip6gre_tap_ops);
	rtnl_link_unregister(&ip6gre_link_ops);
2367
	rtnl_link_unregister(&ip6erspan_tap_ops);
X
xeb@mail.ru 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
	unregister_pernet_device(&ip6gre_net_ops);
}

module_init(ip6gre_init);
module_exit(ip6gre_fini);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
MODULE_ALIAS_RTNL_LINK("ip6gre");
2378
MODULE_ALIAS_RTNL_LINK("ip6gretap");
W
William Tu 已提交
2379
MODULE_ALIAS_RTNL_LINK("ip6erspan");
X
xeb@mail.ru 已提交
2380
MODULE_ALIAS_NETDEV("ip6gre0");