fou.c 28.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
7
#include <linux/icmp.h>
8 9 10 11
#include <linux/udp.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <net/genetlink.h>
12
#include <net/gue.h>
13
#include <net/fou.h>
14
#include <net/ip.h>
T
Tom Herbert 已提交
15
#include <net/protocol.h>
16 17 18 19 20 21 22 23 24
#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;
25
	u8 flags;
W
WANG Cong 已提交
26
	__be16 port;
T
Tom Herbert 已提交
27
	u8 family;
W
WANG Cong 已提交
28
	u16 type;
29
	struct list_head list;
30
	struct rcu_head rcu;
31 32
};

33 34
#define FOU_F_REMCSUM_NOPARTIAL BIT(0)

35
struct fou_cfg {
36
	u16 type;
37
	u8 protocol;
38
	u8 flags;
39 40 41
	struct udp_port_cfg udp_config;
};

W
WANG Cong 已提交
42 43 44 45 46 47 48
static unsigned int fou_net_id;

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

49 50 51 52 53
static inline struct fou *fou_from_sock(struct sock *sk)
{
	return sk->sk_user_data;
}

T
Tom Herbert 已提交
54
static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
55 56
{
	/* Remove 'len' bytes from the packet (UDP header and
57
	 * FOU header if present).
58
	 */
T
Tom Herbert 已提交
59 60 61 62 63 64
	if (fou->family == AF_INET)
		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
	else
		ipv6_hdr(skb)->payload_len =
		    htons(ntohs(ipv6_hdr(skb)->payload_len) - len);

65 66 67
	__skb_pull(skb, len);
	skb_postpull_rcsum(skb, udp_hdr(skb), len);
	skb_reset_transport_header(skb);
68
	return iptunnel_pull_offloads(skb);
69 70 71 72 73 74 75 76 77
}

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

	if (!fou)
		return 1;

T
Tom Herbert 已提交
78
	if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
79
		goto drop;
80 81

	return -fou->protocol;
82 83 84 85

drop:
	kfree_skb(skb);
	return 0;
86 87
}

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

	if (skb->remcsum_offload)
		return guehdr;
100 101 102 103 104

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

105 106
	skb_remcsum_process(skb, (void *)guehdr + hdrlen,
			    start, offset, nopartial);
107 108 109 110

	return guehdr;
}

111 112 113 114 115
static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
{
	/* No support yet */
	kfree_skb(skb);
	return 0;
116 117
}

