xref: /linux/net/ipv4/tcp_offload.c (revision a53f48b6327c12437c9f429da2283e526eda2362)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPV4 GSO/GRO offload support
4  *	Linux INET implementation
5  *
6  *	TCPv4 GSO/GRO support
7  */
8 
9 #include <linux/indirect_call_wrapper.h>
10 #include <linux/skbuff.h>
11 #include <net/gro.h>
12 #include <net/gso.h>
13 #include <net/tcp.h>
14 #include <net/protocol.h>
15 
16 static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
17 			   unsigned int seq, unsigned int mss)
18 {
19 	while (skb) {
20 		if (before(ts_seq, seq + mss)) {
21 			skb_shinfo(skb)->tx_flags |= SKBTX_SW_TSTAMP;
22 			skb_shinfo(skb)->tskey = ts_seq;
23 			return;
24 		}
25 
26 		skb = skb->next;
27 		seq += mss;
28 	}
29 }
30 
31 static void __tcpv4_gso_segment_csum(struct sk_buff *seg,
32 				     __be32 *oldip, __be32 newip,
33 				     __be16 *oldport, __be16 newport)
34 {
35 	struct tcphdr *th;
36 	struct iphdr *iph;
37 
38 	if (*oldip == newip && *oldport == newport)
39 		return;
40 
41 	th = tcp_hdr(seg);
42 	iph = ip_hdr(seg);
43 
44 	inet_proto_csum_replace4(&th->check, seg, *oldip, newip, true);
45 	inet_proto_csum_replace2(&th->check, seg, *oldport, newport, false);
46 	*oldport = newport;
47 
48 	csum_replace4(&iph->check, *oldip, newip);
49 	*oldip = newip;
50 }
51 
52 static struct sk_buff *__tcpv4_gso_segment_list_csum(struct sk_buff *segs)
53 {
54 	const struct tcphdr *th;
55 	const struct iphdr *iph;
56 	struct sk_buff *seg;
57 	struct tcphdr *th2;
58 	struct iphdr *iph2;
59 
60 	seg = segs;
61 	th = tcp_hdr(seg);
62 	iph = ip_hdr(seg);
63 	th2 = tcp_hdr(seg->next);
64 	iph2 = ip_hdr(seg->next);
65 
66 	if (!(*(const u32 *)&th->source ^ *(const u32 *)&th2->source) &&
67 	    iph->daddr == iph2->daddr && iph->saddr == iph2->saddr)
68 		return segs;
69 
70 	while ((seg = seg->next)) {
71 		th2 = tcp_hdr(seg);
72 		iph2 = ip_hdr(seg);
73 
74 		__tcpv4_gso_segment_csum(seg,
75 					 &iph2->saddr, iph->saddr,
76 					 &th2->source, th->source);
77 		__tcpv4_gso_segment_csum(seg,
78 					 &iph2->daddr, iph->daddr,
79 					 &th2->dest, th->dest);
80 	}
81 
82 	return segs;
83 }
84 
85 static struct sk_buff *__tcp4_gso_segment_list(struct sk_buff *skb,
86 					      netdev_features_t features)
87 {
88 	skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
89 	if (IS_ERR(skb))
90 		return skb;
91 
92 	return __tcpv4_gso_segment_list_csum(skb);
93 }
94 
95 static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
96 					netdev_features_t features)
97 {
98 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4))
99 		return ERR_PTR(-EINVAL);
100 
101 	if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
102 		return ERR_PTR(-EINVAL);
103 
104 	if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST)
105 		return __tcp4_gso_segment_list(skb, features);
106 
107 	if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
108 		const struct iphdr *iph = ip_hdr(skb);
109 		struct tcphdr *th = tcp_hdr(skb);
110 
111 		/* Set up checksum pseudo header, usually expect stack to
112 		 * have done this already.
113 		 */
114 
115 		th->check = 0;
116 		skb->ip_summed = CHECKSUM_PARTIAL;
117 		__tcp_v4_send_check(skb, iph->saddr, iph->daddr);
118 	}
119 
120 	return tcp_gso_segment(skb, features);
121 }
122 
123 struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
124 				netdev_features_t features)
125 {
126 	struct sk_buff *segs = ERR_PTR(-EINVAL);
127 	unsigned int sum_truesize = 0;
128 	struct tcphdr *th;
129 	unsigned int thlen;
130 	unsigned int seq;
131 	unsigned int oldlen;
132 	unsigned int mss;
133 	struct sk_buff *gso_skb = skb;
134 	__sum16 newcheck;
135 	bool ooo_okay, copy_destructor;
136 	__wsum delta;
137 
138 	th = tcp_hdr(skb);
139 	thlen = th->doff * 4;
140 	if (thlen < sizeof(*th))
141 		goto out;
142 
143 	if (unlikely(skb_checksum_start(skb) != skb_transport_header(skb)))
144 		goto out;
145 
146 	if (!pskb_may_pull(skb, thlen))
147 		goto out;
148 
149 	oldlen = ~skb->len;
150 	__skb_pull(skb, thlen);
151 
152 	mss = skb_shinfo(skb)->gso_size;
153 	if (unlikely(skb->len <= mss))
154 		goto out;
155 
156 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
157 		/* Packet is from an untrusted source, reset gso_segs. */
158 
159 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
160 
161 		segs = NULL;
162 		goto out;
163 	}
164 
165 	copy_destructor = gso_skb->destructor == tcp_wfree;
166 	ooo_okay = gso_skb->ooo_okay;
167 	/* All segments but the first should have ooo_okay cleared */
168 	skb->ooo_okay = 0;
169 
170 	segs = skb_segment(skb, features);
171 	if (IS_ERR(segs))
172 		goto out;
173 
174 	/* Only first segment might have ooo_okay set */
175 	segs->ooo_okay = ooo_okay;
176 
177 	/* GSO partial and frag_list segmentation only requires splitting
178 	 * the frame into an MSS multiple and possibly a remainder, both
179 	 * cases return a GSO skb. So update the mss now.
180 	 */
181 	if (skb_is_gso(segs))
182 		mss *= skb_shinfo(segs)->gso_segs;
183 
184 	delta = (__force __wsum)htonl(oldlen + thlen + mss);
185 
186 	skb = segs;
187 	th = tcp_hdr(skb);
188 	seq = ntohl(th->seq);
189 
190 	if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
191 		tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
192 
193 	newcheck = ~csum_fold(csum_add(csum_unfold(th->check), delta));
194 
195 	while (skb->next) {
196 		th->fin = th->psh = 0;
197 		th->check = newcheck;
198 
199 		if (skb->ip_summed == CHECKSUM_PARTIAL)
200 			gso_reset_checksum(skb, ~th->check);
201 		else
202 			th->check = gso_make_checksum(skb, ~th->check);
203 
204 		seq += mss;
205 		if (copy_destructor) {
206 			skb->destructor = gso_skb->destructor;
207 			skb->sk = gso_skb->sk;
208 			sum_truesize += skb->truesize;
209 		}
210 		skb = skb->next;
211 		th = tcp_hdr(skb);
212 
213 		th->seq = htonl(seq);
214 		th->cwr = 0;
215 	}
216 
217 	/* Following permits TCP Small Queues to work well with GSO :
218 	 * The callback to TCP stack will be called at the time last frag
219 	 * is freed at TX completion, and not right now when gso_skb
220 	 * is freed by GSO engine
221 	 */
222 	if (copy_destructor) {
223 		int delta;
224 
225 		swap(gso_skb->sk, skb->sk);
226 		swap(gso_skb->destructor, skb->destructor);
227 		sum_truesize += skb->truesize;
228 		delta = sum_truesize - gso_skb->truesize;
229 		/* In some pathological cases, delta can be negative.
230 		 * We need to either use refcount_add() or refcount_sub_and_test()
231 		 */
232 		if (likely(delta >= 0))
233 			refcount_add(delta, &skb->sk->sk_wmem_alloc);
234 		else
235 			WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
236 	}
237 
238 	delta = (__force __wsum)htonl(oldlen +
239 				      (skb_tail_pointer(skb) -
240 				       skb_transport_header(skb)) +
241 				      skb->data_len);
242 	th->check = ~csum_fold(csum_add(csum_unfold(th->check), delta));
243 	if (skb->ip_summed == CHECKSUM_PARTIAL)
244 		gso_reset_checksum(skb, ~th->check);
245 	else
246 		th->check = gso_make_checksum(skb, ~th->check);
247 out:
248 	return segs;
249 }
250 
251 struct sk_buff *tcp_gro_lookup(struct list_head *head, struct tcphdr *th)
252 {
253 	struct tcphdr *th2;
254 	struct sk_buff *p;
255 
256 	list_for_each_entry(p, head, list) {
257 		if (!NAPI_GRO_CB(p)->same_flow)
258 			continue;
259 
260 		th2 = tcp_hdr(p);
261 		if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
262 			NAPI_GRO_CB(p)->same_flow = 0;
263 			continue;
264 		}
265 
266 		return p;
267 	}
268 
269 	return NULL;
270 }
271 
272 struct tcphdr *tcp_gro_pull_header(struct sk_buff *skb)
273 {
274 	unsigned int thlen, hlen, off;
275 	struct tcphdr *th;
276 
277 	off = skb_gro_offset(skb);
278 	hlen = off + sizeof(*th);
279 	th = skb_gro_header(skb, hlen, off);
280 	if (unlikely(!th))
281 		return NULL;
282 
283 	thlen = th->doff * 4;
284 	if (thlen < sizeof(*th))
285 		return NULL;
286 
287 	hlen = off + thlen;
288 	if (!skb_gro_may_pull(skb, hlen)) {
289 		th = skb_gro_header_slow(skb, hlen, off);
290 		if (unlikely(!th))
291 			return NULL;
292 	}
293 
294 	skb_gro_pull(skb, thlen);
295 
296 	return th;
297 }
298 
299 struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb,
300 				struct tcphdr *th)
301 {
302 	unsigned int thlen = th->doff * 4;
303 	struct sk_buff *pp = NULL;
304 	struct sk_buff *p;
305 	struct tcphdr *th2;
306 	unsigned int len;
307 	__be32 flags;
308 	unsigned int mss = 1;
309 	int flush = 1;
310 	int i;
311 
312 	len = skb_gro_len(skb);
313 	flags = tcp_flag_word(th);
314 
315 	p = tcp_gro_lookup(head, th);
316 	if (!p)
317 		goto out_check_final;
318 
319 	th2 = tcp_hdr(p);
320 	flush = (__force int)(flags & TCP_FLAG_CWR);
321 	flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
322 		  ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
323 	flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
324 	for (i = sizeof(*th); i < thlen; i += 4)
325 		flush |= *(u32 *)((u8 *)th + i) ^
326 			 *(u32 *)((u8 *)th2 + i);
327 
328 	flush |= gro_receive_network_flush(th, th2, p);
329 
330 	mss = skb_shinfo(p)->gso_size;
331 
332 	/* If skb is a GRO packet, make sure its gso_size matches prior packet mss.
333 	 * If it is a single frame, do not aggregate it if its length
334 	 * is bigger than our mss.
335 	 */
336 	if (unlikely(skb_is_gso(skb)))
337 		flush |= (mss != skb_shinfo(skb)->gso_size);
338 	else
339 		flush |= (len - 1) >= mss;
340 
341 	flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
342 	flush |= skb_cmp_decrypted(p, skb);
343 
344 	if (unlikely(NAPI_GRO_CB(p)->is_flist)) {
345 		flush |= (__force int)(flags ^ tcp_flag_word(th2));
346 		flush |= skb->ip_summed != p->ip_summed;
347 		flush |= skb->csum_level != p->csum_level;
348 		flush |= NAPI_GRO_CB(p)->count >= 64;
349 
350 		if (flush || skb_gro_receive_list(p, skb))
351 			mss = 1;
352 
353 		goto out_check_final;
354 	}
355 
356 	if (flush || skb_gro_receive(p, skb)) {
357 		mss = 1;
358 		goto out_check_final;
359 	}
360 
361 	tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
362 
363 out_check_final:
364 	/* Force a flush if last segment is smaller than mss. */
365 	if (unlikely(skb_is_gso(skb)))
366 		flush = len != NAPI_GRO_CB(skb)->count * skb_shinfo(skb)->gso_size;
367 	else
368 		flush = len < mss;
369 
370 	flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
371 					TCP_FLAG_RST | TCP_FLAG_SYN |
372 					TCP_FLAG_FIN));
373 
374 	if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
375 		pp = p;
376 
377 	NAPI_GRO_CB(skb)->flush |= (flush != 0);
378 
379 	return pp;
380 }
381 
382 void tcp_gro_complete(struct sk_buff *skb)
383 {
384 	struct tcphdr *th = tcp_hdr(skb);
385 	struct skb_shared_info *shinfo;
386 
387 	if (skb->encapsulation)
388 		skb->inner_transport_header = skb->transport_header;
389 
390 	skb->csum_start = (unsigned char *)th - skb->head;
391 	skb->csum_offset = offsetof(struct tcphdr, check);
392 	skb->ip_summed = CHECKSUM_PARTIAL;
393 
394 	shinfo = skb_shinfo(skb);
395 	shinfo->gso_segs = NAPI_GRO_CB(skb)->count;
396 
397 	if (th->cwr)
398 		shinfo->gso_type |= SKB_GSO_TCP_ECN;
399 }
400 EXPORT_SYMBOL(tcp_gro_complete);
401 
402 static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
403 				    struct tcphdr *th)
404 {
405 	const struct iphdr *iph;
406 	struct sk_buff *p;
407 	struct sock *sk;
408 	struct net *net;
409 	int iif, sdif;
410 
411 	if (likely(!(skb->dev->features & NETIF_F_GRO_FRAGLIST)))
412 		return;
413 
414 	p = tcp_gro_lookup(head, th);
415 	if (p) {
416 		NAPI_GRO_CB(skb)->is_flist = NAPI_GRO_CB(p)->is_flist;
417 		return;
418 	}
419 
420 	inet_get_iif_sdif(skb, &iif, &sdif);
421 	iph = skb_gro_network_header(skb);
422 	net = dev_net(skb->dev);
423 	sk = __inet_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
424 				       iph->saddr, th->source,
425 				       iph->daddr, ntohs(th->dest),
426 				       iif, sdif);
427 	NAPI_GRO_CB(skb)->is_flist = !sk;
428 	if (sk)
429 		sock_put(sk);
430 }
431 
432 INDIRECT_CALLABLE_SCOPE
433 struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
434 {
435 	struct tcphdr *th;
436 
437 	/* Don't bother verifying checksum if we're going to flush anyway. */
438 	if (!NAPI_GRO_CB(skb)->flush &&
439 	    skb_gro_checksum_validate(skb, IPPROTO_TCP,
440 				      inet_gro_compute_pseudo))
441 		goto flush;
442 
443 	th = tcp_gro_pull_header(skb);
444 	if (!th)
445 		goto flush;
446 
447 	tcp4_check_fraglist_gro(head, skb, th);
448 
449 	return tcp_gro_receive(head, skb, th);
450 
451 flush:
452 	NAPI_GRO_CB(skb)->flush = 1;
453 	return NULL;
454 }
455 
456 INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
457 {
458 	const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
459 	const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
460 	struct tcphdr *th = tcp_hdr(skb);
461 
462 	if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
463 		skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
464 		skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
465 
466 		__skb_incr_checksum_unnecessary(skb);
467 
468 		return 0;
469 	}
470 
471 	th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
472 				  iph->daddr, 0);
473 
474 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
475 			(NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
476 
477 	tcp_gro_complete(skb);
478 	return 0;
479 }
480 
481 int __init tcpv4_offload_init(void)
482 {
483 	net_hotdata.tcpv4_offload = (struct net_offload) {
484 		.callbacks = {
485 			.gso_segment	=	tcp4_gso_segment,
486 			.gro_receive	=	tcp4_gro_receive,
487 			.gro_complete	=	tcp4_gro_complete,
488 		},
489 	};
490 	return inet_add_offload(&net_hotdata.tcpv4_offload, IPPROTO_TCP);
491 }
492