blob: 0fbf7d4df9dad74e06d70305ede4ac2fa8a3265f [file] [log] [blame]
Daniel Borkmann28850dc2013-06-07 05:11:46 +00001/*
2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * TCPv4 GSO/GRO support
11 */
12
Paolo Abeni028e0a42018-12-14 11:51:59 +010013#include <linux/indirect_call_wrapper.h>
Daniel Borkmann28850dc2013-06-07 05:11:46 +000014#include <linux/skbuff.h>
15#include <net/tcp.h>
16#include <net/protocol.h>
17
Willem de Bruijnf066e2b2014-08-06 15:09:44 -040018static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
19 unsigned int seq, unsigned int mss)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -040020{
21 while (skb) {
Willem de Bruijnf066e2b2014-08-06 15:09:44 -040022 if (before(ts_seq, seq + mss)) {
23 skb_shinfo(skb)->tx_flags |= SKBTX_SW_TSTAMP;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -040024 skb_shinfo(skb)->tskey = ts_seq;
25 return;
26 }
27
28 skb = skb->next;
29 seq += mss;
30 }
31}
32
Eric Dumazet74abc202015-02-26 19:08:59 -080033static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
34 netdev_features_t features)
Tom Herbertd020f8f2014-09-20 14:52:28 -070035{
Willem de Bruijn121d57a2018-01-19 09:29:18 -050036 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4))
37 return ERR_PTR(-EINVAL);
38
Tom Herbertd020f8f2014-09-20 14:52:28 -070039 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
40 return ERR_PTR(-EINVAL);
41
42 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
43 const struct iphdr *iph = ip_hdr(skb);
44 struct tcphdr *th = tcp_hdr(skb);
45
46 /* Set up checksum pseudo header, usually expect stack to
47 * have done this already.
48 */
49
50 th->check = 0;
51 skb->ip_summed = CHECKSUM_PARTIAL;
52 __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
53 }
54
55 return tcp_gso_segment(skb, features);
56}
57
Eric Dumazet28be6e02013-10-18 10:36:17 -070058struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
Daniel Borkmann28850dc2013-06-07 05:11:46 +000059 netdev_features_t features)
60{
61 struct sk_buff *segs = ERR_PTR(-EINVAL);
Eric Dumazet0d08c422013-10-25 17:26:17 -070062 unsigned int sum_truesize = 0;
Daniel Borkmann28850dc2013-06-07 05:11:46 +000063 struct tcphdr *th;
64 unsigned int thlen;
65 unsigned int seq;
66 __be32 delta;
67 unsigned int oldlen;
68 unsigned int mss;
69 struct sk_buff *gso_skb = skb;
70 __sum16 newcheck;
71 bool ooo_okay, copy_destructor;
72
Daniel Borkmann28850dc2013-06-07 05:11:46 +000073 th = tcp_hdr(skb);
74 thlen = th->doff * 4;
75 if (thlen < sizeof(*th))
76 goto out;
77
78 if (!pskb_may_pull(skb, thlen))
79 goto out;
80
81 oldlen = (u16)~skb->len;
82 __skb_pull(skb, thlen);
83
Eric Dumazeta7eea412015-06-11 09:15:15 -070084 mss = skb_shinfo(skb)->gso_size;
Daniel Borkmann28850dc2013-06-07 05:11:46 +000085 if (unlikely(skb->len <= mss))
86 goto out;
87
88 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
89 /* Packet is from an untrusted source, reset gso_segs. */
Daniel Borkmann28850dc2013-06-07 05:11:46 +000090
91 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
92
93 segs = NULL;
94 goto out;
95 }
96
97 copy_destructor = gso_skb->destructor == tcp_wfree;
98 ooo_okay = gso_skb->ooo_okay;
99 /* All segments but the first should have ooo_okay cleared */
100 skb->ooo_okay = 0;
101
102 segs = skb_segment(skb, features);
103 if (IS_ERR(segs))
104 goto out;
105
106 /* Only first segment might have ooo_okay set */
107 segs->ooo_okay = ooo_okay;
108
Steffen Klassert07b26c92016-09-19 12:58:47 +0200109 /* GSO partial and frag_list segmentation only requires splitting
110 * the frame into an MSS multiple and possibly a remainder, both
111 * cases return a GSO skb. So update the mss now.
112 */
113 if (skb_is_gso(segs))
114 mss *= skb_shinfo(segs)->gso_segs;
115
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000116 delta = htonl(oldlen + (thlen + mss));
117
118 skb = segs;
119 th = tcp_hdr(skb);
120 seq = ntohl(th->seq);
121
Willem de Bruijn4ed2d762014-08-04 22:11:49 -0400122 if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
123 tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
124
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000125 newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
126 (__force u32)delta));
127
Alexander Duyck802ab552016-04-10 21:45:03 -0400128 while (skb->next) {
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000129 th->fin = th->psh = 0;
130 th->check = newcheck;
131
Alexander Duyck08b64fc2016-02-05 15:27:49 -0800132 if (skb->ip_summed == CHECKSUM_PARTIAL)
133 gso_reset_checksum(skb, ~th->check);
134 else
Tom Herberte9c3a242014-06-04 17:20:09 -0700135 th->check = gso_make_checksum(skb, ~th->check);
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000136
137 seq += mss;
138 if (copy_destructor) {
139 skb->destructor = gso_skb->destructor;
140 skb->sk = gso_skb->sk;
Eric Dumazet0d08c422013-10-25 17:26:17 -0700141 sum_truesize += skb->truesize;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000142 }
143 skb = skb->next;
144 th = tcp_hdr(skb);
145
146 th->seq = htonl(seq);
147 th->cwr = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -0400148 }
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000149
150 /* Following permits TCP Small Queues to work well with GSO :
151 * The callback to TCP stack will be called at the time last frag
152 * is freed at TX completion, and not right now when gso_skb
153 * is freed by GSO engine
154 */
155 if (copy_destructor) {
Eric Dumazet7ec318f2017-11-07 15:15:04 -0800156 int delta;
157
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000158 swap(gso_skb->sk, skb->sk);
159 swap(gso_skb->destructor, skb->destructor);
Eric Dumazet0d08c422013-10-25 17:26:17 -0700160 sum_truesize += skb->truesize;
Eric Dumazet7ec318f2017-11-07 15:15:04 -0800161 delta = sum_truesize - gso_skb->truesize;
162 /* In some pathological cases, delta can be negative.
163 * We need to either use refcount_add() or refcount_sub_and_test()
164 */
165 if (likely(delta >= 0))
166 refcount_add(delta, &skb->sk->sk_wmem_alloc);
167 else
168 WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000169 }
170
171 delta = htonl(oldlen + (skb_tail_pointer(skb) -
172 skb_transport_header(skb)) +
173 skb->data_len);
174 th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
175 (__force u32)delta));
Alexander Duyck08b64fc2016-02-05 15:27:49 -0800176 if (skb->ip_summed == CHECKSUM_PARTIAL)
177 gso_reset_checksum(skb, ~th->check);
178 else
Tom Herberte9c3a242014-06-04 17:20:09 -0700179 th->check = gso_make_checksum(skb, ~th->check);
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000180out:
181 return segs;
182}
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000183
David Millerd4546c22018-06-24 14:13:49 +0900184struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb)
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000185{
David Millerd4546c22018-06-24 14:13:49 +0900186 struct sk_buff *pp = NULL;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000187 struct sk_buff *p;
188 struct tcphdr *th;
189 struct tcphdr *th2;
190 unsigned int len;
191 unsigned int thlen;
192 __be32 flags;
193 unsigned int mss = 1;
194 unsigned int hlen;
195 unsigned int off;
196 int flush = 1;
197 int i;
198
199 off = skb_gro_offset(skb);
200 hlen = off + sizeof(*th);
201 th = skb_gro_header_fast(skb, off);
202 if (skb_gro_header_hard(skb, hlen)) {
203 th = skb_gro_header_slow(skb, hlen, off);
204 if (unlikely(!th))
205 goto out;
206 }
207
208 thlen = th->doff * 4;
209 if (thlen < sizeof(*th))
210 goto out;
211
212 hlen = off + thlen;
213 if (skb_gro_header_hard(skb, hlen)) {
214 th = skb_gro_header_slow(skb, hlen, off);
215 if (unlikely(!th))
216 goto out;
217 }
218
219 skb_gro_pull(skb, thlen);
220
221 len = skb_gro_len(skb);
222 flags = tcp_flag_word(th);
223
David Millerd4546c22018-06-24 14:13:49 +0900224 list_for_each_entry(p, head, list) {
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000225 if (!NAPI_GRO_CB(p)->same_flow)
226 continue;
227
228 th2 = tcp_hdr(p);
229
230 if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
231 NAPI_GRO_CB(p)->same_flow = 0;
232 continue;
233 }
234
235 goto found;
236 }
David Millerd4546c22018-06-24 14:13:49 +0900237 p = NULL;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000238 goto out_check_final;
239
240found:
Jerry Chubf5a7552014-01-07 10:23:19 -0800241 /* Include the IP ID check below from the inner most IP hdr */
Alexander Duyck15305452016-04-10 21:44:57 -0400242 flush = NAPI_GRO_CB(p)->flush;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000243 flush |= (__force int)(flags & TCP_FLAG_CWR);
244 flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
245 ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
246 flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
247 for (i = sizeof(*th); i < thlen; i += 4)
248 flush |= *(u32 *)((u8 *)th + i) ^
249 *(u32 *)((u8 *)th2 + i);
250
Alexander Duyck15305452016-04-10 21:44:57 -0400251 /* When we receive our second frame we can made a decision on if we
252 * continue this flow as an atomic flow with a fixed ID or if we use
253 * an incrementing ID.
254 */
255 if (NAPI_GRO_CB(p)->flush_id != 1 ||
256 NAPI_GRO_CB(p)->count != 1 ||
257 !NAPI_GRO_CB(p)->is_atomic)
258 flush |= NAPI_GRO_CB(p)->flush_id;
259 else
260 NAPI_GRO_CB(p)->is_atomic = false;
261
Eric Dumazeta7eea412015-06-11 09:15:15 -0700262 mss = skb_shinfo(p)->gso_size;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000263
264 flush |= (len - 1) >= mss;
265 flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
Boris Pismenny41ed9c02018-07-13 14:33:38 +0300266#ifdef CONFIG_TLS_DEVICE
267 flush |= p->decrypted ^ skb->decrypted;
268#endif
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000269
David Millerd4546c22018-06-24 14:13:49 +0900270 if (flush || skb_gro_receive(p, skb)) {
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000271 mss = 1;
272 goto out_check_final;
273 }
274
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000275 tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
276
277out_check_final:
278 flush = len < mss;
279 flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
280 TCP_FLAG_RST | TCP_FLAG_SYN |
281 TCP_FLAG_FIN));
282
283 if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
David Millerd4546c22018-06-24 14:13:49 +0900284 pp = p;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000285
286out:
Jerry Chubf5a7552014-01-07 10:23:19 -0800287 NAPI_GRO_CB(skb)->flush |= (flush != 0);
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000288
289 return pp;
290}
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000291
292int tcp_gro_complete(struct sk_buff *skb)
293{
294 struct tcphdr *th = tcp_hdr(skb);
295
Jerry Chu299603e82013-12-11 20:53:45 -0800296 skb->csum_start = (unsigned char *)th - skb->head;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000297 skb->csum_offset = offsetof(struct tcphdr, check);
298 skb->ip_summed = CHECKSUM_PARTIAL;
299
300 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
301
302 if (th->cwr)
303 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
304
305 return 0;
306}
307EXPORT_SYMBOL(tcp_gro_complete);
308
Paolo Abeni028e0a42018-12-14 11:51:59 +0100309INDIRECT_CALLABLE_SCOPE
310struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000311{
Herbert Xucc5c00b2013-11-22 10:31:29 +0800312 /* Don't bother verifying checksum if we're going to flush anyway. */
Tom Herbert149d0772014-08-22 13:34:30 -0700313 if (!NAPI_GRO_CB(skb)->flush &&
314 skb_gro_checksum_validate(skb, IPPROTO_TCP,
315 inet_gro_compute_pseudo)) {
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000316 NAPI_GRO_CB(skb)->flush = 1;
317 return NULL;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000318 }
319
320 return tcp_gro_receive(head, skb);
321}
322
Paolo Abeni028e0a42018-12-14 11:51:59 +0100323INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000324{
325 const struct iphdr *iph = ip_hdr(skb);
326 struct tcphdr *th = tcp_hdr(skb);
327
Jerry Chu299603e82013-12-11 20:53:45 -0800328 th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
329 iph->daddr, 0);
Jerry Chuc3caf112014-07-14 15:54:46 -0700330 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000331
Alexander Duyck15305452016-04-10 21:44:57 -0400332 if (NAPI_GRO_CB(skb)->is_atomic)
333 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
334
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000335 return tcp_gro_complete(skb);
336}
337
338static const struct net_offload tcpv4_offload = {
339 .callbacks = {
Tom Herbertd020f8f2014-09-20 14:52:28 -0700340 .gso_segment = tcp4_gso_segment,
Daniel Borkmann28850dc2013-06-07 05:11:46 +0000341 .gro_receive = tcp4_gro_receive,
342 .gro_complete = tcp4_gro_complete,
343 },
344};
345
346int __init tcpv4_offload_init(void)
347{
348 return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
349}