]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - net/core/skbuff.c
mmc: core: complete HS400 before checking status
[thirdparty/kernel/linux.git] / net / core / skbuff.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Routines having to do with the 'struct sk_buff' memory handlers.
4 *
113aa838 5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 *
1da177e4
LT
8 * 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).
1da177e4
LT
29 */
30
31/*
32 * The functions in this file will not compile correctly with gcc 2.4.x
33 */
34
e005d193
JP
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
1da177e4
LT
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/kernel.h>
1da177e4
LT
40#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/slab.h>
de960aa9
FW
45#include <linux/tcp.h>
46#include <linux/udp.h>
90017acc 47#include <linux/sctp.h>
1da177e4
LT
48#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>
9c55e01c 54#include <linux/splice.h>
1da177e4
LT
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
716ea3a7 58#include <linux/scatterlist.h>
ac45f602 59#include <linux/errqueue.h>
268bb0ce 60#include <linux/prefetch.h>
0d5501c1 61#include <linux/if_vlan.h>
1da177e4
LT
62
63#include <net/protocol.h>
64#include <net/dst.h>
65#include <net/sock.h>
66#include <net/checksum.h>
ed1f50c3 67#include <net/ip6_checksum.h>
1da177e4
LT
68#include <net/xfrm.h>
69
7c0f6ba6 70#include <linux/uaccess.h>
ad8d75ff 71#include <trace/events/skb.h>
51c56b00 72#include <linux/highmem.h>
b245be1f
WB
73#include <linux/capability.h>
74#include <linux/user_namespace.h>
a1f8e7f7 75
7b7ed885
BVA
76#include "datagram.h"
77
08009a76
AD
78struct kmem_cache *skbuff_head_cache __ro_after_init;
79static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
df5042f4
FW
80#ifdef CONFIG_SKB_EXTENSIONS
81static struct kmem_cache *skbuff_ext_cache __ro_after_init;
82#endif
5f74f82e
HWR
83int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84EXPORT_SYMBOL(sysctl_max_skb_frags);
1da177e4 85
1da177e4 86/**
f05de73b
JS
87 * skb_panic - private function for out-of-line support
88 * @skb: buffer
89 * @sz: size
90 * @addr: address
99d5851e 91 * @msg: skb_over_panic or skb_under_panic
1da177e4 92 *
f05de73b
JS
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
1da177e4 97 */
f05de73b 98static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99d5851e 99 const char msg[])
1da177e4 100{
e005d193 101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99d5851e 102 msg, addr, skb->len, sz, skb->head, skb->data,
e005d193
JP
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
105 BUG();
106}
107
f05de73b 108static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1da177e4 109{
f05de73b 110 skb_panic(skb, sz, addr, __func__);
1da177e4
LT
111}
112
f05de73b
JS
113static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114{
115 skb_panic(skb, sz, addr, __func__);
116}
c93bdd0e
MG
117
118/*
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
123 * memory is free
124 */
125#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
61c5e88a 127
128static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
c93bdd0e
MG
130{
131 void *obj;
132 bool ret_pfmemalloc = false;
133
134 /*
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
137 */
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140 node);
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 goto out;
143
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
147
148out:
149 if (pfmemalloc)
150 *pfmemalloc = ret_pfmemalloc;
151
152 return obj;
153}
154
1da177e4
LT
155/* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
157 * [BEEP] leaks.
158 *
159 */
160
161/**
d179cd12 162 * __alloc_skb - allocate a network buffer
1da177e4
LT
163 * @size: size to allocate
164 * @gfp_mask: allocation mask
c93bdd0e
MG
165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
b30973f8 169 * @node: numa node to allocate memory on
1da177e4
LT
170 *
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
94b6042c
BH
172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
1da177e4
LT
174 *
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
176 * %GFP_ATOMIC.
177 */
dd0fc66f 178struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
c93bdd0e 179 int flags, int node)
1da177e4 180{
e18b890b 181 struct kmem_cache *cache;
4947d3ef 182 struct skb_shared_info *shinfo;
1da177e4
LT
183 struct sk_buff *skb;
184 u8 *data;
c93bdd0e 185 bool pfmemalloc;
1da177e4 186
c93bdd0e
MG
187 cache = (flags & SKB_ALLOC_FCLONE)
188 ? skbuff_fclone_cache : skbuff_head_cache;
189
190 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
191 gfp_mask |= __GFP_MEMALLOC;
8798b3fb 192
1da177e4 193 /* Get the HEAD */
b30973f8 194 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
195 if (!skb)
196 goto out;
ec7d2f2c 197 prefetchw(skb);
1da177e4 198
87fb4b7b
ED
199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
203 */
bc417e30 204 size = SKB_DATA_ALIGN(size);
87fb4b7b 205 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c93bdd0e 206 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
1da177e4
LT
207 if (!data)
208 goto nodata;
87fb4b7b
ED
209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
212 */
213 size = SKB_WITH_OVERHEAD(ksize(data));
ec7d2f2c 214 prefetchw(data + size);
1da177e4 215
ca0605a7 216 /*
c8005785
JB
217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
ca0605a7
ACM
220 */
221 memset(skb, 0, offsetof(struct sk_buff, tail));
87fb4b7b
ED
222 /* Account for allocated memory : skb + skb->head */
223 skb->truesize = SKB_TRUESIZE(size);
c93bdd0e 224 skb->pfmemalloc = pfmemalloc;
63354797 225 refcount_set(&skb->users, 1);
1da177e4
LT
226 skb->head = data;
227 skb->data = data;
27a884dc 228 skb_reset_tail_pointer(skb);
4305b541 229 skb->end = skb->tail + size;
35d04610
CW
230 skb->mac_header = (typeof(skb->mac_header))~0U;
231 skb->transport_header = (typeof(skb->transport_header))~0U;
19633e12 232
4947d3ef
BL
233 /* make sure we initialize shinfo sequentially */
234 shinfo = skb_shinfo(skb);
ec7d2f2c 235 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
4947d3ef 236 atomic_set(&shinfo->dataref, 1);
4947d3ef 237
c93bdd0e 238 if (flags & SKB_ALLOC_FCLONE) {
d0bf4a9e 239 struct sk_buff_fclones *fclones;
1da177e4 240
d0bf4a9e
ED
241 fclones = container_of(skb, struct sk_buff_fclones, skb1);
242
d179cd12 243 skb->fclone = SKB_FCLONE_ORIG;
2638595a 244 refcount_set(&fclones->fclone_ref, 1);
d179cd12 245
6ffe75eb 246 fclones->skb2.fclone = SKB_FCLONE_CLONE;
d179cd12 247 }
1da177e4
LT
248out:
249 return skb;
250nodata:
8798b3fb 251 kmem_cache_free(cache, skb);
1da177e4
LT
252 skb = NULL;
253 goto out;
1da177e4 254}
b4ac530f 255EXPORT_SYMBOL(__alloc_skb);
1da177e4 256
ba0509b6
JDB
257/* Caller must provide SKB that is memset cleared */
258static struct sk_buff *__build_skb_around(struct sk_buff *skb,
259 void *data, unsigned int frag_size)
260{
261 struct skb_shared_info *shinfo;
262 unsigned int size = frag_size ? : ksize(data);
263
264 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
265
266 /* Assumes caller memset cleared SKB */
267 skb->truesize = SKB_TRUESIZE(size);
268 refcount_set(&skb->users, 1);
269 skb->head = data;
270 skb->data = data;
271 skb_reset_tail_pointer(skb);
272 skb->end = skb->tail + size;
273 skb->mac_header = (typeof(skb->mac_header))~0U;
274 skb->transport_header = (typeof(skb->transport_header))~0U;
275
276 /* make sure we initialize shinfo sequentially */
277 shinfo = skb_shinfo(skb);
278 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
279 atomic_set(&shinfo->dataref, 1);
280
281 return skb;
282}
283
b2b5ce9d 284/**
2ea2f62c 285 * __build_skb - build a network buffer
b2b5ce9d 286 * @data: data buffer provided by caller
2ea2f62c 287 * @frag_size: size of data, or 0 if head was kmalloced
b2b5ce9d
ED
288 *
289 * Allocate a new &sk_buff. Caller provides space holding head and
deceb4c0 290 * skb_shared_info. @data must have been allocated by kmalloc() only if
2ea2f62c
ED
291 * @frag_size is 0, otherwise data should come from the page allocator
292 * or vmalloc()
b2b5ce9d
ED
293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
295 * Notes :
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
302 */
2ea2f62c 303struct sk_buff *__build_skb(void *data, unsigned int frag_size)
b2b5ce9d 304{
b2b5ce9d 305 struct sk_buff *skb;
b2b5ce9d
ED
306
307 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
ba0509b6 308 if (unlikely(!skb))
b2b5ce9d
ED
309 return NULL;
310
b2b5ce9d 311 memset(skb, 0, offsetof(struct sk_buff, tail));
b2b5ce9d 312
ba0509b6 313 return __build_skb_around(skb, data, frag_size);
b2b5ce9d 314}
2ea2f62c
ED
315
316/* build_skb() is wrapper over __build_skb(), that specifically
317 * takes care of skb->head and skb->pfmemalloc
318 * This means that if @frag_size is not zero, then @data must be backed
319 * by a page fragment, not kmalloc() or vmalloc()
320 */
321struct sk_buff *build_skb(void *data, unsigned int frag_size)
322{
323 struct sk_buff *skb = __build_skb(data, frag_size);
324
325 if (skb && frag_size) {
326 skb->head_frag = 1;
2f064f34 327 if (page_is_pfmemalloc(virt_to_head_page(data)))
2ea2f62c
ED
328 skb->pfmemalloc = 1;
329 }
330 return skb;
331}
b2b5ce9d
ED
332EXPORT_SYMBOL(build_skb);
333
ba0509b6
JDB
334/**
335 * build_skb_around - build a network buffer around provided skb
336 * @skb: sk_buff provide by caller, must be memset cleared
337 * @data: data buffer provided by caller
338 * @frag_size: size of data, or 0 if head was kmalloced
339 */
340struct sk_buff *build_skb_around(struct sk_buff *skb,
341 void *data, unsigned int frag_size)
342{
343 if (unlikely(!skb))
344 return NULL;
345
346 skb = __build_skb_around(skb, data, frag_size);
347
348 if (skb && frag_size) {
349 skb->head_frag = 1;
350 if (page_is_pfmemalloc(virt_to_head_page(data)))
351 skb->pfmemalloc = 1;
352 }
353 return skb;
354}
355EXPORT_SYMBOL(build_skb_around);
356
795bb1c0
JDB
357#define NAPI_SKB_CACHE_SIZE 64
358
359struct napi_alloc_cache {
360 struct page_frag_cache page;
e0d7924a 361 unsigned int skb_count;
795bb1c0
JDB
362 void *skb_cache[NAPI_SKB_CACHE_SIZE];
363};
364
b63ae8ca 365static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
795bb1c0 366static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
ffde7328
AD
367
368static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
369{
b63ae8ca 370 struct page_frag_cache *nc;
ffde7328
AD
371 unsigned long flags;
372 void *data;
373
374 local_irq_save(flags);
9451980a 375 nc = this_cpu_ptr(&netdev_alloc_cache);
8c2dd3e4 376 data = page_frag_alloc(nc, fragsz, gfp_mask);
6f532612
ED
377 local_irq_restore(flags);
378 return data;
379}
c93bdd0e
MG
380
381/**
382 * netdev_alloc_frag - allocate a page fragment
383 * @fragsz: fragment size
384 *
385 * Allocates a frag from a page for receive buffer.
386 * Uses GFP_ATOMIC allocations.
387 */
388void *netdev_alloc_frag(unsigned int fragsz)
389{
3bed3cc4
AD
390 fragsz = SKB_DATA_ALIGN(fragsz);
391
453f85d4 392 return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
c93bdd0e 393}
6f532612
ED
394EXPORT_SYMBOL(netdev_alloc_frag);
395
ffde7328
AD
396static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
397{
795bb1c0 398 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
9451980a 399
8c2dd3e4 400 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
ffde7328
AD
401}
402
403void *napi_alloc_frag(unsigned int fragsz)
404{
3bed3cc4
AD
405 fragsz = SKB_DATA_ALIGN(fragsz);
406
453f85d4 407 return __napi_alloc_frag(fragsz, GFP_ATOMIC);
ffde7328
AD
408}
409EXPORT_SYMBOL(napi_alloc_frag);
410
fd11a83d
AD
411/**
412 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
413 * @dev: network device to receive on
d7499160 414 * @len: length to allocate
fd11a83d
AD
415 * @gfp_mask: get_free_pages mask, passed to alloc_skb
416 *
417 * Allocate a new &sk_buff and assign it a usage count of one. The
418 * buffer has NET_SKB_PAD headroom built in. Users should allocate
419 * the headroom they think they need without accounting for the
420 * built in space. The built in space is used for optimisations.
421 *
422 * %NULL is returned if there is no free memory.
423 */
9451980a
AD
424struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
425 gfp_t gfp_mask)
fd11a83d 426{
b63ae8ca 427 struct page_frag_cache *nc;
9451980a 428 unsigned long flags;
fd11a83d 429 struct sk_buff *skb;
9451980a
AD
430 bool pfmemalloc;
431 void *data;
432
433 len += NET_SKB_PAD;
fd11a83d 434
9451980a 435 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
d0164adc 436 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
a080e7bd
AD
437 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
438 if (!skb)
439 goto skb_fail;
440 goto skb_success;
441 }
fd11a83d 442
9451980a
AD
443 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
444 len = SKB_DATA_ALIGN(len);
445
446 if (sk_memalloc_socks())
447 gfp_mask |= __GFP_MEMALLOC;
448
449 local_irq_save(flags);
450
451 nc = this_cpu_ptr(&netdev_alloc_cache);
8c2dd3e4 452 data = page_frag_alloc(nc, len, gfp_mask);
9451980a
AD
453 pfmemalloc = nc->pfmemalloc;
454
455 local_irq_restore(flags);
456
457 if (unlikely(!data))
458 return NULL;
459
460 skb = __build_skb(data, len);
461 if (unlikely(!skb)) {
181edb2b 462 skb_free_frag(data);
9451980a 463 return NULL;
7b2e497a 464 }
fd11a83d 465
9451980a
AD
466 /* use OR instead of assignment to avoid clearing of bits in mask */
467 if (pfmemalloc)
468 skb->pfmemalloc = 1;
469 skb->head_frag = 1;
470
a080e7bd 471skb_success:
9451980a
AD
472 skb_reserve(skb, NET_SKB_PAD);
473 skb->dev = dev;
474
a080e7bd 475skb_fail:
8af27456
CH
476 return skb;
477}
b4ac530f 478EXPORT_SYMBOL(__netdev_alloc_skb);
1da177e4 479
fd11a83d
AD
480/**
481 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
482 * @napi: napi instance this buffer was allocated for
d7499160 483 * @len: length to allocate
fd11a83d
AD
484 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
485 *
486 * Allocate a new sk_buff for use in NAPI receive. This buffer will
487 * attempt to allocate the head from a special reserved region used
488 * only for NAPI Rx allocation. By doing this we can save several
489 * CPU cycles by avoiding having to disable and re-enable IRQs.
490 *
491 * %NULL is returned if there is no free memory.
492 */
9451980a
AD
493struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
494 gfp_t gfp_mask)
fd11a83d 495{
795bb1c0 496 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
fd11a83d 497 struct sk_buff *skb;
9451980a
AD
498 void *data;
499
500 len += NET_SKB_PAD + NET_IP_ALIGN;
fd11a83d 501
9451980a 502 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
d0164adc 503 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
a080e7bd
AD
504 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
505 if (!skb)
506 goto skb_fail;
507 goto skb_success;
508 }
9451980a
AD
509
510 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
511 len = SKB_DATA_ALIGN(len);
512
513 if (sk_memalloc_socks())
514 gfp_mask |= __GFP_MEMALLOC;
fd11a83d 515
8c2dd3e4 516 data = page_frag_alloc(&nc->page, len, gfp_mask);
9451980a
AD
517 if (unlikely(!data))
518 return NULL;
519
520 skb = __build_skb(data, len);
521 if (unlikely(!skb)) {
181edb2b 522 skb_free_frag(data);
9451980a 523 return NULL;
fd11a83d
AD
524 }
525
9451980a 526 /* use OR instead of assignment to avoid clearing of bits in mask */
795bb1c0 527 if (nc->page.pfmemalloc)
9451980a
AD
528 skb->pfmemalloc = 1;
529 skb->head_frag = 1;
530
a080e7bd 531skb_success:
9451980a
AD
532 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
533 skb->dev = napi->dev;
534
a080e7bd 535skb_fail:
fd11a83d
AD
536 return skb;
537}
538EXPORT_SYMBOL(__napi_alloc_skb);
539
654bed16 540void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
50269e19 541 int size, unsigned int truesize)
654bed16
PZ
542{
543 skb_fill_page_desc(skb, i, page, off, size);
544 skb->len += size;
545 skb->data_len += size;
50269e19 546 skb->truesize += truesize;
654bed16
PZ
547}
548EXPORT_SYMBOL(skb_add_rx_frag);
549
f8e617e1
JW
550void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
551 unsigned int truesize)
552{
553 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
554
555 skb_frag_size_add(frag, size);
556 skb->len += size;
557 skb->data_len += size;
558 skb->truesize += truesize;
559}
560EXPORT_SYMBOL(skb_coalesce_rx_frag);
561
27b437c8 562static void skb_drop_list(struct sk_buff **listp)
1da177e4 563{
bd8a7036 564 kfree_skb_list(*listp);
27b437c8 565 *listp = NULL;
1da177e4
LT
566}
567
27b437c8
HX
568static inline void skb_drop_fraglist(struct sk_buff *skb)
569{
570 skb_drop_list(&skb_shinfo(skb)->frag_list);
571}
572
1da177e4
LT
573static void skb_clone_fraglist(struct sk_buff *skb)
574{
575 struct sk_buff *list;
576
fbb398a8 577 skb_walk_frags(skb, list)
1da177e4
LT
578 skb_get(list);
579}
580
d3836f21
ED
581static void skb_free_head(struct sk_buff *skb)
582{
181edb2b
AD
583 unsigned char *head = skb->head;
584
d3836f21 585 if (skb->head_frag)
181edb2b 586 skb_free_frag(head);
d3836f21 587 else
181edb2b 588 kfree(head);
d3836f21
ED
589}
590
5bba1712 591static void skb_release_data(struct sk_buff *skb)
1da177e4 592{
ff04a771
ED
593 struct skb_shared_info *shinfo = skb_shinfo(skb);
594 int i;
1da177e4 595
ff04a771
ED
596 if (skb->cloned &&
597 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
598 &shinfo->dataref))
599 return;
a6686f2f 600
ff04a771
ED
601 for (i = 0; i < shinfo->nr_frags; i++)
602 __skb_frag_unref(&shinfo->frags[i]);
a6686f2f 603
ff04a771
ED
604 if (shinfo->frag_list)
605 kfree_skb_list(shinfo->frag_list);
606
1f8b977a 607 skb_zcopy_clear(skb, true);
ff04a771 608 skb_free_head(skb);
1da177e4
LT
609}
610
611/*
612 * Free an skbuff by memory without cleaning the state.
613 */
2d4baff8 614static void kfree_skbmem(struct sk_buff *skb)
1da177e4 615{
d0bf4a9e 616 struct sk_buff_fclones *fclones;
d179cd12 617
d179cd12
DM
618 switch (skb->fclone) {
619 case SKB_FCLONE_UNAVAILABLE:
620 kmem_cache_free(skbuff_head_cache, skb);
6ffe75eb 621 return;
d179cd12
DM
622
623 case SKB_FCLONE_ORIG:
d0bf4a9e 624 fclones = container_of(skb, struct sk_buff_fclones, skb1);
d179cd12 625
6ffe75eb
ED
626 /* We usually free the clone (TX completion) before original skb
627 * This test would have no chance to be true for the clone,
628 * while here, branch prediction will be good.
d179cd12 629 */
2638595a 630 if (refcount_read(&fclones->fclone_ref) == 1)
6ffe75eb
ED
631 goto fastpath;
632 break;
e7820e39 633
6ffe75eb
ED
634 default: /* SKB_FCLONE_CLONE */
635 fclones = container_of(skb, struct sk_buff_fclones, skb2);
d179cd12 636 break;
3ff50b79 637 }
2638595a 638 if (!refcount_dec_and_test(&fclones->fclone_ref))
6ffe75eb
ED
639 return;
640fastpath:
641 kmem_cache_free(skbuff_fclone_cache, fclones);
1da177e4
LT
642}
643
0a463c78 644void skb_release_head_state(struct sk_buff *skb)
1da177e4 645{
adf30907 646 skb_dst_drop(skb);
9c2b3328
SH
647 if (skb->destructor) {
648 WARN_ON(in_irq());
1da177e4
LT
649 skb->destructor(skb);
650 }
a3bf7ae9 651#if IS_ENABLED(CONFIG_NF_CONNTRACK)
cb9c6836 652 nf_conntrack_put(skb_nfct(skb));
1da177e4 653#endif
df5042f4 654 skb_ext_put(skb);
04a4bb55
LB
655}
656
657/* Free everything but the sk_buff shell. */
658static void skb_release_all(struct sk_buff *skb)
659{
660 skb_release_head_state(skb);
a28b1b90
FW
661 if (likely(skb->head))
662 skb_release_data(skb);
2d4baff8
HX
663}
664
665/**
666 * __kfree_skb - private function
667 * @skb: buffer
668 *
669 * Free an sk_buff. Release anything attached to the buffer.
670 * Clean the state. This is an internal helper function. Users should
671 * always call kfree_skb
672 */
1da177e4 673
2d4baff8
HX
674void __kfree_skb(struct sk_buff *skb)
675{
676 skb_release_all(skb);
1da177e4
LT
677 kfree_skbmem(skb);
678}
b4ac530f 679EXPORT_SYMBOL(__kfree_skb);
1da177e4 680