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
);
1035 sock_zerocopy_get(uarg
);
1041 return sock_zerocopy_alloc(sk
, size
);
1043 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc
);
1045 static bool skb_zerocopy_notify_extend(struct sk_buff
*skb
, u32 lo
, u16 len
)
1047 struct sock_exterr_skb
*serr
= SKB_EXT_ERR(skb
);
1051 old_lo
= serr
->ee
.ee_info
;
1052 old_hi
= serr
->ee
.ee_data
;
1053 sum_len
= old_hi
- old_lo
+ 1ULL + len
;
1055 if (sum_len
>= (1ULL << 32))
1058 if (lo
!= old_hi
+ 1)
1061 serr
->ee
.ee_data
+= len
;
1065 void sock_zerocopy_callback(struct ubuf_info
*uarg
, bool success
)
1067 struct sk_buff
*tail
, *skb
= skb_from_uarg(uarg
);
1068 struct sock_exterr_skb
*serr
;
1069 struct sock
*sk
= skb
->sk
;
1070 struct sk_buff_head
*q
;
1071 unsigned long flags
;
1075 mm_unaccount_pinned_pages(&uarg
->mmp
);
1077 /* if !len, there was only 1 call, and it was aborted
1078 * so do not queue a completion notification
1080 if (!uarg
->len
|| sock_flag(sk
, SOCK_DEAD
))
1085 hi
= uarg
->id
+ len
- 1;
1087 serr
= SKB_EXT_ERR(skb
);
1088 memset(serr
, 0, sizeof(*serr
));
1089 serr
->ee
.ee_errno
= 0;
1090 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ZEROCOPY
;
1091 serr
->ee
.ee_data
= hi
;
1092 serr
->ee
.ee_info
= lo
;
1094 serr
->ee
.ee_code
|= SO_EE_CODE_ZEROCOPY_COPIED
;
1096 q
= &sk
->sk_error_queue
;
1097 spin_lock_irqsave(&q
->lock
, flags
);
1098 tail
= skb_peek_tail(q
);
1099 if (!tail
|| SKB_EXT_ERR(tail
)->ee
.ee_origin
!= SO_EE_ORIGIN_ZEROCOPY
||
1100 !skb_zerocopy_notify_extend(tail
, lo
, len
)) {
1101 __skb_queue_tail(q
, skb
);
1104 spin_unlock_irqrestore(&q
->lock
, flags
);
1106 sk
->sk_error_report(sk
);
1112 EXPORT_SYMBOL_GPL(sock_zerocopy_callback
);
1114 void sock_zerocopy_put(struct ubuf_info
*uarg
)
1116 if (uarg
&& refcount_dec_and_test(&uarg
->refcnt
)) {
1118 uarg
->callback(uarg
, uarg
->zerocopy
);
1120 consume_skb(skb_from_uarg(uarg
));
1123 EXPORT_SYMBOL_GPL(sock_zerocopy_put
);
1125 void sock_zerocopy_put_abort(struct ubuf_info
*uarg
, bool have_uref
)
1128 struct sock
*sk
= skb_from_uarg(uarg
)->sk
;
1130 atomic_dec(&sk
->sk_zckey
);
1134 sock_zerocopy_put(uarg
);
1137 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort
);
1139 int skb_zerocopy_iter_dgram(struct sk_buff
*skb
, struct msghdr
*msg
, int len
)
1141 return __zerocopy_sg_from_iter(skb
->sk
, skb
, &msg
->msg_iter
, len
);
1143 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram
);
1145 int skb_zerocopy_iter_stream(struct sock
*sk
, struct sk_buff
*skb
,
1146 struct msghdr
*msg
, int len
,
1147 struct ubuf_info
*uarg
)
1149 struct ubuf_info
*orig_uarg
= skb_zcopy(skb
);
1150 struct iov_iter orig_iter
= msg
->msg_iter
;
1151 int err
, orig_len
= skb
->len
;
1153 /* An skb can only point to one uarg. This edge case happens when
1154 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1156 if (orig_uarg
&& uarg
!= orig_uarg
)
1159 err
= __zerocopy_sg_from_iter(sk
, skb
, &msg
->msg_iter
, len
);
1160 if (err
== -EFAULT
|| (err
== -EMSGSIZE
&& skb
->len
== orig_len
)) {
1161 struct sock
*save_sk
= skb
->sk
;
1163 /* Streams do not free skb on error. Reset to prev state. */
1164 msg
->msg_iter
= orig_iter
;
1166 ___pskb_trim(skb
, orig_len
);
1171 skb_zcopy_set(skb
, uarg
, NULL
);
1172 return skb
->len
- orig_len
;
1174 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream
);
1176 static int skb_zerocopy_clone(struct sk_buff
*nskb
, struct sk_buff
*orig
,
1179 if (skb_zcopy(orig
)) {
1180 if (skb_zcopy(nskb
)) {
1181 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1186 if (skb_uarg(nskb
) == skb_uarg(orig
))
1188 if (skb_copy_ubufs(nskb
, GFP_ATOMIC
))
1191 skb_zcopy_set(nskb
, skb_uarg(orig
), NULL
);
1197 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1198 * @skb: the skb to modify
1199 * @gfp_mask: allocation priority
1201 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1202 * It will copy all frags into kernel and drop the reference
1203 * to userspace pages.
1205 * If this function is called from an interrupt gfp_mask() must be
1208 * Returns 0 on success or a negative error code on failure
1209 * to allocate kernel memory to copy to.
1211 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
1213 int num_frags
= skb_shinfo(skb
)->nr_frags
;
1214 struct page
*page
, *head
= NULL
;
1218 if (skb_shared(skb
) || skb_unclone(skb
, gfp_mask
))
1224 new_frags
= (__skb_pagelen(skb
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1225 for (i
= 0; i
< new_frags
; i
++) {
1226 page
= alloc_page(gfp_mask
);
1229 struct page
*next
= (struct page
*)page_private(head
);
1235 set_page_private(page
, (unsigned long)head
);
1241 for (i
= 0; i
< num_frags
; i
++) {
1242 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1243 u32 p_off
, p_len
, copied
;
1247 skb_frag_foreach_page(f
, f
->page_offset
, skb_frag_size(f
),
1248 p
, p_off
, p_len
, copied
) {
1250 vaddr
= kmap_atomic(p
);
1252 while (done
< p_len
) {
1253 if (d_off
== PAGE_SIZE
) {
1255 page
= (struct page
*)page_private(page
);
1257 copy
= min_t(u32
, PAGE_SIZE
- d_off
, p_len
- done
);
1258 memcpy(page_address(page
) + d_off
,
1259 vaddr
+ p_off
+ done
, copy
);
1263 kunmap_atomic(vaddr
);
1267 /* skb frags release userspace buffers */
1268 for (i
= 0; i
< num_frags
; i
++)
1269 skb_frag_unref(skb
, i
);
1271 /* skb frags point to kernel buffers */
1272 for (i
= 0; i
< new_frags
- 1; i
++) {
1273 __skb_fill_page_desc(skb
, i
, head
, 0, PAGE_SIZE
);
1274 head
= (struct page
*)page_private(head
);
1276 __skb_fill_page_desc(skb
, new_frags
- 1, head
, 0, d_off
);
1277 skb_shinfo(skb
)->nr_frags
= new_frags
;
1280 skb_zcopy_clear(skb
, false);
1283 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
1286 * skb_clone - duplicate an sk_buff
1287 * @skb: buffer to clone
1288 * @gfp_mask: allocation priority
1290 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1291 * copies share the same packet data but not structure. The new
1292 * buffer has a reference count of 1. If the allocation fails the
1293 * function returns %NULL otherwise the new buffer is returned.
1295 * If this function is called from an interrupt gfp_mask() must be
1299 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
1301 struct sk_buff_fclones
*fclones
= container_of(skb
,
1302 struct sk_buff_fclones
,
1306 if (skb_orphan_frags(skb
, gfp_mask
))
1309 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
1310 refcount_read(&fclones
->fclone_ref
) == 1) {
1312 refcount_set(&fclones
->fclone_ref
, 2);
1314 if (skb_pfmemalloc(skb
))
1315 gfp_mask
|= __GFP_MEMALLOC
;
1317 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
1321 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
1324 return __skb_clone(n
, skb
);
1326 EXPORT_SYMBOL(skb_clone
);
1328 void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
1330 /* Only adjust this if it actually is csum_start rather than csum */
1331 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1332 skb
->csum_start
+= off
;
1333 /* {transport,network,mac}_header and tail are relative to skb->head */
1334 skb
->transport_header
+= off
;
1335 skb
->network_header
+= off
;
1336 if (skb_mac_header_was_set(skb
))
1337 skb
->mac_header
+= off
;
1338 skb
->inner_transport_header
+= off
;
1339 skb
->inner_network_header
+= off
;
1340 skb
->inner_mac_header
+= off
;
1342 EXPORT_SYMBOL(skb_headers_offset_update
);
1344 void skb_copy_header(struct sk_buff
*new, const struct sk_buff
*old
)
1346 __copy_skb_header(new, old
);
1348 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
1349 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
1350 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
1352 EXPORT_SYMBOL(skb_copy_header
);
1354 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
1356 if (skb_pfmemalloc(skb
))
1357 return SKB_ALLOC_RX
;
1362 * skb_copy - create private copy of an sk_buff
1363 * @skb: buffer to copy
1364 * @gfp_mask: allocation priority
1366 * Make a copy of both an &sk_buff and its data. This is used when the
1367 * caller wishes to modify the data and needs a private copy of the
1368 * data to alter. Returns %NULL on failure or the pointer to the buffer
1369 * on success. The returned buffer has a reference count of 1.
1371 * As by-product this function converts non-linear &sk_buff to linear
1372 * one, so that &sk_buff becomes completely private and caller is allowed
1373 * to modify all the data of returned buffer. This means that this
1374 * function is not recommended for use in circumstances when only
1375 * header is going to be modified. Use pskb_copy() instead.
1378 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
1380 int headerlen
= skb_headroom(skb
);
1381 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
1382 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
1383 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
1388 /* Set the data pointer */
1389 skb_reserve(n
, headerlen
);
1390 /* Set the tail pointer and length */
1391 skb_put(n
, skb
->len
);
1393 BUG_ON(skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
));
1395 skb_copy_header(n
, skb
);
1398 EXPORT_SYMBOL(skb_copy
);
1401 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1402 * @skb: buffer to copy
1403 * @headroom: headroom of new skb
1404 * @gfp_mask: allocation priority
1405 * @fclone: if true allocate the copy of the skb from the fclone
1406 * cache instead of the head cache; it is recommended to set this
1407 * to true for the cases where the copy will likely be cloned
1409 * Make a copy of both an &sk_buff and part of its data, located
1410 * in header. Fragmented data remain shared. This is used when
1411 * the caller wishes to modify only header of &sk_buff and needs
1412 * private copy of the header to alter. Returns %NULL on failure
1413 * or the pointer to the buffer on success.
1414 * The returned buffer has a reference count of 1.
1417 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
1418 gfp_t gfp_mask
, bool fclone
)
1420 unsigned int size
= skb_headlen(skb
) + headroom
;
1421 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
1422 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
1427 /* Set the data pointer */
1428 skb_reserve(n
, headroom
);
1429 /* Set the tail pointer and length */
1430 skb_put(n
, skb_headlen(skb
));
1431 /* Copy the bytes */
1432 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1434 n
->truesize
+= skb
->data_len
;
1435 n
->data_len
= skb
->data_len
;
1438 if (skb_shinfo(skb
)->nr_frags
) {
1441 if (skb_orphan_frags(skb
, gfp_mask
) ||
1442 skb_zerocopy_clone(n
, skb
, gfp_mask
)) {
1447 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1448 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1449 skb_frag_ref(skb
, i
);
1451 skb_shinfo(n
)->nr_frags
= i
;
1454 if (skb_has_frag_list(skb
)) {
1455 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1456 skb_clone_fraglist(n
);
1459 skb_copy_header(n
, skb
);
1463 EXPORT_SYMBOL(__pskb_copy_fclone
);
1466 * pskb_expand_head - reallocate header of &sk_buff
1467 * @skb: buffer to reallocate
1468 * @nhead: room to add at head
1469 * @ntail: room to add at tail
1470 * @gfp_mask: allocation priority
1472 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1473 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1474 * reference count of 1. Returns zero in the case of success or error,
1475 * if expansion failed. In the last case, &sk_buff is not changed.
1477 * All the pointers pointing into skb header may change and must be
1478 * reloaded after call to this function.
1481 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1484 int i
, osize
= skb_end_offset(skb
);
1485 int size
= osize
+ nhead
+ ntail
;
1491 BUG_ON(skb_shared(skb
));
1493 size
= SKB_DATA_ALIGN(size
);
1495 if (skb_pfmemalloc(skb
))
1496 gfp_mask
|= __GFP_MEMALLOC
;
1497 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1498 gfp_mask
, NUMA_NO_NODE
, NULL
);
1501 size
= SKB_WITH_OVERHEAD(ksize(data
));
1503 /* Copy only real data... and, alas, header. This should be
1504 * optimized for the cases when header is void.
1506 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1508 memcpy((struct skb_shared_info
*)(data
+ size
),
1510 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1513 * if shinfo is shared we must drop the old head gracefully, but if it
1514 * is not we can just drop the old head and let the existing refcount
1515 * be since all we did is relocate the values
1517 if (skb_cloned(skb
)) {
1518 if (skb_orphan_frags(skb
, gfp_mask
))
1521 refcount_inc(&skb_uarg(skb
)->refcnt
);
1522 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1523 skb_frag_ref(skb
, i
);
1525 if (skb_has_frag_list(skb
))
1526 skb_clone_fraglist(skb
);
1528 skb_release_data(skb
);
1532 off
= (data
+ nhead
) - skb
->head
;
1537 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1541 skb
->end
= skb
->head
+ size
;
1544 skb_headers_offset_update(skb
, nhead
);
1548 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1550 skb_metadata_clear(skb
);
1552 /* It is not generally safe to change skb->truesize.
1553 * For the moment, we really care of rx path, or
1554 * when skb is orphaned (not attached to a socket).
1556 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1557 skb
->truesize
+= size
- osize
;
1566 EXPORT_SYMBOL(pskb_expand_head
);
1568 /* Make private copy of skb with writable head and some headroom */
1570 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1572 struct sk_buff
*skb2
;
1573 int delta
= headroom
- skb_headroom(skb
);
1576 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1578 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1579 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1587 EXPORT_SYMBOL(skb_realloc_headroom
);
1590 * skb_copy_expand - copy and expand sk_buff
1591 * @skb: buffer to copy
1592 * @newheadroom: new free bytes at head
1593 * @newtailroom: new free bytes at tail
1594 * @gfp_mask: allocation priority
1596 * Make a copy of both an &sk_buff and its data and while doing so
1597 * allocate additional space.
1599 * This is used when the caller wishes to modify the data and needs a
1600 * private copy of the data to alter as well as more space for new fields.
1601 * Returns %NULL on failure or the pointer to the buffer
1602 * on success. The returned buffer has a reference count of 1.
1604 * You must pass %GFP_ATOMIC as the allocation priority if this function
1605 * is called from an interrupt.
1607 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1608 int newheadroom
, int newtailroom
,
1612 * Allocate the copy buffer
1614 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1615 gfp_mask
, skb_alloc_rx_flag(skb
),
1617 int oldheadroom
= skb_headroom(skb
);
1618 int head_copy_len
, head_copy_off
;
1623 skb_reserve(n
, newheadroom
);
1625 /* Set the tail pointer and length */
1626 skb_put(n
, skb
->len
);
1628 head_copy_len
= oldheadroom
;
1630 if (newheadroom
<= head_copy_len
)
1631 head_copy_len
= newheadroom
;
1633 head_copy_off
= newheadroom
- head_copy_len
;
1635 /* Copy the linear header and data. */
1636 BUG_ON(skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1637 skb
->len
+ head_copy_len
));
1639 skb_copy_header(n
, skb
);
1641 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1645 EXPORT_SYMBOL(skb_copy_expand
);
1648 * __skb_pad - zero pad the tail of an skb
1649 * @skb: buffer to pad
1650 * @pad: space to pad
1651 * @free_on_error: free buffer on error
1653 * Ensure that a buffer is followed by a padding area that is zero
1654 * filled. Used by network drivers which may DMA or transfer data
1655 * beyond the buffer end onto the wire.
1657 * May return error in out of memory cases. The skb is freed on error
1658 * if @free_on_error is true.
1661 int __skb_pad(struct sk_buff
*skb
, int pad
, bool free_on_error
)
1666 /* If the skbuff is non linear tailroom is always zero.. */
1667 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1668 memset(skb
->data
+skb
->len
, 0, pad
);
1672 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1673 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1674 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1679 /* FIXME: The use of this function with non-linear skb's really needs
1682 err
= skb_linearize(skb
);
1686 memset(skb
->data
+ skb
->len
, 0, pad
);
1694 EXPORT_SYMBOL(__skb_pad
);
1697 * pskb_put - add data to the tail of a potentially fragmented buffer
1698 * @skb: start of the buffer to use
1699 * @tail: tail fragment of the buffer to use
1700 * @len: amount of data to add
1702 * This function extends the used data area of the potentially
1703 * fragmented buffer. @tail must be the last fragment of @skb -- or
1704 * @skb itself. If this would exceed the total buffer size the kernel
1705 * will panic. A pointer to the first byte of the extra data is
1709 void *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1712 skb
->data_len
+= len
;
1715 return skb_put(tail
, len
);
1717 EXPORT_SYMBOL_GPL(pskb_put
);
1720 * skb_put - add data to a buffer
1721 * @skb: buffer to use
1722 * @len: amount of data to add
1724 * This function extends the used data area of the buffer. If this would
1725 * exceed the total buffer size the kernel will panic. A pointer to the
1726 * first byte of the extra data is returned.
1728 void *skb_put(struct sk_buff
*skb
, unsigned int len
)
1730 void *tmp
= skb_tail_pointer(skb
);
1731 SKB_LINEAR_ASSERT(skb
);
1734 if (unlikely(skb
->tail
> skb
->end
))
1735 skb_over_panic(skb
, len
, __builtin_return_address(0));
1738 EXPORT_SYMBOL(skb_put
);
1741 * skb_push - add data to the start of a buffer
1742 * @skb: buffer to use
1743 * @len: amount of data to add
1745 * This function extends the used data area of the buffer at the buffer
1746 * start. If this would exceed the total buffer headroom the kernel will
1747 * panic. A pointer to the first byte of the extra data is returned.
1749 void *skb_push(struct sk_buff
*skb
, unsigned int len
)
1753 if (unlikely(skb
->data
< skb
->head
))
1754 skb_under_panic(skb
, len
, __builtin_return_address(0));
1757 EXPORT_SYMBOL(skb_push
);
1760 * skb_pull - remove data from the start of a buffer
1761 * @skb: buffer to use
1762 * @len: amount of data to remove
1764 * This function removes data from the start of a buffer, returning
1765 * the memory to the headroom. A pointer to the next data in the buffer
1766 * is returned. Once the data has been pulled future pushes will overwrite
1769 void *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1771 return skb_pull_inline(skb
, len
);
1773 EXPORT_SYMBOL(skb_pull
);
1776 * skb_trim - remove end from a buffer
1777 * @skb: buffer to alter
1780 * Cut the length of a buffer down by removing data from the tail. If
1781 * the buffer is already under the length specified it is not modified.
1782 * The skb must be linear.
1784 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1787 __skb_trim(skb
, len
);
1789 EXPORT_SYMBOL(skb_trim
);
1791 /* Trims skb to length len. It can change skb pointers.
1794 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1796 struct sk_buff
**fragp
;
1797 struct sk_buff
*frag
;
1798 int offset
= skb_headlen(skb
);
1799 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1803 if (skb_cloned(skb
) &&
1804 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1811 for (; i
< nfrags
; i
++) {
1812 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1819 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1822 skb_shinfo(skb
)->nr_frags
= i
;
1824 for (; i
< nfrags
; i
++)
1825 skb_frag_unref(skb
, i
);
1827 if (skb_has_frag_list(skb
))
1828 skb_drop_fraglist(skb
);
1832 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1833 fragp
= &frag
->next
) {
1834 int end
= offset
+ frag
->len
;
1836 if (skb_shared(frag
)) {
1837 struct sk_buff
*nfrag
;
1839 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1840 if (unlikely(!nfrag
))
1843 nfrag
->next
= frag
->next
;
1855 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1859 skb_drop_list(&frag
->next
);
1864 if (len
> skb_headlen(skb
)) {
1865 skb
->data_len
-= skb
->len
- len
;
1870 skb_set_tail_pointer(skb
, len
);
1873 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1877 EXPORT_SYMBOL(___pskb_trim
);
1879 /* Note : use pskb_trim_rcsum() instead of calling this directly
1881 int pskb_trim_rcsum_slow(struct sk_buff
*skb
, unsigned int len
)
1883 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
1884 int delta
= skb
->len
- len
;
1886 skb
->csum
= csum_block_sub(skb
->csum
,
1887 skb_checksum(skb
, len
, delta
, 0),
1890 return __pskb_trim(skb
, len
);
1892 EXPORT_SYMBOL(pskb_trim_rcsum_slow
);
1895 * __pskb_pull_tail - advance tail of skb header
1896 * @skb: buffer to reallocate
1897 * @delta: number of bytes to advance tail
1899 * The function makes a sense only on a fragmented &sk_buff,
1900 * it expands header moving its tail forward and copying necessary
1901 * data from fragmented part.
1903 * &sk_buff MUST have reference count of 1.
1905 * Returns %NULL (and &sk_buff does not change) if pull failed
1906 * or value of new tail of skb in the case of success.
1908 * All the pointers pointing into skb header may change and must be
1909 * reloaded after call to this function.
1912 /* Moves tail of skb head forward, copying data from fragmented part,
1913 * when it is necessary.
1914 * 1. It may fail due to malloc failure.
1915 * 2. It may change skb pointers.
1917 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1919 void *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1921 /* If skb has not enough free space at tail, get new one
1922 * plus 128 bytes for future expansions. If we have enough
1923 * room at tail, reallocate without expansion only if skb is cloned.
1925 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1927 if (eat
> 0 || skb_cloned(skb
)) {
1928 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1933 BUG_ON(skb_copy_bits(skb
, skb_headlen(skb
),
1934 skb_tail_pointer(skb
), delta
));
1936 /* Optimization: no fragments, no reasons to preestimate
1937 * size of pulled pages. Superb.
1939 if (!skb_has_frag_list(skb
))
1942 /* Estimate size of pulled pages. */
1944 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1945 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1952 /* If we need update frag list, we are in troubles.
1953 * Certainly, it is possible to add an offset to skb data,
1954 * but taking into account that pulling is expected to
1955 * be very rare operation, it is worth to fight against
1956 * further bloating skb head and crucify ourselves here instead.
1957 * Pure masohism, indeed. 8)8)
1960 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1961 struct sk_buff
*clone
= NULL
;
1962 struct sk_buff
*insp
= NULL
;
1965 if (list
->len
<= eat
) {
1966 /* Eaten as whole. */
1971 /* Eaten partially. */
1973 if (skb_shared(list
)) {
1974 /* Sucks! We need to fork list. :-( */
1975 clone
= skb_clone(list
, GFP_ATOMIC
);
1981 /* This may be pulled without
1985 if (!pskb_pull(list
, eat
)) {
1993 /* Free pulled out fragments. */
1994 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1995 skb_shinfo(skb
)->frag_list
= list
->next
;
1998 /* And insert new clone at head. */
2001 skb_shinfo(skb
)->frag_list
= clone
;
2004 /* Success! Now we may commit changes to skb data. */
2009 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2010 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2013 skb_frag_unref(skb
, i
);
2016 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2018 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
2019 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
2027 skb_shinfo(skb
)->nr_frags
= k
;
2031 skb
->data_len
-= delta
;
2034 skb_zcopy_clear(skb
, false);
2036 return skb_tail_pointer(skb
);
2038 EXPORT_SYMBOL(__pskb_pull_tail
);
2041 * skb_copy_bits - copy bits from skb to kernel buffer
2043 * @offset: offset in source
2044 * @to: destination buffer
2045 * @len: number of bytes to copy
2047 * Copy the specified number of bytes from the source skb to the
2048 * destination buffer.
2051 * If its prototype is ever changed,
2052 * check arch/{*}/net/{*}.S files,
2053 * since it is called from BPF assembly code.
2055 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
2057 int start
= skb_headlen(skb
);
2058 struct sk_buff
*frag_iter
;
2061 if (offset
> (int)skb
->len
- len
)
2065 if ((copy
= start
- offset
) > 0) {
2068 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
2069 if ((len
-= copy
) == 0)
2075 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2077 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
2079 WARN_ON(start
> offset
+ len
);
2081 end
= start
+ skb_frag_size(f
);
2082 if ((copy
= end
- offset
) > 0) {
2083 u32 p_off
, p_len
, copied
;
2090 skb_frag_foreach_page(f
,
2091 f
->page_offset
+ offset
- start
,
2092 copy
, p
, p_off
, p_len
, copied
) {
2093 vaddr
= kmap_atomic(p
);
2094 memcpy(to
+ copied
, vaddr
+ p_off
, p_len
);
2095 kunmap_atomic(vaddr
);
2098 if ((len
-= copy
) == 0)
2106 skb_walk_frags(skb
, frag_iter
) {
2109 WARN_ON(start
> offset
+ len
);
2111 end
= start
+ frag_iter
->len
;
2112 if ((copy
= end
- offset
) > 0) {
2115 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
2117 if ((len
-= copy
) == 0)
2131 EXPORT_SYMBOL(skb_copy_bits
);
2134 * Callback from splice_to_pipe(), if we need to release some pages
2135 * at the end of the spd in case we error'ed out in filling the pipe.
2137 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
2139 put_page(spd
->pages
[i
]);
2142 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
2143 unsigned int *offset
,
2146 struct page_frag
*pfrag
= sk_page_frag(sk
);
2148 if (!sk_page_frag_refill(sk
, pfrag
))
2151 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
2153 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
2154 page_address(page
) + *offset
, *len
);
2155 *offset
= pfrag
->offset
;
2156 pfrag
->offset
+= *len
;
2161 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
2163 unsigned int offset
)
2165 return spd
->nr_pages
&&
2166 spd
->pages
[spd
->nr_pages
- 1] == page
&&
2167 (spd
->partial
[spd
->nr_pages
- 1].offset
+
2168 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
2172 * Fill page/offset/length into spd, if it can hold more pages.
2174 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
2175 struct pipe_inode_info
*pipe
, struct page
*page
,
2176 unsigned int *len
, unsigned int offset
,
2180 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
2184 page
= linear_to_page(page
, len
, &offset
, sk
);
2188 if (spd_can_coalesce(spd
, page
, offset
)) {
2189 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
2193 spd
->pages
[spd
->nr_pages
] = page
;
2194 spd
->partial
[spd
->nr_pages
].len
= *len
;
2195 spd
->partial
[spd
->nr_pages
].offset
= offset
;
2201 static bool __splice_segment(struct page
*page
, unsigned int poff
,
2202 unsigned int plen
, unsigned int *off
,
2204 struct splice_pipe_desc
*spd
, bool linear
,
2206 struct pipe_inode_info
*pipe
)
2211 /* skip this segment if already processed */
2217 /* ignore any bits we already processed */
2223 unsigned int flen
= min(*len
, plen
);
2225 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
2231 } while (*len
&& plen
);
2237 * Map linear and fragment data from the skb to spd. It reports true if the
2238 * pipe is full or if we already spliced the requested length.
2240 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
2241 unsigned int *offset
, unsigned int *len
,
2242 struct splice_pipe_desc
*spd
, struct sock
*sk
)
2245 struct sk_buff
*iter
;
2247 /* map the linear part :
2248 * If skb->head_frag is set, this 'linear' part is backed by a
2249 * fragment, and if the head is not shared with any clones then
2250 * we can avoid a copy since we own the head portion of this page.
2252 if (__splice_segment(virt_to_page(skb
->data
),
2253 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
2256 skb_head_is_locked(skb
),
2261 * then map the fragments
2263 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
2264 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
2266 if (__splice_segment(skb_frag_page(f
),
2267 f
->page_offset
, skb_frag_size(f
),
2268 offset
, len
, spd
, false, sk
, pipe
))
2272 skb_walk_frags(skb
, iter
) {
2273 if (*offset
>= iter
->len
) {
2274 *offset
-= iter
->len
;
2277 /* __skb_splice_bits() only fails if the output has no room
2278 * left, so no point in going over the frag_list for the error
2281 if (__skb_splice_bits(iter
, pipe
, offset
, len
, spd
, sk
))
2289 * Map data from the skb to a pipe. Should handle both the linear part,
2290 * the fragments, and the frag list.
2292 int skb_splice_bits(struct sk_buff
*skb
, struct sock
*sk
, unsigned int offset
,
2293 struct pipe_inode_info
*pipe
, unsigned int tlen
,
2296 struct partial_page partial
[MAX_SKB_FRAGS
];
2297 struct page
*pages
[MAX_SKB_FRAGS
];
2298 struct splice_pipe_desc spd
= {
2301 .nr_pages_max
= MAX_SKB_FRAGS
,
2302 .ops
= &nosteal_pipe_buf_ops
,
2303 .spd_release
= sock_spd_release
,
2307 __skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
);
2310 ret
= splice_to_pipe(pipe
, &spd
);
2314 EXPORT_SYMBOL_GPL(skb_splice_bits
);
2316 /* Send skb data on a socket. Socket must be locked. */
2317 int skb_send_sock_locked(struct sock
*sk
, struct sk_buff
*skb
, int offset
,
2320 unsigned int orig_len
= len
;
2321 struct sk_buff
*head
= skb
;
2322 unsigned short fragidx
;
2327 /* Deal with head data */
2328 while (offset
< skb_headlen(skb
) && len
) {
2332 slen
= min_t(int, len
, skb_headlen(skb
) - offset
);
2333 kv
.iov_base
= skb
->data
+ offset
;
2335 memset(&msg
, 0, sizeof(msg
));
2337 ret
= kernel_sendmsg_locked(sk
, &msg
, &kv
, 1, slen
);
2345 /* All the data was skb head? */
2349 /* Make offset relative to start of frags */
2350 offset
-= skb_headlen(skb
);
2352 /* Find where we are in frag list */
2353 for (fragidx
= 0; fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2354 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2356 if (offset
< frag
->size
)
2359 offset
-= frag
->size
;
2362 for (; len
&& fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2363 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2365 slen
= min_t(size_t, len
, frag
->size
- offset
);
2368 ret
= kernel_sendpage_locked(sk
, frag
->page
.p
,
2369 frag
->page_offset
+ offset
,
2370 slen
, MSG_DONTWAIT
);
2383 /* Process any frag lists */
2386 if (skb_has_frag_list(skb
)) {
2387 skb
= skb_shinfo(skb
)->frag_list
;
2390 } else if (skb
->next
) {
2397 return orig_len
- len
;
2400 return orig_len
== len
? ret
: orig_len
- len
;
2402 EXPORT_SYMBOL_GPL(skb_send_sock_locked
);
2405 * skb_store_bits - store bits from kernel buffer to skb
2406 * @skb: destination buffer
2407 * @offset: offset in destination
2408 * @from: source buffer
2409 * @len: number of bytes to copy
2411 * Copy the specified number of bytes from the source buffer to the
2412 * destination skb. This function handles all the messy bits of
2413 * traversing fragment lists and such.
2416 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
2418 int start
= skb_headlen(skb
);
2419 struct sk_buff
*frag_iter
;
2422 if (offset
> (int)skb
->len
- len
)
2425 if ((copy
= start
- offset
) > 0) {
2428 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
2429 if ((len
-= copy
) == 0)
2435 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2436 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2439 WARN_ON(start
> offset
+ len
);
2441 end
= start
+ skb_frag_size(frag
);
2442 if ((copy
= end
- offset
) > 0) {
2443 u32 p_off
, p_len
, copied
;
2450 skb_frag_foreach_page(frag
,
2451 frag
->page_offset
+ offset
- start
,
2452 copy
, p
, p_off
, p_len
, copied
) {
2453 vaddr
= kmap_atomic(p
);
2454 memcpy(vaddr
+ p_off
, from
+ copied
, p_len
);
2455 kunmap_atomic(vaddr
);
2458 if ((len
-= copy
) == 0)
2466 skb_walk_frags(skb
, frag_iter
) {
2469 WARN_ON(start
> offset
+ len
);
2471 end
= start
+ frag_iter
->len
;
2472 if ((copy
= end
- offset
) > 0) {
2475 if (skb_store_bits(frag_iter
, offset
- start
,
2478 if ((len
-= copy
) == 0)
2491 EXPORT_SYMBOL(skb_store_bits
);
2493 /* Checksum skb data. */
2494 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
2495 __wsum csum
, const struct skb_checksum_ops
*ops
)
2497 int start
= skb_headlen(skb
);
2498 int i
, copy
= start
- offset
;
2499 struct sk_buff
*frag_iter
;
2502 /* Checksum header. */
2506 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
2507 if ((len
-= copy
) == 0)
2513 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2515 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2517 WARN_ON(start
> offset
+ len
);
2519 end
= start
+ skb_frag_size(frag
);
2520 if ((copy
= end
- offset
) > 0) {
2521 u32 p_off
, p_len
, copied
;
2529 skb_frag_foreach_page(frag
,
2530 frag
->page_offset
+ offset
- start
,
2531 copy
, p
, p_off
, p_len
, copied
) {
2532 vaddr
= kmap_atomic(p
);
2533 csum2
= ops
->update(vaddr
+ p_off
, p_len
, 0);
2534 kunmap_atomic(vaddr
);
2535 csum
= ops
->combine(csum
, csum2
, pos
, p_len
);
2546 skb_walk_frags(skb
, frag_iter
) {
2549 WARN_ON(start
> offset
+ len
);
2551 end
= start
+ frag_iter
->len
;
2552 if ((copy
= end
- offset
) > 0) {
2556 csum2
= __skb_checksum(frag_iter
, offset
- start
,
2558 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2559 if ((len
-= copy
) == 0)
2570 EXPORT_SYMBOL(__skb_checksum
);
2572 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2573 int len
, __wsum csum
)
2575 const struct skb_checksum_ops ops
= {
2576 .update
= csum_partial_ext
,
2577 .combine
= csum_block_add_ext
,
2580 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2582 EXPORT_SYMBOL(skb_checksum
);
2584 /* Both of above in one bottle. */
2586 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2587 u8
*to
, int len
, __wsum csum
)
2589 int start
= skb_headlen(skb
);
2590 int i
, copy
= start
- offset
;
2591 struct sk_buff
*frag_iter
;
2598 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2600 if ((len
-= copy
) == 0)
2607 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2610 WARN_ON(start
> offset
+ len
);
2612 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2613 if ((copy
= end
- offset
) > 0) {
2614 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2615 u32 p_off
, p_len
, copied
;
2623 skb_frag_foreach_page(frag
,
2624 frag
->page_offset
+ offset
- start
,
2625 copy
, p
, p_off
, p_len
, copied
) {
2626 vaddr
= kmap_atomic(p
);
2627 csum2
= csum_partial_copy_nocheck(vaddr
+ p_off
,
2630 kunmap_atomic(vaddr
);
2631 csum
= csum_block_add(csum
, csum2
, pos
);
2643 skb_walk_frags(skb
, frag_iter
) {
2647 WARN_ON(start
> offset
+ len
);
2649 end
= start
+ frag_iter
->len
;
2650 if ((copy
= end
- offset
) > 0) {
2653 csum2
= skb_copy_and_csum_bits(frag_iter
,
2656 csum
= csum_block_add(csum
, csum2
, pos
);
2657 if ((len
-= copy
) == 0)
2668 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2670 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
2674 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
2675 /* See comments in __skb_checksum_complete(). */
2677 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
2678 !skb
->csum_complete_sw
)
2679 netdev_rx_csum_fault(skb
->dev
, skb
);
2681 if (!skb_shared(skb
))
2682 skb
->csum_valid
= !sum
;
2685 EXPORT_SYMBOL(__skb_checksum_complete_head
);
2687 /* This function assumes skb->csum already holds pseudo header's checksum,
2688 * which has been changed from the hardware checksum, for example, by
2689 * __skb_checksum_validate_complete(). And, the original skb->csum must
2690 * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2692 * It returns non-zero if the recomputed checksum is still invalid, otherwise
2693 * zero. The new checksum is stored back into skb->csum unless the skb is
2696 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
2701 csum
= skb_checksum(skb
, 0, skb
->len
, 0);
2703 sum
= csum_fold(csum_add(skb
->csum
, csum
));
2704 /* This check is inverted, because we already knew the hardware
2705 * checksum is invalid before calling this function. So, if the
2706 * re-computed checksum is valid instead, then we have a mismatch
2707 * between the original skb->csum and skb_checksum(). This means either
2708 * the original hardware checksum is incorrect or we screw up skb->csum
2709 * when moving skb->data around.
2712 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
2713 !skb
->csum_complete_sw
)
2714 netdev_rx_csum_fault(skb
->dev
, skb
);
2717 if (!skb_shared(skb
)) {
2718 /* Save full packet checksum */
2720 skb
->ip_summed
= CHECKSUM_COMPLETE
;
2721 skb
->csum_complete_sw
= 1;
2722 skb
->csum_valid
= !sum
;
2727 EXPORT_SYMBOL(__skb_checksum_complete
);
2729 static __wsum
warn_crc32c_csum_update(const void *buff
, int len
, __wsum sum
)
2731 net_warn_ratelimited(
2732 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2737 static __wsum
warn_crc32c_csum_combine(__wsum csum
, __wsum csum2
,
2738 int offset
, int len
)
2740 net_warn_ratelimited(
2741 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2746 static const struct skb_checksum_ops default_crc32c_ops
= {
2747 .update
= warn_crc32c_csum_update
,
2748 .combine
= warn_crc32c_csum_combine
,
2751 const struct skb_checksum_ops
*crc32c_csum_stub __read_mostly
=
2752 &default_crc32c_ops
;
2753 EXPORT_SYMBOL(crc32c_csum_stub
);
2756 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2757 * @from: source buffer
2759 * Calculates the amount of linear headroom needed in the 'to' skb passed
2760 * into skb_zerocopy().
2763 skb_zerocopy_headlen(const struct sk_buff
*from
)
2765 unsigned int hlen
= 0;
2767 if (!from
->head_frag
||
2768 skb_headlen(from
) < L1_CACHE_BYTES
||
2769 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2770 hlen
= skb_headlen(from
);
2772 if (skb_has_frag_list(from
))
2777 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2780 * skb_zerocopy - Zero copy skb to skb
2781 * @to: destination buffer
2782 * @from: source buffer
2783 * @len: number of bytes to copy from source buffer
2784 * @hlen: size of linear headroom in destination buffer
2786 * Copies up to `len` bytes from `from` to `to` by creating references
2787 * to the frags in the source buffer.
2789 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2790 * headroom in the `to` buffer.
2793 * 0: everything is OK
2794 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2795 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2798 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2801 int plen
= 0; /* length of skb->head fragment */
2804 unsigned int offset
;
2806 BUG_ON(!from
->head_frag
&& !hlen
);
2808 /* dont bother with small payloads */
2809 if (len
<= skb_tailroom(to
))
2810 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2813 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2818 plen
= min_t(int, skb_headlen(from
), len
);
2820 page
= virt_to_head_page(from
->head
);
2821 offset
= from
->data
- (unsigned char *)page_address(page
);
2822 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2829 to
->truesize
+= len
+ plen
;
2830 to
->len
+= len
+ plen
;
2831 to
->data_len
+= len
+ plen
;
2833 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2837 skb_zerocopy_clone(to
, from
, GFP_ATOMIC
);
2839 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2842 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2843 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2844 len
-= skb_shinfo(to
)->frags
[j
].size
;
2845 skb_frag_ref(to
, j
);
2848 skb_shinfo(to
)->nr_frags
= j
;
2852 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2854 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2859 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2860 csstart
= skb_checksum_start_offset(skb
);
2862 csstart
= skb_headlen(skb
);
2864 BUG_ON(csstart
> skb_headlen(skb
));
2866 skb_copy_from_linear_data(skb
, to
, csstart
);
2869 if (csstart
!= skb
->len
)
2870 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2871 skb
->len
- csstart
, 0);
2873 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2874 long csstuff
= csstart
+ skb
->csum_offset
;
2876 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2879 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2882 * skb_dequeue - remove from the head of the queue
2883 * @list: list to dequeue from
2885 * Remove the head of the list. The list lock is taken so the function
2886 * may be used safely with other locking list functions. The head item is
2887 * returned or %NULL if the list is empty.
2890 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2892 unsigned long flags
;
2893 struct sk_buff
*result
;
2895 spin_lock_irqsave(&list
->lock
, flags
);
2896 result
= __skb_dequeue(list
);
2897 spin_unlock_irqrestore(&list
->lock
, flags
);
2900 EXPORT_SYMBOL(skb_dequeue
);
2903 * skb_dequeue_tail - remove from the tail of the queue
2904 * @list: list to dequeue from
2906 * Remove the tail of the list. The list lock is taken so the function
2907 * may be used safely with other locking list functions. The tail item is
2908 * returned or %NULL if the list is empty.
2910 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2912 unsigned long flags
;
2913 struct sk_buff
*result
;
2915 spin_lock_irqsave(&list
->lock
, flags
);
2916 result
= __skb_dequeue_tail(list
);
2917 spin_unlock_irqrestore(&list
->lock
, flags
);
2920 EXPORT_SYMBOL(skb_dequeue_tail
);
2923 * skb_queue_purge - empty a list
2924 * @list: list to empty
2926 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2927 * the list and one reference dropped. This function takes the list
2928 * lock and is atomic with respect to other list locking functions.
2930 void skb_queue_purge(struct sk_buff_head
*list
)
2932 struct sk_buff
*skb
;
2933 while ((skb
= skb_dequeue(list
)) != NULL
)
2936 EXPORT_SYMBOL(skb_queue_purge
);
2939 * skb_rbtree_purge - empty a skb rbtree
2940 * @root: root of the rbtree to empty
2941 * Return value: the sum of truesizes of all purged skbs.
2943 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2944 * the list and one reference dropped. This function does not take
2945 * any lock. Synchronization should be handled by the caller (e.g., TCP
2946 * out-of-order queue is protected by the socket lock).
2948 unsigned int skb_rbtree_purge(struct rb_root
*root
)
2950 struct rb_node
*p
= rb_first(root
);
2951 unsigned int sum
= 0;
2954 struct sk_buff
*skb
= rb_entry(p
, struct sk_buff
, rbnode
);
2957 rb_erase(&skb
->rbnode
, root
);
2958 sum
+= skb
->truesize
;
2965 * skb_queue_head - queue a buffer at the list head
2966 * @list: list to use
2967 * @newsk: buffer to queue
2969 * Queue a buffer at the start of the list. This function takes the
2970 * list lock and can be used safely with other locking &sk_buff functions
2973 * A buffer cannot be placed on two lists at the same time.
2975 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2977 unsigned long flags
;
2979 spin_lock_irqsave(&list
->lock
, flags
);
2980 __skb_queue_head(list
, newsk
);
2981 spin_unlock_irqrestore(&list
->lock
, flags
);
2983 EXPORT_SYMBOL(skb_queue_head
);
2986 * skb_queue_tail - queue a buffer at the list tail
2987 * @list: list to use
2988 * @newsk: buffer to queue
2990 * Queue a buffer at the tail of the list. This function takes the
2991 * list lock and can be used safely with other locking &sk_buff functions
2994 * A buffer cannot be placed on two lists at the same time.
2996 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2998 unsigned long flags
;
3000 spin_lock_irqsave(&list
->lock
, flags
);
3001 __skb_queue_tail(list
, newsk
);
3002 spin_unlock_irqrestore(&list
->lock
, flags
);
3004 EXPORT_SYMBOL(skb_queue_tail
);
3007 * skb_unlink - remove a buffer from a list
3008 * @skb: buffer to remove
3009 * @list: list to use
3011 * Remove a packet from a list. The list locks are taken and this
3012 * function is atomic with respect to other list locked calls
3014 * You must know what list the SKB is on.
3016 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
3018 unsigned long flags
;
3020 spin_lock_irqsave(&list
->lock
, flags
);
3021 __skb_unlink(skb
, list
);
3022 spin_unlock_irqrestore(&list
->lock
, flags
);
3024 EXPORT_SYMBOL(skb_unlink
);
3027 * skb_append - append a buffer
3028 * @old: buffer to insert after
3029 * @newsk: buffer to insert
3030 * @list: list to use
3032 * Place a packet after a given packet in a list. The list locks are taken
3033 * and this function is atomic with respect to other list locked calls.
3034 * A buffer cannot be placed on two lists at the same time.
3036 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
3038 unsigned long flags
;
3040 spin_lock_irqsave(&list
->lock
, flags
);
3041 __skb_queue_after(list
, old
, newsk
);
3042 spin_unlock_irqrestore(&list
->lock
, flags
);
3044 EXPORT_SYMBOL(skb_append
);
3046 static inline void skb_split_inside_header(struct sk_buff
*skb
,
3047 struct sk_buff
* skb1
,
3048 const u32 len
, const int pos
)
3052 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
3054 /* And move data appendix as is. */
3055 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
3056 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
3058 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
3059 skb_shinfo(skb
)->nr_frags
= 0;
3060 skb1
->data_len
= skb
->data_len
;
3061 skb1
->len
+= skb1
->data_len
;
3064 skb_set_tail_pointer(skb
, len
);
3067 static inline void skb_split_no_header(struct sk_buff
*skb
,
3068 struct sk_buff
* skb1
,
3069 const u32 len
, int pos
)
3072 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
3074 skb_shinfo(skb
)->nr_frags
= 0;
3075 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
3077 skb
->data_len
= len
- pos
;
3079 for (i
= 0; i
< nfrags
; i
++) {
3080 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3082 if (pos
+ size
> len
) {
3083 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
3087 * We have two variants in this case:
3088 * 1. Move all the frag to the second
3089 * part, if it is possible. F.e.
3090 * this approach is mandatory for TUX,
3091 * where splitting is expensive.
3092 * 2. Split is accurately. We make this.
3094 skb_frag_ref(skb
, i
);
3095 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
3096 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
3097 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
3098 skb_shinfo(skb
)->nr_frags
++;
3102 skb_shinfo(skb
)->nr_frags
++;
3105 skb_shinfo(skb1
)->nr_frags
= k
;
3109 * skb_split - Split fragmented skb to two parts at length len.
3110 * @skb: the buffer to split
3111 * @skb1: the buffer to receive the second part
3112 * @len: new length for skb
3114 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
3116 int pos
= skb_headlen(skb
);
3118 skb_shinfo(skb1
)->tx_flags
|= skb_shinfo(skb
)->tx_flags
&
3120 skb_zerocopy_clone(skb1
, skb
, 0);
3121 if (len
< pos
) /* Split line is inside header. */
3122 skb_split_inside_header(skb
, skb1
, len
, pos
);
3123 else /* Second chunk has no header, nothing to copy. */
3124 skb_split_no_header(skb
, skb1
, len
, pos
);
3126 EXPORT_SYMBOL(skb_split
);
3128 /* Shifting from/to a cloned skb is a no-go.
3130 * Caller cannot keep skb_shinfo related pointers past calling here!
3132 static int skb_prepare_for_shift(struct sk_buff
*skb
)
3134 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
3138 * skb_shift - Shifts paged data partially from skb to another
3139 * @tgt: buffer into which tail data gets added
3140 * @skb: buffer from which the paged data comes from
3141 * @shiftlen: shift up to this many bytes
3143 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3144 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3145 * It's up to caller to free skb if everything was shifted.
3147 * If @tgt runs out of frags, the whole operation is aborted.
3149 * Skb cannot include anything else but paged data while tgt is allowed
3150 * to have non-paged data as well.
3152 * TODO: full sized shift could be optimized but that would need
3153 * specialized skb free'er to handle frags without up-to-date nr_frags.
3155 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
3157 int from
, to
, merge
, todo
;
3158 struct skb_frag_struct
*fragfrom
, *fragto
;
3160 BUG_ON(shiftlen
> skb
->len
);
3162 if (skb_headlen(skb
))
3164 if (skb_zcopy(tgt
) || skb_zcopy(skb
))
3169 to
= skb_shinfo(tgt
)->nr_frags
;
3170 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3172 /* Actual merge is delayed until the point when we know we can
3173 * commit all, so that we don't have to undo partial changes
3176 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
3177 fragfrom
->page_offset
)) {
3182 todo
-= skb_frag_size(fragfrom
);
3184 if (skb_prepare_for_shift(skb
) ||
3185 skb_prepare_for_shift(tgt
))
3188 /* All previous frag pointers might be stale! */
3189 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3190 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3192 skb_frag_size_add(fragto
, shiftlen
);
3193 skb_frag_size_sub(fragfrom
, shiftlen
);
3194 fragfrom
->page_offset
+= shiftlen
;
3202 /* Skip full, not-fitting skb to avoid expensive operations */
3203 if ((shiftlen
== skb
->len
) &&
3204 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
3207 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
3210 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
3211 if (to
== MAX_SKB_FRAGS
)
3214 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3215 fragto
= &skb_shinfo(tgt
)->frags
[to
];
3217 if (todo
>= skb_frag_size(fragfrom
)) {
3218 *fragto
= *fragfrom
;
3219 todo
-= skb_frag_size(fragfrom
);
3224 __skb_frag_ref(fragfrom
);
3225 fragto
->page
= fragfrom
->page
;
3226 fragto
->page_offset
= fragfrom
->page_offset
;
3227 skb_frag_size_set(fragto
, todo
);
3229 fragfrom
->page_offset
+= todo
;
3230 skb_frag_size_sub(fragfrom
, todo
);
3238 /* Ready to "commit" this state change to tgt */
3239 skb_shinfo(tgt
)->nr_frags
= to
;
3242 fragfrom
= &skb_shinfo(skb
)->frags
[0];
3243 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3245 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
3246 __skb_frag_unref(fragfrom
);
3249 /* Reposition in the original skb */
3251 while (from
< skb_shinfo(skb
)->nr_frags
)
3252 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
3253 skb_shinfo(skb
)->nr_frags
= to
;
3255 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
3258 /* Most likely the tgt won't ever need its checksum anymore, skb on
3259 * the other hand might need it if it needs to be resent
3261 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
3262 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3264 /* Yak, is it really working this way? Some helper please? */
3265 skb
->len
-= shiftlen
;
3266 skb
->data_len
-= shiftlen
;
3267 skb
->truesize
-= shiftlen
;
3268 tgt
->len
+= shiftlen
;
3269 tgt
->data_len
+= shiftlen
;
3270 tgt
->truesize
+= shiftlen
;
3276 * skb_prepare_seq_read - Prepare a sequential read of skb data
3277 * @skb: the buffer to read
3278 * @from: lower offset of data to be read
3279 * @to: upper offset of data to be read
3280 * @st: state variable
3282 * Initializes the specified state variable. Must be called before
3283 * invoking skb_seq_read() for the first time.
3285 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
3286 unsigned int to
, struct skb_seq_state
*st
)
3288 st
->lower_offset
= from
;
3289 st
->upper_offset
= to
;
3290 st
->root_skb
= st
->cur_skb
= skb
;
3291 st
->frag_idx
= st
->stepped_offset
= 0;
3292 st
->frag_data
= NULL
;
3294 EXPORT_SYMBOL(skb_prepare_seq_read
);
3297 * skb_seq_read - Sequentially read skb data
3298 * @consumed: number of bytes consumed by the caller so far
3299 * @data: destination pointer for data to be returned
3300 * @st: state variable
3302 * Reads a block of skb data at @consumed relative to the
3303 * lower offset specified to skb_prepare_seq_read(). Assigns
3304 * the head of the data block to @data and returns the length
3305 * of the block or 0 if the end of the skb data or the upper
3306 * offset has been reached.
3308 * The caller is not required to consume all of the data
3309 * returned, i.e. @consumed is typically set to the number
3310 * of bytes already consumed and the next call to
3311 * skb_seq_read() will return the remaining part of the block.
3313 * Note 1: The size of each block of data returned can be arbitrary,
3314 * this limitation is the cost for zerocopy sequential
3315 * reads of potentially non linear data.
3317 * Note 2: Fragment lists within fragments are not implemented
3318 * at the moment, state->root_skb could be replaced with
3319 * a stack for this purpose.
3321 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
3322 struct skb_seq_state
*st
)
3324 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
3327 if (unlikely(abs_offset
>= st
->upper_offset
)) {
3328 if (st
->frag_data
) {
3329 kunmap_atomic(st
->frag_data
);
3330 st
->frag_data
= NULL
;
3336 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
3338 if (abs_offset
< block_limit
&& !st
->frag_data
) {
3339 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
3340 return block_limit
- abs_offset
;
3343 if (st
->frag_idx
== 0 && !st
->frag_data
)
3344 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
3346 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
3347 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
3348 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
3350 if (abs_offset
< block_limit
) {
3352 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
3354 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
3355 (abs_offset
- st
->stepped_offset
);
3357 return block_limit
- abs_offset
;
3360 if (st
->frag_data
) {
3361 kunmap_atomic(st
->frag_data
);
3362 st
->frag_data
= NULL
;
3366 st
->stepped_offset
+= skb_frag_size(frag
);
3369 if (st
->frag_data
) {
3370 kunmap_atomic(st
->frag_data
);
3371 st
->frag_data
= NULL
;
3374 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
3375 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
3378 } else if (st
->cur_skb
->next
) {
3379 st
->cur_skb
= st
->cur_skb
->next
;
3386 EXPORT_SYMBOL(skb_seq_read
);
3389 * skb_abort_seq_read - Abort a sequential read of skb data
3390 * @st: state variable
3392 * Must be called if skb_seq_read() was not called until it
3395 void skb_abort_seq_read(struct skb_seq_state
*st
)
3398 kunmap_atomic(st
->frag_data
);
3400 EXPORT_SYMBOL(skb_abort_seq_read
);
3402 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3404 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
3405 struct ts_config
*conf
,
3406 struct ts_state
*state
)
3408 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
3411 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
3413 skb_abort_seq_read(TS_SKB_CB(state
));
3417 * skb_find_text - Find a text pattern in skb data
3418 * @skb: the buffer to look in
3419 * @from: search offset
3421 * @config: textsearch configuration
3423 * Finds a pattern in the skb data according to the specified
3424 * textsearch configuration. Use textsearch_next() to retrieve
3425 * subsequent occurrences of the pattern. Returns the offset
3426 * to the first occurrence or UINT_MAX if no match was found.
3428 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
3429 unsigned int to
, struct ts_config
*config
)
3431 struct ts_state state
;
3434 config
->get_next_block
= skb_ts_get_next_block
;
3435 config
->finish
= skb_ts_finish
;
3437 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(&state
));
3439 ret
= textsearch_find(config
, &state
);
3440 return (ret
<= to
- from
? ret
: UINT_MAX
);
3442 EXPORT_SYMBOL(skb_find_text
);
3444 int skb_append_pagefrags(struct sk_buff
*skb
, struct page
*page
,
3445 int offset
, size_t size
)
3447 int i
= skb_shinfo(skb
)->nr_frags
;
3449 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
3450 skb_frag_size_add(&skb_shinfo(skb
)->frags
[i
- 1], size
);
3451 } else if (i
< MAX_SKB_FRAGS
) {
3453 skb_fill_page_desc(skb
, i
, page
, offset
, size
);
3460 EXPORT_SYMBOL_GPL(skb_append_pagefrags
);
3463 * skb_pull_rcsum - pull skb and update receive checksum
3464 * @skb: buffer to update
3465 * @len: length of data pulled
3467 * This function performs an skb_pull on the packet and updates
3468 * the CHECKSUM_COMPLETE checksum. It should be used on
3469 * receive path processing instead of skb_pull unless you know
3470 * that the checksum difference is zero (e.g., a valid IP header)
3471 * or you are setting ip_summed to CHECKSUM_NONE.
3473 void *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
3475 unsigned char *data
= skb
->data
;
3477 BUG_ON(len
> skb
->len
);
3478 __skb_pull(skb
, len
);
3479 skb_postpull_rcsum(skb
, data
, len
);
3482 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
3484 static inline skb_frag_t
skb_head_frag_to_page_desc(struct sk_buff
*frag_skb
)
3486 skb_frag_t head_frag
;
3489 page
= virt_to_head_page(frag_skb
->head
);
3490 head_frag
.page
.p
= page
;
3491 head_frag
.page_offset
= frag_skb
->data
-
3492 (unsigned char *)page_address(page
);
3493 head_frag
.size
= skb_headlen(frag_skb
);
3498 * skb_segment - Perform protocol segmentation on skb.
3499 * @head_skb: buffer to segment
3500 * @features: features for the output path (see dev->features)
3502 * This function performs segmentation on the given skb. It returns
3503 * a pointer to the first in a list of new skbs for the segments.
3504 * In case of error it returns ERR_PTR(err).
3506 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
3507 netdev_features_t features
)
3509 struct sk_buff
*segs
= NULL
;
3510 struct sk_buff
*tail
= NULL
;
3511 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
3512 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
3513 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
3514 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
3515 struct sk_buff
*frag_skb
= head_skb
;
3516 unsigned int offset
= doffset
;
3517 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
3518 unsigned int partial_segs
= 0;
3519 unsigned int headroom
;
3520 unsigned int len
= head_skb
->len
;
3523 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
3529 __skb_push(head_skb
, doffset
);
3530 proto
= skb_network_protocol(head_skb
, &dummy
);
3531 if (unlikely(!proto
))
3532 return ERR_PTR(-EINVAL
);
3534 sg
= !!(features
& NETIF_F_SG
);
3535 csum
= !!can_checksum_protocol(features
, proto
);
3537 if (sg
&& csum
&& (mss
!= GSO_BY_FRAGS
)) {
3538 if (!(features
& NETIF_F_GSO_PARTIAL
)) {
3539 struct sk_buff
*iter
;
3540 unsigned int frag_len
;
3543 !net_gso_ok(features
, skb_shinfo(head_skb
)->gso_type
))
3546 /* If we get here then all the required
3547 * GSO features except frag_list are supported.
3548 * Try to split the SKB to multiple GSO SKBs
3549 * with no frag_list.
3550 * Currently we can do that only when the buffers don't
3551 * have a linear part and all the buffers except
3552 * the last are of the same length.
3554 frag_len
= list_skb
->len
;
3555 skb_walk_frags(head_skb
, iter
) {
3556 if (frag_len
!= iter
->len
&& iter
->next
)
3558 if (skb_headlen(iter
) && !iter
->head_frag
)
3564 if (len
!= frag_len
)
3568 /* GSO partial only requires that we trim off any excess that
3569 * doesn't fit into an MSS sized block, so take care of that
3572 partial_segs
= len
/ mss
;
3573 if (partial_segs
> 1)
3574 mss
*= partial_segs
;
3580 headroom
= skb_headroom(head_skb
);
3581 pos
= skb_headlen(head_skb
);
3584 struct sk_buff
*nskb
;
3585 skb_frag_t
*nskb_frag
;
3589 if (unlikely(mss
== GSO_BY_FRAGS
)) {
3590 len
= list_skb
->len
;
3592 len
= head_skb
->len
- offset
;
3597 hsize
= skb_headlen(head_skb
) - offset
;
3600 if (hsize
> len
|| !sg
)
3603 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
3604 (skb_headlen(list_skb
) == len
|| sg
)) {
3605 BUG_ON(skb_headlen(list_skb
) > len
);
3608 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3609 frag
= skb_shinfo(list_skb
)->frags
;
3610 frag_skb
= list_skb
;
3611 pos
+= skb_headlen(list_skb
);
3613 while (pos
< offset
+ len
) {
3614 BUG_ON(i
>= nfrags
);
3616 size
= skb_frag_size(frag
);
3617 if (pos
+ size
> offset
+ len
)
3625 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
3626 list_skb
= list_skb
->next
;
3628 if (unlikely(!nskb
))
3631 if (unlikely(pskb_trim(nskb
, len
))) {
3636 hsize
= skb_end_offset(nskb
);
3637 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
3642 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
3643 skb_release_head_state(nskb
);
3644 __skb_push(nskb
, doffset
);
3646 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
3647 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
3650 if (unlikely(!nskb
))
3653 skb_reserve(nskb
, headroom
);
3654 __skb_put(nskb
, doffset
);
3663 __copy_skb_header(nskb
, head_skb
);
3665 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
3666 skb_reset_mac_len(nskb
);
3668 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
3669 nskb
->data
- tnl_hlen
,
3670 doffset
+ tnl_hlen
);
3672 if (nskb
->len
== len
+ doffset
)
3673 goto perform_csum_check
;
3676 if (!nskb
->remcsum_offload
)
3677 nskb
->ip_summed
= CHECKSUM_NONE
;
3678 SKB_GSO_CB(nskb
)->csum
=
3679 skb_copy_and_csum_bits(head_skb
, offset
,
3682 SKB_GSO_CB(nskb
)->csum_start
=
3683 skb_headroom(nskb
) + doffset
;
3687 nskb_frag
= skb_shinfo(nskb
)->frags
;
3689 skb_copy_from_linear_data_offset(head_skb
, offset
,
3690 skb_put(nskb
, hsize
), hsize
);
3692 skb_shinfo(nskb
)->tx_flags
|= skb_shinfo(head_skb
)->tx_flags
&
3695 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3696 skb_zerocopy_clone(nskb
, frag_skb
, GFP_ATOMIC
))
3699 while (pos
< offset
+ len
) {
3702 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3703 frag
= skb_shinfo(list_skb
)->frags
;
3704 frag_skb
= list_skb
;
3705 if (!skb_headlen(list_skb
)) {
3708 BUG_ON(!list_skb
->head_frag
);
3710 /* to make room for head_frag. */
3714 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3715 skb_zerocopy_clone(nskb
, frag_skb
,
3719 list_skb
= list_skb
->next
;
3722 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3724 net_warn_ratelimited(
3725 "skb_segment: too many frags: %u %u\n",
3731 *nskb_frag
= (i
< 0) ? skb_head_frag_to_page_desc(frag_skb
) : *frag
;
3732 __skb_frag_ref(nskb_frag
);
3733 size
= skb_frag_size(nskb_frag
);
3736 nskb_frag
->page_offset
+= offset
- pos
;
3737 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3740 skb_shinfo(nskb
)->nr_frags
++;
3742 if (pos
+ size
<= offset
+ len
) {
3747 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3755 nskb
->data_len
= len
- hsize
;
3756 nskb
->len
+= nskb
->data_len
;
3757 nskb
->truesize
+= nskb
->data_len
;
3761 if (skb_has_shared_frag(nskb
) &&
3762 __skb_linearize(nskb
))
3765 if (!nskb
->remcsum_offload
)
3766 nskb
->ip_summed
= CHECKSUM_NONE
;
3767 SKB_GSO_CB(nskb
)->csum
=
3768 skb_checksum(nskb
, doffset
,
3769 nskb
->len
- doffset
, 0);
3770 SKB_GSO_CB(nskb
)->csum_start
=
3771 skb_headroom(nskb
) + doffset
;
3773 } while ((offset
+= len
) < head_skb
->len
);
3775 /* Some callers want to get the end of the list.
3776 * Put it in segs->prev to avoid walking the list.
3777 * (see validate_xmit_skb_list() for example)
3782 struct sk_buff
*iter
;
3783 int type
= skb_shinfo(head_skb
)->gso_type
;
3784 unsigned short gso_size
= skb_shinfo(head_skb
)->gso_size
;
3786 /* Update type to add partial and then remove dodgy if set */
3787 type
|= (features
& NETIF_F_GSO_PARTIAL
) / NETIF_F_GSO_PARTIAL
* SKB_GSO_PARTIAL
;
3788 type
&= ~SKB_GSO_DODGY
;
3790 /* Update GSO info and prepare to start updating headers on
3791 * our way back down the stack of protocols.
3793 for (iter
= segs
; iter
; iter
= iter
->next
) {
3794 skb_shinfo(iter
)->gso_size
= gso_size
;
3795 skb_shinfo(iter
)->gso_segs
= partial_segs
;
3796 skb_shinfo(iter
)->gso_type
= type
;
3797 SKB_GSO_CB(iter
)->data_offset
= skb_headroom(iter
) + doffset
;
3800 if (tail
->len
- doffset
<= gso_size
)
3801 skb_shinfo(tail
)->gso_size
= 0;
3802 else if (tail
!= segs
)
3803 skb_shinfo(tail
)->gso_segs
= DIV_ROUND_UP(tail
->len
- doffset
, gso_size
);
3806 /* Following permits correct backpressure, for protocols
3807 * using skb_set_owner_w().
3808 * Idea is to tranfert ownership from head_skb to last segment.
3810 if (head_skb
->destructor
== sock_wfree
) {
3811 swap(tail
->truesize
, head_skb
->truesize
);
3812 swap(tail
->destructor
, head_skb
->destructor
);
3813 swap(tail
->sk
, head_skb
->sk
);
3818 kfree_skb_list(segs
);
3819 return ERR_PTR(err
);
3821 EXPORT_SYMBOL_GPL(skb_segment
);
3823 int skb_gro_receive(struct sk_buff
*p
, struct sk_buff
*skb
)
3825 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3826 unsigned int offset
= skb_gro_offset(skb
);
3827 unsigned int headlen
= skb_headlen(skb
);
3828 unsigned int len
= skb_gro_len(skb
);
3829 unsigned int delta_truesize
;
3832 if (unlikely(p
->len
+ len
>= 65536 || NAPI_GRO_CB(skb
)->flush
))
3835 lp
= NAPI_GRO_CB(p
)->last
;
3836 pinfo
= skb_shinfo(lp
);
3838 if (headlen
<= offset
) {
3841 int i
= skbinfo
->nr_frags
;
3842 int nr_frags
= pinfo
->nr_frags
+ i
;
3844 if (nr_frags
> MAX_SKB_FRAGS
)
3848 pinfo
->nr_frags
= nr_frags
;
3849 skbinfo
->nr_frags
= 0;
3851 frag
= pinfo
->frags
+ nr_frags
;
3852 frag2
= skbinfo
->frags
+ i
;
3857 frag
->page_offset
+= offset
;
3858 skb_frag_size_sub(frag
, offset
);
3860 /* all fragments truesize : remove (head size + sk_buff) */
3861 delta_truesize
= skb
->truesize
-
3862 SKB_TRUESIZE(skb_end_offset(skb
));
3864 skb
->truesize
-= skb
->data_len
;
3865 skb
->len
-= skb
->data_len
;
3868 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3870 } else if (skb
->head_frag
) {
3871 int nr_frags
= pinfo
->nr_frags
;
3872 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3873 struct page
*page
= virt_to_head_page(skb
->head
);
3874 unsigned int first_size
= headlen
- offset
;
3875 unsigned int first_offset
;
3877 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3880 first_offset
= skb
->data
-
3881 (unsigned char *)page_address(page
) +
3884 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3886 frag
->page
.p
= page
;
3887 frag
->page_offset
= first_offset
;
3888 skb_frag_size_set(frag
, first_size
);
3890 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3891 /* We dont need to clear skbinfo->nr_frags here */
3893 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3894 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3899 delta_truesize
= skb
->truesize
;
3900 if (offset
> headlen
) {
3901 unsigned int eat
= offset
- headlen
;
3903 skbinfo
->frags
[0].page_offset
+= eat
;
3904 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3905 skb
->data_len
-= eat
;
3910 __skb_pull(skb
, offset
);
3912 if (NAPI_GRO_CB(p
)->last
== p
)
3913 skb_shinfo(p
)->frag_list
= skb
;
3915 NAPI_GRO_CB(p
)->last
->next
= skb
;
3916 NAPI_GRO_CB(p
)->last
= skb
;
3917 __skb_header_release(skb
);
3921 NAPI_GRO_CB(p
)->count
++;
3923 p
->truesize
+= delta_truesize
;
3926 lp
->data_len
+= len
;
3927 lp
->truesize
+= delta_truesize
;
3930 NAPI_GRO_CB(skb
)->same_flow
= 1;
3933 EXPORT_SYMBOL_GPL(skb_gro_receive
);
3935 #ifdef CONFIG_SKB_EXTENSIONS
3936 #define SKB_EXT_ALIGN_VALUE 8
3937 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
3939 static const u8 skb_ext_type_len
[] = {
3940 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3941 [SKB_EXT_BRIDGE_NF
] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info
),
3944 [SKB_EXT_SEC_PATH
] = SKB_EXT_CHUNKSIZEOF(struct sec_path
),
3948 static __always_inline
unsigned int skb_ext_total_length(void)
3950 return SKB_EXT_CHUNKSIZEOF(struct skb_ext
) +
3951 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
3952 skb_ext_type_len
[SKB_EXT_BRIDGE_NF
] +
3955 skb_ext_type_len
[SKB_EXT_SEC_PATH
] +
3960 static void skb_extensions_init(void)
3962 BUILD_BUG_ON(SKB_EXT_NUM
>= 8);
3963 BUILD_BUG_ON(skb_ext_total_length() > 255);
3965 skbuff_ext_cache
= kmem_cache_create("skbuff_ext_cache",
3966 SKB_EXT_ALIGN_VALUE
* skb_ext_total_length(),
3968 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3972 static void skb_extensions_init(void) {}
3975 void __init
skb_init(void)
3977 skbuff_head_cache
= kmem_cache_create_usercopy("skbuff_head_cache",
3978 sizeof(struct sk_buff
),
3980 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3981 offsetof(struct sk_buff
, cb
),
3982 sizeof_field(struct sk_buff
, cb
),
3984 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3985 sizeof(struct sk_buff_fclones
),
3987 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3989 skb_extensions_init();
3993 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
,
3994 unsigned int recursion_level
)
3996 int start
= skb_headlen(skb
);
3997 int i
, copy
= start
- offset
;
3998 struct sk_buff
*frag_iter
;
4001 if (unlikely(recursion_level
>= 24))
4007 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
4009 if ((len
-= copy
) == 0)
4014 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
4017 WARN_ON(start
> offset
+ len
);
4019 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
4020 if ((copy
= end
- offset
) > 0) {
4021 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
4022 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4027 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
4028 frag
->page_offset
+offset
-start
);
4037 skb_walk_frags(skb
, frag_iter
) {
4040 WARN_ON(start
> offset
+ len
);
4042 end
= start
+ frag_iter
->len
;
4043 if ((copy
= end
- offset
) > 0) {
4044 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4049 ret
= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
4050 copy
, recursion_level
+ 1);
4051 if (unlikely(ret
< 0))
4054 if ((len
-= copy
) == 0)
4065 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4066 * @skb: Socket buffer containing the buffers to be mapped
4067 * @sg: The scatter-gather list to map into
4068 * @offset: The offset into the buffer's contents to start mapping
4069 * @len: Length of buffer space to be mapped
4071 * Fill the specified scatter-gather list with mappings/pointers into a
4072 * region of the buffer space attached to a socket buffer. Returns either
4073 * the number of scatterlist items used, or -EMSGSIZE if the contents
4076 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
4078 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4083 sg_mark_end(&sg
[nsg
- 1]);
4087 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
4089 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4090 * sglist without mark the sg which contain last skb data as the end.
4091 * So the caller can mannipulate sg list as will when padding new data after
4092 * the first call without calling sg_unmark_end to expend sg list.
4094 * Scenario to use skb_to_sgvec_nomark:
4096 * 2. skb_to_sgvec_nomark(payload1)
4097 * 3. skb_to_sgvec_nomark(payload2)
4099 * This is equivalent to:
4101 * 2. skb_to_sgvec(payload1)
4103 * 4. skb_to_sgvec(payload2)
4105 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4106 * is more preferable.
4108 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
4109 int offset
, int len
)
4111 return __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4113 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
4118 * skb_cow_data - Check that a socket buffer's data buffers are writable
4119 * @skb: The socket buffer to check.
4120 * @tailbits: Amount of trailing space to be added
4121 * @trailer: Returned pointer to the skb where the @tailbits space begins
4123 * Make sure that the data buffers attached to a socket buffer are
4124 * writable. If they are not, private copies are made of the data buffers
4125 * and the socket buffer is set to use these instead.
4127 * If @tailbits is given, make sure that there is space to write @tailbits
4128 * bytes of data beyond current end of socket buffer. @trailer will be
4129 * set to point to the skb in which this space begins.
4131 * The number of scatterlist elements required to completely map the
4132 * COW'd and extended socket buffer will be returned.
4134 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
4138 struct sk_buff
*skb1
, **skb_p
;
4140 /* If skb is cloned or its head is paged, reallocate
4141 * head pulling out all the pages (pages are considered not writable
4142 * at the moment even if they are anonymous).
4144 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
4145 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
4148 /* Easy case. Most of packets will go this way. */
4149 if (!skb_has_frag_list(skb
)) {
4150 /* A little of trouble, not enough of space for trailer.
4151 * This should not happen, when stack is tuned to generate
4152 * good frames. OK, on miss we reallocate and reserve even more
4153 * space, 128 bytes is fair. */
4155 if (skb_tailroom(skb
) < tailbits
&&
4156 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
4164 /* Misery. We are in troubles, going to mincer fragments... */
4167 skb_p
= &skb_shinfo(skb
)->frag_list
;
4170 while ((skb1
= *skb_p
) != NULL
) {
4173 /* The fragment is partially pulled by someone,
4174 * this can happen on input. Copy it and everything
4177 if (skb_shared(skb1
))
4180 /* If the skb is the last, worry about trailer. */
4182 if (skb1
->next
== NULL
&& tailbits
) {
4183 if (skb_shinfo(skb1
)->nr_frags
||
4184 skb_has_frag_list(skb1
) ||
4185 skb_tailroom(skb1
) < tailbits
)
4186 ntail
= tailbits
+ 128;
4192 skb_shinfo(skb1
)->nr_frags
||
4193 skb_has_frag_list(skb1
)) {
4194 struct sk_buff
*skb2
;
4196 /* Fuck, we are miserable poor guys... */
4198 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
4200 skb2
= skb_copy_expand(skb1
,
4204 if (unlikely(skb2
== NULL
))
4208 skb_set_owner_w(skb2
, skb1
->sk
);
4210 /* Looking around. Are we still alive?
4211 * OK, link new skb, drop old one */
4213 skb2
->next
= skb1
->next
;
4220 skb_p
= &skb1
->next
;
4225 EXPORT_SYMBOL_GPL(skb_cow_data
);
4227 static void sock_rmem_free(struct sk_buff
*skb
)
4229 struct sock
*sk
= skb
->sk
;
4231 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
4234 static void skb_set_err_queue(struct sk_buff
*skb
)
4236 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4237 * So, it is safe to (mis)use it to mark skbs on the error queue.
4239 skb
->pkt_type
= PACKET_OUTGOING
;
4240 BUILD_BUG_ON(PACKET_OUTGOING
== 0);
4244 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4246 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
4248 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
4249 (unsigned int)sk
->sk_rcvbuf
)
4254 skb
->destructor
= sock_rmem_free
;
4255 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
4256 skb_set_err_queue(skb
);
4258 /* before exiting rcu section, make sure dst is refcounted */
4261 skb_queue_tail(&sk
->sk_error_queue
, skb
);
4262 if (!sock_flag(sk
, SOCK_DEAD
))
4263 sk
->sk_error_report(sk
);
4266 EXPORT_SYMBOL(sock_queue_err_skb
);
4268 static bool is_icmp_err_skb(const struct sk_buff
*skb
)
4270 return skb
&& (SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
4271 SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
);
4274 struct sk_buff
*sock_dequeue_err_skb(struct sock
*sk
)
4276 struct sk_buff_head
*q
= &sk
->sk_error_queue
;
4277 struct sk_buff
*skb
, *skb_next
= NULL
;
4278 bool icmp_next
= false;
4279 unsigned long flags
;
4281 spin_lock_irqsave(&q
->lock
, flags
);
4282 skb
= __skb_dequeue(q
);
4283 if (skb
&& (skb_next
= skb_peek(q
))) {
4284 icmp_next
= is_icmp_err_skb(skb_next
);
4286 sk
->sk_err
= SKB_EXT_ERR(skb_next
)->ee
.ee_origin
;
4288 spin_unlock_irqrestore(&q
->lock
, flags
);
4290 if (is_icmp_err_skb(skb
) && !icmp_next
)
4294 sk
->sk_error_report(sk
);
4298 EXPORT_SYMBOL(sock_dequeue_err_skb
);
4301 * skb_clone_sk - create clone of skb, and take reference to socket
4302 * @skb: the skb to clone
4304 * This function creates a clone of a buffer that holds a reference on
4305 * sk_refcnt. Buffers created via this function are meant to be
4306 * returned using sock_queue_err_skb, or free via kfree_skb.
4308 * When passing buffers allocated with this function to sock_queue_err_skb
4309 * it is necessary to wrap the call with sock_hold/sock_put in order to
4310 * prevent the socket from being released prior to being enqueued on
4311 * the sk_error_queue.
4313 struct sk_buff
*skb_clone_sk(struct sk_buff
*skb
)
4315 struct sock
*sk
= skb
->sk
;
4316 struct sk_buff
*clone
;
4318 if (!sk
|| !refcount_inc_not_zero(&sk
->sk_refcnt
))
4321 clone
= skb_clone(skb
, GFP_ATOMIC
);
4328 clone
->destructor
= sock_efree
;
4332 EXPORT_SYMBOL(skb_clone_sk
);
4334 static void __skb_complete_tx_timestamp(struct sk_buff
*skb
,
4339 struct sock_exterr_skb
*serr
;
4342 BUILD_BUG_ON(sizeof(struct sock_exterr_skb
) > sizeof(skb
->cb
));
4344 serr
= SKB_EXT_ERR(skb
);
4345 memset(serr
, 0, sizeof(*serr
));
4346 serr
->ee
.ee_errno
= ENOMSG
;
4347 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
4348 serr
->ee
.ee_info
= tstype
;
4349 serr
->opt_stats
= opt_stats
;
4350 serr
->header
.h4
.iif
= skb
->dev
? skb
->dev
->ifindex
: 0;
4351 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
4352 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
4353 if (sk
->sk_protocol
== IPPROTO_TCP
&&
4354 sk
->sk_type
== SOCK_STREAM
)
4355 serr
->ee
.ee_data
-= sk
->sk_tskey
;
4358 err
= sock_queue_err_skb(sk
, skb
);
4364 static bool skb_may_tx_timestamp(struct sock
*sk
, bool tsonly
)
4368 if (likely(sysctl_tstamp_allow_data
|| tsonly
))
4371 read_lock_bh(&sk
->sk_callback_lock
);
4372 ret
= sk
->sk_socket
&& sk
->sk_socket
->file
&&
4373 file_ns_capable(sk
->sk_socket
->file
, &init_user_ns
, CAP_NET_RAW
);
4374 read_unlock_bh(&sk
->sk_callback_lock
);
4378 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
4379 struct skb_shared_hwtstamps
*hwtstamps
)
4381 struct sock
*sk
= skb
->sk
;
4383 if (!skb_may_tx_timestamp(sk
, false))
4386 /* Take a reference to prevent skb_orphan() from freeing the socket,
4387 * but only if the socket refcount is not zero.
4389 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4390 *skb_hwtstamps(skb
) = *hwtstamps
;
4391 __skb_complete_tx_timestamp(skb
, sk
, SCM_TSTAMP_SND
, false);
4399 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp
);
4401 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
4402 struct skb_shared_hwtstamps
*hwtstamps
,
4403 struct sock
*sk
, int tstype
)
4405 struct sk_buff
*skb
;
4406 bool tsonly
, opt_stats
= false;
4411 if (!hwtstamps
&& !(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TX_SWHW
) &&
4412 skb_shinfo(orig_skb
)->tx_flags
& SKBTX_IN_PROGRESS
)
4415 tsonly
= sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TSONLY
;
4416 if (!skb_may_tx_timestamp(sk
, tsonly
))
4421 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_STATS
) &&
4422 sk
->sk_protocol
== IPPROTO_TCP
&&
4423 sk
->sk_type
== SOCK_STREAM
) {
4424 skb
= tcp_get_timestamping_opt_stats(sk
);
4428 skb
= alloc_skb(0, GFP_ATOMIC
);
4430 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
4436 skb_shinfo(skb
)->tx_flags
|= skb_shinfo(orig_skb
)->tx_flags
&
4438 skb_shinfo(skb
)->tskey
= skb_shinfo(orig_skb
)->tskey
;
4442 *skb_hwtstamps(skb
) = *hwtstamps
;
4444 skb
->tstamp
= ktime_get_real();
4446 __skb_complete_tx_timestamp(skb
, sk
, tstype
, opt_stats
);
4448 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
4450 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
4451 struct skb_shared_hwtstamps
*hwtstamps
)
4453 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
4456 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
4458 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
4460 struct sock
*sk
= skb
->sk
;
4461 struct sock_exterr_skb
*serr
;
4464 skb
->wifi_acked_valid
= 1;
4465 skb
->wifi_acked
= acked
;
4467 serr
= SKB_EXT_ERR(skb
);
4468 memset(serr
, 0, sizeof(*serr
));
4469 serr
->ee
.ee_errno
= ENOMSG
;
4470 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
4472 /* Take a reference to prevent skb_orphan() from freeing the socket,
4473 * but only if the socket refcount is not zero.
4475 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4476 err
= sock_queue_err_skb(sk
, skb
);
4482 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
4485 * skb_partial_csum_set - set up and verify partial csum values for packet
4486 * @skb: the skb to set
4487 * @start: the number of bytes after skb->data to start checksumming.
4488 * @off: the offset from start to place the checksum.
4490 * For untrusted partially-checksummed packets, we need to make sure the values
4491 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4493 * This function checks and sets those values and skb->ip_summed: if this
4494 * returns false you should drop the packet.
4496 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
4498 u32 csum_end
= (u32
)start
+ (u32
)off
+ sizeof(__sum16
);
4499 u32 csum_start
= skb_headroom(skb
) + (u32
)start
;
4501 if (unlikely(csum_start
> U16_MAX
|| csum_end
> skb_headlen(skb
))) {
4502 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4503 start
, off
, skb_headroom(skb
), skb_headlen(skb
));
4506 skb
->ip_summed
= CHECKSUM_PARTIAL
;
4507 skb
->csum_start
= csum_start
;
4508 skb
->csum_offset
= off
;
4509 skb_set_transport_header(skb
, start
);
4512 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
4514 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
4517 if (skb_headlen(skb
) >= len
)
4520 /* If we need to pullup then pullup to the max, so we
4521 * won't need to do it again.
4526 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
4529 if (skb_headlen(skb
) < len
)
4535 #define MAX_TCP_HDR_LEN (15 * 4)
4537 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
4538 typeof(IPPROTO_IP
) proto
,
4545 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
4546 off
+ MAX_TCP_HDR_LEN
);
4547 if (!err
&& !skb_partial_csum_set(skb
, off
,
4548 offsetof(struct tcphdr
,
4551 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
4554 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
4555 off
+ sizeof(struct udphdr
));
4556 if (!err
&& !skb_partial_csum_set(skb
, off
,
4557 offsetof(struct udphdr
,
4560 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
4563 return ERR_PTR(-EPROTO
);
4566 /* This value should be large enough to cover a tagged ethernet header plus
4567 * maximally sized IP and TCP or UDP headers.
4569 #define MAX_IP_HDR_LEN 128
4571 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
4580 err
= skb_maybe_pull_tail(skb
,
4581 sizeof(struct iphdr
),
4586 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
4589 off
= ip_hdrlen(skb
);
4596 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
4598 return PTR_ERR(csum
);
4601 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
4604 ip_hdr(skb
)->protocol
, 0);
4611 /* This value should be large enough to cover a tagged ethernet header plus
4612 * an IPv6 header, all options, and a maximal TCP or UDP header.
4614 #define MAX_IPV6_HDR_LEN 256
4616 #define OPT_HDR(type, skb, off) \
4617 (type *)(skb_network_header(skb) + (off))
4619 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
4632 off
= sizeof(struct ipv6hdr
);
4634 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
4638 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
4640 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
4641 while (off
<= len
&& !done
) {
4643 case IPPROTO_DSTOPTS
:
4644 case IPPROTO_HOPOPTS
:
4645 case IPPROTO_ROUTING
: {
4646 struct ipv6_opt_hdr
*hp
;
4648 err
= skb_maybe_pull_tail(skb
,
4650 sizeof(struct ipv6_opt_hdr
),
4655 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
4656 nexthdr
= hp
->nexthdr
;
4657 off
+= ipv6_optlen(hp
);
4661 struct ip_auth_hdr
*hp
;
4663 err
= skb_maybe_pull_tail(skb
,
4665 sizeof(struct ip_auth_hdr
),
4670 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
4671 nexthdr
= hp
->nexthdr
;
4672 off
+= ipv6_authlen(hp
);
4675 case IPPROTO_FRAGMENT
: {
4676 struct frag_hdr
*hp
;
4678 err
= skb_maybe_pull_tail(skb
,
4680 sizeof(struct frag_hdr
),
4685 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
4687 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
4690 nexthdr
= hp
->nexthdr
;
4691 off
+= sizeof(struct frag_hdr
);
4702 if (!done
|| fragment
)
4705 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
4707 return PTR_ERR(csum
);
4710 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
4711 &ipv6_hdr(skb
)->daddr
,
4712 skb
->len
- off
, nexthdr
, 0);
4720 * skb_checksum_setup - set up partial checksum offset
4721 * @skb: the skb to set up
4722 * @recalculate: if true the pseudo-header checksum will be recalculated
4724 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
4728 switch (skb
->protocol
) {
4729 case htons(ETH_P_IP
):
4730 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
4733 case htons(ETH_P_IPV6
):
4734 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
4744 EXPORT_SYMBOL(skb_checksum_setup
);
4747 * skb_checksum_maybe_trim - maybe trims the given skb
4748 * @skb: the skb to check
4749 * @transport_len: the data length beyond the network header
4751 * Checks whether the given skb has data beyond the given transport length.
4752 * If so, returns a cloned skb trimmed to this transport length.
4753 * Otherwise returns the provided skb. Returns NULL in error cases
4754 * (e.g. transport_len exceeds skb length or out-of-memory).
4756 * Caller needs to set the skb transport header and free any returned skb if it
4757 * differs from the provided skb.
4759 static struct sk_buff
*skb_checksum_maybe_trim(struct sk_buff
*skb
,
4760 unsigned int transport_len
)
4762 struct sk_buff
*skb_chk
;
4763 unsigned int len
= skb_transport_offset(skb
) + transport_len
;
4768 else if (skb
->len
== len
)
4771 skb_chk
= skb_clone(skb
, GFP_ATOMIC
);
4775 ret
= pskb_trim_rcsum(skb_chk
, len
);
4785 * skb_checksum_trimmed - validate checksum of an skb
4786 * @skb: the skb to check
4787 * @transport_len: the data length beyond the network header
4788 * @skb_chkf: checksum function to use
4790 * Applies the given checksum function skb_chkf to the provided skb.
4791 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4793 * If the skb has data beyond the given transport length, then a
4794 * trimmed & cloned skb is checked and returned.
4796 * Caller needs to set the skb transport header and free any returned skb if it
4797 * differs from the provided skb.
4799 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
4800 unsigned int transport_len
,
4801 __sum16(*skb_chkf
)(struct sk_buff
*skb
))
4803 struct sk_buff
*skb_chk
;
4804 unsigned int offset
= skb_transport_offset(skb
);
4807 skb_chk
= skb_checksum_maybe_trim(skb
, transport_len
);
4811 if (!pskb_may_pull(skb_chk
, offset
))
4814 skb_pull_rcsum(skb_chk
, offset
);
4815 ret
= skb_chkf(skb_chk
);
4816 skb_push_rcsum(skb_chk
, offset
);
4824 if (skb_chk
&& skb_chk
!= skb
)
4830 EXPORT_SYMBOL(skb_checksum_trimmed
);
4832 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
4834 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4837 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
4839 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
4842 skb_release_head_state(skb
);
4843 kmem_cache_free(skbuff_head_cache
, skb
);
4848 EXPORT_SYMBOL(kfree_skb_partial
);
4851 * skb_try_coalesce - try to merge skb to prior one
4853 * @from: buffer to add
4854 * @fragstolen: pointer to boolean
4855 * @delta_truesize: how much more was allocated than was requested
4857 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
4858 bool *fragstolen
, int *delta_truesize
)
4860 struct skb_shared_info
*to_shinfo
, *from_shinfo
;
4861 int i
, delta
, len
= from
->len
;
4863 *fragstolen
= false;
4868 if (len
<= skb_tailroom(to
)) {
4870 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
4871 *delta_truesize
= 0;
4875 to_shinfo
= skb_shinfo(to
);
4876 from_shinfo
= skb_shinfo(from
);
4877 if (to_shinfo
->frag_list
|| from_shinfo
->frag_list
)
4879 if (skb_zcopy(to
) || skb_zcopy(from
))
4882 if (skb_headlen(from
) != 0) {
4884 unsigned int offset
;
4886 if (to_shinfo
->nr_frags
+
4887 from_shinfo
->nr_frags
>= MAX_SKB_FRAGS
)
4890 if (skb_head_is_locked(from
))
4893 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
4895 page
= virt_to_head_page(from
->head
);
4896 offset
= from
->data
- (unsigned char *)page_address(page
);
4898 skb_fill_page_desc(to
, to_shinfo
->nr_frags
,
4899 page
, offset
, skb_headlen(from
));
4902 if (to_shinfo
->nr_frags
+
4903 from_shinfo
->nr_frags
> MAX_SKB_FRAGS
)
4906 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
4909 WARN_ON_ONCE(delta
< len
);
4911 memcpy(to_shinfo
->frags
+ to_shinfo
->nr_frags
,
4913 from_shinfo
->nr_frags
* sizeof(skb_frag_t
));
4914 to_shinfo
->nr_frags
+= from_shinfo
->nr_frags
;
4916 if (!skb_cloned(from
))
4917 from_shinfo
->nr_frags
= 0;
4919 /* if the skb is not cloned this does nothing
4920 * since we set nr_frags to 0.
4922 for (i
= 0; i
< from_shinfo
->nr_frags
; i
++)
4923 __skb_frag_ref(&from_shinfo
->frags
[i
]);
4925 to
->truesize
+= delta
;
4927 to
->data_len
+= len
;
4929 *delta_truesize
= delta
;
4932 EXPORT_SYMBOL(skb_try_coalesce
);
4935 * skb_scrub_packet - scrub an skb
4937 * @skb: buffer to clean
4938 * @xnet: packet is crossing netns
4940 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4941 * into/from a tunnel. Some information have to be cleared during these
4943 * skb_scrub_packet can also be used to clean a skb before injecting it in
4944 * another namespace (@xnet == true). We have to clear all information in the
4945 * skb that could impact namespace isolation.
4947 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
4949 skb
->pkt_type
= PACKET_HOST
;
4955 nf_reset_trace(skb
);
4957 #ifdef CONFIG_NET_SWITCHDEV
4958 skb
->offload_fwd_mark
= 0;
4959 skb
->offload_l3_fwd_mark
= 0;
4969 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
4972 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4976 * skb_gso_transport_seglen is used to determine the real size of the
4977 * individual segments, including Layer4 headers (TCP/UDP).
4979 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4981 static unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
4983 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
4984 unsigned int thlen
= 0;
4986 if (skb
->encapsulation
) {
4987 thlen
= skb_inner_transport_header(skb
) -
4988 skb_transport_header(skb
);
4990 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
4991 thlen
+= inner_tcp_hdrlen(skb
);
4992 } else if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
4993 thlen
= tcp_hdrlen(skb
);
4994 } else if (unlikely(skb_is_gso_sctp(skb
))) {
4995 thlen
= sizeof(struct sctphdr
);
4996 } else if (shinfo
->gso_type
& SKB_GSO_UDP_L4
) {
4997 thlen
= sizeof(struct udphdr
);
4999 /* UFO sets gso_size to the size of the fragmentation
5000 * payload, i.e. the size of the L4 (UDP) header is already
5003 return thlen
+ shinfo
->gso_size
;
5007 * skb_gso_network_seglen - Return length of individual segments of a gso packet
5011 * skb_gso_network_seglen is used to determine the real size of the
5012 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5014 * The MAC/L2 header is not accounted for.
5016 static unsigned int skb_gso_network_seglen(const struct sk_buff
*skb
)
5018 unsigned int hdr_len
= skb_transport_header(skb
) -
5019 skb_network_header(skb
);
5021 return hdr_len
+ skb_gso_transport_seglen(skb
);
5025 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5029 * skb_gso_mac_seglen is used to determine the real size of the
5030 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5031 * headers (TCP/UDP).
5033 static unsigned int skb_gso_mac_seglen(const struct sk_buff
*skb
)
5035 unsigned int hdr_len
= skb_transport_header(skb
) - skb_mac_header(skb
);
5037 return hdr_len
+ skb_gso_transport_seglen(skb
);
5041 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5043 * There are a couple of instances where we have a GSO skb, and we
5044 * want to determine what size it would be after it is segmented.
5046 * We might want to check:
5047 * - L3+L4+payload size (e.g. IP forwarding)
5048 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5050 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5054 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5055 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5057 * @max_len: The maximum permissible length.
5059 * Returns true if the segmented length <= max length.
5061 static inline bool skb_gso_size_check(const struct sk_buff
*skb
,
5062 unsigned int seg_len
,
5063 unsigned int max_len
) {
5064 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
5065 const struct sk_buff
*iter
;
5067 if (shinfo
->gso_size
!= GSO_BY_FRAGS
)
5068 return seg_len
<= max_len
;
5070 /* Undo this so we can re-use header sizes */
5071 seg_len
-= GSO_BY_FRAGS
;
5073 skb_walk_frags(skb
, iter
) {
5074 if (seg_len
+ skb_headlen(iter
) > max_len
)
5082 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5085 * @mtu: MTU to validate against
5087 * skb_gso_validate_network_len validates if a given skb will fit a
5088 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5091 bool skb_gso_validate_network_len(const struct sk_buff
*skb
, unsigned int mtu
)
5093 return skb_gso_size_check(skb
, skb_gso_network_seglen(skb
), mtu
);
5095 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len
);
5098 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5101 * @len: length to validate against
5103 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5104 * length once split, including L2, L3 and L4 headers and the payload.
5106 bool skb_gso_validate_mac_len(const struct sk_buff
*skb
, unsigned int len
)
5108 return skb_gso_size_check(skb
, skb_gso_mac_seglen(skb
), len
);
5110 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len
);
5112 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
5114 int mac_len
, meta_len
;
5117 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
5122 mac_len
= skb
->data
- skb_mac_header(skb
);
5123 if (likely(mac_len
> VLAN_HLEN
+ ETH_TLEN
)) {
5124 memmove(skb_mac_header(skb
) + VLAN_HLEN
, skb_mac_header(skb
),
5125 mac_len
- VLAN_HLEN
- ETH_TLEN
);
5128 meta_len
= skb_metadata_len(skb
);
5130 meta
= skb_metadata_end(skb
) - meta_len
;
5131 memmove(meta
+ VLAN_HLEN
, meta
, meta_len
);
5134 skb
->mac_header
+= VLAN_HLEN
;
5138 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
5140 struct vlan_hdr
*vhdr
;
5143 if (unlikely(skb_vlan_tag_present(skb
))) {
5144 /* vlan_tci is already set-up so leave this for another time */
5148 skb
= skb_share_check(skb
, GFP_ATOMIC
);
5152 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
5155 vhdr
= (struct vlan_hdr
*)skb
->data
;
5156 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5157 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
5159 skb_pull_rcsum(skb
, VLAN_HLEN
);
5160 vlan_set_encap_proto(skb
, vhdr
);
5162 skb
= skb_reorder_vlan_header(skb
);
5166 skb_reset_network_header(skb
);
5167 skb_reset_transport_header(skb
);
5168 skb_reset_mac_len(skb
);
5176 EXPORT_SYMBOL(skb_vlan_untag
);
5178 int skb_ensure_writable(struct sk_buff
*skb
, int write_len
)
5180 if (!pskb_may_pull(skb
, write_len
))
5183 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
5186 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
5188 EXPORT_SYMBOL(skb_ensure_writable
);
5190 /* remove VLAN header from packet and update csum accordingly.
5191 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5193 int __skb_vlan_pop(struct sk_buff
*skb
, u16
*vlan_tci
)
5195 struct vlan_hdr
*vhdr
;
5196 int offset
= skb
->data
- skb_mac_header(skb
);
5199 if (WARN_ONCE(offset
,
5200 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5205 err
= skb_ensure_writable(skb
, VLAN_ETH_HLEN
);
5209 skb_postpull_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5211 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
5212 *vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5214 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
5215 __skb_pull(skb
, VLAN_HLEN
);
5217 vlan_set_encap_proto(skb
, vhdr
);
5218 skb
->mac_header
+= VLAN_HLEN
;
5220 if (skb_network_offset(skb
) < ETH_HLEN
)
5221 skb_set_network_header(skb
, ETH_HLEN
);
5223 skb_reset_mac_len(skb
);
5227 EXPORT_SYMBOL(__skb_vlan_pop
);
5229 /* Pop a vlan tag either from hwaccel or from payload.
5230 * Expects skb->data at mac header.
5232 int skb_vlan_pop(struct sk_buff
*skb
)
5238 if (likely(skb_vlan_tag_present(skb
))) {
5239 __vlan_hwaccel_clear_tag(skb
);
5241 if (unlikely(!eth_type_vlan(skb
->protocol
)))
5244 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5248 /* move next vlan tag to hw accel tag */
5249 if (likely(!eth_type_vlan(skb
->protocol
)))
5252 vlan_proto
= skb
->protocol
;
5253 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5257 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5260 EXPORT_SYMBOL(skb_vlan_pop
);
5262 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5263 * Expects skb->data at mac header.
5265 int skb_vlan_push(struct sk_buff
*skb
, __be16 vlan_proto
, u16 vlan_tci
)
5267 if (skb_vlan_tag_present(skb
)) {
5268 int offset
= skb
->data
- skb_mac_header(skb
);
5271 if (WARN_ONCE(offset
,
5272 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5277 err
= __vlan_insert_tag(skb
, skb
->vlan_proto
,
5278 skb_vlan_tag_get(skb
));
5282 skb
->protocol
= skb
->vlan_proto
;
5283 skb
->mac_len
+= VLAN_HLEN
;
5285 skb_postpush_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5287 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5290 EXPORT_SYMBOL(skb_vlan_push
);
5293 * alloc_skb_with_frags - allocate skb with page frags
5295 * @header_len: size of linear part
5296 * @data_len: needed length in frags
5297 * @max_page_order: max page order desired.
5298 * @errcode: pointer to error code if any
5299 * @gfp_mask: allocation mask
5301 * This can be used to allocate a paged skb, given a maximal order for frags.
5303 struct sk_buff
*alloc_skb_with_frags(unsigned long header_len
,
5304 unsigned long data_len
,
5309 int npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
5310 unsigned long chunk
;
5311 struct sk_buff
*skb
;
5315 *errcode
= -EMSGSIZE
;
5316 /* Note this test could be relaxed, if we succeed to allocate
5317 * high order pages...
5319 if (npages
> MAX_SKB_FRAGS
)
5322 *errcode
= -ENOBUFS
;
5323 skb
= alloc_skb(header_len
, gfp_mask
);
5327 skb
->truesize
+= npages
<< PAGE_SHIFT
;
5329 for (i
= 0; npages
> 0; i
++) {
5330 int order
= max_page_order
;
5333 if (npages
>= 1 << order
) {
5334 page
= alloc_pages((gfp_mask
& ~__GFP_DIRECT_RECLAIM
) |
5340 /* Do not retry other high order allocations */
5346 page
= alloc_page(gfp_mask
);
5350 chunk
= min_t(unsigned long, data_len
,
5351 PAGE_SIZE
<< order
);
5352 skb_fill_page_desc(skb
, i
, page
, 0, chunk
);
5354 npages
-= 1 << order
;
5362 EXPORT_SYMBOL(alloc_skb_with_frags
);
5364 /* carve out the first off bytes from skb when off < headlen */
5365 static int pskb_carve_inside_header(struct sk_buff
*skb
, const u32 off
,
5366 const int headlen
, gfp_t gfp_mask
)
5369 int size
= skb_end_offset(skb
);
5370 int new_hlen
= headlen
- off
;
5373 size
= SKB_DATA_ALIGN(size
);
5375 if (skb_pfmemalloc(skb
))
5376 gfp_mask
|= __GFP_MEMALLOC
;
5377 data
= kmalloc_reserve(size
+
5378 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5379 gfp_mask
, NUMA_NO_NODE
, NULL
);
5383 size
= SKB_WITH_OVERHEAD(ksize(data
));
5385 /* Copy real data, and all frags */
5386 skb_copy_from_linear_data_offset(skb
, off
, data
, new_hlen
);
5389 memcpy((struct skb_shared_info
*)(data
+ size
),
5391 offsetof(struct skb_shared_info
,
5392 frags
[skb_shinfo(skb
)->nr_frags
]));
5393 if (skb_cloned(skb
)) {
5394 /* drop the old head gracefully */
5395 if (skb_orphan_frags(skb
, gfp_mask
)) {
5399 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
5400 skb_frag_ref(skb
, i
);
5401 if (skb_has_frag_list(skb
))
5402 skb_clone_fraglist(skb
);
5403 skb_release_data(skb
);
5405 /* we can reuse existing recount- all we did was
5414 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5417 skb
->end
= skb
->head
+ size
;
5419 skb_set_tail_pointer(skb
, skb_headlen(skb
));
5420 skb_headers_offset_update(skb
, 0);
5424 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5429 static int pskb_carve(struct sk_buff
*skb
, const u32 off
, gfp_t gfp
);
5431 /* carve out the first eat bytes from skb's frag_list. May recurse into
5434 static int pskb_carve_frag_list(struct sk_buff
*skb
,
5435 struct skb_shared_info
*shinfo
, int eat
,
5438 struct sk_buff
*list
= shinfo
->frag_list
;
5439 struct sk_buff
*clone
= NULL
;
5440 struct sk_buff
*insp
= NULL
;
5444 pr_err("Not enough bytes to eat. Want %d\n", eat
);
5447 if (list
->len
<= eat
) {
5448 /* Eaten as whole. */
5453 /* Eaten partially. */
5454 if (skb_shared(list
)) {
5455 clone
= skb_clone(list
, gfp_mask
);
5461 /* This may be pulled without problems. */
5464 if (pskb_carve(list
, eat
, gfp_mask
) < 0) {
5472 /* Free pulled out fragments. */
5473 while ((list
= shinfo
->frag_list
) != insp
) {
5474 shinfo
->frag_list
= list
->next
;
5477 /* And insert new clone at head. */
5480 shinfo
->frag_list
= clone
;
5485 /* carve off first len bytes from skb. Split line (off) is in the
5486 * non-linear part of skb
5488 static int pskb_carve_inside_nonlinear(struct sk_buff
*skb
, const u32 off
,
5489 int pos
, gfp_t gfp_mask
)
5492 int size
= skb_end_offset(skb
);
5494 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
5495 struct skb_shared_info
*shinfo
;
5497 size
= SKB_DATA_ALIGN(size
);
5499 if (skb_pfmemalloc(skb
))
5500 gfp_mask
|= __GFP_MEMALLOC
;
5501 data
= kmalloc_reserve(size
+
5502 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5503 gfp_mask
, NUMA_NO_NODE
, NULL
);
5507 size
= SKB_WITH_OVERHEAD(ksize(data
));
5509 memcpy((struct skb_shared_info
*)(data
+ size
),
5510 skb_shinfo(skb
), offsetof(struct skb_shared_info
,
5511 frags
[skb_shinfo(skb
)->nr_frags
]));
5512 if (skb_orphan_frags(skb
, gfp_mask
)) {
5516 shinfo
= (struct skb_shared_info
*)(data
+ size
);
5517 for (i
= 0; i
< nfrags
; i
++) {
5518 int fsize
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
5520 if (pos
+ fsize
> off
) {
5521 shinfo
->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
5525 * We have two variants in this case:
5526 * 1. Move all the frag to the second
5527 * part, if it is possible. F.e.
5528 * this approach is mandatory for TUX,
5529 * where splitting is expensive.
5530 * 2. Split is accurately. We make this.
5532 shinfo
->frags
[0].page_offset
+= off
- pos
;
5533 skb_frag_size_sub(&shinfo
->frags
[0], off
- pos
);
5535 skb_frag_ref(skb
, i
);
5540 shinfo
->nr_frags
= k
;
5541 if (skb_has_frag_list(skb
))
5542 skb_clone_fraglist(skb
);
5545 /* split line is in frag list */
5546 pskb_carve_frag_list(skb
, shinfo
, off
- pos
, gfp_mask
);
5548 skb_release_data(skb
);
5553 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5556 skb
->end
= skb
->head
+ size
;
5558 skb_reset_tail_pointer(skb
);
5559 skb_headers_offset_update(skb
, 0);
5564 skb
->data_len
= skb
->len
;
5565 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5569 /* remove len bytes from the beginning of the skb */
5570 static int pskb_carve(struct sk_buff
*skb
, const u32 len
, gfp_t gfp
)
5572 int headlen
= skb_headlen(skb
);
5575 return pskb_carve_inside_header(skb
, len
, headlen
, gfp
);
5577 return pskb_carve_inside_nonlinear(skb
, len
, headlen
, gfp
);
5580 /* Extract to_copy bytes starting at off from skb, and return this in
5583 struct sk_buff
*pskb_extract(struct sk_buff
*skb
, int off
,
5584 int to_copy
, gfp_t gfp
)
5586 struct sk_buff
*clone
= skb_clone(skb
, gfp
);
5591 if (pskb_carve(clone
, off
, gfp
) < 0 ||
5592 pskb_trim(clone
, to_copy
)) {
5598 EXPORT_SYMBOL(pskb_extract
);
5601 * skb_condense - try to get rid of fragments/frag_list if possible
5604 * Can be used to save memory before skb is added to a busy queue.
5605 * If packet has bytes in frags and enough tail room in skb->head,
5606 * pull all of them, so that we can free the frags right now and adjust
5609 * We do not reallocate skb->head thus can not fail.
5610 * Caller must re-evaluate skb->truesize if needed.
5612 void skb_condense(struct sk_buff
*skb
)
5614 if (skb
->data_len
) {
5615 if (skb
->data_len
> skb
->end
- skb
->tail
||
5619 /* Nice, we can free page frag(s) right now */
5620 __pskb_pull_tail(skb
, skb
->data_len
);
5622 /* At this point, skb->truesize might be over estimated,
5623 * because skb had a fragment, and fragments do not tell
5625 * When we pulled its content into skb->head, fragment
5626 * was freed, but __pskb_pull_tail() could not possibly
5627 * adjust skb->truesize, not knowing the frag truesize.
5629 skb
->truesize
= SKB_TRUESIZE(skb_end_offset(skb
));
5632 #ifdef CONFIG_SKB_EXTENSIONS
5633 static void *skb_ext_get_ptr(struct skb_ext
*ext
, enum skb_ext_id id
)
5635 return (void *)ext
+ (ext
->offset
[id
] * SKB_EXT_ALIGN_VALUE
);
5638 static struct skb_ext
*skb_ext_alloc(void)
5640 struct skb_ext
*new = kmem_cache_alloc(skbuff_ext_cache
, GFP_ATOMIC
);
5643 memset(new->offset
, 0, sizeof(new->offset
));
5644 refcount_set(&new->refcnt
, 1);
5650 static struct skb_ext
*skb_ext_maybe_cow(struct skb_ext
*old
,
5651 unsigned int old_active
)
5653 struct skb_ext
*new;
5655 if (refcount_read(&old
->refcnt
) == 1)
5658 new = kmem_cache_alloc(skbuff_ext_cache
, GFP_ATOMIC
);
5662 memcpy(new, old
, old
->chunks
* SKB_EXT_ALIGN_VALUE
);
5663 refcount_set(&new->refcnt
, 1);
5666 if (old_active
& (1 << SKB_EXT_SEC_PATH
)) {
5667 struct sec_path
*sp
= skb_ext_get_ptr(old
, SKB_EXT_SEC_PATH
);
5670 for (i
= 0; i
< sp
->len
; i
++)
5671 xfrm_state_hold(sp
->xvec
[i
]);
5679 * skb_ext_add - allocate space for given extension, COW if needed
5681 * @id: extension to allocate space for
5683 * Allocates enough space for the given extension.
5684 * If the extension is already present, a pointer to that extension
5687 * If the skb was cloned, COW applies and the returned memory can be
5688 * modified without changing the extension space of clones buffers.
5690 * Returns pointer to the extension or NULL on allocation failure.
5692 void *skb_ext_add(struct sk_buff
*skb
, enum skb_ext_id id
)
5694 struct skb_ext
*new, *old
= NULL
;
5695 unsigned int newlen
, newoff
;
5697 if (skb
->active_extensions
) {
5698 old
= skb
->extensions
;
5700 new = skb_ext_maybe_cow(old
, skb
->active_extensions
);
5704 if (__skb_ext_exist(new, id
))
5707 newoff
= new->chunks
;
5709 newoff
= SKB_EXT_CHUNKSIZEOF(*new);
5711 new = skb_ext_alloc();
5716 newlen
= newoff
+ skb_ext_type_len
[id
];
5717 new->chunks
= newlen
;
5718 new->offset
[id
] = newoff
;
5720 skb
->extensions
= new;
5721 skb
->active_extensions
|= 1 << id
;
5722 return skb_ext_get_ptr(new, id
);
5724 EXPORT_SYMBOL(skb_ext_add
);
5727 static void skb_ext_put_sp(struct sec_path
*sp
)
5731 for (i
= 0; i
< sp
->len
; i
++)
5732 xfrm_state_put(sp
->xvec
[i
]);
5736 void __skb_ext_del(struct sk_buff
*skb
, enum skb_ext_id id
)
5738 struct skb_ext
*ext
= skb
->extensions
;
5740 skb
->active_extensions
&= ~(1 << id
);
5741 if (skb
->active_extensions
== 0) {
5742 skb
->extensions
= NULL
;
5745 } else if (id
== SKB_EXT_SEC_PATH
&&
5746 refcount_read(&ext
->refcnt
) == 1) {
5747 struct sec_path
*sp
= skb_ext_get_ptr(ext
, SKB_EXT_SEC_PATH
);
5754 EXPORT_SYMBOL(__skb_ext_del
);
5756 void __skb_ext_put(struct skb_ext
*ext
)
5758 /* If this is last clone, nothing can increment
5759 * it after check passes. Avoids one atomic op.
5761 if (refcount_read(&ext
->refcnt
) == 1)
5764 if (!refcount_dec_and_test(&ext
->refcnt
))
5768 if (__skb_ext_exist(ext
, SKB_EXT_SEC_PATH
))
5769 skb_ext_put_sp(skb_ext_get_ptr(ext
, SKB_EXT_SEC_PATH
));
5772 kmem_cache_free(skbuff_ext_cache
, ext
);
5774 EXPORT_SYMBOL(__skb_ext_put
);
5775 #endif /* CONFIG_SKB_EXTENSIONS */