blob: fcbd977186b04b9637ee13bfbc91fd7420702756 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Routines having to do with the 'struct sk_buff' memory handlers.
4 *
Alan Cox113aa832008-10-13 19:01:08 -07005 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fixes:
9 * Alan Cox : Fixed the worst of the load
10 * balancer bugs.
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
23 *
24 * NOTE:
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31/*
32 * The functions in this file will not compile correctly with gcc 2.4.x
33 */
34
Joe Perchese005d192012-05-16 19:58:40 +000035#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/slab.h>
Florian Westphalde960aa2014-01-26 10:58:16 +010045#include <linux/tcp.h>
46#include <linux/udp.h>
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030047#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/netdevice.h>
49#ifdef CONFIG_NET_CLS_ACT
50#include <net/pkt_sched.h>
51#endif
52#include <linux/string.h>
53#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080054#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070058#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000059#include <linux/errqueue.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070060#include <linux/prefetch.h>
Vlad Yasevich0d5501c2014-08-08 14:42:13 -040061#include <linux/if_vlan.h>
John Hurley2a2ea502019-07-07 15:01:57 +010062#include <linux/mpls.h>
Sebastian Andrzej Siewior183f47f2021-02-18 18:31:24 +010063#include <linux/kcov.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/protocol.h>
66#include <net/dst.h>
67#include <net/sock.h>
68#include <net/checksum.h>
Paul Durranted1f50c2014-01-09 10:02:46 +000069#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/xfrm.h>
John Hurley8822e272019-07-07 15:01:54 +010071#include <net/mpls.h>
Mat Martineau3ee17bc2020-01-09 07:59:19 -080072#include <net/mptcp.h>
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +020073#include <net/page_pool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/uaccess.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040076#include <trace/events/skb.h>
Eric Dumazet51c56b02012-04-05 11:35:15 +020077#include <linux/highmem.h>
Willem de Bruijnb245be12015-01-30 13:29:32 -050078#include <linux/capability.h>
79#include <linux/user_namespace.h>
Matteo Croce2544af02019-05-29 17:13:48 +020080#include <linux/indirect_call_wrapper.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040081
Bart Van Assche7b7ed882019-03-25 09:17:23 -070082#include "datagram.h"
83
Alexey Dobriyan08009a72018-02-24 21:20:33 +030084struct kmem_cache *skbuff_head_cache __ro_after_init;
85static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
Florian Westphaldf5042f2018-12-18 17:15:16 +010086#ifdef CONFIG_SKB_EXTENSIONS
87static struct kmem_cache *skbuff_ext_cache __ro_after_init;
88#endif
Hans Westgaard Ry5f74f82e2016-02-03 09:26:57 +010089int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
90EXPORT_SYMBOL(sysctl_max_skb_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/**
Jean Sacrenf05de732013-02-11 13:30:38 +000093 * skb_panic - private function for out-of-line support
94 * @skb: buffer
95 * @sz: size
96 * @addr: address
James Hogan99d58512013-02-13 11:20:27 +000097 * @msg: skb_over_panic or skb_under_panic
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Jean Sacrenf05de732013-02-11 13:30:38 +000099 * Out-of-line support for skb_put() and skb_push().
100 * Called via the wrapper skb_over_panic() or skb_under_panic().
101 * Keep out of line to prevent kernel bloat.
102 * __builtin_return_address is not used because it is not always reliable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Jean Sacrenf05de732013-02-11 13:30:38 +0000104static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
James Hogan99d58512013-02-13 11:20:27 +0000105 const char msg[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jesper Dangaard Brouer41a46912020-04-27 18:37:43 +0200107 pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
James Hogan99d58512013-02-13 11:20:27 +0000108 msg, addr, skb->len, sz, skb->head, skb->data,
Joe Perchese005d192012-05-16 19:58:40 +0000109 (unsigned long)skb->tail, (unsigned long)skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 BUG();
112}
113
Jean Sacrenf05de732013-02-11 13:30:38 +0000114static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Jean Sacrenf05de732013-02-11 13:30:38 +0000116 skb_panic(skb, sz, addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Jean Sacrenf05de732013-02-11 13:30:38 +0000119static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
120{
121 skb_panic(skb, sz, addr, __func__);
122}
Mel Gormanc93bdd02012-07-31 16:44:19 -0700123
Alexander Lobakin50fad4b52021-02-13 14:12:13 +0000124#define NAPI_SKB_CACHE_SIZE 64
Alexander Lobakinf450d532021-02-13 14:12:25 +0000125#define NAPI_SKB_CACHE_BULK 16
126#define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2)
Alexander Lobakin50fad4b52021-02-13 14:12:13 +0000127
128struct napi_alloc_cache {
129 struct page_frag_cache page;
130 unsigned int skb_count;
131 void *skb_cache[NAPI_SKB_CACHE_SIZE];
132};
133
134static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
135static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
136
137static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask,
138 unsigned int align_mask)
139{
140 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
141
142 return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask);
143}
144
145void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
146{
147 fragsz = SKB_DATA_ALIGN(fragsz);
148
149 return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
150}
151EXPORT_SYMBOL(__napi_alloc_frag_align);
152
153void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
154{
155 struct page_frag_cache *nc;
156 void *data;
157
158 fragsz = SKB_DATA_ALIGN(fragsz);
159 if (in_irq() || irqs_disabled()) {
160 nc = this_cpu_ptr(&netdev_alloc_cache);
161 data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
162 } else {
163 local_bh_disable();
164 data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask);
165 local_bh_enable();
166 }
167 return data;
168}
169EXPORT_SYMBOL(__netdev_alloc_frag_align);
170
Alexander Lobakinf450d532021-02-13 14:12:25 +0000171static struct sk_buff *napi_skb_cache_get(void)
172{
173 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
174 struct sk_buff *skb;
175
176 if (unlikely(!nc->skb_count))
177 nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache,
178 GFP_ATOMIC,
179 NAPI_SKB_CACHE_BULK,
180 nc->skb_cache);
181 if (unlikely(!nc->skb_count))
182 return NULL;
183
184 skb = nc->skb_cache[--nc->skb_count];
185 kasan_unpoison_object_data(skbuff_head_cache, skb);
186
187 return skb;
188}
189
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200190/* Caller must provide SKB that is memset cleared */
Alexander Lobakin483126b2021-02-13 14:11:26 +0000191static void __build_skb_around(struct sk_buff *skb, void *data,
192 unsigned int frag_size)
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200193{
194 struct skb_shared_info *shinfo;
195 unsigned int size = frag_size ? : ksize(data);
196
197 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
198
199 /* Assumes caller memset cleared SKB */
200 skb->truesize = SKB_TRUESIZE(size);
201 refcount_set(&skb->users, 1);
202 skb->head = data;
203 skb->data = data;
204 skb_reset_tail_pointer(skb);
205 skb->end = skb->tail + size;
206 skb->mac_header = (typeof(skb->mac_header))~0U;
207 skb->transport_header = (typeof(skb->transport_header))~0U;
208
209 /* make sure we initialize shinfo sequentially */
210 shinfo = skb_shinfo(skb);
211 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
212 atomic_set(&shinfo->dataref, 1);
213
Aleksandr Nogikh6370cc32020-10-29 17:36:19 +0000214 skb_set_kcov_handle(skb, kcov_common_handle());
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/**
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700218 * __build_skb - build a network buffer
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000219 * @data: data buffer provided by caller
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700220 * @frag_size: size of data, or 0 if head was kmalloced
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000221 *
222 * Allocate a new &sk_buff. Caller provides space holding head and
Florian Fainellideceb4c2013-07-23 20:22:39 +0100223 * skb_shared_info. @data must have been allocated by kmalloc() only if
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700224 * @frag_size is 0, otherwise data should come from the page allocator
225 * or vmalloc()
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000226 * The return is the new skb buffer.
227 * On a failure the return is %NULL, and @data is not freed.
228 * Notes :
229 * Before IO, driver allocates only data buffer where NIC put incoming frame
230 * Driver should add room at head (NET_SKB_PAD) and
231 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
232 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
233 * before giving packet to stack.
234 * RX rings only contains data buffers, not full skbs.
235 */
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700236struct sk_buff *__build_skb(void *data, unsigned int frag_size)
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000237{
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000238 struct sk_buff *skb;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000239
240 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200241 if (unlikely(!skb))
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000242 return NULL;
243
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000244 memset(skb, 0, offsetof(struct sk_buff, tail));
Alexander Lobakin483126b2021-02-13 14:11:26 +0000245 __build_skb_around(skb, data, frag_size);
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000246
Alexander Lobakin483126b2021-02-13 14:11:26 +0000247 return skb;
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000248}
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700249
250/* build_skb() is wrapper over __build_skb(), that specifically
251 * takes care of skb->head and skb->pfmemalloc
252 * This means that if @frag_size is not zero, then @data must be backed
253 * by a page fragment, not kmalloc() or vmalloc()
254 */
255struct sk_buff *build_skb(void *data, unsigned int frag_size)
256{
257 struct sk_buff *skb = __build_skb(data, frag_size);
258
259 if (skb && frag_size) {
260 skb->head_frag = 1;
Michal Hocko2f064f32015-08-21 14:11:51 -0700261 if (page_is_pfmemalloc(virt_to_head_page(data)))
Eric Dumazet2ea2f622015-04-24 16:05:01 -0700262 skb->pfmemalloc = 1;
263 }
264 return skb;
265}
Eric Dumazetb2b5ce92011-11-14 06:03:34 +0000266EXPORT_SYMBOL(build_skb);
267
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200268/**
269 * build_skb_around - build a network buffer around provided skb
270 * @skb: sk_buff provide by caller, must be memset cleared
271 * @data: data buffer provided by caller
272 * @frag_size: size of data, or 0 if head was kmalloced
273 */
274struct sk_buff *build_skb_around(struct sk_buff *skb,
275 void *data, unsigned int frag_size)
276{
277 if (unlikely(!skb))
278 return NULL;
279
Alexander Lobakin483126b2021-02-13 14:11:26 +0000280 __build_skb_around(skb, data, frag_size);
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200281
Alexander Lobakin483126b2021-02-13 14:11:26 +0000282 if (frag_size) {
Jesper Dangaard Brouerba0509b2019-04-12 17:07:37 +0200283 skb->head_frag = 1;
284 if (page_is_pfmemalloc(virt_to_head_page(data)))
285 skb->pfmemalloc = 1;
286 }
287 return skb;
288}
289EXPORT_SYMBOL(build_skb_around);
290
Alexander Lobakinf450d532021-02-13 14:12:25 +0000291/**
292 * __napi_build_skb - build a network buffer
293 * @data: data buffer provided by caller
294 * @frag_size: size of data, or 0 if head was kmalloced
295 *
296 * Version of __build_skb() that uses NAPI percpu caches to obtain
297 * skbuff_head instead of inplace allocation.
298 *
299 * Returns a new &sk_buff on success, %NULL on allocation failure.
300 */
301static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
302{
303 struct sk_buff *skb;
304
305 skb = napi_skb_cache_get();
306 if (unlikely(!skb))
307 return NULL;
308
309 memset(skb, 0, offsetof(struct sk_buff, tail));
310 __build_skb_around(skb, data, frag_size);
311
312 return skb;
313}
314
315/**
316 * napi_build_skb - build a network buffer
317 * @data: data buffer provided by caller
318 * @frag_size: size of data, or 0 if head was kmalloced
319 *
320 * Version of __napi_build_skb() that takes care of skb->head_frag
321 * and skb->pfmemalloc when the data is a page or page fragment.
322 *
323 * Returns a new &sk_buff on success, %NULL on allocation failure.
324 */
325struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
326{
327 struct sk_buff *skb = __napi_build_skb(data, frag_size);
328
329 if (likely(skb) && frag_size) {
330 skb->head_frag = 1;
331 skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
332 }
333
334 return skb;
335}
336EXPORT_SYMBOL(napi_build_skb);
337
Alexander Lobakin5381b232021-02-13 14:11:00 +0000338/*
339 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
340 * the caller if emergency pfmemalloc reserves are being used. If it is and
341 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
342 * may be used. Otherwise, the packet data may be discarded until enough
343 * memory is free
344 */
Alexander Lobakinef280952021-02-13 14:11:11 +0000345static void *kmalloc_reserve(size_t size, gfp_t flags, int node,
346 bool *pfmemalloc)
Alexander Lobakin5381b232021-02-13 14:11:00 +0000347{
348 void *obj;
349 bool ret_pfmemalloc = false;
350
351 /*
352 * Try a regular allocation, when that fails and we're not entitled
353 * to the reserves, fail.
354 */
355 obj = kmalloc_node_track_caller(size,
356 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
357 node);
358 if (obj || !(gfp_pfmemalloc_allowed(flags)))
359 goto out;
360
361 /* Try again but now we are using pfmemalloc reserves */
362 ret_pfmemalloc = true;
363 obj = kmalloc_node_track_caller(size, flags, node);
364
365out:
366 if (pfmemalloc)
367 *pfmemalloc = ret_pfmemalloc;
368
369 return obj;
370}
371
372/* Allocate a new skbuff. We do this ourselves so we can fill in a few
373 * 'private' fields and also do memory statistics to find all the
374 * [BEEP] leaks.
375 *
376 */
377
378/**
379 * __alloc_skb - allocate a network buffer
380 * @size: size to allocate
381 * @gfp_mask: allocation mask
382 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
383 * instead of head cache and allocate a cloned (child) skb.
384 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
385 * allocations in case the data is required for writeback
386 * @node: numa node to allocate memory on
387 *
388 * Allocate a new &sk_buff. The returned buffer has no headroom and a
389 * tail room of at least size bytes. The object has a reference count
390 * of one. The return is the buffer. On a failure the return is %NULL.
391 *
392 * Buffers may only be allocated from interrupts using a @gfp_mask of
393 * %GFP_ATOMIC.
394 */
395struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
396 int flags, int node)
397{
398 struct kmem_cache *cache;
Alexander Lobakin5381b232021-02-13 14:11:00 +0000399 struct sk_buff *skb;
400 u8 *data;
401 bool pfmemalloc;
402
403 cache = (flags & SKB_ALLOC_FCLONE)
404 ? skbuff_fclone_cache : skbuff_head_cache;
405
406 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
407 gfp_mask |= __GFP_MEMALLOC;
408
409 /* Get the HEAD */
Alexander Lobakind13612b2021-02-13 14:12:38 +0000410 if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
411 likely(node == NUMA_NO_NODE || node == numa_mem_id()))
412 skb = napi_skb_cache_get();
413 else
414 skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
Alexander Lobakindf1ae022021-02-13 14:11:39 +0000415 if (unlikely(!skb))
416 return NULL;
Alexander Lobakin5381b232021-02-13 14:11:00 +0000417 prefetchw(skb);
418
419 /* We do our best to align skb_shared_info on a separate cache
420 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
421 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
422 * Both skb->head and skb_shared_info are cache line aligned.
423 */
424 size = SKB_DATA_ALIGN(size);
425 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
426 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
Alexander Lobakindf1ae022021-02-13 14:11:39 +0000427 if (unlikely(!data))
Alexander Lobakin5381b232021-02-13 14:11:00 +0000428 goto nodata;
429 /* kmalloc(size) might give us more room than requested.
430 * Put skb_shared_info exactly at the end of allocated zone,
431 * to allow max possible filling before reallocation.
432 */
433 size = SKB_WITH_OVERHEAD(ksize(data));
434 prefetchw(data + size);
435
436 /*
437 * Only clear those fields we need to clear, not those that we will
438 * actually initialise below. Hence, don't put any more fields after
439 * the tail pointer in struct sk_buff!
440 */
441 memset(skb, 0, offsetof(struct sk_buff, tail));
Alexander Lobakinf9d67252021-02-13 14:11:50 +0000442 __build_skb_around(skb, data, 0);
Alexander Lobakin5381b232021-02-13 14:11:00 +0000443 skb->pfmemalloc = pfmemalloc;
Alexander Lobakin5381b232021-02-13 14:11:00 +0000444
445 if (flags & SKB_ALLOC_FCLONE) {
446 struct sk_buff_fclones *fclones;
447
448 fclones = container_of(skb, struct sk_buff_fclones, skb1);
449
450 skb->fclone = SKB_FCLONE_ORIG;
451 refcount_set(&fclones->fclone_ref, 1);
452
453 fclones->skb2.fclone = SKB_FCLONE_CLONE;
454 }
455
Alexander Lobakin5381b232021-02-13 14:11:00 +0000456 return skb;
Alexander Lobakindf1ae022021-02-13 14:11:39 +0000457
Alexander Lobakin5381b232021-02-13 14:11:00 +0000458nodata:
459 kmem_cache_free(cache, skb);
Alexander Lobakindf1ae022021-02-13 14:11:39 +0000460 return NULL;
Alexander Lobakin5381b232021-02-13 14:11:00 +0000461}
462EXPORT_SYMBOL(__alloc_skb);
463
Sebastian Andrzej Siewior7ba7aea2019-06-07 21:20:34 +0200464/**
Alexander Duyckfd11a832014-12-09 19:40:49 -0800465 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
466 * @dev: network device to receive on
Masanari Iidad7499162015-08-24 22:56:54 +0900467 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800468 * @gfp_mask: get_free_pages mask, passed to alloc_skb
469 *
470 * Allocate a new &sk_buff and assign it a usage count of one. The
471 * buffer has NET_SKB_PAD headroom built in. Users should allocate
472 * the headroom they think they need without accounting for the
473 * built in space. The built in space is used for optimisations.
474 *
475 * %NULL is returned if there is no free memory.
476 */
Alexander Duyck94519802015-05-06 21:11:40 -0700477struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
478 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800479{
Alexander Duyckb63ae8c2015-05-06 21:11:57 -0700480 struct page_frag_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800481 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700482 bool pfmemalloc;
483 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800484
Alexander Duyck94519802015-05-06 21:11:40 -0700485 len += NET_SKB_PAD;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800486
Alexander Lobakin66c55602021-01-15 15:04:40 +0000487 /* If requested length is either too small or too big,
488 * we use kmalloc() for skb->head allocation.
489 */
490 if (len <= SKB_WITH_OVERHEAD(1024) ||
491 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800492 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Duycka080e7b2015-05-13 13:34:13 -0700493 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
494 if (!skb)
495 goto skb_fail;
496 goto skb_success;
497 }
Alexander Duyck94519802015-05-06 21:11:40 -0700498
499 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
500 len = SKB_DATA_ALIGN(len);
501
502 if (sk_memalloc_socks())
503 gfp_mask |= __GFP_MEMALLOC;
504
Sebastian Andrzej Siewior92dcabd2019-06-07 21:20:35 +0200505 if (in_irq() || irqs_disabled()) {
506 nc = this_cpu_ptr(&netdev_alloc_cache);
507 data = page_frag_alloc(nc, len, gfp_mask);
508 pfmemalloc = nc->pfmemalloc;
509 } else {
510 local_bh_disable();
511 nc = this_cpu_ptr(&napi_alloc_cache.page);
512 data = page_frag_alloc(nc, len, gfp_mask);
513 pfmemalloc = nc->pfmemalloc;
514 local_bh_enable();
515 }
Alexander Duyck94519802015-05-06 21:11:40 -0700516
517 if (unlikely(!data))
518 return NULL;
519
520 skb = __build_skb(data, len);
521 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700522 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700523 return NULL;
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700524 }
Alexander Duyckfd11a832014-12-09 19:40:49 -0800525
Alexander Duyck94519802015-05-06 21:11:40 -0700526 if (pfmemalloc)
527 skb->pfmemalloc = 1;
528 skb->head_frag = 1;
529
Alexander Duycka080e7b2015-05-13 13:34:13 -0700530skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700531 skb_reserve(skb, NET_SKB_PAD);
532 skb->dev = dev;
533
Alexander Duycka080e7b2015-05-13 13:34:13 -0700534skb_fail:
Christoph Hellwig8af27452006-07-31 22:35:23 -0700535 return skb;
536}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800537EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alexander Duyckfd11a832014-12-09 19:40:49 -0800539/**
540 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
541 * @napi: napi instance this buffer was allocated for
Masanari Iidad7499162015-08-24 22:56:54 +0900542 * @len: length to allocate
Alexander Duyckfd11a832014-12-09 19:40:49 -0800543 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
544 *
545 * Allocate a new sk_buff for use in NAPI receive. This buffer will
546 * attempt to allocate the head from a special reserved region used
547 * only for NAPI Rx allocation. By doing this we can save several
548 * CPU cycles by avoiding having to disable and re-enable IRQs.
549 *
550 * %NULL is returned if there is no free memory.
551 */
Alexander Duyck94519802015-05-06 21:11:40 -0700552struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
553 gfp_t gfp_mask)
Alexander Duyckfd11a832014-12-09 19:40:49 -0800554{
Eric Dumazet3226b152021-01-13 08:18:19 -0800555 struct napi_alloc_cache *nc;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800556 struct sk_buff *skb;
Alexander Duyck94519802015-05-06 21:11:40 -0700557 void *data;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800558
Alexander Duyck94519802015-05-06 21:11:40 -0700559 len += NET_SKB_PAD + NET_IP_ALIGN;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800560
Eric Dumazet3226b152021-01-13 08:18:19 -0800561 /* If requested length is either too small or too big,
562 * we use kmalloc() for skb->head allocation.
563 */
564 if (len <= SKB_WITH_OVERHEAD(1024) ||
565 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
Mel Gormand0164ad2015-11-06 16:28:21 -0800566 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
Alexander Lobakincfb8ec62021-02-13 14:12:49 +0000567 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
568 NUMA_NO_NODE);
Alexander Duycka080e7b2015-05-13 13:34:13 -0700569 if (!skb)
570 goto skb_fail;
571 goto skb_success;
572 }
Alexander Duyck94519802015-05-06 21:11:40 -0700573
Eric Dumazet3226b152021-01-13 08:18:19 -0800574 nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Duyck94519802015-05-06 21:11:40 -0700575 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
576 len = SKB_DATA_ALIGN(len);
577
578 if (sk_memalloc_socks())
579 gfp_mask |= __GFP_MEMALLOC;
580
Alexander Duyck8c2dd3e2017-01-10 16:58:06 -0800581 data = page_frag_alloc(&nc->page, len, gfp_mask);
Alexander Duyck94519802015-05-06 21:11:40 -0700582 if (unlikely(!data))
583 return NULL;
584
Alexander Lobakincfb8ec62021-02-13 14:12:49 +0000585 skb = __napi_build_skb(data, len);
Alexander Duyck94519802015-05-06 21:11:40 -0700586 if (unlikely(!skb)) {
Alexander Duyck181edb22015-05-06 21:12:03 -0700587 skb_free_frag(data);
Alexander Duyck94519802015-05-06 21:11:40 -0700588 return NULL;
Alexander Duyckfd11a832014-12-09 19:40:49 -0800589 }
590
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100591 if (nc->page.pfmemalloc)
Alexander Duyck94519802015-05-06 21:11:40 -0700592 skb->pfmemalloc = 1;
593 skb->head_frag = 1;
594
Alexander Duycka080e7b2015-05-13 13:34:13 -0700595skb_success:
Alexander Duyck94519802015-05-06 21:11:40 -0700596 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
597 skb->dev = napi->dev;
598
Alexander Duycka080e7b2015-05-13 13:34:13 -0700599skb_fail:
Alexander Duyckfd11a832014-12-09 19:40:49 -0800600 return skb;
601}
602EXPORT_SYMBOL(__napi_alloc_skb);
603
Peter Zijlstra654bed12008-10-07 14:22:33 -0700604void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
Eric Dumazet50269e12012-03-23 23:59:33 +0000605 int size, unsigned int truesize)
Peter Zijlstra654bed12008-10-07 14:22:33 -0700606{
607 skb_fill_page_desc(skb, i, page, off, size);
608 skb->len += size;
609 skb->data_len += size;
Eric Dumazet50269e12012-03-23 23:59:33 +0000610 skb->truesize += truesize;
Peter Zijlstra654bed12008-10-07 14:22:33 -0700611}
612EXPORT_SYMBOL(skb_add_rx_frag);
613
Jason Wangf8e617e2013-11-01 14:07:47 +0800614void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
615 unsigned int truesize)
616{
617 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
618
619 skb_frag_size_add(frag, size);
620 skb->len += size;
621 skb->data_len += size;
622 skb->truesize += truesize;
623}
624EXPORT_SYMBOL(skb_coalesce_rx_frag);
625
Herbert Xu27b437c2006-07-13 19:26:39 -0700626static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700628 kfree_skb_list(*listp);
Herbert Xu27b437c2006-07-13 19:26:39 -0700629 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Herbert Xu27b437c2006-07-13 19:26:39 -0700632static inline void skb_drop_fraglist(struct sk_buff *skb)
633{
634 skb_drop_list(&skb_shinfo(skb)->frag_list);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static void skb_clone_fraglist(struct sk_buff *skb)
638{
639 struct sk_buff *list;
640
David S. Millerfbb398a2009-06-09 00:18:59 -0700641 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 skb_get(list);
643}
644
Eric Dumazetd3836f22012-04-27 00:33:38 +0000645static void skb_free_head(struct sk_buff *skb)
646{
Alexander Duyck181edb22015-05-06 21:12:03 -0700647 unsigned char *head = skb->head;
648
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +0200649 if (skb->head_frag) {
650 if (skb_pp_recycle(skb, head))
651 return;
Alexander Duyck181edb22015-05-06 21:12:03 -0700652 skb_free_frag(head);
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +0200653 } else {
Alexander Duyck181edb22015-05-06 21:12:03 -0700654 kfree(head);
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +0200655 }
Eric Dumazetd3836f22012-04-27 00:33:38 +0000656}
657
Adrian Bunk5bba1712006-06-29 13:02:35 -0700658static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Eric Dumazetff04a772014-09-23 18:39:30 -0700660 struct skb_shared_info *shinfo = skb_shinfo(skb);
661 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Eric Dumazetff04a772014-09-23 18:39:30 -0700663 if (skb->cloned &&
664 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
665 &shinfo->dataref))
Ilias Apalodimas2cc3aeb2021-07-16 10:02:18 +0300666 goto exit;
Shirley Maa6686f22011-07-06 12:22:12 +0000667
Jonathan Lemon70c43162021-01-06 14:18:36 -0800668 skb_zcopy_clear(skb, true);
669
Eric Dumazetff04a772014-09-23 18:39:30 -0700670 for (i = 0; i < shinfo->nr_frags; i++)
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +0200671 __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
Shirley Maa6686f22011-07-06 12:22:12 +0000672
Eric Dumazetff04a772014-09-23 18:39:30 -0700673 if (shinfo->frag_list)
674 kfree_skb_list(shinfo->frag_list);
675
676 skb_free_head(skb);
Ilias Apalodimas2cc3aeb2021-07-16 10:02:18 +0300677exit:
678 /* When we clone an SKB we copy the reycling bit. The pp_recycle
679 * bit is only set on the head though, so in order to avoid races
680 * while trying to recycle fragments on __skb_frag_unref() we need
681 * to make one SKB responsible for triggering the recycle path.
682 * So disable the recycling bit if an SKB is cloned and we have
683 * additional references to to the fragmented part of the SKB.
684 * Eventually the last SKB will have the recycling bit set and it's
685 * dataref set to 0, which will trigger the recycling
686 */
687 skb->pp_recycle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * Free an skbuff by memory without cleaning the state.
692 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800693static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700695 struct sk_buff_fclones *fclones;
David S. Millerd179cd12005-08-17 14:57:30 -0700696
David S. Millerd179cd12005-08-17 14:57:30 -0700697 switch (skb->fclone) {
698 case SKB_FCLONE_UNAVAILABLE:
699 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800700 return;
David S. Millerd179cd12005-08-17 14:57:30 -0700701
702 case SKB_FCLONE_ORIG:
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700703 fclones = container_of(skb, struct sk_buff_fclones, skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800704
705 /* We usually free the clone (TX completion) before original skb
706 * This test would have no chance to be true for the clone,
707 * while here, branch prediction will be good.
708 */
Reshetova, Elena26385952017-06-30 13:07:59 +0300709 if (refcount_read(&fclones->fclone_ref) == 1)
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800710 goto fastpath;
David S. Millerd179cd12005-08-17 14:57:30 -0700711 break;
712
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800713 default: /* SKB_FCLONE_CLONE */
Eric Dumazetd0bf4a92014-09-29 13:29:15 -0700714 fclones = container_of(skb, struct sk_buff_fclones, skb2);
David S. Millerd179cd12005-08-17 14:57:30 -0700715 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700716 }
Reshetova, Elena26385952017-06-30 13:07:59 +0300717 if (!refcount_dec_and_test(&fclones->fclone_ref))
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -0800718 return;
719fastpath:
720 kmem_cache_free(skbuff_fclone_cache, fclones);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Paolo Abeni0a463c72017-06-12 11:23:42 +0200723void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Eric Dumazetadf30902009-06-02 05:19:30 +0000725 skb_dst_drop(skb);
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700726 if (skb->destructor) {
727 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 skb->destructor(skb);
729 }
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000730#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphalcb9c6832017-01-23 18:21:56 +0100731 nf_conntrack_put(skb_nfct(skb));
KOVACS Krisztian2fc72c72011-01-12 20:25:08 +0100732#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +0100733 skb_ext_put(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700734}
735
736/* Free everything but the sk_buff shell. */
737static void skb_release_all(struct sk_buff *skb)
738{
739 skb_release_head_state(skb);
Florian Westphala28b1b92017-07-23 19:54:47 +0200740 if (likely(skb->head))
741 skb_release_data(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Herbert Xu2d4baff2007-11-26 23:11:19 +0800744/**
745 * __kfree_skb - private function
746 * @skb: buffer
747 *
748 * Free an sk_buff. Release anything attached to the buffer.
749 * Clean the state. This is an internal helper function. Users should
750 * always call kfree_skb
751 */
752
753void __kfree_skb(struct sk_buff *skb)
754{
755 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 kfree_skbmem(skb);
757}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800758EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800761 * kfree_skb - free an sk_buff
762 * @skb: buffer to free
763 *
764 * Drop a reference to the buffer and free it if the usage count has
765 * hit zero.
766 */
767void kfree_skb(struct sk_buff *skb)
768{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200769 if (!skb_unref(skb))
Jörn Engel231d06a2006-03-20 21:28:35 -0800770 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200771
Neil Hormanead2ceb2009-03-11 09:49:55 +0000772 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800773 __kfree_skb(skb);
774}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800775EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800776
Eric Dumazetbd8a7032013-06-24 06:26:00 -0700777void kfree_skb_list(struct sk_buff *segs)
778{
779 while (segs) {
780 struct sk_buff *next = segs->next;
781
782 kfree_skb(segs);
783 segs = next;
784 }
785}
786EXPORT_SYMBOL(kfree_skb_list);
787
Willem de Bruijn64131392019-07-07 05:51:55 -0400788/* Dump skb information and contents.
789 *
790 * Must only be called from net_ratelimit()-ed paths.
791 *
Vladimir Oltean302af7c2020-10-05 17:48:38 +0300792 * Dumps whole packets if full_pkt, only headers otherwise.
Willem de Bruijn64131392019-07-07 05:51:55 -0400793 */
794void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
795{
Willem de Bruijn64131392019-07-07 05:51:55 -0400796 struct skb_shared_info *sh = skb_shinfo(skb);
797 struct net_device *dev = skb->dev;
798 struct sock *sk = skb->sk;
799 struct sk_buff *list_skb;
800 bool has_mac, has_trans;
801 int headroom, tailroom;
802 int i, len, seg_len;
803
804 if (full_pkt)
Willem de Bruijn64131392019-07-07 05:51:55 -0400805 len = skb->len;
806 else
807 len = min_t(int, skb->len, MAX_HEADER + 128);
808
809 headroom = skb_headroom(skb);
810 tailroom = skb_tailroom(skb);
811
812 has_mac = skb_mac_header_was_set(skb);
813 has_trans = skb_transport_header_was_set(skb);
814
815 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
816 "mac=(%d,%d) net=(%d,%d) trans=%d\n"
817 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
818 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
819 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
820 level, skb->len, headroom, skb_headlen(skb), tailroom,
821 has_mac ? skb->mac_header : -1,
822 has_mac ? skb_mac_header_len(skb) : -1,
823 skb->network_header,
824 has_trans ? skb_network_header_len(skb) : -1,
825 has_trans ? skb->transport_header : -1,
826 sh->tx_flags, sh->nr_frags,
827 sh->gso_size, sh->gso_type, sh->gso_segs,
828 skb->csum, skb->ip_summed, skb->csum_complete_sw,
829 skb->csum_valid, skb->csum_level,
830 skb->hash, skb->sw_hash, skb->l4_hash,
831 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
832
833 if (dev)
834 printk("%sdev name=%s feat=0x%pNF\n",
835 level, dev->name, &dev->features);
836 if (sk)
Qian Caidb8051f2019-07-16 11:43:05 -0400837 printk("%ssk family=%hu type=%u proto=%u\n",
Willem de Bruijn64131392019-07-07 05:51:55 -0400838 level, sk->sk_family, sk->sk_type, sk->sk_protocol);
839
840 if (full_pkt && headroom)
841 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
842 16, 1, skb->head, headroom, false);
843
844 seg_len = min_t(int, skb_headlen(skb), len);
845 if (seg_len)
846 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
847 16, 1, skb->data, seg_len, false);
848 len -= seg_len;
849
850 if (full_pkt && tailroom)
851 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
852 16, 1, skb_tail_pointer(skb), tailroom, false);
853
854 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
855 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
856 u32 p_off, p_len, copied;
857 struct page *p;
858 u8 *vaddr;
859
Jonathan Lemonb54c9d52019-07-30 07:40:33 -0700860 skb_frag_foreach_page(frag, skb_frag_off(frag),
Willem de Bruijn64131392019-07-07 05:51:55 -0400861 skb_frag_size(frag), p, p_off, p_len,
862 copied) {
863 seg_len = min_t(int, p_len, len);
864 vaddr = kmap_atomic(p);
865 print_hex_dump(level, "skb frag: ",
866 DUMP_PREFIX_OFFSET,
867 16, 1, vaddr + p_off, seg_len, false);
868 kunmap_atomic(vaddr);
869 len -= seg_len;
870 if (!len)
871 break;
872 }
873 }
874
875 if (full_pkt && skb_has_frag_list(skb)) {
876 printk("skb fraglist:\n");
877 skb_walk_frags(skb, list_skb)
878 skb_dump(level, list_skb, true);
879 }
880}
881EXPORT_SYMBOL(skb_dump);
882
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700883/**
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000884 * skb_tx_error - report an sk_buff xmit error
885 * @skb: buffer that triggered an error
886 *
887 * Report xmit error if a device callback is tracking this skb.
888 * skb must be freed afterwards.
889 */
890void skb_tx_error(struct sk_buff *skb)
891{
Willem de Bruijn1f8b9772017-08-03 16:29:41 -0400892 skb_zcopy_clear(skb, true);
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000893}
894EXPORT_SYMBOL(skb_tx_error);
895
Herbert Xube769db2020-08-22 08:23:29 +1000896#ifdef CONFIG_TRACEPOINTS
Michael S. Tsirkin25121172012-11-01 09:16:28 +0000897/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000898 * consume_skb - free an skbuff
899 * @skb: buffer to free
900 *
901 * Drop a ref to the buffer and free it if the usage count has hit zero
902 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
903 * is being dropped after a failure and notes that
904 */
905void consume_skb(struct sk_buff *skb)
906{
Paolo Abeni3889a8032017-06-12 11:23:41 +0200907 if (!skb_unref(skb))
Neil Hormanead2ceb2009-03-11 09:49:55 +0000908 return;
Paolo Abeni3889a8032017-06-12 11:23:41 +0200909
Koki Sanagi07dc22e2010-08-23 18:46:12 +0900910 trace_consume_skb(skb);
Neil Hormanead2ceb2009-03-11 09:49:55 +0000911 __kfree_skb(skb);
912}
913EXPORT_SYMBOL(consume_skb);
Herbert Xube769db2020-08-22 08:23:29 +1000914#endif
Neil Hormanead2ceb2009-03-11 09:49:55 +0000915
Paolo Abeni0a463c72017-06-12 11:23:42 +0200916/**
Mauro Carvalho Chehabc1639be2020-11-16 11:17:58 +0100917 * __consume_stateless_skb - free an skbuff, assuming it is stateless
Paolo Abeni0a463c72017-06-12 11:23:42 +0200918 * @skb: buffer to free
919 *
Paolo Abenica2c1412017-09-06 14:44:36 +0200920 * Alike consume_skb(), but this variant assumes that this is the last
921 * skb reference and all the head states have been already dropped
Paolo Abeni0a463c72017-06-12 11:23:42 +0200922 */
Paolo Abenica2c1412017-09-06 14:44:36 +0200923void __consume_stateless_skb(struct sk_buff *skb)
Paolo Abeni0a463c72017-06-12 11:23:42 +0200924{
Paolo Abeni0a463c72017-06-12 11:23:42 +0200925 trace_consume_skb(skb);
Florian Westphal06dc75a2017-07-17 18:56:54 +0200926 skb_release_data(skb);
Paolo Abeni0a463c72017-06-12 11:23:42 +0200927 kfree_skbmem(skb);
928}
929
Alexander Lobakinf450d532021-02-13 14:12:25 +0000930static void napi_skb_cache_put(struct sk_buff *skb)
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100931{
932 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
Alexander Lobakinf450d532021-02-13 14:12:25 +0000933 u32 i;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100934
Alexander Lobakinf450d532021-02-13 14:12:25 +0000935 kasan_poison_object_data(skbuff_head_cache, skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100936 nc->skb_cache[nc->skb_count++] = skb;
937
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100938 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
Alexander Lobakinf450d532021-02-13 14:12:25 +0000939 for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
940 kasan_unpoison_object_data(skbuff_head_cache,
941 nc->skb_cache[i]);
942
943 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF,
944 nc->skb_cache + NAPI_SKB_CACHE_HALF);
945 nc->skb_count = NAPI_SKB_CACHE_HALF;
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100946 }
947}
Alexander Lobakinf450d532021-02-13 14:12:25 +0000948
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100949void __kfree_skb_defer(struct sk_buff *skb)
950{
Alexander Lobakin9243adf2021-02-13 14:13:09 +0000951 skb_release_all(skb);
952 napi_skb_cache_put(skb);
953}
954
955void napi_skb_free_stolen_head(struct sk_buff *skb)
956{
Paolo Abeni9efb4b52021-07-28 18:24:02 +0200957 if (unlikely(skb->slow_gro)) {
958 nf_reset_ct(skb);
959 skb_dst_drop(skb);
960 skb_ext_put(skb);
Paolo Abeni5e10da52021-07-28 18:24:03 +0200961 skb_orphan(skb);
Paolo Abeni9efb4b52021-07-28 18:24:02 +0200962 skb->slow_gro = 0;
963 }
Alexander Lobakinf450d532021-02-13 14:12:25 +0000964 napi_skb_cache_put(skb);
Jesper Dangaard Brouer15fad712016-02-08 13:15:04 +0100965}
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100966
967void napi_consume_skb(struct sk_buff *skb, int budget)
968{
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100969 /* Zero budget indicate non-NAPI context called us, like netpoll */
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100970 if (unlikely(!budget)) {
Jesper Dangaard Brouer885eb0a2016-03-11 09:43:58 +0100971 dev_consume_skb_any(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100972 return;
973 }
974
Yunsheng Lin6454eca2020-11-24 18:49:29 +0800975 lockdep_assert_in_softirq();
976
Paolo Abeni76088942017-06-14 11:48:48 +0200977 if (!skb_unref(skb))
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100978 return;
Paolo Abeni76088942017-06-14 11:48:48 +0200979
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100980 /* if reaching here SKB is ready to free */
981 trace_consume_skb(skb);
982
983 /* if SKB is a clone, don't handle this case */
Eric Dumazetabbdb5a2016-03-20 11:27:47 -0700984 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100985 __kfree_skb(skb);
986 return;
987 }
988
Alexander Lobakin9243adf2021-02-13 14:13:09 +0000989 skb_release_all(skb);
Alexander Lobakinf450d532021-02-13 14:12:25 +0000990 napi_skb_cache_put(skb);
Jesper Dangaard Brouer795bb1c2016-02-08 13:14:59 +0100991}
992EXPORT_SYMBOL(napi_consume_skb);
993
Eric Dumazetb1937222014-09-28 22:18:47 -0700994/* Make sure a field is enclosed inside headers_start/headers_end section */
995#define CHECK_SKB_FIELD(field) \
996 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
997 offsetof(struct sk_buff, headers_start)); \
998 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
999 offsetof(struct sk_buff, headers_end)); \
1000
Herbert Xudec18812007-10-14 00:37:30 -07001001static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1002{
1003 new->tstamp = old->tstamp;
Eric Dumazetb1937222014-09-28 22:18:47 -07001004 /* We do not copy old->sk */
Herbert Xudec18812007-10-14 00:37:30 -07001005 new->dev = old->dev;
Eric Dumazetb1937222014-09-28 22:18:47 -07001006 memcpy(new->cb, old->cb, sizeof(old->cb));
Eric Dumazet7fee2262010-05-11 23:19:48 +00001007 skb_dst_copy(new, old);
Florian Westphaldf5042f2018-12-18 17:15:16 +01001008 __skb_ext_copy(new, old);
Eric Dumazetb1937222014-09-28 22:18:47 -07001009 __nf_copy(new, old, false);
Patrick McHardy6aa895b2008-07-14 22:49:06 -07001010
Eric Dumazetb1937222014-09-28 22:18:47 -07001011 /* Note : this field could be in headers_start/headers_end section
1012 * It is not yet because we do not want to have a 16 bit hole
1013 */
1014 new->queue_mapping = old->queue_mapping;
Eliezer Tamir06021292013-06-10 11:39:50 +03001015
Eric Dumazetb1937222014-09-28 22:18:47 -07001016 memcpy(&new->headers_start, &old->headers_start,
1017 offsetof(struct sk_buff, headers_end) -
1018 offsetof(struct sk_buff, headers_start));
1019 CHECK_SKB_FIELD(protocol);
1020 CHECK_SKB_FIELD(csum);
1021 CHECK_SKB_FIELD(hash);
1022 CHECK_SKB_FIELD(priority);
1023 CHECK_SKB_FIELD(skb_iif);
1024 CHECK_SKB_FIELD(vlan_proto);
1025 CHECK_SKB_FIELD(vlan_tci);
1026 CHECK_SKB_FIELD(transport_header);
1027 CHECK_SKB_FIELD(network_header);
1028 CHECK_SKB_FIELD(mac_header);
1029 CHECK_SKB_FIELD(inner_protocol);
1030 CHECK_SKB_FIELD(inner_transport_header);
1031 CHECK_SKB_FIELD(inner_network_header);
1032 CHECK_SKB_FIELD(inner_mac_header);
1033 CHECK_SKB_FIELD(mark);
1034#ifdef CONFIG_NETWORK_SECMARK
1035 CHECK_SKB_FIELD(secmark);
1036#endif
Cong Wange0d10952013-08-01 11:10:25 +08001037#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetb1937222014-09-28 22:18:47 -07001038 CHECK_SKB_FIELD(napi_id);
Eliezer Tamir06021292013-06-10 11:39:50 +03001039#endif
Eric Dumazet2bd82482015-02-03 23:48:24 -08001040#ifdef CONFIG_XPS
1041 CHECK_SKB_FIELD(sender_cpu);
1042#endif
Eric Dumazetb1937222014-09-28 22:18:47 -07001043#ifdef CONFIG_NET_SCHED
1044 CHECK_SKB_FIELD(tc_index);
Eric Dumazetb1937222014-09-28 22:18:47 -07001045#endif
1046
Herbert Xudec18812007-10-14 00:37:30 -07001047}
1048
Herbert Xu82c49a32009-05-22 22:11:37 +00001049/*
1050 * You should not add any new code to this function. Add it to
1051 * __copy_skb_header above instead.
1052 */
Herbert Xue0053ec2007-10-14 00:37:52 -07001053static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#define C(x) n->x = skb->x
1056
1057 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -07001059 __copy_skb_header(n, skb);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 C(len);
1062 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -07001063 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -07001064 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -08001065 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 n->nohdr = 0;
Eric Dumazetb13dda92018-04-07 13:42:39 -07001067 n->peeked = 0;
Stefano Brivioe78bfb02018-07-13 13:21:07 +02001068 C(pfmemalloc);
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +02001069 C(pp_recycle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 C(tail);
1072 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -08001073 C(head);
Eric Dumazetd3836f22012-04-27 00:33:38 +00001074 C(head_frag);
Paul Moore02f1c892008-01-07 21:56:41 -08001075 C(data);
1076 C(truesize);
Reshetova, Elena63354792017-06-30 13:07:58 +03001077 refcount_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 atomic_inc(&(skb_shinfo(skb)->dataref));
1080 skb->cloned = 1;
1081
1082 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001083#undef C
1084}
1085
1086/**
Jakub Kicinskida29e4b2019-06-03 15:16:58 -07001087 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1088 * @first: first sk_buff of the msg
1089 */
1090struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1091{
1092 struct sk_buff *n;
1093
1094 n = alloc_skb(0, GFP_ATOMIC);
1095 if (!n)
1096 return NULL;
1097
1098 n->len = first->len;
1099 n->data_len = first->len;
1100 n->truesize = first->truesize;
1101
1102 skb_shinfo(n)->frag_list = first;
1103
1104 __copy_skb_header(n, first);
1105 n->destructor = NULL;
1106
1107 return n;
1108}
1109EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1110
1111/**
Herbert Xue0053ec2007-10-14 00:37:52 -07001112 * skb_morph - morph one skb into another
1113 * @dst: the skb to receive the contents
1114 * @src: the skb to supply the contents
1115 *
1116 * This is identical to skb_clone except that the target skb is
1117 * supplied by the user.
1118 *
1119 * The target skb is returned upon exit.
1120 */
1121struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1122{
Herbert Xu2d4baff2007-11-26 23:11:19 +08001123 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -07001124 return __skb_clone(dst, src);
1125}
1126EXPORT_SYMBOL_GPL(skb_morph);
1127
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001128int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001129{
1130 unsigned long max_pg, num_pg, new_pg, old_pg;
1131 struct user_struct *user;
1132
1133 if (capable(CAP_IPC_LOCK) || !size)
1134 return 0;
1135
1136 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
1137 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1138 user = mmp->user ? : current_user();
1139
1140 do {
1141 old_pg = atomic_long_read(&user->locked_vm);
1142 new_pg = old_pg + num_pg;
1143 if (new_pg > max_pg)
1144 return -ENOBUFS;
1145 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
1146 old_pg);
1147
1148 if (!mmp->user) {
1149 mmp->user = get_uid(user);
1150 mmp->num_pg = num_pg;
1151 } else {
1152 mmp->num_pg += num_pg;
1153 }
1154
1155 return 0;
1156}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001157EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001158
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001159void mm_unaccount_pinned_pages(struct mmpin *mmp)
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001160{
1161 if (mmp->user) {
1162 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1163 free_uid(mmp->user);
1164 }
1165}
Sowmini Varadhan6f89dbc2018-02-15 10:49:32 -08001166EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001167
Jonathan Lemon8c793822021-01-06 14:18:37 -08001168struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
Willem de Bruijn52267792017-08-03 16:29:39 -04001169{
1170 struct ubuf_info *uarg;
1171 struct sk_buff *skb;
1172
1173 WARN_ON_ONCE(!in_task());
1174
1175 skb = sock_omalloc(sk, 0, GFP_KERNEL);
1176 if (!skb)
1177 return NULL;
1178
1179 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1180 uarg = (void *)skb->cb;
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001181 uarg->mmp.user = NULL;
1182
1183 if (mm_account_pinned_pages(&uarg->mmp, size)) {
1184 kfree_skb(skb);
1185 return NULL;
1186 }
Willem de Bruijn52267792017-08-03 16:29:39 -04001187
Jonathan Lemon8c793822021-01-06 14:18:37 -08001188 uarg->callback = msg_zerocopy_callback;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001189 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1190 uarg->len = 1;
1191 uarg->bytelen = size;
Willem de Bruijn52267792017-08-03 16:29:39 -04001192 uarg->zerocopy = 1;
Jonathan Lemon04c2d332021-01-06 14:18:39 -08001193 uarg->flags = SKBFL_ZEROCOPY_FRAG;
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001194 refcount_set(&uarg->refcnt, 1);
Willem de Bruijn52267792017-08-03 16:29:39 -04001195 sock_hold(sk);
1196
1197 return uarg;
1198}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001199EXPORT_SYMBOL_GPL(msg_zerocopy_alloc);
Willem de Bruijn52267792017-08-03 16:29:39 -04001200
1201static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
1202{
1203 return container_of((void *)uarg, struct sk_buff, cb);
1204}
1205
Jonathan Lemon8c793822021-01-06 14:18:37 -08001206struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1207 struct ubuf_info *uarg)
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001208{
1209 if (uarg) {
1210 const u32 byte_limit = 1 << 19; /* limit to a few TSO */
1211 u32 bytelen, next;
1212
1213 /* realloc only when socket is locked (TCP, UDP cork),
1214 * so uarg->len and sk_zckey access is serialized
1215 */
1216 if (!sock_owned_by_user(sk)) {
1217 WARN_ON_ONCE(1);
1218 return NULL;
1219 }
1220
1221 bytelen = uarg->bytelen + size;
1222 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1223 /* TCP can create new skb to attach new uarg */
1224 if (sk->sk_type == SOCK_STREAM)
1225 goto new_alloc;
1226 return NULL;
1227 }
1228
1229 next = (u32)atomic_read(&sk->sk_zckey);
1230 if ((u32)(uarg->id + uarg->len) == next) {
Willem de Bruijna91dbff2017-08-03 16:29:43 -04001231 if (mm_account_pinned_pages(&uarg->mmp, size))
1232 return NULL;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001233 uarg->len++;
1234 uarg->bytelen = bytelen;
1235 atomic_set(&sk->sk_zckey, ++next);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001236
1237 /* no extra ref when appending to datagram (MSG_MORE) */
1238 if (sk->sk_type == SOCK_STREAM)
Jonathan Lemon8e044912021-01-06 14:18:41 -08001239 net_zcopy_get(uarg);
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001240
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001241 return uarg;
1242 }
1243 }
1244
1245new_alloc:
Jonathan Lemon8c793822021-01-06 14:18:37 -08001246 return msg_zerocopy_alloc(sk, size);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001247}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001248EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001249
1250static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1251{
1252 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1253 u32 old_lo, old_hi;
1254 u64 sum_len;
1255
1256 old_lo = serr->ee.ee_info;
1257 old_hi = serr->ee.ee_data;
1258 sum_len = old_hi - old_lo + 1ULL + len;
1259
1260 if (sum_len >= (1ULL << 32))
1261 return false;
1262
1263 if (lo != old_hi + 1)
1264 return false;
1265
1266 serr->ee.ee_data += len;
1267 return true;
1268}
1269
Jonathan Lemon8c793822021-01-06 14:18:37 -08001270static void __msg_zerocopy_callback(struct ubuf_info *uarg)
Willem de Bruijn52267792017-08-03 16:29:39 -04001271{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001272 struct sk_buff *tail, *skb = skb_from_uarg(uarg);
Willem de Bruijn52267792017-08-03 16:29:39 -04001273 struct sock_exterr_skb *serr;
1274 struct sock *sk = skb->sk;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001275 struct sk_buff_head *q;
1276 unsigned long flags;
Willem de Bruijn3bdd5ee2021-06-09 18:41:57 -04001277 bool is_zerocopy;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001278 u32 lo, hi;
1279 u16 len;
Willem de Bruijn52267792017-08-03 16:29:39 -04001280
Willem de Bruijnccaffff2017-08-09 19:09:43 -04001281 mm_unaccount_pinned_pages(&uarg->mmp);
1282
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001283 /* if !len, there was only 1 call, and it was aborted
1284 * so do not queue a completion notification
1285 */
1286 if (!uarg->len || sock_flag(sk, SOCK_DEAD))
Willem de Bruijn52267792017-08-03 16:29:39 -04001287 goto release;
1288
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001289 len = uarg->len;
1290 lo = uarg->id;
1291 hi = uarg->id + len - 1;
Willem de Bruijn3bdd5ee2021-06-09 18:41:57 -04001292 is_zerocopy = uarg->zerocopy;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001293
Willem de Bruijn52267792017-08-03 16:29:39 -04001294 serr = SKB_EXT_ERR(skb);
1295 memset(serr, 0, sizeof(*serr));
1296 serr->ee.ee_errno = 0;
1297 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001298 serr->ee.ee_data = hi;
1299 serr->ee.ee_info = lo;
Willem de Bruijn3bdd5ee2021-06-09 18:41:57 -04001300 if (!is_zerocopy)
Willem de Bruijn52267792017-08-03 16:29:39 -04001301 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1302
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001303 q = &sk->sk_error_queue;
1304 spin_lock_irqsave(&q->lock, flags);
1305 tail = skb_peek_tail(q);
1306 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1307 !skb_zerocopy_notify_extend(tail, lo, len)) {
1308 __skb_queue_tail(q, skb);
1309 skb = NULL;
1310 }
1311 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn52267792017-08-03 16:29:39 -04001312
Alexander Aringe3ae2362021-06-27 18:48:21 -04001313 sk_error_report(sk);
Willem de Bruijn52267792017-08-03 16:29:39 -04001314
1315release:
1316 consume_skb(skb);
1317 sock_put(sk);
1318}
Jonathan Lemon75518852021-01-06 14:18:31 -08001319
Jonathan Lemon8c793822021-01-06 14:18:37 -08001320void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
1321 bool success)
Jonathan Lemon75518852021-01-06 14:18:31 -08001322{
1323 uarg->zerocopy = uarg->zerocopy & success;
1324
1325 if (refcount_dec_and_test(&uarg->refcnt))
Jonathan Lemon8c793822021-01-06 14:18:37 -08001326 __msg_zerocopy_callback(uarg);
Jonathan Lemon75518852021-01-06 14:18:31 -08001327}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001328EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
Willem de Bruijn52267792017-08-03 16:29:39 -04001329
Jonathan Lemon8c793822021-01-06 14:18:37 -08001330void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
Willem de Bruijn52267792017-08-03 16:29:39 -04001331{
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001332 struct sock *sk = skb_from_uarg(uarg)->sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001333
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001334 atomic_dec(&sk->sk_zckey);
1335 uarg->len--;
Willem de Bruijn52267792017-08-03 16:29:39 -04001336
Jonathan Lemon236a6b12021-01-06 14:18:35 -08001337 if (have_uref)
Jonathan Lemon8c793822021-01-06 14:18:37 -08001338 msg_zerocopy_callback(NULL, uarg, true);
Willem de Bruijn52267792017-08-03 16:29:39 -04001339}
Jonathan Lemon8c793822021-01-06 14:18:37 -08001340EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
Willem de Bruijn52267792017-08-03 16:29:39 -04001341
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001342int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
1343{
1344 return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
1345}
1346EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
1347
Willem de Bruijn52267792017-08-03 16:29:39 -04001348int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1349 struct msghdr *msg, int len,
1350 struct ubuf_info *uarg)
1351{
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001352 struct ubuf_info *orig_uarg = skb_zcopy(skb);
Willem de Bruijn52267792017-08-03 16:29:39 -04001353 struct iov_iter orig_iter = msg->msg_iter;
1354 int err, orig_len = skb->len;
1355
Willem de Bruijn4ab6c992017-08-03 16:29:42 -04001356 /* An skb can only point to one uarg. This edge case happens when
1357 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1358 */
1359 if (orig_uarg && uarg != orig_uarg)
1360 return -EEXIST;
1361
Willem de Bruijn52267792017-08-03 16:29:39 -04001362 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
1363 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
Willem de Bruijn54d431172017-10-19 12:40:39 -04001364 struct sock *save_sk = skb->sk;
1365
Willem de Bruijn52267792017-08-03 16:29:39 -04001366 /* Streams do not free skb on error. Reset to prev state. */
1367 msg->msg_iter = orig_iter;
Willem de Bruijn54d431172017-10-19 12:40:39 -04001368 skb->sk = sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001369 ___pskb_trim(skb, orig_len);
Willem de Bruijn54d431172017-10-19 12:40:39 -04001370 skb->sk = save_sk;
Willem de Bruijn52267792017-08-03 16:29:39 -04001371 return err;
1372 }
1373
Willem de Bruijn52900d22018-11-30 15:32:40 -05001374 skb_zcopy_set(skb, uarg, NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001375 return skb->len - orig_len;
1376}
1377EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1378
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001379static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
Willem de Bruijn52267792017-08-03 16:29:39 -04001380 gfp_t gfp_mask)
1381{
1382 if (skb_zcopy(orig)) {
1383 if (skb_zcopy(nskb)) {
1384 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1385 if (!gfp_mask) {
1386 WARN_ON_ONCE(1);
1387 return -ENOMEM;
1388 }
1389 if (skb_uarg(nskb) == skb_uarg(orig))
1390 return 0;
1391 if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1392 return -EIO;
1393 }
Willem de Bruijn52900d22018-11-30 15:32:40 -05001394 skb_zcopy_set(nskb, skb_uarg(orig), NULL);
Willem de Bruijn52267792017-08-03 16:29:39 -04001395 }
1396 return 0;
1397}
1398
Ben Hutchings2c530402012-07-10 10:55:09 +00001399/**
1400 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001401 * @skb: the skb to modify
1402 * @gfp_mask: allocation priority
1403 *
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08001404 * This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001405 * It will copy all frags into kernel and drop the reference
1406 * to userspace pages.
1407 *
1408 * If this function is called from an interrupt gfp_mask() must be
1409 * %GFP_ATOMIC.
1410 *
1411 * Returns 0 on success or a negative error code on failure
1412 * to allocate kernel memory to copy to.
1413 */
1414int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
Shirley Maa6686f22011-07-06 12:22:12 +00001415{
Shirley Maa6686f22011-07-06 12:22:12 +00001416 int num_frags = skb_shinfo(skb)->nr_frags;
1417 struct page *page, *head = NULL;
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001418 int i, new_frags;
1419 u32 d_off;
Shirley Maa6686f22011-07-06 12:22:12 +00001420
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001421 if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1422 return -EINVAL;
1423
Willem de Bruijnf72c4ac2017-12-28 12:38:13 -05001424 if (!num_frags)
1425 goto release;
1426
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001427 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1428 for (i = 0; i < new_frags; i++) {
Krishna Kumar02756ed2012-07-17 02:05:29 +00001429 page = alloc_page(gfp_mask);
Shirley Maa6686f22011-07-06 12:22:12 +00001430 if (!page) {
1431 while (head) {
Sunghan Suh40dadff2013-07-12 16:17:23 +09001432 struct page *next = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001433 put_page(head);
1434 head = next;
1435 }
1436 return -ENOMEM;
1437 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001438 set_page_private(page, (unsigned long)head);
1439 head = page;
1440 }
1441
1442 page = head;
1443 d_off = 0;
1444 for (i = 0; i < num_frags; i++) {
1445 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1446 u32 p_off, p_len, copied;
1447 struct page *p;
1448 u8 *vaddr;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001449
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07001450 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
Willem de Bruijnc613c202017-07-31 08:15:47 -04001451 p, p_off, p_len, copied) {
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001452 u32 copy, done = 0;
Willem de Bruijnc613c202017-07-31 08:15:47 -04001453 vaddr = kmap_atomic(p);
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001454
1455 while (done < p_len) {
1456 if (d_off == PAGE_SIZE) {
1457 d_off = 0;
1458 page = (struct page *)page_private(page);
1459 }
1460 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1461 memcpy(page_address(page) + d_off,
1462 vaddr + p_off + done, copy);
1463 done += copy;
1464 d_off += copy;
1465 }
Willem de Bruijnc613c202017-07-31 08:15:47 -04001466 kunmap_atomic(vaddr);
1467 }
Shirley Maa6686f22011-07-06 12:22:12 +00001468 }
1469
1470 /* skb frags release userspace buffers */
Krishna Kumar02756ed2012-07-17 02:05:29 +00001471 for (i = 0; i < num_frags; i++)
Ian Campbella8605c62011-10-19 23:01:49 +00001472 skb_frag_unref(skb, i);
Shirley Maa6686f22011-07-06 12:22:12 +00001473
Shirley Maa6686f22011-07-06 12:22:12 +00001474 /* skb frags point to kernel buffers */
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001475 for (i = 0; i < new_frags - 1; i++) {
1476 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
Sunghan Suh40dadff2013-07-12 16:17:23 +09001477 head = (struct page *)page_private(head);
Shirley Maa6686f22011-07-06 12:22:12 +00001478 }
Willem de Bruijn3ece7822017-08-03 16:29:38 -04001479 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1480 skb_shinfo(skb)->nr_frags = new_frags;
Michael S. Tsirkin48c83012011-08-31 08:03:29 +00001481
Willem de Bruijnb90ddd52017-12-20 17:37:50 -05001482release:
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001483 skb_zcopy_clear(skb, false);
Shirley Maa6686f22011-07-06 12:22:12 +00001484 return 0;
1485}
Michael S. Tsirkindcc0fb72012-07-20 09:23:20 +00001486EXPORT_SYMBOL_GPL(skb_copy_ubufs);
Shirley Maa6686f22011-07-06 12:22:12 +00001487
Herbert Xue0053ec2007-10-14 00:37:52 -07001488/**
1489 * skb_clone - duplicate an sk_buff
1490 * @skb: buffer to clone
1491 * @gfp_mask: allocation priority
1492 *
1493 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1494 * copies share the same packet data but not structure. The new
1495 * buffer has a reference count of 1. If the allocation fails the
1496 * function returns %NULL otherwise the new buffer is returned.
1497 *
1498 * If this function is called from an interrupt gfp_mask() must be
1499 * %GFP_ATOMIC.
1500 */
1501
1502struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1503{
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001504 struct sk_buff_fclones *fclones = container_of(skb,
1505 struct sk_buff_fclones,
1506 skb1);
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001507 struct sk_buff *n;
Herbert Xue0053ec2007-10-14 00:37:52 -07001508
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001509 if (skb_orphan_frags(skb, gfp_mask))
1510 return NULL;
Shirley Maa6686f22011-07-06 12:22:12 +00001511
Herbert Xue0053ec2007-10-14 00:37:52 -07001512 if (skb->fclone == SKB_FCLONE_ORIG &&
Reshetova, Elena26385952017-06-30 13:07:59 +03001513 refcount_read(&fclones->fclone_ref) == 1) {
Eric Dumazet6ffe75eb2014-12-03 17:04:39 -08001514 n = &fclones->skb2;
Reshetova, Elena26385952017-06-30 13:07:59 +03001515 refcount_set(&fclones->fclone_ref, 2);
Herbert Xue0053ec2007-10-14 00:37:52 -07001516 } else {
Mel Gormanc93bdd02012-07-31 16:44:19 -07001517 if (skb_pfmemalloc(skb))
1518 gfp_mask |= __GFP_MEMALLOC;
1519
Herbert Xue0053ec2007-10-14 00:37:52 -07001520 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1521 if (!n)
1522 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +02001523
Herbert Xue0053ec2007-10-14 00:37:52 -07001524 n->fclone = SKB_FCLONE_UNAVAILABLE;
1525 }
1526
1527 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001529EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001531void skb_headers_offset_update(struct sk_buff *skb, int off)
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001532{
Eric Dumazet030737b2013-10-19 11:42:54 -07001533 /* Only adjust this if it actually is csum_start rather than csum */
1534 if (skb->ip_summed == CHECKSUM_PARTIAL)
1535 skb->csum_start += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001536 /* {transport,network,mac}_header and tail are relative to skb->head */
1537 skb->transport_header += off;
1538 skb->network_header += off;
1539 if (skb_mac_header_was_set(skb))
1540 skb->mac_header += off;
1541 skb->inner_transport_header += off;
1542 skb->inner_network_header += off;
Pravin B Shelaraefbd2b2013-03-07 13:21:46 +00001543 skb->inner_mac_header += off;
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001544}
Toshiaki Makitab0768a82018-08-03 16:58:09 +09001545EXPORT_SYMBOL(skb_headers_offset_update);
Pravin B Shelarf5b17292013-03-07 13:21:40 +00001546
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001547void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Herbert Xudec18812007-10-14 00:37:30 -07001549 __copy_skb_header(new, old);
1550
Herbert Xu79671682006-06-22 02:40:14 -07001551 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1552 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1553 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001555EXPORT_SYMBOL(skb_copy_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Mel Gormanc93bdd02012-07-31 16:44:19 -07001557static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1558{
1559 if (skb_pfmemalloc(skb))
1560 return SKB_ALLOC_RX;
1561 return 0;
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/**
1565 * skb_copy - create private copy of an sk_buff
1566 * @skb: buffer to copy
1567 * @gfp_mask: allocation priority
1568 *
1569 * Make a copy of both an &sk_buff and its data. This is used when the
1570 * caller wishes to modify the data and needs a private copy of the
1571 * data to alter. Returns %NULL on failure or the pointer to the buffer
1572 * on success. The returned buffer has a reference count of 1.
1573 *
1574 * As by-product this function converts non-linear &sk_buff to linear
1575 * one, so that &sk_buff becomes completely private and caller is allowed
1576 * to modify all the data of returned buffer. This means that this
1577 * function is not recommended for use in circumstances when only
1578 * header is going to be modified. Use pskb_copy() instead.
1579 */
1580
Al Virodd0fc662005-10-07 07:46:04 +01001581struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001583 int headerlen = skb_headroom(skb);
Alexander Duyckec47ea82012-05-04 14:26:56 +00001584 unsigned int size = skb_end_offset(skb) + skb->data_len;
Mel Gormanc93bdd02012-07-31 16:44:19 -07001585 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1586 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (!n)
1589 return NULL;
1590
1591 /* Set the data pointer */
1592 skb_reserve(n, headerlen);
1593 /* Set the tail pointer and length */
1594 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Tim Hansen9f77fad2017-10-09 11:37:59 -04001596 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001598 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return n;
1600}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001601EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603/**
Octavian Purdilabad93e92014-06-12 01:36:26 +03001604 * __pskb_copy_fclone - create copy of an sk_buff with private head.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 * @skb: buffer to copy
Eric Dumazet117632e2011-12-03 21:39:53 +00001606 * @headroom: headroom of new skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * @gfp_mask: allocation priority
Octavian Purdilabad93e92014-06-12 01:36:26 +03001608 * @fclone: if true allocate the copy of the skb from the fclone
1609 * cache instead of the head cache; it is recommended to set this
1610 * to true for the cases where the copy will likely be cloned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 *
1612 * Make a copy of both an &sk_buff and part of its data, located
1613 * in header. Fragmented data remain shared. This is used when
1614 * the caller wishes to modify only header of &sk_buff and needs
1615 * private copy of the header to alter. Returns %NULL on failure
1616 * or the pointer to the buffer on success.
1617 * The returned buffer has a reference count of 1.
1618 */
1619
Octavian Purdilabad93e92014-06-12 01:36:26 +03001620struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1621 gfp_t gfp_mask, bool fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Eric Dumazet117632e2011-12-03 21:39:53 +00001623 unsigned int size = skb_headlen(skb) + headroom;
Octavian Purdilabad93e92014-06-12 01:36:26 +03001624 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1625 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (!n)
1628 goto out;
1629
1630 /* Set the data pointer */
Eric Dumazet117632e2011-12-03 21:39:53 +00001631 skb_reserve(n, headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /* Set the tail pointer and length */
1633 skb_put(n, skb_headlen(skb));
1634 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001635 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Herbert Xu25f484a2006-11-07 14:57:15 -08001637 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 n->data_len = skb->data_len;
1639 n->len = skb->len;
1640
1641 if (skb_shinfo(skb)->nr_frags) {
1642 int i;
1643
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001644 if (skb_orphan_frags(skb, gfp_mask) ||
1645 skb_zerocopy_clone(n, skb, gfp_mask)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001646 kfree_skb(n);
1647 n = NULL;
1648 goto out;
Shirley Maa6686f22011-07-06 12:22:12 +00001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1651 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
Ian Campbellea2ab692011-08-22 23:44:58 +00001652 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654 skb_shinfo(n)->nr_frags = i;
1655 }
1656
David S. Miller21dc3302010-08-23 00:13:46 -07001657 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1659 skb_clone_fraglist(n);
1660 }
1661
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001662 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663out:
1664 return n;
1665}
Octavian Purdilabad93e92014-06-12 01:36:26 +03001666EXPORT_SYMBOL(__pskb_copy_fclone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668/**
1669 * pskb_expand_head - reallocate header of &sk_buff
1670 * @skb: buffer to reallocate
1671 * @nhead: room to add at head
1672 * @ntail: room to add at tail
1673 * @gfp_mask: allocation priority
1674 *
Mathias Krausebc323832013-11-07 14:18:26 +01001675 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1676 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * reference count of 1. Returns zero in the case of success or error,
1678 * if expansion failed. In the last case, &sk_buff is not changed.
1679 *
1680 * All the pointers pointing into skb header may change and must be
1681 * reloaded after call to this function.
1682 */
1683
Victor Fusco86a76ca2005-07-08 14:57:47 -07001684int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +01001685 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Eric Dumazet158f3232017-01-27 07:11:27 -08001687 int i, osize = skb_end_offset(skb);
1688 int size = osize + nhead + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 long off;
Eric Dumazet158f3232017-01-27 07:11:27 -08001690 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Herbert Xu4edd87a2008-10-01 07:09:38 -07001692 BUG_ON(nhead < 0);
1693
Tim Hansen9f77fad2017-10-09 11:37:59 -04001694 BUG_ON(skb_shared(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 size = SKB_DATA_ALIGN(size);
1697
Mel Gormanc93bdd02012-07-31 16:44:19 -07001698 if (skb_pfmemalloc(skb))
1699 gfp_mask |= __GFP_MEMALLOC;
1700 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1701 gfp_mask, NUMA_NO_NODE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!data)
1703 goto nodata;
Eric Dumazet87151b82012-04-10 20:08:39 +00001704 size = SKB_WITH_OVERHEAD(ksize(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +00001707 * optimized for the cases when header is void.
1708 */
1709 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1710
1711 memcpy((struct skb_shared_info *)(data + size),
1712 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +00001713 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Alexander Duyck3e245912012-05-04 14:26:51 +00001715 /*
1716 * if shinfo is shared we must drop the old head gracefully, but if it
1717 * is not we can just drop the old head and let the existing refcount
1718 * be since all we did is relocate the values
1719 */
1720 if (skb_cloned(skb)) {
Michael S. Tsirkin70008aa2012-07-20 09:23:10 +00001721 if (skb_orphan_frags(skb, gfp_mask))
1722 goto nofrags;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04001723 if (skb_zcopy(skb))
Eric Dumazetc1d1b432017-08-31 16:48:22 -07001724 refcount_inc(&skb_uarg(skb)->refcnt);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001725 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00001726 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Eric Dumazet1fd63042010-09-02 23:09:32 +00001728 if (skb_has_frag_list(skb))
1729 skb_clone_fraglist(skb);
1730
1731 skb_release_data(skb);
Alexander Duyck3e245912012-05-04 14:26:51 +00001732 } else {
1733 skb_free_head(skb);
Eric Dumazet1fd63042010-09-02 23:09:32 +00001734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 off = (data + nhead) - skb->head;
1736
1737 skb->head = data;
Eric Dumazetd3836f22012-04-27 00:33:38 +00001738 skb->head_frag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001740#ifdef NET_SKBUFF_DATA_USES_OFFSET
1741 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001742 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001743#else
1744 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -07001745#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001746 skb->tail += off;
Peter Pan(潘卫平)b41abb42013-06-06 21:27:21 +08001747 skb_headers_offset_update(skb, nhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -07001749 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 skb->nohdr = 0;
1751 atomic_set(&skb_shinfo(skb)->dataref, 1);
Eric Dumazet158f3232017-01-27 07:11:27 -08001752
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001753 skb_metadata_clear(skb);
1754
Eric Dumazet158f3232017-01-27 07:11:27 -08001755 /* It is not generally safe to change skb->truesize.
1756 * For the moment, we really care of rx path, or
1757 * when skb is orphaned (not attached to a socket).
1758 */
1759 if (!skb->sk || skb->destructor == sock_edemux)
1760 skb->truesize += size - osize;
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763
Shirley Maa6686f22011-07-06 12:22:12 +00001764nofrags:
1765 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766nodata:
1767 return -ENOMEM;
1768}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001769EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771/* Make private copy of skb with writable head and some headroom */
1772
1773struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1774{
1775 struct sk_buff *skb2;
1776 int delta = headroom - skb_headroom(skb);
1777
1778 if (delta <= 0)
1779 skb2 = pskb_copy(skb, GFP_ATOMIC);
1780 else {
1781 skb2 = skb_clone(skb, GFP_ATOMIC);
1782 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1783 GFP_ATOMIC)) {
1784 kfree_skb(skb2);
1785 skb2 = NULL;
1786 }
1787 }
1788 return skb2;
1789}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001790EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792/**
1793 * skb_copy_expand - copy and expand sk_buff
1794 * @skb: buffer to copy
1795 * @newheadroom: new free bytes at head
1796 * @newtailroom: new free bytes at tail
1797 * @gfp_mask: allocation priority
1798 *
1799 * Make a copy of both an &sk_buff and its data and while doing so
1800 * allocate additional space.
1801 *
1802 * This is used when the caller wishes to modify the data and needs a
1803 * private copy of the data to alter as well as more space for new fields.
1804 * Returns %NULL on failure or the pointer to the buffer
1805 * on success. The returned buffer has a reference count of 1.
1806 *
1807 * You must pass %GFP_ATOMIC as the allocation priority if this function
1808 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
1810struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001811 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001812 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 /*
1815 * Allocate the copy buffer
1816 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001817 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1818 gfp_mask, skb_alloc_rx_flag(skb),
1819 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001820 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int head_copy_len, head_copy_off;
1822
1823 if (!n)
1824 return NULL;
1825
1826 skb_reserve(n, newheadroom);
1827
1828 /* Set the tail pointer and length */
1829 skb_put(n, skb->len);
1830
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001831 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 head_copy_off = 0;
1833 if (newheadroom <= head_copy_len)
1834 head_copy_len = newheadroom;
1835 else
1836 head_copy_off = newheadroom - head_copy_len;
1837
1838 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001839 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1840 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001842 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Eric Dumazet030737b2013-10-19 11:42:54 -07001844 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return n;
1847}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001848EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001851 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * @skb: buffer to pad
1853 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001854 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
1856 * Ensure that a buffer is followed by a padding area that is zero
1857 * filled. Used by network drivers which may DMA or transfer data
1858 * beyond the buffer end onto the wire.
1859 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001860 * May return error in out of memory cases. The skb is freed on error
1861 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001863
Florian Fainellicd0a1372017-08-22 15:12:14 -07001864int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Herbert Xu5b057c62006-06-23 02:06:41 -07001866 int err;
1867 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001870 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001874
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001875 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001876 if (likely(skb_cloned(skb) || ntail > 0)) {
1877 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1878 if (unlikely(err))
1879 goto free_skb;
1880 }
1881
1882 /* FIXME: The use of this function with non-linear skb's really needs
1883 * to be audited.
1884 */
1885 err = skb_linearize(skb);
1886 if (unlikely(err))
1887 goto free_skb;
1888
1889 memset(skb->data + skb->len, 0, pad);
1890 return 0;
1891
1892free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001893 if (free_on_error)
1894 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001895 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001896}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001897EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001898
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001899/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001900 * pskb_put - add data to the tail of a potentially fragmented buffer
1901 * @skb: start of the buffer to use
1902 * @tail: tail fragment of the buffer to use
1903 * @len: amount of data to add
1904 *
1905 * This function extends the used data area of the potentially
1906 * fragmented buffer. @tail must be the last fragment of @skb -- or
1907 * @skb itself. If this would exceed the total buffer size the kernel
1908 * will panic. A pointer to the first byte of the extra data is
1909 * returned.
1910 */
1911
Johannes Berg4df864c2017-06-16 14:29:21 +02001912void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001913{
1914 if (tail != skb) {
1915 skb->data_len += len;
1916 skb->len += len;
1917 }
1918 return skb_put(tail, len);
1919}
1920EXPORT_SYMBOL_GPL(pskb_put);
1921
1922/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001923 * skb_put - add data to a buffer
1924 * @skb: buffer to use
1925 * @len: amount of data to add
1926 *
1927 * This function extends the used data area of the buffer. If this would
1928 * exceed the total buffer size the kernel will panic. A pointer to the
1929 * first byte of the extra data is returned.
1930 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001931void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001932{
Johannes Berg4df864c2017-06-16 14:29:21 +02001933 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001934 SKB_LINEAR_ASSERT(skb);
1935 skb->tail += len;
1936 skb->len += len;
1937 if (unlikely(skb->tail > skb->end))
1938 skb_over_panic(skb, len, __builtin_return_address(0));
1939 return tmp;
1940}
1941EXPORT_SYMBOL(skb_put);
1942
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001943/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001944 * skb_push - add data to the start of a buffer
1945 * @skb: buffer to use
1946 * @len: amount of data to add
1947 *
1948 * This function extends the used data area of the buffer at the buffer
1949 * start. If this would exceed the total buffer headroom the kernel will
1950 * panic. A pointer to the first byte of the extra data is returned.
1951 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001952void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001953{
1954 skb->data -= len;
1955 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301956 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001957 skb_under_panic(skb, len, __builtin_return_address(0));
1958 return skb->data;
1959}
1960EXPORT_SYMBOL(skb_push);
1961
1962/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001963 * skb_pull - remove data from the start of a buffer
1964 * @skb: buffer to use
1965 * @len: amount of data to remove
1966 *
1967 * This function removes data from the start of a buffer, returning
1968 * the memory to the headroom. A pointer to the next data in the buffer
1969 * is returned. Once the data has been pulled future pushes will overwrite
1970 * the old data.
1971 */
Johannes Bergaf728682017-06-16 14:29:22 +02001972void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001973{
David S. Miller47d29642010-05-02 02:21:44 -07001974 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001975}
1976EXPORT_SYMBOL(skb_pull);
1977
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001978/**
1979 * skb_trim - remove end from a buffer
1980 * @skb: buffer to alter
1981 * @len: new length
1982 *
1983 * Cut the length of a buffer down by removing data from the tail. If
1984 * the buffer is already under the length specified it is not modified.
1985 * The skb must be linear.
1986 */
1987void skb_trim(struct sk_buff *skb, unsigned int len)
1988{
1989 if (skb->len > len)
1990 __skb_trim(skb, len);
1991}
1992EXPORT_SYMBOL(skb_trim);
1993
Herbert Xu3cc0e872006-06-09 16:13:38 -07001994/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
1996
Herbert Xu3cc0e872006-06-09 16:13:38 -07001997int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Herbert Xu27b437c2006-07-13 19:26:39 -07001999 struct sk_buff **fragp;
2000 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int offset = skb_headlen(skb);
2002 int nfrags = skb_shinfo(skb)->nr_frags;
2003 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07002004 int err;
2005
2006 if (skb_cloned(skb) &&
2007 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002010 i = 0;
2011 if (offset >= len)
2012 goto drop_pages;
2013
2014 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002015 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07002016
2017 if (end < len) {
2018 offset = end;
2019 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Herbert Xu27b437c2006-07-13 19:26:39 -07002021
Eric Dumazet9e903e02011-10-18 21:00:24 +00002022 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07002023
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002024drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07002025 skb_shinfo(skb)->nr_frags = i;
2026
2027 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00002028 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07002029
David S. Miller21dc3302010-08-23 00:13:46 -07002030 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07002031 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002032 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Herbert Xu27b437c2006-07-13 19:26:39 -07002035 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2036 fragp = &frag->next) {
2037 int end = offset + frag->len;
2038
2039 if (skb_shared(frag)) {
2040 struct sk_buff *nfrag;
2041
2042 nfrag = skb_clone(frag, GFP_ATOMIC);
2043 if (unlikely(!nfrag))
2044 return -ENOMEM;
2045
2046 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00002047 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07002048 frag = nfrag;
2049 *fragp = frag;
2050 }
2051
2052 if (end < len) {
2053 offset = end;
2054 continue;
2055 }
2056
2057 if (end > len &&
2058 unlikely((err = pskb_trim(frag, len - offset))))
2059 return err;
2060
2061 if (frag->next)
2062 skb_drop_list(&frag->next);
2063 break;
2064 }
2065
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002066done:
Herbert Xu27b437c2006-07-13 19:26:39 -07002067 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 skb->data_len -= skb->len - len;
2069 skb->len = len;
2070 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07002071 skb->len = len;
2072 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002073 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002076 if (!skb->sk || skb->destructor == sock_edemux)
2077 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
2079}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002080EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Eric Dumazet88078d92018-04-18 11:43:15 -07002082/* Note : use pskb_trim_rcsum() instead of calling this directly
2083 */
2084int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2085{
2086 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2087 int delta = skb->len - len;
2088
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002089 skb->csum = csum_block_sub(skb->csum,
2090 skb_checksum(skb, len, delta, 0),
2091 len);
Vasily Averin54970a22020-12-14 22:07:39 +03002092 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2093 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2094 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2095
2096 if (offset + sizeof(__sum16) > hdlen)
2097 return -EINVAL;
Eric Dumazet88078d92018-04-18 11:43:15 -07002098 }
2099 return __pskb_trim(skb, len);
2100}
2101EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/**
2104 * __pskb_pull_tail - advance tail of skb header
2105 * @skb: buffer to reallocate
2106 * @delta: number of bytes to advance tail
2107 *
2108 * The function makes a sense only on a fragmented &sk_buff,
2109 * it expands header moving its tail forward and copying necessary
2110 * data from fragmented part.
2111 *
2112 * &sk_buff MUST have reference count of 1.
2113 *
2114 * Returns %NULL (and &sk_buff does not change) if pull failed
2115 * or value of new tail of skb in the case of success.
2116 *
2117 * All the pointers pointing into skb header may change and must be
2118 * reloaded after call to this function.
2119 */
2120
2121/* Moves tail of skb head forward, copying data from fragmented part,
2122 * when it is necessary.
2123 * 1. It may fail due to malloc failure.
2124 * 2. It may change skb pointers.
2125 *
2126 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2127 */
Johannes Bergaf728682017-06-16 14:29:22 +02002128void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 /* If skb has not enough free space at tail, get new one
2131 * plus 128 bytes for future expansions. If we have enough
2132 * room at tail, reallocate without expansion only if skb is cloned.
2133 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002134 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (eat > 0 || skb_cloned(skb)) {
2137 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2138 GFP_ATOMIC))
2139 return NULL;
2140 }
2141
Tim Hansen9f77fad2017-10-09 11:37:59 -04002142 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2143 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* Optimization: no fragments, no reasons to preestimate
2146 * size of pulled pages. Superb.
2147 */
David S. Miller21dc3302010-08-23 00:13:46 -07002148 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto pull_pages;
2150
2151 /* Estimate size of pulled pages. */
2152 eat = delta;
2153 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002154 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2155
2156 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002158 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160
2161 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002162 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * but taking into account that pulling is expected to
2164 * be very rare operation, it is worth to fight against
2165 * further bloating skb head and crucify ourselves here instead.
2166 * Pure masohism, indeed. 8)8)
2167 */
2168 if (eat) {
2169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2170 struct sk_buff *clone = NULL;
2171 struct sk_buff *insp = NULL;
2172
2173 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (list->len <= eat) {
2175 /* Eaten as whole. */
2176 eat -= list->len;
2177 list = list->next;
2178 insp = list;
2179 } else {
2180 /* Eaten partially. */
2181
2182 if (skb_shared(list)) {
2183 /* Sucks! We need to fork list. :-( */
2184 clone = skb_clone(list, GFP_ATOMIC);
2185 if (!clone)
2186 return NULL;
2187 insp = list->next;
2188 list = clone;
2189 } else {
2190 /* This may be pulled without
2191 * problems. */
2192 insp = list;
2193 }
2194 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002195 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return NULL;
2197 }
2198 break;
2199 }
2200 } while (eat);
2201
2202 /* Free pulled out fragments. */
2203 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2204 skb_shinfo(skb)->frag_list = list->next;
2205 kfree_skb(list);
2206 }
2207 /* And insert new clone at head. */
2208 if (clone) {
2209 clone->next = list;
2210 skb_shinfo(skb)->frag_list = clone;
2211 }
2212 }
2213 /* Success! Now we may commit changes to skb data. */
2214
2215pull_pages:
2216 eat = delta;
2217 k = 0;
2218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002219 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2220
2221 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002222 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002223 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 } else {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002225 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2226
2227 *frag = skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (eat) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002229 skb_frag_off_add(frag, eat);
2230 skb_frag_size_sub(frag, eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002231 if (!i)
2232 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 eat = 0;
2234 }
2235 k++;
2236 }
2237 }
2238 skb_shinfo(skb)->nr_frags = k;
2239
linzhang3ccc6c62017-07-17 17:25:02 +08002240end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 skb->tail += delta;
2242 skb->data_len -= delta;
2243
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002244 if (!skb->data_len)
2245 skb_zcopy_clear(skb, false);
2246
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002247 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002249EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Eric Dumazet22019b12011-07-29 18:37:31 +00002251/**
2252 * skb_copy_bits - copy bits from skb to kernel buffer
2253 * @skb: source skb
2254 * @offset: offset in source
2255 * @to: destination buffer
2256 * @len: number of bytes to copy
2257 *
2258 * Copy the specified number of bytes from the source skb to the
2259 * destination buffer.
2260 *
2261 * CAUTION ! :
2262 * If its prototype is ever changed,
2263 * check arch/{*}/net/{*}.S files,
2264 * since it is called from BPF assembly code.
2265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2267{
David S. Miller1a028e52007-04-27 15:21:23 -07002268 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002269 struct sk_buff *frag_iter;
2270 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 if (offset > (int)skb->len - len)
2273 goto fault;
2274
2275 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002276 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (copy > len)
2278 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002279 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if ((len -= copy) == 0)
2281 return 0;
2282 offset += copy;
2283 to += copy;
2284 }
2285
2286 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002287 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002288 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002290 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002291
Eric Dumazet51c56b02012-04-05 11:35:15 +02002292 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002294 u32 p_off, p_len, copied;
2295 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 u8 *vaddr;
2297
2298 if (copy > len)
2299 copy = len;
2300
Willem de Bruijnc613c202017-07-31 08:15:47 -04002301 skb_frag_foreach_page(f,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002302 skb_frag_off(f) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002303 copy, p, p_off, p_len, copied) {
2304 vaddr = kmap_atomic(p);
2305 memcpy(to + copied, vaddr + p_off, p_len);
2306 kunmap_atomic(vaddr);
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if ((len -= copy) == 0)
2310 return 0;
2311 offset += copy;
2312 to += copy;
2313 }
David S. Miller1a028e52007-04-27 15:21:23 -07002314 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316
David S. Millerfbb398a2009-06-09 00:18:59 -07002317 skb_walk_frags(skb, frag_iter) {
2318 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
David S. Millerfbb398a2009-06-09 00:18:59 -07002320 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
David S. Millerfbb398a2009-06-09 00:18:59 -07002322 end = start + frag_iter->len;
2323 if ((copy = end - offset) > 0) {
2324 if (copy > len)
2325 copy = len;
2326 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2327 goto fault;
2328 if ((len -= copy) == 0)
2329 return 0;
2330 offset += copy;
2331 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002333 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Shirley Maa6686f22011-07-06 12:22:12 +00002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (!len)
2337 return 0;
2338
2339fault:
2340 return -EFAULT;
2341}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002342EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Jens Axboe9c55e012007-11-06 23:30:13 -08002344/*
2345 * Callback from splice_to_pipe(), if we need to release some pages
2346 * at the end of the spd in case we error'ed out in filling the pipe.
2347 */
2348static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2349{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002350 put_page(spd->pages[i]);
2351}
Jens Axboe9c55e012007-11-06 23:30:13 -08002352
David S. Millera108d5f2012-04-23 23:06:11 -04002353static struct page *linear_to_page(struct page *page, unsigned int *len,
2354 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002355 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002356{
Eric Dumazet5640f762012-09-23 23:04:42 +00002357 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002358
Eric Dumazet5640f762012-09-23 23:04:42 +00002359 if (!sk_page_frag_refill(sk, pfrag))
2360 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002361
Eric Dumazet5640f762012-09-23 23:04:42 +00002362 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002363
Eric Dumazet5640f762012-09-23 23:04:42 +00002364 memcpy(page_address(pfrag->page) + pfrag->offset,
2365 page_address(page) + *offset, *len);
2366 *offset = pfrag->offset;
2367 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002368
Eric Dumazet5640f762012-09-23 23:04:42 +00002369 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002370}
2371
Eric Dumazet41c73a02012-04-22 12:26:16 +00002372static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2373 struct page *page,
2374 unsigned int offset)
2375{
2376 return spd->nr_pages &&
2377 spd->pages[spd->nr_pages - 1] == page &&
2378 (spd->partial[spd->nr_pages - 1].offset +
2379 spd->partial[spd->nr_pages - 1].len == offset);
2380}
2381
Jens Axboe9c55e012007-11-06 23:30:13 -08002382/*
2383 * Fill page/offset/length into spd, if it can hold more pages.
2384 */
David S. Millera108d5f2012-04-23 23:06:11 -04002385static bool spd_fill_page(struct splice_pipe_desc *spd,
2386 struct pipe_inode_info *pipe, struct page *page,
2387 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002388 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002389 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002390{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002391 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002392 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002393
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002394 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002395 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002396 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002397 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002398 }
2399 if (spd_can_coalesce(spd, page, offset)) {
2400 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002401 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002402 }
2403 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002404 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002405 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002406 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002407 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002408
David S. Millera108d5f2012-04-23 23:06:11 -04002409 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002410}
2411
David S. Millera108d5f2012-04-23 23:06:11 -04002412static bool __splice_segment(struct page *page, unsigned int poff,
2413 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002414 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002415 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002416 struct sock *sk,
2417 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002418{
2419 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002420 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002421
2422 /* skip this segment if already processed */
2423 if (*off >= plen) {
2424 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002425 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002426 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002427
Octavian Purdila2870c432008-07-15 00:49:11 -07002428 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002429 poff += *off;
2430 plen -= *off;
2431 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002432
Eric Dumazet18aafc62013-01-11 14:46:37 +00002433 do {
2434 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002435
Eric Dumazet18aafc62013-01-11 14:46:37 +00002436 if (spd_fill_page(spd, pipe, page, &flen, poff,
2437 linear, sk))
2438 return true;
2439 poff += flen;
2440 plen -= flen;
2441 *len -= flen;
2442 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002443
David S. Millera108d5f2012-04-23 23:06:11 -04002444 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002445}
2446
2447/*
David S. Millera108d5f2012-04-23 23:06:11 -04002448 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002449 * pipe is full or if we already spliced the requested length.
2450 */
David S. Millera108d5f2012-04-23 23:06:11 -04002451static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2452 unsigned int *offset, unsigned int *len,
2453 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002454{
2455 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002456 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002457
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002458 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002459 * If skb->head_frag is set, this 'linear' part is backed by a
2460 * fragment, and if the head is not shared with any clones then
2461 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002462 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002463 if (__splice_segment(virt_to_page(skb->data),
2464 (unsigned long) skb->data & (PAGE_SIZE - 1),
2465 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002466 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002467 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002468 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002469 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002470
2471 /*
2472 * then map the fragments
2473 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002474 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2475 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2476
Ian Campbellea2ab692011-08-22 23:44:58 +00002477 if (__splice_segment(skb_frag_page(f),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002478 skb_frag_off(f), skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002479 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002480 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002481 }
2482
Tom Herbertfa9835e2016-03-07 14:11:04 -08002483 skb_walk_frags(skb, iter) {
2484 if (*offset >= iter->len) {
2485 *offset -= iter->len;
2486 continue;
2487 }
2488 /* __skb_splice_bits() only fails if the output has no room
2489 * left, so no point in going over the frag_list for the error
2490 * case.
2491 */
2492 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2493 return true;
2494 }
2495
David S. Millera108d5f2012-04-23 23:06:11 -04002496 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002497}
2498
2499/*
2500 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002501 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002502 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002503int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002504 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002505 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002506{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002507 struct partial_page partial[MAX_SKB_FRAGS];
2508 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002509 struct splice_pipe_desc spd = {
2510 .pages = pages,
2511 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002512 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002513 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002514 .spd_release = sock_spd_release,
2515 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002516 int ret = 0;
2517
Tom Herbertfa9835e2016-03-07 14:11:04 -08002518 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002519
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002520 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002521 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002522
Jens Axboe35f3d142010-05-20 10:43:18 +02002523 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002524}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002525EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002526
Cong Wang0739cd22021-03-30 19:32:24 -07002527static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2528 struct kvec *vec, size_t num, size_t size)
2529{
2530 struct socket *sock = sk->sk_socket;
2531
2532 if (!sock)
2533 return -EINVAL;
2534 return kernel_sendmsg(sock, msg, vec, num, size);
2535}
2536
2537static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2538 size_t size, int flags)
2539{
2540 struct socket *sock = sk->sk_socket;
2541
2542 if (!sock)
2543 return -EINVAL;
2544 return kernel_sendpage(sock, page, offset, size, flags);
2545}
2546
2547typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2548 struct kvec *vec, size_t num, size_t size);
2549typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2550 size_t size, int flags);
2551static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2552 int len, sendmsg_func sendmsg, sendpage_func sendpage)
Tom Herbert20bf50d2017-07-28 16:22:42 -07002553{
2554 unsigned int orig_len = len;
2555 struct sk_buff *head = skb;
2556 unsigned short fragidx;
2557 int slen, ret;
2558
2559do_frag_list:
2560
2561 /* Deal with head data */
2562 while (offset < skb_headlen(skb) && len) {
2563 struct kvec kv;
2564 struct msghdr msg;
2565
2566 slen = min_t(int, len, skb_headlen(skb) - offset);
2567 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002568 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002569 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002570 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002571
Cong Wang0739cd22021-03-30 19:32:24 -07002572 ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2573 sendmsg_unlocked, sk, &msg, &kv, 1, slen);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002574 if (ret <= 0)
2575 goto error;
2576
2577 offset += ret;
2578 len -= ret;
2579 }
2580
2581 /* All the data was skb head? */
2582 if (!len)
2583 goto out;
2584
2585 /* Make offset relative to start of frags */
2586 offset -= skb_headlen(skb);
2587
2588 /* Find where we are in frag list */
2589 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2590 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2591
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002592 if (offset < skb_frag_size(frag))
Tom Herbert20bf50d2017-07-28 16:22:42 -07002593 break;
2594
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002595 offset -= skb_frag_size(frag);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002596 }
2597
2598 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2599 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2600
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002601 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002602
2603 while (slen) {
Cong Wang0739cd22021-03-30 19:32:24 -07002604 ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2605 sendpage_unlocked, sk,
2606 skb_frag_page(frag),
2607 skb_frag_off(frag) + offset,
2608 slen, MSG_DONTWAIT);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002609 if (ret <= 0)
2610 goto error;
2611
2612 len -= ret;
2613 offset += ret;
2614 slen -= ret;
2615 }
2616
2617 offset = 0;
2618 }
2619
2620 if (len) {
2621 /* Process any frag lists */
2622
2623 if (skb == head) {
2624 if (skb_has_frag_list(skb)) {
2625 skb = skb_shinfo(skb)->frag_list;
2626 goto do_frag_list;
2627 }
2628 } else if (skb->next) {
2629 skb = skb->next;
2630 goto do_frag_list;
2631 }
2632 }
2633
2634out:
2635 return orig_len - len;
2636
2637error:
2638 return orig_len == len ? ret : orig_len - len;
2639}
Cong Wang0739cd22021-03-30 19:32:24 -07002640
2641/* Send skb data on a socket. Socket must be locked. */
2642int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2643 int len)
2644{
2645 return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2646 kernel_sendpage_locked);
2647}
Tom Herbert20bf50d2017-07-28 16:22:42 -07002648EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2649
Cong Wang0739cd22021-03-30 19:32:24 -07002650/* Send skb data on a socket. Socket must be unlocked. */
2651int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2652{
2653 return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
2654 sendpage_unlocked);
2655}
2656
Herbert Xu357b40a2005-04-19 22:30:14 -07002657/**
2658 * skb_store_bits - store bits from kernel buffer to skb
2659 * @skb: destination buffer
2660 * @offset: offset in destination
2661 * @from: source buffer
2662 * @len: number of bytes to copy
2663 *
2664 * Copy the specified number of bytes from the source buffer to the
2665 * destination skb. This function handles all the messy bits of
2666 * traversing fragment lists and such.
2667 */
2668
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002669int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002670{
David S. Miller1a028e52007-04-27 15:21:23 -07002671 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002672 struct sk_buff *frag_iter;
2673 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002674
2675 if (offset > (int)skb->len - len)
2676 goto fault;
2677
David S. Miller1a028e52007-04-27 15:21:23 -07002678 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002679 if (copy > len)
2680 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002681 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002682 if ((len -= copy) == 0)
2683 return 0;
2684 offset += copy;
2685 from += copy;
2686 }
2687
2688 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2689 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002690 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002691
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002692 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002693
Eric Dumazet9e903e02011-10-18 21:00:24 +00002694 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002695 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002696 u32 p_off, p_len, copied;
2697 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002698 u8 *vaddr;
2699
2700 if (copy > len)
2701 copy = len;
2702
Willem de Bruijnc613c202017-07-31 08:15:47 -04002703 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002704 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002705 copy, p, p_off, p_len, copied) {
2706 vaddr = kmap_atomic(p);
2707 memcpy(vaddr + p_off, from + copied, p_len);
2708 kunmap_atomic(vaddr);
2709 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002710
2711 if ((len -= copy) == 0)
2712 return 0;
2713 offset += copy;
2714 from += copy;
2715 }
David S. Miller1a028e52007-04-27 15:21:23 -07002716 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002717 }
2718
David S. Millerfbb398a2009-06-09 00:18:59 -07002719 skb_walk_frags(skb, frag_iter) {
2720 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002721
David S. Millerfbb398a2009-06-09 00:18:59 -07002722 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002723
David S. Millerfbb398a2009-06-09 00:18:59 -07002724 end = start + frag_iter->len;
2725 if ((copy = end - offset) > 0) {
2726 if (copy > len)
2727 copy = len;
2728 if (skb_store_bits(frag_iter, offset - start,
2729 from, copy))
2730 goto fault;
2731 if ((len -= copy) == 0)
2732 return 0;
2733 offset += copy;
2734 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002735 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002736 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002737 }
2738 if (!len)
2739 return 0;
2740
2741fault:
2742 return -EFAULT;
2743}
Herbert Xu357b40a2005-04-19 22:30:14 -07002744EXPORT_SYMBOL(skb_store_bits);
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002747__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2748 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
David S. Miller1a028e52007-04-27 15:21:23 -07002750 int start = skb_headlen(skb);
2751 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002752 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 int pos = 0;
2754
2755 /* Checksum header. */
2756 if (copy > 0) {
2757 if (copy > len)
2758 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002759 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2760 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if ((len -= copy) == 0)
2762 return csum;
2763 offset += copy;
2764 pos = copy;
2765 }
2766
2767 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002768 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002769 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002771 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002772
Eric Dumazet51c56b02012-04-05 11:35:15 +02002773 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002775 u32 p_off, p_len, copied;
2776 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002777 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 if (copy > len)
2781 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002782
2783 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002784 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002785 copy, p, p_off, p_len, copied) {
2786 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002787 csum2 = INDIRECT_CALL_1(ops->update,
2788 csum_partial_ext,
2789 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002790 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002791 csum = INDIRECT_CALL_1(ops->combine,
2792 csum_block_add_ext, csum,
2793 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002794 pos += p_len;
2795 }
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (!(len -= copy))
2798 return csum;
2799 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
David S. Miller1a028e52007-04-27 15:21:23 -07002801 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803
David S. Millerfbb398a2009-06-09 00:18:59 -07002804 skb_walk_frags(skb, frag_iter) {
2805 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
David S. Millerfbb398a2009-06-09 00:18:59 -07002807 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
David S. Millerfbb398a2009-06-09 00:18:59 -07002809 end = start + frag_iter->len;
2810 if ((copy = end - offset) > 0) {
2811 __wsum csum2;
2812 if (copy > len)
2813 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002814 csum2 = __skb_checksum(frag_iter, offset - start,
2815 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002816 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2817 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002818 if ((len -= copy) == 0)
2819 return csum;
2820 offset += copy;
2821 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002823 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002825 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 return csum;
2828}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002829EXPORT_SYMBOL(__skb_checksum);
2830
2831__wsum skb_checksum(const struct sk_buff *skb, int offset,
2832 int len, __wsum csum)
2833{
2834 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002835 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002836 .combine = csum_block_add_ext,
2837 };
2838
2839 return __skb_checksum(skb, offset, len, csum, &ops);
2840}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002841EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843/* Both of above in one bottle. */
2844
Al Viro81d77662006-11-14 21:37:33 -08002845__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
Al Viro8d5930d2020-07-10 20:07:10 -04002846 u8 *to, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
David S. Miller1a028e52007-04-27 15:21:23 -07002848 int start = skb_headlen(skb);
2849 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002850 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 int pos = 0;
Al Viro8d5930d2020-07-10 20:07:10 -04002852 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /* Copy header. */
2855 if (copy > 0) {
2856 if (copy > len)
2857 copy = len;
2858 csum = csum_partial_copy_nocheck(skb->data + offset, to,
Al Virocc44c172020-07-11 00:12:07 -04002859 copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if ((len -= copy) == 0)
2861 return csum;
2862 offset += copy;
2863 to += copy;
2864 pos = copy;
2865 }
2866
2867 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002868 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002870 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002871
Eric Dumazet9e903e02011-10-18 21:00:24 +00002872 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002874 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2875 u32 p_off, p_len, copied;
2876 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002877 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 if (copy > len)
2881 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002882
2883 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002884 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002885 copy, p, p_off, p_len, copied) {
2886 vaddr = kmap_atomic(p);
2887 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2888 to + copied,
Al Virocc44c172020-07-11 00:12:07 -04002889 p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002890 kunmap_atomic(vaddr);
2891 csum = csum_block_add(csum, csum2, pos);
2892 pos += p_len;
2893 }
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (!(len -= copy))
2896 return csum;
2897 offset += copy;
2898 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
David S. Miller1a028e52007-04-27 15:21:23 -07002900 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902
David S. Millerfbb398a2009-06-09 00:18:59 -07002903 skb_walk_frags(skb, frag_iter) {
2904 __wsum csum2;
2905 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
David S. Millerfbb398a2009-06-09 00:18:59 -07002907 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
David S. Millerfbb398a2009-06-09 00:18:59 -07002909 end = start + frag_iter->len;
2910 if ((copy = end - offset) > 0) {
2911 if (copy > len)
2912 copy = len;
2913 csum2 = skb_copy_and_csum_bits(frag_iter,
2914 offset - start,
Al Viro8d5930d2020-07-10 20:07:10 -04002915 to, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002916 csum = csum_block_add(csum, csum2, pos);
2917 if ((len -= copy) == 0)
2918 return csum;
2919 offset += copy;
2920 to += copy;
2921 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002923 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002925 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return csum;
2927}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002928EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Cong Wang49f8e832018-11-08 14:05:42 -08002930__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2931{
2932 __sum16 sum;
2933
2934 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002935 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002936 if (likely(!sum)) {
2937 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2938 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002939 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002940 }
2941 if (!skb_shared(skb))
2942 skb->csum_valid = !sum;
2943 return sum;
2944}
2945EXPORT_SYMBOL(__skb_checksum_complete_head);
2946
Cong Wang14641932018-11-26 09:31:26 -08002947/* This function assumes skb->csum already holds pseudo header's checksum,
2948 * which has been changed from the hardware checksum, for example, by
2949 * __skb_checksum_validate_complete(). And, the original skb->csum must
2950 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2951 *
2952 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2953 * zero. The new checksum is stored back into skb->csum unless the skb is
2954 * shared.
2955 */
Cong Wang49f8e832018-11-08 14:05:42 -08002956__sum16 __skb_checksum_complete(struct sk_buff *skb)
2957{
2958 __wsum csum;
2959 __sum16 sum;
2960
2961 csum = skb_checksum(skb, 0, skb->len, 0);
2962
Cong Wang49f8e832018-11-08 14:05:42 -08002963 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08002964 /* This check is inverted, because we already knew the hardware
2965 * checksum is invalid before calling this function. So, if the
2966 * re-computed checksum is valid instead, then we have a mismatch
2967 * between the original skb->csum and skb_checksum(). This means either
2968 * the original hardware checksum is incorrect or we screw up skb->csum
2969 * when moving skb->data around.
2970 */
Cong Wang49f8e832018-11-08 14:05:42 -08002971 if (likely(!sum)) {
2972 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2973 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002974 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002975 }
2976
2977 if (!skb_shared(skb)) {
2978 /* Save full packet checksum */
2979 skb->csum = csum;
2980 skb->ip_summed = CHECKSUM_COMPLETE;
2981 skb->csum_complete_sw = 1;
2982 skb->csum_valid = !sum;
2983 }
2984
2985 return sum;
2986}
2987EXPORT_SYMBOL(__skb_checksum_complete);
2988
Davide Caratti96178132017-05-18 15:44:37 +02002989static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2990{
2991 net_warn_ratelimited(
2992 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2993 __func__);
2994 return 0;
2995}
2996
2997static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2998 int offset, int len)
2999{
3000 net_warn_ratelimited(
3001 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3002 __func__);
3003 return 0;
3004}
3005
3006static const struct skb_checksum_ops default_crc32c_ops = {
3007 .update = warn_crc32c_csum_update,
3008 .combine = warn_crc32c_csum_combine,
3009};
3010
3011const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3012 &default_crc32c_ops;
3013EXPORT_SYMBOL(crc32c_csum_stub);
3014
Thomas Grafaf2806f2013-12-13 15:22:17 +01003015 /**
3016 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3017 * @from: source buffer
3018 *
3019 * Calculates the amount of linear headroom needed in the 'to' skb passed
3020 * into skb_zerocopy().
3021 */
3022unsigned int
3023skb_zerocopy_headlen(const struct sk_buff *from)
3024{
3025 unsigned int hlen = 0;
3026
3027 if (!from->head_frag ||
3028 skb_headlen(from) < L1_CACHE_BYTES ||
Pravin B Shelara17ad092021-07-15 16:59:00 -07003029 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
Thomas Grafaf2806f2013-12-13 15:22:17 +01003030 hlen = skb_headlen(from);
Pravin B Shelara17ad092021-07-15 16:59:00 -07003031 if (!hlen)
3032 hlen = from->len;
3033 }
Thomas Grafaf2806f2013-12-13 15:22:17 +01003034
3035 if (skb_has_frag_list(from))
3036 hlen = from->len;
3037
3038 return hlen;
3039}
3040EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3041
3042/**
3043 * skb_zerocopy - Zero copy skb to skb
3044 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09003045 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01003046 * @len: number of bytes to copy from source buffer
3047 * @hlen: size of linear headroom in destination buffer
3048 *
3049 * Copies up to `len` bytes from `from` to `to` by creating references
3050 * to the frags in the source buffer.
3051 *
3052 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3053 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003054 *
3055 * Return value:
3056 * 0: everything is OK
3057 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
3058 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01003059 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003060int
3061skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01003062{
3063 int i, j = 0;
3064 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003065 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003066 struct page *page;
3067 unsigned int offset;
3068
3069 BUG_ON(!from->head_frag && !hlen);
3070
3071 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003072 if (len <= skb_tailroom(to))
3073 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01003074
3075 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003076 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3077 if (unlikely(ret))
3078 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003079 len -= hlen;
3080 } else {
3081 plen = min_t(int, skb_headlen(from), len);
3082 if (plen) {
3083 page = virt_to_head_page(from->head);
3084 offset = from->data - (unsigned char *)page_address(page);
3085 __skb_fill_page_desc(to, 0, page, offset, plen);
3086 get_page(page);
3087 j = 1;
3088 len -= plen;
3089 }
3090 }
3091
3092 to->truesize += len + plen;
3093 to->len += len + plen;
3094 to->data_len += len + plen;
3095
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003096 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3097 skb_tx_error(from);
3098 return -ENOMEM;
3099 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003100 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003101
Thomas Grafaf2806f2013-12-13 15:22:17 +01003102 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003103 int size;
3104
Thomas Grafaf2806f2013-12-13 15:22:17 +01003105 if (!len)
3106 break;
3107 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003108 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3109 len);
3110 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3111 len -= size;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003112 skb_frag_ref(to, j);
3113 j++;
3114 }
3115 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003116
3117 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003118}
3119EXPORT_SYMBOL_GPL(skb_zerocopy);
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3122{
Al Virod3bc23e2006-11-14 21:24:49 -08003123 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 long csstart;
3125
Patrick McHardy84fa7932006-08-29 16:44:56 -07003126 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00003127 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 else
3129 csstart = skb_headlen(skb);
3130
Kris Katterjohn09a62662006-01-08 22:24:28 -08003131 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003133 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 csum = 0;
3136 if (csstart != skb->len)
3137 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
Al Viro8d5930d2020-07-10 20:07:10 -04003138 skb->len - csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Patrick McHardy84fa7932006-08-29 16:44:56 -07003140 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003141 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Al Virod3bc23e2006-11-14 21:24:49 -08003143 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003146EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148/**
3149 * skb_dequeue - remove from the head of the queue
3150 * @list: list to dequeue from
3151 *
3152 * Remove the head of the list. The list lock is taken so the function
3153 * may be used safely with other locking list functions. The head item is
3154 * returned or %NULL if the list is empty.
3155 */
3156
3157struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3158{
3159 unsigned long flags;
3160 struct sk_buff *result;
3161
3162 spin_lock_irqsave(&list->lock, flags);
3163 result = __skb_dequeue(list);
3164 spin_unlock_irqrestore(&list->lock, flags);
3165 return result;
3166}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003167EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169/**
3170 * skb_dequeue_tail - remove from the tail of the queue
3171 * @list: list to dequeue from
3172 *
3173 * Remove the tail of the list. The list lock is taken so the function
3174 * may be used safely with other locking list functions. The tail item is
3175 * returned or %NULL if the list is empty.
3176 */
3177struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3178{
3179 unsigned long flags;
3180 struct sk_buff *result;
3181
3182 spin_lock_irqsave(&list->lock, flags);
3183 result = __skb_dequeue_tail(list);
3184 spin_unlock_irqrestore(&list->lock, flags);
3185 return result;
3186}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003187EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189/**
3190 * skb_queue_purge - empty a list
3191 * @list: list to empty
3192 *
3193 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3194 * the list and one reference dropped. This function takes the list
3195 * lock and is atomic with respect to other list locking functions.
3196 */
3197void skb_queue_purge(struct sk_buff_head *list)
3198{
3199 struct sk_buff *skb;
3200 while ((skb = skb_dequeue(list)) != NULL)
3201 kfree_skb(skb);
3202}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003203EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003206 * skb_rbtree_purge - empty a skb rbtree
3207 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003208 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003209 *
3210 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3211 * the list and one reference dropped. This function does not take
3212 * any lock. Synchronization should be handled by the caller (e.g., TCP
3213 * out-of-order queue is protected by the socket lock).
3214 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003215unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003216{
Eric Dumazet7c905842017-09-23 12:39:12 -07003217 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003218 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003219
Eric Dumazet7c905842017-09-23 12:39:12 -07003220 while (p) {
3221 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3222
3223 p = rb_next(p);
3224 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003225 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003226 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003227 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003228 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003229}
3230
3231/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 * skb_queue_head - queue a buffer at the list head
3233 * @list: list to use
3234 * @newsk: buffer to queue
3235 *
3236 * Queue a buffer at the start of the list. This function takes the
3237 * list lock and can be used safely with other locking &sk_buff functions
3238 * safely.
3239 *
3240 * A buffer cannot be placed on two lists at the same time.
3241 */
3242void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3243{
3244 unsigned long flags;
3245
3246 spin_lock_irqsave(&list->lock, flags);
3247 __skb_queue_head(list, newsk);
3248 spin_unlock_irqrestore(&list->lock, flags);
3249}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003250EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252/**
3253 * skb_queue_tail - queue a buffer at the list tail
3254 * @list: list to use
3255 * @newsk: buffer to queue
3256 *
3257 * Queue a buffer at the tail of the list. This function takes the
3258 * list lock and can be used safely with other locking &sk_buff functions
3259 * safely.
3260 *
3261 * A buffer cannot be placed on two lists at the same time.
3262 */
3263void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3264{
3265 unsigned long flags;
3266
3267 spin_lock_irqsave(&list->lock, flags);
3268 __skb_queue_tail(list, newsk);
3269 spin_unlock_irqrestore(&list->lock, flags);
3270}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003271EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273/**
3274 * skb_unlink - remove a buffer from a list
3275 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003276 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 *
David S. Miller8728b832005-08-09 19:25:21 -07003278 * Remove a packet from a list. The list locks are taken and this
3279 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 *
David S. Miller8728b832005-08-09 19:25:21 -07003281 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 */
David S. Miller8728b832005-08-09 19:25:21 -07003283void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
David S. Miller8728b832005-08-09 19:25:21 -07003285 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
David S. Miller8728b832005-08-09 19:25:21 -07003287 spin_lock_irqsave(&list->lock, flags);
3288 __skb_unlink(skb, list);
3289 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003291EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293/**
3294 * skb_append - append a buffer
3295 * @old: buffer to insert after
3296 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003297 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 *
3299 * Place a packet after a given packet in a list. The list locks are taken
3300 * and this function is atomic with respect to other list locked calls.
3301 * A buffer cannot be placed on two lists at the same time.
3302 */
David S. Miller8728b832005-08-09 19:25:21 -07003303void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
3305 unsigned long flags;
3306
David S. Miller8728b832005-08-09 19:25:21 -07003307 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003308 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003309 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003311EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313static inline void skb_split_inside_header(struct sk_buff *skb,
3314 struct sk_buff* skb1,
3315 const u32 len, const int pos)
3316{
3317 int i;
3318
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003319 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3320 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 /* And move data appendix as is. */
3322 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3323 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3324
3325 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3326 skb_shinfo(skb)->nr_frags = 0;
3327 skb1->data_len = skb->data_len;
3328 skb1->len += skb1->data_len;
3329 skb->data_len = 0;
3330 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003331 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334static inline void skb_split_no_header(struct sk_buff *skb,
3335 struct sk_buff* skb1,
3336 const u32 len, int pos)
3337{
3338 int i, k = 0;
3339 const int nfrags = skb_shinfo(skb)->nr_frags;
3340
3341 skb_shinfo(skb)->nr_frags = 0;
3342 skb1->len = skb1->data_len = skb->len - len;
3343 skb->len = len;
3344 skb->data_len = len - pos;
3345
3346 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003347 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 if (pos + size > len) {
3350 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3351
3352 if (pos < len) {
3353 /* Split frag.
3354 * We have two variants in this case:
3355 * 1. Move all the frag to the second
3356 * part, if it is possible. F.e.
3357 * this approach is mandatory for TUX,
3358 * where splitting is expensive.
3359 * 2. Split is accurately. We make this.
3360 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003361 skb_frag_ref(skb, i);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003362 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003363 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3364 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 skb_shinfo(skb)->nr_frags++;
3366 }
3367 k++;
3368 } else
3369 skb_shinfo(skb)->nr_frags++;
3370 pos += size;
3371 }
3372 skb_shinfo(skb1)->nr_frags = k;
3373}
3374
3375/**
3376 * skb_split - Split fragmented skb to two parts at length len.
3377 * @skb: the buffer to split
3378 * @skb1: the buffer to receive the second part
3379 * @len: new length for skb
3380 */
3381void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3382{
3383 int pos = skb_headlen(skb);
3384
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08003385 skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003386 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (len < pos) /* Split line is inside header. */
3388 skb_split_inside_header(skb, skb1, len, pos);
3389 else /* Second chunk has no header, nothing to copy. */
3390 skb_split_no_header(skb, skb1, len, pos);
3391}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003392EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003394/* Shifting from/to a cloned skb is a no-go.
3395 *
3396 * Caller cannot keep skb_shinfo related pointers past calling here!
3397 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003398static int skb_prepare_for_shift(struct sk_buff *skb)
3399{
Marco Elver097b9142021-02-01 17:04:20 +01003400 int ret = 0;
3401
3402 if (skb_cloned(skb)) {
3403 /* Save and restore truesize: pskb_expand_head() may reallocate
3404 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3405 * cannot change truesize at this point.
3406 */
3407 unsigned int save_truesize = skb->truesize;
3408
3409 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3410 skb->truesize = save_truesize;
3411 }
3412 return ret;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003413}
3414
3415/**
3416 * skb_shift - Shifts paged data partially from skb to another
3417 * @tgt: buffer into which tail data gets added
3418 * @skb: buffer from which the paged data comes from
3419 * @shiftlen: shift up to this many bytes
3420 *
3421 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003422 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003423 * It's up to caller to free skb if everything was shifted.
3424 *
3425 * If @tgt runs out of frags, the whole operation is aborted.
3426 *
3427 * Skb cannot include anything else but paged data while tgt is allowed
3428 * to have non-paged data as well.
3429 *
3430 * TODO: full sized shift could be optimized but that would need
3431 * specialized skb free'er to handle frags without up-to-date nr_frags.
3432 */
3433int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3434{
3435 int from, to, merge, todo;
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003436 skb_frag_t *fragfrom, *fragto;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003437
3438 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003439
3440 if (skb_headlen(skb))
3441 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003442 if (skb_zcopy(tgt) || skb_zcopy(skb))
3443 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003444
3445 todo = shiftlen;
3446 from = 0;
3447 to = skb_shinfo(tgt)->nr_frags;
3448 fragfrom = &skb_shinfo(skb)->frags[from];
3449
3450 /* Actual merge is delayed until the point when we know we can
3451 * commit all, so that we don't have to undo partial changes
3452 */
3453 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003454 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003455 skb_frag_off(fragfrom))) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003456 merge = -1;
3457 } else {
3458 merge = to - 1;
3459
Eric Dumazet9e903e02011-10-18 21:00:24 +00003460 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003461 if (todo < 0) {
3462 if (skb_prepare_for_shift(skb) ||
3463 skb_prepare_for_shift(tgt))
3464 return 0;
3465
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003466 /* All previous frag pointers might be stale! */
3467 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003468 fragto = &skb_shinfo(tgt)->frags[merge];
3469
Eric Dumazet9e903e02011-10-18 21:00:24 +00003470 skb_frag_size_add(fragto, shiftlen);
3471 skb_frag_size_sub(fragfrom, shiftlen);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003472 skb_frag_off_add(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003473
3474 goto onlymerged;
3475 }
3476
3477 from++;
3478 }
3479
3480 /* Skip full, not-fitting skb to avoid expensive operations */
3481 if ((shiftlen == skb->len) &&
3482 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3483 return 0;
3484
3485 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3486 return 0;
3487
3488 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3489 if (to == MAX_SKB_FRAGS)
3490 return 0;
3491
3492 fragfrom = &skb_shinfo(skb)->frags[from];
3493 fragto = &skb_shinfo(tgt)->frags[to];
3494
Eric Dumazet9e903e02011-10-18 21:00:24 +00003495 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003496 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003497 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003498 from++;
3499 to++;
3500
3501 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003502 __skb_frag_ref(fragfrom);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003503 skb_frag_page_copy(fragto, fragfrom);
3504 skb_frag_off_copy(fragto, fragfrom);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003505 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003506
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003507 skb_frag_off_add(fragfrom, todo);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003508 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003509 todo = 0;
3510
3511 to++;
3512 break;
3513 }
3514 }
3515
3516 /* Ready to "commit" this state change to tgt */
3517 skb_shinfo(tgt)->nr_frags = to;
3518
3519 if (merge >= 0) {
3520 fragfrom = &skb_shinfo(skb)->frags[0];
3521 fragto = &skb_shinfo(tgt)->frags[merge];
3522
Eric Dumazet9e903e02011-10-18 21:00:24 +00003523 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +02003524 __skb_frag_unref(fragfrom, skb->pp_recycle);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003525 }
3526
3527 /* Reposition in the original skb */
3528 to = 0;
3529 while (from < skb_shinfo(skb)->nr_frags)
3530 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3531 skb_shinfo(skb)->nr_frags = to;
3532
3533 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3534
3535onlymerged:
3536 /* Most likely the tgt won't ever need its checksum anymore, skb on
3537 * the other hand might need it if it needs to be resent
3538 */
3539 tgt->ip_summed = CHECKSUM_PARTIAL;
3540 skb->ip_summed = CHECKSUM_PARTIAL;
3541
3542 /* Yak, is it really working this way? Some helper please? */
3543 skb->len -= shiftlen;
3544 skb->data_len -= shiftlen;
3545 skb->truesize -= shiftlen;
3546 tgt->len += shiftlen;
3547 tgt->data_len += shiftlen;
3548 tgt->truesize += shiftlen;
3549
3550 return shiftlen;
3551}
3552
Thomas Graf677e90e2005-06-23 20:59:51 -07003553/**
3554 * skb_prepare_seq_read - Prepare a sequential read of skb data
3555 * @skb: the buffer to read
3556 * @from: lower offset of data to be read
3557 * @to: upper offset of data to be read
3558 * @st: state variable
3559 *
3560 * Initializes the specified state variable. Must be called before
3561 * invoking skb_seq_read() for the first time.
3562 */
3563void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3564 unsigned int to, struct skb_seq_state *st)
3565{
3566 st->lower_offset = from;
3567 st->upper_offset = to;
3568 st->root_skb = st->cur_skb = skb;
3569 st->frag_idx = st->stepped_offset = 0;
3570 st->frag_data = NULL;
Willem de Bruijn97550f62021-01-09 17:18:33 -05003571 st->frag_off = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003572}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003573EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003574
3575/**
3576 * skb_seq_read - Sequentially read skb data
3577 * @consumed: number of bytes consumed by the caller so far
3578 * @data: destination pointer for data to be returned
3579 * @st: state variable
3580 *
Mathias Krausebc323832013-11-07 14:18:26 +01003581 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003582 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003583 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003584 * of the block or 0 if the end of the skb data or the upper
3585 * offset has been reached.
3586 *
3587 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003588 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003589 * of bytes already consumed and the next call to
3590 * skb_seq_read() will return the remaining part of the block.
3591 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003592 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003593 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003594 * reads of potentially non linear data.
3595 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003596 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003597 * at the moment, state->root_skb could be replaced with
3598 * a stack for this purpose.
3599 */
3600unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3601 struct skb_seq_state *st)
3602{
3603 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3604 skb_frag_t *frag;
3605
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003606 if (unlikely(abs_offset >= st->upper_offset)) {
3607 if (st->frag_data) {
3608 kunmap_atomic(st->frag_data);
3609 st->frag_data = NULL;
3610 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003611 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003612 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003613
3614next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003615 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003616
Thomas Chenault995b3372009-05-18 21:43:27 -07003617 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003618 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003619 return block_limit - abs_offset;
3620 }
3621
3622 if (st->frag_idx == 0 && !st->frag_data)
3623 st->stepped_offset += skb_headlen(st->cur_skb);
3624
3625 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
Willem de Bruijn97550f62021-01-09 17:18:33 -05003626 unsigned int pg_idx, pg_off, pg_sz;
Thomas Graf677e90e2005-06-23 20:59:51 -07003627
Willem de Bruijn97550f62021-01-09 17:18:33 -05003628 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3629
3630 pg_idx = 0;
3631 pg_off = skb_frag_off(frag);
3632 pg_sz = skb_frag_size(frag);
3633
3634 if (skb_frag_must_loop(skb_frag_page(frag))) {
3635 pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3636 pg_off = offset_in_page(pg_off + st->frag_off);
3637 pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3638 PAGE_SIZE - pg_off);
3639 }
3640
3641 block_limit = pg_sz + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003642 if (abs_offset < block_limit) {
3643 if (!st->frag_data)
Willem de Bruijn97550f62021-01-09 17:18:33 -05003644 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
Thomas Graf677e90e2005-06-23 20:59:51 -07003645
Willem de Bruijn97550f62021-01-09 17:18:33 -05003646 *data = (u8 *)st->frag_data + pg_off +
Thomas Graf677e90e2005-06-23 20:59:51 -07003647 (abs_offset - st->stepped_offset);
3648
3649 return block_limit - abs_offset;
3650 }
3651
3652 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003653 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003654 st->frag_data = NULL;
3655 }
3656
Willem de Bruijn97550f62021-01-09 17:18:33 -05003657 st->stepped_offset += pg_sz;
3658 st->frag_off += pg_sz;
3659 if (st->frag_off == skb_frag_size(frag)) {
3660 st->frag_off = 0;
3661 st->frag_idx++;
3662 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003663 }
3664
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003665 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003666 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003667 st->frag_data = NULL;
3668 }
3669
David S. Miller21dc3302010-08-23 00:13:46 -07003670 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003671 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003672 st->frag_idx = 0;
3673 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003674 } else if (st->cur_skb->next) {
3675 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003676 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003677 goto next_skb;
3678 }
3679
3680 return 0;
3681}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003682EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003683
3684/**
3685 * skb_abort_seq_read - Abort a sequential read of skb data
3686 * @st: state variable
3687 *
3688 * Must be called if skb_seq_read() was not called until it
3689 * returned 0.
3690 */
3691void skb_abort_seq_read(struct skb_seq_state *st)
3692{
3693 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003694 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003695}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003696EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003697
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003698#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3699
3700static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3701 struct ts_config *conf,
3702 struct ts_state *state)
3703{
3704 return skb_seq_read(offset, text, TS_SKB_CB(state));
3705}
3706
3707static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3708{
3709 skb_abort_seq_read(TS_SKB_CB(state));
3710}
3711
3712/**
3713 * skb_find_text - Find a text pattern in skb data
3714 * @skb: the buffer to look in
3715 * @from: search offset
3716 * @to: search limit
3717 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003718 *
3719 * Finds a pattern in the skb data according to the specified
3720 * textsearch configuration. Use textsearch_next() to retrieve
3721 * subsequent occurrences of the pattern. Returns the offset
3722 * to the first occurrence or UINT_MAX if no match was found.
3723 */
3724unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003725 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003726{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003727 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003728 unsigned int ret;
3729
Willem de Bruijnb228c9b2021-03-01 15:09:44 +00003730 BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
3731
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003732 config->get_next_block = skb_ts_get_next_block;
3733 config->finish = skb_ts_finish;
3734
Bojan Prtvar059a2442015-02-22 11:46:35 +01003735 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003736
Bojan Prtvar059a2442015-02-22 11:46:35 +01003737 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003738 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003739}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003740EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003741
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003742int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3743 int offset, size_t size)
3744{
3745 int i = skb_shinfo(skb)->nr_frags;
3746
3747 if (skb_can_coalesce(skb, i, page, offset)) {
3748 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3749 } else if (i < MAX_SKB_FRAGS) {
3750 get_page(page);
3751 skb_fill_page_desc(skb, i, page, offset, size);
3752 } else {
3753 return -EMSGSIZE;
3754 }
3755
3756 return 0;
3757}
3758EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3759
Herbert Xucbb042f2006-03-20 22:43:56 -08003760/**
3761 * skb_pull_rcsum - pull skb and update receive checksum
3762 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003763 * @len: length of data pulled
3764 *
3765 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003766 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003767 * receive path processing instead of skb_pull unless you know
3768 * that the checksum difference is zero (e.g., a valid IP header)
3769 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003770 */
Johannes Bergaf728682017-06-16 14:29:22 +02003771void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003772{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003773 unsigned char *data = skb->data;
3774
Herbert Xucbb042f2006-03-20 22:43:56 -08003775 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003776 __skb_pull(skb, len);
3777 skb_postpull_rcsum(skb, data, len);
3778 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003779}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003780EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3781
Yonghong Song13acc942018-03-21 16:31:03 -07003782static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3783{
3784 skb_frag_t head_frag;
3785 struct page *page;
3786
3787 page = virt_to_head_page(frag_skb->head);
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003788 __skb_frag_set_page(&head_frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003789 skb_frag_off_set(&head_frag, frag_skb->data -
3790 (unsigned char *)page_address(page));
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003791 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
Yonghong Song13acc942018-03-21 16:31:03 -07003792 return head_frag;
3793}
3794
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003795struct sk_buff *skb_segment_list(struct sk_buff *skb,
3796 netdev_features_t features,
3797 unsigned int offset)
3798{
3799 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3800 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3801 unsigned int delta_truesize = 0;
3802 unsigned int delta_len = 0;
3803 struct sk_buff *tail = NULL;
Dongseok Yi53475c52021-01-08 11:28:38 +09003804 struct sk_buff *nskb, *tmp;
3805 int err;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003806
3807 skb_push(skb, -skb_network_offset(skb) + offset);
3808
3809 skb_shinfo(skb)->frag_list = NULL;
3810
3811 do {
3812 nskb = list_skb;
3813 list_skb = list_skb->next;
3814
Dongseok Yi53475c52021-01-08 11:28:38 +09003815 err = 0;
3816 if (skb_shared(nskb)) {
3817 tmp = skb_clone(nskb, GFP_ATOMIC);
3818 if (tmp) {
3819 consume_skb(nskb);
3820 nskb = tmp;
3821 err = skb_unclone(nskb, GFP_ATOMIC);
3822 } else {
3823 err = -ENOMEM;
3824 }
3825 }
3826
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003827 if (!tail)
3828 skb->next = nskb;
3829 else
3830 tail->next = nskb;
3831
Dongseok Yi53475c52021-01-08 11:28:38 +09003832 if (unlikely(err)) {
3833 nskb->next = list_skb;
3834 goto err_linearize;
3835 }
3836
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003837 tail = nskb;
3838
3839 delta_len += nskb->len;
3840 delta_truesize += nskb->truesize;
3841
3842 skb_push(nskb, -skb_network_offset(nskb) + offset);
3843
Florian Westphalcf673ed2020-03-30 18:51:29 +02003844 skb_release_head_state(nskb);
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003845 __copy_skb_header(nskb, skb);
3846
3847 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3848 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3849 nskb->data - tnl_hlen,
3850 offset + tnl_hlen);
3851
3852 if (skb_needs_linearize(nskb, features) &&
3853 __skb_linearize(nskb))
3854 goto err_linearize;
3855
3856 } while (list_skb);
3857
3858 skb->truesize = skb->truesize - delta_truesize;
3859 skb->data_len = skb->data_len - delta_len;
3860 skb->len = skb->len - delta_len;
3861
3862 skb_gso_reset(skb);
3863
3864 skb->prev = tail;
3865
3866 if (skb_needs_linearize(skb, features) &&
3867 __skb_linearize(skb))
3868 goto err_linearize;
3869
3870 skb_get(skb);
3871
3872 return skb;
3873
3874err_linearize:
3875 kfree_skb_list(skb->next);
3876 skb->next = NULL;
3877 return ERR_PTR(-ENOMEM);
3878}
3879EXPORT_SYMBOL_GPL(skb_segment_list);
3880
3881int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3882{
3883 if (unlikely(p->len + skb->len >= 65536))
3884 return -E2BIG;
3885
3886 if (NAPI_GRO_CB(p)->last == p)
3887 skb_shinfo(p)->frag_list = skb;
3888 else
3889 NAPI_GRO_CB(p)->last->next = skb;
3890
3891 skb_pull(skb, skb_gro_offset(skb));
3892
3893 NAPI_GRO_CB(p)->last = skb;
3894 NAPI_GRO_CB(p)->count++;
3895 p->data_len += skb->len;
Paolo Abeni5e10da52021-07-28 18:24:03 +02003896
3897 /* sk owenrship - if any - completely transferred to the aggregated packet */
3898 skb->destructor = NULL;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003899 p->truesize += skb->truesize;
3900 p->len += skb->len;
3901
3902 NAPI_GRO_CB(skb)->same_flow = 1;
3903
3904 return 0;
3905}
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003906
Herbert Xuf4c50d92006-06-22 03:02:40 -07003907/**
3908 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003909 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003910 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003911 *
3912 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003913 * a pointer to the first in a list of new skbs for the segments.
3914 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003915 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003916struct sk_buff *skb_segment(struct sk_buff *head_skb,
3917 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003918{
3919 struct sk_buff *segs = NULL;
3920 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003921 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003922 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3923 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3924 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003925 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003926 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003927 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003928 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003929 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003930 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003931 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003932 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003933 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003934 int err = -ENOMEM;
3935 int i = 0;
3936 int pos;
3937
Shmulik Ladkani3dcbdb12019-09-06 12:23:50 +03003938 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3939 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3940 /* gso_size is untrusted, and we have a frag_list with a linear
3941 * non head_frag head.
3942 *
3943 * (we assume checking the first list_skb member suffices;
3944 * i.e if either of the list_skb members have non head_frag
3945 * head, then the first one has too).
3946 *
3947 * If head_skb's headlen does not fit requested gso_size, it
3948 * means that the frag_list members do NOT terminate on exact
3949 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3950 * sharing. Therefore we must fallback to copying the frag_list
3951 * skbs; we do so by disabling SG.
3952 */
3953 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3954 features &= ~NETIF_F_SG;
3955 }
3956
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003957 __skb_push(head_skb, doffset);
Miaohe Lin2f631132020-08-01 17:36:05 +08003958 proto = skb_network_protocol(head_skb, NULL);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003959 if (unlikely(!proto))
3960 return ERR_PTR(-EINVAL);
3961
Alexander Duyck36c98382016-05-02 09:38:18 -07003962 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08003963 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07003964
Steffen Klassert07b26c92016-09-19 12:58:47 +02003965 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3966 if (!(features & NETIF_F_GSO_PARTIAL)) {
3967 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03003968 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003969
3970 if (!list_skb ||
3971 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3972 goto normal;
3973
Ilan Tayari43170c42017-04-19 21:26:07 +03003974 /* If we get here then all the required
3975 * GSO features except frag_list are supported.
3976 * Try to split the SKB to multiple GSO SKBs
3977 * with no frag_list.
3978 * Currently we can do that only when the buffers don't
3979 * have a linear part and all the buffers except
3980 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02003981 */
Ilan Tayari43170c42017-04-19 21:26:07 +03003982 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003983 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03003984 if (frag_len != iter->len && iter->next)
3985 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03003986 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02003987 goto normal;
3988
3989 len -= iter->len;
3990 }
Ilan Tayari43170c42017-04-19 21:26:07 +03003991
3992 if (len != frag_len)
3993 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02003994 }
3995
3996 /* GSO partial only requires that we trim off any excess that
3997 * doesn't fit into an MSS sized block, so take care of that
3998 * now.
3999 */
Alexander Duyck802ab552016-04-10 21:45:03 -04004000 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07004001 if (partial_segs > 1)
4002 mss *= partial_segs;
4003 else
4004 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04004005 }
4006
Steffen Klassert07b26c92016-09-19 12:58:47 +02004007normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004008 headroom = skb_headroom(head_skb);
4009 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004010
4011 do {
4012 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004013 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08004014 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004015 int size;
4016
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03004017 if (unlikely(mss == GSO_BY_FRAGS)) {
4018 len = list_skb->len;
4019 } else {
4020 len = head_skb->len - offset;
4021 if (len > mss)
4022 len = mss;
4023 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004024
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004025 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004026
Xin Longdbd50f22021-01-15 17:36:38 +08004027 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004028 (skb_headlen(list_skb) == len || sg)) {
4029 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08004030
Herbert Xu9d8506c2013-11-21 11:10:04 -08004031 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004032 nfrags = skb_shinfo(list_skb)->nr_frags;
4033 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004034 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004035 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004036
4037 while (pos < offset + len) {
4038 BUG_ON(i >= nfrags);
4039
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004040 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004041 if (pos + size > offset + len)
4042 break;
4043
4044 i++;
4045 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004046 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004047 }
4048
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004049 nskb = skb_clone(list_skb, GFP_ATOMIC);
4050 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08004051
4052 if (unlikely(!nskb))
4053 goto err;
4054
Herbert Xu9d8506c2013-11-21 11:10:04 -08004055 if (unlikely(pskb_trim(nskb, len))) {
4056 kfree_skb(nskb);
4057 goto err;
4058 }
4059
Alexander Duyckec47ea82012-05-04 14:26:56 +00004060 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08004061 if (skb_cow_head(nskb, doffset + headroom)) {
4062 kfree_skb(nskb);
4063 goto err;
4064 }
4065
Alexander Duyckec47ea82012-05-04 14:26:56 +00004066 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08004067 skb_release_head_state(nskb);
4068 __skb_push(nskb, doffset);
4069 } else {
Paolo Abeni00b229f2021-01-19 17:56:56 +01004070 if (hsize < 0)
4071 hsize = 0;
Xin Longdbd50f22021-01-15 17:36:38 +08004072 if (hsize > len || !sg)
4073 hsize = len;
Xin Longdbd50f22021-01-15 17:36:38 +08004074
Mel Gormanc93bdd02012-07-31 16:44:19 -07004075 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004076 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07004077 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08004078
4079 if (unlikely(!nskb))
4080 goto err;
4081
4082 skb_reserve(nskb, headroom);
4083 __skb_put(nskb, doffset);
4084 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004085
4086 if (segs)
4087 tail->next = nskb;
4088 else
4089 segs = nskb;
4090 tail = nskb;
4091
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004092 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004093
Eric Dumazet030737b2013-10-19 11:42:54 -07004094 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04004095 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00004096
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004097 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00004098 nskb->data - tnl_hlen,
4099 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08004100
Herbert Xu9d8506c2013-11-21 11:10:04 -08004101 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00004102 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08004103
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004104 if (!sg) {
Yadu Kishore1454c9f2020-03-17 14:08:38 +05304105 if (!csum) {
4106 if (!nskb->remcsum_offload)
4107 nskb->ip_summed = CHECKSUM_NONE;
4108 SKB_GSO_CB(nskb)->csum =
4109 skb_copy_and_csum_bits(head_skb, offset,
4110 skb_put(nskb,
4111 len),
Al Viro8d5930d2020-07-10 20:07:10 -04004112 len);
Yadu Kishore1454c9f2020-03-17 14:08:38 +05304113 SKB_GSO_CB(nskb)->csum_start =
4114 skb_headroom(nskb) + doffset;
4115 } else {
4116 skb_copy_bits(head_skb, offset,
4117 skb_put(nskb, len),
4118 len);
4119 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004120 continue;
4121 }
4122
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004123 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004124
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004125 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004126 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004127
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08004128 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4129 SKBFL_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00004130
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004131 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4132 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4133 goto err;
4134
Herbert Xu9d8506c2013-11-21 11:10:04 -08004135 while (pos < offset + len) {
4136 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08004137 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004138 nfrags = skb_shinfo(list_skb)->nr_frags;
4139 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004140 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07004141 if (!skb_headlen(list_skb)) {
4142 BUG_ON(!nfrags);
4143 } else {
4144 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004145
Yonghong Song13acc942018-03-21 16:31:03 -07004146 /* to make room for head_frag. */
4147 i--;
4148 frag--;
4149 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004150 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4151 skb_zerocopy_clone(nskb, frag_skb,
4152 GFP_ATOMIC))
4153 goto err;
4154
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004155 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004156 }
4157
4158 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4159 MAX_SKB_FRAGS)) {
4160 net_warn_ratelimited(
4161 "skb_segment: too many frags: %u %u\n",
4162 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07004163 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004164 goto err;
4165 }
4166
Yonghong Song13acc942018-03-21 16:31:03 -07004167 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004168 __skb_frag_ref(nskb_frag);
4169 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004170
4171 if (pos < offset) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004172 skb_frag_off_add(nskb_frag, offset - pos);
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004173 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004174 }
4175
Herbert Xu89319d382008-12-15 23:26:06 -08004176 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004177
4178 if (pos + size <= offset + len) {
4179 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004180 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004181 pos += size;
4182 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004183 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08004184 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004185 }
4186
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004187 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004188 }
4189
Herbert Xu89319d382008-12-15 23:26:06 -08004190skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07004191 nskb->data_len = len - hsize;
4192 nskb->len += nskb->data_len;
4193 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004194
Simon Horman1cdbcb72013-05-19 15:46:49 +00004195perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004196 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07004197 if (skb_has_shared_frag(nskb) &&
4198 __skb_linearize(nskb))
4199 goto err;
4200
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004201 if (!nskb->remcsum_offload)
4202 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08004203 SKB_GSO_CB(nskb)->csum =
4204 skb_checksum(nskb, doffset,
4205 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004206 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08004207 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004208 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004209 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004210
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07004211 /* Some callers want to get the end of the list.
4212 * Put it in segs->prev to avoid walking the list.
4213 * (see validate_xmit_skb_list() for example)
4214 */
4215 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07004216
Alexander Duyck802ab552016-04-10 21:45:03 -04004217 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02004218 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04004219 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004220 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04004221
4222 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004223 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04004224 type &= ~SKB_GSO_DODGY;
4225
4226 /* Update GSO info and prepare to start updating headers on
4227 * our way back down the stack of protocols.
4228 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004229 for (iter = segs; iter; iter = iter->next) {
4230 skb_shinfo(iter)->gso_size = gso_size;
4231 skb_shinfo(iter)->gso_segs = partial_segs;
4232 skb_shinfo(iter)->gso_type = type;
4233 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4234 }
4235
4236 if (tail->len - doffset <= gso_size)
4237 skb_shinfo(tail)->gso_size = 0;
4238 else if (tail != segs)
4239 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04004240 }
4241
Toshiaki Makita432c8562014-10-27 10:30:51 -07004242 /* Following permits correct backpressure, for protocols
4243 * using skb_set_owner_w().
4244 * Idea is to tranfert ownership from head_skb to last segment.
4245 */
4246 if (head_skb->destructor == sock_wfree) {
4247 swap(tail->truesize, head_skb->truesize);
4248 swap(tail->destructor, head_skb->destructor);
4249 swap(tail->sk, head_skb->sk);
4250 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004251 return segs;
4252
4253err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08004254 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004255 return ERR_PTR(err);
4256}
Herbert Xuf4c50d92006-06-22 03:02:40 -07004257EXPORT_SYMBOL_GPL(skb_segment);
4258
David Millerd4546c22018-06-24 14:13:49 +09004259int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08004260{
Eric Dumazet8a291112013-10-08 09:02:23 -07004261 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00004262 unsigned int offset = skb_gro_offset(skb);
4263 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004264 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004265 unsigned int delta_truesize;
Paolo Abeni5e10da52021-07-28 18:24:03 +02004266 unsigned int new_truesize;
David Millerd4546c22018-06-24 14:13:49 +09004267 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08004268
Steffen Klassert0ab03f32019-04-02 08:16:03 +02004269 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08004270 return -E2BIG;
4271
Eric Dumazet29e98242014-05-16 11:34:37 -07004272 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07004273 pinfo = skb_shinfo(lp);
4274
4275 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00004276 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004277 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004278 int i = skbinfo->nr_frags;
4279 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00004280
Herbert Xu66e92fc2009-05-26 18:50:32 +00004281 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004282 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00004283
Eric Dumazet8a291112013-10-08 09:02:23 -07004284 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004285 pinfo->nr_frags = nr_frags;
4286 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08004287
Herbert Xu9aaa1562009-05-26 18:50:33 +00004288 frag = pinfo->frags + nr_frags;
4289 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004290 do {
4291 *--frag = *--frag2;
4292 } while (--i);
4293
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004294 skb_frag_off_add(frag, offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004295 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00004296
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004297 /* all fragments truesize : remove (head size + sk_buff) */
Paolo Abeni5e10da52021-07-28 18:24:03 +02004298 new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
4299 delta_truesize = skb->truesize - new_truesize;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004300
Paolo Abeni5e10da52021-07-28 18:24:03 +02004301 skb->truesize = new_truesize;
Herbert Xuf5572062009-01-14 20:40:03 -08004302 skb->len -= skb->data_len;
4303 skb->data_len = 0;
4304
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004305 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004306 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004307 } else if (skb->head_frag) {
4308 int nr_frags = pinfo->nr_frags;
4309 skb_frag_t *frag = pinfo->frags + nr_frags;
4310 struct page *page = virt_to_head_page(skb->head);
4311 unsigned int first_size = headlen - offset;
4312 unsigned int first_offset;
4313
4314 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004315 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004316
4317 first_offset = skb->data -
4318 (unsigned char *)page_address(page) +
4319 offset;
4320
4321 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4322
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07004323 __skb_frag_set_page(frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004324 skb_frag_off_set(frag, first_offset);
Eric Dumazetd7e88832012-04-30 08:10:34 +00004325 skb_frag_size_set(frag, first_size);
4326
4327 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4328 /* We dont need to clear skbinfo->nr_frags here */
4329
Paolo Abeni5e10da52021-07-28 18:24:03 +02004330 new_truesize = SKB_TRUESIZE(sizeof(struct sk_buff));
4331 delta_truesize = skb->truesize - new_truesize;
4332 skb->truesize = new_truesize;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004333 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4334 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004335 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004336
4337merge:
Paolo Abeni5e10da52021-07-28 18:24:03 +02004338 /* sk owenrship - if any - completely transferred to the aggregated packet */
4339 skb->destructor = NULL;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004340 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004341 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004342 unsigned int eat = offset - headlen;
4343
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004344 skb_frag_off_add(&skbinfo->frags[0], eat);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004345 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004346 skb->data_len -= eat;
4347 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004348 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004349 }
4350
Herbert Xu67147ba2009-05-26 18:50:22 +00004351 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004352
Eric Dumazet29e98242014-05-16 11:34:37 -07004353 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004354 skb_shinfo(p)->frag_list = skb;
4355 else
4356 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004357 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004358 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004359 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004360
Herbert Xu5d38a072009-01-04 16:13:40 -08004361done:
4362 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004363 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004364 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004365 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004366 if (lp != p) {
4367 lp->data_len += len;
4368 lp->truesize += delta_truesize;
4369 lp->len += len;
4370 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004371 NAPI_GRO_CB(skb)->same_flow = 1;
4372 return 0;
4373}
Herbert Xu71d93b32008-12-15 23:42:33 -08004374
Florian Westphaldf5042f2018-12-18 17:15:16 +01004375#ifdef CONFIG_SKB_EXTENSIONS
4376#define SKB_EXT_ALIGN_VALUE 8
4377#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4378
4379static const u8 skb_ext_type_len[] = {
4380#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4381 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4382#endif
Florian Westphal41650792018-12-18 17:15:27 +01004383#ifdef CONFIG_XFRM
4384 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4385#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004386#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4387 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4388#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004389#if IS_ENABLED(CONFIG_MPTCP)
4390 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4391#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004392};
4393
4394static __always_inline unsigned int skb_ext_total_length(void)
4395{
4396 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4397#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4398 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4399#endif
Florian Westphal41650792018-12-18 17:15:27 +01004400#ifdef CONFIG_XFRM
4401 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4402#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004403#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4404 skb_ext_type_len[TC_SKB_EXT] +
4405#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004406#if IS_ENABLED(CONFIG_MPTCP)
4407 skb_ext_type_len[SKB_EXT_MPTCP] +
4408#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004409 0;
4410}
4411
4412static void skb_extensions_init(void)
4413{
4414 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4415 BUILD_BUG_ON(skb_ext_total_length() > 255);
4416
4417 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4418 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4419 0,
4420 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4421 NULL);
4422}
4423#else
4424static void skb_extensions_init(void) {}
4425#endif
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427void __init skb_init(void)
4428{
Kees Cook79a8a642018-02-07 17:44:38 -08004429 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 sizeof(struct sk_buff),
4431 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004432 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004433 offsetof(struct sk_buff, cb),
4434 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004435 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004436 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004437 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004438 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004439 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004440 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004441 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
David S. Miller51c739d2007-10-30 21:29:29 -07004444static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004445__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4446 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004447{
David S. Miller1a028e52007-04-27 15:21:23 -07004448 int start = skb_headlen(skb);
4449 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004450 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004451 int elt = 0;
4452
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004453 if (unlikely(recursion_level >= 24))
4454 return -EMSGSIZE;
4455
David Howells716ea3a2007-04-02 20:19:53 -07004456 if (copy > 0) {
4457 if (copy > len)
4458 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004459 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004460 elt++;
4461 if ((len -= copy) == 0)
4462 return elt;
4463 offset += copy;
4464 }
4465
4466 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004467 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004468
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004469 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004470
Eric Dumazet9e903e02011-10-18 21:00:24 +00004471 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004472 if ((copy = end - offset) > 0) {
4473 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004474 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4475 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004476
4477 if (copy > len)
4478 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004479 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004480 skb_frag_off(frag) + offset - start);
David Howells716ea3a2007-04-02 20:19:53 -07004481 elt++;
4482 if (!(len -= copy))
4483 return elt;
4484 offset += copy;
4485 }
David S. Miller1a028e52007-04-27 15:21:23 -07004486 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004487 }
4488
David S. Millerfbb398a2009-06-09 00:18:59 -07004489 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004490 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004491
David S. Millerfbb398a2009-06-09 00:18:59 -07004492 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004493
David S. Millerfbb398a2009-06-09 00:18:59 -07004494 end = start + frag_iter->len;
4495 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004496 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4497 return -EMSGSIZE;
4498
David S. Millerfbb398a2009-06-09 00:18:59 -07004499 if (copy > len)
4500 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004501 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4502 copy, recursion_level + 1);
4503 if (unlikely(ret < 0))
4504 return ret;
4505 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004506 if ((len -= copy) == 0)
4507 return elt;
4508 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004509 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004510 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004511 }
4512 BUG_ON(len);
4513 return elt;
4514}
4515
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004516/**
4517 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4518 * @skb: Socket buffer containing the buffers to be mapped
4519 * @sg: The scatter-gather list to map into
4520 * @offset: The offset into the buffer's contents to start mapping
4521 * @len: Length of buffer space to be mapped
4522 *
4523 * Fill the specified scatter-gather list with mappings/pointers into a
4524 * region of the buffer space attached to a socket buffer. Returns either
4525 * the number of scatterlist items used, or -EMSGSIZE if the contents
4526 * could not fit.
4527 */
4528int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4529{
4530 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4531
4532 if (nsg <= 0)
4533 return nsg;
4534
4535 sg_mark_end(&sg[nsg - 1]);
4536
4537 return nsg;
4538}
4539EXPORT_SYMBOL_GPL(skb_to_sgvec);
4540
Fan Du25a91d82014-01-18 09:54:23 +08004541/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4542 * sglist without mark the sg which contain last skb data as the end.
4543 * So the caller can mannipulate sg list as will when padding new data after
4544 * the first call without calling sg_unmark_end to expend sg list.
4545 *
4546 * Scenario to use skb_to_sgvec_nomark:
4547 * 1. sg_init_table
4548 * 2. skb_to_sgvec_nomark(payload1)
4549 * 3. skb_to_sgvec_nomark(payload2)
4550 *
4551 * This is equivalent to:
4552 * 1. sg_init_table
4553 * 2. skb_to_sgvec(payload1)
4554 * 3. sg_unmark_end
4555 * 4. skb_to_sgvec(payload2)
4556 *
4557 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4558 * is more preferable.
4559 */
4560int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4561 int offset, int len)
4562{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004563 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004564}
4565EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4566
David S. Miller51c739d2007-10-30 21:29:29 -07004567
David S. Miller51c739d2007-10-30 21:29:29 -07004568
David Howells716ea3a2007-04-02 20:19:53 -07004569/**
4570 * skb_cow_data - Check that a socket buffer's data buffers are writable
4571 * @skb: The socket buffer to check.
4572 * @tailbits: Amount of trailing space to be added
4573 * @trailer: Returned pointer to the skb where the @tailbits space begins
4574 *
4575 * Make sure that the data buffers attached to a socket buffer are
4576 * writable. If they are not, private copies are made of the data buffers
4577 * and the socket buffer is set to use these instead.
4578 *
4579 * If @tailbits is given, make sure that there is space to write @tailbits
4580 * bytes of data beyond current end of socket buffer. @trailer will be
4581 * set to point to the skb in which this space begins.
4582 *
4583 * The number of scatterlist elements required to completely map the
4584 * COW'd and extended socket buffer will be returned.
4585 */
4586int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4587{
4588 int copyflag;
4589 int elt;
4590 struct sk_buff *skb1, **skb_p;
4591
4592 /* If skb is cloned or its head is paged, reallocate
4593 * head pulling out all the pages (pages are considered not writable
4594 * at the moment even if they are anonymous).
4595 */
4596 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
Miaohe Linc15fc192020-08-01 17:30:23 +08004597 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
David Howells716ea3a2007-04-02 20:19:53 -07004598 return -ENOMEM;
4599
4600 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004601 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004602 /* A little of trouble, not enough of space for trailer.
4603 * This should not happen, when stack is tuned to generate
4604 * good frames. OK, on miss we reallocate and reserve even more
4605 * space, 128 bytes is fair. */
4606
4607 if (skb_tailroom(skb) < tailbits &&
4608 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4609 return -ENOMEM;
4610
4611 /* Voila! */
4612 *trailer = skb;
4613 return 1;
4614 }
4615
4616 /* Misery. We are in troubles, going to mincer fragments... */
4617
4618 elt = 1;
4619 skb_p = &skb_shinfo(skb)->frag_list;
4620 copyflag = 0;
4621
4622 while ((skb1 = *skb_p) != NULL) {
4623 int ntail = 0;
4624
4625 /* The fragment is partially pulled by someone,
4626 * this can happen on input. Copy it and everything
4627 * after it. */
4628
4629 if (skb_shared(skb1))
4630 copyflag = 1;
4631
4632 /* If the skb is the last, worry about trailer. */
4633
4634 if (skb1->next == NULL && tailbits) {
4635 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004636 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004637 skb_tailroom(skb1) < tailbits)
4638 ntail = tailbits + 128;
4639 }
4640
4641 if (copyflag ||
4642 skb_cloned(skb1) ||
4643 ntail ||
4644 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004645 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004646 struct sk_buff *skb2;
4647
4648 /* Fuck, we are miserable poor guys... */
4649 if (ntail == 0)
4650 skb2 = skb_copy(skb1, GFP_ATOMIC);
4651 else
4652 skb2 = skb_copy_expand(skb1,
4653 skb_headroom(skb1),
4654 ntail,
4655 GFP_ATOMIC);
4656 if (unlikely(skb2 == NULL))
4657 return -ENOMEM;
4658
4659 if (skb1->sk)
4660 skb_set_owner_w(skb2, skb1->sk);
4661
4662 /* Looking around. Are we still alive?
4663 * OK, link new skb, drop old one */
4664
4665 skb2->next = skb1->next;
4666 *skb_p = skb2;
4667 kfree_skb(skb1);
4668 skb1 = skb2;
4669 }
4670 elt++;
4671 *trailer = skb1;
4672 skb_p = &skb1->next;
4673 }
4674
4675 return elt;
4676}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004677EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004678
Eric Dumazetb1faf562010-05-31 23:44:05 -07004679static void sock_rmem_free(struct sk_buff *skb)
4680{
4681 struct sock *sk = skb->sk;
4682
4683 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4684}
4685
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004686static void skb_set_err_queue(struct sk_buff *skb)
4687{
4688 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4689 * So, it is safe to (mis)use it to mark skbs on the error queue.
4690 */
4691 skb->pkt_type = PACKET_OUTGOING;
4692 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4693}
4694
Eric Dumazetb1faf562010-05-31 23:44:05 -07004695/*
4696 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4697 */
4698int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4699{
4700 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazetebb3b782019-10-10 20:17:44 -07004701 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
Eric Dumazetb1faf562010-05-31 23:44:05 -07004702 return -ENOMEM;
4703
4704 skb_orphan(skb);
4705 skb->sk = sk;
4706 skb->destructor = sock_rmem_free;
4707 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004708 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004709
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004710 /* before exiting rcu section, make sure dst is refcounted */
4711 skb_dst_force(skb);
4712
Eric Dumazetb1faf562010-05-31 23:44:05 -07004713 skb_queue_tail(&sk->sk_error_queue, skb);
4714 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04004715 sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004716 return 0;
4717}
4718EXPORT_SYMBOL(sock_queue_err_skb);
4719
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004720static bool is_icmp_err_skb(const struct sk_buff *skb)
4721{
4722 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4723 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4724}
4725
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004726struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4727{
4728 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004729 struct sk_buff *skb, *skb_next = NULL;
4730 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004731 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004732
Eric Dumazet997d5c32015-02-18 05:47:55 -08004733 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004734 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004735 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004736 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004737 if (icmp_next)
Willem de Bruijn985f7332020-11-26 10:12:20 -05004738 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004739 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004740 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004741
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004742 if (is_icmp_err_skb(skb) && !icmp_next)
4743 sk->sk_err = 0;
4744
4745 if (skb_next)
Alexander Aringe3ae2362021-06-27 18:48:21 -04004746 sk_error_report(sk);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004747
4748 return skb;
4749}
4750EXPORT_SYMBOL(sock_dequeue_err_skb);
4751
Alexander Duyckcab41c42014-09-10 18:05:26 -04004752/**
4753 * skb_clone_sk - create clone of skb, and take reference to socket
4754 * @skb: the skb to clone
4755 *
4756 * This function creates a clone of a buffer that holds a reference on
4757 * sk_refcnt. Buffers created via this function are meant to be
4758 * returned using sock_queue_err_skb, or free via kfree_skb.
4759 *
4760 * When passing buffers allocated with this function to sock_queue_err_skb
4761 * it is necessary to wrap the call with sock_hold/sock_put in order to
4762 * prevent the socket from being released prior to being enqueued on
4763 * the sk_error_queue.
4764 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004765struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4766{
4767 struct sock *sk = skb->sk;
4768 struct sk_buff *clone;
4769
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004770 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004771 return NULL;
4772
4773 clone = skb_clone(skb, GFP_ATOMIC);
4774 if (!clone) {
4775 sock_put(sk);
4776 return NULL;
4777 }
4778
4779 clone->sk = sk;
4780 clone->destructor = sock_efree;
4781
4782 return clone;
4783}
4784EXPORT_SYMBOL(skb_clone_sk);
4785
Alexander Duyck37846ef2014-09-04 13:31:10 -04004786static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4787 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004788 int tstype,
4789 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004790{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004791 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004792 int err;
4793
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004794 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4795
Patrick Ohlyac45f602009-02-12 05:03:37 +00004796 serr = SKB_EXT_ERR(skb);
4797 memset(serr, 0, sizeof(*serr));
4798 serr->ee.ee_errno = ENOMSG;
4799 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004800 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004801 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004802 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004803 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004804 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004805 if (sk->sk_protocol == IPPROTO_TCP &&
4806 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004807 serr->ee.ee_data -= sk->sk_tskey;
4808 }
Eric Dumazet29030372010-05-29 00:20:48 -07004809
Patrick Ohlyac45f602009-02-12 05:03:37 +00004810 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004811
Patrick Ohlyac45f602009-02-12 05:03:37 +00004812 if (err)
4813 kfree_skb(skb);
4814}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004815
Willem de Bruijnb245be12015-01-30 13:29:32 -05004816static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4817{
4818 bool ret;
4819
4820 if (likely(sysctl_tstamp_allow_data || tsonly))
4821 return true;
4822
4823 read_lock_bh(&sk->sk_callback_lock);
4824 ret = sk->sk_socket && sk->sk_socket->file &&
4825 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4826 read_unlock_bh(&sk->sk_callback_lock);
4827 return ret;
4828}
4829
Alexander Duyck37846ef2014-09-04 13:31:10 -04004830void skb_complete_tx_timestamp(struct sk_buff *skb,
4831 struct skb_shared_hwtstamps *hwtstamps)
4832{
4833 struct sock *sk = skb->sk;
4834
Willem de Bruijnb245be12015-01-30 13:29:32 -05004835 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004836 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004837
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004838 /* Take a reference to prevent skb_orphan() from freeing the socket,
4839 * but only if the socket refcount is not zero.
4840 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004841 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004842 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004843 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004844 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004845 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004846 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004847
4848err:
4849 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004850}
4851EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4852
4853void __skb_tstamp_tx(struct sk_buff *orig_skb,
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004854 const struct sk_buff *ack_skb,
Alexander Duyck37846ef2014-09-04 13:31:10 -04004855 struct skb_shared_hwtstamps *hwtstamps,
4856 struct sock *sk, int tstype)
4857{
4858 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004859 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004860
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004861 if (!sk)
4862 return;
4863
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004864 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4865 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4866 return;
4867
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004868 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4869 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004870 return;
4871
Francis Yan1c885802016-11-27 23:07:18 -08004872 if (tsonly) {
4873#ifdef CONFIG_INET
4874 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4875 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004876 sk->sk_type == SOCK_STREAM) {
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004877 skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
4878 ack_skb);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004879 opt_stats = true;
4880 } else
Francis Yan1c885802016-11-27 23:07:18 -08004881#endif
4882 skb = alloc_skb(0, GFP_ATOMIC);
4883 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004884 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004885 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004886 if (!skb)
4887 return;
4888
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004889 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004890 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4891 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004892 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4893 }
4894
4895 if (hwtstamps)
4896 *skb_hwtstamps(skb) = *hwtstamps;
4897 else
4898 skb->tstamp = ktime_get_real();
4899
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004900 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004901}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004902EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4903
4904void skb_tstamp_tx(struct sk_buff *orig_skb,
4905 struct skb_shared_hwtstamps *hwtstamps)
4906{
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004907 return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004908 SCM_TSTAMP_SND);
4909}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004910EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4911
Johannes Berg6e3e9392011-11-09 10:15:42 +01004912void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4913{
4914 struct sock *sk = skb->sk;
4915 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004916 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004917
4918 skb->wifi_acked_valid = 1;
4919 skb->wifi_acked = acked;
4920
4921 serr = SKB_EXT_ERR(skb);
4922 memset(serr, 0, sizeof(*serr));
4923 serr->ee.ee_errno = ENOMSG;
4924 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4925
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004926 /* Take a reference to prevent skb_orphan() from freeing the socket,
4927 * but only if the socket refcount is not zero.
4928 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004929 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004930 err = sock_queue_err_skb(sk, skb);
4931 sock_put(sk);
4932 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004933 if (err)
4934 kfree_skb(skb);
4935}
4936EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4937
Rusty Russellf35d9d82008-02-04 23:49:54 -05004938/**
4939 * skb_partial_csum_set - set up and verify partial csum values for packet
4940 * @skb: the skb to set
4941 * @start: the number of bytes after skb->data to start checksumming.
4942 * @off: the offset from start to place the checksum.
4943 *
4944 * For untrusted partially-checksummed packets, we need to make sure the values
4945 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4946 *
4947 * This function checks and sets those values and skb->ip_summed: if this
4948 * returns false you should drop the packet.
4949 */
4950bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4951{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004952 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4953 u32 csum_start = skb_headroom(skb) + (u32)start;
4954
4955 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4956 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4957 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05004958 return false;
4959 }
4960 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004961 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05004962 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00004963 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004964 return true;
4965}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004966EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05004967
Paul Durranted1f50c2014-01-09 10:02:46 +00004968static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4969 unsigned int max)
4970{
4971 if (skb_headlen(skb) >= len)
4972 return 0;
4973
4974 /* If we need to pullup then pullup to the max, so we
4975 * won't need to do it again.
4976 */
4977 if (max > skb->len)
4978 max = skb->len;
4979
4980 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4981 return -ENOMEM;
4982
4983 if (skb_headlen(skb) < len)
4984 return -EPROTO;
4985
4986 return 0;
4987}
4988
Jan Beulichf9708b42014-03-11 13:56:05 +00004989#define MAX_TCP_HDR_LEN (15 * 4)
4990
4991static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4992 typeof(IPPROTO_IP) proto,
4993 unsigned int off)
4994{
Kees Cook161d1792020-02-19 22:23:04 -08004995 int err;
Jan Beulichf9708b42014-03-11 13:56:05 +00004996
Kees Cook161d1792020-02-19 22:23:04 -08004997 switch (proto) {
Jan Beulichf9708b42014-03-11 13:56:05 +00004998 case IPPROTO_TCP:
4999 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5000 off + MAX_TCP_HDR_LEN);
5001 if (!err && !skb_partial_csum_set(skb, off,
5002 offsetof(struct tcphdr,
5003 check)))
5004 err = -EPROTO;
5005 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5006
5007 case IPPROTO_UDP:
5008 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5009 off + sizeof(struct udphdr));
5010 if (!err && !skb_partial_csum_set(skb, off,
5011 offsetof(struct udphdr,
5012 check)))
5013 err = -EPROTO;
5014 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5015 }
5016
5017 return ERR_PTR(-EPROTO);
5018}
5019
Paul Durranted1f50c2014-01-09 10:02:46 +00005020/* This value should be large enough to cover a tagged ethernet header plus
5021 * maximally sized IP and TCP or UDP headers.
5022 */
5023#define MAX_IP_HDR_LEN 128
5024
Jan Beulichf9708b42014-03-11 13:56:05 +00005025static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00005026{
5027 unsigned int off;
5028 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00005029 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00005030 int err;
5031
5032 fragment = false;
5033
5034 err = skb_maybe_pull_tail(skb,
5035 sizeof(struct iphdr),
5036 MAX_IP_HDR_LEN);
5037 if (err < 0)
5038 goto out;
5039
Miaohe Lin11f920d2020-08-06 19:57:18 +08005040 if (ip_is_fragment(ip_hdr(skb)))
Paul Durranted1f50c2014-01-09 10:02:46 +00005041 fragment = true;
5042
5043 off = ip_hdrlen(skb);
5044
5045 err = -EPROTO;
5046
5047 if (fragment)
5048 goto out;
5049
Jan Beulichf9708b42014-03-11 13:56:05 +00005050 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5051 if (IS_ERR(csum))
5052 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005053
Jan Beulichf9708b42014-03-11 13:56:05 +00005054 if (recalculate)
5055 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5056 ip_hdr(skb)->daddr,
5057 skb->len - off,
5058 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005059 err = 0;
5060
5061out:
5062 return err;
5063}
5064
5065/* This value should be large enough to cover a tagged ethernet header plus
5066 * an IPv6 header, all options, and a maximal TCP or UDP header.
5067 */
5068#define MAX_IPV6_HDR_LEN 256
5069
5070#define OPT_HDR(type, skb, off) \
5071 (type *)(skb_network_header(skb) + (off))
5072
5073static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5074{
5075 int err;
5076 u8 nexthdr;
5077 unsigned int off;
5078 unsigned int len;
5079 bool fragment;
5080 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00005081 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00005082
5083 fragment = false;
5084 done = false;
5085
5086 off = sizeof(struct ipv6hdr);
5087
5088 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5089 if (err < 0)
5090 goto out;
5091
5092 nexthdr = ipv6_hdr(skb)->nexthdr;
5093
5094 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5095 while (off <= len && !done) {
5096 switch (nexthdr) {
5097 case IPPROTO_DSTOPTS:
5098 case IPPROTO_HOPOPTS:
5099 case IPPROTO_ROUTING: {
5100 struct ipv6_opt_hdr *hp;
5101
5102 err = skb_maybe_pull_tail(skb,
5103 off +
5104 sizeof(struct ipv6_opt_hdr),
5105 MAX_IPV6_HDR_LEN);
5106 if (err < 0)
5107 goto out;
5108
5109 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5110 nexthdr = hp->nexthdr;
5111 off += ipv6_optlen(hp);
5112 break;
5113 }
5114 case IPPROTO_AH: {
5115 struct ip_auth_hdr *hp;
5116
5117 err = skb_maybe_pull_tail(skb,
5118 off +
5119 sizeof(struct ip_auth_hdr),
5120 MAX_IPV6_HDR_LEN);
5121 if (err < 0)
5122 goto out;
5123
5124 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5125 nexthdr = hp->nexthdr;
5126 off += ipv6_authlen(hp);
5127 break;
5128 }
5129 case IPPROTO_FRAGMENT: {
5130 struct frag_hdr *hp;
5131
5132 err = skb_maybe_pull_tail(skb,
5133 off +
5134 sizeof(struct frag_hdr),
5135 MAX_IPV6_HDR_LEN);
5136 if (err < 0)
5137 goto out;
5138
5139 hp = OPT_HDR(struct frag_hdr, skb, off);
5140
5141 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5142 fragment = true;
5143
5144 nexthdr = hp->nexthdr;
5145 off += sizeof(struct frag_hdr);
5146 break;
5147 }
5148 default:
5149 done = true;
5150 break;
5151 }
5152 }
5153
5154 err = -EPROTO;
5155
5156 if (!done || fragment)
5157 goto out;
5158
Jan Beulichf9708b42014-03-11 13:56:05 +00005159 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5160 if (IS_ERR(csum))
5161 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005162
Jan Beulichf9708b42014-03-11 13:56:05 +00005163 if (recalculate)
5164 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5165 &ipv6_hdr(skb)->daddr,
5166 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005167 err = 0;
5168
5169out:
5170 return err;
5171}
5172
5173/**
5174 * skb_checksum_setup - set up partial checksum offset
5175 * @skb: the skb to set up
5176 * @recalculate: if true the pseudo-header checksum will be recalculated
5177 */
5178int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5179{
5180 int err;
5181
5182 switch (skb->protocol) {
5183 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00005184 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00005185 break;
5186
5187 case htons(ETH_P_IPV6):
5188 err = skb_checksum_setup_ipv6(skb, recalculate);
5189 break;
5190
5191 default:
5192 err = -EPROTO;
5193 break;
5194 }
5195
5196 return err;
5197}
5198EXPORT_SYMBOL(skb_checksum_setup);
5199
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005200/**
5201 * skb_checksum_maybe_trim - maybe trims the given skb
5202 * @skb: the skb to check
5203 * @transport_len: the data length beyond the network header
5204 *
5205 * Checks whether the given skb has data beyond the given transport length.
5206 * If so, returns a cloned skb trimmed to this transport length.
5207 * Otherwise returns the provided skb. Returns NULL in error cases
5208 * (e.g. transport_len exceeds skb length or out-of-memory).
5209 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005210 * Caller needs to set the skb transport header and free any returned skb if it
5211 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005212 */
5213static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5214 unsigned int transport_len)
5215{
5216 struct sk_buff *skb_chk;
5217 unsigned int len = skb_transport_offset(skb) + transport_len;
5218 int ret;
5219
Linus Lüssinga5169932015-08-13 05:54:07 +02005220 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005221 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02005222 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005223 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005224
5225 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005226 if (!skb_chk)
5227 return NULL;
5228
5229 ret = pskb_trim_rcsum(skb_chk, len);
5230 if (ret) {
5231 kfree_skb(skb_chk);
5232 return NULL;
5233 }
5234
5235 return skb_chk;
5236}
5237
5238/**
5239 * skb_checksum_trimmed - validate checksum of an skb
5240 * @skb: the skb to check
5241 * @transport_len: the data length beyond the network header
5242 * @skb_chkf: checksum function to use
5243 *
5244 * Applies the given checksum function skb_chkf to the provided skb.
5245 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5246 *
5247 * If the skb has data beyond the given transport length, then a
5248 * trimmed & cloned skb is checked and returned.
5249 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005250 * Caller needs to set the skb transport header and free any returned skb if it
5251 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005252 */
5253struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5254 unsigned int transport_len,
5255 __sum16(*skb_chkf)(struct sk_buff *skb))
5256{
5257 struct sk_buff *skb_chk;
5258 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02005259 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005260
5261 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5262 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02005263 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005264
Linus Lüssinga5169932015-08-13 05:54:07 +02005265 if (!pskb_may_pull(skb_chk, offset))
5266 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005267
Linus Lüssing9b368812016-02-24 04:21:42 +01005268 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005269 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01005270 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005271
Linus Lüssinga5169932015-08-13 05:54:07 +02005272 if (ret)
5273 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005274
5275 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02005276
5277err:
5278 if (skb_chk && skb_chk != skb)
5279 kfree_skb(skb_chk);
5280
5281 return NULL;
5282
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005283}
5284EXPORT_SYMBOL(skb_checksum_trimmed);
5285
Ben Hutchings4497b072008-06-19 16:22:28 -07005286void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5287{
Joe Perchese87cc472012-05-13 21:56:26 +00005288 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5289 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07005290}
Ben Hutchings4497b072008-06-19 16:22:28 -07005291EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005292
5293void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5294{
Eric Dumazet3d861f62012-10-22 09:03:40 +00005295 if (head_stolen) {
5296 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005297 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005298 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005299 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005300 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005301}
5302EXPORT_SYMBOL(kfree_skb_partial);
5303
5304/**
5305 * skb_try_coalesce - try to merge skb to prior one
5306 * @to: prior buffer
5307 * @from: buffer to add
5308 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00005309 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005310 */
5311bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5312 bool *fragstolen, int *delta_truesize)
5313{
Eric Dumazetc818fa92017-10-04 10:48:35 -07005314 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005315 int i, delta, len = from->len;
5316
5317 *fragstolen = false;
5318
5319 if (skb_cloned(to))
5320 return false;
5321
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +02005322 /* The page pool signature of struct page will eventually figure out
5323 * which pages can be recycled or not but for now let's prohibit slab
5324 * allocated and page_pool allocated SKBs from being coalesced.
5325 */
5326 if (to->pp_recycle != from->pp_recycle)
5327 return false;
5328
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005329 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005330 if (len)
5331 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005332 *delta_truesize = 0;
5333 return true;
5334 }
5335
Eric Dumazetc818fa92017-10-04 10:48:35 -07005336 to_shinfo = skb_shinfo(to);
5337 from_shinfo = skb_shinfo(from);
5338 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005339 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005340 if (skb_zcopy(to) || skb_zcopy(from))
5341 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005342
5343 if (skb_headlen(from) != 0) {
5344 struct page *page;
5345 unsigned int offset;
5346
Eric Dumazetc818fa92017-10-04 10:48:35 -07005347 if (to_shinfo->nr_frags +
5348 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005349 return false;
5350
5351 if (skb_head_is_locked(from))
5352 return false;
5353
5354 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5355
5356 page = virt_to_head_page(from->head);
5357 offset = from->data - (unsigned char *)page_address(page);
5358
Eric Dumazetc818fa92017-10-04 10:48:35 -07005359 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005360 page, offset, skb_headlen(from));
5361 *fragstolen = true;
5362 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005363 if (to_shinfo->nr_frags +
5364 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005365 return false;
5366
Weiping Panf4b549a2012-09-28 20:15:30 +00005367 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005368 }
5369
5370 WARN_ON_ONCE(delta < len);
5371
Eric Dumazetc818fa92017-10-04 10:48:35 -07005372 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5373 from_shinfo->frags,
5374 from_shinfo->nr_frags * sizeof(skb_frag_t));
5375 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005376
5377 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005378 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005379
Li RongQing8ea853f2012-09-18 16:53:21 +00005380 /* if the skb is not cloned this does nothing
5381 * since we set nr_frags to 0.
5382 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005383 for (i = 0; i < from_shinfo->nr_frags; i++)
5384 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005385
5386 to->truesize += delta;
5387 to->len += len;
5388 to->data_len += len;
5389
5390 *delta_truesize = delta;
5391 return true;
5392}
5393EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005394
5395/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005396 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005397 *
5398 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005399 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005400 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005401 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5402 * into/from a tunnel. Some information have to be cleared during these
5403 * operations.
5404 * skb_scrub_packet can also be used to clean a skb before injecting it in
5405 * another namespace (@xnet == true). We have to clear all information in the
5406 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005407 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005408void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005409{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005410 skb->pkt_type = PACKET_HOST;
5411 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005412 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005413 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005414 skb_ext_reset(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02005415 nf_reset_ct(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005416 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005417
Petr Machata6f9a5062018-11-19 16:11:07 +00005418#ifdef CONFIG_NET_SWITCHDEV
5419 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005420 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005421#endif
5422
Herbert Xu213dd742015-04-16 09:03:27 +08005423 if (!xnet)
5424 return;
5425
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005426 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005427 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005428 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005429}
5430EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005431
5432/**
5433 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5434 *
5435 * @skb: GSO skb
5436 *
5437 * skb_gso_transport_seglen is used to determine the real size of the
5438 * individual segments, including Layer4 headers (TCP/UDP).
5439 *
5440 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5441 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005442static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005443{
5444 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005445 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005446
Florian Westphalf993bc22014-10-20 13:49:18 +02005447 if (skb->encapsulation) {
5448 thlen = skb_inner_transport_header(skb) -
5449 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005450
Florian Westphalf993bc22014-10-20 13:49:18 +02005451 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5452 thlen += inner_tcp_hdrlen(skb);
5453 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5454 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005455 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005456 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005457 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5458 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005459 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005460 /* UFO sets gso_size to the size of the fragmentation
5461 * payload, i.e. the size of the L4 (UDP) header is already
5462 * accounted for.
5463 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005464 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005465}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005466
5467/**
5468 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5469 *
5470 * @skb: GSO skb
5471 *
5472 * skb_gso_network_seglen is used to determine the real size of the
5473 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5474 *
5475 * The MAC/L2 header is not accounted for.
5476 */
5477static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5478{
5479 unsigned int hdr_len = skb_transport_header(skb) -
5480 skb_network_header(skb);
5481
5482 return hdr_len + skb_gso_transport_seglen(skb);
5483}
5484
5485/**
5486 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5487 *
5488 * @skb: GSO skb
5489 *
5490 * skb_gso_mac_seglen is used to determine the real size of the
5491 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5492 * headers (TCP/UDP).
5493 */
5494static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5495{
5496 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5497
5498 return hdr_len + skb_gso_transport_seglen(skb);
5499}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005500
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005501/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005502 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5503 *
5504 * There are a couple of instances where we have a GSO skb, and we
5505 * want to determine what size it would be after it is segmented.
5506 *
5507 * We might want to check:
5508 * - L3+L4+payload size (e.g. IP forwarding)
5509 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5510 *
5511 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5512 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005513 * @skb: GSO skb
5514 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005515 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5516 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5517 *
5518 * @max_len: The maximum permissible length.
5519 *
5520 * Returns true if the segmented length <= max length.
5521 */
5522static inline bool skb_gso_size_check(const struct sk_buff *skb,
5523 unsigned int seg_len,
5524 unsigned int max_len) {
5525 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5526 const struct sk_buff *iter;
5527
5528 if (shinfo->gso_size != GSO_BY_FRAGS)
5529 return seg_len <= max_len;
5530
5531 /* Undo this so we can re-use header sizes */
5532 seg_len -= GSO_BY_FRAGS;
5533
5534 skb_walk_frags(skb, iter) {
5535 if (seg_len + skb_headlen(iter) > max_len)
5536 return false;
5537 }
5538
5539 return true;
5540}
5541
5542/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005543 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005544 *
5545 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005546 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005547 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005548 * skb_gso_validate_network_len validates if a given skb will fit a
5549 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5550 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005551 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005552bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005553{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005554 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005555}
Daniel Axtens779b7932018-03-01 17:13:37 +11005556EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005557
Daniel Axtens2b16f042018-01-31 14:15:33 +11005558/**
5559 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5560 *
5561 * @skb: GSO skb
5562 * @len: length to validate against
5563 *
5564 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5565 * length once split, including L2, L3 and L4 headers and the payload.
5566 */
5567bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5568{
5569 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5570}
5571EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5572
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005573static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5574{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005575 int mac_len, meta_len;
5576 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005577
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005578 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5579 kfree_skb(skb);
5580 return NULL;
5581 }
5582
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005583 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005584 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5585 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5586 mac_len - VLAN_HLEN - ETH_TLEN);
5587 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005588
5589 meta_len = skb_metadata_len(skb);
5590 if (meta_len) {
5591 meta = skb_metadata_end(skb) - meta_len;
5592 memmove(meta + VLAN_HLEN, meta, meta_len);
5593 }
5594
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005595 skb->mac_header += VLAN_HLEN;
5596 return skb;
5597}
5598
5599struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5600{
5601 struct vlan_hdr *vhdr;
5602 u16 vlan_tci;
5603
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005604 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005605 /* vlan_tci is already set-up so leave this for another time */
5606 return skb;
5607 }
5608
5609 skb = skb_share_check(skb, GFP_ATOMIC);
5610 if (unlikely(!skb))
5611 goto err_free;
Miaohe Lin55eff0e2020-08-15 04:44:31 -04005612 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5613 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005614 goto err_free;
5615
5616 vhdr = (struct vlan_hdr *)skb->data;
5617 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5618 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5619
5620 skb_pull_rcsum(skb, VLAN_HLEN);
5621 vlan_set_encap_proto(skb, vhdr);
5622
5623 skb = skb_reorder_vlan_header(skb);
5624 if (unlikely(!skb))
5625 goto err_free;
5626
5627 skb_reset_network_header(skb);
Alexander Lobakin8be33ec2020-11-09 23:47:23 +00005628 if (!skb_transport_header_was_set(skb))
5629 skb_reset_transport_header(skb);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005630 skb_reset_mac_len(skb);
5631
5632 return skb;
5633
5634err_free:
5635 kfree_skb(skb);
5636 return NULL;
5637}
5638EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005639
Jiri Pirkoe2195122014-11-19 14:05:01 +01005640int skb_ensure_writable(struct sk_buff *skb, int write_len)
5641{
5642 if (!pskb_may_pull(skb, write_len))
5643 return -ENOMEM;
5644
5645 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5646 return 0;
5647
5648 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5649}
5650EXPORT_SYMBOL(skb_ensure_writable);
5651
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005652/* remove VLAN header from packet and update csum accordingly.
5653 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5654 */
5655int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005656{
5657 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005658 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005659 int err;
5660
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005661 if (WARN_ONCE(offset,
5662 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5663 offset)) {
5664 return -EINVAL;
5665 }
5666
Jiri Pirko93515d52014-11-19 14:05:02 +01005667 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5668 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005669 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005670
5671 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5672
5673 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5674 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5675
5676 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5677 __skb_pull(skb, VLAN_HLEN);
5678
5679 vlan_set_encap_proto(skb, vhdr);
5680 skb->mac_header += VLAN_HLEN;
5681
5682 if (skb_network_offset(skb) < ETH_HLEN)
5683 skb_set_network_header(skb, ETH_HLEN);
5684
5685 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005686
5687 return err;
5688}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005689EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005690
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005691/* Pop a vlan tag either from hwaccel or from payload.
5692 * Expects skb->data at mac header.
5693 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005694int skb_vlan_pop(struct sk_buff *skb)
5695{
5696 u16 vlan_tci;
5697 __be16 vlan_proto;
5698 int err;
5699
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005700 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005701 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005702 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005703 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005704 return 0;
5705
5706 err = __skb_vlan_pop(skb, &vlan_tci);
5707 if (err)
5708 return err;
5709 }
5710 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005711 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005712 return 0;
5713
5714 vlan_proto = skb->protocol;
5715 err = __skb_vlan_pop(skb, &vlan_tci);
5716 if (unlikely(err))
5717 return err;
5718
5719 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5720 return 0;
5721}
5722EXPORT_SYMBOL(skb_vlan_pop);
5723
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005724/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5725 * Expects skb->data at mac header.
5726 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005727int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5728{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005729 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005730 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005731 int err;
5732
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005733 if (WARN_ONCE(offset,
5734 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5735 offset)) {
5736 return -EINVAL;
5737 }
5738
Jiri Pirko93515d52014-11-19 14:05:02 +01005739 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005740 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005741 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005742 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005743
Jiri Pirko93515d52014-11-19 14:05:02 +01005744 skb->protocol = skb->vlan_proto;
5745 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005746
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005747 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005748 }
5749 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5750 return 0;
5751}
5752EXPORT_SYMBOL(skb_vlan_push);
5753
Guillaume Nault19fbcb32020-10-03 00:44:28 +02005754/**
5755 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5756 *
5757 * @skb: Socket buffer to modify
5758 *
5759 * Drop the Ethernet header of @skb.
5760 *
5761 * Expects that skb->data points to the mac header and that no VLAN tags are
5762 * present.
5763 *
5764 * Returns 0 on success, -errno otherwise.
5765 */
5766int skb_eth_pop(struct sk_buff *skb)
5767{
5768 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5769 skb_network_offset(skb) < ETH_HLEN)
5770 return -EPROTO;
5771
5772 skb_pull_rcsum(skb, ETH_HLEN);
5773 skb_reset_mac_header(skb);
5774 skb_reset_mac_len(skb);
5775
5776 return 0;
5777}
5778EXPORT_SYMBOL(skb_eth_pop);
5779
5780/**
5781 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5782 *
5783 * @skb: Socket buffer to modify
5784 * @dst: Destination MAC address of the new header
5785 * @src: Source MAC address of the new header
5786 *
5787 * Prepend @skb with a new Ethernet header.
5788 *
5789 * Expects that skb->data points to the mac header, which must be empty.
5790 *
5791 * Returns 0 on success, -errno otherwise.
5792 */
5793int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5794 const unsigned char *src)
5795{
5796 struct ethhdr *eth;
5797 int err;
5798
5799 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5800 return -EPROTO;
5801
5802 err = skb_cow_head(skb, sizeof(*eth));
5803 if (err < 0)
5804 return err;
5805
5806 skb_push(skb, sizeof(*eth));
5807 skb_reset_mac_header(skb);
5808 skb_reset_mac_len(skb);
5809
5810 eth = eth_hdr(skb);
5811 ether_addr_copy(eth->h_dest, dst);
5812 ether_addr_copy(eth->h_source, src);
5813 eth->h_proto = skb->protocol;
5814
5815 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5816
5817 return 0;
5818}
5819EXPORT_SYMBOL(skb_eth_push);
5820
John Hurley8822e272019-07-07 15:01:54 +01005821/* Update the ethertype of hdr and the skb csum value if required. */
5822static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5823 __be16 ethertype)
5824{
5825 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5826 __be16 diff[] = { ~hdr->h_proto, ethertype };
5827
5828 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5829 }
5830
5831 hdr->h_proto = ethertype;
5832}
5833
5834/**
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305835 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5836 * the packet
John Hurley8822e272019-07-07 15:01:54 +01005837 *
5838 * @skb: buffer
5839 * @mpls_lse: MPLS label stack entry to push
5840 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
Davide Carattifa4e0f82019-10-12 13:55:07 +02005841 * @mac_len: length of the MAC header
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305842 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5843 * ethernet
John Hurley8822e272019-07-07 15:01:54 +01005844 *
5845 * Expects skb->data at mac header.
5846 *
5847 * Returns 0 on success, -errno otherwise.
5848 */
Davide Carattifa4e0f82019-10-12 13:55:07 +02005849int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
Martin Varghesed04ac222019-12-05 05:57:22 +05305850 int mac_len, bool ethernet)
John Hurley8822e272019-07-07 15:01:54 +01005851{
5852 struct mpls_shim_hdr *lse;
5853 int err;
5854
5855 if (unlikely(!eth_p_mpls(mpls_proto)))
5856 return -EINVAL;
5857
5858 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5859 if (skb->encapsulation)
5860 return -EINVAL;
5861
5862 err = skb_cow_head(skb, MPLS_HLEN);
5863 if (unlikely(err))
5864 return err;
5865
5866 if (!skb->inner_protocol) {
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305867 skb_set_inner_network_header(skb, skb_network_offset(skb));
John Hurley8822e272019-07-07 15:01:54 +01005868 skb_set_inner_protocol(skb, skb->protocol);
5869 }
5870
5871 skb_push(skb, MPLS_HLEN);
5872 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005873 mac_len);
John Hurley8822e272019-07-07 15:01:54 +01005874 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005875 skb_set_network_header(skb, mac_len);
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305876 skb_reset_mac_len(skb);
John Hurley8822e272019-07-07 15:01:54 +01005877
5878 lse = mpls_hdr(skb);
5879 lse->label_stack_entry = mpls_lse;
5880 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5881
Guillaume Nault4296adc2020-10-02 21:53:08 +02005882 if (ethernet && mac_len >= ETH_HLEN)
John Hurley8822e272019-07-07 15:01:54 +01005883 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5884 skb->protocol = mpls_proto;
5885
5886 return 0;
5887}
5888EXPORT_SYMBOL_GPL(skb_mpls_push);
5889
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005890/**
John Hurleyed246ce2019-07-07 15:01:55 +01005891 * skb_mpls_pop() - pop the outermost MPLS header
5892 *
5893 * @skb: buffer
5894 * @next_proto: ethertype of header after popped MPLS header
Davide Carattifa4e0f82019-10-12 13:55:07 +02005895 * @mac_len: length of the MAC header
Martin Varghese76f99f92019-12-21 08:50:23 +05305896 * @ethernet: flag to indicate if the packet is ethernet
John Hurleyed246ce2019-07-07 15:01:55 +01005897 *
5898 * Expects skb->data at mac header.
5899 *
5900 * Returns 0 on success, -errno otherwise.
5901 */
Martin Varghese040b5cf2019-12-02 10:49:51 +05305902int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5903 bool ethernet)
John Hurleyed246ce2019-07-07 15:01:55 +01005904{
5905 int err;
5906
5907 if (unlikely(!eth_p_mpls(skb->protocol)))
Davide Carattidedc5a02019-10-12 13:55:06 +02005908 return 0;
John Hurleyed246ce2019-07-07 15:01:55 +01005909
Davide Carattifa4e0f82019-10-12 13:55:07 +02005910 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
John Hurleyed246ce2019-07-07 15:01:55 +01005911 if (unlikely(err))
5912 return err;
5913
5914 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5915 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005916 mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005917
5918 __skb_pull(skb, MPLS_HLEN);
5919 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005920 skb_set_network_header(skb, mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005921
Guillaume Nault4296adc2020-10-02 21:53:08 +02005922 if (ethernet && mac_len >= ETH_HLEN) {
John Hurleyed246ce2019-07-07 15:01:55 +01005923 struct ethhdr *hdr;
5924
5925 /* use mpls_hdr() to get ethertype to account for VLANs. */
5926 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5927 skb_mod_eth_type(skb, hdr, next_proto);
5928 }
5929 skb->protocol = next_proto;
5930
5931 return 0;
5932}
5933EXPORT_SYMBOL_GPL(skb_mpls_pop);
5934
5935/**
John Hurleyd27cf5c2019-07-07 15:01:56 +01005936 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5937 *
5938 * @skb: buffer
5939 * @mpls_lse: new MPLS label stack entry to update to
5940 *
5941 * Expects skb->data at mac header.
5942 *
5943 * Returns 0 on success, -errno otherwise.
5944 */
5945int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5946{
5947 int err;
5948
5949 if (unlikely(!eth_p_mpls(skb->protocol)))
5950 return -EINVAL;
5951
5952 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5953 if (unlikely(err))
5954 return err;
5955
5956 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5957 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5958
5959 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5960 }
5961
5962 mpls_hdr(skb)->label_stack_entry = mpls_lse;
5963
5964 return 0;
5965}
5966EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5967
5968/**
John Hurley2a2ea502019-07-07 15:01:57 +01005969 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
5970 *
5971 * @skb: buffer
5972 *
5973 * Expects skb->data at mac header.
5974 *
5975 * Returns 0 on success, -errno otherwise.
5976 */
5977int skb_mpls_dec_ttl(struct sk_buff *skb)
5978{
5979 u32 lse;
5980 u8 ttl;
5981
5982 if (unlikely(!eth_p_mpls(skb->protocol)))
5983 return -EINVAL;
5984
Davide Caratti13de4ed2020-12-03 10:58:21 +01005985 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
5986 return -ENOMEM;
5987
John Hurley2a2ea502019-07-07 15:01:57 +01005988 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
5989 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
5990 if (!--ttl)
5991 return -EINVAL;
5992
5993 lse &= ~MPLS_LS_TTL_MASK;
5994 lse |= ttl << MPLS_LS_TTL_SHIFT;
5995
5996 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
5997}
5998EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
5999
6000/**
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006001 * alloc_skb_with_frags - allocate skb with page frags
6002 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09006003 * @header_len: size of linear part
6004 * @data_len: needed length in frags
6005 * @max_page_order: max page order desired.
6006 * @errcode: pointer to error code if any
6007 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006008 *
6009 * This can be used to allocate a paged skb, given a maximal order for frags.
6010 */
6011struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6012 unsigned long data_len,
6013 int max_page_order,
6014 int *errcode,
6015 gfp_t gfp_mask)
6016{
6017 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
6018 unsigned long chunk;
6019 struct sk_buff *skb;
6020 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006021 int i;
6022
6023 *errcode = -EMSGSIZE;
6024 /* Note this test could be relaxed, if we succeed to allocate
6025 * high order pages...
6026 */
6027 if (npages > MAX_SKB_FRAGS)
6028 return NULL;
6029
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006030 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08006031 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006032 if (!skb)
6033 return NULL;
6034
6035 skb->truesize += npages << PAGE_SHIFT;
6036
6037 for (i = 0; npages > 0; i++) {
6038 int order = max_page_order;
6039
6040 while (order) {
6041 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08006042 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006043 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02006044 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006045 order);
6046 if (page)
6047 goto fill_page;
6048 /* Do not retry other high order allocations */
6049 order = 1;
6050 max_page_order = 0;
6051 }
6052 order--;
6053 }
6054 page = alloc_page(gfp_mask);
6055 if (!page)
6056 goto failure;
6057fill_page:
6058 chunk = min_t(unsigned long, data_len,
6059 PAGE_SIZE << order);
6060 skb_fill_page_desc(skb, i, page, 0, chunk);
6061 data_len -= chunk;
6062 npages -= 1 << order;
6063 }
6064 return skb;
6065
6066failure:
6067 kfree_skb(skb);
6068 return NULL;
6069}
6070EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006071
6072/* carve out the first off bytes from skb when off < headlen */
6073static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6074 const int headlen, gfp_t gfp_mask)
6075{
6076 int i;
6077 int size = skb_end_offset(skb);
6078 int new_hlen = headlen - off;
6079 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006080
6081 size = SKB_DATA_ALIGN(size);
6082
6083 if (skb_pfmemalloc(skb))
6084 gfp_mask |= __GFP_MEMALLOC;
6085 data = kmalloc_reserve(size +
6086 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6087 gfp_mask, NUMA_NO_NODE, NULL);
6088 if (!data)
6089 return -ENOMEM;
6090
6091 size = SKB_WITH_OVERHEAD(ksize(data));
6092
6093 /* Copy real data, and all frags */
6094 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6095 skb->len -= off;
6096
6097 memcpy((struct skb_shared_info *)(data + size),
6098 skb_shinfo(skb),
6099 offsetof(struct skb_shared_info,
6100 frags[skb_shinfo(skb)->nr_frags]));
6101 if (skb_cloned(skb)) {
6102 /* drop the old head gracefully */
6103 if (skb_orphan_frags(skb, gfp_mask)) {
6104 kfree(data);
6105 return -ENOMEM;
6106 }
6107 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6108 skb_frag_ref(skb, i);
6109 if (skb_has_frag_list(skb))
6110 skb_clone_fraglist(skb);
6111 skb_release_data(skb);
6112 } else {
6113 /* we can reuse existing recount- all we did was
6114 * relocate values
6115 */
6116 skb_free_head(skb);
6117 }
6118
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006119 skb->head = data;
6120 skb->data = data;
6121 skb->head_frag = 0;
6122#ifdef NET_SKBUFF_DATA_USES_OFFSET
6123 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006124#else
6125 skb->end = skb->head + size;
6126#endif
6127 skb_set_tail_pointer(skb, skb_headlen(skb));
6128 skb_headers_offset_update(skb, 0);
6129 skb->cloned = 0;
6130 skb->hdr_len = 0;
6131 skb->nohdr = 0;
6132 atomic_set(&skb_shinfo(skb)->dataref, 1);
6133
6134 return 0;
6135}
6136
6137static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6138
6139/* carve out the first eat bytes from skb's frag_list. May recurse into
6140 * pskb_carve()
6141 */
6142static int pskb_carve_frag_list(struct sk_buff *skb,
6143 struct skb_shared_info *shinfo, int eat,
6144 gfp_t gfp_mask)
6145{
6146 struct sk_buff *list = shinfo->frag_list;
6147 struct sk_buff *clone = NULL;
6148 struct sk_buff *insp = NULL;
6149
6150 do {
6151 if (!list) {
6152 pr_err("Not enough bytes to eat. Want %d\n", eat);
6153 return -EFAULT;
6154 }
6155 if (list->len <= eat) {
6156 /* Eaten as whole. */
6157 eat -= list->len;
6158 list = list->next;
6159 insp = list;
6160 } else {
6161 /* Eaten partially. */
6162 if (skb_shared(list)) {
6163 clone = skb_clone(list, gfp_mask);
6164 if (!clone)
6165 return -ENOMEM;
6166 insp = list->next;
6167 list = clone;
6168 } else {
6169 /* This may be pulled without problems. */
6170 insp = list;
6171 }
6172 if (pskb_carve(list, eat, gfp_mask) < 0) {
6173 kfree_skb(clone);
6174 return -ENOMEM;
6175 }
6176 break;
6177 }
6178 } while (eat);
6179
6180 /* Free pulled out fragments. */
6181 while ((list = shinfo->frag_list) != insp) {
6182 shinfo->frag_list = list->next;
6183 kfree_skb(list);
6184 }
6185 /* And insert new clone at head. */
6186 if (clone) {
6187 clone->next = list;
6188 shinfo->frag_list = clone;
6189 }
6190 return 0;
6191}
6192
6193/* carve off first len bytes from skb. Split line (off) is in the
6194 * non-linear part of skb
6195 */
6196static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6197 int pos, gfp_t gfp_mask)
6198{
6199 int i, k = 0;
6200 int size = skb_end_offset(skb);
6201 u8 *data;
6202 const int nfrags = skb_shinfo(skb)->nr_frags;
6203 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006204
6205 size = SKB_DATA_ALIGN(size);
6206
6207 if (skb_pfmemalloc(skb))
6208 gfp_mask |= __GFP_MEMALLOC;
6209 data = kmalloc_reserve(size +
6210 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6211 gfp_mask, NUMA_NO_NODE, NULL);
6212 if (!data)
6213 return -ENOMEM;
6214
6215 size = SKB_WITH_OVERHEAD(ksize(data));
6216
6217 memcpy((struct skb_shared_info *)(data + size),
Miaohe Line3ec1e82020-08-15 04:48:53 -04006218 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006219 if (skb_orphan_frags(skb, gfp_mask)) {
6220 kfree(data);
6221 return -ENOMEM;
6222 }
6223 shinfo = (struct skb_shared_info *)(data + size);
6224 for (i = 0; i < nfrags; i++) {
6225 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6226
6227 if (pos + fsize > off) {
6228 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6229
6230 if (pos < off) {
6231 /* Split frag.
6232 * We have two variants in this case:
6233 * 1. Move all the frag to the second
6234 * part, if it is possible. F.e.
6235 * this approach is mandatory for TUX,
6236 * where splitting is expensive.
6237 * 2. Split is accurately. We make this.
6238 */
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006239 skb_frag_off_add(&shinfo->frags[0], off - pos);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006240 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6241 }
6242 skb_frag_ref(skb, i);
6243 k++;
6244 }
6245 pos += fsize;
6246 }
6247 shinfo->nr_frags = k;
6248 if (skb_has_frag_list(skb))
6249 skb_clone_fraglist(skb);
6250
Miaohe Lineabe8612020-08-15 04:46:41 -04006251 /* split line is in frag list */
6252 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6253 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6254 if (skb_has_frag_list(skb))
6255 kfree_skb_list(skb_shinfo(skb)->frag_list);
6256 kfree(data);
6257 return -ENOMEM;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006258 }
6259 skb_release_data(skb);
6260
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006261 skb->head = data;
6262 skb->head_frag = 0;
6263 skb->data = data;
6264#ifdef NET_SKBUFF_DATA_USES_OFFSET
6265 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006266#else
6267 skb->end = skb->head + size;
6268#endif
6269 skb_reset_tail_pointer(skb);
6270 skb_headers_offset_update(skb, 0);
6271 skb->cloned = 0;
6272 skb->hdr_len = 0;
6273 skb->nohdr = 0;
6274 skb->len -= off;
6275 skb->data_len = skb->len;
6276 atomic_set(&skb_shinfo(skb)->dataref, 1);
6277 return 0;
6278}
6279
6280/* remove len bytes from the beginning of the skb */
6281static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6282{
6283 int headlen = skb_headlen(skb);
6284
6285 if (len < headlen)
6286 return pskb_carve_inside_header(skb, len, headlen, gfp);
6287 else
6288 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6289}
6290
6291/* Extract to_copy bytes starting at off from skb, and return this in
6292 * a new skb
6293 */
6294struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6295 int to_copy, gfp_t gfp)
6296{
6297 struct sk_buff *clone = skb_clone(skb, gfp);
6298
6299 if (!clone)
6300 return NULL;
6301
6302 if (pskb_carve(clone, off, gfp) < 0 ||
6303 pskb_trim(clone, to_copy)) {
6304 kfree_skb(clone);
6305 return NULL;
6306 }
6307 return clone;
6308}
6309EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006310
6311/**
6312 * skb_condense - try to get rid of fragments/frag_list if possible
6313 * @skb: buffer
6314 *
6315 * Can be used to save memory before skb is added to a busy queue.
6316 * If packet has bytes in frags and enough tail room in skb->head,
6317 * pull all of them, so that we can free the frags right now and adjust
6318 * truesize.
6319 * Notes:
6320 * We do not reallocate skb->head thus can not fail.
6321 * Caller must re-evaluate skb->truesize if needed.
6322 */
6323void skb_condense(struct sk_buff *skb)
6324{
Eric Dumazet3174fed2016-12-09 08:02:05 -08006325 if (skb->data_len) {
6326 if (skb->data_len > skb->end - skb->tail ||
6327 skb_cloned(skb))
6328 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006329
Eric Dumazet3174fed2016-12-09 08:02:05 -08006330 /* Nice, we can free page frag(s) right now */
6331 __pskb_pull_tail(skb, skb->data_len);
6332 }
6333 /* At this point, skb->truesize might be over estimated,
6334 * because skb had a fragment, and fragments do not tell
6335 * their truesize.
6336 * When we pulled its content into skb->head, fragment
6337 * was freed, but __pskb_pull_tail() could not possibly
6338 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006339 */
6340 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6341}
Florian Westphaldf5042f2018-12-18 17:15:16 +01006342
6343#ifdef CONFIG_SKB_EXTENSIONS
6344static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6345{
6346 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6347}
6348
Paolo Abeni8b69a802020-01-09 07:59:24 -08006349/**
6350 * __skb_ext_alloc - allocate a new skb extensions storage
6351 *
Florian Westphal4930f482020-05-16 10:46:23 +02006352 * @flags: See kmalloc().
6353 *
Paolo Abeni8b69a802020-01-09 07:59:24 -08006354 * Returns the newly allocated pointer. The pointer can later attached to a
6355 * skb via __skb_ext_set().
6356 * Note: caller must handle the skb_ext as an opaque data.
6357 */
Florian Westphal4930f482020-05-16 10:46:23 +02006358struct skb_ext *__skb_ext_alloc(gfp_t flags)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006359{
Florian Westphal4930f482020-05-16 10:46:23 +02006360 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006361
6362 if (new) {
6363 memset(new->offset, 0, sizeof(new->offset));
6364 refcount_set(&new->refcnt, 1);
6365 }
6366
6367 return new;
6368}
6369
Florian Westphal41650792018-12-18 17:15:27 +01006370static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6371 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006372{
6373 struct skb_ext *new;
6374
6375 if (refcount_read(&old->refcnt) == 1)
6376 return old;
6377
6378 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6379 if (!new)
6380 return NULL;
6381
6382 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6383 refcount_set(&new->refcnt, 1);
6384
Florian Westphal41650792018-12-18 17:15:27 +01006385#ifdef CONFIG_XFRM
6386 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6387 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6388 unsigned int i;
6389
6390 for (i = 0; i < sp->len; i++)
6391 xfrm_state_hold(sp->xvec[i]);
6392 }
6393#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006394 __skb_ext_put(old);
6395 return new;
6396}
6397
6398/**
Paolo Abeni8b69a802020-01-09 07:59:24 -08006399 * __skb_ext_set - attach the specified extension storage to this skb
6400 * @skb: buffer
6401 * @id: extension id
6402 * @ext: extension storage previously allocated via __skb_ext_alloc()
6403 *
6404 * Existing extensions, if any, are cleared.
6405 *
6406 * Returns the pointer to the extension.
6407 */
6408void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6409 struct skb_ext *ext)
6410{
6411 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6412
6413 skb_ext_put(skb);
6414 newlen = newoff + skb_ext_type_len[id];
6415 ext->chunks = newlen;
6416 ext->offset[id] = newoff;
6417 skb->extensions = ext;
6418 skb->active_extensions = 1 << id;
6419 return skb_ext_get_ptr(ext, id);
6420}
6421
6422/**
Florian Westphaldf5042f2018-12-18 17:15:16 +01006423 * skb_ext_add - allocate space for given extension, COW if needed
6424 * @skb: buffer
6425 * @id: extension to allocate space for
6426 *
6427 * Allocates enough space for the given extension.
6428 * If the extension is already present, a pointer to that extension
6429 * is returned.
6430 *
6431 * If the skb was cloned, COW applies and the returned memory can be
6432 * modified without changing the extension space of clones buffers.
6433 *
6434 * Returns pointer to the extension or NULL on allocation failure.
6435 */
6436void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6437{
6438 struct skb_ext *new, *old = NULL;
6439 unsigned int newlen, newoff;
6440
6441 if (skb->active_extensions) {
6442 old = skb->extensions;
6443
Florian Westphal41650792018-12-18 17:15:27 +01006444 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006445 if (!new)
6446 return NULL;
6447
Paolo Abeni682ec852018-12-21 19:03:15 +01006448 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01006449 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006450
Paolo Abenie94e50b2018-12-21 19:03:13 +01006451 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006452 } else {
6453 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6454
Florian Westphal4930f482020-05-16 10:46:23 +02006455 new = __skb_ext_alloc(GFP_ATOMIC);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006456 if (!new)
6457 return NULL;
6458 }
6459
6460 newlen = newoff + skb_ext_type_len[id];
6461 new->chunks = newlen;
6462 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006463set_active:
Paolo Abenib0999f32021-07-28 18:24:01 +02006464 skb->slow_gro = 1;
Paolo Abeni682ec852018-12-21 19:03:15 +01006465 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006466 skb->active_extensions |= 1 << id;
6467 return skb_ext_get_ptr(new, id);
6468}
6469EXPORT_SYMBOL(skb_ext_add);
6470
Florian Westphal41650792018-12-18 17:15:27 +01006471#ifdef CONFIG_XFRM
6472static void skb_ext_put_sp(struct sec_path *sp)
6473{
6474 unsigned int i;
6475
6476 for (i = 0; i < sp->len; i++)
6477 xfrm_state_put(sp->xvec[i]);
6478}
6479#endif
6480
Florian Westphaldf5042f2018-12-18 17:15:16 +01006481void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6482{
6483 struct skb_ext *ext = skb->extensions;
6484
6485 skb->active_extensions &= ~(1 << id);
6486 if (skb->active_extensions == 0) {
6487 skb->extensions = NULL;
6488 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01006489#ifdef CONFIG_XFRM
6490 } else if (id == SKB_EXT_SEC_PATH &&
6491 refcount_read(&ext->refcnt) == 1) {
6492 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6493
6494 skb_ext_put_sp(sp);
6495 sp->len = 0;
6496#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006497 }
6498}
6499EXPORT_SYMBOL(__skb_ext_del);
6500
6501void __skb_ext_put(struct skb_ext *ext)
6502{
6503 /* If this is last clone, nothing can increment
6504 * it after check passes. Avoids one atomic op.
6505 */
6506 if (refcount_read(&ext->refcnt) == 1)
6507 goto free_now;
6508
6509 if (!refcount_dec_and_test(&ext->refcnt))
6510 return;
6511free_now:
Florian Westphal41650792018-12-18 17:15:27 +01006512#ifdef CONFIG_XFRM
6513 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6514 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6515#endif
6516
Florian Westphaldf5042f2018-12-18 17:15:16 +01006517 kmem_cache_free(skbuff_ext_cache, ext);
6518}
6519EXPORT_SYMBOL(__skb_ext_put);
6520#endif /* CONFIG_SKB_EXTENSIONS */