fou.c 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <net/genetlink.h>
10
#include <net/gue.h>
11
#include <net/ip.h>
T
Tom Herbert 已提交
12
#include <net/protocol.h>
13 14 15 16 17 18 19 20 21
#include <net/udp.h>
#include <net/udp_tunnel.h>
#include <net/xfrm.h>
#include <uapi/linux/fou.h>
#include <uapi/linux/genetlink.h>

struct fou {
	struct socket *sock;
	u8 protocol;
22
	u8 flags;
W
WANG Cong 已提交
23
	__be16 port;
W
WANG Cong 已提交
24
	u16 type;
T
Tom Herbert 已提交
25
	struct udp_offload udp_offloads;
26
	struct list_head list;
27
	struct rcu_head rcu;
28 29
};

30 31
#define FOU_F_REMCSUM_NOPARTIAL BIT(0)

32
struct fou_cfg {
33
	u16 type;
34
	u8 protocol;
35
	u8 flags;
36 37 38
	struct udp_port_cfg udp_config;
};

W
WANG Cong 已提交
39 40 41 42 43 44 45
static unsigned int fou_net_id;

struct fou_net {
	struct list_head fou_list;
	struct mutex fou_lock;
};

46 47 48 49 50
static inline struct fou *fou_from_sock(struct sock *sk)
{
	return sk->sk_user_data;
}

51
static int fou_recv_pull(struct sk_buff *skb, size_t len)
52 53 54 55
{
	struct iphdr *iph = ip_hdr(skb);

	/* Remove 'len' bytes from the packet (UDP header and
56
	 * FOU header if present).
57 58 59 60 61
	 */
	iph->tot_len = htons(ntohs(iph->tot_len) - len);
	__skb_pull(skb, len);
	skb_postpull_rcsum(skb, udp_hdr(skb), len);
	skb_reset_transport_header(skb);
62
	return iptunnel_pull_offloads(skb);
63 64 65 66 67 68 69 70 71
}

static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
{
	struct fou *fou = fou_from_sock(sk);

	if (!fou)
		return 1;

72 73
	if (fou_recv_pull(skb, sizeof(struct udphdr)))
		goto drop;
74 75

	return -fou->protocol;
76 77 78 79

drop:
	kfree_skb(skb);
	return 0;
80 81
}

82
static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
83 84
				  void *data, size_t hdrlen, u8 ipproto,
				  bool nopartial)
85 86
{
	__be16 *pd = data;
T
Tom Herbert 已提交
87 88
	size_t start = ntohs(pd[0]);
	size_t offset = ntohs(pd[1]);
89 90 91 92 93
	size_t plen = sizeof(struct udphdr) + hdrlen +
	    max_t(size_t, offset + sizeof(u16), start);

	if (skb->remcsum_offload)
		return guehdr;
94 95 96 97 98

	if (!pskb_may_pull(skb, plen))
		return NULL;
	guehdr = (struct guehdr *)&udp_hdr(skb)[1];

99 100
	skb_remcsum_process(skb, (void *)guehdr + hdrlen,
			    start, offset, nopartial);
101 102 103 104

	return guehdr;
}

105 106 107 108 109
static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
{
	/* No support yet */
	kfree_skb(skb);
	return 0;
110 111
}

112 113 114
static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
{
	struct fou *fou = fou_from_sock(sk);
115
	size_t len, optlen, hdrlen;
116
	struct guehdr *guehdr;
117
	void *data;
118
	u16 doffset = 0;
119 120 121 122 123 124 125 126

	if (!fou)
		return 1;

	len = sizeof(struct udphdr) + sizeof(struct guehdr);
	if (!pskb_may_pull(skb, len))
		goto drop;

127 128 129 130
	guehdr = (struct guehdr *)&udp_hdr(skb)[1];

	optlen = guehdr->hlen << 2;
	len += optlen;
131 132 133 134

	if (!pskb_may_pull(skb, len))
		goto drop;

135 136
	/* guehdr may change after pull */
	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
137

138
	hdrlen = sizeof(struct guehdr) + optlen;
139

140
	if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
141
		goto drop;
142

143 144 145 146 147 148 149 150
	hdrlen = sizeof(struct guehdr) + optlen;

	ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);

	/* Pull csum through the guehdr now . This can be used if
	 * there is a remote checksum offload.
	 */
	skb_postpull_rcsum(skb, udp_hdr(skb), len);
