xref: /linux/net/ipv6/udp_offload.c (revision b803c4a4f78834b31ebfbbcea350473333760559)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPV6 GSO/GRO offload support
4  *	Linux INET6 implementation
5  *
6  *      UDPv6 GSO support
7  */
8 #include <linux/skbuff.h>
9 #include <linux/netdevice.h>
10 #include <linux/indirect_call_wrapper.h>
11 #include <net/protocol.h>
12 #include <net/ipv6.h>
13 #include <net/udp.h>
14 #include <net/ip6_checksum.h>
15 #include "ip6_offload.h"
16 #include <net/gro.h>
17 #include <net/gso.h>
18 
19 static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
20 					 netdev_features_t features)
21 {
22 	struct sk_buff *segs = ERR_PTR(-EINVAL);
23 	unsigned int mss;
24 	unsigned int unfrag_ip6hlen, unfrag_len;
25 	struct frag_hdr *fptr;
26 	u8 *packet_start, *prevhdr;
27 	u8 nexthdr;
28 	u8 frag_hdr_sz = sizeof(struct frag_hdr);
29 	__wsum csum;
30 	int tnl_hlen;
31 	int err;
32 
33 	if (skb->encapsulation && skb_shinfo(skb)->gso_type &
34 	    (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))
35 		segs = skb_udp_tunnel_segment(skb, features, true);
36 	else {
37 		const struct ipv6hdr *ipv6h;
38 		struct udphdr *uh;
39 
40 		if (!(skb_shinfo(skb)->gso_type & (SKB_GSO_UDP | SKB_GSO_UDP_L4)))
41 			goto out;
42 
43 		if (!pskb_may_pull(skb, sizeof(struct udphdr)))
44 			goto out;
45 
46 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
47 			return __udp_gso_segment(skb, features, true);
48 
49 		mss = skb_shinfo(skb)->gso_size;
50 		if (unlikely(skb->len <= mss))
51 			goto out;
52 
53 		/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
54 		 * do checksum of UDP packets sent as multiple IP fragments.
55 		 */
56 
57 		uh = udp_hdr(skb);
58 		ipv6h = ipv6_hdr(skb);
59 
60 		uh->check = 0;
61 		csum = skb_checksum(skb, 0, skb->len, 0);
62 		uh->check = udp_v6_check(skb->len, &ipv6h->saddr,
63 					  &ipv6h->daddr, csum);
64 		if (uh->check == 0)
65 			uh->check = CSUM_MANGLED_0;
66 
67 		skb->ip_summed = CHECKSUM_UNNECESSARY;
68 
69 		/* If there is no outer header we can fake a checksum offload
70 		 * due to the fact that we have already done the checksum in
71 		 * software prior to segmenting the frame.
72 		 */
73 		if (!skb->encap_hdr_csum)
74 			features |= NETIF_F_HW_CSUM;
75 
76 		/* Check if there is enough headroom to insert fragment header. */
77 		tnl_hlen = skb_tnl_header_len(skb);
78 		if (skb->mac_header < (tnl_hlen + frag_hdr_sz)) {
79 			if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
80 				goto out;
81 		}
82 
83 		/* Find the unfragmentable header and shift it left by frag_hdr_sz
84 		 * bytes to insert fragment header.
85 		 */
86 		err = ip6_find_1stfragopt(skb, &prevhdr);
87 		if (err < 0)
88 			return ERR_PTR(err);
89 		unfrag_ip6hlen = err;
90 		nexthdr = *prevhdr;
91 		*prevhdr = NEXTHDR_FRAGMENT;
92 		unfrag_len = (skb_network_header(skb) - skb_mac_header(skb)) +
93 			     unfrag_ip6hlen + tnl_hlen;
94 		packet_start = (u8 *) skb->head + SKB_GSO_CB(skb)->mac_offset;
95 		memmove(packet_start-frag_hdr_sz, packet_start, unfrag_len);
96 
97 		SKB_GSO_CB(skb)->mac_offset -= frag_hdr_sz;
98 		skb->mac_header -= frag_hdr_sz;
99 		skb->network_header -= frag_hdr_sz;
100 
101 		fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
102 		fptr->nexthdr = nexthdr;
103 		fptr->reserved = 0;
104 		fptr->identification = ipv6_proxy_select_ident(dev_net(skb->dev), skb);
105 
106 		/* Fragment the skb. ipv6 header and the remaining fields of the
107 		 * fragment header are updated in ipv6_gso_segment()
108 		 */
109 		segs = skb_segment(skb, features);
110 	}
111 
112 out:
113 	return segs;
114 }
115 
116 static struct sock *udp6_gro_lookup_skb(struct sk_buff *skb, __be16 sport,
117 					__be16 dport)
118 {
119 	const struct ipv6hdr *iph = skb_gro_network_header(skb);
120 	struct net *net = dev_net_rcu(skb->dev);
121 	struct sock *sk;
122 	int iif, sdif;
123 
124 	sk = udp_tunnel_sk(net, true);
125 	if (sk && dport == htons(sk->sk_num))
126 		return sk;
127 
128 	inet6_get_iif_sdif(skb, &iif, &sdif);
129 
130 	return __udp6_lib_lookup(net, &iph->saddr, sport,
131 				 &iph->daddr, dport, iif,
132 				 sdif, net->ipv4.udp_table, NULL);
133 }
134 
135 INDIRECT_CALLABLE_SCOPE
136 struct sk_buff *udp6_gro_receive(struct list_head *head, struct sk_buff *skb)
137 {
138 	struct udphdr *uh = udp_gro_udphdr(skb);
139 	struct sock *sk = NULL;
140 	struct sk_buff *pp;
141 
142 	if (unlikely(!uh))
143 		goto flush;
144 
145 	/* Don't bother verifying checksum if we're going to flush anyway. */
146 	if (NAPI_GRO_CB(skb)->flush)
147 		goto skip;
148 
149 	if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
150 						 ip6_gro_compute_pseudo))
151 		goto flush;
152 	else if (uh->check)
153 		skb_gro_checksum_try_convert(skb, IPPROTO_UDP,
154 					     ip6_gro_compute_pseudo);
155 
156 skip:
157 	NAPI_GRO_CB(skb)->is_ipv6 = 1;
158 
159 	if (static_branch_unlikely(&udpv6_encap_needed_key))
160 		sk = udp6_gro_lookup_skb(skb, uh->source, uh->dest);
161 
162 	pp = udp_gro_receive(head, skb, uh, sk);
163 	return pp;
164 
165 flush:
166 	NAPI_GRO_CB(skb)->flush = 1;
167 	return NULL;
168 }
169 
170 INDIRECT_CALLABLE_SCOPE int udp6_gro_complete(struct sk_buff *skb, int nhoff)
171 {
172 	const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
173 	const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + offset);
174 	struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
175 
176 	/* do fraglist only if there is no outer UDP encap (or we already processed it) */
177 	if (NAPI_GRO_CB(skb)->is_flist && !NAPI_GRO_CB(skb)->encap_mark) {
178 		uh->len = htons(skb->len - nhoff);
179 
180 		skb_shinfo(skb)->gso_type |= (SKB_GSO_FRAGLIST|SKB_GSO_UDP_L4);
181 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
182 
183 		__skb_incr_checksum_unnecessary(skb);
184 
185 		return 0;
186 	}
187 
188 	if (uh->check)
189 		uh->check = ~udp_v6_check(skb->len - nhoff, &ipv6h->saddr,
190 					  &ipv6h->daddr, 0);
191 
192 	return udp_gro_complete(skb, nhoff, udp6_lib_lookup_skb);
193 }
194 
195 int __init udpv6_offload_init(void)
196 {
197 	net_hotdata.udpv6_offload = (struct net_offload) {
198 		.callbacks = {
199 			.gso_segment	=	udp6_ufo_fragment,
200 			.gro_receive	=	udp6_gro_receive,
201 			.gro_complete	=	udp6_gro_complete,
202 		},
203 	};
204 	return inet6_add_offload(&net_hotdata.udpv6_offload, IPPROTO_UDP);
205 }
206 
207 int udpv6_offload_exit(void)
208 {
209 	return inet6_del_offload(&net_hotdata.udpv6_offload, IPPROTO_UDP);
210 }
211