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