151 152 153 154

	data = &guehdr[1];

	if (guehdr->flags & GUE_FLAG_PRIV) {
155 156 157 158 159 160
		__be32 flags = *(__be32 *)(data + doffset);

		doffset += GUE_LEN_PRIV;

		if (flags & GUE_PFLAG_REMCSUM) {
			guehdr = gue_remcsum(skb, guehdr, data + doffset,
161 162 163
					     hdrlen, guehdr->proto_ctype,
					     !!(fou->flags &
						FOU_F_REMCSUM_NOPARTIAL));
164 165 166 167
			if (!guehdr)
				goto drop;

			data = &guehdr[1];
168

169 170
			doffset += GUE_PLEN_REMCSUM;
		}
171 172
	}

173 174 175
	if (unlikely(guehdr->control))
		return gue_control_message(skb, guehdr);

T
Tom Herbert 已提交
176
	__skb_pull(skb, sizeof(struct udphdr) + hdrlen);
177 178
	skb_reset_transport_header(skb);

179 180 181
	if (iptunnel_pull_offloads(skb))
		goto drop;

182 183
	return -guehdr->proto_ctype;

184 185 186 187 188
drop:
	kfree_skb(skb);
	return 0;
}

T
Tom Herbert 已提交
189
static struct sk_buff **fou_gro_receive(struct sk_buff **head,
190 191
					struct sk_buff *skb,
					struct udp_offload *uoff)
T
Tom Herbert 已提交
192 193 194 195
{
	const struct net_offload *ops;
	struct sk_buff **pp = NULL;
	u8 proto = NAPI_GRO_CB(skb)->proto;
196
	const struct net_offload **offloads;
T
Tom Herbert 已提交
197

198 199 200 201 202 203 204 205
	/* We can clear the encap_mark for FOU as we are essentially doing
	 * one of two possible things.  We are either adding an L4 tunnel
	 * header to the outer L3 tunnel header, or we are are simply
	 * treating the GRE tunnel header as though it is a UDP protocol
	 * specific header such as VXLAN or GENEVE.
	 */
	NAPI_GRO_CB(skb)->encap_mark = 0;

206 207 208
	/* Flag this frame as already having an outer encap header */
	NAPI_GRO_CB(skb)->is_fou = 1;

T
Tom Herbert 已提交
209
	rcu_read_lock();
210
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
T
Tom Herbert 已提交
211 212 213 214 215 216 217 218 219 220 221 222
	ops = rcu_dereference(offloads[proto]);
	if (!ops || !ops->callbacks.gro_receive)
		goto out_unlock;

	pp = ops->callbacks.gro_receive(head, skb);

out_unlock:
	rcu_read_unlock();

	return pp;
}

223 224
static int fou_gro_complete(struct sk_buff *skb, int nhoff,
			    struct udp_offload *uoff)
T
Tom Herbert 已提交
225 226 227 228
{
	const struct net_offload *ops;
	u8 proto = NAPI_GRO_CB(skb)->proto;
	int err = -ENOSYS;
229
	const struct net_offload **offloads;
T
Tom Herbert 已提交
230 231

	rcu_read_lock();
232
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
T
Tom Herbert 已提交
233 234 235 236 237 238
	ops = rcu_dereference(offloads[proto]);
	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
		goto out_unlock;

	err = ops->callbacks.gro_complete(skb, nhoff);

239 240
	skb_set_inner_mac_header(skb, nhoff);

T
Tom Herbert 已提交
241 242 243 244 245 246
out_unlock:
	rcu_read_unlock();

	return err;
}

