2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
80 struct kmem_cache
*skbuff_head_cache __read_mostly
;
81 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
84 * skb_panic - private function for out-of-line support
88 * @msg: skb_over_panic or skb_under_panic
90 * Out-of-line support for skb_put() and skb_push().
91 * Called via the wrapper skb_over_panic() or skb_under_panic().
92 * Keep out of line to prevent kernel bloat.
93 * __builtin_return_address is not used because it is not always reliable.
95 static void skb_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
,
98 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
99 msg
, addr
, skb
->len
, sz
, skb
->head
, skb
->data
,
100 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
101 skb
->dev
? skb
->dev
->name
: "<NULL>");
105 static void skb_over_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
107 skb_panic(skb
, sz
, addr
, __func__
);
110 static void skb_under_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
112 skb_panic(skb
, sz
, addr
, __func__
);
116 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117 * the caller if emergency pfmemalloc reserves are being used. If it is and
118 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119 * may be used. Otherwise, the packet data may be discarded until enough
122 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
125 static void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
,
126 unsigned long ip
, bool *pfmemalloc
)
129 bool ret_pfmemalloc
= false;
132 * Try a regular allocation, when that fails and we're not entitled
133 * to the reserves, fail.
135 obj
= kmalloc_node_track_caller(size
,
136 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
138 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
141 /* Try again but now we are using pfmemalloc reserves */
142 ret_pfmemalloc
= true;
143 obj
= kmalloc_node_track_caller(size
, flags
, node
);
147 *pfmemalloc
= ret_pfmemalloc
;
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
158 struct sk_buff
*__alloc_skb_head(gfp_t gfp_mask
, int node
)
163 skb
= kmem_cache_alloc_node(skbuff_head_cache
,
164 gfp_mask
& ~__GFP_DMA
, node
);
169 * Only clear those fields we need to clear, not those that we will
170 * actually initialise below. Hence, don't put any more fields after
171 * the tail pointer in struct sk_buff!
173 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
175 skb
->truesize
= sizeof(struct sk_buff
);
176 atomic_set(&skb
->users
, 1);
178 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
184 * __alloc_skb - allocate a network buffer
185 * @size: size to allocate
186 * @gfp_mask: allocation mask
187 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188 * instead of head cache and allocate a cloned (child) skb.
189 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190 * allocations in case the data is required for writeback
191 * @node: numa node to allocate memory on
193 * Allocate a new &sk_buff. The returned buffer has no headroom and a
194 * tail room of at least size bytes. The object has a reference count
195 * of one. The return is the buffer. On a failure the return is %NULL.
197 * Buffers may only be allocated from interrupts using a @gfp_mask of
200 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
203 struct kmem_cache
*cache
;
204 struct skb_shared_info
*shinfo
;
209 cache
= (flags
& SKB_ALLOC_FCLONE
)
210 ? skbuff_fclone_cache
: skbuff_head_cache
;
212 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
213 gfp_mask
|= __GFP_MEMALLOC
;
216 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
221 /* We do our best to align skb_shared_info on a separate cache
222 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
223 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
224 * Both skb->head and skb_shared_info are cache line aligned.
226 size
= SKB_DATA_ALIGN(size
);
227 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
228 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
231 /* kmalloc(size) might give us more room than requested.
232 * Put skb_shared_info exactly at the end of allocated zone,
233 * to allow max possible filling before reallocation.
235 size
= SKB_WITH_OVERHEAD(ksize(data
));
236 prefetchw(data
+ size
);
239 * Only clear those fields we need to clear, not those that we will
240 * actually initialise below. Hence, don't put any more fields after
241 * the tail pointer in struct sk_buff!
243 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
244 /* Account for allocated memory : skb + skb->head */
245 skb
->truesize
= SKB_TRUESIZE(size
);
246 skb
->pfmemalloc
= pfmemalloc
;
247 atomic_set(&skb
->users
, 1);
250 skb_reset_tail_pointer(skb
);
251 skb
->end
= skb
->tail
+ size
;
252 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
253 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
255 /* make sure we initialize shinfo sequentially */
256 shinfo
= skb_shinfo(skb
);
257 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
258 atomic_set(&shinfo
->dataref
, 1);
259 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
261 if (flags
& SKB_ALLOC_FCLONE
) {
262 struct sk_buff_fclones
*fclones
;
264 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
266 kmemcheck_annotate_bitfield(&fclones
->skb2
, flags1
);
267 skb
->fclone
= SKB_FCLONE_ORIG
;
268 atomic_set(&fclones
->fclone_ref
, 1);
270 fclones
->skb2
.fclone
= SKB_FCLONE_CLONE
;
271 fclones
->skb2
.pfmemalloc
= pfmemalloc
;
276 kmem_cache_free(cache
, skb
);
280 EXPORT_SYMBOL(__alloc_skb
);
283 * __build_skb - build a network buffer
284 * @data: data buffer provided by caller
285 * @frag_size: size of data, or 0 if head was kmalloced
287 * Allocate a new &sk_buff. Caller provides space holding head and
288 * skb_shared_info. @data must have been allocated by kmalloc() only if
289 * @frag_size is 0, otherwise data should come from the page allocator
291 * The return is the new skb buffer.
292 * On a failure the return is %NULL, and @data is not freed.
294 * Before IO, driver allocates only data buffer where NIC put incoming frame
295 * Driver should add room at head (NET_SKB_PAD) and
296 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
297 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
298 * before giving packet to stack.
299 * RX rings only contains data buffers, not full skbs.
301 struct sk_buff
*__build_skb(void *data
, unsigned int frag_size
)
303 struct skb_shared_info
*shinfo
;
305 unsigned int size
= frag_size
? : ksize(data
);
307 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
311 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
313 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
314 skb
->truesize
= SKB_TRUESIZE(size
);
315 atomic_set(&skb
->users
, 1);
318 skb_reset_tail_pointer(skb
);
319 skb
->end
= skb
->tail
+ size
;
320 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
321 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
323 /* make sure we initialize shinfo sequentially */
324 shinfo
= skb_shinfo(skb
);
325 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
326 atomic_set(&shinfo
->dataref
, 1);
327 kmemcheck_annotate_variable(shinfo
->destructor_arg
);
332 /* build_skb() is wrapper over __build_skb(), that specifically
333 * takes care of skb->head and skb->pfmemalloc
334 * This means that if @frag_size is not zero, then @data must be backed
335 * by a page fragment, not kmalloc() or vmalloc()
337 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
339 struct sk_buff
*skb
= __build_skb(data
, frag_size
);
341 if (skb
&& frag_size
) {
343 if (page_is_pfmemalloc(virt_to_head_page(data
)))
348 EXPORT_SYMBOL(build_skb
);
350 static DEFINE_PER_CPU(struct page_frag_cache
, netdev_alloc_cache
);
351 static DEFINE_PER_CPU(struct page_frag_cache
, napi_alloc_cache
);
353 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
355 struct page_frag_cache
*nc
;
359 local_irq_save(flags
);
360 nc
= this_cpu_ptr(&netdev_alloc_cache
);
361 data
= __alloc_page_frag(nc
, fragsz
, gfp_mask
);
362 local_irq_restore(flags
);
367 * netdev_alloc_frag - allocate a page fragment
368 * @fragsz: fragment size
370 * Allocates a frag from a page for receive buffer.
371 * Uses GFP_ATOMIC allocations.
373 void *netdev_alloc_frag(unsigned int fragsz
)
375 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
377 EXPORT_SYMBOL(netdev_alloc_frag
);
379 static void *__napi_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
381 struct page_frag_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
383 return __alloc_page_frag(nc
, fragsz
, gfp_mask
);
386 void *napi_alloc_frag(unsigned int fragsz
)
388 return __napi_alloc_frag(fragsz
, GFP_ATOMIC
| __GFP_COLD
);
390 EXPORT_SYMBOL(napi_alloc_frag
);
393 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
394 * @dev: network device to receive on
395 * @length: length to allocate
396 * @gfp_mask: get_free_pages mask, passed to alloc_skb
398 * Allocate a new &sk_buff and assign it a usage count of one. The
399 * buffer has NET_SKB_PAD headroom built in. Users should allocate
400 * the headroom they think they need without accounting for the
401 * built in space. The built in space is used for optimisations.
403 * %NULL is returned if there is no free memory.
405 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
, unsigned int len
,
408 struct page_frag_cache
*nc
;
416 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
417 (gfp_mask
& (__GFP_WAIT
| GFP_DMA
))) {
418 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
424 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
425 len
= SKB_DATA_ALIGN(len
);
427 if (sk_memalloc_socks())
428 gfp_mask
|= __GFP_MEMALLOC
;
430 local_irq_save(flags
);
432 nc
= this_cpu_ptr(&netdev_alloc_cache
);
433 data
= __alloc_page_frag(nc
, len
, gfp_mask
);
434 pfmemalloc
= nc
->pfmemalloc
;
436 local_irq_restore(flags
);
441 skb
= __build_skb(data
, len
);
442 if (unlikely(!skb
)) {
447 /* use OR instead of assignment to avoid clearing of bits in mask */
453 skb_reserve(skb
, NET_SKB_PAD
);
459 EXPORT_SYMBOL(__netdev_alloc_skb
);
462 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
463 * @napi: napi instance this buffer was allocated for
464 * @length: length to allocate
465 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
467 * Allocate a new sk_buff for use in NAPI receive. This buffer will
468 * attempt to allocate the head from a special reserved region used
469 * only for NAPI Rx allocation. By doing this we can save several
470 * CPU cycles by avoiding having to disable and re-enable IRQs.
472 * %NULL is returned if there is no free memory.
474 struct sk_buff
*__napi_alloc_skb(struct napi_struct
*napi
, unsigned int len
,
477 struct page_frag_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
481 len
+= NET_SKB_PAD
+ NET_IP_ALIGN
;
483 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
484 (gfp_mask
& (__GFP_WAIT
| GFP_DMA
))) {
485 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
491 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
492 len
= SKB_DATA_ALIGN(len
);
494 if (sk_memalloc_socks())
495 gfp_mask
|= __GFP_MEMALLOC
;
497 data
= __alloc_page_frag(nc
, len
, gfp_mask
);
501 skb
= __build_skb(data
, len
);
502 if (unlikely(!skb
)) {
507 /* use OR instead of assignment to avoid clearing of bits in mask */
513 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
);
514 skb
->dev
= napi
->dev
;
519 EXPORT_SYMBOL(__napi_alloc_skb
);
521 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
522 int size
, unsigned int truesize
)
524 skb_fill_page_desc(skb
, i
, page
, off
, size
);
526 skb
->data_len
+= size
;
527 skb
->truesize
+= truesize
;
529 EXPORT_SYMBOL(skb_add_rx_frag
);
531 void skb_coalesce_rx_frag(struct sk_buff
*skb
, int i
, int size
,
532 unsigned int truesize
)
534 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
536 skb_frag_size_add(frag
, size
);
538 skb
->data_len
+= size
;
539 skb
->truesize
+= truesize
;
541 EXPORT_SYMBOL(skb_coalesce_rx_frag
);
543 static void skb_drop_list(struct sk_buff
**listp
)
545 kfree_skb_list(*listp
);
549 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
551 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
554 static void skb_clone_fraglist(struct sk_buff
*skb
)
556 struct sk_buff
*list
;
558 skb_walk_frags(skb
, list
)
562 static void skb_free_head(struct sk_buff
*skb
)
564 unsigned char *head
= skb
->head
;
572 static void skb_release_data(struct sk_buff
*skb
)
574 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
578 atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
582 for (i
= 0; i
< shinfo
->nr_frags
; i
++)
583 __skb_frag_unref(&shinfo
->frags
[i
]);
586 * If skb buf is from userspace, we need to notify the caller
587 * the lower device DMA has done;
589 if (shinfo
->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
590 struct ubuf_info
*uarg
;
592 uarg
= shinfo
->destructor_arg
;
594 uarg
->callback(uarg
, true);
597 if (shinfo
->frag_list
)
598 kfree_skb_list(shinfo
->frag_list
);
604 * Free an skbuff by memory without cleaning the state.
606 static void kfree_skbmem(struct sk_buff
*skb
)
608 struct sk_buff_fclones
*fclones
;
610 switch (skb
->fclone
) {
611 case SKB_FCLONE_UNAVAILABLE
:
612 kmem_cache_free(skbuff_head_cache
, skb
);
615 case SKB_FCLONE_ORIG
:
616 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
618 /* We usually free the clone (TX completion) before original skb
619 * This test would have no chance to be true for the clone,
620 * while here, branch prediction will be good.
622 if (atomic_read(&fclones
->fclone_ref
) == 1)
626 default: /* SKB_FCLONE_CLONE */
627 fclones
= container_of(skb
, struct sk_buff_fclones
, skb2
);
630 if (!atomic_dec_and_test(&fclones
->fclone_ref
))
633 kmem_cache_free(skbuff_fclone_cache
, fclones
);
636 static void skb_release_head_state(struct sk_buff
*skb
)
640 secpath_put(skb
->sp
);
642 if (skb
->destructor
) {
644 skb
->destructor(skb
);
646 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
647 nf_conntrack_put(skb
->nfct
);
649 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
650 nf_bridge_put(skb
->nf_bridge
);
654 /* Free everything but the sk_buff shell. */
655 static void skb_release_all(struct sk_buff
*skb
)
657 skb_release_head_state(skb
);
658 if (likely(skb
->head
))
659 skb_release_data(skb
);
663 * __kfree_skb - private function
666 * Free an sk_buff. Release anything attached to the buffer.
667 * Clean the state. This is an internal helper function. Users should
668 * always call kfree_skb
671 void __kfree_skb(struct sk_buff
*skb
)
673 skb_release_all(skb
);
676 EXPORT_SYMBOL(__kfree_skb
);
679 * kfree_skb - free an sk_buff
680 * @skb: buffer to free
682 * Drop a reference to the buffer and free it if the usage count has
685 void kfree_skb(struct sk_buff
*skb
)
689 if (likely(atomic_read(&skb
->users
) == 1))
691 else if (likely(!atomic_dec_and_test(&skb
->users
)))
693 trace_kfree_skb(skb
, __builtin_return_address(0));
696 EXPORT_SYMBOL(kfree_skb
);
698 void kfree_skb_list(struct sk_buff
*segs
)
701 struct sk_buff
*next
= segs
->next
;
707 EXPORT_SYMBOL(kfree_skb_list
);
710 * skb_tx_error - report an sk_buff xmit error
711 * @skb: buffer that triggered an error
713 * Report xmit error if a device callback is tracking this skb.
714 * skb must be freed afterwards.
716 void skb_tx_error(struct sk_buff
*skb
)
718 if (skb_shinfo(skb
)->tx_flags
& SKBTX_DEV_ZEROCOPY
) {
719 struct ubuf_info
*uarg
;
721 uarg
= skb_shinfo(skb
)->destructor_arg
;
723 uarg
->callback(uarg
, false);
724 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
727 EXPORT_SYMBOL(skb_tx_error
);
730 * consume_skb - free an skbuff
731 * @skb: buffer to free
733 * Drop a ref to the buffer and free it if the usage count has hit zero
734 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
735 * is being dropped after a failure and notes that
737 void consume_skb(struct sk_buff
*skb
)
741 if (likely(atomic_read(&skb
->users
) == 1))
743 else if (likely(!atomic_dec_and_test(&skb
->users
)))
745 trace_consume_skb(skb
);
748 EXPORT_SYMBOL(consume_skb
);
750 /* Make sure a field is enclosed inside headers_start/headers_end section */
751 #define CHECK_SKB_FIELD(field) \
752 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
753 offsetof(struct sk_buff, headers_start)); \
754 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
755 offsetof(struct sk_buff, headers_end)); \
757 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
759 new->tstamp
= old
->tstamp
;
760 /* We do not copy old->sk */
762 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
763 skb_dst_copy(new, old
);
765 new->sp
= secpath_get(old
->sp
);
767 __nf_copy(new, old
, false);
769 /* Note : this field could be in headers_start/headers_end section
770 * It is not yet because we do not want to have a 16 bit hole
772 new->queue_mapping
= old
->queue_mapping
;
774 memcpy(&new->headers_start
, &old
->headers_start
,
775 offsetof(struct sk_buff
, headers_end
) -
776 offsetof(struct sk_buff
, headers_start
));
777 CHECK_SKB_FIELD(protocol
);
778 CHECK_SKB_FIELD(csum
);
779 CHECK_SKB_FIELD(hash
);
780 CHECK_SKB_FIELD(priority
);
781 CHECK_SKB_FIELD(skb_iif
);
782 CHECK_SKB_FIELD(vlan_proto
);
783 CHECK_SKB_FIELD(vlan_tci
);
784 CHECK_SKB_FIELD(transport_header
);
785 CHECK_SKB_FIELD(network_header
);
786 CHECK_SKB_FIELD(mac_header
);
787 CHECK_SKB_FIELD(inner_protocol
);
788 CHECK_SKB_FIELD(inner_transport_header
);
789 CHECK_SKB_FIELD(inner_network_header
);
790 CHECK_SKB_FIELD(inner_mac_header
);
791 CHECK_SKB_FIELD(mark
);
792 #ifdef CONFIG_NETWORK_SECMARK
793 CHECK_SKB_FIELD(secmark
);
795 #ifdef CONFIG_NET_RX_BUSY_POLL
796 CHECK_SKB_FIELD(napi_id
);
799 CHECK_SKB_FIELD(sender_cpu
);
801 #ifdef CONFIG_NET_SCHED
802 CHECK_SKB_FIELD(tc_index
);
803 #ifdef CONFIG_NET_CLS_ACT
804 CHECK_SKB_FIELD(tc_verd
);
811 * You should not add any new code to this function. Add it to
812 * __copy_skb_header above instead.
814 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
816 #define C(x) n->x = skb->x
818 n
->next
= n
->prev
= NULL
;
820 __copy_skb_header(n
, skb
);
825 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
828 n
->destructor
= NULL
;
835 atomic_set(&n
->users
, 1);
837 atomic_inc(&(skb_shinfo(skb
)->dataref
));
845 * skb_morph - morph one skb into another
846 * @dst: the skb to receive the contents
847 * @src: the skb to supply the contents
849 * This is identical to skb_clone except that the target skb is
850 * supplied by the user.
852 * The target skb is returned upon exit.
854 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
856 skb_release_all(dst
);
857 return __skb_clone(dst
, src
);
859 EXPORT_SYMBOL_GPL(skb_morph
);
862 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
863 * @skb: the skb to modify
864 * @gfp_mask: allocation priority
866 * This must be called on SKBTX_DEV_ZEROCOPY skb.
867 * It will copy all frags into kernel and drop the reference
868 * to userspace pages.
870 * If this function is called from an interrupt gfp_mask() must be
873 * Returns 0 on success or a negative error code on failure
874 * to allocate kernel memory to copy to.
876 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
879 int num_frags
= skb_shinfo(skb
)->nr_frags
;
880 struct page
*page
, *head
= NULL
;
881 struct ubuf_info
*uarg
= skb_shinfo(skb
)->destructor_arg
;
883 for (i
= 0; i
< num_frags
; i
++) {
885 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
887 page
= alloc_page(gfp_mask
);
890 struct page
*next
= (struct page
*)page_private(head
);
896 vaddr
= kmap_atomic(skb_frag_page(f
));
897 memcpy(page_address(page
),
898 vaddr
+ f
->page_offset
, skb_frag_size(f
));
899 kunmap_atomic(vaddr
);
900 set_page_private(page
, (unsigned long)head
);
904 /* skb frags release userspace buffers */
905 for (i
= 0; i
< num_frags
; i
++)
906 skb_frag_unref(skb
, i
);
908 uarg
->callback(uarg
, false);
910 /* skb frags point to kernel buffers */
911 for (i
= num_frags
- 1; i
>= 0; i
--) {
912 __skb_fill_page_desc(skb
, i
, head
, 0,
913 skb_shinfo(skb
)->frags
[i
].size
);
914 head
= (struct page
*)page_private(head
);
917 skb_shinfo(skb
)->tx_flags
&= ~SKBTX_DEV_ZEROCOPY
;
920 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
923 * skb_clone - duplicate an sk_buff
924 * @skb: buffer to clone
925 * @gfp_mask: allocation priority
927 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
928 * copies share the same packet data but not structure. The new
929 * buffer has a reference count of 1. If the allocation fails the
930 * function returns %NULL otherwise the new buffer is returned.
932 * If this function is called from an interrupt gfp_mask() must be
936 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
938 struct sk_buff_fclones
*fclones
= container_of(skb
,
939 struct sk_buff_fclones
,
943 if (skb_orphan_frags(skb
, gfp_mask
))
946 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
947 atomic_read(&fclones
->fclone_ref
) == 1) {
949 atomic_set(&fclones
->fclone_ref
, 2);
951 if (skb_pfmemalloc(skb
))
952 gfp_mask
|= __GFP_MEMALLOC
;
954 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
958 kmemcheck_annotate_bitfield(n
, flags1
);
959 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
962 return __skb_clone(n
, skb
);
964 EXPORT_SYMBOL(skb_clone
);
966 static void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
968 /* Only adjust this if it actually is csum_start rather than csum */
969 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
970 skb
->csum_start
+= off
;
971 /* {transport,network,mac}_header and tail are relative to skb->head */
972 skb
->transport_header
+= off
;
973 skb
->network_header
+= off
;
974 if (skb_mac_header_was_set(skb
))
975 skb
->mac_header
+= off
;
976 skb
->inner_transport_header
+= off
;
977 skb
->inner_network_header
+= off
;
978 skb
->inner_mac_header
+= off
;
981 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
983 __copy_skb_header(new, old
);
985 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
986 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
987 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
990 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
992 if (skb_pfmemalloc(skb
))
998 * skb_copy - create private copy of an sk_buff
999 * @skb: buffer to copy
1000 * @gfp_mask: allocation priority
1002 * Make a copy of both an &sk_buff and its data. This is used when the
1003 * caller wishes to modify the data and needs a private copy of the
1004 * data to alter. Returns %NULL on failure or the pointer to the buffer
1005 * on success. The returned buffer has a reference count of 1.
1007 * As by-product this function converts non-linear &sk_buff to linear
1008 * one, so that &sk_buff becomes completely private and caller is allowed
1009 * to modify all the data of returned buffer. This means that this
1010 * function is not recommended for use in circumstances when only
1011 * header is going to be modified. Use pskb_copy() instead.
1014 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
1016 int headerlen
= skb_headroom(skb
);
1017 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
1018 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
1019 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
1024 /* Set the data pointer */
1025 skb_reserve(n
, headerlen
);
1026 /* Set the tail pointer and length */
1027 skb_put(n
, skb
->len
);
1029 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
1032 copy_skb_header(n
, skb
);
1035 EXPORT_SYMBOL(skb_copy
);
1038 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1039 * @skb: buffer to copy
1040 * @headroom: headroom of new skb
1041 * @gfp_mask: allocation priority
1042 * @fclone: if true allocate the copy of the skb from the fclone
1043 * cache instead of the head cache; it is recommended to set this
1044 * to true for the cases where the copy will likely be cloned
1046 * Make a copy of both an &sk_buff and part of its data, located
1047 * in header. Fragmented data remain shared. This is used when
1048 * the caller wishes to modify only header of &sk_buff and needs
1049 * private copy of the header to alter. Returns %NULL on failure
1050 * or the pointer to the buffer on success.
1051 * The returned buffer has a reference count of 1.
1054 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
1055 gfp_t gfp_mask
, bool fclone
)
1057 unsigned int size
= skb_headlen(skb
) + headroom
;
1058 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
1059 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
1064 /* Set the data pointer */
1065 skb_reserve(n
, headroom
);
1066 /* Set the tail pointer and length */
1067 skb_put(n
, skb_headlen(skb
));
1068 /* Copy the bytes */
1069 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1071 n
->truesize
+= skb
->data_len
;
1072 n
->data_len
= skb
->data_len
;
1075 if (skb_shinfo(skb
)->nr_frags
) {
1078 if (skb_orphan_frags(skb
, gfp_mask
)) {
1083 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1084 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1085 skb_frag_ref(skb
, i
);
1087 skb_shinfo(n
)->nr_frags
= i
;
1090 if (skb_has_frag_list(skb
)) {
1091 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1092 skb_clone_fraglist(n
);
1095 copy_skb_header(n
, skb
);
1099 EXPORT_SYMBOL(__pskb_copy_fclone
);
1102 * pskb_expand_head - reallocate header of &sk_buff
1103 * @skb: buffer to reallocate
1104 * @nhead: room to add at head
1105 * @ntail: room to add at tail
1106 * @gfp_mask: allocation priority
1108 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1109 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1110 * reference count of 1. Returns zero in the case of success or error,
1111 * if expansion failed. In the last case, &sk_buff is not changed.
1113 * All the pointers pointing into skb header may change and must be
1114 * reloaded after call to this function.
1117 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1122 int size
= nhead
+ skb_end_offset(skb
) + ntail
;
1127 if (skb_shared(skb
))
1130 size
= SKB_DATA_ALIGN(size
);
1132 if (skb_pfmemalloc(skb
))
1133 gfp_mask
|= __GFP_MEMALLOC
;
1134 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1135 gfp_mask
, NUMA_NO_NODE
, NULL
);
1138 size
= SKB_WITH_OVERHEAD(ksize(data
));
1140 /* Copy only real data... and, alas, header. This should be
1141 * optimized for the cases when header is void.
1143 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1145 memcpy((struct skb_shared_info
*)(data
+ size
),
1147 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1150 * if shinfo is shared we must drop the old head gracefully, but if it
1151 * is not we can just drop the old head and let the existing refcount
1152 * be since all we did is relocate the values
1154 if (skb_cloned(skb
)) {
1155 /* copy this zero copy skb frags */
1156 if (skb_orphan_frags(skb
, gfp_mask
))
1158 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1159 skb_frag_ref(skb
, i
);
1161 if (skb_has_frag_list(skb
))
1162 skb_clone_fraglist(skb
);
1164 skb_release_data(skb
);
1168 off
= (data
+ nhead
) - skb
->head
;
1173 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1177 skb
->end
= skb
->head
+ size
;
1180 skb_headers_offset_update(skb
, nhead
);
1184 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1192 EXPORT_SYMBOL(pskb_expand_head
);
1194 /* Make private copy of skb with writable head and some headroom */
1196 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1198 struct sk_buff
*skb2
;
1199 int delta
= headroom
- skb_headroom(skb
);
1202 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1204 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1205 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1213 EXPORT_SYMBOL(skb_realloc_headroom
);
1216 * skb_copy_expand - copy and expand sk_buff
1217 * @skb: buffer to copy
1218 * @newheadroom: new free bytes at head
1219 * @newtailroom: new free bytes at tail
1220 * @gfp_mask: allocation priority
1222 * Make a copy of both an &sk_buff and its data and while doing so
1223 * allocate additional space.
1225 * This is used when the caller wishes to modify the data and needs a
1226 * private copy of the data to alter as well as more space for new fields.
1227 * Returns %NULL on failure or the pointer to the buffer
1228 * on success. The returned buffer has a reference count of 1.
1230 * You must pass %GFP_ATOMIC as the allocation priority if this function
1231 * is called from an interrupt.
1233 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1234 int newheadroom
, int newtailroom
,
1238 * Allocate the copy buffer
1240 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1241 gfp_mask
, skb_alloc_rx_flag(skb
),
1243 int oldheadroom
= skb_headroom(skb
);
1244 int head_copy_len
, head_copy_off
;
1249 skb_reserve(n
, newheadroom
);
1251 /* Set the tail pointer and length */
1252 skb_put(n
, skb
->len
);
1254 head_copy_len
= oldheadroom
;
1256 if (newheadroom
<= head_copy_len
)
1257 head_copy_len
= newheadroom
;
1259 head_copy_off
= newheadroom
- head_copy_len
;
1261 /* Copy the linear header and data. */
1262 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1263 skb
->len
+ head_copy_len
))
1266 copy_skb_header(n
, skb
);
1268 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1272 EXPORT_SYMBOL(skb_copy_expand
);
1275 * skb_pad - zero pad the tail of an skb
1276 * @skb: buffer to pad
1277 * @pad: space to pad
1279 * Ensure that a buffer is followed by a padding area that is zero
1280 * filled. Used by network drivers which may DMA or transfer data
1281 * beyond the buffer end onto the wire.
1283 * May return error in out of memory cases. The skb is freed on error.
1286 int skb_pad(struct sk_buff
*skb
, int pad
)
1291 /* If the skbuff is non linear tailroom is always zero.. */
1292 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1293 memset(skb
->data
+skb
->len
, 0, pad
);
1297 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1298 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1299 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1304 /* FIXME: The use of this function with non-linear skb's really needs
1307 err
= skb_linearize(skb
);
1311 memset(skb
->data
+ skb
->len
, 0, pad
);
1318 EXPORT_SYMBOL(skb_pad
);
1321 * pskb_put - add data to the tail of a potentially fragmented buffer
1322 * @skb: start of the buffer to use
1323 * @tail: tail fragment of the buffer to use
1324 * @len: amount of data to add
1326 * This function extends the used data area of the potentially
1327 * fragmented buffer. @tail must be the last fragment of @skb -- or
1328 * @skb itself. If this would exceed the total buffer size the kernel
1329 * will panic. A pointer to the first byte of the extra data is
1333 unsigned char *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1336 skb
->data_len
+= len
;
1339 return skb_put(tail
, len
);
1341 EXPORT_SYMBOL_GPL(pskb_put
);
1344 * skb_put - add data to a buffer
1345 * @skb: buffer to use
1346 * @len: amount of data to add
1348 * This function extends the used data area of the buffer. If this would
1349 * exceed the total buffer size the kernel will panic. A pointer to the
1350 * first byte of the extra data is returned.
1352 unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
1354 unsigned char *tmp
= skb_tail_pointer(skb
);
1355 SKB_LINEAR_ASSERT(skb
);
1358 if (unlikely(skb
->tail
> skb
->end
))
1359 skb_over_panic(skb
, len
, __builtin_return_address(0));
1362 EXPORT_SYMBOL(skb_put
);
1365 * skb_push - add data to the start of a buffer
1366 * @skb: buffer to use
1367 * @len: amount of data to add
1369 * This function extends the used data area of the buffer at the buffer
1370 * start. If this would exceed the total buffer headroom the kernel will
1371 * panic. A pointer to the first byte of the extra data is returned.
1373 unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
1377 if (unlikely(skb
->data
<skb
->head
))
1378 skb_under_panic(skb
, len
, __builtin_return_address(0));
1381 EXPORT_SYMBOL(skb_push
);
1384 * skb_pull - remove data from the start of a buffer
1385 * @skb: buffer to use
1386 * @len: amount of data to remove
1388 * This function removes data from the start of a buffer, returning
1389 * the memory to the headroom. A pointer to the next data in the buffer
1390 * is returned. Once the data has been pulled future pushes will overwrite
1393 unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1395 return skb_pull_inline(skb
, len
);
1397 EXPORT_SYMBOL(skb_pull
);
1400 * skb_trim - remove end from a buffer
1401 * @skb: buffer to alter
1404 * Cut the length of a buffer down by removing data from the tail. If
1405 * the buffer is already under the length specified it is not modified.
1406 * The skb must be linear.
1408 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1411 __skb_trim(skb
, len
);
1413 EXPORT_SYMBOL(skb_trim
);
1415 /* Trims skb to length len. It can change skb pointers.
1418 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1420 struct sk_buff
**fragp
;
1421 struct sk_buff
*frag
;
1422 int offset
= skb_headlen(skb
);
1423 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1427 if (skb_cloned(skb
) &&
1428 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1435 for (; i
< nfrags
; i
++) {
1436 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1443 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1446 skb_shinfo(skb
)->nr_frags
= i
;
1448 for (; i
< nfrags
; i
++)
1449 skb_frag_unref(skb
, i
);
1451 if (skb_has_frag_list(skb
))
1452 skb_drop_fraglist(skb
);
1456 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1457 fragp
= &frag
->next
) {
1458 int end
= offset
+ frag
->len
;
1460 if (skb_shared(frag
)) {
1461 struct sk_buff
*nfrag
;
1463 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1464 if (unlikely(!nfrag
))
1467 nfrag
->next
= frag
->next
;
1479 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1483 skb_drop_list(&frag
->next
);
1488 if (len
> skb_headlen(skb
)) {
1489 skb
->data_len
-= skb
->len
- len
;
1494 skb_set_tail_pointer(skb
, len
);
1499 EXPORT_SYMBOL(___pskb_trim
);
1502 * __pskb_pull_tail - advance tail of skb header
1503 * @skb: buffer to reallocate
1504 * @delta: number of bytes to advance tail
1506 * The function makes a sense only on a fragmented &sk_buff,
1507 * it expands header moving its tail forward and copying necessary
1508 * data from fragmented part.
1510 * &sk_buff MUST have reference count of 1.
1512 * Returns %NULL (and &sk_buff does not change) if pull failed
1513 * or value of new tail of skb in the case of success.
1515 * All the pointers pointing into skb header may change and must be
1516 * reloaded after call to this function.
1519 /* Moves tail of skb head forward, copying data from fragmented part,
1520 * when it is necessary.
1521 * 1. It may fail due to malloc failure.
1522 * 2. It may change skb pointers.
1524 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1526 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1528 /* If skb has not enough free space at tail, get new one
1529 * plus 128 bytes for future expansions. If we have enough
1530 * room at tail, reallocate without expansion only if skb is cloned.
1532 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1534 if (eat
> 0 || skb_cloned(skb
)) {
1535 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1540 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1543 /* Optimization: no fragments, no reasons to preestimate
1544 * size of pulled pages. Superb.
1546 if (!skb_has_frag_list(skb
))
1549 /* Estimate size of pulled pages. */
1551 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1552 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1559 /* If we need update frag list, we are in troubles.
1560 * Certainly, it possible to add an offset to skb data,
1561 * but taking into account that pulling is expected to
1562 * be very rare operation, it is worth to fight against
1563 * further bloating skb head and crucify ourselves here instead.
1564 * Pure masohism, indeed. 8)8)
1567 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1568 struct sk_buff
*clone
= NULL
;
1569 struct sk_buff
*insp
= NULL
;
1574 if (list
->len
<= eat
) {
1575 /* Eaten as whole. */
1580 /* Eaten partially. */
1582 if (skb_shared(list
)) {
1583 /* Sucks! We need to fork list. :-( */
1584 clone
= skb_clone(list
, GFP_ATOMIC
);
1590 /* This may be pulled without
1594 if (!pskb_pull(list
, eat
)) {
1602 /* Free pulled out fragments. */
1603 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1604 skb_shinfo(skb
)->frag_list
= list
->next
;
1607 /* And insert new clone at head. */
1610 skb_shinfo(skb
)->frag_list
= clone
;
1613 /* Success! Now we may commit changes to skb data. */
1618 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1619 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1622 skb_frag_unref(skb
, i
);
1625 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1627 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1628 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
1634 skb_shinfo(skb
)->nr_frags
= k
;
1637 skb
->data_len
-= delta
;
1639 return skb_tail_pointer(skb
);
1641 EXPORT_SYMBOL(__pskb_pull_tail
);
1644 * skb_copy_bits - copy bits from skb to kernel buffer
1646 * @offset: offset in source
1647 * @to: destination buffer
1648 * @len: number of bytes to copy
1650 * Copy the specified number of bytes from the source skb to the
1651 * destination buffer.
1654 * If its prototype is ever changed,
1655 * check arch/{*}/net/{*}.S files,
1656 * since it is called from BPF assembly code.
1658 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1660 int start
= skb_headlen(skb
);
1661 struct sk_buff
*frag_iter
;
1664 if (offset
> (int)skb
->len
- len
)
1668 if ((copy
= start
- offset
) > 0) {
1671 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1672 if ((len
-= copy
) == 0)
1678 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1680 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1682 WARN_ON(start
> offset
+ len
);
1684 end
= start
+ skb_frag_size(f
);
1685 if ((copy
= end
- offset
) > 0) {
1691 vaddr
= kmap_atomic(skb_frag_page(f
));
1693 vaddr
+ f
->page_offset
+ offset
- start
,
1695 kunmap_atomic(vaddr
);
1697 if ((len
-= copy
) == 0)
1705 skb_walk_frags(skb
, frag_iter
) {
1708 WARN_ON(start
> offset
+ len
);
1710 end
= start
+ frag_iter
->len
;
1711 if ((copy
= end
- offset
) > 0) {
1714 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
1716 if ((len
-= copy
) == 0)
1730 EXPORT_SYMBOL(skb_copy_bits
);
1733 * Callback from splice_to_pipe(), if we need to release some pages
1734 * at the end of the spd in case we error'ed out in filling the pipe.
1736 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1738 put_page(spd
->pages
[i
]);
1741 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
1742 unsigned int *offset
,
1745 struct page_frag
*pfrag
= sk_page_frag(sk
);
1747 if (!sk_page_frag_refill(sk
, pfrag
))
1750 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
1752 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
1753 page_address(page
) + *offset
, *len
);
1754 *offset
= pfrag
->offset
;
1755 pfrag
->offset
+= *len
;
1760 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
1762 unsigned int offset
)
1764 return spd
->nr_pages
&&
1765 spd
->pages
[spd
->nr_pages
- 1] == page
&&
1766 (spd
->partial
[spd
->nr_pages
- 1].offset
+
1767 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
1771 * Fill page/offset/length into spd, if it can hold more pages.
1773 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
1774 struct pipe_inode_info
*pipe
, struct page
*page
,
1775 unsigned int *len
, unsigned int offset
,
1779 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
1783 page
= linear_to_page(page
, len
, &offset
, sk
);
1787 if (spd_can_coalesce(spd
, page
, offset
)) {
1788 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
1792 spd
->pages
[spd
->nr_pages
] = page
;
1793 spd
->partial
[spd
->nr_pages
].len
= *len
;
1794 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1800 static bool __splice_segment(struct page
*page
, unsigned int poff
,
1801 unsigned int plen
, unsigned int *off
,
1803 struct splice_pipe_desc
*spd
, bool linear
,
1805 struct pipe_inode_info
*pipe
)
1810 /* skip this segment if already processed */
1816 /* ignore any bits we already processed */
1822 unsigned int flen
= min(*len
, plen
);
1824 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
1830 } while (*len
&& plen
);
1836 * Map linear and fragment data from the skb to spd. It reports true if the
1837 * pipe is full or if we already spliced the requested length.
1839 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
1840 unsigned int *offset
, unsigned int *len
,
1841 struct splice_pipe_desc
*spd
, struct sock
*sk
)
1845 /* map the linear part :
1846 * If skb->head_frag is set, this 'linear' part is backed by a
1847 * fragment, and if the head is not shared with any clones then
1848 * we can avoid a copy since we own the head portion of this page.
1850 if (__splice_segment(virt_to_page(skb
->data
),
1851 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
1854 skb_head_is_locked(skb
),
1859 * then map the fragments
1861 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1862 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1864 if (__splice_segment(skb_frag_page(f
),
1865 f
->page_offset
, skb_frag_size(f
),
1866 offset
, len
, spd
, false, sk
, pipe
))
1873 ssize_t
skb_socket_splice(struct sock
*sk
,
1874 struct pipe_inode_info
*pipe
,
1875 struct splice_pipe_desc
*spd
)
1879 /* Drop the socket lock, otherwise we have reverse
1880 * locking dependencies between sk_lock and i_mutex
1881 * here as compared to sendfile(). We enter here
1882 * with the socket lock held, and splice_to_pipe() will
1883 * grab the pipe inode lock. For sendfile() emulation,
1884 * we call into ->sendpage() with the i_mutex lock held
1885 * and networking will grab the socket lock.
1888 ret
= splice_to_pipe(pipe
, spd
);
1895 * Map data from the skb to a pipe. Should handle both the linear part,
1896 * the fragments, and the frag list. It does NOT handle frag lists within
1897 * the frag list, if such a thing exists. We'd probably need to recurse to
1898 * handle that cleanly.
1900 int skb_splice_bits(struct sk_buff
*skb
, struct sock
*sk
, unsigned int offset
,
1901 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1903 ssize_t (*splice_cb
)(struct sock
*,
1904 struct pipe_inode_info
*,
1905 struct splice_pipe_desc
*))
1907 struct partial_page partial
[MAX_SKB_FRAGS
];
1908 struct page
*pages
[MAX_SKB_FRAGS
];
1909 struct splice_pipe_desc spd
= {
1912 .nr_pages_max
= MAX_SKB_FRAGS
,
1914 .ops
= &nosteal_pipe_buf_ops
,
1915 .spd_release
= sock_spd_release
,
1917 struct sk_buff
*frag_iter
;
1921 * __skb_splice_bits() only fails if the output has no room left,
1922 * so no point in going over the frag_list for the error case.
1924 if (__skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
))
1930 * now see if we have a frag_list to map
1932 skb_walk_frags(skb
, frag_iter
) {
1935 if (__skb_splice_bits(frag_iter
, pipe
, &offset
, &tlen
, &spd
, sk
))
1941 ret
= splice_cb(sk
, pipe
, &spd
);
1945 EXPORT_SYMBOL_GPL(skb_splice_bits
);
1948 * skb_store_bits - store bits from kernel buffer to skb
1949 * @skb: destination buffer
1950 * @offset: offset in destination
1951 * @from: source buffer
1952 * @len: number of bytes to copy
1954 * Copy the specified number of bytes from the source buffer to the
1955 * destination skb. This function handles all the messy bits of
1956 * traversing fragment lists and such.
1959 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1961 int start
= skb_headlen(skb
);
1962 struct sk_buff
*frag_iter
;
1965 if (offset
> (int)skb
->len
- len
)
1968 if ((copy
= start
- offset
) > 0) {
1971 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1972 if ((len
-= copy
) == 0)
1978 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1979 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1982 WARN_ON(start
> offset
+ len
);
1984 end
= start
+ skb_frag_size(frag
);
1985 if ((copy
= end
- offset
) > 0) {
1991 vaddr
= kmap_atomic(skb_frag_page(frag
));
1992 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1994 kunmap_atomic(vaddr
);
1996 if ((len
-= copy
) == 0)
2004 skb_walk_frags(skb
, frag_iter
) {
2007 WARN_ON(start
> offset
+ len
);
2009 end
= start
+ frag_iter
->len
;
2010 if ((copy
= end
- offset
) > 0) {
2013 if (skb_store_bits(frag_iter
, offset
- start
,
2016 if ((len
-= copy
) == 0)
2029 EXPORT_SYMBOL(skb_store_bits
);
2031 /* Checksum skb data. */
2032 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
2033 __wsum csum
, const struct skb_checksum_ops
*ops
)
2035 int start
= skb_headlen(skb
);
2036 int i
, copy
= start
- offset
;
2037 struct sk_buff
*frag_iter
;
2040 /* Checksum header. */
2044 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
2045 if ((len
-= copy
) == 0)
2051 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2053 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2055 WARN_ON(start
> offset
+ len
);
2057 end
= start
+ skb_frag_size(frag
);
2058 if ((copy
= end
- offset
) > 0) {
2064 vaddr
= kmap_atomic(skb_frag_page(frag
));
2065 csum2
= ops
->update(vaddr
+ frag
->page_offset
+
2066 offset
- start
, copy
, 0);
2067 kunmap_atomic(vaddr
);
2068 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2077 skb_walk_frags(skb
, frag_iter
) {
2080 WARN_ON(start
> offset
+ len
);
2082 end
= start
+ frag_iter
->len
;
2083 if ((copy
= end
- offset
) > 0) {
2087 csum2
= __skb_checksum(frag_iter
, offset
- start
,
2089 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2090 if ((len
-= copy
) == 0)
2101 EXPORT_SYMBOL(__skb_checksum
);
2103 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2104 int len
, __wsum csum
)
2106 const struct skb_checksum_ops ops
= {
2107 .update
= csum_partial_ext
,
2108 .combine
= csum_block_add_ext
,
2111 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2113 EXPORT_SYMBOL(skb_checksum
);
2115 /* Both of above in one bottle. */
2117 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2118 u8
*to
, int len
, __wsum csum
)
2120 int start
= skb_headlen(skb
);
2121 int i
, copy
= start
- offset
;
2122 struct sk_buff
*frag_iter
;
2129 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2131 if ((len
-= copy
) == 0)
2138 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2141 WARN_ON(start
> offset
+ len
);
2143 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2144 if ((copy
= end
- offset
) > 0) {
2147 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2151 vaddr
= kmap_atomic(skb_frag_page(frag
));
2152 csum2
= csum_partial_copy_nocheck(vaddr
+
2156 kunmap_atomic(vaddr
);
2157 csum
= csum_block_add(csum
, csum2
, pos
);
2167 skb_walk_frags(skb
, frag_iter
) {
2171 WARN_ON(start
> offset
+ len
);
2173 end
= start
+ frag_iter
->len
;
2174 if ((copy
= end
- offset
) > 0) {
2177 csum2
= skb_copy_and_csum_bits(frag_iter
,
2180 csum
= csum_block_add(csum
, csum2
, pos
);
2181 if ((len
-= copy
) == 0)
2192 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2195 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2196 * @from: source buffer
2198 * Calculates the amount of linear headroom needed in the 'to' skb passed
2199 * into skb_zerocopy().
2202 skb_zerocopy_headlen(const struct sk_buff
*from
)
2204 unsigned int hlen
= 0;
2206 if (!from
->head_frag
||
2207 skb_headlen(from
) < L1_CACHE_BYTES
||
2208 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2209 hlen
= skb_headlen(from
);
2211 if (skb_has_frag_list(from
))
2216 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2219 * skb_zerocopy - Zero copy skb to skb
2220 * @to: destination buffer
2221 * @from: source buffer
2222 * @len: number of bytes to copy from source buffer
2223 * @hlen: size of linear headroom in destination buffer
2225 * Copies up to `len` bytes from `from` to `to` by creating references
2226 * to the frags in the source buffer.
2228 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2229 * headroom in the `to` buffer.
2232 * 0: everything is OK
2233 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2234 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2237 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2240 int plen
= 0; /* length of skb->head fragment */
2243 unsigned int offset
;
2245 BUG_ON(!from
->head_frag
&& !hlen
);
2247 /* dont bother with small payloads */
2248 if (len
<= skb_tailroom(to
))
2249 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2252 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2257 plen
= min_t(int, skb_headlen(from
), len
);
2259 page
= virt_to_head_page(from
->head
);
2260 offset
= from
->data
- (unsigned char *)page_address(page
);
2261 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2268 to
->truesize
+= len
+ plen
;
2269 to
->len
+= len
+ plen
;
2270 to
->data_len
+= len
+ plen
;
2272 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2277 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2280 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2281 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2282 len
-= skb_shinfo(to
)->frags
[j
].size
;
2283 skb_frag_ref(to
, j
);
2286 skb_shinfo(to
)->nr_frags
= j
;
2290 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2292 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2297 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2298 csstart
= skb_checksum_start_offset(skb
);
2300 csstart
= skb_headlen(skb
);
2302 BUG_ON(csstart
> skb_headlen(skb
));
2304 skb_copy_from_linear_data(skb
, to
, csstart
);
2307 if (csstart
!= skb
->len
)
2308 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2309 skb
->len
- csstart
, 0);
2311 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2312 long csstuff
= csstart
+ skb
->csum_offset
;
2314 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2317 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2320 * skb_dequeue - remove from the head of the queue
2321 * @list: list to dequeue from
2323 * Remove the head of the list. The list lock is taken so the function
2324 * may be used safely with other locking list functions. The head item is
2325 * returned or %NULL if the list is empty.
2328 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2330 unsigned long flags
;
2331 struct sk_buff
*result
;
2333 spin_lock_irqsave(&list
->lock
, flags
);
2334 result
= __skb_dequeue(list
);
2335 spin_unlock_irqrestore(&list
->lock
, flags
);
2338 EXPORT_SYMBOL(skb_dequeue
);
2341 * skb_dequeue_tail - remove from the tail of the queue
2342 * @list: list to dequeue from
2344 * Remove the tail of the list. The list lock is taken so the function
2345 * may be used safely with other locking list functions. The tail item is
2346 * returned or %NULL if the list is empty.
2348 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2350 unsigned long flags
;
2351 struct sk_buff
*result
;
2353 spin_lock_irqsave(&list
->lock
, flags
);
2354 result
= __skb_dequeue_tail(list
);
2355 spin_unlock_irqrestore(&list
->lock
, flags
);
2358 EXPORT_SYMBOL(skb_dequeue_tail
);
2361 * skb_queue_purge - empty a list
2362 * @list: list to empty
2364 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2365 * the list and one reference dropped. This function takes the list
2366 * lock and is atomic with respect to other list locking functions.
2368 void skb_queue_purge(struct sk_buff_head
*list
)
2370 struct sk_buff
*skb
;
2371 while ((skb
= skb_dequeue(list
)) != NULL
)
2374 EXPORT_SYMBOL(skb_queue_purge
);
2377 * skb_queue_head - queue a buffer at the list head
2378 * @list: list to use
2379 * @newsk: buffer to queue
2381 * Queue a buffer at the start of the list. This function takes the
2382 * list lock and can be used safely with other locking &sk_buff functions
2385 * A buffer cannot be placed on two lists at the same time.
2387 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2389 unsigned long flags
;
2391 spin_lock_irqsave(&list
->lock
, flags
);
2392 __skb_queue_head(list
, newsk
);
2393 spin_unlock_irqrestore(&list
->lock
, flags
);
2395 EXPORT_SYMBOL(skb_queue_head
);
2398 * skb_queue_tail - queue a buffer at the list tail
2399 * @list: list to use
2400 * @newsk: buffer to queue
2402 * Queue a buffer at the tail of the list. This function takes the
2403 * list lock and can be used safely with other locking &sk_buff functions
2406 * A buffer cannot be placed on two lists at the same time.
2408 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2410 unsigned long flags
;
2412 spin_lock_irqsave(&list
->lock
, flags
);
2413 __skb_queue_tail(list
, newsk
);
2414 spin_unlock_irqrestore(&list
->lock
, flags
);
2416 EXPORT_SYMBOL(skb_queue_tail
);
2419 * skb_unlink - remove a buffer from a list
2420 * @skb: buffer to remove
2421 * @list: list to use
2423 * Remove a packet from a list. The list locks are taken and this
2424 * function is atomic with respect to other list locked calls
2426 * You must know what list the SKB is on.
2428 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
2430 unsigned long flags
;
2432 spin_lock_irqsave(&list
->lock
, flags
);
2433 __skb_unlink(skb
, list
);
2434 spin_unlock_irqrestore(&list
->lock
, flags
);
2436 EXPORT_SYMBOL(skb_unlink
);
2439 * skb_append - append a buffer
2440 * @old: buffer to insert after
2441 * @newsk: buffer to insert
2442 * @list: list to use
2444 * Place a packet after a given packet in a list. The list locks are taken
2445 * and this function is atomic with respect to other list locked calls.
2446 * A buffer cannot be placed on two lists at the same time.
2448 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2450 unsigned long flags
;
2452 spin_lock_irqsave(&list
->lock
, flags
);
2453 __skb_queue_after(list
, old
, newsk
);
2454 spin_unlock_irqrestore(&list
->lock
, flags
);
2456 EXPORT_SYMBOL(skb_append
);
2459 * skb_insert - insert a buffer
2460 * @old: buffer to insert before
2461 * @newsk: buffer to insert
2462 * @list: list to use
2464 * Place a packet before a given packet in a list. The list locks are
2465 * taken and this function is atomic with respect to other list locked
2468 * A buffer cannot be placed on two lists at the same time.
2470 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2472 unsigned long flags
;
2474 spin_lock_irqsave(&list
->lock
, flags
);
2475 __skb_insert(newsk
, old
->prev
, old
, list
);
2476 spin_unlock_irqrestore(&list
->lock
, flags
);
2478 EXPORT_SYMBOL(skb_insert
);
2480 static inline void skb_split_inside_header(struct sk_buff
*skb
,
2481 struct sk_buff
* skb1
,
2482 const u32 len
, const int pos
)
2486 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
2488 /* And move data appendix as is. */
2489 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
2490 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
2492 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
2493 skb_shinfo(skb
)->nr_frags
= 0;
2494 skb1
->data_len
= skb
->data_len
;
2495 skb1
->len
+= skb1
->data_len
;
2498 skb_set_tail_pointer(skb
, len
);
2501 static inline void skb_split_no_header(struct sk_buff
*skb
,
2502 struct sk_buff
* skb1
,
2503 const u32 len
, int pos
)
2506 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
2508 skb_shinfo(skb
)->nr_frags
= 0;
2509 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
2511 skb
->data_len
= len
- pos
;
2513 for (i
= 0; i
< nfrags
; i
++) {
2514 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2516 if (pos
+ size
> len
) {
2517 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
2521 * We have two variants in this case:
2522 * 1. Move all the frag to the second
2523 * part, if it is possible. F.e.
2524 * this approach is mandatory for TUX,
2525 * where splitting is expensive.
2526 * 2. Split is accurately. We make this.
2528 skb_frag_ref(skb
, i
);
2529 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
2530 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
2531 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
2532 skb_shinfo(skb
)->nr_frags
++;
2536 skb_shinfo(skb
)->nr_frags
++;
2539 skb_shinfo(skb1
)->nr_frags
= k
;
2543 * skb_split - Split fragmented skb to two parts at length len.
2544 * @skb: the buffer to split
2545 * @skb1: the buffer to receive the second part
2546 * @len: new length for skb
2548 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
2550 int pos
= skb_headlen(skb
);
2552 skb_shinfo(skb1
)->tx_flags
= skb_shinfo(skb
)->tx_flags
& SKBTX_SHARED_FRAG
;
2553 if (len
< pos
) /* Split line is inside header. */
2554 skb_split_inside_header(skb
, skb1
, len
, pos
);
2555 else /* Second chunk has no header, nothing to copy. */
2556 skb_split_no_header(skb
, skb1
, len
, pos
);
2558 EXPORT_SYMBOL(skb_split
);
2560 /* Shifting from/to a cloned skb is a no-go.
2562 * Caller cannot keep skb_shinfo related pointers past calling here!
2564 static int skb_prepare_for_shift(struct sk_buff
*skb
)
2566 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
2570 * skb_shift - Shifts paged data partially from skb to another
2571 * @tgt: buffer into which tail data gets added
2572 * @skb: buffer from which the paged data comes from
2573 * @shiftlen: shift up to this many bytes
2575 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2576 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2577 * It's up to caller to free skb if everything was shifted.
2579 * If @tgt runs out of frags, the whole operation is aborted.
2581 * Skb cannot include anything else but paged data while tgt is allowed
2582 * to have non-paged data as well.
2584 * TODO: full sized shift could be optimized but that would need
2585 * specialized skb free'er to handle frags without up-to-date nr_frags.
2587 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
2589 int from
, to
, merge
, todo
;
2590 struct skb_frag_struct
*fragfrom
, *fragto
;
2592 BUG_ON(shiftlen
> skb
->len
);
2593 BUG_ON(skb_headlen(skb
)); /* Would corrupt stream */
2597 to
= skb_shinfo(tgt
)->nr_frags
;
2598 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2600 /* Actual merge is delayed until the point when we know we can
2601 * commit all, so that we don't have to undo partial changes
2604 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
2605 fragfrom
->page_offset
)) {
2610 todo
-= skb_frag_size(fragfrom
);
2612 if (skb_prepare_for_shift(skb
) ||
2613 skb_prepare_for_shift(tgt
))
2616 /* All previous frag pointers might be stale! */
2617 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2618 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2620 skb_frag_size_add(fragto
, shiftlen
);
2621 skb_frag_size_sub(fragfrom
, shiftlen
);
2622 fragfrom
->page_offset
+= shiftlen
;
2630 /* Skip full, not-fitting skb to avoid expensive operations */
2631 if ((shiftlen
== skb
->len
) &&
2632 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
2635 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
2638 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
2639 if (to
== MAX_SKB_FRAGS
)
2642 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
2643 fragto
= &skb_shinfo(tgt
)->frags
[to
];
2645 if (todo
>= skb_frag_size(fragfrom
)) {
2646 *fragto
= *fragfrom
;
2647 todo
-= skb_frag_size(fragfrom
);
2652 __skb_frag_ref(fragfrom
);
2653 fragto
->page
= fragfrom
->page
;
2654 fragto
->page_offset
= fragfrom
->page_offset
;
2655 skb_frag_size_set(fragto
, todo
);
2657 fragfrom
->page_offset
+= todo
;
2658 skb_frag_size_sub(fragfrom
, todo
);
2666 /* Ready to "commit" this state change to tgt */
2667 skb_shinfo(tgt
)->nr_frags
= to
;
2670 fragfrom
= &skb_shinfo(skb
)->frags
[0];
2671 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
2673 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
2674 __skb_frag_unref(fragfrom
);
2677 /* Reposition in the original skb */
2679 while (from
< skb_shinfo(skb
)->nr_frags
)
2680 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
2681 skb_shinfo(skb
)->nr_frags
= to
;
2683 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
2686 /* Most likely the tgt won't ever need its checksum anymore, skb on
2687 * the other hand might need it if it needs to be resent
2689 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
2690 skb
->ip_summed
= CHECKSUM_PARTIAL
;
2692 /* Yak, is it really working this way? Some helper please? */
2693 skb
->len
-= shiftlen
;
2694 skb
->data_len
-= shiftlen
;
2695 skb
->truesize
-= shiftlen
;
2696 tgt
->len
+= shiftlen
;
2697 tgt
->data_len
+= shiftlen
;
2698 tgt
->truesize
+= shiftlen
;
2704 * skb_prepare_seq_read - Prepare a sequential read of skb data
2705 * @skb: the buffer to read
2706 * @from: lower offset of data to be read
2707 * @to: upper offset of data to be read
2708 * @st: state variable
2710 * Initializes the specified state variable. Must be called before
2711 * invoking skb_seq_read() for the first time.
2713 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
2714 unsigned int to
, struct skb_seq_state
*st
)
2716 st
->lower_offset
= from
;
2717 st
->upper_offset
= to
;
2718 st
->root_skb
= st
->cur_skb
= skb
;
2719 st
->frag_idx
= st
->stepped_offset
= 0;
2720 st
->frag_data
= NULL
;
2722 EXPORT_SYMBOL(skb_prepare_seq_read
);
2725 * skb_seq_read - Sequentially read skb data
2726 * @consumed: number of bytes consumed by the caller so far
2727 * @data: destination pointer for data to be returned
2728 * @st: state variable
2730 * Reads a block of skb data at @consumed relative to the
2731 * lower offset specified to skb_prepare_seq_read(). Assigns
2732 * the head of the data block to @data and returns the length
2733 * of the block or 0 if the end of the skb data or the upper
2734 * offset has been reached.
2736 * The caller is not required to consume all of the data
2737 * returned, i.e. @consumed is typically set to the number
2738 * of bytes already consumed and the next call to
2739 * skb_seq_read() will return the remaining part of the block.
2741 * Note 1: The size of each block of data returned can be arbitrary,
2742 * this limitation is the cost for zerocopy sequential
2743 * reads of potentially non linear data.
2745 * Note 2: Fragment lists within fragments are not implemented
2746 * at the moment, state->root_skb could be replaced with
2747 * a stack for this purpose.
2749 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
2750 struct skb_seq_state
*st
)
2752 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
2755 if (unlikely(abs_offset
>= st
->upper_offset
)) {
2756 if (st
->frag_data
) {
2757 kunmap_atomic(st
->frag_data
);
2758 st
->frag_data
= NULL
;
2764 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
2766 if (abs_offset
< block_limit
&& !st
->frag_data
) {
2767 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
2768 return block_limit
- abs_offset
;
2771 if (st
->frag_idx
== 0 && !st
->frag_data
)
2772 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
2774 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
2775 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
2776 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
2778 if (abs_offset
< block_limit
) {
2780 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
2782 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
2783 (abs_offset
- st
->stepped_offset
);
2785 return block_limit
- abs_offset
;
2788 if (st
->frag_data
) {
2789 kunmap_atomic(st
->frag_data
);
2790 st
->frag_data
= NULL
;
2794 st
->stepped_offset
+= skb_frag_size(frag
);
2797 if (st
->frag_data
) {
2798 kunmap_atomic(st
->frag_data
);
2799 st
->frag_data
= NULL
;
2802 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
2803 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2806 } else if (st
->cur_skb
->next
) {
2807 st
->cur_skb
= st
->cur_skb
->next
;
2814 EXPORT_SYMBOL(skb_seq_read
);
2817 * skb_abort_seq_read - Abort a sequential read of skb data
2818 * @st: state variable
2820 * Must be called if skb_seq_read() was not called until it
2823 void skb_abort_seq_read(struct skb_seq_state
*st
)
2826 kunmap_atomic(st
->frag_data
);
2828 EXPORT_SYMBOL(skb_abort_seq_read
);
2830 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2832 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2833 struct ts_config
*conf
,
2834 struct ts_state
*state
)
2836 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2839 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2841 skb_abort_seq_read(TS_SKB_CB(state
));
2845 * skb_find_text - Find a text pattern in skb data
2846 * @skb: the buffer to look in
2847 * @from: search offset
2849 * @config: textsearch configuration
2851 * Finds a pattern in the skb data according to the specified
2852 * textsearch configuration. Use textsearch_next() to retrieve
2853 * subsequent occurrences of the pattern. Returns the offset
2854 * to the first occurrence or UINT_MAX if no match was found.
2856 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2857 unsigned int to
, struct ts_config
*config
)
2859 struct ts_state state
;
2862 config
->get_next_block
= skb_ts_get_next_block
;
2863 config
->finish
= skb_ts_finish
;
2865 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(&state
));
2867 ret
= textsearch_find(config
, &state
);
2868 return (ret
<= to
- from
? ret
: UINT_MAX
);
2870 EXPORT_SYMBOL(skb_find_text
);
2873 * skb_append_datato_frags - append the user data to a skb
2874 * @sk: sock structure
2875 * @skb: skb structure to be appended with user data.
2876 * @getfrag: call back function to be used for getting the user data
2877 * @from: pointer to user message iov
2878 * @length: length of the iov message
2880 * Description: This procedure append the user data in the fragment part
2881 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2883 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2884 int (*getfrag
)(void *from
, char *to
, int offset
,
2885 int len
, int odd
, struct sk_buff
*skb
),
2886 void *from
, int length
)
2888 int frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2892 struct page_frag
*pfrag
= ¤t
->task_frag
;
2895 /* Return error if we don't have space for new frag */
2896 if (frg_cnt
>= MAX_SKB_FRAGS
)
2899 if (!sk_page_frag_refill(sk
, pfrag
))
2902 /* copy the user data to page */
2903 copy
= min_t(int, length
, pfrag
->size
- pfrag
->offset
);
2905 ret
= getfrag(from
, page_address(pfrag
->page
) + pfrag
->offset
,
2906 offset
, copy
, 0, skb
);
2910 /* copy was successful so update the size parameters */
2911 skb_fill_page_desc(skb
, frg_cnt
, pfrag
->page
, pfrag
->offset
,
2914 pfrag
->offset
+= copy
;
2915 get_page(pfrag
->page
);
2917 skb
->truesize
+= copy
;
2918 atomic_add(copy
, &sk
->sk_wmem_alloc
);
2920 skb
->data_len
+= copy
;
2924 } while (length
> 0);
2928 EXPORT_SYMBOL(skb_append_datato_frags
);
2930 int skb_append_pagefrags(struct sk_buff
*skb
, struct page
*page
,
2931 int offset
, size_t size
)
2933 int i
= skb_shinfo(skb
)->nr_frags
;
2935 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
2936 skb_frag_size_add(&skb_shinfo(skb
)->frags
[i
- 1], size
);
2937 } else if (i
< MAX_SKB_FRAGS
) {
2939 skb_fill_page_desc(skb
, i
, page
, offset
, size
);
2946 EXPORT_SYMBOL_GPL(skb_append_pagefrags
);
2949 * skb_pull_rcsum - pull skb and update receive checksum
2950 * @skb: buffer to update
2951 * @len: length of data pulled
2953 * This function performs an skb_pull on the packet and updates
2954 * the CHECKSUM_COMPLETE checksum. It should be used on
2955 * receive path processing instead of skb_pull unless you know
2956 * that the checksum difference is zero (e.g., a valid IP header)
2957 * or you are setting ip_summed to CHECKSUM_NONE.
2959 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2961 BUG_ON(len
> skb
->len
);
2963 BUG_ON(skb
->len
< skb
->data_len
);
2964 skb_postpull_rcsum(skb
, skb
->data
, len
);
2965 return skb
->data
+= len
;
2967 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2970 * skb_segment - Perform protocol segmentation on skb.
2971 * @head_skb: buffer to segment
2972 * @features: features for the output path (see dev->features)
2974 * This function performs segmentation on the given skb. It returns
2975 * a pointer to the first in a list of new skbs for the segments.
2976 * In case of error it returns ERR_PTR(err).
2978 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
2979 netdev_features_t features
)
2981 struct sk_buff
*segs
= NULL
;
2982 struct sk_buff
*tail
= NULL
;
2983 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
2984 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
2985 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
2986 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
2987 struct sk_buff
*frag_skb
= head_skb
;
2988 unsigned int offset
= doffset
;
2989 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
2990 unsigned int headroom
;
2994 int sg
= !!(features
& NETIF_F_SG
);
2995 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
3001 __skb_push(head_skb
, doffset
);
3002 proto
= skb_network_protocol(head_skb
, &dummy
);
3003 if (unlikely(!proto
))
3004 return ERR_PTR(-EINVAL
);
3006 csum
= !head_skb
->encap_hdr_csum
&&
3007 !!can_checksum_protocol(features
, proto
);
3009 headroom
= skb_headroom(head_skb
);
3010 pos
= skb_headlen(head_skb
);
3013 struct sk_buff
*nskb
;
3014 skb_frag_t
*nskb_frag
;
3018 len
= head_skb
->len
- offset
;
3022 hsize
= skb_headlen(head_skb
) - offset
;
3025 if (hsize
> len
|| !sg
)
3028 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
3029 (skb_headlen(list_skb
) == len
|| sg
)) {
3030 BUG_ON(skb_headlen(list_skb
) > len
);
3033 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3034 frag
= skb_shinfo(list_skb
)->frags
;
3035 frag_skb
= list_skb
;
3036 pos
+= skb_headlen(list_skb
);
3038 while (pos
< offset
+ len
) {
3039 BUG_ON(i
>= nfrags
);
3041 size
= skb_frag_size(frag
);
3042 if (pos
+ size
> offset
+ len
)
3050 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
3051 list_skb
= list_skb
->next
;
3053 if (unlikely(!nskb
))
3056 if (unlikely(pskb_trim(nskb
, len
))) {
3061 hsize
= skb_end_offset(nskb
);
3062 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
3067 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
3068 skb_release_head_state(nskb
);
3069 __skb_push(nskb
, doffset
);
3071 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
3072 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
3075 if (unlikely(!nskb
))
3078 skb_reserve(nskb
, headroom
);
3079 __skb_put(nskb
, doffset
);
3088 __copy_skb_header(nskb
, head_skb
);
3090 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
3091 skb_reset_mac_len(nskb
);
3093 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
3094 nskb
->data
- tnl_hlen
,
3095 doffset
+ tnl_hlen
);
3097 if (nskb
->len
== len
+ doffset
)
3098 goto perform_csum_check
;
3100 if (!sg
&& !nskb
->remcsum_offload
) {
3101 nskb
->ip_summed
= CHECKSUM_NONE
;
3102 nskb
->csum
= skb_copy_and_csum_bits(head_skb
, offset
,
3105 SKB_GSO_CB(nskb
)->csum_start
=
3106 skb_headroom(nskb
) + doffset
;
3110 nskb_frag
= skb_shinfo(nskb
)->frags
;
3112 skb_copy_from_linear_data_offset(head_skb
, offset
,
3113 skb_put(nskb
, hsize
), hsize
);
3115 skb_shinfo(nskb
)->tx_flags
= skb_shinfo(head_skb
)->tx_flags
&
3118 while (pos
< offset
+ len
) {
3120 BUG_ON(skb_headlen(list_skb
));
3123 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3124 frag
= skb_shinfo(list_skb
)->frags
;
3125 frag_skb
= list_skb
;
3129 list_skb
= list_skb
->next
;
3132 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3134 net_warn_ratelimited(
3135 "skb_segment: too many frags: %u %u\n",
3140 if (unlikely(skb_orphan_frags(frag_skb
, GFP_ATOMIC
)))
3144 __skb_frag_ref(nskb_frag
);
3145 size
= skb_frag_size(nskb_frag
);
3148 nskb_frag
->page_offset
+= offset
- pos
;
3149 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3152 skb_shinfo(nskb
)->nr_frags
++;
3154 if (pos
+ size
<= offset
+ len
) {
3159 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3167 nskb
->data_len
= len
- hsize
;
3168 nskb
->len
+= nskb
->data_len
;
3169 nskb
->truesize
+= nskb
->data_len
;
3172 if (!csum
&& !nskb
->remcsum_offload
) {
3173 nskb
->csum
= skb_checksum(nskb
, doffset
,
3174 nskb
->len
- doffset
, 0);
3175 nskb
->ip_summed
= CHECKSUM_NONE
;
3176 SKB_GSO_CB(nskb
)->csum_start
=
3177 skb_headroom(nskb
) + doffset
;
3179 } while ((offset
+= len
) < head_skb
->len
);
3181 /* Some callers want to get the end of the list.
3182 * Put it in segs->prev to avoid walking the list.
3183 * (see validate_xmit_skb_list() for example)
3187 /* Following permits correct backpressure, for protocols
3188 * using skb_set_owner_w().
3189 * Idea is to tranfert ownership from head_skb to last segment.
3191 if (head_skb
->destructor
== sock_wfree
) {
3192 swap(tail
->truesize
, head_skb
->truesize
);
3193 swap(tail
->destructor
, head_skb
->destructor
);
3194 swap(tail
->sk
, head_skb
->sk
);
3199 kfree_skb_list(segs
);
3200 return ERR_PTR(err
);
3202 EXPORT_SYMBOL_GPL(skb_segment
);
3204 int skb_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
)
3206 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3207 unsigned int offset
= skb_gro_offset(skb
);
3208 unsigned int headlen
= skb_headlen(skb
);
3209 unsigned int len
= skb_gro_len(skb
);
3210 struct sk_buff
*lp
, *p
= *head
;
3211 unsigned int delta_truesize
;
3213 if (unlikely(p
->len
+ len
>= 65536))
3216 lp
= NAPI_GRO_CB(p
)->last
;
3217 pinfo
= skb_shinfo(lp
);
3219 if (headlen
<= offset
) {
3222 int i
= skbinfo
->nr_frags
;
3223 int nr_frags
= pinfo
->nr_frags
+ i
;
3225 if (nr_frags
> MAX_SKB_FRAGS
)
3229 pinfo
->nr_frags
= nr_frags
;
3230 skbinfo
->nr_frags
= 0;
3232 frag
= pinfo
->frags
+ nr_frags
;
3233 frag2
= skbinfo
->frags
+ i
;
3238 frag
->page_offset
+= offset
;
3239 skb_frag_size_sub(frag
, offset
);
3241 /* all fragments truesize : remove (head size + sk_buff) */
3242 delta_truesize
= skb
->truesize
-
3243 SKB_TRUESIZE(skb_end_offset(skb
));
3245 skb
->truesize
-= skb
->data_len
;
3246 skb
->len
-= skb
->data_len
;
3249 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3251 } else if (skb
->head_frag
) {
3252 int nr_frags
= pinfo
->nr_frags
;
3253 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3254 struct page
*page
= virt_to_head_page(skb
->head
);
3255 unsigned int first_size
= headlen
- offset
;
3256 unsigned int first_offset
;
3258 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3261 first_offset
= skb
->data
-
3262 (unsigned char *)page_address(page
) +
3265 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3267 frag
->page
.p
= page
;
3268 frag
->page_offset
= first_offset
;
3269 skb_frag_size_set(frag
, first_size
);
3271 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3272 /* We dont need to clear skbinfo->nr_frags here */
3274 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3275 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3280 delta_truesize
= skb
->truesize
;
3281 if (offset
> headlen
) {
3282 unsigned int eat
= offset
- headlen
;
3284 skbinfo
->frags
[0].page_offset
+= eat
;
3285 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3286 skb
->data_len
-= eat
;
3291 __skb_pull(skb
, offset
);
3293 if (NAPI_GRO_CB(p
)->last
== p
)
3294 skb_shinfo(p
)->frag_list
= skb
;
3296 NAPI_GRO_CB(p
)->last
->next
= skb
;
3297 NAPI_GRO_CB(p
)->last
= skb
;
3298 __skb_header_release(skb
);
3302 NAPI_GRO_CB(p
)->count
++;
3304 p
->truesize
+= delta_truesize
;
3307 lp
->data_len
+= len
;
3308 lp
->truesize
+= delta_truesize
;
3311 NAPI_GRO_CB(skb
)->same_flow
= 1;
3315 void __init
skb_init(void)
3317 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
3318 sizeof(struct sk_buff
),
3320 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3322 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3323 sizeof(struct sk_buff_fclones
),
3325 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3330 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3331 * @skb: Socket buffer containing the buffers to be mapped
3332 * @sg: The scatter-gather list to map into
3333 * @offset: The offset into the buffer's contents to start mapping
3334 * @len: Length of buffer space to be mapped
3336 * Fill the specified scatter-gather list with mappings/pointers into a
3337 * region of the buffer space attached to a socket buffer.
3340 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3342 int start
= skb_headlen(skb
);
3343 int i
, copy
= start
- offset
;
3344 struct sk_buff
*frag_iter
;
3350 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
3352 if ((len
-= copy
) == 0)
3357 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3360 WARN_ON(start
> offset
+ len
);
3362 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3363 if ((copy
= end
- offset
) > 0) {
3364 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
3368 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
3369 frag
->page_offset
+offset
-start
);
3378 skb_walk_frags(skb
, frag_iter
) {
3381 WARN_ON(start
> offset
+ len
);
3383 end
= start
+ frag_iter
->len
;
3384 if ((copy
= end
- offset
) > 0) {
3387 elt
+= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
3389 if ((len
-= copy
) == 0)
3399 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3400 * sglist without mark the sg which contain last skb data as the end.
3401 * So the caller can mannipulate sg list as will when padding new data after
3402 * the first call without calling sg_unmark_end to expend sg list.
3404 * Scenario to use skb_to_sgvec_nomark:
3406 * 2. skb_to_sgvec_nomark(payload1)
3407 * 3. skb_to_sgvec_nomark(payload2)
3409 * This is equivalent to:
3411 * 2. skb_to_sgvec(payload1)
3413 * 4. skb_to_sgvec(payload2)
3415 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3416 * is more preferable.
3418 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
3419 int offset
, int len
)
3421 return __skb_to_sgvec(skb
, sg
, offset
, len
);
3423 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
3425 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
3427 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
3429 sg_mark_end(&sg
[nsg
- 1]);
3433 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
3436 * skb_cow_data - Check that a socket buffer's data buffers are writable
3437 * @skb: The socket buffer to check.
3438 * @tailbits: Amount of trailing space to be added
3439 * @trailer: Returned pointer to the skb where the @tailbits space begins
3441 * Make sure that the data buffers attached to a socket buffer are
3442 * writable. If they are not, private copies are made of the data buffers
3443 * and the socket buffer is set to use these instead.
3445 * If @tailbits is given, make sure that there is space to write @tailbits
3446 * bytes of data beyond current end of socket buffer. @trailer will be
3447 * set to point to the skb in which this space begins.
3449 * The number of scatterlist elements required to completely map the
3450 * COW'd and extended socket buffer will be returned.
3452 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
3456 struct sk_buff
*skb1
, **skb_p
;
3458 /* If skb is cloned or its head is paged, reallocate
3459 * head pulling out all the pages (pages are considered not writable
3460 * at the moment even if they are anonymous).
3462 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
3463 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
3466 /* Easy case. Most of packets will go this way. */
3467 if (!skb_has_frag_list(skb
)) {
3468 /* A little of trouble, not enough of space for trailer.
3469 * This should not happen, when stack is tuned to generate
3470 * good frames. OK, on miss we reallocate and reserve even more
3471 * space, 128 bytes is fair. */
3473 if (skb_tailroom(skb
) < tailbits
&&
3474 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
3482 /* Misery. We are in troubles, going to mincer fragments... */
3485 skb_p
= &skb_shinfo(skb
)->frag_list
;
3488 while ((skb1
= *skb_p
) != NULL
) {
3491 /* The fragment is partially pulled by someone,
3492 * this can happen on input. Copy it and everything
3495 if (skb_shared(skb1
))
3498 /* If the skb is the last, worry about trailer. */
3500 if (skb1
->next
== NULL
&& tailbits
) {
3501 if (skb_shinfo(skb1
)->nr_frags
||
3502 skb_has_frag_list(skb1
) ||
3503 skb_tailroom(skb1
) < tailbits
)
3504 ntail
= tailbits
+ 128;
3510 skb_shinfo(skb1
)->nr_frags
||
3511 skb_has_frag_list(skb1
)) {
3512 struct sk_buff
*skb2
;
3514 /* Fuck, we are miserable poor guys... */
3516 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
3518 skb2
= skb_copy_expand(skb1
,
3522 if (unlikely(skb2
== NULL
))
3526 skb_set_owner_w(skb2
, skb1
->sk
);
3528 /* Looking around. Are we still alive?
3529 * OK, link new skb, drop old one */
3531 skb2
->next
= skb1
->next
;
3538 skb_p
= &skb1
->next
;
3543 EXPORT_SYMBOL_GPL(skb_cow_data
);
3545 static void sock_rmem_free(struct sk_buff
*skb
)
3547 struct sock
*sk
= skb
->sk
;
3549 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
3553 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3555 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
3557 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
3558 (unsigned int)sk
->sk_rcvbuf
)
3563 skb
->destructor
= sock_rmem_free
;
3564 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
3566 /* before exiting rcu section, make sure dst is refcounted */
3569 skb_queue_tail(&sk
->sk_error_queue
, skb
);
3570 if (!sock_flag(sk
, SOCK_DEAD
))
3571 sk
->sk_data_ready(sk
);
3574 EXPORT_SYMBOL(sock_queue_err_skb
);
3576 struct sk_buff
*sock_dequeue_err_skb(struct sock
*sk
)
3578 struct sk_buff_head
*q
= &sk
->sk_error_queue
;
3579 struct sk_buff
*skb
, *skb_next
;
3580 unsigned long flags
;
3583 spin_lock_irqsave(&q
->lock
, flags
);
3584 skb
= __skb_dequeue(q
);
3585 if (skb
&& (skb_next
= skb_peek(q
)))
3586 err
= SKB_EXT_ERR(skb_next
)->ee
.ee_errno
;
3587 spin_unlock_irqrestore(&q
->lock
, flags
);
3591 sk
->sk_error_report(sk
);
3595 EXPORT_SYMBOL(sock_dequeue_err_skb
);
3598 * skb_clone_sk - create clone of skb, and take reference to socket
3599 * @skb: the skb to clone
3601 * This function creates a clone of a buffer that holds a reference on
3602 * sk_refcnt. Buffers created via this function are meant to be
3603 * returned using sock_queue_err_skb, or free via kfree_skb.
3605 * When passing buffers allocated with this function to sock_queue_err_skb
3606 * it is necessary to wrap the call with sock_hold/sock_put in order to
3607 * prevent the socket from being released prior to being enqueued on
3608 * the sk_error_queue.
3610 struct sk_buff
*skb_clone_sk(struct sk_buff
*skb
)
3612 struct sock
*sk
= skb
->sk
;
3613 struct sk_buff
*clone
;
3615 if (!sk
|| !atomic_inc_not_zero(&sk
->sk_refcnt
))
3618 clone
= skb_clone(skb
, GFP_ATOMIC
);
3625 clone
->destructor
= sock_efree
;
3629 EXPORT_SYMBOL(skb_clone_sk
);
3631 static void __skb_complete_tx_timestamp(struct sk_buff
*skb
,
3635 struct sock_exterr_skb
*serr
;
3638 serr
= SKB_EXT_ERR(skb
);
3639 memset(serr
, 0, sizeof(*serr
));
3640 serr
->ee
.ee_errno
= ENOMSG
;
3641 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
3642 serr
->ee
.ee_info
= tstype
;
3643 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
3644 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
3645 if (sk
->sk_protocol
== IPPROTO_TCP
)
3646 serr
->ee
.ee_data
-= sk
->sk_tskey
;
3649 err
= sock_queue_err_skb(sk
, skb
);
3655 static bool skb_may_tx_timestamp(struct sock
*sk
, bool tsonly
)
3659 if (likely(sysctl_tstamp_allow_data
|| tsonly
))
3662 read_lock_bh(&sk
->sk_callback_lock
);
3663 ret
= sk
->sk_socket
&& sk
->sk_socket
->file
&&
3664 file_ns_capable(sk
->sk_socket
->file
, &init_user_ns
, CAP_NET_RAW
);
3665 read_unlock_bh(&sk
->sk_callback_lock
);
3669 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
3670 struct skb_shared_hwtstamps
*hwtstamps
)
3672 struct sock
*sk
= skb
->sk
;
3674 if (!skb_may_tx_timestamp(sk
, false))
3677 /* take a reference to prevent skb_orphan() from freeing the socket */
3680 *skb_hwtstamps(skb
) = *hwtstamps
;
3681 __skb_complete_tx_timestamp(skb
, sk
, SCM_TSTAMP_SND
);
3685 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp
);
3687 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
3688 struct skb_shared_hwtstamps
*hwtstamps
,
3689 struct sock
*sk
, int tstype
)
3691 struct sk_buff
*skb
;
3697 tsonly
= sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TSONLY
;
3698 if (!skb_may_tx_timestamp(sk
, tsonly
))
3702 skb
= alloc_skb(0, GFP_ATOMIC
);
3704 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
3709 skb_shinfo(skb
)->tx_flags
= skb_shinfo(orig_skb
)->tx_flags
;
3710 skb_shinfo(skb
)->tskey
= skb_shinfo(orig_skb
)->tskey
;
3714 *skb_hwtstamps(skb
) = *hwtstamps
;
3716 skb
->tstamp
= ktime_get_real();
3718 __skb_complete_tx_timestamp(skb
, sk
, tstype
);
3720 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
3722 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
3723 struct skb_shared_hwtstamps
*hwtstamps
)
3725 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
3728 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
3730 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
3732 struct sock
*sk
= skb
->sk
;
3733 struct sock_exterr_skb
*serr
;
3736 skb
->wifi_acked_valid
= 1;
3737 skb
->wifi_acked
= acked
;
3739 serr
= SKB_EXT_ERR(skb
);
3740 memset(serr
, 0, sizeof(*serr
));
3741 serr
->ee
.ee_errno
= ENOMSG
;
3742 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
3744 /* take a reference to prevent skb_orphan() from freeing the socket */
3747 err
= sock_queue_err_skb(sk
, skb
);
3753 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
3756 * skb_partial_csum_set - set up and verify partial csum values for packet
3757 * @skb: the skb to set
3758 * @start: the number of bytes after skb->data to start checksumming.
3759 * @off: the offset from start to place the checksum.
3761 * For untrusted partially-checksummed packets, we need to make sure the values
3762 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3764 * This function checks and sets those values and skb->ip_summed: if this
3765 * returns false you should drop the packet.
3767 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
3769 if (unlikely(start
> skb_headlen(skb
)) ||
3770 unlikely((int)start
+ off
> skb_headlen(skb
) - 2)) {
3771 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3772 start
, off
, skb_headlen(skb
));
3775 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3776 skb
->csum_start
= skb_headroom(skb
) + start
;
3777 skb
->csum_offset
= off
;
3778 skb_set_transport_header(skb
, start
);
3781 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
3783 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
3786 if (skb_headlen(skb
) >= len
)
3789 /* If we need to pullup then pullup to the max, so we
3790 * won't need to do it again.
3795 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
3798 if (skb_headlen(skb
) < len
)
3804 #define MAX_TCP_HDR_LEN (15 * 4)
3806 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
3807 typeof(IPPROTO_IP
) proto
,
3814 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
3815 off
+ MAX_TCP_HDR_LEN
);
3816 if (!err
&& !skb_partial_csum_set(skb
, off
,
3817 offsetof(struct tcphdr
,
3820 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
3823 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
3824 off
+ sizeof(struct udphdr
));
3825 if (!err
&& !skb_partial_csum_set(skb
, off
,
3826 offsetof(struct udphdr
,
3829 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
3832 return ERR_PTR(-EPROTO
);
3835 /* This value should be large enough to cover a tagged ethernet header plus
3836 * maximally sized IP and TCP or UDP headers.
3838 #define MAX_IP_HDR_LEN 128
3840 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
3849 err
= skb_maybe_pull_tail(skb
,
3850 sizeof(struct iphdr
),
3855 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
3858 off
= ip_hdrlen(skb
);
3865 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
3867 return PTR_ERR(csum
);
3870 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
3873 ip_hdr(skb
)->protocol
, 0);
3880 /* This value should be large enough to cover a tagged ethernet header plus
3881 * an IPv6 header, all options, and a maximal TCP or UDP header.
3883 #define MAX_IPV6_HDR_LEN 256
3885 #define OPT_HDR(type, skb, off) \
3886 (type *)(skb_network_header(skb) + (off))
3888 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
3901 off
= sizeof(struct ipv6hdr
);
3903 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
3907 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
3909 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
3910 while (off
<= len
&& !done
) {
3912 case IPPROTO_DSTOPTS
:
3913 case IPPROTO_HOPOPTS
:
3914 case IPPROTO_ROUTING
: {
3915 struct ipv6_opt_hdr
*hp
;
3917 err
= skb_maybe_pull_tail(skb
,
3919 sizeof(struct ipv6_opt_hdr
),
3924 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
3925 nexthdr
= hp
->nexthdr
;
3926 off
+= ipv6_optlen(hp
);
3930 struct ip_auth_hdr
*hp
;
3932 err
= skb_maybe_pull_tail(skb
,
3934 sizeof(struct ip_auth_hdr
),
3939 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
3940 nexthdr
= hp
->nexthdr
;
3941 off
+= ipv6_authlen(hp
);
3944 case IPPROTO_FRAGMENT
: {
3945 struct frag_hdr
*hp
;
3947 err
= skb_maybe_pull_tail(skb
,
3949 sizeof(struct frag_hdr
),
3954 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
3956 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
3959 nexthdr
= hp
->nexthdr
;
3960 off
+= sizeof(struct frag_hdr
);
3971 if (!done
|| fragment
)
3974 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
3976 return PTR_ERR(csum
);
3979 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
3980 &ipv6_hdr(skb
)->daddr
,
3981 skb
->len
- off
, nexthdr
, 0);
3989 * skb_checksum_setup - set up partial checksum offset
3990 * @skb: the skb to set up
3991 * @recalculate: if true the pseudo-header checksum will be recalculated
3993 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
3997 switch (skb
->protocol
) {
3998 case htons(ETH_P_IP
):
3999 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
4002 case htons(ETH_P_IPV6
):
4003 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
4013 EXPORT_SYMBOL(skb_checksum_setup
);
4016 * skb_checksum_maybe_trim - maybe trims the given skb
4017 * @skb: the skb to check
4018 * @transport_len: the data length beyond the network header
4020 * Checks whether the given skb has data beyond the given transport length.
4021 * If so, returns a cloned skb trimmed to this transport length.
4022 * Otherwise returns the provided skb. Returns NULL in error cases
4023 * (e.g. transport_len exceeds skb length or out-of-memory).
4025 * Caller needs to set the skb transport header and free any returned skb if it
4026 * differs from the provided skb.
4028 static struct sk_buff
*skb_checksum_maybe_trim(struct sk_buff
*skb
,
4029 unsigned int transport_len
)
4031 struct sk_buff
*skb_chk
;
4032 unsigned int len
= skb_transport_offset(skb
) + transport_len
;
4037 else if (skb
->len
== len
)
4040 skb_chk
= skb_clone(skb
, GFP_ATOMIC
);
4044 ret
= pskb_trim_rcsum(skb_chk
, len
);
4054 * skb_checksum_trimmed - validate checksum of an skb
4055 * @skb: the skb to check
4056 * @transport_len: the data length beyond the network header
4057 * @skb_chkf: checksum function to use
4059 * Applies the given checksum function skb_chkf to the provided skb.
4060 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4062 * If the skb has data beyond the given transport length, then a
4063 * trimmed & cloned skb is checked and returned.
4065 * Caller needs to set the skb transport header and free any returned skb if it
4066 * differs from the provided skb.
4068 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
4069 unsigned int transport_len
,
4070 __sum16(*skb_chkf
)(struct sk_buff
*skb
))
4072 struct sk_buff
*skb_chk
;
4073 unsigned int offset
= skb_transport_offset(skb
);
4076 skb_chk
= skb_checksum_maybe_trim(skb
, transport_len
);
4080 if (!pskb_may_pull(skb_chk
, offset
))
4083 __skb_pull(skb_chk
, offset
);
4084 ret
= skb_chkf(skb_chk
);
4085 __skb_push(skb_chk
, offset
);
4093 if (skb_chk
&& skb_chk
!= skb
)
4099 EXPORT_SYMBOL(skb_checksum_trimmed
);
4101 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
4103 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4106 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
4108 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
4111 skb_release_head_state(skb
);
4112 kmem_cache_free(skbuff_head_cache
, skb
);
4117 EXPORT_SYMBOL(kfree_skb_partial
);
4120 * skb_try_coalesce - try to merge skb to prior one
4122 * @from: buffer to add
4123 * @fragstolen: pointer to boolean
4124 * @delta_truesize: how much more was allocated than was requested
4126 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
4127 bool *fragstolen
, int *delta_truesize
)
4129 int i
, delta
, len
= from
->len
;
4131 *fragstolen
= false;
4136 if (len
<= skb_tailroom(to
)) {
4138 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
4139 *delta_truesize
= 0;
4143 if (skb_has_frag_list(to
) || skb_has_frag_list(from
))
4146 if (skb_headlen(from
) != 0) {
4148 unsigned int offset
;
4150 if (skb_shinfo(to
)->nr_frags
+
4151 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
4154 if (skb_head_is_locked(from
))
4157 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
4159 page
= virt_to_head_page(from
->head
);
4160 offset
= from
->data
- (unsigned char *)page_address(page
);
4162 skb_fill_page_desc(to
, skb_shinfo(to
)->nr_frags
,
4163 page
, offset
, skb_headlen(from
));
4166 if (skb_shinfo(to
)->nr_frags
+
4167 skb_shinfo(from
)->nr_frags
> MAX_SKB_FRAGS
)
4170 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
4173 WARN_ON_ONCE(delta
< len
);
4175 memcpy(skb_shinfo(to
)->frags
+ skb_shinfo(to
)->nr_frags
,
4176 skb_shinfo(from
)->frags
,
4177 skb_shinfo(from
)->nr_frags
* sizeof(skb_frag_t
));
4178 skb_shinfo(to
)->nr_frags
+= skb_shinfo(from
)->nr_frags
;
4180 if (!skb_cloned(from
))
4181 skb_shinfo(from
)->nr_frags
= 0;
4183 /* if the skb is not cloned this does nothing
4184 * since we set nr_frags to 0.
4186 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++)
4187 skb_frag_ref(from
, i
);
4189 to
->truesize
+= delta
;
4191 to
->data_len
+= len
;
4193 *delta_truesize
= delta
;
4196 EXPORT_SYMBOL(skb_try_coalesce
);
4199 * skb_scrub_packet - scrub an skb
4201 * @skb: buffer to clean
4202 * @xnet: packet is crossing netns
4204 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4205 * into/from a tunnel. Some information have to be cleared during these
4207 * skb_scrub_packet can also be used to clean a skb before injecting it in
4208 * another namespace (@xnet == true). We have to clear all information in the
4209 * skb that could impact namespace isolation.
4211 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
4213 skb
->tstamp
.tv64
= 0;
4214 skb
->pkt_type
= PACKET_HOST
;
4218 skb_sender_cpu_clear(skb
);
4221 nf_reset_trace(skb
);
4229 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
4232 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4236 * skb_gso_transport_seglen is used to determine the real size of the
4237 * individual segments, including Layer4 headers (TCP/UDP).
4239 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4241 unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
4243 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
4244 unsigned int thlen
= 0;
4246 if (skb
->encapsulation
) {
4247 thlen
= skb_inner_transport_header(skb
) -
4248 skb_transport_header(skb
);
4250 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
4251 thlen
+= inner_tcp_hdrlen(skb
);
4252 } else if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
4253 thlen
= tcp_hdrlen(skb
);
4255 /* UFO sets gso_size to the size of the fragmentation
4256 * payload, i.e. the size of the L4 (UDP) header is already
4259 return thlen
+ shinfo
->gso_size
;
4261 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen
);
4263 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
4265 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
4270 memmove(skb
->data
- ETH_HLEN
, skb
->data
- VLAN_ETH_HLEN
, 2 * ETH_ALEN
);
4271 skb
->mac_header
+= VLAN_HLEN
;
4275 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
4277 struct vlan_hdr
*vhdr
;
4280 if (unlikely(skb_vlan_tag_present(skb
))) {
4281 /* vlan_tci is already set-up so leave this for another time */
4285 skb
= skb_share_check(skb
, GFP_ATOMIC
);
4289 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
4292 vhdr
= (struct vlan_hdr
*)skb
->data
;
4293 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
4294 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
4296 skb_pull_rcsum(skb
, VLAN_HLEN
);
4297 vlan_set_encap_proto(skb
, vhdr
);
4299 skb
= skb_reorder_vlan_header(skb
);
4303 skb_reset_network_header(skb
);
4304 skb_reset_transport_header(skb
);
4305 skb_reset_mac_len(skb
);
4313 EXPORT_SYMBOL(skb_vlan_untag
);
4315 int skb_ensure_writable(struct sk_buff
*skb
, int write_len
)
4317 if (!pskb_may_pull(skb
, write_len
))
4320 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
4323 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
4325 EXPORT_SYMBOL(skb_ensure_writable
);
4327 /* remove VLAN header from packet and update csum accordingly. */
4328 static int __skb_vlan_pop(struct sk_buff
*skb
, u16
*vlan_tci
)
4330 struct vlan_hdr
*vhdr
;
4331 unsigned int offset
= skb
->data
- skb_mac_header(skb
);
4334 __skb_push(skb
, offset
);
4335 err
= skb_ensure_writable(skb
, VLAN_ETH_HLEN
);
4339 skb_postpull_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
4341 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
4342 *vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
4344 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
4345 __skb_pull(skb
, VLAN_HLEN
);
4347 vlan_set_encap_proto(skb
, vhdr
);
4348 skb
->mac_header
+= VLAN_HLEN
;
4350 if (skb_network_offset(skb
) < ETH_HLEN
)
4351 skb_set_network_header(skb
, ETH_HLEN
);
4353 skb_reset_mac_len(skb
);
4355 __skb_pull(skb
, offset
);
4360 int skb_vlan_pop(struct sk_buff
*skb
)
4366 if (likely(skb_vlan_tag_present(skb
))) {
4369 if (unlikely((skb
->protocol
!= htons(ETH_P_8021Q
) &&
4370 skb
->protocol
!= htons(ETH_P_8021AD
)) ||
4371 skb
->len
< VLAN_ETH_HLEN
))
4374 err
= __skb_vlan_pop(skb
, &vlan_tci
);
4378 /* move next vlan tag to hw accel tag */
4379 if (likely((skb
->protocol
!= htons(ETH_P_8021Q
) &&
4380 skb
->protocol
!= htons(ETH_P_8021AD
)) ||
4381 skb
->len
< VLAN_ETH_HLEN
))
4384 vlan_proto
= skb
->protocol
;
4385 err
= __skb_vlan_pop(skb
, &vlan_tci
);
4389 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
4392 EXPORT_SYMBOL(skb_vlan_pop
);
4394 int skb_vlan_push(struct sk_buff
*skb
, __be16 vlan_proto
, u16 vlan_tci
)
4396 if (skb_vlan_tag_present(skb
)) {
4397 unsigned int offset
= skb
->data
- skb_mac_header(skb
);
4400 /* __vlan_insert_tag expect skb->data pointing to mac header.
4401 * So change skb->data before calling it and change back to
4402 * original position later
4404 __skb_push(skb
, offset
);
4405 err
= __vlan_insert_tag(skb
, skb
->vlan_proto
,
4406 skb_vlan_tag_get(skb
));
4409 skb
->protocol
= skb
->vlan_proto
;
4410 skb
->mac_len
+= VLAN_HLEN
;
4411 __skb_pull(skb
, offset
);
4413 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
4414 skb
->csum
= csum_add(skb
->csum
, csum_partial(skb
->data
4415 + (2 * ETH_ALEN
), VLAN_HLEN
, 0));
4417 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
4420 EXPORT_SYMBOL(skb_vlan_push
);
4423 * alloc_skb_with_frags - allocate skb with page frags
4425 * @header_len: size of linear part
4426 * @data_len: needed length in frags
4427 * @max_page_order: max page order desired.
4428 * @errcode: pointer to error code if any
4429 * @gfp_mask: allocation mask
4431 * This can be used to allocate a paged skb, given a maximal order for frags.
4433 struct sk_buff
*alloc_skb_with_frags(unsigned long header_len
,
4434 unsigned long data_len
,
4439 int npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
4440 unsigned long chunk
;
4441 struct sk_buff
*skb
;
4446 *errcode
= -EMSGSIZE
;
4447 /* Note this test could be relaxed, if we succeed to allocate
4448 * high order pages...
4450 if (npages
> MAX_SKB_FRAGS
)
4453 gfp_head
= gfp_mask
;
4454 if (gfp_head
& __GFP_WAIT
)
4455 gfp_head
|= __GFP_REPEAT
;
4457 *errcode
= -ENOBUFS
;
4458 skb
= alloc_skb(header_len
, gfp_head
);
4462 skb
->truesize
+= npages
<< PAGE_SHIFT
;
4464 for (i
= 0; npages
> 0; i
++) {
4465 int order
= max_page_order
;
4468 if (npages
>= 1 << order
) {
4469 page
= alloc_pages((gfp_mask
& ~__GFP_WAIT
) |
4476 /* Do not retry other high order allocations */
4482 page
= alloc_page(gfp_mask
);
4486 chunk
= min_t(unsigned long, data_len
,
4487 PAGE_SIZE
<< order
);
4488 skb_fill_page_desc(skb
, i
, page
, 0, chunk
);
4490 npages
-= 1 << order
;
4498 EXPORT_SYMBOL(alloc_skb_with_frags
);