2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
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
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).
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.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache
*skbuff_head_cache __read_mostly
;
81 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
84 * skb_panic - private function for out-of-line support
88 * @msg: skb_over_panic or skb_under_panic
90 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
95 static void skb_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
,
98 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99 msg
, addr
, skb
->len
, sz
, skb
->head
, skb
->data
,
100 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
101 skb
->dev
? skb
->dev
->name
: "<NULL>");
105 static void skb_over_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
107 skb_panic(skb
, sz
, addr
, __func__
);
110 static void skb_under_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
112 skb_panic(skb
, sz
, addr
, __func__
);
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
122 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
125 static void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
,
126 unsigned long ip
, bool *pfmemalloc
)
129 bool ret_pfmemalloc
= false;
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
135 obj
= kmalloc_node_track_caller(size
,
136 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
138 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc
= true;
143 obj
= kmalloc_node_track_caller(size
, flags
, node
);
147 *pfmemalloc
= ret_pfmemalloc
;
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
158 struct sk_buff
*__alloc_skb_head(gfp_t gfp_mask
, int node
)
163 skb
= kmem_cache_alloc_node(skbuff_head_cache
,
164 gfp_mask
& ~__GFP_DMA
, node
);
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
173 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
175 skb
->truesize
= sizeof(struct sk_buff
);
176 atomic_set(&skb
->users
, 1);
178 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
184 * __alloc_skb - allocate a network buffer
185 * @size: size to allocate
186 * @gfp_mask: allocation mask
187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
191 * @node: numa node to allocate memory on
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
200 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
203 struct kmem_cache
*cache
;
204 struct skb_shared_info
*shinfo
;
209 cache
= (flags
& SKB_ALLOC_FCLONE
)
210 ? skbuff_fclone_cache
: skbuff_head_cache
;
212 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
213 gfp_mask
|= __GFP_MEMALLOC
;
216 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
226 size
= SKB_DATA_ALIGN(size
);
227 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
228 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
235 size
= SKB_WITH_OVERHEAD(ksize(data
));
236 prefetchw(data
+ size
);
239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
243 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
244 /* Account for allocated memory : skb + skb->head */
245 skb
->truesize
= SKB_TRUESIZE(size
);
246 skb
->pfmemalloc
= pfmemalloc
;
247 atomic_set(&skb
->users
, 1);
250 skb_reset_tail_pointer(skb
);
251 skb
->end
= skb
->tail
+ size
;
252 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
253 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
255 /* make sure we initialize shinfo sequentially */
256 shinfo
= skb_shinfo(skb
);
257 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
258 atomic_set(&shinfo
->dataref
, 1);
259 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
261 if (flags
& SKB_ALLOC_FCLONE
) {
262 struct sk_buff_fclones
*fclones
;
264 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
266 kmemcheck_annotate_bitfield(&fclones
->skb2
, flags1
);
267 skb
->fclone
= SKB_FCLONE_ORIG
;
268 atomic_set(&fclones
->fclone_ref
, 1);
270 fclones
->skb2
.fclone
= SKB_FCLONE_CLONE
;
271 fclones
->skb2
.pfmemalloc
= pfmemalloc
;
276 kmem_cache_free(cache
, skb
);
280 EXPORT_SYMBOL(__alloc_skb
);
283 * __build_skb - build a network buffer
284 * @data: data buffer provided by caller
285 * @frag_size: size of data, or 0 if head was kmalloced
287 * Allocate a new &sk_buff. Caller provides space holding head and
288 * skb_shared_info. @data must have been allocated by kmalloc() only if
289 * @frag_size is 0, otherwise data should come from the page allocator
291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
301 struct sk_buff
*__build_skb(void *data
, unsigned int frag_size
)
303 struct skb_shared_info
*shinfo
;
305 unsigned int size
= frag_size
? : ksize(data
);
307 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
311 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
313 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
314 skb
->truesize
= SKB_TRUESIZE(size
);
315 atomic_set(&skb
->users
, 1);
318 skb_reset_tail_pointer(skb
);
319 skb
->end
= skb
->tail
+ size
;
320 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
321 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
323 /* make sure we initialize shinfo sequentially */
324 shinfo
= skb_shinfo(skb
);
325 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
326 atomic_set(&shinfo
->dataref
, 1);
327 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
332 /* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
337 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
339 struct sk_buff
*skb
= __build_skb(data
, frag_size
);
341 if (skb
&& frag_size
) {
343 if (page_is_pfmemalloc(virt_to_head_page(data
)))
348 EXPORT_SYMBOL(build_skb
);
350 #define NAPI_SKB_CACHE_SIZE 64
352 struct napi_alloc_cache
{
353 struct page_frag_cache page
;
355 void *skb_cache
[NAPI_SKB_CACHE_SIZE
];
358 static DEFINE_PER_CPU(struct page_frag_cache
, netdev_alloc_cache
);
359 static DEFINE_PER_CPU(struct napi_alloc_cache
, napi_alloc_cache
);
361 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
363 struct page_frag_cache
*nc
;
367 local_irq_save(flags
);
368 nc
= this_cpu_ptr(&netdev_alloc_cache
);
369 data
= __alloc_page_frag(nc
, fragsz
, gfp_mask
);
370 local_irq_restore(flags
);
375 * netdev_alloc_frag - allocate a page fragment
376 * @fragsz: fragment size
378 * Allocates a frag from a page for receive buffer.
379 * Uses GFP_ATOMIC allocations.
381 void *netdev_alloc_frag(unsigned int fragsz
)
383 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
385 EXPORT_SYMBOL(netdev_alloc_frag
);
387 static void *__napi_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
389 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
391 return __alloc_page_frag(&nc
->page
, fragsz
, gfp_mask
);
394 void *napi_alloc_frag(unsigned int fragsz
)
396 return __napi_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
398 EXPORT_SYMBOL(napi_alloc_frag
);
401 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
402 * @dev: network device to receive on
403 * @len: length to allocate
404 * @gfp_mask: get_free_pages mask, passed to alloc_skb
406 * Allocate a new &sk_buff and assign it a usage count of one. The
407 * buffer has NET_SKB_PAD headroom built in. Users should allocate
408 * the headroom they think they need without accounting for the
409 * built in space. The built in space is used for optimisations.
411 * %NULL is returned if there is no free memory.
413 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
, unsigned int len
,
416 struct page_frag_cache
*nc
;
424 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
425 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
426 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
432 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
433 len
= SKB_DATA_ALIGN(len
);
435 if (sk_memalloc_socks())
436 gfp_mask
|= __GFP_MEMALLOC
;
438 local_irq_save(flags
);
440 nc
= this_cpu_ptr(&netdev_alloc_cache
);
441 data
= __alloc_page_frag(nc
, len
, gfp_mask
);
442 pfmemalloc
= nc
->pfmemalloc
;
444 local_irq_restore(flags
);
449 skb
= __build_skb(data
, len
);
450 if (unlikely(!skb
)) {
455 /* use OR instead of assignment to avoid clearing of bits in mask */
461 skb_reserve(skb
, NET_SKB_PAD
);
467 EXPORT_SYMBOL(__netdev_alloc_skb
);
470 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
471 * @napi: napi instance this buffer was allocated for
472 * @len: length to allocate
473 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
475 * Allocate a new sk_buff for use in NAPI receive. This buffer will
476 * attempt to allocate the head from a special reserved region used
477 * only for NAPI Rx allocation. By doing this we can save several
478 * CPU cycles by avoiding having to disable and re-enable IRQs.
480 * %NULL is returned if there is no free memory.
482 struct sk_buff
*__napi_alloc_skb(struct napi_struct
*napi
, unsigned int len
,
485 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
489 len
+= NET_SKB_PAD
+ NET_IP_ALIGN
;
491 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
492 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
493 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
499 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
500 len
= SKB_DATA_ALIGN(len
);
502 if (sk_memalloc_socks())
503 gfp_mask
|= __GFP_MEMALLOC
;
505 data
= __alloc_page_frag(&nc
->page
, len
, gfp_mask
);
509 skb
= __build_skb(data
, len
);
510 if (unlikely(!skb
)) {
515 /* use OR instead of assignment to avoid clearing of bits in mask */
516 if (nc
->page
.pfmemalloc
)
521 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
);
522 skb
->dev
= napi
->dev
;
527 EXPORT_SYMBOL(__napi_alloc_skb
);
529 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
530 int size
, unsigned int truesize
)
532 skb_fill_page_desc(skb
, i
, page
, off
, size
);
534 skb
->data_len
+= size
;
535 skb
->truesize
+= truesize
;
537 EXPORT_SYMBOL(skb_add_rx_frag
);
539 void skb_coalesce_rx_frag(struct sk_buff
*skb
, int i
, int size
,
540 unsigned int truesize
)
542 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
544 skb_frag_size_add(frag
, size
);
546 skb
->data_len
+= size
;
547 skb
->truesize
+= truesize
;
549 EXPORT_SYMBOL(skb_coalesce_rx_frag
);
551 static void skb_drop_list(struct sk_buff
**listp
)
553 kfree_skb_list(*listp
);
557 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
559 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
562 static void skb_clone_fraglist(struct sk_buff
*skb
)
564 struct sk_buff
*list
;
566 skb_walk_frags(skb
, list
)
570 static void skb_free_head(struct sk_buff
*skb
)
572 unsigned char *head
= skb
->head
;
580 static void skb_release_data(struct sk_buff
*skb
)
582 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
586 atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
590 for (i
= 0; i
< shinfo
->nr_frags
; i
++)
591 __skb_frag_unref(&shinfo
->frags
[i
]);
594 * If skb buf is from userspace, we need to notify the caller
595 * the lower device DMA has done;
597 if (shinfo
->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
598 struct ubuf_info
*uarg
;
600 uarg
= shinfo
->destructor_arg
;
602 uarg
->callback(uarg
, true);
605 if (shinfo
->frag_list
)
606 kfree_skb_list(shinfo
->frag_list
);
612 * Free an skbuff by memory without cleaning the state.
614 static void kfree_skbmem(struct sk_buff
*skb
)
616 struct sk_buff_fclones
*fclones
;
618 switch (skb
->fclone
) {
619 case SKB_FCLONE_UNAVAILABLE
:
620 kmem_cache_free(skbuff_head_cache
, skb
);
623 case SKB_FCLONE_ORIG
:
624 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
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.
630 if (atomic_read(&fclones
->fclone_ref
) == 1)
634 default: /* SKB_FCLONE_CLONE */
635 fclones
= container_of(skb
, struct sk_buff_fclones
, skb2
);
638 if (!atomic_dec_and_test(&fclones
->fclone_ref
))
641 kmem_cache_free(skbuff_fclone_cache
, fclones
);
644 static void skb_release_head_state(struct sk_buff
*skb
)
648 secpath_put(skb
->sp
);
650 if (skb
->destructor
) {
652 skb
->destructor(skb
);
654 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
655 nf_conntrack_put(skb
->nfct
);
657 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
658 nf_bridge_put(skb
->nf_bridge
);
662 /* Free everything but the sk_buff shell. */
663 static void skb_release_all(struct sk_buff
*skb
)
665 skb_release_head_state(skb
);
666 if (likely(skb
->head
))
667 skb_release_data(skb
);
671 * __kfree_skb - private function
674 * Free an sk_buff. Release anything attached to the buffer.
675 * Clean the state. This is an internal helper function. Users should
676 * always call kfree_skb
679 void __kfree_skb(struct sk_buff
*skb
)
681 skb_release_all(skb
);
684 EXPORT_SYMBOL(__kfree_skb
);
687 * kfree_skb - free an sk_buff
688 * @skb: buffer to free
690 * Drop a reference to the buffer and free it if the usage count has
693 void kfree_skb(struct sk_buff
*skb
)
697 if (likely(atomic_read(&skb
->users
) == 1))
699 else if (likely(!atomic_dec_and_test(&skb
->users
)))
701 trace_kfree_skb(skb
, __builtin_return_address(0));
704 EXPORT_SYMBOL(kfree_skb
);
706 void kfree_skb_list(struct sk_buff
*segs
)
709 struct sk_buff
*next
= segs
->next
;
715 EXPORT_SYMBOL(kfree_skb_list
);
718 * skb_tx_error - report an sk_buff xmit error
719 * @skb: buffer that triggered an error
721 * Report xmit error if a device callback is tracking this skb.
722 * skb must be freed afterwards.
724 void skb_tx_error(struct sk_buff
*skb
)
726 if (skb_shinfo(skb
)->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
727 struct ubuf_info
*uarg
;
729 uarg
= skb_shinfo(skb
)->destructor_arg
;
731 uarg
->callback(uarg
, false);
732 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
735 EXPORT_SYMBOL(skb_tx_error
);
738 * consume_skb - free an skbuff
739 * @skb: buffer to free
741 * Drop a ref to the buffer and free it if the usage count has hit zero
742 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
743 * is being dropped after a failure and notes that
745 void consume_skb(struct sk_buff
*skb
)
749 if (likely(atomic_read(&skb
->users
) == 1))
751 else if (likely(!atomic_dec_and_test(&skb
->users
)))
753 trace_consume_skb(skb
);
756 EXPORT_SYMBOL(consume_skb
);
758 void __kfree_skb_flush(void)
760 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
762 /* flush skb_cache if containing objects */
764 kmem_cache_free_bulk(skbuff_head_cache
, nc
->skb_count
,
770 static inline void _kfree_skb_defer(struct sk_buff
*skb
)
772 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
774 /* drop skb->head and call any destructors for packet */
775 skb_release_all(skb
);
777 /* record skb to CPU local list */
778 nc
->skb_cache
[nc
->skb_count
++] = skb
;
781 /* SLUB writes into objects when freeing */
785 /* flush skb_cache if it is filled */
786 if (unlikely(nc
->skb_count
== NAPI_SKB_CACHE_SIZE
)) {
787 kmem_cache_free_bulk(skbuff_head_cache
, NAPI_SKB_CACHE_SIZE
,
792 void __kfree_skb_defer(struct sk_buff
*skb
)
794 _kfree_skb_defer(skb
);
797 void napi_consume_skb(struct sk_buff
*skb
, int budget
)
802 /* if budget is 0 assume netpoll w/ IRQs disabled */
803 if (unlikely(!budget
)) {
804 dev_consume_skb_irq(skb
);
808 if (likely(atomic_read(&skb
->users
) == 1))
810 else if (likely(!atomic_dec_and_test(&skb
->users
)))
812 /* if reaching here SKB is ready to free */
813 trace_consume_skb(skb
);
815 /* if SKB is a clone, don't handle this case */
816 if (unlikely(skb
->fclone
!= SKB_FCLONE_UNAVAILABLE
)) {
821 _kfree_skb_defer(skb
);
823 EXPORT_SYMBOL(napi_consume_skb
);
825 /* Make sure a field is enclosed inside headers_start/headers_end section */
826 #define CHECK_SKB_FIELD(field) \
827 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
828 offsetof(struct sk_buff, headers_start)); \
829 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
830 offsetof(struct sk_buff, headers_end)); \
832 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
834 new->tstamp
= old
->tstamp
;
835 /* We do not copy old->sk */
837 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
838 skb_dst_copy(new, old
);
840 new->sp
= secpath_get(old
->sp
);
842 __nf_copy(new, old
, false);
844 /* Note : this field could be in headers_start/headers_end section
845 * It is not yet because we do not want to have a 16 bit hole
847 new->queue_mapping
= old
->queue_mapping
;
849 memcpy(&new->headers_start
, &old
->headers_start
,
850 offsetof(struct sk_buff
, headers_end
) -
851 offsetof(struct sk_buff
, headers_start
));
852 CHECK_SKB_FIELD(protocol
);
853 CHECK_SKB_FIELD(csum
);
854 CHECK_SKB_FIELD(hash
);
855 CHECK_SKB_FIELD(priority
);
856 CHECK_SKB_FIELD(skb_iif
);
857 CHECK_SKB_FIELD(vlan_proto
);
858 CHECK_SKB_FIELD(vlan_tci
);
859 CHECK_SKB_FIELD(transport_header
);
860 CHECK_SKB_FIELD(network_header
);
861 CHECK_SKB_FIELD(mac_header
);
862 CHECK_SKB_FIELD(inner_protocol
);
863 CHECK_SKB_FIELD(inner_transport_header
);
864 CHECK_SKB_FIELD(inner_network_header
);
865 CHECK_SKB_FIELD(inner_mac_header
);
866 CHECK_SKB_FIELD(mark
);
867 #ifdef CONFIG_NETWORK_SECMARK
868 CHECK_SKB_FIELD(secmark
);
870 #ifdef CONFIG_NET_RX_BUSY_POLL
871 CHECK_SKB_FIELD(napi_id
);
874 CHECK_SKB_FIELD(sender_cpu
);
876 #ifdef CONFIG_NET_SCHED
877 CHECK_SKB_FIELD(tc_index
);
878 #ifdef CONFIG_NET_CLS_ACT
879 CHECK_SKB_FIELD(tc_verd
);
886 * You should not add any new code to this function. Add it to
887 * __copy_skb_header above instead.
889 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
891 #define C(x) n->x = skb->x
893 n
->next
= n
->prev
= NULL
;
895 __copy_skb_header(n
, skb
);
900 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
903 n
->destructor
= NULL
;
910 atomic_set(&n
->users
, 1);
912 atomic_inc(&(skb_shinfo(skb
)->dataref
));
920 * skb_morph - morph one skb into another
921 * @dst: the skb to receive the contents
922 * @src: the skb to supply the contents
924 * This is identical to skb_clone except that the target skb is
925 * supplied by the user.
927 * The target skb is returned upon exit.
929 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
931 skb_release_all(dst
);
932 return __skb_clone(dst
, src
);
934 EXPORT_SYMBOL_GPL(skb_morph
);
937 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
938 * @skb: the skb to modify
939 * @gfp_mask: allocation priority
941 * This must be called on SKBTX_DEV_ZEROCOPY skb.
942 * It will copy all frags into kernel and drop the reference
943 * to userspace pages.
945 * If this function is called from an interrupt gfp_mask() must be
948 * Returns 0 on success or a negative error code on failure
949 * to allocate kernel memory to copy to.
951 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
954 int num_frags
= skb_shinfo(skb
)->nr_frags
;
955 struct page
*page
, *head
= NULL
;
956 struct ubuf_info
*uarg
= skb_shinfo(skb
)->destructor_arg
;
958 for (i
= 0; i
< num_frags
; i
++) {
960 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
962 page
= alloc_page(gfp_mask
);
965 struct page
*next
= (struct page
*)page_private(head
);
971 vaddr
= kmap_atomic(skb_frag_page(f
));
972 memcpy(page_address(page
),
973 vaddr
+ f
->page_offset
, skb_frag_size(f
));
974 kunmap_atomic(vaddr
);
975 set_page_private(page
, (unsigned long)head
);
979 /* skb frags release userspace buffers */
980 for (i
= 0; i
< num_frags
; i
++)
981 skb_frag_unref(skb
, i
);
983 uarg
->callback(uarg
, false);
985 /* skb frags point to kernel buffers */
986 for (i
= num_frags
- 1; i
>= 0; i
--) {
987 __skb_fill_page_desc(skb
, i
, head
, 0,
988 skb_shinfo(skb
)->frags
[i
].size
);
989 head
= (struct page
*)page_private(head
);
992 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
995 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
998 * skb_clone - duplicate an sk_buff
999 * @skb: buffer to clone
1000 * @gfp_mask: allocation priority
1002 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1003 * copies share the same packet data but not structure. The new
1004 * buffer has a reference count of 1. If the allocation fails the
1005 * function returns %NULL otherwise the new buffer is returned.
1007 * If this function is called from an interrupt gfp_mask() must be
1011 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
1013 struct sk_buff_fclones
*fclones
= container_of(skb
,
1014 struct sk_buff_fclones
,
1018 if (skb_orphan_frags(skb
, gfp_mask
))
1021 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
1022 atomic_read(&fclones
->fclone_ref
) == 1) {
1024 atomic_set(&fclones
->fclone_ref
, 2);
1026 if (skb_pfmemalloc(skb
))
1027 gfp_mask
|= __GFP_MEMALLOC
;
1029 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
1033 kmemcheck_annotate_bitfield(n
, flags1
);
1034 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
1037 return __skb_clone(n
, skb
);
1039 EXPORT_SYMBOL(skb_clone
);
1041 static void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
1043 /* Only adjust this if it actually is csum_start rather than csum */
1044 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1045 skb
->csum_start
+= off
;
1046 /* {transport,network,mac}_header and tail are relative to skb->head */
1047 skb
->transport_header
+= off
;
1048 skb
->network_header
+= off
;
1049 if (skb_mac_header_was_set(skb
))
1050 skb
->mac_header
+= off
;
1051 skb
->inner_transport_header
+= off
;
1052 skb
->inner_network_header
+= off
;
1053 skb
->inner_mac_header
+= off
;
1056 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
1058 __copy_skb_header(new, old
);
1060 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
1061 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
1062 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
1065 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
1067 if (skb_pfmemalloc(skb
))
1068 return SKB_ALLOC_RX
;
1073 * skb_copy - create private copy of an sk_buff
1074 * @skb: buffer to copy
1075 * @gfp_mask: allocation priority
1077 * Make a copy of both an &sk_buff and its data. This is used when the
1078 * caller wishes to modify the data and needs a private copy of the
1079 * data to alter. Returns %NULL on failure or the pointer to the buffer
1080 * on success. The returned buffer has a reference count of 1.
1082 * As by-product this function converts non-linear &sk_buff to linear
1083 * one, so that &sk_buff becomes completely private and caller is allowed
1084 * to modify all the data of returned buffer. This means that this
1085 * function is not recommended for use in circumstances when only
1086 * header is going to be modified. Use pskb_copy() instead.
1089 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
1091 int headerlen
= skb_headroom(skb
);
1092 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
1093 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
1094 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
1099 /* Set the data pointer */
1100 skb_reserve(n
, headerlen
);
1101 /* Set the tail pointer and length */
1102 skb_put(n
, skb
->len
);
1104 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
1107 copy_skb_header(n
, skb
);
1110 EXPORT_SYMBOL(skb_copy
);
1113 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1114 * @skb: buffer to copy
1115 * @headroom: headroom of new skb
1116 * @gfp_mask: allocation priority
1117 * @fclone: if true allocate the copy of the skb from the fclone
1118 * cache instead of the head cache; it is recommended to set this
1119 * to true for the cases where the copy will likely be cloned
1121 * Make a copy of both an &sk_buff and part of its data, located
1122 * in header. Fragmented data remain shared. This is used when
1123 * the caller wishes to modify only header of &sk_buff and needs
1124 * private copy of the header to alter. Returns %NULL on failure
1125 * or the pointer to the buffer on success.
1126 * The returned buffer has a reference count of 1.
1129 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
1130 gfp_t gfp_mask
, bool fclone
)
1132 unsigned int size
= skb_headlen(skb
) + headroom
;
1133 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
1134 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
1139 /* Set the data pointer */
1140 skb_reserve(n
, headroom
);
1141 /* Set the tail pointer and length */
1142 skb_put(n
, skb_headlen(skb
));
1143 /* Copy the bytes */
1144 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1146 n
->truesize
+= skb
->data_len
;
1147 n
->data_len
= skb
->data_len
;
1150 if (skb_shinfo(skb
)->nr_frags
) {
1153 if (skb_orphan_frags(skb
, gfp_mask
)) {
1158 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1159 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1160 skb_frag_ref(skb
, i
);
1162 skb_shinfo(n
)->nr_frags
= i
;
1165 if (skb_has_frag_list(skb
)) {
1166 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1167 skb_clone_fraglist(n
);
1170 copy_skb_header(n
, skb
);
1174 EXPORT_SYMBOL(__pskb_copy_fclone
);
1177 * pskb_expand_head - reallocate header of &sk_buff
1178 * @skb: buffer to reallocate
1179 * @nhead: room to add at head
1180 * @ntail: room to add at tail
1181 * @gfp_mask: allocation priority
1183 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1184 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1185 * reference count of 1. Returns zero in the case of success or error,
1186 * if expansion failed. In the last case, &sk_buff is not changed.
1188 * All the pointers pointing into skb header may change and must be
1189 * reloaded after call to this function.
1192 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1197 int size
= nhead
+ skb_end_offset(skb
) + ntail
;
1202 if (skb_shared(skb
))
1205 size
= SKB_DATA_ALIGN(size
);
1207 if (skb_pfmemalloc(skb
))
1208 gfp_mask
|= __GFP_MEMALLOC
;
1209 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1210 gfp_mask
, NUMA_NO_NODE
, NULL
);
1213 size
= SKB_WITH_OVERHEAD(ksize(data
));
1215 /* Copy only real data... and, alas, header. This should be
1216 * optimized for the cases when header is void.
1218 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1220 memcpy((struct skb_shared_info
*)(data
+ size
),
1222 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1225 * if shinfo is shared we must drop the old head gracefully, but if it
1226 * is not we can just drop the old head and let the existing refcount
1227 * be since all we did is relocate the values
1229 if (skb_cloned(skb
)) {
1230 /* copy this zero copy skb frags */
1231 if (skb_orphan_frags(skb
, gfp_mask
))
1233 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1234 skb_frag_ref(skb
, i
);
1236 if (skb_has_frag_list(skb
))
1237 skb_clone_fraglist(skb
);
1239 skb_release_data(skb
);
1243 off
= (data
+ nhead
) - skb
->head
;
1248 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1252 skb
->end
= skb
->head
+ size
;
1255 skb_headers_offset_update(skb
, nhead
);
1259 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1267 EXPORT_SYMBOL(pskb_expand_head
);
1269 /* Make private copy of skb with writable head and some headroom */
1271 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1273 struct sk_buff
*skb2
;
1274 int delta
= headroom
- skb_headroom(skb
);
1277 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1279 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1280 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1288 EXPORT_SYMBOL(skb_realloc_headroom
);
1291 * skb_copy_expand - copy and expand sk_buff
1292 * @skb: buffer to copy
1293 * @newheadroom: new free bytes at head
1294 * @newtailroom: new free bytes at tail
1295 * @gfp_mask: allocation priority
1297 * Make a copy of both an &sk_buff and its data and while doing so
1298 * allocate additional space.
1300 * This is used when the caller wishes to modify the data and needs a
1301 * private copy of the data to alter as well as more space for new fields.
1302 * Returns %NULL on failure or the pointer to the buffer
1303 * on success. The returned buffer has a reference count of 1.
1305 * You must pass %GFP_ATOMIC as the allocation priority if this function
1306 * is called from an interrupt.
1308 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1309 int newheadroom
, int newtailroom
,
1313 * Allocate the copy buffer
1315 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1316 gfp_mask
, skb_alloc_rx_flag(skb
),
1318 int oldheadroom
= skb_headroom(skb
);
1319 int head_copy_len
, head_copy_off
;
1324 skb_reserve(n
, newheadroom
);
1326 /* Set the tail pointer and length */
1327 skb_put(n
, skb
->len
);
1329 head_copy_len
= oldheadroom
;
1331 if (newheadroom
<= head_copy_len
)
1332 head_copy_len
= newheadroom
;
1334 head_copy_off
= newheadroom
- head_copy_len
;
1336 /* Copy the linear header and data. */
1337 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1338 skb
->len
+ head_copy_len
))
1341 copy_skb_header(n
, skb
);
1343 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1347 EXPORT_SYMBOL(skb_copy_expand
);
1350 * skb_pad - zero pad the tail of an skb
1351 * @skb: buffer to pad
1352 * @pad: space to pad
1354 * Ensure that a buffer is followed by a padding area that is zero
1355 * filled. Used by network drivers which may DMA or transfer data
1356 * beyond the buffer end onto the wire.
1358 * May return error in out of memory cases. The skb is freed on error.
1361 int skb_pad(struct sk_buff
*skb
, int pad
)
1366 /* If the skbuff is non linear tailroom is always zero.. */
1367 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1368 memset(skb
->data
+skb
->len
, 0, pad
);
1372 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1373 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1374 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1379 /* FIXME: The use of this function with non-linear skb's really needs
1382 err
= skb_linearize(skb
);
1386 memset(skb
->data
+ skb
->len
, 0, pad
);
1393 EXPORT_SYMBOL(skb_pad
);
1396 * pskb_put - add data to the tail of a potentially fragmented buffer
1397 * @skb: start of the buffer to use
1398 * @tail: tail fragment of the buffer to use
1399 * @len: amount of data to add
1401 * This function extends the used data area of the potentially
1402 * fragmented buffer. @tail must be the last fragment of @skb -- or
1403 * @skb itself. If this would exceed the total buffer size the kernel
1404 * will panic. A pointer to the first byte of the extra data is
1408 unsigned char *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1411 skb
->data_len
+= len
;
1414 return skb_put(tail
, len
);
1416 EXPORT_SYMBOL_GPL(pskb_put
);
1419 * skb_put - add data to a buffer
1420 * @skb: buffer to use
1421 * @len: amount of data to add
1423 * This function extends the used data area of the buffer. If this would
1424 * exceed the total buffer size the kernel will panic. A pointer to the
1425 * first byte of the extra data is returned.
1427 unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
1429 unsigned char *tmp
= skb_tail_pointer(skb
);
1430 SKB_LINEAR_ASSERT(skb
);
1433 if (unlikely(skb
->tail
> skb
->end
))
1434 skb_over_panic(skb
, len
, __builtin_return_address(0));
1437 EXPORT_SYMBOL(skb_put
);
1440 * skb_push - add data to the start of a buffer
1441 * @skb: buffer to use
1442 * @len: amount of data to add
1444 * This function extends the used data area of the buffer at the buffer
1445 * start. If this would exceed the total buffer headroom the kernel will
1446 * panic. A pointer to the first byte of the extra data is returned.
1448 unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
1452 if (unlikely(skb
->data
<skb
->head
))
1453 skb_under_panic(skb
, len
, __builtin_return_address(0));
1456 EXPORT_SYMBOL(skb_push
);
1459 * skb_pull - remove data from the start of a buffer
1460 * @skb: buffer to use
1461 * @len: amount of data to remove
1463 * This function removes data from the start of a buffer, returning
1464 * the memory to the headroom. A pointer to the next data in the buffer
1465 * is returned. Once the data has been pulled future pushes will overwrite
1468 unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1470 return skb_pull_inline(skb
, len
);
1472 EXPORT_SYMBOL(skb_pull
);
1475 * skb_trim - remove end from a buffer
1476 * @skb: buffer to alter
1479 * Cut the length of a buffer down by removing data from the tail. If
1480 * the buffer is already under the length specified it is not modified.
1481 * The skb must be linear.
1483 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1486 __skb_trim(skb
, len
);
1488 EXPORT_SYMBOL(skb_trim
);
1490 /* Trims skb to length len. It can change skb pointers.
1493 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1495 struct sk_buff
**fragp
;
1496 struct sk_buff
*frag
;
1497 int offset
= skb_headlen(skb
);
1498 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1502 if (skb_cloned(skb
) &&
1503 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1510 for (; i
< nfrags
; i
++) {
1511 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1518 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1521 skb_shinfo(skb
)->nr_frags
= i
;
1523 for (; i
< nfrags
; i
++)
1524 skb_frag_unref(skb
, i
);
1526 if (skb_has_frag_list(skb
))
1527 skb_drop_fraglist(skb
);
1531 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1532 fragp
= &frag
->next
) {
1533 int end
= offset
+ frag
->len
;
1535 if (skb_shared(frag
)) {
1536 struct sk_buff
*nfrag
;
1538 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1539 if (unlikely(!nfrag
))
1542 nfrag
->next
= frag
->next
;
1554 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1558 skb_drop_list(&frag
->next
);
1563 if (len
> skb_headlen(skb
)) {
1564 skb
->data_len
-= skb
->len
- len
;
1569 skb_set_tail_pointer(skb
, len
);
1574 EXPORT_SYMBOL(___pskb_trim
);
1577 * __pskb_pull_tail - advance tail of skb header
1578 * @skb: buffer to reallocate
1579 * @delta: number of bytes to advance tail
1581 * The function makes a sense only on a fragmented &sk_buff,
1582 * it expands header moving its tail forward and copying necessary
1583 * data from fragmented part.
1585 * &sk_buff MUST have reference count of 1.
1587 * Returns %NULL (and &sk_buff does not change) if pull failed
1588 * or value of new tail of skb in the case of success.
1590 * All the pointers pointing into skb header may change and must be
1591 * reloaded after call to this function.
1594 /* Moves tail of skb head forward, copying data from fragmented part,
1595 * when it is necessary.
1596 * 1. It may fail due to malloc failure.
1597 * 2. It may change skb pointers.
1599 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1601 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1603 /* If skb has not enough free space at tail, get new one
1604 * plus 128 bytes for future expansions. If we have enough
1605 * room at tail, reallocate without expansion only if skb is cloned.
1607 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1609 if (eat
> 0 || skb_cloned(skb
)) {
1610 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1615 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1618 /* Optimization: no fragments, no reasons to preestimate
1619 * size of pulled pages. Superb.
1621 if (!skb_has_frag_list(skb
))
1624 /* Estimate size of pulled pages. */
1626 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1627 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1634 /* If we need update frag list, we are in troubles.
1635 * Certainly, it possible to add an offset to skb data,
1636 * but taking into account that pulling is expected to
1637 * be very rare operation, it is worth to fight against
1638 * further bloating skb head and crucify ourselves here instead.
1639 * Pure masohism, indeed. 8)8)
1642 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1643 struct sk_buff
*clone
= NULL
;
1644 struct sk_buff
*insp
= NULL
;
1649 if (list
->len
<= eat
) {
1650 /* Eaten as whole. */
1655 /* Eaten partially. */
1657 if (skb_shared(list
)) {
1658 /* Sucks! We need to fork list. :-( */
1659 clone
= skb_clone(list
, GFP_ATOMIC
);
1665 /* This may be pulled without
1669 if (!pskb_pull(list
, eat
)) {
1677 /* Free pulled out fragments. */
1678 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1679 skb_shinfo(skb
)->frag_list
= list
->next
;
1682 /* And insert new clone at head. */
1685 skb_shinfo(skb
)->frag_list
= clone
;
1688 /* Success! Now we may commit changes to skb data. */
1693 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1694 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1697 skb_frag_unref(skb
, i
);
1700 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1702 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1703 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
1709 skb_shinfo(skb
)->nr_frags
= k
;
1712 skb
->data_len
-= delta
;
1714 return skb_tail_pointer(skb
);
1716 EXPORT_SYMBOL(__pskb_pull_tail
);
1719 * skb_copy_bits - copy bits from skb to kernel buffer
1721 * @offset: offset in source
1722 * @to: destination buffer
1723 * @len: number of bytes to copy
1725 * Copy the specified number of bytes from the source skb to the
1726 * destination buffer.
1729 * If its prototype is ever changed,
1730 * check arch/{*}/net/{*}.S files,
1731 * since it is called from BPF assembly code.
1733 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1735 int start
= skb_headlen(skb
);
1736 struct sk_buff
*frag_iter
;
1739 if (offset
> (int)skb
->len
- len
)
1743 if ((copy
= start
- offset
) > 0) {
1746 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1747 if ((len
-= copy
) == 0)
1753 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1755 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1757 WARN_ON(start
> offset
+ len
);
1759 end
= start
+ skb_frag_size(f
);
1760 if ((copy
= end
- offset
) > 0) {
1766 vaddr
= kmap_atomic(skb_frag_page(f
));
1768 vaddr
+ f
->page_offset
+ offset
- start
,
1770 kunmap_atomic(vaddr
);
1772 if ((len
-= copy
) == 0)
1780 skb_walk_frags(skb
, frag_iter
) {
1783 WARN_ON(start
> offset
+ len
);
1785 end
= start
+ frag_iter
->len
;
1786 if ((copy
= end
- offset
) > 0) {
1789 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
1791 if ((len
-= copy
) == 0)
1805 EXPORT_SYMBOL(skb_copy_bits
);
1808 * Callback from splice_to_pipe(), if we need to release some pages
1809 * at the end of the spd in case we error'ed out in filling the pipe.
1811 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1813 put_page(spd
->pages
[i
]);
1816 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
1817 unsigned int *offset
,
1820 struct page_frag
*pfrag
= sk_page_frag(sk
);
1822 if (!sk_page_frag_refill(sk
, pfrag
))
1825 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
1827 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
1828 page_address(page
) + *offset
, *len
);
1829 *offset
= pfrag
->offset
;
1830 pfrag
->offset
+= *len
;
1835 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
1837 unsigned int offset
)
1839 return spd
->nr_pages
&&
1840 spd
->pages
[spd
->nr_pages
- 1] == page
&&
1841 (spd
->partial
[spd
->nr_pages
- 1].offset
+
1842 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
1846 * Fill page/offset/length into spd, if it can hold more pages.
1848 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
1849 struct pipe_inode_info
*pipe
, struct page
*page
,
1850 unsigned int *len
, unsigned int offset
,
1854 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
1858 page
= linear_to_page(page
, len
, &offset
, sk
);
1862 if (spd_can_coalesce(spd
, page
, offset
)) {
1863 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
1867 spd
->pages
[spd
->nr_pages
] = page
;
1868 spd
->partial
[spd
->nr_pages
].len
= *len
;
1869 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1875 static bool __splice_segment(struct page
*page
, unsigned int poff
,
1876 unsigned int plen
, unsigned int *off
,
1878 struct splice_pipe_desc
*spd
, bool linear
,
1880 struct pipe_inode_info
*pipe
)
1885 /* skip this segment if already processed */
1891 /* ignore any bits we already processed */
1897 unsigned int flen
= min(*len
, plen
);
1899 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
1905 } while (*len
&& plen
);
1911 * Map linear and fragment data from the skb to spd. It reports true if the
1912 * pipe is full or if we already spliced the requested length.
1914 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
1915 unsigned int *offset
, unsigned int *len
,
1916 struct splice_pipe_desc
*spd
, struct sock
*sk
)
1920 /* map the linear part :
1921 * If skb->head_frag is set, this 'linear' part is backed by a
1922 * fragment, and if the head is not shared with any clones then
1923 * we can avoid a copy since we own the head portion of this page.
1925 if (__splice_segment(virt_to_page(skb
->data
),
1926 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
1929 skb_head_is_locked(skb
),
1934 * then map the fragments
1936 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1937 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1939 if (__splice_segment(skb_frag_page(f
),
1940 f
->page_offset
, skb_frag_size(f
),
1941 offset
, len
, spd
, false, sk
, pipe
))
1948 ssize_t
skb_socket_splice(struct sock
*sk
,
1949 struct pipe_inode_info
*pipe
,
1950 struct splice_pipe_desc
*spd
)
1954 /* Drop the socket lock, otherwise we have reverse
1955 * locking dependencies between sk_lock and i_mutex
1956 * here as compared to sendfile(). We enter here
1957 * with the socket lock held, and splice_to_pipe() will
1958 * grab the pipe inode lock. For sendfile() emulation,
1959 * we call into ->sendpage() with the i_mutex lock held
1960 * and networking will grab the socket lock.
1963 ret
= splice_to_pipe(pipe
, spd
);
1970 * Map data from the skb to a pipe. Should handle both the linear part,
1971 * the fragments, and the frag list. It does NOT handle frag lists within
1972 * the frag list, if such a thing exists. We'd probably need to recurse to
1973 * handle that cleanly.
1975 int skb_splice_bits(struct sk_buff
*skb
, struct sock
*sk
, unsigned int offset
,
1976 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1978 ssize_t (*splice_cb
)(struct sock
*,
1979 struct pipe_inode_info
*,
1980 struct splice_pipe_desc
*))
1982 struct partial_page partial
[MAX_SKB_FRAGS
];
1983 struct page
*pages
[MAX_SKB_FRAGS
];
1984 struct splice_pipe_desc spd
= {
1987 .nr_pages_max
= MAX_SKB_FRAGS
,
1989 .ops
= &nosteal_pipe_buf_ops
,
1990 .spd_release
= sock_spd_release
,
1992 struct sk_buff
*frag_iter
;
1996 * __skb_splice_bits() only fails if the output has no room left,
1997 * so no point in going over the frag_list for the error case.
1999 if (__skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
))
2005 * now see if we have a frag_list to map
2007 skb_walk_frags(skb
, frag_iter
) {
2010 if (__skb_splice_bits(frag_iter
, pipe
, &offset
, &tlen
, &spd
, sk
))
2016 ret
= splice_cb(sk
, pipe
, &spd
);
2020 EXPORT_SYMBOL_GPL(skb_splice_bits
);
2023 * skb_store_bits - store bits from kernel buffer to skb
2024 * @skb: destination buffer
2025 * @offset: offset in destination
2026 * @from: source buffer
2027 * @len: number of bytes to copy
2029 * Copy the specified number of bytes from the source buffer to the
2030 * destination skb. This function handles all the messy bits of
2031 * traversing fragment lists and such.
2034 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
2036 int start
= skb_headlen(skb
);
2037 struct sk_buff
*frag_iter
;
2040 if (offset
> (int)skb
->len
- len
)
2043 if ((copy
= start
- offset
) > 0) {
2046 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
2047 if ((len
-= copy
) == 0)
2053 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2054 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2057 WARN_ON(start
> offset
+ len
);
2059 end
= start
+ skb_frag_size(frag
);
2060 if ((copy
= end
- offset
) > 0) {
2066 vaddr
= kmap_atomic(skb_frag_page(frag
));
2067 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
2069 kunmap_atomic(vaddr
);
2071 if ((len
-= copy
) == 0)
2079 skb_walk_frags(skb
, frag_iter
) {
2082 WARN_ON(start
> offset
+ len
);
2084 end
= start
+ frag_iter
->len
;
2085 if ((copy
= end
- offset
) > 0) {
2088 if (skb_store_bits(frag_iter
, offset
- start
,
2091 if ((len
-= copy
) == 0)
2104 EXPORT_SYMBOL(skb_store_bits
);
2106 /* Checksum skb data. */
2107 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
2108 __wsum csum
, const struct skb_checksum_ops
*ops
)
2110 int start
= skb_headlen(skb
);
2111 int i
, copy
= start
- offset
;
2112 struct sk_buff
*frag_iter
;
2115 /* Checksum header. */
2119 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
2120 if ((len
-= copy
) == 0)
2126 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2128 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2130 WARN_ON(start
> offset
+ len
);
2132 end
= start
+ skb_frag_size(frag
);
2133 if ((copy
= end
- offset
) > 0) {
2139 vaddr
= kmap_atomic(skb_frag_page(frag
));
2140 csum2
= ops
->update(vaddr
+ frag
->page_offset
+
2141 offset
- start
, copy
, 0);
2142 kunmap_atomic(vaddr
);
2143 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2152 skb_walk_frags(skb
, frag_iter
) {
2155 WARN_ON(start
> offset
+ len
);
2157 end
= start
+ frag_iter
->len
;
2158 if ((copy
= end
- offset
) > 0) {
2162 csum2
= __skb_checksum(frag_iter
, offset
- start
,
2164 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2165 if ((len
-= copy
) == 0)
2176 EXPORT_SYMBOL(__skb_checksum
);
2178 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2179 int len
, __wsum csum
)
2181 const struct skb_checksum_ops ops
= {
2182 .update
= csum_partial_ext
,
2183 .combine
= csum_block_add_ext
,
2186 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2188 EXPORT_SYMBOL(skb_checksum
);
2190 /* Both of above in one bottle. */
2192 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2193 u8
*to
, int len
, __wsum csum
)
2195 int start
= skb_headlen(skb
);
2196 int i
, copy
= start
- offset
;
2197 struct sk_buff
*frag_iter
;
2204 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2206 if ((len
-= copy
) == 0)
2213 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2216 WARN_ON(start
> offset
+ len
);
2218 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2219 if ((copy
= end
- offset
) > 0) {
2222 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2226 vaddr
= kmap_atomic(skb_frag_page(frag
));
2227 csum2
= csum_partial_copy_nocheck(vaddr
+
2231 kunmap_atomic(vaddr
);
2232 csum
= csum_block_add(csum
, csum2
, pos
);
2242 skb_walk_frags(skb
, frag_iter
) {
2246 WARN_ON(start
> offset
+ len
);
2248 end
= start
+ frag_iter
->len
;
2249 if ((copy
= end
- offset
) > 0) {
2252 csum2
= skb_copy_and_csum_bits(frag_iter
,
2255 csum
= csum_block_add(csum
, csum2
, pos
);
2256 if ((len
-= copy
) == 0)
2267 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2270 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2271 * @from: source buffer
2273 * Calculates the amount of linear headroom needed in the 'to' skb passed
2274 * into skb_zerocopy().
2277 skb_zerocopy_headlen(const struct sk_buff
*from
)
2279 unsigned int hlen
= 0;
2281 if (!from
->head_frag
||
2282 skb_headlen(from
) < L1_CACHE_BYTES
||
2283 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2284 hlen
= skb_headlen(from
);
2286 if (skb_has_frag_list(from
))
2291 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2294 * skb_zerocopy - Zero copy skb to skb
2295 * @to: destination buffer
2296 * @from: source buffer
2297 * @len: number of bytes to copy from source buffer
2298 * @hlen: size of linear headroom in destination buffer
2300 * Copies up to `len` bytes from `from` to `to` by creating references
2301 * to the frags in the source buffer.
2303 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2304 * headroom in the `to` buffer.
2307 * 0: everything is OK
2308 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2309 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2312 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2315 int plen
= 0; /* length of skb->head fragment */
2318 unsigned int offset
;
2320 BUG_ON(!from
->head_frag
&& !hlen
);
2322 /* dont bother with small payloads */
2323 if (len
<= skb_tailroom(to
))
2324 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2327 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2332 plen
= min_t(int, skb_headlen(from
), len
);
2334 page
= virt_to_head_page(from
->head
);
2335 offset
= from
->data
- (unsigned char *)page_address(page
);
2336 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2343 to
->truesize
+= len
+ plen
;
2344 to
->len
+= len
+ plen
;
2345 to
->data_len
+= len
+ plen
;
2347 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2352 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2355 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2356 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2357 len
-= skb_shinfo(to
)->frags
[j
].size
;
2358 skb_frag_ref(to
, j
);
2361 skb_shinfo(to
)->nr_frags
= j
;
2365 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2367 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2372 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2373 csstart
= skb_checksum_start_offset(skb
);
2375 csstart
= skb_headlen(skb
);
2377 BUG_ON(csstart
> skb_headlen(skb
));
2379 skb_copy_from_linear_data(skb
, to
, csstart
);
2382 if (csstart
!= skb
->len
)
2383 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2384 skb
->len
- csstart
, 0);
2386 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2387 long csstuff
= csstart
+ skb
->csum_offset
;
2389 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2392 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2395 * skb_dequeue - remove from the head of the queue
2396 * @list: list to dequeue from
2398 * Remove the head of the list. The list lock is taken so the function
2399 * may be used safely with other locking list functions. The head item is
2400 * returned or %NULL if the list is empty.
2403 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2405 unsigned long flags
;
2406 struct sk_buff
*result
;
2408 spin_lock_irqsave(&list
->lock
, flags
);
2409 result
= __skb_dequeue(list
);
2410 spin_unlock_irqrestore(&list
->lock
, flags
);
2413 EXPORT_SYMBOL(skb_dequeue
);
2416 * skb_dequeue_tail - remove from the tail of the queue
2417 * @list: list to dequeue from
2419 * Remove the tail of the list. The list lock is taken so the function
2420 * may be used safely with other locking list functions. The tail item is
2421 * returned or %NULL if the list is empty.
2423 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2425 unsigned long flags
;
2426 struct sk_buff
*result
;
2428 spin_lock_irqsave(&list
->lock
, flags
);
2429 result
= __skb_dequeue_tail(list
);
2430 spin_unlock_irqrestore(&list
->lock
, flags
);
2433 EXPORT_SYMBOL(skb_dequeue_tail
);
2436 * skb_queue_purge - empty a list
2437 * @list: list to empty
2439 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2440 * the list and one reference dropped. This function takes the list
2441 * lock and is atomic with respect to other list locking functions.
2443 void skb_queue_purge(struct sk_buff_head
*list
)
2445 struct sk_buff
*skb
;
2446 while ((skb
= skb_dequeue(list
)) != NULL
)
2449 EXPORT_SYMBOL(skb_queue_purge
);
2452 * skb_queue_head - queue a buffer at the list head
2453 * @list: list to use
2454 * @newsk: buffer to queue
2456 * Queue a buffer at the start of the list. This function takes the
2457 * list lock and can be used safely with other locking &sk_buff functions
2460 * A buffer cannot be placed on two lists at the same time.
2462 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2464 unsigned long flags
;
2466 spin_lock_irqsave(&list
->lock
, flags
);
2467 __skb_queue_head(list
, newsk
);
2468 spin_unlock_irqrestore(&list
->lock
, flags
);
2470 EXPORT_SYMBOL(skb_queue_head
);
2473 * skb_queue_tail - queue a buffer at the list tail
2474 * @list: list to use
2475 * @newsk: buffer to queue
2477 * Queue a buffer at the tail of the list. This function takes the
2478 * list lock and can be used safely with other locking &sk_buff functions
2481 * A buffer cannot be placed on two lists at the same time.
2483 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2485 unsigned long flags
;
2487 spin_lock_irqsave(&list
->lock
, flags
);
2488 __skb_queue_tail(list
, newsk
);
2489 spin_unlock_irqrestore(&list
->lock
, flags
);
2491 EXPORT_SYMBOL(skb_queue_tail
);
2494 * skb_unlink - remove a buffer from a list
2495 * @skb: buffer to remove
2496 * @list: list to use
2498 * Remove a packet from a list. The list locks are taken and this
2499 * function is atomic with respect to other list locked calls
2501 * You must know what list the SKB is on.
2503 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
2505 unsigned long flags
;
2507 spin_lock_irqsave(&list
->lock
, flags
);
2508 __skb_unlink(skb
, list
);
2509 spin_unlock_irqrestore(&list
->lock
, flags
);
2511 EXPORT_SYMBOL(skb_unlink
);
2514 * skb_append - append a buffer
2515 * @old: buffer to insert after
2516 * @newsk: buffer to insert
2517 * @list: list to use
2519 * Place a packet after a given packet in a list. The list locks are taken
2520 * and this function is atomic with respect to other list locked calls.
2521 * A buffer cannot be placed on two lists at the same time.
2523 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2525 unsigned long flags
;
2527 spin_lock_irqsave(&list
->lock
, flags
);
2528 __skb_queue_after(list
, old
, newsk
);
2529 spin_unlock_irqrestore(&list
->lock
, flags
);
2531 EXPORT_SYMBOL(skb_append
);
2534 * skb_insert - insert a buffer
2535 * @old: buffer to insert before
2536 * @newsk: buffer to insert
2537 * @list: list to use
2539 * Place a packet before a given packet in a list. The list locks are
2540 * taken and this function is atomic with respect to other list locked
2543 * A buffer cannot be placed on two lists at the same time.
2545 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2547 unsigned long flags
;
2549 spin_lock_irqsave(&list
->lock
, flags
);
2550 __skb_insert(newsk
, old
->prev
, old
, list
);
2551 spin_unlock_irqrestore(&list
->lock
, flags
);
2553 EXPORT_SYMBOL(skb_insert
);
2555 static inline void skb_split_inside_header(struct sk_buff
*skb
,
2556 struct sk_buff
* skb1
,
2557 const u32 len
, const int pos
)
2561 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
2563 /* And move data appendix as is. */
2564 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
2565 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
2567 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
2568 skb_shinfo(skb
)->nr_frags
= 0;
2569 skb1
->data_len
= skb
->data_len
;
2570 skb1
->len
+= skb1
->data_len
;
2573 skb_set_tail_pointer(skb
, len
);
2576 static inline void skb_split_no_header(struct sk_buff
*skb
,
2577 struct sk_buff
* skb1
,
2578 const u32 len
, int pos
)
2581 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
2583 skb_shinfo(skb
)->nr_frags
= 0;
2584 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
2586 skb
->data_len
= len
- pos
;
2588 for (i
= 0; i
< nfrags
; i
++) {
2589 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2591 if (pos
+ size
> len
) {
2592 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2596 * We have two variants in this case:
2597 * 1. Move all the frag to the second
2598 * part, if it is possible. F.e.
2599 * this approach is mandatory for TUX,
2600 * where splitting is expensive.
2601 * 2. Split is accurately. We make this.
2603 skb_frag_ref(skb
, i
);
2604 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
2605 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
2606 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
2607 skb_shinfo(skb
)->nr_frags
++;
2611 skb_shinfo(skb
)->nr_frags
++;
2614 skb_shinfo(skb1
)->nr_frags
= k
;
2618 * skb_split - Split fragmented skb to two parts at length len.
2619 * @skb: the buffer to split
2620 * @skb1: the buffer to receive the second part
2621 * @len: new length for skb
2623 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
2625 int pos
= skb_headlen(skb
);
2627 skb_shinfo(skb1
)->tx_flags
= skb_shinfo(skb
)->tx_flags
& SKBTX_SHARED_FRAG
;
2628 if (len
< pos
) /* Split line is inside header. */
2629 skb_split_inside_header(skb
, skb1
, len
, pos
);
2630 else /* Second chunk has no header, nothing to copy. */
2631 skb_split_no_header(skb
, skb1
, len
, pos
);
2633 EXPORT_SYMBOL(skb_split
);
2635 /* Shifting from/to a cloned skb is a no-go.
2637 * Caller cannot keep skb_shinfo related pointers past calling here!
2639 static int skb_prepare_for_shift(struct sk_buff
*skb
)
2641 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
2645 * skb_shift - Shifts paged data partially from skb to another
2646 * @tgt: buffer into which tail data gets added
2647 * @skb: buffer from which the paged data comes from
2648 * @shiftlen: shift up to this many bytes
2650 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2651 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2652 * It's up to caller to free skb if everything was shifted.
2654 * If @tgt runs out of frags, the whole operation is aborted.
2656 * Skb cannot include anything else but paged data while tgt is allowed
2657 * to have non-paged data as well.
2659 * TODO: full sized shift could be optimized but that would need
2660 * specialized skb free'er to handle frags without up-to-date nr_frags.
2662 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
2664 int from
, to
, merge
, todo
;
2665 struct skb_frag_struct
*fragfrom
, *fragto
;
2667 BUG_ON(shiftlen
> skb
->len
);
2668 BUG_ON(skb_headlen(skb
)); /* Would corrupt stream */
2672 to
= skb_shinfo(tgt
)->nr_frags
;
2673 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2675 /* Actual merge is delayed until the point when we know we can
2676 * commit all, so that we don't have to undo partial changes
2679 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
2680 fragfrom
->page_offset
)) {
2685 todo
-= skb_frag_size(fragfrom
);
2687 if (skb_prepare_for_shift(skb
) ||
2688 skb_prepare_for_shift(tgt
))
2691 /* All previous frag pointers might be stale! */
2692 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2693 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2695 skb_frag_size_add(fragto
, shiftlen
);
2696 skb_frag_size_sub(fragfrom
, shiftlen
);
2697 fragfrom
->page_offset
+= shiftlen
;
2705 /* Skip full, not-fitting skb to avoid expensive operations */
2706 if ((shiftlen
== skb
->len
) &&
2707 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
2710 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
2713 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
2714 if (to
== MAX_SKB_FRAGS
)
2717 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2718 fragto
= &skb_shinfo(tgt
)->frags
[to
];
2720 if (todo
>= skb_frag_size(fragfrom
)) {
2721 *fragto
= *fragfrom
;
2722 todo
-= skb_frag_size(fragfrom
);
2727 __skb_frag_ref(fragfrom
);
2728 fragto
->page
= fragfrom
->page
;
2729 fragto
->page_offset
= fragfrom
->page_offset
;
2730 skb_frag_size_set(fragto
, todo
);
2732 fragfrom
->page_offset
+= todo
;
2733 skb_frag_size_sub(fragfrom
, todo
);
2741 /* Ready to "commit" this state change to tgt */
2742 skb_shinfo(tgt
)->nr_frags
= to
;
2745 fragfrom
= &skb_shinfo(skb
)->frags
[0];
2746 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2748 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
2749 __skb_frag_unref(fragfrom
);
2752 /* Reposition in the original skb */
2754 while (from
< skb_shinfo(skb
)->nr_frags
)
2755 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
2756 skb_shinfo(skb
)->nr_frags
= to
;
2758 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
2761 /* Most likely the tgt won't ever need its checksum anymore, skb on
2762 * the other hand might need it if it needs to be resent
2764 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
2765 skb
->ip_summed
= CHECKSUM_PARTIAL
;
2767 /* Yak, is it really working this way? Some helper please? */
2768 skb
->len
-= shiftlen
;
2769 skb
->data_len
-= shiftlen
;
2770 skb
->truesize
-= shiftlen
;
2771 tgt
->len
+= shiftlen
;
2772 tgt
->data_len
+= shiftlen
;
2773 tgt
->truesize
+= shiftlen
;
2779 * skb_prepare_seq_read - Prepare a sequential read of skb data
2780 * @skb: the buffer to read
2781 * @from: lower offset of data to be read
2782 * @to: upper offset of data to be read
2783 * @st: state variable
2785 * Initializes the specified state variable. Must be called before
2786 * invoking skb_seq_read() for the first time.
2788 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
2789 unsigned int to
, struct skb_seq_state
*st
)
2791 st
->lower_offset
= from
;
2792 st
->upper_offset
= to
;
2793 st
->root_skb
= st
->cur_skb
= skb
;
2794 st
->frag_idx
= st
->stepped_offset
= 0;
2795 st
->frag_data
= NULL
;
2797 EXPORT_SYMBOL(skb_prepare_seq_read
);
2800 * skb_seq_read - Sequentially read skb data
2801 * @consumed: number of bytes consumed by the caller so far
2802 * @data: destination pointer for data to be returned
2803 * @st: state variable
2805 * Reads a block of skb data at @consumed relative to the
2806 * lower offset specified to skb_prepare_seq_read(). Assigns
2807 * the head of the data block to @data and returns the length
2808 * of the block or 0 if the end of the skb data or the upper
2809 * offset has been reached.
2811 * The caller is not required to consume all of the data
2812 * returned, i.e. @consumed is typically set to the number
2813 * of bytes already consumed and the next call to
2814 * skb_seq_read() will return the remaining part of the block.
2816 * Note 1: The size of each block of data returned can be arbitrary,
2817 * this limitation is the cost for zerocopy sequential
2818 * reads of potentially non linear data.
2820 * Note 2: Fragment lists within fragments are not implemented
2821 * at the moment, state->root_skb could be replaced with
2822 * a stack for this purpose.
2824 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
2825 struct skb_seq_state
*st
)
2827 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
2830 if (unlikely(abs_offset
>= st
->upper_offset
)) {
2831 if (st
->frag_data
) {
2832 kunmap_atomic(st
->frag_data
);
2833 st
->frag_data
= NULL
;
2839 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
2841 if (abs_offset
< block_limit
&& !st
->frag_data
) {
2842 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
2843 return block_limit
- abs_offset
;
2846 if (st
->frag_idx
== 0 && !st
->frag_data
)
2847 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
2849 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
2850 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
2851 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
2853 if (abs_offset
< block_limit
) {
2855 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
2857 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
2858 (abs_offset
- st
->stepped_offset
);
2860 return block_limit
- abs_offset
;
2863 if (st
->frag_data
) {
2864 kunmap_atomic(st
->frag_data
);
2865 st
->frag_data
= NULL
;
2869 st
->stepped_offset
+= skb_frag_size(frag
);
2872 if (st
->frag_data
) {
2873 kunmap_atomic(st
->frag_data
);
2874 st
->frag_data
= NULL
;
2877 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
2878 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2881 } else if (st
->cur_skb
->next
) {
2882 st
->cur_skb
= st
->cur_skb
->next
;
2889 EXPORT_SYMBOL(skb_seq_read
);
2892 * skb_abort_seq_read - Abort a sequential read of skb data
2893 * @st: state variable
2895 * Must be called if skb_seq_read() was not called until it
2898 void skb_abort_seq_read(struct skb_seq_state
*st
)
2901 kunmap_atomic(st
->frag_data
);
2903 EXPORT_SYMBOL(skb_abort_seq_read
);
2905 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2907 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2908 struct ts_config
*conf
,
2909 struct ts_state
*state
)
2911 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2914 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2916 skb_abort_seq_read(TS_SKB_CB(state
));
2920 * skb_find_text - Find a text pattern in skb data
2921 * @skb: the buffer to look in
2922 * @from: search offset
2924 * @config: textsearch configuration
2926 * Finds a pattern in the skb data according to the specified
2927 * textsearch configuration. Use textsearch_next() to retrieve
2928 * subsequent occurrences of the pattern. Returns the offset
2929 * to the first occurrence or UINT_MAX if no match was found.
2931 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2932 unsigned int to
, struct ts_config
*config
)
2934 struct ts_state state
;
2937 config
->get_next_block
= skb_ts_get_next_block
;
2938 config
->finish
= skb_ts_finish
;
2940 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(&state
));
2942 ret
= textsearch_find(config
, &state
);
2943 return (ret
<= to
- from
? ret
: UINT_MAX
);
2945 EXPORT_SYMBOL(skb_find_text
);
2948 * skb_append_datato_frags - append the user data to a skb
2949 * @sk: sock structure
2950 * @skb: skb structure to be appended with user data.
2951 * @getfrag: call back function to be used for getting the user data
2952 * @from: pointer to user message iov
2953 * @length: length of the iov message
2955 * Description: This procedure append the user data in the fragment part
2956 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2958 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2959 int (*getfrag
)(void *from
, char *to
, int offset
,
2960 int len
, int odd
, struct sk_buff
*skb
),
2961 void *from
, int length
)
2963 int frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2967 struct page_frag
*pfrag
= ¤t
->task_frag
;
2970 /* Return error if we don't have space for new frag */
2971 if (frg_cnt
>= MAX_SKB_FRAGS
)
2974 if (!sk_page_frag_refill(sk
, pfrag
))
2977 /* copy the user data to page */
2978 copy
= min_t(int, length
, pfrag
->size
- pfrag
->offset
);
2980 ret
= getfrag(from
, page_address(pfrag
->page
) + pfrag
->offset
,
2981 offset
, copy
, 0, skb
);
2985 /* copy was successful so update the size parameters */
2986 skb_fill_page_desc(skb
, frg_cnt
, pfrag
->page
, pfrag
->offset
,
2989 pfrag
->offset
+= copy
;
2990 get_page(pfrag
->page
);
2992 skb
->truesize
+= copy
;
2993 atomic_add(copy
, &sk
->sk_wmem_alloc
);
2995 skb
->data_len
+= copy
;
2999 } while (length
> 0);
3003 EXPORT_SYMBOL(skb_append_datato_frags
);
3005 int skb_append_pagefrags(struct sk_buff
*skb
, struct page
*page
,
3006 int offset
, size_t size
)
3008 int i
= skb_shinfo(skb
)->nr_frags
;
3010 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
3011 skb_frag_size_add(&skb_shinfo(skb
)->frags
[i
- 1], size
);
3012 } else if (i
< MAX_SKB_FRAGS
) {
3014 skb_fill_page_desc(skb
, i
, page
, offset
, size
);
3021 EXPORT_SYMBOL_GPL(skb_append_pagefrags
);
3024 * skb_pull_rcsum - pull skb and update receive checksum
3025 * @skb: buffer to update
3026 * @len: length of data pulled
3028 * This function performs an skb_pull on the packet and updates
3029 * the CHECKSUM_COMPLETE checksum. It should be used on
3030 * receive path processing instead of skb_pull unless you know
3031 * that the checksum difference is zero (e.g., a valid IP header)
3032 * or you are setting ip_summed to CHECKSUM_NONE.
3034 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
3036 unsigned char *data
= skb
->data
;
3038 BUG_ON(len
> skb
->len
);
3039 __skb_pull(skb
, len
);
3040 skb_postpull_rcsum(skb
, data
, len
);
3043 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
3046 * skb_segment - Perform protocol segmentation on skb.
3047 * @head_skb: buffer to segment
3048 * @features: features for the output path (see dev->features)
3050 * This function performs segmentation on the given skb. It returns
3051 * a pointer to the first in a list of new skbs for the segments.
3052 * In case of error it returns ERR_PTR(err).
3054 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
3055 netdev_features_t features
)
3057 struct sk_buff
*segs
= NULL
;
3058 struct sk_buff
*tail
= NULL
;
3059 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
3060 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
3061 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
3062 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
3063 struct sk_buff
*frag_skb
= head_skb
;
3064 unsigned int offset
= doffset
;
3065 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
3066 unsigned int headroom
;
3070 int sg
= !!(features
& NETIF_F_SG
);
3071 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
3077 __skb_push(head_skb
, doffset
);
3078 proto
= skb_network_protocol(head_skb
, &dummy
);
3079 if (unlikely(!proto
))
3080 return ERR_PTR(-EINVAL
);
3082 csum
= !!can_checksum_protocol(features
, proto
);
3084 headroom
= skb_headroom(head_skb
);
3085 pos
= skb_headlen(head_skb
);
3088 struct sk_buff
*nskb
;
3089 skb_frag_t
*nskb_frag
;
3093 len
= head_skb
->len
- offset
;
3097 hsize
= skb_headlen(head_skb
) - offset
;
3100 if (hsize
> len
|| !sg
)
3103 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
3104 (skb_headlen(list_skb
) == len
|| sg
)) {
3105 BUG_ON(skb_headlen(list_skb
) > len
);
3108 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3109 frag
= skb_shinfo(list_skb
)->frags
;
3110 frag_skb
= list_skb
;
3111 pos
+= skb_headlen(list_skb
);
3113 while (pos
< offset
+ len
) {
3114 BUG_ON(i
>= nfrags
);
3116 size
= skb_frag_size(frag
);
3117 if (pos
+ size
> offset
+ len
)
3125 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
3126 list_skb
= list_skb
->next
;
3128 if (unlikely(!nskb
))
3131 if (unlikely(pskb_trim(nskb
, len
))) {
3136 hsize
= skb_end_offset(nskb
);
3137 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
3142 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
3143 skb_release_head_state(nskb
);
3144 __skb_push(nskb
, doffset
);
3146 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
3147 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
3150 if (unlikely(!nskb
))
3153 skb_reserve(nskb
, headroom
);
3154 __skb_put(nskb
, doffset
);
3163 __copy_skb_header(nskb
, head_skb
);
3165 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
3166 skb_reset_mac_len(nskb
);
3168 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
3169 nskb
->data
- tnl_hlen
,
3170 doffset
+ tnl_hlen
);
3172 if (nskb
->len
== len
+ doffset
)
3173 goto perform_csum_check
;
3176 if (!nskb
->remcsum_offload
)
3177 nskb
->ip_summed
= CHECKSUM_NONE
;
3178 SKB_GSO_CB(nskb
)->csum
=
3179 skb_copy_and_csum_bits(head_skb
, offset
,
3182 SKB_GSO_CB(nskb
)->csum_start
=
3183 skb_headroom(nskb
) + doffset
;
3187 nskb_frag
= skb_shinfo(nskb
)->frags
;
3189 skb_copy_from_linear_data_offset(head_skb
, offset
,
3190 skb_put(nskb
, hsize
), hsize
);
3192 skb_shinfo(nskb
)->tx_flags
= skb_shinfo(head_skb
)->tx_flags
&
3195 while (pos
< offset
+ len
) {
3197 BUG_ON(skb_headlen(list_skb
));
3200 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3201 frag
= skb_shinfo(list_skb
)->frags
;
3202 frag_skb
= list_skb
;
3206 list_skb
= list_skb
->next
;
3209 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3211 net_warn_ratelimited(
3212 "skb_segment: too many frags: %u %u\n",
3217 if (unlikely(skb_orphan_frags(frag_skb
, GFP_ATOMIC
)))
3221 __skb_frag_ref(nskb_frag
);
3222 size
= skb_frag_size(nskb_frag
);
3225 nskb_frag
->page_offset
+= offset
- pos
;
3226 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3229 skb_shinfo(nskb
)->nr_frags
++;
3231 if (pos
+ size
<= offset
+ len
) {
3236 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3244 nskb
->data_len
= len
- hsize
;
3245 nskb
->len
+= nskb
->data_len
;
3246 nskb
->truesize
+= nskb
->data_len
;
3250 if (skb_has_shared_frag(nskb
)) {
3251 err
= __skb_linearize(nskb
);
3255 if (!nskb
->remcsum_offload
)
3256 nskb
->ip_summed
= CHECKSUM_NONE
;
3257 SKB_GSO_CB(nskb
)->csum
=
3258 skb_checksum(nskb
, doffset
,
3259 nskb
->len
- doffset
, 0);
3260 SKB_GSO_CB(nskb
)->csum_start
=
3261 skb_headroom(nskb
) + doffset
;
3263 } while ((offset
+= len
) < head_skb
->len
);
3265 /* Some callers want to get the end of the list.
3266 * Put it in segs->prev to avoid walking the list.
3267 * (see validate_xmit_skb_list() for example)
3271 /* Following permits correct backpressure, for protocols
3272 * using skb_set_owner_w().
3273 * Idea is to tranfert ownership from head_skb to last segment.
3275 if (head_skb
->destructor
== sock_wfree
) {
3276 swap(tail
->truesize
, head_skb
->truesize
);
3277 swap(tail
->destructor
, head_skb
->destructor
);
3278 swap(tail
->sk
, head_skb
->sk
);
3283 kfree_skb_list(segs
);
3284 return ERR_PTR(err
);
3286 EXPORT_SYMBOL_GPL(skb_segment
);
3288 int skb_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
)
3290 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3291 unsigned int offset
= skb_gro_offset(skb
);
3292 unsigned int headlen
= skb_headlen(skb
);
3293 unsigned int len
= skb_gro_len(skb
);
3294 struct sk_buff
*lp
, *p
= *head
;
3295 unsigned int delta_truesize
;
3297 if (unlikely(p
->len
+ len
>= 65536))
3300 lp
= NAPI_GRO_CB(p
)->last
;
3301 pinfo
= skb_shinfo(lp
);
3303 if (headlen
<= offset
) {
3306 int i
= skbinfo
->nr_frags
;
3307 int nr_frags
= pinfo
->nr_frags
+ i
;
3309 if (nr_frags
> MAX_SKB_FRAGS
)
3313 pinfo
->nr_frags
= nr_frags
;
3314 skbinfo
->nr_frags
= 0;
3316 frag
= pinfo
->frags
+ nr_frags
;
3317 frag2
= skbinfo
->frags
+ i
;
3322 frag
->page_offset
+= offset
;
3323 skb_frag_size_sub(frag
, offset
);
3325 /* all fragments truesize : remove (head size + sk_buff) */
3326 delta_truesize
= skb
->truesize
-
3327 SKB_TRUESIZE(skb_end_offset(skb
));
3329 skb
->truesize
-= skb
->data_len
;
3330 skb
->len
-= skb
->data_len
;
3333 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3335 } else if (skb
->head_frag
) {
3336 int nr_frags
= pinfo
->nr_frags
;
3337 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3338 struct page
*page
= virt_to_head_page(skb
->head
);
3339 unsigned int first_size
= headlen
- offset
;
3340 unsigned int first_offset
;
3342 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3345 first_offset
= skb
->data
-
3346 (unsigned char *)page_address(page
) +
3349 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3351 frag
->page
.p
= page
;
3352 frag
->page_offset
= first_offset
;
3353 skb_frag_size_set(frag
, first_size
);
3355 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3356 /* We dont need to clear skbinfo->nr_frags here */
3358 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3359 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3364 delta_truesize
= skb
->truesize
;
3365 if (offset
> headlen
) {
3366 unsigned int eat
= offset
- headlen
;
3368 skbinfo
->frags
[0].page_offset
+= eat
;
3369 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3370 skb
->data_len
-= eat
;
3375 __skb_pull(skb
, offset
);
3377 if (NAPI_GRO_CB(p
)->last
== p
)
3378 skb_shinfo(p
)->frag_list
= skb
;
3380 NAPI_GRO_CB(p
)->last
->next
= skb
;
3381 NAPI_GRO_CB(p
)->last
= skb
;
3382 __skb_header_release(skb
);
3386 NAPI_GRO_CB(p
)->count
++;
3388 p
->truesize
+= delta_truesize
;
3391 lp
->data_len
+= len
;
3392 lp
->truesize
+= delta_truesize
;
3395 NAPI_GRO_CB(skb
)->same_flow
= 1;
3399 void __init
skb_init(void)
3401 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
3402 sizeof(struct sk_buff
),
3404 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3406 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3407 sizeof(struct sk_buff_fclones
),
3409 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3414 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3415 * @skb: Socket buffer containing the buffers to be mapped
3416 * @sg: The scatter-gather list to map into
3417 * @offset: The offset into the buffer's contents to start mapping
3418 * @len: Length of buffer space to be mapped
3420 * Fill the specified scatter-gather list with mappings/pointers into a
3421 * region of the buffer space attached to a socket buffer.
3424 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3426 int start
= skb_headlen(skb
);
3427 int i
, copy
= start
- offset
;
3428 struct sk_buff
*frag_iter
;
3434 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
3436 if ((len
-= copy
) == 0)
3441 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3444 WARN_ON(start
> offset
+ len
);
3446 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3447 if ((copy
= end
- offset
) > 0) {
3448 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
3452 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
3453 frag
->page_offset
+offset
-start
);
3462 skb_walk_frags(skb
, frag_iter
) {
3465 WARN_ON(start
> offset
+ len
);
3467 end
= start
+ frag_iter
->len
;
3468 if ((copy
= end
- offset
) > 0) {
3471 elt
+= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
3473 if ((len
-= copy
) == 0)
3483 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3484 * sglist without mark the sg which contain last skb data as the end.
3485 * So the caller can mannipulate sg list as will when padding new data after
3486 * the first call without calling sg_unmark_end to expend sg list.
3488 * Scenario to use skb_to_sgvec_nomark:
3490 * 2. skb_to_sgvec_nomark(payload1)
3491 * 3. skb_to_sgvec_nomark(payload2)
3493 * This is equivalent to:
3495 * 2. skb_to_sgvec(payload1)
3497 * 4. skb_to_sgvec(payload2)
3499 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3500 * is more preferable.
3502 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
3503 int offset
, int len
)
3505 return __skb_to_sgvec(skb
, sg
, offset
, len
);
3507 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
3509 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3511 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
3513 sg_mark_end(&sg
[nsg
- 1]);
3517 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
3520 * skb_cow_data - Check that a socket buffer's data buffers are writable
3521 * @skb: The socket buffer to check.
3522 * @tailbits: Amount of trailing space to be added
3523 * @trailer: Returned pointer to the skb where the @tailbits space begins
3525 * Make sure that the data buffers attached to a socket buffer are
3526 * writable. If they are not, private copies are made of the data buffers
3527 * and the socket buffer is set to use these instead.
3529 * If @tailbits is given, make sure that there is space to write @tailbits
3530 * bytes of data beyond current end of socket buffer. @trailer will be
3531 * set to point to the skb in which this space begins.
3533 * The number of scatterlist elements required to completely map the
3534 * COW'd and extended socket buffer will be returned.
3536 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
3540 struct sk_buff
*skb1
, **skb_p
;
3542 /* If skb is cloned or its head is paged, reallocate
3543 * head pulling out all the pages (pages are considered not writable
3544 * at the moment even if they are anonymous).
3546 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
3547 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
3550 /* Easy case. Most of packets will go this way. */
3551 if (!skb_has_frag_list(skb
)) {
3552 /* A little of trouble, not enough of space for trailer.
3553 * This should not happen, when stack is tuned to generate
3554 * good frames. OK, on miss we reallocate and reserve even more
3555 * space, 128 bytes is fair. */
3557 if (skb_tailroom(skb
) < tailbits
&&
3558 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
3566 /* Misery. We are in troubles, going to mincer fragments... */
3569 skb_p
= &skb_shinfo(skb
)->frag_list
;
3572 while ((skb1
= *skb_p
) != NULL
) {
3575 /* The fragment is partially pulled by someone,
3576 * this can happen on input. Copy it and everything
3579 if (skb_shared(skb1
))
3582 /* If the skb is the last, worry about trailer. */
3584 if (skb1
->next
== NULL
&& tailbits
) {
3585 if (skb_shinfo(skb1
)->nr_frags
||
3586 skb_has_frag_list(skb1
) ||
3587 skb_tailroom(skb1
) < tailbits
)
3588 ntail
= tailbits
+ 128;
3594 skb_shinfo(skb1
)->nr_frags
||
3595 skb_has_frag_list(skb1
)) {
3596 struct sk_buff
*skb2
;
3598 /* Fuck, we are miserable poor guys... */
3600 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
3602 skb2
= skb_copy_expand(skb1
,
3606 if (unlikely(skb2
== NULL
))
3610 skb_set_owner_w(skb2
, skb1
->sk
);
3612 /* Looking around. Are we still alive?
3613 * OK, link new skb, drop old one */
3615 skb2
->next
= skb1
->next
;
3622 skb_p
= &skb1
->next
;
3627 EXPORT_SYMBOL_GPL(skb_cow_data
);
3629 static void sock_rmem_free(struct sk_buff
*skb
)
3631 struct sock
*sk
= skb
->sk
;
3633 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
3637 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3639 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
3641 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
3642 (unsigned int)sk
->sk_rcvbuf
)
3647 skb
->destructor
= sock_rmem_free
;
3648 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
3650 /* before exiting rcu section, make sure dst is refcounted */
3653 skb_queue_tail(&sk
->sk_error_queue
, skb
);
3654 if (!sock_flag(sk
, SOCK_DEAD
))
3655 sk
->sk_data_ready(sk
);
3658 EXPORT_SYMBOL(sock_queue_err_skb
);
3660 struct sk_buff
*sock_dequeue_err_skb(struct sock
*sk
)
3662 struct sk_buff_head
*q
= &sk
->sk_error_queue
;
3663 struct sk_buff
*skb
, *skb_next
;
3664 unsigned long flags
;
3667 spin_lock_irqsave(&q
->lock
, flags
);
3668 skb
= __skb_dequeue(q
);
3669 if (skb
&& (skb_next
= skb_peek(q
)))
3670 err
= SKB_EXT_ERR(skb_next
)->ee
.ee_errno
;
3671 spin_unlock_irqrestore(&q
->lock
, flags
);
3675 sk
->sk_error_report(sk
);
3679 EXPORT_SYMBOL(sock_dequeue_err_skb
);
3682 * skb_clone_sk - create clone of skb, and take reference to socket
3683 * @skb: the skb to clone
3685 * This function creates a clone of a buffer that holds a reference on
3686 * sk_refcnt. Buffers created via this function are meant to be
3687 * returned using sock_queue_err_skb, or free via kfree_skb.
3689 * When passing buffers allocated with this function to sock_queue_err_skb
3690 * it is necessary to wrap the call with sock_hold/sock_put in order to
3691 * prevent the socket from being released prior to being enqueued on
3692 * the sk_error_queue.
3694 struct sk_buff
*skb_clone_sk(struct sk_buff
*skb
)
3696 struct sock
*sk
= skb
->sk
;
3697 struct sk_buff
*clone
;
3699 if (!sk
|| !atomic_inc_not_zero(&sk
->sk_refcnt
))
3702 clone
= skb_clone(skb
, GFP_ATOMIC
);
3709 clone
->destructor
= sock_efree
;
3713 EXPORT_SYMBOL(skb_clone_sk
);
3715 static void __skb_complete_tx_timestamp(struct sk_buff
*skb
,
3719 struct sock_exterr_skb
*serr
;
3722 serr
= SKB_EXT_ERR(skb
);
3723 memset(serr
, 0, sizeof(*serr
));
3724 serr
->ee
.ee_errno
= ENOMSG
;
3725 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
3726 serr
->ee
.ee_info
= tstype
;
3727 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
3728 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
3729 if (sk
->sk_protocol
== IPPROTO_TCP
&&
3730 sk
->sk_type
== SOCK_STREAM
)
3731 serr
->ee
.ee_data
-= sk
->sk_tskey
;
3734 err
= sock_queue_err_skb(sk
, skb
);
3740 static bool skb_may_tx_timestamp(struct sock
*sk
, bool tsonly
)
3744 if (likely(sysctl_tstamp_allow_data
|| tsonly
))
3747 read_lock_bh(&sk
->sk_callback_lock
);
3748 ret
= sk
->sk_socket
&& sk
->sk_socket
->file
&&
3749 file_ns_capable(sk
->sk_socket
->file
, &init_user_ns
, CAP_NET_RAW
);
3750 read_unlock_bh(&sk
->sk_callback_lock
);
3754 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
3755 struct skb_shared_hwtstamps
*hwtstamps
)
3757 struct sock
*sk
= skb
->sk
;
3759 if (!skb_may_tx_timestamp(sk
, false))
3762 /* take a reference to prevent skb_orphan() from freeing the socket */
3765 *skb_hwtstamps(skb
) = *hwtstamps
;
3766 __skb_complete_tx_timestamp(skb
, sk
, SCM_TSTAMP_SND
);
3770 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp
);
3772 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
3773 struct skb_shared_hwtstamps
*hwtstamps
,
3774 struct sock
*sk
, int tstype
)
3776 struct sk_buff
*skb
;
3782 tsonly
= sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TSONLY
;
3783 if (!skb_may_tx_timestamp(sk
, tsonly
))
3787 skb
= alloc_skb(0, GFP_ATOMIC
);
3789 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
3794 skb_shinfo(skb
)->tx_flags
= skb_shinfo(orig_skb
)->tx_flags
;
3795 skb_shinfo(skb
)->tskey
= skb_shinfo(orig_skb
)->tskey
;
3799 *skb_hwtstamps(skb
) = *hwtstamps
;
3801 skb
->tstamp
= ktime_get_real();
3803 __skb_complete_tx_timestamp(skb
, sk
, tstype
);
3805 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
3807 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
3808 struct skb_shared_hwtstamps
*hwtstamps
)
3810 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
3813 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
3815 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
3817 struct sock
*sk
= skb
->sk
;
3818 struct sock_exterr_skb
*serr
;
3821 skb
->wifi_acked_valid
= 1;
3822 skb
->wifi_acked
= acked
;
3824 serr
= SKB_EXT_ERR(skb
);
3825 memset(serr
, 0, sizeof(*serr
));
3826 serr
->ee
.ee_errno
= ENOMSG
;
3827 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
3829 /* take a reference to prevent skb_orphan() from freeing the socket */
3832 err
= sock_queue_err_skb(sk
, skb
);
3838 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
3841 * skb_partial_csum_set - set up and verify partial csum values for packet
3842 * @skb: the skb to set
3843 * @start: the number of bytes after skb->data to start checksumming.
3844 * @off: the offset from start to place the checksum.
3846 * For untrusted partially-checksummed packets, we need to make sure the values
3847 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3849 * This function checks and sets those values and skb->ip_summed: if this
3850 * returns false you should drop the packet.
3852 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
3854 if (unlikely(start
> skb_headlen(skb
)) ||
3855 unlikely((int)start
+ off
> skb_headlen(skb
) - 2)) {
3856 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3857 start
, off
, skb_headlen(skb
));
3860 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3861 skb
->csum_start
= skb_headroom(skb
) + start
;
3862 skb
->csum_offset
= off
;
3863 skb_set_transport_header(skb
, start
);
3866 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
3868 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
3871 if (skb_headlen(skb
) >= len
)
3874 /* If we need to pullup then pullup to the max, so we
3875 * won't need to do it again.
3880 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
3883 if (skb_headlen(skb
) < len
)
3889 #define MAX_TCP_HDR_LEN (15 * 4)
3891 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
3892 typeof(IPPROTO_IP
) proto
,
3899 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
3900 off
+ MAX_TCP_HDR_LEN
);
3901 if (!err
&& !skb_partial_csum_set(skb
, off
,
3902 offsetof(struct tcphdr
,
3905 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
3908 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
3909 off
+ sizeof(struct udphdr
));
3910 if (!err
&& !skb_partial_csum_set(skb
, off
,
3911 offsetof(struct udphdr
,
3914 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
3917 return ERR_PTR(-EPROTO
);
3920 /* This value should be large enough to cover a tagged ethernet header plus
3921 * maximally sized IP and TCP or UDP headers.
3923 #define MAX_IP_HDR_LEN 128
3925 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
3934 err
= skb_maybe_pull_tail(skb
,
3935 sizeof(struct iphdr
),
3940 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
3943 off
= ip_hdrlen(skb
);
3950 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
3952 return PTR_ERR(csum
);
3955 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
3958 ip_hdr(skb
)->protocol
, 0);
3965 /* This value should be large enough to cover a tagged ethernet header plus
3966 * an IPv6 header, all options, and a maximal TCP or UDP header.
3968 #define MAX_IPV6_HDR_LEN 256
3970 #define OPT_HDR(type, skb, off) \
3971 (type *)(skb_network_header(skb) + (off))
3973 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
3986 off
= sizeof(struct ipv6hdr
);
3988 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
3992 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
3994 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
3995 while (off
<= len
&& !done
) {
3997 case IPPROTO_DSTOPTS
:
3998 case IPPROTO_HOPOPTS
:
3999 case IPPROTO_ROUTING
: {
4000 struct ipv6_opt_hdr
*hp
;
4002 err
= skb_maybe_pull_tail(skb
,
4004 sizeof(struct ipv6_opt_hdr
),
4009 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
4010 nexthdr
= hp
->nexthdr
;
4011 off
+= ipv6_optlen(hp
);
4015 struct ip_auth_hdr
*hp
;
4017 err
= skb_maybe_pull_tail(skb
,
4019 sizeof(struct ip_auth_hdr
),
4024 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
4025 nexthdr
= hp
->nexthdr
;
4026 off
+= ipv6_authlen(hp
);
4029 case IPPROTO_FRAGMENT
: {
4030 struct frag_hdr
*hp
;
4032 err
= skb_maybe_pull_tail(skb
,
4034 sizeof(struct frag_hdr
),
4039 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
4041 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
4044 nexthdr
= hp
->nexthdr
;
4045 off
+= sizeof(struct frag_hdr
);
4056 if (!done
|| fragment
)
4059 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
4061 return PTR_ERR(csum
);
4064 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
4065 &ipv6_hdr(skb
)->daddr
,
4066 skb
->len
- off
, nexthdr
, 0);
4074 * skb_checksum_setup - set up partial checksum offset
4075 * @skb: the skb to set up
4076 * @recalculate: if true the pseudo-header checksum will be recalculated
4078 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
4082 switch (skb
->protocol
) {
4083 case htons(ETH_P_IP
):
4084 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
4087 case htons(ETH_P_IPV6
):
4088 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
4098 EXPORT_SYMBOL(skb_checksum_setup
);
4101 * skb_checksum_maybe_trim - maybe trims the given skb
4102 * @skb: the skb to check
4103 * @transport_len: the data length beyond the network header
4105 * Checks whether the given skb has data beyond the given transport length.
4106 * If so, returns a cloned skb trimmed to this transport length.
4107 * Otherwise returns the provided skb. Returns NULL in error cases
4108 * (e.g. transport_len exceeds skb length or out-of-memory).
4110 * Caller needs to set the skb transport header and free any returned skb if it
4111 * differs from the provided skb.
4113 static struct sk_buff
*skb_checksum_maybe_trim(struct sk_buff
*skb
,
4114 unsigned int transport_len
)
4116 struct sk_buff
*skb_chk
;
4117 unsigned int len
= skb_transport_offset(skb
) + transport_len
;
4122 else if (skb
->len
== len
)
4125 skb_chk
= skb_clone(skb
, GFP_ATOMIC
);
4129 ret
= pskb_trim_rcsum(skb_chk
, len
);
4139 * skb_checksum_trimmed - validate checksum of an skb
4140 * @skb: the skb to check
4141 * @transport_len: the data length beyond the network header
4142 * @skb_chkf: checksum function to use
4144 * Applies the given checksum function skb_chkf to the provided skb.
4145 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4147 * If the skb has data beyond the given transport length, then a
4148 * trimmed & cloned skb is checked and returned.
4150 * Caller needs to set the skb transport header and free any returned skb if it
4151 * differs from the provided skb.
4153 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
4154 unsigned int transport_len
,
4155 __sum16(*skb_chkf
)(struct sk_buff
*skb
))
4157 struct sk_buff
*skb_chk
;
4158 unsigned int offset
= skb_transport_offset(skb
);
4161 skb_chk
= skb_checksum_maybe_trim(skb
, transport_len
);
4165 if (!pskb_may_pull(skb_chk
, offset
))
4168 __skb_pull(skb_chk
, offset
);
4169 ret
= skb_chkf(skb_chk
);
4170 __skb_push(skb_chk
, offset
);
4178 if (skb_chk
&& skb_chk
!= skb
)
4184 EXPORT_SYMBOL(skb_checksum_trimmed
);
4186 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
4188 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4191 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
4193 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
4196 skb_release_head_state(skb
);
4197 kmem_cache_free(skbuff_head_cache
, skb
);
4202 EXPORT_SYMBOL(kfree_skb_partial
);
4205 * skb_try_coalesce - try to merge skb to prior one
4207 * @from: buffer to add
4208 * @fragstolen: pointer to boolean
4209 * @delta_truesize: how much more was allocated than was requested
4211 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
4212 bool *fragstolen
, int *delta_truesize
)
4214 int i
, delta
, len
= from
->len
;
4216 *fragstolen
= false;
4221 if (len
<= skb_tailroom(to
)) {
4223 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
4224 *delta_truesize
= 0;
4228 if (skb_has_frag_list(to
) || skb_has_frag_list(from
))
4231 if (skb_headlen(from
) != 0) {
4233 unsigned int offset
;
4235 if (skb_shinfo(to
)->nr_frags
+
4236 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
4239 if (skb_head_is_locked(from
))
4242 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
4244 page
= virt_to_head_page(from
->head
);
4245 offset
= from
->data
- (unsigned char *)page_address(page
);
4247 skb_fill_page_desc(to
, skb_shinfo(to
)->nr_frags
,
4248 page
, offset
, skb_headlen(from
));
4251 if (skb_shinfo(to
)->nr_frags
+
4252 skb_shinfo(from
)->nr_frags
> MAX_SKB_FRAGS
)
4255 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
4258 WARN_ON_ONCE(delta
< len
);
4260 memcpy(skb_shinfo(to
)->frags
+ skb_shinfo(to
)->nr_frags
,
4261 skb_shinfo(from
)->frags
,
4262 skb_shinfo(from
)->nr_frags
* sizeof(skb_frag_t
));
4263 skb_shinfo(to
)->nr_frags
+= skb_shinfo(from
)->nr_frags
;
4265 if (!skb_cloned(from
))
4266 skb_shinfo(from
)->nr_frags
= 0;
4268 /* if the skb is not cloned this does nothing
4269 * since we set nr_frags to 0.
4271 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++)
4272 skb_frag_ref(from
, i
);
4274 to
->truesize
+= delta
;
4276 to
->data_len
+= len
;
4278 *delta_truesize
= delta
;
4281 EXPORT_SYMBOL(skb_try_coalesce
);
4284 * skb_scrub_packet - scrub an skb
4286 * @skb: buffer to clean
4287 * @xnet: packet is crossing netns
4289 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4290 * into/from a tunnel. Some information have to be cleared during these
4292 * skb_scrub_packet can also be used to clean a skb before injecting it in
4293 * another namespace (@xnet == true). We have to clear all information in the
4294 * skb that could impact namespace isolation.
4296 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
4298 skb
->tstamp
.tv64
= 0;
4299 skb
->pkt_type
= PACKET_HOST
;
4303 skb_sender_cpu_clear(skb
);
4306 nf_reset_trace(skb
);
4314 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
4317 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4321 * skb_gso_transport_seglen is used to determine the real size of the
4322 * individual segments, including Layer4 headers (TCP/UDP).
4324 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4326 unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
4328 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
4329 unsigned int thlen
= 0;
4331 if (skb
->encapsulation
) {
4332 thlen
= skb_inner_transport_header(skb
) -
4333 skb_transport_header(skb
);
4335 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
4336 thlen
+= inner_tcp_hdrlen(skb
);
4337 } else if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
4338 thlen
= tcp_hdrlen(skb
);
4340 /* UFO sets gso_size to the size of the fragmentation
4341 * payload, i.e. the size of the L4 (UDP) header is already
4344 return thlen
+ shinfo
->gso_size
;
4346 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen
);
4348 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
4350 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
4355 memmove(skb
->data
- ETH_HLEN
, skb
->data
- skb
->mac_len
- VLAN_HLEN
,
4357 skb
->mac_header
+= VLAN_HLEN
;
4361 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
4363 struct vlan_hdr
*vhdr
;
4366 if (unlikely(skb_vlan_tag_present(skb
))) {
4367 /* vlan_tci is already set-up so leave this for another time */
4371 skb
= skb_share_check(skb
, GFP_ATOMIC
);
4375 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
4378 vhdr
= (struct vlan_hdr
*)skb
->data
;
4379 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
4380 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
4382 skb_pull_rcsum(skb
, VLAN_HLEN
);
4383 vlan_set_encap_proto(skb
, vhdr
);
4385 skb
= skb_reorder_vlan_header(skb
);
4389 skb_reset_network_header(skb
);
4390 skb_reset_transport_header(skb
);
4391 skb_reset_mac_len(skb
);
4399 EXPORT_SYMBOL(skb_vlan_untag
);
4401 int skb_ensure_writable(struct sk_buff
*skb
, int write_len
)
4403 if (!pskb_may_pull(skb
, write_len
))
4406 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
4409 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
4411 EXPORT_SYMBOL(skb_ensure_writable
);
4413 /* remove VLAN header from packet and update csum accordingly. */
4414 static int __skb_vlan_pop(struct sk_buff
*skb
, u16
*vlan_tci
)
4416 struct vlan_hdr
*vhdr
;
4417 unsigned int offset
= skb
->data
- skb_mac_header(skb
);
4420 __skb_push(skb
, offset
);
4421 err
= skb_ensure_writable(skb
, VLAN_ETH_HLEN
);
4425 skb_postpull_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
4427 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
4428 *vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
4430 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
4431 __skb_pull(skb
, VLAN_HLEN
);
4433 vlan_set_encap_proto(skb
, vhdr
);
4434 skb
->mac_header
+= VLAN_HLEN
;
4436 if (skb_network_offset(skb
) < ETH_HLEN
)
4437 skb_set_network_header(skb
, ETH_HLEN
);
4439 skb_reset_mac_len(skb
);
4441 __skb_pull(skb
, offset
);
4446 int skb_vlan_pop(struct sk_buff
*skb
)
4452 if (likely(skb_vlan_tag_present(skb
))) {
4455 if (unlikely((skb
->protocol
!= htons(ETH_P_8021Q
) &&
4456 skb
->protocol
!= htons(ETH_P_8021AD
)) ||
4457 skb
->len
< VLAN_ETH_HLEN
))
4460 err
= __skb_vlan_pop(skb
, &vlan_tci
);
4464 /* move next vlan tag to hw accel tag */
4465 if (likely((skb
->protocol
!= htons(ETH_P_8021Q
) &&
4466 skb
->protocol
!= htons(ETH_P_8021AD
)) ||
4467 skb
->len
< VLAN_ETH_HLEN
))
4470 vlan_proto
= skb
->protocol
;
4471 err
= __skb_vlan_pop(skb
, &vlan_tci
);
4475 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
4478 EXPORT_SYMBOL(skb_vlan_pop
);
4480 int skb_vlan_push(struct sk_buff
*skb
, __be16 vlan_proto
, u16 vlan_tci
)
4482 if (skb_vlan_tag_present(skb
)) {
4483 unsigned int offset
= skb
->data
- skb_mac_header(skb
);
4486 /* __vlan_insert_tag expect skb->data pointing to mac header.
4487 * So change skb->data before calling it and change back to
4488 * original position later
4490 __skb_push(skb
, offset
);
4491 err
= __vlan_insert_tag(skb
, skb
->vlan_proto
,
4492 skb_vlan_tag_get(skb
));
4495 skb
->protocol
= skb
->vlan_proto
;
4496 skb
->mac_len
+= VLAN_HLEN
;
4497 __skb_pull(skb
, offset
);
4499 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
4500 skb
->csum
= csum_add(skb
->csum
, csum_partial(skb
->data
4501 + (2 * ETH_ALEN
), VLAN_HLEN
, 0));
4503 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
4506 EXPORT_SYMBOL(skb_vlan_push
);
4509 * alloc_skb_with_frags - allocate skb with page frags
4511 * @header_len: size of linear part
4512 * @data_len: needed length in frags
4513 * @max_page_order: max page order desired.
4514 * @errcode: pointer to error code if any
4515 * @gfp_mask: allocation mask
4517 * This can be used to allocate a paged skb, given a maximal order for frags.
4519 struct sk_buff
*alloc_skb_with_frags(unsigned long header_len
,
4520 unsigned long data_len
,
4525 int npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
4526 unsigned long chunk
;
4527 struct sk_buff
*skb
;
4532 *errcode
= -EMSGSIZE
;
4533 /* Note this test could be relaxed, if we succeed to allocate
4534 * high order pages...
4536 if (npages
> MAX_SKB_FRAGS
)
4539 gfp_head
= gfp_mask
;
4540 if (gfp_head
& __GFP_DIRECT_RECLAIM
)
4541 gfp_head
|= __GFP_REPEAT
;
4543 *errcode
= -ENOBUFS
;
4544 skb
= alloc_skb(header_len
, gfp_head
);
4548 skb
->truesize
+= npages
<< PAGE_SHIFT
;
4550 for (i
= 0; npages
> 0; i
++) {
4551 int order
= max_page_order
;
4554 if (npages
>= 1 << order
) {
4555 page
= alloc_pages((gfp_mask
& ~__GFP_DIRECT_RECLAIM
) |
4562 /* Do not retry other high order allocations */
4568 page
= alloc_page(gfp_mask
);
4572 chunk
= min_t(unsigned long, data_len
,
4573 PAGE_SIZE
<< order
);
4574 skb_fill_page_desc(skb
, i
, page
, 0, chunk
);
4576 npages
-= 1 << order
;
4584 EXPORT_SYMBOL(alloc_skb_with_frags
);