247 248
static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
				      struct guehdr *guehdr, void *data,
249 250
				      size_t hdrlen, struct gro_remcsum *grc,
				      bool nopartial)
251 252
{
	__be16 *pd = data;
T
Tom Herbert 已提交
253 254
	size_t start = ntohs(pd[0]);
	size_t offset = ntohs(pd[1]);
255 256

	if (skb->remcsum_offload)
257
		return guehdr;
258

T
Tom Herbert 已提交
259
	if (!NAPI_GRO_CB(skb)->csum_valid)
260 261
		return NULL;

262 263
	guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
					 start, offset, grc, nopartial);
264 265 266 267 268 269

	skb->remcsum_offload = 1;

	return guehdr;
}

270
static struct sk_buff **gue_gro_receive(struct sk_buff **head,
271 272
					struct sk_buff *skb,
					struct udp_offload *uoff)
273 274 275 276 277 278
{
	const struct net_offload **offloads;
	const struct net_offload *ops;
	struct sk_buff **pp = NULL;
	struct sk_buff *p;
	struct guehdr *guehdr;
279 280
	size_t len, optlen, hdrlen, off;
	void *data;
281
	u16 doffset = 0;
282
	int flush = 1;
283
	struct fou *fou = container_of(uoff, struct fou, udp_offloads);
284 285 286
	struct gro_remcsum grc;

	skb_gro_remcsum_init(&grc);
287 288

	off = skb_gro_offset(skb);
289 290
	len = off + sizeof(*guehdr);

291
	guehdr = skb_gro_header_fast(skb, off);
292 293
	if (skb_gro_header_hard(skb, len)) {
		guehdr = skb_gro_header_slow(skb, len, off);
294 295 296 297
		if (unlikely(!guehdr))
			goto out;
	}

298 299
	optlen = guehdr->hlen << 2;
	len += optlen;
300

301 302 303 304 305
	if (skb_gro_header_hard(skb, len)) {
		guehdr = skb_gro_header_slow(skb, len, off);
		if (unlikely(!guehdr))
			goto out;
	}
306

307 308 309
	if (unlikely(guehdr->control) || guehdr->version != 0 ||
	    validate_gue_flags(guehdr, optlen))
		goto out;
310

311 312
	hdrlen = sizeof(*guehdr) + optlen;

313 314 315
	/* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
	 * this is needed if there is a remote checkcsum offload.
	 */
316 317 318 319 320
	skb_gro_postpull_rcsum(skb, guehdr, hdrlen);

	data = &guehdr[1];

	if (guehdr->flags & GUE_FLAG_PRIV) {
321
		__be32 flags = *(__be32 *)(data + doffset);
322

323 324 325 326
		doffset += GUE_LEN_PRIV;

		if (flags & GUE_PFLAG_REMCSUM) {
			guehdr = gue_gro_remcsum(skb, off, guehdr,
327
						 data + doffset, hdrlen, &grc,
328 329
						 !!(fou->flags &
						    FOU_F_REMCSUM_NOPARTIAL));
330

331 332 333 334 335 336 337
			if (!guehdr)
				goto out;

			data = &guehdr[1];

			doffset += GUE_PLEN_REMCSUM;
		}
338 339
	}

340 341
	skb_gro_pull(skb, hdrlen);

342 343 344 345 346 347 348 349 350
	for (p = *head; p; p = p->next) {
		const struct guehdr *guehdr2;

		if (!NAPI_GRO_CB(p)->same_flow)
			continue;

		guehdr2 = (struct guehdr *)(p->data + off);

		/* Compare base GUE header to be equal (covers
351
		 * hlen, version, proto_ctype, and flags.
352 353 354 355 356 357 358 359 360 361 362 363 364 365
		 */
		if (guehdr->word != guehdr2->word) {
			NAPI_GRO_CB(p)->same_flow = 0;
			continue;
		}

		/* Compare optional fields are the same. */
		if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
					   guehdr->hlen << 2)) {
			NAPI_GRO_CB(p)->same_flow = 0;
			continue;
		}
	}

