18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	IPV6 GSO/GRO offload support
48c2ecf20Sopenharmony_ci *	Linux INET6 implementation
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *      UDPv6 GSO support
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
98c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
108c2ecf20Sopenharmony_ci#include <linux/indirect_call_wrapper.h>
118c2ecf20Sopenharmony_ci#include <net/protocol.h>
128c2ecf20Sopenharmony_ci#include <net/ipv6.h>
138c2ecf20Sopenharmony_ci#include <net/udp.h>
148c2ecf20Sopenharmony_ci#include <net/ip6_checksum.h>
158c2ecf20Sopenharmony_ci#include "ip6_offload.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
188c2ecf20Sopenharmony_ci					 netdev_features_t features)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	struct sk_buff *segs = ERR_PTR(-EINVAL);
218c2ecf20Sopenharmony_ci	unsigned int mss;
228c2ecf20Sopenharmony_ci	unsigned int unfrag_ip6hlen, unfrag_len;
238c2ecf20Sopenharmony_ci	struct frag_hdr *fptr;
248c2ecf20Sopenharmony_ci	u8 *packet_start, *prevhdr;
258c2ecf20Sopenharmony_ci	u8 nexthdr;
268c2ecf20Sopenharmony_ci	u8 frag_hdr_sz = sizeof(struct frag_hdr);
278c2ecf20Sopenharmony_ci	__wsum csum;
288c2ecf20Sopenharmony_ci	int tnl_hlen;
298c2ecf20Sopenharmony_ci	int err;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	mss = skb_shinfo(skb)->gso_size;
328c2ecf20Sopenharmony_ci	if (unlikely(skb->len <= mss))
338c2ecf20Sopenharmony_ci		goto out;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (skb->encapsulation && skb_shinfo(skb)->gso_type &
368c2ecf20Sopenharmony_ci	    (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))
378c2ecf20Sopenharmony_ci		segs = skb_udp_tunnel_segment(skb, features, true);
388c2ecf20Sopenharmony_ci	else {
398c2ecf20Sopenharmony_ci		const struct ipv6hdr *ipv6h;
408c2ecf20Sopenharmony_ci		struct udphdr *uh;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci		if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
438c2ecf20Sopenharmony_ci			goto out;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
468c2ecf20Sopenharmony_ci			goto out;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
498c2ecf20Sopenharmony_ci			return __udp_gso_segment(skb, features, true);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
528c2ecf20Sopenharmony_ci		 * do checksum of UDP packets sent as multiple IP fragments.
538c2ecf20Sopenharmony_ci		 */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		uh = udp_hdr(skb);
568c2ecf20Sopenharmony_ci		ipv6h = ipv6_hdr(skb);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		uh->check = 0;
598c2ecf20Sopenharmony_ci		csum = skb_checksum(skb, 0, skb->len, 0);
608c2ecf20Sopenharmony_ci		uh->check = udp_v6_check(skb->len, &ipv6h->saddr,
618c2ecf20Sopenharmony_ci					  &ipv6h->daddr, csum);
628c2ecf20Sopenharmony_ci		if (uh->check == 0)
638c2ecf20Sopenharmony_ci			uh->check = CSUM_MANGLED_0;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci		skb->ip_summed = CHECKSUM_UNNECESSARY;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci		/* If there is no outer header we can fake a checksum offload
688c2ecf20Sopenharmony_ci		 * due to the fact that we have already done the checksum in
698c2ecf20Sopenharmony_ci		 * software prior to segmenting the frame.
708c2ecf20Sopenharmony_ci		 */
718c2ecf20Sopenharmony_ci		if (!skb->encap_hdr_csum)
728c2ecf20Sopenharmony_ci			features |= NETIF_F_HW_CSUM;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		/* Check if there is enough headroom to insert fragment header. */
758c2ecf20Sopenharmony_ci		tnl_hlen = skb_tnl_header_len(skb);
768c2ecf20Sopenharmony_ci		if (skb->mac_header < (tnl_hlen + frag_hdr_sz)) {
778c2ecf20Sopenharmony_ci			if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
788c2ecf20Sopenharmony_ci				goto out;
798c2ecf20Sopenharmony_ci		}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci		/* Find the unfragmentable header and shift it left by frag_hdr_sz
828c2ecf20Sopenharmony_ci		 * bytes to insert fragment header.
838c2ecf20Sopenharmony_ci		 */
848c2ecf20Sopenharmony_ci		err = ip6_find_1stfragopt(skb, &prevhdr);
858c2ecf20Sopenharmony_ci		if (err < 0)
868c2ecf20Sopenharmony_ci			return ERR_PTR(err);
878c2ecf20Sopenharmony_ci		unfrag_ip6hlen = err;
888c2ecf20Sopenharmony_ci		nexthdr = *prevhdr;
898c2ecf20Sopenharmony_ci		*prevhdr = NEXTHDR_FRAGMENT;
908c2ecf20Sopenharmony_ci		unfrag_len = (skb_network_header(skb) - skb_mac_header(skb)) +
918c2ecf20Sopenharmony_ci			     unfrag_ip6hlen + tnl_hlen;
928c2ecf20Sopenharmony_ci		packet_start = (u8 *) skb->head + SKB_GSO_CB(skb)->mac_offset;
938c2ecf20Sopenharmony_ci		memmove(packet_start-frag_hdr_sz, packet_start, unfrag_len);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		SKB_GSO_CB(skb)->mac_offset -= frag_hdr_sz;
968c2ecf20Sopenharmony_ci		skb->mac_header -= frag_hdr_sz;
978c2ecf20Sopenharmony_ci		skb->network_header -= frag_hdr_sz;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1008c2ecf20Sopenharmony_ci		fptr->nexthdr = nexthdr;
1018c2ecf20Sopenharmony_ci		fptr->reserved = 0;
1028c2ecf20Sopenharmony_ci		fptr->identification = ipv6_proxy_select_ident(dev_net(skb->dev), skb);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		/* Fragment the skb. ipv6 header and the remaining fields of the
1058c2ecf20Sopenharmony_ci		 * fragment header are updated in ipv6_gso_segment()
1068c2ecf20Sopenharmony_ci		 */
1078c2ecf20Sopenharmony_ci		segs = skb_segment(skb, features);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciout:
1118c2ecf20Sopenharmony_ci	return segs;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct sock *udp6_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
1158c2ecf20Sopenharmony_ci					__be16 dport)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	const struct ipv6hdr *iph = skb_gro_network_header(skb);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
1208c2ecf20Sopenharmony_ci				 &iph->daddr, dport, inet6_iif(skb),
1218c2ecf20Sopenharmony_ci				 inet6_sdif(skb), &udp_table, NULL);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_SCOPE
1258c2ecf20Sopenharmony_cistruct sk_buff *udp6_gro_receive(struct list_head *head, struct sk_buff *skb)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct udphdr *uh = udp_gro_udphdr(skb);
1288c2ecf20Sopenharmony_ci	struct sock *sk = NULL;
1298c2ecf20Sopenharmony_ci	struct sk_buff *pp;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (unlikely(!uh))
1328c2ecf20Sopenharmony_ci		goto flush;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/* Don't bother verifying checksum if we're going to flush anyway. */
1358c2ecf20Sopenharmony_ci	if (NAPI_GRO_CB(skb)->flush)
1368c2ecf20Sopenharmony_ci		goto skip;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
1398c2ecf20Sopenharmony_ci						 ip6_gro_compute_pseudo))
1408c2ecf20Sopenharmony_ci		goto flush;
1418c2ecf20Sopenharmony_ci	else if (uh->check)
1428c2ecf20Sopenharmony_ci		skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
1438c2ecf20Sopenharmony_ci					     ip6_gro_compute_pseudo);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciskip:
1468c2ecf20Sopenharmony_ci	NAPI_GRO_CB(skb)->is_ipv6 = 1;
1478c2ecf20Sopenharmony_ci	rcu_read_lock();
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (static_branch_unlikely(&udpv6_encap_needed_key))
1508c2ecf20Sopenharmony_ci		sk = udp6_gro_lookup_skb(skb, uh->source, uh->dest);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	pp = udp_gro_receive(head, skb, uh, sk);
1538c2ecf20Sopenharmony_ci	rcu_read_unlock();
1548c2ecf20Sopenharmony_ci	return pp;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciflush:
1578c2ecf20Sopenharmony_ci	NAPI_GRO_CB(skb)->flush = 1;
1588c2ecf20Sopenharmony_ci	return NULL;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_SCOPE int udp6_gro_complete(struct sk_buff *skb, int nhoff)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1648c2ecf20Sopenharmony_ci	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (NAPI_GRO_CB(skb)->is_flist) {
1678c2ecf20Sopenharmony_ci		uh->len = htons(skb->len - nhoff);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
1708c2ecf20Sopenharmony_ci		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1738c2ecf20Sopenharmony_ci			if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
1748c2ecf20Sopenharmony_ci				skb->csum_level++;
1758c2ecf20Sopenharmony_ci		} else {
1768c2ecf20Sopenharmony_ci			skb->ip_summed = CHECKSUM_UNNECESSARY;
1778c2ecf20Sopenharmony_ci			skb->csum_level = 0;
1788c2ecf20Sopenharmony_ci		}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		return 0;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (uh->check)
1848c2ecf20Sopenharmony_ci		uh->check = ~udp_v6_check(skb->len - nhoff, &ipv6h->saddr,
1858c2ecf20Sopenharmony_ci					  &ipv6h->daddr, 0);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return udp_gro_complete(skb, nhoff, udp6_lib_lookup_skb);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic const struct net_offload udpv6_offload = {
1918c2ecf20Sopenharmony_ci	.callbacks = {
1928c2ecf20Sopenharmony_ci		.gso_segment	=	udp6_ufo_fragment,
1938c2ecf20Sopenharmony_ci		.gro_receive	=	udp6_gro_receive,
1948c2ecf20Sopenharmony_ci		.gro_complete	=	udp6_gro_complete,
1958c2ecf20Sopenharmony_ci	},
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciint udpv6_offload_init(void)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	return inet6_add_offload(&udpv6_offload, IPPROTO_UDP);
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciint udpv6_offload_exit(void)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	return inet6_del_offload(&udpv6_offload, IPPROTO_UDP);
2068c2ecf20Sopenharmony_ci}
207