118 119 120
static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
{
	struct fou *fou = fou_from_sock(sk);
121
	size_t len, optlen, hdrlen;
122
	struct guehdr *guehdr;
123
	void *data;
124
	u16 doffset = 0;
125
	u8 proto_ctype;
126 127 128 129 130 131 132 133

	if (!fou)
		return 1;

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

134 135
	guehdr = (struct guehdr *)&udp_hdr(skb)[1];

136 137 138 139 140
	switch (guehdr->version) {
	case 0: /* Full GUE header present */
		break;

	case 1: {
141
		/* Direct encapsulation of IPv4 or IPv6 */
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

		int prot;

		switch (((struct iphdr *)guehdr)->version) {
		case 4:
			prot = IPPROTO_IPIP;
			break;
		case 6:
			prot = IPPROTO_IPV6;
			break;
		default:
			goto drop;
		}

		if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
			goto drop;

		return -prot;
	}

	default: /* Undefined version */
		goto drop;
	}

166 167
	optlen = guehdr->hlen << 2;
	len += optlen;
168 169 170 171

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

172 173
	/* guehdr may change after pull */
	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
174

175
	if (validate_gue_flags(guehdr, optlen))
176
		goto drop;
177

178 179
	hdrlen = sizeof(struct guehdr) + optlen;

T
Tom Herbert 已提交
180 181 182 183 184
	if (fou->family == AF_INET)
		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
	else
		ipv6_hdr(skb)->payload_len =
		    htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
185 186 187 188 189

	/* 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);
190 191 192 193

	data = &guehdr[1];

	if (guehdr->flags & GUE_FLAG_PRIV) {
194 195 196 197 198 199
		__be32 flags = *(__be32 *)(data + doffset);

		doffset += GUE_LEN_PRIV;

		if (flags & GUE_PFLAG_REMCSUM) {
			guehdr = gue_remcsum(skb, guehdr, data + doffset,
200 201 202
					     hdrlen, guehdr->proto_ctype,
					     !!(fou->flags &
						FOU_F_REMCSUM_NOPARTIAL));
203 204 205 206
			if (!guehdr)
				goto drop;

			data = &guehdr[1];
207

208 209
			doffset += GUE_PLEN_REMCSUM;
		}
210 211
	}

212 213 214
	if (unlikely(guehdr->control))
		return gue_control_message(skb, guehdr);

215
	proto_ctype = guehdr->proto_ctype;
T
Tom Herbert 已提交
216
	__skb_pull(skb, sizeof(struct udphdr) + hdrlen);
217 218
	skb_reset_transport_header(skb);

219 220 221
	if (iptunnel_pull_offloads(skb))
		goto drop;

222
	return -proto_ctype;
223

224 225 226 227 228
drop:
	kfree_skb(skb);
	return 0;
}

229 230 231
static struct sk_buff *fou_gro_receive(struct sock *sk,
				       struct list_head *head,
				       struct sk_buff *skb)
T
Tom Herbert 已提交
232
{
T
Tom Herbert 已提交
233
	u8 proto = fou_from_sock(sk)->protocol;
234
	const struct net_offload **offloads;
235 236
	const struct net_offload *ops;
	struct sk_buff *pp = NULL;
T
Tom Herbert 已提交
237

238 239
	/* 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
R
Randy Dunlap 已提交
240
	 * header to the outer L3 tunnel header, or we are simply
241 242 243 244 245
	 * 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;

246 247 248
	/* Flag this frame as already having an outer encap header */
	NAPI_GRO_CB(skb)->is_fou = 1;

T
Tom Herbert 已提交
249
	rcu_read_lock();
250
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
T
Tom Herbert 已提交
251 252 253 254
	ops = rcu_dereference(offloads[proto]);
	if (!ops || !ops->callbacks.gro_receive)
		goto out_unlock;

S
Sabrina Dubroca 已提交
255
	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
T
Tom Herbert 已提交
256 257 258 259 260 261 262

out_unlock:
	rcu_read_unlock();

	return pp;
}

T
Tom Herbert 已提交
263 264
static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
			    int nhoff)
T
Tom Herbert 已提交
265 266
{
	const struct net_offload *ops;
T
Tom Herbert 已提交
267
	u8 proto = fou_from_sock(sk)->protocol;
T
Tom Herbert 已提交
268
	int err = -ENOSYS;
269
	const struct net_offload **offloads;
T
Tom Herbert 已提交
270 271

	rcu_read_lock();
272
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
T
Tom Herbert 已提交
273 274 275 276 277 278
	ops = rcu_dereference(offloads[proto]);
	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
		goto out_unlock;

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

279 280
	skb_set_inner_mac_header(skb, nhoff);

T
Tom Herbert 已提交
281 282 283 284 285 286
out_unlock:
	rcu_read_unlock();

	return err;
}

287 288
static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
				      struct guehdr *guehdr, void *data,
289 290
				      size_t hdrlen, struct gro_remcsum *grc,
				      bool nopartial)
291 292
{
	__be16 *pd = data;
T
Tom Herbert 已提交
293 294
	size_t start = ntohs(pd[0]);
	size_t offset = ntohs(pd[1]);
295 296

	if (skb->remcsum_offload)
297
		return guehdr;
298

T
Tom Herbert 已提交
299
	if (!NAPI_GRO_CB(skb)->csum_valid)
300 301
		return NULL;

302 303
	guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
					 start, offset, grc, nopartial);
304 305 306 307 308 309

	skb->remcsum_offload = 1;

	return guehdr;
}

310 311 312
static struct sk_buff *gue_gro_receive(struct sock *sk,
				       struct list_head *head,
				       struct sk_buff *skb)