366 367 368 369 370 371 372 373
	/* We can clear the encap_mark for GUE as we are essentially doing
	 * one of two possible things.  We are either adding an L4 tunnel
	 * header to the outer L3 tunnel header, or we are are simply
	 * treating the GRE tunnel header as though it is a UDP protocol
	 * specific header such as VXLAN or GENEVE.
	 */
	NAPI_GRO_CB(skb)->encap_mark = 0;

374 375 376
	/* Flag this frame as already having an outer encap header */
	NAPI_GRO_CB(skb)->is_fou = 1;

377 378 379
	rcu_read_lock();
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
	ops = rcu_dereference(offloads[guehdr->proto_ctype]);
380
	if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
381
		goto out_unlock;
382 383

	pp = ops->callbacks.gro_receive(head, skb);
384
	flush = 0;
385 386 387 388 389

out_unlock:
	rcu_read_unlock();
out:
	NAPI_GRO_CB(skb)->flush |= flush;
390
	skb_gro_remcsum_cleanup(skb, &grc);
391 392 393 394

	return pp;
}

395 396
static int gue_gro_complete(struct sk_buff *skb, int nhoff,
			    struct udp_offload *uoff)
397 398 399 400 401 402 403 404
{
	const struct net_offload **offloads;
	struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
	const struct net_offload *ops;
	unsigned int guehlen;
	u8 proto;
	int err = -ENOENT;

405
	proto = guehdr->proto_ctype;
406 407 408 409 410 411 412 413 414 415 416

	guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);

	rcu_read_lock();
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
	ops = rcu_dereference(offloads[proto]);
	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
		goto out_unlock;

	err = ops->callbacks.gro_complete(skb, nhoff + guehlen);

417 418
	skb_set_inner_mac_header(skb, nhoff + guehlen);

419 420 421 422 423
out_unlock:
	rcu_read_unlock();
	return err;
}

W
WANG Cong 已提交
424
static int fou_add_to_port_list(struct net *net, struct fou *fou)
425
{
W
WANG Cong 已提交
426
	struct fou_net *fn = net_generic(net, fou_net_id);
427 428
	struct fou *fout;

W
WANG Cong 已提交
429 430
	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fout, &fn->fou_list, list) {
431
		if (fou->port == fout->port) {
W
WANG Cong 已提交
432
			mutex_unlock(&fn->fou_lock);
433 434 435 436
			return -EALREADY;
		}
	}

W
WANG Cong 已提交
437 438
	list_add(&fou->list, &fn->fou_list);
	mutex_unlock(&fn->fou_lock);
439 440 441 442 443 444 445 446 447

	return 0;
}

