GSO: Add GSO type for fixed IPv4 ID
[deliverable/linux.git] / net / ipv4 / tcp_offload.c
CommitLineData
28850dc7
DB
1/*
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
13#include <linux/skbuff.h>
14#include <net/tcp.h>
15#include <net/protocol.h>
16
f066e2b0
WB
17static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
18 unsigned int seq, unsigned int mss)
4ed2d765
WB
19{
20 while (skb) {
f066e2b0
WB
21 if (before(ts_seq, seq + mss)) {
22 skb_shinfo(skb)->tx_flags |= SKBTX_SW_TSTAMP;
4ed2d765
WB
23 skb_shinfo(skb)->tskey = ts_seq;
24 return;
25 }
26
27 skb = skb->next;
28 seq += mss;
29 }
30}
31
74abc20c
ED
32static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
33 netdev_features_t features)
d020f8f7
TH
34{
35 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
36 return ERR_PTR(-EINVAL);
37
38 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
39 const struct iphdr *iph = ip_hdr(skb);
40 struct tcphdr *th = tcp_hdr(skb);
41
42 /* Set up checksum pseudo header, usually expect stack to
43 * have done this already.
44 */
45
46 th->check = 0;
47 skb->ip_summed = CHECKSUM_PARTIAL;
48 __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
49 }
50
51 return tcp_gso_segment(skb, features);
52}
53
28be6e07 54struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
28850dc7
DB
55 netdev_features_t features)
56{
57 struct sk_buff *segs = ERR_PTR(-EINVAL);
0d08c42c 58 unsigned int sum_truesize = 0;
28850dc7
DB
59 struct tcphdr *th;
60 unsigned int thlen;
61 unsigned int seq;
62 __be32 delta;
63 unsigned int oldlen;
64 unsigned int mss;
65 struct sk_buff *gso_skb = skb;
66 __sum16 newcheck;
67 bool ooo_okay, copy_destructor;
68
28850dc7
DB
69 th = tcp_hdr(skb);
70 thlen = th->doff * 4;
71 if (thlen < sizeof(*th))
72 goto out;
73
74 if (!pskb_may_pull(skb, thlen))
75 goto out;
76
77 oldlen = (u16)~skb->len;
78 __skb_pull(skb, thlen);
79
a7eea416 80 mss = skb_shinfo(skb)->gso_size;
28850dc7
DB
81 if (unlikely(skb->len <= mss))
82 goto out;
83
84 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
85 /* Packet is from an untrusted source, reset gso_segs. */
86 int type = skb_shinfo(skb)->gso_type;
87
88 if (unlikely(type &
89 ~(SKB_GSO_TCPV4 |
90 SKB_GSO_DODGY |
91 SKB_GSO_TCP_ECN |
cbc53e08 92 SKB_GSO_TCP_FIXEDID |
28850dc7
DB
93 SKB_GSO_TCPV6 |
94 SKB_GSO_GRE |
4749c09c 95 SKB_GSO_GRE_CSUM |
cb32f511 96 SKB_GSO_IPIP |
61c1db7f 97 SKB_GSO_SIT |
28850dc7 98 SKB_GSO_UDP_TUNNEL |
0f4f4ffa 99 SKB_GSO_UDP_TUNNEL_CSUM |
e585f236 100 SKB_GSO_TUNNEL_REMCSUM |
28850dc7 101 0) ||
cbc53e08
AD
102 !(type & (SKB_GSO_TCPV4 |
103 SKB_GSO_TCPV6))))
28850dc7
DB
104 goto out;
105
106 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
107
108 segs = NULL;
109 goto out;
110 }
111
112 copy_destructor = gso_skb->destructor == tcp_wfree;
113 ooo_okay = gso_skb->ooo_okay;
114 /* All segments but the first should have ooo_okay cleared */
115 skb->ooo_okay = 0;
116
117 segs = skb_segment(skb, features);
118 if (IS_ERR(segs))
119 goto out;
120
121 /* Only first segment might have ooo_okay set */
122 segs->ooo_okay = ooo_okay;
123
124 delta = htonl(oldlen + (thlen + mss));
125
126 skb = segs;
127 th = tcp_hdr(skb);
128 seq = ntohl(th->seq);
129
4ed2d765
WB
130 if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
131 tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
132
28850dc7
DB
133 newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
134 (__force u32)delta));
135
136 do {
137 th->fin = th->psh = 0;
138 th->check = newcheck;
139
08b64fcc
AD
140 if (skb->ip_summed == CHECKSUM_PARTIAL)
141 gso_reset_checksum(skb, ~th->check);
142 else
e9c3a24b 143 th->check = gso_make_checksum(skb, ~th->check);
28850dc7
DB
144
145 seq += mss;
146 if (copy_destructor) {
147 skb->destructor = gso_skb->destructor;
148 skb->sk = gso_skb->sk;
0d08c42c 149 sum_truesize += skb->truesize;
28850dc7
DB
150 }
151 skb = skb->next;
152 th = tcp_hdr(skb);
153
154 th->seq = htonl(seq);
155 th->cwr = 0;
156 } while (skb->next);
157
158 /* Following permits TCP Small Queues to work well with GSO :
159 * The callback to TCP stack will be called at the time last frag
160 * is freed at TX completion, and not right now when gso_skb
161 * is freed by GSO engine
162 */
163 if (copy_destructor) {
164 swap(gso_skb->sk, skb->sk);
165 swap(gso_skb->destructor, skb->destructor);
0d08c42c
ED
166 sum_truesize += skb->truesize;
167 atomic_add(sum_truesize - gso_skb->truesize,
168 &skb->sk->sk_wmem_alloc);
28850dc7
DB
169 }
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));
08b64fcc
AD
176 if (skb->ip_summed == CHECKSUM_PARTIAL)
177 gso_reset_checksum(skb, ~th->check);
178 else
e9c3a24b 179 th->check = gso_make_checksum(skb, ~th->check);
28850dc7
DB
180out:
181 return segs;
182}
28850dc7
DB
183
184struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
185{
186 struct sk_buff **pp = NULL;
187 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
224 for (; (p = *head); head = &p->next) {
225 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 }
237
238 goto out_check_final;
239
240found:
bf5a755f
JC
241 /* Include the IP ID check below from the inner most IP hdr */
242 flush = NAPI_GRO_CB(p)->flush | NAPI_GRO_CB(p)->flush_id;
28850dc7
DB
243 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
a7eea416 251 mss = skb_shinfo(p)->gso_size;
28850dc7
DB
252
253 flush |= (len - 1) >= mss;
254 flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
255
256 if (flush || skb_gro_receive(head, skb)) {
257 mss = 1;
258 goto out_check_final;
259 }
260
261 p = *head;
262 th2 = tcp_hdr(p);
263 tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
264
265out_check_final:
266 flush = len < mss;
267 flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
268 TCP_FLAG_RST | TCP_FLAG_SYN |
269 TCP_FLAG_FIN));
270
271 if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
272 pp = head;
273
274out:
bf5a755f 275 NAPI_GRO_CB(skb)->flush |= (flush != 0);
28850dc7
DB
276
277 return pp;
278}
28850dc7
DB
279
280int tcp_gro_complete(struct sk_buff *skb)
281{
282 struct tcphdr *th = tcp_hdr(skb);
283
299603e8 284 skb->csum_start = (unsigned char *)th - skb->head;
28850dc7
DB
285 skb->csum_offset = offsetof(struct tcphdr, check);
286 skb->ip_summed = CHECKSUM_PARTIAL;
287
288 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
289
290 if (th->cwr)
291 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
292
293 return 0;
294}
295EXPORT_SYMBOL(tcp_gro_complete);
296
28850dc7
DB
297static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
298{
cc5c00bb 299 /* Don't bother verifying checksum if we're going to flush anyway. */
149d0774
TH
300 if (!NAPI_GRO_CB(skb)->flush &&
301 skb_gro_checksum_validate(skb, IPPROTO_TCP,
302 inet_gro_compute_pseudo)) {
28850dc7
DB
303 NAPI_GRO_CB(skb)->flush = 1;
304 return NULL;
28850dc7
DB
305 }
306
307 return tcp_gro_receive(head, skb);
308}
309
299603e8 310static int tcp4_gro_complete(struct sk_buff *skb, int thoff)
28850dc7
DB
311{
312 const struct iphdr *iph = ip_hdr(skb);
313 struct tcphdr *th = tcp_hdr(skb);
314
299603e8
JC
315 th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
316 iph->daddr, 0);
c3caf119 317 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
28850dc7
DB
318
319 return tcp_gro_complete(skb);
320}
321
322static const struct net_offload tcpv4_offload = {
323 .callbacks = {
d020f8f7 324 .gso_segment = tcp4_gso_segment,
28850dc7
DB
325 .gro_receive = tcp4_gro_receive,
326 .gro_complete = tcp4_gro_complete,
327 },
328};
329
330int __init tcpv4_offload_init(void)
331{
332 return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
333}
This page took 0.173352 seconds and 5 git commands to generate.