313 314 315
{
	const struct net_offload **offloads;
	const struct net_offload *ops;
316
	struct sk_buff *pp = NULL;
317 318
	struct sk_buff *p;
	struct guehdr *guehdr;
319 320
	size_t len, optlen, hdrlen, off;
	void *data;
321
	u16 doffset = 0;
322
	int flush = 1;
T
Tom Herbert 已提交
323
	struct fou *fou = fou_from_sock(sk);
324
	struct gro_remcsum grc;
325
	u8 proto;
326 327

	skb_gro_remcsum_init(&grc);
328 329

	off = skb_gro_offset(skb);
330 331
	len = off + sizeof(*guehdr);

332
	guehdr = skb_gro_header_fast(skb, off);
333 334
	if (skb_gro_header_hard(skb, len)) {
		guehdr = skb_gro_header_slow(skb, len, off);
335 336 337 338
		if (unlikely(!guehdr))
			goto out;
	}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	switch (guehdr->version) {
	case 0:
		break;
	case 1:
		switch (((struct iphdr *)guehdr)->version) {
		case 4:
			proto = IPPROTO_IPIP;
			break;
		case 6:
			proto = IPPROTO_IPV6;
			break;
		default:
			goto out;
		}
		goto next_proto;
	default:
		goto out;
	}

358 359
	optlen = guehdr->hlen << 2;
	len += optlen;
360

361 362 363 364 365
	if (skb_gro_header_hard(skb, len)) {
		guehdr = skb_gro_header_slow(skb, len, off);
		if (unlikely(!guehdr))
			goto out;
	}
366

367 368 369
	if (unlikely(guehdr->control) || guehdr->version != 0 ||
	    validate_gue_flags(guehdr, optlen))
		goto out;
370

371 372
	hdrlen = sizeof(*guehdr) + optlen;

373 374 375
	/* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
	 * this is needed if there is a remote checkcsum offload.
	 */
376 377 378 379 380
	skb_gro_postpull_rcsum(skb, guehdr, hdrlen);

	data = &guehdr[1];

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

383 384 385 386
		doffset += GUE_LEN_PRIV;

		if (flags & GUE_PFLAG_REMCSUM) {
			guehdr = gue_gro_remcsum(skb, off, guehdr,
387
						 data + doffset, hdrlen, &grc,
388 389
						 !!(fou->flags &
						    FOU_F_REMCSUM_NOPARTIAL));
390

391 392 393 394 395 396 397
			if (!guehdr)
				goto out;

			data = &guehdr[1];

			doffset += GUE_PLEN_REMCSUM;
		}
398 399
	}

400 401
	skb_gro_pull(skb, hdrlen);

402
	list_for_each_entry(p, head, list) {
403 404 405 406 407 408 409 410
		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
411
		 * hlen, version, proto_ctype, and flags.
412 413 414 415 416 417 418 419 420 421 422 423 424 425
		 */
		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;
		}
	}

426 427 428 429
	proto = guehdr->proto_ctype;

next_proto:

430 431
	/* 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
R
Randy Dunlap 已提交
432
	 * header to the outer L3 tunnel header, or we are simply
433 434 435 436 437
	 * 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;

438 439 440
	/* Flag this frame as already having an outer encap header */
	NAPI_GRO_CB(skb)->is_fou = 1;

441 442
	rcu_read_lock();
	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
443
	ops = rcu_dereference(offloads[proto]);
444
	if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
445
		goto out_unlock;
446

S
Sabrina Dubroca 已提交
447
	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
448
	flush = 0;
449 450 451 452

out_unlock:
	rcu_read_unlock();
out:
453
	skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
454 455 456 457

	return pp;
}

T
Tom Herbert 已提交
458
static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
459 460 461 462
{
	const struct net_offload **offloads;
	struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
	const struct net_offload *ops;
463
	unsigned int guehlen = 0;
464 465 466
	u8 proto;
	int err = -ENOENT;

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	switch (guehdr->version) {
	case 0:
		proto = guehdr->proto_ctype;
		guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
		break;
	case 1:
		switch (((struct iphdr *)guehdr)->version) {
		case 4:
			proto = IPPROTO_IPIP;
			break;
		case 6:
			proto = IPPROTO_IPV6;
			break;
		default:
			return err;
		}
		break;
	default:
		return err;
	}
487 488 489 490 491 492 493 494 495

	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);

496 497
	skb_set_inner_mac_header(skb, nhoff + guehlen);

498 499 500 501 502
out_unlock:
	rcu_read_unlock();
	return err;
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
{
	struct sock *sk = fou->sock->sk;
	struct udp_port_cfg *udp_cfg = &cfg->udp_config;

	if (fou->family != udp_cfg->family ||
	    fou->port != udp_cfg->local_udp_port ||
	    sk->sk_dport != udp_cfg->peer_udp_port ||
	    sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
		return false;

	if (fou->family == AF_INET) {
		if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
		    sk->sk_daddr != udp_cfg->peer_ip.s_addr)
			return false;
		else
			return true;
#if IS_ENABLED(CONFIG_IPV6)
	} else {
		if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
		    ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
			return false;
		else
			return true;
#endif
	}

	return false;
}