static void fou_release(struct fou *fou)
{
	struct socket *sock = fou->sock;
	struct sock *sk = sock->sk;

448 449
	if (sk->sk_family == AF_INET)
		udp_del_offload(&fou->udp_offloads);
450
	list_del(&fou->list);
W
WANG Cong 已提交
451
	udp_tunnel_sock_release(sock);
452

453
	kfree_rcu(fou, rcu);
454 455
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static int fou_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
{
	udp_sk(sk)->encap_rcv = fou_udp_recv;
	fou->protocol = cfg->protocol;
	fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
	fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
	fou->udp_offloads.port = cfg->udp_config.local_udp_port;
	fou->udp_offloads.ipproto = cfg->protocol;

	return 0;
}

static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
{
	udp_sk(sk)->encap_rcv = gue_udp_recv;
	fou->udp_offloads.callbacks.gro_receive = gue_gro_receive;
	fou->udp_offloads.callbacks.gro_complete = gue_gro_complete;
	fou->udp_offloads.port = cfg->udp_config.local_udp_port;

	return 0;
}

478 479 480 481
static int fou_create(struct net *net, struct fou_cfg *cfg,
		      struct socket **sockp)
{
	struct socket *sock = NULL;
W
WANG Cong 已提交
482
	struct fou *fou = NULL;
483
	struct sock *sk;
W
WANG Cong 已提交
484
	int err;
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

	/* Open UDP socket */
	err = udp_sock_create(net, &cfg->udp_config, &sock);
	if (err < 0)
		goto error;

	/* Allocate FOU port structure */
	fou = kzalloc(sizeof(*fou), GFP_KERNEL);
	if (!fou) {
		err = -ENOMEM;
		goto error;
	}

	sk = sock->sk;

500
	fou->flags = cfg->flags;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	fou->port = cfg->udp_config.local_udp_port;

	/* Initial for fou type */
	switch (cfg->type) {
	case FOU_ENCAP_DIRECT:
		err = fou_encap_init(sk, fou, cfg);
		if (err)
			goto error;
		break;
	case FOU_ENCAP_GUE:
		err = gue_encap_init(sk, fou, cfg);
		if (err)
			goto error;
		break;
	default:
		err = -EINVAL;
		goto error;
	}
519

W
WANG Cong 已提交
520 521
	fou->type = cfg->type;

522 523 524 525 526 527
	udp_sk(sk)->encap_type = 1;
	udp_encap_enable();

	sk->sk_user_data = fou;
	fou->sock = sock;

528
	inet_inc_convert_csum(sk);
529 530 531

	sk->sk_allocation = GFP_ATOMIC;

T
Tom Herbert 已提交
532
	if (cfg->udp_config.family == AF_INET) {
533
		err = udp_add_offload(net, &fou->udp_offloads);
T
Tom Herbert 已提交
534 535 536 537
		if (err)
			goto error;
	}

W
WANG Cong 已提交
538
	err = fou_add_to_port_list(net, fou);
539 540 541 542 543 544 545 546 547 548 549
	if (err)
		goto error;

	if (sockp)
		*sockp = sock;

	return 0;

error:
	kfree(fou);
	if (sock)
W
WANG Cong 已提交
550
		udp_tunnel_sock_release(sock);
551 552 553 554 555 556

	return err;
}

static int fou_destroy(struct net *net, struct fou_cfg *cfg)
{
W
WANG Cong 已提交
557
	struct fou_net *fn = net_generic(net, fou_net_id);
W
WANG Cong 已提交
558
	__be16 port = cfg->udp_config.local_udp_port;
559
	int err = -EINVAL;
W
WANG Cong 已提交
560
	struct fou *fou;
561

W
WANG Cong 已提交
562 563
	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fou, &fn->fou_list, list) {
564 565 566 567 568 569
		if (fou->port == port) {
			fou_release(fou);
			err = 0;
			break;
		}
	}
W
WANG Cong 已提交
570
	mutex_unlock(&fn->fou_lock);
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

	return err;
}

static struct genl_family fou_nl_family = {
	.id		= GENL_ID_GENERATE,
	.hdrsize	= 0,
	.name		= FOU_GENL_NAME,
	.version	= FOU_GENL_VERSION,
	.maxattr	= FOU_ATTR_MAX,
	.netnsok	= true,
};

