]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/core/skbuff.c
Merge remote-tracking branches 'asoc/topic/adsp', 'asoc/topic/ak4613', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / net / core / skbuff.c
CommitLineData
1da177e4
LT
1/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
113aa838 4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
1da177e4
LT
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
e005d193
JP
39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
1da177e4
LT
41#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
fe55f6d5 44#include <linux/kmemcheck.h>
1da177e4
LT
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
de960aa9
FW
50#include <linux/tcp.h>
51#include <linux/udp.h>
90017acc 52#include <linux/sctp.h>
1da177e4
LT
53#include <linux/netdevice.h>
54#ifdef CONFIG_NET_CLS_ACT
55#include <net/pkt_sched.h>
56#endif
57#include <linux/string.h>
58#include <linux/skbuff.h>
9c55e01c 59#include <linux/splice.h>
1da177e4
LT
60#include <linux/cache.h>
61#include <linux/rtnetlink.h>
62#include <linux/init.h>
716ea3a7 63#include <linux/scatterlist.h>
ac45f602 64#include <linux/errqueue.h>
268bb0ce 65#include <linux/prefetch.h>
0d5501c1 66#include <linux/if_vlan.h>
1da177e4
LT
67
68#include <net/protocol.h>
69#include <net/dst.h>
70#include <net/sock.h>
71#include <net/checksum.h>
ed1f50c3 72#include <net/ip6_checksum.h>
1da177e4
LT
73#include <net/xfrm.h>
74
7c0f6ba6 75#include <linux/uaccess.h>
ad8d75ff 76#include <trace/events/skb.h>
51c56b00 77#include <linux/highmem.h>
b245be1f
WB
78#include <linux/capability.h>
79#include <linux/user_namespace.h>
a1f8e7f7 80
d7e8883c 81struct kmem_cache *skbuff_head_cache __read_mostly;
e18b890b 82static struct kmem_cache *skbuff_fclone_cache __read_mostly;
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
0ebd0ac5
PM
161struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
162{
163 struct sk_buff *skb;
164
165 /* Get the HEAD */
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
168 if (!skb)
169 goto out;
170
171 /*
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
175 */
176 memset(skb, 0, offsetof(struct sk_buff, tail));
5e71d9d7 177 skb->head = NULL;
0ebd0ac5
PM
178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
180
35d04610 181 skb->mac_header = (typeof(skb->mac_header))~0U;
0ebd0ac5
PM
182out:
183 return skb;
184}
185
1da177e4 186/**
d179cd12 187 * __alloc_skb - allocate a network buffer
1da177e4
LT
188 * @size: size to allocate
189 * @gfp_mask: allocation mask
c93bdd0e
MG
190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
b30973f8 194 * @node: numa node to allocate memory on
1da177e4
LT
195 *
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
94b6042c
BH
197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
1da177e4
LT
199 *
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
201 * %GFP_ATOMIC.
202 */
dd0fc66f 203struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
c93bdd0e 204 int flags, int node)
1da177e4 205{
e18b890b 206 struct kmem_cache *cache;
4947d3ef 207 struct skb_shared_info *shinfo;
1da177e4
LT
208 struct sk_buff *skb;
209 u8 *data;
c93bdd0e 210 bool pfmemalloc;
1da177e4 211
c93bdd0e
MG
212 cache = (flags & SKB_ALLOC_FCLONE)
213 ? skbuff_fclone_cache : skbuff_head_cache;
214
215 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216 gfp_mask |= __GFP_MEMALLOC;
8798b3fb 217
1da177e4 218 /* Get the HEAD */
b30973f8 219 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
220 if (!skb)
221 goto out;
ec7d2f2c 222 prefetchw(skb);
1da177e4 223
87fb4b7b
ED
224 /* We do our best to align skb_shared_info on a separate cache
225 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
226 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
227 * Both skb->head and skb_shared_info are cache line aligned.
228 */
bc417e30 229 size = SKB_DATA_ALIGN(size);
87fb4b7b 230 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c93bdd0e 231 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
1da177e4
LT
232 if (!data)
233 goto nodata;
87fb4b7b
ED
234 /* kmalloc(size) might give us more room than requested.
235 * Put skb_shared_info exactly at the end of allocated zone,
236 * to allow max possible filling before reallocation.
237 */
238 size = SKB_WITH_OVERHEAD(ksize(data));
ec7d2f2c 239 prefetchw(data + size);
1da177e4 240
ca0605a7 241 /*
c8005785
JB
242 * Only clear those fields we need to clear, not those that we will
243 * actually initialise below. Hence, don't put any more fields after
244 * the tail pointer in struct sk_buff!
ca0605a7
ACM
245 */
246 memset(skb, 0, offsetof(struct sk_buff, tail));
87fb4b7b
ED
247 /* Account for allocated memory : skb + skb->head */
248 skb->truesize = SKB_TRUESIZE(size);
c93bdd0e 249 skb->pfmemalloc = pfmemalloc;
1da177e4
LT
250 atomic_set(&skb->users, 1);
251 skb->head = data;
252 skb->data = data;
27a884dc 253 skb_reset_tail_pointer(skb);
4305b541 254 skb->end = skb->tail + size;
35d04610
CW
255 skb->mac_header = (typeof(skb->mac_header))~0U;
256 skb->transport_header = (typeof(skb->transport_header))~0U;
19633e12 257
4947d3ef
BL
258 /* make sure we initialize shinfo sequentially */
259 shinfo = skb_shinfo(skb);
ec7d2f2c 260 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
4947d3ef 261 atomic_set(&shinfo->dataref, 1);
c2aa3665 262 kmemcheck_annotate_variable(shinfo->destructor_arg);
4947d3ef 263
c93bdd0e 264 if (flags & SKB_ALLOC_FCLONE) {
d0bf4a9e 265 struct sk_buff_fclones *fclones;
1da177e4 266
d0bf4a9e
ED
267 fclones = container_of(skb, struct sk_buff_fclones, skb1);
268
269 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
d179cd12 270 skb->fclone = SKB_FCLONE_ORIG;
d0bf4a9e 271 atomic_set(&fclones->fclone_ref, 1);
d179cd12 272
6ffe75eb 273 fclones->skb2.fclone = SKB_FCLONE_CLONE;
d179cd12 274 }
1da177e4
LT
275out:
276 return skb;
277nodata:
8798b3fb 278 kmem_cache_free(cache, skb);
1da177e4
LT
279 skb = NULL;
280 goto out;
1da177e4 281}
b4ac530f 282EXPORT_SYMBOL(__alloc_skb);
1da177e4 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
ED
304{
305 struct skb_shared_info *shinfo;
306 struct sk_buff *skb;
d3836f21 307 unsigned int size = frag_size ? : ksize(data);
b2b5ce9d
ED
308
309 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310 if (!skb)
311 return NULL;
312
d3836f21 313 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
b2b5ce9d
ED
314
315 memset(skb, 0, offsetof(struct sk_buff, tail));
316 skb->truesize = SKB_TRUESIZE(size);
317 atomic_set(&skb->users, 1);
318 skb->head = data;
319 skb->data = data;
320 skb_reset_tail_pointer(skb);
321 skb->end = skb->tail + size;
35d04610
CW
322 skb->mac_header = (typeof(skb->mac_header))~0U;
323 skb->transport_header = (typeof(skb->transport_header))~0U;
b2b5ce9d
ED
324
325 /* make sure we initialize shinfo sequentially */
326 shinfo = skb_shinfo(skb);
327 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 atomic_set(&shinfo->dataref, 1);
329 kmemcheck_annotate_variable(shinfo->destructor_arg);
330
331 return skb;
332}
2ea2f62c
ED
333
334/* build_skb() is wrapper over __build_skb(), that specifically
335 * takes care of skb->head and skb->pfmemalloc
336 * This means that if @frag_size is not zero, then @data must be backed
337 * by a page fragment, not kmalloc() or vmalloc()
338 */
339struct sk_buff *build_skb(void *data, unsigned int frag_size)
340{
341 struct sk_buff *skb = __build_skb(data, frag_size);
342
343 if (skb && frag_size) {
344 skb->head_frag = 1;
2f064f34 345 if (page_is_pfmemalloc(virt_to_head_page(data)))
2ea2f62c
ED
346 skb->pfmemalloc = 1;
347 }
348 return skb;
349}
b2b5ce9d
ED
350EXPORT_SYMBOL(build_skb);
351
795bb1c0
JDB
352#define NAPI_SKB_CACHE_SIZE 64
353
354struct napi_alloc_cache {
355 struct page_frag_cache page;
e0d7924a 356 unsigned int skb_count;
795bb1c0
JDB
357 void *skb_cache[NAPI_SKB_CACHE_SIZE];
358};
359
b63ae8ca 360static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
795bb1c0 361static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
ffde7328
AD
362
363static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364{
b63ae8ca 365 struct page_frag_cache *nc;
ffde7328
AD
366 unsigned long flags;
367 void *data;
368
369 local_irq_save(flags);
9451980a 370 nc = this_cpu_ptr(&netdev_alloc_cache);
8c2dd3e4 371 data = page_frag_alloc(nc, fragsz, gfp_mask);
6f532612
ED
372 local_irq_restore(flags);
373 return data;
374}
c93bdd0e
MG
375
376/**
377 * netdev_alloc_frag - allocate a page fragment
378 * @fragsz: fragment size
379 *
380 * Allocates a frag from a page for receive buffer.
381 * Uses GFP_ATOMIC allocations.
382 */
383void *netdev_alloc_frag(unsigned int fragsz)
384{
385 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
386}
6f532612
ED
387EXPORT_SYMBOL(netdev_alloc_frag);
388
ffde7328
AD
389static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
390{
795bb1c0 391 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
9451980a 392
8c2dd3e4 393 return page_frag_alloc(&nc->page, fragsz, gfp_mask);
ffde7328
AD
394}
395
396void *napi_alloc_frag(unsigned int fragsz)
397{
398 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399}
400EXPORT_SYMBOL(napi_alloc_frag);
401
fd11a83d
AD
402/**
403 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
404 * @dev: network device to receive on
d7499160 405 * @len: length to allocate
fd11a83d
AD
406 * @gfp_mask: get_free_pages mask, passed to alloc_skb
407 *
408 * Allocate a new &sk_buff and assign it a usage count of one. The
409 * buffer has NET_SKB_PAD headroom built in. Users should allocate
410 * the headroom they think they need without accounting for the
411 * built in space. The built in space is used for optimisations.
412 *
413 * %NULL is returned if there is no free memory.
414 */
9451980a
AD
415struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
416 gfp_t gfp_mask)
fd11a83d 417{
b63ae8ca 418 struct page_frag_cache *nc;
9451980a 419 unsigned long flags;
fd11a83d 420 struct sk_buff *skb;
9451980a
AD
421 bool pfmemalloc;
422 void *data;
423
424 len += NET_SKB_PAD;
fd11a83d 425
9451980a 426 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
d0164adc 427 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
a080e7bd
AD
428 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
429 if (!skb)
430 goto skb_fail;
431 goto skb_success;
432 }
fd11a83d 433
9451980a
AD
434 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
435 len = SKB_DATA_ALIGN(len);
436
437 if (sk_memalloc_socks())
438 gfp_mask |= __GFP_MEMALLOC;
439
440 local_irq_save(flags);
441
442 nc = this_cpu_ptr(&netdev_alloc_cache);
8c2dd3e4 443 data = page_frag_alloc(nc, len, gfp_mask);
9451980a
AD
444 pfmemalloc = nc->pfmemalloc;
445
446 local_irq_restore(flags);
447
448 if (unlikely(!data))
449 return NULL;
450
451 skb = __build_skb(data, len);
452 if (unlikely(!skb)) {
181edb2b 453 skb_free_frag(data);
9451980a 454 return NULL;
7b2e497a 455 }
fd11a83d 456
9451980a
AD
457 /* use OR instead of assignment to avoid clearing of bits in mask */
458 if (pfmemalloc)
459 skb->pfmemalloc = 1;
460 skb->head_frag = 1;
461
a080e7bd 462skb_success:
9451980a
AD
463 skb_reserve(skb, NET_SKB_PAD);
464 skb->dev = dev;
465
a080e7bd 466skb_fail:
8af27456
CH
467 return skb;
468}
b4ac530f 469EXPORT_SYMBOL(__netdev_alloc_skb);
1da177e4 470
fd11a83d
AD
471/**
472 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
473 * @napi: napi instance this buffer was allocated for
d7499160 474 * @len: length to allocate
fd11a83d
AD
475 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
476 *
477 * Allocate a new sk_buff for use in NAPI receive. This buffer will
478 * attempt to allocate the head from a special reserved region used
479 * only for NAPI Rx allocation. By doing this we can save several
480 * CPU cycles by avoiding having to disable and re-enable IRQs.
481 *
482 * %NULL is returned if there is no free memory.
483 */
9451980a
AD
484struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
485 gfp_t gfp_mask)
fd11a83d 486{
795bb1c0 487 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
fd11a83d 488 struct sk_buff *skb;
9451980a
AD
489 void *data;
490
491 len += NET_SKB_PAD + NET_IP_ALIGN;
fd11a83d 492
9451980a 493 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
d0164adc 494 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
a080e7bd
AD
495 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
496 if (!skb)
497 goto skb_fail;
498 goto skb_success;
499 }
9451980a
AD
500
501 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
502 len = SKB_DATA_ALIGN(len);
503
504 if (sk_memalloc_socks())
505 gfp_mask |= __GFP_MEMALLOC;
fd11a83d 506
8c2dd3e4 507 data = page_frag_alloc(&nc->page, len, gfp_mask);
9451980a
AD
508 if (unlikely(!data))
509 return NULL;
510
511 skb = __build_skb(data, len);
512 if (unlikely(!skb)) {
181edb2b 513 skb_free_frag(data);
9451980a 514 return NULL;
fd11a83d
AD
515 }
516
9451980a 517 /* use OR instead of assignment to avoid clearing of bits in mask */
795bb1c0 518 if (nc->page.pfmemalloc)
9451980a
AD
519 skb->pfmemalloc = 1;
520 skb->head_frag = 1;
521
a080e7bd 522skb_success:
9451980a
AD
523 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
524 skb->dev = napi->dev;
525
a080e7bd 526skb_fail:
fd11a83d
AD
527 return skb;
528}
529EXPORT_SYMBOL(__napi_alloc_skb);
530
654bed16 531void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
50269e19 532 int size, unsigned int truesize)
654bed16
PZ
533{
534 skb_fill_page_desc(skb, i, page, off, size);
535 skb->len += size;
536 skb->data_len += size;
50269e19 537 skb->truesize += truesize;
654bed16
PZ
538}
539EXPORT_SYMBOL(skb_add_rx_frag);
540
f8e617e1
JW
541void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
542 unsigned int truesize)
543{
544 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
545
546 skb_frag_size_add(frag, size);
547 skb->len += size;
548 skb->data_len += size;
549 skb->truesize += truesize;
550}
551EXPORT_SYMBOL(skb_coalesce_rx_frag);
552
27b437c8 553static void skb_drop_list(struct sk_buff **listp)
1da177e4 554{
bd8a7036 555 kfree_skb_list(*listp);
27b437c8 556 *listp = NULL;
1da177e4
LT
557}
558
27b437c8
HX
559static inline void skb_drop_fraglist(struct sk_buff *skb)
560{
561 skb_drop_list(&skb_shinfo(skb)->frag_list);
562}
563
1da177e4
LT
564static void skb_clone_fraglist(struct sk_buff *skb)
565{
566 struct sk_buff *list;
567
fbb398a8 568 skb_walk_frags(skb, list)
1da177e4
LT
569 skb_get(list);
570}
571
d3836f21
ED
572static void skb_free_head(struct sk_buff *skb)
573{
181edb2b
AD
574 unsigned char *head = skb->head;
575
d3836f21 576 if (skb->head_frag)
181edb2b 577 skb_free_frag(head);
d3836f21 578 else
181edb2b 579 kfree(head);
d3836f21
ED
580}
581
5bba1712 582static void skb_release_data(struct sk_buff *skb)
1da177e4 583{
ff04a771
ED
584 struct skb_shared_info *shinfo = skb_shinfo(skb);
585 int i;
1da177e4 586
ff04a771
ED
587 if (skb->cloned &&
588 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
589 &shinfo->dataref))
590 return;
a6686f2f 591
ff04a771
ED
592 for (i = 0; i < shinfo->nr_frags; i++)
593 __skb_frag_unref(&shinfo->frags[i]);
a6686f2f 594
ff04a771
ED
595 /*
596 * If skb buf is from userspace, we need to notify the caller
597 * the lower device DMA has done;
598 */
599 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
600 struct ubuf_info *uarg;
1da177e4 601
ff04a771
ED
602 uarg = shinfo->destructor_arg;
603 if (uarg->callback)
604 uarg->callback(uarg, true);
1da177e4 605 }
ff04a771
ED
606
607 if (shinfo->frag_list)
608 kfree_skb_list(shinfo->frag_list);
609
610 skb_free_head(skb);
1da177e4
LT
611}
612
613/*
614 * Free an skbuff by memory without cleaning the state.
615 */
2d4baff8 616static void kfree_skbmem(struct sk_buff *skb)
1da177e4 617{
d0bf4a9e 618 struct sk_buff_fclones *fclones;
d179cd12 619
d179cd12
DM
620 switch (skb->fclone) {
621 case SKB_FCLONE_UNAVAILABLE:
622 kmem_cache_free(skbuff_head_cache, skb);
6ffe75eb 623 return;
d179cd12
DM
624
625 case SKB_FCLONE_ORIG:
d0bf4a9e 626 fclones = container_of(skb, struct sk_buff_fclones, skb1);
d179cd12 627
6ffe75eb
ED
628 /* We usually free the clone (TX completion) before original skb
629 * This test would have no chance to be true for the clone,
630 * while here, branch prediction will be good.
d179cd12 631 */
6ffe75eb
ED
632 if (atomic_read(&fclones->fclone_ref) == 1)
633 goto fastpath;
634 break;
e7820e39 635
6ffe75eb
ED
636 default: /* SKB_FCLONE_CLONE */
637 fclones = container_of(skb, struct sk_buff_fclones, skb2);
d179cd12 638 break;
3ff50b79 639 }
6ffe75eb
ED
640 if (!atomic_dec_and_test(&fclones->fclone_ref))
641 return;
642fastpath:
643 kmem_cache_free(skbuff_fclone_cache, fclones);
1da177e4
LT
644}
645
04a4bb55 646static void skb_release_head_state(struct sk_buff *skb)
1da177e4 647{
adf30907 648 skb_dst_drop(skb);
1da177e4
LT
649#ifdef CONFIG_XFRM
650 secpath_put(skb->sp);
651#endif
9c2b3328
SH
652 if (skb->destructor) {
653 WARN_ON(in_irq());
1da177e4
LT
654 skb->destructor(skb);
655 }
a3bf7ae9 656#if IS_ENABLED(CONFIG_NF_CONNTRACK)
cb9c6836 657 nf_conntrack_put(skb_nfct(skb));
2fc72c7b 658#endif
1109a90c 659#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1da177e4
LT
660 nf_bridge_put(skb->nf_bridge);
661#endif
04a4bb55
LB
662}
663
664/* Free everything but the sk_buff shell. */
665static void skb_release_all(struct sk_buff *skb)
666{
667 skb_release_head_state(skb);
5e71d9d7 668 if (likely(skb->head))
0ebd0ac5 669 skb_release_data(skb);
2d4baff8
HX
670}
671
672/**
673 * __kfree_skb - private function
674 * @skb: buffer
675 *
676 * Free an sk_buff. Release anything attached to the buffer.
677 * Clean the state. This is an internal helper function. Users should
678 * always call kfree_skb
679 */
1da177e4 680
2d4baff8
HX
681void __kfree_skb(struct sk_buff *skb)
682{
683 skb_release_all(skb);
1da177e4
LT
684 kfree_skbmem(skb);
685}
b4ac530f 686EXPORT_SYMBOL(__kfree_skb);
1da177e4 687