static int fou_add_to_port_list(struct net *net, struct fou *fou,
				struct fou_cfg *cfg)
535
{
W
WANG Cong 已提交
536
	struct fou_net *fn = net_generic(net, fou_net_id);
537 538
	struct fou *fout;

W
WANG Cong 已提交
539 540
	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fout, &fn->fou_list, list) {
541
		if (fou_cfg_cmp(fout, cfg)) {
W
WANG Cong 已提交
542
			mutex_unlock(&fn->fou_lock);
543 544 545 546
			return -EALREADY;
		}
	}

W
WANG Cong 已提交
547 548
	list_add(&fou->list, &fn->fou_list);
	mutex_unlock(&fn->fou_lock);
549 550 551 552 553 554 555 556 557

	return 0;
}

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

	list_del(&fou->list);
W
WANG Cong 已提交
558
	udp_tunnel_sock_release(sock);
559

560
	kfree_rcu(fou, rcu);
561 562 563 564 565 566
}

static int fou_create(struct net *net, struct fou_cfg *cfg,
		      struct socket **sockp)
{
	struct socket *sock = NULL;
W
WANG Cong 已提交
567
	struct fou *fou = NULL;
568
	struct sock *sk;
T
Tom Herbert 已提交
569
	struct udp_tunnel_sock_cfg tunnel_cfg;
W
WANG Cong 已提交
570
	int err;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

	/* 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;

586
	fou->port = cfg->udp_config.local_udp_port;
T
Tom Herbert 已提交
587 588
	fou->family = cfg->udp_config.family;
	fou->flags = cfg->flags;
T
Tom Herbert 已提交
589 590 591 592 593 594 595
	fou->type = cfg->type;
	fou->sock = sock;

	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
	tunnel_cfg.encap_type = 1;
	tunnel_cfg.sk_user_data = fou;
	tunnel_cfg.encap_destroy = NULL;
596 597 598 599

	/* Initial for fou type */
	switch (cfg->type) {
	case FOU_ENCAP_DIRECT:
T
Tom Herbert 已提交
600 601 602 603
		tunnel_cfg.encap_rcv = fou_udp_recv;
		tunnel_cfg.gro_receive = fou_gro_receive;
		tunnel_cfg.gro_complete = fou_gro_complete;
		fou->protocol = cfg->protocol;
604 605
		break;
	case FOU_ENCAP_GUE:
T
Tom Herbert 已提交
606 607 608
		tunnel_cfg.encap_rcv = gue_udp_recv;
		tunnel_cfg.gro_receive = gue_gro_receive;
		tunnel_cfg.gro_complete = gue_gro_complete;
609 610 611 612 613
		break;
	default:
		err = -EINVAL;
		goto error;
	}
614

T
Tom Herbert 已提交
615
	setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
616 617 618

	sk->sk_allocation = GFP_ATOMIC;

619
	err = fou_add_to_port_list(net, fou, cfg);
620 621 622 623 624 625 626 627 628 629 630
	if (err)
		goto error;

	if (sockp)
		*sockp = sock;

	return 0;

error:
	kfree(fou);
	if (sock)
W
WANG Cong 已提交
631
		udp_tunnel_sock_release(sock);
632 633 634 635 636 637

	return err;
}

static int fou_destroy(struct net *net, struct fou_cfg *cfg)
{
W
WANG Cong 已提交
638
	struct fou_net *fn = net_generic(net, fou_net_id);
639
	int err = -EINVAL;
W
WANG Cong 已提交
640
	struct fou *fou;
641

W
WANG Cong 已提交
642 643
	mutex_lock(&fn->fou_lock);
	list_for_each_entry(fou, &fn->fou_list, list) {
644
		if (fou_cfg_cmp(fou, cfg)) {
645 646 647 648 649
			fou_release(fou);
			err = 0;
			break;
		}
	}
W
WANG Cong 已提交
650
	mutex_unlock(&fn->fou_lock);
651 652 653 654

	return err;
}

655
static struct genl_family fou_nl_family;
656