static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
	[FOU_ATTR_PORT] = { .type = NLA_U16, },
	[FOU_ATTR_AF] = { .type = NLA_U8, },
	[FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
588
	[FOU_ATTR_TYPE] = { .type = NLA_U8, },
589
	[FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
590 591 592 593 594 595 596 597 598 599 600 601
};

static int parse_nl_config(struct genl_info *info,
			   struct fou_cfg *cfg)
{
	memset(cfg, 0, sizeof(*cfg));

	cfg->udp_config.family = AF_INET;

	if (info->attrs[FOU_ATTR_AF]) {
		u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);

J
Jiri Benc 已提交
602
		if (family != AF_INET)
603 604 605 606 607 608
			return -EINVAL;

		cfg->udp_config.family = family;
	}

	if (info->attrs[FOU_ATTR_PORT]) {
W
WANG Cong 已提交
609
		__be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
610 611 612 613 614 615 616

		cfg->udp_config.local_udp_port = port;
	}

	if (info->attrs[FOU_ATTR_IPPROTO])
		cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);

617 618 619
	if (info->attrs[FOU_ATTR_TYPE])
		cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);

620 621 622
	if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
		cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;

623 624 625 626 627
	return 0;
}

static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
{
W
WANG Cong 已提交
628
	struct net *net = genl_info_net(info);
629 630 631 632 633 634 635
	struct fou_cfg cfg;
	int err;

	err = parse_nl_config(info, &cfg);
	if (err)
		return err;

W
WANG Cong 已提交
636
	return fou_create(net, &cfg, NULL);
637 638 639 640
}

static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
{
W
WANG Cong 已提交
641
	struct net *net = genl_info_net(info);
642
	struct fou_cfg cfg;
643
	int err;
644

645 646 647
	err = parse_nl_config(info, &cfg);
	if (err)
		return err;
648

W
WANG Cong 已提交
649
	return fou_destroy(net, &cfg);
650 651
}

W
WANG Cong 已提交
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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
{
	if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
	    nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
	    nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
	    nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
		return -1;

	if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
		if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
			return -1;
	return 0;
}

static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
			 u32 flags, struct sk_buff *skb, u8 cmd)
{
	void *hdr;

	hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
	if (!hdr)
		return -ENOMEM;

	if (fou_fill_info(fou, skb) < 0)
		goto nla_put_failure;

	genlmsg_end(skb, hdr);
	return 0;

nla_put_failure:
	genlmsg_cancel(skb, hdr);
	return -EMSGSIZE;
}

static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
{
	struct net *net = genl_info_net(info);
	struct fou_net *fn = net_generic(net, fou_net_id);
	struct sk_buff *msg;
	struct fou_cfg cfg;
	struct fou *fout;
	__be16 port;
	int ret;

	ret = parse_nl_config(info, &cfg);
	if (ret)
		return ret;
	port = cfg.udp_config.local_udp_port;
	if (port == 0)
		return -EINVAL;

	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg)
		return -ENOMEM;

	ret = -ESRCH;
	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fout, &fn->fou_list, list) {
		if (port == fout->port) {
			ret = fou_dump_info(fout, info->snd_portid,
					    info->snd_seq, 0, msg,
					    info->genlhdr->cmd);
			break;
		}
	}
	mutex_unlock(&fn->fou_lock);
	if (ret < 0)
		goto out_free;

	return genlmsg_reply(msg, info);

out_free:
	nlmsg_free(msg);
	return ret;
}

static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct net *net = sock_net(skb->sk);
	struct fou_net *fn = net_generic(net, fou_net_id);
	struct fou *fout;
	int idx = 0, ret;

	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fout, &fn->fou_list, list) {
		if (idx++ < cb->args[0])
			continue;
		ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
				    cb->nlh->nlmsg_seq, NLM_F_MULTI,
				    skb, FOU_CMD_GET);
		if (ret)
743
			break;
W
WANG Cong 已提交
744 745 746 747 748 749 750
	}
	mutex_unlock(&fn->fou_lock);

	cb->args[0] = idx;
	return skb->len;
}

