1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines having to do with the 'struct sk_buff' memory handlers.
5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
6 * Florian La Roche <rzsfl@rz.uni-sb.de>
9 * Alan Cox : Fixed the worst of the load
11 * Dave Platt : Interrupt stacking fix.
12 * Richard Kooijman : Timestamp fixes.
13 * Alan Cox : Changed buffer format.
14 * Alan Cox : destructor hook for AF_UNIX etc.
15 * Linus Torvalds : Better skb_clone.
16 * Alan Cox : Added skb_copy.
17 * Alan Cox : Added all the changed routines Linus
18 * only put in the headers
19 * Ray VanTassle : Fixed --skb->lock in free
20 * Alan Cox : skb_copy copy arp field
21 * Andi Kleen : slabified it.
22 * Robert Olsson : Removed skb_head_pool
25 * The __skb_ routines should be called with interrupts
26 * disabled, or you better be *real* sure that the operation is atomic
27 * with respect to whatever list is being frobbed (e.g. via lock_sock()
28 * or via disabling bottom half handlers, etc).
32 * The functions in this file will not compile correctly with gcc 2.4.x
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
41 #include <linux/interrupt.h>
43 #include <linux/inet.h>
44 #include <linux/slab.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 #include <linux/sctp.h>
48 #include <linux/netdevice.h>
49 #ifdef CONFIG_NET_CLS_ACT
50 #include <net/pkt_sched.h>
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/splice.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
59 #include <linux/errqueue.h>
60 #include <linux/prefetch.h>
61 #include <linux/if_vlan.h>
63 #include <net/protocol.h>
66 #include <net/checksum.h>
67 #include <net/ip6_checksum.h>
70 #include <linux/uaccess.h>
71 #include <trace/events/skb.h>
72 #include <linux/highmem.h>
73 #include <linux/capability.h>
74 #include <linux/user_namespace.h>
78 struct kmem_cache
*skbuff_head_cache __ro_after_init
;
79 static struct kmem_cache
*skbuff_fclone_cache __ro_after_init
;
80 #ifdef CONFIG_SKB_EXTENSIONS
81 static struct kmem_cache
*skbuff_ext_cache __ro_after_init
;
83 int sysctl_max_skb_frags __read_mostly
= MAX_SKB_FRAGS
;
84 EXPORT_SYMBOL(sysctl_max_skb_frags
);
87 * skb_panic - private function for out-of-line support
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
,
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg
, addr
, skb
->len
, sz
, skb
->head
, skb
->data
,
103 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
104 skb
->dev
? skb
->dev
->name
: "<NULL>");
108 static void skb_over_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
110 skb_panic(skb
, sz
, addr
, __func__
);
113 static void skb_under_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
115 skb_panic(skb
, sz
, addr
, __func__
);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
,
129 unsigned long ip
, bool *pfmemalloc
)
132 bool ret_pfmemalloc
= false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj
= kmalloc_node_track_caller(size
,
139 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
141 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc
= true;
146 obj
= kmalloc_node_track_caller(size
, flags
, node
);
150 *pfmemalloc
= ret_pfmemalloc
;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
162 * __alloc_skb - allocate a network buffer
163 * @size: size to allocate
164 * @gfp_mask: allocation mask
165 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
166 * instead of head cache and allocate a cloned (child) skb.
167 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
168 * allocations in case the data is required for writeback
169 * @node: numa node to allocate memory on
171 * Allocate a new &sk_buff. The returned buffer has no headroom and a
172 * tail room of at least size bytes. The object has a reference count
173 * of one. The return is the buffer. On a failure the return is %NULL.
175 * Buffers may only be allocated from interrupts using a @gfp_mask of
178 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
181 struct kmem_cache
*cache
;
182 struct skb_shared_info
*shinfo
;
187 cache
= (flags
& SKB_ALLOC_FCLONE
)
188 ? skbuff_fclone_cache
: skbuff_head_cache
;
190 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
191 gfp_mask
|= __GFP_MEMALLOC
;
194 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
199 /* We do our best to align skb_shared_info on a separate cache
200 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
201 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
202 * Both skb->head and skb_shared_info are cache line aligned.
204 size
= SKB_DATA_ALIGN(size
);
205 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
206 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
209 /* kmalloc(size) might give us more room than requested.
210 * Put skb_shared_info exactly at the end of allocated zone,
211 * to allow max possible filling before reallocation.
213 size
= SKB_WITH_OVERHEAD(ksize(data
));
214 prefetchw(data
+ size
);
217 * Only clear those fields we need to clear, not those that we will
218 * actually initialise below. Hence, don't put any more fields after
219 * the tail pointer in struct sk_buff!
221 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
222 /* Account for allocated memory : skb + skb->head */
223 skb
->truesize
= SKB_TRUESIZE(size
);
224 skb
->pfmemalloc
= pfmemalloc
;
225 refcount_set(&skb
->users
, 1);
228 skb_reset_tail_pointer(skb
);
229 skb
->end
= skb
->tail
+ size
;
230 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
231 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
233 /* make sure we initialize shinfo sequentially */
234 shinfo
= skb_shinfo(skb
);
235 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
236 atomic_set(&shinfo
->dataref
, 1);
238 if (flags
& SKB_ALLOC_FCLONE
) {
239 struct sk_buff_fclones
*fclones
;
241 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
243 skb
->fclone
= SKB_FCLONE_ORIG
;
244 refcount_set(&fclones
->fclone_ref
, 1);
246 fclones
->skb2
.fclone
= SKB_FCLONE_CLONE
;
251 kmem_cache_free(cache
, skb
);
255 EXPORT_SYMBOL(__alloc_skb
);
257 /* Caller must provide SKB that is memset cleared */
258 static struct sk_buff
*__build_skb_around(struct sk_buff
*skb
,
259 void *data
, unsigned int frag_size
)
261 struct skb_shared_info
*shinfo
;
262 unsigned int size
= frag_size
? : ksize(data
);
264 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
266 /* Assumes caller memset cleared SKB */
267 skb
->truesize
= SKB_TRUESIZE(size
);
268 refcount_set(&skb
->users
, 1);
271 skb_reset_tail_pointer(skb
);
272 skb
->end
= skb
->tail
+ size
;
273 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
274 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
276 /* make sure we initialize shinfo sequentially */
277 shinfo
= skb_shinfo(skb
);
278 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
279 atomic_set(&shinfo
->dataref
, 1);
285 * __build_skb - build a network buffer
286 * @data: data buffer provided by caller
287 * @frag_size: size of data, or 0 if head was kmalloced
289 * Allocate a new &sk_buff. Caller provides space holding head and
290 * skb_shared_info. @data must have been allocated by kmalloc() only if
291 * @frag_size is 0, otherwise data should come from the page allocator
293 * The return is the new skb buffer.
294 * On a failure the return is %NULL, and @data is not freed.
296 * Before IO, driver allocates only data buffer where NIC put incoming frame
297 * Driver should add room at head (NET_SKB_PAD) and
298 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
300 * before giving packet to stack.
301 * RX rings only contains data buffers, not full skbs.
303 struct sk_buff
*__build_skb(void *data
, unsigned int frag_size
)
307 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
311 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
313 return __build_skb_around(skb
, data
, frag_size
);
316 /* build_skb() is wrapper over __build_skb(), that specifically
317 * takes care of skb->head and skb->pfmemalloc
318 * This means that if @frag_size is not zero, then @data must be backed
319 * by a page fragment, not kmalloc() or vmalloc()
321 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
323 struct sk_buff
*skb
= __build_skb(data
, frag_size
);
325 if (skb
&& frag_size
) {
327 if (page_is_pfmemalloc(virt_to_head_page(data
)))
332 EXPORT_SYMBOL(build_skb
);
335 * build_skb_around - build a network buffer around provided skb
336 * @skb: sk_buff provide by caller, must be memset cleared
337 * @data: data buffer provided by caller
338 * @frag_size: size of data, or 0 if head was kmalloced
340 struct sk_buff
*build_skb_around(struct sk_buff
*skb
,
341 void *data
, unsigned int frag_size
)
346 skb
= __build_skb_around(skb
, data
, frag_size
);
348 if (skb
&& frag_size
) {
350 if (page_is_pfmemalloc(virt_to_head_page(data
)))
355 EXPORT_SYMBOL(build_skb_around
);
357 #define NAPI_SKB_CACHE_SIZE 64
359 struct napi_alloc_cache
{
360 struct page_frag_cache page
;
361 unsigned int skb_count
;
362 void *skb_cache
[NAPI_SKB_CACHE_SIZE
];
365 static DEFINE_PER_CPU(struct page_frag_cache
, netdev_alloc_cache
);
366 static DEFINE_PER_CPU(struct napi_alloc_cache
, napi_alloc_cache
);
368 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
370 struct page_frag_cache
*nc
;
374 local_irq_save(flags
);
375 nc
= this_cpu_ptr(&netdev_alloc_cache
);
376 data
= page_frag_alloc(nc
, fragsz
, gfp_mask
);
377 local_irq_restore(flags
);
382 * netdev_alloc_frag - allocate a page fragment
383 * @fragsz: fragment size
385 * Allocates a frag from a page for receive buffer.
386 * Uses GFP_ATOMIC allocations.
388 void *netdev_alloc_frag(unsigned int fragsz
)
390 fragsz
= SKB_DATA_ALIGN(fragsz
);
392 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
);
394 EXPORT_SYMBOL(netdev_alloc_frag
);
396 static void *__napi_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
398 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
400 return page_frag_alloc(&nc
->page
, fragsz
, gfp_mask
);
403 void *napi_alloc_frag(unsigned int fragsz
)
405 fragsz
= SKB_DATA_ALIGN(fragsz
);
407 return __napi_alloc_frag(fragsz
, GFP_ATOMIC
);
409 EXPORT_SYMBOL(napi_alloc_frag
);
412 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
413 * @dev: network device to receive on
414 * @len: length to allocate
415 * @gfp_mask: get_free_pages mask, passed to alloc_skb
417 * Allocate a new &sk_buff and assign it a usage count of one. The
418 * buffer has NET_SKB_PAD headroom built in. Users should allocate
419 * the headroom they think they need without accounting for the
420 * built in space. The built in space is used for optimisations.
422 * %NULL is returned if there is no free memory.
424 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
, unsigned int len
,
427 struct page_frag_cache
*nc
;
435 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
436 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
437 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
443 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
444 len
= SKB_DATA_ALIGN(len
);
446 if (sk_memalloc_socks())
447 gfp_mask
|= __GFP_MEMALLOC
;
449 local_irq_save(flags
);
451 nc
= this_cpu_ptr(&netdev_alloc_cache
);
452 data
= page_frag_alloc(nc
, len
, gfp_mask
);
453 pfmemalloc
= nc
->pfmemalloc
;
455 local_irq_restore(flags
);
460 skb
= __build_skb(data
, len
);
461 if (unlikely(!skb
)) {
466 /* use OR instead of assignment to avoid clearing of bits in mask */
472 skb_reserve(skb
, NET_SKB_PAD
);
478 EXPORT_SYMBOL(__netdev_alloc_skb
);
481 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
482 * @napi: napi instance this buffer was allocated for
483 * @len: length to allocate
484 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
486 * Allocate a new sk_buff for use in NAPI receive. This buffer will
487 * attempt to allocate the head from a special reserved region used
488 * only for NAPI Rx allocation. By doing this we can save several
489 * CPU cycles by avoiding having to disable and re-enable IRQs.
491 * %NULL is returned if there is no free memory.
493 struct sk_buff
*__napi_alloc_skb(struct napi_struct
*napi
, unsigned int len
,
496 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
500 len
+= NET_SKB_PAD
+ NET_IP_ALIGN
;
502 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
503 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
504 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
510 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
511 len
= SKB_DATA_ALIGN(len
);
513 if (sk_memalloc_socks())
514 gfp_mask
|= __GFP_MEMALLOC
;
516 data
= page_frag_alloc(&nc
->page
, len
, gfp_mask
);
520 skb
= __build_skb(data
, len
);
521 if (unlikely(!skb
)) {
526 /* use OR instead of assignment to avoid clearing of bits in mask */
527 if (nc
->page
.pfmemalloc
)
532 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
);
533 skb
->dev
= napi
->dev
;
538 EXPORT_SYMBOL(__napi_alloc_skb
);
540 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
541 int size
, unsigned int truesize
)
543 skb_fill_page_desc(skb
, i
, page
, off
, size
);
545 skb
->data_len
+= size
;
546 skb
->truesize
+= truesize
;
548 EXPORT_SYMBOL(skb_add_rx_frag
);
550 void skb_coalesce_rx_frag(struct sk_buff
*skb
, int i
, int size
,
551 unsigned int truesize
)
553 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
555 skb_frag_size_add(frag
, size
);
557 skb
->data_len
+= size
;
558 skb
->truesize
+= truesize
;
560 EXPORT_SYMBOL(skb_coalesce_rx_frag
);
562 static void skb_drop_list(struct sk_buff
**listp
)
564 kfree_skb_list(*listp
);
568 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
570 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
573 static void skb_clone_fraglist(struct sk_buff
*skb
)
575 struct sk_buff
*list
;
577 skb_walk_frags(skb
, list
)
581 static void skb_free_head(struct sk_buff
*skb
)
583 unsigned char *head
= skb
->head
;
591 static void skb_release_data(struct sk_buff
*skb
)
593 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
597 atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
601 for (i
= 0; i
< shinfo
->nr_frags
; i
++)
602 __skb_frag_unref(&shinfo
->frags
[i
]);
604 if (shinfo
->frag_list
)
605 kfree_skb_list(shinfo
->frag_list
);
607 skb_zcopy_clear(skb
, true);
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 (refcount_read(&fclones
->fclone_ref
) == 1)
634 default: /* SKB_FCLONE_CLONE */
635 fclones
= container_of(skb
, struct sk_buff_fclones
, skb2
);
638 if (!refcount_dec_and_test(&fclones
->fclone_ref
))
641 kmem_cache_free(skbuff_fclone_cache
, fclones
);
644 void skb_release_head_state(struct sk_buff
*skb
)
647 if (skb
->destructor
) {
649 skb
->destructor(skb
);
651 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
652 nf_conntrack_put(skb_nfct(skb
));
657 /* Free everything but the sk_buff shell. */
658 static void skb_release_all(struct sk_buff
*skb
)
660 skb_release_head_state(skb
);
661 if (likely(skb
->head
))
662 skb_release_data(skb
);
666 * __kfree_skb - private function
669 * Free an sk_buff. Release anything attached to the buffer.
670 * Clean the state. This is an internal helper function. Users should
671 * always call kfree_skb
674 void __kfree_skb(struct sk_buff
*skb
)
676 skb_release_all(skb
);
679 EXPORT_SYMBOL(__kfree_skb
);
682 * kfree_skb - free an sk_buff
683 * @skb: buffer to free
685 * Drop a reference to the buffer and free it if the usage count has
688 void kfree_skb(struct sk_buff
*skb
)
693 trace_kfree_skb(skb
, __builtin_return_address(0));
696 EXPORT_SYMBOL(kfree_skb
);
698 void kfree_skb_list(struct sk_buff
*segs
)
701 struct sk_buff
*next
= segs
->next
;
707 EXPORT_SYMBOL(kfree_skb_list
);
710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
716 void skb_tx_error(struct sk_buff
*skb
)
718 skb_zcopy_clear(skb
, true);
720 EXPORT_SYMBOL(skb_tx_error
);
723 * consume_skb - free an skbuff
724 * @skb: buffer to free
726 * Drop a ref to the buffer and free it if the usage count has hit zero
727 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
728 * is being dropped after a failure and notes that
730 void consume_skb(struct sk_buff
*skb
)
735 trace_consume_skb(skb
);
738 EXPORT_SYMBOL(consume_skb
);
741 * consume_stateless_skb - free an skbuff, assuming it is stateless
742 * @skb: buffer to free
744 * Alike consume_skb(), but this variant assumes that this is the last
745 * skb reference and all the head states have been already dropped
747 void __consume_stateless_skb(struct sk_buff
*skb
)
749 trace_consume_skb(skb
);
750 skb_release_data(skb
);
754 void __kfree_skb_flush(void)
756 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
758 /* flush skb_cache if containing objects */
760 kmem_cache_free_bulk(skbuff_head_cache
, nc
->skb_count
,
766 static inline void _kfree_skb_defer(struct sk_buff
*skb
)
768 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
770 /* drop skb->head and call any destructors for packet */
771 skb_release_all(skb
);
773 /* record skb to CPU local list */
774 nc
->skb_cache
[nc
->skb_count
++] = skb
;
777 /* SLUB writes into objects when freeing */
781 /* flush skb_cache if it is filled */
782 if (unlikely(nc
->skb_count
== NAPI_SKB_CACHE_SIZE
)) {
783 kmem_cache_free_bulk(skbuff_head_cache
, NAPI_SKB_CACHE_SIZE
,
788 void __kfree_skb_defer(struct sk_buff
*skb
)
790 _kfree_skb_defer(skb
);
793 void napi_consume_skb(struct sk_buff
*skb
, int budget
)
798 /* Zero budget indicate non-NAPI context called us, like netpoll */
799 if (unlikely(!budget
)) {
800 dev_consume_skb_any(skb
);
807 /* if reaching here SKB is ready to free */
808 trace_consume_skb(skb
);
810 /* if SKB is a clone, don't handle this case */
811 if (skb
->fclone
!= SKB_FCLONE_UNAVAILABLE
) {
816 _kfree_skb_defer(skb
);
818 EXPORT_SYMBOL(napi_consume_skb
);
820 /* Make sure a field is enclosed inside headers_start/headers_end section */
821 #define CHECK_SKB_FIELD(field) \
822 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
823 offsetof(struct sk_buff, headers_start)); \
824 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
825 offsetof(struct sk_buff, headers_end)); \
827 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
829 new->tstamp
= old
->tstamp
;
830 /* We do not copy old->sk */
832 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
833 skb_dst_copy(new, old
);
834 __skb_ext_copy(new, old
);
835 __nf_copy(new, old
, false);
837 /* Note : this field could be in headers_start/headers_end section
838 * It is not yet because we do not want to have a 16 bit hole
840 new->queue_mapping
= old
->queue_mapping
;
842 memcpy(&new->headers_start
, &old
->headers_start
,
843 offsetof(struct sk_buff
, headers_end
) -
844 offsetof(struct sk_buff
, headers_start
));
845 CHECK_SKB_FIELD(protocol
);
846 CHECK_SKB_FIELD(csum
);
847 CHECK_SKB_FIELD(hash
);
848 CHECK_SKB_FIELD(priority
);
849 CHECK_SKB_FIELD(skb_iif
);
850 CHECK_SKB_FIELD(vlan_proto
);
851 CHECK_SKB_FIELD(vlan_tci
);
852 CHECK_SKB_FIELD(transport_header
);
853 CHECK_SKB_FIELD(network_header
);
854 CHECK_SKB_FIELD(mac_header
);
855 CHECK_SKB_FIELD(inner_protocol
);
856 CHECK_SKB_FIELD(inner_transport_header
);
857 CHECK_SKB_FIELD(inner_network_header
);
858 CHECK_SKB_FIELD(inner_mac_header
);
859 CHECK_SKB_FIELD(mark
);
860 #ifdef CONFIG_NETWORK_SECMARK
861 CHECK_SKB_FIELD(secmark
);
863 #ifdef CONFIG_NET_RX_BUSY_POLL
864 CHECK_SKB_FIELD(napi_id
);
867 CHECK_SKB_FIELD(sender_cpu
);
869 #ifdef CONFIG_NET_SCHED
870 CHECK_SKB_FIELD(tc_index
);
876 * You should not add any new code to this function. Add it to
877 * __copy_skb_header above instead.
879 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
881 #define C(x) n->x = skb->x
883 n
->next
= n
->prev
= NULL
;
885 __copy_skb_header(n
, skb
);
890 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
895 n
->destructor
= NULL
;
902 refcount_set(&n
->users
, 1);
904 atomic_inc(&(skb_shinfo(skb
)->dataref
));
912 * skb_morph - morph one skb into another
913 * @dst: the skb to receive the contents
914 * @src: the skb to supply the contents
916 * This is identical to skb_clone except that the target skb is
917 * supplied by the user.
919 * The target skb is returned upon exit.
921 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
923 skb_release_all(dst
);
924 return __skb_clone(dst
, src
);
926 EXPORT_SYMBOL_GPL(skb_morph
);
928 int mm_account_pinned_pages(struct mmpin
*mmp
, size_t size
)
930 unsigned long max_pg
, num_pg
, new_pg
, old_pg
;
931 struct user_struct
*user
;
933 if (capable(CAP_IPC_LOCK
) || !size
)
936 num_pg
= (size
>> PAGE_SHIFT
) + 2; /* worst case */
937 max_pg
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
938 user
= mmp
->user
? : current_user();
941 old_pg
= atomic_long_read(&user
->locked_vm
);
942 new_pg
= old_pg
+ num_pg
;
945 } while (atomic_long_cmpxchg(&user
->locked_vm
, old_pg
, new_pg
) !=
949 mmp
->user
= get_uid(user
);
950 mmp
->num_pg
= num_pg
;
952 mmp
->num_pg
+= num_pg
;
957 EXPORT_SYMBOL_GPL(mm_account_pinned_pages
);
959 void mm_unaccount_pinned_pages(struct mmpin
*mmp
)
962 atomic_long_sub(mmp
->num_pg
, &mmp
->user
->locked_vm
);
966 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages
);
968 struct ubuf_info
*sock_zerocopy_alloc(struct sock
*sk
, size_t size
)
970 struct ubuf_info
*uarg
;
973 WARN_ON_ONCE(!in_task());
975 skb
= sock_omalloc(sk
, 0, GFP_KERNEL
);
979 BUILD_BUG_ON(sizeof(*uarg
) > sizeof(skb
->cb
));
980 uarg
= (void *)skb
->cb
;
981 uarg
->mmp
.user
= NULL
;
983 if (mm_account_pinned_pages(&uarg
->mmp
, size
)) {
988 uarg
->callback
= sock_zerocopy_callback
;
989 uarg
->id
= ((u32
)atomic_inc_return(&sk
->sk_zckey
)) - 1;
991 uarg
->bytelen
= size
;
993 refcount_set(&uarg
->refcnt
, 1);
998 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc
);
1000 static inline struct sk_buff
*skb_from_uarg(struct ubuf_info
*uarg
)
1002 return container_of((void *)uarg
, struct sk_buff
, cb
);
1005 struct ubuf_info
*sock_zerocopy_realloc(struct sock
*sk
, size_t size
,
1006 struct ubuf_info
*uarg
)
1009 const u32 byte_limit
= 1 << 19; /* limit to a few TSO */
1012 /* realloc only when socket is locked (TCP, UDP cork),
1013 * so uarg->len and sk_zckey access is serialized
1015 if (!sock_owned_by_user(sk
)) {
1020 bytelen
= uarg
->bytelen
+ size
;
1021 if (uarg
->len
== USHRT_MAX
- 1 || bytelen
> byte_limit
) {
1022 /* TCP can create new skb to attach new uarg */
1023 if (sk
->sk_type
== SOCK_STREAM
)
1028 next
= (u32
)atomic_read(&sk
->sk_zckey
);
1029 if ((u32
)(uarg
->id
+ uarg
->len
) == next
) {
1030 if (mm_account_pinned_pages(&uarg
->mmp
, size
))
1033 uarg
->bytelen
= bytelen
;
1034 atomic_set(&sk
->sk_zckey
, ++next
);
1036 /* no extra ref when appending to datagram (MSG_MORE) */
1037 if (sk
->sk_type
== SOCK_STREAM
)
1038 sock_zerocopy_get(uarg
);
1045 return sock_zerocopy_alloc(sk
, size
);
1047 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc
);
1049 static bool skb_zerocopy_notify_extend(struct sk_buff
*skb
, u32 lo
, u16 len
)
1051 struct sock_exterr_skb
*serr
= SKB_EXT_ERR(skb
);
1055 old_lo
= serr
->ee
.ee_info
;
1056 old_hi
= serr
->ee
.ee_data
;
1057 sum_len
= old_hi
- old_lo
+ 1ULL + len
;
1059 if (sum_len
>= (1ULL << 32))
1062 if (lo
!= old_hi
+ 1)
1065 serr
->ee
.ee_data
+= len
;
1069 void sock_zerocopy_callback(struct ubuf_info
*uarg
, bool success
)
1071 struct sk_buff
*tail
, *skb
= skb_from_uarg(uarg
);
1072 struct sock_exterr_skb
*serr
;
1073 struct sock
*sk
= skb
->sk
;
1074 struct sk_buff_head
*q
;
1075 unsigned long flags
;
1079 mm_unaccount_pinned_pages(&uarg
->mmp
);
1081 /* if !len, there was only 1 call, and it was aborted
1082 * so do not queue a completion notification
1084 if (!uarg
->len
|| sock_flag(sk
, SOCK_DEAD
))
1089 hi
= uarg
->id
+ len
- 1;
1091 serr
= SKB_EXT_ERR(skb
);
1092 memset(serr
, 0, sizeof(*serr
));
1093 serr
->ee
.ee_errno
= 0;
1094 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ZEROCOPY
;
1095 serr
->ee
.ee_data
= hi
;
1096 serr
->ee
.ee_info
= lo
;
1098 serr
->ee
.ee_code
|= SO_EE_CODE_ZEROCOPY_COPIED
;
1100 q
= &sk
->sk_error_queue
;
1101 spin_lock_irqsave(&q
->lock
, flags
);
1102 tail
= skb_peek_tail(q
);
1103 if (!tail
|| SKB_EXT_ERR(tail
)->ee
.ee_origin
!= SO_EE_ORIGIN_ZEROCOPY
||
1104 !skb_zerocopy_notify_extend(tail
, lo
, len
)) {
1105 __skb_queue_tail(q
, skb
);
1108 spin_unlock_irqrestore(&q
->lock
, flags
);
1110 sk
->sk_error_report(sk
);
1116 EXPORT_SYMBOL_GPL(sock_zerocopy_callback
);
1118 void sock_zerocopy_put(struct ubuf_info
*uarg
)
1120 if (uarg
&& refcount_dec_and_test(&uarg
->refcnt
)) {
1122 uarg
->callback(uarg
, uarg
->zerocopy
);
1124 consume_skb(skb_from_uarg(uarg
));
1127 EXPORT_SYMBOL_GPL(sock_zerocopy_put
);
1129 void sock_zerocopy_put_abort(struct ubuf_info
*uarg
, bool have_uref
)
1132 struct sock
*sk
= skb_from_uarg(uarg
)->sk
;
1134 atomic_dec(&sk
->sk_zckey
);
1138 sock_zerocopy_put(uarg
);
1141 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort
);
1143 int skb_zerocopy_iter_dgram(struct sk_buff
*skb
, struct msghdr
*msg
, int len
)
1145 return __zerocopy_sg_from_iter(skb
->sk
, skb
, &msg
->msg_iter
, len
);
1147 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram
);
1149 int skb_zerocopy_iter_stream(struct sock
*sk
, struct sk_buff
*skb
,
1150 struct msghdr
*msg
, int len
,
1151 struct ubuf_info
*uarg
)
1153 struct ubuf_info
*orig_uarg
= skb_zcopy(skb
);
1154 struct iov_iter orig_iter
= msg
->msg_iter
;
1155 int err
, orig_len
= skb
->len
;
1157 /* An skb can only point to one uarg. This edge case happens when
1158 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1160 if (orig_uarg
&& uarg
!= orig_uarg
)
1163 err
= __zerocopy_sg_from_iter(sk
, skb
, &msg
->msg_iter
, len
);
1164 if (err
== -EFAULT
|| (err
== -EMSGSIZE
&& skb
->len
== orig_len
)) {
1165 struct sock
*save_sk
= skb
->sk
;
1167 /* Streams do not free skb on error. Reset to prev state. */
1168 msg
->msg_iter
= orig_iter
;
1170 ___pskb_trim(skb
, orig_len
);
1175 skb_zcopy_set(skb
, uarg
, NULL
);
1176 return skb
->len
- orig_len
;
1178 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream
);
1180 static int skb_zerocopy_clone(struct sk_buff
*nskb
, struct sk_buff
*orig
,
1183 if (skb_zcopy(orig
)) {
1184 if (skb_zcopy(nskb
)) {
1185 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1190 if (skb_uarg(nskb
) == skb_uarg(orig
))
1192 if (skb_copy_ubufs(nskb
, GFP_ATOMIC
))
1195 skb_zcopy_set(nskb
, skb_uarg(orig
), NULL
);
1201 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1202 * @skb: the skb to modify
1203 * @gfp_mask: allocation priority
1205 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1206 * It will copy all frags into kernel and drop the reference
1207 * to userspace pages.
1209 * If this function is called from an interrupt gfp_mask() must be
1212 * Returns 0 on success or a negative error code on failure
1213 * to allocate kernel memory to copy to.
1215 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
1217 int num_frags
= skb_shinfo(skb
)->nr_frags
;
1218 struct page
*page
, *head
= NULL
;
1222 if (skb_shared(skb
) || skb_unclone(skb
, gfp_mask
))
1228 new_frags
= (__skb_pagelen(skb
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1229 for (i
= 0; i
< new_frags
; i
++) {
1230 page
= alloc_page(gfp_mask
);
1233 struct page
*next
= (struct page
*)page_private(head
);
1239 set_page_private(page
, (unsigned long)head
);
1245 for (i
= 0; i
< num_frags
; i
++) {
1246 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1247 u32 p_off
, p_len
, copied
;
1251 skb_frag_foreach_page(f
, f
->page_offset
, skb_frag_size(f
),
1252 p
, p_off
, p_len
, copied
) {
1254 vaddr
= kmap_atomic(p
);
1256 while (done
< p_len
) {
1257 if (d_off
== PAGE_SIZE
) {
1259 page
= (struct page
*)page_private(page
);
1261 copy
= min_t(u32
, PAGE_SIZE
- d_off
, p_len
- done
);
1262 memcpy(page_address(page
) + d_off
,
1263 vaddr
+ p_off
+ done
, copy
);
1267 kunmap_atomic(vaddr
);
1271 /* skb frags release userspace buffers */
1272 for (i
= 0; i
< num_frags
; i
++)
1273 skb_frag_unref(skb
, i
);
1275 /* skb frags point to kernel buffers */
1276 for (i
= 0; i
< new_frags
- 1; i
++) {
1277 __skb_fill_page_desc(skb
, i
, head
, 0, PAGE_SIZE
);
1278 head
= (struct page
*)page_private(head
);
1280 __skb_fill_page_desc(skb
, new_frags
- 1, head
, 0, d_off
);
1281 skb_shinfo(skb
)->nr_frags
= new_frags
;
1284 skb_zcopy_clear(skb
, false);
1287 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
1290 * skb_clone - duplicate an sk_buff
1291 * @skb: buffer to clone
1292 * @gfp_mask: allocation priority
1294 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1295 * copies share the same packet data but not structure. The new
1296 * buffer has a reference count of 1. If the allocation fails the
1297 * function returns %NULL otherwise the new buffer is returned.
1299 * If this function is called from an interrupt gfp_mask() must be
1303 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
1305 struct sk_buff_fclones
*fclones
= container_of(skb
,
1306 struct sk_buff_fclones
,
1310 if (skb_orphan_frags(skb
, gfp_mask
))
1313 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
1314 refcount_read(&fclones
->fclone_ref
) == 1) {
1316 refcount_set(&fclones
->fclone_ref
, 2);
1318 if (skb_pfmemalloc(skb
))
1319 gfp_mask
|= __GFP_MEMALLOC
;
1321 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
1325 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
1328 return __skb_clone(n
, skb
);
1330 EXPORT_SYMBOL(skb_clone
);
1332 void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
1334 /* Only adjust this if it actually is csum_start rather than csum */
1335 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1336 skb
->csum_start
+= off
;
1337 /* {transport,network,mac}_header and tail are relative to skb->head */
1338 skb
->transport_header
+= off
;
1339 skb
->network_header
+= off
;
1340 if (skb_mac_header_was_set(skb
))
1341 skb
->mac_header
+= off
;
1342 skb
->inner_transport_header
+= off
;
1343 skb
->inner_network_header
+= off
;
1344 skb
->inner_mac_header
+= off
;
1346 EXPORT_SYMBOL(skb_headers_offset_update
);
1348 void skb_copy_header(struct sk_buff
*new, const struct sk_buff
*old
)
1350 __copy_skb_header(new, old
);
1352 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
1353 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
1354 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
1356 EXPORT_SYMBOL(skb_copy_header
);
1358 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
1360 if (skb_pfmemalloc(skb
))
1361 return SKB_ALLOC_RX
;
1366 * skb_copy - create private copy of an sk_buff
1367 * @skb: buffer to copy
1368 * @gfp_mask: allocation priority
1370 * Make a copy of both an &sk_buff and its data. This is used when the
1371 * caller wishes to modify the data and needs a private copy of the
1372 * data to alter. Returns %NULL on failure or the pointer to the buffer
1373 * on success. The returned buffer has a reference count of 1.
1375 * As by-product this function converts non-linear &sk_buff to linear
1376 * one, so that &sk_buff becomes completely private and caller is allowed
1377 * to modify all the data of returned buffer. This means that this
1378 * function is not recommended for use in circumstances when only
1379 * header is going to be modified. Use pskb_copy() instead.
1382 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
1384 int headerlen
= skb_headroom(skb
);
1385 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
1386 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
1387 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
1392 /* Set the data pointer */
1393 skb_reserve(n
, headerlen
);
1394 /* Set the tail pointer and length */
1395 skb_put(n
, skb
->len
);
1397 BUG_ON(skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
));
1399 skb_copy_header(n
, skb
);
1402 EXPORT_SYMBOL(skb_copy
);
1405 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1406 * @skb: buffer to copy
1407 * @headroom: headroom of new skb
1408 * @gfp_mask: allocation priority
1409 * @fclone: if true allocate the copy of the skb from the fclone
1410 * cache instead of the head cache; it is recommended to set this
1411 * to true for the cases where the copy will likely be cloned
1413 * Make a copy of both an &sk_buff and part of its data, located
1414 * in header. Fragmented data remain shared. This is used when
1415 * the caller wishes to modify only header of &sk_buff and needs
1416 * private copy of the header to alter. Returns %NULL on failure
1417 * or the pointer to the buffer on success.
1418 * The returned buffer has a reference count of 1.
1421 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
1422 gfp_t gfp_mask
, bool fclone
)
1424 unsigned int size
= skb_headlen(skb
) + headroom
;
1425 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
1426 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
1431 /* Set the data pointer */
1432 skb_reserve(n
, headroom
);
1433 /* Set the tail pointer and length */
1434 skb_put(n
, skb_headlen(skb
));
1435 /* Copy the bytes */
1436 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1438 n
->truesize
+= skb
->data_len
;
1439 n
->data_len
= skb
->data_len
;
1442 if (skb_shinfo(skb
)->nr_frags
) {
1445 if (skb_orphan_frags(skb
, gfp_mask
) ||
1446 skb_zerocopy_clone(n
, skb
, gfp_mask
)) {
1451 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1452 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1453 skb_frag_ref(skb
, i
);
1455 skb_shinfo(n
)->nr_frags
= i
;
1458 if (skb_has_frag_list(skb
)) {
1459 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1460 skb_clone_fraglist(n
);
1463 skb_copy_header(n
, skb
);
1467 EXPORT_SYMBOL(__pskb_copy_fclone
);
1470 * pskb_expand_head - reallocate header of &sk_buff
1471 * @skb: buffer to reallocate
1472 * @nhead: room to add at head
1473 * @ntail: room to add at tail
1474 * @gfp_mask: allocation priority
1476 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1477 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1478 * reference count of 1. Returns zero in the case of success or error,
1479 * if expansion failed. In the last case, &sk_buff is not changed.
1481 * All the pointers pointing into skb header may change and must be
1482 * reloaded after call to this function.
1485 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1488 int i
, osize
= skb_end_offset(skb
);
1489 int size
= osize
+ nhead
+ ntail
;
1495 BUG_ON(skb_shared(skb
));
1497 size
= SKB_DATA_ALIGN(size
);
1499 if (skb_pfmemalloc(skb
))
1500 gfp_mask
|= __GFP_MEMALLOC
;
1501 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1502 gfp_mask
, NUMA_NO_NODE
, NULL
);
1505 size
= SKB_WITH_OVERHEAD(ksize(data
));
1507 /* Copy only real data... and, alas, header. This should be
1508 * optimized for the cases when header is void.
1510 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1512 memcpy((struct skb_shared_info
*)(data
+ size
),
1514 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1517 * if shinfo is shared we must drop the old head gracefully, but if it
1518 * is not we can just drop the old head and let the existing refcount
1519 * be since all we did is relocate the values
1521 if (skb_cloned(skb
)) {
1522 if (skb_orphan_frags(skb
, gfp_mask
))
1525 refcount_inc(&skb_uarg(skb
)->refcnt
);
1526 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1527 skb_frag_ref(skb
, i
);
1529 if (skb_has_frag_list(skb
))
1530 skb_clone_fraglist(skb
);
1532 skb_release_data(skb
);
1536 off
= (data
+ nhead
) - skb
->head
;
1541 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1545 skb
->end
= skb
->head
+ size
;
1548 skb_headers_offset_update(skb
, nhead
);
1552 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1554 skb_metadata_clear(skb
);
1556 /* It is not generally safe to change skb->truesize.
1557 * For the moment, we really care of rx path, or
1558 * when skb is orphaned (not attached to a socket).
1560 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1561 skb
->truesize
+= size
- osize
;
1570 EXPORT_SYMBOL(pskb_expand_head
);
1572 /* Make private copy of skb with writable head and some headroom */
1574 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1576 struct sk_buff
*skb2
;
1577 int delta
= headroom
- skb_headroom(skb
);
1580 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1582 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1583 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1591 EXPORT_SYMBOL(skb_realloc_headroom
);
1594 * skb_copy_expand - copy and expand sk_buff
1595 * @skb: buffer to copy
1596 * @newheadroom: new free bytes at head
1597 * @newtailroom: new free bytes at tail
1598 * @gfp_mask: allocation priority
1600 * Make a copy of both an &sk_buff and its data and while doing so
1601 * allocate additional space.
1603 * This is used when the caller wishes to modify the data and needs a
1604 * private copy of the data to alter as well as more space for new fields.
1605 * Returns %NULL on failure or the pointer to the buffer
1606 * on success. The returned buffer has a reference count of 1.
1608 * You must pass %GFP_ATOMIC as the allocation priority if this function
1609 * is called from an interrupt.
1611 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1612 int newheadroom
, int newtailroom
,
1616 * Allocate the copy buffer
1618 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1619 gfp_mask
, skb_alloc_rx_flag(skb
),
1621 int oldheadroom
= skb_headroom(skb
);
1622 int head_copy_len
, head_copy_off
;
1627 skb_reserve(n
, newheadroom
);
1629 /* Set the tail pointer and length */
1630 skb_put(n
, skb
->len
);
1632 head_copy_len
= oldheadroom
;
1634 if (newheadroom
<= head_copy_len
)
1635 head_copy_len
= newheadroom
;
1637 head_copy_off
= newheadroom
- head_copy_len
;
1639 /* Copy the linear header and data. */
1640 BUG_ON(skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1641 skb
->len
+ head_copy_len
));
1643 skb_copy_header(n
, skb
);
1645 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1649 EXPORT_SYMBOL(skb_copy_expand
);
1652 * __skb_pad - zero pad the tail of an skb
1653 * @skb: buffer to pad
1654 * @pad: space to pad
1655 * @free_on_error: free buffer on error
1657 * Ensure that a buffer is followed by a padding area that is zero
1658 * filled. Used by network drivers which may DMA or transfer data
1659 * beyond the buffer end onto the wire.
1661 * May return error in out of memory cases. The skb is freed on error
1662 * if @free_on_error is true.
1665 int __skb_pad(struct sk_buff
*skb
, int pad
, bool free_on_error
)
1670 /* If the skbuff is non linear tailroom is always zero.. */
1671 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1672 memset(skb
->data
+skb
->len
, 0, pad
);
1676 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1677 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1678 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1683 /* FIXME: The use of this function with non-linear skb's really needs
1686 err
= skb_linearize(skb
);
1690 memset(skb
->data
+ skb
->len
, 0, pad
);
1698 EXPORT_SYMBOL(__skb_pad
);
1701 * pskb_put - add data to the tail of a potentially fragmented buffer
1702 * @skb: start of the buffer to use
1703 * @tail: tail fragment of the buffer to use
1704 * @len: amount of data to add
1706 * This function extends the used data area of the potentially
1707 * fragmented buffer. @tail must be the last fragment of @skb -- or
1708 * @skb itself. If this would exceed the total buffer size the kernel
1709 * will panic. A pointer to the first byte of the extra data is
1713 void *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1716 skb
->data_len
+= len
;
1719 return skb_put(tail
, len
);
1721 EXPORT_SYMBOL_GPL(pskb_put
);
1724 * skb_put - add data to a buffer
1725 * @skb: buffer to use
1726 * @len: amount of data to add
1728 * This function extends the used data area of the buffer. If this would
1729 * exceed the total buffer size the kernel will panic. A pointer to the
1730 * first byte of the extra data is returned.
1732 void *skb_put(struct sk_buff
*skb
, unsigned int len
)
1734 void *tmp
= skb_tail_pointer(skb
);
1735 SKB_LINEAR_ASSERT(skb
);
1738 if (unlikely(skb
->tail
> skb
->end
))
1739 skb_over_panic(skb
, len
, __builtin_return_address(0));
1742 EXPORT_SYMBOL(skb_put
);
1745 * skb_push - add data to the start of a buffer
1746 * @skb: buffer to use
1747 * @len: amount of data to add
1749 * This function extends the used data area of the buffer at the buffer
1750 * start. If this would exceed the total buffer headroom the kernel will
1751 * panic. A pointer to the first byte of the extra data is returned.
1753 void *skb_push(struct sk_buff
*skb
, unsigned int len
)
1757 if (unlikely(skb
->data
< skb
->head
))
1758 skb_under_panic(skb
, len
, __builtin_return_address(0));
1761 EXPORT_SYMBOL(skb_push
);
1764 * skb_pull - remove data from the start of a buffer
1765 * @skb: buffer to use
1766 * @len: amount of data to remove
1768 * This function removes data from the start of a buffer, returning
1769 * the memory to the headroom. A pointer to the next data in the buffer
1770 * is returned. Once the data has been pulled future pushes will overwrite
1773 void *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1775 return skb_pull_inline(skb
, len
);
1777 EXPORT_SYMBOL(skb_pull
);
1780 * skb_trim - remove end from a buffer
1781 * @skb: buffer to alter
1784 * Cut the length of a buffer down by removing data from the tail. If
1785 * the buffer is already under the length specified it is not modified.
1786 * The skb must be linear.
1788 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1791 __skb_trim(skb
, len
);
1793 EXPORT_SYMBOL(skb_trim
);
1795 /* Trims skb to length len. It can change skb pointers.
1798 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1800 struct sk_buff
**fragp
;
1801 struct sk_buff
*frag
;
1802 int offset
= skb_headlen(skb
);
1803 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1807 if (skb_cloned(skb
) &&
1808 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1815 for (; i
< nfrags
; i
++) {
1816 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1823 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1826 skb_shinfo(skb
)->nr_frags
= i
;
1828 for (; i
< nfrags
; i
++)
1829 skb_frag_unref(skb
, i
);
1831 if (skb_has_frag_list(skb
))
1832 skb_drop_fraglist(skb
);
1836 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1837 fragp
= &frag
->next
) {
1838 int end
= offset
+ frag
->len
;
1840 if (skb_shared(frag
)) {
1841 struct sk_buff
*nfrag
;
1843 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1844 if (unlikely(!nfrag
))
1847 nfrag
->next
= frag
->next
;
1859 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1863 skb_drop_list(&frag
->next
);
1868 if (len
> skb_headlen(skb
)) {
1869 skb
->data_len
-= skb
->len
- len
;
1874 skb_set_tail_pointer(skb
, len
);
1877 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1881 EXPORT_SYMBOL(___pskb_trim
);
1883 /* Note : use pskb_trim_rcsum() instead of calling this directly
1885 int pskb_trim_rcsum_slow(struct sk_buff
*skb
, unsigned int len
)
1887 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
1888 int delta
= skb
->len
- len
;
1890 skb
->csum
= csum_block_sub(skb
->csum
,
1891 skb_checksum(skb
, len
, delta
, 0),
1894 return __pskb_trim(skb
, len
);
1896 EXPORT_SYMBOL(pskb_trim_rcsum_slow
);
1899 * __pskb_pull_tail - advance tail of skb header
1900 * @skb: buffer to reallocate
1901 * @delta: number of bytes to advance tail
1903 * The function makes a sense only on a fragmented &sk_buff,
1904 * it expands header moving its tail forward and copying necessary
1905 * data from fragmented part.
1907 * &sk_buff MUST have reference count of 1.
1909 * Returns %NULL (and &sk_buff does not change) if pull failed
1910 * or value of new tail of skb in the case of success.
1912 * All the pointers pointing into skb header may change and must be
1913 * reloaded after call to this function.
1916 /* Moves tail of skb head forward, copying data from fragmented part,
1917 * when it is necessary.
1918 * 1. It may fail due to malloc failure.
1919 * 2. It may change skb pointers.
1921 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1923 void *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1925 /* If skb has not enough free space at tail, get new one
1926 * plus 128 bytes for future expansions. If we have enough
1927 * room at tail, reallocate without expansion only if skb is cloned.
1929 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1931 if (eat
> 0 || skb_cloned(skb
)) {
1932 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1937 BUG_ON(skb_copy_bits(skb
, skb_headlen(skb
),
1938 skb_tail_pointer(skb
), delta
));
1940 /* Optimization: no fragments, no reasons to preestimate
1941 * size of pulled pages. Superb.
1943 if (!skb_has_frag_list(skb
))
1946 /* Estimate size of pulled pages. */
1948 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1949 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1956 /* If we need update frag list, we are in troubles.
1957 * Certainly, it is possible to add an offset to skb data,
1958 * but taking into account that pulling is expected to
1959 * be very rare operation, it is worth to fight against
1960 * further bloating skb head and crucify ourselves here instead.
1961 * Pure masohism, indeed. 8)8)
1964 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1965 struct sk_buff
*clone
= NULL
;
1966 struct sk_buff
*insp
= NULL
;
1969 if (list
->len
<= eat
) {
1970 /* Eaten as whole. */
1975 /* Eaten partially. */
1977 if (skb_shared(list
)) {
1978 /* Sucks! We need to fork list. :-( */
1979 clone
= skb_clone(list
, GFP_ATOMIC
);
1985 /* This may be pulled without
1989 if (!pskb_pull(list
, eat
)) {
1997 /* Free pulled out fragments. */
1998 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1999 skb_shinfo(skb
)->frag_list
= list
->next
;
2002 /* And insert new clone at head. */
2005 skb_shinfo(skb
)->frag_list
= clone
;
2008 /* Success! Now we may commit changes to skb data. */
2013 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2014 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2017 skb_frag_unref(skb
, i
);
2020 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2022 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
2023 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
2031 skb_shinfo(skb
)->nr_frags
= k
;
2035 skb
->data_len
-= delta
;
2038 skb_zcopy_clear(skb
, false);
2040 return skb_tail_pointer(skb
);
2042 EXPORT_SYMBOL(__pskb_pull_tail
);
2045 * skb_copy_bits - copy bits from skb to kernel buffer
2047 * @offset: offset in source
2048 * @to: destination buffer
2049 * @len: number of bytes to copy
2051 * Copy the specified number of bytes from the source skb to the
2052 * destination buffer.
2055 * If its prototype is ever changed,
2056 * check arch/{*}/net/{*}.S files,
2057 * since it is called from BPF assembly code.
2059 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
2061 int start
= skb_headlen(skb
);
2062 struct sk_buff
*frag_iter
;
2065 if (offset
> (int)skb
->len
- len
)
2069 if ((copy
= start
- offset
) > 0) {
2072 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
2073 if ((len
-= copy
) == 0)
2079 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2081 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
2083 WARN_ON(start
> offset
+ len
);
2085 end
= start
+ skb_frag_size(f
);
2086 if ((copy
= end
- offset
) > 0) {
2087 u32 p_off
, p_len
, copied
;
2094 skb_frag_foreach_page(f
,
2095 f
->page_offset
+ offset
- start
,
2096 copy
, p
, p_off
, p_len
, copied
) {
2097 vaddr
= kmap_atomic(p
);
2098 memcpy(to
+ copied
, vaddr
+ p_off
, p_len
);
2099 kunmap_atomic(vaddr
);
2102 if ((len
-= copy
) == 0)
2110 skb_walk_frags(skb
, frag_iter
) {
2113 WARN_ON(start
> offset
+ len
);
2115 end
= start
+ frag_iter
->len
;
2116 if ((copy
= end
- offset
) > 0) {
2119 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
2121 if ((len
-= copy
) == 0)
2135 EXPORT_SYMBOL(skb_copy_bits
);
2138 * Callback from splice_to_pipe(), if we need to release some pages
2139 * at the end of the spd in case we error'ed out in filling the pipe.
2141 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
2143 put_page(spd
->pages
[i
]);
2146 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
2147 unsigned int *offset
,
2150 struct page_frag
*pfrag
= sk_page_frag(sk
);
2152 if (!sk_page_frag_refill(sk
, pfrag
))
2155 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
2157 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
2158 page_address(page
) + *offset
, *len
);
2159 *offset
= pfrag
->offset
;
2160 pfrag
->offset
+= *len
;
2165 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
2167 unsigned int offset
)
2169 return spd
->nr_pages
&&
2170 spd
->pages
[spd
->nr_pages
- 1] == page
&&
2171 (spd
->partial
[spd
->nr_pages
- 1].offset
+
2172 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
2176 * Fill page/offset/length into spd, if it can hold more pages.
2178 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
2179 struct pipe_inode_info
*pipe
, struct page
*page
,
2180 unsigned int *len
, unsigned int offset
,
2184 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
2188 page
= linear_to_page(page
, len
, &offset
, sk
);
2192 if (spd_can_coalesce(spd
, page
, offset
)) {
2193 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
2197 spd
->pages
[spd
->nr_pages
] = page
;
2198 spd
->partial
[spd
->nr_pages
].len
= *len
;
2199 spd
->partial
[spd
->nr_pages
].offset
= offset
;
2205 static bool __splice_segment(struct page
*page
, unsigned int poff
,
2206 unsigned int plen
, unsigned int *off
,
2208 struct splice_pipe_desc
*spd
, bool linear
,
2210 struct pipe_inode_info
*pipe
)
2215 /* skip this segment if already processed */
2221 /* ignore any bits we already processed */
2227 unsigned int flen
= min(*len
, plen
);
2229 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
2235 } while (*len
&& plen
);
2241 * Map linear and fragment data from the skb to spd. It reports true if the
2242 * pipe is full or if we already spliced the requested length.
2244 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
2245 unsigned int *offset
, unsigned int *len
,
2246 struct splice_pipe_desc
*spd
, struct sock
*sk
)
2249 struct sk_buff
*iter
;
2251 /* map the linear part :
2252 * If skb->head_frag is set, this 'linear' part is backed by a
2253 * fragment, and if the head is not shared with any clones then
2254 * we can avoid a copy since we own the head portion of this page.
2256 if (__splice_segment(virt_to_page(skb
->data
),
2257 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
2260 skb_head_is_locked(skb
),
2265 * then map the fragments
2267 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
2268 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
2270 if (__splice_segment(skb_frag_page(f
),
2271 f
->page_offset
, skb_frag_size(f
),
2272 offset
, len
, spd
, false, sk
, pipe
))
2276 skb_walk_frags(skb
, iter
) {
2277 if (*offset
>= iter
->len
) {
2278 *offset
-= iter
->len
;
2281 /* __skb_splice_bits() only fails if the output has no room
2282 * left, so no point in going over the frag_list for the error
2285 if (__skb_splice_bits(iter
, pipe
, offset
, len
, spd
, sk
))
2293 * Map data from the skb to a pipe. Should handle both the linear part,
2294 * the fragments, and the frag list.
2296 int skb_splice_bits(struct sk_buff
*skb
, struct sock
*sk
, unsigned int offset
,
2297 struct pipe_inode_info
*pipe
, unsigned int tlen
,
2300 struct partial_page partial
[MAX_SKB_FRAGS
];
2301 struct page
*pages
[MAX_SKB_FRAGS
];
2302 struct splice_pipe_desc spd
= {
2305 .nr_pages_max
= MAX_SKB_FRAGS
,
2306 .ops
= &nosteal_pipe_buf_ops
,
2307 .spd_release
= sock_spd_release
,
2311 __skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
);
2314 ret
= splice_to_pipe(pipe
, &spd
);
2318 EXPORT_SYMBOL_GPL(skb_splice_bits
);
2320 /* Send skb data on a socket. Socket must be locked. */
2321 int skb_send_sock_locked(struct sock
*sk
, struct sk_buff
*skb
, int offset
,
2324 unsigned int orig_len
= len
;
2325 struct sk_buff
*head
= skb
;
2326 unsigned short fragidx
;
2331 /* Deal with head data */
2332 while (offset
< skb_headlen(skb
) && len
) {
2336 slen
= min_t(int, len
, skb_headlen(skb
) - offset
);
2337 kv
.iov_base
= skb
->data
+ offset
;
2339 memset(&msg
, 0, sizeof(msg
));
2340 msg
.msg_flags
= MSG_DONTWAIT
;
2342 ret
= kernel_sendmsg_locked(sk
, &msg
, &kv
, 1, slen
);
2350 /* All the data was skb head? */
2354 /* Make offset relative to start of frags */
2355 offset
-= skb_headlen(skb
);
2357 /* Find where we are in frag list */
2358 for (fragidx
= 0; fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2359 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2361 if (offset
< frag
->size
)
2364 offset
-= frag
->size
;
2367 for (; len
&& fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2368 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2370 slen
= min_t(size_t, len
, frag
->size
- offset
);
2373 ret
= kernel_sendpage_locked(sk
, frag
->page
.p
,
2374 frag
->page_offset
+ offset
,
2375 slen
, MSG_DONTWAIT
);
2388 /* Process any frag lists */
2391 if (skb_has_frag_list(skb
)) {
2392 skb
= skb_shinfo(skb
)->frag_list
;
2395 } else if (skb
->next
) {
2402 return orig_len
- len
;
2405 return orig_len
== len
? ret
: orig_len
- len
;
2407 EXPORT_SYMBOL_GPL(skb_send_sock_locked
);
2410 * skb_store_bits - store bits from kernel buffer to skb
2411 * @skb: destination buffer
2412 * @offset: offset in destination
2413 * @from: source buffer
2414 * @len: number of bytes to copy
2416 * Copy the specified number of bytes from the source buffer to the
2417 * destination skb. This function handles all the messy bits of
2418 * traversing fragment lists and such.
2421 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
2423 int start
= skb_headlen(skb
);
2424 struct sk_buff
*frag_iter
;
2427 if (offset
> (int)skb
->len
- len
)
2430 if ((copy
= start
- offset
) > 0) {
2433 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
2434 if ((len
-= copy
) == 0)
2440 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2441 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2444 WARN_ON(start
> offset
+ len
);
2446 end
= start
+ skb_frag_size(frag
);
2447 if ((copy
= end
- offset
) > 0) {
2448 u32 p_off
, p_len
, copied
;
2455 skb_frag_foreach_page(frag
,
2456 frag
->page_offset
+ offset
- start
,
2457 copy
, p
, p_off
, p_len
, copied
) {
2458 vaddr
= kmap_atomic(p
);
2459 memcpy(vaddr
+ p_off
, from
+ copied
, p_len
);
2460 kunmap_atomic(vaddr
);
2463 if ((len
-= copy
) == 0)
2471 skb_walk_frags(skb
, frag_iter
) {
2474 WARN_ON(start
> offset
+ len
);
2476 end
= start
+ frag_iter
->len
;
2477 if ((copy
= end
- offset
) > 0) {
2480 if (skb_store_bits(frag_iter
, offset
- start
,
2483 if ((len
-= copy
) == 0)
2496 EXPORT_SYMBOL(skb_store_bits
);
2498 /* Checksum skb data. */
2499 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
2500 __wsum csum
, const struct skb_checksum_ops
*ops
)
2502 int start
= skb_headlen(skb
);
2503 int i
, copy
= start
- offset
;
2504 struct sk_buff
*frag_iter
;
2507 /* Checksum header. */
2511 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
2512 if ((len
-= copy
) == 0)
2518 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2520 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2522 WARN_ON(start
> offset
+ len
);
2524 end
= start
+ skb_frag_size(frag
);
2525 if ((copy
= end
- offset
) > 0) {
2526 u32 p_off
, p_len
, copied
;
2534 skb_frag_foreach_page(frag
,
2535 frag
->page_offset
+ offset
- start
,
2536 copy
, p
, p_off
, p_len
, copied
) {
2537 vaddr
= kmap_atomic(p
);
2538 csum2
= ops
->update(vaddr
+ p_off
, p_len
, 0);
2539 kunmap_atomic(vaddr
);
2540 csum
= ops
->combine(csum
, csum2
, pos
, p_len
);
2551 skb_walk_frags(skb
, frag_iter
) {
2554 WARN_ON(start
> offset
+ len
);
2556 end
= start
+ frag_iter
->len
;
2557 if ((copy
= end
- offset
) > 0) {
2561 csum2
= __skb_checksum(frag_iter
, offset
- start
,
2563 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2564 if ((len
-= copy
) == 0)
2575 EXPORT_SYMBOL(__skb_checksum
);
2577 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2578 int len
, __wsum csum
)
2580 const struct skb_checksum_ops ops
= {
2581 .update
= csum_partial_ext
,
2582 .combine
= csum_block_add_ext
,
2585 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2587 EXPORT_SYMBOL(skb_checksum
);
2589 /* Both of above in one bottle. */
2591 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2592 u8
*to
, int len
, __wsum csum
)
2594 int start
= skb_headlen(skb
);
2595 int i
, copy
= start
- offset
;
2596 struct sk_buff
*frag_iter
;
2603 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2605 if ((len
-= copy
) == 0)
2612 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2615 WARN_ON(start
> offset
+ len
);
2617 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2618 if ((copy
= end
- offset
) > 0) {
2619 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2620 u32 p_off
, p_len
, copied
;
2628 skb_frag_foreach_page(frag
,
2629 frag
->page_offset
+ offset
- start
,
2630 copy
, p
, p_off
, p_len
, copied
) {
2631 vaddr
= kmap_atomic(p
);
2632 csum2
= csum_partial_copy_nocheck(vaddr
+ p_off
,
2635 kunmap_atomic(vaddr
);
2636 csum
= csum_block_add(csum
, csum2
, pos
);
2648 skb_walk_frags(skb
, frag_iter
) {
2652 WARN_ON(start
> offset
+ len
);
2654 end
= start
+ frag_iter
->len
;
2655 if ((copy
= end
- offset
) > 0) {
2658 csum2
= skb_copy_and_csum_bits(frag_iter
,
2661 csum
= csum_block_add(csum
, csum2
, pos
);
2662 if ((len
-= copy
) == 0)
2673 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2675 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
2679 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
2680 /* See comments in __skb_checksum_complete(). */
2682 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
2683 !skb
->csum_complete_sw
)
2684 netdev_rx_csum_fault(skb
->dev
, skb
);
2686 if (!skb_shared(skb
))
2687 skb
->csum_valid
= !sum
;
2690 EXPORT_SYMBOL(__skb_checksum_complete_head
);
2692 /* This function assumes skb->csum already holds pseudo header's checksum,
2693 * which has been changed from the hardware checksum, for example, by
2694 * __skb_checksum_validate_complete(). And, the original skb->csum must
2695 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2697 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2698 * zero. The new checksum is stored back into skb->csum unless the skb is
2701 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
2706 csum
= skb_checksum(skb
, 0, skb
->len
, 0);
2708 sum
= csum_fold(csum_add(skb
->csum
, csum
));
2709 /* This check is inverted, because we already knew the hardware
2710 * checksum is invalid before calling this function. So, if the
2711 * re-computed checksum is valid instead, then we have a mismatch
2712 * between the original skb->csum and skb_checksum(). This means either
2713 * the original hardware checksum is incorrect or we screw up skb->csum
2714 * when moving skb->data around.
2717 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
2718 !skb
->csum_complete_sw
)
2719 netdev_rx_csum_fault(skb
->dev
, skb
);
2722 if (!skb_shared(skb
)) {
2723 /* Save full packet checksum */
2725 skb
->ip_summed
= CHECKSUM_COMPLETE
;
2726 skb
->csum_complete_sw
= 1;
2727 skb
->csum_valid
= !sum
;
2732 EXPORT_SYMBOL(__skb_checksum_complete
);
2734 static __wsum
warn_crc32c_csum_update(const void *buff
, int len
, __wsum sum
)
2736 net_warn_ratelimited(
2737 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2742 static __wsum
warn_crc32c_csum_combine(__wsum csum
, __wsum csum2
,
2743 int offset
, int len
)
2745 net_warn_ratelimited(
2746 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2751 static const struct skb_checksum_ops default_crc32c_ops
= {
2752 .update
= warn_crc32c_csum_update
,
2753 .combine
= warn_crc32c_csum_combine
,
2756 const struct skb_checksum_ops
*crc32c_csum_stub __read_mostly
=
2757 &default_crc32c_ops
;
2758 EXPORT_SYMBOL(crc32c_csum_stub
);
2761 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2762 * @from: source buffer
2764 * Calculates the amount of linear headroom needed in the 'to' skb passed
2765 * into skb_zerocopy().
2768 skb_zerocopy_headlen(const struct sk_buff
*from
)
2770 unsigned int hlen
= 0;
2772 if (!from
->head_frag
||
2773 skb_headlen(from
) < L1_CACHE_BYTES
||
2774 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2775 hlen
= skb_headlen(from
);
2777 if (skb_has_frag_list(from
))
2782 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2785 * skb_zerocopy - Zero copy skb to skb
2786 * @to: destination buffer
2787 * @from: source buffer
2788 * @len: number of bytes to copy from source buffer
2789 * @hlen: size of linear headroom in destination buffer
2791 * Copies up to `len` bytes from `from` to `to` by creating references
2792 * to the frags in the source buffer.
2794 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2795 * headroom in the `to` buffer.
2798 * 0: everything is OK
2799 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2800 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2803 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2806 int plen
= 0; /* length of skb->head fragment */
2809 unsigned int offset
;
2811 BUG_ON(!from
->head_frag
&& !hlen
);
2813 /* dont bother with small payloads */
2814 if (len
<= skb_tailroom(to
))
2815 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2818 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2823 plen
= min_t(int, skb_headlen(from
), len
);
2825 page
= virt_to_head_page(from
->head
);
2826 offset
= from
->data
- (unsigned char *)page_address(page
);
2827 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2834 to
->truesize
+= len
+ plen
;
2835 to
->len
+= len
+ plen
;
2836 to
->data_len
+= len
+ plen
;
2838 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2842 skb_zerocopy_clone(to
, from
, GFP_ATOMIC
);
2844 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2847 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2848 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2849 len
-= skb_shinfo(to
)->frags
[j
].size
;
2850 skb_frag_ref(to
, j
);
2853 skb_shinfo(to
)->nr_frags
= j
;
2857 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2859 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2864 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2865 csstart
= skb_checksum_start_offset(skb
);
2867 csstart
= skb_headlen(skb
);
2869 BUG_ON(csstart
> skb_headlen(skb
));
2871 skb_copy_from_linear_data(skb
, to
, csstart
);
2874 if (csstart
!= skb
->len
)
2875 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2876 skb
->len
- csstart
, 0);
2878 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2879 long csstuff
= csstart
+ skb
->csum_offset
;
2881 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2884 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2887 * skb_dequeue - remove from the head of the queue
2888 * @list: list to dequeue from
2890 * Remove the head of the list. The list lock is taken so the function
2891 * may be used safely with other locking list functions. The head item is
2892 * returned or %NULL if the list is empty.
2895 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2897 unsigned long flags
;
2898 struct sk_buff
*result
;
2900 spin_lock_irqsave(&list
->lock
, flags
);
2901 result
= __skb_dequeue(list
);
2902 spin_unlock_irqrestore(&list
->lock
, flags
);
2905 EXPORT_SYMBOL(skb_dequeue
);
2908 * skb_dequeue_tail - remove from the tail of the queue
2909 * @list: list to dequeue from
2911 * Remove the tail of the list. The list lock is taken so the function
2912 * may be used safely with other locking list functions. The tail item is
2913 * returned or %NULL if the list is empty.
2915 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2917 unsigned long flags
;
2918 struct sk_buff
*result
;
2920 spin_lock_irqsave(&list
->lock
, flags
);
2921 result
= __skb_dequeue_tail(list
);
2922 spin_unlock_irqrestore(&list
->lock
, flags
);
2925 EXPORT_SYMBOL(skb_dequeue_tail
);
2928 * skb_queue_purge - empty a list
2929 * @list: list to empty
2931 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2932 * the list and one reference dropped. This function takes the list
2933 * lock and is atomic with respect to other list locking functions.
2935 void skb_queue_purge(struct sk_buff_head
*list
)
2937 struct sk_buff
*skb
;
2938 while ((skb
= skb_dequeue(list
)) != NULL
)
2941 EXPORT_SYMBOL(skb_queue_purge
);
2944 * skb_rbtree_purge - empty a skb rbtree
2945 * @root: root of the rbtree to empty
2946 * Return value: the sum of truesizes of all purged skbs.
2948 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2949 * the list and one reference dropped. This function does not take
2950 * any lock. Synchronization should be handled by the caller (e.g., TCP
2951 * out-of-order queue is protected by the socket lock).
2953 unsigned int skb_rbtree_purge(struct rb_root
*root
)
2955 struct rb_node
*p
= rb_first(root
);
2956 unsigned int sum
= 0;
2959 struct sk_buff
*skb
= rb_entry(p
, struct sk_buff
, rbnode
);
2962 rb_erase(&skb
->rbnode
, root
);
2963 sum
+= skb
->truesize
;
2970 * skb_queue_head - queue a buffer at the list head
2971 * @list: list to use
2972 * @newsk: buffer to queue
2974 * Queue a buffer at the start of the list. This function takes the
2975 * list lock and can be used safely with other locking &sk_buff functions
2978 * A buffer cannot be placed on two lists at the same time.
2980 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2982 unsigned long flags
;
2984 spin_lock_irqsave(&list
->lock
, flags
);
2985 __skb_queue_head(list
, newsk
);
2986 spin_unlock_irqrestore(&list
->lock
, flags
);
2988 EXPORT_SYMBOL(skb_queue_head
);
2991 * skb_queue_tail - queue a buffer at the list tail
2992 * @list: list to use
2993 * @newsk: buffer to queue
2995 * Queue a buffer at the tail of the list. This function takes the
2996 * list lock and can be used safely with other locking &sk_buff functions
2999 * A buffer cannot be placed on two lists at the same time.
3001 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
3003 unsigned long flags
;
3005 spin_lock_irqsave(&list
->lock
, flags
);
3006 __skb_queue_tail(list
, newsk
);
3007 spin_unlock_irqrestore(&list
->lock
, flags
);
3009 EXPORT_SYMBOL(skb_queue_tail
);
3012 * skb_unlink - remove a buffer from a list
3013 * @skb: buffer to remove
3014 * @list: list to use
3016 * Remove a packet from a list. The list locks are taken and this
3017 * function is atomic with respect to other list locked calls
3019 * You must know what list the SKB is on.
3021 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
3023 unsigned long flags
;
3025 spin_lock_irqsave(&list
->lock
, flags
);
3026 __skb_unlink(skb
, list
);
3027 spin_unlock_irqrestore(&list
->lock
, flags
);
3029 EXPORT_SYMBOL(skb_unlink
);
3032 * skb_append - append a buffer
3033 * @old: buffer to insert after
3034 * @newsk: buffer to insert
3035 * @list: list to use
3037 * Place a packet after a given packet in a list. The list locks are taken
3038 * and this function is atomic with respect to other list locked calls.
3039 * A buffer cannot be placed on two lists at the same time.
3041 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
3043 unsigned long flags
;
3045 spin_lock_irqsave(&list
->lock
, flags
);
3046 __skb_queue_after(list
, old
, newsk
);
3047 spin_unlock_irqrestore(&list
->lock
, flags
);
3049 EXPORT_SYMBOL(skb_append
);
3051 static inline void skb_split_inside_header(struct sk_buff
*skb
,
3052 struct sk_buff
* skb1
,
3053 const u32 len
, const int pos
)
3057 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
3059 /* And move data appendix as is. */
3060 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
3061 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
3063 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
3064 skb_shinfo(skb
)->nr_frags
= 0;
3065 skb1
->data_len
= skb
->data_len
;
3066 skb1
->len
+= skb1
->data_len
;
3069 skb_set_tail_pointer(skb
, len
);
3072 static inline void skb_split_no_header(struct sk_buff
*skb
,
3073 struct sk_buff
* skb1
,
3074 const u32 len
, int pos
)
3077 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
3079 skb_shinfo(skb
)->nr_frags
= 0;
3080 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
3082 skb
->data_len
= len
- pos
;
3084 for (i
= 0; i
< nfrags
; i
++) {
3085 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3087 if (pos
+ size
> len
) {
3088 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
3092 * We have two variants in this case:
3093 * 1. Move all the frag to the second
3094 * part, if it is possible. F.e.
3095 * this approach is mandatory for TUX,
3096 * where splitting is expensive.
3097 * 2. Split is accurately. We make this.
3099 skb_frag_ref(skb
, i
);
3100 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
3101 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
3102 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
3103 skb_shinfo(skb
)->nr_frags
++;
3107 skb_shinfo(skb
)->nr_frags
++;
3110 skb_shinfo(skb1
)->nr_frags
= k
;
3114 * skb_split - Split fragmented skb to two parts at length len.
3115 * @skb: the buffer to split
3116 * @skb1: the buffer to receive the second part
3117 * @len: new length for skb
3119 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
3121 int pos
= skb_headlen(skb
);
3123 skb_shinfo(skb1
)->tx_flags
|= skb_shinfo(skb
)->tx_flags
&
3125 skb_zerocopy_clone(skb1
, skb
, 0);
3126 if (len
< pos
) /* Split line is inside header. */
3127 skb_split_inside_header(skb
, skb1
, len
, pos
);
3128 else /* Second chunk has no header, nothing to copy. */
3129 skb_split_no_header(skb
, skb1
, len
, pos
);
3131 EXPORT_SYMBOL(skb_split
);
3133 /* Shifting from/to a cloned skb is a no-go.
3135 * Caller cannot keep skb_shinfo related pointers past calling here!
3137 static int skb_prepare_for_shift(struct sk_buff
*skb
)
3139 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
3143 * skb_shift - Shifts paged data partially from skb to another
3144 * @tgt: buffer into which tail data gets added
3145 * @skb: buffer from which the paged data comes from
3146 * @shiftlen: shift up to this many bytes
3148 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3149 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3150 * It's up to caller to free skb if everything was shifted.
3152 * If @tgt runs out of frags, the whole operation is aborted.
3154 * Skb cannot include anything else but paged data while tgt is allowed
3155 * to have non-paged data as well.
3157 * TODO: full sized shift could be optimized but that would need
3158 * specialized skb free'er to handle frags without up-to-date nr_frags.
3160 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
3162 int from
, to
, merge
, todo
;
3163 struct skb_frag_struct
*fragfrom
, *fragto
;
3165 BUG_ON(shiftlen
> skb
->len
);
3167 if (skb_headlen(skb
))
3169 if (skb_zcopy(tgt
) || skb_zcopy(skb
))
3174 to
= skb_shinfo(tgt
)->nr_frags
;
3175 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3177 /* Actual merge is delayed until the point when we know we can
3178 * commit all, so that we don't have to undo partial changes
3181 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
3182 fragfrom
->page_offset
)) {
3187 todo
-= skb_frag_size(fragfrom
);
3189 if (skb_prepare_for_shift(skb
) ||
3190 skb_prepare_for_shift(tgt
))
3193 /* All previous frag pointers might be stale! */
3194 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3195 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3197 skb_frag_size_add(fragto
, shiftlen
);
3198 skb_frag_size_sub(fragfrom
, shiftlen
);
3199 fragfrom
->page_offset
+= shiftlen
;
3207 /* Skip full, not-fitting skb to avoid expensive operations */
3208 if ((shiftlen
== skb
->len
) &&
3209 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
3212 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
3215 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
3216 if (to
== MAX_SKB_FRAGS
)
3219 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3220 fragto
= &skb_shinfo(tgt
)->frags
[to
];
3222 if (todo
>= skb_frag_size(fragfrom
)) {
3223 *fragto
= *fragfrom
;
3224 todo
-= skb_frag_size(fragfrom
);
3229 __skb_frag_ref(fragfrom
);
3230 fragto
->page
= fragfrom
->page
;
3231 fragto
->page_offset
= fragfrom
->page_offset
;
3232 skb_frag_size_set(fragto
, todo
);
3234 fragfrom
->page_offset
+= todo
;
3235 skb_frag_size_sub(fragfrom
, todo
);
3243 /* Ready to "commit" this state change to tgt */
3244 skb_shinfo(tgt
)->nr_frags
= to
;
3247 fragfrom
= &skb_shinfo(skb
)->frags
[0];
3248 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3250 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
3251 __skb_frag_unref(fragfrom
);
3254 /* Reposition in the original skb */
3256 while (from
< skb_shinfo(skb
)->nr_frags
)
3257 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
3258 skb_shinfo(skb
)->nr_frags
= to
;
3260 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
3263 /* Most likely the tgt won't ever need its checksum anymore, skb on
3264 * the other hand might need it if it needs to be resent
3266 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
3267 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3269 /* Yak, is it really working this way? Some helper please? */
3270 skb
->len
-= shiftlen
;
3271 skb
->data_len
-= shiftlen
;
3272 skb
->truesize
-= shiftlen
;
3273 tgt
->len
+= shiftlen
;
3274 tgt
->data_len
+= shiftlen
;
3275 tgt
->truesize
+= shiftlen
;
3281 * skb_prepare_seq_read - Prepare a sequential read of skb data
3282 * @skb: the buffer to read
3283 * @from: lower offset of data to be read
3284 * @to: upper offset of data to be read
3285 * @st: state variable
3287 * Initializes the specified state variable. Must be called before
3288 * invoking skb_seq_read() for the first time.
3290 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
3291 unsigned int to
, struct skb_seq_state
*st
)
3293 st
->lower_offset
= from
;
3294 st
->upper_offset
= to
;
3295 st
->root_skb
= st
->cur_skb
= skb
;
3296 st
->frag_idx
= st
->stepped_offset
= 0;
3297 st
->frag_data
= NULL
;
3299 EXPORT_SYMBOL(skb_prepare_seq_read
);
3302 * skb_seq_read - Sequentially read skb data
3303 * @consumed: number of bytes consumed by the caller so far
3304 * @data: destination pointer for data to be returned
3305 * @st: state variable
3307 * Reads a block of skb data at @consumed relative to the
3308 * lower offset specified to skb_prepare_seq_read(). Assigns
3309 * the head of the data block to @data and returns the length
3310 * of the block or 0 if the end of the skb data or the upper
3311 * offset has been reached.
3313 * The caller is not required to consume all of the data
3314 * returned, i.e. @consumed is typically set to the number
3315 * of bytes already consumed and the next call to
3316 * skb_seq_read() will return the remaining part of the block.
3318 * Note 1: The size of each block of data returned can be arbitrary,
3319 * this limitation is the cost for zerocopy sequential
3320 * reads of potentially non linear data.
3322 * Note 2: Fragment lists within fragments are not implemented
3323 * at the moment, state->root_skb could be replaced with
3324 * a stack for this purpose.
3326 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
3327 struct skb_seq_state
*st
)
3329 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
3332 if (unlikely(abs_offset
>= st
->upper_offset
)) {
3333 if (st
->frag_data
) {
3334 kunmap_atomic(st
->frag_data
);
3335 st
->frag_data
= NULL
;
3341 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
3343 if (abs_offset
< block_limit
&& !st
->frag_data
) {
3344 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
3345 return block_limit
- abs_offset
;
3348 if (st
->frag_idx
== 0 && !st
->frag_data
)
3349 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
3351 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
3352 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
3353 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
3355 if (abs_offset
< block_limit
) {
3357 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
3359 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
3360 (abs_offset
- st
->stepped_offset
);
3362 return block_limit
- abs_offset
;
3365 if (st
->frag_data
) {
3366 kunmap_atomic(st
->frag_data
);
3367 st
->frag_data
= NULL
;
3371 st
->stepped_offset
+= skb_frag_size(frag
);
3374 if (st
->frag_data
) {
3375 kunmap_atomic(st
->frag_data
);
3376 st
->frag_data
= NULL
;
3379 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
3380 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
3383 } else if (st
->cur_skb
->next
) {
3384 st
->cur_skb
= st
->cur_skb
->next
;
3391 EXPORT_SYMBOL(skb_seq_read
);
3394 * skb_abort_seq_read - Abort a sequential read of skb data
3395 * @st: state variable
3397 * Must be called if skb_seq_read() was not called until it
3400 void skb_abort_seq_read(struct skb_seq_state
*st
)
3403 kunmap_atomic(st
->frag_data
);
3405 EXPORT_SYMBOL(skb_abort_seq_read
);
3407 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3409 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
3410 struct ts_config
*conf
,
3411 struct ts_state
*state
)
3413 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
3416 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
3418 skb_abort_seq_read(TS_SKB_CB(state
));
3422 * skb_find_text - Find a text pattern in skb data
3423 * @skb: the buffer to look in
3424 * @from: search offset
3426 * @config: textsearch configuration
3428 * Finds a pattern in the skb data according to the specified
3429 * textsearch configuration. Use textsearch_next() to retrieve
3430 * subsequent occurrences of the pattern. Returns the offset
3431 * to the first occurrence or UINT_MAX if no match was found.
3433 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
3434 unsigned int to
, struct ts_config
*config
)
3436 struct ts_state state
;
3439 config
->get_next_block
= skb_ts_get_next_block
;
3440 config
->finish
= skb_ts_finish
;
3442 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(&state
));
3444 ret
= textsearch_find(config
, &state
);
3445 return (ret
<= to
- from
? ret
: UINT_MAX
);
3447 EXPORT_SYMBOL(skb_find_text
);
3449 int skb_append_pagefrags(struct sk_buff
*skb
, struct page
*page
,
3450 int offset
, size_t size
)
3452 int i
= skb_shinfo(skb
)->nr_frags
;
3454 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
3455 skb_frag_size_add(&skb_shinfo(skb
)->frags
[i
- 1], size
);
3456 } else if (i
< MAX_SKB_FRAGS
) {
3458 skb_fill_page_desc(skb
, i
, page
, offset
, size
);
3465 EXPORT_SYMBOL_GPL(skb_append_pagefrags
);
3468 * skb_pull_rcsum - pull skb and update receive checksum
3469 * @skb: buffer to update
3470 * @len: length of data pulled
3472 * This function performs an skb_pull on the packet and updates
3473 * the CHECKSUM_COMPLETE checksum. It should be used on
3474 * receive path processing instead of skb_pull unless you know
3475 * that the checksum difference is zero (e.g., a valid IP header)
3476 * or you are setting ip_summed to CHECKSUM_NONE.
3478 void *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
3480 unsigned char *data
= skb
->data
;
3482 BUG_ON(len
> skb
->len
);
3483 __skb_pull(skb
, len
);
3484 skb_postpull_rcsum(skb
, data
, len
);
3487 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
3489 static inline skb_frag_t
skb_head_frag_to_page_desc(struct sk_buff
*frag_skb
)
3491 skb_frag_t head_frag
;
3494 page
= virt_to_head_page(frag_skb
->head
);
3495 head_frag
.page
.p
= page
;
3496 head_frag
.page_offset
= frag_skb
->data
-
3497 (unsigned char *)page_address(page
);
3498 head_frag
.size
= skb_headlen(frag_skb
);
3503 * skb_segment - Perform protocol segmentation on skb.
3504 * @head_skb: buffer to segment
3505 * @features: features for the output path (see dev->features)
3507 * This function performs segmentation on the given skb. It returns
3508 * a pointer to the first in a list of new skbs for the segments.
3509 * In case of error it returns ERR_PTR(err).
3511 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
3512 netdev_features_t features
)
3514 struct sk_buff
*segs
= NULL
;
3515 struct sk_buff
*tail
= NULL
;
3516 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
3517 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
3518 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
3519 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
3520 struct sk_buff
*frag_skb
= head_skb
;
3521 unsigned int offset
= doffset
;
3522 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
3523 unsigned int partial_segs
= 0;
3524 unsigned int headroom
;
3525 unsigned int len
= head_skb
->len
;
3528 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
3534 __skb_push(head_skb
, doffset
);
3535 proto
= skb_network_protocol(head_skb
, &dummy
);
3536 if (unlikely(!proto
))
3537 return ERR_PTR(-EINVAL
);
3539 sg
= !!(features
& NETIF_F_SG
);
3540 csum
= !!can_checksum_protocol(features
, proto
);
3542 if (sg
&& csum
&& (mss
!= GSO_BY_FRAGS
)) {
3543 if (!(features
& NETIF_F_GSO_PARTIAL
)) {
3544 struct sk_buff
*iter
;
3545 unsigned int frag_len
;
3548 !net_gso_ok(features
, skb_shinfo(head_skb
)->gso_type
))
3551 /* If we get here then all the required
3552 * GSO features except frag_list are supported.
3553 * Try to split the SKB to multiple GSO SKBs
3554 * with no frag_list.
3555 * Currently we can do that only when the buffers don't
3556 * have a linear part and all the buffers except
3557 * the last are of the same length.
3559 frag_len
= list_skb
->len
;
3560 skb_walk_frags(head_skb
, iter
) {
3561 if (frag_len
!= iter
->len
&& iter
->next
)
3563 if (skb_headlen(iter
) && !iter
->head_frag
)
3569 if (len
!= frag_len
)
3573 /* GSO partial only requires that we trim off any excess that
3574 * doesn't fit into an MSS sized block, so take care of that
3577 partial_segs
= len
/ mss
;
3578 if (partial_segs
> 1)
3579 mss
*= partial_segs
;
3585 headroom
= skb_headroom(head_skb
);
3586 pos
= skb_headlen(head_skb
);
3589 struct sk_buff
*nskb
;
3590 skb_frag_t
*nskb_frag
;
3594 if (unlikely(mss
== GSO_BY_FRAGS
)) {
3595 len
= list_skb
->len
;
3597 len
= head_skb
->len
- offset
;
3602 hsize
= skb_headlen(head_skb
) - offset
;
3605 if (hsize
> len
|| !sg
)
3608 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
3609 (skb_headlen(list_skb
) == len
|| sg
)) {
3610 BUG_ON(skb_headlen(list_skb
) > len
);
3613 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3614 frag
= skb_shinfo(list_skb
)->frags
;
3615 frag_skb
= list_skb
;
3616 pos
+= skb_headlen(list_skb
);
3618 while (pos
< offset
+ len
) {
3619 BUG_ON(i
>= nfrags
);
3621 size
= skb_frag_size(frag
);
3622 if (pos
+ size
> offset
+ len
)
3630 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
3631 list_skb
= list_skb
->next
;
3633 if (unlikely(!nskb
))
3636 if (unlikely(pskb_trim(nskb
, len
))) {
3641 hsize
= skb_end_offset(nskb
);
3642 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
3647 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
3648 skb_release_head_state(nskb
);
3649 __skb_push(nskb
, doffset
);
3651 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
3652 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
3655 if (unlikely(!nskb
))
3658 skb_reserve(nskb
, headroom
);
3659 __skb_put(nskb
, doffset
);
3668 __copy_skb_header(nskb
, head_skb
);
3670 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
3671 skb_reset_mac_len(nskb
);
3673 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
3674 nskb
->data
- tnl_hlen
,
3675 doffset
+ tnl_hlen
);
3677 if (nskb
->len
== len
+ doffset
)
3678 goto perform_csum_check
;
3681 if (!nskb
->remcsum_offload
)
3682 nskb
->ip_summed
= CHECKSUM_NONE
;
3683 SKB_GSO_CB(nskb
)->csum
=
3684 skb_copy_and_csum_bits(head_skb
, offset
,
3687 SKB_GSO_CB(nskb
)->csum_start
=
3688 skb_headroom(nskb
) + doffset
;
3692 nskb_frag
= skb_shinfo(nskb
)->frags
;
3694 skb_copy_from_linear_data_offset(head_skb
, offset
,
3695 skb_put(nskb
, hsize
), hsize
);
3697 skb_shinfo(nskb
)->tx_flags
|= skb_shinfo(head_skb
)->tx_flags
&
3700 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3701 skb_zerocopy_clone(nskb
, frag_skb
, GFP_ATOMIC
))
3704 while (pos
< offset
+ len
) {
3707 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3708 frag
= skb_shinfo(list_skb
)->frags
;
3709 frag_skb
= list_skb
;
3710 if (!skb_headlen(list_skb
)) {
3713 BUG_ON(!list_skb
->head_frag
);
3715 /* to make room for head_frag. */
3719 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3720 skb_zerocopy_clone(nskb
, frag_skb
,
3724 list_skb
= list_skb
->next
;
3727 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3729 net_warn_ratelimited(
3730 "skb_segment: too many frags: %u %u\n",
3736 *nskb_frag
= (i
< 0) ? skb_head_frag_to_page_desc(frag_skb
) : *frag
;
3737 __skb_frag_ref(nskb_frag
);
3738 size
= skb_frag_size(nskb_frag
);
3741 nskb_frag
->page_offset
+= offset
- pos
;
3742 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3745 skb_shinfo(nskb
)->nr_frags
++;
3747 if (pos
+ size
<= offset
+ len
) {
3752 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3760 nskb
->data_len
= len
- hsize
;
3761 nskb
->len
+= nskb
->data_len
;
3762 nskb
->truesize
+= nskb
->data_len
;
3766 if (skb_has_shared_frag(nskb
) &&
3767 __skb_linearize(nskb
))
3770 if (!nskb
->remcsum_offload
)
3771 nskb
->ip_summed
= CHECKSUM_NONE
;
3772 SKB_GSO_CB(nskb
)->csum
=
3773 skb_checksum(nskb
, doffset
,
3774 nskb
->len
- doffset
, 0);
3775 SKB_GSO_CB(nskb
)->csum_start
=
3776 skb_headroom(nskb
) + doffset
;
3778 } while ((offset
+= len
) < head_skb
->len
);
3780 /* Some callers want to get the end of the list.
3781 * Put it in segs->prev to avoid walking the list.
3782 * (see validate_xmit_skb_list() for example)
3787 struct sk_buff
*iter
;
3788 int type
= skb_shinfo(head_skb
)->gso_type
;
3789 unsigned short gso_size
= skb_shinfo(head_skb
)->gso_size
;
3791 /* Update type to add partial and then remove dodgy if set */
3792 type
|= (features
& NETIF_F_GSO_PARTIAL
) / NETIF_F_GSO_PARTIAL
* SKB_GSO_PARTIAL
;
3793 type
&= ~SKB_GSO_DODGY
;
3795 /* Update GSO info and prepare to start updating headers on
3796 * our way back down the stack of protocols.
3798 for (iter
= segs
; iter
; iter
= iter
->next
) {
3799 skb_shinfo(iter
)->gso_size
= gso_size
;
3800 skb_shinfo(iter
)->gso_segs
= partial_segs
;
3801 skb_shinfo(iter
)->gso_type
= type
;
3802 SKB_GSO_CB(iter
)->data_offset
= skb_headroom(iter
) + doffset
;
3805 if (tail
->len
- doffset
<= gso_size
)
3806 skb_shinfo(tail
)->gso_size
= 0;
3807 else if (tail
!= segs
)
3808 skb_shinfo(tail
)->gso_segs
= DIV_ROUND_UP(tail
->len
- doffset
, gso_size
);
3811 /* Following permits correct backpressure, for protocols
3812 * using skb_set_owner_w().
3813 * Idea is to tranfert ownership from head_skb to last segment.
3815 if (head_skb
->destructor
== sock_wfree
) {
3816 swap(tail
->truesize
, head_skb
->truesize
);
3817 swap(tail
->destructor
, head_skb
->destructor
);
3818 swap(tail
->sk
, head_skb
->sk
);
3823 kfree_skb_list(segs
);
3824 return ERR_PTR(err
);
3826 EXPORT_SYMBOL_GPL(skb_segment
);
3828 int skb_gro_receive(struct sk_buff
*p
, struct sk_buff
*skb
)
3830 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3831 unsigned int offset
= skb_gro_offset(skb
);
3832 unsigned int headlen
= skb_headlen(skb
);
3833 unsigned int len
= skb_gro_len(skb
);
3834 unsigned int delta_truesize
;
3837 if (unlikely(p
->len
+ len
>= 65536 || NAPI_GRO_CB(skb
)->flush
))
3840 lp
= NAPI_GRO_CB(p
)->last
;
3841 pinfo
= skb_shinfo(lp
);
3843 if (headlen
<= offset
) {
3846 int i
= skbinfo
->nr_frags
;
3847 int nr_frags
= pinfo
->nr_frags
+ i
;
3849 if (nr_frags
> MAX_SKB_FRAGS
)
3853 pinfo
->nr_frags
= nr_frags
;
3854 skbinfo
->nr_frags
= 0;
3856 frag
= pinfo
->frags
+ nr_frags
;
3857 frag2
= skbinfo
->frags
+ i
;
3862 frag
->page_offset
+= offset
;
3863 skb_frag_size_sub(frag
, offset
);
3865 /* all fragments truesize : remove (head size + sk_buff) */
3866 delta_truesize
= skb
->truesize
-
3867 SKB_TRUESIZE(skb_end_offset(skb
));
3869 skb
->truesize
-= skb
->data_len
;
3870 skb
->len
-= skb
->data_len
;
3873 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3875 } else if (skb
->head_frag
) {
3876 int nr_frags
= pinfo
->nr_frags
;
3877 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3878 struct page
*page
= virt_to_head_page(skb
->head
);
3879 unsigned int first_size
= headlen
- offset
;
3880 unsigned int first_offset
;
3882 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3885 first_offset
= skb
->data
-
3886 (unsigned char *)page_address(page
) +
3889 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3891 frag
->page
.p
= page
;
3892 frag
->page_offset
= first_offset
;
3893 skb_frag_size_set(frag
, first_size
);
3895 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3896 /* We dont need to clear skbinfo->nr_frags here */
3898 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3899 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3904 delta_truesize
= skb
->truesize
;
3905 if (offset
> headlen
) {
3906 unsigned int eat
= offset
- headlen
;
3908 skbinfo
->frags
[0].page_offset
+= eat
;
3909 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3910 skb
->data_len
-= eat
;
3915 __skb_pull(skb
, offset
);
3917 if (NAPI_GRO_CB(p
)->last
== p
)
3918 skb_shinfo(p
)->frag_list
= skb
;
3920 NAPI_GRO_CB(p
)->last
->next
= skb
;
3921 NAPI_GRO_CB(p
)->last
= skb
;
3922 __skb_header_release(skb
);
3926 NAPI_GRO_CB(p
)->count
++;
3928 p
->truesize
+= delta_truesize
;
3931 lp
->data_len
+= len
;
3932 lp
->truesize
+= delta_truesize
;
3935 NAPI_GRO_CB(skb
)->same_flow
= 1;
3938 EXPORT_SYMBOL_GPL(skb_gro_receive
);
3940 #ifdef CONFIG_SKB_EXTENSIONS
3941 #define SKB_EXT_ALIGN_VALUE 8
3942 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3944 static const u8 skb_ext_type_len
[] = {
3945 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3946 [SKB_EXT_BRIDGE_NF
] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info
),
3949 [SKB_EXT_SEC_PATH
] = SKB_EXT_CHUNKSIZEOF(struct sec_path
),
3953 static __always_inline
unsigned int skb_ext_total_length(void)
3955 return SKB_EXT_CHUNKSIZEOF(struct skb_ext
) +
3956 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3957 skb_ext_type_len
[SKB_EXT_BRIDGE_NF
] +
3960 skb_ext_type_len
[SKB_EXT_SEC_PATH
] +
3965 static void skb_extensions_init(void)
3967 BUILD_BUG_ON(SKB_EXT_NUM
>= 8);
3968 BUILD_BUG_ON(skb_ext_total_length() > 255);
3970 skbuff_ext_cache
= kmem_cache_create("skbuff_ext_cache",
3971 SKB_EXT_ALIGN_VALUE
* skb_ext_total_length(),
3973 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3977 static void skb_extensions_init(void) {}
3980 void __init
skb_init(void)
3982 skbuff_head_cache
= kmem_cache_create_usercopy("skbuff_head_cache",
3983 sizeof(struct sk_buff
),
3985 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3986 offsetof(struct sk_buff
, cb
),
3987 sizeof_field(struct sk_buff
, cb
),
3989 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3990 sizeof(struct sk_buff_fclones
),
3992 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3994 skb_extensions_init();
3998 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
,
3999 unsigned int recursion_level
)
4001 int start
= skb_headlen(skb
);
4002 int i
, copy
= start
- offset
;
4003 struct sk_buff
*frag_iter
;
4006 if (unlikely(recursion_level
>= 24))
4012 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
4014 if ((len
-= copy
) == 0)
4019 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
4022 WARN_ON(start
> offset
+ len
);
4024 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
4025 if ((copy
= end
- offset
) > 0) {
4026 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
4027 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4032 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
4033 frag
->page_offset
+offset
-start
);
4042 skb_walk_frags(skb
, frag_iter
) {
4045 WARN_ON(start
> offset
+ len
);
4047 end
= start
+ frag_iter
->len
;
4048 if ((copy
= end
- offset
) > 0) {
4049 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4054 ret
= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
4055 copy
, recursion_level
+ 1);
4056 if (unlikely(ret
< 0))
4059 if ((len
-= copy
) == 0)
4070 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4071 * @skb: Socket buffer containing the buffers to be mapped
4072 * @sg: The scatter-gather list to map into
4073 * @offset: The offset into the buffer's contents to start mapping
4074 * @len: Length of buffer space to be mapped
4076 * Fill the specified scatter-gather list with mappings/pointers into a
4077 * region of the buffer space attached to a socket buffer. Returns either
4078 * the number of scatterlist items used, or -EMSGSIZE if the contents
4081 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
4083 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4088 sg_mark_end(&sg
[nsg
- 1]);
4092 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
4094 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4095 * sglist without mark the sg which contain last skb data as the end.
4096 * So the caller can mannipulate sg list as will when padding new data after
4097 * the first call without calling sg_unmark_end to expend sg list.
4099 * Scenario to use skb_to_sgvec_nomark:
4101 * 2. skb_to_sgvec_nomark(payload1)
4102 * 3. skb_to_sgvec_nomark(payload2)
4104 * This is equivalent to:
4106 * 2. skb_to_sgvec(payload1)
4108 * 4. skb_to_sgvec(payload2)
4110 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4111 * is more preferable.
4113 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
4114 int offset
, int len
)
4116 return __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4118 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
4123 * skb_cow_data - Check that a socket buffer's data buffers are writable
4124 * @skb: The socket buffer to check.
4125 * @tailbits: Amount of trailing space to be added
4126 * @trailer: Returned pointer to the skb where the @tailbits space begins
4128 * Make sure that the data buffers attached to a socket buffer are
4129 * writable. If they are not, private copies are made of the data buffers
4130 * and the socket buffer is set to use these instead.
4132 * If @tailbits is given, make sure that there is space to write @tailbits
4133 * bytes of data beyond current end of socket buffer. @trailer will be
4134 * set to point to the skb in which this space begins.
4136 * The number of scatterlist elements required to completely map the
4137 * COW'd and extended socket buffer will be returned.
4139 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
4143 struct sk_buff
*skb1
, **skb_p
;
4145 /* If skb is cloned or its head is paged, reallocate
4146 * head pulling out all the pages (pages are considered not writable
4147 * at the moment even if they are anonymous).
4149 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
4150 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
4153 /* Easy case. Most of packets will go this way. */
4154 if (!skb_has_frag_list(skb
)) {
4155 /* A little of trouble, not enough of space for trailer.
4156 * This should not happen, when stack is tuned to generate
4157 * good frames. OK, on miss we reallocate and reserve even more
4158 * space, 128 bytes is fair. */
4160 if (skb_tailroom(skb
) < tailbits
&&
4161 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
4169 /* Misery. We are in troubles, going to mincer fragments... */
4172 skb_p
= &skb_shinfo(skb
)->frag_list
;
4175 while ((skb1
= *skb_p
) != NULL
) {
4178 /* The fragment is partially pulled by someone,
4179 * this can happen on input. Copy it and everything
4182 if (skb_shared(skb1
))
4185 /* If the skb is the last, worry about trailer. */
4187 if (skb1
->next
== NULL
&& tailbits
) {
4188 if (skb_shinfo(skb1
)->nr_frags
||
4189 skb_has_frag_list(skb1
) ||
4190 skb_tailroom(skb1
) < tailbits
)
4191 ntail
= tailbits
+ 128;
4197 skb_shinfo(skb1
)->nr_frags
||
4198 skb_has_frag_list(skb1
)) {
4199 struct sk_buff
*skb2
;
4201 /* Fuck, we are miserable poor guys... */
4203 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
4205 skb2
= skb_copy_expand(skb1
,
4209 if (unlikely(skb2
== NULL
))
4213 skb_set_owner_w(skb2
, skb1
->sk
);
4215 /* Looking around. Are we still alive?
4216 * OK, link new skb, drop old one */
4218 skb2
->next
= skb1
->next
;
4225 skb_p
= &skb1
->next
;
4230 EXPORT_SYMBOL_GPL(skb_cow_data
);
4232 static void sock_rmem_free(struct sk_buff
*skb
)
4234 struct sock
*sk
= skb
->sk
;
4236 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
4239 static void skb_set_err_queue(struct sk_buff
*skb
)
4241 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4242 * So, it is safe to (mis)use it to mark skbs on the error queue.
4244 skb
->pkt_type
= PACKET_OUTGOING
;
4245 BUILD_BUG_ON(PACKET_OUTGOING
== 0);
4249 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4251 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
4253 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
4254 (unsigned int)sk
->sk_rcvbuf
)
4259 skb
->destructor
= sock_rmem_free
;
4260 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
4261 skb_set_err_queue(skb
);
4263 /* before exiting rcu section, make sure dst is refcounted */
4266 skb_queue_tail(&sk
->sk_error_queue
, skb
);
4267 if (!sock_flag(sk
, SOCK_DEAD
))
4268 sk
->sk_error_report(sk
);
4271 EXPORT_SYMBOL(sock_queue_err_skb
);
4273 static bool is_icmp_err_skb(const struct sk_buff
*skb
)
4275 return skb
&& (SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
4276 SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
);
4279 struct sk_buff
*sock_dequeue_err_skb(struct sock
*sk
)
4281 struct sk_buff_head
*q
= &sk
->sk_error_queue
;
4282 struct sk_buff
*skb
, *skb_next
= NULL
;
4283 bool icmp_next
= false;
4284 unsigned long flags
;
4286 spin_lock_irqsave(&q
->lock
, flags
);
4287 skb
= __skb_dequeue(q
);
4288 if (skb
&& (skb_next
= skb_peek(q
))) {
4289 icmp_next
= is_icmp_err_skb(skb_next
);
4291 sk
->sk_err
= SKB_EXT_ERR(skb_next
)->ee
.ee_origin
;
4293 spin_unlock_irqrestore(&q
->lock
, flags
);
4295 if (is_icmp_err_skb(skb
) && !icmp_next
)
4299 sk
->sk_error_report(sk
);
4303 EXPORT_SYMBOL(sock_dequeue_err_skb
);
4306 * skb_clone_sk - create clone of skb, and take reference to socket
4307 * @skb: the skb to clone
4309 * This function creates a clone of a buffer that holds a reference on
4310 * sk_refcnt. Buffers created via this function are meant to be
4311 * returned using sock_queue_err_skb, or free via kfree_skb.
4313 * When passing buffers allocated with this function to sock_queue_err_skb
4314 * it is necessary to wrap the call with sock_hold/sock_put in order to
4315 * prevent the socket from being released prior to being enqueued on
4316 * the sk_error_queue.
4318 struct sk_buff
*skb_clone_sk(struct sk_buff
*skb
)
4320 struct sock
*sk
= skb
->sk
;
4321 struct sk_buff
*clone
;
4323 if (!sk
|| !refcount_inc_not_zero(&sk
->sk_refcnt
))
4326 clone
= skb_clone(skb
, GFP_ATOMIC
);
4333 clone
->destructor
= sock_efree
;
4337 EXPORT_SYMBOL(skb_clone_sk
);
4339 static void __skb_complete_tx_timestamp(struct sk_buff
*skb
,
4344 struct sock_exterr_skb
*serr
;
4347 BUILD_BUG_ON(sizeof(struct sock_exterr_skb
) > sizeof(skb
->cb
));
4349 serr
= SKB_EXT_ERR(skb
);
4350 memset(serr
, 0, sizeof(*serr
));
4351 serr
->ee
.ee_errno
= ENOMSG
;
4352 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
4353 serr
->ee
.ee_info
= tstype
;
4354 serr
->opt_stats
= opt_stats
;
4355 serr
->header
.h4
.iif
= skb
->dev
? skb
->dev
->ifindex
: 0;
4356 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
4357 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
4358 if (sk
->sk_protocol
== IPPROTO_TCP
&&
4359 sk
->sk_type
== SOCK_STREAM
)
4360 serr
->ee
.ee_data
-= sk
->sk_tskey
;
4363 err
= sock_queue_err_skb(sk
, skb
);
4369 static bool skb_may_tx_timestamp(struct sock
*sk
, bool tsonly
)
4373 if (likely(sysctl_tstamp_allow_data
|| tsonly
))
4376 read_lock_bh(&sk
->sk_callback_lock
);
4377 ret
= sk
->sk_socket
&& sk
->sk_socket
->file
&&
4378 file_ns_capable(sk
->sk_socket
->file
, &init_user_ns
, CAP_NET_RAW
);
4379 read_unlock_bh(&sk
->sk_callback_lock
);
4383 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
4384 struct skb_shared_hwtstamps
*hwtstamps
)
4386 struct sock
*sk
= skb
->sk
;
4388 if (!skb_may_tx_timestamp(sk
, false))
4391 /* Take a reference to prevent skb_orphan() from freeing the socket,
4392 * but only if the socket refcount is not zero.
4394 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4395 *skb_hwtstamps(skb
) = *hwtstamps
;
4396 __skb_complete_tx_timestamp(skb
, sk
, SCM_TSTAMP_SND
, false);
4404 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp
);
4406 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
4407 struct skb_shared_hwtstamps
*hwtstamps
,
4408 struct sock
*sk
, int tstype
)
4410 struct sk_buff
*skb
;
4411 bool tsonly
, opt_stats
= false;
4416 if (!hwtstamps
&& !(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TX_SWHW
) &&
4417 skb_shinfo(orig_skb
)->tx_flags
& SKBTX_IN_PROGRESS
)
4420 tsonly
= sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TSONLY
;
4421 if (!skb_may_tx_timestamp(sk
, tsonly
))
4426 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_STATS
) &&
4427 sk
->sk_protocol
== IPPROTO_TCP
&&
4428 sk
->sk_type
== SOCK_STREAM
) {
4429 skb
= tcp_get_timestamping_opt_stats(sk
);
4433 skb
= alloc_skb(0, GFP_ATOMIC
);
4435 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
4441 skb_shinfo(skb
)->tx_flags
|= skb_shinfo(orig_skb
)->tx_flags
&
4443 skb_shinfo(skb
)->tskey
= skb_shinfo(orig_skb
)->tskey
;
4447 *skb_hwtstamps(skb
) = *hwtstamps
;
4449 skb
->tstamp
= ktime_get_real();
4451 __skb_complete_tx_timestamp(skb
, sk
, tstype
, opt_stats
);
4453 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
4455 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
4456 struct skb_shared_hwtstamps
*hwtstamps
)
4458 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
4461 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
4463 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
4465 struct sock
*sk
= skb
->sk
;
4466 struct sock_exterr_skb
*serr
;
4469 skb
->wifi_acked_valid
= 1;
4470 skb
->wifi_acked
= acked
;
4472 serr
= SKB_EXT_ERR(skb
);
4473 memset(serr
, 0, sizeof(*serr
));
4474 serr
->ee
.ee_errno
= ENOMSG
;
4475 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
4477 /* Take a reference to prevent skb_orphan() from freeing the socket,
4478 * but only if the socket refcount is not zero.
4480 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4481 err
= sock_queue_err_skb(sk
, skb
);
4487 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
4490 * skb_partial_csum_set - set up and verify partial csum values for packet
4491 * @skb: the skb to set
4492 * @start: the number of bytes after skb->data to start checksumming.
4493 * @off: the offset from start to place the checksum.
4495 * For untrusted partially-checksummed packets, we need to make sure the values
4496 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4498 * This function checks and sets those values and skb->ip_summed: if this
4499 * returns false you should drop the packet.
4501 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
4503 u32 csum_end
= (u32
)start
+ (u32
)off
+ sizeof(__sum16
);
4504 u32 csum_start
= skb_headroom(skb
) + (u32
)start
;
4506 if (unlikely(csum_start
> U16_MAX
|| csum_end
> skb_headlen(skb
))) {
4507 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4508 start
, off
, skb_headroom(skb
), skb_headlen(skb
));
4511 skb
->ip_summed
= CHECKSUM_PARTIAL
;
4512 skb
->csum_start
= csum_start
;
4513 skb
->csum_offset
= off
;
4514 skb_set_transport_header(skb
, start
);
4517 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
4519 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
4522 if (skb_headlen(skb
) >= len
)
4525 /* If we need to pullup then pullup to the max, so we
4526 * won't need to do it again.
4531 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
4534 if (skb_headlen(skb
) < len
)
4540 #define MAX_TCP_HDR_LEN (15 * 4)
4542 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
4543 typeof(IPPROTO_IP
) proto
,
4550 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
4551 off
+ MAX_TCP_HDR_LEN
);
4552 if (!err
&& !skb_partial_csum_set(skb
, off
,
4553 offsetof(struct tcphdr
,
4556 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
4559 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
4560 off
+ sizeof(struct udphdr
));
4561 if (!err
&& !skb_partial_csum_set(skb
, off
,
4562 offsetof(struct udphdr
,
4565 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
4568 return ERR_PTR(-EPROTO
);
4571 /* This value should be large enough to cover a tagged ethernet header plus
4572 * maximally sized IP and TCP or UDP headers.
4574 #define MAX_IP_HDR_LEN 128
4576 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
4585 err
= skb_maybe_pull_tail(skb
,
4586 sizeof(struct iphdr
),
4591 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
4594 off
= ip_hdrlen(skb
);
4601 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
4603 return PTR_ERR(csum
);
4606 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
4609 ip_hdr(skb
)->protocol
, 0);
4616 /* This value should be large enough to cover a tagged ethernet header plus
4617 * an IPv6 header, all options, and a maximal TCP or UDP header.
4619 #define MAX_IPV6_HDR_LEN 256
4621 #define OPT_HDR(type, skb, off) \
4622 (type *)(skb_network_header(skb) + (off))
4624 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
4637 off
= sizeof(struct ipv6hdr
);
4639 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
4643 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
4645 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
4646 while (off
<= len
&& !done
) {
4648 case IPPROTO_DSTOPTS
:
4649 case IPPROTO_HOPOPTS
:
4650 case IPPROTO_ROUTING
: {
4651 struct ipv6_opt_hdr
*hp
;
4653 err
= skb_maybe_pull_tail(skb
,
4655 sizeof(struct ipv6_opt_hdr
),
4660 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
4661 nexthdr
= hp
->nexthdr
;
4662 off
+= ipv6_optlen(hp
);
4666 struct ip_auth_hdr
*hp
;
4668 err
= skb_maybe_pull_tail(skb
,
4670 sizeof(struct ip_auth_hdr
),
4675 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
4676 nexthdr
= hp
->nexthdr
;
4677 off
+= ipv6_authlen(hp
);
4680 case IPPROTO_FRAGMENT
: {
4681 struct frag_hdr
*hp
;
4683 err
= skb_maybe_pull_tail(skb
,
4685 sizeof(struct frag_hdr
),
4690 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
4692 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
4695 nexthdr
= hp
->nexthdr
;
4696 off
+= sizeof(struct frag_hdr
);
4707 if (!done
|| fragment
)
4710 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
4712 return PTR_ERR(csum
);
4715 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
4716 &ipv6_hdr(skb
)->daddr
,
4717 skb
->len
- off
, nexthdr
, 0);
4725 * skb_checksum_setup - set up partial checksum offset
4726 * @skb: the skb to set up
4727 * @recalculate: if true the pseudo-header checksum will be recalculated
4729 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
4733 switch (skb
->protocol
) {
4734 case htons(ETH_P_IP
):
4735 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
4738 case htons(ETH_P_IPV6
):
4739 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
4749 EXPORT_SYMBOL(skb_checksum_setup
);
4752 * skb_checksum_maybe_trim - maybe trims the given skb
4753 * @skb: the skb to check
4754 * @transport_len: the data length beyond the network header
4756 * Checks whether the given skb has data beyond the given transport length.
4757 * If so, returns a cloned skb trimmed to this transport length.
4758 * Otherwise returns the provided skb. Returns NULL in error cases
4759 * (e.g. transport_len exceeds skb length or out-of-memory).
4761 * Caller needs to set the skb transport header and free any returned skb if it
4762 * differs from the provided skb.
4764 static struct sk_buff
*skb_checksum_maybe_trim(struct sk_buff
*skb
,
4765 unsigned int transport_len
)
4767 struct sk_buff
*skb_chk
;
4768 unsigned int len
= skb_transport_offset(skb
) + transport_len
;
4773 else if (skb
->len
== len
)
4776 skb_chk
= skb_clone(skb
, GFP_ATOMIC
);
4780 ret
= pskb_trim_rcsum(skb_chk
, len
);
4790 * skb_checksum_trimmed - validate checksum of an skb
4791 * @skb: the skb to check
4792 * @transport_len: the data length beyond the network header
4793 * @skb_chkf: checksum function to use
4795 * Applies the given checksum function skb_chkf to the provided skb.
4796 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4798 * If the skb has data beyond the given transport length, then a
4799 * trimmed & cloned skb is checked and returned.
4801 * Caller needs to set the skb transport header and free any returned skb if it
4802 * differs from the provided skb.
4804 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
4805 unsigned int transport_len
,
4806 __sum16(*skb_chkf
)(struct sk_buff
*skb
))
4808 struct sk_buff
*skb_chk
;
4809 unsigned int offset
= skb_transport_offset(skb
);
4812 skb_chk
= skb_checksum_maybe_trim(skb
, transport_len
);
4816 if (!pskb_may_pull(skb_chk
, offset
))
4819 skb_pull_rcsum(skb_chk
, offset
);
4820 ret
= skb_chkf(skb_chk
);
4821 skb_push_rcsum(skb_chk
, offset
);
4829 if (skb_chk
&& skb_chk
!= skb
)
4835 EXPORT_SYMBOL(skb_checksum_trimmed
);
4837 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
4839 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4842 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
4844 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
4847 skb_release_head_state(skb
);
4848 kmem_cache_free(skbuff_head_cache
, skb
);
4853 EXPORT_SYMBOL(kfree_skb_partial
);
4856 * skb_try_coalesce - try to merge skb to prior one
4858 * @from: buffer to add
4859 * @fragstolen: pointer to boolean
4860 * @delta_truesize: how much more was allocated than was requested
4862 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
4863 bool *fragstolen
, int *delta_truesize
)
4865 struct skb_shared_info
*to_shinfo
, *from_shinfo
;
4866 int i
, delta
, len
= from
->len
;
4868 *fragstolen
= false;
4873 if (len
<= skb_tailroom(to
)) {
4875 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
4876 *delta_truesize
= 0;
4880 to_shinfo
= skb_shinfo(to
);
4881 from_shinfo
= skb_shinfo(from
);
4882 if (to_shinfo
->frag_list
|| from_shinfo
->frag_list
)
4884 if (skb_zcopy(to
) || skb_zcopy(from
))
4887 if (skb_headlen(from
) != 0) {
4889 unsigned int offset
;
4891 if (to_shinfo
->nr_frags
+
4892 from_shinfo
->nr_frags
>= MAX_SKB_FRAGS
)
4895 if (skb_head_is_locked(from
))
4898 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
4900 page
= virt_to_head_page(from
->head
);
4901 offset
= from
->data
- (unsigned char *)page_address(page
);
4903 skb_fill_page_desc(to
, to_shinfo
->nr_frags
,
4904 page
, offset
, skb_headlen(from
));
4907 if (to_shinfo
->nr_frags
+
4908 from_shinfo
->nr_frags
> MAX_SKB_FRAGS
)
4911 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
4914 WARN_ON_ONCE(delta
< len
);
4916 memcpy(to_shinfo
->frags
+ to_shinfo
->nr_frags
,
4918 from_shinfo
->nr_frags
* sizeof(skb_frag_t
));
4919 to_shinfo
->nr_frags
+= from_shinfo
->nr_frags
;
4921 if (!skb_cloned(from
))
4922 from_shinfo
->nr_frags
= 0;
4924 /* if the skb is not cloned this does nothing
4925 * since we set nr_frags to 0.
4927 for (i
= 0; i
< from_shinfo
->nr_frags
; i
++)
4928 __skb_frag_ref(&from_shinfo
->frags
[i
]);
4930 to
->truesize
+= delta
;
4932 to
->data_len
+= len
;
4934 *delta_truesize
= delta
;
4937 EXPORT_SYMBOL(skb_try_coalesce
);
4940 * skb_scrub_packet - scrub an skb
4942 * @skb: buffer to clean
4943 * @xnet: packet is crossing netns
4945 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4946 * into/from a tunnel. Some information have to be cleared during these
4948 * skb_scrub_packet can also be used to clean a skb before injecting it in
4949 * another namespace (@xnet == true). We have to clear all information in the
4950 * skb that could impact namespace isolation.
4952 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
4954 skb
->pkt_type
= PACKET_HOST
;
4960 nf_reset_trace(skb
);
4962 #ifdef CONFIG_NET_SWITCHDEV
4963 skb
->offload_fwd_mark
= 0;
4964 skb
->offload_l3_fwd_mark
= 0;
4974 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
4977 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4981 * skb_gso_transport_seglen is used to determine the real size of the
4982 * individual segments, including Layer4 headers (TCP/UDP).
4984 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4986 static unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
4988 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
4989 unsigned int thlen
= 0;
4991 if (skb
->encapsulation
) {
4992 thlen
= skb_inner_transport_header(skb
) -
4993 skb_transport_header(skb
);
4995 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
4996 thlen
+= inner_tcp_hdrlen(skb
);
4997 } else if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
4998 thlen
= tcp_hdrlen(skb
);
4999 } else if (unlikely(skb_is_gso_sctp(skb
))) {
5000 thlen
= sizeof(struct sctphdr
);
5001 } else if (shinfo
->gso_type
& SKB_GSO_UDP_L4
) {
5002 thlen
= sizeof(struct udphdr
);
5004 /* UFO sets gso_size to the size of the fragmentation
5005 * payload, i.e. the size of the L4 (UDP) header is already
5008 return thlen
+ shinfo
->gso_size
;
5012 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5016 * skb_gso_network_seglen is used to determine the real size of the
5017 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5019 * The MAC/L2 header is not accounted for.
5021 static unsigned int skb_gso_network_seglen(const struct sk_buff
*skb
)
5023 unsigned int hdr_len
= skb_transport_header(skb
) -
5024 skb_network_header(skb
);
5026 return hdr_len
+ skb_gso_transport_seglen(skb
);
5030 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5034 * skb_gso_mac_seglen is used to determine the real size of the
5035 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5036 * headers (TCP/UDP).
5038 static unsigned int skb_gso_mac_seglen(const struct sk_buff
*skb
)
5040 unsigned int hdr_len
= skb_transport_header(skb
) - skb_mac_header(skb
);
5042 return hdr_len
+ skb_gso_transport_seglen(skb
);
5046 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5048 * There are a couple of instances where we have a GSO skb, and we
5049 * want to determine what size it would be after it is segmented.
5051 * We might want to check:
5052 * - L3+L4+payload size (e.g. IP forwarding)
5053 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5055 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5059 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5060 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5062 * @max_len: The maximum permissible length.
5064 * Returns true if the segmented length <= max length.
5066 static inline bool skb_gso_size_check(const struct sk_buff
*skb
,
5067 unsigned int seg_len
,
5068 unsigned int max_len
) {
5069 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
5070 const struct sk_buff
*iter
;
5072 if (shinfo
->gso_size
!= GSO_BY_FRAGS
)
5073 return seg_len
<= max_len
;
5075 /* Undo this so we can re-use header sizes */
5076 seg_len
-= GSO_BY_FRAGS
;
5078 skb_walk_frags(skb
, iter
) {
5079 if (seg_len
+ skb_headlen(iter
) > max_len
)
5087 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5090 * @mtu: MTU to validate against
5092 * skb_gso_validate_network_len validates if a given skb will fit a
5093 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5096 bool skb_gso_validate_network_len(const struct sk_buff
*skb
, unsigned int mtu
)
5098 return skb_gso_size_check(skb
, skb_gso_network_seglen(skb
), mtu
);
5100 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len
);
5103 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5106 * @len: length to validate against
5108 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5109 * length once split, including L2, L3 and L4 headers and the payload.
5111 bool skb_gso_validate_mac_len(const struct sk_buff
*skb
, unsigned int len
)
5113 return skb_gso_size_check(skb
, skb_gso_mac_seglen(skb
), len
);
5115 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len
);
5117 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
5119 int mac_len
, meta_len
;
5122 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
5127 mac_len
= skb
->data
- skb_mac_header(skb
);
5128 if (likely(mac_len
> VLAN_HLEN
+ ETH_TLEN
)) {
5129 memmove(skb_mac_header(skb
) + VLAN_HLEN
, skb_mac_header(skb
),
5130 mac_len
- VLAN_HLEN
- ETH_TLEN
);
5133 meta_len
= skb_metadata_len(skb
);
5135 meta
= skb_metadata_end(skb
) - meta_len
;
5136 memmove(meta
+ VLAN_HLEN
, meta
, meta_len
);
5139 skb
->mac_header
+= VLAN_HLEN
;
5143 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
5145 struct vlan_hdr
*vhdr
;
5148 if (unlikely(skb_vlan_tag_present(skb
))) {
5149 /* vlan_tci is already set-up so leave this for another time */
5153 skb
= skb_share_check(skb
, GFP_ATOMIC
);
5157 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
5160 vhdr
= (struct vlan_hdr
*)skb
->data
;
5161 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5162 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
5164 skb_pull_rcsum(skb
, VLAN_HLEN
);
5165 vlan_set_encap_proto(skb
, vhdr
);
5167 skb
= skb_reorder_vlan_header(skb
);
5171 skb_reset_network_header(skb
);
5172 skb_reset_transport_header(skb
);
5173 skb_reset_mac_len(skb
);
5181 EXPORT_SYMBOL(skb_vlan_untag
);
5183 int skb_ensure_writable(struct sk_buff
*skb
, int write_len
)
5185 if (!pskb_may_pull(skb
, write_len
))
5188 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
5191 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
5193 EXPORT_SYMBOL(skb_ensure_writable
);
5195 /* remove VLAN header from packet and update csum accordingly.
5196 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5198 int __skb_vlan_pop(struct sk_buff
*skb
, u16
*vlan_tci
)
5200 struct vlan_hdr
*vhdr
;
5201 int offset
= skb
->data
- skb_mac_header(skb
);
5204 if (WARN_ONCE(offset
,
5205 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5210 err
= skb_ensure_writable(skb
, VLAN_ETH_HLEN
);
5214 skb_postpull_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5216 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
5217 *vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5219 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
5220 __skb_pull(skb
, VLAN_HLEN
);
5222 vlan_set_encap_proto(skb
, vhdr
);
5223 skb
->mac_header
+= VLAN_HLEN
;
5225 if (skb_network_offset(skb
) < ETH_HLEN
)
5226 skb_set_network_header(skb
, ETH_HLEN
);
5228 skb_reset_mac_len(skb
);
5232 EXPORT_SYMBOL(__skb_vlan_pop
);
5234 /* Pop a vlan tag either from hwaccel or from payload.
5235 * Expects skb->data at mac header.
5237 int skb_vlan_pop(struct sk_buff
*skb
)
5243 if (likely(skb_vlan_tag_present(skb
))) {
5244 __vlan_hwaccel_clear_tag(skb
);
5246 if (unlikely(!eth_type_vlan(skb
->protocol
)))
5249 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5253 /* move next vlan tag to hw accel tag */
5254 if (likely(!eth_type_vlan(skb
->protocol
)))
5257 vlan_proto
= skb
->protocol
;
5258 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5262 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5265 EXPORT_SYMBOL(skb_vlan_pop
);
5267 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5268 * Expects skb->data at mac header.
5270 int skb_vlan_push(struct sk_buff
*skb
, __be16 vlan_proto
, u16 vlan_tci
)
5272 if (skb_vlan_tag_present(skb
)) {
5273 int offset
= skb
->data
- skb_mac_header(skb
);
5276 if (WARN_ONCE(offset
,
5277 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5282 err
= __vlan_insert_tag(skb
, skb
->vlan_proto
,
5283 skb_vlan_tag_get(skb
));
5287 skb
->protocol
= skb
->vlan_proto
;
5288 skb
->mac_len
+= VLAN_HLEN
;
5290 skb_postpush_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5292 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5295 EXPORT_SYMBOL(skb_vlan_push
);
5298 * alloc_skb_with_frags - allocate skb with page frags
5300 * @header_len: size of linear part
5301 * @data_len: needed length in frags
5302 * @max_page_order: max page order desired.
5303 * @errcode: pointer to error code if any
5304 * @gfp_mask: allocation mask
5306 * This can be used to allocate a paged skb, given a maximal order for frags.
5308 struct sk_buff
*alloc_skb_with_frags(unsigned long header_len
,
5309 unsigned long data_len
,
5314 int npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
5315 unsigned long chunk
;
5316 struct sk_buff
*skb
;
5320 *errcode
= -EMSGSIZE
;
5321 /* Note this test could be relaxed, if we succeed to allocate
5322 * high order pages...
5324 if (npages
> MAX_SKB_FRAGS
)
5327 *errcode
= -ENOBUFS
;
5328 skb
= alloc_skb(header_len
, gfp_mask
);
5332 skb
->truesize
+= npages
<< PAGE_SHIFT
;
5334 for (i
= 0; npages
> 0; i
++) {
5335 int order
= max_page_order
;
5338 if (npages
>= 1 << order
) {
5339 page
= alloc_pages((gfp_mask
& ~__GFP_DIRECT_RECLAIM
) |
5345 /* Do not retry other high order allocations */
5351 page
= alloc_page(gfp_mask
);
5355 chunk
= min_t(unsigned long, data_len
,
5356 PAGE_SIZE
<< order
);
5357 skb_fill_page_desc(skb
, i
, page
, 0, chunk
);
5359 npages
-= 1 << order
;
5367 EXPORT_SYMBOL(alloc_skb_with_frags
);
5369 /* carve out the first off bytes from skb when off < headlen */
5370 static int pskb_carve_inside_header(struct sk_buff
*skb
, const u32 off
,
5371 const int headlen
, gfp_t gfp_mask
)
5374 int size
= skb_end_offset(skb
);
5375 int new_hlen
= headlen
- off
;
5378 size
= SKB_DATA_ALIGN(size
);
5380 if (skb_pfmemalloc(skb
))
5381 gfp_mask
|= __GFP_MEMALLOC
;
5382 data
= kmalloc_reserve(size
+
5383 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5384 gfp_mask
, NUMA_NO_NODE
, NULL
);
5388 size
= SKB_WITH_OVERHEAD(ksize(data
));
5390 /* Copy real data, and all frags */
5391 skb_copy_from_linear_data_offset(skb
, off
, data
, new_hlen
);
5394 memcpy((struct skb_shared_info
*)(data
+ size
),
5396 offsetof(struct skb_shared_info
,
5397 frags
[skb_shinfo(skb
)->nr_frags
]));
5398 if (skb_cloned(skb
)) {
5399 /* drop the old head gracefully */
5400 if (skb_orphan_frags(skb
, gfp_mask
)) {
5404 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
5405 skb_frag_ref(skb
, i
);
5406 if (skb_has_frag_list(skb
))
5407 skb_clone_fraglist(skb
);
5408 skb_release_data(skb
);
5410 /* we can reuse existing recount- all we did was
5419 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5422 skb
->end
= skb
->head
+ size
;
5424 skb_set_tail_pointer(skb
, skb_headlen(skb
));
5425 skb_headers_offset_update(skb
, 0);
5429 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5434 static int pskb_carve(struct sk_buff
*skb
, const u32 off
, gfp_t gfp
);
5436 /* carve out the first eat bytes from skb's frag_list. May recurse into
5439 static int pskb_carve_frag_list(struct sk_buff
*skb
,
5440 struct skb_shared_info
*shinfo
, int eat
,
5443 struct sk_buff
*list
= shinfo
->frag_list
;
5444 struct sk_buff
*clone
= NULL
;
5445 struct sk_buff
*insp
= NULL
;
5449 pr_err("Not enough bytes to eat. Want %d\n", eat
);
5452 if (list
->len
<= eat
) {
5453 /* Eaten as whole. */
5458 /* Eaten partially. */
5459 if (skb_shared(list
)) {
5460 clone
= skb_clone(list
, gfp_mask
);
5466 /* This may be pulled without problems. */
5469 if (pskb_carve(list
, eat
, gfp_mask
) < 0) {
5477 /* Free pulled out fragments. */
5478 while ((list
= shinfo
->frag_list
) != insp
) {
5479 shinfo
->frag_list
= list
->next
;
5482 /* And insert new clone at head. */
5485 shinfo
->frag_list
= clone
;
5490 /* carve off first len bytes from skb. Split line (off) is in the
5491 * non-linear part of skb
5493 static int pskb_carve_inside_nonlinear(struct sk_buff
*skb
, const u32 off
,
5494 int pos
, gfp_t gfp_mask
)
5497 int size
= skb_end_offset(skb
);
5499 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
5500 struct skb_shared_info
*shinfo
;
5502 size
= SKB_DATA_ALIGN(size
);
5504 if (skb_pfmemalloc(skb
))
5505 gfp_mask
|= __GFP_MEMALLOC
;
5506 data
= kmalloc_reserve(size
+
5507 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5508 gfp_mask
, NUMA_NO_NODE
, NULL
);
5512 size
= SKB_WITH_OVERHEAD(ksize(data
));
5514 memcpy((struct skb_shared_info
*)(data
+ size
),
5515 skb_shinfo(skb
), offsetof(struct skb_shared_info
,
5516 frags
[skb_shinfo(skb
)->nr_frags
]));
5517 if (skb_orphan_frags(skb
, gfp_mask
)) {
5521 shinfo
= (struct skb_shared_info
*)(data
+ size
);
5522 for (i
= 0; i
< nfrags
; i
++) {
5523 int fsize
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
5525 if (pos
+ fsize
> off
) {
5526 shinfo
->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
5530 * We have two variants in this case:
5531 * 1. Move all the frag to the second
5532 * part, if it is possible. F.e.
5533 * this approach is mandatory for TUX,
5534 * where splitting is expensive.
5535 * 2. Split is accurately. We make this.
5537 shinfo
->frags
[0].page_offset
+= off
- pos
;
5538 skb_frag_size_sub(&shinfo
->frags
[0], off
- pos
);
5540 skb_frag_ref(skb
, i
);
5545 shinfo
->nr_frags
= k
;
5546 if (skb_has_frag_list(skb
))
5547 skb_clone_fraglist(skb
);
5550 /* split line is in frag list */
5551 pskb_carve_frag_list(skb
, shinfo
, off
- pos
, gfp_mask
);
5553 skb_release_data(skb
);
5558 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5561 skb
->end
= skb
->head
+ size
;
5563 skb_reset_tail_pointer(skb
);
5564 skb_headers_offset_update(skb
, 0);
5569 skb
->data_len
= skb
->len
;
5570 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5574 /* remove len bytes from the beginning of the skb */
5575 static int pskb_carve(struct sk_buff
*skb
, const u32 len
, gfp_t gfp
)
5577 int headlen
= skb_headlen(skb
);
5580 return pskb_carve_inside_header(skb
, len
, headlen
, gfp
);
5582 return pskb_carve_inside_nonlinear(skb
, len
, headlen
, gfp
);
5585 /* Extract to_copy bytes starting at off from skb, and return this in
5588 struct sk_buff
*pskb_extract(struct sk_buff
*skb
, int off
,
5589 int to_copy
, gfp_t gfp
)
5591 struct sk_buff
*clone
= skb_clone(skb
, gfp
);
5596 if (pskb_carve(clone
, off
, gfp
) < 0 ||
5597 pskb_trim(clone
, to_copy
)) {
5603 EXPORT_SYMBOL(pskb_extract
);
5606 * skb_condense - try to get rid of fragments/frag_list if possible
5609 * Can be used to save memory before skb is added to a busy queue.
5610 * If packet has bytes in frags and enough tail room in skb->head,
5611 * pull all of them, so that we can free the frags right now and adjust
5614 * We do not reallocate skb->head thus can not fail.
5615 * Caller must re-evaluate skb->truesize if needed.
5617 void skb_condense(struct sk_buff
*skb
)
5619 if (skb
->data_len
) {
5620 if (skb
->data_len
> skb
->end
- skb
->tail
||
5624 /* Nice, we can free page frag(s) right now */
5625 __pskb_pull_tail(skb
, skb
->data_len
);
5627 /* At this point, skb->truesize might be over estimated,
5628 * because skb had a fragment, and fragments do not tell
5630 * When we pulled its content into skb->head, fragment
5631 * was freed, but __pskb_pull_tail() could not possibly
5632 * adjust skb->truesize, not knowing the frag truesize.
5634 skb
->truesize
= SKB_TRUESIZE(skb_end_offset(skb
));
5637 #ifdef CONFIG_SKB_EXTENSIONS
5638 static void *skb_ext_get_ptr(struct skb_ext
*ext
, enum skb_ext_id id
)
5640 return (void *)ext
+ (ext
->offset
[id
] * SKB_EXT_ALIGN_VALUE
);
5643 static struct skb_ext
*skb_ext_alloc(void)
5645 struct skb_ext
*new = kmem_cache_alloc(skbuff_ext_cache
, GFP_ATOMIC
);
5648 memset(new->offset
, 0, sizeof(new->offset
));
5649 refcount_set(&new->refcnt
, 1);
5655 static struct skb_ext
*skb_ext_maybe_cow(struct skb_ext
*old
,
5656 unsigned int old_active
)
5658 struct skb_ext
*new;
5660 if (refcount_read(&old
->refcnt
) == 1)
5663 new = kmem_cache_alloc(skbuff_ext_cache
, GFP_ATOMIC
);
5667 memcpy(new, old
, old
->chunks
* SKB_EXT_ALIGN_VALUE
);
5668 refcount_set(&new->refcnt
, 1);
5671 if (old_active
& (1 << SKB_EXT_SEC_PATH
)) {
5672 struct sec_path
*sp
= skb_ext_get_ptr(old
, SKB_EXT_SEC_PATH
);
5675 for (i
= 0; i
< sp
->len
; i
++)
5676 xfrm_state_hold(sp
->xvec
[i
]);
5684 * skb_ext_add - allocate space for given extension, COW if needed
5686 * @id: extension to allocate space for
5688 * Allocates enough space for the given extension.
5689 * If the extension is already present, a pointer to that extension
5692 * If the skb was cloned, COW applies and the returned memory can be
5693 * modified without changing the extension space of clones buffers.
5695 * Returns pointer to the extension or NULL on allocation failure.
5697 void *skb_ext_add(struct sk_buff
*skb
, enum skb_ext_id id
)
5699 struct skb_ext
*new, *old
= NULL
;
5700 unsigned int newlen
, newoff
;
5702 if (skb
->active_extensions
) {
5703 old
= skb
->extensions
;
5705 new = skb_ext_maybe_cow(old
, skb
->active_extensions
);
5709 if (__skb_ext_exist(new, id
))
5712 newoff
= new->chunks
;
5714 newoff
= SKB_EXT_CHUNKSIZEOF(*new);
5716 new = skb_ext_alloc();
5721 newlen
= newoff
+ skb_ext_type_len
[id
];
5722 new->chunks
= newlen
;
5723 new->offset
[id
] = newoff
;
5725 skb
->extensions
= new;
5726 skb
->active_extensions
|= 1 << id
;
5727 return skb_ext_get_ptr(new, id
);
5729 EXPORT_SYMBOL(skb_ext_add
);
5732 static void skb_ext_put_sp(struct sec_path
*sp
)
5736 for (i
= 0; i
< sp
->len
; i
++)
5737 xfrm_state_put(sp
->xvec
[i
]);
5741 void __skb_ext_del(struct sk_buff
*skb
, enum skb_ext_id id
)
5743 struct skb_ext
*ext
= skb
->extensions
;
5745 skb
->active_extensions
&= ~(1 << id
);
5746 if (skb
->active_extensions
== 0) {
5747 skb
->extensions
= NULL
;
5750 } else if (id
== SKB_EXT_SEC_PATH
&&
5751 refcount_read(&ext
->refcnt
) == 1) {
5752 struct sec_path
*sp
= skb_ext_get_ptr(ext
, SKB_EXT_SEC_PATH
);
5759 EXPORT_SYMBOL(__skb_ext_del
);
5761 void __skb_ext_put(struct skb_ext
*ext
)
5763 /* If this is last clone, nothing can increment
5764 * it after check passes. Avoids one atomic op.
5766 if (refcount_read(&ext
->refcnt
) == 1)
5769 if (!refcount_dec_and_test(&ext
->refcnt
))
5773 if (__skb_ext_exist(ext
, SKB_EXT_SEC_PATH
))
5774 skb_ext_put_sp(skb_ext_get_ptr(ext
, SKB_EXT_SEC_PATH
));
5777 kmem_cache_free(skbuff_ext_cache
, ext
);
5779 EXPORT_SYMBOL(__skb_ext_put
);
5780 #endif /* CONFIG_SKB_EXTENSIONS */