blob: f9311762cc475bd38d87c33e988d7c983b902e56 [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);
Changbin Duafa79d02021-08-13 22:57:49 +0800159 if (in_hardirq() || irqs_disabled()) {
Alexander Lobakin50fad4b52021-02-13 14:12:13 +0000160 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
Changbin Duafa79d02021-08-13 22:57:49 +0800505 if (in_hardirq() || irqs_disabled()) {
Sebastian Andrzej Siewior92dcabd2019-06-07 21:20:35 +0200506 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) {
Changbin Duafa79d02021-08-13 22:57:49 +0800727 WARN_ON(in_hardirq());
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/**
Vasily Averinf1260ff2021-08-02 11:52:15 +03001793 * skb_expand_head - reallocate header of &sk_buff
1794 * @skb: buffer to reallocate
1795 * @headroom: needed headroom
1796 *
1797 * Unlike skb_realloc_headroom, this one does not allocate a new skb
1798 * if possible; copies skb->sk to new skb as needed
1799 * and frees original skb in case of failures.
1800 *
1801 * It expect increased headroom and generates warning otherwise.
1802 */
1803
1804struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
1805{
1806 int delta = headroom - skb_headroom(skb);
1807
1808 if (WARN_ONCE(delta <= 0,
1809 "%s is expecting an increase in the headroom", __func__))
1810 return skb;
1811
1812 /* pskb_expand_head() might crash, if skb is shared */
1813 if (skb_shared(skb)) {
1814 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1815
1816 if (likely(nskb)) {
1817 if (skb->sk)
1818 skb_set_owner_w(nskb, skb->sk);
1819 consume_skb(skb);
1820 } else {
1821 kfree_skb(skb);
1822 }
1823 skb = nskb;
1824 }
1825 if (skb &&
1826 pskb_expand_head(skb, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
1827 kfree_skb(skb);
1828 skb = NULL;
1829 }
1830 return skb;
1831}
1832EXPORT_SYMBOL(skb_expand_head);
1833
1834/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 * skb_copy_expand - copy and expand sk_buff
1836 * @skb: buffer to copy
1837 * @newheadroom: new free bytes at head
1838 * @newtailroom: new free bytes at tail
1839 * @gfp_mask: allocation priority
1840 *
1841 * Make a copy of both an &sk_buff and its data and while doing so
1842 * allocate additional space.
1843 *
1844 * This is used when the caller wishes to modify the data and needs a
1845 * private copy of the data to alter as well as more space for new fields.
1846 * Returns %NULL on failure or the pointer to the buffer
1847 * on success. The returned buffer has a reference count of 1.
1848 *
1849 * You must pass %GFP_ATOMIC as the allocation priority if this function
1850 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 */
1852struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -07001853 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +01001854 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 /*
1857 * Allocate the copy buffer
1858 */
Mel Gormanc93bdd02012-07-31 16:44:19 -07001859 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1860 gfp_mask, skb_alloc_rx_flag(skb),
1861 NUMA_NO_NODE);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001862 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 int head_copy_len, head_copy_off;
1864
1865 if (!n)
1866 return NULL;
1867
1868 skb_reserve(n, newheadroom);
1869
1870 /* Set the tail pointer and length */
1871 skb_put(n, skb->len);
1872
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001873 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 head_copy_off = 0;
1875 if (newheadroom <= head_copy_len)
1876 head_copy_len = newheadroom;
1877 else
1878 head_copy_off = newheadroom - head_copy_len;
1879
1880 /* Copy the linear header and data. */
Tim Hansen9f77fad2017-10-09 11:37:59 -04001881 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1882 skb->len + head_copy_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Ilya Lesokhin08303c12018-04-30 10:16:11 +03001884 skb_copy_header(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Eric Dumazet030737b2013-10-19 11:42:54 -07001886 skb_headers_offset_update(n, newheadroom - oldheadroom);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -07001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return n;
1889}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001890EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892/**
Florian Fainellicd0a1372017-08-22 15:12:14 -07001893 * __skb_pad - zero pad the tail of an skb
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * @skb: buffer to pad
1895 * @pad: space to pad
Florian Fainellicd0a1372017-08-22 15:12:14 -07001896 * @free_on_error: free buffer on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 * Ensure that a buffer is followed by a padding area that is zero
1899 * filled. Used by network drivers which may DMA or transfer data
1900 * beyond the buffer end onto the wire.
1901 *
Florian Fainellicd0a1372017-08-22 15:12:14 -07001902 * May return error in out of memory cases. The skb is freed on error
1903 * if @free_on_error is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001905
Florian Fainellicd0a1372017-08-22 15:12:14 -07001906int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Herbert Xu5b057c62006-06-23 02:06:41 -07001908 int err;
1909 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -07001912 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -07001914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Herbert Xu5b057c62006-06-23 02:06:41 -07001916
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001917 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -07001918 if (likely(skb_cloned(skb) || ntail > 0)) {
1919 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1920 if (unlikely(err))
1921 goto free_skb;
1922 }
1923
1924 /* FIXME: The use of this function with non-linear skb's really needs
1925 * to be audited.
1926 */
1927 err = skb_linearize(skb);
1928 if (unlikely(err))
1929 goto free_skb;
1930
1931 memset(skb->data + skb->len, 0, pad);
1932 return 0;
1933
1934free_skb:
Florian Fainellicd0a1372017-08-22 15:12:14 -07001935 if (free_on_error)
1936 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -07001937 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001938}
Florian Fainellicd0a1372017-08-22 15:12:14 -07001939EXPORT_SYMBOL(__skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001940
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001941/**
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001942 * pskb_put - add data to the tail of a potentially fragmented buffer
1943 * @skb: start of the buffer to use
1944 * @tail: tail fragment of the buffer to use
1945 * @len: amount of data to add
1946 *
1947 * This function extends the used data area of the potentially
1948 * fragmented buffer. @tail must be the last fragment of @skb -- or
1949 * @skb itself. If this would exceed the total buffer size the kernel
1950 * will panic. A pointer to the first byte of the extra data is
1951 * returned.
1952 */
1953
Johannes Berg4df864c2017-06-16 14:29:21 +02001954void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
Mathias Krause0c7ddf32013-11-07 14:18:24 +01001955{
1956 if (tail != skb) {
1957 skb->data_len += len;
1958 skb->len += len;
1959 }
1960 return skb_put(tail, len);
1961}
1962EXPORT_SYMBOL_GPL(pskb_put);
1963
1964/**
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001965 * skb_put - add data to a buffer
1966 * @skb: buffer to use
1967 * @len: amount of data to add
1968 *
1969 * This function extends the used data area of the buffer. If this would
1970 * exceed the total buffer size the kernel will panic. A pointer to the
1971 * first byte of the extra data is returned.
1972 */
Johannes Berg4df864c2017-06-16 14:29:21 +02001973void *skb_put(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001974{
Johannes Berg4df864c2017-06-16 14:29:21 +02001975 void *tmp = skb_tail_pointer(skb);
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -07001976 SKB_LINEAR_ASSERT(skb);
1977 skb->tail += len;
1978 skb->len += len;
1979 if (unlikely(skb->tail > skb->end))
1980 skb_over_panic(skb, len, __builtin_return_address(0));
1981 return tmp;
1982}
1983EXPORT_SYMBOL(skb_put);
1984
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001985/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001986 * skb_push - add data to the start of a buffer
1987 * @skb: buffer to use
1988 * @len: amount of data to add
1989 *
1990 * This function extends the used data area of the buffer at the buffer
1991 * start. If this would exceed the total buffer headroom the kernel will
1992 * panic. A pointer to the first byte of the extra data is returned.
1993 */
Johannes Bergd58ff352017-06-16 14:29:23 +02001994void *skb_push(struct sk_buff *skb, unsigned int len)
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001995{
1996 skb->data -= len;
1997 skb->len += len;
Ganesh Goudar9aba2f82018-08-02 15:34:52 +05301998 if (unlikely(skb->data < skb->head))
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001999 skb_under_panic(skb, len, __builtin_return_address(0));
2000 return skb->data;
2001}
2002EXPORT_SYMBOL(skb_push);
2003
2004/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07002005 * skb_pull - remove data from the start of a buffer
2006 * @skb: buffer to use
2007 * @len: amount of data to remove
2008 *
2009 * This function removes data from the start of a buffer, returning
2010 * the memory to the headroom. A pointer to the next data in the buffer
2011 * is returned. Once the data has been pulled future pushes will overwrite
2012 * the old data.
2013 */
Johannes Bergaf728682017-06-16 14:29:22 +02002014void *skb_pull(struct sk_buff *skb, unsigned int len)
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07002015{
David S. Miller47d29642010-05-02 02:21:44 -07002016 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07002017}
2018EXPORT_SYMBOL(skb_pull);
2019
Ilpo Järvinen419ae742008-03-27 17:54:01 -07002020/**
2021 * skb_trim - remove end from a buffer
2022 * @skb: buffer to alter
2023 * @len: new length
2024 *
2025 * Cut the length of a buffer down by removing data from the tail. If
2026 * the buffer is already under the length specified it is not modified.
2027 * The skb must be linear.
2028 */
2029void skb_trim(struct sk_buff *skb, unsigned int len)
2030{
2031 if (skb->len > len)
2032 __skb_trim(skb, len);
2033}
2034EXPORT_SYMBOL(skb_trim);
2035
Herbert Xu3cc0e872006-06-09 16:13:38 -07002036/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038
Herbert Xu3cc0e872006-06-09 16:13:38 -07002039int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Herbert Xu27b437c2006-07-13 19:26:39 -07002041 struct sk_buff **fragp;
2042 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int offset = skb_headlen(skb);
2044 int nfrags = skb_shinfo(skb)->nr_frags;
2045 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07002046 int err;
2047
2048 if (skb_cloned(skb) &&
2049 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002052 i = 0;
2053 if (offset >= len)
2054 goto drop_pages;
2055
2056 for (; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002057 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Herbert Xu27b437c2006-07-13 19:26:39 -07002058
2059 if (end < len) {
2060 offset = end;
2061 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Herbert Xu27b437c2006-07-13 19:26:39 -07002063
Eric Dumazet9e903e02011-10-18 21:00:24 +00002064 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
Herbert Xu27b437c2006-07-13 19:26:39 -07002065
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002066drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07002067 skb_shinfo(skb)->nr_frags = i;
2068
2069 for (; i < nfrags; i++)
Ian Campbellea2ab692011-08-22 23:44:58 +00002070 skb_frag_unref(skb, i);
Herbert Xu27b437c2006-07-13 19:26:39 -07002071
David S. Miller21dc3302010-08-23 00:13:46 -07002072 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07002073 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002074 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076
Herbert Xu27b437c2006-07-13 19:26:39 -07002077 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2078 fragp = &frag->next) {
2079 int end = offset + frag->len;
2080
2081 if (skb_shared(frag)) {
2082 struct sk_buff *nfrag;
2083
2084 nfrag = skb_clone(frag, GFP_ATOMIC);
2085 if (unlikely(!nfrag))
2086 return -ENOMEM;
2087
2088 nfrag->next = frag->next;
Eric Dumazet85bb2a62012-04-19 02:24:53 +00002089 consume_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07002090 frag = nfrag;
2091 *fragp = frag;
2092 }
2093
2094 if (end < len) {
2095 offset = end;
2096 continue;
2097 }
2098
2099 if (end > len &&
2100 unlikely((err = pskb_trim(frag, len - offset))))
2101 return err;
2102
2103 if (frag->next)
2104 skb_drop_list(&frag->next);
2105 break;
2106 }
2107
Herbert Xuf4d26fb2006-07-30 20:20:28 -07002108done:
Herbert Xu27b437c2006-07-13 19:26:39 -07002109 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 skb->data_len -= skb->len - len;
2111 skb->len = len;
2112 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07002113 skb->len = len;
2114 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002115 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
Eric Dumazetc21b48c2017-04-26 09:07:46 -07002118 if (!skb->sk || skb->destructor == sock_edemux)
2119 skb_condense(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return 0;
2121}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002122EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Eric Dumazet88078d92018-04-18 11:43:15 -07002124/* Note : use pskb_trim_rcsum() instead of calling this directly
2125 */
2126int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2127{
2128 if (skb->ip_summed == CHECKSUM_COMPLETE) {
2129 int delta = skb->len - len;
2130
Dimitris Michailidisd55bef502018-10-19 17:07:13 -07002131 skb->csum = csum_block_sub(skb->csum,
2132 skb_checksum(skb, len, delta, 0),
2133 len);
Vasily Averin54970a22020-12-14 22:07:39 +03002134 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2135 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2136 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2137
2138 if (offset + sizeof(__sum16) > hdlen)
2139 return -EINVAL;
Eric Dumazet88078d92018-04-18 11:43:15 -07002140 }
2141 return __pskb_trim(skb, len);
2142}
2143EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/**
2146 * __pskb_pull_tail - advance tail of skb header
2147 * @skb: buffer to reallocate
2148 * @delta: number of bytes to advance tail
2149 *
2150 * The function makes a sense only on a fragmented &sk_buff,
2151 * it expands header moving its tail forward and copying necessary
2152 * data from fragmented part.
2153 *
2154 * &sk_buff MUST have reference count of 1.
2155 *
2156 * Returns %NULL (and &sk_buff does not change) if pull failed
2157 * or value of new tail of skb in the case of success.
2158 *
2159 * All the pointers pointing into skb header may change and must be
2160 * reloaded after call to this function.
2161 */
2162
2163/* Moves tail of skb head forward, copying data from fragmented part,
2164 * when it is necessary.
2165 * 1. It may fail due to malloc failure.
2166 * 2. It may change skb pointers.
2167 *
2168 * It is pretty complicated. Luckily, it is called only in exceptional cases.
2169 */
Johannes Bergaf728682017-06-16 14:29:22 +02002170void *__pskb_pull_tail(struct sk_buff *skb, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 /* If skb has not enough free space at tail, get new one
2173 * plus 128 bytes for future expansions. If we have enough
2174 * room at tail, reallocate without expansion only if skb is cloned.
2175 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07002176 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 if (eat > 0 || skb_cloned(skb)) {
2179 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2180 GFP_ATOMIC))
2181 return NULL;
2182 }
2183
Tim Hansen9f77fad2017-10-09 11:37:59 -04002184 BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2185 skb_tail_pointer(skb), delta));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* Optimization: no fragments, no reasons to preestimate
2188 * size of pulled pages. Superb.
2189 */
David S. Miller21dc3302010-08-23 00:13:46 -07002190 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto pull_pages;
2192
2193 /* Estimate size of pulled pages. */
2194 eat = delta;
2195 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002196 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2197
2198 if (size >= eat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 goto pull_pages;
Eric Dumazet9e903e02011-10-18 21:00:24 +00002200 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
2203 /* If we need update frag list, we are in troubles.
Wenhua Shi09001b02017-10-14 18:51:36 +02002204 * Certainly, it is possible to add an offset to skb data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 * but taking into account that pulling is expected to
2206 * be very rare operation, it is worth to fight against
2207 * further bloating skb head and crucify ourselves here instead.
2208 * Pure masohism, indeed. 8)8)
2209 */
2210 if (eat) {
2211 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2212 struct sk_buff *clone = NULL;
2213 struct sk_buff *insp = NULL;
2214
2215 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (list->len <= eat) {
2217 /* Eaten as whole. */
2218 eat -= list->len;
2219 list = list->next;
2220 insp = list;
2221 } else {
2222 /* Eaten partially. */
2223
2224 if (skb_shared(list)) {
2225 /* Sucks! We need to fork list. :-( */
2226 clone = skb_clone(list, GFP_ATOMIC);
2227 if (!clone)
2228 return NULL;
2229 insp = list->next;
2230 list = clone;
2231 } else {
2232 /* This may be pulled without
2233 * problems. */
2234 insp = list;
2235 }
2236 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00002237 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return NULL;
2239 }
2240 break;
2241 }
2242 } while (eat);
2243
2244 /* Free pulled out fragments. */
2245 while ((list = skb_shinfo(skb)->frag_list) != insp) {
2246 skb_shinfo(skb)->frag_list = list->next;
2247 kfree_skb(list);
2248 }
2249 /* And insert new clone at head. */
2250 if (clone) {
2251 clone->next = list;
2252 skb_shinfo(skb)->frag_list = clone;
2253 }
2254 }
2255 /* Success! Now we may commit changes to skb data. */
2256
2257pull_pages:
2258 eat = delta;
2259 k = 0;
2260 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002261 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2262
2263 if (size <= eat) {
Ian Campbellea2ab692011-08-22 23:44:58 +00002264 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002265 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 } else {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002267 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2268
2269 *frag = skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (eat) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002271 skb_frag_off_add(frag, eat);
2272 skb_frag_size_sub(frag, eat);
linzhang3ccc6c62017-07-17 17:25:02 +08002273 if (!i)
2274 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 eat = 0;
2276 }
2277 k++;
2278 }
2279 }
2280 skb_shinfo(skb)->nr_frags = k;
2281
linzhang3ccc6c62017-07-17 17:25:02 +08002282end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 skb->tail += delta;
2284 skb->data_len -= delta;
2285
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04002286 if (!skb->data_len)
2287 skb_zcopy_clear(skb, false);
2288
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002289 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002291EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Eric Dumazet22019b12011-07-29 18:37:31 +00002293/**
2294 * skb_copy_bits - copy bits from skb to kernel buffer
2295 * @skb: source skb
2296 * @offset: offset in source
2297 * @to: destination buffer
2298 * @len: number of bytes to copy
2299 *
2300 * Copy the specified number of bytes from the source skb to the
2301 * destination buffer.
2302 *
2303 * CAUTION ! :
2304 * If its prototype is ever changed,
2305 * check arch/{*}/net/{*}.S files,
2306 * since it is called from BPF assembly code.
2307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2309{
David S. Miller1a028e52007-04-27 15:21:23 -07002310 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002311 struct sk_buff *frag_iter;
2312 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 if (offset > (int)skb->len - len)
2315 goto fault;
2316
2317 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07002318 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (copy > len)
2320 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002321 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if ((len -= copy) == 0)
2323 return 0;
2324 offset += copy;
2325 to += copy;
2326 }
2327
2328 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002329 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002330 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002332 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002333
Eric Dumazet51c56b02012-04-05 11:35:15 +02002334 end = start + skb_frag_size(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002336 u32 p_off, p_len, copied;
2337 struct page *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 u8 *vaddr;
2339
2340 if (copy > len)
2341 copy = len;
2342
Willem de Bruijnc613c202017-07-31 08:15:47 -04002343 skb_frag_foreach_page(f,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002344 skb_frag_off(f) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002345 copy, p, p_off, p_len, copied) {
2346 vaddr = kmap_atomic(p);
2347 memcpy(to + copied, vaddr + p_off, p_len);
2348 kunmap_atomic(vaddr);
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 if ((len -= copy) == 0)
2352 return 0;
2353 offset += copy;
2354 to += copy;
2355 }
David S. Miller1a028e52007-04-27 15:21:23 -07002356 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358
David S. Millerfbb398a2009-06-09 00:18:59 -07002359 skb_walk_frags(skb, frag_iter) {
2360 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
David S. Millerfbb398a2009-06-09 00:18:59 -07002362 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
David S. Millerfbb398a2009-06-09 00:18:59 -07002364 end = start + frag_iter->len;
2365 if ((copy = end - offset) > 0) {
2366 if (copy > len)
2367 copy = len;
2368 if (skb_copy_bits(frag_iter, offset - start, to, copy))
2369 goto fault;
2370 if ((len -= copy) == 0)
2371 return 0;
2372 offset += copy;
2373 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002375 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Shirley Maa6686f22011-07-06 12:22:12 +00002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (!len)
2379 return 0;
2380
2381fault:
2382 return -EFAULT;
2383}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002384EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Jens Axboe9c55e012007-11-06 23:30:13 -08002386/*
2387 * Callback from splice_to_pipe(), if we need to release some pages
2388 * at the end of the spd in case we error'ed out in filling the pipe.
2389 */
2390static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2391{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002392 put_page(spd->pages[i]);
2393}
Jens Axboe9c55e012007-11-06 23:30:13 -08002394
David S. Millera108d5f2012-04-23 23:06:11 -04002395static struct page *linear_to_page(struct page *page, unsigned int *len,
2396 unsigned int *offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002397 struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002398{
Eric Dumazet5640f762012-09-23 23:04:42 +00002399 struct page_frag *pfrag = sk_page_frag(sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002400
Eric Dumazet5640f762012-09-23 23:04:42 +00002401 if (!sk_page_frag_refill(sk, pfrag))
2402 return NULL;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002403
Eric Dumazet5640f762012-09-23 23:04:42 +00002404 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002405
Eric Dumazet5640f762012-09-23 23:04:42 +00002406 memcpy(page_address(pfrag->page) + pfrag->offset,
2407 page_address(page) + *offset, *len);
2408 *offset = pfrag->offset;
2409 pfrag->offset += *len;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002410
Eric Dumazet5640f762012-09-23 23:04:42 +00002411 return pfrag->page;
Jens Axboe9c55e012007-11-06 23:30:13 -08002412}
2413
Eric Dumazet41c73a02012-04-22 12:26:16 +00002414static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2415 struct page *page,
2416 unsigned int offset)
2417{
2418 return spd->nr_pages &&
2419 spd->pages[spd->nr_pages - 1] == page &&
2420 (spd->partial[spd->nr_pages - 1].offset +
2421 spd->partial[spd->nr_pages - 1].len == offset);
2422}
2423
Jens Axboe9c55e012007-11-06 23:30:13 -08002424/*
2425 * Fill page/offset/length into spd, if it can hold more pages.
2426 */
David S. Millera108d5f2012-04-23 23:06:11 -04002427static bool spd_fill_page(struct splice_pipe_desc *spd,
2428 struct pipe_inode_info *pipe, struct page *page,
2429 unsigned int *len, unsigned int offset,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002430 bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002431 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08002432{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002433 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
David S. Millera108d5f2012-04-23 23:06:11 -04002434 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002435
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002436 if (linear) {
Eric Dumazet18aafc62013-01-11 14:46:37 +00002437 page = linear_to_page(page, len, &offset, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002438 if (!page)
David S. Millera108d5f2012-04-23 23:06:11 -04002439 return true;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002440 }
2441 if (spd_can_coalesce(spd, page, offset)) {
2442 spd->partial[spd->nr_pages - 1].len += *len;
David S. Millera108d5f2012-04-23 23:06:11 -04002443 return false;
Eric Dumazet41c73a02012-04-22 12:26:16 +00002444 }
2445 get_page(page);
Jens Axboe9c55e012007-11-06 23:30:13 -08002446 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08002447 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08002448 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08002449 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08002450
David S. Millera108d5f2012-04-23 23:06:11 -04002451 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002452}
2453
David S. Millera108d5f2012-04-23 23:06:11 -04002454static bool __splice_segment(struct page *page, unsigned int poff,
2455 unsigned int plen, unsigned int *off,
Eric Dumazet18aafc62013-01-11 14:46:37 +00002456 unsigned int *len,
Eric Dumazetd7ccf7c2012-04-23 23:35:04 -04002457 struct splice_pipe_desc *spd, bool linear,
David S. Millera108d5f2012-04-23 23:06:11 -04002458 struct sock *sk,
2459 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07002460{
2461 if (!*len)
David S. Millera108d5f2012-04-23 23:06:11 -04002462 return true;
Octavian Purdila2870c432008-07-15 00:49:11 -07002463
2464 /* skip this segment if already processed */
2465 if (*off >= plen) {
2466 *off -= plen;
David S. Millera108d5f2012-04-23 23:06:11 -04002467 return false;
Octavian Purdiladb43a282008-06-27 17:27:21 -07002468 }
Jens Axboe9c55e012007-11-06 23:30:13 -08002469
Octavian Purdila2870c432008-07-15 00:49:11 -07002470 /* ignore any bits we already processed */
Eric Dumazet9ca1b222013-01-05 21:31:18 +00002471 poff += *off;
2472 plen -= *off;
2473 *off = 0;
Octavian Purdila2870c432008-07-15 00:49:11 -07002474
Eric Dumazet18aafc62013-01-11 14:46:37 +00002475 do {
2476 unsigned int flen = min(*len, plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002477
Eric Dumazet18aafc62013-01-11 14:46:37 +00002478 if (spd_fill_page(spd, pipe, page, &flen, poff,
2479 linear, sk))
2480 return true;
2481 poff += flen;
2482 plen -= flen;
2483 *len -= flen;
2484 } while (*len && plen);
Octavian Purdila2870c432008-07-15 00:49:11 -07002485
David S. Millera108d5f2012-04-23 23:06:11 -04002486 return false;
Octavian Purdila2870c432008-07-15 00:49:11 -07002487}
2488
2489/*
David S. Millera108d5f2012-04-23 23:06:11 -04002490 * Map linear and fragment data from the skb to spd. It reports true if the
Octavian Purdila2870c432008-07-15 00:49:11 -07002491 * pipe is full or if we already spliced the requested length.
2492 */
David S. Millera108d5f2012-04-23 23:06:11 -04002493static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2494 unsigned int *offset, unsigned int *len,
2495 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07002496{
2497 int seg;
Tom Herbertfa9835e2016-03-07 14:11:04 -08002498 struct sk_buff *iter;
Octavian Purdila2870c432008-07-15 00:49:11 -07002499
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002500 /* map the linear part :
Alexander Duyck2996d312012-05-02 18:18:42 +00002501 * If skb->head_frag is set, this 'linear' part is backed by a
2502 * fragment, and if the head is not shared with any clones then
2503 * we can avoid a copy since we own the head portion of this page.
Jens Axboe9c55e012007-11-06 23:30:13 -08002504 */
Octavian Purdila2870c432008-07-15 00:49:11 -07002505 if (__splice_segment(virt_to_page(skb->data),
2506 (unsigned long) skb->data & (PAGE_SIZE - 1),
2507 skb_headlen(skb),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002508 offset, len, spd,
Alexander Duyck3a7c1ee42012-05-03 01:09:42 +00002509 skb_head_is_locked(skb),
Eric Dumazet1d0c0b32012-04-27 02:10:03 +00002510 sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002511 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002512
2513 /*
2514 * then map the fragments
2515 */
Jens Axboe9c55e012007-11-06 23:30:13 -08002516 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2517 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2518
Ian Campbellea2ab692011-08-22 23:44:58 +00002519 if (__splice_segment(skb_frag_page(f),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002520 skb_frag_off(f), skb_frag_size(f),
Eric Dumazet18aafc62013-01-11 14:46:37 +00002521 offset, len, spd, false, sk, pipe))
David S. Millera108d5f2012-04-23 23:06:11 -04002522 return true;
Jens Axboe9c55e012007-11-06 23:30:13 -08002523 }
2524
Tom Herbertfa9835e2016-03-07 14:11:04 -08002525 skb_walk_frags(skb, iter) {
2526 if (*offset >= iter->len) {
2527 *offset -= iter->len;
2528 continue;
2529 }
2530 /* __skb_splice_bits() only fails if the output has no room
2531 * left, so no point in going over the frag_list for the error
2532 * case.
2533 */
2534 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2535 return true;
2536 }
2537
David S. Millera108d5f2012-04-23 23:06:11 -04002538 return false;
Jens Axboe9c55e012007-11-06 23:30:13 -08002539}
2540
2541/*
2542 * Map data from the skb to a pipe. Should handle both the linear part,
Tom Herbertfa9835e2016-03-07 14:11:04 -08002543 * the fragments, and the frag list.
Jens Axboe9c55e012007-11-06 23:30:13 -08002544 */
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002545int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08002546 struct pipe_inode_info *pipe, unsigned int tlen,
Al Viro25869262016-09-17 21:02:10 -04002547 unsigned int flags)
Jens Axboe9c55e012007-11-06 23:30:13 -08002548{
Eric Dumazet41c73a02012-04-22 12:26:16 +00002549 struct partial_page partial[MAX_SKB_FRAGS];
2550 struct page *pages[MAX_SKB_FRAGS];
Jens Axboe9c55e012007-11-06 23:30:13 -08002551 struct splice_pipe_desc spd = {
2552 .pages = pages,
2553 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002554 .nr_pages_max = MAX_SKB_FRAGS,
Miklos Szeredi28a625c2014-01-22 19:36:57 +01002555 .ops = &nosteal_pipe_buf_ops,
Jens Axboe9c55e012007-11-06 23:30:13 -08002556 .spd_release = sock_spd_release,
2557 };
Jens Axboe35f3d142010-05-20 10:43:18 +02002558 int ret = 0;
2559
Tom Herbertfa9835e2016-03-07 14:11:04 -08002560 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08002561
Hannes Frederic Sowaa60e3cc2015-05-21 17:00:00 +02002562 if (spd.nr_pages)
Al Viro25869262016-09-17 21:02:10 -04002563 ret = splice_to_pipe(pipe, &spd);
Jens Axboe9c55e012007-11-06 23:30:13 -08002564
Jens Axboe35f3d142010-05-20 10:43:18 +02002565 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08002566}
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002567EXPORT_SYMBOL_GPL(skb_splice_bits);
Jens Axboe9c55e012007-11-06 23:30:13 -08002568
Cong Wang0739cd22021-03-30 19:32:24 -07002569static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2570 struct kvec *vec, size_t num, size_t size)
2571{
2572 struct socket *sock = sk->sk_socket;
2573
2574 if (!sock)
2575 return -EINVAL;
2576 return kernel_sendmsg(sock, msg, vec, num, size);
2577}
2578
2579static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2580 size_t size, int flags)
2581{
2582 struct socket *sock = sk->sk_socket;
2583
2584 if (!sock)
2585 return -EINVAL;
2586 return kernel_sendpage(sock, page, offset, size, flags);
2587}
2588
2589typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2590 struct kvec *vec, size_t num, size_t size);
2591typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2592 size_t size, int flags);
2593static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2594 int len, sendmsg_func sendmsg, sendpage_func sendpage)
Tom Herbert20bf50d2017-07-28 16:22:42 -07002595{
2596 unsigned int orig_len = len;
2597 struct sk_buff *head = skb;
2598 unsigned short fragidx;
2599 int slen, ret;
2600
2601do_frag_list:
2602
2603 /* Deal with head data */
2604 while (offset < skb_headlen(skb) && len) {
2605 struct kvec kv;
2606 struct msghdr msg;
2607
2608 slen = min_t(int, len, skb_headlen(skb) - offset);
2609 kv.iov_base = skb->data + offset;
John Fastabenddb5980d2017-08-15 22:31:34 -07002610 kv.iov_len = slen;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002611 memset(&msg, 0, sizeof(msg));
John Fastabendbd95e6782019-05-24 08:01:00 -07002612 msg.msg_flags = MSG_DONTWAIT;
Tom Herbert20bf50d2017-07-28 16:22:42 -07002613
Cong Wang0739cd22021-03-30 19:32:24 -07002614 ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2615 sendmsg_unlocked, sk, &msg, &kv, 1, slen);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002616 if (ret <= 0)
2617 goto error;
2618
2619 offset += ret;
2620 len -= ret;
2621 }
2622
2623 /* All the data was skb head? */
2624 if (!len)
2625 goto out;
2626
2627 /* Make offset relative to start of frags */
2628 offset -= skb_headlen(skb);
2629
2630 /* Find where we are in frag list */
2631 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2632 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2633
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002634 if (offset < skb_frag_size(frag))
Tom Herbert20bf50d2017-07-28 16:22:42 -07002635 break;
2636
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002637 offset -= skb_frag_size(frag);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002638 }
2639
2640 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2641 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
2642
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07002643 slen = min_t(size_t, len, skb_frag_size(frag) - offset);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002644
2645 while (slen) {
Cong Wang0739cd22021-03-30 19:32:24 -07002646 ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2647 sendpage_unlocked, sk,
2648 skb_frag_page(frag),
2649 skb_frag_off(frag) + offset,
2650 slen, MSG_DONTWAIT);
Tom Herbert20bf50d2017-07-28 16:22:42 -07002651 if (ret <= 0)
2652 goto error;
2653
2654 len -= ret;
2655 offset += ret;
2656 slen -= ret;
2657 }
2658
2659 offset = 0;
2660 }
2661
2662 if (len) {
2663 /* Process any frag lists */
2664
2665 if (skb == head) {
2666 if (skb_has_frag_list(skb)) {
2667 skb = skb_shinfo(skb)->frag_list;
2668 goto do_frag_list;
2669 }
2670 } else if (skb->next) {
2671 skb = skb->next;
2672 goto do_frag_list;
2673 }
2674 }
2675
2676out:
2677 return orig_len - len;
2678
2679error:
2680 return orig_len == len ? ret : orig_len - len;
2681}
Cong Wang0739cd22021-03-30 19:32:24 -07002682
2683/* Send skb data on a socket. Socket must be locked. */
2684int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2685 int len)
2686{
2687 return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2688 kernel_sendpage_locked);
2689}
Tom Herbert20bf50d2017-07-28 16:22:42 -07002690EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2691
Cong Wang0739cd22021-03-30 19:32:24 -07002692/* Send skb data on a socket. Socket must be unlocked. */
2693int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2694{
2695 return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
2696 sendpage_unlocked);
2697}
2698
Herbert Xu357b40a2005-04-19 22:30:14 -07002699/**
2700 * skb_store_bits - store bits from kernel buffer to skb
2701 * @skb: destination buffer
2702 * @offset: offset in destination
2703 * @from: source buffer
2704 * @len: number of bytes to copy
2705 *
2706 * Copy the specified number of bytes from the source buffer to the
2707 * destination skb. This function handles all the messy bits of
2708 * traversing fragment lists and such.
2709 */
2710
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07002711int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07002712{
David S. Miller1a028e52007-04-27 15:21:23 -07002713 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07002714 struct sk_buff *frag_iter;
2715 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002716
2717 if (offset > (int)skb->len - len)
2718 goto fault;
2719
David S. Miller1a028e52007-04-27 15:21:23 -07002720 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07002721 if (copy > len)
2722 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002723 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07002724 if ((len -= copy) == 0)
2725 return 0;
2726 offset += copy;
2727 from += copy;
2728 }
2729
2730 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2731 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07002732 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002733
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002734 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002735
Eric Dumazet9e903e02011-10-18 21:00:24 +00002736 end = start + skb_frag_size(frag);
Herbert Xu357b40a2005-04-19 22:30:14 -07002737 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002738 u32 p_off, p_len, copied;
2739 struct page *p;
Herbert Xu357b40a2005-04-19 22:30:14 -07002740 u8 *vaddr;
2741
2742 if (copy > len)
2743 copy = len;
2744
Willem de Bruijnc613c202017-07-31 08:15:47 -04002745 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002746 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002747 copy, p, p_off, p_len, copied) {
2748 vaddr = kmap_atomic(p);
2749 memcpy(vaddr + p_off, from + copied, p_len);
2750 kunmap_atomic(vaddr);
2751 }
Herbert Xu357b40a2005-04-19 22:30:14 -07002752
2753 if ((len -= copy) == 0)
2754 return 0;
2755 offset += copy;
2756 from += copy;
2757 }
David S. Miller1a028e52007-04-27 15:21:23 -07002758 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002759 }
2760
David S. Millerfbb398a2009-06-09 00:18:59 -07002761 skb_walk_frags(skb, frag_iter) {
2762 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002763
David S. Millerfbb398a2009-06-09 00:18:59 -07002764 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07002765
David S. Millerfbb398a2009-06-09 00:18:59 -07002766 end = start + frag_iter->len;
2767 if ((copy = end - offset) > 0) {
2768 if (copy > len)
2769 copy = len;
2770 if (skb_store_bits(frag_iter, offset - start,
2771 from, copy))
2772 goto fault;
2773 if ((len -= copy) == 0)
2774 return 0;
2775 offset += copy;
2776 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07002777 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002778 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07002779 }
2780 if (!len)
2781 return 0;
2782
2783fault:
2784 return -EFAULT;
2785}
Herbert Xu357b40a2005-04-19 22:30:14 -07002786EXPORT_SYMBOL(skb_store_bits);
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/* Checksum skb data. */
Daniel Borkmann2817a332013-10-30 11:50:51 +01002789__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2790 __wsum csum, const struct skb_checksum_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
David S. Miller1a028e52007-04-27 15:21:23 -07002792 int start = skb_headlen(skb);
2793 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002794 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 int pos = 0;
2796
2797 /* Checksum header. */
2798 if (copy > 0) {
2799 if (copy > len)
2800 copy = len;
Matteo Croce2544af02019-05-29 17:13:48 +02002801 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2802 skb->data + offset, copy, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if ((len -= copy) == 0)
2804 return csum;
2805 offset += copy;
2806 pos = copy;
2807 }
2808
2809 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002810 int end;
Eric Dumazet51c56b02012-04-05 11:35:15 +02002811 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002813 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002814
Eric Dumazet51c56b02012-04-05 11:35:15 +02002815 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002817 u32 p_off, p_len, copied;
2818 struct page *p;
Al Viro44bb9362006-11-14 21:36:14 -08002819 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 if (copy > len)
2823 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002824
2825 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002826 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002827 copy, p, p_off, p_len, copied) {
2828 vaddr = kmap_atomic(p);
Matteo Croce2544af02019-05-29 17:13:48 +02002829 csum2 = INDIRECT_CALL_1(ops->update,
2830 csum_partial_ext,
2831 vaddr + p_off, p_len, 0);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002832 kunmap_atomic(vaddr);
Matteo Croce2544af02019-05-29 17:13:48 +02002833 csum = INDIRECT_CALL_1(ops->combine,
2834 csum_block_add_ext, csum,
2835 csum2, pos, p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002836 pos += p_len;
2837 }
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (!(len -= copy))
2840 return csum;
2841 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
David S. Miller1a028e52007-04-27 15:21:23 -07002843 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845
David S. Millerfbb398a2009-06-09 00:18:59 -07002846 skb_walk_frags(skb, frag_iter) {
2847 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
David S. Millerfbb398a2009-06-09 00:18:59 -07002849 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
David S. Millerfbb398a2009-06-09 00:18:59 -07002851 end = start + frag_iter->len;
2852 if ((copy = end - offset) > 0) {
2853 __wsum csum2;
2854 if (copy > len)
2855 copy = len;
Daniel Borkmann2817a332013-10-30 11:50:51 +01002856 csum2 = __skb_checksum(frag_iter, offset - start,
2857 copy, 0, ops);
Matteo Croce2544af02019-05-29 17:13:48 +02002858 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2859 csum, csum2, pos, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002860 if ((len -= copy) == 0)
2861 return csum;
2862 offset += copy;
2863 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002865 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002867 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 return csum;
2870}
Daniel Borkmann2817a332013-10-30 11:50:51 +01002871EXPORT_SYMBOL(__skb_checksum);
2872
2873__wsum skb_checksum(const struct sk_buff *skb, int offset,
2874 int len, __wsum csum)
2875{
2876 const struct skb_checksum_ops ops = {
Daniel Borkmanncea80ea2013-11-04 17:10:25 +01002877 .update = csum_partial_ext,
Daniel Borkmann2817a332013-10-30 11:50:51 +01002878 .combine = csum_block_add_ext,
2879 };
2880
2881 return __skb_checksum(skb, offset, len, csum, &ops);
2882}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002883EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885/* Both of above in one bottle. */
2886
Al Viro81d77662006-11-14 21:37:33 -08002887__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
Al Viro8d5930d2020-07-10 20:07:10 -04002888 u8 *to, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
David S. Miller1a028e52007-04-27 15:21:23 -07002890 int start = skb_headlen(skb);
2891 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002892 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 int pos = 0;
Al Viro8d5930d2020-07-10 20:07:10 -04002894 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 /* Copy header. */
2897 if (copy > 0) {
2898 if (copy > len)
2899 copy = len;
2900 csum = csum_partial_copy_nocheck(skb->data + offset, to,
Al Virocc44c172020-07-11 00:12:07 -04002901 copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if ((len -= copy) == 0)
2903 return csum;
2904 offset += copy;
2905 to += copy;
2906 pos = copy;
2907 }
2908
2909 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002910 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002912 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002913
Eric Dumazet9e903e02011-10-18 21:00:24 +00002914 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if ((copy = end - offset) > 0) {
Willem de Bruijnc613c202017-07-31 08:15:47 -04002916 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2917 u32 p_off, p_len, copied;
2918 struct page *p;
Al Viro50842052006-11-14 21:36:34 -08002919 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 if (copy > len)
2923 copy = len;
Willem de Bruijnc613c202017-07-31 08:15:47 -04002924
2925 skb_frag_foreach_page(frag,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07002926 skb_frag_off(frag) + offset - start,
Willem de Bruijnc613c202017-07-31 08:15:47 -04002927 copy, p, p_off, p_len, copied) {
2928 vaddr = kmap_atomic(p);
2929 csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2930 to + copied,
Al Virocc44c172020-07-11 00:12:07 -04002931 p_len);
Willem de Bruijnc613c202017-07-31 08:15:47 -04002932 kunmap_atomic(vaddr);
2933 csum = csum_block_add(csum, csum2, pos);
2934 pos += p_len;
2935 }
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (!(len -= copy))
2938 return csum;
2939 offset += copy;
2940 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
David S. Miller1a028e52007-04-27 15:21:23 -07002942 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944
David S. Millerfbb398a2009-06-09 00:18:59 -07002945 skb_walk_frags(skb, frag_iter) {
2946 __wsum csum2;
2947 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
David S. Millerfbb398a2009-06-09 00:18:59 -07002949 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
David S. Millerfbb398a2009-06-09 00:18:59 -07002951 end = start + frag_iter->len;
2952 if ((copy = end - offset) > 0) {
2953 if (copy > len)
2954 copy = len;
2955 csum2 = skb_copy_and_csum_bits(frag_iter,
2956 offset - start,
Al Viro8d5930d2020-07-10 20:07:10 -04002957 to, copy);
David S. Millerfbb398a2009-06-09 00:18:59 -07002958 csum = csum_block_add(csum, csum2, pos);
2959 if ((len -= copy) == 0)
2960 return csum;
2961 offset += copy;
2962 to += copy;
2963 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002965 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08002967 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return csum;
2969}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002970EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Cong Wang49f8e832018-11-08 14:05:42 -08002972__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2973{
2974 __sum16 sum;
2975
2976 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Cong Wang14641932018-11-26 09:31:26 -08002977 /* See comments in __skb_checksum_complete(). */
Cong Wang49f8e832018-11-08 14:05:42 -08002978 if (likely(!sum)) {
2979 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2980 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08002981 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08002982 }
2983 if (!skb_shared(skb))
2984 skb->csum_valid = !sum;
2985 return sum;
2986}
2987EXPORT_SYMBOL(__skb_checksum_complete_head);
2988
Cong Wang14641932018-11-26 09:31:26 -08002989/* This function assumes skb->csum already holds pseudo header's checksum,
2990 * which has been changed from the hardware checksum, for example, by
2991 * __skb_checksum_validate_complete(). And, the original skb->csum must
2992 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2993 *
2994 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2995 * zero. The new checksum is stored back into skb->csum unless the skb is
2996 * shared.
2997 */
Cong Wang49f8e832018-11-08 14:05:42 -08002998__sum16 __skb_checksum_complete(struct sk_buff *skb)
2999{
3000 __wsum csum;
3001 __sum16 sum;
3002
3003 csum = skb_checksum(skb, 0, skb->len, 0);
3004
Cong Wang49f8e832018-11-08 14:05:42 -08003005 sum = csum_fold(csum_add(skb->csum, csum));
Cong Wang14641932018-11-26 09:31:26 -08003006 /* This check is inverted, because we already knew the hardware
3007 * checksum is invalid before calling this function. So, if the
3008 * re-computed checksum is valid instead, then we have a mismatch
3009 * between the original skb->csum and skb_checksum(). This means either
3010 * the original hardware checksum is incorrect or we screw up skb->csum
3011 * when moving skb->data around.
3012 */
Cong Wang49f8e832018-11-08 14:05:42 -08003013 if (likely(!sum)) {
3014 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3015 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -08003016 netdev_rx_csum_fault(skb->dev, skb);
Cong Wang49f8e832018-11-08 14:05:42 -08003017 }
3018
3019 if (!skb_shared(skb)) {
3020 /* Save full packet checksum */
3021 skb->csum = csum;
3022 skb->ip_summed = CHECKSUM_COMPLETE;
3023 skb->csum_complete_sw = 1;
3024 skb->csum_valid = !sum;
3025 }
3026
3027 return sum;
3028}
3029EXPORT_SYMBOL(__skb_checksum_complete);
3030
Davide Caratti96178132017-05-18 15:44:37 +02003031static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
3032{
3033 net_warn_ratelimited(
3034 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3035 __func__);
3036 return 0;
3037}
3038
3039static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
3040 int offset, int len)
3041{
3042 net_warn_ratelimited(
3043 "%s: attempt to compute crc32c without libcrc32c.ko\n",
3044 __func__);
3045 return 0;
3046}
3047
3048static const struct skb_checksum_ops default_crc32c_ops = {
3049 .update = warn_crc32c_csum_update,
3050 .combine = warn_crc32c_csum_combine,
3051};
3052
3053const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3054 &default_crc32c_ops;
3055EXPORT_SYMBOL(crc32c_csum_stub);
3056
Thomas Grafaf2806f2013-12-13 15:22:17 +01003057 /**
3058 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3059 * @from: source buffer
3060 *
3061 * Calculates the amount of linear headroom needed in the 'to' skb passed
3062 * into skb_zerocopy().
3063 */
3064unsigned int
3065skb_zerocopy_headlen(const struct sk_buff *from)
3066{
3067 unsigned int hlen = 0;
3068
3069 if (!from->head_frag ||
3070 skb_headlen(from) < L1_CACHE_BYTES ||
Pravin B Shelara17ad092021-07-15 16:59:00 -07003071 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
Thomas Grafaf2806f2013-12-13 15:22:17 +01003072 hlen = skb_headlen(from);
Pravin B Shelara17ad092021-07-15 16:59:00 -07003073 if (!hlen)
3074 hlen = from->len;
3075 }
Thomas Grafaf2806f2013-12-13 15:22:17 +01003076
3077 if (skb_has_frag_list(from))
3078 hlen = from->len;
3079
3080 return hlen;
3081}
3082EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3083
3084/**
3085 * skb_zerocopy - Zero copy skb to skb
3086 * @to: destination buffer
Masanari Iida7fceb4d2014-01-29 01:05:28 +09003087 * @from: source buffer
Thomas Grafaf2806f2013-12-13 15:22:17 +01003088 * @len: number of bytes to copy from source buffer
3089 * @hlen: size of linear headroom in destination buffer
3090 *
3091 * Copies up to `len` bytes from `from` to `to` by creating references
3092 * to the frags in the source buffer.
3093 *
3094 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3095 * headroom in the `to` buffer.
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003096 *
3097 * Return value:
3098 * 0: everything is OK
3099 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
3100 * -EFAULT: skb_copy_bits() found some problem with skb geometry
Thomas Grafaf2806f2013-12-13 15:22:17 +01003101 */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003102int
3103skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
Thomas Grafaf2806f2013-12-13 15:22:17 +01003104{
3105 int i, j = 0;
3106 int plen = 0; /* length of skb->head fragment */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003107 int ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003108 struct page *page;
3109 unsigned int offset;
3110
3111 BUG_ON(!from->head_frag && !hlen);
3112
3113 /* dont bother with small payloads */
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003114 if (len <= skb_tailroom(to))
3115 return skb_copy_bits(from, 0, skb_put(to, len), len);
Thomas Grafaf2806f2013-12-13 15:22:17 +01003116
3117 if (hlen) {
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003118 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3119 if (unlikely(ret))
3120 return ret;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003121 len -= hlen;
3122 } else {
3123 plen = min_t(int, skb_headlen(from), len);
3124 if (plen) {
3125 page = virt_to_head_page(from->head);
3126 offset = from->data - (unsigned char *)page_address(page);
3127 __skb_fill_page_desc(to, 0, page, offset, plen);
3128 get_page(page);
3129 j = 1;
3130 len -= plen;
3131 }
3132 }
3133
3134 to->truesize += len + plen;
3135 to->len += len + plen;
3136 to->data_len += len + plen;
3137
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003138 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3139 skb_tx_error(from);
3140 return -ENOMEM;
3141 }
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003142 skb_zerocopy_clone(to, from, GFP_ATOMIC);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003143
Thomas Grafaf2806f2013-12-13 15:22:17 +01003144 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003145 int size;
3146
Thomas Grafaf2806f2013-12-13 15:22:17 +01003147 if (!len)
3148 break;
3149 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003150 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3151 len);
3152 skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3153 len -= size;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003154 skb_frag_ref(to, j);
3155 j++;
3156 }
3157 skb_shinfo(to)->nr_frags = j;
Zoltan Kiss36d5fe62014-03-26 22:37:45 +00003158
3159 return 0;
Thomas Grafaf2806f2013-12-13 15:22:17 +01003160}
3161EXPORT_SYMBOL_GPL(skb_zerocopy);
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3164{
Al Virod3bc23e2006-11-14 21:24:49 -08003165 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 long csstart;
3167
Patrick McHardy84fa7932006-08-29 16:44:56 -07003168 if (skb->ip_summed == CHECKSUM_PARTIAL)
Michał Mirosław55508d62010-12-14 15:24:08 +00003169 csstart = skb_checksum_start_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 else
3171 csstart = skb_headlen(skb);
3172
Kris Katterjohn09a62662006-01-08 22:24:28 -08003173 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003175 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 csum = 0;
3178 if (csstart != skb->len)
3179 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
Al Viro8d5930d2020-07-10 20:07:10 -04003180 skb->len - csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Patrick McHardy84fa7932006-08-29 16:44:56 -07003182 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08003183 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Al Virod3bc23e2006-11-14 21:24:49 -08003185 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003188EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190/**
3191 * skb_dequeue - remove from the head of the queue
3192 * @list: list to dequeue from
3193 *
3194 * Remove the head of the list. The list lock is taken so the function
3195 * may be used safely with other locking list functions. The head item is
3196 * returned or %NULL if the list is empty.
3197 */
3198
3199struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3200{
3201 unsigned long flags;
3202 struct sk_buff *result;
3203
3204 spin_lock_irqsave(&list->lock, flags);
3205 result = __skb_dequeue(list);
3206 spin_unlock_irqrestore(&list->lock, flags);
3207 return result;
3208}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003209EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211/**
3212 * skb_dequeue_tail - remove from the tail of the queue
3213 * @list: list to dequeue from
3214 *
3215 * Remove the tail of the list. The list lock is taken so the function
3216 * may be used safely with other locking list functions. The tail item is
3217 * returned or %NULL if the list is empty.
3218 */
3219struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3220{
3221 unsigned long flags;
3222 struct sk_buff *result;
3223
3224 spin_lock_irqsave(&list->lock, flags);
3225 result = __skb_dequeue_tail(list);
3226 spin_unlock_irqrestore(&list->lock, flags);
3227 return result;
3228}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003229EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231/**
3232 * skb_queue_purge - empty a list
3233 * @list: list to empty
3234 *
3235 * Delete all buffers on an &sk_buff list. Each buffer is removed from
3236 * the list and one reference dropped. This function takes the list
3237 * lock and is atomic with respect to other list locking functions.
3238 */
3239void skb_queue_purge(struct sk_buff_head *list)
3240{
3241 struct sk_buff *skb;
3242 while ((skb = skb_dequeue(list)) != NULL)
3243 kfree_skb(skb);
3244}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003245EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247/**
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003248 * skb_rbtree_purge - empty a skb rbtree
3249 * @root: root of the rbtree to empty
Peter Oskolkov385114d2018-08-02 23:34:38 +00003250 * Return value: the sum of truesizes of all purged skbs.
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003251 *
3252 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3253 * the list and one reference dropped. This function does not take
3254 * any lock. Synchronization should be handled by the caller (e.g., TCP
3255 * out-of-order queue is protected by the socket lock).
3256 */
Peter Oskolkov385114d2018-08-02 23:34:38 +00003257unsigned int skb_rbtree_purge(struct rb_root *root)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003258{
Eric Dumazet7c905842017-09-23 12:39:12 -07003259 struct rb_node *p = rb_first(root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003260 unsigned int sum = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003261
Eric Dumazet7c905842017-09-23 12:39:12 -07003262 while (p) {
3263 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3264
3265 p = rb_next(p);
3266 rb_erase(&skb->rbnode, root);
Peter Oskolkov385114d2018-08-02 23:34:38 +00003267 sum += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003268 kfree_skb(skb);
Eric Dumazet7c905842017-09-23 12:39:12 -07003269 }
Peter Oskolkov385114d2018-08-02 23:34:38 +00003270 return sum;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07003271}
3272
3273/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 * skb_queue_head - queue a buffer at the list head
3275 * @list: list to use
3276 * @newsk: buffer to queue
3277 *
3278 * Queue a buffer at the start of the list. This function takes the
3279 * list lock and can be used safely with other locking &sk_buff functions
3280 * safely.
3281 *
3282 * A buffer cannot be placed on two lists at the same time.
3283 */
3284void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3285{
3286 unsigned long flags;
3287
3288 spin_lock_irqsave(&list->lock, flags);
3289 __skb_queue_head(list, newsk);
3290 spin_unlock_irqrestore(&list->lock, flags);
3291}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003292EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294/**
3295 * skb_queue_tail - queue a buffer at the list tail
3296 * @list: list to use
3297 * @newsk: buffer to queue
3298 *
3299 * Queue a buffer at the tail of the list. This function takes the
3300 * list lock and can be used safely with other locking &sk_buff functions
3301 * safely.
3302 *
3303 * A buffer cannot be placed on two lists at the same time.
3304 */
3305void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3306{
3307 unsigned long flags;
3308
3309 spin_lock_irqsave(&list->lock, flags);
3310 __skb_queue_tail(list, newsk);
3311 spin_unlock_irqrestore(&list->lock, flags);
3312}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003313EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07003314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315/**
3316 * skb_unlink - remove a buffer from a list
3317 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07003318 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 *
David S. Miller8728b832005-08-09 19:25:21 -07003320 * Remove a packet from a list. The list locks are taken and this
3321 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 *
David S. Miller8728b832005-08-09 19:25:21 -07003323 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 */
David S. Miller8728b832005-08-09 19:25:21 -07003325void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
David S. Miller8728b832005-08-09 19:25:21 -07003327 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
David S. Miller8728b832005-08-09 19:25:21 -07003329 spin_lock_irqsave(&list->lock, flags);
3330 __skb_unlink(skb, list);
3331 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003333EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/**
3336 * skb_append - append a buffer
3337 * @old: buffer to insert after
3338 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07003339 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 *
3341 * Place a packet after a given packet in a list. The list locks are taken
3342 * and this function is atomic with respect to other list locked calls.
3343 * A buffer cannot be placed on two lists at the same time.
3344 */
David S. Miller8728b832005-08-09 19:25:21 -07003345void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 unsigned long flags;
3348
David S. Miller8728b832005-08-09 19:25:21 -07003349 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07003350 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07003351 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003353EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355static inline void skb_split_inside_header(struct sk_buff *skb,
3356 struct sk_buff* skb1,
3357 const u32 len, const int pos)
3358{
3359 int i;
3360
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03003361 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3362 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /* And move data appendix as is. */
3364 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3365 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3366
3367 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3368 skb_shinfo(skb)->nr_frags = 0;
3369 skb1->data_len = skb->data_len;
3370 skb1->len += skb1->data_len;
3371 skb->data_len = 0;
3372 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003373 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
3376static inline void skb_split_no_header(struct sk_buff *skb,
3377 struct sk_buff* skb1,
3378 const u32 len, int pos)
3379{
3380 int i, k = 0;
3381 const int nfrags = skb_shinfo(skb)->nr_frags;
3382
3383 skb_shinfo(skb)->nr_frags = 0;
3384 skb1->len = skb1->data_len = skb->len - len;
3385 skb->len = len;
3386 skb->data_len = len - pos;
3387
3388 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003389 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 if (pos + size > len) {
3392 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3393
3394 if (pos < len) {
3395 /* Split frag.
3396 * We have two variants in this case:
3397 * 1. Move all the frag to the second
3398 * part, if it is possible. F.e.
3399 * this approach is mandatory for TUX,
3400 * where splitting is expensive.
3401 * 2. Split is accurately. We make this.
3402 */
Ian Campbellea2ab692011-08-22 23:44:58 +00003403 skb_frag_ref(skb, i);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003404 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003405 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3406 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 skb_shinfo(skb)->nr_frags++;
3408 }
3409 k++;
3410 } else
3411 skb_shinfo(skb)->nr_frags++;
3412 pos += size;
3413 }
3414 skb_shinfo(skb1)->nr_frags = k;
3415}
3416
3417/**
3418 * skb_split - Split fragmented skb to two parts at length len.
3419 * @skb: the buffer to split
3420 * @skb1: the buffer to receive the second part
3421 * @len: new length for skb
3422 */
3423void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3424{
3425 int pos = skb_headlen(skb);
3426
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08003427 skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003428 skb_zerocopy_clone(skb1, skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 if (len < pos) /* Split line is inside header. */
3430 skb_split_inside_header(skb, skb1, len, pos);
3431 else /* Second chunk has no header, nothing to copy. */
3432 skb_split_no_header(skb, skb1, len, pos);
3433}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003434EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003436/* Shifting from/to a cloned skb is a no-go.
3437 *
3438 * Caller cannot keep skb_shinfo related pointers past calling here!
3439 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003440static int skb_prepare_for_shift(struct sk_buff *skb)
3441{
Marco Elver097b9142021-02-01 17:04:20 +01003442 int ret = 0;
3443
3444 if (skb_cloned(skb)) {
3445 /* Save and restore truesize: pskb_expand_head() may reallocate
3446 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3447 * cannot change truesize at this point.
3448 */
3449 unsigned int save_truesize = skb->truesize;
3450
3451 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3452 skb->truesize = save_truesize;
3453 }
3454 return ret;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003455}
3456
3457/**
3458 * skb_shift - Shifts paged data partially from skb to another
3459 * @tgt: buffer into which tail data gets added
3460 * @skb: buffer from which the paged data comes from
3461 * @shiftlen: shift up to this many bytes
3462 *
3463 * Attempts to shift up to shiftlen worth of bytes, which may be less than
Feng King20e994a2011-11-21 01:47:11 +00003464 * the length of the skb, from skb to tgt. Returns number bytes shifted.
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003465 * It's up to caller to free skb if everything was shifted.
3466 *
3467 * If @tgt runs out of frags, the whole operation is aborted.
3468 *
3469 * Skb cannot include anything else but paged data while tgt is allowed
3470 * to have non-paged data as well.
3471 *
3472 * TODO: full sized shift could be optimized but that would need
3473 * specialized skb free'er to handle frags without up-to-date nr_frags.
3474 */
3475int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3476{
3477 int from, to, merge, todo;
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003478 skb_frag_t *fragfrom, *fragto;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003479
3480 BUG_ON(shiftlen > skb->len);
Eric Dumazetf8071cd2016-11-15 12:51:50 -08003481
3482 if (skb_headlen(skb))
3483 return 0;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04003484 if (skb_zcopy(tgt) || skb_zcopy(skb))
3485 return 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003486
3487 todo = shiftlen;
3488 from = 0;
3489 to = skb_shinfo(tgt)->nr_frags;
3490 fragfrom = &skb_shinfo(skb)->frags[from];
3491
3492 /* Actual merge is delayed until the point when we know we can
3493 * commit all, so that we don't have to undo partial changes
3494 */
3495 if (!to ||
Ian Campbellea2ab692011-08-22 23:44:58 +00003496 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003497 skb_frag_off(fragfrom))) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003498 merge = -1;
3499 } else {
3500 merge = to - 1;
3501
Eric Dumazet9e903e02011-10-18 21:00:24 +00003502 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003503 if (todo < 0) {
3504 if (skb_prepare_for_shift(skb) ||
3505 skb_prepare_for_shift(tgt))
3506 return 0;
3507
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08003508 /* All previous frag pointers might be stale! */
3509 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003510 fragto = &skb_shinfo(tgt)->frags[merge];
3511
Eric Dumazet9e903e02011-10-18 21:00:24 +00003512 skb_frag_size_add(fragto, shiftlen);
3513 skb_frag_size_sub(fragfrom, shiftlen);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003514 skb_frag_off_add(fragfrom, shiftlen);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003515
3516 goto onlymerged;
3517 }
3518
3519 from++;
3520 }
3521
3522 /* Skip full, not-fitting skb to avoid expensive operations */
3523 if ((shiftlen == skb->len) &&
3524 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3525 return 0;
3526
3527 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3528 return 0;
3529
3530 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3531 if (to == MAX_SKB_FRAGS)
3532 return 0;
3533
3534 fragfrom = &skb_shinfo(skb)->frags[from];
3535 fragto = &skb_shinfo(tgt)->frags[to];
3536
Eric Dumazet9e903e02011-10-18 21:00:24 +00003537 if (todo >= skb_frag_size(fragfrom)) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003538 *fragto = *fragfrom;
Eric Dumazet9e903e02011-10-18 21:00:24 +00003539 todo -= skb_frag_size(fragfrom);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003540 from++;
3541 to++;
3542
3543 } else {
Ian Campbellea2ab692011-08-22 23:44:58 +00003544 __skb_frag_ref(fragfrom);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003545 skb_frag_page_copy(fragto, fragfrom);
3546 skb_frag_off_copy(fragto, fragfrom);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003547 skb_frag_size_set(fragto, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003548
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003549 skb_frag_off_add(fragfrom, todo);
Eric Dumazet9e903e02011-10-18 21:00:24 +00003550 skb_frag_size_sub(fragfrom, todo);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003551 todo = 0;
3552
3553 to++;
3554 break;
3555 }
3556 }
3557
3558 /* Ready to "commit" this state change to tgt */
3559 skb_shinfo(tgt)->nr_frags = to;
3560
3561 if (merge >= 0) {
3562 fragfrom = &skb_shinfo(skb)->frags[0];
3563 fragto = &skb_shinfo(tgt)->frags[merge];
3564
Eric Dumazet9e903e02011-10-18 21:00:24 +00003565 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +02003566 __skb_frag_unref(fragfrom, skb->pp_recycle);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08003567 }
3568
3569 /* Reposition in the original skb */
3570 to = 0;
3571 while (from < skb_shinfo(skb)->nr_frags)
3572 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3573 skb_shinfo(skb)->nr_frags = to;
3574
3575 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3576
3577onlymerged:
3578 /* Most likely the tgt won't ever need its checksum anymore, skb on
3579 * the other hand might need it if it needs to be resent
3580 */
3581 tgt->ip_summed = CHECKSUM_PARTIAL;
3582 skb->ip_summed = CHECKSUM_PARTIAL;
3583
3584 /* Yak, is it really working this way? Some helper please? */
3585 skb->len -= shiftlen;
3586 skb->data_len -= shiftlen;
3587 skb->truesize -= shiftlen;
3588 tgt->len += shiftlen;
3589 tgt->data_len += shiftlen;
3590 tgt->truesize += shiftlen;
3591
3592 return shiftlen;
3593}
3594
Thomas Graf677e90e2005-06-23 20:59:51 -07003595/**
3596 * skb_prepare_seq_read - Prepare a sequential read of skb data
3597 * @skb: the buffer to read
3598 * @from: lower offset of data to be read
3599 * @to: upper offset of data to be read
3600 * @st: state variable
3601 *
3602 * Initializes the specified state variable. Must be called before
3603 * invoking skb_seq_read() for the first time.
3604 */
3605void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3606 unsigned int to, struct skb_seq_state *st)
3607{
3608 st->lower_offset = from;
3609 st->upper_offset = to;
3610 st->root_skb = st->cur_skb = skb;
3611 st->frag_idx = st->stepped_offset = 0;
3612 st->frag_data = NULL;
Willem de Bruijn97550f62021-01-09 17:18:33 -05003613 st->frag_off = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003614}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003615EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003616
3617/**
3618 * skb_seq_read - Sequentially read skb data
3619 * @consumed: number of bytes consumed by the caller so far
3620 * @data: destination pointer for data to be returned
3621 * @st: state variable
3622 *
Mathias Krausebc323832013-11-07 14:18:26 +01003623 * Reads a block of skb data at @consumed relative to the
Thomas Graf677e90e2005-06-23 20:59:51 -07003624 * lower offset specified to skb_prepare_seq_read(). Assigns
Mathias Krausebc323832013-11-07 14:18:26 +01003625 * the head of the data block to @data and returns the length
Thomas Graf677e90e2005-06-23 20:59:51 -07003626 * of the block or 0 if the end of the skb data or the upper
3627 * offset has been reached.
3628 *
3629 * The caller is not required to consume all of the data
Mathias Krausebc323832013-11-07 14:18:26 +01003630 * returned, i.e. @consumed is typically set to the number
Thomas Graf677e90e2005-06-23 20:59:51 -07003631 * of bytes already consumed and the next call to
3632 * skb_seq_read() will return the remaining part of the block.
3633 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003634 * Note 1: The size of each block of data returned can be arbitrary,
Masanari Iidae793c0f2014-09-04 23:44:36 +09003635 * this limitation is the cost for zerocopy sequential
Thomas Graf677e90e2005-06-23 20:59:51 -07003636 * reads of potentially non linear data.
3637 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08003638 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07003639 * at the moment, state->root_skb could be replaced with
3640 * a stack for this purpose.
3641 */
3642unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3643 struct skb_seq_state *st)
3644{
3645 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3646 skb_frag_t *frag;
3647
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003648 if (unlikely(abs_offset >= st->upper_offset)) {
3649 if (st->frag_data) {
3650 kunmap_atomic(st->frag_data);
3651 st->frag_data = NULL;
3652 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003653 return 0;
Wedson Almeida Filhoaeb193e2013-06-23 23:33:48 -07003654 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003655
3656next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08003657 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003658
Thomas Chenault995b3372009-05-18 21:43:27 -07003659 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08003660 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07003661 return block_limit - abs_offset;
3662 }
3663
3664 if (st->frag_idx == 0 && !st->frag_data)
3665 st->stepped_offset += skb_headlen(st->cur_skb);
3666
3667 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
Willem de Bruijn97550f62021-01-09 17:18:33 -05003668 unsigned int pg_idx, pg_off, pg_sz;
Thomas Graf677e90e2005-06-23 20:59:51 -07003669
Willem de Bruijn97550f62021-01-09 17:18:33 -05003670 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3671
3672 pg_idx = 0;
3673 pg_off = skb_frag_off(frag);
3674 pg_sz = skb_frag_size(frag);
3675
3676 if (skb_frag_must_loop(skb_frag_page(frag))) {
3677 pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3678 pg_off = offset_in_page(pg_off + st->frag_off);
3679 pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3680 PAGE_SIZE - pg_off);
3681 }
3682
3683 block_limit = pg_sz + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07003684 if (abs_offset < block_limit) {
3685 if (!st->frag_data)
Willem de Bruijn97550f62021-01-09 17:18:33 -05003686 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
Thomas Graf677e90e2005-06-23 20:59:51 -07003687
Willem de Bruijn97550f62021-01-09 17:18:33 -05003688 *data = (u8 *)st->frag_data + pg_off +
Thomas Graf677e90e2005-06-23 20:59:51 -07003689 (abs_offset - st->stepped_offset);
3690
3691 return block_limit - abs_offset;
3692 }
3693
3694 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003695 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003696 st->frag_data = NULL;
3697 }
3698
Willem de Bruijn97550f62021-01-09 17:18:33 -05003699 st->stepped_offset += pg_sz;
3700 st->frag_off += pg_sz;
3701 if (st->frag_off == skb_frag_size(frag)) {
3702 st->frag_off = 0;
3703 st->frag_idx++;
3704 }
Thomas Graf677e90e2005-06-23 20:59:51 -07003705 }
3706
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003707 if (st->frag_data) {
Eric Dumazet51c56b02012-04-05 11:35:15 +02003708 kunmap_atomic(st->frag_data);
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07003709 st->frag_data = NULL;
3710 }
3711
David S. Miller21dc3302010-08-23 00:13:46 -07003712 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08003713 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07003714 st->frag_idx = 0;
3715 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08003716 } else if (st->cur_skb->next) {
3717 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08003718 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07003719 goto next_skb;
3720 }
3721
3722 return 0;
3723}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003724EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003725
3726/**
3727 * skb_abort_seq_read - Abort a sequential read of skb data
3728 * @st: state variable
3729 *
3730 * Must be called if skb_seq_read() was not called until it
3731 * returned 0.
3732 */
3733void skb_abort_seq_read(struct skb_seq_state *st)
3734{
3735 if (st->frag_data)
Eric Dumazet51c56b02012-04-05 11:35:15 +02003736 kunmap_atomic(st->frag_data);
Thomas Graf677e90e2005-06-23 20:59:51 -07003737}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003738EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07003739
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003740#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3741
3742static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3743 struct ts_config *conf,
3744 struct ts_state *state)
3745{
3746 return skb_seq_read(offset, text, TS_SKB_CB(state));
3747}
3748
3749static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3750{
3751 skb_abort_seq_read(TS_SKB_CB(state));
3752}
3753
3754/**
3755 * skb_find_text - Find a text pattern in skb data
3756 * @skb: the buffer to look in
3757 * @from: search offset
3758 * @to: search limit
3759 * @config: textsearch configuration
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003760 *
3761 * Finds a pattern in the skb data according to the specified
3762 * textsearch configuration. Use textsearch_next() to retrieve
3763 * subsequent occurrences of the pattern. Returns the offset
3764 * to the first occurrence or UINT_MAX if no match was found.
3765 */
3766unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
Bojan Prtvar059a2442015-02-22 11:46:35 +01003767 unsigned int to, struct ts_config *config)
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003768{
Bojan Prtvar059a2442015-02-22 11:46:35 +01003769 struct ts_state state;
Phil Oesterf72b9482006-06-26 00:00:57 -07003770 unsigned int ret;
3771
Willem de Bruijnb228c9b2021-03-01 15:09:44 +00003772 BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
3773
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003774 config->get_next_block = skb_ts_get_next_block;
3775 config->finish = skb_ts_finish;
3776
Bojan Prtvar059a2442015-02-22 11:46:35 +01003777 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003778
Bojan Prtvar059a2442015-02-22 11:46:35 +01003779 ret = textsearch_find(config, &state);
Phil Oesterf72b9482006-06-26 00:00:57 -07003780 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003781}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003782EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07003783
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02003784int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3785 int offset, size_t size)
3786{
3787 int i = skb_shinfo(skb)->nr_frags;
3788
3789 if (skb_can_coalesce(skb, i, page, offset)) {
3790 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3791 } else if (i < MAX_SKB_FRAGS) {
3792 get_page(page);
3793 skb_fill_page_desc(skb, i, page, offset, size);
3794 } else {
3795 return -EMSGSIZE;
3796 }
3797
3798 return 0;
3799}
3800EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3801
Herbert Xucbb042f2006-03-20 22:43:56 -08003802/**
3803 * skb_pull_rcsum - pull skb and update receive checksum
3804 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08003805 * @len: length of data pulled
3806 *
3807 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08003808 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07003809 * receive path processing instead of skb_pull unless you know
3810 * that the checksum difference is zero (e.g., a valid IP header)
3811 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08003812 */
Johannes Bergaf728682017-06-16 14:29:22 +02003813void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
Herbert Xucbb042f2006-03-20 22:43:56 -08003814{
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003815 unsigned char *data = skb->data;
3816
Herbert Xucbb042f2006-03-20 22:43:56 -08003817 BUG_ON(len > skb->len);
Pravin B Shelar31b33df2015-09-28 17:24:25 -07003818 __skb_pull(skb, len);
3819 skb_postpull_rcsum(skb, data, len);
3820 return skb->data;
Herbert Xucbb042f2006-03-20 22:43:56 -08003821}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08003822EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3823
Yonghong Song13acc942018-03-21 16:31:03 -07003824static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3825{
3826 skb_frag_t head_frag;
3827 struct page *page;
3828
3829 page = virt_to_head_page(frag_skb->head);
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003830 __skb_frag_set_page(&head_frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07003831 skb_frag_off_set(&head_frag, frag_skb->data -
3832 (unsigned char *)page_address(page));
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07003833 skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
Yonghong Song13acc942018-03-21 16:31:03 -07003834 return head_frag;
3835}
3836
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003837struct sk_buff *skb_segment_list(struct sk_buff *skb,
3838 netdev_features_t features,
3839 unsigned int offset)
3840{
3841 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3842 unsigned int tnl_hlen = skb_tnl_header_len(skb);
3843 unsigned int delta_truesize = 0;
3844 unsigned int delta_len = 0;
3845 struct sk_buff *tail = NULL;
Dongseok Yi53475c52021-01-08 11:28:38 +09003846 struct sk_buff *nskb, *tmp;
3847 int err;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003848
3849 skb_push(skb, -skb_network_offset(skb) + offset);
3850
3851 skb_shinfo(skb)->frag_list = NULL;
3852
3853 do {
3854 nskb = list_skb;
3855 list_skb = list_skb->next;
3856
Dongseok Yi53475c52021-01-08 11:28:38 +09003857 err = 0;
3858 if (skb_shared(nskb)) {
3859 tmp = skb_clone(nskb, GFP_ATOMIC);
3860 if (tmp) {
3861 consume_skb(nskb);
3862 nskb = tmp;
3863 err = skb_unclone(nskb, GFP_ATOMIC);
3864 } else {
3865 err = -ENOMEM;
3866 }
3867 }
3868
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003869 if (!tail)
3870 skb->next = nskb;
3871 else
3872 tail->next = nskb;
3873
Dongseok Yi53475c52021-01-08 11:28:38 +09003874 if (unlikely(err)) {
3875 nskb->next = list_skb;
3876 goto err_linearize;
3877 }
3878
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003879 tail = nskb;
3880
3881 delta_len += nskb->len;
3882 delta_truesize += nskb->truesize;
3883
3884 skb_push(nskb, -skb_network_offset(nskb) + offset);
3885
Florian Westphalcf673ed2020-03-30 18:51:29 +02003886 skb_release_head_state(nskb);
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003887 __copy_skb_header(nskb, skb);
3888
3889 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3890 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3891 nskb->data - tnl_hlen,
3892 offset + tnl_hlen);
3893
3894 if (skb_needs_linearize(nskb, features) &&
3895 __skb_linearize(nskb))
3896 goto err_linearize;
3897
3898 } while (list_skb);
3899
3900 skb->truesize = skb->truesize - delta_truesize;
3901 skb->data_len = skb->data_len - delta_len;
3902 skb->len = skb->len - delta_len;
3903
3904 skb_gso_reset(skb);
3905
3906 skb->prev = tail;
3907
3908 if (skb_needs_linearize(skb, features) &&
3909 __skb_linearize(skb))
3910 goto err_linearize;
3911
3912 skb_get(skb);
3913
3914 return skb;
3915
3916err_linearize:
3917 kfree_skb_list(skb->next);
3918 skb->next = NULL;
3919 return ERR_PTR(-ENOMEM);
3920}
3921EXPORT_SYMBOL_GPL(skb_segment_list);
3922
3923int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3924{
3925 if (unlikely(p->len + skb->len >= 65536))
3926 return -E2BIG;
3927
3928 if (NAPI_GRO_CB(p)->last == p)
3929 skb_shinfo(p)->frag_list = skb;
3930 else
3931 NAPI_GRO_CB(p)->last->next = skb;
3932
3933 skb_pull(skb, skb_gro_offset(skb));
3934
3935 NAPI_GRO_CB(p)->last = skb;
3936 NAPI_GRO_CB(p)->count++;
3937 p->data_len += skb->len;
Paolo Abeni5e10da52021-07-28 18:24:03 +02003938
3939 /* sk owenrship - if any - completely transferred to the aggregated packet */
3940 skb->destructor = NULL;
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003941 p->truesize += skb->truesize;
3942 p->len += skb->len;
3943
3944 NAPI_GRO_CB(skb)->same_flow = 1;
3945
3946 return 0;
3947}
Steffen Klassert3a1296a2020-01-25 11:26:44 +01003948
Herbert Xuf4c50d92006-06-22 03:02:40 -07003949/**
3950 * skb_segment - Perform protocol segmentation on skb.
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003951 * @head_skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07003952 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003953 *
3954 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07003955 * a pointer to the first in a list of new skbs for the segments.
3956 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07003957 */
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003958struct sk_buff *skb_segment(struct sk_buff *head_skb,
3959 netdev_features_t features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07003960{
3961 struct sk_buff *segs = NULL;
3962 struct sk_buff *tail = NULL;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02003963 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003964 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3965 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3966 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02003967 struct sk_buff *frag_skb = head_skb;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003968 unsigned int offset = doffset;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003969 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
Alexander Duyck802ab552016-04-10 21:45:03 -04003970 unsigned int partial_segs = 0;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003971 unsigned int headroom;
Alexander Duyck802ab552016-04-10 21:45:03 -04003972 unsigned int len = head_skb->len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00003973 __be16 proto;
Alexander Duyck36c98382016-05-02 09:38:18 -07003974 bool csum, sg;
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02003975 int nfrags = skb_shinfo(head_skb)->nr_frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07003976 int err = -ENOMEM;
3977 int i = 0;
3978 int pos;
3979
Shmulik Ladkani3dcbdb12019-09-06 12:23:50 +03003980 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3981 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3982 /* gso_size is untrusted, and we have a frag_list with a linear
3983 * non head_frag head.
3984 *
3985 * (we assume checking the first list_skb member suffices;
3986 * i.e if either of the list_skb members have non head_frag
3987 * head, then the first one has too).
3988 *
3989 * If head_skb's headlen does not fit requested gso_size, it
3990 * means that the frag_list members do NOT terminate on exact
3991 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3992 * sharing. Therefore we must fallback to copying the frag_list
3993 * skbs; we do so by disabling SG.
3994 */
3995 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3996 features &= ~NETIF_F_SG;
3997 }
3998
Wei-Chun Chao5882a072014-06-08 23:48:54 -07003999 __skb_push(head_skb, doffset);
Miaohe Lin2f631132020-08-01 17:36:05 +08004000 proto = skb_network_protocol(head_skb, NULL);
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004001 if (unlikely(!proto))
4002 return ERR_PTR(-EINVAL);
4003
Alexander Duyck36c98382016-05-02 09:38:18 -07004004 sg = !!(features & NETIF_F_SG);
Alexander Duyckf245d072016-02-05 15:28:26 -08004005 csum = !!can_checksum_protocol(features, proto);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004006
Steffen Klassert07b26c92016-09-19 12:58:47 +02004007 if (sg && csum && (mss != GSO_BY_FRAGS)) {
4008 if (!(features & NETIF_F_GSO_PARTIAL)) {
4009 struct sk_buff *iter;
Ilan Tayari43170c42017-04-19 21:26:07 +03004010 unsigned int frag_len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004011
4012 if (!list_skb ||
4013 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
4014 goto normal;
4015
Ilan Tayari43170c42017-04-19 21:26:07 +03004016 /* If we get here then all the required
4017 * GSO features except frag_list are supported.
4018 * Try to split the SKB to multiple GSO SKBs
4019 * with no frag_list.
4020 * Currently we can do that only when the buffers don't
4021 * have a linear part and all the buffers except
4022 * the last are of the same length.
Steffen Klassert07b26c92016-09-19 12:58:47 +02004023 */
Ilan Tayari43170c42017-04-19 21:26:07 +03004024 frag_len = list_skb->len;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004025 skb_walk_frags(head_skb, iter) {
Ilan Tayari43170c42017-04-19 21:26:07 +03004026 if (frag_len != iter->len && iter->next)
4027 goto normal;
Ilan Tayarieaffadb2017-04-08 02:07:08 +03004028 if (skb_headlen(iter) && !iter->head_frag)
Steffen Klassert07b26c92016-09-19 12:58:47 +02004029 goto normal;
4030
4031 len -= iter->len;
4032 }
Ilan Tayari43170c42017-04-19 21:26:07 +03004033
4034 if (len != frag_len)
4035 goto normal;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004036 }
4037
4038 /* GSO partial only requires that we trim off any excess that
4039 * doesn't fit into an MSS sized block, so take care of that
4040 * now.
4041 */
Alexander Duyck802ab552016-04-10 21:45:03 -04004042 partial_segs = len / mss;
Alexander Duyckd7fb5a82016-05-02 09:38:12 -07004043 if (partial_segs > 1)
4044 mss *= partial_segs;
4045 else
4046 partial_segs = 0;
Alexander Duyck802ab552016-04-10 21:45:03 -04004047 }
4048
Steffen Klassert07b26c92016-09-19 12:58:47 +02004049normal:
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004050 headroom = skb_headroom(head_skb);
4051 pos = skb_headlen(head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004052
4053 do {
4054 struct sk_buff *nskb;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004055 skb_frag_t *nskb_frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08004056 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004057 int size;
4058
Marcelo Ricardo Leitner3953c462016-06-02 15:05:40 -03004059 if (unlikely(mss == GSO_BY_FRAGS)) {
4060 len = list_skb->len;
4061 } else {
4062 len = head_skb->len - offset;
4063 if (len > mss)
4064 len = mss;
4065 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004066
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004067 hsize = skb_headlen(head_skb) - offset;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004068
Xin Longdbd50f22021-01-15 17:36:38 +08004069 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004070 (skb_headlen(list_skb) == len || sg)) {
4071 BUG_ON(skb_headlen(list_skb) > len);
Herbert Xu89319d382008-12-15 23:26:06 -08004072
Herbert Xu9d8506c2013-11-21 11:10:04 -08004073 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004074 nfrags = skb_shinfo(list_skb)->nr_frags;
4075 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004076 frag_skb = list_skb;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004077 pos += skb_headlen(list_skb);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004078
4079 while (pos < offset + len) {
4080 BUG_ON(i >= nfrags);
4081
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004082 size = skb_frag_size(frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004083 if (pos + size > offset + len)
4084 break;
4085
4086 i++;
4087 pos += size;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004088 frag++;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004089 }
4090
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004091 nskb = skb_clone(list_skb, GFP_ATOMIC);
4092 list_skb = list_skb->next;
Herbert Xu89319d382008-12-15 23:26:06 -08004093
4094 if (unlikely(!nskb))
4095 goto err;
4096
Herbert Xu9d8506c2013-11-21 11:10:04 -08004097 if (unlikely(pskb_trim(nskb, len))) {
4098 kfree_skb(nskb);
4099 goto err;
4100 }
4101
Alexander Duyckec47ea82012-05-04 14:26:56 +00004102 hsize = skb_end_offset(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08004103 if (skb_cow_head(nskb, doffset + headroom)) {
4104 kfree_skb(nskb);
4105 goto err;
4106 }
4107
Alexander Duyckec47ea82012-05-04 14:26:56 +00004108 nskb->truesize += skb_end_offset(nskb) - hsize;
Herbert Xu89319d382008-12-15 23:26:06 -08004109 skb_release_head_state(nskb);
4110 __skb_push(nskb, doffset);
4111 } else {
Paolo Abeni00b229f2021-01-19 17:56:56 +01004112 if (hsize < 0)
4113 hsize = 0;
Xin Longdbd50f22021-01-15 17:36:38 +08004114 if (hsize > len || !sg)
4115 hsize = len;
Xin Longdbd50f22021-01-15 17:36:38 +08004116
Mel Gormanc93bdd02012-07-31 16:44:19 -07004117 nskb = __alloc_skb(hsize + doffset + headroom,
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004118 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
Mel Gormanc93bdd02012-07-31 16:44:19 -07004119 NUMA_NO_NODE);
Herbert Xu89319d382008-12-15 23:26:06 -08004120
4121 if (unlikely(!nskb))
4122 goto err;
4123
4124 skb_reserve(nskb, headroom);
4125 __skb_put(nskb, doffset);
4126 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004127
4128 if (segs)
4129 tail->next = nskb;
4130 else
4131 segs = nskb;
4132 tail = nskb;
4133
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004134 __copy_skb_header(nskb, head_skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004135
Eric Dumazet030737b2013-10-19 11:42:54 -07004136 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
Vlad Yasevichfcdfe3a2014-07-31 10:33:06 -04004137 skb_reset_mac_len(nskb);
Pravin B Shelar68c33162013-02-14 14:02:41 +00004138
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004139 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
Pravin B Shelar68c33162013-02-14 14:02:41 +00004140 nskb->data - tnl_hlen,
4141 doffset + tnl_hlen);
Herbert Xu89319d382008-12-15 23:26:06 -08004142
Herbert Xu9d8506c2013-11-21 11:10:04 -08004143 if (nskb->len == len + doffset)
Simon Horman1cdbcb72013-05-19 15:46:49 +00004144 goto perform_csum_check;
Herbert Xu89319d382008-12-15 23:26:06 -08004145
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004146 if (!sg) {
Yadu Kishore1454c9f2020-03-17 14:08:38 +05304147 if (!csum) {
4148 if (!nskb->remcsum_offload)
4149 nskb->ip_summed = CHECKSUM_NONE;
4150 SKB_GSO_CB(nskb)->csum =
4151 skb_copy_and_csum_bits(head_skb, offset,
4152 skb_put(nskb,
4153 len),
Al Viro8d5930d2020-07-10 20:07:10 -04004154 len);
Yadu Kishore1454c9f2020-03-17 14:08:38 +05304155 SKB_GSO_CB(nskb)->csum_start =
4156 skb_headroom(nskb) + doffset;
4157 } else {
4158 skb_copy_bits(head_skb, offset,
4159 skb_put(nskb, len),
4160 len);
4161 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004162 continue;
4163 }
4164
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004165 nskb_frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004166
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004167 skb_copy_from_linear_data_offset(head_skb, offset,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004168 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004169
Jonathan Lemon06b4feb2021-01-06 14:18:38 -08004170 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4171 SKBFL_SHARED_FRAG;
Eric Dumazetcef401d2013-01-25 20:34:37 +00004172
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004173 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4174 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4175 goto err;
4176
Herbert Xu9d8506c2013-11-21 11:10:04 -08004177 while (pos < offset + len) {
4178 if (i >= nfrags) {
Herbert Xu9d8506c2013-11-21 11:10:04 -08004179 i = 0;
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004180 nfrags = skb_shinfo(list_skb)->nr_frags;
4181 frag = skb_shinfo(list_skb)->frags;
Michael S. Tsirkin1fd819e2014-03-10 19:28:08 +02004182 frag_skb = list_skb;
Yonghong Song13acc942018-03-21 16:31:03 -07004183 if (!skb_headlen(list_skb)) {
4184 BUG_ON(!nfrags);
4185 } else {
4186 BUG_ON(!list_skb->head_frag);
Herbert Xu9d8506c2013-11-21 11:10:04 -08004187
Yonghong Song13acc942018-03-21 16:31:03 -07004188 /* to make room for head_frag. */
4189 i--;
4190 frag--;
4191 }
Willem de Bruijnbf5c25d2017-12-22 19:00:17 -05004192 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4193 skb_zerocopy_clone(nskb, frag_skb,
4194 GFP_ATOMIC))
4195 goto err;
4196
Michael S. Tsirkin1a4ceda2014-03-10 19:27:59 +02004197 list_skb = list_skb->next;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004198 }
4199
4200 if (unlikely(skb_shinfo(nskb)->nr_frags >=
4201 MAX_SKB_FRAGS)) {
4202 net_warn_ratelimited(
4203 "skb_segment: too many frags: %u %u\n",
4204 pos, mss);
Eric Dumazetff907a12018-07-19 16:04:38 -07004205 err = -EINVAL;
Herbert Xu9d8506c2013-11-21 11:10:04 -08004206 goto err;
4207 }
4208
Yonghong Song13acc942018-03-21 16:31:03 -07004209 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004210 __skb_frag_ref(nskb_frag);
4211 size = skb_frag_size(nskb_frag);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004212
4213 if (pos < offset) {
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004214 skb_frag_off_add(nskb_frag, offset - pos);
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004215 skb_frag_size_sub(nskb_frag, offset - pos);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004216 }
4217
Herbert Xu89319d382008-12-15 23:26:06 -08004218 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004219
4220 if (pos + size <= offset + len) {
4221 i++;
Michael S. Tsirkin4e1beba2014-03-10 18:29:14 +02004222 frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004223 pos += size;
4224 } else {
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004225 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
Herbert Xu89319d382008-12-15 23:26:06 -08004226 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004227 }
4228
Michael S. Tsirkin8cb19902014-03-10 18:29:04 +02004229 nskb_frag++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07004230 }
4231
Herbert Xu89319d382008-12-15 23:26:06 -08004232skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07004233 nskb->data_len = len - hsize;
4234 nskb->len += nskb->data_len;
4235 nskb->truesize += nskb->data_len;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004236
Simon Horman1cdbcb72013-05-19 15:46:49 +00004237perform_csum_check:
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004238 if (!csum) {
Eric Dumazetff907a12018-07-19 16:04:38 -07004239 if (skb_has_shared_frag(nskb) &&
4240 __skb_linearize(nskb))
4241 goto err;
4242
Alexander Duyck7fbeffe2016-02-05 15:27:43 -08004243 if (!nskb->remcsum_offload)
4244 nskb->ip_summed = CHECKSUM_NONE;
Alexander Duyck76443452016-02-05 15:27:37 -08004245 SKB_GSO_CB(nskb)->csum =
4246 skb_checksum(nskb, doffset,
4247 nskb->len - doffset, 0);
Tom Herbert7e2b10c2014-06-04 17:20:02 -07004248 SKB_GSO_CB(nskb)->csum_start =
Alexander Duyck76443452016-02-05 15:27:37 -08004249 skb_headroom(nskb) + doffset;
Pravin B Shelarec5f0612013-03-07 09:28:01 +00004250 }
Michael S. Tsirkindf5771f2014-03-10 18:29:19 +02004251 } while ((offset += len) < head_skb->len);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004252
Eric Dumazetbec3cfd2014-10-03 20:59:19 -07004253 /* Some callers want to get the end of the list.
4254 * Put it in segs->prev to avoid walking the list.
4255 * (see validate_xmit_skb_list() for example)
4256 */
4257 segs->prev = tail;
Toshiaki Makita432c8562014-10-27 10:30:51 -07004258
Alexander Duyck802ab552016-04-10 21:45:03 -04004259 if (partial_segs) {
Steffen Klassert07b26c92016-09-19 12:58:47 +02004260 struct sk_buff *iter;
Alexander Duyck802ab552016-04-10 21:45:03 -04004261 int type = skb_shinfo(head_skb)->gso_type;
Steffen Klassert07b26c92016-09-19 12:58:47 +02004262 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
Alexander Duyck802ab552016-04-10 21:45:03 -04004263
4264 /* Update type to add partial and then remove dodgy if set */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004265 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
Alexander Duyck802ab552016-04-10 21:45:03 -04004266 type &= ~SKB_GSO_DODGY;
4267
4268 /* Update GSO info and prepare to start updating headers on
4269 * our way back down the stack of protocols.
4270 */
Steffen Klassert07b26c92016-09-19 12:58:47 +02004271 for (iter = segs; iter; iter = iter->next) {
4272 skb_shinfo(iter)->gso_size = gso_size;
4273 skb_shinfo(iter)->gso_segs = partial_segs;
4274 skb_shinfo(iter)->gso_type = type;
4275 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4276 }
4277
4278 if (tail->len - doffset <= gso_size)
4279 skb_shinfo(tail)->gso_size = 0;
4280 else if (tail != segs)
4281 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
Alexander Duyck802ab552016-04-10 21:45:03 -04004282 }
4283
Toshiaki Makita432c8562014-10-27 10:30:51 -07004284 /* Following permits correct backpressure, for protocols
4285 * using skb_set_owner_w().
4286 * Idea is to tranfert ownership from head_skb to last segment.
4287 */
4288 if (head_skb->destructor == sock_wfree) {
4289 swap(tail->truesize, head_skb->truesize);
4290 swap(tail->destructor, head_skb->destructor);
4291 swap(tail->sk, head_skb->sk);
4292 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07004293 return segs;
4294
4295err:
Eric Dumazet289dccb2013-12-20 14:29:08 -08004296 kfree_skb_list(segs);
Herbert Xuf4c50d92006-06-22 03:02:40 -07004297 return ERR_PTR(err);
4298}
Herbert Xuf4c50d92006-06-22 03:02:40 -07004299EXPORT_SYMBOL_GPL(skb_segment);
4300
David Millerd4546c22018-06-24 14:13:49 +09004301int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
Herbert Xu71d93b32008-12-15 23:42:33 -08004302{
Eric Dumazet8a291112013-10-08 09:02:23 -07004303 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00004304 unsigned int offset = skb_gro_offset(skb);
4305 unsigned int headlen = skb_headlen(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004306 unsigned int len = skb_gro_len(skb);
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004307 unsigned int delta_truesize;
Paolo Abeni5e10da52021-07-28 18:24:03 +02004308 unsigned int new_truesize;
David Millerd4546c22018-06-24 14:13:49 +09004309 struct sk_buff *lp;
Herbert Xu71d93b32008-12-15 23:42:33 -08004310
Steffen Klassert0ab03f32019-04-02 08:16:03 +02004311 if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
Herbert Xu71d93b32008-12-15 23:42:33 -08004312 return -E2BIG;
4313
Eric Dumazet29e98242014-05-16 11:34:37 -07004314 lp = NAPI_GRO_CB(p)->last;
Eric Dumazet8a291112013-10-08 09:02:23 -07004315 pinfo = skb_shinfo(lp);
4316
4317 if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00004318 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004319 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004320 int i = skbinfo->nr_frags;
4321 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00004322
Herbert Xu66e92fc2009-05-26 18:50:32 +00004323 if (nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004324 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00004325
Eric Dumazet8a291112013-10-08 09:02:23 -07004326 offset -= headlen;
Herbert Xu9aaa1562009-05-26 18:50:33 +00004327 pinfo->nr_frags = nr_frags;
4328 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08004329
Herbert Xu9aaa1562009-05-26 18:50:33 +00004330 frag = pinfo->frags + nr_frags;
4331 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00004332 do {
4333 *--frag = *--frag2;
4334 } while (--i);
4335
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004336 skb_frag_off_add(frag, offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004337 skb_frag_size_sub(frag, offset);
Herbert Xu66e92fc2009-05-26 18:50:32 +00004338
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004339 /* all fragments truesize : remove (head size + sk_buff) */
Paolo Abeni5e10da52021-07-28 18:24:03 +02004340 new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
4341 delta_truesize = skb->truesize - new_truesize;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004342
Paolo Abeni5e10da52021-07-28 18:24:03 +02004343 skb->truesize = new_truesize;
Herbert Xuf5572062009-01-14 20:40:03 -08004344 skb->len -= skb->data_len;
4345 skb->data_len = 0;
4346
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004347 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
Herbert Xu5d38a072009-01-04 16:13:40 -08004348 goto done;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004349 } else if (skb->head_frag) {
4350 int nr_frags = pinfo->nr_frags;
4351 skb_frag_t *frag = pinfo->frags + nr_frags;
4352 struct page *page = virt_to_head_page(skb->head);
4353 unsigned int first_size = headlen - offset;
4354 unsigned int first_offset;
4355
4356 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazet8a291112013-10-08 09:02:23 -07004357 goto merge;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004358
4359 first_offset = skb->data -
4360 (unsigned char *)page_address(page) +
4361 offset;
4362
4363 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4364
Matthew Wilcox (Oracle)d8e18a52019-07-22 20:08:26 -07004365 __skb_frag_set_page(frag, page);
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004366 skb_frag_off_set(frag, first_offset);
Eric Dumazetd7e88832012-04-30 08:10:34 +00004367 skb_frag_size_set(frag, first_size);
4368
4369 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4370 /* We dont need to clear skbinfo->nr_frags here */
4371
Paolo Abeniaf352462021-08-04 21:07:00 +02004372 new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
Paolo Abeni5e10da52021-07-28 18:24:03 +02004373 delta_truesize = skb->truesize - new_truesize;
4374 skb->truesize = new_truesize;
Eric Dumazetd7e88832012-04-30 08:10:34 +00004375 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4376 goto done;
Eric Dumazet8a291112013-10-08 09:02:23 -07004377 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004378
4379merge:
Paolo Abeni5e10da52021-07-28 18:24:03 +02004380 /* sk owenrship - if any - completely transferred to the aggregated packet */
4381 skb->destructor = NULL;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004382 delta_truesize = skb->truesize;
Herbert Xu67147ba2009-05-26 18:50:22 +00004383 if (offset > headlen) {
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004384 unsigned int eat = offset - headlen;
4385
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004386 skb_frag_off_add(&skbinfo->frags[0], eat);
Eric Dumazet9e903e02011-10-18 21:00:24 +00004387 skb_frag_size_sub(&skbinfo->frags[0], eat);
Michal Schmidtd1dc7ab2011-01-24 12:08:48 +00004388 skb->data_len -= eat;
4389 skb->len -= eat;
Herbert Xu67147ba2009-05-26 18:50:22 +00004390 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08004391 }
4392
Herbert Xu67147ba2009-05-26 18:50:22 +00004393 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08004394
Eric Dumazet29e98242014-05-16 11:34:37 -07004395 if (NAPI_GRO_CB(p)->last == p)
Eric Dumazet8a291112013-10-08 09:02:23 -07004396 skb_shinfo(p)->frag_list = skb;
4397 else
4398 NAPI_GRO_CB(p)->last->next = skb;
Eric Dumazetc3c7c252012-12-06 13:54:59 +00004399 NAPI_GRO_CB(p)->last = skb;
Eric Dumazetf4a775d2014-09-22 16:29:32 -07004400 __skb_header_release(skb);
Eric Dumazet8a291112013-10-08 09:02:23 -07004401 lp = p;
Herbert Xu71d93b32008-12-15 23:42:33 -08004402
Herbert Xu5d38a072009-01-04 16:13:40 -08004403done:
4404 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00004405 p->data_len += len;
Eric Dumazet715dc1f2012-05-02 23:33:21 +00004406 p->truesize += delta_truesize;
Herbert Xu37fe4732009-01-17 19:48:13 +00004407 p->len += len;
Eric Dumazet8a291112013-10-08 09:02:23 -07004408 if (lp != p) {
4409 lp->data_len += len;
4410 lp->truesize += delta_truesize;
4411 lp->len += len;
4412 }
Herbert Xu71d93b32008-12-15 23:42:33 -08004413 NAPI_GRO_CB(skb)->same_flow = 1;
4414 return 0;
4415}
Herbert Xu71d93b32008-12-15 23:42:33 -08004416
Florian Westphaldf5042f2018-12-18 17:15:16 +01004417#ifdef CONFIG_SKB_EXTENSIONS
4418#define SKB_EXT_ALIGN_VALUE 8
4419#define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4420
4421static const u8 skb_ext_type_len[] = {
4422#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4423 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4424#endif
Florian Westphal41650792018-12-18 17:15:27 +01004425#ifdef CONFIG_XFRM
4426 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4427#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004428#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4429 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4430#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004431#if IS_ENABLED(CONFIG_MPTCP)
4432 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4433#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004434};
4435
4436static __always_inline unsigned int skb_ext_total_length(void)
4437{
4438 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4439#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4440 skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4441#endif
Florian Westphal41650792018-12-18 17:15:27 +01004442#ifdef CONFIG_XFRM
4443 skb_ext_type_len[SKB_EXT_SEC_PATH] +
4444#endif
Paul Blakey95a72332019-09-04 16:56:37 +03004445#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4446 skb_ext_type_len[TC_SKB_EXT] +
4447#endif
Mat Martineau3ee17bc2020-01-09 07:59:19 -08004448#if IS_ENABLED(CONFIG_MPTCP)
4449 skb_ext_type_len[SKB_EXT_MPTCP] +
4450#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01004451 0;
4452}
4453
4454static void skb_extensions_init(void)
4455{
4456 BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4457 BUILD_BUG_ON(skb_ext_total_length() > 255);
4458
4459 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4460 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4461 0,
4462 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4463 NULL);
4464}
4465#else
4466static void skb_extensions_init(void) {}
4467#endif
4468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469void __init skb_init(void)
4470{
Kees Cook79a8a642018-02-07 17:44:38 -08004471 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 sizeof(struct sk_buff),
4473 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004474 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Kees Cook79a8a642018-02-07 17:44:38 -08004475 offsetof(struct sk_buff, cb),
4476 sizeof_field(struct sk_buff, cb),
Paul Mundt20c2df82007-07-20 10:11:58 +09004477 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07004478 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07004479 sizeof(struct sk_buff_fclones),
David S. Millerd179cd12005-08-17 14:57:30 -07004480 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004481 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004482 NULL);
Florian Westphaldf5042f2018-12-18 17:15:16 +01004483 skb_extensions_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
David S. Miller51c739d2007-10-30 21:29:29 -07004486static int
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004487__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4488 unsigned int recursion_level)
David Howells716ea3a2007-04-02 20:19:53 -07004489{
David S. Miller1a028e52007-04-27 15:21:23 -07004490 int start = skb_headlen(skb);
4491 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07004492 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07004493 int elt = 0;
4494
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004495 if (unlikely(recursion_level >= 24))
4496 return -EMSGSIZE;
4497
David Howells716ea3a2007-04-02 20:19:53 -07004498 if (copy > 0) {
4499 if (copy > len)
4500 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02004501 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07004502 elt++;
4503 if ((len -= copy) == 0)
4504 return elt;
4505 offset += copy;
4506 }
4507
4508 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07004509 int end;
David Howells716ea3a2007-04-02 20:19:53 -07004510
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004511 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07004512
Eric Dumazet9e903e02011-10-18 21:00:24 +00004513 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
David Howells716ea3a2007-04-02 20:19:53 -07004514 if ((copy = end - offset) > 0) {
4515 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004516 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4517 return -EMSGSIZE;
David Howells716ea3a2007-04-02 20:19:53 -07004518
4519 if (copy > len)
4520 copy = len;
Ian Campbellea2ab692011-08-22 23:44:58 +00004521 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07004522 skb_frag_off(frag) + offset - start);
David Howells716ea3a2007-04-02 20:19:53 -07004523 elt++;
4524 if (!(len -= copy))
4525 return elt;
4526 offset += copy;
4527 }
David S. Miller1a028e52007-04-27 15:21:23 -07004528 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004529 }
4530
David S. Millerfbb398a2009-06-09 00:18:59 -07004531 skb_walk_frags(skb, frag_iter) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004532 int end, ret;
David Howells716ea3a2007-04-02 20:19:53 -07004533
David S. Millerfbb398a2009-06-09 00:18:59 -07004534 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07004535
David S. Millerfbb398a2009-06-09 00:18:59 -07004536 end = start + frag_iter->len;
4537 if ((copy = end - offset) > 0) {
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004538 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4539 return -EMSGSIZE;
4540
David S. Millerfbb398a2009-06-09 00:18:59 -07004541 if (copy > len)
4542 copy = len;
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004543 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4544 copy, recursion_level + 1);
4545 if (unlikely(ret < 0))
4546 return ret;
4547 elt += ret;
David S. Millerfbb398a2009-06-09 00:18:59 -07004548 if ((len -= copy) == 0)
4549 return elt;
4550 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07004551 }
David S. Millerfbb398a2009-06-09 00:18:59 -07004552 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07004553 }
4554 BUG_ON(len);
4555 return elt;
4556}
4557
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004558/**
4559 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4560 * @skb: Socket buffer containing the buffers to be mapped
4561 * @sg: The scatter-gather list to map into
4562 * @offset: The offset into the buffer's contents to start mapping
4563 * @len: Length of buffer space to be mapped
4564 *
4565 * Fill the specified scatter-gather list with mappings/pointers into a
4566 * region of the buffer space attached to a socket buffer. Returns either
4567 * the number of scatterlist items used, or -EMSGSIZE if the contents
4568 * could not fit.
4569 */
4570int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4571{
4572 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4573
4574 if (nsg <= 0)
4575 return nsg;
4576
4577 sg_mark_end(&sg[nsg - 1]);
4578
4579 return nsg;
4580}
4581EXPORT_SYMBOL_GPL(skb_to_sgvec);
4582
Fan Du25a91d82014-01-18 09:54:23 +08004583/* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4584 * sglist without mark the sg which contain last skb data as the end.
4585 * So the caller can mannipulate sg list as will when padding new data after
4586 * the first call without calling sg_unmark_end to expend sg list.
4587 *
4588 * Scenario to use skb_to_sgvec_nomark:
4589 * 1. sg_init_table
4590 * 2. skb_to_sgvec_nomark(payload1)
4591 * 3. skb_to_sgvec_nomark(payload2)
4592 *
4593 * This is equivalent to:
4594 * 1. sg_init_table
4595 * 2. skb_to_sgvec(payload1)
4596 * 3. sg_unmark_end
4597 * 4. skb_to_sgvec(payload2)
4598 *
4599 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4600 * is more preferable.
4601 */
4602int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4603 int offset, int len)
4604{
Jason A. Donenfeld48a1df62017-06-04 04:16:22 +02004605 return __skb_to_sgvec(skb, sg, offset, len, 0);
Fan Du25a91d82014-01-18 09:54:23 +08004606}
4607EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4608
David S. Miller51c739d2007-10-30 21:29:29 -07004609
David S. Miller51c739d2007-10-30 21:29:29 -07004610
David Howells716ea3a2007-04-02 20:19:53 -07004611/**
4612 * skb_cow_data - Check that a socket buffer's data buffers are writable
4613 * @skb: The socket buffer to check.
4614 * @tailbits: Amount of trailing space to be added
4615 * @trailer: Returned pointer to the skb where the @tailbits space begins
4616 *
4617 * Make sure that the data buffers attached to a socket buffer are
4618 * writable. If they are not, private copies are made of the data buffers
4619 * and the socket buffer is set to use these instead.
4620 *
4621 * If @tailbits is given, make sure that there is space to write @tailbits
4622 * bytes of data beyond current end of socket buffer. @trailer will be
4623 * set to point to the skb in which this space begins.
4624 *
4625 * The number of scatterlist elements required to completely map the
4626 * COW'd and extended socket buffer will be returned.
4627 */
4628int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4629{
4630 int copyflag;
4631 int elt;
4632 struct sk_buff *skb1, **skb_p;
4633
4634 /* If skb is cloned or its head is paged, reallocate
4635 * head pulling out all the pages (pages are considered not writable
4636 * at the moment even if they are anonymous).
4637 */
4638 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
Miaohe Linc15fc192020-08-01 17:30:23 +08004639 !__pskb_pull_tail(skb, __skb_pagelen(skb)))
David Howells716ea3a2007-04-02 20:19:53 -07004640 return -ENOMEM;
4641
4642 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07004643 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07004644 /* A little of trouble, not enough of space for trailer.
4645 * This should not happen, when stack is tuned to generate
4646 * good frames. OK, on miss we reallocate and reserve even more
4647 * space, 128 bytes is fair. */
4648
4649 if (skb_tailroom(skb) < tailbits &&
4650 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4651 return -ENOMEM;
4652
4653 /* Voila! */
4654 *trailer = skb;
4655 return 1;
4656 }
4657
4658 /* Misery. We are in troubles, going to mincer fragments... */
4659
4660 elt = 1;
4661 skb_p = &skb_shinfo(skb)->frag_list;
4662 copyflag = 0;
4663
4664 while ((skb1 = *skb_p) != NULL) {
4665 int ntail = 0;
4666
4667 /* The fragment is partially pulled by someone,
4668 * this can happen on input. Copy it and everything
4669 * after it. */
4670
4671 if (skb_shared(skb1))
4672 copyflag = 1;
4673
4674 /* If the skb is the last, worry about trailer. */
4675
4676 if (skb1->next == NULL && tailbits) {
4677 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004678 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07004679 skb_tailroom(skb1) < tailbits)
4680 ntail = tailbits + 128;
4681 }
4682
4683 if (copyflag ||
4684 skb_cloned(skb1) ||
4685 ntail ||
4686 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07004687 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07004688 struct sk_buff *skb2;
4689
4690 /* Fuck, we are miserable poor guys... */
4691 if (ntail == 0)
4692 skb2 = skb_copy(skb1, GFP_ATOMIC);
4693 else
4694 skb2 = skb_copy_expand(skb1,
4695 skb_headroom(skb1),
4696 ntail,
4697 GFP_ATOMIC);
4698 if (unlikely(skb2 == NULL))
4699 return -ENOMEM;
4700
4701 if (skb1->sk)
4702 skb_set_owner_w(skb2, skb1->sk);
4703
4704 /* Looking around. Are we still alive?
4705 * OK, link new skb, drop old one */
4706
4707 skb2->next = skb1->next;
4708 *skb_p = skb2;
4709 kfree_skb(skb1);
4710 skb1 = skb2;
4711 }
4712 elt++;
4713 *trailer = skb1;
4714 skb_p = &skb1->next;
4715 }
4716
4717 return elt;
4718}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08004719EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07004720
Eric Dumazetb1faf562010-05-31 23:44:05 -07004721static void sock_rmem_free(struct sk_buff *skb)
4722{
4723 struct sock *sk = skb->sk;
4724
4725 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4726}
4727
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004728static void skb_set_err_queue(struct sk_buff *skb)
4729{
4730 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4731 * So, it is safe to (mis)use it to mark skbs on the error queue.
4732 */
4733 skb->pkt_type = PACKET_OUTGOING;
4734 BUILD_BUG_ON(PACKET_OUTGOING == 0);
4735}
4736
Eric Dumazetb1faf562010-05-31 23:44:05 -07004737/*
4738 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4739 */
4740int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4741{
4742 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazetebb3b782019-10-10 20:17:44 -07004743 (unsigned int)READ_ONCE(sk->sk_rcvbuf))
Eric Dumazetb1faf562010-05-31 23:44:05 -07004744 return -ENOMEM;
4745
4746 skb_orphan(skb);
4747 skb->sk = sk;
4748 skb->destructor = sock_rmem_free;
4749 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
Soheil Hassas Yeganeh8605330a2017-03-18 17:02:59 -04004750 skb_set_err_queue(skb);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004751
Eric Dumazetabb57ea2011-05-18 02:21:31 -04004752 /* before exiting rcu section, make sure dst is refcounted */
4753 skb_dst_force(skb);
4754
Eric Dumazetb1faf562010-05-31 23:44:05 -07004755 skb_queue_tail(&sk->sk_error_queue, skb);
4756 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04004757 sk_error_report(sk);
Eric Dumazetb1faf562010-05-31 23:44:05 -07004758 return 0;
4759}
4760EXPORT_SYMBOL(sock_queue_err_skb);
4761
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004762static bool is_icmp_err_skb(const struct sk_buff *skb)
4763{
4764 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4765 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4766}
4767
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004768struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4769{
4770 struct sk_buff_head *q = &sk->sk_error_queue;
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004771 struct sk_buff *skb, *skb_next = NULL;
4772 bool icmp_next = false;
Eric Dumazet997d5c32015-02-18 05:47:55 -08004773 unsigned long flags;
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004774
Eric Dumazet997d5c32015-02-18 05:47:55 -08004775 spin_lock_irqsave(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004776 skb = __skb_dequeue(q);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004777 if (skb && (skb_next = skb_peek(q))) {
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004778 icmp_next = is_icmp_err_skb(skb_next);
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004779 if (icmp_next)
Willem de Bruijn985f7332020-11-26 10:12:20 -05004780 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
Soheil Hassas Yeganeh38b25792017-06-02 12:38:22 -04004781 }
Eric Dumazet997d5c32015-02-18 05:47:55 -08004782 spin_unlock_irqrestore(&q->lock, flags);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004783
Soheil Hassas Yeganeh83a1a1a2016-11-30 14:01:08 -05004784 if (is_icmp_err_skb(skb) && !icmp_next)
4785 sk->sk_err = 0;
4786
4787 if (skb_next)
Alexander Aringe3ae2362021-06-27 18:48:21 -04004788 sk_error_report(sk);
Willem de Bruijn364a9e92014-08-31 21:30:27 -04004789
4790 return skb;
4791}
4792EXPORT_SYMBOL(sock_dequeue_err_skb);
4793
Alexander Duyckcab41c42014-09-10 18:05:26 -04004794/**
4795 * skb_clone_sk - create clone of skb, and take reference to socket
4796 * @skb: the skb to clone
4797 *
4798 * This function creates a clone of a buffer that holds a reference on
4799 * sk_refcnt. Buffers created via this function are meant to be
4800 * returned using sock_queue_err_skb, or free via kfree_skb.
4801 *
4802 * When passing buffers allocated with this function to sock_queue_err_skb
4803 * it is necessary to wrap the call with sock_hold/sock_put in order to
4804 * prevent the socket from being released prior to being enqueued on
4805 * the sk_error_queue.
4806 */
Alexander Duyck62bccb82014-09-04 13:31:35 -04004807struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4808{
4809 struct sock *sk = skb->sk;
4810 struct sk_buff *clone;
4811
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004812 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
Alexander Duyck62bccb82014-09-04 13:31:35 -04004813 return NULL;
4814
4815 clone = skb_clone(skb, GFP_ATOMIC);
4816 if (!clone) {
4817 sock_put(sk);
4818 return NULL;
4819 }
4820
4821 clone->sk = sk;
4822 clone->destructor = sock_efree;
4823
4824 return clone;
4825}
4826EXPORT_SYMBOL(skb_clone_sk);
4827
Alexander Duyck37846ef2014-09-04 13:31:10 -04004828static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4829 struct sock *sk,
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004830 int tstype,
4831 bool opt_stats)
Patrick Ohlyac45f602009-02-12 05:03:37 +00004832{
Patrick Ohlyac45f602009-02-12 05:03:37 +00004833 struct sock_exterr_skb *serr;
Patrick Ohlyac45f602009-02-12 05:03:37 +00004834 int err;
4835
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004836 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4837
Patrick Ohlyac45f602009-02-12 05:03:37 +00004838 serr = SKB_EXT_ERR(skb);
4839 memset(serr, 0, sizeof(*serr));
4840 serr->ee.ee_errno = ENOMSG;
4841 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004842 serr->ee.ee_info = tstype;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004843 serr->opt_stats = opt_stats;
Willem de Bruijn1862d622017-04-12 19:24:35 -04004844 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004845 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
Willem de Bruijn09c2d252014-08-04 22:11:47 -04004846 serr->ee.ee_data = skb_shinfo(skb)->tskey;
WANG Congac5cc972015-12-16 23:39:04 -08004847 if (sk->sk_protocol == IPPROTO_TCP &&
4848 sk->sk_type == SOCK_STREAM)
Willem de Bruijn4ed2d762014-08-04 22:11:49 -04004849 serr->ee.ee_data -= sk->sk_tskey;
4850 }
Eric Dumazet29030372010-05-29 00:20:48 -07004851
Patrick Ohlyac45f602009-02-12 05:03:37 +00004852 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07004853
Patrick Ohlyac45f602009-02-12 05:03:37 +00004854 if (err)
4855 kfree_skb(skb);
4856}
Alexander Duyck37846ef2014-09-04 13:31:10 -04004857
Willem de Bruijnb245be12015-01-30 13:29:32 -05004858static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4859{
4860 bool ret;
4861
4862 if (likely(sysctl_tstamp_allow_data || tsonly))
4863 return true;
4864
4865 read_lock_bh(&sk->sk_callback_lock);
4866 ret = sk->sk_socket && sk->sk_socket->file &&
4867 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4868 read_unlock_bh(&sk->sk_callback_lock);
4869 return ret;
4870}
4871
Alexander Duyck37846ef2014-09-04 13:31:10 -04004872void skb_complete_tx_timestamp(struct sk_buff *skb,
4873 struct skb_shared_hwtstamps *hwtstamps)
4874{
4875 struct sock *sk = skb->sk;
4876
Willem de Bruijnb245be12015-01-30 13:29:32 -05004877 if (!skb_may_tx_timestamp(sk, false))
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004878 goto err;
Willem de Bruijnb245be12015-01-30 13:29:32 -05004879
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004880 /* Take a reference to prevent skb_orphan() from freeing the socket,
4881 * but only if the socket refcount is not zero.
4882 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004883 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004884 *skb_hwtstamps(skb) = *hwtstamps;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004885 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004886 sock_put(sk);
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004887 return;
Eric Dumazet9ac25fc2017-03-03 21:01:03 -08004888 }
Willem de Bruijn35b99df2017-12-13 14:41:06 -05004889
4890err:
4891 kfree_skb(skb);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004892}
4893EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4894
4895void __skb_tstamp_tx(struct sk_buff *orig_skb,
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004896 const struct sk_buff *ack_skb,
Alexander Duyck37846ef2014-09-04 13:31:10 -04004897 struct skb_shared_hwtstamps *hwtstamps,
4898 struct sock *sk, int tstype)
4899{
4900 struct sk_buff *skb;
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004901 bool tsonly, opt_stats = false;
Alexander Duyck37846ef2014-09-04 13:31:10 -04004902
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004903 if (!sk)
4904 return;
4905
Miroslav Lichvarb50a5c72017-05-19 17:52:40 +02004906 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4907 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4908 return;
4909
Willem de Bruijn3a8dd972015-03-11 15:43:55 -04004910 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4911 if (!skb_may_tx_timestamp(sk, tsonly))
Alexander Duyck37846ef2014-09-04 13:31:10 -04004912 return;
4913
Francis Yan1c885802016-11-27 23:07:18 -08004914 if (tsonly) {
4915#ifdef CONFIG_INET
4916 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4917 sk->sk_protocol == IPPROTO_TCP &&
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004918 sk->sk_type == SOCK_STREAM) {
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004919 skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
4920 ack_skb);
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004921 opt_stats = true;
4922 } else
Francis Yan1c885802016-11-27 23:07:18 -08004923#endif
4924 skb = alloc_skb(0, GFP_ATOMIC);
4925 } else {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004926 skb = skb_clone(orig_skb, GFP_ATOMIC);
Francis Yan1c885802016-11-27 23:07:18 -08004927 }
Alexander Duyck37846ef2014-09-04 13:31:10 -04004928 if (!skb)
4929 return;
4930
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004931 if (tsonly) {
Willem de Bruijnfff88032017-06-08 11:35:03 -04004932 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4933 SKBTX_ANY_TSTAMP;
Willem de Bruijn49ca0d82015-01-30 13:29:31 -05004934 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4935 }
4936
4937 if (hwtstamps)
4938 *skb_hwtstamps(skb) = *hwtstamps;
4939 else
4940 skb->tstamp = ktime_get_real();
4941
Soheil Hassas Yeganeh4ef1b282017-03-18 17:03:00 -04004942 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
Alexander Duyck37846ef2014-09-04 13:31:10 -04004943}
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004944EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4945
4946void skb_tstamp_tx(struct sk_buff *orig_skb,
4947 struct skb_shared_hwtstamps *hwtstamps)
4948{
Yousuk Seunge7ed11e2021-01-20 12:41:55 -08004949 return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
Willem de Bruijne7fd2882014-08-04 22:11:48 -04004950 SCM_TSTAMP_SND);
4951}
Patrick Ohlyac45f602009-02-12 05:03:37 +00004952EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4953
Johannes Berg6e3e9392011-11-09 10:15:42 +01004954void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4955{
4956 struct sock *sk = skb->sk;
4957 struct sock_exterr_skb *serr;
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004958 int err = 1;
Johannes Berg6e3e9392011-11-09 10:15:42 +01004959
4960 skb->wifi_acked_valid = 1;
4961 skb->wifi_acked = acked;
4962
4963 serr = SKB_EXT_ERR(skb);
4964 memset(serr, 0, sizeof(*serr));
4965 serr->ee.ee_errno = ENOMSG;
4966 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4967
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004968 /* Take a reference to prevent skb_orphan() from freeing the socket,
4969 * but only if the socket refcount is not zero.
4970 */
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004971 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
Eric Dumazetdd4f1072017-03-03 21:01:02 -08004972 err = sock_queue_err_skb(sk, skb);
4973 sock_put(sk);
4974 }
Johannes Berg6e3e9392011-11-09 10:15:42 +01004975 if (err)
4976 kfree_skb(skb);
4977}
4978EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4979
Rusty Russellf35d9d82008-02-04 23:49:54 -05004980/**
4981 * skb_partial_csum_set - set up and verify partial csum values for packet
4982 * @skb: the skb to set
4983 * @start: the number of bytes after skb->data to start checksumming.
4984 * @off: the offset from start to place the checksum.
4985 *
4986 * For untrusted partially-checksummed packets, we need to make sure the values
4987 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4988 *
4989 * This function checks and sets those values and skb->ip_summed: if this
4990 * returns false you should drop the packet.
4991 */
4992bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4993{
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07004994 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4995 u32 csum_start = skb_headroom(skb) + (u32)start;
4996
4997 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4998 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4999 start, off, skb_headroom(skb), skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05005000 return false;
5001 }
5002 skb->ip_summed = CHECKSUM_PARTIAL;
Eric Dumazet52b5d6f2018-10-10 06:59:35 -07005003 skb->csum_start = csum_start;
Rusty Russellf35d9d82008-02-04 23:49:54 -05005004 skb->csum_offset = off;
Jason Wange5d5dec2013-03-26 23:11:20 +00005005 skb_set_transport_header(skb, start);
Rusty Russellf35d9d82008-02-04 23:49:54 -05005006 return true;
5007}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08005008EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05005009
Paul Durranted1f50c2014-01-09 10:02:46 +00005010static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5011 unsigned int max)
5012{
5013 if (skb_headlen(skb) >= len)
5014 return 0;
5015
5016 /* If we need to pullup then pullup to the max, so we
5017 * won't need to do it again.
5018 */
5019 if (max > skb->len)
5020 max = skb->len;
5021
5022 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5023 return -ENOMEM;
5024
5025 if (skb_headlen(skb) < len)
5026 return -EPROTO;
5027
5028 return 0;
5029}
5030
Jan Beulichf9708b42014-03-11 13:56:05 +00005031#define MAX_TCP_HDR_LEN (15 * 4)
5032
5033static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5034 typeof(IPPROTO_IP) proto,
5035 unsigned int off)
5036{
Kees Cook161d1792020-02-19 22:23:04 -08005037 int err;
Jan Beulichf9708b42014-03-11 13:56:05 +00005038
Kees Cook161d1792020-02-19 22:23:04 -08005039 switch (proto) {
Jan Beulichf9708b42014-03-11 13:56:05 +00005040 case IPPROTO_TCP:
5041 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5042 off + MAX_TCP_HDR_LEN);
5043 if (!err && !skb_partial_csum_set(skb, off,
5044 offsetof(struct tcphdr,
5045 check)))
5046 err = -EPROTO;
5047 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5048
5049 case IPPROTO_UDP:
5050 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5051 off + sizeof(struct udphdr));
5052 if (!err && !skb_partial_csum_set(skb, off,
5053 offsetof(struct udphdr,
5054 check)))
5055 err = -EPROTO;
5056 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5057 }
5058
5059 return ERR_PTR(-EPROTO);
5060}
5061
Paul Durranted1f50c2014-01-09 10:02:46 +00005062/* This value should be large enough to cover a tagged ethernet header plus
5063 * maximally sized IP and TCP or UDP headers.
5064 */
5065#define MAX_IP_HDR_LEN 128
5066
Jan Beulichf9708b42014-03-11 13:56:05 +00005067static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
Paul Durranted1f50c2014-01-09 10:02:46 +00005068{
5069 unsigned int off;
5070 bool fragment;
Jan Beulichf9708b42014-03-11 13:56:05 +00005071 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00005072 int err;
5073
5074 fragment = false;
5075
5076 err = skb_maybe_pull_tail(skb,
5077 sizeof(struct iphdr),
5078 MAX_IP_HDR_LEN);
5079 if (err < 0)
5080 goto out;
5081
Miaohe Lin11f920d2020-08-06 19:57:18 +08005082 if (ip_is_fragment(ip_hdr(skb)))
Paul Durranted1f50c2014-01-09 10:02:46 +00005083 fragment = true;
5084
5085 off = ip_hdrlen(skb);
5086
5087 err = -EPROTO;
5088
5089 if (fragment)
5090 goto out;
5091
Jan Beulichf9708b42014-03-11 13:56:05 +00005092 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5093 if (IS_ERR(csum))
5094 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005095
Jan Beulichf9708b42014-03-11 13:56:05 +00005096 if (recalculate)
5097 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5098 ip_hdr(skb)->daddr,
5099 skb->len - off,
5100 ip_hdr(skb)->protocol, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005101 err = 0;
5102
5103out:
5104 return err;
5105}
5106
5107/* This value should be large enough to cover a tagged ethernet header plus
5108 * an IPv6 header, all options, and a maximal TCP or UDP header.
5109 */
5110#define MAX_IPV6_HDR_LEN 256
5111
5112#define OPT_HDR(type, skb, off) \
5113 (type *)(skb_network_header(skb) + (off))
5114
5115static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5116{
5117 int err;
5118 u8 nexthdr;
5119 unsigned int off;
5120 unsigned int len;
5121 bool fragment;
5122 bool done;
Jan Beulichf9708b42014-03-11 13:56:05 +00005123 __sum16 *csum;
Paul Durranted1f50c2014-01-09 10:02:46 +00005124
5125 fragment = false;
5126 done = false;
5127
5128 off = sizeof(struct ipv6hdr);
5129
5130 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5131 if (err < 0)
5132 goto out;
5133
5134 nexthdr = ipv6_hdr(skb)->nexthdr;
5135
5136 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5137 while (off <= len && !done) {
5138 switch (nexthdr) {
5139 case IPPROTO_DSTOPTS:
5140 case IPPROTO_HOPOPTS:
5141 case IPPROTO_ROUTING: {
5142 struct ipv6_opt_hdr *hp;
5143
5144 err = skb_maybe_pull_tail(skb,
5145 off +
5146 sizeof(struct ipv6_opt_hdr),
5147 MAX_IPV6_HDR_LEN);
5148 if (err < 0)
5149 goto out;
5150
5151 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5152 nexthdr = hp->nexthdr;
5153 off += ipv6_optlen(hp);
5154 break;
5155 }
5156 case IPPROTO_AH: {
5157 struct ip_auth_hdr *hp;
5158
5159 err = skb_maybe_pull_tail(skb,
5160 off +
5161 sizeof(struct ip_auth_hdr),
5162 MAX_IPV6_HDR_LEN);
5163 if (err < 0)
5164 goto out;
5165
5166 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5167 nexthdr = hp->nexthdr;
5168 off += ipv6_authlen(hp);
5169 break;
5170 }
5171 case IPPROTO_FRAGMENT: {
5172 struct frag_hdr *hp;
5173
5174 err = skb_maybe_pull_tail(skb,
5175 off +
5176 sizeof(struct frag_hdr),
5177 MAX_IPV6_HDR_LEN);
5178 if (err < 0)
5179 goto out;
5180
5181 hp = OPT_HDR(struct frag_hdr, skb, off);
5182
5183 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5184 fragment = true;
5185
5186 nexthdr = hp->nexthdr;
5187 off += sizeof(struct frag_hdr);
5188 break;
5189 }
5190 default:
5191 done = true;
5192 break;
5193 }
5194 }
5195
5196 err = -EPROTO;
5197
5198 if (!done || fragment)
5199 goto out;
5200
Jan Beulichf9708b42014-03-11 13:56:05 +00005201 csum = skb_checksum_setup_ip(skb, nexthdr, off);
5202 if (IS_ERR(csum))
5203 return PTR_ERR(csum);
Paul Durranted1f50c2014-01-09 10:02:46 +00005204
Jan Beulichf9708b42014-03-11 13:56:05 +00005205 if (recalculate)
5206 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5207 &ipv6_hdr(skb)->daddr,
5208 skb->len - off, nexthdr, 0);
Paul Durranted1f50c2014-01-09 10:02:46 +00005209 err = 0;
5210
5211out:
5212 return err;
5213}
5214
5215/**
5216 * skb_checksum_setup - set up partial checksum offset
5217 * @skb: the skb to set up
5218 * @recalculate: if true the pseudo-header checksum will be recalculated
5219 */
5220int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5221{
5222 int err;
5223
5224 switch (skb->protocol) {
5225 case htons(ETH_P_IP):
Jan Beulichf9708b42014-03-11 13:56:05 +00005226 err = skb_checksum_setup_ipv4(skb, recalculate);
Paul Durranted1f50c2014-01-09 10:02:46 +00005227 break;
5228
5229 case htons(ETH_P_IPV6):
5230 err = skb_checksum_setup_ipv6(skb, recalculate);
5231 break;
5232
5233 default:
5234 err = -EPROTO;
5235 break;
5236 }
5237
5238 return err;
5239}
5240EXPORT_SYMBOL(skb_checksum_setup);
5241
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005242/**
5243 * skb_checksum_maybe_trim - maybe trims the given skb
5244 * @skb: the skb to check
5245 * @transport_len: the data length beyond the network header
5246 *
5247 * Checks whether the given skb has data beyond the given transport length.
5248 * If so, returns a cloned skb trimmed to this transport length.
5249 * Otherwise returns the provided skb. Returns NULL in error cases
5250 * (e.g. transport_len exceeds skb length or out-of-memory).
5251 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005252 * Caller needs to set the skb transport header and free any returned skb if it
5253 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005254 */
5255static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5256 unsigned int transport_len)
5257{
5258 struct sk_buff *skb_chk;
5259 unsigned int len = skb_transport_offset(skb) + transport_len;
5260 int ret;
5261
Linus Lüssinga5169932015-08-13 05:54:07 +02005262 if (skb->len < len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005263 return NULL;
Linus Lüssinga5169932015-08-13 05:54:07 +02005264 else if (skb->len == len)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005265 return skb;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005266
5267 skb_chk = skb_clone(skb, GFP_ATOMIC);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005268 if (!skb_chk)
5269 return NULL;
5270
5271 ret = pskb_trim_rcsum(skb_chk, len);
5272 if (ret) {
5273 kfree_skb(skb_chk);
5274 return NULL;
5275 }
5276
5277 return skb_chk;
5278}
5279
5280/**
5281 * skb_checksum_trimmed - validate checksum of an skb
5282 * @skb: the skb to check
5283 * @transport_len: the data length beyond the network header
5284 * @skb_chkf: checksum function to use
5285 *
5286 * Applies the given checksum function skb_chkf to the provided skb.
5287 * Returns a checked and maybe trimmed skb. Returns NULL on error.
5288 *
5289 * If the skb has data beyond the given transport length, then a
5290 * trimmed & cloned skb is checked and returned.
5291 *
Linus Lüssinga5169932015-08-13 05:54:07 +02005292 * Caller needs to set the skb transport header and free any returned skb if it
5293 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005294 */
5295struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5296 unsigned int transport_len,
5297 __sum16(*skb_chkf)(struct sk_buff *skb))
5298{
5299 struct sk_buff *skb_chk;
5300 unsigned int offset = skb_transport_offset(skb);
Linus Lüssingfcba67c2015-05-05 00:19:35 +02005301 __sum16 ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005302
5303 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5304 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02005305 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005306
Linus Lüssinga5169932015-08-13 05:54:07 +02005307 if (!pskb_may_pull(skb_chk, offset))
5308 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005309
Linus Lüssing9b368812016-02-24 04:21:42 +01005310 skb_pull_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005311 ret = skb_chkf(skb_chk);
Linus Lüssing9b368812016-02-24 04:21:42 +01005312 skb_push_rcsum(skb_chk, offset);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005313
Linus Lüssinga5169932015-08-13 05:54:07 +02005314 if (ret)
5315 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005316
5317 return skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02005318
5319err:
5320 if (skb_chk && skb_chk != skb)
5321 kfree_skb(skb_chk);
5322
5323 return NULL;
5324
Linus Lüssing9afd85c2015-05-02 14:01:07 +02005325}
5326EXPORT_SYMBOL(skb_checksum_trimmed);
5327
Ben Hutchings4497b072008-06-19 16:22:28 -07005328void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5329{
Joe Perchese87cc472012-05-13 21:56:26 +00005330 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5331 skb->dev->name);
Ben Hutchings4497b072008-06-19 16:22:28 -07005332}
Ben Hutchings4497b072008-06-19 16:22:28 -07005333EXPORT_SYMBOL(__skb_warn_lro_forwarding);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005334
5335void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5336{
Eric Dumazet3d861f62012-10-22 09:03:40 +00005337 if (head_stolen) {
5338 skb_release_head_state(skb);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005339 kmem_cache_free(skbuff_head_cache, skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005340 } else {
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005341 __kfree_skb(skb);
Eric Dumazet3d861f62012-10-22 09:03:40 +00005342 }
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005343}
5344EXPORT_SYMBOL(kfree_skb_partial);
5345
5346/**
5347 * skb_try_coalesce - try to merge skb to prior one
5348 * @to: prior buffer
5349 * @from: buffer to add
5350 * @fragstolen: pointer to boolean
Randy Dunlapc6c4b972012-06-08 14:01:44 +00005351 * @delta_truesize: how much more was allocated than was requested
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005352 */
5353bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5354 bool *fragstolen, int *delta_truesize)
5355{
Eric Dumazetc818fa92017-10-04 10:48:35 -07005356 struct skb_shared_info *to_shinfo, *from_shinfo;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005357 int i, delta, len = from->len;
5358
5359 *fragstolen = false;
5360
5361 if (skb_cloned(to))
5362 return false;
5363
Ilias Apalodimas6a5bcd82021-06-07 21:02:38 +02005364 /* The page pool signature of struct page will eventually figure out
5365 * which pages can be recycled or not but for now let's prohibit slab
5366 * allocated and page_pool allocated SKBs from being coalesced.
5367 */
5368 if (to->pp_recycle != from->pp_recycle)
5369 return false;
5370
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005371 if (len <= skb_tailroom(to)) {
Eric Dumazete93a0432014-09-15 04:19:52 -07005372 if (len)
5373 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005374 *delta_truesize = 0;
5375 return true;
5376 }
5377
Eric Dumazetc818fa92017-10-04 10:48:35 -07005378 to_shinfo = skb_shinfo(to);
5379 from_shinfo = skb_shinfo(from);
5380 if (to_shinfo->frag_list || from_shinfo->frag_list)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005381 return false;
Willem de Bruijn1f8b9772017-08-03 16:29:41 -04005382 if (skb_zcopy(to) || skb_zcopy(from))
5383 return false;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005384
5385 if (skb_headlen(from) != 0) {
5386 struct page *page;
5387 unsigned int offset;
5388
Eric Dumazetc818fa92017-10-04 10:48:35 -07005389 if (to_shinfo->nr_frags +
5390 from_shinfo->nr_frags >= MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005391 return false;
5392
5393 if (skb_head_is_locked(from))
5394 return false;
5395
5396 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5397
5398 page = virt_to_head_page(from->head);
5399 offset = from->data - (unsigned char *)page_address(page);
5400
Eric Dumazetc818fa92017-10-04 10:48:35 -07005401 skb_fill_page_desc(to, to_shinfo->nr_frags,
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005402 page, offset, skb_headlen(from));
5403 *fragstolen = true;
5404 } else {
Eric Dumazetc818fa92017-10-04 10:48:35 -07005405 if (to_shinfo->nr_frags +
5406 from_shinfo->nr_frags > MAX_SKB_FRAGS)
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005407 return false;
5408
Weiping Panf4b549a2012-09-28 20:15:30 +00005409 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005410 }
5411
5412 WARN_ON_ONCE(delta < len);
5413
Eric Dumazetc818fa92017-10-04 10:48:35 -07005414 memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5415 from_shinfo->frags,
5416 from_shinfo->nr_frags * sizeof(skb_frag_t));
5417 to_shinfo->nr_frags += from_shinfo->nr_frags;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005418
5419 if (!skb_cloned(from))
Eric Dumazetc818fa92017-10-04 10:48:35 -07005420 from_shinfo->nr_frags = 0;
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005421
Li RongQing8ea853f2012-09-18 16:53:21 +00005422 /* if the skb is not cloned this does nothing
5423 * since we set nr_frags to 0.
5424 */
Eric Dumazetc818fa92017-10-04 10:48:35 -07005425 for (i = 0; i < from_shinfo->nr_frags; i++)
5426 __skb_frag_ref(&from_shinfo->frags[i]);
Eric Dumazetbad43ca2012-05-19 03:02:02 +00005427
5428 to->truesize += delta;
5429 to->len += len;
5430 to->data_len += len;
5431
5432 *delta_truesize = delta;
5433 return true;
5434}
5435EXPORT_SYMBOL(skb_try_coalesce);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005436
5437/**
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005438 * skb_scrub_packet - scrub an skb
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005439 *
5440 * @skb: buffer to clean
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005441 * @xnet: packet is crossing netns
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005442 *
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005443 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5444 * into/from a tunnel. Some information have to be cleared during these
5445 * operations.
5446 * skb_scrub_packet can also be used to clean a skb before injecting it in
5447 * another namespace (@xnet == true). We have to clear all information in the
5448 * skb that could impact namespace isolation.
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005449 */
Nicolas Dichtel8b27f272013-09-02 15:34:56 +02005450void skb_scrub_packet(struct sk_buff *skb, bool xnet)
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005451{
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005452 skb->pkt_type = PACKET_HOST;
5453 skb->skb_iif = 0;
WANG Cong60ff7462014-05-04 16:39:18 -07005454 skb->ignore_df = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005455 skb_dst_drop(skb);
Florian Westphal174e2382019-09-26 20:37:05 +02005456 skb_ext_reset(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +02005457 nf_reset_ct(skb);
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005458 nf_reset_trace(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005459
Petr Machata6f9a5062018-11-19 16:11:07 +00005460#ifdef CONFIG_NET_SWITCHDEV
5461 skb->offload_fwd_mark = 0;
Ido Schimmel875e8932018-12-04 08:15:10 +00005462 skb->offload_l3_fwd_mark = 0;
Petr Machata6f9a5062018-11-19 16:11:07 +00005463#endif
5464
Herbert Xu213dd742015-04-16 09:03:27 +08005465 if (!xnet)
5466 return;
5467
Ye Yin2b5ec1a2017-10-26 16:57:05 +08005468 ipvs_reset(skb);
Herbert Xu213dd742015-04-16 09:03:27 +08005469 skb->mark = 0;
Jesus Sanchez-Palenciac47d8c22018-07-03 15:42:47 -07005470 skb->tstamp = 0;
Nicolas Dichtel621e84d2013-06-26 16:11:27 +02005471}
5472EXPORT_SYMBOL_GPL(skb_scrub_packet);
Florian Westphalde960aa2014-01-26 10:58:16 +01005473
5474/**
5475 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5476 *
5477 * @skb: GSO skb
5478 *
5479 * skb_gso_transport_seglen is used to determine the real size of the
5480 * individual segments, including Layer4 headers (TCP/UDP).
5481 *
5482 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5483 */
Daniel Axtensa4a77712018-03-01 17:13:40 +11005484static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
Florian Westphalde960aa2014-01-26 10:58:16 +01005485{
5486 const struct skb_shared_info *shinfo = skb_shinfo(skb);
Florian Westphalf993bc22014-10-20 13:49:18 +02005487 unsigned int thlen = 0;
Florian Westphalde960aa2014-01-26 10:58:16 +01005488
Florian Westphalf993bc22014-10-20 13:49:18 +02005489 if (skb->encapsulation) {
5490 thlen = skb_inner_transport_header(skb) -
5491 skb_transport_header(skb);
Florian Westphal6d39d582014-04-09 10:28:50 +02005492
Florian Westphalf993bc22014-10-20 13:49:18 +02005493 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5494 thlen += inner_tcp_hdrlen(skb);
5495 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5496 thlen = tcp_hdrlen(skb);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11005497 } else if (unlikely(skb_is_gso_sctp(skb))) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03005498 thlen = sizeof(struct sctphdr);
Willem de Bruijnee80d1e2018-04-26 13:42:16 -04005499 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5500 thlen = sizeof(struct udphdr);
Florian Westphalf993bc22014-10-20 13:49:18 +02005501 }
Florian Westphal6d39d582014-04-09 10:28:50 +02005502 /* UFO sets gso_size to the size of the fragmentation
5503 * payload, i.e. the size of the L4 (UDP) header is already
5504 * accounted for.
5505 */
Florian Westphalf993bc22014-10-20 13:49:18 +02005506 return thlen + shinfo->gso_size;
Florian Westphalde960aa2014-01-26 10:58:16 +01005507}
Daniel Axtensa4a77712018-03-01 17:13:40 +11005508
5509/**
5510 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5511 *
5512 * @skb: GSO skb
5513 *
5514 * skb_gso_network_seglen is used to determine the real size of the
5515 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5516 *
5517 * The MAC/L2 header is not accounted for.
5518 */
5519static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5520{
5521 unsigned int hdr_len = skb_transport_header(skb) -
5522 skb_network_header(skb);
5523
5524 return hdr_len + skb_gso_transport_seglen(skb);
5525}
5526
5527/**
5528 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5529 *
5530 * @skb: GSO skb
5531 *
5532 * skb_gso_mac_seglen is used to determine the real size of the
5533 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5534 * headers (TCP/UDP).
5535 */
5536static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5537{
5538 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5539
5540 return hdr_len + skb_gso_transport_seglen(skb);
5541}
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005542
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005543/**
Daniel Axtens2b16f042018-01-31 14:15:33 +11005544 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5545 *
5546 * There are a couple of instances where we have a GSO skb, and we
5547 * want to determine what size it would be after it is segmented.
5548 *
5549 * We might want to check:
5550 * - L3+L4+payload size (e.g. IP forwarding)
5551 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5552 *
5553 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5554 *
Mathieu Malaterre49682bf2018-10-31 13:16:58 +01005555 * @skb: GSO skb
5556 *
Daniel Axtens2b16f042018-01-31 14:15:33 +11005557 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5558 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5559 *
5560 * @max_len: The maximum permissible length.
5561 *
5562 * Returns true if the segmented length <= max length.
5563 */
5564static inline bool skb_gso_size_check(const struct sk_buff *skb,
5565 unsigned int seg_len,
5566 unsigned int max_len) {
5567 const struct skb_shared_info *shinfo = skb_shinfo(skb);
5568 const struct sk_buff *iter;
5569
5570 if (shinfo->gso_size != GSO_BY_FRAGS)
5571 return seg_len <= max_len;
5572
5573 /* Undo this so we can re-use header sizes */
5574 seg_len -= GSO_BY_FRAGS;
5575
5576 skb_walk_frags(skb, iter) {
5577 if (seg_len + skb_headlen(iter) > max_len)
5578 return false;
5579 }
5580
5581 return true;
5582}
5583
5584/**
Daniel Axtens779b7932018-03-01 17:13:37 +11005585 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005586 *
5587 * @skb: GSO skb
David S. Miller76f21b92016-06-03 22:56:28 -07005588 * @mtu: MTU to validate against
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005589 *
Daniel Axtens779b7932018-03-01 17:13:37 +11005590 * skb_gso_validate_network_len validates if a given skb will fit a
5591 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5592 * payload.
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005593 */
Daniel Axtens779b7932018-03-01 17:13:37 +11005594bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005595{
Daniel Axtens2b16f042018-01-31 14:15:33 +11005596 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005597}
Daniel Axtens779b7932018-03-01 17:13:37 +11005598EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -03005599
Daniel Axtens2b16f042018-01-31 14:15:33 +11005600/**
5601 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5602 *
5603 * @skb: GSO skb
5604 * @len: length to validate against
5605 *
5606 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5607 * length once split, including L2, L3 and L4 headers and the payload.
5608 */
5609bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5610{
5611 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5612}
5613EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5614
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005615static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5616{
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005617 int mac_len, meta_len;
5618 void *meta;
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005619
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005620 if (skb_cow(skb, skb_headroom(skb)) < 0) {
5621 kfree_skb(skb);
5622 return NULL;
5623 }
5624
Toshiaki Makita4bbb3e02018-03-13 14:51:27 +09005625 mac_len = skb->data - skb_mac_header(skb);
Toshiaki Makitaae474572018-03-29 19:05:29 +09005626 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5627 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5628 mac_len - VLAN_HLEN - ETH_TLEN);
5629 }
Yuya Kusakabed85e8be2019-04-16 10:22:28 +09005630
5631 meta_len = skb_metadata_len(skb);
5632 if (meta_len) {
5633 meta = skb_metadata_end(skb) - meta_len;
5634 memmove(meta + VLAN_HLEN, meta, meta_len);
5635 }
5636
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005637 skb->mac_header += VLAN_HLEN;
5638 return skb;
5639}
5640
5641struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5642{
5643 struct vlan_hdr *vhdr;
5644 u16 vlan_tci;
5645
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005646 if (unlikely(skb_vlan_tag_present(skb))) {
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005647 /* vlan_tci is already set-up so leave this for another time */
5648 return skb;
5649 }
5650
5651 skb = skb_share_check(skb, GFP_ATOMIC);
5652 if (unlikely(!skb))
5653 goto err_free;
Miaohe Lin55eff0e2020-08-15 04:44:31 -04005654 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5655 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005656 goto err_free;
5657
5658 vhdr = (struct vlan_hdr *)skb->data;
5659 vlan_tci = ntohs(vhdr->h_vlan_TCI);
5660 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5661
5662 skb_pull_rcsum(skb, VLAN_HLEN);
5663 vlan_set_encap_proto(skb, vhdr);
5664
5665 skb = skb_reorder_vlan_header(skb);
5666 if (unlikely(!skb))
5667 goto err_free;
5668
5669 skb_reset_network_header(skb);
Alexander Lobakin8be33ec2020-11-09 23:47:23 +00005670 if (!skb_transport_header_was_set(skb))
5671 skb_reset_transport_header(skb);
Vlad Yasevich0d5501c2014-08-08 14:42:13 -04005672 skb_reset_mac_len(skb);
5673
5674 return skb;
5675
5676err_free:
5677 kfree_skb(skb);
5678 return NULL;
5679}
5680EXPORT_SYMBOL(skb_vlan_untag);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005681
Jiri Pirkoe2195122014-11-19 14:05:01 +01005682int skb_ensure_writable(struct sk_buff *skb, int write_len)
5683{
5684 if (!pskb_may_pull(skb, write_len))
5685 return -ENOMEM;
5686
5687 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5688 return 0;
5689
5690 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5691}
5692EXPORT_SYMBOL(skb_ensure_writable);
5693
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005694/* remove VLAN header from packet and update csum accordingly.
5695 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5696 */
5697int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
Jiri Pirko93515d52014-11-19 14:05:02 +01005698{
5699 struct vlan_hdr *vhdr;
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005700 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005701 int err;
5702
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005703 if (WARN_ONCE(offset,
5704 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5705 offset)) {
5706 return -EINVAL;
5707 }
5708
Jiri Pirko93515d52014-11-19 14:05:02 +01005709 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5710 if (unlikely(err))
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005711 return err;
Jiri Pirko93515d52014-11-19 14:05:02 +01005712
5713 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5714
5715 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5716 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
5717
5718 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5719 __skb_pull(skb, VLAN_HLEN);
5720
5721 vlan_set_encap_proto(skb, vhdr);
5722 skb->mac_header += VLAN_HLEN;
5723
5724 if (skb_network_offset(skb) < ETH_HLEN)
5725 skb_set_network_header(skb, ETH_HLEN);
5726
5727 skb_reset_mac_len(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005728
5729 return err;
5730}
Shmulik Ladkanibfca4c52016-09-19 19:11:09 +03005731EXPORT_SYMBOL(__skb_vlan_pop);
Jiri Pirko93515d52014-11-19 14:05:02 +01005732
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005733/* Pop a vlan tag either from hwaccel or from payload.
5734 * Expects skb->data at mac header.
5735 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005736int skb_vlan_pop(struct sk_buff *skb)
5737{
5738 u16 vlan_tci;
5739 __be16 vlan_proto;
5740 int err;
5741
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005742 if (likely(skb_vlan_tag_present(skb))) {
Michał Mirosławb18175242018-11-09 00:18:02 +01005743 __vlan_hwaccel_clear_tag(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005744 } else {
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005745 if (unlikely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005746 return 0;
5747
5748 err = __skb_vlan_pop(skb, &vlan_tci);
5749 if (err)
5750 return err;
5751 }
5752 /* move next vlan tag to hw accel tag */
Shmulik Ladkaniecf4ee42016-09-20 12:48:37 +03005753 if (likely(!eth_type_vlan(skb->protocol)))
Jiri Pirko93515d52014-11-19 14:05:02 +01005754 return 0;
5755
5756 vlan_proto = skb->protocol;
5757 err = __skb_vlan_pop(skb, &vlan_tci);
5758 if (unlikely(err))
5759 return err;
5760
5761 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5762 return 0;
5763}
5764EXPORT_SYMBOL(skb_vlan_pop);
5765
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005766/* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5767 * Expects skb->data at mac header.
5768 */
Jiri Pirko93515d52014-11-19 14:05:02 +01005769int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5770{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005771 if (skb_vlan_tag_present(skb)) {
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005772 int offset = skb->data - skb_mac_header(skb);
Jiri Pirko93515d52014-11-19 14:05:02 +01005773 int err;
5774
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005775 if (WARN_ONCE(offset,
5776 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5777 offset)) {
5778 return -EINVAL;
5779 }
5780
Jiri Pirko93515d52014-11-19 14:05:02 +01005781 err = __vlan_insert_tag(skb, skb->vlan_proto,
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005782 skb_vlan_tag_get(skb));
Shmulik Ladkanib6a79202016-09-29 12:10:41 +03005783 if (err)
Jiri Pirko93515d52014-11-19 14:05:02 +01005784 return err;
Daniel Borkmann9241e2d2016-04-16 02:27:58 +02005785
Jiri Pirko93515d52014-11-19 14:05:02 +01005786 skb->protocol = skb->vlan_proto;
5787 skb->mac_len += VLAN_HLEN;
Jiri Pirko93515d52014-11-19 14:05:02 +01005788
Daniel Borkmann6b83d282016-02-20 00:29:30 +01005789 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
Jiri Pirko93515d52014-11-19 14:05:02 +01005790 }
5791 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5792 return 0;
5793}
5794EXPORT_SYMBOL(skb_vlan_push);
5795
Guillaume Nault19fbcb32020-10-03 00:44:28 +02005796/**
5797 * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5798 *
5799 * @skb: Socket buffer to modify
5800 *
5801 * Drop the Ethernet header of @skb.
5802 *
5803 * Expects that skb->data points to the mac header and that no VLAN tags are
5804 * present.
5805 *
5806 * Returns 0 on success, -errno otherwise.
5807 */
5808int skb_eth_pop(struct sk_buff *skb)
5809{
5810 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5811 skb_network_offset(skb) < ETH_HLEN)
5812 return -EPROTO;
5813
5814 skb_pull_rcsum(skb, ETH_HLEN);
5815 skb_reset_mac_header(skb);
5816 skb_reset_mac_len(skb);
5817
5818 return 0;
5819}
5820EXPORT_SYMBOL(skb_eth_pop);
5821
5822/**
5823 * skb_eth_push() - Add a new Ethernet header at the head of a packet
5824 *
5825 * @skb: Socket buffer to modify
5826 * @dst: Destination MAC address of the new header
5827 * @src: Source MAC address of the new header
5828 *
5829 * Prepend @skb with a new Ethernet header.
5830 *
5831 * Expects that skb->data points to the mac header, which must be empty.
5832 *
5833 * Returns 0 on success, -errno otherwise.
5834 */
5835int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5836 const unsigned char *src)
5837{
5838 struct ethhdr *eth;
5839 int err;
5840
5841 if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5842 return -EPROTO;
5843
5844 err = skb_cow_head(skb, sizeof(*eth));
5845 if (err < 0)
5846 return err;
5847
5848 skb_push(skb, sizeof(*eth));
5849 skb_reset_mac_header(skb);
5850 skb_reset_mac_len(skb);
5851
5852 eth = eth_hdr(skb);
5853 ether_addr_copy(eth->h_dest, dst);
5854 ether_addr_copy(eth->h_source, src);
5855 eth->h_proto = skb->protocol;
5856
5857 skb_postpush_rcsum(skb, eth, sizeof(*eth));
5858
5859 return 0;
5860}
5861EXPORT_SYMBOL(skb_eth_push);
5862
John Hurley8822e272019-07-07 15:01:54 +01005863/* Update the ethertype of hdr and the skb csum value if required. */
5864static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5865 __be16 ethertype)
5866{
5867 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5868 __be16 diff[] = { ~hdr->h_proto, ethertype };
5869
5870 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5871 }
5872
5873 hdr->h_proto = ethertype;
5874}
5875
5876/**
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305877 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5878 * the packet
John Hurley8822e272019-07-07 15:01:54 +01005879 *
5880 * @skb: buffer
5881 * @mpls_lse: MPLS label stack entry to push
5882 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
Davide Carattifa4e0f82019-10-12 13:55:07 +02005883 * @mac_len: length of the MAC header
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305884 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5885 * ethernet
John Hurley8822e272019-07-07 15:01:54 +01005886 *
5887 * Expects skb->data at mac header.
5888 *
5889 * Returns 0 on success, -errno otherwise.
5890 */
Davide Carattifa4e0f82019-10-12 13:55:07 +02005891int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
Martin Varghesed04ac222019-12-05 05:57:22 +05305892 int mac_len, bool ethernet)
John Hurley8822e272019-07-07 15:01:54 +01005893{
5894 struct mpls_shim_hdr *lse;
5895 int err;
5896
5897 if (unlikely(!eth_p_mpls(mpls_proto)))
5898 return -EINVAL;
5899
5900 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5901 if (skb->encapsulation)
5902 return -EINVAL;
5903
5904 err = skb_cow_head(skb, MPLS_HLEN);
5905 if (unlikely(err))
5906 return err;
5907
5908 if (!skb->inner_protocol) {
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305909 skb_set_inner_network_header(skb, skb_network_offset(skb));
John Hurley8822e272019-07-07 15:01:54 +01005910 skb_set_inner_protocol(skb, skb->protocol);
5911 }
5912
5913 skb_push(skb, MPLS_HLEN);
5914 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005915 mac_len);
John Hurley8822e272019-07-07 15:01:54 +01005916 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005917 skb_set_network_header(skb, mac_len);
Martin Varghesee7dbfed2019-12-21 08:50:01 +05305918 skb_reset_mac_len(skb);
John Hurley8822e272019-07-07 15:01:54 +01005919
5920 lse = mpls_hdr(skb);
5921 lse->label_stack_entry = mpls_lse;
5922 skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5923
Guillaume Nault4296adc2020-10-02 21:53:08 +02005924 if (ethernet && mac_len >= ETH_HLEN)
John Hurley8822e272019-07-07 15:01:54 +01005925 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5926 skb->protocol = mpls_proto;
5927
5928 return 0;
5929}
5930EXPORT_SYMBOL_GPL(skb_mpls_push);
5931
Eric Dumazet2e4e4412014-09-17 04:49:49 -07005932/**
John Hurleyed246ce2019-07-07 15:01:55 +01005933 * skb_mpls_pop() - pop the outermost MPLS header
5934 *
5935 * @skb: buffer
5936 * @next_proto: ethertype of header after popped MPLS header
Davide Carattifa4e0f82019-10-12 13:55:07 +02005937 * @mac_len: length of the MAC header
Martin Varghese76f99f92019-12-21 08:50:23 +05305938 * @ethernet: flag to indicate if the packet is ethernet
John Hurleyed246ce2019-07-07 15:01:55 +01005939 *
5940 * Expects skb->data at mac header.
5941 *
5942 * Returns 0 on success, -errno otherwise.
5943 */
Martin Varghese040b5cf2019-12-02 10:49:51 +05305944int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5945 bool ethernet)
John Hurleyed246ce2019-07-07 15:01:55 +01005946{
5947 int err;
5948
5949 if (unlikely(!eth_p_mpls(skb->protocol)))
Davide Carattidedc5a02019-10-12 13:55:06 +02005950 return 0;
John Hurleyed246ce2019-07-07 15:01:55 +01005951
Davide Carattifa4e0f82019-10-12 13:55:07 +02005952 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
John Hurleyed246ce2019-07-07 15:01:55 +01005953 if (unlikely(err))
5954 return err;
5955
5956 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5957 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
Davide Carattifa4e0f82019-10-12 13:55:07 +02005958 mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005959
5960 __skb_pull(skb, MPLS_HLEN);
5961 skb_reset_mac_header(skb);
Davide Carattifa4e0f82019-10-12 13:55:07 +02005962 skb_set_network_header(skb, mac_len);
John Hurleyed246ce2019-07-07 15:01:55 +01005963
Guillaume Nault4296adc2020-10-02 21:53:08 +02005964 if (ethernet && mac_len >= ETH_HLEN) {
John Hurleyed246ce2019-07-07 15:01:55 +01005965 struct ethhdr *hdr;
5966
5967 /* use mpls_hdr() to get ethertype to account for VLANs. */
5968 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5969 skb_mod_eth_type(skb, hdr, next_proto);
5970 }
5971 skb->protocol = next_proto;
5972
5973 return 0;
5974}
5975EXPORT_SYMBOL_GPL(skb_mpls_pop);
5976
5977/**
John Hurleyd27cf5c2019-07-07 15:01:56 +01005978 * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5979 *
5980 * @skb: buffer
5981 * @mpls_lse: new MPLS label stack entry to update to
5982 *
5983 * Expects skb->data at mac header.
5984 *
5985 * Returns 0 on success, -errno otherwise.
5986 */
5987int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5988{
5989 int err;
5990
5991 if (unlikely(!eth_p_mpls(skb->protocol)))
5992 return -EINVAL;
5993
5994 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5995 if (unlikely(err))
5996 return err;
5997
5998 if (skb->ip_summed == CHECKSUM_COMPLETE) {
5999 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6000
6001 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6002 }
6003
6004 mpls_hdr(skb)->label_stack_entry = mpls_lse;
6005
6006 return 0;
6007}
6008EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6009
6010/**
John Hurley2a2ea502019-07-07 15:01:57 +01006011 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
6012 *
6013 * @skb: buffer
6014 *
6015 * Expects skb->data at mac header.
6016 *
6017 * Returns 0 on success, -errno otherwise.
6018 */
6019int skb_mpls_dec_ttl(struct sk_buff *skb)
6020{
6021 u32 lse;
6022 u8 ttl;
6023
6024 if (unlikely(!eth_p_mpls(skb->protocol)))
6025 return -EINVAL;
6026
Davide Caratti13de4ed2020-12-03 10:58:21 +01006027 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
6028 return -ENOMEM;
6029
John Hurley2a2ea502019-07-07 15:01:57 +01006030 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
6031 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
6032 if (!--ttl)
6033 return -EINVAL;
6034
6035 lse &= ~MPLS_LS_TTL_MASK;
6036 lse |= ttl << MPLS_LS_TTL_SHIFT;
6037
6038 return skb_mpls_update_lse(skb, cpu_to_be32(lse));
6039}
6040EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
6041
6042/**
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006043 * alloc_skb_with_frags - allocate skb with page frags
6044 *
Masanari Iidade3f0d02014-10-09 12:58:08 +09006045 * @header_len: size of linear part
6046 * @data_len: needed length in frags
6047 * @max_page_order: max page order desired.
6048 * @errcode: pointer to error code if any
6049 * @gfp_mask: allocation mask
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006050 *
6051 * This can be used to allocate a paged skb, given a maximal order for frags.
6052 */
6053struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6054 unsigned long data_len,
6055 int max_page_order,
6056 int *errcode,
6057 gfp_t gfp_mask)
6058{
6059 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
6060 unsigned long chunk;
6061 struct sk_buff *skb;
6062 struct page *page;
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006063 int i;
6064
6065 *errcode = -EMSGSIZE;
6066 /* Note this test could be relaxed, if we succeed to allocate
6067 * high order pages...
6068 */
6069 if (npages > MAX_SKB_FRAGS)
6070 return NULL;
6071
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006072 *errcode = -ENOBUFS;
David Rientjesf8c468e2019-01-02 13:01:43 -08006073 skb = alloc_skb(header_len, gfp_mask);
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006074 if (!skb)
6075 return NULL;
6076
6077 skb->truesize += npages << PAGE_SHIFT;
6078
6079 for (i = 0; npages > 0; i++) {
6080 int order = max_page_order;
6081
6082 while (order) {
6083 if (npages >= 1 << order) {
Mel Gormand0164ad2015-11-06 16:28:21 -08006084 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006085 __GFP_COMP |
Michal Hockod14b56f2018-06-28 17:53:06 +02006086 __GFP_NOWARN,
Eric Dumazet2e4e4412014-09-17 04:49:49 -07006087 order);
6088 if (page)
6089 goto fill_page;
6090 /* Do not retry other high order allocations */
6091 order = 1;
6092 max_page_order = 0;
6093 }
6094 order--;
6095 }
6096 page = alloc_page(gfp_mask);
6097 if (!page)
6098 goto failure;
6099fill_page:
6100 chunk = min_t(unsigned long, data_len,
6101 PAGE_SIZE << order);
6102 skb_fill_page_desc(skb, i, page, 0, chunk);
6103 data_len -= chunk;
6104 npages -= 1 << order;
6105 }
6106 return skb;
6107
6108failure:
6109 kfree_skb(skb);
6110 return NULL;
6111}
6112EXPORT_SYMBOL(alloc_skb_with_frags);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006113
6114/* carve out the first off bytes from skb when off < headlen */
6115static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6116 const int headlen, gfp_t gfp_mask)
6117{
6118 int i;
6119 int size = skb_end_offset(skb);
6120 int new_hlen = headlen - off;
6121 u8 *data;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006122
6123 size = SKB_DATA_ALIGN(size);
6124
6125 if (skb_pfmemalloc(skb))
6126 gfp_mask |= __GFP_MEMALLOC;
6127 data = kmalloc_reserve(size +
6128 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6129 gfp_mask, NUMA_NO_NODE, NULL);
6130 if (!data)
6131 return -ENOMEM;
6132
6133 size = SKB_WITH_OVERHEAD(ksize(data));
6134
6135 /* Copy real data, and all frags */
6136 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6137 skb->len -= off;
6138
6139 memcpy((struct skb_shared_info *)(data + size),
6140 skb_shinfo(skb),
6141 offsetof(struct skb_shared_info,
6142 frags[skb_shinfo(skb)->nr_frags]));
6143 if (skb_cloned(skb)) {
6144 /* drop the old head gracefully */
6145 if (skb_orphan_frags(skb, gfp_mask)) {
6146 kfree(data);
6147 return -ENOMEM;
6148 }
6149 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6150 skb_frag_ref(skb, i);
6151 if (skb_has_frag_list(skb))
6152 skb_clone_fraglist(skb);
6153 skb_release_data(skb);
6154 } else {
6155 /* we can reuse existing recount- all we did was
6156 * relocate values
6157 */
6158 skb_free_head(skb);
6159 }
6160
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006161 skb->head = data;
6162 skb->data = data;
6163 skb->head_frag = 0;
6164#ifdef NET_SKBUFF_DATA_USES_OFFSET
6165 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006166#else
6167 skb->end = skb->head + size;
6168#endif
6169 skb_set_tail_pointer(skb, skb_headlen(skb));
6170 skb_headers_offset_update(skb, 0);
6171 skb->cloned = 0;
6172 skb->hdr_len = 0;
6173 skb->nohdr = 0;
6174 atomic_set(&skb_shinfo(skb)->dataref, 1);
6175
6176 return 0;
6177}
6178
6179static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6180
6181/* carve out the first eat bytes from skb's frag_list. May recurse into
6182 * pskb_carve()
6183 */
6184static int pskb_carve_frag_list(struct sk_buff *skb,
6185 struct skb_shared_info *shinfo, int eat,
6186 gfp_t gfp_mask)
6187{
6188 struct sk_buff *list = shinfo->frag_list;
6189 struct sk_buff *clone = NULL;
6190 struct sk_buff *insp = NULL;
6191
6192 do {
6193 if (!list) {
6194 pr_err("Not enough bytes to eat. Want %d\n", eat);
6195 return -EFAULT;
6196 }
6197 if (list->len <= eat) {
6198 /* Eaten as whole. */
6199 eat -= list->len;
6200 list = list->next;
6201 insp = list;
6202 } else {
6203 /* Eaten partially. */
6204 if (skb_shared(list)) {
6205 clone = skb_clone(list, gfp_mask);
6206 if (!clone)
6207 return -ENOMEM;
6208 insp = list->next;
6209 list = clone;
6210 } else {
6211 /* This may be pulled without problems. */
6212 insp = list;
6213 }
6214 if (pskb_carve(list, eat, gfp_mask) < 0) {
6215 kfree_skb(clone);
6216 return -ENOMEM;
6217 }
6218 break;
6219 }
6220 } while (eat);
6221
6222 /* Free pulled out fragments. */
6223 while ((list = shinfo->frag_list) != insp) {
6224 shinfo->frag_list = list->next;
6225 kfree_skb(list);
6226 }
6227 /* And insert new clone at head. */
6228 if (clone) {
6229 clone->next = list;
6230 shinfo->frag_list = clone;
6231 }
6232 return 0;
6233}
6234
6235/* carve off first len bytes from skb. Split line (off) is in the
6236 * non-linear part of skb
6237 */
6238static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6239 int pos, gfp_t gfp_mask)
6240{
6241 int i, k = 0;
6242 int size = skb_end_offset(skb);
6243 u8 *data;
6244 const int nfrags = skb_shinfo(skb)->nr_frags;
6245 struct skb_shared_info *shinfo;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006246
6247 size = SKB_DATA_ALIGN(size);
6248
6249 if (skb_pfmemalloc(skb))
6250 gfp_mask |= __GFP_MEMALLOC;
6251 data = kmalloc_reserve(size +
6252 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6253 gfp_mask, NUMA_NO_NODE, NULL);
6254 if (!data)
6255 return -ENOMEM;
6256
6257 size = SKB_WITH_OVERHEAD(ksize(data));
6258
6259 memcpy((struct skb_shared_info *)(data + size),
Miaohe Line3ec1e82020-08-15 04:48:53 -04006260 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006261 if (skb_orphan_frags(skb, gfp_mask)) {
6262 kfree(data);
6263 return -ENOMEM;
6264 }
6265 shinfo = (struct skb_shared_info *)(data + size);
6266 for (i = 0; i < nfrags; i++) {
6267 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6268
6269 if (pos + fsize > off) {
6270 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6271
6272 if (pos < off) {
6273 /* Split frag.
6274 * We have two variants in this case:
6275 * 1. Move all the frag to the second
6276 * part, if it is possible. F.e.
6277 * this approach is mandatory for TUX,
6278 * where splitting is expensive.
6279 * 2. Split is accurately. We make this.
6280 */
Jonathan Lemonb54c9d52019-07-30 07:40:33 -07006281 skb_frag_off_add(&shinfo->frags[0], off - pos);
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006282 skb_frag_size_sub(&shinfo->frags[0], off - pos);
6283 }
6284 skb_frag_ref(skb, i);
6285 k++;
6286 }
6287 pos += fsize;
6288 }
6289 shinfo->nr_frags = k;
6290 if (skb_has_frag_list(skb))
6291 skb_clone_fraglist(skb);
6292
Miaohe Lineabe8612020-08-15 04:46:41 -04006293 /* split line is in frag list */
6294 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6295 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6296 if (skb_has_frag_list(skb))
6297 kfree_skb_list(skb_shinfo(skb)->frag_list);
6298 kfree(data);
6299 return -ENOMEM;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006300 }
6301 skb_release_data(skb);
6302
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006303 skb->head = data;
6304 skb->head_frag = 0;
6305 skb->data = data;
6306#ifdef NET_SKBUFF_DATA_USES_OFFSET
6307 skb->end = size;
Sowmini Varadhan6fa01cc2016-04-22 18:36:35 -07006308#else
6309 skb->end = skb->head + size;
6310#endif
6311 skb_reset_tail_pointer(skb);
6312 skb_headers_offset_update(skb, 0);
6313 skb->cloned = 0;
6314 skb->hdr_len = 0;
6315 skb->nohdr = 0;
6316 skb->len -= off;
6317 skb->data_len = skb->len;
6318 atomic_set(&skb_shinfo(skb)->dataref, 1);
6319 return 0;
6320}
6321
6322/* remove len bytes from the beginning of the skb */
6323static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6324{
6325 int headlen = skb_headlen(skb);
6326
6327 if (len < headlen)
6328 return pskb_carve_inside_header(skb, len, headlen, gfp);
6329 else
6330 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6331}
6332
6333/* Extract to_copy bytes starting at off from skb, and return this in
6334 * a new skb
6335 */
6336struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6337 int to_copy, gfp_t gfp)
6338{
6339 struct sk_buff *clone = skb_clone(skb, gfp);
6340
6341 if (!clone)
6342 return NULL;
6343
6344 if (pskb_carve(clone, off, gfp) < 0 ||
6345 pskb_trim(clone, to_copy)) {
6346 kfree_skb(clone);
6347 return NULL;
6348 }
6349 return clone;
6350}
6351EXPORT_SYMBOL(pskb_extract);
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006352
6353/**
6354 * skb_condense - try to get rid of fragments/frag_list if possible
6355 * @skb: buffer
6356 *
6357 * Can be used to save memory before skb is added to a busy queue.
6358 * If packet has bytes in frags and enough tail room in skb->head,
6359 * pull all of them, so that we can free the frags right now and adjust
6360 * truesize.
6361 * Notes:
6362 * We do not reallocate skb->head thus can not fail.
6363 * Caller must re-evaluate skb->truesize if needed.
6364 */
6365void skb_condense(struct sk_buff *skb)
6366{
Eric Dumazet3174fed2016-12-09 08:02:05 -08006367 if (skb->data_len) {
6368 if (skb->data_len > skb->end - skb->tail ||
6369 skb_cloned(skb))
6370 return;
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006371
Eric Dumazet3174fed2016-12-09 08:02:05 -08006372 /* Nice, we can free page frag(s) right now */
6373 __pskb_pull_tail(skb, skb->data_len);
6374 }
6375 /* At this point, skb->truesize might be over estimated,
6376 * because skb had a fragment, and fragments do not tell
6377 * their truesize.
6378 * When we pulled its content into skb->head, fragment
6379 * was freed, but __pskb_pull_tail() could not possibly
6380 * adjust skb->truesize, not knowing the frag truesize.
Eric Dumazetc8c8b122016-12-07 09:19:33 -08006381 */
6382 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6383}
Florian Westphaldf5042f2018-12-18 17:15:16 +01006384
6385#ifdef CONFIG_SKB_EXTENSIONS
6386static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6387{
6388 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6389}
6390
Paolo Abeni8b69a802020-01-09 07:59:24 -08006391/**
6392 * __skb_ext_alloc - allocate a new skb extensions storage
6393 *
Florian Westphal4930f482020-05-16 10:46:23 +02006394 * @flags: See kmalloc().
6395 *
Paolo Abeni8b69a802020-01-09 07:59:24 -08006396 * Returns the newly allocated pointer. The pointer can later attached to a
6397 * skb via __skb_ext_set().
6398 * Note: caller must handle the skb_ext as an opaque data.
6399 */
Florian Westphal4930f482020-05-16 10:46:23 +02006400struct skb_ext *__skb_ext_alloc(gfp_t flags)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006401{
Florian Westphal4930f482020-05-16 10:46:23 +02006402 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006403
6404 if (new) {
6405 memset(new->offset, 0, sizeof(new->offset));
6406 refcount_set(&new->refcnt, 1);
6407 }
6408
6409 return new;
6410}
6411
Florian Westphal41650792018-12-18 17:15:27 +01006412static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6413 unsigned int old_active)
Florian Westphaldf5042f2018-12-18 17:15:16 +01006414{
6415 struct skb_ext *new;
6416
6417 if (refcount_read(&old->refcnt) == 1)
6418 return old;
6419
6420 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6421 if (!new)
6422 return NULL;
6423
6424 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6425 refcount_set(&new->refcnt, 1);
6426
Florian Westphal41650792018-12-18 17:15:27 +01006427#ifdef CONFIG_XFRM
6428 if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6429 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6430 unsigned int i;
6431
6432 for (i = 0; i < sp->len; i++)
6433 xfrm_state_hold(sp->xvec[i]);
6434 }
6435#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006436 __skb_ext_put(old);
6437 return new;
6438}
6439
6440/**
Paolo Abeni8b69a802020-01-09 07:59:24 -08006441 * __skb_ext_set - attach the specified extension storage to this skb
6442 * @skb: buffer
6443 * @id: extension id
6444 * @ext: extension storage previously allocated via __skb_ext_alloc()
6445 *
6446 * Existing extensions, if any, are cleared.
6447 *
6448 * Returns the pointer to the extension.
6449 */
6450void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6451 struct skb_ext *ext)
6452{
6453 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6454
6455 skb_ext_put(skb);
6456 newlen = newoff + skb_ext_type_len[id];
6457 ext->chunks = newlen;
6458 ext->offset[id] = newoff;
6459 skb->extensions = ext;
6460 skb->active_extensions = 1 << id;
6461 return skb_ext_get_ptr(ext, id);
6462}
6463
6464/**
Florian Westphaldf5042f2018-12-18 17:15:16 +01006465 * skb_ext_add - allocate space for given extension, COW if needed
6466 * @skb: buffer
6467 * @id: extension to allocate space for
6468 *
6469 * Allocates enough space for the given extension.
6470 * If the extension is already present, a pointer to that extension
6471 * is returned.
6472 *
6473 * If the skb was cloned, COW applies and the returned memory can be
6474 * modified without changing the extension space of clones buffers.
6475 *
6476 * Returns pointer to the extension or NULL on allocation failure.
6477 */
6478void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6479{
6480 struct skb_ext *new, *old = NULL;
6481 unsigned int newlen, newoff;
6482
6483 if (skb->active_extensions) {
6484 old = skb->extensions;
6485
Florian Westphal41650792018-12-18 17:15:27 +01006486 new = skb_ext_maybe_cow(old, skb->active_extensions);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006487 if (!new)
6488 return NULL;
6489
Paolo Abeni682ec852018-12-21 19:03:15 +01006490 if (__skb_ext_exist(new, id))
Florian Westphaldf5042f2018-12-18 17:15:16 +01006491 goto set_active;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006492
Paolo Abenie94e50b2018-12-21 19:03:13 +01006493 newoff = new->chunks;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006494 } else {
6495 newoff = SKB_EXT_CHUNKSIZEOF(*new);
6496
Florian Westphal4930f482020-05-16 10:46:23 +02006497 new = __skb_ext_alloc(GFP_ATOMIC);
Florian Westphaldf5042f2018-12-18 17:15:16 +01006498 if (!new)
6499 return NULL;
6500 }
6501
6502 newlen = newoff + skb_ext_type_len[id];
6503 new->chunks = newlen;
6504 new->offset[id] = newoff;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006505set_active:
Paolo Abenib0999f32021-07-28 18:24:01 +02006506 skb->slow_gro = 1;
Paolo Abeni682ec852018-12-21 19:03:15 +01006507 skb->extensions = new;
Florian Westphaldf5042f2018-12-18 17:15:16 +01006508 skb->active_extensions |= 1 << id;
6509 return skb_ext_get_ptr(new, id);
6510}
6511EXPORT_SYMBOL(skb_ext_add);
6512
Florian Westphal41650792018-12-18 17:15:27 +01006513#ifdef CONFIG_XFRM
6514static void skb_ext_put_sp(struct sec_path *sp)
6515{
6516 unsigned int i;
6517
6518 for (i = 0; i < sp->len; i++)
6519 xfrm_state_put(sp->xvec[i]);
6520}
6521#endif
6522
Florian Westphaldf5042f2018-12-18 17:15:16 +01006523void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6524{
6525 struct skb_ext *ext = skb->extensions;
6526
6527 skb->active_extensions &= ~(1 << id);
6528 if (skb->active_extensions == 0) {
6529 skb->extensions = NULL;
6530 __skb_ext_put(ext);
Florian Westphal41650792018-12-18 17:15:27 +01006531#ifdef CONFIG_XFRM
6532 } else if (id == SKB_EXT_SEC_PATH &&
6533 refcount_read(&ext->refcnt) == 1) {
6534 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6535
6536 skb_ext_put_sp(sp);
6537 sp->len = 0;
6538#endif
Florian Westphaldf5042f2018-12-18 17:15:16 +01006539 }
6540}
6541EXPORT_SYMBOL(__skb_ext_del);
6542
6543void __skb_ext_put(struct skb_ext *ext)
6544{
6545 /* If this is last clone, nothing can increment
6546 * it after check passes. Avoids one atomic op.
6547 */
6548 if (refcount_read(&ext->refcnt) == 1)
6549 goto free_now;
6550
6551 if (!refcount_dec_and_test(&ext->refcnt))
6552 return;
6553free_now:
Florian Westphal41650792018-12-18 17:15:27 +01006554#ifdef CONFIG_XFRM
6555 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6556 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6557#endif
6558
Florian Westphaldf5042f2018-12-18 17:15:16 +01006559 kmem_cache_free(skbuff_ext_cache, ext);
6560}
6561EXPORT_SYMBOL(__skb_ext_put);
6562#endif /* CONFIG_SKB_EXTENSIONS */