1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPV4 GSO/GRO offload support
4 * Linux INET implementation
6 * TCPv4 GSO/GRO support
9 #include <linux/indirect_call_wrapper.h>
10 #include <linux/skbuff.h>
13 #include <net/protocol.h>
15 static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
16 unsigned int seq, unsigned int mss)
19 if (before(ts_seq, seq + mss)) {
20 skb_shinfo(skb)->tx_flags |= SKBTX_SW_TSTAMP;
21 skb_shinfo(skb)->tskey = ts_seq;
30 static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
31 netdev_features_t features)
33 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4))
34 return ERR_PTR(-EINVAL);
36 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
37 return ERR_PTR(-EINVAL);
39 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
40 const struct iphdr *iph = ip_hdr(skb);
41 struct tcphdr *th = tcp_hdr(skb);
43 /* Set up checksum pseudo header, usually expect stack to
44 * have done this already.
48 skb->ip_summed = CHECKSUM_PARTIAL;
49 __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
52 return tcp_gso_segment(skb, features);
55 struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
56 netdev_features_t features)
58 struct sk_buff *segs = ERR_PTR(-EINVAL);
59 unsigned int sum_truesize = 0;
65 struct sk_buff *gso_skb = skb;
67 bool ooo_okay, copy_destructor;
72 if (thlen < sizeof(*th))
75 if (!pskb_may_pull(skb, thlen))
79 __skb_pull(skb, thlen);
81 mss = skb_shinfo(skb)->gso_size;
82 if (unlikely(skb->len <= mss))
85 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
86 /* Packet is from an untrusted source, reset gso_segs. */
88 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
94 copy_destructor = gso_skb->destructor == tcp_wfree;
95 ooo_okay = gso_skb->ooo_okay;
96 /* All segments but the first should have ooo_okay cleared */
99 segs = skb_segment(skb, features);
103 /* Only first segment might have ooo_okay set */
104 segs->ooo_okay = ooo_okay;
106 /* GSO partial and frag_list segmentation only requires splitting
107 * the frame into an MSS multiple and possibly a remainder, both
108 * cases return a GSO skb. So update the mss now.
110 if (skb_is_gso(segs))
111 mss *= skb_shinfo(segs)->gso_segs;
113 delta = (__force __wsum)htonl(oldlen + thlen + mss);
117 seq = ntohl(th->seq);
119 if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_SW_TSTAMP))
120 tcp_gso_tstamp(segs, skb_shinfo(gso_skb)->tskey, seq, mss);
122 newcheck = ~csum_fold(csum_add(csum_unfold(th->check), delta));
125 th->fin = th->psh = 0;
126 th->check = newcheck;
128 if (skb->ip_summed == CHECKSUM_PARTIAL)
129 gso_reset_checksum(skb, ~th->check);
131 th->check = gso_make_checksum(skb, ~th->check);
134 if (copy_destructor) {
135 skb->destructor = gso_skb->destructor;
136 skb->sk = gso_skb->sk;
137 sum_truesize += skb->truesize;
142 th->seq = htonl(seq);
146 /* Following permits TCP Small Queues to work well with GSO :
147 * The callback to TCP stack will be called at the time last frag
148 * is freed at TX completion, and not right now when gso_skb
149 * is freed by GSO engine
151 if (copy_destructor) {
154 swap(gso_skb->sk, skb->sk);
155 swap(gso_skb->destructor, skb->destructor);
156 sum_truesize += skb->truesize;
157 delta = sum_truesize - gso_skb->truesize;
158 /* In some pathological cases, delta can be negative.
159 * We need to either use refcount_add() or refcount_sub_and_test()
161 if (likely(delta >= 0))
162 refcount_add(delta, &skb->sk->sk_wmem_alloc);
164 WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
167 delta = (__force __wsum)htonl(oldlen +
168 (skb_tail_pointer(skb) -
169 skb_transport_header(skb)) +
171 th->check = ~csum_fold(csum_add(csum_unfold(th->check), delta));
172 if (skb->ip_summed == CHECKSUM_PARTIAL)
173 gso_reset_checksum(skb, ~th->check);
175 th->check = gso_make_checksum(skb, ~th->check);
180 struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb)
182 struct sk_buff *pp = NULL;
189 unsigned int mss = 1;
195 off = skb_gro_offset(skb);
196 hlen = off + sizeof(*th);
197 th = skb_gro_header(skb, hlen, off);
201 thlen = th->doff * 4;
202 if (thlen < sizeof(*th))
206 if (skb_gro_header_hard(skb, hlen)) {
207 th = skb_gro_header_slow(skb, hlen, off);
212 skb_gro_pull(skb, thlen);
214 len = skb_gro_len(skb);
215 flags = tcp_flag_word(th);
217 list_for_each_entry(p, head, list) {
218 if (!NAPI_GRO_CB(p)->same_flow)
223 if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
224 NAPI_GRO_CB(p)->same_flow = 0;
231 goto out_check_final;
234 /* Include the IP ID check below from the inner most IP hdr */
235 flush = NAPI_GRO_CB(p)->flush;
236 flush |= (__force int)(flags & TCP_FLAG_CWR);
237 flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
238 ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
239 flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
240 for (i = sizeof(*th); i < thlen; i += 4)
241 flush |= *(u32 *)((u8 *)th + i) ^
242 *(u32 *)((u8 *)th2 + i);
244 /* When we receive our second frame we can made a decision on if we
245 * continue this flow as an atomic flow with a fixed ID or if we use
246 * an incrementing ID.
248 if (NAPI_GRO_CB(p)->flush_id != 1 ||
249 NAPI_GRO_CB(p)->count != 1 ||
250 !NAPI_GRO_CB(p)->is_atomic)
251 flush |= NAPI_GRO_CB(p)->flush_id;
253 NAPI_GRO_CB(p)->is_atomic = false;
255 mss = skb_shinfo(p)->gso_size;
257 /* If skb is a GRO packet, make sure its gso_size matches prior packet mss.
258 * If it is a single frame, do not aggregate it if its length
259 * is bigger than our mss.
261 if (unlikely(skb_is_gso(skb)))
262 flush |= (mss != skb_shinfo(skb)->gso_size);
264 flush |= (len - 1) >= mss;
266 flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
267 #ifdef CONFIG_TLS_DEVICE
268 flush |= p->decrypted ^ skb->decrypted;
271 if (flush || skb_gro_receive(p, skb)) {
273 goto out_check_final;
276 tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
279 /* Force a flush if last segment is smaller than mss. */
280 if (unlikely(skb_is_gso(skb)))
281 flush = len != NAPI_GRO_CB(skb)->count * skb_shinfo(skb)->gso_size;
285 flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
286 TCP_FLAG_RST | TCP_FLAG_SYN |
289 if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
293 NAPI_GRO_CB(skb)->flush |= (flush != 0);
298 int tcp_gro_complete(struct sk_buff *skb)
300 struct tcphdr *th = tcp_hdr(skb);
302 skb->csum_start = (unsigned char *)th - skb->head;
303 skb->csum_offset = offsetof(struct tcphdr, check);
304 skb->ip_summed = CHECKSUM_PARTIAL;
306 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
309 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
311 if (skb->encapsulation)
312 skb->inner_transport_header = skb->transport_header;
316 EXPORT_SYMBOL(tcp_gro_complete);
318 INDIRECT_CALLABLE_SCOPE
319 struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
321 /* Don't bother verifying checksum if we're going to flush anyway. */
322 if (!NAPI_GRO_CB(skb)->flush &&
323 skb_gro_checksum_validate(skb, IPPROTO_TCP,
324 inet_gro_compute_pseudo)) {
325 NAPI_GRO_CB(skb)->flush = 1;
329 return tcp_gro_receive(head, skb);
332 INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
334 const struct iphdr *iph = ip_hdr(skb);
335 struct tcphdr *th = tcp_hdr(skb);
337 th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
339 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
341 if (NAPI_GRO_CB(skb)->is_atomic)
342 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
344 return tcp_gro_complete(skb);
347 static const struct net_offload tcpv4_offload = {
349 .gso_segment = tcp4_gso_segment,
350 .gro_receive = tcp4_gro_receive,
351 .gro_complete = tcp4_gro_complete,
355 int __init tcpv4_offload_init(void)
357 return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);