2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
78 struct kmem_cache *skbuff_head_cache __read_mostly;
79 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 * skb_panic - private function for out-of-line support
86 * @msg: skb_over_panic or skb_under_panic
88 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
93 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
96 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
97 msg, addr, skb->len, sz, skb->head, skb->data,
98 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
103 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
105 skb_panic(skb, sz, addr, __func__);
108 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
120 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
123 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
124 unsigned long ip, bool *pfmemalloc)
127 bool ret_pfmemalloc = false;
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
133 obj = kmalloc_node_track_caller(size,
134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
136 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc = true;
141 obj = kmalloc_node_track_caller(size, flags, node);
145 *pfmemalloc = ret_pfmemalloc;
150 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
156 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
161 skb = kmem_cache_alloc_node(skbuff_head_cache,
162 gfp_mask & ~__GFP_DMA, node);
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
171 memset(skb, 0, offsetof(struct sk_buff, tail));
173 skb->truesize = sizeof(struct sk_buff);
174 atomic_set(&skb->users, 1);
176 skb->mac_header = (typeof(skb->mac_header))~0U;
182 * __alloc_skb - allocate a network buffer
183 * @size: size to allocate
184 * @gfp_mask: allocation mask
185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
189 * @node: numa node to allocate memory on
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
201 struct kmem_cache *cache;
202 struct skb_shared_info *shinfo;
207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
224 size = SKB_DATA_ALIGN(size);
225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
233 size = SKB_WITH_OVERHEAD(ksize(data));
234 prefetchw(data + size);
237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
241 memset(skb, 0, offsetof(struct sk_buff, tail));
242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
244 skb->pfmemalloc = pfmemalloc;
245 atomic_set(&skb->users, 1);
248 skb_reset_tail_pointer(skb);
249 skb->end = skb->tail + size;
250 skb->mac_header = (typeof(skb->mac_header))~0U;
251 skb->transport_header = (typeof(skb->transport_header))~0U;
253 /* make sure we initialize shinfo sequentially */
254 shinfo = skb_shinfo(skb);
255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
256 atomic_set(&shinfo->dataref, 1);
257 kmemcheck_annotate_variable(shinfo->destructor_arg);
259 if (flags & SKB_ALLOC_FCLONE) {
260 struct sk_buff_fclones *fclones;
262 fclones = container_of(skb, struct sk_buff_fclones, skb1);
264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
265 skb->fclone = SKB_FCLONE_ORIG;
266 atomic_set(&fclones->fclone_ref, 1);
268 fclones->skb2.fclone = SKB_FCLONE_CLONE;
269 fclones->skb2.pfmemalloc = pfmemalloc;
274 kmem_cache_free(cache, skb);
278 EXPORT_SYMBOL(__alloc_skb);
281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
283 * @frag_size: size of fragment, or 0 if head was kmalloced
285 * Allocate a new &sk_buff. Caller provides space holding head and
286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
298 struct sk_buff *build_skb(void *data, unsigned int frag_size)
300 struct skb_shared_info *shinfo;
302 unsigned int size = frag_size ? : ksize(data);
304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 skb->truesize = SKB_TRUESIZE(size);
312 skb->head_frag = frag_size != 0;
313 atomic_set(&skb->users, 1);
316 skb_reset_tail_pointer(skb);
317 skb->end = skb->tail + size;
318 skb->mac_header = (typeof(skb->mac_header))~0U;
319 skb->transport_header = (typeof(skb->transport_header))~0U;
321 /* make sure we initialize shinfo sequentially */
322 shinfo = skb_shinfo(skb);
323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324 atomic_set(&shinfo->dataref, 1);
325 kmemcheck_annotate_variable(shinfo->destructor_arg);
329 EXPORT_SYMBOL(build_skb);
331 struct netdev_alloc_cache {
332 struct page_frag frag;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
336 unsigned int pagecnt_bias;
338 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
339 static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
341 static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
344 const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
345 struct page *page = NULL;
346 gfp_t gfp = gfp_mask;
349 gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
350 page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
351 nc->frag.size = PAGE_SIZE << (page ? order : 0);
355 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
357 nc->frag.page = page;
362 static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
363 unsigned int fragsz, gfp_t gfp_mask)
365 struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
366 struct page *page = nc->frag.page;
370 if (unlikely(!page)) {
372 page = __page_frag_refill(nc, gfp_mask);
376 /* if size can vary use frag.size else just use PAGE_SIZE */
377 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
379 /* Even if we own the page, we do not use atomic_set().
380 * This would break get_page_unless_zero() users.
382 atomic_add(size - 1, &page->_count);
384 /* reset page count bias and offset to start of new frag */
385 nc->pagecnt_bias = size;
386 nc->frag.offset = size;
389 offset = nc->frag.offset - fragsz;
390 if (unlikely(offset < 0)) {
391 if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
394 /* if size can vary use frag.size else just use PAGE_SIZE */
395 size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
397 /* OK, page count is 0, we can safely set it */
398 atomic_set(&page->_count, size);
400 /* reset page count bias and offset to start of new frag */
401 nc->pagecnt_bias = size;
402 offset = size - fragsz;
406 nc->frag.offset = offset;
408 return page_address(page) + offset;
411 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
416 local_irq_save(flags);
417 data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
418 local_irq_restore(flags);
423 * netdev_alloc_frag - allocate a page fragment
424 * @fragsz: fragment size
426 * Allocates a frag from a page for receive buffer.
427 * Uses GFP_ATOMIC allocations.
429 void *netdev_alloc_frag(unsigned int fragsz)
431 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
433 EXPORT_SYMBOL(netdev_alloc_frag);
435 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
437 return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
440 void *napi_alloc_frag(unsigned int fragsz)
442 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
444 EXPORT_SYMBOL(napi_alloc_frag);
447 * __alloc_rx_skb - allocate an skbuff for rx
448 * @length: length to allocate
449 * @gfp_mask: get_free_pages mask, passed to alloc_skb
450 * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
451 * allocations in case we have to fallback to __alloc_skb()
452 * If SKB_ALLOC_NAPI is set, page fragment will be allocated
453 * from napi_cache instead of netdev_cache.
455 * Allocate a new &sk_buff and assign it a usage count of one. The
456 * buffer has unspecified headroom built in. Users should allocate
457 * the headroom they think they need without accounting for the
458 * built in space. The built in space is used for optimisations.
460 * %NULL is returned if there is no free memory.
462 static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
465 struct sk_buff *skb = NULL;
466 unsigned int fragsz = SKB_DATA_ALIGN(length) +
467 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
469 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
472 if (sk_memalloc_socks())
473 gfp_mask |= __GFP_MEMALLOC;
475 data = (flags & SKB_ALLOC_NAPI) ?
476 __napi_alloc_frag(fragsz, gfp_mask) :
477 __netdev_alloc_frag(fragsz, gfp_mask);
480 skb = build_skb(data, fragsz);
482 put_page(virt_to_head_page(data));
485 skb = __alloc_skb(length, gfp_mask,
486 SKB_ALLOC_RX, NUMA_NO_NODE);
492 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
493 * @dev: network device to receive on
494 * @length: length to allocate
495 * @gfp_mask: get_free_pages mask, passed to alloc_skb
497 * Allocate a new &sk_buff and assign it a usage count of one. The
498 * buffer has NET_SKB_PAD headroom built in. Users should allocate
499 * the headroom they think they need without accounting for the
500 * built in space. The built in space is used for optimisations.
502 * %NULL is returned if there is no free memory.
504 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
505 unsigned int length, gfp_t gfp_mask)
509 length += NET_SKB_PAD;
510 skb = __alloc_rx_skb(length, gfp_mask, 0);
513 skb_reserve(skb, NET_SKB_PAD);
519 EXPORT_SYMBOL(__netdev_alloc_skb);
522 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
523 * @napi: napi instance this buffer was allocated for
524 * @length: length to allocate
525 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
527 * Allocate a new sk_buff for use in NAPI receive. This buffer will
528 * attempt to allocate the head from a special reserved region used
529 * only for NAPI Rx allocation. By doing this we can save several
530 * CPU cycles by avoiding having to disable and re-enable IRQs.
532 * %NULL is returned if there is no free memory.
534 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
535 unsigned int length, gfp_t gfp_mask)
539 length += NET_SKB_PAD + NET_IP_ALIGN;
540 skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
543 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
544 skb->dev = napi->dev;
549 EXPORT_SYMBOL(__napi_alloc_skb);
551 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
552 int size, unsigned int truesize)
554 skb_fill_page_desc(skb, i, page, off, size);
556 skb->data_len += size;
557 skb->truesize += truesize;
559 EXPORT_SYMBOL(skb_add_rx_frag);
561 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
562 unsigned int truesize)
564 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
566 skb_frag_size_add(frag, size);
568 skb->data_len += size;
569 skb->truesize += truesize;
571 EXPORT_SYMBOL(skb_coalesce_rx_frag);
573 static void skb_drop_list(struct sk_buff **listp)
575 kfree_skb_list(*listp);
579 static inline void skb_drop_fraglist(struct sk_buff *skb)
581 skb_drop_list(&skb_shinfo(skb)->frag_list);
584 static void skb_clone_fraglist(struct sk_buff *skb)
586 struct sk_buff *list;
588 skb_walk_frags(skb, list)
592 static void skb_free_head(struct sk_buff *skb)
595 put_page(virt_to_head_page(skb->head));
600 static void skb_release_data(struct sk_buff *skb)
602 struct skb_shared_info *shinfo = skb_shinfo(skb);
606 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
610 for (i = 0; i < shinfo->nr_frags; i++)
611 __skb_frag_unref(&shinfo->frags[i]);
614 * If skb buf is from userspace, we need to notify the caller
615 * the lower device DMA has done;
617 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
618 struct ubuf_info *uarg;
620 uarg = shinfo->destructor_arg;
622 uarg->callback(uarg, true);
625 if (shinfo->frag_list)
626 kfree_skb_list(shinfo->frag_list);
632 * Free an skbuff by memory without cleaning the state.
634 static void kfree_skbmem(struct sk_buff *skb)
636 struct sk_buff_fclones *fclones;
638 switch (skb->fclone) {
639 case SKB_FCLONE_UNAVAILABLE:
640 kmem_cache_free(skbuff_head_cache, skb);
643 case SKB_FCLONE_ORIG:
644 fclones = container_of(skb, struct sk_buff_fclones, skb1);
646 /* We usually free the clone (TX completion) before original skb
647 * This test would have no chance to be true for the clone,
648 * while here, branch prediction will be good.
650 if (atomic_read(&fclones->fclone_ref) == 1)
654 default: /* SKB_FCLONE_CLONE */
655 fclones = container_of(skb, struct sk_buff_fclones, skb2);
658 if (!atomic_dec_and_test(&fclones->fclone_ref))
661 kmem_cache_free(skbuff_fclone_cache, fclones);
664 static void skb_release_head_state(struct sk_buff *skb)
668 secpath_put(skb->sp);
670 if (skb->destructor) {
672 skb->destructor(skb);
674 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
675 nf_conntrack_put(skb->nfct);
677 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
678 nf_bridge_put(skb->nf_bridge);
682 /* Free everything but the sk_buff shell. */
683 static void skb_release_all(struct sk_buff *skb)
685 skb_release_head_state(skb);
686 if (likely(skb->head))
687 skb_release_data(skb);
691 * __kfree_skb - private function
694 * Free an sk_buff. Release anything attached to the buffer.
695 * Clean the state. This is an internal helper function. Users should
696 * always call kfree_skb
699 void __kfree_skb(struct sk_buff *skb)
701 skb_release_all(skb);
704 EXPORT_SYMBOL(__kfree_skb);
707 * kfree_skb - free an sk_buff
708 * @skb: buffer to free
710 * Drop a reference to the buffer and free it if the usage count has
713 void kfree_skb(struct sk_buff *skb)
717 if (likely(atomic_read(&skb->users) == 1))
719 else if (likely(!atomic_dec_and_test(&skb->users)))
721 trace_kfree_skb(skb, __builtin_return_address(0));
724 EXPORT_SYMBOL(kfree_skb);
726 void kfree_skb_list(struct sk_buff *segs)
729 struct sk_buff *next = segs->next;
735 EXPORT_SYMBOL(kfree_skb_list);
738 * skb_tx_error - report an sk_buff xmit error
739 * @skb: buffer that triggered an error
741 * Report xmit error if a device callback is tracking this skb.
742 * skb must be freed afterwards.
744 void skb_tx_error(struct sk_buff *skb)
746 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
747 struct ubuf_info *uarg;
749 uarg = skb_shinfo(skb)->destructor_arg;
751 uarg->callback(uarg, false);
752 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
755 EXPORT_SYMBOL(skb_tx_error);
758 * consume_skb - free an skbuff
759 * @skb: buffer to free
761 * Drop a ref to the buffer and free it if the usage count has hit zero
762 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
763 * is being dropped after a failure and notes that
765 void consume_skb(struct sk_buff *skb)
769 if (likely(atomic_read(&skb->users) == 1))
771 else if (likely(!atomic_dec_and_test(&skb->users)))
773 trace_consume_skb(skb);
776 EXPORT_SYMBOL(consume_skb);
778 /* Make sure a field is enclosed inside headers_start/headers_end section */
779 #define CHECK_SKB_FIELD(field) \
780 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
781 offsetof(struct sk_buff, headers_start)); \
782 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
783 offsetof(struct sk_buff, headers_end)); \
785 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
787 new->tstamp = old->tstamp;
788 /* We do not copy old->sk */
790 memcpy(new->cb, old->cb, sizeof(old->cb));
791 skb_dst_copy(new, old);
793 new->sp = secpath_get(old->sp);
795 __nf_copy(new, old, false);
797 /* Note : this field could be in headers_start/headers_end section
798 * It is not yet because we do not want to have a 16 bit hole
800 new->queue_mapping = old->queue_mapping;
802 memcpy(&new->headers_start, &old->headers_start,
803 offsetof(struct sk_buff, headers_end) -
804 offsetof(struct sk_buff, headers_start));
805 CHECK_SKB_FIELD(protocol);
806 CHECK_SKB_FIELD(csum);
807 CHECK_SKB_FIELD(hash);
808 CHECK_SKB_FIELD(priority);
809 CHECK_SKB_FIELD(skb_iif);
810 CHECK_SKB_FIELD(vlan_proto);
811 CHECK_SKB_FIELD(vlan_tci);
812 CHECK_SKB_FIELD(transport_header);
813 CHECK_SKB_FIELD(network_header);
814 CHECK_SKB_FIELD(mac_header);
815 CHECK_SKB_FIELD(inner_protocol);
816 CHECK_SKB_FIELD(inner_transport_header);
817 CHECK_SKB_FIELD(inner_network_header);
818 CHECK_SKB_FIELD(inner_mac_header);
819 CHECK_SKB_FIELD(mark);
820 #ifdef CONFIG_NETWORK_SECMARK
821 CHECK_SKB_FIELD(secmark);
823 #ifdef CONFIG_NET_RX_BUSY_POLL
824 CHECK_SKB_FIELD(napi_id);
826 #ifdef CONFIG_NET_SCHED
827 CHECK_SKB_FIELD(tc_index);
828 #ifdef CONFIG_NET_CLS_ACT
829 CHECK_SKB_FIELD(tc_verd);
836 * You should not add any new code to this function. Add it to
837 * __copy_skb_header above instead.
839 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
841 #define C(x) n->x = skb->x
843 n->next = n->prev = NULL;
845 __copy_skb_header(n, skb);
850 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
853 n->destructor = NULL;
860 atomic_set(&n->users, 1);
862 atomic_inc(&(skb_shinfo(skb)->dataref));
870 * skb_morph - morph one skb into another
871 * @dst: the skb to receive the contents
872 * @src: the skb to supply the contents
874 * This is identical to skb_clone except that the target skb is
875 * supplied by the user.
877 * The target skb is returned upon exit.
879 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
881 skb_release_all(dst);
882 return __skb_clone(dst, src);
884 EXPORT_SYMBOL_GPL(skb_morph);
887 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
888 * @skb: the skb to modify
889 * @gfp_mask: allocation priority
891 * This must be called on SKBTX_DEV_ZEROCOPY skb.
892 * It will copy all frags into kernel and drop the reference
893 * to userspace pages.
895 * If this function is called from an interrupt gfp_mask() must be
898 * Returns 0 on success or a negative error code on failure
899 * to allocate kernel memory to copy to.
901 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
904 int num_frags = skb_shinfo(skb)->nr_frags;
905 struct page *page, *head = NULL;
906 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
908 for (i = 0; i < num_frags; i++) {
910 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
912 page = alloc_page(gfp_mask);
915 struct page *next = (struct page *)page_private(head);
921 vaddr = kmap_atomic(skb_frag_page(f));
922 memcpy(page_address(page),
923 vaddr + f->page_offset, skb_frag_size(f));
924 kunmap_atomic(vaddr);
925 set_page_private(page, (unsigned long)head);
929 /* skb frags release userspace buffers */
930 for (i = 0; i < num_frags; i++)
931 skb_frag_unref(skb, i);
933 uarg->callback(uarg, false);
935 /* skb frags point to kernel buffers */
936 for (i = num_frags - 1; i >= 0; i--) {
937 __skb_fill_page_desc(skb, i, head, 0,
938 skb_shinfo(skb)->frags[i].size);
939 head = (struct page *)page_private(head);
942 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
945 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
948 * skb_clone - duplicate an sk_buff
949 * @skb: buffer to clone
950 * @gfp_mask: allocation priority
952 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
953 * copies share the same packet data but not structure. The new
954 * buffer has a reference count of 1. If the allocation fails the
955 * function returns %NULL otherwise the new buffer is returned.
957 * If this function is called from an interrupt gfp_mask() must be
961 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
963 struct sk_buff_fclones *fclones = container_of(skb,
964 struct sk_buff_fclones,
968 if (skb_orphan_frags(skb, gfp_mask))
971 if (skb->fclone == SKB_FCLONE_ORIG &&
972 atomic_read(&fclones->fclone_ref) == 1) {
974 atomic_set(&fclones->fclone_ref, 2);
976 if (skb_pfmemalloc(skb))
977 gfp_mask |= __GFP_MEMALLOC;
979 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
983 kmemcheck_annotate_bitfield(n, flags1);
984 n->fclone = SKB_FCLONE_UNAVAILABLE;
987 return __skb_clone(n, skb);
989 EXPORT_SYMBOL(skb_clone);
991 static void skb_headers_offset_update(struct sk_buff *skb, int off)
993 /* Only adjust this if it actually is csum_start rather than csum */
994 if (skb->ip_summed == CHECKSUM_PARTIAL)
995 skb->csum_start += off;
996 /* {transport,network,mac}_header and tail are relative to skb->head */
997 skb->transport_header += off;
998 skb->network_header += off;
999 if (skb_mac_header_was_set(skb))
1000 skb->mac_header += off;
1001 skb->inner_transport_header += off;
1002 skb->inner_network_header += off;
1003 skb->inner_mac_header += off;
1006 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1008 __copy_skb_header(new, old);
1010 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1011 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1012 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1015 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1017 if (skb_pfmemalloc(skb))
1018 return SKB_ALLOC_RX;
1023 * skb_copy - create private copy of an sk_buff
1024 * @skb: buffer to copy
1025 * @gfp_mask: allocation priority
1027 * Make a copy of both an &sk_buff and its data. This is used when the
1028 * caller wishes to modify the data and needs a private copy of the
1029 * data to alter. Returns %NULL on failure or the pointer to the buffer
1030 * on success. The returned buffer has a reference count of 1.
1032 * As by-product this function converts non-linear &sk_buff to linear
1033 * one, so that &sk_buff becomes completely private and caller is allowed
1034 * to modify all the data of returned buffer. This means that this
1035 * function is not recommended for use in circumstances when only
1036 * header is going to be modified. Use pskb_copy() instead.
1039 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1041 int headerlen = skb_headroom(skb);
1042 unsigned int size = skb_end_offset(skb) + skb->data_len;
1043 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1044 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1049 /* Set the data pointer */
1050 skb_reserve(n, headerlen);
1051 /* Set the tail pointer and length */
1052 skb_put(n, skb->len);
1054 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1057 copy_skb_header(n, skb);
1060 EXPORT_SYMBOL(skb_copy);
1063 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1064 * @skb: buffer to copy
1065 * @headroom: headroom of new skb
1066 * @gfp_mask: allocation priority
1067 * @fclone: if true allocate the copy of the skb from the fclone
1068 * cache instead of the head cache; it is recommended to set this
1069 * to true for the cases where the copy will likely be cloned
1071 * Make a copy of both an &sk_buff and part of its data, located
1072 * in header. Fragmented data remain shared. This is used when
1073 * the caller wishes to modify only header of &sk_buff and needs
1074 * private copy of the header to alter. Returns %NULL on failure
1075 * or the pointer to the buffer on success.
1076 * The returned buffer has a reference count of 1.
1079 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1080 gfp_t gfp_mask, bool fclone)
1082 unsigned int size = skb_headlen(skb) + headroom;
1083 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1084 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1089 /* Set the data pointer */
1090 skb_reserve(n, headroom);
1091 /* Set the tail pointer and length */
1092 skb_put(n, skb_headlen(skb));
1093 /* Copy the bytes */
1094 skb_copy_from_linear_data(skb, n->data, n->len);
1096 n->truesize += skb->data_len;
1097 n->data_len = skb->data_len;
1100 if (skb_shinfo(skb)->nr_frags) {
1103 if (skb_orphan_frags(skb, gfp_mask)) {
1108 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1109 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1110 skb_frag_ref(skb, i);
1112 skb_shinfo(n)->nr_frags = i;
1115 if (skb_has_frag_list(skb)) {
1116 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1117 skb_clone_fraglist(n);
1120 copy_skb_header(n, skb);
1124 EXPORT_SYMBOL(__pskb_copy_fclone);
1127 * pskb_expand_head - reallocate header of &sk_buff
1128 * @skb: buffer to reallocate
1129 * @nhead: room to add at head
1130 * @ntail: room to add at tail
1131 * @gfp_mask: allocation priority
1133 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1134 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1135 * reference count of 1. Returns zero in the case of success or error,
1136 * if expansion failed. In the last case, &sk_buff is not changed.
1138 * All the pointers pointing into skb header may change and must be
1139 * reloaded after call to this function.
1142 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1147 int size = nhead + skb_end_offset(skb) + ntail;
1152 if (skb_shared(skb))
1155 size = SKB_DATA_ALIGN(size);
1157 if (skb_pfmemalloc(skb))
1158 gfp_mask |= __GFP_MEMALLOC;
1159 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1160 gfp_mask, NUMA_NO_NODE, NULL);
1163 size = SKB_WITH_OVERHEAD(ksize(data));
1165 /* Copy only real data... and, alas, header. This should be
1166 * optimized for the cases when header is void.
1168 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1170 memcpy((struct skb_shared_info *)(data + size),
1172 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1175 * if shinfo is shared we must drop the old head gracefully, but if it
1176 * is not we can just drop the old head and let the existing refcount
1177 * be since all we did is relocate the values
1179 if (skb_cloned(skb)) {
1180 /* copy this zero copy skb frags */
1181 if (skb_orphan_frags(skb, gfp_mask))
1183 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1184 skb_frag_ref(skb, i);
1186 if (skb_has_frag_list(skb))
1187 skb_clone_fraglist(skb);
1189 skb_release_data(skb);
1193 off = (data + nhead) - skb->head;
1198 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1202 skb->end = skb->head + size;
1205 skb_headers_offset_update(skb, nhead);
1209 atomic_set(&skb_shinfo(skb)->dataref, 1);
1217 EXPORT_SYMBOL(pskb_expand_head);
1219 /* Make private copy of skb with writable head and some headroom */
1221 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1223 struct sk_buff *skb2;
1224 int delta = headroom - skb_headroom(skb);
1227 skb2 = pskb_copy(skb, GFP_ATOMIC);
1229 skb2 = skb_clone(skb, GFP_ATOMIC);
1230 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1238 EXPORT_SYMBOL(skb_realloc_headroom);
1241 * skb_copy_expand - copy and expand sk_buff
1242 * @skb: buffer to copy
1243 * @newheadroom: new free bytes at head
1244 * @newtailroom: new free bytes at tail
1245 * @gfp_mask: allocation priority
1247 * Make a copy of both an &sk_buff and its data and while doing so
1248 * allocate additional space.
1250 * This is used when the caller wishes to modify the data and needs a
1251 * private copy of the data to alter as well as more space for new fields.
1252 * Returns %NULL on failure or the pointer to the buffer
1253 * on success. The returned buffer has a reference count of 1.
1255 * You must pass %GFP_ATOMIC as the allocation priority if this function
1256 * is called from an interrupt.
1258 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1259 int newheadroom, int newtailroom,
1263 * Allocate the copy buffer
1265 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1266 gfp_mask, skb_alloc_rx_flag(skb),
1268 int oldheadroom = skb_headroom(skb);
1269 int head_copy_len, head_copy_off;
1274 skb_reserve(n, newheadroom);
1276 /* Set the tail pointer and length */
1277 skb_put(n, skb->len);
1279 head_copy_len = oldheadroom;
1281 if (newheadroom <= head_copy_len)
1282 head_copy_len = newheadroom;
1284 head_copy_off = newheadroom - head_copy_len;
1286 /* Copy the linear header and data. */
1287 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1288 skb->len + head_copy_len))
1291 copy_skb_header(n, skb);
1293 skb_headers_offset_update(n, newheadroom - oldheadroom);
1297 EXPORT_SYMBOL(skb_copy_expand);
1300 * skb_pad - zero pad the tail of an skb
1301 * @skb: buffer to pad
1302 * @pad: space to pad
1304 * Ensure that a buffer is followed by a padding area that is zero
1305 * filled. Used by network drivers which may DMA or transfer data
1306 * beyond the buffer end onto the wire.
1308 * May return error in out of memory cases. The skb is freed on error.
1311 int skb_pad(struct sk_buff *skb, int pad)
1316 /* If the skbuff is non linear tailroom is always zero.. */
1317 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1318 memset(skb->data+skb->len, 0, pad);
1322 ntail = skb->data_len + pad - (skb->end - skb->tail);
1323 if (likely(skb_cloned(skb) || ntail > 0)) {
1324 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1329 /* FIXME: The use of this function with non-linear skb's really needs
1332 err = skb_linearize(skb);
1336 memset(skb->data + skb->len, 0, pad);
1343 EXPORT_SYMBOL(skb_pad);
1346 * pskb_put - add data to the tail of a potentially fragmented buffer
1347 * @skb: start of the buffer to use
1348 * @tail: tail fragment of the buffer to use
1349 * @len: amount of data to add
1351 * This function extends the used data area of the potentially
1352 * fragmented buffer. @tail must be the last fragment of @skb -- or
1353 * @skb itself. If this would exceed the total buffer size the kernel
1354 * will panic. A pointer to the first byte of the extra data is
1358 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1361 skb->data_len += len;
1364 return skb_put(tail, len);
1366 EXPORT_SYMBOL_GPL(pskb_put);
1369 * skb_put - add data to a buffer
1370 * @skb: buffer to use
1371 * @len: amount of data to add
1373 * This function extends the used data area of the buffer. If this would
1374 * exceed the total buffer size the kernel will panic. A pointer to the
1375 * first byte of the extra data is returned.
1377 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1379 unsigned char *tmp = skb_tail_pointer(skb);
1380 SKB_LINEAR_ASSERT(skb);
1383 if (unlikely(skb->tail > skb->end))
1384 skb_over_panic(skb, len, __builtin_return_address(0));
1387 EXPORT_SYMBOL(skb_put);
1390 * skb_push - add data to the start of a buffer
1391 * @skb: buffer to use
1392 * @len: amount of data to add
1394 * This function extends the used data area of the buffer at the buffer
1395 * start. If this would exceed the total buffer headroom the kernel will
1396 * panic. A pointer to the first byte of the extra data is returned.
1398 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1402 if (unlikely(skb->data<skb->head))
1403 skb_under_panic(skb, len, __builtin_return_address(0));
1406 EXPORT_SYMBOL(skb_push);
1409 * skb_pull - remove data from the start of a buffer
1410 * @skb: buffer to use
1411 * @len: amount of data to remove
1413 * This function removes data from the start of a buffer, returning
1414 * the memory to the headroom. A pointer to the next data in the buffer
1415 * is returned. Once the data has been pulled future pushes will overwrite
1418 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1420 return skb_pull_inline(skb, len);
1422 EXPORT_SYMBOL(skb_pull);
1425 * skb_trim - remove end from a buffer
1426 * @skb: buffer to alter
1429 * Cut the length of a buffer down by removing data from the tail. If
1430 * the buffer is already under the length specified it is not modified.
1431 * The skb must be linear.
1433 void skb_trim(struct sk_buff *skb, unsigned int len)
1436 __skb_trim(skb, len);
1438 EXPORT_SYMBOL(skb_trim);
1440 /* Trims skb to length len. It can change skb pointers.
1443 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1445 struct sk_buff **fragp;
1446 struct sk_buff *frag;
1447 int offset = skb_headlen(skb);
1448 int nfrags = skb_shinfo(skb)->nr_frags;
1452 if (skb_cloned(skb) &&
1453 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1460 for (; i < nfrags; i++) {
1461 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1468 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1471 skb_shinfo(skb)->nr_frags = i;
1473 for (; i < nfrags; i++)
1474 skb_frag_unref(skb, i);
1476 if (skb_has_frag_list(skb))
1477 skb_drop_fraglist(skb);
1481 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1482 fragp = &frag->next) {
1483 int end = offset + frag->len;
1485 if (skb_shared(frag)) {
1486 struct sk_buff *nfrag;
1488 nfrag = skb_clone(frag, GFP_ATOMIC);
1489 if (unlikely(!nfrag))
1492 nfrag->next = frag->next;
1504 unlikely((err = pskb_trim(frag, len - offset))))
1508 skb_drop_list(&frag->next);
1513 if (len > skb_headlen(skb)) {
1514 skb->data_len -= skb->len - len;
1519 skb_set_tail_pointer(skb, len);
1524 EXPORT_SYMBOL(___pskb_trim);
1527 * __pskb_pull_tail - advance tail of skb header
1528 * @skb: buffer to reallocate
1529 * @delta: number of bytes to advance tail
1531 * The function makes a sense only on a fragmented &sk_buff,
1532 * it expands header moving its tail forward and copying necessary
1533 * data from fragmented part.
1535 * &sk_buff MUST have reference count of 1.
1537 * Returns %NULL (and &sk_buff does not change) if pull failed
1538 * or value of new tail of skb in the case of success.
1540 * All the pointers pointing into skb header may change and must be
1541 * reloaded after call to this function.
1544 /* Moves tail of skb head forward, copying data from fragmented part,
1545 * when it is necessary.
1546 * 1. It may fail due to malloc failure.
1547 * 2. It may change skb pointers.
1549 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1551 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1553 /* If skb has not enough free space at tail, get new one
1554 * plus 128 bytes for future expansions. If we have enough
1555 * room at tail, reallocate without expansion only if skb is cloned.
1557 int i, k, eat = (skb->tail + delta) - skb->end;
1559 if (eat > 0 || skb_cloned(skb)) {
1560 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1565 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1568 /* Optimization: no fragments, no reasons to preestimate
1569 * size of pulled pages. Superb.
1571 if (!skb_has_frag_list(skb))
1574 /* Estimate size of pulled pages. */
1576 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1577 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1584 /* If we need update frag list, we are in troubles.
1585 * Certainly, it possible to add an offset to skb data,
1586 * but taking into account that pulling is expected to
1587 * be very rare operation, it is worth to fight against
1588 * further bloating skb head and crucify ourselves here instead.
1589 * Pure masohism, indeed. 8)8)
1592 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1593 struct sk_buff *clone = NULL;
1594 struct sk_buff *insp = NULL;
1599 if (list->len <= eat) {
1600 /* Eaten as whole. */
1605 /* Eaten partially. */
1607 if (skb_shared(list)) {
1608 /* Sucks! We need to fork list. :-( */
1609 clone = skb_clone(list, GFP_ATOMIC);
1615 /* This may be pulled without
1619 if (!pskb_pull(list, eat)) {
1627 /* Free pulled out fragments. */
1628 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1629 skb_shinfo(skb)->frag_list = list->next;
1632 /* And insert new clone at head. */
1635 skb_shinfo(skb)->frag_list = clone;
1638 /* Success! Now we may commit changes to skb data. */
1643 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1644 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1647 skb_frag_unref(skb, i);
1650 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1652 skb_shinfo(skb)->frags[k].page_offset += eat;
1653 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1659 skb_shinfo(skb)->nr_frags = k;
1662 skb->data_len -= delta;
1664 return skb_tail_pointer(skb);
1666 EXPORT_SYMBOL(__pskb_pull_tail);
1669 * skb_copy_bits - copy bits from skb to kernel buffer
1671 * @offset: offset in source
1672 * @to: destination buffer
1673 * @len: number of bytes to copy
1675 * Copy the specified number of bytes from the source skb to the
1676 * destination buffer.
1679 * If its prototype is ever changed,
1680 * check arch/{*}/net/{*}.S files,
1681 * since it is called from BPF assembly code.
1683 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1685 int start = skb_headlen(skb);
1686 struct sk_buff *frag_iter;
1689 if (offset > (int)skb->len - len)
1693 if ((copy = start - offset) > 0) {
1696 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1697 if ((len -= copy) == 0)
1703 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1705 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1707 WARN_ON(start > offset + len);
1709 end = start + skb_frag_size(f);
1710 if ((copy = end - offset) > 0) {
1716 vaddr = kmap_atomic(skb_frag_page(f));
1718 vaddr + f->page_offset + offset - start,
1720 kunmap_atomic(vaddr);
1722 if ((len -= copy) == 0)
1730 skb_walk_frags(skb, frag_iter) {
1733 WARN_ON(start > offset + len);
1735 end = start + frag_iter->len;
1736 if ((copy = end - offset) > 0) {
1739 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1741 if ((len -= copy) == 0)
1755 EXPORT_SYMBOL(skb_copy_bits);
1758 * Callback from splice_to_pipe(), if we need to release some pages
1759 * at the end of the spd in case we error'ed out in filling the pipe.
1761 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1763 put_page(spd->pages[i]);
1766 static struct page *linear_to_page(struct page *page, unsigned int *len,
1767 unsigned int *offset,
1770 struct page_frag *pfrag = sk_page_frag(sk);
1772 if (!sk_page_frag_refill(sk, pfrag))
1775 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1777 memcpy(page_address(pfrag->page) + pfrag->offset,
1778 page_address(page) + *offset, *len);
1779 *offset = pfrag->offset;
1780 pfrag->offset += *len;
1785 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1787 unsigned int offset)
1789 return spd->nr_pages &&
1790 spd->pages[spd->nr_pages - 1] == page &&
1791 (spd->partial[spd->nr_pages - 1].offset +
1792 spd->partial[spd->nr_pages - 1].len == offset);
1796 * Fill page/offset/length into spd, if it can hold more pages.
1798 static bool spd_fill_page(struct splice_pipe_desc *spd,
1799 struct pipe_inode_info *pipe, struct page *page,
1800 unsigned int *len, unsigned int offset,
1804 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1808 page = linear_to_page(page, len, &offset, sk);
1812 if (spd_can_coalesce(spd, page, offset)) {
1813 spd->partial[spd->nr_pages - 1].len += *len;
1817 spd->pages[spd->nr_pages] = page;
1818 spd->partial[spd->nr_pages].len = *len;
1819 spd->partial[spd->nr_pages].offset = offset;
1825 static bool __splice_segment(struct page *page, unsigned int poff,
1826 unsigned int plen, unsigned int *off,
1828 struct splice_pipe_desc *spd, bool linear,
1830 struct pipe_inode_info *pipe)
1835 /* skip this segment if already processed */
1841 /* ignore any bits we already processed */
1847 unsigned int flen = min(*len, plen);
1849 if (spd_fill_page(spd, pipe, page, &flen, poff,
1855 } while (*len && plen);
1861 * Map linear and fragment data from the skb to spd. It reports true if the
1862 * pipe is full or if we already spliced the requested length.
1864 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1865 unsigned int *offset, unsigned int *len,
1866 struct splice_pipe_desc *spd, struct sock *sk)
1870 /* map the linear part :
1871 * If skb->head_frag is set, this 'linear' part is backed by a
1872 * fragment, and if the head is not shared with any clones then
1873 * we can avoid a copy since we own the head portion of this page.
1875 if (__splice_segment(virt_to_page(skb->data),
1876 (unsigned long) skb->data & (PAGE_SIZE - 1),
1879 skb_head_is_locked(skb),
1884 * then map the fragments
1886 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1887 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1889 if (__splice_segment(skb_frag_page(f),
1890 f->page_offset, skb_frag_size(f),
1891 offset, len, spd, false, sk, pipe))
1899 * Map data from the skb to a pipe. Should handle both the linear part,
1900 * the fragments, and the frag list. It does NOT handle frag lists within
1901 * the frag list, if such a thing exists. We'd probably need to recurse to
1902 * handle that cleanly.
1904 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1905 struct pipe_inode_info *pipe, unsigned int tlen,
1908 struct partial_page partial[MAX_SKB_FRAGS];
1909 struct page *pages[MAX_SKB_FRAGS];
1910 struct splice_pipe_desc spd = {
1913 .nr_pages_max = MAX_SKB_FRAGS,
1915 .ops = &nosteal_pipe_buf_ops,
1916 .spd_release = sock_spd_release,
1918 struct sk_buff *frag_iter;
1919 struct sock *sk = skb->sk;
1923 * __skb_splice_bits() only fails if the output has no room left,
1924 * so no point in going over the frag_list for the error case.
1926 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1932 * now see if we have a frag_list to map
1934 skb_walk_frags(skb, frag_iter) {
1937 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1944 * Drop the socket lock, otherwise we have reverse
1945 * locking dependencies between sk_lock and i_mutex
1946 * here as compared to sendfile(). We enter here
1947 * with the socket lock held, and splice_to_pipe() will
1948 * grab the pipe inode lock. For sendfile() emulation,
1949 * we call into ->sendpage() with the i_mutex lock held
1950 * and networking will grab the socket lock.
1953 ret = splice_to_pipe(pipe, &spd);
1961 * skb_store_bits - store bits from kernel buffer to skb
1962 * @skb: destination buffer
1963 * @offset: offset in destination
1964 * @from: source buffer
1965 * @len: number of bytes to copy
1967 * Copy the specified number of bytes from the source buffer to the
1968 * destination skb. This function handles all the messy bits of
1969 * traversing fragment lists and such.
1972 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1974 int start = skb_headlen(skb);
1975 struct sk_buff *frag_iter;
1978 if (offset > (int)skb->len - len)
1981 if ((copy = start - offset) > 0) {
1984 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1985 if ((len -= copy) == 0)
1991 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1992 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1995 WARN_ON(start > offset + len);
1997 end = start + skb_frag_size(frag);
1998 if ((copy = end - offset) > 0) {
2004 vaddr = kmap_atomic(skb_frag_page(frag));
2005 memcpy(vaddr + frag->page_offset + offset - start,
2007 kunmap_atomic(vaddr);
2009 if ((len -= copy) == 0)
2017 skb_walk_frags(skb, frag_iter) {
2020 WARN_ON(start > offset + len);
2022 end = start + frag_iter->len;
2023 if ((copy = end - offset) > 0) {
2026 if (skb_store_bits(frag_iter, offset - start,
2029 if ((len -= copy) == 0)
2042 EXPORT_SYMBOL(skb_store_bits);
2044 /* Checksum skb data. */
2045 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2046 __wsum csum, const struct skb_checksum_ops *ops)
2048 int start = skb_headlen(skb);
2049 int i, copy = start - offset;
2050 struct sk_buff *frag_iter;
2053 /* Checksum header. */
2057 csum = ops->update(skb->data + offset, copy, csum);
2058 if ((len -= copy) == 0)
2064 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2066 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2068 WARN_ON(start > offset + len);
2070 end = start + skb_frag_size(frag);
2071 if ((copy = end - offset) > 0) {
2077 vaddr = kmap_atomic(skb_frag_page(frag));
2078 csum2 = ops->update(vaddr + frag->page_offset +
2079 offset - start, copy, 0);
2080 kunmap_atomic(vaddr);
2081 csum = ops->combine(csum, csum2, pos, copy);
2090 skb_walk_frags(skb, frag_iter) {
2093 WARN_ON(start > offset + len);
2095 end = start + frag_iter->len;
2096 if ((copy = end - offset) > 0) {
2100 csum2 = __skb_checksum(frag_iter, offset - start,
2102 csum = ops->combine(csum, csum2, pos, copy);
2103 if ((len -= copy) == 0)
2114 EXPORT_SYMBOL(__skb_checksum);
2116 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2117 int len, __wsum csum)
2119 const struct skb_checksum_ops ops = {
2120 .update = csum_partial_ext,
2121 .combine = csum_block_add_ext,
2124 return __skb_checksum(skb, offset, len, csum, &ops);
2126 EXPORT_SYMBOL(skb_checksum);
2128 /* Both of above in one bottle. */
2130 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2131 u8 *to, int len, __wsum csum)
2133 int start = skb_headlen(skb);
2134 int i, copy = start - offset;
2135 struct sk_buff *frag_iter;
2142 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2144 if ((len -= copy) == 0)
2151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2154 WARN_ON(start > offset + len);
2156 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2157 if ((copy = end - offset) > 0) {
2160 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2164 vaddr = kmap_atomic(skb_frag_page(frag));
2165 csum2 = csum_partial_copy_nocheck(vaddr +
2169 kunmap_atomic(vaddr);
2170 csum = csum_block_add(csum, csum2, pos);
2180 skb_walk_frags(skb, frag_iter) {
2184 WARN_ON(start > offset + len);
2186 end = start + frag_iter->len;
2187 if ((copy = end - offset) > 0) {
2190 csum2 = skb_copy_and_csum_bits(frag_iter,
2193 csum = csum_block_add(csum, csum2, pos);
2194 if ((len -= copy) == 0)
2205 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2208 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2209 * @from: source buffer
2211 * Calculates the amount of linear headroom needed in the 'to' skb passed
2212 * into skb_zerocopy().
2215 skb_zerocopy_headlen(const struct sk_buff *from)
2217 unsigned int hlen = 0;
2219 if (!from->head_frag ||
2220 skb_headlen(from) < L1_CACHE_BYTES ||
2221 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2222 hlen = skb_headlen(from);
2224 if (skb_has_frag_list(from))
2229 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2232 * skb_zerocopy - Zero copy skb to skb
2233 * @to: destination buffer
2234 * @from: source buffer
2235 * @len: number of bytes to copy from source buffer
2236 * @hlen: size of linear headroom in destination buffer
2238 * Copies up to `len` bytes from `from` to `to` by creating references
2239 * to the frags in the source buffer.
2241 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2242 * headroom in the `to` buffer.
2245 * 0: everything is OK
2246 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2247 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2250 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2253 int plen = 0; /* length of skb->head fragment */
2256 unsigned int offset;
2258 BUG_ON(!from->head_frag && !hlen);
2260 /* dont bother with small payloads */
2261 if (len <= skb_tailroom(to))
2262 return skb_copy_bits(from, 0, skb_put(to, len), len);
2265 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2270 plen = min_t(int, skb_headlen(from), len);
2272 page = virt_to_head_page(from->head);
2273 offset = from->data - (unsigned char *)page_address(page);
2274 __skb_fill_page_desc(to, 0, page, offset, plen);
2281 to->truesize += len + plen;
2282 to->len += len + plen;
2283 to->data_len += len + plen;
2285 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2290 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2293 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2294 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2295 len -= skb_shinfo(to)->frags[j].size;
2296 skb_frag_ref(to, j);
2299 skb_shinfo(to)->nr_frags = j;
2303 EXPORT_SYMBOL_GPL(skb_zerocopy);
2305 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2310 if (skb->ip_summed == CHECKSUM_PARTIAL)
2311 csstart = skb_checksum_start_offset(skb);
2313 csstart = skb_headlen(skb);
2315 BUG_ON(csstart > skb_headlen(skb));
2317 skb_copy_from_linear_data(skb, to, csstart);
2320 if (csstart != skb->len)
2321 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2322 skb->len - csstart, 0);
2324 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2325 long csstuff = csstart + skb->csum_offset;
2327 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2330 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2333 * skb_dequeue - remove from the head of the queue
2334 * @list: list to dequeue from
2336 * Remove the head of the list. The list lock is taken so the function
2337 * may be used safely with other locking list functions. The head item is
2338 * returned or %NULL if the list is empty.
2341 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2343 unsigned long flags;
2344 struct sk_buff *result;
2346 spin_lock_irqsave(&list->lock, flags);
2347 result = __skb_dequeue(list);
2348 spin_unlock_irqrestore(&list->lock, flags);
2351 EXPORT_SYMBOL(skb_dequeue);
2354 * skb_dequeue_tail - remove from the tail of the queue
2355 * @list: list to dequeue from
2357 * Remove the tail of the list. The list lock is taken so the function
2358 * may be used safely with other locking list functions. The tail item is
2359 * returned or %NULL if the list is empty.
2361 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2363 unsigned long flags;
2364 struct sk_buff *result;
2366 spin_lock_irqsave(&list->lock, flags);
2367 result = __skb_dequeue_tail(list);
2368 spin_unlock_irqrestore(&list->lock, flags);
2371 EXPORT_SYMBOL(skb_dequeue_tail);
2374 * skb_queue_purge - empty a list
2375 * @list: list to empty
2377 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2378 * the list and one reference dropped. This function takes the list
2379 * lock and is atomic with respect to other list locking functions.
2381 void skb_queue_purge(struct sk_buff_head *list)
2383 struct sk_buff *skb;
2384 while ((skb = skb_dequeue(list)) != NULL)
2387 EXPORT_SYMBOL(skb_queue_purge);
2390 * skb_queue_head - queue a buffer at the list head
2391 * @list: list to use
2392 * @newsk: buffer to queue
2394 * Queue a buffer at the start of the list. This function takes the
2395 * list lock and can be used safely with other locking &sk_buff functions
2398 * A buffer cannot be placed on two lists at the same time.
2400 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2402 unsigned long flags;
2404 spin_lock_irqsave(&list->lock, flags);
2405 __skb_queue_head(list, newsk);
2406 spin_unlock_irqrestore(&list->lock, flags);
2408 EXPORT_SYMBOL(skb_queue_head);
2411 * skb_queue_tail - queue a buffer at the list tail
2412 * @list: list to use
2413 * @newsk: buffer to queue
2415 * Queue a buffer at the tail of the list. This function takes the
2416 * list lock and can be used safely with other locking &sk_buff functions
2419 * A buffer cannot be placed on two lists at the same time.
2421 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2423 unsigned long flags;
2425 spin_lock_irqsave(&list->lock, flags);
2426 __skb_queue_tail(list, newsk);
2427 spin_unlock_irqrestore(&list->lock, flags);
2429 EXPORT_SYMBOL(skb_queue_tail);
2432 * skb_unlink - remove a buffer from a list
2433 * @skb: buffer to remove
2434 * @list: list to use
2436 * Remove a packet from a list. The list locks are taken and this
2437 * function is atomic with respect to other list locked calls
2439 * You must know what list the SKB is on.
2441 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2443 unsigned long flags;
2445 spin_lock_irqsave(&list->lock, flags);
2446 __skb_unlink(skb, list);
2447 spin_unlock_irqrestore(&list->lock, flags);
2449 EXPORT_SYMBOL(skb_unlink);
2452 * skb_append - append a buffer
2453 * @old: buffer to insert after
2454 * @newsk: buffer to insert
2455 * @list: list to use
2457 * Place a packet after a given packet in a list. The list locks are taken
2458 * and this function is atomic with respect to other list locked calls.
2459 * A buffer cannot be placed on two lists at the same time.
2461 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2463 unsigned long flags;
2465 spin_lock_irqsave(&list->lock, flags);
2466 __skb_queue_after(list, old, newsk);
2467 spin_unlock_irqrestore(&list->lock, flags);
2469 EXPORT_SYMBOL(skb_append);
2472 * skb_insert - insert a buffer
2473 * @old: buffer to insert before
2474 * @newsk: buffer to insert
2475 * @list: list to use
2477 * Place a packet before a given packet in a list. The list locks are
2478 * taken and this function is atomic with respect to other list locked
2481 * A buffer cannot be placed on two lists at the same time.
2483 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2485 unsigned long flags;
2487 spin_lock_irqsave(&list->lock, flags);
2488 __skb_insert(newsk, old->prev, old, list);
2489 spin_unlock_irqrestore(&list->lock, flags);
2491 EXPORT_SYMBOL(skb_insert);
2493 static inline void skb_split_inside_header(struct sk_buff *skb,
2494 struct sk_buff* skb1,
2495 const u32 len, const int pos)
2499 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2501 /* And move data appendix as is. */
2502 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2503 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2505 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2506 skb_shinfo(skb)->nr_frags = 0;
2507 skb1->data_len = skb->data_len;
2508 skb1->len += skb1->data_len;
2511 skb_set_tail_pointer(skb, len);
2514 static inline void skb_split_no_header(struct sk_buff *skb,
2515 struct sk_buff* skb1,
2516 const u32 len, int pos)
2519 const int nfrags = skb_shinfo(skb)->nr_frags;
2521 skb_shinfo(skb)->nr_frags = 0;
2522 skb1->len = skb1->data_len = skb->len - len;
2524 skb->data_len = len - pos;
2526 for (i = 0; i < nfrags; i++) {
2527 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2529 if (pos + size > len) {
2530 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2534 * We have two variants in this case:
2535 * 1. Move all the frag to the second
2536 * part, if it is possible. F.e.
2537 * this approach is mandatory for TUX,
2538 * where splitting is expensive.
2539 * 2. Split is accurately. We make this.
2541 skb_frag_ref(skb, i);
2542 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2543 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2544 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2545 skb_shinfo(skb)->nr_frags++;
2549 skb_shinfo(skb)->nr_frags++;
2552 skb_shinfo(skb1)->nr_frags = k;
2556 * skb_split - Split fragmented skb to two parts at length len.
2557 * @skb: the buffer to split
2558 * @skb1: the buffer to receive the second part
2559 * @len: new length for skb
2561 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2563 int pos = skb_headlen(skb);
2565 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2566 if (len < pos) /* Split line is inside header. */
2567 skb_split_inside_header(skb, skb1, len, pos);
2568 else /* Second chunk has no header, nothing to copy. */
2569 skb_split_no_header(skb, skb1, len, pos);
2571 EXPORT_SYMBOL(skb_split);
2573 /* Shifting from/to a cloned skb is a no-go.
2575 * Caller cannot keep skb_shinfo related pointers past calling here!
2577 static int skb_prepare_for_shift(struct sk_buff *skb)
2579 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2583 * skb_shift - Shifts paged data partially from skb to another
2584 * @tgt: buffer into which tail data gets added
2585 * @skb: buffer from which the paged data comes from
2586 * @shiftlen: shift up to this many bytes
2588 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2589 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2590 * It's up to caller to free skb if everything was shifted.
2592 * If @tgt runs out of frags, the whole operation is aborted.
2594 * Skb cannot include anything else but paged data while tgt is allowed
2595 * to have non-paged data as well.
2597 * TODO: full sized shift could be optimized but that would need
2598 * specialized skb free'er to handle frags without up-to-date nr_frags.
2600 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2602 int from, to, merge, todo;
2603 struct skb_frag_struct *fragfrom, *fragto;
2605 BUG_ON(shiftlen > skb->len);
2606 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2610 to = skb_shinfo(tgt)->nr_frags;
2611 fragfrom = &skb_shinfo(skb)->frags[from];
2613 /* Actual merge is delayed until the point when we know we can
2614 * commit all, so that we don't have to undo partial changes
2617 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2618 fragfrom->page_offset)) {
2623 todo -= skb_frag_size(fragfrom);
2625 if (skb_prepare_for_shift(skb) ||
2626 skb_prepare_for_shift(tgt))
2629 /* All previous frag pointers might be stale! */
2630 fragfrom = &skb_shinfo(skb)->frags[from];
2631 fragto = &skb_shinfo(tgt)->frags[merge];
2633 skb_frag_size_add(fragto, shiftlen);
2634 skb_frag_size_sub(fragfrom, shiftlen);
2635 fragfrom->page_offset += shiftlen;
2643 /* Skip full, not-fitting skb to avoid expensive operations */
2644 if ((shiftlen == skb->len) &&
2645 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2648 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2651 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2652 if (to == MAX_SKB_FRAGS)
2655 fragfrom = &skb_shinfo(skb)->frags[from];
2656 fragto = &skb_shinfo(tgt)->frags[to];
2658 if (todo >= skb_frag_size(fragfrom)) {
2659 *fragto = *fragfrom;
2660 todo -= skb_frag_size(fragfrom);
2665 __skb_frag_ref(fragfrom);
2666 fragto->page = fragfrom->page;
2667 fragto->page_offset = fragfrom->page_offset;
2668 skb_frag_size_set(fragto, todo);
2670 fragfrom->page_offset += todo;
2671 skb_frag_size_sub(fragfrom, todo);
2679 /* Ready to "commit" this state change to tgt */
2680 skb_shinfo(tgt)->nr_frags = to;
2683 fragfrom = &skb_shinfo(skb)->frags[0];
2684 fragto = &skb_shinfo(tgt)->frags[merge];
2686 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2687 __skb_frag_unref(fragfrom);
2690 /* Reposition in the original skb */
2692 while (from < skb_shinfo(skb)->nr_frags)
2693 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2694 skb_shinfo(skb)->nr_frags = to;
2696 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2699 /* Most likely the tgt won't ever need its checksum anymore, skb on
2700 * the other hand might need it if it needs to be resent
2702 tgt->ip_summed = CHECKSUM_PARTIAL;
2703 skb->ip_summed = CHECKSUM_PARTIAL;
2705 /* Yak, is it really working this way? Some helper please? */
2706 skb->len -= shiftlen;
2707 skb->data_len -= shiftlen;
2708 skb->truesize -= shiftlen;
2709 tgt->len += shiftlen;
2710 tgt->data_len += shiftlen;
2711 tgt->truesize += shiftlen;
2717 * skb_prepare_seq_read - Prepare a sequential read of skb data
2718 * @skb: the buffer to read
2719 * @from: lower offset of data to be read
2720 * @to: upper offset of data to be read
2721 * @st: state variable
2723 * Initializes the specified state variable. Must be called before
2724 * invoking skb_seq_read() for the first time.
2726 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2727 unsigned int to, struct skb_seq_state *st)
2729 st->lower_offset = from;
2730 st->upper_offset = to;
2731 st->root_skb = st->cur_skb = skb;
2732 st->frag_idx = st->stepped_offset = 0;
2733 st->frag_data = NULL;
2735 EXPORT_SYMBOL(skb_prepare_seq_read);
2738 * skb_seq_read - Sequentially read skb data
2739 * @consumed: number of bytes consumed by the caller so far
2740 * @data: destination pointer for data to be returned
2741 * @st: state variable
2743 * Reads a block of skb data at @consumed relative to the
2744 * lower offset specified to skb_prepare_seq_read(). Assigns
2745 * the head of the data block to @data and returns the length
2746 * of the block or 0 if the end of the skb data or the upper
2747 * offset has been reached.
2749 * The caller is not required to consume all of the data
2750 * returned, i.e. @consumed is typically set to the number
2751 * of bytes already consumed and the next call to
2752 * skb_seq_read() will return the remaining part of the block.
2754 * Note 1: The size of each block of data returned can be arbitrary,
2755 * this limitation is the cost for zerocopy sequential
2756 * reads of potentially non linear data.
2758 * Note 2: Fragment lists within fragments are not implemented
2759 * at the moment, state->root_skb could be replaced with
2760 * a stack for this purpose.
2762 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2763 struct skb_seq_state *st)
2765 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2768 if (unlikely(abs_offset >= st->upper_offset)) {
2769 if (st->frag_data) {
2770 kunmap_atomic(st->frag_data);
2771 st->frag_data = NULL;
2777 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2779 if (abs_offset < block_limit && !st->frag_data) {
2780 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2781 return block_limit - abs_offset;
2784 if (st->frag_idx == 0 && !st->frag_data)
2785 st->stepped_offset += skb_headlen(st->cur_skb);
2787 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2788 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2789 block_limit = skb_frag_size(frag) + st->stepped_offset;
2791 if (abs_offset < block_limit) {
2793 st->frag_data = kmap_atomic(skb_frag_page(frag));
2795 *data = (u8 *) st->frag_data + frag->page_offset +
2796 (abs_offset - st->stepped_offset);
2798 return block_limit - abs_offset;
2801 if (st->frag_data) {
2802 kunmap_atomic(st->frag_data);
2803 st->frag_data = NULL;
2807 st->stepped_offset += skb_frag_size(frag);
2810 if (st->frag_data) {
2811 kunmap_atomic(st->frag_data);
2812 st->frag_data = NULL;
2815 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2816 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2819 } else if (st->cur_skb->next) {
2820 st->cur_skb = st->cur_skb->next;
2827 EXPORT_SYMBOL(skb_seq_read);
2830 * skb_abort_seq_read - Abort a sequential read of skb data
2831 * @st: state variable
2833 * Must be called if skb_seq_read() was not called until it
2836 void skb_abort_seq_read(struct skb_seq_state *st)
2839 kunmap_atomic(st->frag_data);
2841 EXPORT_SYMBOL(skb_abort_seq_read);
2843 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2845 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2846 struct ts_config *conf,
2847 struct ts_state *state)
2849 return skb_seq_read(offset, text, TS_SKB_CB(state));
2852 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2854 skb_abort_seq_read(TS_SKB_CB(state));
2858 * skb_find_text - Find a text pattern in skb data
2859 * @skb: the buffer to look in
2860 * @from: search offset
2862 * @config: textsearch configuration
2863 * @state: uninitialized textsearch state variable
2865 * Finds a pattern in the skb data according to the specified
2866 * textsearch configuration. Use textsearch_next() to retrieve
2867 * subsequent occurrences of the pattern. Returns the offset
2868 * to the first occurrence or UINT_MAX if no match was found.
2870 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2871 unsigned int to, struct ts_config *config,
2872 struct ts_state *state)
2876 config->get_next_block = skb_ts_get_next_block;
2877 config->finish = skb_ts_finish;
2879 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2881 ret = textsearch_find(config, state);
2882 return (ret <= to - from ? ret : UINT_MAX);
2884 EXPORT_SYMBOL(skb_find_text);
2887 * skb_append_datato_frags - append the user data to a skb
2888 * @sk: sock structure
2889 * @skb: skb structure to be appended with user data.
2890 * @getfrag: call back function to be used for getting the user data
2891 * @from: pointer to user message iov
2892 * @length: length of the iov message
2894 * Description: This procedure append the user data in the fragment part
2895 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2897 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2898 int (*getfrag)(void *from, char *to, int offset,
2899 int len, int odd, struct sk_buff *skb),
2900 void *from, int length)
2902 int frg_cnt = skb_shinfo(skb)->nr_frags;
2906 struct page_frag *pfrag = ¤t->task_frag;
2909 /* Return error if we don't have space for new frag */
2910 if (frg_cnt >= MAX_SKB_FRAGS)
2913 if (!sk_page_frag_refill(sk, pfrag))
2916 /* copy the user data to page */
2917 copy = min_t(int, length, pfrag->size - pfrag->offset);
2919 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2920 offset, copy, 0, skb);
2924 /* copy was successful so update the size parameters */
2925 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2928 pfrag->offset += copy;
2929 get_page(pfrag->page);
2931 skb->truesize += copy;
2932 atomic_add(copy, &sk->sk_wmem_alloc);
2934 skb->data_len += copy;
2938 } while (length > 0);
2942 EXPORT_SYMBOL(skb_append_datato_frags);
2945 * skb_pull_rcsum - pull skb and update receive checksum
2946 * @skb: buffer to update
2947 * @len: length of data pulled
2949 * This function performs an skb_pull on the packet and updates
2950 * the CHECKSUM_COMPLETE checksum. It should be used on
2951 * receive path processing instead of skb_pull unless you know
2952 * that the checksum difference is zero (e.g., a valid IP header)
2953 * or you are setting ip_summed to CHECKSUM_NONE.
2955 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2957 BUG_ON(len > skb->len);
2959 BUG_ON(skb->len < skb->data_len);
2960 skb_postpull_rcsum(skb, skb->data, len);
2961 return skb->data += len;
2963 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2966 * skb_segment - Perform protocol segmentation on skb.
2967 * @head_skb: buffer to segment
2968 * @features: features for the output path (see dev->features)
2970 * This function performs segmentation on the given skb. It returns
2971 * a pointer to the first in a list of new skbs for the segments.
2972 * In case of error it returns ERR_PTR(err).
2974 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2975 netdev_features_t features)
2977 struct sk_buff *segs = NULL;
2978 struct sk_buff *tail = NULL;
2979 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2980 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2981 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2982 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2983 struct sk_buff *frag_skb = head_skb;
2984 unsigned int offset = doffset;
2985 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2986 unsigned int headroom;
2990 int sg = !!(features & NETIF_F_SG);
2991 int nfrags = skb_shinfo(head_skb)->nr_frags;
2997 __skb_push(head_skb, doffset);
2998 proto = skb_network_protocol(head_skb, &dummy);
2999 if (unlikely(!proto))
3000 return ERR_PTR(-EINVAL);
3002 csum = !head_skb->encap_hdr_csum &&
3003 !!can_checksum_protocol(features, proto);
3005 headroom = skb_headroom(head_skb);
3006 pos = skb_headlen(head_skb);
3009 struct sk_buff *nskb;
3010 skb_frag_t *nskb_frag;
3014 len = head_skb->len - offset;
3018 hsize = skb_headlen(head_skb) - offset;
3021 if (hsize > len || !sg)
3024 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3025 (skb_headlen(list_skb) == len || sg)) {
3026 BUG_ON(skb_headlen(list_skb) > len);
3029 nfrags = skb_shinfo(list_skb)->nr_frags;
3030 frag = skb_shinfo(list_skb)->frags;
3031 frag_skb = list_skb;
3032 pos += skb_headlen(list_skb);
3034 while (pos < offset + len) {
3035 BUG_ON(i >= nfrags);
3037 size = skb_frag_size(frag);
3038 if (pos + size > offset + len)
3046 nskb = skb_clone(list_skb, GFP_ATOMIC);
3047 list_skb = list_skb->next;
3049 if (unlikely(!nskb))
3052 if (unlikely(pskb_trim(nskb, len))) {
3057 hsize = skb_end_offset(nskb);
3058 if (skb_cow_head(nskb, doffset + headroom)) {
3063 nskb->truesize += skb_end_offset(nskb) - hsize;
3064 skb_release_head_state(nskb);
3065 __skb_push(nskb, doffset);
3067 nskb = __alloc_skb(hsize + doffset + headroom,
3068 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3071 if (unlikely(!nskb))
3074 skb_reserve(nskb, headroom);
3075 __skb_put(nskb, doffset);
3084 __copy_skb_header(nskb, head_skb);
3086 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3087 skb_reset_mac_len(nskb);
3089 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3090 nskb->data - tnl_hlen,
3091 doffset + tnl_hlen);
3093 if (nskb->len == len + doffset)
3094 goto perform_csum_check;
3096 if (!sg && !nskb->remcsum_offload) {
3097 nskb->ip_summed = CHECKSUM_NONE;
3098 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3101 SKB_GSO_CB(nskb)->csum_start =
3102 skb_headroom(nskb) + doffset;
3106 nskb_frag = skb_shinfo(nskb)->frags;
3108 skb_copy_from_linear_data_offset(head_skb, offset,
3109 skb_put(nskb, hsize), hsize);
3111 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3114 while (pos < offset + len) {
3116 BUG_ON(skb_headlen(list_skb));
3119 nfrags = skb_shinfo(list_skb)->nr_frags;
3120 frag = skb_shinfo(list_skb)->frags;
3121 frag_skb = list_skb;
3125 list_skb = list_skb->next;
3128 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3130 net_warn_ratelimited(
3131 "skb_segment: too many frags: %u %u\n",
3136 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3140 __skb_frag_ref(nskb_frag);
3141 size = skb_frag_size(nskb_frag);
3144 nskb_frag->page_offset += offset - pos;
3145 skb_frag_size_sub(nskb_frag, offset - pos);
3148 skb_shinfo(nskb)->nr_frags++;
3150 if (pos + size <= offset + len) {
3155 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3163 nskb->data_len = len - hsize;
3164 nskb->len += nskb->data_len;
3165 nskb->truesize += nskb->data_len;
3168 if (!csum && !nskb->remcsum_offload) {
3169 nskb->csum = skb_checksum(nskb, doffset,
3170 nskb->len - doffset, 0);
3171 nskb->ip_summed = CHECKSUM_NONE;
3172 SKB_GSO_CB(nskb)->csum_start =
3173 skb_headroom(nskb) + doffset;
3175 } while ((offset += len) < head_skb->len);
3177 /* Some callers want to get the end of the list.
3178 * Put it in segs->prev to avoid walking the list.
3179 * (see validate_xmit_skb_list() for example)
3183 /* Following permits correct backpressure, for protocols
3184 * using skb_set_owner_w().
3185 * Idea is to tranfert ownership from head_skb to last segment.
3187 if (head_skb->destructor == sock_wfree) {
3188 swap(tail->truesize, head_skb->truesize);
3189 swap(tail->destructor, head_skb->destructor);
3190 swap(tail->sk, head_skb->sk);
3195 kfree_skb_list(segs);
3196 return ERR_PTR(err);
3198 EXPORT_SYMBOL_GPL(skb_segment);
3200 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3202 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3203 unsigned int offset = skb_gro_offset(skb);
3204 unsigned int headlen = skb_headlen(skb);
3205 struct sk_buff *nskb, *lp, *p = *head;
3206 unsigned int len = skb_gro_len(skb);
3207 unsigned int delta_truesize;
3208 unsigned int headroom;
3210 if (unlikely(p->len + len >= 65536))
3213 lp = NAPI_GRO_CB(p)->last;
3214 pinfo = skb_shinfo(lp);
3216 if (headlen <= offset) {
3219 int i = skbinfo->nr_frags;
3220 int nr_frags = pinfo->nr_frags + i;
3222 if (nr_frags > MAX_SKB_FRAGS)
3226 pinfo->nr_frags = nr_frags;
3227 skbinfo->nr_frags = 0;
3229 frag = pinfo->frags + nr_frags;
3230 frag2 = skbinfo->frags + i;
3235 frag->page_offset += offset;
3236 skb_frag_size_sub(frag, offset);
3238 /* all fragments truesize : remove (head size + sk_buff) */
3239 delta_truesize = skb->truesize -
3240 SKB_TRUESIZE(skb_end_offset(skb));
3242 skb->truesize -= skb->data_len;
3243 skb->len -= skb->data_len;
3246 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3248 } else if (skb->head_frag) {
3249 int nr_frags = pinfo->nr_frags;
3250 skb_frag_t *frag = pinfo->frags + nr_frags;
3251 struct page *page = virt_to_head_page(skb->head);
3252 unsigned int first_size = headlen - offset;
3253 unsigned int first_offset;
3255 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3258 first_offset = skb->data -
3259 (unsigned char *)page_address(page) +
3262 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3264 frag->page.p = page;
3265 frag->page_offset = first_offset;
3266 skb_frag_size_set(frag, first_size);
3268 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3269 /* We dont need to clear skbinfo->nr_frags here */
3271 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3272 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3275 /* switch back to head shinfo */
3276 pinfo = skb_shinfo(p);
3278 if (pinfo->frag_list)
3280 if (skb_gro_len(p) != pinfo->gso_size)
3283 headroom = skb_headroom(p);
3284 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3285 if (unlikely(!nskb))
3288 __copy_skb_header(nskb, p);
3289 nskb->mac_len = p->mac_len;
3291 skb_reserve(nskb, headroom);
3292 __skb_put(nskb, skb_gro_offset(p));
3294 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3295 skb_set_network_header(nskb, skb_network_offset(p));
3296 skb_set_transport_header(nskb, skb_transport_offset(p));
3298 __skb_pull(p, skb_gro_offset(p));
3299 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3300 p->data - skb_mac_header(p));
3302 skb_shinfo(nskb)->frag_list = p;
3303 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3304 pinfo->gso_size = 0;
3305 __skb_header_release(p);
3306 NAPI_GRO_CB(nskb)->last = p;
3308 nskb->data_len += p->len;
3309 nskb->truesize += p->truesize;
3310 nskb->len += p->len;
3313 nskb->next = p->next;
3319 delta_truesize = skb->truesize;
3320 if (offset > headlen) {
3321 unsigned int eat = offset - headlen;
3323 skbinfo->frags[0].page_offset += eat;
3324 skb_frag_size_sub(&skbinfo->frags[0], eat);
3325 skb->data_len -= eat;
3330 __skb_pull(skb, offset);
3332 if (NAPI_GRO_CB(p)->last == p)
3333 skb_shinfo(p)->frag_list = skb;
3335 NAPI_GRO_CB(p)->last->next = skb;
3336 NAPI_GRO_CB(p)->last = skb;
3337 __skb_header_release(skb);
3341 NAPI_GRO_CB(p)->count++;
3343 p->truesize += delta_truesize;
3346 lp->data_len += len;
3347 lp->truesize += delta_truesize;
3350 NAPI_GRO_CB(skb)->same_flow = 1;
3354 void __init skb_init(void)
3356 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3357 sizeof(struct sk_buff),
3359 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3361 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3362 sizeof(struct sk_buff_fclones),
3364 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3369 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3370 * @skb: Socket buffer containing the buffers to be mapped
3371 * @sg: The scatter-gather list to map into
3372 * @offset: The offset into the buffer's contents to start mapping
3373 * @len: Length of buffer space to be mapped
3375 * Fill the specified scatter-gather list with mappings/pointers into a
3376 * region of the buffer space attached to a socket buffer.
3379 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3381 int start = skb_headlen(skb);
3382 int i, copy = start - offset;
3383 struct sk_buff *frag_iter;
3389 sg_set_buf(sg, skb->data + offset, copy);
3391 if ((len -= copy) == 0)
3396 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3399 WARN_ON(start > offset + len);
3401 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3402 if ((copy = end - offset) > 0) {
3403 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3407 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3408 frag->page_offset+offset-start);
3417 skb_walk_frags(skb, frag_iter) {
3420 WARN_ON(start > offset + len);
3422 end = start + frag_iter->len;
3423 if ((copy = end - offset) > 0) {
3426 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3428 if ((len -= copy) == 0)
3438 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3439 * sglist without mark the sg which contain last skb data as the end.
3440 * So the caller can mannipulate sg list as will when padding new data after
3441 * the first call without calling sg_unmark_end to expend sg list.
3443 * Scenario to use skb_to_sgvec_nomark:
3445 * 2. skb_to_sgvec_nomark(payload1)
3446 * 3. skb_to_sgvec_nomark(payload2)
3448 * This is equivalent to:
3450 * 2. skb_to_sgvec(payload1)
3452 * 4. skb_to_sgvec(payload2)
3454 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3455 * is more preferable.
3457 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3458 int offset, int len)
3460 return __skb_to_sgvec(skb, sg, offset, len);
3462 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3464 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3466 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3468 sg_mark_end(&sg[nsg - 1]);
3472 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3475 * skb_cow_data - Check that a socket buffer's data buffers are writable
3476 * @skb: The socket buffer to check.
3477 * @tailbits: Amount of trailing space to be added
3478 * @trailer: Returned pointer to the skb where the @tailbits space begins
3480 * Make sure that the data buffers attached to a socket buffer are
3481 * writable. If they are not, private copies are made of the data buffers
3482 * and the socket buffer is set to use these instead.
3484 * If @tailbits is given, make sure that there is space to write @tailbits
3485 * bytes of data beyond current end of socket buffer. @trailer will be
3486 * set to point to the skb in which this space begins.
3488 * The number of scatterlist elements required to completely map the
3489 * COW'd and extended socket buffer will be returned.
3491 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3495 struct sk_buff *skb1, **skb_p;
3497 /* If skb is cloned or its head is paged, reallocate
3498 * head pulling out all the pages (pages are considered not writable
3499 * at the moment even if they are anonymous).
3501 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3502 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3505 /* Easy case. Most of packets will go this way. */
3506 if (!skb_has_frag_list(skb)) {
3507 /* A little of trouble, not enough of space for trailer.
3508 * This should not happen, when stack is tuned to generate
3509 * good frames. OK, on miss we reallocate and reserve even more
3510 * space, 128 bytes is fair. */
3512 if (skb_tailroom(skb) < tailbits &&
3513 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3521 /* Misery. We are in troubles, going to mincer fragments... */
3524 skb_p = &skb_shinfo(skb)->frag_list;
3527 while ((skb1 = *skb_p) != NULL) {
3530 /* The fragment is partially pulled by someone,
3531 * this can happen on input. Copy it and everything
3534 if (skb_shared(skb1))
3537 /* If the skb is the last, worry about trailer. */
3539 if (skb1->next == NULL && tailbits) {
3540 if (skb_shinfo(skb1)->nr_frags ||
3541 skb_has_frag_list(skb1) ||
3542 skb_tailroom(skb1) < tailbits)
3543 ntail = tailbits + 128;
3549 skb_shinfo(skb1)->nr_frags ||
3550 skb_has_frag_list(skb1)) {
3551 struct sk_buff *skb2;
3553 /* Fuck, we are miserable poor guys... */
3555 skb2 = skb_copy(skb1, GFP_ATOMIC);
3557 skb2 = skb_copy_expand(skb1,
3561 if (unlikely(skb2 == NULL))
3565 skb_set_owner_w(skb2, skb1->sk);
3567 /* Looking around. Are we still alive?
3568 * OK, link new skb, drop old one */
3570 skb2->next = skb1->next;
3577 skb_p = &skb1->next;
3582 EXPORT_SYMBOL_GPL(skb_cow_data);
3584 static void sock_rmem_free(struct sk_buff *skb)
3586 struct sock *sk = skb->sk;
3588 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3592 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3594 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3596 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3597 (unsigned int)sk->sk_rcvbuf)
3602 skb->destructor = sock_rmem_free;
3603 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3605 /* before exiting rcu section, make sure dst is refcounted */
3608 skb_queue_tail(&sk->sk_error_queue, skb);
3609 if (!sock_flag(sk, SOCK_DEAD))
3610 sk->sk_data_ready(sk);
3613 EXPORT_SYMBOL(sock_queue_err_skb);
3615 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3617 struct sk_buff_head *q = &sk->sk_error_queue;
3618 struct sk_buff *skb, *skb_next;
3621 spin_lock_bh(&q->lock);
3622 skb = __skb_dequeue(q);
3623 if (skb && (skb_next = skb_peek(q)))
3624 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3625 spin_unlock_bh(&q->lock);
3629 sk->sk_error_report(sk);
3633 EXPORT_SYMBOL(sock_dequeue_err_skb);
3636 * skb_clone_sk - create clone of skb, and take reference to socket
3637 * @skb: the skb to clone
3639 * This function creates a clone of a buffer that holds a reference on
3640 * sk_refcnt. Buffers created via this function are meant to be
3641 * returned using sock_queue_err_skb, or free via kfree_skb.
3643 * When passing buffers allocated with this function to sock_queue_err_skb
3644 * it is necessary to wrap the call with sock_hold/sock_put in order to
3645 * prevent the socket from being released prior to being enqueued on
3646 * the sk_error_queue.
3648 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3650 struct sock *sk = skb->sk;
3651 struct sk_buff *clone;
3653 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3656 clone = skb_clone(skb, GFP_ATOMIC);
3663 clone->destructor = sock_efree;
3667 EXPORT_SYMBOL(skb_clone_sk);
3669 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3673 struct sock_exterr_skb *serr;
3676 serr = SKB_EXT_ERR(skb);
3677 memset(serr, 0, sizeof(*serr));
3678 serr->ee.ee_errno = ENOMSG;
3679 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3680 serr->ee.ee_info = tstype;
3681 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3682 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3683 if (sk->sk_protocol == IPPROTO_TCP)
3684 serr->ee.ee_data -= sk->sk_tskey;
3687 err = sock_queue_err_skb(sk, skb);
3693 void skb_complete_tx_timestamp(struct sk_buff *skb,
3694 struct skb_shared_hwtstamps *hwtstamps)
3696 struct sock *sk = skb->sk;
3698 /* take a reference to prevent skb_orphan() from freeing the socket */
3701 *skb_hwtstamps(skb) = *hwtstamps;
3702 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3706 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3708 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3709 struct skb_shared_hwtstamps *hwtstamps,
3710 struct sock *sk, int tstype)
3712 struct sk_buff *skb;
3718 *skb_hwtstamps(orig_skb) = *hwtstamps;
3720 orig_skb->tstamp = ktime_get_real();
3722 skb = skb_clone(orig_skb, GFP_ATOMIC);
3726 __skb_complete_tx_timestamp(skb, sk, tstype);
3728 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3730 void skb_tstamp_tx(struct sk_buff *orig_skb,
3731 struct skb_shared_hwtstamps *hwtstamps)
3733 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3736 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3738 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3740 struct sock *sk = skb->sk;
3741 struct sock_exterr_skb *serr;
3744 skb->wifi_acked_valid = 1;
3745 skb->wifi_acked = acked;
3747 serr = SKB_EXT_ERR(skb);
3748 memset(serr, 0, sizeof(*serr));
3749 serr->ee.ee_errno = ENOMSG;
3750 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3752 /* take a reference to prevent skb_orphan() from freeing the socket */
3755 err = sock_queue_err_skb(sk, skb);
3761 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3765 * skb_partial_csum_set - set up and verify partial csum values for packet
3766 * @skb: the skb to set
3767 * @start: the number of bytes after skb->data to start checksumming.
3768 * @off: the offset from start to place the checksum.
3770 * For untrusted partially-checksummed packets, we need to make sure the values
3771 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3773 * This function checks and sets those values and skb->ip_summed: if this
3774 * returns false you should drop the packet.
3776 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3778 if (unlikely(start > skb_headlen(skb)) ||
3779 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3780 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3781 start, off, skb_headlen(skb));
3784 skb->ip_summed = CHECKSUM_PARTIAL;
3785 skb->csum_start = skb_headroom(skb) + start;
3786 skb->csum_offset = off;
3787 skb_set_transport_header(skb, start);
3790 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3792 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3795 if (skb_headlen(skb) >= len)
3798 /* If we need to pullup then pullup to the max, so we
3799 * won't need to do it again.
3804 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3807 if (skb_headlen(skb) < len)
3813 #define MAX_TCP_HDR_LEN (15 * 4)
3815 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3816 typeof(IPPROTO_IP) proto,
3823 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3824 off + MAX_TCP_HDR_LEN);
3825 if (!err && !skb_partial_csum_set(skb, off,
3826 offsetof(struct tcphdr,
3829 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3832 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3833 off + sizeof(struct udphdr));
3834 if (!err && !skb_partial_csum_set(skb, off,
3835 offsetof(struct udphdr,
3838 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3841 return ERR_PTR(-EPROTO);
3844 /* This value should be large enough to cover a tagged ethernet header plus
3845 * maximally sized IP and TCP or UDP headers.
3847 #define MAX_IP_HDR_LEN 128
3849 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3858 err = skb_maybe_pull_tail(skb,
3859 sizeof(struct iphdr),
3864 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3867 off = ip_hdrlen(skb);
3874 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3876 return PTR_ERR(csum);
3879 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3882 ip_hdr(skb)->protocol, 0);
3889 /* This value should be large enough to cover a tagged ethernet header plus
3890 * an IPv6 header, all options, and a maximal TCP or UDP header.
3892 #define MAX_IPV6_HDR_LEN 256
3894 #define OPT_HDR(type, skb, off) \
3895 (type *)(skb_network_header(skb) + (off))
3897 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3910 off = sizeof(struct ipv6hdr);
3912 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3916 nexthdr = ipv6_hdr(skb)->nexthdr;
3918 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3919 while (off <= len && !done) {
3921 case IPPROTO_DSTOPTS:
3922 case IPPROTO_HOPOPTS:
3923 case IPPROTO_ROUTING: {
3924 struct ipv6_opt_hdr *hp;
3926 err = skb_maybe_pull_tail(skb,
3928 sizeof(struct ipv6_opt_hdr),
3933 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3934 nexthdr = hp->nexthdr;
3935 off += ipv6_optlen(hp);
3939 struct ip_auth_hdr *hp;
3941 err = skb_maybe_pull_tail(skb,
3943 sizeof(struct ip_auth_hdr),
3948 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3949 nexthdr = hp->nexthdr;
3950 off += ipv6_authlen(hp);
3953 case IPPROTO_FRAGMENT: {
3954 struct frag_hdr *hp;
3956 err = skb_maybe_pull_tail(skb,
3958 sizeof(struct frag_hdr),
3963 hp = OPT_HDR(struct frag_hdr, skb, off);
3965 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3968 nexthdr = hp->nexthdr;
3969 off += sizeof(struct frag_hdr);
3980 if (!done || fragment)
3983 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3985 return PTR_ERR(csum);
3988 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3989 &ipv6_hdr(skb)->daddr,
3990 skb->len - off, nexthdr, 0);
3998 * skb_checksum_setup - set up partial checksum offset
3999 * @skb: the skb to set up
4000 * @recalculate: if true the pseudo-header checksum will be recalculated
4002 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4006 switch (skb->protocol) {
4007 case htons(ETH_P_IP):
4008 err = skb_checksum_setup_ipv4(skb, recalculate);
4011 case htons(ETH_P_IPV6):
4012 err = skb_checksum_setup_ipv6(skb, recalculate);
4022 EXPORT_SYMBOL(skb_checksum_setup);
4024 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4026 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4029 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4031 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4034 skb_release_head_state(skb);
4035 kmem_cache_free(skbuff_head_cache, skb);
4040 EXPORT_SYMBOL(kfree_skb_partial);
4043 * skb_try_coalesce - try to merge skb to prior one
4045 * @from: buffer to add
4046 * @fragstolen: pointer to boolean
4047 * @delta_truesize: how much more was allocated than was requested
4049 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4050 bool *fragstolen, int *delta_truesize)
4052 int i, delta, len = from->len;
4054 *fragstolen = false;
4059 if (len <= skb_tailroom(to)) {
4061 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4062 *delta_truesize = 0;
4066 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4069 if (skb_headlen(from) != 0) {
4071 unsigned int offset;
4073 if (skb_shinfo(to)->nr_frags +
4074 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4077 if (skb_head_is_locked(from))
4080 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4082 page = virt_to_head_page(from->head);
4083 offset = from->data - (unsigned char *)page_address(page);
4085 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4086 page, offset, skb_headlen(from));
4089 if (skb_shinfo(to)->nr_frags +
4090 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4093 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4096 WARN_ON_ONCE(delta < len);
4098 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4099 skb_shinfo(from)->frags,
4100 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4101 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4103 if (!skb_cloned(from))
4104 skb_shinfo(from)->nr_frags = 0;
4106 /* if the skb is not cloned this does nothing
4107 * since we set nr_frags to 0.
4109 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4110 skb_frag_ref(from, i);
4112 to->truesize += delta;
4114 to->data_len += len;
4116 *delta_truesize = delta;
4119 EXPORT_SYMBOL(skb_try_coalesce);
4122 * skb_scrub_packet - scrub an skb
4124 * @skb: buffer to clean
4125 * @xnet: packet is crossing netns
4127 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4128 * into/from a tunnel. Some information have to be cleared during these
4130 * skb_scrub_packet can also be used to clean a skb before injecting it in
4131 * another namespace (@xnet == true). We have to clear all information in the
4132 * skb that could impact namespace isolation.
4134 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4138 skb->tstamp.tv64 = 0;
4139 skb->pkt_type = PACKET_HOST;
4144 skb_init_secmark(skb);
4147 nf_reset_trace(skb);
4149 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4152 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4156 * skb_gso_transport_seglen is used to determine the real size of the
4157 * individual segments, including Layer4 headers (TCP/UDP).
4159 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4161 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4163 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4164 unsigned int thlen = 0;
4166 if (skb->encapsulation) {
4167 thlen = skb_inner_transport_header(skb) -
4168 skb_transport_header(skb);
4170 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4171 thlen += inner_tcp_hdrlen(skb);
4172 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4173 thlen = tcp_hdrlen(skb);
4175 /* UFO sets gso_size to the size of the fragmentation
4176 * payload, i.e. the size of the L4 (UDP) header is already
4179 return thlen + shinfo->gso_size;
4181 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4183 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4185 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4190 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4191 skb->mac_header += VLAN_HLEN;
4195 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4197 struct vlan_hdr *vhdr;
4200 if (unlikely(vlan_tx_tag_present(skb))) {
4201 /* vlan_tci is already set-up so leave this for another time */
4205 skb = skb_share_check(skb, GFP_ATOMIC);
4209 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4212 vhdr = (struct vlan_hdr *)skb->data;
4213 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4214 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4216 skb_pull_rcsum(skb, VLAN_HLEN);
4217 vlan_set_encap_proto(skb, vhdr);
4219 skb = skb_reorder_vlan_header(skb);
4223 skb_reset_network_header(skb);
4224 skb_reset_transport_header(skb);
4225 skb_reset_mac_len(skb);
4233 EXPORT_SYMBOL(skb_vlan_untag);
4235 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4237 if (!pskb_may_pull(skb, write_len))
4240 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4243 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4245 EXPORT_SYMBOL(skb_ensure_writable);
4247 /* remove VLAN header from packet and update csum accordingly. */
4248 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4250 struct vlan_hdr *vhdr;
4251 unsigned int offset = skb->data - skb_mac_header(skb);
4254 __skb_push(skb, offset);
4255 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4259 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4261 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4262 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4264 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4265 __skb_pull(skb, VLAN_HLEN);
4267 vlan_set_encap_proto(skb, vhdr);
4268 skb->mac_header += VLAN_HLEN;
4270 if (skb_network_offset(skb) < ETH_HLEN)
4271 skb_set_network_header(skb, ETH_HLEN);
4273 skb_reset_mac_len(skb);
4275 __skb_pull(skb, offset);
4280 int skb_vlan_pop(struct sk_buff *skb)
4286 if (likely(vlan_tx_tag_present(skb))) {
4289 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4290 skb->protocol != htons(ETH_P_8021AD)) ||
4291 skb->len < VLAN_ETH_HLEN))
4294 err = __skb_vlan_pop(skb, &vlan_tci);
4298 /* move next vlan tag to hw accel tag */
4299 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4300 skb->protocol != htons(ETH_P_8021AD)) ||
4301 skb->len < VLAN_ETH_HLEN))
4304 vlan_proto = skb->protocol;
4305 err = __skb_vlan_pop(skb, &vlan_tci);
4309 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4312 EXPORT_SYMBOL(skb_vlan_pop);
4314 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4316 if (vlan_tx_tag_present(skb)) {
4317 unsigned int offset = skb->data - skb_mac_header(skb);
4320 /* __vlan_insert_tag expect skb->data pointing to mac header.
4321 * So change skb->data before calling it and change back to
4322 * original position later
4324 __skb_push(skb, offset);
4325 err = __vlan_insert_tag(skb, skb->vlan_proto,
4326 vlan_tx_tag_get(skb));
4329 skb->protocol = skb->vlan_proto;
4330 skb->mac_len += VLAN_HLEN;
4331 __skb_pull(skb, offset);
4333 if (skb->ip_summed == CHECKSUM_COMPLETE)
4334 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4335 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4337 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4340 EXPORT_SYMBOL(skb_vlan_push);
4343 * alloc_skb_with_frags - allocate skb with page frags
4345 * @header_len: size of linear part
4346 * @data_len: needed length in frags
4347 * @max_page_order: max page order desired.
4348 * @errcode: pointer to error code if any
4349 * @gfp_mask: allocation mask
4351 * This can be used to allocate a paged skb, given a maximal order for frags.
4353 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4354 unsigned long data_len,
4359 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4360 unsigned long chunk;
4361 struct sk_buff *skb;
4366 *errcode = -EMSGSIZE;
4367 /* Note this test could be relaxed, if we succeed to allocate
4368 * high order pages...
4370 if (npages > MAX_SKB_FRAGS)
4373 gfp_head = gfp_mask;
4374 if (gfp_head & __GFP_WAIT)
4375 gfp_head |= __GFP_REPEAT;
4377 *errcode = -ENOBUFS;
4378 skb = alloc_skb(header_len, gfp_head);
4382 skb->truesize += npages << PAGE_SHIFT;
4384 for (i = 0; npages > 0; i++) {
4385 int order = max_page_order;
4388 if (npages >= 1 << order) {
4389 page = alloc_pages(gfp_mask |
4396 /* Do not retry other high order allocations */
4402 page = alloc_page(gfp_mask);
4406 chunk = min_t(unsigned long, data_len,
4407 PAGE_SIZE << order);
4408 skb_fill_page_desc(skb, i, page, 0, chunk);
4410 npages -= 1 << order;
4418 EXPORT_SYMBOL(alloc_skb_with_frags);