162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *	IPV6 GSO/GRO offload support
462306a36Sopenharmony_ci *	Linux INET6 implementation
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *      UDPv6 GSO support
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/skbuff.h>
962306a36Sopenharmony_ci#include <linux/netdevice.h>
1062306a36Sopenharmony_ci#include <linux/indirect_call_wrapper.h>
1162306a36Sopenharmony_ci#include <net/protocol.h>
1262306a36Sopenharmony_ci#include <net/ipv6.h>
1362306a36Sopenharmony_ci#include <net/udp.h>
1462306a36Sopenharmony_ci#include <net/ip6_checksum.h>
1562306a36Sopenharmony_ci#include "ip6_offload.h"
1662306a36Sopenharmony_ci#include <net/gro.h>
1762306a36Sopenharmony_ci#include <net/gso.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
2062306a36Sopenharmony_ci					 netdev_features_t features)
2162306a36Sopenharmony_ci{
2262306a36Sopenharmony_ci	struct sk_buff *segs = ERR_PTR(-EINVAL);
2362306a36Sopenharmony_ci	unsigned int mss;
2462306a36Sopenharmony_ci	unsigned int unfrag_ip6hlen, unfrag_len;
2562306a36Sopenharmony_ci	struct frag_hdr *fptr;
2662306a36Sopenharmony_ci	u8 *packet_start, *prevhdr;
2762306a36Sopenharmony_ci	u8 nexthdr;
2862306a36Sopenharmony_ci	u8 frag_hdr_sz = sizeof(struct frag_hdr);
2962306a36Sopenharmony_ci	__wsum csum;
3062306a36Sopenharmony_ci	int tnl_hlen;
3162306a36Sopenharmony_ci	int err;
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	if (skb->encapsulation && skb_shinfo(skb)->gso_type &
3462306a36Sopenharmony_ci	    (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))
3562306a36Sopenharmony_ci		segs = skb_udp_tunnel_segment(skb, features, true);
3662306a36Sopenharmony_ci	else {
3762306a36Sopenharmony_ci		const struct ipv6hdr *ipv6h;
3862306a36Sopenharmony_ci		struct udphdr *uh;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci		if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
4162306a36Sopenharmony_ci			goto out;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
4462306a36Sopenharmony_ci			goto out;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
4762306a36Sopenharmony_ci			return __udp_gso_segment(skb, features, true);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci		mss = skb_shinfo(skb)->gso_size;
5062306a36Sopenharmony_ci		if (unlikely(skb->len <= mss))
5162306a36Sopenharmony_ci			goto out;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci		/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
5462306a36Sopenharmony_ci		 * do checksum of UDP packets sent as multiple IP fragments.
5562306a36Sopenharmony_ci		 */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci		uh = udp_hdr(skb);
5862306a36Sopenharmony_ci		ipv6h = ipv6_hdr(skb);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci		uh->check = 0;
6162306a36Sopenharmony_ci		csum = skb_checksum(skb, 0, skb->len, 0);
6262306a36Sopenharmony_ci		uh->check = udp_v6_check(skb->len, &ipv6h->saddr,
6362306a36Sopenharmony_ci					  &ipv6h->daddr, csum);
6462306a36Sopenharmony_ci		if (uh->check == 0)
6562306a36Sopenharmony_ci			uh->check = CSUM_MANGLED_0;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci		skb->ip_summed = CHECKSUM_UNNECESSARY;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci		/* If there is no outer header we can fake a checksum offload
7062306a36Sopenharmony_ci		 * due to the fact that we have already done the checksum in
7162306a36Sopenharmony_ci		 * software prior to segmenting the frame.
7262306a36Sopenharmony_ci		 */
7362306a36Sopenharmony_ci		if (!skb->encap_hdr_csum)
7462306a36Sopenharmony_ci			features |= NETIF_F_HW_CSUM;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci		/* Check if there is enough headroom to insert fragment header. */
7762306a36Sopenharmony_ci		tnl_hlen = skb_tnl_header_len(skb);
7862306a36Sopenharmony_ci		if (skb->mac_header < (tnl_hlen + frag_hdr_sz)) {
7962306a36Sopenharmony_ci			if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
8062306a36Sopenharmony_ci				goto out;
8162306a36Sopenharmony_ci		}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci		/* Find the unfragmentable header and shift it left by frag_hdr_sz
8462306a36Sopenharmony_ci		 * bytes to insert fragment header.
8562306a36Sopenharmony_ci		 */
8662306a36Sopenharmony_ci		err = ip6_find_1stfragopt(skb, &prevhdr);
8762306a36Sopenharmony_ci		if (err < 0)
8862306a36Sopenharmony_ci			return ERR_PTR(err);
8962306a36Sopenharmony_ci		unfrag_ip6hlen = err;
9062306a36Sopenharmony_ci		nexthdr = *prevhdr;
9162306a36Sopenharmony_ci		*prevhdr = NEXTHDR_FRAGMENT;
9262306a36Sopenharmony_ci		unfrag_len = (skb_network_header(skb) - skb_mac_header(skb)) +
9362306a36Sopenharmony_ci			     unfrag_ip6hlen + tnl_hlen;
9462306a36Sopenharmony_ci		packet_start = (u8 *) skb->head + SKB_GSO_CB(skb)->mac_offset;
9562306a36Sopenharmony_ci		memmove(packet_start-frag_hdr_sz, packet_start, unfrag_len);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci		SKB_GSO_CB(skb)->mac_offset -= frag_hdr_sz;
9862306a36Sopenharmony_ci		skb->mac_header -= frag_hdr_sz;
9962306a36Sopenharmony_ci		skb->network_header -= frag_hdr_sz;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
10262306a36Sopenharmony_ci		fptr->nexthdr = nexthdr;
10362306a36Sopenharmony_ci		fptr->reserved = 0;
10462306a36Sopenharmony_ci		fptr->identification = ipv6_proxy_select_ident(dev_net(skb->dev), skb);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci		/* Fragment the skb. ipv6 header and the remaining fields of the
10762306a36Sopenharmony_ci		 * fragment header are updated in ipv6_gso_segment()
10862306a36Sopenharmony_ci		 */
10962306a36Sopenharmony_ci		segs = skb_segment(skb, features);
11062306a36Sopenharmony_ci	}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciout:
11362306a36Sopenharmony_ci	return segs;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic struct sock *udp6_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
11762306a36Sopenharmony_ci					__be16 dport)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	const struct ipv6hdr *iph = skb_gro_network_header(skb);
12062306a36Sopenharmony_ci	struct net *net = dev_net(skb->dev);
12162306a36Sopenharmony_ci	int iif, sdif;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	inet6_get_iif_sdif(skb, &iif, &sdif);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	return __udp6_lib_lookup(net, &iph->saddr, sport,
12662306a36Sopenharmony_ci				 &iph->daddr, dport, iif,
12762306a36Sopenharmony_ci				 sdif, net->ipv4.udp_table, NULL);
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciINDIRECT_CALLABLE_SCOPE
13162306a36Sopenharmony_cistruct sk_buff *udp6_gro_receive(struct list_head *head, struct sk_buff *skb)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	struct udphdr *uh = udp_gro_udphdr(skb);
13462306a36Sopenharmony_ci	struct sock *sk = NULL;
13562306a36Sopenharmony_ci	struct sk_buff *pp;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	if (unlikely(!uh))
13862306a36Sopenharmony_ci		goto flush;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* Don't bother verifying checksum if we're going to flush anyway. */
14162306a36Sopenharmony_ci	if (NAPI_GRO_CB(skb)->flush)
14262306a36Sopenharmony_ci		goto skip;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
14562306a36Sopenharmony_ci						 ip6_gro_compute_pseudo))
14662306a36Sopenharmony_ci		goto flush;
14762306a36Sopenharmony_ci	else if (uh->check)
14862306a36Sopenharmony_ci		skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
14962306a36Sopenharmony_ci					     ip6_gro_compute_pseudo);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciskip:
15262306a36Sopenharmony_ci	NAPI_GRO_CB(skb)->is_ipv6 = 1;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	if (static_branch_unlikely(&udpv6_encap_needed_key))
15562306a36Sopenharmony_ci		sk = udp6_gro_lookup_skb(skb, uh->source, uh->dest);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	pp = udp_gro_receive(head, skb, uh, sk);
15862306a36Sopenharmony_ci	return pp;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciflush:
16162306a36Sopenharmony_ci	NAPI_GRO_CB(skb)->flush = 1;
16262306a36Sopenharmony_ci	return NULL;
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciINDIRECT_CALLABLE_SCOPE int udp6_gro_complete(struct sk_buff *skb, int nhoff)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
16862306a36Sopenharmony_ci	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	/* do fraglist only if there is no outer UDP encap (or we already processed it) */
17162306a36Sopenharmony_ci	if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
17262306a36Sopenharmony_ci		uh->len = htons(skb->len - nhoff);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci		skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
17562306a36Sopenharmony_ci		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci		if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
17862306a36Sopenharmony_ci			if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
17962306a36Sopenharmony_ci				skb->csum_level++;
18062306a36Sopenharmony_ci		} else {
18162306a36Sopenharmony_ci			skb->ip_summed = CHECKSUM_UNNECESSARY;
18262306a36Sopenharmony_ci			skb->csum_level = 0;
18362306a36Sopenharmony_ci		}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci		return 0;
18662306a36Sopenharmony_ci	}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	if (uh->check)
18962306a36Sopenharmony_ci		uh->check = ~udp_v6_check(skb->len - nhoff, &ipv6h->saddr,
19062306a36Sopenharmony_ci					  &ipv6h->daddr, 0);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	return udp_gro_complete(skb, nhoff, udp6_lib_lookup_skb);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic const struct net_offload udpv6_offload = {
19662306a36Sopenharmony_ci	.callbacks = {
19762306a36Sopenharmony_ci		.gso_segment	=	udp6_ufo_fragment,
19862306a36Sopenharmony_ci		.gro_receive	=	udp6_gro_receive,
19962306a36Sopenharmony_ci		.gro_complete	=	udp6_gro_complete,
20062306a36Sopenharmony_ci	},
20162306a36Sopenharmony_ci};
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciint udpv6_offload_init(void)
20462306a36Sopenharmony_ci{
20562306a36Sopenharmony_ci	return inet6_add_offload(&udpv6_offload, IPPROTO_UDP);
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciint udpv6_offload_exit(void)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	return inet6_del_offload(&udpv6_offload, IPPROTO_UDP);
21162306a36Sopenharmony_ci}
212