S
stephen hemminger 已提交
657
static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
658 659 660 661 662 663 664
	[FOU_ATTR_PORT]			= { .type = NLA_U16, },
	[FOU_ATTR_AF]			= { .type = NLA_U8, },
	[FOU_ATTR_IPPROTO]		= { .type = NLA_U8, },
	[FOU_ATTR_TYPE]			= { .type = NLA_U8, },
	[FOU_ATTR_REMCSUM_NOPARTIAL]	= { .type = NLA_FLAG, },
	[FOU_ATTR_LOCAL_V4]		= { .type = NLA_U32, },
	[FOU_ATTR_PEER_V4]		= { .type = NLA_U32, },
K
Kristian Evensen 已提交
665 666
	[FOU_ATTR_LOCAL_V6]		= { .len = sizeof(struct in6_addr), },
	[FOU_ATTR_PEER_V6]		= { .len = sizeof(struct in6_addr), },
667 668
	[FOU_ATTR_PEER_PORT]		= { .type = NLA_U16, },
	[FOU_ATTR_IFINDEX]		= { .type = NLA_S32, },
669 670 671 672 673
};

static int parse_nl_config(struct genl_info *info,
			   struct fou_cfg *cfg)
{
674 675 676 677 678
	bool has_local = false, has_peer = false;
	struct nlattr *attr;
	int ifindex;
	__be16 port;

679 680 681 682 683 684 685
	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]);

T
Tom Herbert 已提交
686 687 688 689 690 691 692 693 694
		switch (family) {
		case AF_INET:
			break;
		case AF_INET6:
			cfg->udp_config.ipv6_v6only = 1;
			break;
		default:
			return -EAFNOSUPPORT;
		}
695 696 697 698 699

		cfg->udp_config.family = family;
	}

	if (info->attrs[FOU_ATTR_PORT]) {
700
		port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
701 702 703 704 705 706
		cfg->udp_config.local_udp_port = port;
	}

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

707 708 709
	if (info->attrs[FOU_ATTR_TYPE])
		cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);

710 711 712
	if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
		cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;

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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	if (cfg->udp_config.family == AF_INET) {
		if (info->attrs[FOU_ATTR_LOCAL_V4]) {
			attr = info->attrs[FOU_ATTR_LOCAL_V4];
			cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
			has_local = true;
		}

		if (info->attrs[FOU_ATTR_PEER_V4]) {
			attr = info->attrs[FOU_ATTR_PEER_V4];
			cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
			has_peer = true;
		}
#if IS_ENABLED(CONFIG_IPV6)
	} else {
		if (info->attrs[FOU_ATTR_LOCAL_V6]) {
			attr = info->attrs[FOU_ATTR_LOCAL_V6];
			cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
			has_local = true;
		}

		if (info->attrs[FOU_ATTR_PEER_V6]) {
			attr = info->attrs[FOU_ATTR_PEER_V6];
			cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
			has_peer = true;
		}
#endif
	}

	if (has_peer) {
		if (info->attrs[FOU_ATTR_PEER_PORT]) {
			port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
			cfg->udp_config.peer_udp_port = port;
		} else {
			return -EINVAL;
		}
	}

	if (info->attrs[FOU_ATTR_IFINDEX]) {
		if (!has_local)
			return -EINVAL;

		ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);

		cfg->udp_config.bind_ifindex = ifindex;
	}

759 760 761 762 763
	return 0;
}

static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
{
W
WANG Cong 已提交
764
	struct net *net = genl_info_net(info);
765 766 767 768 769 770 771
	struct fou_cfg cfg;
	int err;

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

W
WANG Cong 已提交
772
	return fou_create(net, &cfg, NULL);
773 774 775 776
}

static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
{
W
WANG Cong 已提交
777
	struct net *net = genl_info_net(info);
778
	struct fou_cfg cfg;
779
	int err;
780

781 782 783
	err = parse_nl_config(info, &cfg);
	if (err)
		return err;
784

W
WANG Cong 已提交
785
	return fou_destroy(net, &cfg);
786 787
}

W
WANG Cong 已提交
788 789
static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
{
790 791
	struct sock *sk = fou->sock->sk;

W
WANG Cong 已提交
792 793
	if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
	    nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
794
	    nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
W
WANG Cong 已提交
795
	    nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
796 797
	    nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
	    nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
W
WANG Cong 已提交
798 799 800 801 802
		return -1;

	if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
		if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
			return -1;
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

	if (fou->sock->sk->sk_family == AF_INET) {
		if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
			return -1;

		if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
			return -1;
#if IS_ENABLED(CONFIG_IPV6)
	} else {
		if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
				     &sk->sk_v6_rcv_saddr))
			return -1;

		if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
			return -1;
