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>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/tcp.h>
50 #include <linux/udp.h>
51 #include <linux/sctp.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 <linux/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 __ro_after_init
;
81 static struct kmem_cache
*skbuff_fclone_cache __ro_after_init
;
82 int sysctl_max_skb_frags __read_mostly
= MAX_SKB_FRAGS
;
83 EXPORT_SYMBOL(sysctl_max_skb_frags
);
86 * skb_panic - private function for out-of-line support
90 * @msg: skb_over_panic or skb_under_panic
92 * Out-of-line support for skb_put() and skb_push().
93 * Called via the wrapper skb_over_panic() or skb_under_panic().
94 * Keep out of line to prevent kernel bloat.
95 * __builtin_return_address is not used because it is not always reliable.
97 static void skb_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
,
100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 msg
, addr
, skb
->len
, sz
, skb
->head
, skb
->data
,
102 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
103 skb
->dev
? skb
->dev
->name
: "<NULL>");
107 static void skb_over_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
109 skb_panic(skb
, sz
, addr
, __func__
);
112 static void skb_under_panic(struct sk_buff
*skb
, unsigned int sz
, void *addr
)
114 skb_panic(skb
, sz
, addr
, __func__
);
118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119 * the caller if emergency pfmemalloc reserves are being used. If it is and
120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121 * may be used. Otherwise, the packet data may be discarded until enough
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
127 static void *__kmalloc_reserve(size_t size
, gfp_t flags
, int node
,
128 unsigned long ip
, bool *pfmemalloc
)
131 bool ret_pfmemalloc
= false;
134 * Try a regular allocation, when that fails and we're not entitled
135 * to the reserves, fail.
137 obj
= kmalloc_node_track_caller(size
,
138 flags
| __GFP_NOMEMALLOC
| __GFP_NOWARN
,
140 if (obj
|| !(gfp_pfmemalloc_allowed(flags
)))
143 /* Try again but now we are using pfmemalloc reserves */
144 ret_pfmemalloc
= true;
145 obj
= kmalloc_node_track_caller(size
, flags
, node
);
149 *pfmemalloc
= ret_pfmemalloc
;
154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
155 * 'private' fields and also do memory statistics to find all the
161 * __alloc_skb - allocate a network buffer
162 * @size: size to allocate
163 * @gfp_mask: allocation mask
164 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
165 * instead of head cache and allocate a cloned (child) skb.
166 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
167 * allocations in case the data is required for writeback
168 * @node: numa node to allocate memory on
170 * Allocate a new &sk_buff. The returned buffer has no headroom and a
171 * tail room of at least size bytes. The object has a reference count
172 * of one. The return is the buffer. On a failure the return is %NULL.
174 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
180 struct kmem_cache
*cache
;
181 struct skb_shared_info
*shinfo
;
186 cache
= (flags
& SKB_ALLOC_FCLONE
)
187 ? skbuff_fclone_cache
: skbuff_head_cache
;
189 if (sk_memalloc_socks() && (flags
& SKB_ALLOC_RX
))
190 gfp_mask
|= __GFP_MEMALLOC
;
193 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
198 /* We do our best to align skb_shared_info on a separate cache
199 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
200 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
201 * Both skb->head and skb_shared_info are cache line aligned.
203 size
= SKB_DATA_ALIGN(size
);
204 size
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
205 data
= kmalloc_reserve(size
, gfp_mask
, node
, &pfmemalloc
);
208 /* kmalloc(size) might give us more room than requested.
209 * Put skb_shared_info exactly at the end of allocated zone,
210 * to allow max possible filling before reallocation.
212 size
= SKB_WITH_OVERHEAD(ksize(data
));
213 prefetchw(data
+ size
);
216 * Only clear those fields we need to clear, not those that we will
217 * actually initialise below. Hence, don't put any more fields after
218 * the tail pointer in struct sk_buff!
220 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
221 /* Account for allocated memory : skb + skb->head */
222 skb
->truesize
= SKB_TRUESIZE(size
);
223 skb
->pfmemalloc
= pfmemalloc
;
224 refcount_set(&skb
->users
, 1);
227 skb_reset_tail_pointer(skb
);
228 skb
->end
= skb
->tail
+ size
;
229 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
230 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
232 /* make sure we initialize shinfo sequentially */
233 shinfo
= skb_shinfo(skb
);
234 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
235 atomic_set(&shinfo
->dataref
, 1);
237 if (flags
& SKB_ALLOC_FCLONE
) {
238 struct sk_buff_fclones
*fclones
;
240 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
242 skb
->fclone
= SKB_FCLONE_ORIG
;
243 refcount_set(&fclones
->fclone_ref
, 1);
245 fclones
->skb2
.fclone
= SKB_FCLONE_CLONE
;
250 kmem_cache_free(cache
, skb
);
254 EXPORT_SYMBOL(__alloc_skb
);
257 * __build_skb - build a network buffer
258 * @data: data buffer provided by caller
259 * @frag_size: size of data, or 0 if head was kmalloced
261 * Allocate a new &sk_buff. Caller provides space holding head and
262 * skb_shared_info. @data must have been allocated by kmalloc() only if
263 * @frag_size is 0, otherwise data should come from the page allocator
265 * The return is the new skb buffer.
266 * On a failure the return is %NULL, and @data is not freed.
268 * Before IO, driver allocates only data buffer where NIC put incoming frame
269 * Driver should add room at head (NET_SKB_PAD) and
270 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
271 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
272 * before giving packet to stack.
273 * RX rings only contains data buffers, not full skbs.
275 struct sk_buff
*__build_skb(void *data
, unsigned int frag_size
)
277 struct skb_shared_info
*shinfo
;
279 unsigned int size
= frag_size
? : ksize(data
);
281 skb
= kmem_cache_alloc(skbuff_head_cache
, GFP_ATOMIC
);
285 size
-= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
287 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
288 skb
->truesize
= SKB_TRUESIZE(size
);
289 refcount_set(&skb
->users
, 1);
292 skb_reset_tail_pointer(skb
);
293 skb
->end
= skb
->tail
+ size
;
294 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
295 skb
->transport_header
= (typeof(skb
->transport_header
))~0U;
297 /* make sure we initialize shinfo sequentially */
298 shinfo
= skb_shinfo(skb
);
299 memset(shinfo
, 0, offsetof(struct skb_shared_info
, dataref
));
300 atomic_set(&shinfo
->dataref
, 1);
305 /* build_skb() is wrapper over __build_skb(), that specifically
306 * takes care of skb->head and skb->pfmemalloc
307 * This means that if @frag_size is not zero, then @data must be backed
308 * by a page fragment, not kmalloc() or vmalloc()
310 struct sk_buff
*build_skb(void *data
, unsigned int frag_size
)
312 struct sk_buff
*skb
= __build_skb(data
, frag_size
);
314 if (skb
&& frag_size
) {
316 if (page_is_pfmemalloc(virt_to_head_page(data
)))
321 EXPORT_SYMBOL(build_skb
);
323 #define NAPI_SKB_CACHE_SIZE 64
325 struct napi_alloc_cache
{
326 struct page_frag_cache page
;
327 unsigned int skb_count
;
328 void *skb_cache
[NAPI_SKB_CACHE_SIZE
];
331 static DEFINE_PER_CPU(struct page_frag_cache
, netdev_alloc_cache
);
332 static DEFINE_PER_CPU(struct napi_alloc_cache
, napi_alloc_cache
);
334 static void *__netdev_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
336 struct page_frag_cache
*nc
;
340 local_irq_save(flags
);
341 nc
= this_cpu_ptr(&netdev_alloc_cache
);
342 data
= page_frag_alloc(nc
, fragsz
, gfp_mask
);
343 local_irq_restore(flags
);
348 * netdev_alloc_frag - allocate a page fragment
349 * @fragsz: fragment size
351 * Allocates a frag from a page for receive buffer.
352 * Uses GFP_ATOMIC allocations.
354 void *netdev_alloc_frag(unsigned int fragsz
)
356 fragsz
= SKB_DATA_ALIGN(fragsz
);
358 return __netdev_alloc_frag(fragsz
, GFP_ATOMIC
);
360 EXPORT_SYMBOL(netdev_alloc_frag
);
362 static void *__napi_alloc_frag(unsigned int fragsz
, gfp_t gfp_mask
)
364 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
366 return page_frag_alloc(&nc
->page
, fragsz
, gfp_mask
);
369 void *napi_alloc_frag(unsigned int fragsz
)
371 fragsz
= SKB_DATA_ALIGN(fragsz
);
373 return __napi_alloc_frag(fragsz
, GFP_ATOMIC
);
375 EXPORT_SYMBOL(napi_alloc_frag
);
378 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
379 * @dev: network device to receive on
380 * @len: length to allocate
381 * @gfp_mask: get_free_pages mask, passed to alloc_skb
383 * Allocate a new &sk_buff and assign it a usage count of one. The
384 * buffer has NET_SKB_PAD headroom built in. Users should allocate
385 * the headroom they think they need without accounting for the
386 * built in space. The built in space is used for optimisations.
388 * %NULL is returned if there is no free memory.
390 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
, unsigned int len
,
393 struct page_frag_cache
*nc
;
401 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
402 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
403 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
409 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
410 len
= SKB_DATA_ALIGN(len
);
412 if (sk_memalloc_socks())
413 gfp_mask
|= __GFP_MEMALLOC
;
415 local_irq_save(flags
);
417 nc
= this_cpu_ptr(&netdev_alloc_cache
);
418 data
= page_frag_alloc(nc
, len
, gfp_mask
);
419 pfmemalloc
= nc
->pfmemalloc
;
421 local_irq_restore(flags
);
426 skb
= __build_skb(data
, len
);
427 if (unlikely(!skb
)) {
432 /* use OR instead of assignment to avoid clearing of bits in mask */
438 skb_reserve(skb
, NET_SKB_PAD
);
444 EXPORT_SYMBOL(__netdev_alloc_skb
);
447 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
448 * @napi: napi instance this buffer was allocated for
449 * @len: length to allocate
450 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
452 * Allocate a new sk_buff for use in NAPI receive. This buffer will
453 * attempt to allocate the head from a special reserved region used
454 * only for NAPI Rx allocation. By doing this we can save several
455 * CPU cycles by avoiding having to disable and re-enable IRQs.
457 * %NULL is returned if there is no free memory.
459 struct sk_buff
*__napi_alloc_skb(struct napi_struct
*napi
, unsigned int len
,
462 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
466 len
+= NET_SKB_PAD
+ NET_IP_ALIGN
;
468 if ((len
> SKB_WITH_OVERHEAD(PAGE_SIZE
)) ||
469 (gfp_mask
& (__GFP_DIRECT_RECLAIM
| GFP_DMA
))) {
470 skb
= __alloc_skb(len
, gfp_mask
, SKB_ALLOC_RX
, NUMA_NO_NODE
);
476 len
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
477 len
= SKB_DATA_ALIGN(len
);
479 if (sk_memalloc_socks())
480 gfp_mask
|= __GFP_MEMALLOC
;
482 data
= page_frag_alloc(&nc
->page
, len
, gfp_mask
);
486 skb
= __build_skb(data
, len
);
487 if (unlikely(!skb
)) {
492 /* use OR instead of assignment to avoid clearing of bits in mask */
493 if (nc
->page
.pfmemalloc
)
498 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
);
499 skb
->dev
= napi
->dev
;
504 EXPORT_SYMBOL(__napi_alloc_skb
);
506 void skb_add_rx_frag(struct sk_buff
*skb
, int i
, struct page
*page
, int off
,
507 int size
, unsigned int truesize
)
509 skb_fill_page_desc(skb
, i
, page
, off
, size
);
511 skb
->data_len
+= size
;
512 skb
->truesize
+= truesize
;
514 EXPORT_SYMBOL(skb_add_rx_frag
);
516 void skb_coalesce_rx_frag(struct sk_buff
*skb
, int i
, int size
,
517 unsigned int truesize
)
519 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
521 skb_frag_size_add(frag
, size
);
523 skb
->data_len
+= size
;
524 skb
->truesize
+= truesize
;
526 EXPORT_SYMBOL(skb_coalesce_rx_frag
);
528 static void skb_drop_list(struct sk_buff
**listp
)
530 kfree_skb_list(*listp
);
534 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
536 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
539 static void skb_clone_fraglist(struct sk_buff
*skb
)
541 struct sk_buff
*list
;
543 skb_walk_frags(skb
, list
)
547 static void skb_free_head(struct sk_buff
*skb
)
549 unsigned char *head
= skb
->head
;
557 static void skb_release_data(struct sk_buff
*skb
)
559 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
563 atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
567 for (i
= 0; i
< shinfo
->nr_frags
; i
++)
568 __skb_frag_unref(&shinfo
->frags
[i
]);
570 if (shinfo
->frag_list
)
571 kfree_skb_list(shinfo
->frag_list
);
573 skb_zcopy_clear(skb
, true);
578 * Free an skbuff by memory without cleaning the state.
580 static void kfree_skbmem(struct sk_buff
*skb
)
582 struct sk_buff_fclones
*fclones
;
584 switch (skb
->fclone
) {
585 case SKB_FCLONE_UNAVAILABLE
:
586 kmem_cache_free(skbuff_head_cache
, skb
);
589 case SKB_FCLONE_ORIG
:
590 fclones
= container_of(skb
, struct sk_buff_fclones
, skb1
);
592 /* We usually free the clone (TX completion) before original skb
593 * This test would have no chance to be true for the clone,
594 * while here, branch prediction will be good.
596 if (refcount_read(&fclones
->fclone_ref
) == 1)
600 default: /* SKB_FCLONE_CLONE */
601 fclones
= container_of(skb
, struct sk_buff_fclones
, skb2
);
604 if (!refcount_dec_and_test(&fclones
->fclone_ref
))
607 kmem_cache_free(skbuff_fclone_cache
, fclones
);
610 void skb_release_head_state(struct sk_buff
*skb
)
614 if (skb
->destructor
) {
616 skb
->destructor(skb
);
618 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
619 nf_conntrack_put(skb_nfct(skb
));
621 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
622 nf_bridge_put(skb
->nf_bridge
);
626 /* Free everything but the sk_buff shell. */
627 static void skb_release_all(struct sk_buff
*skb
)
629 skb_release_head_state(skb
);
630 if (likely(skb
->head
))
631 skb_release_data(skb
);
635 * __kfree_skb - private function
638 * Free an sk_buff. Release anything attached to the buffer.
639 * Clean the state. This is an internal helper function. Users should
640 * always call kfree_skb
643 void __kfree_skb(struct sk_buff
*skb
)
645 skb_release_all(skb
);
648 EXPORT_SYMBOL(__kfree_skb
);
651 * kfree_skb - free an sk_buff
652 * @skb: buffer to free
654 * Drop a reference to the buffer and free it if the usage count has
657 void kfree_skb(struct sk_buff
*skb
)
662 trace_kfree_skb(skb
, __builtin_return_address(0));
665 EXPORT_SYMBOL(kfree_skb
);
667 void kfree_skb_list(struct sk_buff
*segs
)
670 struct sk_buff
*next
= segs
->next
;
676 EXPORT_SYMBOL(kfree_skb_list
);
679 * skb_tx_error - report an sk_buff xmit error
680 * @skb: buffer that triggered an error
682 * Report xmit error if a device callback is tracking this skb.
683 * skb must be freed afterwards.
685 void skb_tx_error(struct sk_buff
*skb
)
687 skb_zcopy_clear(skb
, true);
689 EXPORT_SYMBOL(skb_tx_error
);
692 * consume_skb - free an skbuff
693 * @skb: buffer to free
695 * Drop a ref to the buffer and free it if the usage count has hit zero
696 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
697 * is being dropped after a failure and notes that
699 void consume_skb(struct sk_buff
*skb
)
704 trace_consume_skb(skb
);
707 EXPORT_SYMBOL(consume_skb
);
710 * consume_stateless_skb - free an skbuff, assuming it is stateless
711 * @skb: buffer to free
713 * Alike consume_skb(), but this variant assumes that this is the last
714 * skb reference and all the head states have been already dropped
716 void __consume_stateless_skb(struct sk_buff
*skb
)
718 trace_consume_skb(skb
);
719 skb_release_data(skb
);
723 void __kfree_skb_flush(void)
725 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
727 /* flush skb_cache if containing objects */
729 kmem_cache_free_bulk(skbuff_head_cache
, nc
->skb_count
,
735 static inline void _kfree_skb_defer(struct sk_buff
*skb
)
737 struct napi_alloc_cache
*nc
= this_cpu_ptr(&napi_alloc_cache
);
739 /* drop skb->head and call any destructors for packet */
740 skb_release_all(skb
);
742 /* record skb to CPU local list */
743 nc
->skb_cache
[nc
->skb_count
++] = skb
;
746 /* SLUB writes into objects when freeing */
750 /* flush skb_cache if it is filled */
751 if (unlikely(nc
->skb_count
== NAPI_SKB_CACHE_SIZE
)) {
752 kmem_cache_free_bulk(skbuff_head_cache
, NAPI_SKB_CACHE_SIZE
,
757 void __kfree_skb_defer(struct sk_buff
*skb
)
759 _kfree_skb_defer(skb
);
762 void napi_consume_skb(struct sk_buff
*skb
, int budget
)
767 /* Zero budget indicate non-NAPI context called us, like netpoll */
768 if (unlikely(!budget
)) {
769 dev_consume_skb_any(skb
);
776 /* if reaching here SKB is ready to free */
777 trace_consume_skb(skb
);
779 /* if SKB is a clone, don't handle this case */
780 if (skb
->fclone
!= SKB_FCLONE_UNAVAILABLE
) {
785 _kfree_skb_defer(skb
);
787 EXPORT_SYMBOL(napi_consume_skb
);
789 /* Make sure a field is enclosed inside headers_start/headers_end section */
790 #define CHECK_SKB_FIELD(field) \
791 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
792 offsetof(struct sk_buff, headers_start)); \
793 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
794 offsetof(struct sk_buff, headers_end)); \
796 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
798 new->tstamp
= old
->tstamp
;
799 /* We do not copy old->sk */
801 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
802 skb_dst_copy(new, old
);
804 new->sp
= secpath_get(old
->sp
);
806 __nf_copy(new, old
, false);
808 /* Note : this field could be in headers_start/headers_end section
809 * It is not yet because we do not want to have a 16 bit hole
811 new->queue_mapping
= old
->queue_mapping
;
813 memcpy(&new->headers_start
, &old
->headers_start
,
814 offsetof(struct sk_buff
, headers_end
) -
815 offsetof(struct sk_buff
, headers_start
));
816 CHECK_SKB_FIELD(protocol
);
817 CHECK_SKB_FIELD(csum
);
818 CHECK_SKB_FIELD(hash
);
819 CHECK_SKB_FIELD(priority
);
820 CHECK_SKB_FIELD(skb_iif
);
821 CHECK_SKB_FIELD(vlan_proto
);
822 CHECK_SKB_FIELD(vlan_tci
);
823 CHECK_SKB_FIELD(transport_header
);
824 CHECK_SKB_FIELD(network_header
);
825 CHECK_SKB_FIELD(mac_header
);
826 CHECK_SKB_FIELD(inner_protocol
);
827 CHECK_SKB_FIELD(inner_transport_header
);
828 CHECK_SKB_FIELD(inner_network_header
);
829 CHECK_SKB_FIELD(inner_mac_header
);
830 CHECK_SKB_FIELD(mark
);
831 #ifdef CONFIG_NETWORK_SECMARK
832 CHECK_SKB_FIELD(secmark
);
834 #ifdef CONFIG_NET_RX_BUSY_POLL
835 CHECK_SKB_FIELD(napi_id
);
838 CHECK_SKB_FIELD(sender_cpu
);
840 #ifdef CONFIG_NET_SCHED
841 CHECK_SKB_FIELD(tc_index
);
847 * You should not add any new code to this function. Add it to
848 * __copy_skb_header above instead.
850 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
852 #define C(x) n->x = skb->x
854 n
->next
= n
->prev
= NULL
;
856 __copy_skb_header(n
, skb
);
861 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
866 n
->destructor
= NULL
;
873 refcount_set(&n
->users
, 1);
875 atomic_inc(&(skb_shinfo(skb
)->dataref
));
883 * skb_morph - morph one skb into another
884 * @dst: the skb to receive the contents
885 * @src: the skb to supply the contents
887 * This is identical to skb_clone except that the target skb is
888 * supplied by the user.
890 * The target skb is returned upon exit.
892 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
894 skb_release_all(dst
);
895 return __skb_clone(dst
, src
);
897 EXPORT_SYMBOL_GPL(skb_morph
);
899 int mm_account_pinned_pages(struct mmpin
*mmp
, size_t size
)
901 unsigned long max_pg
, num_pg
, new_pg
, old_pg
;
902 struct user_struct
*user
;
904 if (capable(CAP_IPC_LOCK
) || !size
)
907 num_pg
= (size
>> PAGE_SHIFT
) + 2; /* worst case */
908 max_pg
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
909 user
= mmp
->user
? : current_user();
912 old_pg
= atomic_long_read(&user
->locked_vm
);
913 new_pg
= old_pg
+ num_pg
;
916 } while (atomic_long_cmpxchg(&user
->locked_vm
, old_pg
, new_pg
) !=
920 mmp
->user
= get_uid(user
);
921 mmp
->num_pg
= num_pg
;
923 mmp
->num_pg
+= num_pg
;
928 EXPORT_SYMBOL_GPL(mm_account_pinned_pages
);
930 void mm_unaccount_pinned_pages(struct mmpin
*mmp
)
933 atomic_long_sub(mmp
->num_pg
, &mmp
->user
->locked_vm
);
937 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages
);
939 struct ubuf_info
*sock_zerocopy_alloc(struct sock
*sk
, size_t size
)
941 struct ubuf_info
*uarg
;
944 WARN_ON_ONCE(!in_task());
946 skb
= sock_omalloc(sk
, 0, GFP_KERNEL
);
950 BUILD_BUG_ON(sizeof(*uarg
) > sizeof(skb
->cb
));
951 uarg
= (void *)skb
->cb
;
952 uarg
->mmp
.user
= NULL
;
954 if (mm_account_pinned_pages(&uarg
->mmp
, size
)) {
959 uarg
->callback
= sock_zerocopy_callback
;
960 uarg
->id
= ((u32
)atomic_inc_return(&sk
->sk_zckey
)) - 1;
962 uarg
->bytelen
= size
;
964 refcount_set(&uarg
->refcnt
, 1);
969 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc
);
971 static inline struct sk_buff
*skb_from_uarg(struct ubuf_info
*uarg
)
973 return container_of((void *)uarg
, struct sk_buff
, cb
);
976 struct ubuf_info
*sock_zerocopy_realloc(struct sock
*sk
, size_t size
,
977 struct ubuf_info
*uarg
)
980 const u32 byte_limit
= 1 << 19; /* limit to a few TSO */
983 /* realloc only when socket is locked (TCP, UDP cork),
984 * so uarg->len and sk_zckey access is serialized
986 if (!sock_owned_by_user(sk
)) {
991 bytelen
= uarg
->bytelen
+ size
;
992 if (uarg
->len
== USHRT_MAX
- 1 || bytelen
> byte_limit
) {
993 /* TCP can create new skb to attach new uarg */
994 if (sk
->sk_type
== SOCK_STREAM
)
999 next
= (u32
)atomic_read(&sk
->sk_zckey
);
1000 if ((u32
)(uarg
->id
+ uarg
->len
) == next
) {
1001 if (mm_account_pinned_pages(&uarg
->mmp
, size
))
1004 uarg
->bytelen
= bytelen
;
1005 atomic_set(&sk
->sk_zckey
, ++next
);
1006 sock_zerocopy_get(uarg
);
1012 return sock_zerocopy_alloc(sk
, size
);
1014 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc
);
1016 static bool skb_zerocopy_notify_extend(struct sk_buff
*skb
, u32 lo
, u16 len
)
1018 struct sock_exterr_skb
*serr
= SKB_EXT_ERR(skb
);
1022 old_lo
= serr
->ee
.ee_info
;
1023 old_hi
= serr
->ee
.ee_data
;
1024 sum_len
= old_hi
- old_lo
+ 1ULL + len
;
1026 if (sum_len
>= (1ULL << 32))
1029 if (lo
!= old_hi
+ 1)
1032 serr
->ee
.ee_data
+= len
;
1036 void sock_zerocopy_callback(struct ubuf_info
*uarg
, bool success
)
1038 struct sk_buff
*tail
, *skb
= skb_from_uarg(uarg
);
1039 struct sock_exterr_skb
*serr
;
1040 struct sock
*sk
= skb
->sk
;
1041 struct sk_buff_head
*q
;
1042 unsigned long flags
;
1046 mm_unaccount_pinned_pages(&uarg
->mmp
);
1048 /* if !len, there was only 1 call, and it was aborted
1049 * so do not queue a completion notification
1051 if (!uarg
->len
|| sock_flag(sk
, SOCK_DEAD
))
1056 hi
= uarg
->id
+ len
- 1;
1058 serr
= SKB_EXT_ERR(skb
);
1059 memset(serr
, 0, sizeof(*serr
));
1060 serr
->ee
.ee_errno
= 0;
1061 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ZEROCOPY
;
1062 serr
->ee
.ee_data
= hi
;
1063 serr
->ee
.ee_info
= lo
;
1065 serr
->ee
.ee_code
|= SO_EE_CODE_ZEROCOPY_COPIED
;
1067 q
= &sk
->sk_error_queue
;
1068 spin_lock_irqsave(&q
->lock
, flags
);
1069 tail
= skb_peek_tail(q
);
1070 if (!tail
|| SKB_EXT_ERR(tail
)->ee
.ee_origin
!= SO_EE_ORIGIN_ZEROCOPY
||
1071 !skb_zerocopy_notify_extend(tail
, lo
, len
)) {
1072 __skb_queue_tail(q
, skb
);
1075 spin_unlock_irqrestore(&q
->lock
, flags
);
1077 sk
->sk_error_report(sk
);
1083 EXPORT_SYMBOL_GPL(sock_zerocopy_callback
);
1085 void sock_zerocopy_put(struct ubuf_info
*uarg
)
1087 if (uarg
&& refcount_dec_and_test(&uarg
->refcnt
)) {
1089 uarg
->callback(uarg
, uarg
->zerocopy
);
1091 consume_skb(skb_from_uarg(uarg
));
1094 EXPORT_SYMBOL_GPL(sock_zerocopy_put
);
1096 void sock_zerocopy_put_abort(struct ubuf_info
*uarg
)
1099 struct sock
*sk
= skb_from_uarg(uarg
)->sk
;
1101 atomic_dec(&sk
->sk_zckey
);
1104 sock_zerocopy_put(uarg
);
1107 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort
);
1109 extern int __zerocopy_sg_from_iter(struct sock
*sk
, struct sk_buff
*skb
,
1110 struct iov_iter
*from
, size_t length
);
1112 int skb_zerocopy_iter_stream(struct sock
*sk
, struct sk_buff
*skb
,
1113 struct msghdr
*msg
, int len
,
1114 struct ubuf_info
*uarg
)
1116 struct ubuf_info
*orig_uarg
= skb_zcopy(skb
);
1117 struct iov_iter orig_iter
= msg
->msg_iter
;
1118 int err
, orig_len
= skb
->len
;
1120 /* An skb can only point to one uarg. This edge case happens when
1121 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1123 if (orig_uarg
&& uarg
!= orig_uarg
)
1126 err
= __zerocopy_sg_from_iter(sk
, skb
, &msg
->msg_iter
, len
);
1127 if (err
== -EFAULT
|| (err
== -EMSGSIZE
&& skb
->len
== orig_len
)) {
1128 struct sock
*save_sk
= skb
->sk
;
1130 /* Streams do not free skb on error. Reset to prev state. */
1131 msg
->msg_iter
= orig_iter
;
1133 ___pskb_trim(skb
, orig_len
);
1138 skb_zcopy_set(skb
, uarg
);
1139 return skb
->len
- orig_len
;
1141 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream
);
1143 static int skb_zerocopy_clone(struct sk_buff
*nskb
, struct sk_buff
*orig
,
1146 if (skb_zcopy(orig
)) {
1147 if (skb_zcopy(nskb
)) {
1148 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1153 if (skb_uarg(nskb
) == skb_uarg(orig
))
1155 if (skb_copy_ubufs(nskb
, GFP_ATOMIC
))
1158 skb_zcopy_set(nskb
, skb_uarg(orig
));
1164 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
1165 * @skb: the skb to modify
1166 * @gfp_mask: allocation priority
1168 * This must be called on SKBTX_DEV_ZEROCOPY skb.
1169 * It will copy all frags into kernel and drop the reference
1170 * to userspace pages.
1172 * If this function is called from an interrupt gfp_mask() must be
1175 * Returns 0 on success or a negative error code on failure
1176 * to allocate kernel memory to copy to.
1178 int skb_copy_ubufs(struct sk_buff
*skb
, gfp_t gfp_mask
)
1180 int num_frags
= skb_shinfo(skb
)->nr_frags
;
1181 struct page
*page
, *head
= NULL
;
1185 if (skb_shared(skb
) || skb_unclone(skb
, gfp_mask
))
1191 new_frags
= (__skb_pagelen(skb
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1192 for (i
= 0; i
< new_frags
; i
++) {
1193 page
= alloc_page(gfp_mask
);
1196 struct page
*next
= (struct page
*)page_private(head
);
1202 set_page_private(page
, (unsigned long)head
);
1208 for (i
= 0; i
< num_frags
; i
++) {
1209 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
1210 u32 p_off
, p_len
, copied
;
1214 skb_frag_foreach_page(f
, f
->page_offset
, skb_frag_size(f
),
1215 p
, p_off
, p_len
, copied
) {
1217 vaddr
= kmap_atomic(p
);
1219 while (done
< p_len
) {
1220 if (d_off
== PAGE_SIZE
) {
1222 page
= (struct page
*)page_private(page
);
1224 copy
= min_t(u32
, PAGE_SIZE
- d_off
, p_len
- done
);
1225 memcpy(page_address(page
) + d_off
,
1226 vaddr
+ p_off
+ done
, copy
);
1230 kunmap_atomic(vaddr
);
1234 /* skb frags release userspace buffers */
1235 for (i
= 0; i
< num_frags
; i
++)
1236 skb_frag_unref(skb
, i
);
1238 /* skb frags point to kernel buffers */
1239 for (i
= 0; i
< new_frags
- 1; i
++) {
1240 __skb_fill_page_desc(skb
, i
, head
, 0, PAGE_SIZE
);
1241 head
= (struct page
*)page_private(head
);
1243 __skb_fill_page_desc(skb
, new_frags
- 1, head
, 0, d_off
);
1244 skb_shinfo(skb
)->nr_frags
= new_frags
;
1247 skb_zcopy_clear(skb
, false);
1250 EXPORT_SYMBOL_GPL(skb_copy_ubufs
);
1253 * skb_clone - duplicate an sk_buff
1254 * @skb: buffer to clone
1255 * @gfp_mask: allocation priority
1257 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1258 * copies share the same packet data but not structure. The new
1259 * buffer has a reference count of 1. If the allocation fails the
1260 * function returns %NULL otherwise the new buffer is returned.
1262 * If this function is called from an interrupt gfp_mask() must be
1266 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
1268 struct sk_buff_fclones
*fclones
= container_of(skb
,
1269 struct sk_buff_fclones
,
1273 if (skb_orphan_frags(skb
, gfp_mask
))
1276 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
1277 refcount_read(&fclones
->fclone_ref
) == 1) {
1279 refcount_set(&fclones
->fclone_ref
, 2);
1281 if (skb_pfmemalloc(skb
))
1282 gfp_mask
|= __GFP_MEMALLOC
;
1284 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
1288 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
1291 return __skb_clone(n
, skb
);
1293 EXPORT_SYMBOL(skb_clone
);
1295 void skb_headers_offset_update(struct sk_buff
*skb
, int off
)
1297 /* Only adjust this if it actually is csum_start rather than csum */
1298 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1299 skb
->csum_start
+= off
;
1300 /* {transport,network,mac}_header and tail are relative to skb->head */
1301 skb
->transport_header
+= off
;
1302 skb
->network_header
+= off
;
1303 if (skb_mac_header_was_set(skb
))
1304 skb
->mac_header
+= off
;
1305 skb
->inner_transport_header
+= off
;
1306 skb
->inner_network_header
+= off
;
1307 skb
->inner_mac_header
+= off
;
1309 EXPORT_SYMBOL(skb_headers_offset_update
);
1311 void skb_copy_header(struct sk_buff
*new, const struct sk_buff
*old
)
1313 __copy_skb_header(new, old
);
1315 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
1316 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
1317 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
1319 EXPORT_SYMBOL(skb_copy_header
);
1321 static inline int skb_alloc_rx_flag(const struct sk_buff
*skb
)
1323 if (skb_pfmemalloc(skb
))
1324 return SKB_ALLOC_RX
;
1329 * skb_copy - create private copy of an sk_buff
1330 * @skb: buffer to copy
1331 * @gfp_mask: allocation priority
1333 * Make a copy of both an &sk_buff and its data. This is used when the
1334 * caller wishes to modify the data and needs a private copy of the
1335 * data to alter. Returns %NULL on failure or the pointer to the buffer
1336 * on success. The returned buffer has a reference count of 1.
1338 * As by-product this function converts non-linear &sk_buff to linear
1339 * one, so that &sk_buff becomes completely private and caller is allowed
1340 * to modify all the data of returned buffer. This means that this
1341 * function is not recommended for use in circumstances when only
1342 * header is going to be modified. Use pskb_copy() instead.
1345 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
1347 int headerlen
= skb_headroom(skb
);
1348 unsigned int size
= skb_end_offset(skb
) + skb
->data_len
;
1349 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
,
1350 skb_alloc_rx_flag(skb
), NUMA_NO_NODE
);
1355 /* Set the data pointer */
1356 skb_reserve(n
, headerlen
);
1357 /* Set the tail pointer and length */
1358 skb_put(n
, skb
->len
);
1360 BUG_ON(skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
));
1362 skb_copy_header(n
, skb
);
1365 EXPORT_SYMBOL(skb_copy
);
1368 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1369 * @skb: buffer to copy
1370 * @headroom: headroom of new skb
1371 * @gfp_mask: allocation priority
1372 * @fclone: if true allocate the copy of the skb from the fclone
1373 * cache instead of the head cache; it is recommended to set this
1374 * to true for the cases where the copy will likely be cloned
1376 * Make a copy of both an &sk_buff and part of its data, located
1377 * in header. Fragmented data remain shared. This is used when
1378 * the caller wishes to modify only header of &sk_buff and needs
1379 * private copy of the header to alter. Returns %NULL on failure
1380 * or the pointer to the buffer on success.
1381 * The returned buffer has a reference count of 1.
1384 struct sk_buff
*__pskb_copy_fclone(struct sk_buff
*skb
, int headroom
,
1385 gfp_t gfp_mask
, bool fclone
)
1387 unsigned int size
= skb_headlen(skb
) + headroom
;
1388 int flags
= skb_alloc_rx_flag(skb
) | (fclone
? SKB_ALLOC_FCLONE
: 0);
1389 struct sk_buff
*n
= __alloc_skb(size
, gfp_mask
, flags
, NUMA_NO_NODE
);
1394 /* Set the data pointer */
1395 skb_reserve(n
, headroom
);
1396 /* Set the tail pointer and length */
1397 skb_put(n
, skb_headlen(skb
));
1398 /* Copy the bytes */
1399 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
1401 n
->truesize
+= skb
->data_len
;
1402 n
->data_len
= skb
->data_len
;
1405 if (skb_shinfo(skb
)->nr_frags
) {
1408 if (skb_orphan_frags(skb
, gfp_mask
) ||
1409 skb_zerocopy_clone(n
, skb
, gfp_mask
)) {
1414 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1415 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1416 skb_frag_ref(skb
, i
);
1418 skb_shinfo(n
)->nr_frags
= i
;
1421 if (skb_has_frag_list(skb
)) {
1422 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
1423 skb_clone_fraglist(n
);
1426 skb_copy_header(n
, skb
);
1430 EXPORT_SYMBOL(__pskb_copy_fclone
);
1433 * pskb_expand_head - reallocate header of &sk_buff
1434 * @skb: buffer to reallocate
1435 * @nhead: room to add at head
1436 * @ntail: room to add at tail
1437 * @gfp_mask: allocation priority
1439 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1440 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1441 * reference count of 1. Returns zero in the case of success or error,
1442 * if expansion failed. In the last case, &sk_buff is not changed.
1444 * All the pointers pointing into skb header may change and must be
1445 * reloaded after call to this function.
1448 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
1451 int i
, osize
= skb_end_offset(skb
);
1452 int size
= osize
+ nhead
+ ntail
;
1458 BUG_ON(skb_shared(skb
));
1460 size
= SKB_DATA_ALIGN(size
);
1462 if (skb_pfmemalloc(skb
))
1463 gfp_mask
|= __GFP_MEMALLOC
;
1464 data
= kmalloc_reserve(size
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
1465 gfp_mask
, NUMA_NO_NODE
, NULL
);
1468 size
= SKB_WITH_OVERHEAD(ksize(data
));
1470 /* Copy only real data... and, alas, header. This should be
1471 * optimized for the cases when header is void.
1473 memcpy(data
+ nhead
, skb
->head
, skb_tail_pointer(skb
) - skb
->head
);
1475 memcpy((struct skb_shared_info
*)(data
+ size
),
1477 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
1480 * if shinfo is shared we must drop the old head gracefully, but if it
1481 * is not we can just drop the old head and let the existing refcount
1482 * be since all we did is relocate the values
1484 if (skb_cloned(skb
)) {
1485 if (skb_orphan_frags(skb
, gfp_mask
))
1488 refcount_inc(&skb_uarg(skb
)->refcnt
);
1489 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1490 skb_frag_ref(skb
, i
);
1492 if (skb_has_frag_list(skb
))
1493 skb_clone_fraglist(skb
);
1495 skb_release_data(skb
);
1499 off
= (data
+ nhead
) - skb
->head
;
1504 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1508 skb
->end
= skb
->head
+ size
;
1511 skb_headers_offset_update(skb
, nhead
);
1515 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
1517 skb_metadata_clear(skb
);
1519 /* It is not generally safe to change skb->truesize.
1520 * For the moment, we really care of rx path, or
1521 * when skb is orphaned (not attached to a socket).
1523 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1524 skb
->truesize
+= size
- osize
;
1533 EXPORT_SYMBOL(pskb_expand_head
);
1535 /* Make private copy of skb with writable head and some headroom */
1537 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
1539 struct sk_buff
*skb2
;
1540 int delta
= headroom
- skb_headroom(skb
);
1543 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
1545 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1546 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
1554 EXPORT_SYMBOL(skb_realloc_headroom
);
1557 * skb_copy_expand - copy and expand sk_buff
1558 * @skb: buffer to copy
1559 * @newheadroom: new free bytes at head
1560 * @newtailroom: new free bytes at tail
1561 * @gfp_mask: allocation priority
1563 * Make a copy of both an &sk_buff and its data and while doing so
1564 * allocate additional space.
1566 * This is used when the caller wishes to modify the data and needs a
1567 * private copy of the data to alter as well as more space for new fields.
1568 * Returns %NULL on failure or the pointer to the buffer
1569 * on success. The returned buffer has a reference count of 1.
1571 * You must pass %GFP_ATOMIC as the allocation priority if this function
1572 * is called from an interrupt.
1574 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
1575 int newheadroom
, int newtailroom
,
1579 * Allocate the copy buffer
1581 struct sk_buff
*n
= __alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
1582 gfp_mask
, skb_alloc_rx_flag(skb
),
1584 int oldheadroom
= skb_headroom(skb
);
1585 int head_copy_len
, head_copy_off
;
1590 skb_reserve(n
, newheadroom
);
1592 /* Set the tail pointer and length */
1593 skb_put(n
, skb
->len
);
1595 head_copy_len
= oldheadroom
;
1597 if (newheadroom
<= head_copy_len
)
1598 head_copy_len
= newheadroom
;
1600 head_copy_off
= newheadroom
- head_copy_len
;
1602 /* Copy the linear header and data. */
1603 BUG_ON(skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
1604 skb
->len
+ head_copy_len
));
1606 skb_copy_header(n
, skb
);
1608 skb_headers_offset_update(n
, newheadroom
- oldheadroom
);
1612 EXPORT_SYMBOL(skb_copy_expand
);
1615 * __skb_pad - zero pad the tail of an skb
1616 * @skb: buffer to pad
1617 * @pad: space to pad
1618 * @free_on_error: free buffer on error
1620 * Ensure that a buffer is followed by a padding area that is zero
1621 * filled. Used by network drivers which may DMA or transfer data
1622 * beyond the buffer end onto the wire.
1624 * May return error in out of memory cases. The skb is freed on error
1625 * if @free_on_error is true.
1628 int __skb_pad(struct sk_buff
*skb
, int pad
, bool free_on_error
)
1633 /* If the skbuff is non linear tailroom is always zero.. */
1634 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
1635 memset(skb
->data
+skb
->len
, 0, pad
);
1639 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
1640 if (likely(skb_cloned(skb
) || ntail
> 0)) {
1641 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
1646 /* FIXME: The use of this function with non-linear skb's really needs
1649 err
= skb_linearize(skb
);
1653 memset(skb
->data
+ skb
->len
, 0, pad
);
1661 EXPORT_SYMBOL(__skb_pad
);
1664 * pskb_put - add data to the tail of a potentially fragmented buffer
1665 * @skb: start of the buffer to use
1666 * @tail: tail fragment of the buffer to use
1667 * @len: amount of data to add
1669 * This function extends the used data area of the potentially
1670 * fragmented buffer. @tail must be the last fragment of @skb -- or
1671 * @skb itself. If this would exceed the total buffer size the kernel
1672 * will panic. A pointer to the first byte of the extra data is
1676 void *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
1679 skb
->data_len
+= len
;
1682 return skb_put(tail
, len
);
1684 EXPORT_SYMBOL_GPL(pskb_put
);
1687 * skb_put - add data to a buffer
1688 * @skb: buffer to use
1689 * @len: amount of data to add
1691 * This function extends the used data area of the buffer. If this would
1692 * exceed the total buffer size the kernel will panic. A pointer to the
1693 * first byte of the extra data is returned.
1695 void *skb_put(struct sk_buff
*skb
, unsigned int len
)
1697 void *tmp
= skb_tail_pointer(skb
);
1698 SKB_LINEAR_ASSERT(skb
);
1701 if (unlikely(skb
->tail
> skb
->end
))
1702 skb_over_panic(skb
, len
, __builtin_return_address(0));
1705 EXPORT_SYMBOL(skb_put
);
1708 * skb_push - add data to the start of a buffer
1709 * @skb: buffer to use
1710 * @len: amount of data to add
1712 * This function extends the used data area of the buffer at the buffer
1713 * start. If this would exceed the total buffer headroom the kernel will
1714 * panic. A pointer to the first byte of the extra data is returned.
1716 void *skb_push(struct sk_buff
*skb
, unsigned int len
)
1720 if (unlikely(skb
->data
< skb
->head
))
1721 skb_under_panic(skb
, len
, __builtin_return_address(0));
1724 EXPORT_SYMBOL(skb_push
);
1727 * skb_pull - remove data from the start of a buffer
1728 * @skb: buffer to use
1729 * @len: amount of data to remove
1731 * This function removes data from the start of a buffer, returning
1732 * the memory to the headroom. A pointer to the next data in the buffer
1733 * is returned. Once the data has been pulled future pushes will overwrite
1736 void *skb_pull(struct sk_buff
*skb
, unsigned int len
)
1738 return skb_pull_inline(skb
, len
);
1740 EXPORT_SYMBOL(skb_pull
);
1743 * skb_trim - remove end from a buffer
1744 * @skb: buffer to alter
1747 * Cut the length of a buffer down by removing data from the tail. If
1748 * the buffer is already under the length specified it is not modified.
1749 * The skb must be linear.
1751 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
1754 __skb_trim(skb
, len
);
1756 EXPORT_SYMBOL(skb_trim
);
1758 /* Trims skb to length len. It can change skb pointers.
1761 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
1763 struct sk_buff
**fragp
;
1764 struct sk_buff
*frag
;
1765 int offset
= skb_headlen(skb
);
1766 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1770 if (skb_cloned(skb
) &&
1771 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
1778 for (; i
< nfrags
; i
++) {
1779 int end
= offset
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1786 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
++], len
- offset
);
1789 skb_shinfo(skb
)->nr_frags
= i
;
1791 for (; i
< nfrags
; i
++)
1792 skb_frag_unref(skb
, i
);
1794 if (skb_has_frag_list(skb
))
1795 skb_drop_fraglist(skb
);
1799 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
1800 fragp
= &frag
->next
) {
1801 int end
= offset
+ frag
->len
;
1803 if (skb_shared(frag
)) {
1804 struct sk_buff
*nfrag
;
1806 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
1807 if (unlikely(!nfrag
))
1810 nfrag
->next
= frag
->next
;
1822 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1826 skb_drop_list(&frag
->next
);
1831 if (len
> skb_headlen(skb
)) {
1832 skb
->data_len
-= skb
->len
- len
;
1837 skb_set_tail_pointer(skb
, len
);
1840 if (!skb
->sk
|| skb
->destructor
== sock_edemux
)
1844 EXPORT_SYMBOL(___pskb_trim
);
1846 /* Note : use pskb_trim_rcsum() instead of calling this directly
1848 int pskb_trim_rcsum_slow(struct sk_buff
*skb
, unsigned int len
)
1850 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
1851 int delta
= skb
->len
- len
;
1853 skb
->csum
= csum_block_sub(skb
->csum
,
1854 skb_checksum(skb
, len
, delta
, 0),
1857 return __pskb_trim(skb
, len
);
1859 EXPORT_SYMBOL(pskb_trim_rcsum_slow
);
1862 * __pskb_pull_tail - advance tail of skb header
1863 * @skb: buffer to reallocate
1864 * @delta: number of bytes to advance tail
1866 * The function makes a sense only on a fragmented &sk_buff,
1867 * it expands header moving its tail forward and copying necessary
1868 * data from fragmented part.
1870 * &sk_buff MUST have reference count of 1.
1872 * Returns %NULL (and &sk_buff does not change) if pull failed
1873 * or value of new tail of skb in the case of success.
1875 * All the pointers pointing into skb header may change and must be
1876 * reloaded after call to this function.
1879 /* Moves tail of skb head forward, copying data from fragmented part,
1880 * when it is necessary.
1881 * 1. It may fail due to malloc failure.
1882 * 2. It may change skb pointers.
1884 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1886 void *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1888 /* If skb has not enough free space at tail, get new one
1889 * plus 128 bytes for future expansions. If we have enough
1890 * room at tail, reallocate without expansion only if skb is cloned.
1892 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1894 if (eat
> 0 || skb_cloned(skb
)) {
1895 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1900 BUG_ON(skb_copy_bits(skb
, skb_headlen(skb
),
1901 skb_tail_pointer(skb
), delta
));
1903 /* Optimization: no fragments, no reasons to preestimate
1904 * size of pulled pages. Superb.
1906 if (!skb_has_frag_list(skb
))
1909 /* Estimate size of pulled pages. */
1911 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1912 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1919 /* If we need update frag list, we are in troubles.
1920 * Certainly, it is possible to add an offset to skb data,
1921 * but taking into account that pulling is expected to
1922 * be very rare operation, it is worth to fight against
1923 * further bloating skb head and crucify ourselves here instead.
1924 * Pure masohism, indeed. 8)8)
1927 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1928 struct sk_buff
*clone
= NULL
;
1929 struct sk_buff
*insp
= NULL
;
1934 if (list
->len
<= eat
) {
1935 /* Eaten as whole. */
1940 /* Eaten partially. */
1942 if (skb_shared(list
)) {
1943 /* Sucks! We need to fork list. :-( */
1944 clone
= skb_clone(list
, GFP_ATOMIC
);
1950 /* This may be pulled without
1954 if (!pskb_pull(list
, eat
)) {
1962 /* Free pulled out fragments. */
1963 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1964 skb_shinfo(skb
)->frag_list
= list
->next
;
1967 /* And insert new clone at head. */
1970 skb_shinfo(skb
)->frag_list
= clone
;
1973 /* Success! Now we may commit changes to skb data. */
1978 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1979 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
1982 skb_frag_unref(skb
, i
);
1985 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1987 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1988 skb_frag_size_sub(&skb_shinfo(skb
)->frags
[k
], eat
);
1996 skb_shinfo(skb
)->nr_frags
= k
;
2000 skb
->data_len
-= delta
;
2003 skb_zcopy_clear(skb
, false);
2005 return skb_tail_pointer(skb
);
2007 EXPORT_SYMBOL(__pskb_pull_tail
);
2010 * skb_copy_bits - copy bits from skb to kernel buffer
2012 * @offset: offset in source
2013 * @to: destination buffer
2014 * @len: number of bytes to copy
2016 * Copy the specified number of bytes from the source skb to the
2017 * destination buffer.
2020 * If its prototype is ever changed,
2021 * check arch/{*}/net/{*}.S files,
2022 * since it is called from BPF assembly code.
2024 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
2026 int start
= skb_headlen(skb
);
2027 struct sk_buff
*frag_iter
;
2030 if (offset
> (int)skb
->len
- len
)
2034 if ((copy
= start
- offset
) > 0) {
2037 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
2038 if ((len
-= copy
) == 0)
2044 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2046 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
2048 WARN_ON(start
> offset
+ len
);
2050 end
= start
+ skb_frag_size(f
);
2051 if ((copy
= end
- offset
) > 0) {
2052 u32 p_off
, p_len
, copied
;
2059 skb_frag_foreach_page(f
,
2060 f
->page_offset
+ offset
- start
,
2061 copy
, p
, p_off
, p_len
, copied
) {
2062 vaddr
= kmap_atomic(p
);
2063 memcpy(to
+ copied
, vaddr
+ p_off
, p_len
);
2064 kunmap_atomic(vaddr
);
2067 if ((len
-= copy
) == 0)
2075 skb_walk_frags(skb
, frag_iter
) {
2078 WARN_ON(start
> offset
+ len
);
2080 end
= start
+ frag_iter
->len
;
2081 if ((copy
= end
- offset
) > 0) {
2084 if (skb_copy_bits(frag_iter
, offset
- start
, to
, copy
))
2086 if ((len
-= copy
) == 0)
2100 EXPORT_SYMBOL(skb_copy_bits
);
2103 * Callback from splice_to_pipe(), if we need to release some pages
2104 * at the end of the spd in case we error'ed out in filling the pipe.
2106 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
2108 put_page(spd
->pages
[i
]);
2111 static struct page
*linear_to_page(struct page
*page
, unsigned int *len
,
2112 unsigned int *offset
,
2115 struct page_frag
*pfrag
= sk_page_frag(sk
);
2117 if (!sk_page_frag_refill(sk
, pfrag
))
2120 *len
= min_t(unsigned int, *len
, pfrag
->size
- pfrag
->offset
);
2122 memcpy(page_address(pfrag
->page
) + pfrag
->offset
,
2123 page_address(page
) + *offset
, *len
);
2124 *offset
= pfrag
->offset
;
2125 pfrag
->offset
+= *len
;
2130 static bool spd_can_coalesce(const struct splice_pipe_desc
*spd
,
2132 unsigned int offset
)
2134 return spd
->nr_pages
&&
2135 spd
->pages
[spd
->nr_pages
- 1] == page
&&
2136 (spd
->partial
[spd
->nr_pages
- 1].offset
+
2137 spd
->partial
[spd
->nr_pages
- 1].len
== offset
);
2141 * Fill page/offset/length into spd, if it can hold more pages.
2143 static bool spd_fill_page(struct splice_pipe_desc
*spd
,
2144 struct pipe_inode_info
*pipe
, struct page
*page
,
2145 unsigned int *len
, unsigned int offset
,
2149 if (unlikely(spd
->nr_pages
== MAX_SKB_FRAGS
))
2153 page
= linear_to_page(page
, len
, &offset
, sk
);
2157 if (spd_can_coalesce(spd
, page
, offset
)) {
2158 spd
->partial
[spd
->nr_pages
- 1].len
+= *len
;
2162 spd
->pages
[spd
->nr_pages
] = page
;
2163 spd
->partial
[spd
->nr_pages
].len
= *len
;
2164 spd
->partial
[spd
->nr_pages
].offset
= offset
;
2170 static bool __splice_segment(struct page
*page
, unsigned int poff
,
2171 unsigned int plen
, unsigned int *off
,
2173 struct splice_pipe_desc
*spd
, bool linear
,
2175 struct pipe_inode_info
*pipe
)
2180 /* skip this segment if already processed */
2186 /* ignore any bits we already processed */
2192 unsigned int flen
= min(*len
, plen
);
2194 if (spd_fill_page(spd
, pipe
, page
, &flen
, poff
,
2200 } while (*len
&& plen
);
2206 * Map linear and fragment data from the skb to spd. It reports true if the
2207 * pipe is full or if we already spliced the requested length.
2209 static bool __skb_splice_bits(struct sk_buff
*skb
, struct pipe_inode_info
*pipe
,
2210 unsigned int *offset
, unsigned int *len
,
2211 struct splice_pipe_desc
*spd
, struct sock
*sk
)
2214 struct sk_buff
*iter
;
2216 /* map the linear part :
2217 * If skb->head_frag is set, this 'linear' part is backed by a
2218 * fragment, and if the head is not shared with any clones then
2219 * we can avoid a copy since we own the head portion of this page.
2221 if (__splice_segment(virt_to_page(skb
->data
),
2222 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
2225 skb_head_is_locked(skb
),
2230 * then map the fragments
2232 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
2233 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
2235 if (__splice_segment(skb_frag_page(f
),
2236 f
->page_offset
, skb_frag_size(f
),
2237 offset
, len
, spd
, false, sk
, pipe
))
2241 skb_walk_frags(skb
, iter
) {
2242 if (*offset
>= iter
->len
) {
2243 *offset
-= iter
->len
;
2246 /* __skb_splice_bits() only fails if the output has no room
2247 * left, so no point in going over the frag_list for the error
2250 if (__skb_splice_bits(iter
, pipe
, offset
, len
, spd
, sk
))
2258 * Map data from the skb to a pipe. Should handle both the linear part,
2259 * the fragments, and the frag list.
2261 int skb_splice_bits(struct sk_buff
*skb
, struct sock
*sk
, unsigned int offset
,
2262 struct pipe_inode_info
*pipe
, unsigned int tlen
,
2265 struct partial_page partial
[MAX_SKB_FRAGS
];
2266 struct page
*pages
[MAX_SKB_FRAGS
];
2267 struct splice_pipe_desc spd
= {
2270 .nr_pages_max
= MAX_SKB_FRAGS
,
2271 .ops
= &nosteal_pipe_buf_ops
,
2272 .spd_release
= sock_spd_release
,
2276 __skb_splice_bits(skb
, pipe
, &offset
, &tlen
, &spd
, sk
);
2279 ret
= splice_to_pipe(pipe
, &spd
);
2283 EXPORT_SYMBOL_GPL(skb_splice_bits
);
2285 /* Send skb data on a socket. Socket must be locked. */
2286 int skb_send_sock_locked(struct sock
*sk
, struct sk_buff
*skb
, int offset
,
2289 unsigned int orig_len
= len
;
2290 struct sk_buff
*head
= skb
;
2291 unsigned short fragidx
;
2296 /* Deal with head data */
2297 while (offset
< skb_headlen(skb
) && len
) {
2301 slen
= min_t(int, len
, skb_headlen(skb
) - offset
);
2302 kv
.iov_base
= skb
->data
+ offset
;
2304 memset(&msg
, 0, sizeof(msg
));
2305 msg
.msg_flags
= MSG_DONTWAIT
;
2307 ret
= kernel_sendmsg_locked(sk
, &msg
, &kv
, 1, slen
);
2315 /* All the data was skb head? */
2319 /* Make offset relative to start of frags */
2320 offset
-= skb_headlen(skb
);
2322 /* Find where we are in frag list */
2323 for (fragidx
= 0; fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2324 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2326 if (offset
< frag
->size
)
2329 offset
-= frag
->size
;
2332 for (; len
&& fragidx
< skb_shinfo(skb
)->nr_frags
; fragidx
++) {
2333 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[fragidx
];
2335 slen
= min_t(size_t, len
, frag
->size
- offset
);
2338 ret
= kernel_sendpage_locked(sk
, frag
->page
.p
,
2339 frag
->page_offset
+ offset
,
2340 slen
, MSG_DONTWAIT
);
2353 /* Process any frag lists */
2356 if (skb_has_frag_list(skb
)) {
2357 skb
= skb_shinfo(skb
)->frag_list
;
2360 } else if (skb
->next
) {
2367 return orig_len
- len
;
2370 return orig_len
== len
? ret
: orig_len
- len
;
2372 EXPORT_SYMBOL_GPL(skb_send_sock_locked
);
2374 /* Send skb data on a socket. */
2375 int skb_send_sock(struct sock
*sk
, struct sk_buff
*skb
, int offset
, int len
)
2380 ret
= skb_send_sock_locked(sk
, skb
, offset
, len
);
2385 EXPORT_SYMBOL_GPL(skb_send_sock
);
2388 * skb_store_bits - store bits from kernel buffer to skb
2389 * @skb: destination buffer
2390 * @offset: offset in destination
2391 * @from: source buffer
2392 * @len: number of bytes to copy
2394 * Copy the specified number of bytes from the source buffer to the
2395 * destination skb. This function handles all the messy bits of
2396 * traversing fragment lists and such.
2399 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
2401 int start
= skb_headlen(skb
);
2402 struct sk_buff
*frag_iter
;
2405 if (offset
> (int)skb
->len
- len
)
2408 if ((copy
= start
- offset
) > 0) {
2411 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
2412 if ((len
-= copy
) == 0)
2418 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2419 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2422 WARN_ON(start
> offset
+ len
);
2424 end
= start
+ skb_frag_size(frag
);
2425 if ((copy
= end
- offset
) > 0) {
2426 u32 p_off
, p_len
, copied
;
2433 skb_frag_foreach_page(frag
,
2434 frag
->page_offset
+ offset
- start
,
2435 copy
, p
, p_off
, p_len
, copied
) {
2436 vaddr
= kmap_atomic(p
);
2437 memcpy(vaddr
+ p_off
, from
+ copied
, p_len
);
2438 kunmap_atomic(vaddr
);
2441 if ((len
-= copy
) == 0)
2449 skb_walk_frags(skb
, frag_iter
) {
2452 WARN_ON(start
> offset
+ len
);
2454 end
= start
+ frag_iter
->len
;
2455 if ((copy
= end
- offset
) > 0) {
2458 if (skb_store_bits(frag_iter
, offset
- start
,
2461 if ((len
-= copy
) == 0)
2474 EXPORT_SYMBOL(skb_store_bits
);
2476 /* Checksum skb data. */
2477 __wsum
__skb_checksum(const struct sk_buff
*skb
, int offset
, int len
,
2478 __wsum csum
, const struct skb_checksum_ops
*ops
)
2480 int start
= skb_headlen(skb
);
2481 int i
, copy
= start
- offset
;
2482 struct sk_buff
*frag_iter
;
2485 /* Checksum header. */
2489 csum
= ops
->update(skb
->data
+ offset
, copy
, csum
);
2490 if ((len
-= copy
) == 0)
2496 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2498 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2500 WARN_ON(start
> offset
+ len
);
2502 end
= start
+ skb_frag_size(frag
);
2503 if ((copy
= end
- offset
) > 0) {
2504 u32 p_off
, p_len
, copied
;
2512 skb_frag_foreach_page(frag
,
2513 frag
->page_offset
+ offset
- start
,
2514 copy
, p
, p_off
, p_len
, copied
) {
2515 vaddr
= kmap_atomic(p
);
2516 csum2
= ops
->update(vaddr
+ p_off
, p_len
, 0);
2517 kunmap_atomic(vaddr
);
2518 csum
= ops
->combine(csum
, csum2
, pos
, p_len
);
2529 skb_walk_frags(skb
, frag_iter
) {
2532 WARN_ON(start
> offset
+ len
);
2534 end
= start
+ frag_iter
->len
;
2535 if ((copy
= end
- offset
) > 0) {
2539 csum2
= __skb_checksum(frag_iter
, offset
- start
,
2541 csum
= ops
->combine(csum
, csum2
, pos
, copy
);
2542 if ((len
-= copy
) == 0)
2553 EXPORT_SYMBOL(__skb_checksum
);
2555 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
2556 int len
, __wsum csum
)
2558 const struct skb_checksum_ops ops
= {
2559 .update
= csum_partial_ext
,
2560 .combine
= csum_block_add_ext
,
2563 return __skb_checksum(skb
, offset
, len
, csum
, &ops
);
2565 EXPORT_SYMBOL(skb_checksum
);
2567 /* Both of above in one bottle. */
2569 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
2570 u8
*to
, int len
, __wsum csum
)
2572 int start
= skb_headlen(skb
);
2573 int i
, copy
= start
- offset
;
2574 struct sk_buff
*frag_iter
;
2581 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
2583 if ((len
-= copy
) == 0)
2590 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2593 WARN_ON(start
> offset
+ len
);
2595 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
2596 if ((copy
= end
- offset
) > 0) {
2597 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2598 u32 p_off
, p_len
, copied
;
2606 skb_frag_foreach_page(frag
,
2607 frag
->page_offset
+ offset
- start
,
2608 copy
, p
, p_off
, p_len
, copied
) {
2609 vaddr
= kmap_atomic(p
);
2610 csum2
= csum_partial_copy_nocheck(vaddr
+ p_off
,
2613 kunmap_atomic(vaddr
);
2614 csum
= csum_block_add(csum
, csum2
, pos
);
2626 skb_walk_frags(skb
, frag_iter
) {
2630 WARN_ON(start
> offset
+ len
);
2632 end
= start
+ frag_iter
->len
;
2633 if ((copy
= end
- offset
) > 0) {
2636 csum2
= skb_copy_and_csum_bits(frag_iter
,
2639 csum
= csum_block_add(csum
, csum2
, pos
);
2640 if ((len
-= copy
) == 0)
2651 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2653 static __wsum
warn_crc32c_csum_update(const void *buff
, int len
, __wsum sum
)
2655 net_warn_ratelimited(
2656 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2661 static __wsum
warn_crc32c_csum_combine(__wsum csum
, __wsum csum2
,
2662 int offset
, int len
)
2664 net_warn_ratelimited(
2665 "%s: attempt to compute crc32c without libcrc32c.ko\n",
2670 static const struct skb_checksum_ops default_crc32c_ops
= {
2671 .update
= warn_crc32c_csum_update
,
2672 .combine
= warn_crc32c_csum_combine
,
2675 const struct skb_checksum_ops
*crc32c_csum_stub __read_mostly
=
2676 &default_crc32c_ops
;
2677 EXPORT_SYMBOL(crc32c_csum_stub
);
2680 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2681 * @from: source buffer
2683 * Calculates the amount of linear headroom needed in the 'to' skb passed
2684 * into skb_zerocopy().
2687 skb_zerocopy_headlen(const struct sk_buff
*from
)
2689 unsigned int hlen
= 0;
2691 if (!from
->head_frag
||
2692 skb_headlen(from
) < L1_CACHE_BYTES
||
2693 skb_shinfo(from
)->nr_frags
>= MAX_SKB_FRAGS
)
2694 hlen
= skb_headlen(from
);
2696 if (skb_has_frag_list(from
))
2701 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen
);
2704 * skb_zerocopy - Zero copy skb to skb
2705 * @to: destination buffer
2706 * @from: source buffer
2707 * @len: number of bytes to copy from source buffer
2708 * @hlen: size of linear headroom in destination buffer
2710 * Copies up to `len` bytes from `from` to `to` by creating references
2711 * to the frags in the source buffer.
2713 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2714 * headroom in the `to` buffer.
2717 * 0: everything is OK
2718 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2719 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2722 skb_zerocopy(struct sk_buff
*to
, struct sk_buff
*from
, int len
, int hlen
)
2725 int plen
= 0; /* length of skb->head fragment */
2728 unsigned int offset
;
2730 BUG_ON(!from
->head_frag
&& !hlen
);
2732 /* dont bother with small payloads */
2733 if (len
<= skb_tailroom(to
))
2734 return skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
2737 ret
= skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
2742 plen
= min_t(int, skb_headlen(from
), len
);
2744 page
= virt_to_head_page(from
->head
);
2745 offset
= from
->data
- (unsigned char *)page_address(page
);
2746 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
2753 to
->truesize
+= len
+ plen
;
2754 to
->len
+= len
+ plen
;
2755 to
->data_len
+= len
+ plen
;
2757 if (unlikely(skb_orphan_frags(from
, GFP_ATOMIC
))) {
2761 skb_zerocopy_clone(to
, from
, GFP_ATOMIC
);
2763 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
2766 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
2767 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
2768 len
-= skb_shinfo(to
)->frags
[j
].size
;
2769 skb_frag_ref(to
, j
);
2772 skb_shinfo(to
)->nr_frags
= j
;
2776 EXPORT_SYMBOL_GPL(skb_zerocopy
);
2778 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
2783 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2784 csstart
= skb_checksum_start_offset(skb
);
2786 csstart
= skb_headlen(skb
);
2788 BUG_ON(csstart
> skb_headlen(skb
));
2790 skb_copy_from_linear_data(skb
, to
, csstart
);
2793 if (csstart
!= skb
->len
)
2794 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
2795 skb
->len
- csstart
, 0);
2797 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
2798 long csstuff
= csstart
+ skb
->csum_offset
;
2800 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
2803 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2806 * skb_dequeue - remove from the head of the queue
2807 * @list: list to dequeue from
2809 * Remove the head of the list. The list lock is taken so the function
2810 * may be used safely with other locking list functions. The head item is
2811 * returned or %NULL if the list is empty.
2814 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
2816 unsigned long flags
;
2817 struct sk_buff
*result
;
2819 spin_lock_irqsave(&list
->lock
, flags
);
2820 result
= __skb_dequeue(list
);
2821 spin_unlock_irqrestore(&list
->lock
, flags
);
2824 EXPORT_SYMBOL(skb_dequeue
);
2827 * skb_dequeue_tail - remove from the tail of the queue
2828 * @list: list to dequeue from
2830 * Remove the tail of the list. The list lock is taken so the function
2831 * may be used safely with other locking list functions. The tail item is
2832 * returned or %NULL if the list is empty.
2834 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
2836 unsigned long flags
;
2837 struct sk_buff
*result
;
2839 spin_lock_irqsave(&list
->lock
, flags
);
2840 result
= __skb_dequeue_tail(list
);
2841 spin_unlock_irqrestore(&list
->lock
, flags
);
2844 EXPORT_SYMBOL(skb_dequeue_tail
);
2847 * skb_queue_purge - empty a list
2848 * @list: list to empty
2850 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2851 * the list and one reference dropped. This function takes the list
2852 * lock and is atomic with respect to other list locking functions.
2854 void skb_queue_purge(struct sk_buff_head
*list
)
2856 struct sk_buff
*skb
;
2857 while ((skb
= skb_dequeue(list
)) != NULL
)
2860 EXPORT_SYMBOL(skb_queue_purge
);
2863 * skb_rbtree_purge - empty a skb rbtree
2864 * @root: root of the rbtree to empty
2865 * Return value: the sum of truesizes of all purged skbs.
2867 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2868 * the list and one reference dropped. This function does not take
2869 * any lock. Synchronization should be handled by the caller (e.g., TCP
2870 * out-of-order queue is protected by the socket lock).
2872 unsigned int skb_rbtree_purge(struct rb_root
*root
)
2874 struct rb_node
*p
= rb_first(root
);
2875 unsigned int sum
= 0;
2878 struct sk_buff
*skb
= rb_entry(p
, struct sk_buff
, rbnode
);
2881 rb_erase(&skb
->rbnode
, root
);
2882 sum
+= skb
->truesize
;
2889 * skb_queue_head - queue a buffer at the list head
2890 * @list: list to use
2891 * @newsk: buffer to queue
2893 * Queue a buffer at the start of the list. This function takes the
2894 * list lock and can be used safely with other locking &sk_buff functions
2897 * A buffer cannot be placed on two lists at the same time.
2899 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2901 unsigned long flags
;
2903 spin_lock_irqsave(&list
->lock
, flags
);
2904 __skb_queue_head(list
, newsk
);
2905 spin_unlock_irqrestore(&list
->lock
, flags
);
2907 EXPORT_SYMBOL(skb_queue_head
);
2910 * skb_queue_tail - queue a buffer at the list tail
2911 * @list: list to use
2912 * @newsk: buffer to queue
2914 * Queue a buffer at the tail of the list. This function takes the
2915 * list lock and can be used safely with other locking &sk_buff functions
2918 * A buffer cannot be placed on two lists at the same time.
2920 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
2922 unsigned long flags
;
2924 spin_lock_irqsave(&list
->lock
, flags
);
2925 __skb_queue_tail(list
, newsk
);
2926 spin_unlock_irqrestore(&list
->lock
, flags
);
2928 EXPORT_SYMBOL(skb_queue_tail
);
2931 * skb_unlink - remove a buffer from a list
2932 * @skb: buffer to remove
2933 * @list: list to use
2935 * Remove a packet from a list. The list locks are taken and this
2936 * function is atomic with respect to other list locked calls
2938 * You must know what list the SKB is on.
2940 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
2942 unsigned long flags
;
2944 spin_lock_irqsave(&list
->lock
, flags
);
2945 __skb_unlink(skb
, list
);
2946 spin_unlock_irqrestore(&list
->lock
, flags
);
2948 EXPORT_SYMBOL(skb_unlink
);
2951 * skb_append - append a buffer
2952 * @old: buffer to insert after
2953 * @newsk: buffer to insert
2954 * @list: list to use
2956 * Place a packet after a given packet in a list. The list locks are taken
2957 * and this function is atomic with respect to other list locked calls.
2958 * A buffer cannot be placed on two lists at the same time.
2960 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2962 unsigned long flags
;
2964 spin_lock_irqsave(&list
->lock
, flags
);
2965 __skb_queue_after(list
, old
, newsk
);
2966 spin_unlock_irqrestore(&list
->lock
, flags
);
2968 EXPORT_SYMBOL(skb_append
);
2971 * skb_insert - insert a buffer
2972 * @old: buffer to insert before
2973 * @newsk: buffer to insert
2974 * @list: list to use
2976 * Place a packet before a given packet in a list. The list locks are
2977 * taken and this function is atomic with respect to other list locked
2980 * A buffer cannot be placed on two lists at the same time.
2982 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
2984 unsigned long flags
;
2986 spin_lock_irqsave(&list
->lock
, flags
);
2987 __skb_insert(newsk
, old
->prev
, old
, list
);
2988 spin_unlock_irqrestore(&list
->lock
, flags
);
2990 EXPORT_SYMBOL(skb_insert
);
2992 static inline void skb_split_inside_header(struct sk_buff
*skb
,
2993 struct sk_buff
* skb1
,
2994 const u32 len
, const int pos
)
2998 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
3000 /* And move data appendix as is. */
3001 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
3002 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
3004 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
3005 skb_shinfo(skb
)->nr_frags
= 0;
3006 skb1
->data_len
= skb
->data_len
;
3007 skb1
->len
+= skb1
->data_len
;
3010 skb_set_tail_pointer(skb
, len
);
3013 static inline void skb_split_no_header(struct sk_buff
*skb
,
3014 struct sk_buff
* skb1
,
3015 const u32 len
, int pos
)
3018 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
3020 skb_shinfo(skb
)->nr_frags
= 0;
3021 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
3023 skb
->data_len
= len
- pos
;
3025 for (i
= 0; i
< nfrags
; i
++) {
3026 int size
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
3028 if (pos
+ size
> len
) {
3029 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
3033 * We have two variants in this case:
3034 * 1. Move all the frag to the second
3035 * part, if it is possible. F.e.
3036 * this approach is mandatory for TUX,
3037 * where splitting is expensive.
3038 * 2. Split is accurately. We make this.
3040 skb_frag_ref(skb
, i
);
3041 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
3042 skb_frag_size_sub(&skb_shinfo(skb1
)->frags
[0], len
- pos
);
3043 skb_frag_size_set(&skb_shinfo(skb
)->frags
[i
], len
- pos
);
3044 skb_shinfo(skb
)->nr_frags
++;
3048 skb_shinfo(skb
)->nr_frags
++;
3051 skb_shinfo(skb1
)->nr_frags
= k
;
3055 * skb_split - Split fragmented skb to two parts at length len.
3056 * @skb: the buffer to split
3057 * @skb1: the buffer to receive the second part
3058 * @len: new length for skb
3060 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
3062 int pos
= skb_headlen(skb
);
3064 skb_shinfo(skb1
)->tx_flags
|= skb_shinfo(skb
)->tx_flags
&
3066 skb_zerocopy_clone(skb1
, skb
, 0);
3067 if (len
< pos
) /* Split line is inside header. */
3068 skb_split_inside_header(skb
, skb1
, len
, pos
);
3069 else /* Second chunk has no header, nothing to copy. */
3070 skb_split_no_header(skb
, skb1
, len
, pos
);
3072 EXPORT_SYMBOL(skb_split
);
3074 /* Shifting from/to a cloned skb is a no-go.
3076 * Caller cannot keep skb_shinfo related pointers past calling here!
3078 static int skb_prepare_for_shift(struct sk_buff
*skb
)
3080 return skb_cloned(skb
) && pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
3084 * skb_shift - Shifts paged data partially from skb to another
3085 * @tgt: buffer into which tail data gets added
3086 * @skb: buffer from which the paged data comes from
3087 * @shiftlen: shift up to this many bytes
3089 * Attempts to shift up to shiftlen worth of bytes, which may be less than
3090 * the length of the skb, from skb to tgt. Returns number bytes shifted.
3091 * It's up to caller to free skb if everything was shifted.
3093 * If @tgt runs out of frags, the whole operation is aborted.
3095 * Skb cannot include anything else but paged data while tgt is allowed
3096 * to have non-paged data as well.
3098 * TODO: full sized shift could be optimized but that would need
3099 * specialized skb free'er to handle frags without up-to-date nr_frags.
3101 int skb_shift(struct sk_buff
*tgt
, struct sk_buff
*skb
, int shiftlen
)
3103 int from
, to
, merge
, todo
;
3104 struct skb_frag_struct
*fragfrom
, *fragto
;
3106 BUG_ON(shiftlen
> skb
->len
);
3108 if (skb_headlen(skb
))
3110 if (skb_zcopy(tgt
) || skb_zcopy(skb
))
3115 to
= skb_shinfo(tgt
)->nr_frags
;
3116 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3118 /* Actual merge is delayed until the point when we know we can
3119 * commit all, so that we don't have to undo partial changes
3122 !skb_can_coalesce(tgt
, to
, skb_frag_page(fragfrom
),
3123 fragfrom
->page_offset
)) {
3128 todo
-= skb_frag_size(fragfrom
);
3130 if (skb_prepare_for_shift(skb
) ||
3131 skb_prepare_for_shift(tgt
))
3134 /* All previous frag pointers might be stale! */
3135 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3136 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3138 skb_frag_size_add(fragto
, shiftlen
);
3139 skb_frag_size_sub(fragfrom
, shiftlen
);
3140 fragfrom
->page_offset
+= shiftlen
;
3148 /* Skip full, not-fitting skb to avoid expensive operations */
3149 if ((shiftlen
== skb
->len
) &&
3150 (skb_shinfo(skb
)->nr_frags
- from
) > (MAX_SKB_FRAGS
- to
))
3153 if (skb_prepare_for_shift(skb
) || skb_prepare_for_shift(tgt
))
3156 while ((todo
> 0) && (from
< skb_shinfo(skb
)->nr_frags
)) {
3157 if (to
== MAX_SKB_FRAGS
)
3160 fragfrom
= &skb_shinfo(skb
)->frags
[from
];
3161 fragto
= &skb_shinfo(tgt
)->frags
[to
];
3163 if (todo
>= skb_frag_size(fragfrom
)) {
3164 *fragto
= *fragfrom
;
3165 todo
-= skb_frag_size(fragfrom
);
3170 __skb_frag_ref(fragfrom
);
3171 fragto
->page
= fragfrom
->page
;
3172 fragto
->page_offset
= fragfrom
->page_offset
;
3173 skb_frag_size_set(fragto
, todo
);
3175 fragfrom
->page_offset
+= todo
;
3176 skb_frag_size_sub(fragfrom
, todo
);
3184 /* Ready to "commit" this state change to tgt */
3185 skb_shinfo(tgt
)->nr_frags
= to
;
3188 fragfrom
= &skb_shinfo(skb
)->frags
[0];
3189 fragto
= &skb_shinfo(tgt
)->frags
[merge
];
3191 skb_frag_size_add(fragto
, skb_frag_size(fragfrom
));
3192 __skb_frag_unref(fragfrom
);
3195 /* Reposition in the original skb */
3197 while (from
< skb_shinfo(skb
)->nr_frags
)
3198 skb_shinfo(skb
)->frags
[to
++] = skb_shinfo(skb
)->frags
[from
++];
3199 skb_shinfo(skb
)->nr_frags
= to
;
3201 BUG_ON(todo
> 0 && !skb_shinfo(skb
)->nr_frags
);
3204 /* Most likely the tgt won't ever need its checksum anymore, skb on
3205 * the other hand might need it if it needs to be resent
3207 tgt
->ip_summed
= CHECKSUM_PARTIAL
;
3208 skb
->ip_summed
= CHECKSUM_PARTIAL
;
3210 /* Yak, is it really working this way? Some helper please? */
3211 skb
->len
-= shiftlen
;
3212 skb
->data_len
-= shiftlen
;
3213 skb
->truesize
-= shiftlen
;
3214 tgt
->len
+= shiftlen
;
3215 tgt
->data_len
+= shiftlen
;
3216 tgt
->truesize
+= shiftlen
;
3222 * skb_prepare_seq_read - Prepare a sequential read of skb data
3223 * @skb: the buffer to read
3224 * @from: lower offset of data to be read
3225 * @to: upper offset of data to be read
3226 * @st: state variable
3228 * Initializes the specified state variable. Must be called before
3229 * invoking skb_seq_read() for the first time.
3231 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
3232 unsigned int to
, struct skb_seq_state
*st
)
3234 st
->lower_offset
= from
;
3235 st
->upper_offset
= to
;
3236 st
->root_skb
= st
->cur_skb
= skb
;
3237 st
->frag_idx
= st
->stepped_offset
= 0;
3238 st
->frag_data
= NULL
;
3240 EXPORT_SYMBOL(skb_prepare_seq_read
);
3243 * skb_seq_read - Sequentially read skb data
3244 * @consumed: number of bytes consumed by the caller so far
3245 * @data: destination pointer for data to be returned
3246 * @st: state variable
3248 * Reads a block of skb data at @consumed relative to the
3249 * lower offset specified to skb_prepare_seq_read(). Assigns
3250 * the head of the data block to @data and returns the length
3251 * of the block or 0 if the end of the skb data or the upper
3252 * offset has been reached.
3254 * The caller is not required to consume all of the data
3255 * returned, i.e. @consumed is typically set to the number
3256 * of bytes already consumed and the next call to
3257 * skb_seq_read() will return the remaining part of the block.
3259 * Note 1: The size of each block of data returned can be arbitrary,
3260 * this limitation is the cost for zerocopy sequential
3261 * reads of potentially non linear data.
3263 * Note 2: Fragment lists within fragments are not implemented
3264 * at the moment, state->root_skb could be replaced with
3265 * a stack for this purpose.
3267 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
3268 struct skb_seq_state
*st
)
3270 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
3273 if (unlikely(abs_offset
>= st
->upper_offset
)) {
3274 if (st
->frag_data
) {
3275 kunmap_atomic(st
->frag_data
);
3276 st
->frag_data
= NULL
;
3282 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
3284 if (abs_offset
< block_limit
&& !st
->frag_data
) {
3285 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
3286 return block_limit
- abs_offset
;
3289 if (st
->frag_idx
== 0 && !st
->frag_data
)
3290 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
3292 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
3293 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
3294 block_limit
= skb_frag_size(frag
) + st
->stepped_offset
;
3296 if (abs_offset
< block_limit
) {
3298 st
->frag_data
= kmap_atomic(skb_frag_page(frag
));
3300 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
3301 (abs_offset
- st
->stepped_offset
);
3303 return block_limit
- abs_offset
;
3306 if (st
->frag_data
) {
3307 kunmap_atomic(st
->frag_data
);
3308 st
->frag_data
= NULL
;
3312 st
->stepped_offset
+= skb_frag_size(frag
);
3315 if (st
->frag_data
) {
3316 kunmap_atomic(st
->frag_data
);
3317 st
->frag_data
= NULL
;
3320 if (st
->root_skb
== st
->cur_skb
&& skb_has_frag_list(st
->root_skb
)) {
3321 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
3324 } else if (st
->cur_skb
->next
) {
3325 st
->cur_skb
= st
->cur_skb
->next
;
3332 EXPORT_SYMBOL(skb_seq_read
);
3335 * skb_abort_seq_read - Abort a sequential read of skb data
3336 * @st: state variable
3338 * Must be called if skb_seq_read() was not called until it
3341 void skb_abort_seq_read(struct skb_seq_state
*st
)
3344 kunmap_atomic(st
->frag_data
);
3346 EXPORT_SYMBOL(skb_abort_seq_read
);
3348 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
3350 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
3351 struct ts_config
*conf
,
3352 struct ts_state
*state
)
3354 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
3357 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
3359 skb_abort_seq_read(TS_SKB_CB(state
));
3363 * skb_find_text - Find a text pattern in skb data
3364 * @skb: the buffer to look in
3365 * @from: search offset
3367 * @config: textsearch configuration
3369 * Finds a pattern in the skb data according to the specified
3370 * textsearch configuration. Use textsearch_next() to retrieve
3371 * subsequent occurrences of the pattern. Returns the offset
3372 * to the first occurrence or UINT_MAX if no match was found.
3374 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
3375 unsigned int to
, struct ts_config
*config
)
3377 struct ts_state state
;
3380 config
->get_next_block
= skb_ts_get_next_block
;
3381 config
->finish
= skb_ts_finish
;
3383 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(&state
));
3385 ret
= textsearch_find(config
, &state
);
3386 return (ret
<= to
- from
? ret
: UINT_MAX
);
3388 EXPORT_SYMBOL(skb_find_text
);
3391 * skb_append_datato_frags - append the user data to a skb
3392 * @sk: sock structure
3393 * @skb: skb structure to be appended with user data.
3394 * @getfrag: call back function to be used for getting the user data
3395 * @from: pointer to user message iov
3396 * @length: length of the iov message
3398 * Description: This procedure append the user data in the fragment part
3399 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3401 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
3402 int (*getfrag
)(void *from
, char *to
, int offset
,
3403 int len
, int odd
, struct sk_buff
*skb
),
3404 void *from
, int length
)
3406 int frg_cnt
= skb_shinfo(skb
)->nr_frags
;
3410 struct page_frag
*pfrag
= ¤t
->task_frag
;
3413 /* Return error if we don't have space for new frag */
3414 if (frg_cnt
>= MAX_SKB_FRAGS
)
3417 if (!sk_page_frag_refill(sk
, pfrag
))
3420 /* copy the user data to page */
3421 copy
= min_t(int, length
, pfrag
->size
- pfrag
->offset
);
3423 ret
= getfrag(from
, page_address(pfrag
->page
) + pfrag
->offset
,
3424 offset
, copy
, 0, skb
);
3428 /* copy was successful so update the size parameters */
3429 skb_fill_page_desc(skb
, frg_cnt
, pfrag
->page
, pfrag
->offset
,
3432 pfrag
->offset
+= copy
;
3433 get_page(pfrag
->page
);
3435 skb
->truesize
+= copy
;
3436 refcount_add(copy
, &sk
->sk_wmem_alloc
);
3438 skb
->data_len
+= copy
;
3442 } while (length
> 0);
3446 EXPORT_SYMBOL(skb_append_datato_frags
);
3448 int skb_append_pagefrags(struct sk_buff
*skb
, struct page
*page
,
3449 int offset
, size_t size
)
3451 int i
= skb_shinfo(skb
)->nr_frags
;
3453 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
3454 skb_frag_size_add(&skb_shinfo(skb
)->frags
[i
- 1], size
);
3455 } else if (i
< MAX_SKB_FRAGS
) {
3457 skb_fill_page_desc(skb
, i
, page
, offset
, size
);
3464 EXPORT_SYMBOL_GPL(skb_append_pagefrags
);
3467 * skb_pull_rcsum - pull skb and update receive checksum
3468 * @skb: buffer to update
3469 * @len: length of data pulled
3471 * This function performs an skb_pull on the packet and updates
3472 * the CHECKSUM_COMPLETE checksum. It should be used on
3473 * receive path processing instead of skb_pull unless you know
3474 * that the checksum difference is zero (e.g., a valid IP header)
3475 * or you are setting ip_summed to CHECKSUM_NONE.
3477 void *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
3479 unsigned char *data
= skb
->data
;
3481 BUG_ON(len
> skb
->len
);
3482 __skb_pull(skb
, len
);
3483 skb_postpull_rcsum(skb
, data
, len
);
3486 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
3488 static inline skb_frag_t
skb_head_frag_to_page_desc(struct sk_buff
*frag_skb
)
3490 skb_frag_t head_frag
;
3493 page
= virt_to_head_page(frag_skb
->head
);
3494 head_frag
.page
.p
= page
;
3495 head_frag
.page_offset
= frag_skb
->data
-
3496 (unsigned char *)page_address(page
);
3497 head_frag
.size
= skb_headlen(frag_skb
);
3502 * skb_segment - Perform protocol segmentation on skb.
3503 * @head_skb: buffer to segment
3504 * @features: features for the output path (see dev->features)
3506 * This function performs segmentation on the given skb. It returns
3507 * a pointer to the first in a list of new skbs for the segments.
3508 * In case of error it returns ERR_PTR(err).
3510 struct sk_buff
*skb_segment(struct sk_buff
*head_skb
,
3511 netdev_features_t features
)
3513 struct sk_buff
*segs
= NULL
;
3514 struct sk_buff
*tail
= NULL
;
3515 struct sk_buff
*list_skb
= skb_shinfo(head_skb
)->frag_list
;
3516 skb_frag_t
*frag
= skb_shinfo(head_skb
)->frags
;
3517 unsigned int mss
= skb_shinfo(head_skb
)->gso_size
;
3518 unsigned int doffset
= head_skb
->data
- skb_mac_header(head_skb
);
3519 struct sk_buff
*frag_skb
= head_skb
;
3520 unsigned int offset
= doffset
;
3521 unsigned int tnl_hlen
= skb_tnl_header_len(head_skb
);
3522 unsigned int partial_segs
= 0;
3523 unsigned int headroom
;
3524 unsigned int len
= head_skb
->len
;
3527 int nfrags
= skb_shinfo(head_skb
)->nr_frags
;
3533 if (list_skb
&& !list_skb
->head_frag
&& skb_headlen(list_skb
) &&
3534 (skb_shinfo(head_skb
)->gso_type
& SKB_GSO_DODGY
)) {
3535 /* gso_size is untrusted, and we have a frag_list with a linear
3536 * non head_frag head.
3538 * (we assume checking the first list_skb member suffices;
3539 * i.e if either of the list_skb members have non head_frag
3540 * head, then the first one has too).
3542 * If head_skb's headlen does not fit requested gso_size, it
3543 * means that the frag_list members do NOT terminate on exact
3544 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3545 * sharing. Therefore we must fallback to copying the frag_list
3546 * skbs; we do so by disabling SG.
3548 if (mss
!= GSO_BY_FRAGS
&& mss
!= skb_headlen(head_skb
))
3549 features
&= ~NETIF_F_SG
;
3552 __skb_push(head_skb
, doffset
);
3553 proto
= skb_network_protocol(head_skb
, &dummy
);
3554 if (unlikely(!proto
))
3555 return ERR_PTR(-EINVAL
);
3557 sg
= !!(features
& NETIF_F_SG
);
3558 csum
= !!can_checksum_protocol(features
, proto
);
3560 if (sg
&& csum
&& (mss
!= GSO_BY_FRAGS
)) {
3561 if (!(features
& NETIF_F_GSO_PARTIAL
)) {
3562 struct sk_buff
*iter
;
3563 unsigned int frag_len
;
3566 !net_gso_ok(features
, skb_shinfo(head_skb
)->gso_type
))
3569 /* If we get here then all the required
3570 * GSO features except frag_list are supported.
3571 * Try to split the SKB to multiple GSO SKBs
3572 * with no frag_list.
3573 * Currently we can do that only when the buffers don't
3574 * have a linear part and all the buffers except
3575 * the last are of the same length.
3577 frag_len
= list_skb
->len
;
3578 skb_walk_frags(head_skb
, iter
) {
3579 if (frag_len
!= iter
->len
&& iter
->next
)
3581 if (skb_headlen(iter
) && !iter
->head_frag
)
3587 if (len
!= frag_len
)
3591 /* GSO partial only requires that we trim off any excess that
3592 * doesn't fit into an MSS sized block, so take care of that
3595 partial_segs
= len
/ mss
;
3596 if (partial_segs
> 1)
3597 mss
*= partial_segs
;
3603 headroom
= skb_headroom(head_skb
);
3604 pos
= skb_headlen(head_skb
);
3607 struct sk_buff
*nskb
;
3608 skb_frag_t
*nskb_frag
;
3612 if (unlikely(mss
== GSO_BY_FRAGS
)) {
3613 len
= list_skb
->len
;
3615 len
= head_skb
->len
- offset
;
3620 hsize
= skb_headlen(head_skb
) - offset
;
3623 if (hsize
> len
|| !sg
)
3626 if (!hsize
&& i
>= nfrags
&& skb_headlen(list_skb
) &&
3627 (skb_headlen(list_skb
) == len
|| sg
)) {
3628 BUG_ON(skb_headlen(list_skb
) > len
);
3631 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3632 frag
= skb_shinfo(list_skb
)->frags
;
3633 frag_skb
= list_skb
;
3634 pos
+= skb_headlen(list_skb
);
3636 while (pos
< offset
+ len
) {
3637 BUG_ON(i
>= nfrags
);
3639 size
= skb_frag_size(frag
);
3640 if (pos
+ size
> offset
+ len
)
3648 nskb
= skb_clone(list_skb
, GFP_ATOMIC
);
3649 list_skb
= list_skb
->next
;
3651 if (unlikely(!nskb
))
3654 if (unlikely(pskb_trim(nskb
, len
))) {
3659 hsize
= skb_end_offset(nskb
);
3660 if (skb_cow_head(nskb
, doffset
+ headroom
)) {
3665 nskb
->truesize
+= skb_end_offset(nskb
) - hsize
;
3666 skb_release_head_state(nskb
);
3667 __skb_push(nskb
, doffset
);
3669 nskb
= __alloc_skb(hsize
+ doffset
+ headroom
,
3670 GFP_ATOMIC
, skb_alloc_rx_flag(head_skb
),
3673 if (unlikely(!nskb
))
3676 skb_reserve(nskb
, headroom
);
3677 __skb_put(nskb
, doffset
);
3686 __copy_skb_header(nskb
, head_skb
);
3688 skb_headers_offset_update(nskb
, skb_headroom(nskb
) - headroom
);
3689 skb_reset_mac_len(nskb
);
3691 skb_copy_from_linear_data_offset(head_skb
, -tnl_hlen
,
3692 nskb
->data
- tnl_hlen
,
3693 doffset
+ tnl_hlen
);
3695 if (nskb
->len
== len
+ doffset
)
3696 goto perform_csum_check
;
3699 if (!nskb
->remcsum_offload
)
3700 nskb
->ip_summed
= CHECKSUM_NONE
;
3701 SKB_GSO_CB(nskb
)->csum
=
3702 skb_copy_and_csum_bits(head_skb
, offset
,
3705 SKB_GSO_CB(nskb
)->csum_start
=
3706 skb_headroom(nskb
) + doffset
;
3710 nskb_frag
= skb_shinfo(nskb
)->frags
;
3712 skb_copy_from_linear_data_offset(head_skb
, offset
,
3713 skb_put(nskb
, hsize
), hsize
);
3715 skb_shinfo(nskb
)->tx_flags
|= skb_shinfo(head_skb
)->tx_flags
&
3718 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3719 skb_zerocopy_clone(nskb
, frag_skb
, GFP_ATOMIC
))
3722 while (pos
< offset
+ len
) {
3725 nfrags
= skb_shinfo(list_skb
)->nr_frags
;
3726 frag
= skb_shinfo(list_skb
)->frags
;
3727 frag_skb
= list_skb
;
3728 if (!skb_headlen(list_skb
)) {
3731 BUG_ON(!list_skb
->head_frag
);
3733 /* to make room for head_frag. */
3737 if (skb_orphan_frags(frag_skb
, GFP_ATOMIC
) ||
3738 skb_zerocopy_clone(nskb
, frag_skb
,
3742 list_skb
= list_skb
->next
;
3745 if (unlikely(skb_shinfo(nskb
)->nr_frags
>=
3747 net_warn_ratelimited(
3748 "skb_segment: too many frags: %u %u\n",
3754 *nskb_frag
= (i
< 0) ? skb_head_frag_to_page_desc(frag_skb
) : *frag
;
3755 __skb_frag_ref(nskb_frag
);
3756 size
= skb_frag_size(nskb_frag
);
3759 nskb_frag
->page_offset
+= offset
- pos
;
3760 skb_frag_size_sub(nskb_frag
, offset
- pos
);
3763 skb_shinfo(nskb
)->nr_frags
++;
3765 if (pos
+ size
<= offset
+ len
) {
3770 skb_frag_size_sub(nskb_frag
, pos
+ size
- (offset
+ len
));
3778 nskb
->data_len
= len
- hsize
;
3779 nskb
->len
+= nskb
->data_len
;
3780 nskb
->truesize
+= nskb
->data_len
;
3784 if (skb_has_shared_frag(nskb
) &&
3785 __skb_linearize(nskb
))
3788 if (!nskb
->remcsum_offload
)
3789 nskb
->ip_summed
= CHECKSUM_NONE
;
3790 SKB_GSO_CB(nskb
)->csum
=
3791 skb_checksum(nskb
, doffset
,
3792 nskb
->len
- doffset
, 0);
3793 SKB_GSO_CB(nskb
)->csum_start
=
3794 skb_headroom(nskb
) + doffset
;
3796 } while ((offset
+= len
) < head_skb
->len
);
3798 /* Some callers want to get the end of the list.
3799 * Put it in segs->prev to avoid walking the list.
3800 * (see validate_xmit_skb_list() for example)
3805 struct sk_buff
*iter
;
3806 int type
= skb_shinfo(head_skb
)->gso_type
;
3807 unsigned short gso_size
= skb_shinfo(head_skb
)->gso_size
;
3809 /* Update type to add partial and then remove dodgy if set */
3810 type
|= (features
& NETIF_F_GSO_PARTIAL
) / NETIF_F_GSO_PARTIAL
* SKB_GSO_PARTIAL
;
3811 type
&= ~SKB_GSO_DODGY
;
3813 /* Update GSO info and prepare to start updating headers on
3814 * our way back down the stack of protocols.
3816 for (iter
= segs
; iter
; iter
= iter
->next
) {
3817 skb_shinfo(iter
)->gso_size
= gso_size
;
3818 skb_shinfo(iter
)->gso_segs
= partial_segs
;
3819 skb_shinfo(iter
)->gso_type
= type
;
3820 SKB_GSO_CB(iter
)->data_offset
= skb_headroom(iter
) + doffset
;
3823 if (tail
->len
- doffset
<= gso_size
)
3824 skb_shinfo(tail
)->gso_size
= 0;
3825 else if (tail
!= segs
)
3826 skb_shinfo(tail
)->gso_segs
= DIV_ROUND_UP(tail
->len
- doffset
, gso_size
);
3829 /* Following permits correct backpressure, for protocols
3830 * using skb_set_owner_w().
3831 * Idea is to tranfert ownership from head_skb to last segment.
3833 if (head_skb
->destructor
== sock_wfree
) {
3834 swap(tail
->truesize
, head_skb
->truesize
);
3835 swap(tail
->destructor
, head_skb
->destructor
);
3836 swap(tail
->sk
, head_skb
->sk
);
3841 kfree_skb_list(segs
);
3842 return ERR_PTR(err
);
3844 EXPORT_SYMBOL_GPL(skb_segment
);
3846 int skb_gro_receive(struct sk_buff
*p
, struct sk_buff
*skb
)
3848 struct skb_shared_info
*pinfo
, *skbinfo
= skb_shinfo(skb
);
3849 unsigned int offset
= skb_gro_offset(skb
);
3850 unsigned int headlen
= skb_headlen(skb
);
3851 unsigned int len
= skb_gro_len(skb
);
3852 unsigned int delta_truesize
;
3855 if (unlikely(p
->len
+ len
>= 65536 || NAPI_GRO_CB(skb
)->flush
))
3858 lp
= NAPI_GRO_CB(p
)->last
;
3859 pinfo
= skb_shinfo(lp
);
3861 if (headlen
<= offset
) {
3864 int i
= skbinfo
->nr_frags
;
3865 int nr_frags
= pinfo
->nr_frags
+ i
;
3867 if (nr_frags
> MAX_SKB_FRAGS
)
3871 pinfo
->nr_frags
= nr_frags
;
3872 skbinfo
->nr_frags
= 0;
3874 frag
= pinfo
->frags
+ nr_frags
;
3875 frag2
= skbinfo
->frags
+ i
;
3880 frag
->page_offset
+= offset
;
3881 skb_frag_size_sub(frag
, offset
);
3883 /* all fragments truesize : remove (head size + sk_buff) */
3884 delta_truesize
= skb
->truesize
-
3885 SKB_TRUESIZE(skb_end_offset(skb
));
3887 skb
->truesize
-= skb
->data_len
;
3888 skb
->len
-= skb
->data_len
;
3891 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE
;
3893 } else if (skb
->head_frag
) {
3894 int nr_frags
= pinfo
->nr_frags
;
3895 skb_frag_t
*frag
= pinfo
->frags
+ nr_frags
;
3896 struct page
*page
= virt_to_head_page(skb
->head
);
3897 unsigned int first_size
= headlen
- offset
;
3898 unsigned int first_offset
;
3900 if (nr_frags
+ 1 + skbinfo
->nr_frags
> MAX_SKB_FRAGS
)
3903 first_offset
= skb
->data
-
3904 (unsigned char *)page_address(page
) +
3907 pinfo
->nr_frags
= nr_frags
+ 1 + skbinfo
->nr_frags
;
3909 frag
->page
.p
= page
;
3910 frag
->page_offset
= first_offset
;
3911 skb_frag_size_set(frag
, first_size
);
3913 memcpy(frag
+ 1, skbinfo
->frags
, sizeof(*frag
) * skbinfo
->nr_frags
);
3914 /* We dont need to clear skbinfo->nr_frags here */
3916 delta_truesize
= skb
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
3917 NAPI_GRO_CB(skb
)->free
= NAPI_GRO_FREE_STOLEN_HEAD
;
3922 delta_truesize
= skb
->truesize
;
3923 if (offset
> headlen
) {
3924 unsigned int eat
= offset
- headlen
;
3926 skbinfo
->frags
[0].page_offset
+= eat
;
3927 skb_frag_size_sub(&skbinfo
->frags
[0], eat
);
3928 skb
->data_len
-= eat
;
3933 __skb_pull(skb
, offset
);
3935 if (NAPI_GRO_CB(p
)->last
== p
)
3936 skb_shinfo(p
)->frag_list
= skb
;
3938 NAPI_GRO_CB(p
)->last
->next
= skb
;
3939 NAPI_GRO_CB(p
)->last
= skb
;
3940 __skb_header_release(skb
);
3944 NAPI_GRO_CB(p
)->count
++;
3946 p
->truesize
+= delta_truesize
;
3949 lp
->data_len
+= len
;
3950 lp
->truesize
+= delta_truesize
;
3953 NAPI_GRO_CB(skb
)->same_flow
= 1;
3956 EXPORT_SYMBOL_GPL(skb_gro_receive
);
3958 void __init
skb_init(void)
3960 skbuff_head_cache
= kmem_cache_create_usercopy("skbuff_head_cache",
3961 sizeof(struct sk_buff
),
3963 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3964 offsetof(struct sk_buff
, cb
),
3965 sizeof_field(struct sk_buff
, cb
),
3967 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
3968 sizeof(struct sk_buff_fclones
),
3970 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
3975 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
,
3976 unsigned int recursion_level
)
3978 int start
= skb_headlen(skb
);
3979 int i
, copy
= start
- offset
;
3980 struct sk_buff
*frag_iter
;
3983 if (unlikely(recursion_level
>= 24))
3989 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
3991 if ((len
-= copy
) == 0)
3996 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
3999 WARN_ON(start
> offset
+ len
);
4001 end
= start
+ skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
4002 if ((copy
= end
- offset
) > 0) {
4003 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
4004 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4009 sg_set_page(&sg
[elt
], skb_frag_page(frag
), copy
,
4010 frag
->page_offset
+offset
-start
);
4019 skb_walk_frags(skb
, frag_iter
) {
4022 WARN_ON(start
> offset
+ len
);
4024 end
= start
+ frag_iter
->len
;
4025 if ((copy
= end
- offset
) > 0) {
4026 if (unlikely(elt
&& sg_is_last(&sg
[elt
- 1])))
4031 ret
= __skb_to_sgvec(frag_iter
, sg
+elt
, offset
- start
,
4032 copy
, recursion_level
+ 1);
4033 if (unlikely(ret
< 0))
4036 if ((len
-= copy
) == 0)
4047 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4048 * @skb: Socket buffer containing the buffers to be mapped
4049 * @sg: The scatter-gather list to map into
4050 * @offset: The offset into the buffer's contents to start mapping
4051 * @len: Length of buffer space to be mapped
4053 * Fill the specified scatter-gather list with mappings/pointers into a
4054 * region of the buffer space attached to a socket buffer. Returns either
4055 * the number of scatterlist items used, or -EMSGSIZE if the contents
4058 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
4060 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4065 sg_mark_end(&sg
[nsg
- 1]);
4069 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
4071 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4072 * sglist without mark the sg which contain last skb data as the end.
4073 * So the caller can mannipulate sg list as will when padding new data after
4074 * the first call without calling sg_unmark_end to expend sg list.
4076 * Scenario to use skb_to_sgvec_nomark:
4078 * 2. skb_to_sgvec_nomark(payload1)
4079 * 3. skb_to_sgvec_nomark(payload2)
4081 * This is equivalent to:
4083 * 2. skb_to_sgvec(payload1)
4085 * 4. skb_to_sgvec(payload2)
4087 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4088 * is more preferable.
4090 int skb_to_sgvec_nomark(struct sk_buff
*skb
, struct scatterlist
*sg
,
4091 int offset
, int len
)
4093 return __skb_to_sgvec(skb
, sg
, offset
, len
, 0);
4095 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark
);
4100 * skb_cow_data - Check that a socket buffer's data buffers are writable
4101 * @skb: The socket buffer to check.
4102 * @tailbits: Amount of trailing space to be added
4103 * @trailer: Returned pointer to the skb where the @tailbits space begins
4105 * Make sure that the data buffers attached to a socket buffer are
4106 * writable. If they are not, private copies are made of the data buffers
4107 * and the socket buffer is set to use these instead.
4109 * If @tailbits is given, make sure that there is space to write @tailbits
4110 * bytes of data beyond current end of socket buffer. @trailer will be
4111 * set to point to the skb in which this space begins.
4113 * The number of scatterlist elements required to completely map the
4114 * COW'd and extended socket buffer will be returned.
4116 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
4120 struct sk_buff
*skb1
, **skb_p
;
4122 /* If skb is cloned or its head is paged, reallocate
4123 * head pulling out all the pages (pages are considered not writable
4124 * at the moment even if they are anonymous).
4126 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
4127 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
4130 /* Easy case. Most of packets will go this way. */
4131 if (!skb_has_frag_list(skb
)) {
4132 /* A little of trouble, not enough of space for trailer.
4133 * This should not happen, when stack is tuned to generate
4134 * good frames. OK, on miss we reallocate and reserve even more
4135 * space, 128 bytes is fair. */
4137 if (skb_tailroom(skb
) < tailbits
&&
4138 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
4146 /* Misery. We are in troubles, going to mincer fragments... */
4149 skb_p
= &skb_shinfo(skb
)->frag_list
;
4152 while ((skb1
= *skb_p
) != NULL
) {
4155 /* The fragment is partially pulled by someone,
4156 * this can happen on input. Copy it and everything
4159 if (skb_shared(skb1
))
4162 /* If the skb is the last, worry about trailer. */
4164 if (skb1
->next
== NULL
&& tailbits
) {
4165 if (skb_shinfo(skb1
)->nr_frags
||
4166 skb_has_frag_list(skb1
) ||
4167 skb_tailroom(skb1
) < tailbits
)
4168 ntail
= tailbits
+ 128;
4174 skb_shinfo(skb1
)->nr_frags
||
4175 skb_has_frag_list(skb1
)) {
4176 struct sk_buff
*skb2
;
4178 /* Fuck, we are miserable poor guys... */
4180 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
4182 skb2
= skb_copy_expand(skb1
,
4186 if (unlikely(skb2
== NULL
))
4190 skb_set_owner_w(skb2
, skb1
->sk
);
4192 /* Looking around. Are we still alive?
4193 * OK, link new skb, drop old one */
4195 skb2
->next
= skb1
->next
;
4202 skb_p
= &skb1
->next
;
4207 EXPORT_SYMBOL_GPL(skb_cow_data
);
4209 static void sock_rmem_free(struct sk_buff
*skb
)
4211 struct sock
*sk
= skb
->sk
;
4213 atomic_sub(skb
->truesize
, &sk
->sk_rmem_alloc
);
4216 static void skb_set_err_queue(struct sk_buff
*skb
)
4218 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4219 * So, it is safe to (mis)use it to mark skbs on the error queue.
4221 skb
->pkt_type
= PACKET_OUTGOING
;
4222 BUILD_BUG_ON(PACKET_OUTGOING
== 0);
4226 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4228 int sock_queue_err_skb(struct sock
*sk
, struct sk_buff
*skb
)
4230 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
4231 (unsigned int)sk
->sk_rcvbuf
)
4236 skb
->destructor
= sock_rmem_free
;
4237 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
4238 skb_set_err_queue(skb
);
4240 /* before exiting rcu section, make sure dst is refcounted */
4243 skb_queue_tail(&sk
->sk_error_queue
, skb
);
4244 if (!sock_flag(sk
, SOCK_DEAD
))
4245 sk
->sk_error_report(sk
);
4248 EXPORT_SYMBOL(sock_queue_err_skb
);
4250 static bool is_icmp_err_skb(const struct sk_buff
*skb
)
4252 return skb
&& (SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
||
4253 SKB_EXT_ERR(skb
)->ee
.ee_origin
== SO_EE_ORIGIN_ICMP6
);
4256 struct sk_buff
*sock_dequeue_err_skb(struct sock
*sk
)
4258 struct sk_buff_head
*q
= &sk
->sk_error_queue
;
4259 struct sk_buff
*skb
, *skb_next
= NULL
;
4260 bool icmp_next
= false;
4261 unsigned long flags
;
4263 spin_lock_irqsave(&q
->lock
, flags
);
4264 skb
= __skb_dequeue(q
);
4265 if (skb
&& (skb_next
= skb_peek(q
))) {
4266 icmp_next
= is_icmp_err_skb(skb_next
);
4268 sk
->sk_err
= SKB_EXT_ERR(skb_next
)->ee
.ee_origin
;
4270 spin_unlock_irqrestore(&q
->lock
, flags
);
4272 if (is_icmp_err_skb(skb
) && !icmp_next
)
4276 sk
->sk_error_report(sk
);
4280 EXPORT_SYMBOL(sock_dequeue_err_skb
);
4283 * skb_clone_sk - create clone of skb, and take reference to socket
4284 * @skb: the skb to clone
4286 * This function creates a clone of a buffer that holds a reference on
4287 * sk_refcnt. Buffers created via this function are meant to be
4288 * returned using sock_queue_err_skb, or free via kfree_skb.
4290 * When passing buffers allocated with this function to sock_queue_err_skb
4291 * it is necessary to wrap the call with sock_hold/sock_put in order to
4292 * prevent the socket from being released prior to being enqueued on
4293 * the sk_error_queue.
4295 struct sk_buff
*skb_clone_sk(struct sk_buff
*skb
)
4297 struct sock
*sk
= skb
->sk
;
4298 struct sk_buff
*clone
;
4300 if (!sk
|| !refcount_inc_not_zero(&sk
->sk_refcnt
))
4303 clone
= skb_clone(skb
, GFP_ATOMIC
);
4310 clone
->destructor
= sock_efree
;
4314 EXPORT_SYMBOL(skb_clone_sk
);
4316 static void __skb_complete_tx_timestamp(struct sk_buff
*skb
,
4321 struct sock_exterr_skb
*serr
;
4324 BUILD_BUG_ON(sizeof(struct sock_exterr_skb
) > sizeof(skb
->cb
));
4326 serr
= SKB_EXT_ERR(skb
);
4327 memset(serr
, 0, sizeof(*serr
));
4328 serr
->ee
.ee_errno
= ENOMSG
;
4329 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TIMESTAMPING
;
4330 serr
->ee
.ee_info
= tstype
;
4331 serr
->opt_stats
= opt_stats
;
4332 serr
->header
.h4
.iif
= skb
->dev
? skb
->dev
->ifindex
: 0;
4333 if (sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_ID
) {
4334 serr
->ee
.ee_data
= skb_shinfo(skb
)->tskey
;
4335 if (sk
->sk_protocol
== IPPROTO_TCP
&&
4336 sk
->sk_type
== SOCK_STREAM
)
4337 serr
->ee
.ee_data
-= sk
->sk_tskey
;
4340 err
= sock_queue_err_skb(sk
, skb
);
4346 static bool skb_may_tx_timestamp(struct sock
*sk
, bool tsonly
)
4350 if (likely(sysctl_tstamp_allow_data
|| tsonly
))
4353 read_lock_bh(&sk
->sk_callback_lock
);
4354 ret
= sk
->sk_socket
&& sk
->sk_socket
->file
&&
4355 file_ns_capable(sk
->sk_socket
->file
, &init_user_ns
, CAP_NET_RAW
);
4356 read_unlock_bh(&sk
->sk_callback_lock
);
4360 void skb_complete_tx_timestamp(struct sk_buff
*skb
,
4361 struct skb_shared_hwtstamps
*hwtstamps
)
4363 struct sock
*sk
= skb
->sk
;
4365 if (!skb_may_tx_timestamp(sk
, false))
4368 /* Take a reference to prevent skb_orphan() from freeing the socket,
4369 * but only if the socket refcount is not zero.
4371 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4372 *skb_hwtstamps(skb
) = *hwtstamps
;
4373 __skb_complete_tx_timestamp(skb
, sk
, SCM_TSTAMP_SND
, false);
4381 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp
);
4383 void __skb_tstamp_tx(struct sk_buff
*orig_skb
,
4384 struct skb_shared_hwtstamps
*hwtstamps
,
4385 struct sock
*sk
, int tstype
)
4387 struct sk_buff
*skb
;
4388 bool tsonly
, opt_stats
= false;
4393 if (!hwtstamps
&& !(sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TX_SWHW
) &&
4394 skb_shinfo(orig_skb
)->tx_flags
& SKBTX_IN_PROGRESS
)
4397 tsonly
= sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_TSONLY
;
4398 if (!skb_may_tx_timestamp(sk
, tsonly
))
4403 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_STATS
) &&
4404 sk
->sk_protocol
== IPPROTO_TCP
&&
4405 sk
->sk_type
== SOCK_STREAM
) {
4406 skb
= tcp_get_timestamping_opt_stats(sk
);
4410 skb
= alloc_skb(0, GFP_ATOMIC
);
4412 skb
= skb_clone(orig_skb
, GFP_ATOMIC
);
4418 skb_shinfo(skb
)->tx_flags
|= skb_shinfo(orig_skb
)->tx_flags
&
4420 skb_shinfo(skb
)->tskey
= skb_shinfo(orig_skb
)->tskey
;
4424 *skb_hwtstamps(skb
) = *hwtstamps
;
4426 skb
->tstamp
= ktime_get_real();
4428 __skb_complete_tx_timestamp(skb
, sk
, tstype
, opt_stats
);
4430 EXPORT_SYMBOL_GPL(__skb_tstamp_tx
);
4432 void skb_tstamp_tx(struct sk_buff
*orig_skb
,
4433 struct skb_shared_hwtstamps
*hwtstamps
)
4435 return __skb_tstamp_tx(orig_skb
, hwtstamps
, orig_skb
->sk
,
4438 EXPORT_SYMBOL_GPL(skb_tstamp_tx
);
4440 void skb_complete_wifi_ack(struct sk_buff
*skb
, bool acked
)
4442 struct sock
*sk
= skb
->sk
;
4443 struct sock_exterr_skb
*serr
;
4446 skb
->wifi_acked_valid
= 1;
4447 skb
->wifi_acked
= acked
;
4449 serr
= SKB_EXT_ERR(skb
);
4450 memset(serr
, 0, sizeof(*serr
));
4451 serr
->ee
.ee_errno
= ENOMSG
;
4452 serr
->ee
.ee_origin
= SO_EE_ORIGIN_TXSTATUS
;
4454 /* Take a reference to prevent skb_orphan() from freeing the socket,
4455 * but only if the socket refcount is not zero.
4457 if (likely(refcount_inc_not_zero(&sk
->sk_refcnt
))) {
4458 err
= sock_queue_err_skb(sk
, skb
);
4464 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack
);
4467 * skb_partial_csum_set - set up and verify partial csum values for packet
4468 * @skb: the skb to set
4469 * @start: the number of bytes after skb->data to start checksumming.
4470 * @off: the offset from start to place the checksum.
4472 * For untrusted partially-checksummed packets, we need to make sure the values
4473 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4475 * This function checks and sets those values and skb->ip_summed: if this
4476 * returns false you should drop the packet.
4478 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
4480 u32 csum_end
= (u32
)start
+ (u32
)off
+ sizeof(__sum16
);
4481 u32 csum_start
= skb_headroom(skb
) + (u32
)start
;
4483 if (unlikely(csum_start
> U16_MAX
|| csum_end
> skb_headlen(skb
))) {
4484 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4485 start
, off
, skb_headroom(skb
), skb_headlen(skb
));
4488 skb
->ip_summed
= CHECKSUM_PARTIAL
;
4489 skb
->csum_start
= csum_start
;
4490 skb
->csum_offset
= off
;
4491 skb_set_transport_header(skb
, start
);
4494 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);
4496 static int skb_maybe_pull_tail(struct sk_buff
*skb
, unsigned int len
,
4499 if (skb_headlen(skb
) >= len
)
4502 /* If we need to pullup then pullup to the max, so we
4503 * won't need to do it again.
4508 if (__pskb_pull_tail(skb
, max
- skb_headlen(skb
)) == NULL
)
4511 if (skb_headlen(skb
) < len
)
4517 #define MAX_TCP_HDR_LEN (15 * 4)
4519 static __sum16
*skb_checksum_setup_ip(struct sk_buff
*skb
,
4520 typeof(IPPROTO_IP
) proto
,
4527 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct tcphdr
),
4528 off
+ MAX_TCP_HDR_LEN
);
4529 if (!err
&& !skb_partial_csum_set(skb
, off
,
4530 offsetof(struct tcphdr
,
4533 return err
? ERR_PTR(err
) : &tcp_hdr(skb
)->check
;
4536 err
= skb_maybe_pull_tail(skb
, off
+ sizeof(struct udphdr
),
4537 off
+ sizeof(struct udphdr
));
4538 if (!err
&& !skb_partial_csum_set(skb
, off
,
4539 offsetof(struct udphdr
,
4542 return err
? ERR_PTR(err
) : &udp_hdr(skb
)->check
;
4545 return ERR_PTR(-EPROTO
);
4548 /* This value should be large enough to cover a tagged ethernet header plus
4549 * maximally sized IP and TCP or UDP headers.
4551 #define MAX_IP_HDR_LEN 128
4553 static int skb_checksum_setup_ipv4(struct sk_buff
*skb
, bool recalculate
)
4562 err
= skb_maybe_pull_tail(skb
,
4563 sizeof(struct iphdr
),
4568 if (ip_hdr(skb
)->frag_off
& htons(IP_OFFSET
| IP_MF
))
4571 off
= ip_hdrlen(skb
);
4578 csum
= skb_checksum_setup_ip(skb
, ip_hdr(skb
)->protocol
, off
);
4580 return PTR_ERR(csum
);
4583 *csum
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
4586 ip_hdr(skb
)->protocol
, 0);
4593 /* This value should be large enough to cover a tagged ethernet header plus
4594 * an IPv6 header, all options, and a maximal TCP or UDP header.
4596 #define MAX_IPV6_HDR_LEN 256
4598 #define OPT_HDR(type, skb, off) \
4599 (type *)(skb_network_header(skb) + (off))
4601 static int skb_checksum_setup_ipv6(struct sk_buff
*skb
, bool recalculate
)
4614 off
= sizeof(struct ipv6hdr
);
4616 err
= skb_maybe_pull_tail(skb
, off
, MAX_IPV6_HDR_LEN
);
4620 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
4622 len
= sizeof(struct ipv6hdr
) + ntohs(ipv6_hdr(skb
)->payload_len
);
4623 while (off
<= len
&& !done
) {
4625 case IPPROTO_DSTOPTS
:
4626 case IPPROTO_HOPOPTS
:
4627 case IPPROTO_ROUTING
: {
4628 struct ipv6_opt_hdr
*hp
;
4630 err
= skb_maybe_pull_tail(skb
,
4632 sizeof(struct ipv6_opt_hdr
),
4637 hp
= OPT_HDR(struct ipv6_opt_hdr
, skb
, off
);
4638 nexthdr
= hp
->nexthdr
;
4639 off
+= ipv6_optlen(hp
);
4643 struct ip_auth_hdr
*hp
;
4645 err
= skb_maybe_pull_tail(skb
,
4647 sizeof(struct ip_auth_hdr
),
4652 hp
= OPT_HDR(struct ip_auth_hdr
, skb
, off
);
4653 nexthdr
= hp
->nexthdr
;
4654 off
+= ipv6_authlen(hp
);
4657 case IPPROTO_FRAGMENT
: {
4658 struct frag_hdr
*hp
;
4660 err
= skb_maybe_pull_tail(skb
,
4662 sizeof(struct frag_hdr
),
4667 hp
= OPT_HDR(struct frag_hdr
, skb
, off
);
4669 if (hp
->frag_off
& htons(IP6_OFFSET
| IP6_MF
))
4672 nexthdr
= hp
->nexthdr
;
4673 off
+= sizeof(struct frag_hdr
);
4684 if (!done
|| fragment
)
4687 csum
= skb_checksum_setup_ip(skb
, nexthdr
, off
);
4689 return PTR_ERR(csum
);
4692 *csum
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
4693 &ipv6_hdr(skb
)->daddr
,
4694 skb
->len
- off
, nexthdr
, 0);
4702 * skb_checksum_setup - set up partial checksum offset
4703 * @skb: the skb to set up
4704 * @recalculate: if true the pseudo-header checksum will be recalculated
4706 int skb_checksum_setup(struct sk_buff
*skb
, bool recalculate
)
4710 switch (skb
->protocol
) {
4711 case htons(ETH_P_IP
):
4712 err
= skb_checksum_setup_ipv4(skb
, recalculate
);
4715 case htons(ETH_P_IPV6
):
4716 err
= skb_checksum_setup_ipv6(skb
, recalculate
);
4726 EXPORT_SYMBOL(skb_checksum_setup
);
4729 * skb_checksum_maybe_trim - maybe trims the given skb
4730 * @skb: the skb to check
4731 * @transport_len: the data length beyond the network header
4733 * Checks whether the given skb has data beyond the given transport length.
4734 * If so, returns a cloned skb trimmed to this transport length.
4735 * Otherwise returns the provided skb. Returns NULL in error cases
4736 * (e.g. transport_len exceeds skb length or out-of-memory).
4738 * Caller needs to set the skb transport header and free any returned skb if it
4739 * differs from the provided skb.
4741 static struct sk_buff
*skb_checksum_maybe_trim(struct sk_buff
*skb
,
4742 unsigned int transport_len
)
4744 struct sk_buff
*skb_chk
;
4745 unsigned int len
= skb_transport_offset(skb
) + transport_len
;
4750 else if (skb
->len
== len
)
4753 skb_chk
= skb_clone(skb
, GFP_ATOMIC
);
4757 ret
= pskb_trim_rcsum(skb_chk
, len
);
4767 * skb_checksum_trimmed - validate checksum of an skb
4768 * @skb: the skb to check
4769 * @transport_len: the data length beyond the network header
4770 * @skb_chkf: checksum function to use
4772 * Applies the given checksum function skb_chkf to the provided skb.
4773 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4775 * If the skb has data beyond the given transport length, then a
4776 * trimmed & cloned skb is checked and returned.
4778 * Caller needs to set the skb transport header and free any returned skb if it
4779 * differs from the provided skb.
4781 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
4782 unsigned int transport_len
,
4783 __sum16(*skb_chkf
)(struct sk_buff
*skb
))
4785 struct sk_buff
*skb_chk
;
4786 unsigned int offset
= skb_transport_offset(skb
);
4789 skb_chk
= skb_checksum_maybe_trim(skb
, transport_len
);
4793 if (!pskb_may_pull(skb_chk
, offset
))
4796 skb_pull_rcsum(skb_chk
, offset
);
4797 ret
= skb_chkf(skb_chk
);
4798 skb_push_rcsum(skb_chk
, offset
);
4806 if (skb_chk
&& skb_chk
!= skb
)
4812 EXPORT_SYMBOL(skb_checksum_trimmed
);
4814 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
4816 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4819 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
4821 void kfree_skb_partial(struct sk_buff
*skb
, bool head_stolen
)
4824 skb_release_head_state(skb
);
4825 kmem_cache_free(skbuff_head_cache
, skb
);
4830 EXPORT_SYMBOL(kfree_skb_partial
);
4833 * skb_try_coalesce - try to merge skb to prior one
4835 * @from: buffer to add
4836 * @fragstolen: pointer to boolean
4837 * @delta_truesize: how much more was allocated than was requested
4839 bool skb_try_coalesce(struct sk_buff
*to
, struct sk_buff
*from
,
4840 bool *fragstolen
, int *delta_truesize
)
4842 struct skb_shared_info
*to_shinfo
, *from_shinfo
;
4843 int i
, delta
, len
= from
->len
;
4845 *fragstolen
= false;
4850 if (len
<= skb_tailroom(to
)) {
4852 BUG_ON(skb_copy_bits(from
, 0, skb_put(to
, len
), len
));
4853 *delta_truesize
= 0;
4857 to_shinfo
= skb_shinfo(to
);
4858 from_shinfo
= skb_shinfo(from
);
4859 if (to_shinfo
->frag_list
|| from_shinfo
->frag_list
)
4861 if (skb_zcopy(to
) || skb_zcopy(from
))
4864 if (skb_headlen(from
) != 0) {
4866 unsigned int offset
;
4868 if (to_shinfo
->nr_frags
+
4869 from_shinfo
->nr_frags
>= MAX_SKB_FRAGS
)
4872 if (skb_head_is_locked(from
))
4875 delta
= from
->truesize
- SKB_DATA_ALIGN(sizeof(struct sk_buff
));
4877 page
= virt_to_head_page(from
->head
);
4878 offset
= from
->data
- (unsigned char *)page_address(page
);
4880 skb_fill_page_desc(to
, to_shinfo
->nr_frags
,
4881 page
, offset
, skb_headlen(from
));
4884 if (to_shinfo
->nr_frags
+
4885 from_shinfo
->nr_frags
> MAX_SKB_FRAGS
)
4888 delta
= from
->truesize
- SKB_TRUESIZE(skb_end_offset(from
));
4891 WARN_ON_ONCE(delta
< len
);
4893 memcpy(to_shinfo
->frags
+ to_shinfo
->nr_frags
,
4895 from_shinfo
->nr_frags
* sizeof(skb_frag_t
));
4896 to_shinfo
->nr_frags
+= from_shinfo
->nr_frags
;
4898 if (!skb_cloned(from
))
4899 from_shinfo
->nr_frags
= 0;
4901 /* if the skb is not cloned this does nothing
4902 * since we set nr_frags to 0.
4904 for (i
= 0; i
< from_shinfo
->nr_frags
; i
++)
4905 __skb_frag_ref(&from_shinfo
->frags
[i
]);
4907 to
->truesize
+= delta
;
4909 to
->data_len
+= len
;
4911 *delta_truesize
= delta
;
4914 EXPORT_SYMBOL(skb_try_coalesce
);
4917 * skb_scrub_packet - scrub an skb
4919 * @skb: buffer to clean
4920 * @xnet: packet is crossing netns
4922 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4923 * into/from a tunnel. Some information have to be cleared during these
4925 * skb_scrub_packet can also be used to clean a skb before injecting it in
4926 * another namespace (@xnet == true). We have to clear all information in the
4927 * skb that could impact namespace isolation.
4929 void skb_scrub_packet(struct sk_buff
*skb
, bool xnet
)
4931 skb
->pkt_type
= PACKET_HOST
;
4937 nf_reset_trace(skb
);
4939 #ifdef CONFIG_NET_SWITCHDEV
4940 skb
->offload_fwd_mark
= 0;
4941 skb
->offload_mr_fwd_mark
= 0;
4951 EXPORT_SYMBOL_GPL(skb_scrub_packet
);
4954 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4958 * skb_gso_transport_seglen is used to determine the real size of the
4959 * individual segments, including Layer4 headers (TCP/UDP).
4961 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4963 static unsigned int skb_gso_transport_seglen(const struct sk_buff
*skb
)
4965 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
4966 unsigned int thlen
= 0;
4968 if (skb
->encapsulation
) {
4969 thlen
= skb_inner_transport_header(skb
) -
4970 skb_transport_header(skb
);
4972 if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
)))
4973 thlen
+= inner_tcp_hdrlen(skb
);
4974 } else if (likely(shinfo
->gso_type
& (SKB_GSO_TCPV4
| SKB_GSO_TCPV6
))) {
4975 thlen
= tcp_hdrlen(skb
);
4976 } else if (unlikely(skb_is_gso_sctp(skb
))) {
4977 thlen
= sizeof(struct sctphdr
);
4978 } else if (shinfo
->gso_type
& SKB_GSO_UDP_L4
) {
4979 thlen
= sizeof(struct udphdr
);
4981 /* UFO sets gso_size to the size of the fragmentation
4982 * payload, i.e. the size of the L4 (UDP) header is already
4985 return thlen
+ shinfo
->gso_size
;
4989 * skb_gso_network_seglen - Return length of individual segments of a gso packet
4993 * skb_gso_network_seglen is used to determine the real size of the
4994 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
4996 * The MAC/L2 header is not accounted for.
4998 static unsigned int skb_gso_network_seglen(const struct sk_buff
*skb
)
5000 unsigned int hdr_len
= skb_transport_header(skb
) -
5001 skb_network_header(skb
);
5003 return hdr_len
+ skb_gso_transport_seglen(skb
);
5007 * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5011 * skb_gso_mac_seglen is used to determine the real size of the
5012 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5013 * headers (TCP/UDP).
5015 static unsigned int skb_gso_mac_seglen(const struct sk_buff
*skb
)
5017 unsigned int hdr_len
= skb_transport_header(skb
) - skb_mac_header(skb
);
5019 return hdr_len
+ skb_gso_transport_seglen(skb
);
5023 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5025 * There are a couple of instances where we have a GSO skb, and we
5026 * want to determine what size it would be after it is segmented.
5028 * We might want to check:
5029 * - L3+L4+payload size (e.g. IP forwarding)
5030 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5032 * This is a helper to do that correctly considering GSO_BY_FRAGS.
5034 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5035 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5037 * @max_len: The maximum permissible length.
5039 * Returns true if the segmented length <= max length.
5041 static inline bool skb_gso_size_check(const struct sk_buff
*skb
,
5042 unsigned int seg_len
,
5043 unsigned int max_len
) {
5044 const struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
5045 const struct sk_buff
*iter
;
5047 if (shinfo
->gso_size
!= GSO_BY_FRAGS
)
5048 return seg_len
<= max_len
;
5050 /* Undo this so we can re-use header sizes */
5051 seg_len
-= GSO_BY_FRAGS
;
5053 skb_walk_frags(skb
, iter
) {
5054 if (seg_len
+ skb_headlen(iter
) > max_len
)
5062 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5065 * @mtu: MTU to validate against
5067 * skb_gso_validate_network_len validates if a given skb will fit a
5068 * wanted MTU once split. It considers L3 headers, L4 headers, and the
5071 bool skb_gso_validate_network_len(const struct sk_buff
*skb
, unsigned int mtu
)
5073 return skb_gso_size_check(skb
, skb_gso_network_seglen(skb
), mtu
);
5075 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len
);
5078 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5081 * @len: length to validate against
5083 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5084 * length once split, including L2, L3 and L4 headers and the payload.
5086 bool skb_gso_validate_mac_len(const struct sk_buff
*skb
, unsigned int len
)
5088 return skb_gso_size_check(skb
, skb_gso_mac_seglen(skb
), len
);
5090 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len
);
5092 static struct sk_buff
*skb_reorder_vlan_header(struct sk_buff
*skb
)
5094 int mac_len
, meta_len
;
5097 if (skb_cow(skb
, skb_headroom(skb
)) < 0) {
5102 mac_len
= skb
->data
- skb_mac_header(skb
);
5103 if (likely(mac_len
> VLAN_HLEN
+ ETH_TLEN
)) {
5104 memmove(skb_mac_header(skb
) + VLAN_HLEN
, skb_mac_header(skb
),
5105 mac_len
- VLAN_HLEN
- ETH_TLEN
);
5108 meta_len
= skb_metadata_len(skb
);
5110 meta
= skb_metadata_end(skb
) - meta_len
;
5111 memmove(meta
+ VLAN_HLEN
, meta
, meta_len
);
5114 skb
->mac_header
+= VLAN_HLEN
;
5118 struct sk_buff
*skb_vlan_untag(struct sk_buff
*skb
)
5120 struct vlan_hdr
*vhdr
;
5123 if (unlikely(skb_vlan_tag_present(skb
))) {
5124 /* vlan_tci is already set-up so leave this for another time */
5128 skb
= skb_share_check(skb
, GFP_ATOMIC
);
5132 if (unlikely(!pskb_may_pull(skb
, VLAN_HLEN
)))
5135 vhdr
= (struct vlan_hdr
*)skb
->data
;
5136 vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5137 __vlan_hwaccel_put_tag(skb
, skb
->protocol
, vlan_tci
);
5139 skb_pull_rcsum(skb
, VLAN_HLEN
);
5140 vlan_set_encap_proto(skb
, vhdr
);
5142 skb
= skb_reorder_vlan_header(skb
);
5146 skb_reset_network_header(skb
);
5147 skb_reset_transport_header(skb
);
5148 skb_reset_mac_len(skb
);
5156 EXPORT_SYMBOL(skb_vlan_untag
);
5158 int skb_ensure_writable(struct sk_buff
*skb
, int write_len
)
5160 if (!pskb_may_pull(skb
, write_len
))
5163 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
5166 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
5168 EXPORT_SYMBOL(skb_ensure_writable
);
5170 /* remove VLAN header from packet and update csum accordingly.
5171 * expects a non skb_vlan_tag_present skb with a vlan tag payload
5173 int __skb_vlan_pop(struct sk_buff
*skb
, u16
*vlan_tci
)
5175 struct vlan_hdr
*vhdr
;
5176 int offset
= skb
->data
- skb_mac_header(skb
);
5179 if (WARN_ONCE(offset
,
5180 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5185 err
= skb_ensure_writable(skb
, VLAN_ETH_HLEN
);
5189 skb_postpull_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5191 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
5192 *vlan_tci
= ntohs(vhdr
->h_vlan_TCI
);
5194 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
5195 __skb_pull(skb
, VLAN_HLEN
);
5197 vlan_set_encap_proto(skb
, vhdr
);
5198 skb
->mac_header
+= VLAN_HLEN
;
5200 if (skb_network_offset(skb
) < ETH_HLEN
)
5201 skb_set_network_header(skb
, ETH_HLEN
);
5203 skb_reset_mac_len(skb
);
5207 EXPORT_SYMBOL(__skb_vlan_pop
);
5209 /* Pop a vlan tag either from hwaccel or from payload.
5210 * Expects skb->data at mac header.
5212 int skb_vlan_pop(struct sk_buff
*skb
)
5218 if (likely(skb_vlan_tag_present(skb
))) {
5221 if (unlikely(!eth_type_vlan(skb
->protocol
)))
5224 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5228 /* move next vlan tag to hw accel tag */
5229 if (likely(!eth_type_vlan(skb
->protocol
)))
5232 vlan_proto
= skb
->protocol
;
5233 err
= __skb_vlan_pop(skb
, &vlan_tci
);
5237 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5240 EXPORT_SYMBOL(skb_vlan_pop
);
5242 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5243 * Expects skb->data at mac header.
5245 int skb_vlan_push(struct sk_buff
*skb
, __be16 vlan_proto
, u16 vlan_tci
)
5247 if (skb_vlan_tag_present(skb
)) {
5248 int offset
= skb
->data
- skb_mac_header(skb
);
5251 if (WARN_ONCE(offset
,
5252 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5257 err
= __vlan_insert_tag(skb
, skb
->vlan_proto
,
5258 skb_vlan_tag_get(skb
));
5262 skb
->protocol
= skb
->vlan_proto
;
5263 skb
->mac_len
+= VLAN_HLEN
;
5265 skb_postpush_rcsum(skb
, skb
->data
+ (2 * ETH_ALEN
), VLAN_HLEN
);
5267 __vlan_hwaccel_put_tag(skb
, vlan_proto
, vlan_tci
);
5270 EXPORT_SYMBOL(skb_vlan_push
);
5273 * alloc_skb_with_frags - allocate skb with page frags
5275 * @header_len: size of linear part
5276 * @data_len: needed length in frags
5277 * @max_page_order: max page order desired.
5278 * @errcode: pointer to error code if any
5279 * @gfp_mask: allocation mask
5281 * This can be used to allocate a paged skb, given a maximal order for frags.
5283 struct sk_buff
*alloc_skb_with_frags(unsigned long header_len
,
5284 unsigned long data_len
,
5289 int npages
= (data_len
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
5290 unsigned long chunk
;
5291 struct sk_buff
*skb
;
5295 *errcode
= -EMSGSIZE
;
5296 /* Note this test could be relaxed, if we succeed to allocate
5297 * high order pages...
5299 if (npages
> MAX_SKB_FRAGS
)
5302 *errcode
= -ENOBUFS
;
5303 skb
= alloc_skb(header_len
, gfp_mask
);
5307 skb
->truesize
+= npages
<< PAGE_SHIFT
;
5309 for (i
= 0; npages
> 0; i
++) {
5310 int order
= max_page_order
;
5313 if (npages
>= 1 << order
) {
5314 page
= alloc_pages((gfp_mask
& ~__GFP_DIRECT_RECLAIM
) |
5320 /* Do not retry other high order allocations */
5326 page
= alloc_page(gfp_mask
);
5330 chunk
= min_t(unsigned long, data_len
,
5331 PAGE_SIZE
<< order
);
5332 skb_fill_page_desc(skb
, i
, page
, 0, chunk
);
5334 npages
-= 1 << order
;
5342 EXPORT_SYMBOL(alloc_skb_with_frags
);
5344 /* carve out the first off bytes from skb when off < headlen */
5345 static int pskb_carve_inside_header(struct sk_buff
*skb
, const u32 off
,
5346 const int headlen
, gfp_t gfp_mask
)
5349 int size
= skb_end_offset(skb
);
5350 int new_hlen
= headlen
- off
;
5353 size
= SKB_DATA_ALIGN(size
);
5355 if (skb_pfmemalloc(skb
))
5356 gfp_mask
|= __GFP_MEMALLOC
;
5357 data
= kmalloc_reserve(size
+
5358 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5359 gfp_mask
, NUMA_NO_NODE
, NULL
);
5363 size
= SKB_WITH_OVERHEAD(ksize(data
));
5365 /* Copy real data, and all frags */
5366 skb_copy_from_linear_data_offset(skb
, off
, data
, new_hlen
);
5369 memcpy((struct skb_shared_info
*)(data
+ size
),
5371 offsetof(struct skb_shared_info
,
5372 frags
[skb_shinfo(skb
)->nr_frags
]));
5373 if (skb_cloned(skb
)) {
5374 /* drop the old head gracefully */
5375 if (skb_orphan_frags(skb
, gfp_mask
)) {
5379 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
5380 skb_frag_ref(skb
, i
);
5381 if (skb_has_frag_list(skb
))
5382 skb_clone_fraglist(skb
);
5383 skb_release_data(skb
);
5385 /* we can reuse existing recount- all we did was
5394 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5397 skb
->end
= skb
->head
+ size
;
5399 skb_set_tail_pointer(skb
, skb_headlen(skb
));
5400 skb_headers_offset_update(skb
, 0);
5404 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5409 static int pskb_carve(struct sk_buff
*skb
, const u32 off
, gfp_t gfp
);
5411 /* carve out the first eat bytes from skb's frag_list. May recurse into
5414 static int pskb_carve_frag_list(struct sk_buff
*skb
,
5415 struct skb_shared_info
*shinfo
, int eat
,
5418 struct sk_buff
*list
= shinfo
->frag_list
;
5419 struct sk_buff
*clone
= NULL
;
5420 struct sk_buff
*insp
= NULL
;
5424 pr_err("Not enough bytes to eat. Want %d\n", eat
);
5427 if (list
->len
<= eat
) {
5428 /* Eaten as whole. */
5433 /* Eaten partially. */
5434 if (skb_shared(list
)) {
5435 clone
= skb_clone(list
, gfp_mask
);
5441 /* This may be pulled without problems. */
5444 if (pskb_carve(list
, eat
, gfp_mask
) < 0) {
5452 /* Free pulled out fragments. */
5453 while ((list
= shinfo
->frag_list
) != insp
) {
5454 shinfo
->frag_list
= list
->next
;
5457 /* And insert new clone at head. */
5460 shinfo
->frag_list
= clone
;
5465 /* carve off first len bytes from skb. Split line (off) is in the
5466 * non-linear part of skb
5468 static int pskb_carve_inside_nonlinear(struct sk_buff
*skb
, const u32 off
,
5469 int pos
, gfp_t gfp_mask
)
5472 int size
= skb_end_offset(skb
);
5474 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
5475 struct skb_shared_info
*shinfo
;
5477 size
= SKB_DATA_ALIGN(size
);
5479 if (skb_pfmemalloc(skb
))
5480 gfp_mask
|= __GFP_MEMALLOC
;
5481 data
= kmalloc_reserve(size
+
5482 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)),
5483 gfp_mask
, NUMA_NO_NODE
, NULL
);
5487 size
= SKB_WITH_OVERHEAD(ksize(data
));
5489 memcpy((struct skb_shared_info
*)(data
+ size
),
5490 skb_shinfo(skb
), offsetof(struct skb_shared_info
,
5491 frags
[skb_shinfo(skb
)->nr_frags
]));
5492 if (skb_orphan_frags(skb
, gfp_mask
)) {
5496 shinfo
= (struct skb_shared_info
*)(data
+ size
);
5497 for (i
= 0; i
< nfrags
; i
++) {
5498 int fsize
= skb_frag_size(&skb_shinfo(skb
)->frags
[i
]);
5500 if (pos
+ fsize
> off
) {
5501 shinfo
->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
5505 * We have two variants in this case:
5506 * 1. Move all the frag to the second
5507 * part, if it is possible. F.e.
5508 * this approach is mandatory for TUX,
5509 * where splitting is expensive.
5510 * 2. Split is accurately. We make this.
5512 shinfo
->frags
[0].page_offset
+= off
- pos
;
5513 skb_frag_size_sub(&shinfo
->frags
[0], off
- pos
);
5515 skb_frag_ref(skb
, i
);
5520 shinfo
->nr_frags
= k
;
5521 if (skb_has_frag_list(skb
))
5522 skb_clone_fraglist(skb
);
5525 /* split line is in frag list */
5526 pskb_carve_frag_list(skb
, shinfo
, off
- pos
, gfp_mask
);
5528 skb_release_data(skb
);
5533 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5536 skb
->end
= skb
->head
+ size
;
5538 skb_reset_tail_pointer(skb
);
5539 skb_headers_offset_update(skb
, 0);
5544 skb
->data_len
= skb
->len
;
5545 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
5549 /* remove len bytes from the beginning of the skb */
5550 static int pskb_carve(struct sk_buff
*skb
, const u32 len
, gfp_t gfp
)
5552 int headlen
= skb_headlen(skb
);
5555 return pskb_carve_inside_header(skb
, len
, headlen
, gfp
);
5557 return pskb_carve_inside_nonlinear(skb
, len
, headlen
, gfp
);
5560 /* Extract to_copy bytes starting at off from skb, and return this in
5563 struct sk_buff
*pskb_extract(struct sk_buff
*skb
, int off
,
5564 int to_copy
, gfp_t gfp
)
5566 struct sk_buff
*clone
= skb_clone(skb
, gfp
);
5571 if (pskb_carve(clone
, off
, gfp
) < 0 ||
5572 pskb_trim(clone
, to_copy
)) {
5578 EXPORT_SYMBOL(pskb_extract
);
5581 * skb_condense - try to get rid of fragments/frag_list if possible
5584 * Can be used to save memory before skb is added to a busy queue.
5585 * If packet has bytes in frags and enough tail room in skb->head,
5586 * pull all of them, so that we can free the frags right now and adjust
5589 * We do not reallocate skb->head thus can not fail.
5590 * Caller must re-evaluate skb->truesize if needed.
5592 void skb_condense(struct sk_buff
*skb
)
5594 if (skb
->data_len
) {
5595 if (skb
->data_len
> skb
->end
- skb
->tail
||
5599 /* Nice, we can free page frag(s) right now */
5600 __pskb_pull_tail(skb
, skb
->data_len
);
5602 /* At this point, skb->truesize might be over estimated,
5603 * because skb had a fragment, and fragments do not tell
5605 * When we pulled its content into skb->head, fragment
5606 * was freed, but __pskb_pull_tail() could not possibly
5607 * adjust skb->truesize, not knowing the frag truesize.
5609 skb
->truesize
= SKB_TRUESIZE(skb_end_offset(skb
));