2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/splice.h>
57 #include <linux/cache.h>
58 #include <linux/rtnetlink.h>
59 #include <linux/init.h>
60 #include <linux/scatterlist.h>
61 #include <linux/errqueue.h>
62 #include <linux/prefetch.h>
64 #include <net/protocol.h>
67 #include <net/checksum.h>
70 #include <asm/uaccess.h>
71 #include <trace/events/skb.h>
72 #include <linux/highmem.h>
74 struct kmem_cache
*skbuff_head_cache __read_mostly
;
75 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
77 static void sock_pipe_buf_release(struct pipe_inode_info
*pipe
,
78 struct pipe_buffer
*buf
)
83 static void sock_pipe_buf_get(struct pipe_inode_info
*pipe
,
84 struct pipe_buffer
*buf
)
89 static int sock_pipe_buf_steal(struct pipe_inode_info
*pipe
,
90 struct pipe_buffer
*buf
)
96 /* Pipe buffer operations for a socket. */
97 static const struct pipe_buf_operations sock_pipe_buf_ops
= {
99 .map
= generic_pipe_buf_map
,
100 .unmap
= generic_pipe_buf_unmap
,
101 .confirm
= generic_pipe_buf_confirm
,
102 .release
= sock_pipe_buf_release
,
103 .steal
= sock_pipe_buf_steal
,
104 .get
= sock_pipe_buf_get
,
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
114 * skb_over_panic - private function
119 * Out of line support code for skb_put(). Not user callable.
121 static void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
123 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
124 __func__
, here
, skb
->len
, sz
, skb
->head
, skb
->data
,
125 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
126 skb
->dev
? skb
->dev
->name
: "<NULL>");
131 * skb_under_panic - private function
136 * Out of line support code for skb_push(). Not user callable.
139 static void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
141 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
142 __func__
, here
, skb
->len
, sz
, skb
->head
, skb
->data
,
143 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
144 skb
->dev
? skb
->dev
->name
: "<NULL>");
150 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
151 * the caller if emergency pfmemalloc reserves are being used. If it is and
152 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
153 * may be used. Otherwise, the packet data may be discarded until enough
156 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
157 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
158 void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
, unsigned long ip
,
162 bool ret_pfmemalloc
= false;
165 * Try a regular allocation, when that fails and we're not entitled
166 * to the reserves, fail.
168 obj
= kmalloc_node_track_caller(size
,
169 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
171 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
174 /* Try again but now we are using pfmemalloc reserves */
175 ret_pfmemalloc
= true;
176 obj
= kmalloc_node_track_caller(size
, flags
, node
);
180 *pfmemalloc
= ret_pfmemalloc
;
185 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
186 * 'private' fields and also do memory statistics to find all the
192 * __alloc_skb - allocate a network buffer
193 * @size: size to allocate
194 * @gfp_mask: allocation mask
195 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
196 * instead of head cache and allocate a cloned (child) skb.
197 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
198 * allocations in case the data is required for writeback
199 * @node: numa node to allocate memory on
201 * Allocate a new &sk_buff. The returned buffer has no headroom and a
202 * tail room of at least size bytes. The object has a reference count
203 * of one. The return is the buffer. On a failure the return is %NULL.
205 * Buffers may only be allocated from interrupts using a @gfp_mask of
208 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
211 struct kmem_cache
*cache
;
212 struct skb_shared_info
*shinfo
;
217 cache
= (flags
& SKB_ALLOC_FCLONE
)
218 ? skbuff_fclone_cache
: skbuff_head_cache
;
220 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
221 gfp_mask
|= __GFP_MEMALLOC
;
224 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
229 /* We do our best to align skb_shared_info on a separate cache
230 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
231 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
232 * Both skb->head and skb_shared_info are cache line aligned.
234 size
= SKB_DATA_ALIGN(size
);
235 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
236 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
239 /* kmalloc(size) might give us more room than requested.
240 * Put skb_shared_info exactly at the end of allocated zone,
241 * to allow max possible filling before reallocation.
243 size
= SKB_WITH_OVERHEAD(ksize(data
));
244 prefetchw(data
+ size
);
247 * Only clear those fields we need to clear, not those that we will
248 * actually initialise below. Hence, don't put any more fields after
249 * the tail pointer in struct sk_buff!
251 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
252 /* Account for allocated memory : skb + skb->head */
253 skb
->truesize
= SKB_TRUESIZE(size
);
254 skb
->pfmemalloc
= pfmemalloc
;
255 atomic_set(&skb
->users
, 1);
258 skb_reset_tail_pointer(skb
);
259 skb
->end
= skb
->tail
+ size
;
260 #ifdef NET_SKBUFF_DATA_USES_OFFSET
261 skb
->mac_header
= ~0U;
264 /* make sure we initialize shinfo sequentially */
265 shinfo
= skb_shinfo(skb
);
266 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
267 atomic_set(&shinfo
->dataref
, 1);
268 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
270 if (flags
& SKB_ALLOC_FCLONE
) {
271 struct sk_buff
*child
= skb
+ 1;
272 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
274 kmemcheck_annotate_bitfield(child
, flags1
);
275 kmemcheck_annotate_bitfield(child
, flags2
);
276 skb
->fclone
= SKB_FCLONE_ORIG
;
277 atomic_set(fclone_ref
, 1);
279 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
280 child
->pfmemalloc
= pfmemalloc
;
285 kmem_cache_free(cache
, skb
);
289 EXPORT_SYMBOL(__alloc_skb
);
292 * build_skb - build a network buffer
293 * @data: data buffer provided by caller
294 * @frag_size: size of fragment, or 0 if head was kmalloced
296 * Allocate a new &sk_buff. Caller provides space holding head and
297 * skb_shared_info. @data must have been allocated by kmalloc()
298 * The return is the new skb buffer.
299 * On a failure the return is %NULL, and @data is not freed.
301 * Before IO, driver allocates only data buffer where NIC put incoming frame
302 * Driver should add room at head (NET_SKB_PAD) and
303 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
304 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
305 * before giving packet to stack.
306 * RX rings only contains data buffers, not full skbs.
308 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
310 struct skb_shared_info
*shinfo
;
312 unsigned int size
= frag_size
? : ksize(data
);
314 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
318 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
320 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
321 skb
->truesize
= SKB_TRUESIZE(size
);
322 skb
->head_frag
= frag_size
!= 0;
323 atomic_set(&skb
->users
, 1);
326 skb_reset_tail_pointer(skb
);
327 skb
->end
= skb
->tail
+ size
;
328 #ifdef NET_SKBUFF_DATA_USES_OFFSET
329 skb
->mac_header
= ~0U;
332 /* make sure we initialize shinfo sequentially */
333 shinfo
= skb_shinfo(skb
);
334 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
335 atomic_set(&shinfo
->dataref
, 1);
336 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
340 EXPORT_SYMBOL(build_skb
);
342 struct netdev_alloc_cache
{
343 struct page_frag frag
;
344 /* we maintain a pagecount bias, so that we dont dirty cache line
345 * containing page->_count every time we allocate a fragment.
347 unsigned int pagecnt_bias
;
349 static DEFINE_PER_CPU(struct netdev_alloc_cache
, netdev_alloc_cache
);
351 #define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
352 #define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
353 #define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
355 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
357 struct netdev_alloc_cache
*nc
;
362 local_irq_save(flags
);
363 nc
= &__get_cpu_var(netdev_alloc_cache
);
364 if (unlikely(!nc
->frag
.page
)) {
366 for (order
= NETDEV_FRAG_PAGE_MAX_ORDER
; ;) {
367 gfp_t gfp
= gfp_mask
;
370 gfp
|= __GFP_COMP
| __GFP_NOWARN
;
371 nc
->frag
.page
= alloc_pages(gfp
, order
);
372 if (likely(nc
->frag
.page
))
377 nc
->frag
.size
= PAGE_SIZE
<< order
;
379 atomic_set(&nc
->frag
.page
->_count
, NETDEV_PAGECNT_MAX_BIAS
);
380 nc
->pagecnt_bias
= NETDEV_PAGECNT_MAX_BIAS
;
384 if (nc
->frag
.offset
+ fragsz
> nc
->frag
.size
) {
385 /* avoid unnecessary locked operations if possible */
386 if ((atomic_read(&nc
->frag
.page
->_count
) == nc
->pagecnt_bias
) ||
387 atomic_sub_and_test(nc
->pagecnt_bias
, &nc
->frag
.page
->_count
))
392 data
= page_address(nc
->frag
.page
) + nc
->frag
.offset
;
393 nc
->frag
.offset
+= fragsz
;
396 local_irq_restore(flags
);
401 * netdev_alloc_frag - allocate a page fragment
402 * @fragsz: fragment size
404 * Allocates a frag from a page for receive buffer.
405 * Uses GFP_ATOMIC allocations.
407 void *netdev_alloc_frag(unsigned int fragsz
)
409 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
411 EXPORT_SYMBOL(netdev_alloc_frag
);
414 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
415 * @dev: network device to receive on
416 * @length: length to allocate
417 * @gfp_mask: get_free_pages mask, passed to alloc_skb
419 * Allocate a new &sk_buff and assign it a usage count of one. The
420 * buffer has unspecified headroom built in. Users should allocate
421 * the headroom they think they need without accounting for the
422 * built in space. The built in space is used for optimisations.
424 * %NULL is returned if there is no free memory.
426 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
427 unsigned int length
, gfp_t gfp_mask
)
429 struct sk_buff
*skb
= NULL
;
430 unsigned int fragsz
= SKB_DATA_ALIGN(length
+ NET_SKB_PAD
) +
431 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
433 if (fragsz
<= PAGE_SIZE
&& !(gfp_mask
& (__GFP_WAIT
| GFP_DMA
))) {
436 if (sk_memalloc_socks())
437 gfp_mask
|= __GFP_MEMALLOC
;
439 data
= __netdev_alloc_frag(fragsz
, gfp_mask
);
442 skb
= build_skb(data
, fragsz
);
444 put_page(virt_to_head_page(data
));
447 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
,
448 SKB_ALLOC_RX
, NUMA_NO_NODE
);
451 skb_reserve(skb
, NET_SKB_PAD
);
456 EXPORT_SYMBOL(__netdev_alloc_skb
);
458 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
459 int size
, unsigned int truesize
)
461 skb_fill_page_desc(skb
, i
, page
, off
, size
);
463 skb
->data_len
+= size
;
464 skb
->truesize
+= truesize
;
466 EXPORT_SYMBOL(skb_add_rx_frag
);
468 static void skb_drop_list(struct sk_buff
**listp
)
470 struct sk_buff
*list
= *listp
;
475 struct sk_buff
*this = list
;
481 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
483 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
486 static void skb_clone_fraglist(struct sk_buff
*skb
)
488 struct sk_buff
*list
;
490 skb_walk_frags(skb
, list
)
494 static void skb_free_head(struct sk_buff
*skb
)
497 put_page(virt_to_head_page(skb
->head
));
502 static void skb_release_data(struct sk_buff
*skb
)
505 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
506 &skb_shinfo(skb
)->dataref
)) {
507 if (skb_shinfo(skb
)->nr_frags
) {
509 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
510 skb_frag_unref(skb
, i
);
514 * If skb buf is from userspace, we need to notify the caller
515 * the lower device DMA has done;
517 if (skb_shinfo(skb
)->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
518 struct ubuf_info
*uarg
;
520 uarg
= skb_shinfo(skb
)->destructor_arg
;
522 uarg
->callback(uarg
);
525 if (skb_has_frag_list(skb
))
526 skb_drop_fraglist(skb
);
533 * Free an skbuff by memory without cleaning the state.
535 static void kfree_skbmem(struct sk_buff
*skb
)
537 struct sk_buff
*other
;
538 atomic_t
*fclone_ref
;
540 switch (skb
->fclone
) {
541 case SKB_FCLONE_UNAVAILABLE
:
542 kmem_cache_free(skbuff_head_cache
, skb
);
545 case SKB_FCLONE_ORIG
:
546 fclone_ref
= (atomic_t
*) (skb
+ 2);
547 if (atomic_dec_and_test(fclone_ref
))
548 kmem_cache_free(skbuff_fclone_cache
, skb
);
551 case SKB_FCLONE_CLONE
:
552 fclone_ref
= (atomic_t
*) (skb
+ 1);
555 /* The clone portion is available for
556 * fast-cloning again.
558 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
560 if (atomic_dec_and_test(fclone_ref
))
561 kmem_cache_free(skbuff_fclone_cache
, other
);
566 static void skb_release_head_state(struct sk_buff
*skb
)
570 secpath_put(skb
->sp
);
572 if (skb
->destructor
) {
574 skb
->destructor(skb
);
576 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
577 nf_conntrack_put(skb
->nfct
);
579 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
580 nf_conntrack_put_reasm(skb
->nfct_reasm
);
582 #ifdef CONFIG_BRIDGE_NETFILTER
583 nf_bridge_put(skb
->nf_bridge
);
585 /* XXX: IS this still necessary? - JHS */
586 #ifdef CONFIG_NET_SCHED
588 #ifdef CONFIG_NET_CLS_ACT
594 /* Free everything but the sk_buff shell. */
595 static void skb_release_all(struct sk_buff
*skb
)
597 skb_release_head_state(skb
);
598 skb_release_data(skb
);
602 * __kfree_skb - private function
605 * Free an sk_buff. Release anything attached to the buffer.
606 * Clean the state. This is an internal helper function. Users should
607 * always call kfree_skb
610 void __kfree_skb(struct sk_buff
*skb
)
612 skb_release_all(skb
);
615 EXPORT_SYMBOL(__kfree_skb
);
618 * kfree_skb - free an sk_buff
619 * @skb: buffer to free
621 * Drop a reference to the buffer and free it if the usage count has
624 void kfree_skb(struct sk_buff
*skb
)
628 if (likely(atomic_read(&skb
->users
) == 1))
630 else if (likely(!atomic_dec_and_test(&skb
->users
)))
632 trace_kfree_skb(skb
, __builtin_return_address(0));
635 EXPORT_SYMBOL(kfree_skb
);
638 * consume_skb - free an skbuff
639 * @skb: buffer to free
641 * Drop a ref to the buffer and free it if the usage count has hit zero
642 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
643 * is being dropped after a failure and notes that
645 void consume_skb(struct sk_buff
*skb
)
649 if (likely(atomic_read(&skb
->users
) == 1))
651 else if (likely(!atomic_dec_and_test(&skb
->users
)))
653 trace_consume_skb(skb
);
656 EXPORT_SYMBOL(consume_skb
);
659 * skb_recycle - clean up an skb for reuse
662 * Recycles the skb to be reused as a receive buffer. This
663 * function does any necessary reference count dropping, and
664 * cleans up the skbuff as if it just came from __alloc_skb().
666 void skb_recycle(struct sk_buff
*skb
)
668 struct skb_shared_info
*shinfo
;
670 skb_release_head_state(skb
);
672 shinfo
= skb_shinfo(skb
);
673 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
674 atomic_set(&shinfo
->dataref
, 1);
676 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
677 skb
->data
= skb
->head
+ NET_SKB_PAD
;
678 skb_reset_tail_pointer(skb
);
680 EXPORT_SYMBOL(skb_recycle
);
683 * skb_recycle_check - check if skb can be reused for receive
685 * @skb_size: minimum receive buffer size
687 * Checks that the skb passed in is not shared or cloned, and
688 * that it is linear and its head portion at least as large as
689 * skb_size so that it can be recycled as a receive buffer.
690 * If these conditions are met, this function does any necessary
691 * reference count dropping and cleans up the skbuff as if it
692 * just came from __alloc_skb().
694 bool skb_recycle_check(struct sk_buff
*skb
, int skb_size
)
696 if (!skb_is_recycleable(skb
, skb_size
))
703 EXPORT_SYMBOL(skb_recycle_check
);
705 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
707 new->tstamp
= old
->tstamp
;
709 new->transport_header
= old
->transport_header
;
710 new->network_header
= old
->network_header
;
711 new->mac_header
= old
->mac_header
;
712 skb_dst_copy(new, old
);
713 new->rxhash
= old
->rxhash
;
714 new->ooo_okay
= old
->ooo_okay
;
715 new->l4_rxhash
= old
->l4_rxhash
;
716 new->no_fcs
= old
->no_fcs
;
718 new->sp
= secpath_get(old
->sp
);
720 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
721 new->csum
= old
->csum
;
722 new->local_df
= old
->local_df
;
723 new->pkt_type
= old
->pkt_type
;
724 new->ip_summed
= old
->ip_summed
;
725 skb_copy_queue_mapping(new, old
);
726 new->priority
= old
->priority
;
727 #if IS_ENABLED(CONFIG_IP_VS)
728 new->ipvs_property
= old
->ipvs_property
;
730 new->pfmemalloc
= old
->pfmemalloc
;
731 new->protocol
= old
->protocol
;
732 new->mark
= old
->mark
;
733 new->skb_iif
= old
->skb_iif
;
735 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
736 new->nf_trace
= old
->nf_trace
;
738 #ifdef CONFIG_NET_SCHED
739 new->tc_index
= old
->tc_index
;
740 #ifdef CONFIG_NET_CLS_ACT
741 new->tc_verd
= old
->tc_verd
;
744 new->vlan_tci
= old
->vlan_tci
;
746 skb_copy_secmark(new, old
);
750 * You should not add any new code to this function. Add it to
751 * __copy_skb_header above instead.
753 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
755 #define C(x) n->x = skb->x
757 n
->next
= n
->prev
= NULL
;
759 __copy_skb_header(n
, skb
);
764 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
767 n
->destructor
= NULL
;
774 atomic_set(&n
->users
, 1);
776 atomic_inc(&(skb_shinfo(skb
)->dataref
));
784 * skb_morph - morph one skb into another
785 * @dst: the skb to receive the contents
786 * @src: the skb to supply the contents
788 * This is identical to skb_clone except that the target skb is
789 * supplied by the user.
791 * The target skb is returned upon exit.
793 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
795 skb_release_all(dst
);
796 return __skb_clone(dst
, src
);
798 EXPORT_SYMBOL_GPL(skb_morph
);
801 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
802 * @skb: the skb to modify
803 * @gfp_mask: allocation priority
805 * This must be called on SKBTX_DEV_ZEROCOPY skb.
806 * It will copy all frags into kernel and drop the reference
807 * to userspace pages.
809 * If this function is called from an interrupt gfp_mask() must be
812 * Returns 0 on success or a negative error code on failure
813 * to allocate kernel memory to copy to.
815 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
818 int num_frags
= skb_shinfo(skb
)->nr_frags
;
819 struct page
*page
, *head
= NULL
;
820 struct ubuf_info
*uarg
= skb_shinfo(skb
)->destructor_arg
;
822 for (i
= 0; i
< num_frags
; i
++) {
824 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
826 page
= alloc_page(gfp_mask
);
829 struct page
*next
= (struct page
*)head
->private;
835 vaddr
= kmap_atomic(skb_frag_page(f
));
836 memcpy(page_address(page
),
837 vaddr
+ f
->page_offset
, skb_frag_size(f
));
838 kunmap_atomic(vaddr
);
839 page
->private = (unsigned long)head
;
843 /* skb frags release userspace buffers */
844 for (i
= 0; i
< num_frags
; i
++)
845 skb_frag_unref(skb
, i
);
847 uarg
->callback(uarg
);
849 /* skb frags point to kernel buffers */
850 for (i
= num_frags
- 1; i
>= 0; i
--) {
851 __skb_fill_page_desc(skb
, i
, head
, 0,
852 skb_shinfo(skb
)->frags
[i
].size
);
853 head
= (struct page
*)head
->private;
856 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
859 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
862 * skb_clone - duplicate an sk_buff
863 * @skb: buffer to clone
864 * @gfp_mask: allocation priority
866 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
867 * copies share the same packet data but not structure. The new
868 * buffer has a reference count of 1. If the allocation fails the
869 * function returns %NULL otherwise the new buffer is returned.
871 * If this function is called from an interrupt gfp_mask() must be
875 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
879 if (skb_orphan_frags(skb
, gfp_mask
))
883 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
884 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
885 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
886 n
->fclone
= SKB_FCLONE_CLONE
;
887 atomic_inc(fclone_ref
);
889 if (skb_pfmemalloc(skb
))
890 gfp_mask
|= __GFP_MEMALLOC
;
892 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
896 kmemcheck_annotate_bitfield(n
, flags1
);
897 kmemcheck_annotate_bitfield(n
, flags2
);
898 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
901 return __skb_clone(n
, skb
);
903 EXPORT_SYMBOL(skb_clone
);
905 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
907 #ifndef NET_SKBUFF_DATA_USES_OFFSET
909 * Shift between the two data areas in bytes
911 unsigned long offset
= new->data
- old
->data
;
914 __copy_skb_header(new, old
);
916 #ifndef NET_SKBUFF_DATA_USES_OFFSET
917 /* {transport,network,mac}_header are relative to skb->head */
918 new->transport_header
+= offset
;
919 new->network_header
+= offset
;
920 if (skb_mac_header_was_set(new))
921 new->mac_header
+= offset
;
923 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
924 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
925 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
928 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
930 if (skb_pfmemalloc(skb
))
936 * skb_copy - create private copy of an sk_buff
937 * @skb: buffer to copy
938 * @gfp_mask: allocation priority
940 * Make a copy of both an &sk_buff and its data. This is used when the
941 * caller wishes to modify the data and needs a private copy of the
942 * data to alter. Returns %NULL on failure or the pointer to the buffer
943 * on success. The returned buffer has a reference count of 1.
945 * As by-product this function converts non-linear &sk_buff to linear
946 * one, so that &sk_buff becomes completely private and caller is allowed
947 * to modify all the data of returned buffer. This means that this
948 * function is not recommended for use in circumstances when only
949 * header is going to be modified. Use pskb_copy() instead.
952 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
954 int headerlen
= skb_headroom(skb
);
955 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
956 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
957 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
962 /* Set the data pointer */
963 skb_reserve(n
, headerlen
);
964 /* Set the tail pointer and length */
965 skb_put(n
, skb
->len
);
967 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
970 copy_skb_header(n
, skb
);
973 EXPORT_SYMBOL(skb_copy
);
976 * __pskb_copy - create copy of an sk_buff with private head.
977 * @skb: buffer to copy
978 * @headroom: headroom of new skb
979 * @gfp_mask: allocation priority
981 * Make a copy of both an &sk_buff and part of its data, located
982 * in header. Fragmented data remain shared. This is used when
983 * the caller wishes to modify only header of &sk_buff and needs
984 * private copy of the header to alter. Returns %NULL on failure
985 * or the pointer to the buffer on success.
986 * The returned buffer has a reference count of 1.
989 struct sk_buff
*__pskb_copy(struct sk_buff
*skb
, int headroom
, gfp_t gfp_mask
)
991 unsigned int size
= skb_headlen(skb
) + headroom
;
992 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
993 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
998 /* Set the data pointer */
999 skb_reserve(n
, headroom
);
1000 /* Set the tail pointer and length */
1001 skb_put(n
, skb_headlen(skb
));
1002 /* Copy the bytes */
1003 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1005 n
->truesize
+= skb
->data_len
;
1006 n
->data_len
= skb
->data_len
;
1009 if (skb_shinfo(skb
)->nr_frags
) {
1012 if (skb_orphan_frags(skb
, gfp_mask
)) {
1017 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1018 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1019 skb_frag_ref(skb
, i
);
1021 skb_shinfo(n
)->nr_frags
= i
;
1024 if (skb_has_frag_list(skb
)) {
1025 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1026 skb_clone_fraglist(n
);
1029 copy_skb_header(n
, skb
);
1033 EXPORT_SYMBOL(__pskb_copy
);
1036 * pskb_expand_head - reallocate header of &sk_buff
1037 * @skb: buffer to reallocate
1038 * @nhead: room to add at head
1039 * @ntail: room to add at tail
1040 * @gfp_mask: allocation priority
1042 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1043 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1044 * reference count of 1. Returns zero in the case of success or error,
1045 * if expansion failed. In the last case, &sk_buff is not changed.
1047 * All the pointers pointing into skb header may change and must be
1048 * reloaded after call to this function.
1051 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1056 int size
= nhead
+ skb_end_offset(skb
) + ntail
;
1061 if (skb_shared(skb
))
1064 size
= SKB_DATA_ALIGN(size
);
1066 if (skb_pfmemalloc(skb
))
1067 gfp_mask
|= __GFP_MEMALLOC
;
1068 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1069 gfp_mask
, NUMA_NO_NODE
, NULL
);
1072 size
= SKB_WITH_OVERHEAD(ksize(data
));
1074 /* Copy only real data... and, alas, header. This should be
1075 * optimized for the cases when header is void.
1077 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1079 memcpy((struct skb_shared_info
*)(data
+ size
),
1081 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1084 * if shinfo is shared we must drop the old head gracefully, but if it
1085 * is not we can just drop the old head and let the existing refcount
1086 * be since all we did is relocate the values
1088 if (skb_cloned(skb
)) {
1089 /* copy this zero copy skb frags */
1090 if (skb_orphan_frags(skb
, gfp_mask
))
1092 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1093 skb_frag_ref(skb
, i
);
1095 if (skb_has_frag_list(skb
))
1096 skb_clone_fraglist(skb
);
1098 skb_release_data(skb
);
1102 off
= (data
+ nhead
) - skb
->head
;
1107 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1111 skb
->end
= skb
->head
+ size
;
1113 /* {transport,network,mac}_header and tail are relative to skb->head */
1115 skb
->transport_header
+= off
;
1116 skb
->network_header
+= off
;
1117 if (skb_mac_header_was_set(skb
))
1118 skb
->mac_header
+= off
;
1119 /* Only adjust this if it actually is csum_start rather than csum */
1120 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1121 skb
->csum_start
+= nhead
;
1125 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1133 EXPORT_SYMBOL(pskb_expand_head
);
1135 /* Make private copy of skb with writable head and some headroom */
1137 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1139 struct sk_buff
*skb2
;
1140 int delta
= headroom
- skb_headroom(skb
);
1143 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1145 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1146 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1154 EXPORT_SYMBOL(skb_realloc_headroom
);
1157 * skb_copy_expand - copy and expand sk_buff
1158 * @skb: buffer to copy
1159 * @newheadroom: new free bytes at head
1160 * @newtailroom: new free bytes at tail
1161 * @gfp_mask: allocation priority
1163 * Make a copy of both an &sk_buff and its data and while doing so
1164 * allocate additional space.
1166 * This is used when the caller wishes to modify the data and needs a
1167 * private copy of the data to alter as well as more space for new fields.
1168 * Returns %NULL on failure or the pointer to the buffer
1169 * on success. The returned buffer has a reference count of 1.
1171 * You must pass %GFP_ATOMIC as the allocation priority if this function
1172 * is called from an interrupt.
1174 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1175 int newheadroom
, int newtailroom
,
1179 * Allocate the copy buffer
1181 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1182 gfp_mask
, skb_alloc_rx_flag(skb
),
1184 int oldheadroom
= skb_headroom(skb
);
1185 int head_copy_len
, head_copy_off
;
1191 skb_reserve(n
, newheadroom
);
1193 /* Set the tail pointer and length */
1194 skb_put(n
, skb
->len
);
1196 head_copy_len
= oldheadroom
;
1198 if (newheadroom
<= head_copy_len
)
1199 head_copy_len
= newheadroom
;
1201 head_copy_off
= newheadroom
- head_copy_len
;
1203 /* Copy the linear header and data. */
1204 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1205 skb
->len
+ head_copy_len
))
1208 copy_skb_header(n
, skb
);
1210 off
= newheadroom
- oldheadroom
;
1211 if (n
->ip_summed
== CHECKSUM_PARTIAL
)
1212 n
->csum_start
+= off
;
1213 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1214 n
->transport_header
+= off
;
1215 n
->network_header
+= off
;
1216 if (skb_mac_header_was_set(skb
))
1217 n
->mac_header
+= off
;
1222 EXPORT_SYMBOL(skb_copy_expand
);
1225 * skb_pad - zero pad the tail of an skb
1226 * @skb: buffer to pad
1227 * @pad: space to pad
1229 * Ensure that a buffer is followed by a padding area that is zero
1230 * filled. Used by network drivers which may DMA or transfer data
1231 * beyond the buffer end onto the wire.
1233 * May return error in out of memory cases. The skb is freed on error.
1236 int skb_pad(struct sk_buff
*skb
, int pad
)
1241 /* If the skbuff is non linear tailroom is always zero.. */
1242 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1243 memset(skb
->data
+skb
->len
, 0, pad
);
1247 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1248 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1249 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1254 /* FIXME: The use of this function with non-linear skb's really needs
1257 err
= skb_linearize(skb
);
1261 memset(skb
->data
+ skb
->len
, 0, pad
);
1268 EXPORT_SYMBOL(skb_pad
);
1271 * skb_put - add data to a buffer
1272 * @skb: buffer to use
1273 * @len: amount of data to add
1275 * This function extends the used data area of the buffer. If this would
1276 * exceed the total buffer size the kernel will panic. A pointer to the
1277 * first byte of the extra data is returned.
1279 unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
1281 unsigned char *tmp
= skb_tail_pointer(skb
);
1282 SKB_LINEAR_ASSERT(skb
);
1285 if (unlikely(skb
->tail
> skb
->end
))
1286 skb_over_panic(skb
, len
, __builtin_return_address(0));
1289 EXPORT_SYMBOL(skb_put
);
1292 * skb_push - add data to the start of a buffer
1293 * @skb: buffer to use
1294 * @len: amount of data to add
1296 * This function extends the used data area of the buffer at the buffer
1297 * start. If this would exceed the total buffer headroom the kernel will
1298 * panic. A pointer to the first byte of the extra data is returned.
1300 unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
1304 if (unlikely(skb
->data
<skb
->head
))
1305 skb_under_panic(skb
, len
, __builtin_return_address(0));
1308 EXPORT_SYMBOL(skb_push
);
1311 * skb_pull - remove data from the start of a buffer
1312 * @skb: buffer to use
1313 * @len: amount of data to remove
1315 * This function removes data from the start of a buffer, returning
1316 * the memory to the headroom. A pointer to the next data in the buffer
1317 * is returned. Once the data has been pulled future pushes will overwrite
1320 unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1322 return skb_pull_inline(skb
, len
);
1324 EXPORT_SYMBOL(skb_pull
);
1327 * skb_trim - remove end from a buffer
1328 * @skb: buffer to alter
1331 * Cut the length of a buffer down by removing data from the tail. If
1332 * the buffer is already under the length specified it is not modified.
1333 * The skb must be linear.
1335 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1338 __skb_trim(skb
, len
);
1340 EXPORT_SYMBOL(skb_trim
);
1342 /* Trims skb to length len. It can change skb pointers.
1345 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1347 struct sk_buff
**fragp
;
1348 struct sk_buff
*frag
;
1349 int offset
= skb_headlen(skb
);
1350 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1354 if (skb_cloned(skb
) &&
1355 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1362 for (; i
< nfrags
; i
++) {
1363 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1370 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1373 skb_shinfo(skb
)->nr_frags
= i
;
1375 for (; i
< nfrags
; i
++)
1376 skb_frag_unref(skb
, i
);
1378 if (skb_has_frag_list(skb
))
1379 skb_drop_fraglist(skb
);
1383 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1384 fragp
= &frag
->next
) {
1385 int end
= offset
+ frag
->len
;
1387 if (skb_shared(frag
)) {
1388 struct sk_buff
*nfrag
;
1390 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1391 if (unlikely(!nfrag
))
1394 nfrag
->next
= frag
->next
;
1406 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1410 skb_drop_list(&frag
->next
);
1415 if (len
> skb_headlen(skb
)) {
1416 skb
->data_len
-= skb
->len
- len
;
1421 skb_set_tail_pointer(skb
, len
);
1426 EXPORT_SYMBOL(___pskb_trim
);
1429 * __pskb_pull_tail - advance tail of skb header
1430 * @skb: buffer to reallocate
1431 * @delta: number of bytes to advance tail
1433 * The function makes a sense only on a fragmented &sk_buff,
1434 * it expands header moving its tail forward and copying necessary
1435 * data from fragmented part.
1437 * &sk_buff MUST have reference count of 1.
1439 * Returns %NULL (and &sk_buff does not change) if pull failed
1440 * or value of new tail of skb in the case of success.
1442 * All the pointers pointing into skb header may change and must be
1443 * reloaded after call to this function.
1446 /* Moves tail of skb head forward, copying data from fragmented part,
1447 * when it is necessary.
1448 * 1. It may fail due to malloc failure.
1449 * 2. It may change skb pointers.
1451 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1453 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1455 /* If skb has not enough free space at tail, get new one
1456 * plus 128 bytes for future expansions. If we have enough
1457 * room at tail, reallocate without expansion only if skb is cloned.
1459 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1461 if (eat
> 0 || skb_cloned(skb
)) {
1462 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1467 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1470 /* Optimization: no fragments, no reasons to preestimate
1471 * size of pulled pages. Superb.
1473 if (!skb_has_frag_list(skb
))
1476 /* Estimate size of pulled pages. */
1478 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1479 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1486 /* If we need update frag list, we are in troubles.
1487 * Certainly, it possible to add an offset to skb data,
1488 * but taking into account that pulling is expected to
1489 * be very rare operation, it is worth to fight against
1490 * further bloating skb head and crucify ourselves here instead.
1491 * Pure masohism, indeed. 8)8)
1494 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1495 struct sk_buff
*clone
= NULL
;
1496 struct sk_buff
*insp
= NULL
;
1501 if (list
->len
<= eat
) {
1502 /* Eaten as whole. */
1507 /* Eaten partially. */
1509 if (skb_shared(list
)) {
1510 /* Sucks! We need to fork list. :-( */
1511 clone
= skb_clone(list
, GFP_ATOMIC
);
1517 /* This may be pulled without
1521 if (!pskb_pull(list
, eat
)) {
1529 /* Free pulled out fragments. */
1530 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1531 skb_shinfo(skb
)->frag_list
= list
->next
;
1534 /* And insert new clone at head. */
1537 skb_shinfo(skb
)->frag_list
= clone
;
1540 /* Success! Now we may commit changes to skb data. */
1545 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1546 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1549 skb_frag_unref(skb
, i
);
1552 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1554 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1555 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
1561 skb_shinfo(skb
)->nr_frags
= k
;
1564 skb
->data_len
-= delta
;
1566 return skb_tail_pointer(skb
);
1568 EXPORT_SYMBOL(__pskb_pull_tail
);
1571 * skb_copy_bits - copy bits from skb to kernel buffer
1573 * @offset: offset in source
1574 * @to: destination buffer
1575 * @len: number of bytes to copy
1577 * Copy the specified number of bytes from the source skb to the
1578 * destination buffer.
1581 * If its prototype is ever changed,
1582 * check arch/{*}/net/{*}.S files,
1583 * since it is called from BPF assembly code.
1585 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1587 int start
= skb_headlen(skb
);
1588 struct sk_buff
*frag_iter
;
1591 if (offset
> (int)skb
->len
- len
)
1595 if ((copy
= start
- offset
) > 0) {
1598 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1599 if ((len
-= copy
) == 0)
1605 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1607 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1609 WARN_ON(start
> offset
+ len
);
1611 end
= start
+ skb_frag_size(f
);
1612 if ((copy
= end
- offset
) > 0) {
1618 vaddr
= kmap_atomic(skb_frag_page(f
));
1620 vaddr
+ f
->page_offset
+ offset
- start
,
1622 kunmap_atomic(vaddr
);
1624 if ((len
-= copy
) == 0)
1632 skb_walk_frags(skb
, frag_iter
) {
1635 WARN_ON(start
> offset
+ len
);
1637 end
= start
+ frag_iter
->len
;
1638 if ((copy
= end
- offset
) > 0) {
1641 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
1643 if ((len
-= copy
) == 0)
1657 EXPORT_SYMBOL(skb_copy_bits
);
1660 * Callback from splice_to_pipe(), if we need to release some pages
1661 * at the end of the spd in case we error'ed out in filling the pipe.
1663 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1665 put_page(spd
->pages
[i
]);
1668 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
1669 unsigned int *offset
,
1670 struct sk_buff
*skb
, struct sock
*sk
)
1672 struct page_frag
*pfrag
= sk_page_frag(sk
);
1674 if (!sk_page_frag_refill(sk
, pfrag
))
1677 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
1679 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
1680 page_address(page
) + *offset
, *len
);
1681 *offset
= pfrag
->offset
;
1682 pfrag
->offset
+= *len
;
1687 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
1689 unsigned int offset
)
1691 return spd
->nr_pages
&&
1692 spd
->pages
[spd
->nr_pages
- 1] == page
&&
1693 (spd
->partial
[spd
->nr_pages
- 1].offset
+
1694 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
1698 * Fill page/offset/length into spd, if it can hold more pages.
1700 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
1701 struct pipe_inode_info
*pipe
, struct page
*page
,
1702 unsigned int *len
, unsigned int offset
,
1703 struct sk_buff
*skb
, bool linear
,
1706 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
1710 page
= linear_to_page(page
, len
, &offset
, skb
, sk
);
1714 if (spd_can_coalesce(spd
, page
, offset
)) {
1715 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
1719 spd
->pages
[spd
->nr_pages
] = page
;
1720 spd
->partial
[spd
->nr_pages
].len
= *len
;
1721 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1727 static inline void __segment_seek(struct page
**page
, unsigned int *poff
,
1728 unsigned int *plen
, unsigned int off
)
1733 n
= *poff
/ PAGE_SIZE
;
1735 *page
= nth_page(*page
, n
);
1737 *poff
= *poff
% PAGE_SIZE
;
1741 static bool __splice_segment(struct page
*page
, unsigned int poff
,
1742 unsigned int plen
, unsigned int *off
,
1743 unsigned int *len
, struct sk_buff
*skb
,
1744 struct splice_pipe_desc
*spd
, bool linear
,
1746 struct pipe_inode_info
*pipe
)
1751 /* skip this segment if already processed */
1757 /* ignore any bits we already processed */
1759 __segment_seek(&page
, &poff
, &plen
, *off
);
1764 unsigned int flen
= min(*len
, plen
);
1766 /* the linear region may spread across several pages */
1767 flen
= min_t(unsigned int, flen
, PAGE_SIZE
- poff
);
1769 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
, skb
, linear
, sk
))
1772 __segment_seek(&page
, &poff
, &plen
, flen
);
1775 } while (*len
&& plen
);
1781 * Map linear and fragment data from the skb to spd. It reports true if the
1782 * pipe is full or if we already spliced the requested length.
1784 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
1785 unsigned int *offset
, unsigned int *len
,
1786 struct splice_pipe_desc
*spd
, struct sock
*sk
)
1790 /* map the linear part :
1791 * If skb->head_frag is set, this 'linear' part is backed by a
1792 * fragment, and if the head is not shared with any clones then
1793 * we can avoid a copy since we own the head portion of this page.
1795 if (__splice_segment(virt_to_page(skb
->data
),
1796 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
1798 offset
, len
, skb
, spd
,
1799 skb_head_is_locked(skb
),
1804 * then map the fragments
1806 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1807 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1809 if (__splice_segment(skb_frag_page(f
),
1810 f
->page_offset
, skb_frag_size(f
),
1811 offset
, len
, skb
, spd
, false, sk
, pipe
))
1819 * Map data from the skb to a pipe. Should handle both the linear part,
1820 * the fragments, and the frag list. It does NOT handle frag lists within
1821 * the frag list, if such a thing exists. We'd probably need to recurse to
1822 * handle that cleanly.
1824 int skb_splice_bits(struct sk_buff
*skb
, unsigned int offset
,
1825 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1828 struct partial_page partial
[MAX_SKB_FRAGS
];
1829 struct page
*pages
[MAX_SKB_FRAGS
];
1830 struct splice_pipe_desc spd
= {
1833 .nr_pages_max
= MAX_SKB_FRAGS
,
1835 .ops
= &sock_pipe_buf_ops
,
1836 .spd_release
= sock_spd_release
,
1838 struct sk_buff
*frag_iter
;
1839 struct sock
*sk
= skb
->sk
;
1843 * __skb_splice_bits() only fails if the output has no room left,
1844 * so no point in going over the frag_list for the error case.
1846 if (__skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
))
1852 * now see if we have a frag_list to map
1854 skb_walk_frags(skb
, frag_iter
) {
1857 if (__skb_splice_bits(frag_iter
, pipe
, &offset
, &tlen
, &spd
, sk
))
1864 * Drop the socket lock, otherwise we have reverse
1865 * locking dependencies between sk_lock and i_mutex
1866 * here as compared to sendfile(). We enter here
1867 * with the socket lock held, and splice_to_pipe() will
1868 * grab the pipe inode lock. For sendfile() emulation,
1869 * we call into ->sendpage() with the i_mutex lock held
1870 * and networking will grab the socket lock.
1873 ret
= splice_to_pipe(pipe
, &spd
);
1881 * skb_store_bits - store bits from kernel buffer to skb
1882 * @skb: destination buffer
1883 * @offset: offset in destination
1884 * @from: source buffer
1885 * @len: number of bytes to copy
1887 * Copy the specified number of bytes from the source buffer to the
1888 * destination skb. This function handles all the messy bits of
1889 * traversing fragment lists and such.
1892 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1894 int start
= skb_headlen(skb
);
1895 struct sk_buff
*frag_iter
;
1898 if (offset
> (int)skb
->len
- len
)
1901 if ((copy
= start
- offset
) > 0) {
1904 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1905 if ((len
-= copy
) == 0)
1911 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1912 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1915 WARN_ON(start
> offset
+ len
);
1917 end
= start
+ skb_frag_size(frag
);
1918 if ((copy
= end
- offset
) > 0) {
1924 vaddr
= kmap_atomic(skb_frag_page(frag
));
1925 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1927 kunmap_atomic(vaddr
);
1929 if ((len
-= copy
) == 0)
1937 skb_walk_frags(skb
, frag_iter
) {
1940 WARN_ON(start
> offset
+ len
);
1942 end
= start
+ frag_iter
->len
;
1943 if ((copy
= end
- offset
) > 0) {
1946 if (skb_store_bits(frag_iter
, offset
- start
,
1949 if ((len
-= copy
) == 0)
1962 EXPORT_SYMBOL(skb_store_bits
);
1964 /* Checksum skb data. */
1966 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1967 int len
, __wsum csum
)
1969 int start
= skb_headlen(skb
);
1970 int i
, copy
= start
- offset
;
1971 struct sk_buff
*frag_iter
;
1974 /* Checksum header. */
1978 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1979 if ((len
-= copy
) == 0)
1985 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1987 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1989 WARN_ON(start
> offset
+ len
);
1991 end
= start
+ skb_frag_size(frag
);
1992 if ((copy
= end
- offset
) > 0) {
1998 vaddr
= kmap_atomic(skb_frag_page(frag
));
1999 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
2000 offset
- start
, copy
, 0);
2001 kunmap_atomic(vaddr
);
2002 csum
= csum_block_add(csum
, csum2
, pos
);
2011 skb_walk_frags(skb
, frag_iter
) {
2014 WARN_ON(start
> offset
+ len
);
2016 end
= start
+ frag_iter
->len
;
2017 if ((copy
= end
- offset
) > 0) {
2021 csum2
= skb_checksum(frag_iter
, offset
- start
,
2023 csum
= csum_block_add(csum
, csum2
, pos
);
2024 if ((len
-= copy
) == 0)
2035 EXPORT_SYMBOL(skb_checksum
);
2037 /* Both of above in one bottle. */
2039 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2040 u8
*to
, int len
, __wsum csum
)
2042 int start
= skb_headlen(skb
);
2043 int i
, copy
= start
- offset
;
2044 struct sk_buff
*frag_iter
;
2051 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2053 if ((len
-= copy
) == 0)
2060 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2063 WARN_ON(start
> offset
+ len
);
2065 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2066 if ((copy
= end
- offset
) > 0) {
2069 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2073 vaddr
= kmap_atomic(skb_frag_page(frag
));
2074 csum2
= csum_partial_copy_nocheck(vaddr
+
2078 kunmap_atomic(vaddr
);
2079 csum
= csum_block_add(csum
, csum2
, pos
);
2089 skb_walk_frags(skb
, frag_iter
) {
2093 WARN_ON(start
> offset
+ len
);
2095 end
= start
+ frag_iter
->len
;
2096 if ((copy
= end
- offset
) > 0) {
2099 csum2
= skb_copy_and_csum_bits(frag_iter
,
2102 csum
= csum_block_add(csum
, csum2
, pos
);
2103 if ((len
-= copy
) == 0)
2114 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2116 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2121 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2122 csstart
= skb_checksum_start_offset(skb
);
2124 csstart
= skb_headlen(skb
);
2126 BUG_ON(csstart
> skb_headlen(skb
));
2128 skb_copy_from_linear_data(skb
, to
, csstart
);
2131 if (csstart
!= skb
->len
)
2132 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2133 skb
->len
- csstart
, 0);
2135 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2136 long csstuff
= csstart
+ skb
->csum_offset
;
2138 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2141 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2144 * skb_dequeue - remove from the head of the queue
2145 * @list: list to dequeue from
2147 * Remove the head of the list. The list lock is taken so the function
2148 * may be used safely with other locking list functions. The head item is
2149 * returned or %NULL if the list is empty.
2152 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2154 unsigned long flags
;
2155 struct sk_buff
*result
;
2157 spin_lock_irqsave(&list
->lock
, flags
);
2158 result
= __skb_dequeue(list
);
2159 spin_unlock_irqrestore(&list
->lock
, flags
);
2162 EXPORT_SYMBOL(skb_dequeue
);
2165 * skb_dequeue_tail - remove from the tail of the queue
2166 * @list: list to dequeue from
2168 * Remove the tail of the list. The list lock is taken so the function
2169 * may be used safely with other locking list functions. The tail item is
2170 * returned or %NULL if the list is empty.
2172 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2174 unsigned long flags
;
2175 struct sk_buff
*result
;
2177 spin_lock_irqsave(&list
->lock
, flags
);
2178 result
= __skb_dequeue_tail(list
);
2179 spin_unlock_irqrestore(&list
->lock
, flags
);
2182 EXPORT_SYMBOL(skb_dequeue_tail
);
2185 * skb_queue_purge - empty a list
2186 * @list: list to empty
2188 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2189 * the list and one reference dropped. This function takes the list
2190 * lock and is atomic with respect to other list locking functions.
2192 void skb_queue_purge(struct sk_buff_head
*list
)
2194 struct sk_buff
*skb
;
2195 while ((skb
= skb_dequeue(list
)) != NULL
)
2198 EXPORT_SYMBOL(skb_queue_purge
);
2201 * skb_queue_head - queue a buffer at the list head
2202 * @list: list to use
2203 * @newsk: buffer to queue
2205 * Queue a buffer at the start of the list. This function takes the
2206 * list lock and can be used safely with other locking &sk_buff functions
2209 * A buffer cannot be placed on two lists at the same time.
2211 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2213 unsigned long flags
;
2215 spin_lock_irqsave(&list
->lock
, flags
);
2216 __skb_queue_head(list
, newsk
);
2217 spin_unlock_irqrestore(&list
->lock
, flags
);
2219 EXPORT_SYMBOL(skb_queue_head
);
2222 * skb_queue_tail - queue a buffer at the list tail
2223 * @list: list to use
2224 * @newsk: buffer to queue
2226 * Queue a buffer at the tail of the list. This function takes the
2227 * list lock and can be used safely with other locking &sk_buff functions
2230 * A buffer cannot be placed on two lists at the same time.
2232 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2234 unsigned long flags
;
2236 spin_lock_irqsave(&list
->lock
, flags
);
2237 __skb_queue_tail(list
, newsk
);
2238 spin_unlock_irqrestore(&list
->lock
, flags
);
2240 EXPORT_SYMBOL(skb_queue_tail
);
2243 * skb_unlink - remove a buffer from a list
2244 * @skb: buffer to remove
2245 * @list: list to use
2247 * Remove a packet from a list. The list locks are taken and this
2248 * function is atomic with respect to other list locked calls
2250 * You must know what list the SKB is on.
2252 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
2254 unsigned long flags
;
2256 spin_lock_irqsave(&list
->lock
, flags
);
2257 __skb_unlink(skb
, list
);
2258 spin_unlock_irqrestore(&list
->lock
, flags
);
2260 EXPORT_SYMBOL(skb_unlink
);
2263 * skb_append - append a buffer
2264 * @old: buffer to insert after
2265 * @newsk: buffer to insert
2266 * @list: list to use
2268 * Place a packet after a given packet in a list. The list locks are taken
2269 * and this function is atomic with respect to other list locked calls.
2270 * A buffer cannot be placed on two lists at the same time.
2272 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2274 unsigned long flags
;
2276 spin_lock_irqsave(&list
->lock
, flags
);
2277 __skb_queue_after(list
, old
, newsk
);
2278 spin_unlock_irqrestore(&list
->lock
, flags
);
2280 EXPORT_SYMBOL(skb_append
);
2283 * skb_insert - insert a buffer
2284 * @old: buffer to insert before
2285 * @newsk: buffer to insert
2286 * @list: list to use
2288 * Place a packet before a given packet in a list. The list locks are
2289 * taken and this function is atomic with respect to other list locked
2292 * A buffer cannot be placed on two lists at the same time.
2294 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2296 unsigned long flags
;
2298 spin_lock_irqsave(&list
->lock
, flags
);
2299 __skb_insert(newsk
, old
->prev
, old
, list
);
2300 spin_unlock_irqrestore(&list
->lock
, flags
);
2302 EXPORT_SYMBOL(skb_insert
);
2304 static inline void skb_split_inside_header(struct sk_buff
*skb
,
2305 struct sk_buff
* skb1
,
2306 const u32 len
, const int pos
)
2310 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
2312 /* And move data appendix as is. */
2313 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
2314 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
2316 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
2317 skb_shinfo(skb
)->nr_frags
= 0;
2318 skb1
->data_len
= skb
->data_len
;
2319 skb1
->len
+= skb1
->data_len
;
2322 skb_set_tail_pointer(skb
, len
);
2325 static inline void skb_split_no_header(struct sk_buff
*skb
,
2326 struct sk_buff
* skb1
,
2327 const u32 len
, int pos
)
2330 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
2332 skb_shinfo(skb
)->nr_frags
= 0;
2333 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
2335 skb
->data_len
= len
- pos
;
2337 for (i
= 0; i
< nfrags
; i
++) {
2338 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2340 if (pos
+ size
> len
) {
2341 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2345 * We have two variants in this case:
2346 * 1. Move all the frag to the second
2347 * part, if it is possible. F.e.
2348 * this approach is mandatory for TUX,
2349 * where splitting is expensive.
2350 * 2. Split is accurately. We make this.
2352 skb_frag_ref(skb
, i
);
2353 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
2354 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
2355 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
2356 skb_shinfo(skb
)->nr_frags
++;
2360 skb_shinfo(skb
)->nr_frags
++;
2363 skb_shinfo(skb1
)->nr_frags
= k
;
2367 * skb_split - Split fragmented skb to two parts at length len.
2368 * @skb: the buffer to split
2369 * @skb1: the buffer to receive the second part
2370 * @len: new length for skb
2372 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
2374 int pos
= skb_headlen(skb
);
2376 if (len
< pos
) /* Split line is inside header. */
2377 skb_split_inside_header(skb
, skb1
, len
, pos
);
2378 else /* Second chunk has no header, nothing to copy. */
2379 skb_split_no_header(skb
, skb1
, len
, pos
);
2381 EXPORT_SYMBOL(skb_split
);
2383 /* Shifting from/to a cloned skb is a no-go.
2385 * Caller cannot keep skb_shinfo related pointers past calling here!
2387 static int skb_prepare_for_shift(struct sk_buff
*skb
)
2389 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
2393 * skb_shift - Shifts paged data partially from skb to another
2394 * @tgt: buffer into which tail data gets added
2395 * @skb: buffer from which the paged data comes from
2396 * @shiftlen: shift up to this many bytes
2398 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2399 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2400 * It's up to caller to free skb if everything was shifted.
2402 * If @tgt runs out of frags, the whole operation is aborted.
2404 * Skb cannot include anything else but paged data while tgt is allowed
2405 * to have non-paged data as well.
2407 * TODO: full sized shift could be optimized but that would need
2408 * specialized skb free'er to handle frags without up-to-date nr_frags.
2410 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
2412 int from
, to
, merge
, todo
;
2413 struct skb_frag_struct
*fragfrom
, *fragto
;
2415 BUG_ON(shiftlen
> skb
->len
);
2416 BUG_ON(skb_headlen(skb
)); /* Would corrupt stream */
2420 to
= skb_shinfo(tgt
)->nr_frags
;
2421 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2423 /* Actual merge is delayed until the point when we know we can
2424 * commit all, so that we don't have to undo partial changes
2427 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
2428 fragfrom
->page_offset
)) {
2433 todo
-= skb_frag_size(fragfrom
);
2435 if (skb_prepare_for_shift(skb
) ||
2436 skb_prepare_for_shift(tgt
))
2439 /* All previous frag pointers might be stale! */
2440 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2441 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2443 skb_frag_size_add(fragto
, shiftlen
);
2444 skb_frag_size_sub(fragfrom
, shiftlen
);
2445 fragfrom
->page_offset
+= shiftlen
;
2453 /* Skip full, not-fitting skb to avoid expensive operations */
2454 if ((shiftlen
== skb
->len
) &&
2455 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
2458 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
2461 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
2462 if (to
== MAX_SKB_FRAGS
)
2465 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2466 fragto
= &skb_shinfo(tgt
)->frags
[to
];
2468 if (todo
>= skb_frag_size(fragfrom
)) {
2469 *fragto
= *fragfrom
;
2470 todo
-= skb_frag_size(fragfrom
);
2475 __skb_frag_ref(fragfrom
);
2476 fragto
->page
= fragfrom
->page
;
2477 fragto
->page_offset
= fragfrom
->page_offset
;
2478 skb_frag_size_set(fragto
, todo
);
2480 fragfrom
->page_offset
+= todo
;
2481 skb_frag_size_sub(fragfrom
, todo
);
2489 /* Ready to "commit" this state change to tgt */
2490 skb_shinfo(tgt
)->nr_frags
= to
;
2493 fragfrom
= &skb_shinfo(skb
)->frags
[0];
2494 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2496 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
2497 __skb_frag_unref(fragfrom
);
2500 /* Reposition in the original skb */
2502 while (from
< skb_shinfo(skb
)->nr_frags
)
2503 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
2504 skb_shinfo(skb
)->nr_frags
= to
;
2506 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
2509 /* Most likely the tgt won't ever need its checksum anymore, skb on
2510 * the other hand might need it if it needs to be resent
2512 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
2513 skb
->ip_summed
= CHECKSUM_PARTIAL
;
2515 /* Yak, is it really working this way? Some helper please? */
2516 skb
->len
-= shiftlen
;
2517 skb
->data_len
-= shiftlen
;
2518 skb
->truesize
-= shiftlen
;
2519 tgt
->len
+= shiftlen
;
2520 tgt
->data_len
+= shiftlen
;
2521 tgt
->truesize
+= shiftlen
;
2527 * skb_prepare_seq_read - Prepare a sequential read of skb data
2528 * @skb: the buffer to read
2529 * @from: lower offset of data to be read
2530 * @to: upper offset of data to be read
2531 * @st: state variable
2533 * Initializes the specified state variable. Must be called before
2534 * invoking skb_seq_read() for the first time.
2536 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
2537 unsigned int to
, struct skb_seq_state
*st
)
2539 st
->lower_offset
= from
;
2540 st
->upper_offset
= to
;
2541 st
->root_skb
= st
->cur_skb
= skb
;
2542 st
->frag_idx
= st
->stepped_offset
= 0;
2543 st
->frag_data
= NULL
;
2545 EXPORT_SYMBOL(skb_prepare_seq_read
);
2548 * skb_seq_read - Sequentially read skb data
2549 * @consumed: number of bytes consumed by the caller so far
2550 * @data: destination pointer for data to be returned
2551 * @st: state variable
2553 * Reads a block of skb data at &consumed relative to the
2554 * lower offset specified to skb_prepare_seq_read(). Assigns
2555 * the head of the data block to &data and returns the length
2556 * of the block or 0 if the end of the skb data or the upper
2557 * offset has been reached.
2559 * The caller is not required to consume all of the data
2560 * returned, i.e. &consumed is typically set to the number
2561 * of bytes already consumed and the next call to
2562 * skb_seq_read() will return the remaining part of the block.
2564 * Note 1: The size of each block of data returned can be arbitrary,
2565 * this limitation is the cost for zerocopy seqeuental
2566 * reads of potentially non linear data.
2568 * Note 2: Fragment lists within fragments are not implemented
2569 * at the moment, state->root_skb could be replaced with
2570 * a stack for this purpose.
2572 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
2573 struct skb_seq_state
*st
)
2575 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
2578 if (unlikely(abs_offset
>= st
->upper_offset
))
2582 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
2584 if (abs_offset
< block_limit
&& !st
->frag_data
) {
2585 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
2586 return block_limit
- abs_offset
;
2589 if (st
->frag_idx
== 0 && !st
->frag_data
)
2590 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
2592 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
2593 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
2594 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
2596 if (abs_offset
< block_limit
) {
2598 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
2600 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
2601 (abs_offset
- st
->stepped_offset
);
2603 return block_limit
- abs_offset
;
2606 if (st
->frag_data
) {
2607 kunmap_atomic(st
->frag_data
);
2608 st
->frag_data
= NULL
;
2612 st
->stepped_offset
+= skb_frag_size(frag
);
2615 if (st
->frag_data
) {
2616 kunmap_atomic(st
->frag_data
);
2617 st
->frag_data
= NULL
;
2620 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
2621 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2624 } else if (st
->cur_skb
->next
) {
2625 st
->cur_skb
= st
->cur_skb
->next
;
2632 EXPORT_SYMBOL(skb_seq_read
);
2635 * skb_abort_seq_read - Abort a sequential read of skb data
2636 * @st: state variable
2638 * Must be called if skb_seq_read() was not called until it
2641 void skb_abort_seq_read(struct skb_seq_state
*st
)
2644 kunmap_atomic(st
->frag_data
);
2646 EXPORT_SYMBOL(skb_abort_seq_read
);
2648 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2650 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2651 struct ts_config
*conf
,
2652 struct ts_state
*state
)
2654 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2657 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2659 skb_abort_seq_read(TS_SKB_CB(state
));
2663 * skb_find_text - Find a text pattern in skb data
2664 * @skb: the buffer to look in
2665 * @from: search offset
2667 * @config: textsearch configuration
2668 * @state: uninitialized textsearch state variable
2670 * Finds a pattern in the skb data according to the specified
2671 * textsearch configuration. Use textsearch_next() to retrieve
2672 * subsequent occurrences of the pattern. Returns the offset
2673 * to the first occurrence or UINT_MAX if no match was found.
2675 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2676 unsigned int to
, struct ts_config
*config
,
2677 struct ts_state
*state
)
2681 config
->get_next_block
= skb_ts_get_next_block
;
2682 config
->finish
= skb_ts_finish
;
2684 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2686 ret
= textsearch_find(config
, state
);
2687 return (ret
<= to
- from
? ret
: UINT_MAX
);
2689 EXPORT_SYMBOL(skb_find_text
);
2692 * skb_append_datato_frags - append the user data to a skb
2693 * @sk: sock structure
2694 * @skb: skb structure to be appened with user data.
2695 * @getfrag: call back function to be used for getting the user data
2696 * @from: pointer to user message iov
2697 * @length: length of the iov message
2699 * Description: This procedure append the user data in the fragment part
2700 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2702 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2703 int (*getfrag
)(void *from
, char *to
, int offset
,
2704 int len
, int odd
, struct sk_buff
*skb
),
2705 void *from
, int length
)
2708 skb_frag_t
*frag
= NULL
;
2709 struct page
*page
= NULL
;
2715 /* Return error if we don't have space for new frag */
2716 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2717 if (frg_cnt
>= MAX_SKB_FRAGS
)
2720 /* allocate a new page for next frag */
2721 page
= alloc_pages(sk
->sk_allocation
, 0);
2723 /* If alloc_page fails just return failure and caller will
2724 * free previous allocated pages by doing kfree_skb()
2729 /* initialize the next frag */
2730 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2731 skb
->truesize
+= PAGE_SIZE
;
2732 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2734 /* get the new initialized frag */
2735 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2736 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2738 /* copy the user data to page */
2739 left
= PAGE_SIZE
- frag
->page_offset
;
2740 copy
= (length
> left
)? left
: length
;
2742 ret
= getfrag(from
, skb_frag_address(frag
) + skb_frag_size(frag
),
2743 offset
, copy
, 0, skb
);
2747 /* copy was successful so update the size parameters */
2748 skb_frag_size_add(frag
, copy
);
2750 skb
->data_len
+= copy
;
2754 } while (length
> 0);
2758 EXPORT_SYMBOL(skb_append_datato_frags
);
2761 * skb_pull_rcsum - pull skb and update receive checksum
2762 * @skb: buffer to update
2763 * @len: length of data pulled
2765 * This function performs an skb_pull on the packet and updates
2766 * the CHECKSUM_COMPLETE checksum. It should be used on
2767 * receive path processing instead of skb_pull unless you know
2768 * that the checksum difference is zero (e.g., a valid IP header)
2769 * or you are setting ip_summed to CHECKSUM_NONE.
2771 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2773 BUG_ON(len
> skb
->len
);
2775 BUG_ON(skb
->len
< skb
->data_len
);
2776 skb_postpull_rcsum(skb
, skb
->data
, len
);
2777 return skb
->data
+= len
;
2779 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2782 * skb_segment - Perform protocol segmentation on skb.
2783 * @skb: buffer to segment
2784 * @features: features for the output path (see dev->features)
2786 * This function performs segmentation on the given skb. It returns
2787 * a pointer to the first in a list of new skbs for the segments.
2788 * In case of error it returns ERR_PTR(err).
2790 struct sk_buff
*skb_segment(struct sk_buff
*skb
, netdev_features_t features
)
2792 struct sk_buff
*segs
= NULL
;
2793 struct sk_buff
*tail
= NULL
;
2794 struct sk_buff
*fskb
= skb_shinfo(skb
)->frag_list
;
2795 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2796 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2797 unsigned int offset
= doffset
;
2798 unsigned int headroom
;
2800 int sg
= !!(features
& NETIF_F_SG
);
2801 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2806 __skb_push(skb
, doffset
);
2807 headroom
= skb_headroom(skb
);
2808 pos
= skb_headlen(skb
);
2811 struct sk_buff
*nskb
;
2816 len
= skb
->len
- offset
;
2820 hsize
= skb_headlen(skb
) - offset
;
2823 if (hsize
> len
|| !sg
)
2826 if (!hsize
&& i
>= nfrags
) {
2827 BUG_ON(fskb
->len
!= len
);
2830 nskb
= skb_clone(fskb
, GFP_ATOMIC
);
2833 if (unlikely(!nskb
))
2836 hsize
= skb_end_offset(nskb
);
2837 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
2842 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
2843 skb_release_head_state(nskb
);
2844 __skb_push(nskb
, doffset
);
2846 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
2847 GFP_ATOMIC
, skb_alloc_rx_flag(skb
),
2850 if (unlikely(!nskb
))
2853 skb_reserve(nskb
, headroom
);
2854 __skb_put(nskb
, doffset
);
2863 __copy_skb_header(nskb
, skb
);
2864 nskb
->mac_len
= skb
->mac_len
;
2866 /* nskb and skb might have different headroom */
2867 if (nskb
->ip_summed
== CHECKSUM_PARTIAL
)
2868 nskb
->csum_start
+= skb_headroom(nskb
) - headroom
;
2870 skb_reset_mac_header(nskb
);
2871 skb_set_network_header(nskb
, skb
->mac_len
);
2872 nskb
->transport_header
= (nskb
->network_header
+
2873 skb_network_header_len(skb
));
2874 skb_copy_from_linear_data(skb
, nskb
->data
, doffset
);
2876 if (fskb
!= skb_shinfo(skb
)->frag_list
)
2880 nskb
->ip_summed
= CHECKSUM_NONE
;
2881 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2887 frag
= skb_shinfo(nskb
)->frags
;
2889 skb_copy_from_linear_data_offset(skb
, offset
,
2890 skb_put(nskb
, hsize
), hsize
);
2892 while (pos
< offset
+ len
&& i
< nfrags
) {
2893 *frag
= skb_shinfo(skb
)->frags
[i
];
2894 __skb_frag_ref(frag
);
2895 size
= skb_frag_size(frag
);
2898 frag
->page_offset
+= offset
- pos
;
2899 skb_frag_size_sub(frag
, offset
- pos
);
2902 skb_shinfo(nskb
)->nr_frags
++;
2904 if (pos
+ size
<= offset
+ len
) {
2908 skb_frag_size_sub(frag
, pos
+ size
- (offset
+ len
));
2915 if (pos
< offset
+ len
) {
2916 struct sk_buff
*fskb2
= fskb
;
2918 BUG_ON(pos
+ fskb
->len
!= offset
+ len
);
2924 fskb2
= skb_clone(fskb2
, GFP_ATOMIC
);
2930 SKB_FRAG_ASSERT(nskb
);
2931 skb_shinfo(nskb
)->frag_list
= fskb2
;
2935 nskb
->data_len
= len
- hsize
;
2936 nskb
->len
+= nskb
->data_len
;
2937 nskb
->truesize
+= nskb
->data_len
;
2938 } while ((offset
+= len
) < skb
->len
);
2943 while ((skb
= segs
)) {
2947 return ERR_PTR(err
);
2949 EXPORT_SYMBOL_GPL(skb_segment
);
2951 int skb_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
)
2953 struct sk_buff
*p
= *head
;
2954 struct sk_buff
*nskb
;
2955 struct skb_shared_info
*skbinfo
= skb_shinfo(skb
);
2956 struct skb_shared_info
*pinfo
= skb_shinfo(p
);
2957 unsigned int headroom
;
2958 unsigned int len
= skb_gro_len(skb
);
2959 unsigned int offset
= skb_gro_offset(skb
);
2960 unsigned int headlen
= skb_headlen(skb
);
2961 unsigned int delta_truesize
;
2963 if (p
->len
+ len
>= 65536)
2966 if (pinfo
->frag_list
)
2968 else if (headlen
<= offset
) {
2971 int i
= skbinfo
->nr_frags
;
2972 int nr_frags
= pinfo
->nr_frags
+ i
;
2976 if (nr_frags
> MAX_SKB_FRAGS
)
2979 pinfo
->nr_frags
= nr_frags
;
2980 skbinfo
->nr_frags
= 0;
2982 frag
= pinfo
->frags
+ nr_frags
;
2983 frag2
= skbinfo
->frags
+ i
;
2988 frag
->page_offset
+= offset
;
2989 skb_frag_size_sub(frag
, offset
);
2991 /* all fragments truesize : remove (head size + sk_buff) */
2992 delta_truesize
= skb
->truesize
-
2993 SKB_TRUESIZE(skb_end_offset(skb
));
2995 skb
->truesize
-= skb
->data_len
;
2996 skb
->len
-= skb
->data_len
;
2999 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3001 } else if (skb
->head_frag
) {
3002 int nr_frags
= pinfo
->nr_frags
;
3003 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3004 struct page
*page
= virt_to_head_page(skb
->head
);
3005 unsigned int first_size
= headlen
- offset
;
3006 unsigned int first_offset
;
3008 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3011 first_offset
= skb
->data
-
3012 (unsigned char *)page_address(page
) +
3015 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3017 frag
->page
.p
= page
;
3018 frag
->page_offset
= first_offset
;
3019 skb_frag_size_set(frag
, first_size
);
3021 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3022 /* We dont need to clear skbinfo->nr_frags here */
3024 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3025 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3027 } else if (skb_gro_len(p
) != pinfo
->gso_size
)
3030 headroom
= skb_headroom(p
);
3031 nskb
= alloc_skb(headroom
+ skb_gro_offset(p
), GFP_ATOMIC
);
3032 if (unlikely(!nskb
))
3035 __copy_skb_header(nskb
, p
);
3036 nskb
->mac_len
= p
->mac_len
;
3038 skb_reserve(nskb
, headroom
);
3039 __skb_put(nskb
, skb_gro_offset(p
));
3041 skb_set_mac_header(nskb
, skb_mac_header(p
) - p
->data
);
3042 skb_set_network_header(nskb
, skb_network_offset(p
));
3043 skb_set_transport_header(nskb
, skb_transport_offset(p
));
3045 __skb_pull(p
, skb_gro_offset(p
));
3046 memcpy(skb_mac_header(nskb
), skb_mac_header(p
),
3047 p
->data
- skb_mac_header(p
));
3049 *NAPI_GRO_CB(nskb
) = *NAPI_GRO_CB(p
);
3050 skb_shinfo(nskb
)->frag_list
= p
;
3051 skb_shinfo(nskb
)->gso_size
= pinfo
->gso_size
;
3052 pinfo
->gso_size
= 0;
3053 skb_header_release(p
);
3056 nskb
->data_len
+= p
->len
;
3057 nskb
->truesize
+= p
->truesize
;
3058 nskb
->len
+= p
->len
;
3061 nskb
->next
= p
->next
;
3067 delta_truesize
= skb
->truesize
;
3068 if (offset
> headlen
) {
3069 unsigned int eat
= offset
- headlen
;
3071 skbinfo
->frags
[0].page_offset
+= eat
;
3072 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3073 skb
->data_len
-= eat
;
3078 __skb_pull(skb
, offset
);
3080 p
->prev
->next
= skb
;
3082 skb_header_release(skb
);
3085 NAPI_GRO_CB(p
)->count
++;
3087 p
->truesize
+= delta_truesize
;
3090 NAPI_GRO_CB(skb
)->same_flow
= 1;
3093 EXPORT_SYMBOL_GPL(skb_gro_receive
);
3095 void __init
skb_init(void)
3097 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
3098 sizeof(struct sk_buff
),
3100 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3102 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3103 (2*sizeof(struct sk_buff
)) +
3106 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3111 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3112 * @skb: Socket buffer containing the buffers to be mapped
3113 * @sg: The scatter-gather list to map into
3114 * @offset: The offset into the buffer's contents to start mapping
3115 * @len: Length of buffer space to be mapped
3117 * Fill the specified scatter-gather list with mappings/pointers into a
3118 * region of the buffer space attached to a socket buffer.
3121 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3123 int start
= skb_headlen(skb
);
3124 int i
, copy
= start
- offset
;
3125 struct sk_buff
*frag_iter
;
3131 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
3133 if ((len
-= copy
) == 0)
3138 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3141 WARN_ON(start
> offset
+ len
);
3143 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3144 if ((copy
= end
- offset
) > 0) {
3145 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
3149 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
3150 frag
->page_offset
+offset
-start
);
3159 skb_walk_frags(skb
, frag_iter
) {
3162 WARN_ON(start
> offset
+ len
);
3164 end
= start
+ frag_iter
->len
;
3165 if ((copy
= end
- offset
) > 0) {
3168 elt
+= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
3170 if ((len
-= copy
) == 0)
3180 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3182 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
3184 sg_mark_end(&sg
[nsg
- 1]);
3188 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
3191 * skb_cow_data - Check that a socket buffer's data buffers are writable
3192 * @skb: The socket buffer to check.
3193 * @tailbits: Amount of trailing space to be added
3194 * @trailer: Returned pointer to the skb where the @tailbits space begins
3196 * Make sure that the data buffers attached to a socket buffer are
3197 * writable. If they are not, private copies are made of the data buffers
3198 * and the socket buffer is set to use these instead.
3200 * If @tailbits is given, make sure that there is space to write @tailbits
3201 * bytes of data beyond current end of socket buffer. @trailer will be
3202 * set to point to the skb in which this space begins.
3204 * The number of scatterlist elements required to completely map the
3205 * COW'd and extended socket buffer will be returned.
3207 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
3211 struct sk_buff
*skb1
, **skb_p
;
3213 /* If skb is cloned or its head is paged, reallocate
3214 * head pulling out all the pages (pages are considered not writable
3215 * at the moment even if they are anonymous).
3217 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
3218 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
3221 /* Easy case. Most of packets will go this way. */
3222 if (!skb_has_frag_list(skb
)) {
3223 /* A little of trouble, not enough of space for trailer.
3224 * This should not happen, when stack is tuned to generate
3225 * good frames. OK, on miss we reallocate and reserve even more
3226 * space, 128 bytes is fair. */
3228 if (skb_tailroom(skb
) < tailbits
&&
3229 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
3237 /* Misery. We are in troubles, going to mincer fragments... */
3240 skb_p
= &skb_shinfo(skb
)->frag_list
;
3243 while ((skb1
= *skb_p
) != NULL
) {
3246 /* The fragment is partially pulled by someone,
3247 * this can happen on input. Copy it and everything
3250 if (skb_shared(skb1
))
3253 /* If the skb is the last, worry about trailer. */
3255 if (skb1
->next
== NULL
&& tailbits
) {
3256 if (skb_shinfo(skb1
)->nr_frags
||
3257 skb_has_frag_list(skb1
) ||
3258 skb_tailroom(skb1
) < tailbits
)
3259 ntail
= tailbits
+ 128;
3265 skb_shinfo(skb1
)->nr_frags
||
3266 skb_has_frag_list(skb1
)) {
3267 struct sk_buff
*skb2
;
3269 /* Fuck, we are miserable poor guys... */
3271 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
3273 skb2
= skb_copy_expand(skb1
,
3277 if (unlikely(skb2
== NULL
))
3281 skb_set_owner_w(skb2
, skb1
->sk
);
3283 /* Looking around. Are we still alive?
3284 * OK, link new skb, drop old one */
3286 skb2
->next
= skb1
->next
;
3293 skb_p
= &skb1
->next
;
3298 EXPORT_SYMBOL_GPL(skb_cow_data
);
3300 static void sock_rmem_free(struct sk_buff
*skb
)
3302 struct sock
*sk
= skb
->sk
;
3304 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
3308 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3310 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
3314 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
3315 (unsigned int)sk
->sk_rcvbuf
)
3320 skb
->destructor
= sock_rmem_free
;
3321 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
3323 /* before exiting rcu section, make sure dst is refcounted */
3326 skb_queue_tail(&sk
->sk_error_queue
, skb
);
3327 if (!sock_flag(sk
, SOCK_DEAD
))
3328 sk
->sk_data_ready(sk
, len
);
3331 EXPORT_SYMBOL(sock_queue_err_skb
);
3333 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
3334 struct skb_shared_hwtstamps
*hwtstamps
)
3336 struct sock
*sk
= orig_skb
->sk
;
3337 struct sock_exterr_skb
*serr
;
3338 struct sk_buff
*skb
;
3344 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
3349 *skb_hwtstamps(skb
) =
3353 * no hardware time stamps available,
3354 * so keep the shared tx_flags and only
3355 * store software time stamp
3357 skb
->tstamp
= ktime_get_real();
3360 serr
= SKB_EXT_ERR(skb
);
3361 memset(serr
, 0, sizeof(*serr
));
3362 serr
->ee
.ee_errno
= ENOMSG
;
3363 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
3365 err
= sock_queue_err_skb(sk
, skb
);
3370 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
3372 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
3374 struct sock
*sk
= skb
->sk
;
3375 struct sock_exterr_skb
*serr
;
3378 skb
->wifi_acked_valid
= 1;
3379 skb
->wifi_acked
= acked
;
3381 serr
= SKB_EXT_ERR(skb
);
3382 memset(serr
, 0, sizeof(*serr
));
3383 serr
->ee
.ee_errno
= ENOMSG
;
3384 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
3386 err
= sock_queue_err_skb(sk
, skb
);
3390 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
3394 * skb_partial_csum_set - set up and verify partial csum values for packet
3395 * @skb: the skb to set
3396 * @start: the number of bytes after skb->data to start checksumming.
3397 * @off: the offset from start to place the checksum.
3399 * For untrusted partially-checksummed packets, we need to make sure the values
3400 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3402 * This function checks and sets those values and skb->ip_summed: if this
3403 * returns false you should drop the packet.
3405 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
3407 if (unlikely(start
> skb_headlen(skb
)) ||
3408 unlikely((int)start
+ off
> skb_headlen(skb
) - 2)) {
3409 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3410 start
, off
, skb_headlen(skb
));
3413 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3414 skb
->csum_start
= skb_headroom(skb
) + start
;
3415 skb
->csum_offset
= off
;
3418 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
3420 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
3422 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3425 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
3427 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
3430 kmem_cache_free(skbuff_head_cache
, skb
);
3434 EXPORT_SYMBOL(kfree_skb_partial
);
3437 * skb_try_coalesce - try to merge skb to prior one
3439 * @from: buffer to add
3440 * @fragstolen: pointer to boolean
3441 * @delta_truesize: how much more was allocated than was requested
3443 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
3444 bool *fragstolen
, int *delta_truesize
)
3446 int i
, delta
, len
= from
->len
;
3448 *fragstolen
= false;
3453 if (len
<= skb_tailroom(to
)) {
3454 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
3455 *delta_truesize
= 0;
3459 if (skb_has_frag_list(to
) || skb_has_frag_list(from
))
3462 if (skb_headlen(from
) != 0) {
3464 unsigned int offset
;
3466 if (skb_shinfo(to
)->nr_frags
+
3467 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
3470 if (skb_head_is_locked(from
))
3473 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3475 page
= virt_to_head_page(from
->head
);
3476 offset
= from
->data
- (unsigned char *)page_address(page
);
3478 skb_fill_page_desc(to
, skb_shinfo(to
)->nr_frags
,
3479 page
, offset
, skb_headlen(from
));
3482 if (skb_shinfo(to
)->nr_frags
+
3483 skb_shinfo(from
)->nr_frags
> MAX_SKB_FRAGS
)
3486 delta
= from
->truesize
-
3487 SKB_TRUESIZE(skb_end_pointer(from
) - from
->head
);
3490 WARN_ON_ONCE(delta
< len
);
3492 memcpy(skb_shinfo(to
)->frags
+ skb_shinfo(to
)->nr_frags
,
3493 skb_shinfo(from
)->frags
,
3494 skb_shinfo(from
)->nr_frags
* sizeof(skb_frag_t
));
3495 skb_shinfo(to
)->nr_frags
+= skb_shinfo(from
)->nr_frags
;
3497 if (!skb_cloned(from
))
3498 skb_shinfo(from
)->nr_frags
= 0;
3500 /* if the skb is not cloned this does nothing
3501 * since we set nr_frags to 0.
3503 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++)
3504 skb_frag_ref(from
, i
);
3506 to
->truesize
+= delta
;
3508 to
->data_len
+= len
;
3510 *delta_truesize
= delta
;
3513 EXPORT_SYMBOL(skb_try_coalesce
);