#endif
	}

W
WANG Cong 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
	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;
T
Tom Herbert 已提交
852
	u8 family;
W
WANG Cong 已提交
853 854 855 856 857 858 859 860 861
	int ret;

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

T
Tom Herbert 已提交
862 863 864 865
	family = cfg.udp_config.family;
	if (family != AF_INET && family != AF_INET6)
		return -EINVAL;

W
WANG Cong 已提交
866 867 868 869 870 871 872
	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) {
873
		if (fou_cfg_cmp(fout, &cfg)) {
W
WANG Cong 已提交
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
			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)
906
			break;
W
WANG Cong 已提交
907 908 909 910 911 912 913
	}
	mutex_unlock(&fn->fou_lock);

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

914 915 916
static const struct genl_ops fou_nl_ops[] = {
	{
		.cmd = FOU_CMD_ADD,
917
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
918 919 920 921 922
		.doit = fou_nl_cmd_add_port,
		.flags = GENL_ADMIN_PERM,
	},
	{
		.cmd = FOU_CMD_DEL,
923
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
924 925 926
		.doit = fou_nl_cmd_rm_port,
		.flags = GENL_ADMIN_PERM,
	},
W
WANG Cong 已提交
927 928
	{
		.cmd = FOU_CMD_GET,
929
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
W
WANG Cong 已提交
930 931 932
		.doit = fou_nl_cmd_get_port,
		.dumpit = fou_nl_dump,
	},
933 934
};

935
static struct genl_family fou_nl_family __ro_after_init = {
936 937 938 939
	.hdrsize	= 0,
	.name		= FOU_GENL_NAME,
	.version	= FOU_GENL_VERSION,
	.maxattr	= FOU_ATTR_MAX,
940
	.policy = fou_nl_policy,
941 942 943 944 945 946
	.netnsok	= true,
	.module		= THIS_MODULE,
	.ops		= fou_nl_ops,
	.n_ops		= ARRAY_SIZE(fou_nl_ops),
};

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
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);

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
		       u8 *protocol, __be16 *sport, int type)
{
	int err;

	err = iptunnel_handle_offloads(skb, type);
	if (err)
		return err;

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

	return 0;
}
EXPORT_SYMBOL(__fou_build_header);

int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
		       u8 *protocol, __be16 *sport, int type)
989 990
{
	struct guehdr *guehdr;
991
	size_t hdrlen, optlen = 0;
992 993
	void *data;
	bool need_priv = false;
994
	int err;
995

996 997 998 999 1000 1001 1002
	if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
	    skb->ip_summed == CHECKSUM_PARTIAL) {
		optlen += GUE_PLEN_REMCSUM;
		type |= SKB_GSO_TUNNEL_REMCSUM;
		need_priv = true;
	}

1003
	optlen += need_priv ? GUE_LEN_PRIV : 0;
1004

1005 1006 1007
	err = iptunnel_handle_offloads(skb, type);
	if (err)
		return err;
1008 1009

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

1013 1014 1015
	hdrlen = sizeof(struct guehdr) + optlen;

	skb_push(skb, hdrlen);
1016 1017 1018

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

1019
	guehdr->control = 0;
1020
	guehdr->version = 0;
1021
	guehdr->hlen = optlen >> 2;
1022
	guehdr->flags = 0;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	guehdr->proto_ctype = *protocol;

	data = &guehdr[1];

	if (need_priv) {
		__be32 *flags = data;

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

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
		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;
		}

1054
	}
1055

1056 1057 1058 1059
	return 0;
}
EXPORT_SYMBOL(__gue_build_header);

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 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
#ifdef CONFIG_NET_FOU_IP_TUNNELS

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;
}

static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
			    u8 *protocol, struct flowi4 *fl4)
{
	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
						       SKB_GSO_UDP_TUNNEL;
	__be16 sport;
	int err;

	err = __fou_build_header(skb, e, protocol, &sport, type);
	if (err)
		return err;

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

	return 0;
}

