xref: /openbmc/linux/net/ipv4/fou_core.c (revision 769218209d8f00a4003dc9ee351198a473043eaf)
108d32323SJakub Kicinski // SPDX-License-Identifier: GPL-2.0-only
208d32323SJakub Kicinski #include <linux/module.h>
308d32323SJakub Kicinski #include <linux/errno.h>
408d32323SJakub Kicinski #include <linux/socket.h>
508d32323SJakub Kicinski #include <linux/skbuff.h>
608d32323SJakub Kicinski #include <linux/ip.h>
708d32323SJakub Kicinski #include <linux/icmp.h>
808d32323SJakub Kicinski #include <linux/udp.h>
908d32323SJakub Kicinski #include <linux/types.h>
1008d32323SJakub Kicinski #include <linux/kernel.h>
1108d32323SJakub Kicinski #include <net/genetlink.h>
1208d32323SJakub Kicinski #include <net/gro.h>
1308d32323SJakub Kicinski #include <net/gue.h>
1408d32323SJakub Kicinski #include <net/fou.h>
1508d32323SJakub Kicinski #include <net/ip.h>
1608d32323SJakub Kicinski #include <net/protocol.h>
1708d32323SJakub Kicinski #include <net/udp.h>
1808d32323SJakub Kicinski #include <net/udp_tunnel.h>
1908d32323SJakub Kicinski #include <uapi/linux/fou.h>
2008d32323SJakub Kicinski #include <uapi/linux/genetlink.h>
2108d32323SJakub Kicinski 
221d562c32SJakub Kicinski #include "fou_nl.h"
231d562c32SJakub Kicinski 
2408d32323SJakub Kicinski struct fou {
2508d32323SJakub Kicinski 	struct socket *sock;
2608d32323SJakub Kicinski 	u8 protocol;
2708d32323SJakub Kicinski 	u8 flags;
2808d32323SJakub Kicinski 	__be16 port;
2908d32323SJakub Kicinski 	u8 family;
3008d32323SJakub Kicinski 	u16 type;
3108d32323SJakub Kicinski 	struct list_head list;
3208d32323SJakub Kicinski 	struct rcu_head rcu;
3308d32323SJakub Kicinski };
3408d32323SJakub Kicinski 
3508d32323SJakub Kicinski #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
3608d32323SJakub Kicinski 
3708d32323SJakub Kicinski struct fou_cfg {
3808d32323SJakub Kicinski 	u16 type;
3908d32323SJakub Kicinski 	u8 protocol;
4008d32323SJakub Kicinski 	u8 flags;
4108d32323SJakub Kicinski 	struct udp_port_cfg udp_config;
4208d32323SJakub Kicinski };
4308d32323SJakub Kicinski 
4408d32323SJakub Kicinski static unsigned int fou_net_id;
4508d32323SJakub Kicinski 
4608d32323SJakub Kicinski struct fou_net {
4708d32323SJakub Kicinski 	struct list_head fou_list;
4808d32323SJakub Kicinski 	struct mutex fou_lock;
4908d32323SJakub Kicinski };
5008d32323SJakub Kicinski 
fou_from_sock(struct sock * sk)5108d32323SJakub Kicinski static inline struct fou *fou_from_sock(struct sock *sk)
5208d32323SJakub Kicinski {
531df42be3SKuniyuki Iwashima 	return rcu_dereference_sk_user_data(sk);
5408d32323SJakub Kicinski }
5508d32323SJakub Kicinski 
fou_recv_pull(struct sk_buff * skb,struct fou * fou,size_t len)5608d32323SJakub Kicinski static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
5708d32323SJakub Kicinski {
5808d32323SJakub Kicinski 	/* Remove 'len' bytes from the packet (UDP header and
5908d32323SJakub Kicinski 	 * FOU header if present).
6008d32323SJakub Kicinski 	 */
6108d32323SJakub Kicinski 	if (fou->family == AF_INET)
6208d32323SJakub Kicinski 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
6308d32323SJakub Kicinski 	else
6408d32323SJakub Kicinski 		ipv6_hdr(skb)->payload_len =
6508d32323SJakub Kicinski 		    htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
6608d32323SJakub Kicinski 
6708d32323SJakub Kicinski 	__skb_pull(skb, len);
6808d32323SJakub Kicinski 	skb_postpull_rcsum(skb, udp_hdr(skb), len);
6908d32323SJakub Kicinski 	skb_reset_transport_header(skb);
7008d32323SJakub Kicinski 	return iptunnel_pull_offloads(skb);
7108d32323SJakub Kicinski }
7208d32323SJakub Kicinski 
fou_udp_recv(struct sock * sk,struct sk_buff * skb)7308d32323SJakub Kicinski static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
7408d32323SJakub Kicinski {
7508d32323SJakub Kicinski 	struct fou *fou = fou_from_sock(sk);
7608d32323SJakub Kicinski 
7708d32323SJakub Kicinski 	if (!fou)
7808d32323SJakub Kicinski 		return 1;
7908d32323SJakub Kicinski 
8008d32323SJakub Kicinski 	if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
8108d32323SJakub Kicinski 		goto drop;
8208d32323SJakub Kicinski 
8308d32323SJakub Kicinski 	return -fou->protocol;
8408d32323SJakub Kicinski 
8508d32323SJakub Kicinski drop:
8608d32323SJakub Kicinski 	kfree_skb(skb);
8708d32323SJakub Kicinski 	return 0;
8808d32323SJakub Kicinski }
8908d32323SJakub Kicinski 
gue_remcsum(struct sk_buff * skb,struct guehdr * guehdr,void * data,size_t hdrlen,u8 ipproto,bool nopartial)9008d32323SJakub Kicinski static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
9108d32323SJakub Kicinski 				  void *data, size_t hdrlen, u8 ipproto,
9208d32323SJakub Kicinski 				  bool nopartial)
9308d32323SJakub Kicinski {
9408d32323SJakub Kicinski 	__be16 *pd = data;
9508d32323SJakub Kicinski 	size_t start = ntohs(pd[0]);
9608d32323SJakub Kicinski 	size_t offset = ntohs(pd[1]);
9708d32323SJakub Kicinski 	size_t plen = sizeof(struct udphdr) + hdrlen +
9808d32323SJakub Kicinski 	    max_t(size_t, offset + sizeof(u16), start);
9908d32323SJakub Kicinski 
10008d32323SJakub Kicinski 	if (skb->remcsum_offload)
10108d32323SJakub Kicinski 		return guehdr;
10208d32323SJakub Kicinski 
10308d32323SJakub Kicinski 	if (!pskb_may_pull(skb, plen))
10408d32323SJakub Kicinski 		return NULL;
10508d32323SJakub Kicinski 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
10608d32323SJakub Kicinski 
10708d32323SJakub Kicinski 	skb_remcsum_process(skb, (void *)guehdr + hdrlen,
10808d32323SJakub Kicinski 			    start, offset, nopartial);
10908d32323SJakub Kicinski 
11008d32323SJakub Kicinski 	return guehdr;
11108d32323SJakub Kicinski }
11208d32323SJakub Kicinski 
gue_control_message(struct sk_buff * skb,struct guehdr * guehdr)11308d32323SJakub Kicinski static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
11408d32323SJakub Kicinski {
11508d32323SJakub Kicinski 	/* No support yet */
11608d32323SJakub Kicinski 	kfree_skb(skb);
11708d32323SJakub Kicinski 	return 0;
11808d32323SJakub Kicinski }
11908d32323SJakub Kicinski 
gue_udp_recv(struct sock * sk,struct sk_buff * skb)12008d32323SJakub Kicinski static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
12108d32323SJakub Kicinski {
12208d32323SJakub Kicinski 	struct fou *fou = fou_from_sock(sk);
12308d32323SJakub Kicinski 	size_t len, optlen, hdrlen;
12408d32323SJakub Kicinski 	struct guehdr *guehdr;
12508d32323SJakub Kicinski 	void *data;
12608d32323SJakub Kicinski 	u16 doffset = 0;
12708d32323SJakub Kicinski 	u8 proto_ctype;
12808d32323SJakub Kicinski 
12908d32323SJakub Kicinski 	if (!fou)
13008d32323SJakub Kicinski 		return 1;
13108d32323SJakub Kicinski 
13208d32323SJakub Kicinski 	len = sizeof(struct udphdr) + sizeof(struct guehdr);
13308d32323SJakub Kicinski 	if (!pskb_may_pull(skb, len))
13408d32323SJakub Kicinski 		goto drop;
13508d32323SJakub Kicinski 
13608d32323SJakub Kicinski 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
13708d32323SJakub Kicinski 
13808d32323SJakub Kicinski 	switch (guehdr->version) {
13908d32323SJakub Kicinski 	case 0: /* Full GUE header present */
14008d32323SJakub Kicinski 		break;
14108d32323SJakub Kicinski 
14208d32323SJakub Kicinski 	case 1: {
14308d32323SJakub Kicinski 		/* Direct encapsulation of IPv4 or IPv6 */
14408d32323SJakub Kicinski 
14508d32323SJakub Kicinski 		int prot;
14608d32323SJakub Kicinski 
14708d32323SJakub Kicinski 		switch (((struct iphdr *)guehdr)->version) {
14808d32323SJakub Kicinski 		case 4:
14908d32323SJakub Kicinski 			prot = IPPROTO_IPIP;
15008d32323SJakub Kicinski 			break;
15108d32323SJakub Kicinski 		case 6:
15208d32323SJakub Kicinski 			prot = IPPROTO_IPV6;
15308d32323SJakub Kicinski 			break;
15408d32323SJakub Kicinski 		default:
15508d32323SJakub Kicinski 			goto drop;
15608d32323SJakub Kicinski 		}
15708d32323SJakub Kicinski 
15808d32323SJakub Kicinski 		if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
15908d32323SJakub Kicinski 			goto drop;
16008d32323SJakub Kicinski 
16108d32323SJakub Kicinski 		return -prot;
16208d32323SJakub Kicinski 	}
16308d32323SJakub Kicinski 
16408d32323SJakub Kicinski 	default: /* Undefined version */
16508d32323SJakub Kicinski 		goto drop;
16608d32323SJakub Kicinski 	}
16708d32323SJakub Kicinski 
16808d32323SJakub Kicinski 	optlen = guehdr->hlen << 2;
16908d32323SJakub Kicinski 	len += optlen;
17008d32323SJakub Kicinski 
17108d32323SJakub Kicinski 	if (!pskb_may_pull(skb, len))
17208d32323SJakub Kicinski 		goto drop;
17308d32323SJakub Kicinski 
17408d32323SJakub Kicinski 	/* guehdr may change after pull */
17508d32323SJakub Kicinski 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
17608d32323SJakub Kicinski 
17708d32323SJakub Kicinski 	if (validate_gue_flags(guehdr, optlen))
17808d32323SJakub Kicinski 		goto drop;
17908d32323SJakub Kicinski 
18008d32323SJakub Kicinski 	hdrlen = sizeof(struct guehdr) + optlen;
18108d32323SJakub Kicinski 
18208d32323SJakub Kicinski 	if (fou->family == AF_INET)
18308d32323SJakub Kicinski 		ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
18408d32323SJakub Kicinski 	else
18508d32323SJakub Kicinski 		ipv6_hdr(skb)->payload_len =
18608d32323SJakub Kicinski 		    htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
18708d32323SJakub Kicinski 
18808d32323SJakub Kicinski 	/* Pull csum through the guehdr now . This can be used if
18908d32323SJakub Kicinski 	 * there is a remote checksum offload.
19008d32323SJakub Kicinski 	 */
19108d32323SJakub Kicinski 	skb_postpull_rcsum(skb, udp_hdr(skb), len);
19208d32323SJakub Kicinski 
19308d32323SJakub Kicinski 	data = &guehdr[1];
19408d32323SJakub Kicinski 
19508d32323SJakub Kicinski 	if (guehdr->flags & GUE_FLAG_PRIV) {
19608d32323SJakub Kicinski 		__be32 flags = *(__be32 *)(data + doffset);
19708d32323SJakub Kicinski 
19808d32323SJakub Kicinski 		doffset += GUE_LEN_PRIV;
19908d32323SJakub Kicinski 
20008d32323SJakub Kicinski 		if (flags & GUE_PFLAG_REMCSUM) {
20108d32323SJakub Kicinski 			guehdr = gue_remcsum(skb, guehdr, data + doffset,
20208d32323SJakub Kicinski 					     hdrlen, guehdr->proto_ctype,
20308d32323SJakub Kicinski 					     !!(fou->flags &
20408d32323SJakub Kicinski 						FOU_F_REMCSUM_NOPARTIAL));
20508d32323SJakub Kicinski 			if (!guehdr)
20608d32323SJakub Kicinski 				goto drop;
20708d32323SJakub Kicinski 
20808d32323SJakub Kicinski 			data = &guehdr[1];
20908d32323SJakub Kicinski 
21008d32323SJakub Kicinski 			doffset += GUE_PLEN_REMCSUM;
21108d32323SJakub Kicinski 		}
21208d32323SJakub Kicinski 	}
21308d32323SJakub Kicinski 
21408d32323SJakub Kicinski 	if (unlikely(guehdr->control))
21508d32323SJakub Kicinski 		return gue_control_message(skb, guehdr);
21608d32323SJakub Kicinski 
21708d32323SJakub Kicinski 	proto_ctype = guehdr->proto_ctype;
21808d32323SJakub Kicinski 	__skb_pull(skb, sizeof(struct udphdr) + hdrlen);
21908d32323SJakub Kicinski 	skb_reset_transport_header(skb);
22008d32323SJakub Kicinski 
22108d32323SJakub Kicinski 	if (iptunnel_pull_offloads(skb))
22208d32323SJakub Kicinski 		goto drop;
22308d32323SJakub Kicinski 
22408d32323SJakub Kicinski 	return -proto_ctype;
22508d32323SJakub Kicinski 
22608d32323SJakub Kicinski drop:
22708d32323SJakub Kicinski 	kfree_skb(skb);
22808d32323SJakub Kicinski 	return 0;
22908d32323SJakub Kicinski }
23008d32323SJakub Kicinski 
fou_gro_receive(struct sock * sk,struct list_head * head,struct sk_buff * skb)23108d32323SJakub Kicinski static struct sk_buff *fou_gro_receive(struct sock *sk,
23208d32323SJakub Kicinski 				       struct list_head *head,
23308d32323SJakub Kicinski 				       struct sk_buff *skb)
23408d32323SJakub Kicinski {
23508d32323SJakub Kicinski 	const struct net_offload __rcu **offloads;
2361df42be3SKuniyuki Iwashima 	struct fou *fou = fou_from_sock(sk);
23708d32323SJakub Kicinski 	const struct net_offload *ops;
23808d32323SJakub Kicinski 	struct sk_buff *pp = NULL;
2391df42be3SKuniyuki Iwashima 	u8 proto;
2401df42be3SKuniyuki Iwashima 
2411df42be3SKuniyuki Iwashima 	if (!fou)
2421df42be3SKuniyuki Iwashima 		goto out;
2431df42be3SKuniyuki Iwashima 
2441df42be3SKuniyuki Iwashima 	proto = fou->protocol;
24508d32323SJakub Kicinski 
24608d32323SJakub Kicinski 	/* We can clear the encap_mark for FOU as we are essentially doing
24708d32323SJakub Kicinski 	 * one of two possible things.  We are either adding an L4 tunnel
24808d32323SJakub Kicinski 	 * header to the outer L3 tunnel header, or we are simply
24908d32323SJakub Kicinski 	 * treating the GRE tunnel header as though it is a UDP protocol
25008d32323SJakub Kicinski 	 * specific header such as VXLAN or GENEVE.
25108d32323SJakub Kicinski 	 */
25208d32323SJakub Kicinski 	NAPI_GRO_CB(skb)->encap_mark = 0;
25308d32323SJakub Kicinski 
25408d32323SJakub Kicinski 	/* Flag this frame as already having an outer encap header */
25508d32323SJakub Kicinski 	NAPI_GRO_CB(skb)->is_fou = 1;
25608d32323SJakub Kicinski 
25708d32323SJakub Kicinski 	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
25808d32323SJakub Kicinski 	ops = rcu_dereference(offloads[proto]);
25908d32323SJakub Kicinski 	if (!ops || !ops->callbacks.gro_receive)
26008d32323SJakub Kicinski 		goto out;
26108d32323SJakub Kicinski 
26208d32323SJakub Kicinski 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
26308d32323SJakub Kicinski 
26408d32323SJakub Kicinski out:
26508d32323SJakub Kicinski 	return pp;
26608d32323SJakub Kicinski }
26708d32323SJakub Kicinski 
fou_gro_complete(struct sock * sk,struct sk_buff * skb,int nhoff)26808d32323SJakub Kicinski static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
26908d32323SJakub Kicinski 			    int nhoff)
27008d32323SJakub Kicinski {
27108d32323SJakub Kicinski 	const struct net_offload __rcu **offloads;
2721df42be3SKuniyuki Iwashima 	struct fou *fou = fou_from_sock(sk);
27308d32323SJakub Kicinski 	const struct net_offload *ops;
2741df42be3SKuniyuki Iwashima 	u8 proto;
2751df42be3SKuniyuki Iwashima 	int err;
2761df42be3SKuniyuki Iwashima 
2771df42be3SKuniyuki Iwashima 	if (!fou) {
2781df42be3SKuniyuki Iwashima 		err = -ENOENT;
2791df42be3SKuniyuki Iwashima 		goto out;
2801df42be3SKuniyuki Iwashima 	}
2811df42be3SKuniyuki Iwashima 
2821df42be3SKuniyuki Iwashima 	proto = fou->protocol;
28308d32323SJakub Kicinski 
28408d32323SJakub Kicinski 	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
28508d32323SJakub Kicinski 	ops = rcu_dereference(offloads[proto]);
2861df42be3SKuniyuki Iwashima 	if (WARN_ON(!ops || !ops->callbacks.gro_complete)) {
2871df42be3SKuniyuki Iwashima 		err = -ENOSYS;
28808d32323SJakub Kicinski 		goto out;
2891df42be3SKuniyuki Iwashima 	}
29008d32323SJakub Kicinski 
29108d32323SJakub Kicinski 	err = ops->callbacks.gro_complete(skb, nhoff);
29208d32323SJakub Kicinski 
29308d32323SJakub Kicinski 	skb_set_inner_mac_header(skb, nhoff);
29408d32323SJakub Kicinski 
29508d32323SJakub Kicinski out:
29608d32323SJakub Kicinski 	return err;
29708d32323SJakub Kicinski }
29808d32323SJakub Kicinski 
gue_gro_remcsum(struct sk_buff * skb,unsigned int off,struct guehdr * guehdr,void * data,size_t hdrlen,struct gro_remcsum * grc,bool nopartial)29908d32323SJakub Kicinski static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
30008d32323SJakub Kicinski 				      struct guehdr *guehdr, void *data,
30108d32323SJakub Kicinski 				      size_t hdrlen, struct gro_remcsum *grc,
30208d32323SJakub Kicinski 				      bool nopartial)
30308d32323SJakub Kicinski {
30408d32323SJakub Kicinski 	__be16 *pd = data;
30508d32323SJakub Kicinski 	size_t start = ntohs(pd[0]);
30608d32323SJakub Kicinski 	size_t offset = ntohs(pd[1]);
30708d32323SJakub Kicinski 
30808d32323SJakub Kicinski 	if (skb->remcsum_offload)
30908d32323SJakub Kicinski 		return guehdr;
31008d32323SJakub Kicinski 
31108d32323SJakub Kicinski 	if (!NAPI_GRO_CB(skb)->csum_valid)
31208d32323SJakub Kicinski 		return NULL;
31308d32323SJakub Kicinski 
31408d32323SJakub Kicinski 	guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
31508d32323SJakub Kicinski 					 start, offset, grc, nopartial);
31608d32323SJakub Kicinski 
31708d32323SJakub Kicinski 	skb->remcsum_offload = 1;
31808d32323SJakub Kicinski 
31908d32323SJakub Kicinski 	return guehdr;
32008d32323SJakub Kicinski }
32108d32323SJakub Kicinski 
gue_gro_receive(struct sock * sk,struct list_head * head,struct sk_buff * skb)32208d32323SJakub Kicinski static struct sk_buff *gue_gro_receive(struct sock *sk,
32308d32323SJakub Kicinski 				       struct list_head *head,
32408d32323SJakub Kicinski 				       struct sk_buff *skb)
32508d32323SJakub Kicinski {
32608d32323SJakub Kicinski 	const struct net_offload __rcu **offloads;
32708d32323SJakub Kicinski 	const struct net_offload *ops;
32808d32323SJakub Kicinski 	struct sk_buff *pp = NULL;
32908d32323SJakub Kicinski 	struct sk_buff *p;
33008d32323SJakub Kicinski 	struct guehdr *guehdr;
33108d32323SJakub Kicinski 	size_t len, optlen, hdrlen, off;
33208d32323SJakub Kicinski 	void *data;
33308d32323SJakub Kicinski 	u16 doffset = 0;
33408d32323SJakub Kicinski 	int flush = 1;
33508d32323SJakub Kicinski 	struct fou *fou = fou_from_sock(sk);
33608d32323SJakub Kicinski 	struct gro_remcsum grc;
33708d32323SJakub Kicinski 	u8 proto;
33808d32323SJakub Kicinski 
339*7ae890eeSMuhammad Usama Anjum 	skb_gro_remcsum_init(&grc);
340*7ae890eeSMuhammad Usama Anjum 
3411df42be3SKuniyuki Iwashima 	if (!fou)
3421df42be3SKuniyuki Iwashima 		goto out;
3431df42be3SKuniyuki Iwashima 
34408d32323SJakub Kicinski 	off = skb_gro_offset(skb);
34508d32323SJakub Kicinski 	len = off + sizeof(*guehdr);
34608d32323SJakub Kicinski 
34708d32323SJakub Kicinski 	guehdr = skb_gro_header(skb, len, off);
34808d32323SJakub Kicinski 	if (unlikely(!guehdr))
34908d32323SJakub Kicinski 		goto out;
35008d32323SJakub Kicinski 
35108d32323SJakub Kicinski 	switch (guehdr->version) {
35208d32323SJakub Kicinski 	case 0:
35308d32323SJakub Kicinski 		break;
35408d32323SJakub Kicinski 	case 1:
35508d32323SJakub Kicinski 		switch (((struct iphdr *)guehdr)->version) {
35608d32323SJakub Kicinski 		case 4:
35708d32323SJakub Kicinski 			proto = IPPROTO_IPIP;
35808d32323SJakub Kicinski 			break;
35908d32323SJakub Kicinski 		case 6:
36008d32323SJakub Kicinski 			proto = IPPROTO_IPV6;
36108d32323SJakub Kicinski 			break;
36208d32323SJakub Kicinski 		default:
36308d32323SJakub Kicinski 			goto out;
36408d32323SJakub Kicinski 		}
36508d32323SJakub Kicinski 		goto next_proto;
36608d32323SJakub Kicinski 	default:
36708d32323SJakub Kicinski 		goto out;
36808d32323SJakub Kicinski 	}
36908d32323SJakub Kicinski 
37008d32323SJakub Kicinski 	optlen = guehdr->hlen << 2;
37108d32323SJakub Kicinski 	len += optlen;
37208d32323SJakub Kicinski 
37308d32323SJakub Kicinski 	if (skb_gro_header_hard(skb, len)) {
37408d32323SJakub Kicinski 		guehdr = skb_gro_header_slow(skb, len, off);
37508d32323SJakub Kicinski 		if (unlikely(!guehdr))
37608d32323SJakub Kicinski 			goto out;
37708d32323SJakub Kicinski 	}
37808d32323SJakub Kicinski 
37908d32323SJakub Kicinski 	if (unlikely(guehdr->control) || guehdr->version != 0 ||
38008d32323SJakub Kicinski 	    validate_gue_flags(guehdr, optlen))
38108d32323SJakub Kicinski 		goto out;
38208d32323SJakub Kicinski 
38308d32323SJakub Kicinski 	hdrlen = sizeof(*guehdr) + optlen;
38408d32323SJakub Kicinski 
38508d32323SJakub Kicinski 	/* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
38608d32323SJakub Kicinski 	 * this is needed if there is a remote checkcsum offload.
38708d32323SJakub Kicinski 	 */
38808d32323SJakub Kicinski 	skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
38908d32323SJakub Kicinski 
39008d32323SJakub Kicinski 	data = &guehdr[1];
39108d32323SJakub Kicinski 
39208d32323SJakub Kicinski 	if (guehdr->flags & GUE_FLAG_PRIV) {
39308d32323SJakub Kicinski 		__be32 flags = *(__be32 *)(data + doffset);
39408d32323SJakub Kicinski 
39508d32323SJakub Kicinski 		doffset += GUE_LEN_PRIV;
39608d32323SJakub Kicinski 
39708d32323SJakub Kicinski 		if (flags & GUE_PFLAG_REMCSUM) {
39808d32323SJakub Kicinski 			guehdr = gue_gro_remcsum(skb, off, guehdr,
39908d32323SJakub Kicinski 						 data + doffset, hdrlen, &grc,
40008d32323SJakub Kicinski 						 !!(fou->flags &
40108d32323SJakub Kicinski 						    FOU_F_REMCSUM_NOPARTIAL));
40208d32323SJakub Kicinski 
40308d32323SJakub Kicinski 			if (!guehdr)
40408d32323SJakub Kicinski 				goto out;
40508d32323SJakub Kicinski 
40608d32323SJakub Kicinski 			data = &guehdr[1];
40708d32323SJakub Kicinski 
40808d32323SJakub Kicinski 			doffset += GUE_PLEN_REMCSUM;
40908d32323SJakub Kicinski 		}
41008d32323SJakub Kicinski 	}
41108d32323SJakub Kicinski 
41208d32323SJakub Kicinski 	skb_gro_pull(skb, hdrlen);
41308d32323SJakub Kicinski 
41408d32323SJakub Kicinski 	list_for_each_entry(p, head, list) {
41508d32323SJakub Kicinski 		const struct guehdr *guehdr2;
41608d32323SJakub Kicinski 
41708d32323SJakub Kicinski 		if (!NAPI_GRO_CB(p)->same_flow)
41808d32323SJakub Kicinski 			continue;
41908d32323SJakub Kicinski 
42008d32323SJakub Kicinski 		guehdr2 = (struct guehdr *)(p->data + off);
42108d32323SJakub Kicinski 
42208d32323SJakub Kicinski 		/* Compare base GUE header to be equal (covers
42308d32323SJakub Kicinski 		 * hlen, version, proto_ctype, and flags.
42408d32323SJakub Kicinski 		 */
42508d32323SJakub Kicinski 		if (guehdr->word != guehdr2->word) {
42608d32323SJakub Kicinski 			NAPI_GRO_CB(p)->same_flow = 0;
42708d32323SJakub Kicinski 			continue;
42808d32323SJakub Kicinski 		}
42908d32323SJakub Kicinski 
43008d32323SJakub Kicinski 		/* Compare optional fields are the same. */
43108d32323SJakub Kicinski 		if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
43208d32323SJakub Kicinski 					   guehdr->hlen << 2)) {
43308d32323SJakub Kicinski 			NAPI_GRO_CB(p)->same_flow = 0;
43408d32323SJakub Kicinski 			continue;
43508d32323SJakub Kicinski 		}
43608d32323SJakub Kicinski 	}
43708d32323SJakub Kicinski 
43808d32323SJakub Kicinski 	proto = guehdr->proto_ctype;
43908d32323SJakub Kicinski 
44008d32323SJakub Kicinski next_proto:
44108d32323SJakub Kicinski 
44208d32323SJakub Kicinski 	/* We can clear the encap_mark for GUE as we are essentially doing
44308d32323SJakub Kicinski 	 * one of two possible things.  We are either adding an L4 tunnel
44408d32323SJakub Kicinski 	 * header to the outer L3 tunnel header, or we are simply
44508d32323SJakub Kicinski 	 * treating the GRE tunnel header as though it is a UDP protocol
44608d32323SJakub Kicinski 	 * specific header such as VXLAN or GENEVE.
44708d32323SJakub Kicinski 	 */
44808d32323SJakub Kicinski 	NAPI_GRO_CB(skb)->encap_mark = 0;
44908d32323SJakub Kicinski 
45008d32323SJakub Kicinski 	/* Flag this frame as already having an outer encap header */
45108d32323SJakub Kicinski 	NAPI_GRO_CB(skb)->is_fou = 1;
45208d32323SJakub Kicinski 
45308d32323SJakub Kicinski 	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
45408d32323SJakub Kicinski 	ops = rcu_dereference(offloads[proto]);
4553db43953SWillem de Bruijn 	if (!ops || !ops->callbacks.gro_receive)
45608d32323SJakub Kicinski 		goto out;
45708d32323SJakub Kicinski 
45808d32323SJakub Kicinski 	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
45908d32323SJakub Kicinski 	flush = 0;
46008d32323SJakub Kicinski 
46108d32323SJakub Kicinski out:
46208d32323SJakub Kicinski 	skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
46308d32323SJakub Kicinski 
46408d32323SJakub Kicinski 	return pp;
46508d32323SJakub Kicinski }
46608d32323SJakub Kicinski 
gue_gro_complete(struct sock * sk,struct sk_buff * skb,int nhoff)46708d32323SJakub Kicinski static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
46808d32323SJakub Kicinski {
46908d32323SJakub Kicinski 	struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
47008d32323SJakub Kicinski 	const struct net_offload __rcu **offloads;
47108d32323SJakub Kicinski 	const struct net_offload *ops;
47208d32323SJakub Kicinski 	unsigned int guehlen = 0;
47308d32323SJakub Kicinski 	u8 proto;
47408d32323SJakub Kicinski 	int err = -ENOENT;
47508d32323SJakub Kicinski 
47608d32323SJakub Kicinski 	switch (guehdr->version) {
47708d32323SJakub Kicinski 	case 0:
47808d32323SJakub Kicinski 		proto = guehdr->proto_ctype;
47908d32323SJakub Kicinski 		guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
48008d32323SJakub Kicinski 		break;
48108d32323SJakub Kicinski 	case 1:
48208d32323SJakub Kicinski 		switch (((struct iphdr *)guehdr)->version) {
48308d32323SJakub Kicinski 		case 4:
48408d32323SJakub Kicinski 			proto = IPPROTO_IPIP;
48508d32323SJakub Kicinski 			break;
48608d32323SJakub Kicinski 		case 6:
48708d32323SJakub Kicinski 			proto = IPPROTO_IPV6;
48808d32323SJakub Kicinski 			break;
48908d32323SJakub Kicinski 		default:
49008d32323SJakub Kicinski 			return err;
49108d32323SJakub Kicinski 		}
49208d32323SJakub Kicinski 		break;
49308d32323SJakub Kicinski 	default:
49408d32323SJakub Kicinski 		return err;
49508d32323SJakub Kicinski 	}
49608d32323SJakub Kicinski 
49708d32323SJakub Kicinski 	offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
49808d32323SJakub Kicinski 	ops = rcu_dereference(offloads[proto]);
49908d32323SJakub Kicinski 	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
50008d32323SJakub Kicinski 		goto out;
50108d32323SJakub Kicinski 
50208d32323SJakub Kicinski 	err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
50308d32323SJakub Kicinski 
50408d32323SJakub Kicinski 	skb_set_inner_mac_header(skb, nhoff + guehlen);
50508d32323SJakub Kicinski 
50608d32323SJakub Kicinski out:
50708d32323SJakub Kicinski 	return err;
50808d32323SJakub Kicinski }
50908d32323SJakub Kicinski 
fou_cfg_cmp(struct fou * fou,struct fou_cfg * cfg)51008d32323SJakub Kicinski static bool fou_cfg_cmp(struct fou *fou, struct fou_cfg *cfg)
51108d32323SJakub Kicinski {
51208d32323SJakub Kicinski 	struct sock *sk = fou->sock->sk;
51308d32323SJakub Kicinski 	struct udp_port_cfg *udp_cfg = &cfg->udp_config;
51408d32323SJakub Kicinski 
51508d32323SJakub Kicinski 	if (fou->family != udp_cfg->family ||
51608d32323SJakub Kicinski 	    fou->port != udp_cfg->local_udp_port ||
51708d32323SJakub Kicinski 	    sk->sk_dport != udp_cfg->peer_udp_port ||
51808d32323SJakub Kicinski 	    sk->sk_bound_dev_if != udp_cfg->bind_ifindex)
51908d32323SJakub Kicinski 		return false;
52008d32323SJakub Kicinski 
52108d32323SJakub Kicinski 	if (fou->family == AF_INET) {
52208d32323SJakub Kicinski 		if (sk->sk_rcv_saddr != udp_cfg->local_ip.s_addr ||
52308d32323SJakub Kicinski 		    sk->sk_daddr != udp_cfg->peer_ip.s_addr)
52408d32323SJakub Kicinski 			return false;
52508d32323SJakub Kicinski 		else
52608d32323SJakub Kicinski 			return true;
52708d32323SJakub Kicinski #if IS_ENABLED(CONFIG_IPV6)
52808d32323SJakub Kicinski 	} else {
52908d32323SJakub Kicinski 		if (ipv6_addr_cmp(&sk->sk_v6_rcv_saddr, &udp_cfg->local_ip6) ||
53008d32323SJakub Kicinski 		    ipv6_addr_cmp(&sk->sk_v6_daddr, &udp_cfg->peer_ip6))
53108d32323SJakub Kicinski 			return false;
53208d32323SJakub Kicinski 		else
53308d32323SJakub Kicinski 			return true;
53408d32323SJakub Kicinski #endif
53508d32323SJakub Kicinski 	}
53608d32323SJakub Kicinski 
53708d32323SJakub Kicinski 	return false;
53808d32323SJakub Kicinski }
53908d32323SJakub Kicinski 
fou_add_to_port_list(struct net * net,struct fou * fou,struct fou_cfg * cfg)54008d32323SJakub Kicinski static int fou_add_to_port_list(struct net *net, struct fou *fou,
54108d32323SJakub Kicinski 				struct fou_cfg *cfg)
54208d32323SJakub Kicinski {
54308d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
54408d32323SJakub Kicinski 	struct fou *fout;
54508d32323SJakub Kicinski 
54608d32323SJakub Kicinski 	mutex_lock(&fn->fou_lock);
54708d32323SJakub Kicinski 	list_for_each_entry(fout, &fn->fou_list, list) {
54808d32323SJakub Kicinski 		if (fou_cfg_cmp(fout, cfg)) {
54908d32323SJakub Kicinski 			mutex_unlock(&fn->fou_lock);
55008d32323SJakub Kicinski 			return -EALREADY;
55108d32323SJakub Kicinski 		}
55208d32323SJakub Kicinski 	}
55308d32323SJakub Kicinski 
55408d32323SJakub Kicinski 	list_add(&fou->list, &fn->fou_list);
55508d32323SJakub Kicinski 	mutex_unlock(&fn->fou_lock);
55608d32323SJakub Kicinski 
55708d32323SJakub Kicinski 	return 0;
55808d32323SJakub Kicinski }
55908d32323SJakub Kicinski 
fou_release(struct fou * fou)56008d32323SJakub Kicinski static void fou_release(struct fou *fou)
56108d32323SJakub Kicinski {
56208d32323SJakub Kicinski 	struct socket *sock = fou->sock;
56308d32323SJakub Kicinski 
56408d32323SJakub Kicinski 	list_del(&fou->list);
56508d32323SJakub Kicinski 	udp_tunnel_sock_release(sock);
56608d32323SJakub Kicinski 
56708d32323SJakub Kicinski 	kfree_rcu(fou, rcu);
56808d32323SJakub Kicinski }
56908d32323SJakub Kicinski 
fou_create(struct net * net,struct fou_cfg * cfg,struct socket ** sockp)57008d32323SJakub Kicinski static int fou_create(struct net *net, struct fou_cfg *cfg,
57108d32323SJakub Kicinski 		      struct socket **sockp)
57208d32323SJakub Kicinski {
57308d32323SJakub Kicinski 	struct socket *sock = NULL;
57408d32323SJakub Kicinski 	struct fou *fou = NULL;
57508d32323SJakub Kicinski 	struct sock *sk;
57608d32323SJakub Kicinski 	struct udp_tunnel_sock_cfg tunnel_cfg;
57708d32323SJakub Kicinski 	int err;
57808d32323SJakub Kicinski 
57908d32323SJakub Kicinski 	/* Open UDP socket */
58008d32323SJakub Kicinski 	err = udp_sock_create(net, &cfg->udp_config, &sock);
58108d32323SJakub Kicinski 	if (err < 0)
58208d32323SJakub Kicinski 		goto error;
58308d32323SJakub Kicinski 
58408d32323SJakub Kicinski 	/* Allocate FOU port structure */
58508d32323SJakub Kicinski 	fou = kzalloc(sizeof(*fou), GFP_KERNEL);
58608d32323SJakub Kicinski 	if (!fou) {
58708d32323SJakub Kicinski 		err = -ENOMEM;
58808d32323SJakub Kicinski 		goto error;
58908d32323SJakub Kicinski 	}
59008d32323SJakub Kicinski 
59108d32323SJakub Kicinski 	sk = sock->sk;
59208d32323SJakub Kicinski 
59308d32323SJakub Kicinski 	fou->port = cfg->udp_config.local_udp_port;
59408d32323SJakub Kicinski 	fou->family = cfg->udp_config.family;
59508d32323SJakub Kicinski 	fou->flags = cfg->flags;
59608d32323SJakub Kicinski 	fou->type = cfg->type;
59708d32323SJakub Kicinski 	fou->sock = sock;
59808d32323SJakub Kicinski 
59908d32323SJakub Kicinski 	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
60008d32323SJakub Kicinski 	tunnel_cfg.encap_type = 1;
60108d32323SJakub Kicinski 	tunnel_cfg.sk_user_data = fou;
60208d32323SJakub Kicinski 	tunnel_cfg.encap_destroy = NULL;
60308d32323SJakub Kicinski 
60408d32323SJakub Kicinski 	/* Initial for fou type */
60508d32323SJakub Kicinski 	switch (cfg->type) {
60608d32323SJakub Kicinski 	case FOU_ENCAP_DIRECT:
60708d32323SJakub Kicinski 		tunnel_cfg.encap_rcv = fou_udp_recv;
60808d32323SJakub Kicinski 		tunnel_cfg.gro_receive = fou_gro_receive;
60908d32323SJakub Kicinski 		tunnel_cfg.gro_complete = fou_gro_complete;
61008d32323SJakub Kicinski 		fou->protocol = cfg->protocol;
61108d32323SJakub Kicinski 		break;
61208d32323SJakub Kicinski 	case FOU_ENCAP_GUE:
61308d32323SJakub Kicinski 		tunnel_cfg.encap_rcv = gue_udp_recv;
61408d32323SJakub Kicinski 		tunnel_cfg.gro_receive = gue_gro_receive;
61508d32323SJakub Kicinski 		tunnel_cfg.gro_complete = gue_gro_complete;
61608d32323SJakub Kicinski 		break;
61708d32323SJakub Kicinski 	default:
61808d32323SJakub Kicinski 		err = -EINVAL;
61908d32323SJakub Kicinski 		goto error;
62008d32323SJakub Kicinski 	}
62108d32323SJakub Kicinski 
62208d32323SJakub Kicinski 	setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
62308d32323SJakub Kicinski 
62408d32323SJakub Kicinski 	sk->sk_allocation = GFP_ATOMIC;
62508d32323SJakub Kicinski 
62608d32323SJakub Kicinski 	err = fou_add_to_port_list(net, fou, cfg);
62708d32323SJakub Kicinski 	if (err)
62808d32323SJakub Kicinski 		goto error;
62908d32323SJakub Kicinski 
63008d32323SJakub Kicinski 	if (sockp)
63108d32323SJakub Kicinski 		*sockp = sock;
63208d32323SJakub Kicinski 
63308d32323SJakub Kicinski 	return 0;
63408d32323SJakub Kicinski 
63508d32323SJakub Kicinski error:
63608d32323SJakub Kicinski 	kfree(fou);
63708d32323SJakub Kicinski 	if (sock)
63808d32323SJakub Kicinski 		udp_tunnel_sock_release(sock);
63908d32323SJakub Kicinski 
64008d32323SJakub Kicinski 	return err;
64108d32323SJakub Kicinski }
64208d32323SJakub Kicinski 
fou_destroy(struct net * net,struct fou_cfg * cfg)64308d32323SJakub Kicinski static int fou_destroy(struct net *net, struct fou_cfg *cfg)
64408d32323SJakub Kicinski {
64508d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
64608d32323SJakub Kicinski 	int err = -EINVAL;
64708d32323SJakub Kicinski 	struct fou *fou;
64808d32323SJakub Kicinski 
64908d32323SJakub Kicinski 	mutex_lock(&fn->fou_lock);
65008d32323SJakub Kicinski 	list_for_each_entry(fou, &fn->fou_list, list) {
65108d32323SJakub Kicinski 		if (fou_cfg_cmp(fou, cfg)) {
65208d32323SJakub Kicinski 			fou_release(fou);
65308d32323SJakub Kicinski 			err = 0;
65408d32323SJakub Kicinski 			break;
65508d32323SJakub Kicinski 		}
65608d32323SJakub Kicinski 	}
65708d32323SJakub Kicinski 	mutex_unlock(&fn->fou_lock);
65808d32323SJakub Kicinski 
65908d32323SJakub Kicinski 	return err;
66008d32323SJakub Kicinski }
66108d32323SJakub Kicinski 
66208d32323SJakub Kicinski static struct genl_family fou_nl_family;
66308d32323SJakub Kicinski 
parse_nl_config(struct genl_info * info,struct fou_cfg * cfg)66408d32323SJakub Kicinski static int parse_nl_config(struct genl_info *info,
66508d32323SJakub Kicinski 			   struct fou_cfg *cfg)
66608d32323SJakub Kicinski {
66708d32323SJakub Kicinski 	bool has_local = false, has_peer = false;
66808d32323SJakub Kicinski 	struct nlattr *attr;
66908d32323SJakub Kicinski 	int ifindex;
67008d32323SJakub Kicinski 	__be16 port;
67108d32323SJakub Kicinski 
67208d32323SJakub Kicinski 	memset(cfg, 0, sizeof(*cfg));
67308d32323SJakub Kicinski 
67408d32323SJakub Kicinski 	cfg->udp_config.family = AF_INET;
67508d32323SJakub Kicinski 
67608d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_AF]) {
67708d32323SJakub Kicinski 		u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
67808d32323SJakub Kicinski 
67908d32323SJakub Kicinski 		switch (family) {
68008d32323SJakub Kicinski 		case AF_INET:
68108d32323SJakub Kicinski 			break;
68208d32323SJakub Kicinski 		case AF_INET6:
68308d32323SJakub Kicinski 			cfg->udp_config.ipv6_v6only = 1;
68408d32323SJakub Kicinski 			break;
68508d32323SJakub Kicinski 		default:
68608d32323SJakub Kicinski 			return -EAFNOSUPPORT;
68708d32323SJakub Kicinski 		}
68808d32323SJakub Kicinski 
68908d32323SJakub Kicinski 		cfg->udp_config.family = family;
69008d32323SJakub Kicinski 	}
69108d32323SJakub Kicinski 
69208d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_PORT]) {
69308d32323SJakub Kicinski 		port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
69408d32323SJakub Kicinski 		cfg->udp_config.local_udp_port = port;
69508d32323SJakub Kicinski 	}
69608d32323SJakub Kicinski 
69708d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_IPPROTO])
69808d32323SJakub Kicinski 		cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
69908d32323SJakub Kicinski 
70008d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_TYPE])
70108d32323SJakub Kicinski 		cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
70208d32323SJakub Kicinski 
70308d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
70408d32323SJakub Kicinski 		cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
70508d32323SJakub Kicinski 
70608d32323SJakub Kicinski 	if (cfg->udp_config.family == AF_INET) {
70708d32323SJakub Kicinski 		if (info->attrs[FOU_ATTR_LOCAL_V4]) {
70808d32323SJakub Kicinski 			attr = info->attrs[FOU_ATTR_LOCAL_V4];
70908d32323SJakub Kicinski 			cfg->udp_config.local_ip.s_addr = nla_get_in_addr(attr);
71008d32323SJakub Kicinski 			has_local = true;
71108d32323SJakub Kicinski 		}
71208d32323SJakub Kicinski 
71308d32323SJakub Kicinski 		if (info->attrs[FOU_ATTR_PEER_V4]) {
71408d32323SJakub Kicinski 			attr = info->attrs[FOU_ATTR_PEER_V4];
71508d32323SJakub Kicinski 			cfg->udp_config.peer_ip.s_addr = nla_get_in_addr(attr);
71608d32323SJakub Kicinski 			has_peer = true;
71708d32323SJakub Kicinski 		}
71808d32323SJakub Kicinski #if IS_ENABLED(CONFIG_IPV6)
71908d32323SJakub Kicinski 	} else {
72008d32323SJakub Kicinski 		if (info->attrs[FOU_ATTR_LOCAL_V6]) {
72108d32323SJakub Kicinski 			attr = info->attrs[FOU_ATTR_LOCAL_V6];
72208d32323SJakub Kicinski 			cfg->udp_config.local_ip6 = nla_get_in6_addr(attr);
72308d32323SJakub Kicinski 			has_local = true;
72408d32323SJakub Kicinski 		}
72508d32323SJakub Kicinski 
72608d32323SJakub Kicinski 		if (info->attrs[FOU_ATTR_PEER_V6]) {
72708d32323SJakub Kicinski 			attr = info->attrs[FOU_ATTR_PEER_V6];
72808d32323SJakub Kicinski 			cfg->udp_config.peer_ip6 = nla_get_in6_addr(attr);
72908d32323SJakub Kicinski 			has_peer = true;
73008d32323SJakub Kicinski 		}
73108d32323SJakub Kicinski #endif
73208d32323SJakub Kicinski 	}
73308d32323SJakub Kicinski 
73408d32323SJakub Kicinski 	if (has_peer) {
73508d32323SJakub Kicinski 		if (info->attrs[FOU_ATTR_PEER_PORT]) {
73608d32323SJakub Kicinski 			port = nla_get_be16(info->attrs[FOU_ATTR_PEER_PORT]);
73708d32323SJakub Kicinski 			cfg->udp_config.peer_udp_port = port;
73808d32323SJakub Kicinski 		} else {
73908d32323SJakub Kicinski 			return -EINVAL;
74008d32323SJakub Kicinski 		}
74108d32323SJakub Kicinski 	}
74208d32323SJakub Kicinski 
74308d32323SJakub Kicinski 	if (info->attrs[FOU_ATTR_IFINDEX]) {
74408d32323SJakub Kicinski 		if (!has_local)
74508d32323SJakub Kicinski 			return -EINVAL;
74608d32323SJakub Kicinski 
74708d32323SJakub Kicinski 		ifindex = nla_get_s32(info->attrs[FOU_ATTR_IFINDEX]);
74808d32323SJakub Kicinski 
74908d32323SJakub Kicinski 		cfg->udp_config.bind_ifindex = ifindex;
75008d32323SJakub Kicinski 	}
75108d32323SJakub Kicinski 
75208d32323SJakub Kicinski 	return 0;
75308d32323SJakub Kicinski }
75408d32323SJakub Kicinski 
fou_nl_add_doit(struct sk_buff * skb,struct genl_info * info)7551d562c32SJakub Kicinski int fou_nl_add_doit(struct sk_buff *skb, struct genl_info *info)
75608d32323SJakub Kicinski {
75708d32323SJakub Kicinski 	struct net *net = genl_info_net(info);
75808d32323SJakub Kicinski 	struct fou_cfg cfg;
75908d32323SJakub Kicinski 	int err;
76008d32323SJakub Kicinski 
76108d32323SJakub Kicinski 	err = parse_nl_config(info, &cfg);
76208d32323SJakub Kicinski 	if (err)
76308d32323SJakub Kicinski 		return err;
76408d32323SJakub Kicinski 
76508d32323SJakub Kicinski 	return fou_create(net, &cfg, NULL);
76608d32323SJakub Kicinski }
76708d32323SJakub Kicinski 
fou_nl_del_doit(struct sk_buff * skb,struct genl_info * info)7681d562c32SJakub Kicinski int fou_nl_del_doit(struct sk_buff *skb, struct genl_info *info)
76908d32323SJakub Kicinski {
77008d32323SJakub Kicinski 	struct net *net = genl_info_net(info);
77108d32323SJakub Kicinski 	struct fou_cfg cfg;
77208d32323SJakub Kicinski 	int err;
77308d32323SJakub Kicinski 
77408d32323SJakub Kicinski 	err = parse_nl_config(info, &cfg);
77508d32323SJakub Kicinski 	if (err)
77608d32323SJakub Kicinski 		return err;
77708d32323SJakub Kicinski 
77808d32323SJakub Kicinski 	return fou_destroy(net, &cfg);
77908d32323SJakub Kicinski }
78008d32323SJakub Kicinski 
fou_fill_info(struct fou * fou,struct sk_buff * msg)78108d32323SJakub Kicinski static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
78208d32323SJakub Kicinski {
78308d32323SJakub Kicinski 	struct sock *sk = fou->sock->sk;
78408d32323SJakub Kicinski 
78508d32323SJakub Kicinski 	if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
78608d32323SJakub Kicinski 	    nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
78708d32323SJakub Kicinski 	    nla_put_be16(msg, FOU_ATTR_PEER_PORT, sk->sk_dport) ||
78808d32323SJakub Kicinski 	    nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
78908d32323SJakub Kicinski 	    nla_put_u8(msg, FOU_ATTR_TYPE, fou->type) ||
79008d32323SJakub Kicinski 	    nla_put_s32(msg, FOU_ATTR_IFINDEX, sk->sk_bound_dev_if))
79108d32323SJakub Kicinski 		return -1;
79208d32323SJakub Kicinski 
79308d32323SJakub Kicinski 	if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
79408d32323SJakub Kicinski 		if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
79508d32323SJakub Kicinski 			return -1;
79608d32323SJakub Kicinski 
79708d32323SJakub Kicinski 	if (fou->sock->sk->sk_family == AF_INET) {
79808d32323SJakub Kicinski 		if (nla_put_in_addr(msg, FOU_ATTR_LOCAL_V4, sk->sk_rcv_saddr))
79908d32323SJakub Kicinski 			return -1;
80008d32323SJakub Kicinski 
80108d32323SJakub Kicinski 		if (nla_put_in_addr(msg, FOU_ATTR_PEER_V4, sk->sk_daddr))
80208d32323SJakub Kicinski 			return -1;
80308d32323SJakub Kicinski #if IS_ENABLED(CONFIG_IPV6)
80408d32323SJakub Kicinski 	} else {
80508d32323SJakub Kicinski 		if (nla_put_in6_addr(msg, FOU_ATTR_LOCAL_V6,
80608d32323SJakub Kicinski 				     &sk->sk_v6_rcv_saddr))
80708d32323SJakub Kicinski 			return -1;
80808d32323SJakub Kicinski 
80908d32323SJakub Kicinski 		if (nla_put_in6_addr(msg, FOU_ATTR_PEER_V6, &sk->sk_v6_daddr))
81008d32323SJakub Kicinski 			return -1;
81108d32323SJakub Kicinski #endif
81208d32323SJakub Kicinski 	}
81308d32323SJakub Kicinski 
81408d32323SJakub Kicinski 	return 0;
81508d32323SJakub Kicinski }
81608d32323SJakub Kicinski 
fou_dump_info(struct fou * fou,u32 portid,u32 seq,u32 flags,struct sk_buff * skb,u8 cmd)81708d32323SJakub Kicinski static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
81808d32323SJakub Kicinski 			 u32 flags, struct sk_buff *skb, u8 cmd)
81908d32323SJakub Kicinski {
82008d32323SJakub Kicinski 	void *hdr;
82108d32323SJakub Kicinski 
82208d32323SJakub Kicinski 	hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
82308d32323SJakub Kicinski 	if (!hdr)
82408d32323SJakub Kicinski 		return -ENOMEM;
82508d32323SJakub Kicinski 
82608d32323SJakub Kicinski 	if (fou_fill_info(fou, skb) < 0)
82708d32323SJakub Kicinski 		goto nla_put_failure;
82808d32323SJakub Kicinski 
82908d32323SJakub Kicinski 	genlmsg_end(skb, hdr);
83008d32323SJakub Kicinski 	return 0;
83108d32323SJakub Kicinski 
83208d32323SJakub Kicinski nla_put_failure:
83308d32323SJakub Kicinski 	genlmsg_cancel(skb, hdr);
83408d32323SJakub Kicinski 	return -EMSGSIZE;
83508d32323SJakub Kicinski }
83608d32323SJakub Kicinski 
fou_nl_get_doit(struct sk_buff * skb,struct genl_info * info)8371d562c32SJakub Kicinski int fou_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
83808d32323SJakub Kicinski {
83908d32323SJakub Kicinski 	struct net *net = genl_info_net(info);
84008d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
84108d32323SJakub Kicinski 	struct sk_buff *msg;
84208d32323SJakub Kicinski 	struct fou_cfg cfg;
84308d32323SJakub Kicinski 	struct fou *fout;
84408d32323SJakub Kicinski 	__be16 port;
84508d32323SJakub Kicinski 	u8 family;
84608d32323SJakub Kicinski 	int ret;
84708d32323SJakub Kicinski 
84808d32323SJakub Kicinski 	ret = parse_nl_config(info, &cfg);
84908d32323SJakub Kicinski 	if (ret)
85008d32323SJakub Kicinski 		return ret;
85108d32323SJakub Kicinski 	port = cfg.udp_config.local_udp_port;
85208d32323SJakub Kicinski 	if (port == 0)
85308d32323SJakub Kicinski 		return -EINVAL;
85408d32323SJakub Kicinski 
85508d32323SJakub Kicinski 	family = cfg.udp_config.family;
85608d32323SJakub Kicinski 	if (family != AF_INET && family != AF_INET6)
85708d32323SJakub Kicinski 		return -EINVAL;
85808d32323SJakub Kicinski 
85908d32323SJakub Kicinski 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
86008d32323SJakub Kicinski 	if (!msg)
86108d32323SJakub Kicinski 		return -ENOMEM;
86208d32323SJakub Kicinski 
86308d32323SJakub Kicinski 	ret = -ESRCH;
86408d32323SJakub Kicinski 	mutex_lock(&fn->fou_lock);
86508d32323SJakub Kicinski 	list_for_each_entry(fout, &fn->fou_list, list) {
86608d32323SJakub Kicinski 		if (fou_cfg_cmp(fout, &cfg)) {
86708d32323SJakub Kicinski 			ret = fou_dump_info(fout, info->snd_portid,
86808d32323SJakub Kicinski 					    info->snd_seq, 0, msg,
86908d32323SJakub Kicinski 					    info->genlhdr->cmd);
87008d32323SJakub Kicinski 			break;
87108d32323SJakub Kicinski 		}
87208d32323SJakub Kicinski 	}
87308d32323SJakub Kicinski 	mutex_unlock(&fn->fou_lock);
87408d32323SJakub Kicinski 	if (ret < 0)
87508d32323SJakub Kicinski 		goto out_free;
87608d32323SJakub Kicinski 
87708d32323SJakub Kicinski 	return genlmsg_reply(msg, info);
87808d32323SJakub Kicinski 
87908d32323SJakub Kicinski out_free:
88008d32323SJakub Kicinski 	nlmsg_free(msg);
88108d32323SJakub Kicinski 	return ret;
88208d32323SJakub Kicinski }
88308d32323SJakub Kicinski 
fou_nl_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)8841d562c32SJakub Kicinski int fou_nl_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
88508d32323SJakub Kicinski {
88608d32323SJakub Kicinski 	struct net *net = sock_net(skb->sk);
88708d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
88808d32323SJakub Kicinski 	struct fou *fout;
88908d32323SJakub Kicinski 	int idx = 0, ret;
89008d32323SJakub Kicinski 
89108d32323SJakub Kicinski 	mutex_lock(&fn->fou_lock);
89208d32323SJakub Kicinski 	list_for_each_entry(fout, &fn->fou_list, list) {
89308d32323SJakub Kicinski 		if (idx++ < cb->args[0])
89408d32323SJakub Kicinski 			continue;
89508d32323SJakub Kicinski 		ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
89608d32323SJakub Kicinski 				    cb->nlh->nlmsg_seq, NLM_F_MULTI,
89708d32323SJakub Kicinski 				    skb, FOU_CMD_GET);
89808d32323SJakub Kicinski 		if (ret)
89908d32323SJakub Kicinski 			break;
90008d32323SJakub Kicinski 	}
90108d32323SJakub Kicinski 	mutex_unlock(&fn->fou_lock);
90208d32323SJakub Kicinski 
90308d32323SJakub Kicinski 	cb->args[0] = idx;
90408d32323SJakub Kicinski 	return skb->len;
90508d32323SJakub Kicinski }
90608d32323SJakub Kicinski 
90708d32323SJakub Kicinski static struct genl_family fou_nl_family __ro_after_init = {
90808d32323SJakub Kicinski 	.hdrsize	= 0,
90908d32323SJakub Kicinski 	.name		= FOU_GENL_NAME,
91008d32323SJakub Kicinski 	.version	= FOU_GENL_VERSION,
91108d32323SJakub Kicinski 	.maxattr	= FOU_ATTR_MAX,
91208d32323SJakub Kicinski 	.policy		= fou_nl_policy,
91308d32323SJakub Kicinski 	.netnsok	= true,
91408d32323SJakub Kicinski 	.module		= THIS_MODULE,
91508d32323SJakub Kicinski 	.small_ops	= fou_nl_ops,
91608d32323SJakub Kicinski 	.n_small_ops	= ARRAY_SIZE(fou_nl_ops),
91708d32323SJakub Kicinski 	.resv_start_op	= FOU_CMD_GET + 1,
91808d32323SJakub Kicinski };
91908d32323SJakub Kicinski 
fou_encap_hlen(struct ip_tunnel_encap * e)92008d32323SJakub Kicinski size_t fou_encap_hlen(struct ip_tunnel_encap *e)
92108d32323SJakub Kicinski {
92208d32323SJakub Kicinski 	return sizeof(struct udphdr);
92308d32323SJakub Kicinski }
92408d32323SJakub Kicinski EXPORT_SYMBOL(fou_encap_hlen);
92508d32323SJakub Kicinski 
gue_encap_hlen(struct ip_tunnel_encap * e)92608d32323SJakub Kicinski size_t gue_encap_hlen(struct ip_tunnel_encap *e)
92708d32323SJakub Kicinski {
92808d32323SJakub Kicinski 	size_t len;
92908d32323SJakub Kicinski 	bool need_priv = false;
93008d32323SJakub Kicinski 
93108d32323SJakub Kicinski 	len = sizeof(struct udphdr) + sizeof(struct guehdr);
93208d32323SJakub Kicinski 
93308d32323SJakub Kicinski 	if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
93408d32323SJakub Kicinski 		len += GUE_PLEN_REMCSUM;
93508d32323SJakub Kicinski 		need_priv = true;
93608d32323SJakub Kicinski 	}
93708d32323SJakub Kicinski 
93808d32323SJakub Kicinski 	len += need_priv ? GUE_LEN_PRIV : 0;
93908d32323SJakub Kicinski 
94008d32323SJakub Kicinski 	return len;
94108d32323SJakub Kicinski }
94208d32323SJakub Kicinski EXPORT_SYMBOL(gue_encap_hlen);
94308d32323SJakub Kicinski 
__fou_build_header(struct sk_buff * skb,struct ip_tunnel_encap * e,u8 * protocol,__be16 * sport,int type)94408d32323SJakub Kicinski int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
94508d32323SJakub Kicinski 		       u8 *protocol, __be16 *sport, int type)
94608d32323SJakub Kicinski {
94708d32323SJakub Kicinski 	int err;
94808d32323SJakub Kicinski 
94908d32323SJakub Kicinski 	err = iptunnel_handle_offloads(skb, type);
95008d32323SJakub Kicinski 	if (err)
95108d32323SJakub Kicinski 		return err;
95208d32323SJakub Kicinski 
95308d32323SJakub Kicinski 	*sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
95408d32323SJakub Kicinski 						skb, 0, 0, false);
95508d32323SJakub Kicinski 
95608d32323SJakub Kicinski 	return 0;
95708d32323SJakub Kicinski }
95808d32323SJakub Kicinski EXPORT_SYMBOL(__fou_build_header);
95908d32323SJakub Kicinski 
__gue_build_header(struct sk_buff * skb,struct ip_tunnel_encap * e,u8 * protocol,__be16 * sport,int type)96008d32323SJakub Kicinski int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
96108d32323SJakub Kicinski 		       u8 *protocol, __be16 *sport, int type)
96208d32323SJakub Kicinski {
96308d32323SJakub Kicinski 	struct guehdr *guehdr;
96408d32323SJakub Kicinski 	size_t hdrlen, optlen = 0;
96508d32323SJakub Kicinski 	void *data;
96608d32323SJakub Kicinski 	bool need_priv = false;
96708d32323SJakub Kicinski 	int err;
96808d32323SJakub Kicinski 
96908d32323SJakub Kicinski 	if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
97008d32323SJakub Kicinski 	    skb->ip_summed == CHECKSUM_PARTIAL) {
97108d32323SJakub Kicinski 		optlen += GUE_PLEN_REMCSUM;
97208d32323SJakub Kicinski 		type |= SKB_GSO_TUNNEL_REMCSUM;
97308d32323SJakub Kicinski 		need_priv = true;
97408d32323SJakub Kicinski 	}
97508d32323SJakub Kicinski 
97608d32323SJakub Kicinski 	optlen += need_priv ? GUE_LEN_PRIV : 0;
97708d32323SJakub Kicinski 
97808d32323SJakub Kicinski 	err = iptunnel_handle_offloads(skb, type);
97908d32323SJakub Kicinski 	if (err)
98008d32323SJakub Kicinski 		return err;
98108d32323SJakub Kicinski 
98208d32323SJakub Kicinski 	/* Get source port (based on flow hash) before skb_push */
98308d32323SJakub Kicinski 	*sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
98408d32323SJakub Kicinski 						skb, 0, 0, false);
98508d32323SJakub Kicinski 
98608d32323SJakub Kicinski 	hdrlen = sizeof(struct guehdr) + optlen;
98708d32323SJakub Kicinski 
98808d32323SJakub Kicinski 	skb_push(skb, hdrlen);
98908d32323SJakub Kicinski 
99008d32323SJakub Kicinski 	guehdr = (struct guehdr *)skb->data;
99108d32323SJakub Kicinski 
99208d32323SJakub Kicinski 	guehdr->control = 0;
99308d32323SJakub Kicinski 	guehdr->version = 0;
99408d32323SJakub Kicinski 	guehdr->hlen = optlen >> 2;
99508d32323SJakub Kicinski 	guehdr->flags = 0;
99608d32323SJakub Kicinski 	guehdr->proto_ctype = *protocol;
99708d32323SJakub Kicinski 
99808d32323SJakub Kicinski 	data = &guehdr[1];
99908d32323SJakub Kicinski 
100008d32323SJakub Kicinski 	if (need_priv) {
100108d32323SJakub Kicinski 		__be32 *flags = data;
100208d32323SJakub Kicinski 
100308d32323SJakub Kicinski 		guehdr->flags |= GUE_FLAG_PRIV;
100408d32323SJakub Kicinski 		*flags = 0;
100508d32323SJakub Kicinski 		data += GUE_LEN_PRIV;
100608d32323SJakub Kicinski 
100708d32323SJakub Kicinski 		if (type & SKB_GSO_TUNNEL_REMCSUM) {
100808d32323SJakub Kicinski 			u16 csum_start = skb_checksum_start_offset(skb);
100908d32323SJakub Kicinski 			__be16 *pd = data;
101008d32323SJakub Kicinski 
101108d32323SJakub Kicinski 			if (csum_start < hdrlen)
101208d32323SJakub Kicinski 				return -EINVAL;
101308d32323SJakub Kicinski 
101408d32323SJakub Kicinski 			csum_start -= hdrlen;
101508d32323SJakub Kicinski 			pd[0] = htons(csum_start);
101608d32323SJakub Kicinski 			pd[1] = htons(csum_start + skb->csum_offset);
101708d32323SJakub Kicinski 
101808d32323SJakub Kicinski 			if (!skb_is_gso(skb)) {
101908d32323SJakub Kicinski 				skb->ip_summed = CHECKSUM_NONE;
102008d32323SJakub Kicinski 				skb->encapsulation = 0;
102108d32323SJakub Kicinski 			}
102208d32323SJakub Kicinski 
102308d32323SJakub Kicinski 			*flags |= GUE_PFLAG_REMCSUM;
102408d32323SJakub Kicinski 			data += GUE_PLEN_REMCSUM;
102508d32323SJakub Kicinski 		}
102608d32323SJakub Kicinski 
102708d32323SJakub Kicinski 	}
102808d32323SJakub Kicinski 
102908d32323SJakub Kicinski 	return 0;
103008d32323SJakub Kicinski }
103108d32323SJakub Kicinski EXPORT_SYMBOL(__gue_build_header);
103208d32323SJakub Kicinski 
103308d32323SJakub Kicinski #ifdef CONFIG_NET_FOU_IP_TUNNELS
103408d32323SJakub Kicinski 
fou_build_udp(struct sk_buff * skb,struct ip_tunnel_encap * e,struct flowi4 * fl4,u8 * protocol,__be16 sport)103508d32323SJakub Kicinski static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
103608d32323SJakub Kicinski 			  struct flowi4 *fl4, u8 *protocol, __be16 sport)
103708d32323SJakub Kicinski {
103808d32323SJakub Kicinski 	struct udphdr *uh;
103908d32323SJakub Kicinski 
104008d32323SJakub Kicinski 	skb_push(skb, sizeof(struct udphdr));
104108d32323SJakub Kicinski 	skb_reset_transport_header(skb);
104208d32323SJakub Kicinski 
104308d32323SJakub Kicinski 	uh = udp_hdr(skb);
104408d32323SJakub Kicinski 
104508d32323SJakub Kicinski 	uh->dest = e->dport;
104608d32323SJakub Kicinski 	uh->source = sport;
104708d32323SJakub Kicinski 	uh->len = htons(skb->len);
104808d32323SJakub Kicinski 	udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
104908d32323SJakub Kicinski 		     fl4->saddr, fl4->daddr, skb->len);
105008d32323SJakub Kicinski 
105108d32323SJakub Kicinski 	*protocol = IPPROTO_UDP;
105208d32323SJakub Kicinski }
105308d32323SJakub Kicinski 
fou_build_header(struct sk_buff * skb,struct ip_tunnel_encap * e,u8 * protocol,struct flowi4 * fl4)105408d32323SJakub Kicinski static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
105508d32323SJakub Kicinski 			    u8 *protocol, struct flowi4 *fl4)
105608d32323SJakub Kicinski {
105708d32323SJakub Kicinski 	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
105808d32323SJakub Kicinski 						       SKB_GSO_UDP_TUNNEL;
105908d32323SJakub Kicinski 	__be16 sport;
106008d32323SJakub Kicinski 	int err;
106108d32323SJakub Kicinski 
106208d32323SJakub Kicinski 	err = __fou_build_header(skb, e, protocol, &sport, type);
106308d32323SJakub Kicinski 	if (err)
106408d32323SJakub Kicinski 		return err;
106508d32323SJakub Kicinski 
106608d32323SJakub Kicinski 	fou_build_udp(skb, e, fl4, protocol, sport);
106708d32323SJakub Kicinski 
106808d32323SJakub Kicinski 	return 0;
106908d32323SJakub Kicinski }
107008d32323SJakub Kicinski 
gue_build_header(struct sk_buff * skb,struct ip_tunnel_encap * e,u8 * protocol,struct flowi4 * fl4)107108d32323SJakub Kicinski static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
107208d32323SJakub Kicinski 			    u8 *protocol, struct flowi4 *fl4)
107308d32323SJakub Kicinski {
107408d32323SJakub Kicinski 	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
107508d32323SJakub Kicinski 						       SKB_GSO_UDP_TUNNEL;
107608d32323SJakub Kicinski 	__be16 sport;
107708d32323SJakub Kicinski 	int err;
107808d32323SJakub Kicinski 
107908d32323SJakub Kicinski 	err = __gue_build_header(skb, e, protocol, &sport, type);
108008d32323SJakub Kicinski 	if (err)
108108d32323SJakub Kicinski 		return err;
108208d32323SJakub Kicinski 
108308d32323SJakub Kicinski 	fou_build_udp(skb, e, fl4, protocol, sport);
108408d32323SJakub Kicinski 
108508d32323SJakub Kicinski 	return 0;
108608d32323SJakub Kicinski }
108708d32323SJakub Kicinski 
gue_err_proto_handler(int proto,struct sk_buff * skb,u32 info)108808d32323SJakub Kicinski static int gue_err_proto_handler(int proto, struct sk_buff *skb, u32 info)
108908d32323SJakub Kicinski {
109008d32323SJakub Kicinski 	const struct net_protocol *ipprot = rcu_dereference(inet_protos[proto]);
109108d32323SJakub Kicinski 
109208d32323SJakub Kicinski 	if (ipprot && ipprot->err_handler) {
109308d32323SJakub Kicinski 		if (!ipprot->err_handler(skb, info))
109408d32323SJakub Kicinski 			return 0;
109508d32323SJakub Kicinski 	}
109608d32323SJakub Kicinski 
109708d32323SJakub Kicinski 	return -ENOENT;
109808d32323SJakub Kicinski }
109908d32323SJakub Kicinski 
gue_err(struct sk_buff * skb,u32 info)110008d32323SJakub Kicinski static int gue_err(struct sk_buff *skb, u32 info)
110108d32323SJakub Kicinski {
110208d32323SJakub Kicinski 	int transport_offset = skb_transport_offset(skb);
110308d32323SJakub Kicinski 	struct guehdr *guehdr;
110408d32323SJakub Kicinski 	size_t len, optlen;
110508d32323SJakub Kicinski 	int ret;
110608d32323SJakub Kicinski 
110708d32323SJakub Kicinski 	len = sizeof(struct udphdr) + sizeof(struct guehdr);
110808d32323SJakub Kicinski 	if (!pskb_may_pull(skb, transport_offset + len))
110908d32323SJakub Kicinski 		return -EINVAL;
111008d32323SJakub Kicinski 
111108d32323SJakub Kicinski 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
111208d32323SJakub Kicinski 
111308d32323SJakub Kicinski 	switch (guehdr->version) {
111408d32323SJakub Kicinski 	case 0: /* Full GUE header present */
111508d32323SJakub Kicinski 		break;
111608d32323SJakub Kicinski 	case 1: {
111708d32323SJakub Kicinski 		/* Direct encapsulation of IPv4 or IPv6 */
111808d32323SJakub Kicinski 		skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
111908d32323SJakub Kicinski 
112008d32323SJakub Kicinski 		switch (((struct iphdr *)guehdr)->version) {
112108d32323SJakub Kicinski 		case 4:
112208d32323SJakub Kicinski 			ret = gue_err_proto_handler(IPPROTO_IPIP, skb, info);
112308d32323SJakub Kicinski 			goto out;
112408d32323SJakub Kicinski #if IS_ENABLED(CONFIG_IPV6)
112508d32323SJakub Kicinski 		case 6:
112608d32323SJakub Kicinski 			ret = gue_err_proto_handler(IPPROTO_IPV6, skb, info);
112708d32323SJakub Kicinski 			goto out;
112808d32323SJakub Kicinski #endif
112908d32323SJakub Kicinski 		default:
113008d32323SJakub Kicinski 			ret = -EOPNOTSUPP;
113108d32323SJakub Kicinski 			goto out;
113208d32323SJakub Kicinski 		}
113308d32323SJakub Kicinski 	}
113408d32323SJakub Kicinski 	default: /* Undefined version */
113508d32323SJakub Kicinski 		return -EOPNOTSUPP;
113608d32323SJakub Kicinski 	}
113708d32323SJakub Kicinski 
113808d32323SJakub Kicinski 	if (guehdr->control)
113908d32323SJakub Kicinski 		return -ENOENT;
114008d32323SJakub Kicinski 
114108d32323SJakub Kicinski 	optlen = guehdr->hlen << 2;
114208d32323SJakub Kicinski 
114308d32323SJakub Kicinski 	if (!pskb_may_pull(skb, transport_offset + len + optlen))
114408d32323SJakub Kicinski 		return -EINVAL;
114508d32323SJakub Kicinski 
114608d32323SJakub Kicinski 	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
114708d32323SJakub Kicinski 	if (validate_gue_flags(guehdr, optlen))
114808d32323SJakub Kicinski 		return -EINVAL;
114908d32323SJakub Kicinski 
115008d32323SJakub Kicinski 	/* Handling exceptions for direct UDP encapsulation in GUE would lead to
115108d32323SJakub Kicinski 	 * recursion. Besides, this kind of encapsulation can't even be
115208d32323SJakub Kicinski 	 * configured currently. Discard this.
115308d32323SJakub Kicinski 	 */
115408d32323SJakub Kicinski 	if (guehdr->proto_ctype == IPPROTO_UDP ||
115508d32323SJakub Kicinski 	    guehdr->proto_ctype == IPPROTO_UDPLITE)
115608d32323SJakub Kicinski 		return -EOPNOTSUPP;
115708d32323SJakub Kicinski 
115808d32323SJakub Kicinski 	skb_set_transport_header(skb, -(int)sizeof(struct icmphdr));
115908d32323SJakub Kicinski 	ret = gue_err_proto_handler(guehdr->proto_ctype, skb, info);
116008d32323SJakub Kicinski 
116108d32323SJakub Kicinski out:
116208d32323SJakub Kicinski 	skb_set_transport_header(skb, transport_offset);
116308d32323SJakub Kicinski 	return ret;
116408d32323SJakub Kicinski }
116508d32323SJakub Kicinski 
116608d32323SJakub Kicinski 
116708d32323SJakub Kicinski static const struct ip_tunnel_encap_ops fou_iptun_ops = {
116808d32323SJakub Kicinski 	.encap_hlen = fou_encap_hlen,
116908d32323SJakub Kicinski 	.build_header = fou_build_header,
117008d32323SJakub Kicinski 	.err_handler = gue_err,
117108d32323SJakub Kicinski };
117208d32323SJakub Kicinski 
117308d32323SJakub Kicinski static const struct ip_tunnel_encap_ops gue_iptun_ops = {
117408d32323SJakub Kicinski 	.encap_hlen = gue_encap_hlen,
117508d32323SJakub Kicinski 	.build_header = gue_build_header,
117608d32323SJakub Kicinski 	.err_handler = gue_err,
117708d32323SJakub Kicinski };
117808d32323SJakub Kicinski 
ip_tunnel_encap_add_fou_ops(void)117908d32323SJakub Kicinski static int ip_tunnel_encap_add_fou_ops(void)
118008d32323SJakub Kicinski {
118108d32323SJakub Kicinski 	int ret;
118208d32323SJakub Kicinski 
118308d32323SJakub Kicinski 	ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
118408d32323SJakub Kicinski 	if (ret < 0) {
118508d32323SJakub Kicinski 		pr_err("can't add fou ops\n");
118608d32323SJakub Kicinski 		return ret;
118708d32323SJakub Kicinski 	}
118808d32323SJakub Kicinski 
118908d32323SJakub Kicinski 	ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
119008d32323SJakub Kicinski 	if (ret < 0) {
119108d32323SJakub Kicinski 		pr_err("can't add gue ops\n");
119208d32323SJakub Kicinski 		ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
119308d32323SJakub Kicinski 		return ret;
119408d32323SJakub Kicinski 	}
119508d32323SJakub Kicinski 
119608d32323SJakub Kicinski 	return 0;
119708d32323SJakub Kicinski }
119808d32323SJakub Kicinski 
ip_tunnel_encap_del_fou_ops(void)119908d32323SJakub Kicinski static void ip_tunnel_encap_del_fou_ops(void)
120008d32323SJakub Kicinski {
120108d32323SJakub Kicinski 	ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
120208d32323SJakub Kicinski 	ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
120308d32323SJakub Kicinski }
120408d32323SJakub Kicinski 
120508d32323SJakub Kicinski #else
120608d32323SJakub Kicinski 
ip_tunnel_encap_add_fou_ops(void)120708d32323SJakub Kicinski static int ip_tunnel_encap_add_fou_ops(void)
120808d32323SJakub Kicinski {
120908d32323SJakub Kicinski 	return 0;
121008d32323SJakub Kicinski }
121108d32323SJakub Kicinski 
ip_tunnel_encap_del_fou_ops(void)121208d32323SJakub Kicinski static void ip_tunnel_encap_del_fou_ops(void)
121308d32323SJakub Kicinski {
121408d32323SJakub Kicinski }
121508d32323SJakub Kicinski 
121608d32323SJakub Kicinski #endif
121708d32323SJakub Kicinski 
fou_init_net(struct net * net)121808d32323SJakub Kicinski static __net_init int fou_init_net(struct net *net)
121908d32323SJakub Kicinski {
122008d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
122108d32323SJakub Kicinski 
122208d32323SJakub Kicinski 	INIT_LIST_HEAD(&fn->fou_list);
122308d32323SJakub Kicinski 	mutex_init(&fn->fou_lock);
122408d32323SJakub Kicinski 	return 0;
122508d32323SJakub Kicinski }
122608d32323SJakub Kicinski 
fou_exit_net(struct net * net)122708d32323SJakub Kicinski static __net_exit void fou_exit_net(struct net *net)
122808d32323SJakub Kicinski {
122908d32323SJakub Kicinski 	struct fou_net *fn = net_generic(net, fou_net_id);
123008d32323SJakub Kicinski 	struct fou *fou, *next;
123108d32323SJakub Kicinski 
123208d32323SJakub Kicinski 	/* Close all the FOU sockets */
123308d32323SJakub Kicinski 	mutex_lock(&fn->fou_lock);
123408d32323SJakub Kicinski 	list_for_each_entry_safe(fou, next, &fn->fou_list, list)
123508d32323SJakub Kicinski 		fou_release(fou);
123608d32323SJakub Kicinski 	mutex_unlock(&fn->fou_lock);
123708d32323SJakub Kicinski }
123808d32323SJakub Kicinski 
123908d32323SJakub Kicinski static struct pernet_operations fou_net_ops = {
124008d32323SJakub Kicinski 	.init = fou_init_net,
124108d32323SJakub Kicinski 	.exit = fou_exit_net,
124208d32323SJakub Kicinski 	.id   = &fou_net_id,
124308d32323SJakub Kicinski 	.size = sizeof(struct fou_net),
124408d32323SJakub Kicinski };
124508d32323SJakub Kicinski 
fou_init(void)124608d32323SJakub Kicinski static int __init fou_init(void)
124708d32323SJakub Kicinski {
124808d32323SJakub Kicinski 	int ret;
124908d32323SJakub Kicinski 
125008d32323SJakub Kicinski 	ret = register_pernet_device(&fou_net_ops);
125108d32323SJakub Kicinski 	if (ret)
125208d32323SJakub Kicinski 		goto exit;
125308d32323SJakub Kicinski 
125408d32323SJakub Kicinski 	ret = genl_register_family(&fou_nl_family);
125508d32323SJakub Kicinski 	if (ret < 0)
125608d32323SJakub Kicinski 		goto unregister;
125708d32323SJakub Kicinski 
1258c50e9609SChristian Ehrig 	ret = register_fou_bpf();
1259c50e9609SChristian Ehrig 	if (ret < 0)
1260c50e9609SChristian Ehrig 		goto kfunc_failed;
1261c50e9609SChristian Ehrig 
126208d32323SJakub Kicinski 	ret = ip_tunnel_encap_add_fou_ops();
126308d32323SJakub Kicinski 	if (ret == 0)
126408d32323SJakub Kicinski 		return 0;
126508d32323SJakub Kicinski 
1266c50e9609SChristian Ehrig kfunc_failed:
126708d32323SJakub Kicinski 	genl_unregister_family(&fou_nl_family);
126808d32323SJakub Kicinski unregister:
126908d32323SJakub Kicinski 	unregister_pernet_device(&fou_net_ops);
127008d32323SJakub Kicinski exit:
127108d32323SJakub Kicinski 	return ret;
127208d32323SJakub Kicinski }
127308d32323SJakub Kicinski 
fou_fini(void)127408d32323SJakub Kicinski static void __exit fou_fini(void)
127508d32323SJakub Kicinski {
127608d32323SJakub Kicinski 	ip_tunnel_encap_del_fou_ops();
127708d32323SJakub Kicinski 	genl_unregister_family(&fou_nl_family);
127808d32323SJakub Kicinski 	unregister_pernet_device(&fou_net_ops);
127908d32323SJakub Kicinski }
128008d32323SJakub Kicinski 
128108d32323SJakub Kicinski module_init(fou_init);
128208d32323SJakub Kicinski module_exit(fou_fini);
128308d32323SJakub Kicinski MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
128408d32323SJakub Kicinski MODULE_LICENSE("GPL");
128508d32323SJakub Kicinski MODULE_DESCRIPTION("Foo over UDP");
1286