751 752 753 754 755 756 757 758 759 760 761 762 763
static const struct genl_ops fou_nl_ops[] = {
	{
		.cmd = FOU_CMD_ADD,
		.doit = fou_nl_cmd_add_port,
		.policy = fou_nl_policy,
		.flags = GENL_ADMIN_PERM,
	},
	{
		.cmd = FOU_CMD_DEL,
		.doit = fou_nl_cmd_rm_port,
		.policy = fou_nl_policy,
		.flags = GENL_ADMIN_PERM,
	},
W
WANG Cong 已提交
764 765 766 767 768 769
	{
		.cmd = FOU_CMD_GET,
		.doit = fou_nl_cmd_get_port,
		.dumpit = fou_nl_dump,
		.policy = fou_nl_policy,
	},
770 771
};

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
size_t fou_encap_hlen(struct ip_tunnel_encap *e)
{
	return sizeof(struct udphdr);
}
EXPORT_SYMBOL(fou_encap_hlen);

size_t gue_encap_hlen(struct ip_tunnel_encap *e)
{
	size_t len;
	bool need_priv = false;

	len = sizeof(struct udphdr) + sizeof(struct guehdr);

	if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
		len += GUE_PLEN_REMCSUM;
		need_priv = true;
	}

	len += need_priv ? GUE_LEN_PRIV : 0;

	return len;
}
EXPORT_SYMBOL(gue_encap_hlen);

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
			  struct flowi4 *fl4, u8 *protocol, __be16 sport)
{
	struct udphdr *uh;

	skb_push(skb, sizeof(struct udphdr));
	skb_reset_transport_header(skb);

	uh = udp_hdr(skb);

	uh->dest = e->dport;
	uh->source = sport;
	uh->len = htons(skb->len);
	udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
		     fl4->saddr, fl4->daddr, skb->len);

	*protocol = IPPROTO_UDP;
}

int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
		     u8 *protocol, struct flowi4 *fl4)
{
E
Edward Cree 已提交
818 819
	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
						       SKB_GSO_UDP_TUNNEL;
820 821
	__be16 sport;

822
	skb = iptunnel_handle_offloads(skb, type);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

	if (IS_ERR(skb))
		return PTR_ERR(skb);

	sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
					       skb, 0, 0, false);
	fou_build_udp(skb, e, fl4, protocol, sport);

	return 0;
}
EXPORT_SYMBOL(fou_build_header);

int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
		     u8 *protocol, struct flowi4 *fl4)
{
E
Edward Cree 已提交
838 839
	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
						       SKB_GSO_UDP_TUNNEL;
840
	struct guehdr *guehdr;
841
	size_t hdrlen, optlen = 0;
842
	__be16 sport;
843 844 845
	void *data;
	bool need_priv = false;

846 847 848 849 850 851 852
	if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
	    skb->ip_summed == CHECKSUM_PARTIAL) {
		optlen += GUE_PLEN_REMCSUM;
		type |= SKB_GSO_TUNNEL_REMCSUM;
		need_priv = true;
	}

853
	optlen += need_priv ? GUE_LEN_PRIV : 0;
854

855
	skb = iptunnel_handle_offloads(skb, type);
856 857 858 859 860 861 862 863

	if (IS_ERR(skb))
		return PTR_ERR(skb);

	/* Get source port (based on flow hash) before skb_push */
	sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
					       skb, 0, 0, false);

864 865 866
	hdrlen = sizeof(struct guehdr) + optlen;

	skb_push(skb, hdrlen);
867 868 869

	guehdr = (struct guehdr *)skb->data;

870
	guehdr->control = 0;
871
	guehdr->version = 0;
872
	guehdr->hlen = optlen >> 2;
873
	guehdr->flags = 0;
