2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
78 struct kmem_cache
*skbuff_head_cache __read_mostly
;
79 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
82 * skb_panic - private function for out-of-line support
86 * @msg: skb_over_panic or skb_under_panic
88 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
93 static void skb_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
,
96 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
97 msg
, addr
, skb
->len
, sz
, skb
->head
, skb
->data
,
98 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
99 skb
->dev
? skb
->dev
->name
: "<NULL>");
103 static void skb_over_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
105 skb_panic(skb
, sz
, addr
, __func__
);
108 static void skb_under_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
110 skb_panic(skb
, sz
, addr
, __func__
);
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
120 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
123 static void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
,
124 unsigned long ip
, bool *pfmemalloc
)
127 bool ret_pfmemalloc
= false;
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
133 obj
= kmalloc_node_track_caller(size
,
134 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
136 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc
= true;
141 obj
= kmalloc_node_track_caller(size
, flags
, node
);
145 *pfmemalloc
= ret_pfmemalloc
;
150 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
156 struct sk_buff
*__alloc_skb_head(gfp_t gfp_mask
, int node
)
161 skb
= kmem_cache_alloc_node(skbuff_head_cache
,
162 gfp_mask
& ~__GFP_DMA
, node
);
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
171 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
173 skb
->truesize
= sizeof(struct sk_buff
);
174 atomic_set(&skb
->users
, 1);
176 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
182 * __alloc_skb - allocate a network buffer
183 * @size: size to allocate
184 * @gfp_mask: allocation mask
185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
189 * @node: numa node to allocate memory on
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
201 struct kmem_cache
*cache
;
202 struct skb_shared_info
*shinfo
;
207 cache
= (flags
& SKB_ALLOC_FCLONE
)
208 ? skbuff_fclone_cache
: skbuff_head_cache
;
210 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
211 gfp_mask
|= __GFP_MEMALLOC
;
214 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
224 size
= SKB_DATA_ALIGN(size
);
225 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
226 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
233 size
= SKB_WITH_OVERHEAD(ksize(data
));
234 prefetchw(data
+ size
);
237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
241 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
242 /* Account for allocated memory : skb + skb->head */
243 skb
->truesize
= SKB_TRUESIZE(size
);
244 skb
->pfmemalloc
= pfmemalloc
;
245 atomic_set(&skb
->users
, 1);
248 skb_reset_tail_pointer(skb
);
249 skb
->end
= skb
->tail
+ size
;
250 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
251 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
253 /* make sure we initialize shinfo sequentially */
254 shinfo
= skb_shinfo(skb
);
255 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
256 atomic_set(&shinfo
->dataref
, 1);
257 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
259 if (flags
& SKB_ALLOC_FCLONE
) {
260 struct sk_buff
*child
= skb
+ 1;
261 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
263 kmemcheck_annotate_bitfield(child
, flags1
);
264 kmemcheck_annotate_bitfield(child
, flags2
);
265 skb
->fclone
= SKB_FCLONE_ORIG
;
266 atomic_set(fclone_ref
, 1);
268 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
269 child
->pfmemalloc
= pfmemalloc
;
274 kmem_cache_free(cache
, skb
);
278 EXPORT_SYMBOL(__alloc_skb
);
281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
283 * @frag_size: size of fragment, or 0 if head was kmalloced
285 * Allocate a new &sk_buff. Caller provides space holding head and
286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
298 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
300 struct skb_shared_info
*shinfo
;
302 unsigned int size
= frag_size
? : ksize(data
);
304 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
308 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
310 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
311 skb
->truesize
= SKB_TRUESIZE(size
);
312 skb
->head_frag
= frag_size
!= 0;
313 atomic_set(&skb
->users
, 1);
316 skb_reset_tail_pointer(skb
);
317 skb
->end
= skb
->tail
+ size
;
318 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
319 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
321 /* make sure we initialize shinfo sequentially */
322 shinfo
= skb_shinfo(skb
);
323 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
324 atomic_set(&shinfo
->dataref
, 1);
325 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
329 EXPORT_SYMBOL(build_skb
);
331 struct netdev_alloc_cache
{
332 struct page_frag frag
;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
336 unsigned int pagecnt_bias
;
338 static DEFINE_PER_CPU(struct netdev_alloc_cache
, netdev_alloc_cache
);
340 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
342 struct netdev_alloc_cache
*nc
;
347 local_irq_save(flags
);
348 nc
= &__get_cpu_var(netdev_alloc_cache
);
349 if (unlikely(!nc
->frag
.page
)) {
351 for (order
= NETDEV_FRAG_PAGE_MAX_ORDER
; ;) {
352 gfp_t gfp
= gfp_mask
;
355 gfp
|= __GFP_COMP
| __GFP_NOWARN
;
356 nc
->frag
.page
= alloc_pages(gfp
, order
);
357 if (likely(nc
->frag
.page
))
362 nc
->frag
.size
= PAGE_SIZE
<< order
;
364 atomic_set(&nc
->frag
.page
->_count
, NETDEV_PAGECNT_MAX_BIAS
);
365 nc
->pagecnt_bias
= NETDEV_PAGECNT_MAX_BIAS
;
369 if (nc
->frag
.offset
+ fragsz
> nc
->frag
.size
) {
370 /* avoid unnecessary locked operations if possible */
371 if ((atomic_read(&nc
->frag
.page
->_count
) == nc
->pagecnt_bias
) ||
372 atomic_sub_and_test(nc
->pagecnt_bias
, &nc
->frag
.page
->_count
))
377 data
= page_address(nc
->frag
.page
) + nc
->frag
.offset
;
378 nc
->frag
.offset
+= fragsz
;
381 local_irq_restore(flags
);
386 * netdev_alloc_frag - allocate a page fragment
387 * @fragsz: fragment size
389 * Allocates a frag from a page for receive buffer.
390 * Uses GFP_ATOMIC allocations.
392 void *netdev_alloc_frag(unsigned int fragsz
)
394 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
396 EXPORT_SYMBOL(netdev_alloc_frag
);
399 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
400 * @dev: network device to receive on
401 * @length: length to allocate
402 * @gfp_mask: get_free_pages mask, passed to alloc_skb
404 * Allocate a new &sk_buff and assign it a usage count of one. The
405 * buffer has unspecified headroom built in. Users should allocate
406 * the headroom they think they need without accounting for the
407 * built in space. The built in space is used for optimisations.
409 * %NULL is returned if there is no free memory.
411 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
412 unsigned int length
, gfp_t gfp_mask
)
414 struct sk_buff
*skb
= NULL
;
415 unsigned int fragsz
= SKB_DATA_ALIGN(length
+ NET_SKB_PAD
) +
416 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
418 if (fragsz
<= PAGE_SIZE
&& !(gfp_mask
& (__GFP_WAIT
| GFP_DMA
))) {
421 if (sk_memalloc_socks())
422 gfp_mask
|= __GFP_MEMALLOC
;
424 data
= __netdev_alloc_frag(fragsz
, gfp_mask
);
427 skb
= build_skb(data
, fragsz
);
429 put_page(virt_to_head_page(data
));
432 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
,
433 SKB_ALLOC_RX
, NUMA_NO_NODE
);
436 skb_reserve(skb
, NET_SKB_PAD
);
441 EXPORT_SYMBOL(__netdev_alloc_skb
);
443 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
444 int size
, unsigned int truesize
)
446 skb_fill_page_desc(skb
, i
, page
, off
, size
);
448 skb
->data_len
+= size
;
449 skb
->truesize
+= truesize
;
451 EXPORT_SYMBOL(skb_add_rx_frag
);
453 void skb_coalesce_rx_frag(struct sk_buff
*skb
, int i
, int size
,
454 unsigned int truesize
)
456 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
458 skb_frag_size_add(frag
, size
);
460 skb
->data_len
+= size
;
461 skb
->truesize
+= truesize
;
463 EXPORT_SYMBOL(skb_coalesce_rx_frag
);
465 static void skb_drop_list(struct sk_buff
**listp
)
467 kfree_skb_list(*listp
);
471 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
473 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
476 static void skb_clone_fraglist(struct sk_buff
*skb
)
478 struct sk_buff
*list
;
480 skb_walk_frags(skb
, list
)
484 static void skb_free_head(struct sk_buff
*skb
)
487 put_page(virt_to_head_page(skb
->head
));
492 static void skb_release_data(struct sk_buff
*skb
)
495 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
496 &skb_shinfo(skb
)->dataref
)) {
497 if (skb_shinfo(skb
)->nr_frags
) {
499 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
500 skb_frag_unref(skb
, i
);
504 * If skb buf is from userspace, we need to notify the caller
505 * the lower device DMA has done;
507 if (skb_shinfo(skb
)->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
508 struct ubuf_info
*uarg
;
510 uarg
= skb_shinfo(skb
)->destructor_arg
;
512 uarg
->callback(uarg
, true);
515 if (skb_has_frag_list(skb
))
516 skb_drop_fraglist(skb
);
523 * Free an skbuff by memory without cleaning the state.
525 static void kfree_skbmem(struct sk_buff
*skb
)
527 struct sk_buff
*other
;
528 atomic_t
*fclone_ref
;
530 switch (skb
->fclone
) {
531 case SKB_FCLONE_UNAVAILABLE
:
532 kmem_cache_free(skbuff_head_cache
, skb
);
535 case SKB_FCLONE_ORIG
:
536 fclone_ref
= (atomic_t
*) (skb
+ 2);
537 if (atomic_dec_and_test(fclone_ref
))
538 kmem_cache_free(skbuff_fclone_cache
, skb
);
541 case SKB_FCLONE_CLONE
:
542 fclone_ref
= (atomic_t
*) (skb
+ 1);
545 /* The clone portion is available for
546 * fast-cloning again.
548 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
550 if (atomic_dec_and_test(fclone_ref
))
551 kmem_cache_free(skbuff_fclone_cache
, other
);
556 static void skb_release_head_state(struct sk_buff
*skb
)
560 secpath_put(skb
->sp
);
562 if (skb
->destructor
) {
564 skb
->destructor(skb
);
566 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
567 nf_conntrack_put(skb
->nfct
);
569 #ifdef CONFIG_BRIDGE_NETFILTER
570 nf_bridge_put(skb
->nf_bridge
);
572 /* XXX: IS this still necessary? - JHS */
573 #ifdef CONFIG_NET_SCHED
575 #ifdef CONFIG_NET_CLS_ACT
581 /* Free everything but the sk_buff shell. */
582 static void skb_release_all(struct sk_buff
*skb
)
584 skb_release_head_state(skb
);
585 if (likely(skb
->head
))
586 skb_release_data(skb
);
590 * __kfree_skb - private function
593 * Free an sk_buff. Release anything attached to the buffer.
594 * Clean the state. This is an internal helper function. Users should
595 * always call kfree_skb
598 void __kfree_skb(struct sk_buff
*skb
)
600 skb_release_all(skb
);
603 EXPORT_SYMBOL(__kfree_skb
);
606 * kfree_skb - free an sk_buff
607 * @skb: buffer to free
609 * Drop a reference to the buffer and free it if the usage count has
612 void kfree_skb(struct sk_buff
*skb
)
616 if (likely(atomic_read(&skb
->users
) == 1))
618 else if (likely(!atomic_dec_and_test(&skb
->users
)))
620 trace_kfree_skb(skb
, __builtin_return_address(0));
623 EXPORT_SYMBOL(kfree_skb
);
625 void kfree_skb_list(struct sk_buff
*segs
)
628 struct sk_buff
*next
= segs
->next
;
634 EXPORT_SYMBOL(kfree_skb_list
);
637 * skb_tx_error - report an sk_buff xmit error
638 * @skb: buffer that triggered an error
640 * Report xmit error if a device callback is tracking this skb.
641 * skb must be freed afterwards.
643 void skb_tx_error(struct sk_buff
*skb
)
645 if (skb_shinfo(skb
)->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
646 struct ubuf_info
*uarg
;
648 uarg
= skb_shinfo(skb
)->destructor_arg
;
650 uarg
->callback(uarg
, false);
651 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
654 EXPORT_SYMBOL(skb_tx_error
);
657 * consume_skb - free an skbuff
658 * @skb: buffer to free
660 * Drop a ref to the buffer and free it if the usage count has hit zero
661 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
662 * is being dropped after a failure and notes that
664 void consume_skb(struct sk_buff
*skb
)
668 if (likely(atomic_read(&skb
->users
) == 1))
670 else if (likely(!atomic_dec_and_test(&skb
->users
)))
672 trace_consume_skb(skb
);
675 EXPORT_SYMBOL(consume_skb
);
677 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
679 new->tstamp
= old
->tstamp
;
681 new->transport_header
= old
->transport_header
;
682 new->network_header
= old
->network_header
;
683 new->mac_header
= old
->mac_header
;
684 new->inner_protocol
= old
->inner_protocol
;
685 new->inner_transport_header
= old
->inner_transport_header
;
686 new->inner_network_header
= old
->inner_network_header
;
687 new->inner_mac_header
= old
->inner_mac_header
;
688 skb_dst_copy(new, old
);
689 skb_copy_hash(new, old
);
690 new->ooo_okay
= old
->ooo_okay
;
691 new->no_fcs
= old
->no_fcs
;
692 new->encapsulation
= old
->encapsulation
;
693 new->encap_hdr_csum
= old
->encap_hdr_csum
;
694 new->csum_valid
= old
->csum_valid
;
695 new->csum_complete_sw
= old
->csum_complete_sw
;
697 new->sp
= secpath_get(old
->sp
);
699 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
700 new->csum
= old
->csum
;
701 new->ignore_df
= old
->ignore_df
;
702 new->pkt_type
= old
->pkt_type
;
703 new->ip_summed
= old
->ip_summed
;
704 skb_copy_queue_mapping(new, old
);
705 new->priority
= old
->priority
;
706 #if IS_ENABLED(CONFIG_IP_VS)
707 new->ipvs_property
= old
->ipvs_property
;
709 new->pfmemalloc
= old
->pfmemalloc
;
710 new->protocol
= old
->protocol
;
711 new->mark
= old
->mark
;
712 new->skb_iif
= old
->skb_iif
;
714 #ifdef CONFIG_NET_SCHED
715 new->tc_index
= old
->tc_index
;
716 #ifdef CONFIG_NET_CLS_ACT
717 new->tc_verd
= old
->tc_verd
;
720 new->vlan_proto
= old
->vlan_proto
;
721 new->vlan_tci
= old
->vlan_tci
;
723 skb_copy_secmark(new, old
);
725 #ifdef CONFIG_NET_RX_BUSY_POLL
726 new->napi_id
= old
->napi_id
;
731 * You should not add any new code to this function. Add it to
732 * __copy_skb_header above instead.
734 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
736 #define C(x) n->x = skb->x
738 n
->next
= n
->prev
= NULL
;
740 __copy_skb_header(n
, skb
);
745 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
748 n
->destructor
= NULL
;
755 atomic_set(&n
->users
, 1);
757 atomic_inc(&(skb_shinfo(skb
)->dataref
));
765 * skb_morph - morph one skb into another
766 * @dst: the skb to receive the contents
767 * @src: the skb to supply the contents
769 * This is identical to skb_clone except that the target skb is
770 * supplied by the user.
772 * The target skb is returned upon exit.
774 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
776 skb_release_all(dst
);
777 return __skb_clone(dst
, src
);
779 EXPORT_SYMBOL_GPL(skb_morph
);
782 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
783 * @skb: the skb to modify
784 * @gfp_mask: allocation priority
786 * This must be called on SKBTX_DEV_ZEROCOPY skb.
787 * It will copy all frags into kernel and drop the reference
788 * to userspace pages.
790 * If this function is called from an interrupt gfp_mask() must be
793 * Returns 0 on success or a negative error code on failure
794 * to allocate kernel memory to copy to.
796 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
799 int num_frags
= skb_shinfo(skb
)->nr_frags
;
800 struct page
*page
, *head
= NULL
;
801 struct ubuf_info
*uarg
= skb_shinfo(skb
)->destructor_arg
;
803 for (i
= 0; i
< num_frags
; i
++) {
805 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
807 page
= alloc_page(gfp_mask
);
810 struct page
*next
= (struct page
*)page_private(head
);
816 vaddr
= kmap_atomic(skb_frag_page(f
));
817 memcpy(page_address(page
),
818 vaddr
+ f
->page_offset
, skb_frag_size(f
));
819 kunmap_atomic(vaddr
);
820 set_page_private(page
, (unsigned long)head
);
824 /* skb frags release userspace buffers */
825 for (i
= 0; i
< num_frags
; i
++)
826 skb_frag_unref(skb
, i
);
828 uarg
->callback(uarg
, false);
830 /* skb frags point to kernel buffers */
831 for (i
= num_frags
- 1; i
>= 0; i
--) {
832 __skb_fill_page_desc(skb
, i
, head
, 0,
833 skb_shinfo(skb
)->frags
[i
].size
);
834 head
= (struct page
*)page_private(head
);
837 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
840 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
843 * skb_clone - duplicate an sk_buff
844 * @skb: buffer to clone
845 * @gfp_mask: allocation priority
847 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
848 * copies share the same packet data but not structure. The new
849 * buffer has a reference count of 1. If the allocation fails the
850 * function returns %NULL otherwise the new buffer is returned.
852 * If this function is called from an interrupt gfp_mask() must be
856 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
860 if (skb_orphan_frags(skb
, gfp_mask
))
864 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
865 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
866 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
867 n
->fclone
= SKB_FCLONE_CLONE
;
868 atomic_inc(fclone_ref
);
870 if (skb_pfmemalloc(skb
))
871 gfp_mask
|= __GFP_MEMALLOC
;
873 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
877 kmemcheck_annotate_bitfield(n
, flags1
);
878 kmemcheck_annotate_bitfield(n
, flags2
);
879 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
882 return __skb_clone(n
, skb
);
884 EXPORT_SYMBOL(skb_clone
);
886 static void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
888 /* Only adjust this if it actually is csum_start rather than csum */
889 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
890 skb
->csum_start
+= off
;
891 /* {transport,network,mac}_header and tail are relative to skb->head */
892 skb
->transport_header
+= off
;
893 skb
->network_header
+= off
;
894 if (skb_mac_header_was_set(skb
))
895 skb
->mac_header
+= off
;
896 skb
->inner_transport_header
+= off
;
897 skb
->inner_network_header
+= off
;
898 skb
->inner_mac_header
+= off
;
901 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
903 __copy_skb_header(new, old
);
905 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
906 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
907 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
910 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
912 if (skb_pfmemalloc(skb
))
918 * skb_copy - create private copy of an sk_buff
919 * @skb: buffer to copy
920 * @gfp_mask: allocation priority
922 * Make a copy of both an &sk_buff and its data. This is used when the
923 * caller wishes to modify the data and needs a private copy of the
924 * data to alter. Returns %NULL on failure or the pointer to the buffer
925 * on success. The returned buffer has a reference count of 1.
927 * As by-product this function converts non-linear &sk_buff to linear
928 * one, so that &sk_buff becomes completely private and caller is allowed
929 * to modify all the data of returned buffer. This means that this
930 * function is not recommended for use in circumstances when only
931 * header is going to be modified. Use pskb_copy() instead.
934 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
936 int headerlen
= skb_headroom(skb
);
937 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
938 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
939 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
944 /* Set the data pointer */
945 skb_reserve(n
, headerlen
);
946 /* Set the tail pointer and length */
947 skb_put(n
, skb
->len
);
949 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
952 copy_skb_header(n
, skb
);
955 EXPORT_SYMBOL(skb_copy
);
958 * __pskb_copy_fclone - create copy of an sk_buff with private head.
959 * @skb: buffer to copy
960 * @headroom: headroom of new skb
961 * @gfp_mask: allocation priority
962 * @fclone: if true allocate the copy of the skb from the fclone
963 * cache instead of the head cache; it is recommended to set this
964 * to true for the cases where the copy will likely be cloned
966 * Make a copy of both an &sk_buff and part of its data, located
967 * in header. Fragmented data remain shared. This is used when
968 * the caller wishes to modify only header of &sk_buff and needs
969 * private copy of the header to alter. Returns %NULL on failure
970 * or the pointer to the buffer on success.
971 * The returned buffer has a reference count of 1.
974 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
975 gfp_t gfp_mask
, bool fclone
)
977 unsigned int size
= skb_headlen(skb
) + headroom
;
978 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
979 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
984 /* Set the data pointer */
985 skb_reserve(n
, headroom
);
986 /* Set the tail pointer and length */
987 skb_put(n
, skb_headlen(skb
));
989 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
991 n
->truesize
+= skb
->data_len
;
992 n
->data_len
= skb
->data_len
;
995 if (skb_shinfo(skb
)->nr_frags
) {
998 if (skb_orphan_frags(skb
, gfp_mask
)) {
1003 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1004 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1005 skb_frag_ref(skb
, i
);
1007 skb_shinfo(n
)->nr_frags
= i
;
1010 if (skb_has_frag_list(skb
)) {
1011 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1012 skb_clone_fraglist(n
);
1015 copy_skb_header(n
, skb
);
1019 EXPORT_SYMBOL(__pskb_copy_fclone
);
1022 * pskb_expand_head - reallocate header of &sk_buff
1023 * @skb: buffer to reallocate
1024 * @nhead: room to add at head
1025 * @ntail: room to add at tail
1026 * @gfp_mask: allocation priority
1028 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1029 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1030 * reference count of 1. Returns zero in the case of success or error,
1031 * if expansion failed. In the last case, &sk_buff is not changed.
1033 * All the pointers pointing into skb header may change and must be
1034 * reloaded after call to this function.
1037 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1042 int size
= nhead
+ skb_end_offset(skb
) + ntail
;
1047 if (skb_shared(skb
))
1050 size
= SKB_DATA_ALIGN(size
);
1052 if (skb_pfmemalloc(skb
))
1053 gfp_mask
|= __GFP_MEMALLOC
;
1054 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1055 gfp_mask
, NUMA_NO_NODE
, NULL
);
1058 size
= SKB_WITH_OVERHEAD(ksize(data
));
1060 /* Copy only real data... and, alas, header. This should be
1061 * optimized for the cases when header is void.
1063 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1065 memcpy((struct skb_shared_info
*)(data
+ size
),
1067 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1070 * if shinfo is shared we must drop the old head gracefully, but if it
1071 * is not we can just drop the old head and let the existing refcount
1072 * be since all we did is relocate the values
1074 if (skb_cloned(skb
)) {
1075 /* copy this zero copy skb frags */
1076 if (skb_orphan_frags(skb
, gfp_mask
))
1078 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1079 skb_frag_ref(skb
, i
);
1081 if (skb_has_frag_list(skb
))
1082 skb_clone_fraglist(skb
);
1084 skb_release_data(skb
);
1088 off
= (data
+ nhead
) - skb
->head
;
1093 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1097 skb
->end
= skb
->head
+ size
;
1100 skb_headers_offset_update(skb
, nhead
);
1104 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1112 EXPORT_SYMBOL(pskb_expand_head
);
1114 /* Make private copy of skb with writable head and some headroom */
1116 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1118 struct sk_buff
*skb2
;
1119 int delta
= headroom
- skb_headroom(skb
);
1122 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1124 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1125 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1133 EXPORT_SYMBOL(skb_realloc_headroom
);
1136 * skb_copy_expand - copy and expand sk_buff
1137 * @skb: buffer to copy
1138 * @newheadroom: new free bytes at head
1139 * @newtailroom: new free bytes at tail
1140 * @gfp_mask: allocation priority
1142 * Make a copy of both an &sk_buff and its data and while doing so
1143 * allocate additional space.
1145 * This is used when the caller wishes to modify the data and needs a
1146 * private copy of the data to alter as well as more space for new fields.
1147 * Returns %NULL on failure or the pointer to the buffer
1148 * on success. The returned buffer has a reference count of 1.
1150 * You must pass %GFP_ATOMIC as the allocation priority if this function
1151 * is called from an interrupt.
1153 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1154 int newheadroom
, int newtailroom
,
1158 * Allocate the copy buffer
1160 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1161 gfp_mask
, skb_alloc_rx_flag(skb
),
1163 int oldheadroom
= skb_headroom(skb
);
1164 int head_copy_len
, head_copy_off
;
1169 skb_reserve(n
, newheadroom
);
1171 /* Set the tail pointer and length */
1172 skb_put(n
, skb
->len
);
1174 head_copy_len
= oldheadroom
;
1176 if (newheadroom
<= head_copy_len
)
1177 head_copy_len
= newheadroom
;
1179 head_copy_off
= newheadroom
- head_copy_len
;
1181 /* Copy the linear header and data. */
1182 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1183 skb
->len
+ head_copy_len
))
1186 copy_skb_header(n
, skb
);
1188 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1192 EXPORT_SYMBOL(skb_copy_expand
);
1195 * skb_pad - zero pad the tail of an skb
1196 * @skb: buffer to pad
1197 * @pad: space to pad
1199 * Ensure that a buffer is followed by a padding area that is zero
1200 * filled. Used by network drivers which may DMA or transfer data
1201 * beyond the buffer end onto the wire.
1203 * May return error in out of memory cases. The skb is freed on error.
1206 int skb_pad(struct sk_buff
*skb
, int pad
)
1211 /* If the skbuff is non linear tailroom is always zero.. */
1212 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1213 memset(skb
->data
+skb
->len
, 0, pad
);
1217 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1218 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1219 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1224 /* FIXME: The use of this function with non-linear skb's really needs
1227 err
= skb_linearize(skb
);
1231 memset(skb
->data
+ skb
->len
, 0, pad
);
1238 EXPORT_SYMBOL(skb_pad
);
1241 * pskb_put - add data to the tail of a potentially fragmented buffer
1242 * @skb: start of the buffer to use
1243 * @tail: tail fragment of the buffer to use
1244 * @len: amount of data to add
1246 * This function extends the used data area of the potentially
1247 * fragmented buffer. @tail must be the last fragment of @skb -- or
1248 * @skb itself. If this would exceed the total buffer size the kernel
1249 * will panic. A pointer to the first byte of the extra data is
1253 unsigned char *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1256 skb
->data_len
+= len
;
1259 return skb_put(tail
, len
);
1261 EXPORT_SYMBOL_GPL(pskb_put
);
1264 * skb_put - add data to a buffer
1265 * @skb: buffer to use
1266 * @len: amount of data to add
1268 * This function extends the used data area of the buffer. If this would
1269 * exceed the total buffer size the kernel will panic. A pointer to the
1270 * first byte of the extra data is returned.
1272 unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
1274 unsigned char *tmp
= skb_tail_pointer(skb
);
1275 SKB_LINEAR_ASSERT(skb
);
1278 if (unlikely(skb
->tail
> skb
->end
))
1279 skb_over_panic(skb
, len
, __builtin_return_address(0));
1282 EXPORT_SYMBOL(skb_put
);
1285 * skb_push - add data to the start of a buffer
1286 * @skb: buffer to use
1287 * @len: amount of data to add
1289 * This function extends the used data area of the buffer at the buffer
1290 * start. If this would exceed the total buffer headroom the kernel will
1291 * panic. A pointer to the first byte of the extra data is returned.
1293 unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
1297 if (unlikely(skb
->data
<skb
->head
))
1298 skb_under_panic(skb
, len
, __builtin_return_address(0));
1301 EXPORT_SYMBOL(skb_push
);
1304 * skb_pull - remove data from the start of a buffer
1305 * @skb: buffer to use
1306 * @len: amount of data to remove
1308 * This function removes data from the start of a buffer, returning
1309 * the memory to the headroom. A pointer to the next data in the buffer
1310 * is returned. Once the data has been pulled future pushes will overwrite
1313 unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1315 return skb_pull_inline(skb
, len
);
1317 EXPORT_SYMBOL(skb_pull
);
1320 * skb_trim - remove end from a buffer
1321 * @skb: buffer to alter
1324 * Cut the length of a buffer down by removing data from the tail. If
1325 * the buffer is already under the length specified it is not modified.
1326 * The skb must be linear.
1328 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1331 __skb_trim(skb
, len
);
1333 EXPORT_SYMBOL(skb_trim
);
1335 /* Trims skb to length len. It can change skb pointers.
1338 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1340 struct sk_buff
**fragp
;
1341 struct sk_buff
*frag
;
1342 int offset
= skb_headlen(skb
);
1343 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1347 if (skb_cloned(skb
) &&
1348 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1355 for (; i
< nfrags
; i
++) {
1356 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1363 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1366 skb_shinfo(skb
)->nr_frags
= i
;
1368 for (; i
< nfrags
; i
++)
1369 skb_frag_unref(skb
, i
);
1371 if (skb_has_frag_list(skb
))
1372 skb_drop_fraglist(skb
);
1376 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1377 fragp
= &frag
->next
) {
1378 int end
= offset
+ frag
->len
;
1380 if (skb_shared(frag
)) {
1381 struct sk_buff
*nfrag
;
1383 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1384 if (unlikely(!nfrag
))
1387 nfrag
->next
= frag
->next
;
1399 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1403 skb_drop_list(&frag
->next
);
1408 if (len
> skb_headlen(skb
)) {
1409 skb
->data_len
-= skb
->len
- len
;
1414 skb_set_tail_pointer(skb
, len
);
1419 EXPORT_SYMBOL(___pskb_trim
);
1422 * __pskb_pull_tail - advance tail of skb header
1423 * @skb: buffer to reallocate
1424 * @delta: number of bytes to advance tail
1426 * The function makes a sense only on a fragmented &sk_buff,
1427 * it expands header moving its tail forward and copying necessary
1428 * data from fragmented part.
1430 * &sk_buff MUST have reference count of 1.
1432 * Returns %NULL (and &sk_buff does not change) if pull failed
1433 * or value of new tail of skb in the case of success.
1435 * All the pointers pointing into skb header may change and must be
1436 * reloaded after call to this function.
1439 /* Moves tail of skb head forward, copying data from fragmented part,
1440 * when it is necessary.
1441 * 1. It may fail due to malloc failure.
1442 * 2. It may change skb pointers.
1444 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1446 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1448 /* If skb has not enough free space at tail, get new one
1449 * plus 128 bytes for future expansions. If we have enough
1450 * room at tail, reallocate without expansion only if skb is cloned.
1452 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1454 if (eat
> 0 || skb_cloned(skb
)) {
1455 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1460 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1463 /* Optimization: no fragments, no reasons to preestimate
1464 * size of pulled pages. Superb.
1466 if (!skb_has_frag_list(skb
))
1469 /* Estimate size of pulled pages. */
1471 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1472 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1479 /* If we need update frag list, we are in troubles.
1480 * Certainly, it possible to add an offset to skb data,
1481 * but taking into account that pulling is expected to
1482 * be very rare operation, it is worth to fight against
1483 * further bloating skb head and crucify ourselves here instead.
1484 * Pure masohism, indeed. 8)8)
1487 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1488 struct sk_buff
*clone
= NULL
;
1489 struct sk_buff
*insp
= NULL
;
1494 if (list
->len
<= eat
) {
1495 /* Eaten as whole. */
1500 /* Eaten partially. */
1502 if (skb_shared(list
)) {
1503 /* Sucks! We need to fork list. :-( */
1504 clone
= skb_clone(list
, GFP_ATOMIC
);
1510 /* This may be pulled without
1514 if (!pskb_pull(list
, eat
)) {
1522 /* Free pulled out fragments. */
1523 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1524 skb_shinfo(skb
)->frag_list
= list
->next
;
1527 /* And insert new clone at head. */
1530 skb_shinfo(skb
)->frag_list
= clone
;
1533 /* Success! Now we may commit changes to skb data. */
1538 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1539 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1542 skb_frag_unref(skb
, i
);
1545 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1547 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1548 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
1554 skb_shinfo(skb
)->nr_frags
= k
;
1557 skb
->data_len
-= delta
;
1559 return skb_tail_pointer(skb
);
1561 EXPORT_SYMBOL(__pskb_pull_tail
);
1564 * skb_copy_bits - copy bits from skb to kernel buffer
1566 * @offset: offset in source
1567 * @to: destination buffer
1568 * @len: number of bytes to copy
1570 * Copy the specified number of bytes from the source skb to the
1571 * destination buffer.
1574 * If its prototype is ever changed,
1575 * check arch/{*}/net/{*}.S files,
1576 * since it is called from BPF assembly code.
1578 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1580 int start
= skb_headlen(skb
);
1581 struct sk_buff
*frag_iter
;
1584 if (offset
> (int)skb
->len
- len
)
1588 if ((copy
= start
- offset
) > 0) {
1591 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1592 if ((len
-= copy
) == 0)
1598 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1600 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1602 WARN_ON(start
> offset
+ len
);
1604 end
= start
+ skb_frag_size(f
);
1605 if ((copy
= end
- offset
) > 0) {
1611 vaddr
= kmap_atomic(skb_frag_page(f
));
1613 vaddr
+ f
->page_offset
+ offset
- start
,
1615 kunmap_atomic(vaddr
);
1617 if ((len
-= copy
) == 0)
1625 skb_walk_frags(skb
, frag_iter
) {
1628 WARN_ON(start
> offset
+ len
);
1630 end
= start
+ frag_iter
->len
;
1631 if ((copy
= end
- offset
) > 0) {
1634 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
1636 if ((len
-= copy
) == 0)
1650 EXPORT_SYMBOL(skb_copy_bits
);
1653 * Callback from splice_to_pipe(), if we need to release some pages
1654 * at the end of the spd in case we error'ed out in filling the pipe.
1656 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1658 put_page(spd
->pages
[i
]);
1661 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
1662 unsigned int *offset
,
1665 struct page_frag
*pfrag
= sk_page_frag(sk
);
1667 if (!sk_page_frag_refill(sk
, pfrag
))
1670 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
1672 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
1673 page_address(page
) + *offset
, *len
);
1674 *offset
= pfrag
->offset
;
1675 pfrag
->offset
+= *len
;
1680 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
1682 unsigned int offset
)
1684 return spd
->nr_pages
&&
1685 spd
->pages
[spd
->nr_pages
- 1] == page
&&
1686 (spd
->partial
[spd
->nr_pages
- 1].offset
+
1687 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
1691 * Fill page/offset/length into spd, if it can hold more pages.
1693 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
1694 struct pipe_inode_info
*pipe
, struct page
*page
,
1695 unsigned int *len
, unsigned int offset
,
1699 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
1703 page
= linear_to_page(page
, len
, &offset
, sk
);
1707 if (spd_can_coalesce(spd
, page
, offset
)) {
1708 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
1712 spd
->pages
[spd
->nr_pages
] = page
;
1713 spd
->partial
[spd
->nr_pages
].len
= *len
;
1714 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1720 static bool __splice_segment(struct page
*page
, unsigned int poff
,
1721 unsigned int plen
, unsigned int *off
,
1723 struct splice_pipe_desc
*spd
, bool linear
,
1725 struct pipe_inode_info
*pipe
)
1730 /* skip this segment if already processed */
1736 /* ignore any bits we already processed */
1742 unsigned int flen
= min(*len
, plen
);
1744 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
1750 } while (*len
&& plen
);
1756 * Map linear and fragment data from the skb to spd. It reports true if the
1757 * pipe is full or if we already spliced the requested length.
1759 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
1760 unsigned int *offset
, unsigned int *len
,
1761 struct splice_pipe_desc
*spd
, struct sock
*sk
)
1765 /* map the linear part :
1766 * If skb->head_frag is set, this 'linear' part is backed by a
1767 * fragment, and if the head is not shared with any clones then
1768 * we can avoid a copy since we own the head portion of this page.
1770 if (__splice_segment(virt_to_page(skb
->data
),
1771 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
1774 skb_head_is_locked(skb
),
1779 * then map the fragments
1781 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1782 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1784 if (__splice_segment(skb_frag_page(f
),
1785 f
->page_offset
, skb_frag_size(f
),
1786 offset
, len
, spd
, false, sk
, pipe
))
1794 * Map data from the skb to a pipe. Should handle both the linear part,
1795 * the fragments, and the frag list. It does NOT handle frag lists within
1796 * the frag list, if such a thing exists. We'd probably need to recurse to
1797 * handle that cleanly.
1799 int skb_splice_bits(struct sk_buff
*skb
, unsigned int offset
,
1800 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1803 struct partial_page partial
[MAX_SKB_FRAGS
];
1804 struct page
*pages
[MAX_SKB_FRAGS
];
1805 struct splice_pipe_desc spd
= {
1808 .nr_pages_max
= MAX_SKB_FRAGS
,
1810 .ops
= &nosteal_pipe_buf_ops
,
1811 .spd_release
= sock_spd_release
,
1813 struct sk_buff
*frag_iter
;
1814 struct sock
*sk
= skb
->sk
;
1818 * __skb_splice_bits() only fails if the output has no room left,
1819 * so no point in going over the frag_list for the error case.
1821 if (__skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
))
1827 * now see if we have a frag_list to map
1829 skb_walk_frags(skb
, frag_iter
) {
1832 if (__skb_splice_bits(frag_iter
, pipe
, &offset
, &tlen
, &spd
, sk
))
1839 * Drop the socket lock, otherwise we have reverse
1840 * locking dependencies between sk_lock and i_mutex
1841 * here as compared to sendfile(). We enter here
1842 * with the socket lock held, and splice_to_pipe() will
1843 * grab the pipe inode lock. For sendfile() emulation,
1844 * we call into ->sendpage() with the i_mutex lock held
1845 * and networking will grab the socket lock.
1848 ret
= splice_to_pipe(pipe
, &spd
);
1856 * skb_store_bits - store bits from kernel buffer to skb
1857 * @skb: destination buffer
1858 * @offset: offset in destination
1859 * @from: source buffer
1860 * @len: number of bytes to copy
1862 * Copy the specified number of bytes from the source buffer to the
1863 * destination skb. This function handles all the messy bits of
1864 * traversing fragment lists and such.
1867 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1869 int start
= skb_headlen(skb
);
1870 struct sk_buff
*frag_iter
;
1873 if (offset
> (int)skb
->len
- len
)
1876 if ((copy
= start
- offset
) > 0) {
1879 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1880 if ((len
-= copy
) == 0)
1886 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1887 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1890 WARN_ON(start
> offset
+ len
);
1892 end
= start
+ skb_frag_size(frag
);
1893 if ((copy
= end
- offset
) > 0) {
1899 vaddr
= kmap_atomic(skb_frag_page(frag
));
1900 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1902 kunmap_atomic(vaddr
);
1904 if ((len
-= copy
) == 0)
1912 skb_walk_frags(skb
, frag_iter
) {
1915 WARN_ON(start
> offset
+ len
);
1917 end
= start
+ frag_iter
->len
;
1918 if ((copy
= end
- offset
) > 0) {
1921 if (skb_store_bits(frag_iter
, offset
- start
,
1924 if ((len
-= copy
) == 0)
1937 EXPORT_SYMBOL(skb_store_bits
);
1939 /* Checksum skb data. */
1940 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
1941 __wsum csum
, const struct skb_checksum_ops
*ops
)
1943 int start
= skb_headlen(skb
);
1944 int i
, copy
= start
- offset
;
1945 struct sk_buff
*frag_iter
;
1948 /* Checksum header. */
1952 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
1953 if ((len
-= copy
) == 0)
1959 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1961 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1963 WARN_ON(start
> offset
+ len
);
1965 end
= start
+ skb_frag_size(frag
);
1966 if ((copy
= end
- offset
) > 0) {
1972 vaddr
= kmap_atomic(skb_frag_page(frag
));
1973 csum2
= ops
->update(vaddr
+ frag
->page_offset
+
1974 offset
- start
, copy
, 0);
1975 kunmap_atomic(vaddr
);
1976 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
1985 skb_walk_frags(skb
, frag_iter
) {
1988 WARN_ON(start
> offset
+ len
);
1990 end
= start
+ frag_iter
->len
;
1991 if ((copy
= end
- offset
) > 0) {
1995 csum2
= __skb_checksum(frag_iter
, offset
- start
,
1997 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
1998 if ((len
-= copy
) == 0)
2009 EXPORT_SYMBOL(__skb_checksum
);
2011 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2012 int len
, __wsum csum
)
2014 const struct skb_checksum_ops ops
= {
2015 .update
= csum_partial_ext
,
2016 .combine
= csum_block_add_ext
,
2019 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2021 EXPORT_SYMBOL(skb_checksum
);
2023 /* Both of above in one bottle. */
2025 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2026 u8
*to
, int len
, __wsum csum
)
2028 int start
= skb_headlen(skb
);
2029 int i
, copy
= start
- offset
;
2030 struct sk_buff
*frag_iter
;
2037 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2039 if ((len
-= copy
) == 0)
2046 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2049 WARN_ON(start
> offset
+ len
);
2051 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2052 if ((copy
= end
- offset
) > 0) {
2055 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2059 vaddr
= kmap_atomic(skb_frag_page(frag
));
2060 csum2
= csum_partial_copy_nocheck(vaddr
+
2064 kunmap_atomic(vaddr
);
2065 csum
= csum_block_add(csum
, csum2
, pos
);
2075 skb_walk_frags(skb
, frag_iter
) {
2079 WARN_ON(start
> offset
+ len
);
2081 end
= start
+ frag_iter
->len
;
2082 if ((copy
= end
- offset
) > 0) {
2085 csum2
= skb_copy_and_csum_bits(frag_iter
,
2088 csum
= csum_block_add(csum
, csum2
, pos
);
2089 if ((len
-= copy
) == 0)
2100 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2103 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2104 * @from: source buffer
2106 * Calculates the amount of linear headroom needed in the 'to' skb passed
2107 * into skb_zerocopy().
2110 skb_zerocopy_headlen(const struct sk_buff
*from
)
2112 unsigned int hlen
= 0;
2114 if (!from
->head_frag
||
2115 skb_headlen(from
) < L1_CACHE_BYTES
||
2116 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2117 hlen
= skb_headlen(from
);
2119 if (skb_has_frag_list(from
))
2124 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2127 * skb_zerocopy - Zero copy skb to skb
2128 * @to: destination buffer
2129 * @from: source buffer
2130 * @len: number of bytes to copy from source buffer
2131 * @hlen: size of linear headroom in destination buffer
2133 * Copies up to `len` bytes from `from` to `to` by creating references
2134 * to the frags in the source buffer.
2136 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2137 * headroom in the `to` buffer.
2140 * 0: everything is OK
2141 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2142 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2145 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2148 int plen
= 0; /* length of skb->head fragment */
2151 unsigned int offset
;
2153 BUG_ON(!from
->head_frag
&& !hlen
);
2155 /* dont bother with small payloads */
2156 if (len
<= skb_tailroom(to
))
2157 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2160 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2165 plen
= min_t(int, skb_headlen(from
), len
);
2167 page
= virt_to_head_page(from
->head
);
2168 offset
= from
->data
- (unsigned char *)page_address(page
);
2169 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2176 to
->truesize
+= len
+ plen
;
2177 to
->len
+= len
+ plen
;
2178 to
->data_len
+= len
+ plen
;
2180 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2185 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2188 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2189 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2190 len
-= skb_shinfo(to
)->frags
[j
].size
;
2191 skb_frag_ref(to
, j
);
2194 skb_shinfo(to
)->nr_frags
= j
;
2198 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2200 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2205 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2206 csstart
= skb_checksum_start_offset(skb
);
2208 csstart
= skb_headlen(skb
);
2210 BUG_ON(csstart
> skb_headlen(skb
));
2212 skb_copy_from_linear_data(skb
, to
, csstart
);
2215 if (csstart
!= skb
->len
)
2216 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2217 skb
->len
- csstart
, 0);
2219 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2220 long csstuff
= csstart
+ skb
->csum_offset
;
2222 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2225 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2228 * skb_dequeue - remove from the head of the queue
2229 * @list: list to dequeue from
2231 * Remove the head of the list. The list lock is taken so the function
2232 * may be used safely with other locking list functions. The head item is
2233 * returned or %NULL if the list is empty.
2236 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2238 unsigned long flags
;
2239 struct sk_buff
*result
;
2241 spin_lock_irqsave(&list
->lock
, flags
);
2242 result
= __skb_dequeue(list
);
2243 spin_unlock_irqrestore(&list
->lock
, flags
);
2246 EXPORT_SYMBOL(skb_dequeue
);
2249 * skb_dequeue_tail - remove from the tail of the queue
2250 * @list: list to dequeue from
2252 * Remove the tail of the list. The list lock is taken so the function
2253 * may be used safely with other locking list functions. The tail item is
2254 * returned or %NULL if the list is empty.
2256 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2258 unsigned long flags
;
2259 struct sk_buff
*result
;
2261 spin_lock_irqsave(&list
->lock
, flags
);
2262 result
= __skb_dequeue_tail(list
);
2263 spin_unlock_irqrestore(&list
->lock
, flags
);
2266 EXPORT_SYMBOL(skb_dequeue_tail
);
2269 * skb_queue_purge - empty a list
2270 * @list: list to empty
2272 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2273 * the list and one reference dropped. This function takes the list
2274 * lock and is atomic with respect to other list locking functions.
2276 void skb_queue_purge(struct sk_buff_head
*list
)
2278 struct sk_buff
*skb
;
2279 while ((skb
= skb_dequeue(list
)) != NULL
)
2282 EXPORT_SYMBOL(skb_queue_purge
);
2285 * skb_queue_head - queue a buffer at the list head
2286 * @list: list to use
2287 * @newsk: buffer to queue
2289 * Queue a buffer at the start of the list. This function takes the
2290 * list lock and can be used safely with other locking &sk_buff functions
2293 * A buffer cannot be placed on two lists at the same time.
2295 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2297 unsigned long flags
;
2299 spin_lock_irqsave(&list
->lock
, flags
);
2300 __skb_queue_head(list
, newsk
);
2301 spin_unlock_irqrestore(&list
->lock
, flags
);
2303 EXPORT_SYMBOL(skb_queue_head
);
2306 * skb_queue_tail - queue a buffer at the list tail
2307 * @list: list to use
2308 * @newsk: buffer to queue
2310 * Queue a buffer at the tail of the list. This function takes the
2311 * list lock and can be used safely with other locking &sk_buff functions
2314 * A buffer cannot be placed on two lists at the same time.
2316 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2318 unsigned long flags
;
2320 spin_lock_irqsave(&list
->lock
, flags
);
2321 __skb_queue_tail(list
, newsk
);
2322 spin_unlock_irqrestore(&list
->lock
, flags
);
2324 EXPORT_SYMBOL(skb_queue_tail
);
2327 * skb_unlink - remove a buffer from a list
2328 * @skb: buffer to remove
2329 * @list: list to use
2331 * Remove a packet from a list. The list locks are taken and this
2332 * function is atomic with respect to other list locked calls
2334 * You must know what list the SKB is on.
2336 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
2338 unsigned long flags
;
2340 spin_lock_irqsave(&list
->lock
, flags
);
2341 __skb_unlink(skb
, list
);
2342 spin_unlock_irqrestore(&list
->lock
, flags
);
2344 EXPORT_SYMBOL(skb_unlink
);
2347 * skb_append - append a buffer
2348 * @old: buffer to insert after
2349 * @newsk: buffer to insert
2350 * @list: list to use
2352 * Place a packet after a given packet in a list. The list locks are taken
2353 * and this function is atomic with respect to other list locked calls.
2354 * A buffer cannot be placed on two lists at the same time.
2356 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2358 unsigned long flags
;
2360 spin_lock_irqsave(&list
->lock
, flags
);
2361 __skb_queue_after(list
, old
, newsk
);
2362 spin_unlock_irqrestore(&list
->lock
, flags
);
2364 EXPORT_SYMBOL(skb_append
);
2367 * skb_insert - insert a buffer
2368 * @old: buffer to insert before
2369 * @newsk: buffer to insert
2370 * @list: list to use
2372 * Place a packet before a given packet in a list. The list locks are
2373 * taken and this function is atomic with respect to other list locked
2376 * A buffer cannot be placed on two lists at the same time.
2378 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2380 unsigned long flags
;
2382 spin_lock_irqsave(&list
->lock
, flags
);
2383 __skb_insert(newsk
, old
->prev
, old
, list
);
2384 spin_unlock_irqrestore(&list
->lock
, flags
);
2386 EXPORT_SYMBOL(skb_insert
);
2388 static inline void skb_split_inside_header(struct sk_buff
*skb
,
2389 struct sk_buff
* skb1
,
2390 const u32 len
, const int pos
)
2394 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
2396 /* And move data appendix as is. */
2397 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
2398 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
2400 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
2401 skb_shinfo(skb
)->nr_frags
= 0;
2402 skb1
->data_len
= skb
->data_len
;
2403 skb1
->len
+= skb1
->data_len
;
2406 skb_set_tail_pointer(skb
, len
);
2409 static inline void skb_split_no_header(struct sk_buff
*skb
,
2410 struct sk_buff
* skb1
,
2411 const u32 len
, int pos
)
2414 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
2416 skb_shinfo(skb
)->nr_frags
= 0;
2417 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
2419 skb
->data_len
= len
- pos
;
2421 for (i
= 0; i
< nfrags
; i
++) {
2422 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2424 if (pos
+ size
> len
) {
2425 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2429 * We have two variants in this case:
2430 * 1. Move all the frag to the second
2431 * part, if it is possible. F.e.
2432 * this approach is mandatory for TUX,
2433 * where splitting is expensive.
2434 * 2. Split is accurately. We make this.
2436 skb_frag_ref(skb
, i
);
2437 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
2438 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
2439 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
2440 skb_shinfo(skb
)->nr_frags
++;
2444 skb_shinfo(skb
)->nr_frags
++;
2447 skb_shinfo(skb1
)->nr_frags
= k
;
2451 * skb_split - Split fragmented skb to two parts at length len.
2452 * @skb: the buffer to split
2453 * @skb1: the buffer to receive the second part
2454 * @len: new length for skb
2456 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
2458 int pos
= skb_headlen(skb
);
2460 skb_shinfo(skb1
)->tx_flags
= skb_shinfo(skb
)->tx_flags
& SKBTX_SHARED_FRAG
;
2461 if (len
< pos
) /* Split line is inside header. */
2462 skb_split_inside_header(skb
, skb1
, len
, pos
);
2463 else /* Second chunk has no header, nothing to copy. */
2464 skb_split_no_header(skb
, skb1
, len
, pos
);
2466 EXPORT_SYMBOL(skb_split
);
2468 /* Shifting from/to a cloned skb is a no-go.
2470 * Caller cannot keep skb_shinfo related pointers past calling here!
2472 static int skb_prepare_for_shift(struct sk_buff
*skb
)
2474 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
2478 * skb_shift - Shifts paged data partially from skb to another
2479 * @tgt: buffer into which tail data gets added
2480 * @skb: buffer from which the paged data comes from
2481 * @shiftlen: shift up to this many bytes
2483 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2484 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2485 * It's up to caller to free skb if everything was shifted.
2487 * If @tgt runs out of frags, the whole operation is aborted.
2489 * Skb cannot include anything else but paged data while tgt is allowed
2490 * to have non-paged data as well.
2492 * TODO: full sized shift could be optimized but that would need
2493 * specialized skb free'er to handle frags without up-to-date nr_frags.
2495 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
2497 int from
, to
, merge
, todo
;
2498 struct skb_frag_struct
*fragfrom
, *fragto
;
2500 BUG_ON(shiftlen
> skb
->len
);
2501 BUG_ON(skb_headlen(skb
)); /* Would corrupt stream */
2505 to
= skb_shinfo(tgt
)->nr_frags
;
2506 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2508 /* Actual merge is delayed until the point when we know we can
2509 * commit all, so that we don't have to undo partial changes
2512 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
2513 fragfrom
->page_offset
)) {
2518 todo
-= skb_frag_size(fragfrom
);
2520 if (skb_prepare_for_shift(skb
) ||
2521 skb_prepare_for_shift(tgt
))
2524 /* All previous frag pointers might be stale! */
2525 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2526 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2528 skb_frag_size_add(fragto
, shiftlen
);
2529 skb_frag_size_sub(fragfrom
, shiftlen
);
2530 fragfrom
->page_offset
+= shiftlen
;
2538 /* Skip full, not-fitting skb to avoid expensive operations */
2539 if ((shiftlen
== skb
->len
) &&
2540 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
2543 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
2546 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
2547 if (to
== MAX_SKB_FRAGS
)
2550 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2551 fragto
= &skb_shinfo(tgt
)->frags
[to
];
2553 if (todo
>= skb_frag_size(fragfrom
)) {
2554 *fragto
= *fragfrom
;
2555 todo
-= skb_frag_size(fragfrom
);
2560 __skb_frag_ref(fragfrom
);
2561 fragto
->page
= fragfrom
->page
;
2562 fragto
->page_offset
= fragfrom
->page_offset
;
2563 skb_frag_size_set(fragto
, todo
);
2565 fragfrom
->page_offset
+= todo
;
2566 skb_frag_size_sub(fragfrom
, todo
);
2574 /* Ready to "commit" this state change to tgt */
2575 skb_shinfo(tgt
)->nr_frags
= to
;
2578 fragfrom
= &skb_shinfo(skb
)->frags
[0];
2579 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2581 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
2582 __skb_frag_unref(fragfrom
);
2585 /* Reposition in the original skb */
2587 while (from
< skb_shinfo(skb
)->nr_frags
)
2588 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
2589 skb_shinfo(skb
)->nr_frags
= to
;
2591 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
2594 /* Most likely the tgt won't ever need its checksum anymore, skb on
2595 * the other hand might need it if it needs to be resent
2597 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
2598 skb
->ip_summed
= CHECKSUM_PARTIAL
;
2600 /* Yak, is it really working this way? Some helper please? */
2601 skb
->len
-= shiftlen
;
2602 skb
->data_len
-= shiftlen
;
2603 skb
->truesize
-= shiftlen
;
2604 tgt
->len
+= shiftlen
;
2605 tgt
->data_len
+= shiftlen
;
2606 tgt
->truesize
+= shiftlen
;
2612 * skb_prepare_seq_read - Prepare a sequential read of skb data
2613 * @skb: the buffer to read
2614 * @from: lower offset of data to be read
2615 * @to: upper offset of data to be read
2616 * @st: state variable
2618 * Initializes the specified state variable. Must be called before
2619 * invoking skb_seq_read() for the first time.
2621 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
2622 unsigned int to
, struct skb_seq_state
*st
)
2624 st
->lower_offset
= from
;
2625 st
->upper_offset
= to
;
2626 st
->root_skb
= st
->cur_skb
= skb
;
2627 st
->frag_idx
= st
->stepped_offset
= 0;
2628 st
->frag_data
= NULL
;
2630 EXPORT_SYMBOL(skb_prepare_seq_read
);
2633 * skb_seq_read - Sequentially read skb data
2634 * @consumed: number of bytes consumed by the caller so far
2635 * @data: destination pointer for data to be returned
2636 * @st: state variable
2638 * Reads a block of skb data at @consumed relative to the
2639 * lower offset specified to skb_prepare_seq_read(). Assigns
2640 * the head of the data block to @data and returns the length
2641 * of the block or 0 if the end of the skb data or the upper
2642 * offset has been reached.
2644 * The caller is not required to consume all of the data
2645 * returned, i.e. @consumed is typically set to the number
2646 * of bytes already consumed and the next call to
2647 * skb_seq_read() will return the remaining part of the block.
2649 * Note 1: The size of each block of data returned can be arbitrary,
2650 * this limitation is the cost for zerocopy sequential
2651 * reads of potentially non linear data.
2653 * Note 2: Fragment lists within fragments are not implemented
2654 * at the moment, state->root_skb could be replaced with
2655 * a stack for this purpose.
2657 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
2658 struct skb_seq_state
*st
)
2660 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
2663 if (unlikely(abs_offset
>= st
->upper_offset
)) {
2664 if (st
->frag_data
) {
2665 kunmap_atomic(st
->frag_data
);
2666 st
->frag_data
= NULL
;
2672 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
2674 if (abs_offset
< block_limit
&& !st
->frag_data
) {
2675 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
2676 return block_limit
- abs_offset
;
2679 if (st
->frag_idx
== 0 && !st
->frag_data
)
2680 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
2682 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
2683 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
2684 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
2686 if (abs_offset
< block_limit
) {
2688 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
2690 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
2691 (abs_offset
- st
->stepped_offset
);
2693 return block_limit
- abs_offset
;
2696 if (st
->frag_data
) {
2697 kunmap_atomic(st
->frag_data
);
2698 st
->frag_data
= NULL
;
2702 st
->stepped_offset
+= skb_frag_size(frag
);
2705 if (st
->frag_data
) {
2706 kunmap_atomic(st
->frag_data
);
2707 st
->frag_data
= NULL
;
2710 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
2711 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2714 } else if (st
->cur_skb
->next
) {
2715 st
->cur_skb
= st
->cur_skb
->next
;
2722 EXPORT_SYMBOL(skb_seq_read
);
2725 * skb_abort_seq_read - Abort a sequential read of skb data
2726 * @st: state variable
2728 * Must be called if skb_seq_read() was not called until it
2731 void skb_abort_seq_read(struct skb_seq_state
*st
)
2734 kunmap_atomic(st
->frag_data
);
2736 EXPORT_SYMBOL(skb_abort_seq_read
);
2738 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2740 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2741 struct ts_config
*conf
,
2742 struct ts_state
*state
)
2744 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2747 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2749 skb_abort_seq_read(TS_SKB_CB(state
));
2753 * skb_find_text - Find a text pattern in skb data
2754 * @skb: the buffer to look in
2755 * @from: search offset
2757 * @config: textsearch configuration
2758 * @state: uninitialized textsearch state variable
2760 * Finds a pattern in the skb data according to the specified
2761 * textsearch configuration. Use textsearch_next() to retrieve
2762 * subsequent occurrences of the pattern. Returns the offset
2763 * to the first occurrence or UINT_MAX if no match was found.
2765 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2766 unsigned int to
, struct ts_config
*config
,
2767 struct ts_state
*state
)
2771 config
->get_next_block
= skb_ts_get_next_block
;
2772 config
->finish
= skb_ts_finish
;
2774 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2776 ret
= textsearch_find(config
, state
);
2777 return (ret
<= to
- from
? ret
: UINT_MAX
);
2779 EXPORT_SYMBOL(skb_find_text
);
2782 * skb_append_datato_frags - append the user data to a skb
2783 * @sk: sock structure
2784 * @skb: skb structure to be appended with user data.
2785 * @getfrag: call back function to be used for getting the user data
2786 * @from: pointer to user message iov
2787 * @length: length of the iov message
2789 * Description: This procedure append the user data in the fragment part
2790 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2792 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2793 int (*getfrag
)(void *from
, char *to
, int offset
,
2794 int len
, int odd
, struct sk_buff
*skb
),
2795 void *from
, int length
)
2797 int frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2801 struct page_frag
*pfrag
= ¤t
->task_frag
;
2804 /* Return error if we don't have space for new frag */
2805 if (frg_cnt
>= MAX_SKB_FRAGS
)
2808 if (!sk_page_frag_refill(sk
, pfrag
))
2811 /* copy the user data to page */
2812 copy
= min_t(int, length
, pfrag
->size
- pfrag
->offset
);
2814 ret
= getfrag(from
, page_address(pfrag
->page
) + pfrag
->offset
,
2815 offset
, copy
, 0, skb
);
2819 /* copy was successful so update the size parameters */
2820 skb_fill_page_desc(skb
, frg_cnt
, pfrag
->page
, pfrag
->offset
,
2823 pfrag
->offset
+= copy
;
2824 get_page(pfrag
->page
);
2826 skb
->truesize
+= copy
;
2827 atomic_add(copy
, &sk
->sk_wmem_alloc
);
2829 skb
->data_len
+= copy
;
2833 } while (length
> 0);
2837 EXPORT_SYMBOL(skb_append_datato_frags
);
2840 * skb_pull_rcsum - pull skb and update receive checksum
2841 * @skb: buffer to update
2842 * @len: length of data pulled
2844 * This function performs an skb_pull on the packet and updates
2845 * the CHECKSUM_COMPLETE checksum. It should be used on
2846 * receive path processing instead of skb_pull unless you know
2847 * that the checksum difference is zero (e.g., a valid IP header)
2848 * or you are setting ip_summed to CHECKSUM_NONE.
2850 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2852 BUG_ON(len
> skb
->len
);
2854 BUG_ON(skb
->len
< skb
->data_len
);
2855 skb_postpull_rcsum(skb
, skb
->data
, len
);
2856 return skb
->data
+= len
;
2858 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2861 * skb_segment - Perform protocol segmentation on skb.
2862 * @head_skb: buffer to segment
2863 * @features: features for the output path (see dev->features)
2865 * This function performs segmentation on the given skb. It returns
2866 * a pointer to the first in a list of new skbs for the segments.
2867 * In case of error it returns ERR_PTR(err).
2869 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
2870 netdev_features_t features
)
2872 struct sk_buff
*segs
= NULL
;
2873 struct sk_buff
*tail
= NULL
;
2874 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
2875 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
2876 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
2877 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
2878 struct sk_buff
*frag_skb
= head_skb
;
2879 unsigned int offset
= doffset
;
2880 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
2881 unsigned int headroom
;
2885 int sg
= !!(features
& NETIF_F_SG
);
2886 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
2892 __skb_push(head_skb
, doffset
);
2893 proto
= skb_network_protocol(head_skb
, &dummy
);
2894 if (unlikely(!proto
))
2895 return ERR_PTR(-EINVAL
);
2897 csum
= !head_skb
->encap_hdr_csum
&&
2898 !!can_checksum_protocol(features
, proto
);
2900 headroom
= skb_headroom(head_skb
);
2901 pos
= skb_headlen(head_skb
);
2904 struct sk_buff
*nskb
;
2905 skb_frag_t
*nskb_frag
;
2909 len
= head_skb
->len
- offset
;
2913 hsize
= skb_headlen(head_skb
) - offset
;
2916 if (hsize
> len
|| !sg
)
2919 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
2920 (skb_headlen(list_skb
) == len
|| sg
)) {
2921 BUG_ON(skb_headlen(list_skb
) > len
);
2924 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
2925 frag
= skb_shinfo(list_skb
)->frags
;
2926 frag_skb
= list_skb
;
2927 pos
+= skb_headlen(list_skb
);
2929 while (pos
< offset
+ len
) {
2930 BUG_ON(i
>= nfrags
);
2932 size
= skb_frag_size(frag
);
2933 if (pos
+ size
> offset
+ len
)
2941 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
2942 list_skb
= list_skb
->next
;
2944 if (unlikely(!nskb
))
2947 if (unlikely(pskb_trim(nskb
, len
))) {
2952 hsize
= skb_end_offset(nskb
);
2953 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
2958 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
2959 skb_release_head_state(nskb
);
2960 __skb_push(nskb
, doffset
);
2962 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
2963 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
2966 if (unlikely(!nskb
))
2969 skb_reserve(nskb
, headroom
);
2970 __skb_put(nskb
, doffset
);
2979 __copy_skb_header(nskb
, head_skb
);
2981 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
2982 skb_reset_mac_len(nskb
);
2984 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
2985 nskb
->data
- tnl_hlen
,
2986 doffset
+ tnl_hlen
);
2988 if (nskb
->len
== len
+ doffset
)
2989 goto perform_csum_check
;
2992 nskb
->ip_summed
= CHECKSUM_NONE
;
2993 nskb
->csum
= skb_copy_and_csum_bits(head_skb
, offset
,
2996 SKB_GSO_CB(nskb
)->csum_start
=
2997 skb_headroom(nskb
) + doffset
;
3001 nskb_frag
= skb_shinfo(nskb
)->frags
;
3003 skb_copy_from_linear_data_offset(head_skb
, offset
,
3004 skb_put(nskb
, hsize
), hsize
);
3006 skb_shinfo(nskb
)->tx_flags
= skb_shinfo(head_skb
)->tx_flags
&
3009 while (pos
< offset
+ len
) {
3011 BUG_ON(skb_headlen(list_skb
));
3014 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3015 frag
= skb_shinfo(list_skb
)->frags
;
3016 frag_skb
= list_skb
;
3020 list_skb
= list_skb
->next
;
3023 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3025 net_warn_ratelimited(
3026 "skb_segment: too many frags: %u %u\n",
3031 if (unlikely(skb_orphan_frags(frag_skb
, GFP_ATOMIC
)))
3035 __skb_frag_ref(nskb_frag
);
3036 size
= skb_frag_size(nskb_frag
);
3039 nskb_frag
->page_offset
+= offset
- pos
;
3040 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3043 skb_shinfo(nskb
)->nr_frags
++;
3045 if (pos
+ size
<= offset
+ len
) {
3050 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3058 nskb
->data_len
= len
- hsize
;
3059 nskb
->len
+= nskb
->data_len
;
3060 nskb
->truesize
+= nskb
->data_len
;
3064 nskb
->csum
= skb_checksum(nskb
, doffset
,
3065 nskb
->len
- doffset
, 0);
3066 nskb
->ip_summed
= CHECKSUM_NONE
;
3067 SKB_GSO_CB(nskb
)->csum_start
=
3068 skb_headroom(nskb
) + doffset
;
3070 } while ((offset
+= len
) < head_skb
->len
);
3075 kfree_skb_list(segs
);
3076 return ERR_PTR(err
);
3078 EXPORT_SYMBOL_GPL(skb_segment
);
3080 int skb_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
)
3082 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3083 unsigned int offset
= skb_gro_offset(skb
);
3084 unsigned int headlen
= skb_headlen(skb
);
3085 struct sk_buff
*nskb
, *lp
, *p
= *head
;
3086 unsigned int len
= skb_gro_len(skb
);
3087 unsigned int delta_truesize
;
3088 unsigned int headroom
;
3090 if (unlikely(p
->len
+ len
>= 65536))
3093 lp
= NAPI_GRO_CB(p
)->last
;
3094 pinfo
= skb_shinfo(lp
);
3096 if (headlen
<= offset
) {
3099 int i
= skbinfo
->nr_frags
;
3100 int nr_frags
= pinfo
->nr_frags
+ i
;
3102 if (nr_frags
> MAX_SKB_FRAGS
)
3106 pinfo
->nr_frags
= nr_frags
;
3107 skbinfo
->nr_frags
= 0;
3109 frag
= pinfo
->frags
+ nr_frags
;
3110 frag2
= skbinfo
->frags
+ i
;
3115 frag
->page_offset
+= offset
;
3116 skb_frag_size_sub(frag
, offset
);
3118 /* all fragments truesize : remove (head size + sk_buff) */
3119 delta_truesize
= skb
->truesize
-
3120 SKB_TRUESIZE(skb_end_offset(skb
));
3122 skb
->truesize
-= skb
->data_len
;
3123 skb
->len
-= skb
->data_len
;
3126 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3128 } else if (skb
->head_frag
) {
3129 int nr_frags
= pinfo
->nr_frags
;
3130 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3131 struct page
*page
= virt_to_head_page(skb
->head
);
3132 unsigned int first_size
= headlen
- offset
;
3133 unsigned int first_offset
;
3135 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3138 first_offset
= skb
->data
-
3139 (unsigned char *)page_address(page
) +
3142 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3144 frag
->page
.p
= page
;
3145 frag
->page_offset
= first_offset
;
3146 skb_frag_size_set(frag
, first_size
);
3148 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3149 /* We dont need to clear skbinfo->nr_frags here */
3151 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3152 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3155 /* switch back to head shinfo */
3156 pinfo
= skb_shinfo(p
);
3158 if (pinfo
->frag_list
)
3160 if (skb_gro_len(p
) != pinfo
->gso_size
)
3163 headroom
= skb_headroom(p
);
3164 nskb
= alloc_skb(headroom
+ skb_gro_offset(p
), GFP_ATOMIC
);
3165 if (unlikely(!nskb
))
3168 __copy_skb_header(nskb
, p
);
3169 nskb
->mac_len
= p
->mac_len
;
3171 skb_reserve(nskb
, headroom
);
3172 __skb_put(nskb
, skb_gro_offset(p
));
3174 skb_set_mac_header(nskb
, skb_mac_header(p
) - p
->data
);
3175 skb_set_network_header(nskb
, skb_network_offset(p
));
3176 skb_set_transport_header(nskb
, skb_transport_offset(p
));
3178 __skb_pull(p
, skb_gro_offset(p
));
3179 memcpy(skb_mac_header(nskb
), skb_mac_header(p
),
3180 p
->data
- skb_mac_header(p
));
3182 skb_shinfo(nskb
)->frag_list
= p
;
3183 skb_shinfo(nskb
)->gso_size
= pinfo
->gso_size
;
3184 pinfo
->gso_size
= 0;
3185 skb_header_release(p
);
3186 NAPI_GRO_CB(nskb
)->last
= p
;
3188 nskb
->data_len
+= p
->len
;
3189 nskb
->truesize
+= p
->truesize
;
3190 nskb
->len
+= p
->len
;
3193 nskb
->next
= p
->next
;
3199 delta_truesize
= skb
->truesize
;
3200 if (offset
> headlen
) {
3201 unsigned int eat
= offset
- headlen
;
3203 skbinfo
->frags
[0].page_offset
+= eat
;
3204 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3205 skb
->data_len
-= eat
;
3210 __skb_pull(skb
, offset
);
3212 if (NAPI_GRO_CB(p
)->last
== p
)
3213 skb_shinfo(p
)->frag_list
= skb
;
3215 NAPI_GRO_CB(p
)->last
->next
= skb
;
3216 NAPI_GRO_CB(p
)->last
= skb
;
3217 skb_header_release(skb
);
3221 NAPI_GRO_CB(p
)->count
++;
3223 p
->truesize
+= delta_truesize
;
3226 lp
->data_len
+= len
;
3227 lp
->truesize
+= delta_truesize
;
3230 NAPI_GRO_CB(skb
)->same_flow
= 1;
3233 EXPORT_SYMBOL_GPL(skb_gro_receive
);
3235 void __init
skb_init(void)
3237 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
3238 sizeof(struct sk_buff
),
3240 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3242 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3243 (2*sizeof(struct sk_buff
)) +
3246 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3251 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3252 * @skb: Socket buffer containing the buffers to be mapped
3253 * @sg: The scatter-gather list to map into
3254 * @offset: The offset into the buffer's contents to start mapping
3255 * @len: Length of buffer space to be mapped
3257 * Fill the specified scatter-gather list with mappings/pointers into a
3258 * region of the buffer space attached to a socket buffer.
3261 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3263 int start
= skb_headlen(skb
);
3264 int i
, copy
= start
- offset
;
3265 struct sk_buff
*frag_iter
;
3271 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
3273 if ((len
-= copy
) == 0)
3278 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3281 WARN_ON(start
> offset
+ len
);
3283 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3284 if ((copy
= end
- offset
) > 0) {
3285 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
3289 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
3290 frag
->page_offset
+offset
-start
);
3299 skb_walk_frags(skb
, frag_iter
) {
3302 WARN_ON(start
> offset
+ len
);
3304 end
= start
+ frag_iter
->len
;
3305 if ((copy
= end
- offset
) > 0) {
3308 elt
+= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
3310 if ((len
-= copy
) == 0)
3320 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3321 * sglist without mark the sg which contain last skb data as the end.
3322 * So the caller can mannipulate sg list as will when padding new data after
3323 * the first call without calling sg_unmark_end to expend sg list.
3325 * Scenario to use skb_to_sgvec_nomark:
3327 * 2. skb_to_sgvec_nomark(payload1)
3328 * 3. skb_to_sgvec_nomark(payload2)
3330 * This is equivalent to:
3332 * 2. skb_to_sgvec(payload1)
3334 * 4. skb_to_sgvec(payload2)
3336 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3337 * is more preferable.
3339 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
3340 int offset
, int len
)
3342 return __skb_to_sgvec(skb
, sg
, offset
, len
);
3344 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
3346 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3348 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
3350 sg_mark_end(&sg
[nsg
- 1]);
3354 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
3357 * skb_cow_data - Check that a socket buffer's data buffers are writable
3358 * @skb: The socket buffer to check.
3359 * @tailbits: Amount of trailing space to be added
3360 * @trailer: Returned pointer to the skb where the @tailbits space begins
3362 * Make sure that the data buffers attached to a socket buffer are
3363 * writable. If they are not, private copies are made of the data buffers
3364 * and the socket buffer is set to use these instead.
3366 * If @tailbits is given, make sure that there is space to write @tailbits
3367 * bytes of data beyond current end of socket buffer. @trailer will be
3368 * set to point to the skb in which this space begins.
3370 * The number of scatterlist elements required to completely map the
3371 * COW'd and extended socket buffer will be returned.
3373 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
3377 struct sk_buff
*skb1
, **skb_p
;
3379 /* If skb is cloned or its head is paged, reallocate
3380 * head pulling out all the pages (pages are considered not writable
3381 * at the moment even if they are anonymous).
3383 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
3384 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
3387 /* Easy case. Most of packets will go this way. */
3388 if (!skb_has_frag_list(skb
)) {
3389 /* A little of trouble, not enough of space for trailer.
3390 * This should not happen, when stack is tuned to generate
3391 * good frames. OK, on miss we reallocate and reserve even more
3392 * space, 128 bytes is fair. */
3394 if (skb_tailroom(skb
) < tailbits
&&
3395 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
3403 /* Misery. We are in troubles, going to mincer fragments... */
3406 skb_p
= &skb_shinfo(skb
)->frag_list
;
3409 while ((skb1
= *skb_p
) != NULL
) {
3412 /* The fragment is partially pulled by someone,
3413 * this can happen on input. Copy it and everything
3416 if (skb_shared(skb1
))
3419 /* If the skb is the last, worry about trailer. */
3421 if (skb1
->next
== NULL
&& tailbits
) {
3422 if (skb_shinfo(skb1
)->nr_frags
||
3423 skb_has_frag_list(skb1
) ||
3424 skb_tailroom(skb1
) < tailbits
)
3425 ntail
= tailbits
+ 128;
3431 skb_shinfo(skb1
)->nr_frags
||
3432 skb_has_frag_list(skb1
)) {
3433 struct sk_buff
*skb2
;
3435 /* Fuck, we are miserable poor guys... */
3437 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
3439 skb2
= skb_copy_expand(skb1
,
3443 if (unlikely(skb2
== NULL
))
3447 skb_set_owner_w(skb2
, skb1
->sk
);
3449 /* Looking around. Are we still alive?
3450 * OK, link new skb, drop old one */
3452 skb2
->next
= skb1
->next
;
3459 skb_p
= &skb1
->next
;
3464 EXPORT_SYMBOL_GPL(skb_cow_data
);
3466 static void sock_rmem_free(struct sk_buff
*skb
)
3468 struct sock
*sk
= skb
->sk
;
3470 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
3474 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3476 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
3478 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
3479 (unsigned int)sk
->sk_rcvbuf
)
3484 skb
->destructor
= sock_rmem_free
;
3485 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
3487 /* before exiting rcu section, make sure dst is refcounted */
3490 skb_queue_tail(&sk
->sk_error_queue
, skb
);
3491 if (!sock_flag(sk
, SOCK_DEAD
))
3492 sk
->sk_data_ready(sk
);
3495 EXPORT_SYMBOL(sock_queue_err_skb
);
3497 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
3498 struct skb_shared_hwtstamps
*hwtstamps
,
3499 struct sock
*sk
, int tstype
)
3501 struct sock_exterr_skb
*serr
;
3502 struct sk_buff
*skb
;
3509 *skb_hwtstamps(orig_skb
) =
3513 * no hardware time stamps available,
3514 * so keep the shared tx_flags and only
3515 * store software time stamp
3517 orig_skb
->tstamp
= ktime_get_real();
3520 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
3524 serr
= SKB_EXT_ERR(skb
);
3525 memset(serr
, 0, sizeof(*serr
));
3526 serr
->ee
.ee_errno
= ENOMSG
;
3527 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
3528 serr
->ee
.ee_info
= tstype
;
3529 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
3530 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
3531 if (sk
->sk_protocol
== IPPROTO_TCP
)
3532 serr
->ee
.ee_data
-= sk
->sk_tskey
;
3535 err
= sock_queue_err_skb(sk
, skb
);
3540 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
3542 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
3543 struct skb_shared_hwtstamps
*hwtstamps
)
3545 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
3548 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
3550 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
3552 struct sock
*sk
= skb
->sk
;
3553 struct sock_exterr_skb
*serr
;
3556 skb
->wifi_acked_valid
= 1;
3557 skb
->wifi_acked
= acked
;
3559 serr
= SKB_EXT_ERR(skb
);
3560 memset(serr
, 0, sizeof(*serr
));
3561 serr
->ee
.ee_errno
= ENOMSG
;
3562 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
3564 err
= sock_queue_err_skb(sk
, skb
);
3568 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
3572 * skb_partial_csum_set - set up and verify partial csum values for packet
3573 * @skb: the skb to set
3574 * @start: the number of bytes after skb->data to start checksumming.
3575 * @off: the offset from start to place the checksum.
3577 * For untrusted partially-checksummed packets, we need to make sure the values
3578 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3580 * This function checks and sets those values and skb->ip_summed: if this
3581 * returns false you should drop the packet.
3583 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
3585 if (unlikely(start
> skb_headlen(skb
)) ||
3586 unlikely((int)start
+ off
> skb_headlen(skb
) - 2)) {
3587 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3588 start
, off
, skb_headlen(skb
));
3591 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3592 skb
->csum_start
= skb_headroom(skb
) + start
;
3593 skb
->csum_offset
= off
;
3594 skb_set_transport_header(skb
, start
);
3597 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
3599 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
3602 if (skb_headlen(skb
) >= len
)
3605 /* If we need to pullup then pullup to the max, so we
3606 * won't need to do it again.
3611 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
3614 if (skb_headlen(skb
) < len
)
3620 #define MAX_TCP_HDR_LEN (15 * 4)
3622 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
3623 typeof(IPPROTO_IP
) proto
,
3630 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
3631 off
+ MAX_TCP_HDR_LEN
);
3632 if (!err
&& !skb_partial_csum_set(skb
, off
,
3633 offsetof(struct tcphdr
,
3636 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
3639 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
3640 off
+ sizeof(struct udphdr
));
3641 if (!err
&& !skb_partial_csum_set(skb
, off
,
3642 offsetof(struct udphdr
,
3645 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
3648 return ERR_PTR(-EPROTO
);
3651 /* This value should be large enough to cover a tagged ethernet header plus
3652 * maximally sized IP and TCP or UDP headers.
3654 #define MAX_IP_HDR_LEN 128
3656 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
3665 err
= skb_maybe_pull_tail(skb
,
3666 sizeof(struct iphdr
),
3671 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
3674 off
= ip_hdrlen(skb
);
3681 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
3683 return PTR_ERR(csum
);
3686 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
3689 ip_hdr(skb
)->protocol
, 0);
3696 /* This value should be large enough to cover a tagged ethernet header plus
3697 * an IPv6 header, all options, and a maximal TCP or UDP header.
3699 #define MAX_IPV6_HDR_LEN 256
3701 #define OPT_HDR(type, skb, off) \
3702 (type *)(skb_network_header(skb) + (off))
3704 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
3717 off
= sizeof(struct ipv6hdr
);
3719 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
3723 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
3725 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
3726 while (off
<= len
&& !done
) {
3728 case IPPROTO_DSTOPTS
:
3729 case IPPROTO_HOPOPTS
:
3730 case IPPROTO_ROUTING
: {
3731 struct ipv6_opt_hdr
*hp
;
3733 err
= skb_maybe_pull_tail(skb
,
3735 sizeof(struct ipv6_opt_hdr
),
3740 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
3741 nexthdr
= hp
->nexthdr
;
3742 off
+= ipv6_optlen(hp
);
3746 struct ip_auth_hdr
*hp
;
3748 err
= skb_maybe_pull_tail(skb
,
3750 sizeof(struct ip_auth_hdr
),
3755 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
3756 nexthdr
= hp
->nexthdr
;
3757 off
+= ipv6_authlen(hp
);
3760 case IPPROTO_FRAGMENT
: {
3761 struct frag_hdr
*hp
;
3763 err
= skb_maybe_pull_tail(skb
,
3765 sizeof(struct frag_hdr
),
3770 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
3772 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
3775 nexthdr
= hp
->nexthdr
;
3776 off
+= sizeof(struct frag_hdr
);
3787 if (!done
|| fragment
)
3790 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
3792 return PTR_ERR(csum
);
3795 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
3796 &ipv6_hdr(skb
)->daddr
,
3797 skb
->len
- off
, nexthdr
, 0);
3805 * skb_checksum_setup - set up partial checksum offset
3806 * @skb: the skb to set up
3807 * @recalculate: if true the pseudo-header checksum will be recalculated
3809 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
3813 switch (skb
->protocol
) {
3814 case htons(ETH_P_IP
):
3815 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
3818 case htons(ETH_P_IPV6
):
3819 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
3829 EXPORT_SYMBOL(skb_checksum_setup
);
3831 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
3833 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3836 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
3838 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
3841 skb_release_head_state(skb
);
3842 kmem_cache_free(skbuff_head_cache
, skb
);
3847 EXPORT_SYMBOL(kfree_skb_partial
);
3850 * skb_try_coalesce - try to merge skb to prior one
3852 * @from: buffer to add
3853 * @fragstolen: pointer to boolean
3854 * @delta_truesize: how much more was allocated than was requested
3856 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
3857 bool *fragstolen
, int *delta_truesize
)
3859 int i
, delta
, len
= from
->len
;
3861 *fragstolen
= false;
3866 if (len
<= skb_tailroom(to
)) {
3867 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
3868 *delta_truesize
= 0;
3872 if (skb_has_frag_list(to
) || skb_has_frag_list(from
))
3875 if (skb_headlen(from
) != 0) {
3877 unsigned int offset
;
3879 if (skb_shinfo(to
)->nr_frags
+
3880 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
3883 if (skb_head_is_locked(from
))
3886 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3888 page
= virt_to_head_page(from
->head
);
3889 offset
= from
->data
- (unsigned char *)page_address(page
);
3891 skb_fill_page_desc(to
, skb_shinfo(to
)->nr_frags
,
3892 page
, offset
, skb_headlen(from
));
3895 if (skb_shinfo(to
)->nr_frags
+
3896 skb_shinfo(from
)->nr_frags
> MAX_SKB_FRAGS
)
3899 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
3902 WARN_ON_ONCE(delta
< len
);
3904 memcpy(skb_shinfo(to
)->frags
+ skb_shinfo(to
)->nr_frags
,
3905 skb_shinfo(from
)->frags
,
3906 skb_shinfo(from
)->nr_frags
* sizeof(skb_frag_t
));
3907 skb_shinfo(to
)->nr_frags
+= skb_shinfo(from
)->nr_frags
;
3909 if (!skb_cloned(from
))
3910 skb_shinfo(from
)->nr_frags
= 0;
3912 /* if the skb is not cloned this does nothing
3913 * since we set nr_frags to 0.
3915 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++)
3916 skb_frag_ref(from
, i
);
3918 to
->truesize
+= delta
;
3920 to
->data_len
+= len
;
3922 *delta_truesize
= delta
;
3925 EXPORT_SYMBOL(skb_try_coalesce
);
3928 * skb_scrub_packet - scrub an skb
3930 * @skb: buffer to clean
3931 * @xnet: packet is crossing netns
3933 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
3934 * into/from a tunnel. Some information have to be cleared during these
3936 * skb_scrub_packet can also be used to clean a skb before injecting it in
3937 * another namespace (@xnet == true). We have to clear all information in the
3938 * skb that could impact namespace isolation.
3940 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
3944 skb
->tstamp
.tv64
= 0;
3945 skb
->pkt_type
= PACKET_HOST
;
3952 nf_reset_trace(skb
);
3954 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
3957 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3961 * skb_gso_transport_seglen is used to determine the real size of the
3962 * individual segments, including Layer4 headers (TCP/UDP).
3964 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3966 unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
3968 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
3970 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
3971 return tcp_hdrlen(skb
) + shinfo
->gso_size
;
3973 /* UFO sets gso_size to the size of the fragmentation
3974 * payload, i.e. the size of the L4 (UDP) header is already
3977 return shinfo
->gso_size
;
3979 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen
);
3981 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
3983 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
3988 memmove(skb
->data
- ETH_HLEN
, skb
->data
- VLAN_ETH_HLEN
, 2 * ETH_ALEN
);
3989 skb
->mac_header
+= VLAN_HLEN
;
3993 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
3995 struct vlan_hdr
*vhdr
;
3998 if (unlikely(vlan_tx_tag_present(skb
))) {
3999 /* vlan_tci is already set-up so leave this for another time */
4003 skb
= skb_share_check(skb
, GFP_ATOMIC
);
4007 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
4010 vhdr
= (struct vlan_hdr
*)skb
->data
;
4011 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
4012 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
4014 skb_pull_rcsum(skb
, VLAN_HLEN
);
4015 vlan_set_encap_proto(skb
, vhdr
);
4017 skb
= skb_reorder_vlan_header(skb
);
4021 skb_reset_network_header(skb
);
4022 skb_reset_transport_header(skb
);
4023 skb_reset_mac_len(skb
);
4031 EXPORT_SYMBOL(skb_vlan_untag
);