2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
66 #include <linux/locallock.h>
68 #include <net/protocol.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
75 #include <asm/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
85 * skb_panic - private function for out-of-line support
89 * @msg: skb_over_panic or skb_under_panic
91 * Out-of-line support for skb_put() and skb_push().
92 * Called via the wrapper skb_over_panic() or skb_under_panic().
93 * Keep out of line to prevent kernel bloat.
94 * __builtin_return_address is not used because it is not always reliable.
96 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
99 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
100 msg, addr, skb->len, sz, skb->head, skb->data,
101 (unsigned long)skb->tail, (unsigned long)skb->end,
102 skb->dev ? skb->dev->name : "<NULL>");
106 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
108 skb_panic(skb, sz, addr, __func__);
111 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
113 skb_panic(skb, sz, addr, __func__);
117 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
118 * the caller if emergency pfmemalloc reserves are being used. If it is and
119 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
120 * may be used. Otherwise, the packet data may be discarded until enough
123 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
124 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
126 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
127 unsigned long ip, bool *pfmemalloc)
130 bool ret_pfmemalloc = false;
133 * Try a regular allocation, when that fails and we're not entitled
134 * to the reserves, fail.
136 obj = kmalloc_node_track_caller(size,
137 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
139 if (obj || !(gfp_pfmemalloc_allowed(flags)))
142 /* Try again but now we are using pfmemalloc reserves */
143 ret_pfmemalloc = true;
144 obj = kmalloc_node_track_caller(size, flags, node);
148 *pfmemalloc = ret_pfmemalloc;
153 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
154 * 'private' fields and also do memory statistics to find all the
159 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
164 skb = kmem_cache_alloc_node(skbuff_head_cache,
165 gfp_mask & ~__GFP_DMA, node);
170 * Only clear those fields we need to clear, not those that we will
171 * actually initialise below. Hence, don't put any more fields after
172 * the tail pointer in struct sk_buff!
174 memset(skb, 0, offsetof(struct sk_buff, tail));
176 skb->truesize = sizeof(struct sk_buff);
177 atomic_set(&skb->users, 1);
179 skb->mac_header = (typeof(skb->mac_header))~0U;
185 * __alloc_skb - allocate a network buffer
186 * @size: size to allocate
187 * @gfp_mask: allocation mask
188 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
189 * instead of head cache and allocate a cloned (child) skb.
190 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
191 * allocations in case the data is required for writeback
192 * @node: numa node to allocate memory on
194 * Allocate a new &sk_buff. The returned buffer has no headroom and a
195 * tail room of at least size bytes. The object has a reference count
196 * of one. The return is the buffer. On a failure the return is %NULL.
198 * Buffers may only be allocated from interrupts using a @gfp_mask of
201 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
204 struct kmem_cache *cache;
205 struct skb_shared_info *shinfo;
210 cache = (flags & SKB_ALLOC_FCLONE)
211 ? skbuff_fclone_cache : skbuff_head_cache;
213 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
214 gfp_mask |= __GFP_MEMALLOC;
217 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
222 /* We do our best to align skb_shared_info on a separate cache
223 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
224 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
225 * Both skb->head and skb_shared_info are cache line aligned.
227 size = SKB_DATA_ALIGN(size);
228 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
229 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
232 /* kmalloc(size) might give us more room than requested.
233 * Put skb_shared_info exactly at the end of allocated zone,
234 * to allow max possible filling before reallocation.
236 size = SKB_WITH_OVERHEAD(ksize(data));
237 prefetchw(data + size);
240 * Only clear those fields we need to clear, not those that we will
241 * actually initialise below. Hence, don't put any more fields after
242 * the tail pointer in struct sk_buff!
244 memset(skb, 0, offsetof(struct sk_buff, tail));
245 /* Account for allocated memory : skb + skb->head */
246 skb->truesize = SKB_TRUESIZE(size);
247 skb->pfmemalloc = pfmemalloc;
248 atomic_set(&skb->users, 1);
251 skb_reset_tail_pointer(skb);
252 skb->end = skb->tail + size;
253 skb->mac_header = (typeof(skb->mac_header))~0U;
254 skb->transport_header = (typeof(skb->transport_header))~0U;
256 /* make sure we initialize shinfo sequentially */
257 shinfo = skb_shinfo(skb);
258 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
259 atomic_set(&shinfo->dataref, 1);
260 kmemcheck_annotate_variable(shinfo->destructor_arg);
262 if (flags & SKB_ALLOC_FCLONE) {
263 struct sk_buff_fclones *fclones;
265 fclones = container_of(skb, struct sk_buff_fclones, skb1);
267 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
268 skb->fclone = SKB_FCLONE_ORIG;
269 atomic_set(&fclones->fclone_ref, 1);
271 fclones->skb2.fclone = SKB_FCLONE_CLONE;
272 fclones->skb2.pfmemalloc = pfmemalloc;
277 kmem_cache_free(cache, skb);
281 EXPORT_SYMBOL(__alloc_skb);
284 * __build_skb - build a network buffer
285 * @data: data buffer provided by caller
286 * @frag_size: size of data, or 0 if head was kmalloced
288 * Allocate a new &sk_buff. Caller provides space holding head and
289 * skb_shared_info. @data must have been allocated by kmalloc() only if
290 * @frag_size is 0, otherwise data should come from the page allocator
292 * The return is the new skb buffer.
293 * On a failure the return is %NULL, and @data is not freed.
295 * Before IO, driver allocates only data buffer where NIC put incoming frame
296 * Driver should add room at head (NET_SKB_PAD) and
297 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
298 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
299 * before giving packet to stack.
300 * RX rings only contains data buffers, not full skbs.
302 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
304 struct skb_shared_info *shinfo;
306 unsigned int size = frag_size ? : ksize(data);
308 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
312 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
314 memset(skb, 0, offsetof(struct sk_buff, tail));
315 skb->truesize = SKB_TRUESIZE(size);
316 atomic_set(&skb->users, 1);
319 skb_reset_tail_pointer(skb);
320 skb->end = skb->tail + size;
321 skb->mac_header = (typeof(skb->mac_header))~0U;
322 skb->transport_header = (typeof(skb->transport_header))~0U;
324 /* make sure we initialize shinfo sequentially */
325 shinfo = skb_shinfo(skb);
326 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
327 atomic_set(&shinfo->dataref, 1);
328 kmemcheck_annotate_variable(shinfo->destructor_arg);
333 /* build_skb() is wrapper over __build_skb(), that specifically
334 * takes care of skb->head and skb->pfmemalloc
335 * This means that if @frag_size is not zero, then @data must be backed
336 * by a page fragment, not kmalloc() or vmalloc()
338 struct sk_buff *build_skb(void *data, unsigned int frag_size)
340 struct sk_buff *skb = __build_skb(data, frag_size);
342 if (skb && frag_size) {
344 if (page_is_pfmemalloc(virt_to_head_page(data)))
349 EXPORT_SYMBOL(build_skb);
351 struct netdev_alloc_cache {
352 struct page_frag frag;
353 /* we maintain a pagecount bias, so that we dont dirty cache line
354 * containing page->_count every time we allocate a fragment.
356 unsigned int pagecnt_bias;
358 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
359 static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
360 static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
362 static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
365 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
366 struct page *page = NULL;
367 gfp_t gfp = gfp_mask;
370 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
372 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
373 nc->frag.size = PAGE_SIZE << (page ? order : 0);
377 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
379 nc->frag.page = page;
384 static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
385 unsigned int fragsz, gfp_t gfp_mask)
387 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
388 struct page *page = nc->frag.page;
392 if (unlikely(!page)) {
394 page = __page_frag_refill(nc, gfp_mask);
398 /* if size can vary use frag.size else just use PAGE_SIZE */
399 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
401 /* Even if we own the page, we do not use atomic_set().
402 * This would break get_page_unless_zero() users.
404 atomic_add(size - 1, &page->_count);
406 /* reset page count bias and offset to start of new frag */
407 nc->pagecnt_bias = size;
408 nc->frag.offset = size;
411 offset = nc->frag.offset - fragsz;
412 if (unlikely(offset < 0)) {
413 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
416 /* if size can vary use frag.size else just use PAGE_SIZE */
417 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
419 /* OK, page count is 0, we can safely set it */
420 atomic_set(&page->_count, size);
422 /* reset page count bias and offset to start of new frag */
423 nc->pagecnt_bias = size;
424 offset = size - fragsz;
428 nc->frag.offset = offset;
430 return page_address(page) + offset;
433 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
438 local_lock_irqsave(netdev_alloc_lock, flags);
439 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
440 local_unlock_irqrestore(netdev_alloc_lock, flags);
445 * netdev_alloc_frag - allocate a page fragment
446 * @fragsz: fragment size
448 * Allocates a frag from a page for receive buffer.
449 * Uses GFP_ATOMIC allocations.
451 void *netdev_alloc_frag(unsigned int fragsz)
453 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
455 EXPORT_SYMBOL(netdev_alloc_frag);
457 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
459 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
462 void *napi_alloc_frag(unsigned int fragsz)
464 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
466 EXPORT_SYMBOL(napi_alloc_frag);
469 * __alloc_rx_skb - allocate an skbuff for rx
470 * @length: length to allocate
471 * @gfp_mask: get_free_pages mask, passed to alloc_skb
472 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
473 * allocations in case we have to fallback to __alloc_skb()
474 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
475 * from napi_cache instead of netdev_cache.
477 * Allocate a new &sk_buff and assign it a usage count of one. The
478 * buffer has unspecified headroom built in. Users should allocate
479 * the headroom they think they need without accounting for the
480 * built in space. The built in space is used for optimisations.
482 * %NULL is returned if there is no free memory.
484 static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
487 struct sk_buff *skb = NULL;
488 unsigned int fragsz = SKB_DATA_ALIGN(length) +
489 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
491 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
494 if (sk_memalloc_socks())
495 gfp_mask |= __GFP_MEMALLOC;
497 data = (flags & SKB_ALLOC_NAPI) ?
498 __napi_alloc_frag(fragsz, gfp_mask) :
499 __netdev_alloc_frag(fragsz, gfp_mask);
502 skb = build_skb(data, fragsz);
504 put_page(virt_to_head_page(data));
507 skb = __alloc_skb(length, gfp_mask,
508 SKB_ALLOC_RX, NUMA_NO_NODE);
514 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
515 * @dev: network device to receive on
516 * @length: length to allocate
517 * @gfp_mask: get_free_pages mask, passed to alloc_skb
519 * Allocate a new &sk_buff and assign it a usage count of one. The
520 * buffer has NET_SKB_PAD headroom built in. Users should allocate
521 * the headroom they think they need without accounting for the
522 * built in space. The built in space is used for optimisations.
524 * %NULL is returned if there is no free memory.
526 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
527 unsigned int length, gfp_t gfp_mask)
531 length += NET_SKB_PAD;
532 skb = __alloc_rx_skb(length, gfp_mask, 0);
535 skb_reserve(skb, NET_SKB_PAD);
541 EXPORT_SYMBOL(__netdev_alloc_skb);
544 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
545 * @napi: napi instance this buffer was allocated for
546 * @length: length to allocate
547 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
549 * Allocate a new sk_buff for use in NAPI receive. This buffer will
550 * attempt to allocate the head from a special reserved region used
551 * only for NAPI Rx allocation. By doing this we can save several
552 * CPU cycles by avoiding having to disable and re-enable IRQs.
554 * %NULL is returned if there is no free memory.
556 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
557 unsigned int length, gfp_t gfp_mask)
561 length += NET_SKB_PAD + NET_IP_ALIGN;
562 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
565 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
566 skb->dev = napi->dev;
571 EXPORT_SYMBOL(__napi_alloc_skb);
573 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
574 int size, unsigned int truesize)
576 skb_fill_page_desc(skb, i, page, off, size);
578 skb->data_len += size;
579 skb->truesize += truesize;
581 EXPORT_SYMBOL(skb_add_rx_frag);
583 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
584 unsigned int truesize)
586 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
588 skb_frag_size_add(frag, size);
590 skb->data_len += size;
591 skb->truesize += truesize;
593 EXPORT_SYMBOL(skb_coalesce_rx_frag);
595 static void skb_drop_list(struct sk_buff **listp)
597 kfree_skb_list(*listp);
601 static inline void skb_drop_fraglist(struct sk_buff *skb)
603 skb_drop_list(&skb_shinfo(skb)->frag_list);
606 static void skb_clone_fraglist(struct sk_buff *skb)
608 struct sk_buff *list;
610 skb_walk_frags(skb, list)
614 static void skb_free_head(struct sk_buff *skb)
617 put_page(virt_to_head_page(skb->head));
622 static void skb_release_data(struct sk_buff *skb)
624 struct skb_shared_info *shinfo = skb_shinfo(skb);
628 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
632 for (i = 0; i < shinfo->nr_frags; i++)
633 __skb_frag_unref(&shinfo->frags[i]);
636 * If skb buf is from userspace, we need to notify the caller
637 * the lower device DMA has done;
639 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
640 struct ubuf_info *uarg;
642 uarg = shinfo->destructor_arg;
644 uarg->callback(uarg, true);
647 if (shinfo->frag_list)
648 kfree_skb_list(shinfo->frag_list);
654 * Free an skbuff by memory without cleaning the state.
656 static void kfree_skbmem(struct sk_buff *skb)
658 struct sk_buff_fclones *fclones;
660 switch (skb->fclone) {
661 case SKB_FCLONE_UNAVAILABLE:
662 kmem_cache_free(skbuff_head_cache, skb);
665 case SKB_FCLONE_ORIG:
666 fclones = container_of(skb, struct sk_buff_fclones, skb1);
668 /* We usually free the clone (TX completion) before original skb
669 * This test would have no chance to be true for the clone,
670 * while here, branch prediction will be good.
672 if (atomic_read(&fclones->fclone_ref) == 1)
676 default: /* SKB_FCLONE_CLONE */
677 fclones = container_of(skb, struct sk_buff_fclones, skb2);
680 if (!atomic_dec_and_test(&fclones->fclone_ref))
683 kmem_cache_free(skbuff_fclone_cache, fclones);
686 static void skb_release_head_state(struct sk_buff *skb)
690 secpath_put(skb->sp);
692 if (skb->destructor) {
694 skb->destructor(skb);
696 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
697 nf_conntrack_put(skb->nfct);
699 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
700 nf_bridge_put(skb->nf_bridge);
704 /* Free everything but the sk_buff shell. */
705 static void skb_release_all(struct sk_buff *skb)
707 skb_release_head_state(skb);
708 if (likely(skb->head))
709 skb_release_data(skb);
713 * __kfree_skb - private function
716 * Free an sk_buff. Release anything attached to the buffer.
717 * Clean the state. This is an internal helper function. Users should
718 * always call kfree_skb
721 void __kfree_skb(struct sk_buff *skb)
723 skb_release_all(skb);
726 EXPORT_SYMBOL(__kfree_skb);
729 * kfree_skb - free an sk_buff
730 * @skb: buffer to free
732 * Drop a reference to the buffer and free it if the usage count has
735 void kfree_skb(struct sk_buff *skb)
739 if (likely(atomic_read(&skb->users) == 1))
741 else if (likely(!atomic_dec_and_test(&skb->users)))
743 trace_kfree_skb(skb, __builtin_return_address(0));
746 EXPORT_SYMBOL(kfree_skb);
748 void kfree_skb_list(struct sk_buff *segs)
751 struct sk_buff *next = segs->next;
757 EXPORT_SYMBOL(kfree_skb_list);
760 * skb_tx_error - report an sk_buff xmit error
761 * @skb: buffer that triggered an error
763 * Report xmit error if a device callback is tracking this skb.
764 * skb must be freed afterwards.
766 void skb_tx_error(struct sk_buff *skb)
768 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
769 struct ubuf_info *uarg;
771 uarg = skb_shinfo(skb)->destructor_arg;
773 uarg->callback(uarg, false);
774 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
777 EXPORT_SYMBOL(skb_tx_error);
780 * consume_skb - free an skbuff
781 * @skb: buffer to free
783 * Drop a ref to the buffer and free it if the usage count has hit zero
784 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
785 * is being dropped after a failure and notes that
787 void consume_skb(struct sk_buff *skb)
791 if (likely(atomic_read(&skb->users) == 1))
793 else if (likely(!atomic_dec_and_test(&skb->users)))
795 trace_consume_skb(skb);
798 EXPORT_SYMBOL(consume_skb);
800 /* Make sure a field is enclosed inside headers_start/headers_end section */
801 #define CHECK_SKB_FIELD(field) \
802 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
803 offsetof(struct sk_buff, headers_start)); \
804 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
805 offsetof(struct sk_buff, headers_end)); \
807 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
809 new->tstamp = old->tstamp;
810 /* We do not copy old->sk */
812 memcpy(new->cb, old->cb, sizeof(old->cb));
813 skb_dst_copy(new, old);
815 new->sp = secpath_get(old->sp);
817 __nf_copy(new, old, false);
819 /* Note : this field could be in headers_start/headers_end section
820 * It is not yet because we do not want to have a 16 bit hole
822 new->queue_mapping = old->queue_mapping;
824 memcpy(&new->headers_start, &old->headers_start,
825 offsetof(struct sk_buff, headers_end) -
826 offsetof(struct sk_buff, headers_start));
827 CHECK_SKB_FIELD(protocol);
828 CHECK_SKB_FIELD(csum);
829 CHECK_SKB_FIELD(hash);
830 CHECK_SKB_FIELD(priority);
831 CHECK_SKB_FIELD(skb_iif);
832 CHECK_SKB_FIELD(vlan_proto);
833 CHECK_SKB_FIELD(vlan_tci);
834 CHECK_SKB_FIELD(transport_header);
835 CHECK_SKB_FIELD(network_header);
836 CHECK_SKB_FIELD(mac_header);
837 CHECK_SKB_FIELD(inner_protocol);
838 CHECK_SKB_FIELD(inner_transport_header);
839 CHECK_SKB_FIELD(inner_network_header);
840 CHECK_SKB_FIELD(inner_mac_header);
841 CHECK_SKB_FIELD(mark);
842 #ifdef CONFIG_NETWORK_SECMARK
843 CHECK_SKB_FIELD(secmark);
845 #ifdef CONFIG_NET_RX_BUSY_POLL
846 CHECK_SKB_FIELD(napi_id);
849 CHECK_SKB_FIELD(sender_cpu);
851 #ifdef CONFIG_NET_SCHED
852 CHECK_SKB_FIELD(tc_index);
853 #ifdef CONFIG_NET_CLS_ACT
854 CHECK_SKB_FIELD(tc_verd);
861 * You should not add any new code to this function. Add it to
862 * __copy_skb_header above instead.
864 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
866 #define C(x) n->x = skb->x
868 n->next = n->prev = NULL;
870 __copy_skb_header(n, skb);
875 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
878 n->destructor = NULL;
885 atomic_set(&n->users, 1);
887 atomic_inc(&(skb_shinfo(skb)->dataref));
895 * skb_morph - morph one skb into another
896 * @dst: the skb to receive the contents
897 * @src: the skb to supply the contents
899 * This is identical to skb_clone except that the target skb is
900 * supplied by the user.
902 * The target skb is returned upon exit.
904 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
906 skb_release_all(dst);
907 return __skb_clone(dst, src);
909 EXPORT_SYMBOL_GPL(skb_morph);
912 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
913 * @skb: the skb to modify
914 * @gfp_mask: allocation priority
916 * This must be called on SKBTX_DEV_ZEROCOPY skb.
917 * It will copy all frags into kernel and drop the reference
918 * to userspace pages.
920 * If this function is called from an interrupt gfp_mask() must be
923 * Returns 0 on success or a negative error code on failure
924 * to allocate kernel memory to copy to.
926 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
929 int num_frags = skb_shinfo(skb)->nr_frags;
930 struct page *page, *head = NULL;
931 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
933 for (i = 0; i < num_frags; i++) {
935 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
937 page = alloc_page(gfp_mask);
940 struct page *next = (struct page *)page_private(head);
946 vaddr = kmap_atomic(skb_frag_page(f));
947 memcpy(page_address(page),
948 vaddr + f->page_offset, skb_frag_size(f));
949 kunmap_atomic(vaddr);
950 set_page_private(page, (unsigned long)head);
954 /* skb frags release userspace buffers */
955 for (i = 0; i < num_frags; i++)
956 skb_frag_unref(skb, i);
958 uarg->callback(uarg, false);
960 /* skb frags point to kernel buffers */
961 for (i = num_frags - 1; i >= 0; i--) {
962 __skb_fill_page_desc(skb, i, head, 0,
963 skb_shinfo(skb)->frags[i].size);
964 head = (struct page *)page_private(head);
967 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
970 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
973 * skb_clone - duplicate an sk_buff
974 * @skb: buffer to clone
975 * @gfp_mask: allocation priority
977 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
978 * copies share the same packet data but not structure. The new
979 * buffer has a reference count of 1. If the allocation fails the
980 * function returns %NULL otherwise the new buffer is returned.
982 * If this function is called from an interrupt gfp_mask() must be
986 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
988 struct sk_buff_fclones *fclones = container_of(skb,
989 struct sk_buff_fclones,
993 if (skb_orphan_frags(skb, gfp_mask))
996 if (skb->fclone == SKB_FCLONE_ORIG &&
997 atomic_read(&fclones->fclone_ref) == 1) {
999 atomic_set(&fclones->fclone_ref, 2);
1001 if (skb_pfmemalloc(skb))
1002 gfp_mask |= __GFP_MEMALLOC;
1004 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1008 kmemcheck_annotate_bitfield(n, flags1);
1009 n->fclone = SKB_FCLONE_UNAVAILABLE;
1012 return __skb_clone(n, skb);
1014 EXPORT_SYMBOL(skb_clone);
1016 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1018 /* Only adjust this if it actually is csum_start rather than csum */
1019 if (skb->ip_summed == CHECKSUM_PARTIAL)
1020 skb->csum_start += off;
1021 /* {transport,network,mac}_header and tail are relative to skb->head */
1022 skb->transport_header += off;
1023 skb->network_header += off;
1024 if (skb_mac_header_was_set(skb))
1025 skb->mac_header += off;
1026 skb->inner_transport_header += off;
1027 skb->inner_network_header += off;
1028 skb->inner_mac_header += off;
1031 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1033 __copy_skb_header(new, old);
1035 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1036 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1037 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1040 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1042 if (skb_pfmemalloc(skb))
1043 return SKB_ALLOC_RX;
1048 * skb_copy - create private copy of an sk_buff
1049 * @skb: buffer to copy
1050 * @gfp_mask: allocation priority
1052 * Make a copy of both an &sk_buff and its data. This is used when the
1053 * caller wishes to modify the data and needs a private copy of the
1054 * data to alter. Returns %NULL on failure or the pointer to the buffer
1055 * on success. The returned buffer has a reference count of 1.
1057 * As by-product this function converts non-linear &sk_buff to linear
1058 * one, so that &sk_buff becomes completely private and caller is allowed
1059 * to modify all the data of returned buffer. This means that this
1060 * function is not recommended for use in circumstances when only
1061 * header is going to be modified. Use pskb_copy() instead.
1064 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1066 int headerlen = skb_headroom(skb);
1067 unsigned int size = skb_end_offset(skb) + skb->data_len;
1068 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1069 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1074 /* Set the data pointer */
1075 skb_reserve(n, headerlen);
1076 /* Set the tail pointer and length */
1077 skb_put(n, skb->len);
1079 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1082 copy_skb_header(n, skb);
1085 EXPORT_SYMBOL(skb_copy);
1088 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1089 * @skb: buffer to copy
1090 * @headroom: headroom of new skb
1091 * @gfp_mask: allocation priority
1092 * @fclone: if true allocate the copy of the skb from the fclone
1093 * cache instead of the head cache; it is recommended to set this
1094 * to true for the cases where the copy will likely be cloned
1096 * Make a copy of both an &sk_buff and part of its data, located
1097 * in header. Fragmented data remain shared. This is used when
1098 * the caller wishes to modify only header of &sk_buff and needs
1099 * private copy of the header to alter. Returns %NULL on failure
1100 * or the pointer to the buffer on success.
1101 * The returned buffer has a reference count of 1.
1104 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1105 gfp_t gfp_mask, bool fclone)
1107 unsigned int size = skb_headlen(skb) + headroom;
1108 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1109 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1114 /* Set the data pointer */
1115 skb_reserve(n, headroom);
1116 /* Set the tail pointer and length */
1117 skb_put(n, skb_headlen(skb));
1118 /* Copy the bytes */
1119 skb_copy_from_linear_data(skb, n->data, n->len);
1121 n->truesize += skb->data_len;
1122 n->data_len = skb->data_len;
1125 if (skb_shinfo(skb)->nr_frags) {
1128 if (skb_orphan_frags(skb, gfp_mask)) {
1133 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1134 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1135 skb_frag_ref(skb, i);
1137 skb_shinfo(n)->nr_frags = i;
1140 if (skb_has_frag_list(skb)) {
1141 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1142 skb_clone_fraglist(n);
1145 copy_skb_header(n, skb);
1149 EXPORT_SYMBOL(__pskb_copy_fclone);
1152 * pskb_expand_head - reallocate header of &sk_buff
1153 * @skb: buffer to reallocate
1154 * @nhead: room to add at head
1155 * @ntail: room to add at tail
1156 * @gfp_mask: allocation priority
1158 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1159 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1160 * reference count of 1. Returns zero in the case of success or error,
1161 * if expansion failed. In the last case, &sk_buff is not changed.
1163 * All the pointers pointing into skb header may change and must be
1164 * reloaded after call to this function.
1167 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1172 int size = nhead + skb_end_offset(skb) + ntail;
1177 if (skb_shared(skb))
1180 size = SKB_DATA_ALIGN(size);
1182 if (skb_pfmemalloc(skb))
1183 gfp_mask |= __GFP_MEMALLOC;
1184 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1185 gfp_mask, NUMA_NO_NODE, NULL);
1188 size = SKB_WITH_OVERHEAD(ksize(data));
1190 /* Copy only real data... and, alas, header. This should be
1191 * optimized for the cases when header is void.
1193 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1195 memcpy((struct skb_shared_info *)(data + size),
1197 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1200 * if shinfo is shared we must drop the old head gracefully, but if it
1201 * is not we can just drop the old head and let the existing refcount
1202 * be since all we did is relocate the values
1204 if (skb_cloned(skb)) {
1205 /* copy this zero copy skb frags */
1206 if (skb_orphan_frags(skb, gfp_mask))
1208 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1209 skb_frag_ref(skb, i);
1211 if (skb_has_frag_list(skb))
1212 skb_clone_fraglist(skb);
1214 skb_release_data(skb);
1218 off = (data + nhead) - skb->head;
1223 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1227 skb->end = skb->head + size;
1230 skb_headers_offset_update(skb, nhead);
1234 atomic_set(&skb_shinfo(skb)->dataref, 1);
1242 EXPORT_SYMBOL(pskb_expand_head);
1244 /* Make private copy of skb with writable head and some headroom */
1246 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1248 struct sk_buff *skb2;
1249 int delta = headroom - skb_headroom(skb);
1252 skb2 = pskb_copy(skb, GFP_ATOMIC);
1254 skb2 = skb_clone(skb, GFP_ATOMIC);
1255 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1263 EXPORT_SYMBOL(skb_realloc_headroom);
1266 * skb_copy_expand - copy and expand sk_buff
1267 * @skb: buffer to copy
1268 * @newheadroom: new free bytes at head
1269 * @newtailroom: new free bytes at tail
1270 * @gfp_mask: allocation priority
1272 * Make a copy of both an &sk_buff and its data and while doing so
1273 * allocate additional space.
1275 * This is used when the caller wishes to modify the data and needs a
1276 * private copy of the data to alter as well as more space for new fields.
1277 * Returns %NULL on failure or the pointer to the buffer
1278 * on success. The returned buffer has a reference count of 1.
1280 * You must pass %GFP_ATOMIC as the allocation priority if this function
1281 * is called from an interrupt.
1283 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1284 int newheadroom, int newtailroom,
1288 * Allocate the copy buffer
1290 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1291 gfp_mask, skb_alloc_rx_flag(skb),
1293 int oldheadroom = skb_headroom(skb);
1294 int head_copy_len, head_copy_off;
1299 skb_reserve(n, newheadroom);
1301 /* Set the tail pointer and length */
1302 skb_put(n, skb->len);
1304 head_copy_len = oldheadroom;
1306 if (newheadroom <= head_copy_len)
1307 head_copy_len = newheadroom;
1309 head_copy_off = newheadroom - head_copy_len;
1311 /* Copy the linear header and data. */
1312 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1313 skb->len + head_copy_len))
1316 copy_skb_header(n, skb);
1318 skb_headers_offset_update(n, newheadroom - oldheadroom);
1322 EXPORT_SYMBOL(skb_copy_expand);
1325 * skb_pad - zero pad the tail of an skb
1326 * @skb: buffer to pad
1327 * @pad: space to pad
1329 * Ensure that a buffer is followed by a padding area that is zero
1330 * filled. Used by network drivers which may DMA or transfer data
1331 * beyond the buffer end onto the wire.
1333 * May return error in out of memory cases. The skb is freed on error.
1336 int skb_pad(struct sk_buff *skb, int pad)
1341 /* If the skbuff is non linear tailroom is always zero.. */
1342 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1343 memset(skb->data+skb->len, 0, pad);
1347 ntail = skb->data_len + pad - (skb->end - skb->tail);
1348 if (likely(skb_cloned(skb) || ntail > 0)) {
1349 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1354 /* FIXME: The use of this function with non-linear skb's really needs
1357 err = skb_linearize(skb);
1361 memset(skb->data + skb->len, 0, pad);
1368 EXPORT_SYMBOL(skb_pad);
1371 * pskb_put - add data to the tail of a potentially fragmented buffer
1372 * @skb: start of the buffer to use
1373 * @tail: tail fragment of the buffer to use
1374 * @len: amount of data to add
1376 * This function extends the used data area of the potentially
1377 * fragmented buffer. @tail must be the last fragment of @skb -- or
1378 * @skb itself. If this would exceed the total buffer size the kernel
1379 * will panic. A pointer to the first byte of the extra data is
1383 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1386 skb->data_len += len;
1389 return skb_put(tail, len);
1391 EXPORT_SYMBOL_GPL(pskb_put);
1394 * skb_put - add data to a buffer
1395 * @skb: buffer to use
1396 * @len: amount of data to add
1398 * This function extends the used data area of the buffer. If this would
1399 * exceed the total buffer size the kernel will panic. A pointer to the
1400 * first byte of the extra data is returned.
1402 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1404 unsigned char *tmp = skb_tail_pointer(skb);
1405 SKB_LINEAR_ASSERT(skb);
1408 if (unlikely(skb->tail > skb->end))
1409 skb_over_panic(skb, len, __builtin_return_address(0));
1412 EXPORT_SYMBOL(skb_put);
1415 * skb_push - add data to the start of a buffer
1416 * @skb: buffer to use
1417 * @len: amount of data to add
1419 * This function extends the used data area of the buffer at the buffer
1420 * start. If this would exceed the total buffer headroom the kernel will
1421 * panic. A pointer to the first byte of the extra data is returned.
1423 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1427 if (unlikely(skb->data<skb->head))
1428 skb_under_panic(skb, len, __builtin_return_address(0));
1431 EXPORT_SYMBOL(skb_push);
1434 * skb_pull - remove data from the start of a buffer
1435 * @skb: buffer to use
1436 * @len: amount of data to remove
1438 * This function removes data from the start of a buffer, returning
1439 * the memory to the headroom. A pointer to the next data in the buffer
1440 * is returned. Once the data has been pulled future pushes will overwrite
1443 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1445 return skb_pull_inline(skb, len);
1447 EXPORT_SYMBOL(skb_pull);
1450 * skb_trim - remove end from a buffer
1451 * @skb: buffer to alter
1454 * Cut the length of a buffer down by removing data from the tail. If
1455 * the buffer is already under the length specified it is not modified.
1456 * The skb must be linear.
1458 void skb_trim(struct sk_buff *skb, unsigned int len)
1461 __skb_trim(skb, len);
1463 EXPORT_SYMBOL(skb_trim);
1465 /* Trims skb to length len. It can change skb pointers.
1468 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1470 struct sk_buff **fragp;
1471 struct sk_buff *frag;
1472 int offset = skb_headlen(skb);
1473 int nfrags = skb_shinfo(skb)->nr_frags;
1477 if (skb_cloned(skb) &&
1478 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1485 for (; i < nfrags; i++) {
1486 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1493 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1496 skb_shinfo(skb)->nr_frags = i;
1498 for (; i < nfrags; i++)
1499 skb_frag_unref(skb, i);
1501 if (skb_has_frag_list(skb))
1502 skb_drop_fraglist(skb);
1506 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1507 fragp = &frag->next) {
1508 int end = offset + frag->len;
1510 if (skb_shared(frag)) {
1511 struct sk_buff *nfrag;
1513 nfrag = skb_clone(frag, GFP_ATOMIC);
1514 if (unlikely(!nfrag))
1517 nfrag->next = frag->next;
1529 unlikely((err = pskb_trim(frag, len - offset))))
1533 skb_drop_list(&frag->next);
1538 if (len > skb_headlen(skb)) {
1539 skb->data_len -= skb->len - len;
1544 skb_set_tail_pointer(skb, len);
1549 EXPORT_SYMBOL(___pskb_trim);
1552 * __pskb_pull_tail - advance tail of skb header
1553 * @skb: buffer to reallocate
1554 * @delta: number of bytes to advance tail
1556 * The function makes a sense only on a fragmented &sk_buff,
1557 * it expands header moving its tail forward and copying necessary
1558 * data from fragmented part.
1560 * &sk_buff MUST have reference count of 1.
1562 * Returns %NULL (and &sk_buff does not change) if pull failed
1563 * or value of new tail of skb in the case of success.
1565 * All the pointers pointing into skb header may change and must be
1566 * reloaded after call to this function.
1569 /* Moves tail of skb head forward, copying data from fragmented part,
1570 * when it is necessary.
1571 * 1. It may fail due to malloc failure.
1572 * 2. It may change skb pointers.
1574 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1576 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1578 /* If skb has not enough free space at tail, get new one
1579 * plus 128 bytes for future expansions. If we have enough
1580 * room at tail, reallocate without expansion only if skb is cloned.
1582 int i, k, eat = (skb->tail + delta) - skb->end;
1584 if (eat > 0 || skb_cloned(skb)) {
1585 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1590 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1593 /* Optimization: no fragments, no reasons to preestimate
1594 * size of pulled pages. Superb.
1596 if (!skb_has_frag_list(skb))
1599 /* Estimate size of pulled pages. */
1601 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1602 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1609 /* If we need update frag list, we are in troubles.
1610 * Certainly, it possible to add an offset to skb data,
1611 * but taking into account that pulling is expected to
1612 * be very rare operation, it is worth to fight against
1613 * further bloating skb head and crucify ourselves here instead.
1614 * Pure masohism, indeed. 8)8)
1617 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1618 struct sk_buff *clone = NULL;
1619 struct sk_buff *insp = NULL;
1624 if (list->len <= eat) {
1625 /* Eaten as whole. */
1630 /* Eaten partially. */
1632 if (skb_shared(list)) {
1633 /* Sucks! We need to fork list. :-( */
1634 clone = skb_clone(list, GFP_ATOMIC);
1640 /* This may be pulled without
1644 if (!pskb_pull(list, eat)) {
1652 /* Free pulled out fragments. */
1653 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1654 skb_shinfo(skb)->frag_list = list->next;
1657 /* And insert new clone at head. */
1660 skb_shinfo(skb)->frag_list = clone;
1663 /* Success! Now we may commit changes to skb data. */
1668 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1669 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1672 skb_frag_unref(skb, i);
1675 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1677 skb_shinfo(skb)->frags[k].page_offset += eat;
1678 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1684 skb_shinfo(skb)->nr_frags = k;
1687 skb->data_len -= delta;
1689 return skb_tail_pointer(skb);
1691 EXPORT_SYMBOL(__pskb_pull_tail);
1694 * skb_copy_bits - copy bits from skb to kernel buffer
1696 * @offset: offset in source
1697 * @to: destination buffer
1698 * @len: number of bytes to copy
1700 * Copy the specified number of bytes from the source skb to the
1701 * destination buffer.
1704 * If its prototype is ever changed,
1705 * check arch/{*}/net/{*}.S files,
1706 * since it is called from BPF assembly code.
1708 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1710 int start = skb_headlen(skb);
1711 struct sk_buff *frag_iter;
1714 if (offset > (int)skb->len - len)
1718 if ((copy = start - offset) > 0) {
1721 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1722 if ((len -= copy) == 0)
1728 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1730 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1732 WARN_ON(start > offset + len);
1734 end = start + skb_frag_size(f);
1735 if ((copy = end - offset) > 0) {
1741 vaddr = kmap_atomic(skb_frag_page(f));
1743 vaddr + f->page_offset + offset - start,
1745 kunmap_atomic(vaddr);
1747 if ((len -= copy) == 0)
1755 skb_walk_frags(skb, frag_iter) {
1758 WARN_ON(start > offset + len);
1760 end = start + frag_iter->len;
1761 if ((copy = end - offset) > 0) {
1764 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1766 if ((len -= copy) == 0)
1780 EXPORT_SYMBOL(skb_copy_bits);
1783 * Callback from splice_to_pipe(), if we need to release some pages
1784 * at the end of the spd in case we error'ed out in filling the pipe.
1786 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1788 put_page(spd->pages[i]);
1791 static struct page *linear_to_page(struct page *page, unsigned int *len,
1792 unsigned int *offset,
1795 struct page_frag *pfrag = sk_page_frag(sk);
1797 if (!sk_page_frag_refill(sk, pfrag))
1800 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1802 memcpy(page_address(pfrag->page) + pfrag->offset,
1803 page_address(page) + *offset, *len);
1804 *offset = pfrag->offset;
1805 pfrag->offset += *len;
1810 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1812 unsigned int offset)
1814 return spd->nr_pages &&
1815 spd->pages[spd->nr_pages - 1] == page &&
1816 (spd->partial[spd->nr_pages - 1].offset +
1817 spd->partial[spd->nr_pages - 1].len == offset);
1821 * Fill page/offset/length into spd, if it can hold more pages.
1823 static bool spd_fill_page(struct splice_pipe_desc *spd,
1824 struct pipe_inode_info *pipe, struct page *page,
1825 unsigned int *len, unsigned int offset,
1829 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1833 page = linear_to_page(page, len, &offset, sk);
1837 if (spd_can_coalesce(spd, page, offset)) {
1838 spd->partial[spd->nr_pages - 1].len += *len;
1842 spd->pages[spd->nr_pages] = page;
1843 spd->partial[spd->nr_pages].len = *len;
1844 spd->partial[spd->nr_pages].offset = offset;
1850 static bool __splice_segment(struct page *page, unsigned int poff,
1851 unsigned int plen, unsigned int *off,
1853 struct splice_pipe_desc *spd, bool linear,
1855 struct pipe_inode_info *pipe)
1860 /* skip this segment if already processed */
1866 /* ignore any bits we already processed */
1872 unsigned int flen = min(*len, plen);
1874 if (spd_fill_page(spd, pipe, page, &flen, poff,
1880 } while (*len && plen);
1886 * Map linear and fragment data from the skb to spd. It reports true if the
1887 * pipe is full or if we already spliced the requested length.
1889 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1890 unsigned int *offset, unsigned int *len,
1891 struct splice_pipe_desc *spd, struct sock *sk)
1895 /* map the linear part :
1896 * If skb->head_frag is set, this 'linear' part is backed by a
1897 * fragment, and if the head is not shared with any clones then
1898 * we can avoid a copy since we own the head portion of this page.
1900 if (__splice_segment(virt_to_page(skb->data),
1901 (unsigned long) skb->data & (PAGE_SIZE - 1),
1904 skb_head_is_locked(skb),
1909 * then map the fragments
1911 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1912 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1914 if (__splice_segment(skb_frag_page(f),
1915 f->page_offset, skb_frag_size(f),
1916 offset, len, spd, false, sk, pipe))
1924 * Map data from the skb to a pipe. Should handle both the linear part,
1925 * the fragments, and the frag list. It does NOT handle frag lists within
1926 * the frag list, if such a thing exists. We'd probably need to recurse to
1927 * handle that cleanly.
1929 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1930 struct pipe_inode_info *pipe, unsigned int tlen,
1933 struct partial_page partial[MAX_SKB_FRAGS];
1934 struct page *pages[MAX_SKB_FRAGS];
1935 struct splice_pipe_desc spd = {
1938 .nr_pages_max = MAX_SKB_FRAGS,
1940 .ops = &nosteal_pipe_buf_ops,
1941 .spd_release = sock_spd_release,
1943 struct sk_buff *frag_iter;
1944 struct sock *sk = skb->sk;
1948 * __skb_splice_bits() only fails if the output has no room left,
1949 * so no point in going over the frag_list for the error case.
1951 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1957 * now see if we have a frag_list to map
1959 skb_walk_frags(skb, frag_iter) {
1962 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1969 * Drop the socket lock, otherwise we have reverse
1970 * locking dependencies between sk_lock and i_mutex
1971 * here as compared to sendfile(). We enter here
1972 * with the socket lock held, and splice_to_pipe() will
1973 * grab the pipe inode lock. For sendfile() emulation,
1974 * we call into ->sendpage() with the i_mutex lock held
1975 * and networking will grab the socket lock.
1978 ret = splice_to_pipe(pipe, &spd);
1986 * skb_store_bits - store bits from kernel buffer to skb
1987 * @skb: destination buffer
1988 * @offset: offset in destination
1989 * @from: source buffer
1990 * @len: number of bytes to copy
1992 * Copy the specified number of bytes from the source buffer to the
1993 * destination skb. This function handles all the messy bits of
1994 * traversing fragment lists and such.
1997 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1999 int start = skb_headlen(skb);
2000 struct sk_buff *frag_iter;
2003 if (offset > (int)skb->len - len)
2006 if ((copy = start - offset) > 0) {
2009 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2010 if ((len -= copy) == 0)
2016 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2017 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2020 WARN_ON(start > offset + len);
2022 end = start + skb_frag_size(frag);
2023 if ((copy = end - offset) > 0) {
2029 vaddr = kmap_atomic(skb_frag_page(frag));
2030 memcpy(vaddr + frag->page_offset + offset - start,
2032 kunmap_atomic(vaddr);
2034 if ((len -= copy) == 0)
2042 skb_walk_frags(skb, frag_iter) {
2045 WARN_ON(start > offset + len);
2047 end = start + frag_iter->len;
2048 if ((copy = end - offset) > 0) {
2051 if (skb_store_bits(frag_iter, offset - start,
2054 if ((len -= copy) == 0)
2067 EXPORT_SYMBOL(skb_store_bits);
2069 /* Checksum skb data. */
2070 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2071 __wsum csum, const struct skb_checksum_ops *ops)
2073 int start = skb_headlen(skb);
2074 int i, copy = start - offset;
2075 struct sk_buff *frag_iter;
2078 /* Checksum header. */
2082 csum = ops->update(skb->data + offset, copy, csum);
2083 if ((len -= copy) == 0)
2089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2091 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2093 WARN_ON(start > offset + len);
2095 end = start + skb_frag_size(frag);
2096 if ((copy = end - offset) > 0) {
2102 vaddr = kmap_atomic(skb_frag_page(frag));
2103 csum2 = ops->update(vaddr + frag->page_offset +
2104 offset - start, copy, 0);
2105 kunmap_atomic(vaddr);
2106 csum = ops->combine(csum, csum2, pos, copy);
2115 skb_walk_frags(skb, frag_iter) {
2118 WARN_ON(start > offset + len);
2120 end = start + frag_iter->len;
2121 if ((copy = end - offset) > 0) {
2125 csum2 = __skb_checksum(frag_iter, offset - start,
2127 csum = ops->combine(csum, csum2, pos, copy);
2128 if ((len -= copy) == 0)
2139 EXPORT_SYMBOL(__skb_checksum);
2141 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2142 int len, __wsum csum)
2144 const struct skb_checksum_ops ops = {
2145 .update = csum_partial_ext,
2146 .combine = csum_block_add_ext,
2149 return __skb_checksum(skb, offset, len, csum, &ops);
2151 EXPORT_SYMBOL(skb_checksum);
2153 /* Both of above in one bottle. */
2155 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2156 u8 *to, int len, __wsum csum)
2158 int start = skb_headlen(skb);
2159 int i, copy = start - offset;
2160 struct sk_buff *frag_iter;
2167 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2169 if ((len -= copy) == 0)
2176 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2179 WARN_ON(start > offset + len);
2181 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2182 if ((copy = end - offset) > 0) {
2185 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2189 vaddr = kmap_atomic(skb_frag_page(frag));
2190 csum2 = csum_partial_copy_nocheck(vaddr +
2194 kunmap_atomic(vaddr);
2195 csum = csum_block_add(csum, csum2, pos);
2205 skb_walk_frags(skb, frag_iter) {
2209 WARN_ON(start > offset + len);
2211 end = start + frag_iter->len;
2212 if ((copy = end - offset) > 0) {
2215 csum2 = skb_copy_and_csum_bits(frag_iter,
2218 csum = csum_block_add(csum, csum2, pos);
2219 if ((len -= copy) == 0)
2230 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2233 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2234 * @from: source buffer
2236 * Calculates the amount of linear headroom needed in the 'to' skb passed
2237 * into skb_zerocopy().
2240 skb_zerocopy_headlen(const struct sk_buff *from)
2242 unsigned int hlen = 0;
2244 if (!from->head_frag ||
2245 skb_headlen(from) < L1_CACHE_BYTES ||
2246 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2247 hlen = skb_headlen(from);
2249 if (skb_has_frag_list(from))
2254 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2257 * skb_zerocopy - Zero copy skb to skb
2258 * @to: destination buffer
2259 * @from: source buffer
2260 * @len: number of bytes to copy from source buffer
2261 * @hlen: size of linear headroom in destination buffer
2263 * Copies up to `len` bytes from `from` to `to` by creating references
2264 * to the frags in the source buffer.
2266 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2267 * headroom in the `to` buffer.
2270 * 0: everything is OK
2271 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2272 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2275 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2278 int plen = 0; /* length of skb->head fragment */
2281 unsigned int offset;
2283 BUG_ON(!from->head_frag && !hlen);
2285 /* dont bother with small payloads */
2286 if (len <= skb_tailroom(to))
2287 return skb_copy_bits(from, 0, skb_put(to, len), len);
2290 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2295 plen = min_t(int, skb_headlen(from), len);
2297 page = virt_to_head_page(from->head);
2298 offset = from->data - (unsigned char *)page_address(page);
2299 __skb_fill_page_desc(to, 0, page, offset, plen);
2306 to->truesize += len + plen;
2307 to->len += len + plen;
2308 to->data_len += len + plen;
2310 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2315 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2318 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2319 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2320 len -= skb_shinfo(to)->frags[j].size;
2321 skb_frag_ref(to, j);
2324 skb_shinfo(to)->nr_frags = j;
2328 EXPORT_SYMBOL_GPL(skb_zerocopy);
2330 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2335 if (skb->ip_summed == CHECKSUM_PARTIAL)
2336 csstart = skb_checksum_start_offset(skb);
2338 csstart = skb_headlen(skb);
2340 BUG_ON(csstart > skb_headlen(skb));
2342 skb_copy_from_linear_data(skb, to, csstart);
2345 if (csstart != skb->len)
2346 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2347 skb->len - csstart, 0);
2349 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2350 long csstuff = csstart + skb->csum_offset;
2352 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2355 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2358 * skb_dequeue - remove from the head of the queue
2359 * @list: list to dequeue from
2361 * Remove the head of the list. The list lock is taken so the function
2362 * may be used safely with other locking list functions. The head item is
2363 * returned or %NULL if the list is empty.
2366 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2368 unsigned long flags;
2369 struct sk_buff *result;
2371 spin_lock_irqsave(&list->lock, flags);
2372 result = __skb_dequeue(list);
2373 spin_unlock_irqrestore(&list->lock, flags);
2376 EXPORT_SYMBOL(skb_dequeue);
2379 * skb_dequeue_tail - remove from the tail of the queue
2380 * @list: list to dequeue from
2382 * Remove the tail of the list. The list lock is taken so the function
2383 * may be used safely with other locking list functions. The tail item is
2384 * returned or %NULL if the list is empty.
2386 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2388 unsigned long flags;
2389 struct sk_buff *result;
2391 spin_lock_irqsave(&list->lock, flags);
2392 result = __skb_dequeue_tail(list);
2393 spin_unlock_irqrestore(&list->lock, flags);
2396 EXPORT_SYMBOL(skb_dequeue_tail);
2399 * skb_queue_purge - empty a list
2400 * @list: list to empty
2402 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2403 * the list and one reference dropped. This function takes the list
2404 * lock and is atomic with respect to other list locking functions.
2406 void skb_queue_purge(struct sk_buff_head *list)
2408 struct sk_buff *skb;
2409 while ((skb = skb_dequeue(list)) != NULL)
2412 EXPORT_SYMBOL(skb_queue_purge);
2415 * skb_queue_head - queue a buffer at the list head
2416 * @list: list to use
2417 * @newsk: buffer to queue
2419 * Queue a buffer at the start of the list. This function takes the
2420 * list lock and can be used safely with other locking &sk_buff functions
2423 * A buffer cannot be placed on two lists at the same time.
2425 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2427 unsigned long flags;
2429 spin_lock_irqsave(&list->lock, flags);
2430 __skb_queue_head(list, newsk);
2431 spin_unlock_irqrestore(&list->lock, flags);
2433 EXPORT_SYMBOL(skb_queue_head);
2436 * skb_queue_tail - queue a buffer at the list tail
2437 * @list: list to use
2438 * @newsk: buffer to queue
2440 * Queue a buffer at the tail of the list. This function takes the
2441 * list lock and can be used safely with other locking &sk_buff functions
2444 * A buffer cannot be placed on two lists at the same time.
2446 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2448 unsigned long flags;
2450 spin_lock_irqsave(&list->lock, flags);
2451 __skb_queue_tail(list, newsk);
2452 spin_unlock_irqrestore(&list->lock, flags);
2454 EXPORT_SYMBOL(skb_queue_tail);
2457 * skb_unlink - remove a buffer from a list
2458 * @skb: buffer to remove
2459 * @list: list to use
2461 * Remove a packet from a list. The list locks are taken and this
2462 * function is atomic with respect to other list locked calls
2464 * You must know what list the SKB is on.
2466 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2468 unsigned long flags;
2470 spin_lock_irqsave(&list->lock, flags);
2471 __skb_unlink(skb, list);
2472 spin_unlock_irqrestore(&list->lock, flags);
2474 EXPORT_SYMBOL(skb_unlink);
2477 * skb_append - append a buffer
2478 * @old: buffer to insert after
2479 * @newsk: buffer to insert
2480 * @list: list to use
2482 * Place a packet after a given packet in a list. The list locks are taken
2483 * and this function is atomic with respect to other list locked calls.
2484 * A buffer cannot be placed on two lists at the same time.
2486 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2488 unsigned long flags;
2490 spin_lock_irqsave(&list->lock, flags);
2491 __skb_queue_after(list, old, newsk);
2492 spin_unlock_irqrestore(&list->lock, flags);
2494 EXPORT_SYMBOL(skb_append);
2497 * skb_insert - insert a buffer
2498 * @old: buffer to insert before
2499 * @newsk: buffer to insert
2500 * @list: list to use
2502 * Place a packet before a given packet in a list. The list locks are
2503 * taken and this function is atomic with respect to other list locked
2506 * A buffer cannot be placed on two lists at the same time.
2508 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2510 unsigned long flags;
2512 spin_lock_irqsave(&list->lock, flags);
2513 __skb_insert(newsk, old->prev, old, list);
2514 spin_unlock_irqrestore(&list->lock, flags);
2516 EXPORT_SYMBOL(skb_insert);
2518 static inline void skb_split_inside_header(struct sk_buff *skb,
2519 struct sk_buff* skb1,
2520 const u32 len, const int pos)
2524 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2526 /* And move data appendix as is. */
2527 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2528 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2530 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2531 skb_shinfo(skb)->nr_frags = 0;
2532 skb1->data_len = skb->data_len;
2533 skb1->len += skb1->data_len;
2536 skb_set_tail_pointer(skb, len);
2539 static inline void skb_split_no_header(struct sk_buff *skb,
2540 struct sk_buff* skb1,
2541 const u32 len, int pos)
2544 const int nfrags = skb_shinfo(skb)->nr_frags;
2546 skb_shinfo(skb)->nr_frags = 0;
2547 skb1->len = skb1->data_len = skb->len - len;
2549 skb->data_len = len - pos;
2551 for (i = 0; i < nfrags; i++) {
2552 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2554 if (pos + size > len) {
2555 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2559 * We have two variants in this case:
2560 * 1. Move all the frag to the second
2561 * part, if it is possible. F.e.
2562 * this approach is mandatory for TUX,
2563 * where splitting is expensive.
2564 * 2. Split is accurately. We make this.
2566 skb_frag_ref(skb, i);
2567 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2568 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2569 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2570 skb_shinfo(skb)->nr_frags++;
2574 skb_shinfo(skb)->nr_frags++;
2577 skb_shinfo(skb1)->nr_frags = k;
2581 * skb_split - Split fragmented skb to two parts at length len.
2582 * @skb: the buffer to split
2583 * @skb1: the buffer to receive the second part
2584 * @len: new length for skb
2586 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2588 int pos = skb_headlen(skb);
2590 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2591 if (len < pos) /* Split line is inside header. */
2592 skb_split_inside_header(skb, skb1, len, pos);
2593 else /* Second chunk has no header, nothing to copy. */
2594 skb_split_no_header(skb, skb1, len, pos);
2596 EXPORT_SYMBOL(skb_split);
2598 /* Shifting from/to a cloned skb is a no-go.
2600 * Caller cannot keep skb_shinfo related pointers past calling here!
2602 static int skb_prepare_for_shift(struct sk_buff *skb)
2604 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2608 * skb_shift - Shifts paged data partially from skb to another
2609 * @tgt: buffer into which tail data gets added
2610 * @skb: buffer from which the paged data comes from
2611 * @shiftlen: shift up to this many bytes
2613 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2614 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2615 * It's up to caller to free skb if everything was shifted.
2617 * If @tgt runs out of frags, the whole operation is aborted.
2619 * Skb cannot include anything else but paged data while tgt is allowed
2620 * to have non-paged data as well.
2622 * TODO: full sized shift could be optimized but that would need
2623 * specialized skb free'er to handle frags without up-to-date nr_frags.
2625 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2627 int from, to, merge, todo;
2628 struct skb_frag_struct *fragfrom, *fragto;
2630 BUG_ON(shiftlen > skb->len);
2631 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2635 to = skb_shinfo(tgt)->nr_frags;
2636 fragfrom = &skb_shinfo(skb)->frags[from];
2638 /* Actual merge is delayed until the point when we know we can
2639 * commit all, so that we don't have to undo partial changes
2642 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2643 fragfrom->page_offset)) {
2648 todo -= skb_frag_size(fragfrom);
2650 if (skb_prepare_for_shift(skb) ||
2651 skb_prepare_for_shift(tgt))
2654 /* All previous frag pointers might be stale! */
2655 fragfrom = &skb_shinfo(skb)->frags[from];
2656 fragto = &skb_shinfo(tgt)->frags[merge];
2658 skb_frag_size_add(fragto, shiftlen);
2659 skb_frag_size_sub(fragfrom, shiftlen);
2660 fragfrom->page_offset += shiftlen;
2668 /* Skip full, not-fitting skb to avoid expensive operations */
2669 if ((shiftlen == skb->len) &&
2670 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2673 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2676 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2677 if (to == MAX_SKB_FRAGS)
2680 fragfrom = &skb_shinfo(skb)->frags[from];
2681 fragto = &skb_shinfo(tgt)->frags[to];
2683 if (todo >= skb_frag_size(fragfrom)) {
2684 *fragto = *fragfrom;
2685 todo -= skb_frag_size(fragfrom);
2690 __skb_frag_ref(fragfrom);
2691 fragto->page = fragfrom->page;
2692 fragto->page_offset = fragfrom->page_offset;
2693 skb_frag_size_set(fragto, todo);
2695 fragfrom->page_offset += todo;
2696 skb_frag_size_sub(fragfrom, todo);
2704 /* Ready to "commit" this state change to tgt */
2705 skb_shinfo(tgt)->nr_frags = to;
2708 fragfrom = &skb_shinfo(skb)->frags[0];
2709 fragto = &skb_shinfo(tgt)->frags[merge];
2711 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2712 __skb_frag_unref(fragfrom);
2715 /* Reposition in the original skb */
2717 while (from < skb_shinfo(skb)->nr_frags)
2718 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2719 skb_shinfo(skb)->nr_frags = to;
2721 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2724 /* Most likely the tgt won't ever need its checksum anymore, skb on
2725 * the other hand might need it if it needs to be resent
2727 tgt->ip_summed = CHECKSUM_PARTIAL;
2728 skb->ip_summed = CHECKSUM_PARTIAL;
2730 /* Yak, is it really working this way? Some helper please? */
2731 skb->len -= shiftlen;
2732 skb->data_len -= shiftlen;
2733 skb->truesize -= shiftlen;
2734 tgt->len += shiftlen;
2735 tgt->data_len += shiftlen;
2736 tgt->truesize += shiftlen;
2742 * skb_prepare_seq_read - Prepare a sequential read of skb data
2743 * @skb: the buffer to read
2744 * @from: lower offset of data to be read
2745 * @to: upper offset of data to be read
2746 * @st: state variable
2748 * Initializes the specified state variable. Must be called before
2749 * invoking skb_seq_read() for the first time.
2751 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2752 unsigned int to, struct skb_seq_state *st)
2754 st->lower_offset = from;
2755 st->upper_offset = to;
2756 st->root_skb = st->cur_skb = skb;
2757 st->frag_idx = st->stepped_offset = 0;
2758 st->frag_data = NULL;
2760 EXPORT_SYMBOL(skb_prepare_seq_read);
2763 * skb_seq_read - Sequentially read skb data
2764 * @consumed: number of bytes consumed by the caller so far
2765 * @data: destination pointer for data to be returned
2766 * @st: state variable
2768 * Reads a block of skb data at @consumed relative to the
2769 * lower offset specified to skb_prepare_seq_read(). Assigns
2770 * the head of the data block to @data and returns the length
2771 * of the block or 0 if the end of the skb data or the upper
2772 * offset has been reached.
2774 * The caller is not required to consume all of the data
2775 * returned, i.e. @consumed is typically set to the number
2776 * of bytes already consumed and the next call to
2777 * skb_seq_read() will return the remaining part of the block.
2779 * Note 1: The size of each block of data returned can be arbitrary,
2780 * this limitation is the cost for zerocopy sequential
2781 * reads of potentially non linear data.
2783 * Note 2: Fragment lists within fragments are not implemented
2784 * at the moment, state->root_skb could be replaced with
2785 * a stack for this purpose.
2787 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2788 struct skb_seq_state *st)
2790 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2793 if (unlikely(abs_offset >= st->upper_offset)) {
2794 if (st->frag_data) {
2795 kunmap_atomic(st->frag_data);
2796 st->frag_data = NULL;
2802 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2804 if (abs_offset < block_limit && !st->frag_data) {
2805 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2806 return block_limit - abs_offset;
2809 if (st->frag_idx == 0 && !st->frag_data)
2810 st->stepped_offset += skb_headlen(st->cur_skb);
2812 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2813 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2814 block_limit = skb_frag_size(frag) + st->stepped_offset;
2816 if (abs_offset < block_limit) {
2818 st->frag_data = kmap_atomic(skb_frag_page(frag));
2820 *data = (u8 *) st->frag_data + frag->page_offset +
2821 (abs_offset - st->stepped_offset);
2823 return block_limit - abs_offset;
2826 if (st->frag_data) {
2827 kunmap_atomic(st->frag_data);
2828 st->frag_data = NULL;
2832 st->stepped_offset += skb_frag_size(frag);
2835 if (st->frag_data) {
2836 kunmap_atomic(st->frag_data);
2837 st->frag_data = NULL;
2840 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2841 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2844 } else if (st->cur_skb->next) {
2845 st->cur_skb = st->cur_skb->next;
2852 EXPORT_SYMBOL(skb_seq_read);
2855 * skb_abort_seq_read - Abort a sequential read of skb data
2856 * @st: state variable
2858 * Must be called if skb_seq_read() was not called until it
2861 void skb_abort_seq_read(struct skb_seq_state *st)
2864 kunmap_atomic(st->frag_data);
2866 EXPORT_SYMBOL(skb_abort_seq_read);
2868 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2870 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2871 struct ts_config *conf,
2872 struct ts_state *state)
2874 return skb_seq_read(offset, text, TS_SKB_CB(state));
2877 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2879 skb_abort_seq_read(TS_SKB_CB(state));
2883 * skb_find_text - Find a text pattern in skb data
2884 * @skb: the buffer to look in
2885 * @from: search offset
2887 * @config: textsearch configuration
2889 * Finds a pattern in the skb data according to the specified
2890 * textsearch configuration. Use textsearch_next() to retrieve
2891 * subsequent occurrences of the pattern. Returns the offset
2892 * to the first occurrence or UINT_MAX if no match was found.
2894 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2895 unsigned int to, struct ts_config *config)
2897 struct ts_state state;
2900 config->get_next_block = skb_ts_get_next_block;
2901 config->finish = skb_ts_finish;
2903 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2905 ret = textsearch_find(config, &state);
2906 return (ret <= to - from ? ret : UINT_MAX);
2908 EXPORT_SYMBOL(skb_find_text);
2911 * skb_append_datato_frags - append the user data to a skb
2912 * @sk: sock structure
2913 * @skb: skb structure to be appended with user data.
2914 * @getfrag: call back function to be used for getting the user data
2915 * @from: pointer to user message iov
2916 * @length: length of the iov message
2918 * Description: This procedure append the user data in the fragment part
2919 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2921 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2922 int (*getfrag)(void *from, char *to, int offset,
2923 int len, int odd, struct sk_buff *skb),
2924 void *from, int length)
2926 int frg_cnt = skb_shinfo(skb)->nr_frags;
2930 struct page_frag *pfrag = ¤t->task_frag;
2933 /* Return error if we don't have space for new frag */
2934 if (frg_cnt >= MAX_SKB_FRAGS)
2937 if (!sk_page_frag_refill(sk, pfrag))
2940 /* copy the user data to page */
2941 copy = min_t(int, length, pfrag->size - pfrag->offset);
2943 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2944 offset, copy, 0, skb);
2948 /* copy was successful so update the size parameters */
2949 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2952 pfrag->offset += copy;
2953 get_page(pfrag->page);
2955 skb->truesize += copy;
2956 atomic_add(copy, &sk->sk_wmem_alloc);
2958 skb->data_len += copy;
2962 } while (length > 0);
2966 EXPORT_SYMBOL(skb_append_datato_frags);
2969 * skb_pull_rcsum - pull skb and update receive checksum
2970 * @skb: buffer to update
2971 * @len: length of data pulled
2973 * This function performs an skb_pull on the packet and updates
2974 * the CHECKSUM_COMPLETE checksum. It should be used on
2975 * receive path processing instead of skb_pull unless you know
2976 * that the checksum difference is zero (e.g., a valid IP header)
2977 * or you are setting ip_summed to CHECKSUM_NONE.
2979 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2981 BUG_ON(len > skb->len);
2983 BUG_ON(skb->len < skb->data_len);
2984 skb_postpull_rcsum(skb, skb->data, len);
2985 return skb->data += len;
2987 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2990 * skb_segment - Perform protocol segmentation on skb.
2991 * @head_skb: buffer to segment
2992 * @features: features for the output path (see dev->features)
2994 * This function performs segmentation on the given skb. It returns
2995 * a pointer to the first in a list of new skbs for the segments.
2996 * In case of error it returns ERR_PTR(err).
2998 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2999 netdev_features_t features)
3001 struct sk_buff *segs = NULL;
3002 struct sk_buff *tail = NULL;
3003 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3004 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3005 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3006 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3007 struct sk_buff *frag_skb = head_skb;
3008 unsigned int offset = doffset;
3009 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3010 unsigned int headroom;
3014 int sg = !!(features & NETIF_F_SG);
3015 int nfrags = skb_shinfo(head_skb)->nr_frags;
3021 __skb_push(head_skb, doffset);
3022 proto = skb_network_protocol(head_skb, &dummy);
3023 if (unlikely(!proto))
3024 return ERR_PTR(-EINVAL);
3026 csum = !head_skb->encap_hdr_csum &&
3027 !!can_checksum_protocol(features, proto);
3029 headroom = skb_headroom(head_skb);
3030 pos = skb_headlen(head_skb);
3033 struct sk_buff *nskb;
3034 skb_frag_t *nskb_frag;
3038 len = head_skb->len - offset;
3042 hsize = skb_headlen(head_skb) - offset;
3045 if (hsize > len || !sg)
3048 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3049 (skb_headlen(list_skb) == len || sg)) {
3050 BUG_ON(skb_headlen(list_skb) > len);
3053 nfrags = skb_shinfo(list_skb)->nr_frags;
3054 frag = skb_shinfo(list_skb)->frags;
3055 frag_skb = list_skb;
3056 pos += skb_headlen(list_skb);
3058 while (pos < offset + len) {
3059 BUG_ON(i >= nfrags);
3061 size = skb_frag_size(frag);
3062 if (pos + size > offset + len)
3070 nskb = skb_clone(list_skb, GFP_ATOMIC);
3071 list_skb = list_skb->next;
3073 if (unlikely(!nskb))
3076 if (unlikely(pskb_trim(nskb, len))) {
3081 hsize = skb_end_offset(nskb);
3082 if (skb_cow_head(nskb, doffset + headroom)) {
3087 nskb->truesize += skb_end_offset(nskb) - hsize;
3088 skb_release_head_state(nskb);
3089 __skb_push(nskb, doffset);
3091 nskb = __alloc_skb(hsize + doffset + headroom,
3092 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3095 if (unlikely(!nskb))
3098 skb_reserve(nskb, headroom);
3099 __skb_put(nskb, doffset);
3108 __copy_skb_header(nskb, head_skb);
3110 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3111 skb_reset_mac_len(nskb);
3113 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3114 nskb->data - tnl_hlen,
3115 doffset + tnl_hlen);
3117 if (nskb->len == len + doffset)
3118 goto perform_csum_check;
3120 if (!sg && !nskb->remcsum_offload) {
3121 nskb->ip_summed = CHECKSUM_NONE;
3122 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3125 SKB_GSO_CB(nskb)->csum_start =
3126 skb_headroom(nskb) + doffset;
3130 nskb_frag = skb_shinfo(nskb)->frags;
3132 skb_copy_from_linear_data_offset(head_skb, offset,
3133 skb_put(nskb, hsize), hsize);
3135 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3138 while (pos < offset + len) {
3140 BUG_ON(skb_headlen(list_skb));
3143 nfrags = skb_shinfo(list_skb)->nr_frags;
3144 frag = skb_shinfo(list_skb)->frags;
3145 frag_skb = list_skb;
3149 list_skb = list_skb->next;
3152 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3154 net_warn_ratelimited(
3155 "skb_segment: too many frags: %u %u\n",
3160 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3164 __skb_frag_ref(nskb_frag);
3165 size = skb_frag_size(nskb_frag);
3168 nskb_frag->page_offset += offset - pos;
3169 skb_frag_size_sub(nskb_frag, offset - pos);
3172 skb_shinfo(nskb)->nr_frags++;
3174 if (pos + size <= offset + len) {
3179 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3187 nskb->data_len = len - hsize;
3188 nskb->len += nskb->data_len;
3189 nskb->truesize += nskb->data_len;
3192 if (!csum && !nskb->remcsum_offload) {
3193 nskb->csum = skb_checksum(nskb, doffset,
3194 nskb->len - doffset, 0);
3195 nskb->ip_summed = CHECKSUM_NONE;
3196 SKB_GSO_CB(nskb)->csum_start =
3197 skb_headroom(nskb) + doffset;
3199 } while ((offset += len) < head_skb->len);
3201 /* Some callers want to get the end of the list.
3202 * Put it in segs->prev to avoid walking the list.
3203 * (see validate_xmit_skb_list() for example)
3207 /* Following permits correct backpressure, for protocols
3208 * using skb_set_owner_w().
3209 * Idea is to tranfert ownership from head_skb to last segment.
3211 if (head_skb->destructor == sock_wfree) {
3212 swap(tail->truesize, head_skb->truesize);
3213 swap(tail->destructor, head_skb->destructor);
3214 swap(tail->sk, head_skb->sk);
3219 kfree_skb_list(segs);
3220 return ERR_PTR(err);
3222 EXPORT_SYMBOL_GPL(skb_segment);
3224 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3226 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3227 unsigned int offset = skb_gro_offset(skb);
3228 unsigned int headlen = skb_headlen(skb);
3229 unsigned int len = skb_gro_len(skb);
3230 struct sk_buff *lp, *p = *head;
3231 unsigned int delta_truesize;
3233 if (unlikely(p->len + len >= 65536))
3236 lp = NAPI_GRO_CB(p)->last;
3237 pinfo = skb_shinfo(lp);
3239 if (headlen <= offset) {
3242 int i = skbinfo->nr_frags;
3243 int nr_frags = pinfo->nr_frags + i;
3245 if (nr_frags > MAX_SKB_FRAGS)
3249 pinfo->nr_frags = nr_frags;
3250 skbinfo->nr_frags = 0;
3252 frag = pinfo->frags + nr_frags;
3253 frag2 = skbinfo->frags + i;
3258 frag->page_offset += offset;
3259 skb_frag_size_sub(frag, offset);
3261 /* all fragments truesize : remove (head size + sk_buff) */
3262 delta_truesize = skb->truesize -
3263 SKB_TRUESIZE(skb_end_offset(skb));
3265 skb->truesize -= skb->data_len;
3266 skb->len -= skb->data_len;
3269 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3271 } else if (skb->head_frag) {
3272 int nr_frags = pinfo->nr_frags;
3273 skb_frag_t *frag = pinfo->frags + nr_frags;
3274 struct page *page = virt_to_head_page(skb->head);
3275 unsigned int first_size = headlen - offset;
3276 unsigned int first_offset;
3278 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3281 first_offset = skb->data -
3282 (unsigned char *)page_address(page) +
3285 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3287 frag->page.p = page;
3288 frag->page_offset = first_offset;
3289 skb_frag_size_set(frag, first_size);
3291 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3292 /* We dont need to clear skbinfo->nr_frags here */
3294 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3295 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3300 delta_truesize = skb->truesize;
3301 if (offset > headlen) {
3302 unsigned int eat = offset - headlen;
3304 skbinfo->frags[0].page_offset += eat;
3305 skb_frag_size_sub(&skbinfo->frags[0], eat);
3306 skb->data_len -= eat;
3311 __skb_pull(skb, offset);
3313 if (NAPI_GRO_CB(p)->last == p)
3314 skb_shinfo(p)->frag_list = skb;
3316 NAPI_GRO_CB(p)->last->next = skb;
3317 NAPI_GRO_CB(p)->last = skb;
3318 __skb_header_release(skb);
3322 NAPI_GRO_CB(p)->count++;
3324 p->truesize += delta_truesize;
3327 lp->data_len += len;
3328 lp->truesize += delta_truesize;
3331 NAPI_GRO_CB(skb)->same_flow = 1;
3335 void __init skb_init(void)
3337 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3338 sizeof(struct sk_buff),
3340 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3342 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3343 sizeof(struct sk_buff_fclones),
3345 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3350 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3351 * @skb: Socket buffer containing the buffers to be mapped
3352 * @sg: The scatter-gather list to map into
3353 * @offset: The offset into the buffer's contents to start mapping
3354 * @len: Length of buffer space to be mapped
3356 * Fill the specified scatter-gather list with mappings/pointers into a
3357 * region of the buffer space attached to a socket buffer.
3360 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3362 int start = skb_headlen(skb);
3363 int i, copy = start - offset;
3364 struct sk_buff *frag_iter;
3370 sg_set_buf(sg, skb->data + offset, copy);
3372 if ((len -= copy) == 0)
3377 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3380 WARN_ON(start > offset + len);
3382 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3383 if ((copy = end - offset) > 0) {
3384 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3388 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3389 frag->page_offset+offset-start);
3398 skb_walk_frags(skb, frag_iter) {
3401 WARN_ON(start > offset + len);
3403 end = start + frag_iter->len;
3404 if ((copy = end - offset) > 0) {
3407 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3409 if ((len -= copy) == 0)
3419 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3420 * sglist without mark the sg which contain last skb data as the end.
3421 * So the caller can mannipulate sg list as will when padding new data after
3422 * the first call without calling sg_unmark_end to expend sg list.
3424 * Scenario to use skb_to_sgvec_nomark:
3426 * 2. skb_to_sgvec_nomark(payload1)
3427 * 3. skb_to_sgvec_nomark(payload2)
3429 * This is equivalent to:
3431 * 2. skb_to_sgvec(payload1)
3433 * 4. skb_to_sgvec(payload2)
3435 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3436 * is more preferable.
3438 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3439 int offset, int len)
3441 return __skb_to_sgvec(skb, sg, offset, len);
3443 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3445 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3447 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3449 sg_mark_end(&sg[nsg - 1]);
3453 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3456 * skb_cow_data - Check that a socket buffer's data buffers are writable
3457 * @skb: The socket buffer to check.
3458 * @tailbits: Amount of trailing space to be added
3459 * @trailer: Returned pointer to the skb where the @tailbits space begins
3461 * Make sure that the data buffers attached to a socket buffer are
3462 * writable. If they are not, private copies are made of the data buffers
3463 * and the socket buffer is set to use these instead.
3465 * If @tailbits is given, make sure that there is space to write @tailbits
3466 * bytes of data beyond current end of socket buffer. @trailer will be
3467 * set to point to the skb in which this space begins.
3469 * The number of scatterlist elements required to completely map the
3470 * COW'd and extended socket buffer will be returned.
3472 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3476 struct sk_buff *skb1, **skb_p;
3478 /* If skb is cloned or its head is paged, reallocate
3479 * head pulling out all the pages (pages are considered not writable
3480 * at the moment even if they are anonymous).
3482 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3483 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3486 /* Easy case. Most of packets will go this way. */
3487 if (!skb_has_frag_list(skb)) {
3488 /* A little of trouble, not enough of space for trailer.
3489 * This should not happen, when stack is tuned to generate
3490 * good frames. OK, on miss we reallocate and reserve even more
3491 * space, 128 bytes is fair. */
3493 if (skb_tailroom(skb) < tailbits &&
3494 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3502 /* Misery. We are in troubles, going to mincer fragments... */
3505 skb_p = &skb_shinfo(skb)->frag_list;
3508 while ((skb1 = *skb_p) != NULL) {
3511 /* The fragment is partially pulled by someone,
3512 * this can happen on input. Copy it and everything
3515 if (skb_shared(skb1))
3518 /* If the skb is the last, worry about trailer. */
3520 if (skb1->next == NULL && tailbits) {
3521 if (skb_shinfo(skb1)->nr_frags ||
3522 skb_has_frag_list(skb1) ||
3523 skb_tailroom(skb1) < tailbits)
3524 ntail = tailbits + 128;
3530 skb_shinfo(skb1)->nr_frags ||
3531 skb_has_frag_list(skb1)) {
3532 struct sk_buff *skb2;
3534 /* Fuck, we are miserable poor guys... */
3536 skb2 = skb_copy(skb1, GFP_ATOMIC);
3538 skb2 = skb_copy_expand(skb1,
3542 if (unlikely(skb2 == NULL))
3546 skb_set_owner_w(skb2, skb1->sk);
3548 /* Looking around. Are we still alive?
3549 * OK, link new skb, drop old one */
3551 skb2->next = skb1->next;
3558 skb_p = &skb1->next;
3563 EXPORT_SYMBOL_GPL(skb_cow_data);
3565 static void sock_rmem_free(struct sk_buff *skb)
3567 struct sock *sk = skb->sk;
3569 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3573 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3575 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3577 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3578 (unsigned int)sk->sk_rcvbuf)
3583 skb->destructor = sock_rmem_free;
3584 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3586 /* before exiting rcu section, make sure dst is refcounted */
3589 skb_queue_tail(&sk->sk_error_queue, skb);
3590 if (!sock_flag(sk, SOCK_DEAD))
3591 sk->sk_data_ready(sk);
3594 EXPORT_SYMBOL(sock_queue_err_skb);
3596 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3598 struct sk_buff_head *q = &sk->sk_error_queue;
3599 struct sk_buff *skb, *skb_next;
3600 unsigned long flags;
3603 spin_lock_irqsave(&q->lock, flags);
3604 skb = __skb_dequeue(q);
3605 if (skb && (skb_next = skb_peek(q)))
3606 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3607 spin_unlock_irqrestore(&q->lock, flags);
3611 sk->sk_error_report(sk);
3615 EXPORT_SYMBOL(sock_dequeue_err_skb);
3618 * skb_clone_sk - create clone of skb, and take reference to socket
3619 * @skb: the skb to clone
3621 * This function creates a clone of a buffer that holds a reference on
3622 * sk_refcnt. Buffers created via this function are meant to be
3623 * returned using sock_queue_err_skb, or free via kfree_skb.
3625 * When passing buffers allocated with this function to sock_queue_err_skb
3626 * it is necessary to wrap the call with sock_hold/sock_put in order to
3627 * prevent the socket from being released prior to being enqueued on
3628 * the sk_error_queue.
3630 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3632 struct sock *sk = skb->sk;
3633 struct sk_buff *clone;
3635 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3638 clone = skb_clone(skb, GFP_ATOMIC);
3645 clone->destructor = sock_efree;
3649 EXPORT_SYMBOL(skb_clone_sk);
3651 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3655 struct sock_exterr_skb *serr;
3658 serr = SKB_EXT_ERR(skb);
3659 memset(serr, 0, sizeof(*serr));
3660 serr->ee.ee_errno = ENOMSG;
3661 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3662 serr->ee.ee_info = tstype;
3663 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3664 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3665 if (sk->sk_protocol == IPPROTO_TCP)
3666 serr->ee.ee_data -= sk->sk_tskey;
3669 err = sock_queue_err_skb(sk, skb);
3675 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3679 if (likely(sysctl_tstamp_allow_data || tsonly))
3682 read_lock_bh(&sk->sk_callback_lock);
3683 ret = sk->sk_socket && sk->sk_socket->file &&
3684 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3685 read_unlock_bh(&sk->sk_callback_lock);
3689 void skb_complete_tx_timestamp(struct sk_buff *skb,
3690 struct skb_shared_hwtstamps *hwtstamps)
3692 struct sock *sk = skb->sk;
3694 if (!skb_may_tx_timestamp(sk, false))
3697 /* take a reference to prevent skb_orphan() from freeing the socket */
3700 *skb_hwtstamps(skb) = *hwtstamps;
3701 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3705 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3707 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3708 struct skb_shared_hwtstamps *hwtstamps,
3709 struct sock *sk, int tstype)
3711 struct sk_buff *skb;
3717 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3718 if (!skb_may_tx_timestamp(sk, tsonly))
3722 skb = alloc_skb(0, GFP_ATOMIC);
3724 skb = skb_clone(orig_skb, GFP_ATOMIC);
3729 skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3730 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3734 *skb_hwtstamps(skb) = *hwtstamps;
3736 skb->tstamp = ktime_get_real();
3738 __skb_complete_tx_timestamp(skb, sk, tstype);
3740 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3742 void skb_tstamp_tx(struct sk_buff *orig_skb,
3743 struct skb_shared_hwtstamps *hwtstamps)
3745 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3748 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3750 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3752 struct sock *sk = skb->sk;
3753 struct sock_exterr_skb *serr;
3756 skb->wifi_acked_valid = 1;
3757 skb->wifi_acked = acked;
3759 serr = SKB_EXT_ERR(skb);
3760 memset(serr, 0, sizeof(*serr));
3761 serr->ee.ee_errno = ENOMSG;
3762 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3764 /* take a reference to prevent skb_orphan() from freeing the socket */
3767 err = sock_queue_err_skb(sk, skb);
3773 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3776 * skb_partial_csum_set - set up and verify partial csum values for packet
3777 * @skb: the skb to set
3778 * @start: the number of bytes after skb->data to start checksumming.
3779 * @off: the offset from start to place the checksum.
3781 * For untrusted partially-checksummed packets, we need to make sure the values
3782 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3784 * This function checks and sets those values and skb->ip_summed: if this
3785 * returns false you should drop the packet.
3787 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3789 if (unlikely(start > skb_headlen(skb)) ||
3790 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3791 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3792 start, off, skb_headlen(skb));
3795 skb->ip_summed = CHECKSUM_PARTIAL;
3796 skb->csum_start = skb_headroom(skb) + start;
3797 skb->csum_offset = off;
3798 skb_set_transport_header(skb, start);
3801 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3803 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3806 if (skb_headlen(skb) >= len)
3809 /* If we need to pullup then pullup to the max, so we
3810 * won't need to do it again.
3815 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3818 if (skb_headlen(skb) < len)
3824 #define MAX_TCP_HDR_LEN (15 * 4)
3826 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3827 typeof(IPPROTO_IP) proto,
3834 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3835 off + MAX_TCP_HDR_LEN);
3836 if (!err && !skb_partial_csum_set(skb, off,
3837 offsetof(struct tcphdr,
3840 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3843 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3844 off + sizeof(struct udphdr));
3845 if (!err && !skb_partial_csum_set(skb, off,
3846 offsetof(struct udphdr,
3849 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3852 return ERR_PTR(-EPROTO);
3855 /* This value should be large enough to cover a tagged ethernet header plus
3856 * maximally sized IP and TCP or UDP headers.
3858 #define MAX_IP_HDR_LEN 128
3860 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3869 err = skb_maybe_pull_tail(skb,
3870 sizeof(struct iphdr),
3875 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3878 off = ip_hdrlen(skb);
3885 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3887 return PTR_ERR(csum);
3890 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3893 ip_hdr(skb)->protocol, 0);
3900 /* This value should be large enough to cover a tagged ethernet header plus
3901 * an IPv6 header, all options, and a maximal TCP or UDP header.
3903 #define MAX_IPV6_HDR_LEN 256
3905 #define OPT_HDR(type, skb, off) \
3906 (type *)(skb_network_header(skb) + (off))
3908 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3921 off = sizeof(struct ipv6hdr);
3923 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3927 nexthdr = ipv6_hdr(skb)->nexthdr;
3929 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3930 while (off <= len && !done) {
3932 case IPPROTO_DSTOPTS:
3933 case IPPROTO_HOPOPTS:
3934 case IPPROTO_ROUTING: {
3935 struct ipv6_opt_hdr *hp;
3937 err = skb_maybe_pull_tail(skb,
3939 sizeof(struct ipv6_opt_hdr),
3944 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3945 nexthdr = hp->nexthdr;
3946 off += ipv6_optlen(hp);
3950 struct ip_auth_hdr *hp;
3952 err = skb_maybe_pull_tail(skb,
3954 sizeof(struct ip_auth_hdr),
3959 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3960 nexthdr = hp->nexthdr;
3961 off += ipv6_authlen(hp);
3964 case IPPROTO_FRAGMENT: {
3965 struct frag_hdr *hp;
3967 err = skb_maybe_pull_tail(skb,
3969 sizeof(struct frag_hdr),
3974 hp = OPT_HDR(struct frag_hdr, skb, off);
3976 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3979 nexthdr = hp->nexthdr;
3980 off += sizeof(struct frag_hdr);
3991 if (!done || fragment)
3994 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3996 return PTR_ERR(csum);
3999 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4000 &ipv6_hdr(skb)->daddr,
4001 skb->len - off, nexthdr, 0);
4009 * skb_checksum_setup - set up partial checksum offset
4010 * @skb: the skb to set up
4011 * @recalculate: if true the pseudo-header checksum will be recalculated
4013 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4017 switch (skb->protocol) {
4018 case htons(ETH_P_IP):
4019 err = skb_checksum_setup_ipv4(skb, recalculate);
4022 case htons(ETH_P_IPV6):
4023 err = skb_checksum_setup_ipv6(skb, recalculate);
4033 EXPORT_SYMBOL(skb_checksum_setup);
4035 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4037 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4040 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4042 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4045 skb_release_head_state(skb);
4046 kmem_cache_free(skbuff_head_cache, skb);
4051 EXPORT_SYMBOL(kfree_skb_partial);
4054 * skb_try_coalesce - try to merge skb to prior one
4056 * @from: buffer to add
4057 * @fragstolen: pointer to boolean
4058 * @delta_truesize: how much more was allocated than was requested
4060 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4061 bool *fragstolen, int *delta_truesize)
4063 int i, delta, len = from->len;
4065 *fragstolen = false;
4070 if (len <= skb_tailroom(to)) {
4072 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4073 *delta_truesize = 0;
4077 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4080 if (skb_headlen(from) != 0) {
4082 unsigned int offset;
4084 if (skb_shinfo(to)->nr_frags +
4085 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4088 if (skb_head_is_locked(from))
4091 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4093 page = virt_to_head_page(from->head);
4094 offset = from->data - (unsigned char *)page_address(page);
4096 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4097 page, offset, skb_headlen(from));
4100 if (skb_shinfo(to)->nr_frags +
4101 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4104 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4107 WARN_ON_ONCE(delta < len);
4109 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4110 skb_shinfo(from)->frags,
4111 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4112 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4114 if (!skb_cloned(from))
4115 skb_shinfo(from)->nr_frags = 0;
4117 /* if the skb is not cloned this does nothing
4118 * since we set nr_frags to 0.
4120 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4121 skb_frag_ref(from, i);
4123 to->truesize += delta;
4125 to->data_len += len;
4127 *delta_truesize = delta;
4130 EXPORT_SYMBOL(skb_try_coalesce);
4133 * skb_scrub_packet - scrub an skb
4135 * @skb: buffer to clean
4136 * @xnet: packet is crossing netns
4138 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4139 * into/from a tunnel. Some information have to be cleared during these
4141 * skb_scrub_packet can also be used to clean a skb before injecting it in
4142 * another namespace (@xnet == true). We have to clear all information in the
4143 * skb that could impact namespace isolation.
4145 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4147 skb->tstamp.tv64 = 0;
4148 skb->pkt_type = PACKET_HOST;
4152 skb_sender_cpu_clear(skb);
4155 nf_reset_trace(skb);
4163 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4166 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4170 * skb_gso_transport_seglen is used to determine the real size of the
4171 * individual segments, including Layer4 headers (TCP/UDP).
4173 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4175 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4177 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4178 unsigned int thlen = 0;
4180 if (skb->encapsulation) {
4181 thlen = skb_inner_transport_header(skb) -
4182 skb_transport_header(skb);
4184 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4185 thlen += inner_tcp_hdrlen(skb);
4186 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4187 thlen = tcp_hdrlen(skb);
4189 /* UFO sets gso_size to the size of the fragmentation
4190 * payload, i.e. the size of the L4 (UDP) header is already
4193 return thlen + shinfo->gso_size;
4195 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4197 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4199 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4204 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4205 skb->mac_header += VLAN_HLEN;
4209 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4211 struct vlan_hdr *vhdr;
4214 if (unlikely(skb_vlan_tag_present(skb))) {
4215 /* vlan_tci is already set-up so leave this for another time */
4219 skb = skb_share_check(skb, GFP_ATOMIC);
4223 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4226 vhdr = (struct vlan_hdr *)skb->data;
4227 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4228 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4230 skb_pull_rcsum(skb, VLAN_HLEN);
4231 vlan_set_encap_proto(skb, vhdr);
4233 skb = skb_reorder_vlan_header(skb);
4237 skb_reset_network_header(skb);
4238 skb_reset_transport_header(skb);
4239 skb_reset_mac_len(skb);
4247 EXPORT_SYMBOL(skb_vlan_untag);
4249 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4251 if (!pskb_may_pull(skb, write_len))
4254 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4257 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4259 EXPORT_SYMBOL(skb_ensure_writable);
4261 /* remove VLAN header from packet and update csum accordingly. */
4262 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4264 struct vlan_hdr *vhdr;
4265 unsigned int offset = skb->data - skb_mac_header(skb);
4268 __skb_push(skb, offset);
4269 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4273 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4275 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4276 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4278 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4279 __skb_pull(skb, VLAN_HLEN);
4281 vlan_set_encap_proto(skb, vhdr);
4282 skb->mac_header += VLAN_HLEN;
4284 if (skb_network_offset(skb) < ETH_HLEN)
4285 skb_set_network_header(skb, ETH_HLEN);
4287 skb_reset_mac_len(skb);
4289 __skb_pull(skb, offset);
4294 int skb_vlan_pop(struct sk_buff *skb)
4300 if (likely(skb_vlan_tag_present(skb))) {
4303 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4304 skb->protocol != htons(ETH_P_8021AD)) ||
4305 skb->len < VLAN_ETH_HLEN))
4308 err = __skb_vlan_pop(skb, &vlan_tci);
4312 /* move next vlan tag to hw accel tag */
4313 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4314 skb->protocol != htons(ETH_P_8021AD)) ||
4315 skb->len < VLAN_ETH_HLEN))
4318 vlan_proto = skb->protocol;
4319 err = __skb_vlan_pop(skb, &vlan_tci);
4323 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4326 EXPORT_SYMBOL(skb_vlan_pop);
4328 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4330 if (skb_vlan_tag_present(skb)) {
4331 unsigned int offset = skb->data - skb_mac_header(skb);
4334 /* __vlan_insert_tag expect skb->data pointing to mac header.
4335 * So change skb->data before calling it and change back to
4336 * original position later
4338 __skb_push(skb, offset);
4339 err = __vlan_insert_tag(skb, skb->vlan_proto,
4340 skb_vlan_tag_get(skb));
4343 skb->protocol = skb->vlan_proto;
4344 skb->mac_len += VLAN_HLEN;
4345 __skb_pull(skb, offset);
4347 if (skb->ip_summed == CHECKSUM_COMPLETE)
4348 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4349 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4351 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4354 EXPORT_SYMBOL(skb_vlan_push);
4357 * alloc_skb_with_frags - allocate skb with page frags
4359 * @header_len: size of linear part
4360 * @data_len: needed length in frags
4361 * @max_page_order: max page order desired.
4362 * @errcode: pointer to error code if any
4363 * @gfp_mask: allocation mask
4365 * This can be used to allocate a paged skb, given a maximal order for frags.
4367 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4368 unsigned long data_len,
4373 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4374 unsigned long chunk;
4375 struct sk_buff *skb;
4380 *errcode = -EMSGSIZE;
4381 /* Note this test could be relaxed, if we succeed to allocate
4382 * high order pages...
4384 if (npages > MAX_SKB_FRAGS)
4387 gfp_head = gfp_mask;
4388 if (gfp_head & __GFP_WAIT)
4389 gfp_head |= __GFP_REPEAT;
4391 *errcode = -ENOBUFS;
4392 skb = alloc_skb(header_len, gfp_head);
4396 skb->truesize += npages << PAGE_SHIFT;
4398 for (i = 0; npages > 0; i++) {
4399 int order = max_page_order;
4402 if (npages >= 1 << order) {
4403 page = alloc_pages((gfp_mask & ~__GFP_WAIT) |
4410 /* Do not retry other high order allocations */
4416 page = alloc_page(gfp_mask);
4420 chunk = min_t(unsigned long, data_len,
4421 PAGE_SIZE << order);
4422 skb_fill_page_desc(skb, i, page, 0, chunk);
4424 npages -= 1 << order;
4432 EXPORT_SYMBOL(alloc_skb_with_frags);