874 875 876 877 878 879 880 881 882 883 884
	guehdr->proto_ctype = *protocol;

	data = &guehdr[1];

	if (need_priv) {
		__be32 *flags = data;

		guehdr->flags |= GUE_FLAG_PRIV;
		*flags = 0;
		data += GUE_LEN_PRIV;

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
		if (type & SKB_GSO_TUNNEL_REMCSUM) {
			u16 csum_start = skb_checksum_start_offset(skb);
			__be16 *pd = data;

			if (csum_start < hdrlen)
				return -EINVAL;

			csum_start -= hdrlen;
			pd[0] = htons(csum_start);
			pd[1] = htons(csum_start + skb->csum_offset);

			if (!skb_is_gso(skb)) {
				skb->ip_summed = CHECKSUM_NONE;
				skb->encapsulation = 0;
			}

			*flags |= GUE_PFLAG_REMCSUM;
			data += GUE_PLEN_REMCSUM;
		}

905
	}
906 907 908 909 910 911 912

	fou_build_udp(skb, e, fl4, protocol, sport);

	return 0;
}
EXPORT_SYMBOL(gue_build_header);

913 914
#ifdef CONFIG_NET_FOU_IP_TUNNELS

A
Andi Kleen 已提交
915
static const struct ip_tunnel_encap_ops fou_iptun_ops = {
916 917 918 919
	.encap_hlen = fou_encap_hlen,
	.build_header = fou_build_header,
};

A
Andi Kleen 已提交
920
static const struct ip_tunnel_encap_ops gue_iptun_ops = {
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	.encap_hlen = gue_encap_hlen,
	.build_header = gue_build_header,
};

static int ip_tunnel_encap_add_fou_ops(void)
{
	int ret;

	ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
	if (ret < 0) {
		pr_err("can't add fou ops\n");
		return ret;
	}

	ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
	if (ret < 0) {
		pr_err("can't add gue ops\n");
		ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
		return ret;
	}

	return 0;
}

static void ip_tunnel_encap_del_fou_ops(void)
{
	ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
	ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
}

#else

static int ip_tunnel_encap_add_fou_ops(void)
{
	return 0;
}

958
static void ip_tunnel_encap_del_fou_ops(void)
959 960 961 962 963
{
}

#endif

W
WANG Cong 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
static __net_init int fou_init_net(struct net *net)
{
	struct fou_net *fn = net_generic(net, fou_net_id);

	INIT_LIST_HEAD(&fn->fou_list);
	mutex_init(&fn->fou_lock);
	return 0;
}

static __net_exit void fou_exit_net(struct net *net)
{
	struct fou_net *fn = net_generic(net, fou_net_id);
	struct fou *fou, *next;

	/* Close all the FOU sockets */
	mutex_lock(&fn->fou_lock);
	list_for_each_entry_safe(fou, next, &fn->fou_list, list)
		fou_release(fou);
	mutex_unlock(&fn->fou_lock);
}

static struct pernet_operations fou_net_ops = {
	.init = fou_init_net,
	.exit = fou_exit_net,
	.id   = &fou_net_id,
	.size = sizeof(struct fou_net),
};

992 993 994 995
static int __init fou_init(void)
{
	int ret;

W
WANG Cong 已提交
996 997 998 999
	ret = register_pernet_device(&fou_net_ops);
	if (ret)
		goto exit;

1000 1001
	ret = genl_register_family_with_ops(&fou_nl_family,
					    fou_nl_ops);
1002
	if (ret < 0)
W
WANG Cong 已提交
1003
		goto unregister;
1004 1005

	ret = ip_tunnel_encap_add_fou_ops();
W
WANG Cong 已提交
1006 1007
	if (ret == 0)
		return 0;
1008

W
WANG Cong 已提交
1009 1010 1011
	genl_unregister_family(&fou_nl_family);
unregister:
	unregister_pernet_device(&fou_net_ops);
1012
exit:
1013 1014 1015 1016 1017
	return ret;
}

static void __exit fou_fini(void)
{
1018
	ip_tunnel_encap_del_fou_ops();
1019
	genl_unregister_family(&fou_nl_family);
W
WANG Cong 已提交
1020
	unregister_pernet_device(&fou_net_ops);
1021 1022 1023 1024 1025 1026
}

module_init(fou_init);
module_exit(fou_fini);
MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
MODULE_LICENSE("GPL");