static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
			    u8 *protocol, struct flowi4 *fl4)
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
						       SKB_GSO_UDP_TUNNEL;
	__be16 sport;
	int err;

	err = __gue_build_header(skb, e, protocol, &sport, type);
	if (err)
		return err;

1110 1111 1112 1113 1114
	fou_build_udp(skb, e, fl4, protocol, sport);

	return 0;
}

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
{
	const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);

	if (ipprot && ipprot->err_handler) {
		if (!ipprot->err_handler(skb, info))
			return 0;
	}

	return -ENOENT;
}

static int gue_err(struct sk_buff *skb, u32 info)
{
	int transport_offset = skb_transport_offset(skb);
	struct guehdr *guehdr;
1131
	size_t len, optlen;
1132 1133
	int ret;

1134
	len = sizeof(struct udphdr) + sizeof(struct guehdr);
1135
	if (!pskb_may_pull(skb, transport_offset + len))
1136 1137 1138 1139 1140 1141 1142 1143
		return -EINVAL;

	guehdr = (struct guehdr *)&udp_hdr(skb)[1];

	switch (guehdr->version) {
	case 0: /* Full GUE header present */
		break;
	case 1: {
1144
		/* Direct encapsulation of IPv4 or IPv6 */
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
		skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));

		switch (((struct iphdr *)guehdr)->version) {
		case 4:
			ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
			goto out;
#if IS_ENABLED(CONFIG_IPV6)
		case 6:
			ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
			goto out;
#endif
		default:
			ret = -EOPNOTSUPP;
			goto out;
		}
	}
	default: /* Undefined version */
		return -EOPNOTSUPP;
	}

	if (guehdr->control)
		return -ENOENT;

	optlen = guehdr->hlen << 2;

1170
	if (!pskb_may_pull(skb, transport_offset + len + optlen))
1171 1172 1173
		return -EINVAL;

	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
1174 1175 1176
	if (validate_gue_flags(guehdr, optlen))
		return -EINVAL;

1177 1178 1179 1180
	/* Handling exceptions for direct UDP encapsulation in GUE would lead to
	 * recursion. Besides, this kind of encapsulation can't even be
	 * configured currently. Discard this.
	 */
1181 1182
	if (guehdr->proto_ctype == IPPROTO_UDP ||
	    guehdr->proto_ctype == IPPROTO_UDPLITE)
1183 1184
		return -EOPNOTSUPP;

1185 1186 1187 1188 1189 1190 1191 1192
	skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
	ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);

out:
	skb_set_transport_header(skb, transport_offset);
	return ret;
}

1193

A
Andi Kleen 已提交
1194
static const struct ip_tunnel_encap_ops fou_iptun_ops = {
1195 1196
	.encap_hlen = fou_encap_hlen,
	.build_header = fou_build_header,
1197
	.err_handler = gue_err,
1198 1199
};

A
Andi Kleen 已提交
1200
static const struct ip_tunnel_encap_ops gue_iptun_ops = {
1201 1202
	.encap_hlen = gue_encap_hlen,
	.build_header = gue_build_header,
1203
	.err_handler = gue_err,
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
};

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;
}

1239
static void ip_tunnel_encap_del_fou_ops(void)
1240 1241 1242 1243 1244
{
}

#endif

W
WANG Cong 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
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),
};

1273 1274 1275 1276
static int __init fou_init(void)
{
	int ret;

W
WANG Cong 已提交
1277 1278 1279 1280
	ret = register_pernet_device(&fou_net_ops);
	if (ret)
		goto exit;

1281
	ret = genl_register_family(&fou_nl_family);
1282
	if (ret < 0)
W
WANG Cong 已提交
1283
		goto unregister;
1284 1285

	ret = ip_tunnel_encap_add_fou_ops();
W
WANG Cong 已提交
1286 1287
	if (ret == 0)
		return 0;
1288

W
WANG Cong 已提交
1289 1290 1291
	genl_unregister_family(&fou_nl_family);
unregister:
	unregister_pernet_device(&fou_net_ops);
1292
exit:
1293 1294 1295 1296 1297
	return ret;
}

static void __exit fou_fini(void)
{
1298
	ip_tunnel_encap_del_fou_ops();
1299
	genl_unregister_family(&fou_nl_family);
W
WANG Cong 已提交
1300
	unregister_pernet_device(&fou_net_ops);
1301 1302 1303 1304 1305 1306
}

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