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/sctp.h>
53 #include <linux/netdevice.h>
54 #ifdef CONFIG_NET_CLS_ACT
55 #include <net/pkt_sched.h>
57 #include <linux/string.h>
58 #include <linux/skbuff.h>
59 #include <linux/splice.h>
60 #include <linux/cache.h>
61 #include <linux/rtnetlink.h>
62 #include <linux/init.h>
63 #include <linux/scatterlist.h>
64 #include <linux/errqueue.h>
65 #include <linux/prefetch.h>
66 #include <linux/if_vlan.h>
68 #include <net/protocol.h>
71 #include <net/checksum.h>
72 #include <net/ip6_checksum.h>
75 #include <asm/uaccess.h>
76 #include <trace/events/skb.h>
77 #include <linux/highmem.h>
78 #include <linux/capability.h>
79 #include <linux/user_namespace.h>
81 struct kmem_cache *skbuff_head_cache __read_mostly;
82 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
83 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
84 EXPORT_SYMBOL(sysctl_max_skb_frags);
87 * skb_panic - private function for out-of-line support
91 * @msg: skb_over_panic or skb_under_panic
93 * Out-of-line support for skb_put() and skb_push().
94 * Called via the wrapper skb_over_panic() or skb_under_panic().
95 * Keep out of line to prevent kernel bloat.
96 * __builtin_return_address is not used because it is not always reliable.
98 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
101 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
102 msg, addr, skb->len, sz, skb->head, skb->data,
103 (unsigned long)skb->tail, (unsigned long)skb->end,
104 skb->dev ? skb->dev->name : "<NULL>");
108 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
113 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
115 skb_panic(skb, sz, addr, __func__);
119 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120 * the caller if emergency pfmemalloc reserves are being used. If it is and
121 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122 * may be used. Otherwise, the packet data may be discarded until enough
125 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
128 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
129 unsigned long ip, bool *pfmemalloc)
132 bool ret_pfmemalloc = false;
135 * Try a regular allocation, when that fails and we're not entitled
136 * to the reserves, fail.
138 obj = kmalloc_node_track_caller(size,
139 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
141 if (obj || !(gfp_pfmemalloc_allowed(flags)))
144 /* Try again but now we are using pfmemalloc reserves */
145 ret_pfmemalloc = true;
146 obj = kmalloc_node_track_caller(size, flags, node);
150 *pfmemalloc = ret_pfmemalloc;
155 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
156 * 'private' fields and also do memory statistics to find all the
161 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
166 skb = kmem_cache_alloc_node(skbuff_head_cache,
167 gfp_mask & ~__GFP_DMA, node);
172 * Only clear those fields we need to clear, not those that we will
173 * actually initialise below. Hence, don't put any more fields after
174 * the tail pointer in struct sk_buff!
176 memset(skb, 0, offsetof(struct sk_buff, tail));
178 skb->truesize = sizeof(struct sk_buff);
179 atomic_set(&skb->users, 1);
181 skb->mac_header = (typeof(skb->mac_header))~0U;
187 * __alloc_skb - allocate a network buffer
188 * @size: size to allocate
189 * @gfp_mask: allocation mask
190 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191 * instead of head cache and allocate a cloned (child) skb.
192 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193 * allocations in case the data is required for writeback
194 * @node: numa node to allocate memory on
196 * Allocate a new &sk_buff. The returned buffer has no headroom and a
197 * tail room of at least size bytes. The object has a reference count
198 * of one. The return is the buffer. On a failure the return is %NULL.
200 * Buffers may only be allocated from interrupts using a @gfp_mask of
203 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
206 struct kmem_cache *cache;
207 struct skb_shared_info *shinfo;
212 cache = (flags & SKB_ALLOC_FCLONE)
213 ? skbuff_fclone_cache : skbuff_head_cache;
215 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216 gfp_mask |= __GFP_MEMALLOC;
219 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
224 /* We do our best to align skb_shared_info on a separate cache
225 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
226 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
227 * Both skb->head and skb_shared_info are cache line aligned.
229 size = SKB_DATA_ALIGN(size);
230 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
231 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
234 /* kmalloc(size) might give us more room than requested.
235 * Put skb_shared_info exactly at the end of allocated zone,
236 * to allow max possible filling before reallocation.
238 size = SKB_WITH_OVERHEAD(ksize(data));
239 prefetchw(data + size);
242 * Only clear those fields we need to clear, not those that we will
243 * actually initialise below. Hence, don't put any more fields after
244 * the tail pointer in struct sk_buff!
246 memset(skb, 0, offsetof(struct sk_buff, tail));
247 /* Account for allocated memory : skb + skb->head */
248 skb->truesize = SKB_TRUESIZE(size);
249 skb->pfmemalloc = pfmemalloc;
250 atomic_set(&skb->users, 1);
253 skb_reset_tail_pointer(skb);
254 skb->end = skb->tail + size;
255 skb->mac_header = (typeof(skb->mac_header))~0U;
256 skb->transport_header = (typeof(skb->transport_header))~0U;
258 /* make sure we initialize shinfo sequentially */
259 shinfo = skb_shinfo(skb);
260 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
261 atomic_set(&shinfo->dataref, 1);
262 kmemcheck_annotate_variable(shinfo->destructor_arg);
264 if (flags & SKB_ALLOC_FCLONE) {
265 struct sk_buff_fclones *fclones;
267 fclones = container_of(skb, struct sk_buff_fclones, skb1);
269 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
270 skb->fclone = SKB_FCLONE_ORIG;
271 atomic_set(&fclones->fclone_ref, 1);
273 fclones->skb2.fclone = SKB_FCLONE_CLONE;
274 fclones->skb2.pfmemalloc = pfmemalloc;
279 kmem_cache_free(cache, skb);
283 EXPORT_SYMBOL(__alloc_skb);
286 * __build_skb - build a network buffer
287 * @data: data buffer provided by caller
288 * @frag_size: size of data, or 0 if head was kmalloced
290 * Allocate a new &sk_buff. Caller provides space holding head and
291 * skb_shared_info. @data must have been allocated by kmalloc() only if
292 * @frag_size is 0, otherwise data should come from the page allocator
294 * The return is the new skb buffer.
295 * On a failure the return is %NULL, and @data is not freed.
297 * Before IO, driver allocates only data buffer where NIC put incoming frame
298 * Driver should add room at head (NET_SKB_PAD) and
299 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
300 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
301 * before giving packet to stack.
302 * RX rings only contains data buffers, not full skbs.
304 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
306 struct skb_shared_info *shinfo;
308 unsigned int size = frag_size ? : ksize(data);
310 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
314 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
316 memset(skb, 0, offsetof(struct sk_buff, tail));
317 skb->truesize = SKB_TRUESIZE(size);
318 atomic_set(&skb->users, 1);
321 skb_reset_tail_pointer(skb);
322 skb->end = skb->tail + size;
323 skb->mac_header = (typeof(skb->mac_header))~0U;
324 skb->transport_header = (typeof(skb->transport_header))~0U;
326 /* make sure we initialize shinfo sequentially */
327 shinfo = skb_shinfo(skb);
328 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
329 atomic_set(&shinfo->dataref, 1);
330 kmemcheck_annotate_variable(shinfo->destructor_arg);
335 /* build_skb() is wrapper over __build_skb(), that specifically
336 * takes care of skb->head and skb->pfmemalloc
337 * This means that if @frag_size is not zero, then @data must be backed
338 * by a page fragment, not kmalloc() or vmalloc()
340 struct sk_buff *build_skb(void *data, unsigned int frag_size)
342 struct sk_buff *skb = __build_skb(data, frag_size);
344 if (skb && frag_size) {
346 if (page_is_pfmemalloc(virt_to_head_page(data)))
351 EXPORT_SYMBOL(build_skb);
353 #define NAPI_SKB_CACHE_SIZE 64
355 struct napi_alloc_cache {
356 struct page_frag_cache page;
358 void *skb_cache[NAPI_SKB_CACHE_SIZE];
361 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
362 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
364 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
366 struct page_frag_cache *nc;
370 local_irq_save(flags);
371 nc = this_cpu_ptr(&netdev_alloc_cache);
372 data = __alloc_page_frag(nc, fragsz, gfp_mask);
373 local_irq_restore(flags);
378 * netdev_alloc_frag - allocate a page fragment
379 * @fragsz: fragment size
381 * Allocates a frag from a page for receive buffer.
382 * Uses GFP_ATOMIC allocations.
384 void *netdev_alloc_frag(unsigned int fragsz)
386 fragsz = SKB_DATA_ALIGN(fragsz);
388 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
390 EXPORT_SYMBOL(netdev_alloc_frag);
392 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
394 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
396 return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
399 void *napi_alloc_frag(unsigned int fragsz)
401 fragsz = SKB_DATA_ALIGN(fragsz);
403 return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
405 EXPORT_SYMBOL(napi_alloc_frag);
408 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
409 * @dev: network device to receive on
410 * @len: length to allocate
411 * @gfp_mask: get_free_pages mask, passed to alloc_skb
413 * Allocate a new &sk_buff and assign it a usage count of one. The
414 * buffer has NET_SKB_PAD headroom built in. Users should allocate
415 * the headroom they think they need without accounting for the
416 * built in space. The built in space is used for optimisations.
418 * %NULL is returned if there is no free memory.
420 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
423 struct page_frag_cache *nc;
431 /* If requested length is either too small or too big,
432 * we use kmalloc() for skb->head allocation.
434 if (len <= SKB_WITH_OVERHEAD(1024) ||
435 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
436 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
437 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
443 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
444 len = SKB_DATA_ALIGN(len);
446 if (sk_memalloc_socks())
447 gfp_mask |= __GFP_MEMALLOC;
449 local_irq_save(flags);
451 nc = this_cpu_ptr(&netdev_alloc_cache);
452 data = __alloc_page_frag(nc, len, gfp_mask);
453 pfmemalloc = nc->pfmemalloc;
455 local_irq_restore(flags);
460 skb = __build_skb(data, len);
461 if (unlikely(!skb)) {
466 /* use OR instead of assignment to avoid clearing of bits in mask */
472 skb_reserve(skb, NET_SKB_PAD);
478 EXPORT_SYMBOL(__netdev_alloc_skb);
481 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
482 * @napi: napi instance this buffer was allocated for
483 * @len: length to allocate
484 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
486 * Allocate a new sk_buff for use in NAPI receive. This buffer will
487 * attempt to allocate the head from a special reserved region used
488 * only for NAPI Rx allocation. By doing this we can save several
489 * CPU cycles by avoiding having to disable and re-enable IRQs.
491 * %NULL is returned if there is no free memory.
493 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
496 struct napi_alloc_cache *nc;
500 len += NET_SKB_PAD + NET_IP_ALIGN;
502 /* If requested length is either too small or too big,
503 * we use kmalloc() for skb->head allocation.
505 if (len <= SKB_WITH_OVERHEAD(1024) ||
506 len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
507 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
508 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
514 nc = this_cpu_ptr(&napi_alloc_cache);
515 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
516 len = SKB_DATA_ALIGN(len);
518 if (sk_memalloc_socks())
519 gfp_mask |= __GFP_MEMALLOC;
521 data = __alloc_page_frag(&nc->page, len, gfp_mask);
525 skb = __build_skb(data, len);
526 if (unlikely(!skb)) {
531 /* use OR instead of assignment to avoid clearing of bits in mask */
532 if (nc->page.pfmemalloc)
537 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
538 skb->dev = napi->dev;
543 EXPORT_SYMBOL(__napi_alloc_skb);
545 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
546 int size, unsigned int truesize)
548 skb_fill_page_desc(skb, i, page, off, size);
550 skb->data_len += size;
551 skb->truesize += truesize;
553 EXPORT_SYMBOL(skb_add_rx_frag);
555 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
556 unsigned int truesize)
558 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
560 skb_frag_size_add(frag, size);
562 skb->data_len += size;
563 skb->truesize += truesize;
565 EXPORT_SYMBOL(skb_coalesce_rx_frag);
567 static void skb_drop_list(struct sk_buff **listp)
569 kfree_skb_list(*listp);
573 static inline void skb_drop_fraglist(struct sk_buff *skb)
575 skb_drop_list(&skb_shinfo(skb)->frag_list);
578 static void skb_clone_fraglist(struct sk_buff *skb)
580 struct sk_buff *list;
582 skb_walk_frags(skb, list)
586 static void skb_free_head(struct sk_buff *skb)
588 unsigned char *head = skb->head;
596 static void skb_release_data(struct sk_buff *skb)
598 struct skb_shared_info *shinfo = skb_shinfo(skb);
602 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
606 for (i = 0; i < shinfo->nr_frags; i++)
607 __skb_frag_unref(&shinfo->frags[i]);
610 * If skb buf is from userspace, we need to notify the caller
611 * the lower device DMA has done;
613 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
614 struct ubuf_info *uarg;
616 uarg = shinfo->destructor_arg;
618 uarg->callback(uarg, true);
621 if (shinfo->frag_list)
622 kfree_skb_list(shinfo->frag_list);
628 * Free an skbuff by memory without cleaning the state.
630 static void kfree_skbmem(struct sk_buff *skb)
632 struct sk_buff_fclones *fclones;
634 switch (skb->fclone) {
635 case SKB_FCLONE_UNAVAILABLE:
636 kmem_cache_free(skbuff_head_cache, skb);
639 case SKB_FCLONE_ORIG:
640 fclones = container_of(skb, struct sk_buff_fclones, skb1);
642 /* We usually free the clone (TX completion) before original skb
643 * This test would have no chance to be true for the clone,
644 * while here, branch prediction will be good.
646 if (atomic_read(&fclones->fclone_ref) == 1)
650 default: /* SKB_FCLONE_CLONE */
651 fclones = container_of(skb, struct sk_buff_fclones, skb2);
654 if (!atomic_dec_and_test(&fclones->fclone_ref))
657 kmem_cache_free(skbuff_fclone_cache, fclones);
660 static void skb_release_head_state(struct sk_buff *skb)
664 secpath_put(skb->sp);
666 if (skb->destructor) {
668 skb->destructor(skb);
670 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
671 nf_conntrack_put(skb->nfct);
673 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
674 nf_bridge_put(skb->nf_bridge);
678 /* Free everything but the sk_buff shell. */
679 static void skb_release_all(struct sk_buff *skb)
681 skb_release_head_state(skb);
682 if (likely(skb->head))
683 skb_release_data(skb);
687 * __kfree_skb - private function
690 * Free an sk_buff. Release anything attached to the buffer.
691 * Clean the state. This is an internal helper function. Users should
692 * always call kfree_skb
695 void __kfree_skb(struct sk_buff *skb)
697 skb_release_all(skb);
700 EXPORT_SYMBOL(__kfree_skb);
703 * kfree_skb - free an sk_buff
704 * @skb: buffer to free
706 * Drop a reference to the buffer and free it if the usage count has
709 void kfree_skb(struct sk_buff *skb)
713 if (likely(atomic_read(&skb->users) == 1))
715 else if (likely(!atomic_dec_and_test(&skb->users)))
717 trace_kfree_skb(skb, __builtin_return_address(0));
720 EXPORT_SYMBOL(kfree_skb);
722 void kfree_skb_list(struct sk_buff *segs)
725 struct sk_buff *next = segs->next;
731 EXPORT_SYMBOL(kfree_skb_list);
734 * skb_tx_error - report an sk_buff xmit error
735 * @skb: buffer that triggered an error
737 * Report xmit error if a device callback is tracking this skb.
738 * skb must be freed afterwards.
740 void skb_tx_error(struct sk_buff *skb)
742 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
743 struct ubuf_info *uarg;
745 uarg = skb_shinfo(skb)->destructor_arg;
747 uarg->callback(uarg, false);
748 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
751 EXPORT_SYMBOL(skb_tx_error);
754 * consume_skb - free an skbuff
755 * @skb: buffer to free
757 * Drop a ref to the buffer and free it if the usage count has hit zero
758 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
759 * is being dropped after a failure and notes that
761 void consume_skb(struct sk_buff *skb)
765 if (likely(atomic_read(&skb->users) == 1))
767 else if (likely(!atomic_dec_and_test(&skb->users)))
769 trace_consume_skb(skb);
772 EXPORT_SYMBOL(consume_skb);
774 void __kfree_skb_flush(void)
776 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
778 /* flush skb_cache if containing objects */
780 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
786 static inline void _kfree_skb_defer(struct sk_buff *skb)
788 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
790 /* drop skb->head and call any destructors for packet */
791 skb_release_all(skb);
793 /* record skb to CPU local list */
794 nc->skb_cache[nc->skb_count++] = skb;
797 /* SLUB writes into objects when freeing */
801 /* flush skb_cache if it is filled */
802 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
803 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
808 void __kfree_skb_defer(struct sk_buff *skb)
810 _kfree_skb_defer(skb);
813 void napi_consume_skb(struct sk_buff *skb, int budget)
818 /* Zero budget indicate non-NAPI context called us, like netpoll */
819 if (unlikely(!budget)) {
820 dev_consume_skb_any(skb);
824 if (likely(atomic_read(&skb->users) == 1))
826 else if (likely(!atomic_dec_and_test(&skb->users)))
828 /* if reaching here SKB is ready to free */
829 trace_consume_skb(skb);
831 /* if SKB is a clone, don't handle this case */
832 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
837 _kfree_skb_defer(skb);
839 EXPORT_SYMBOL(napi_consume_skb);
841 /* Make sure a field is enclosed inside headers_start/headers_end section */
842 #define CHECK_SKB_FIELD(field) \
843 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
844 offsetof(struct sk_buff, headers_start)); \
845 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
846 offsetof(struct sk_buff, headers_end)); \
848 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
850 new->tstamp = old->tstamp;
851 /* We do not copy old->sk */
853 memcpy(new->cb, old->cb, sizeof(old->cb));
854 skb_dst_copy(new, old);
856 new->sp = secpath_get(old->sp);
858 __nf_copy(new, old, false);
860 /* Note : this field could be in headers_start/headers_end section
861 * It is not yet because we do not want to have a 16 bit hole
863 new->queue_mapping = old->queue_mapping;
865 memcpy(&new->headers_start, &old->headers_start,
866 offsetof(struct sk_buff, headers_end) -
867 offsetof(struct sk_buff, headers_start));
868 CHECK_SKB_FIELD(protocol);
869 CHECK_SKB_FIELD(csum);
870 CHECK_SKB_FIELD(hash);
871 CHECK_SKB_FIELD(priority);
872 CHECK_SKB_FIELD(skb_iif);
873 CHECK_SKB_FIELD(vlan_proto);
874 CHECK_SKB_FIELD(vlan_tci);
875 CHECK_SKB_FIELD(transport_header);
876 CHECK_SKB_FIELD(network_header);
877 CHECK_SKB_FIELD(mac_header);
878 CHECK_SKB_FIELD(inner_protocol);
879 CHECK_SKB_FIELD(inner_transport_header);
880 CHECK_SKB_FIELD(inner_network_header);
881 CHECK_SKB_FIELD(inner_mac_header);
882 CHECK_SKB_FIELD(mark);
883 #ifdef CONFIG_NETWORK_SECMARK
884 CHECK_SKB_FIELD(secmark);
886 #ifdef CONFIG_NET_RX_BUSY_POLL
887 CHECK_SKB_FIELD(napi_id);
890 CHECK_SKB_FIELD(sender_cpu);
892 #ifdef CONFIG_NET_SCHED
893 CHECK_SKB_FIELD(tc_index);
894 #ifdef CONFIG_NET_CLS_ACT
895 CHECK_SKB_FIELD(tc_verd);
902 * You should not add any new code to this function. Add it to
903 * __copy_skb_header above instead.
905 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
907 #define C(x) n->x = skb->x
909 n->next = n->prev = NULL;
911 __copy_skb_header(n, skb);
916 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
921 n->destructor = NULL;
928 atomic_set(&n->users, 1);
930 atomic_inc(&(skb_shinfo(skb)->dataref));
938 * skb_morph - morph one skb into another
939 * @dst: the skb to receive the contents
940 * @src: the skb to supply the contents
942 * This is identical to skb_clone except that the target skb is
943 * supplied by the user.
945 * The target skb is returned upon exit.
947 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
949 skb_release_all(dst);
950 return __skb_clone(dst, src);
952 EXPORT_SYMBOL_GPL(skb_morph);
955 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
956 * @skb: the skb to modify
957 * @gfp_mask: allocation priority
959 * This must be called on SKBTX_DEV_ZEROCOPY skb.
960 * It will copy all frags into kernel and drop the reference
961 * to userspace pages.
963 * If this function is called from an interrupt gfp_mask() must be
966 * Returns 0 on success or a negative error code on failure
967 * to allocate kernel memory to copy to.
969 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
972 int num_frags = skb_shinfo(skb)->nr_frags;
973 struct page *page, *head = NULL;
974 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
976 for (i = 0; i < num_frags; i++) {
978 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
980 page = alloc_page(gfp_mask);
983 struct page *next = (struct page *)page_private(head);
989 vaddr = kmap_atomic(skb_frag_page(f));
990 memcpy(page_address(page),
991 vaddr + f->page_offset, skb_frag_size(f));
992 kunmap_atomic(vaddr);
993 set_page_private(page, (unsigned long)head);
997 /* skb frags release userspace buffers */
998 for (i = 0; i < num_frags; i++)
999 skb_frag_unref(skb, i);
1001 uarg->callback(uarg, false);
1003 /* skb frags point to kernel buffers */
1004 for (i = num_frags - 1; i >= 0; i--) {
1005 __skb_fill_page_desc(skb, i, head, 0,
1006 skb_shinfo(skb)->frags[i].size);
1007 head = (struct page *)page_private(head);
1010 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
1013 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1016 * skb_clone - duplicate an sk_buff
1017 * @skb: buffer to clone
1018 * @gfp_mask: allocation priority
1020 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
1021 * copies share the same packet data but not structure. The new
1022 * buffer has a reference count of 1. If the allocation fails the
1023 * function returns %NULL otherwise the new buffer is returned.
1025 * If this function is called from an interrupt gfp_mask() must be
1029 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1031 struct sk_buff_fclones *fclones = container_of(skb,
1032 struct sk_buff_fclones,
1036 if (skb_orphan_frags(skb, gfp_mask))
1039 if (skb->fclone == SKB_FCLONE_ORIG &&
1040 atomic_read(&fclones->fclone_ref) == 1) {
1042 atomic_set(&fclones->fclone_ref, 2);
1044 if (skb_pfmemalloc(skb))
1045 gfp_mask |= __GFP_MEMALLOC;
1047 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1051 kmemcheck_annotate_bitfield(n, flags1);
1052 n->fclone = SKB_FCLONE_UNAVAILABLE;
1055 return __skb_clone(n, skb);
1057 EXPORT_SYMBOL(skb_clone);
1059 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1061 /* Only adjust this if it actually is csum_start rather than csum */
1062 if (skb->ip_summed == CHECKSUM_PARTIAL)
1063 skb->csum_start += off;
1064 /* {transport,network,mac}_header and tail are relative to skb->head */
1065 skb->transport_header += off;
1066 skb->network_header += off;
1067 if (skb_mac_header_was_set(skb))
1068 skb->mac_header += off;
1069 skb->inner_transport_header += off;
1070 skb->inner_network_header += off;
1071 skb->inner_mac_header += off;
1074 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1076 __copy_skb_header(new, old);
1078 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1079 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1080 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1083 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1085 if (skb_pfmemalloc(skb))
1086 return SKB_ALLOC_RX;
1091 * skb_copy - create private copy of an sk_buff
1092 * @skb: buffer to copy
1093 * @gfp_mask: allocation priority
1095 * Make a copy of both an &sk_buff and its data. This is used when the
1096 * caller wishes to modify the data and needs a private copy of the
1097 * data to alter. Returns %NULL on failure or the pointer to the buffer
1098 * on success. The returned buffer has a reference count of 1.
1100 * As by-product this function converts non-linear &sk_buff to linear
1101 * one, so that &sk_buff becomes completely private and caller is allowed
1102 * to modify all the data of returned buffer. This means that this
1103 * function is not recommended for use in circumstances when only
1104 * header is going to be modified. Use pskb_copy() instead.
1107 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1109 int headerlen = skb_headroom(skb);
1110 unsigned int size = skb_end_offset(skb) + skb->data_len;
1111 struct sk_buff *n = __alloc_skb(size, gfp_mask,
1112 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1117 /* Set the data pointer */
1118 skb_reserve(n, headerlen);
1119 /* Set the tail pointer and length */
1120 skb_put(n, skb->len);
1122 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1125 copy_skb_header(n, skb);
1128 EXPORT_SYMBOL(skb_copy);
1131 * __pskb_copy_fclone - create copy of an sk_buff with private head.
1132 * @skb: buffer to copy
1133 * @headroom: headroom of new skb
1134 * @gfp_mask: allocation priority
1135 * @fclone: if true allocate the copy of the skb from the fclone
1136 * cache instead of the head cache; it is recommended to set this
1137 * to true for the cases where the copy will likely be cloned
1139 * Make a copy of both an &sk_buff and part of its data, located
1140 * in header. Fragmented data remain shared. This is used when
1141 * the caller wishes to modify only header of &sk_buff and needs
1142 * private copy of the header to alter. Returns %NULL on failure
1143 * or the pointer to the buffer on success.
1144 * The returned buffer has a reference count of 1.
1147 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1148 gfp_t gfp_mask, bool fclone)
1150 unsigned int size = skb_headlen(skb) + headroom;
1151 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1152 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1157 /* Set the data pointer */
1158 skb_reserve(n, headroom);
1159 /* Set the tail pointer and length */
1160 skb_put(n, skb_headlen(skb));
1161 /* Copy the bytes */
1162 skb_copy_from_linear_data(skb, n->data, n->len);
1164 n->truesize += skb->data_len;
1165 n->data_len = skb->data_len;
1168 if (skb_shinfo(skb)->nr_frags) {
1171 if (skb_orphan_frags(skb, gfp_mask)) {
1176 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1177 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1178 skb_frag_ref(skb, i);
1180 skb_shinfo(n)->nr_frags = i;
1183 if (skb_has_frag_list(skb)) {
1184 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1185 skb_clone_fraglist(n);
1188 copy_skb_header(n, skb);
1192 EXPORT_SYMBOL(__pskb_copy_fclone);
1195 * pskb_expand_head - reallocate header of &sk_buff
1196 * @skb: buffer to reallocate
1197 * @nhead: room to add at head
1198 * @ntail: room to add at tail
1199 * @gfp_mask: allocation priority
1201 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1202 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1203 * reference count of 1. Returns zero in the case of success or error,
1204 * if expansion failed. In the last case, &sk_buff is not changed.
1206 * All the pointers pointing into skb header may change and must be
1207 * reloaded after call to this function.
1210 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1215 int size = nhead + skb_end_offset(skb) + ntail;
1220 if (skb_shared(skb))
1223 size = SKB_DATA_ALIGN(size);
1225 if (skb_pfmemalloc(skb))
1226 gfp_mask |= __GFP_MEMALLOC;
1227 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1228 gfp_mask, NUMA_NO_NODE, NULL);
1231 size = SKB_WITH_OVERHEAD(ksize(data));
1233 /* Copy only real data... and, alas, header. This should be
1234 * optimized for the cases when header is void.
1236 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1238 memcpy((struct skb_shared_info *)(data + size),
1240 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1243 * if shinfo is shared we must drop the old head gracefully, but if it
1244 * is not we can just drop the old head and let the existing refcount
1245 * be since all we did is relocate the values
1247 if (skb_cloned(skb)) {
1248 /* copy this zero copy skb frags */
1249 if (skb_orphan_frags(skb, gfp_mask))
1251 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1252 skb_frag_ref(skb, i);
1254 if (skb_has_frag_list(skb))
1255 skb_clone_fraglist(skb);
1257 skb_release_data(skb);
1261 off = (data + nhead) - skb->head;
1266 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1270 skb->end = skb->head + size;
1273 skb_headers_offset_update(skb, nhead);
1277 atomic_set(&skb_shinfo(skb)->dataref, 1);
1285 EXPORT_SYMBOL(pskb_expand_head);
1287 /* Make private copy of skb with writable head and some headroom */
1289 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1291 struct sk_buff *skb2;
1292 int delta = headroom - skb_headroom(skb);
1295 skb2 = pskb_copy(skb, GFP_ATOMIC);
1297 skb2 = skb_clone(skb, GFP_ATOMIC);
1298 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1306 EXPORT_SYMBOL(skb_realloc_headroom);
1309 * skb_copy_expand - copy and expand sk_buff
1310 * @skb: buffer to copy
1311 * @newheadroom: new free bytes at head
1312 * @newtailroom: new free bytes at tail
1313 * @gfp_mask: allocation priority
1315 * Make a copy of both an &sk_buff and its data and while doing so
1316 * allocate additional space.
1318 * This is used when the caller wishes to modify the data and needs a
1319 * private copy of the data to alter as well as more space for new fields.
1320 * Returns %NULL on failure or the pointer to the buffer
1321 * on success. The returned buffer has a reference count of 1.
1323 * You must pass %GFP_ATOMIC as the allocation priority if this function
1324 * is called from an interrupt.
1326 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1327 int newheadroom, int newtailroom,
1331 * Allocate the copy buffer
1333 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1334 gfp_mask, skb_alloc_rx_flag(skb),
1336 int oldheadroom = skb_headroom(skb);
1337 int head_copy_len, head_copy_off;
1342 skb_reserve(n, newheadroom);
1344 /* Set the tail pointer and length */
1345 skb_put(n, skb->len);
1347 head_copy_len = oldheadroom;
1349 if (newheadroom <= head_copy_len)
1350 head_copy_len = newheadroom;
1352 head_copy_off = newheadroom - head_copy_len;
1354 /* Copy the linear header and data. */
1355 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1356 skb->len + head_copy_len))
1359 copy_skb_header(n, skb);
1361 skb_headers_offset_update(n, newheadroom - oldheadroom);
1365 EXPORT_SYMBOL(skb_copy_expand);
1368 * skb_pad - zero pad the tail of an skb
1369 * @skb: buffer to pad
1370 * @pad: space to pad
1372 * Ensure that a buffer is followed by a padding area that is zero
1373 * filled. Used by network drivers which may DMA or transfer data
1374 * beyond the buffer end onto the wire.
1376 * May return error in out of memory cases. The skb is freed on error.
1379 int skb_pad(struct sk_buff *skb, int pad)
1384 /* If the skbuff is non linear tailroom is always zero.. */
1385 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1386 memset(skb->data+skb->len, 0, pad);
1390 ntail = skb->data_len + pad - (skb->end - skb->tail);
1391 if (likely(skb_cloned(skb) || ntail > 0)) {
1392 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1397 /* FIXME: The use of this function with non-linear skb's really needs
1400 err = skb_linearize(skb);
1404 memset(skb->data + skb->len, 0, pad);
1411 EXPORT_SYMBOL(skb_pad);
1414 * pskb_put - add data to the tail of a potentially fragmented buffer
1415 * @skb: start of the buffer to use
1416 * @tail: tail fragment of the buffer to use
1417 * @len: amount of data to add
1419 * This function extends the used data area of the potentially
1420 * fragmented buffer. @tail must be the last fragment of @skb -- or
1421 * @skb itself. If this would exceed the total buffer size the kernel
1422 * will panic. A pointer to the first byte of the extra data is
1426 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1429 skb->data_len += len;
1432 return skb_put(tail, len);
1434 EXPORT_SYMBOL_GPL(pskb_put);
1437 * skb_put - add data to a buffer
1438 * @skb: buffer to use
1439 * @len: amount of data to add
1441 * This function extends the used data area of the buffer. If this would
1442 * exceed the total buffer size the kernel will panic. A pointer to the
1443 * first byte of the extra data is returned.
1445 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1447 unsigned char *tmp = skb_tail_pointer(skb);
1448 SKB_LINEAR_ASSERT(skb);
1451 if (unlikely(skb->tail > skb->end))
1452 skb_over_panic(skb, len, __builtin_return_address(0));
1455 EXPORT_SYMBOL(skb_put);
1458 * skb_push - add data to the start of a buffer
1459 * @skb: buffer to use
1460 * @len: amount of data to add
1462 * This function extends the used data area of the buffer at the buffer
1463 * start. If this would exceed the total buffer headroom the kernel will
1464 * panic. A pointer to the first byte of the extra data is returned.
1466 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1470 if (unlikely(skb->data<skb->head))
1471 skb_under_panic(skb, len, __builtin_return_address(0));
1474 EXPORT_SYMBOL(skb_push);
1477 * skb_pull - remove data from the start of a buffer
1478 * @skb: buffer to use
1479 * @len: amount of data to remove
1481 * This function removes data from the start of a buffer, returning
1482 * the memory to the headroom. A pointer to the next data in the buffer
1483 * is returned. Once the data has been pulled future pushes will overwrite
1486 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1488 return skb_pull_inline(skb, len);
1490 EXPORT_SYMBOL(skb_pull);
1493 * skb_trim - remove end from a buffer
1494 * @skb: buffer to alter
1497 * Cut the length of a buffer down by removing data from the tail. If
1498 * the buffer is already under the length specified it is not modified.
1499 * The skb must be linear.
1501 void skb_trim(struct sk_buff *skb, unsigned int len)
1504 __skb_trim(skb, len);
1506 EXPORT_SYMBOL(skb_trim);
1508 /* Trims skb to length len. It can change skb pointers.
1511 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1513 struct sk_buff **fragp;
1514 struct sk_buff *frag;
1515 int offset = skb_headlen(skb);
1516 int nfrags = skb_shinfo(skb)->nr_frags;
1520 if (skb_cloned(skb) &&
1521 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1528 for (; i < nfrags; i++) {
1529 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1536 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1539 skb_shinfo(skb)->nr_frags = i;
1541 for (; i < nfrags; i++)
1542 skb_frag_unref(skb, i);
1544 if (skb_has_frag_list(skb))
1545 skb_drop_fraglist(skb);
1549 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1550 fragp = &frag->next) {
1551 int end = offset + frag->len;
1553 if (skb_shared(frag)) {
1554 struct sk_buff *nfrag;
1556 nfrag = skb_clone(frag, GFP_ATOMIC);
1557 if (unlikely(!nfrag))
1560 nfrag->next = frag->next;
1572 unlikely((err = pskb_trim(frag, len - offset))))
1576 skb_drop_list(&frag->next);
1581 if (len > skb_headlen(skb)) {
1582 skb->data_len -= skb->len - len;
1587 skb_set_tail_pointer(skb, len);
1592 EXPORT_SYMBOL(___pskb_trim);
1594 /* Note : use pskb_trim_rcsum() instead of calling this directly
1596 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
1598 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1599 int delta = skb->len - len;
1601 skb->csum = csum_block_sub(skb->csum,
1602 skb_checksum(skb, len, delta, 0),
1604 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1605 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
1606 int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
1608 if (offset + sizeof(__sum16) > hdlen)
1611 return __pskb_trim(skb, len);
1613 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
1616 * __pskb_pull_tail - advance tail of skb header
1617 * @skb: buffer to reallocate
1618 * @delta: number of bytes to advance tail
1620 * The function makes a sense only on a fragmented &sk_buff,
1621 * it expands header moving its tail forward and copying necessary
1622 * data from fragmented part.
1624 * &sk_buff MUST have reference count of 1.
1626 * Returns %NULL (and &sk_buff does not change) if pull failed
1627 * or value of new tail of skb in the case of success.
1629 * All the pointers pointing into skb header may change and must be
1630 * reloaded after call to this function.
1633 /* Moves tail of skb head forward, copying data from fragmented part,
1634 * when it is necessary.
1635 * 1. It may fail due to malloc failure.
1636 * 2. It may change skb pointers.
1638 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1640 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1642 /* If skb has not enough free space at tail, get new one
1643 * plus 128 bytes for future expansions. If we have enough
1644 * room at tail, reallocate without expansion only if skb is cloned.
1646 int i, k, eat = (skb->tail + delta) - skb->end;
1648 if (eat > 0 || skb_cloned(skb)) {
1649 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1654 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1657 /* Optimization: no fragments, no reasons to preestimate
1658 * size of pulled pages. Superb.
1660 if (!skb_has_frag_list(skb))
1663 /* Estimate size of pulled pages. */
1665 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1666 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1673 /* If we need update frag list, we are in troubles.
1674 * Certainly, it possible to add an offset to skb data,
1675 * but taking into account that pulling is expected to
1676 * be very rare operation, it is worth to fight against
1677 * further bloating skb head and crucify ourselves here instead.
1678 * Pure masohism, indeed. 8)8)
1681 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1682 struct sk_buff *clone = NULL;
1683 struct sk_buff *insp = NULL;
1688 if (list->len <= eat) {
1689 /* Eaten as whole. */
1694 /* Eaten partially. */
1696 if (skb_shared(list)) {
1697 /* Sucks! We need to fork list. :-( */
1698 clone = skb_clone(list, GFP_ATOMIC);
1704 /* This may be pulled without
1708 if (!pskb_pull(list, eat)) {
1716 /* Free pulled out fragments. */
1717 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1718 skb_shinfo(skb)->frag_list = list->next;
1721 /* And insert new clone at head. */
1724 skb_shinfo(skb)->frag_list = clone;
1727 /* Success! Now we may commit changes to skb data. */
1732 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1733 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1736 skb_frag_unref(skb, i);
1739 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1741 skb_shinfo(skb)->frags[k].page_offset += eat;
1742 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1748 skb_shinfo(skb)->nr_frags = k;
1751 skb->data_len -= delta;
1753 return skb_tail_pointer(skb);
1755 EXPORT_SYMBOL(__pskb_pull_tail);
1758 * skb_copy_bits - copy bits from skb to kernel buffer
1760 * @offset: offset in source
1761 * @to: destination buffer
1762 * @len: number of bytes to copy
1764 * Copy the specified number of bytes from the source skb to the
1765 * destination buffer.
1768 * If its prototype is ever changed,
1769 * check arch/{*}/net/{*}.S files,
1770 * since it is called from BPF assembly code.
1772 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1774 int start = skb_headlen(skb);
1775 struct sk_buff *frag_iter;
1778 if (offset > (int)skb->len - len)
1782 if ((copy = start - offset) > 0) {
1785 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1786 if ((len -= copy) == 0)
1792 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1794 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1796 WARN_ON(start > offset + len);
1798 end = start + skb_frag_size(f);
1799 if ((copy = end - offset) > 0) {
1805 vaddr = kmap_atomic(skb_frag_page(f));
1807 vaddr + f->page_offset + offset - start,
1809 kunmap_atomic(vaddr);
1811 if ((len -= copy) == 0)
1819 skb_walk_frags(skb, frag_iter) {
1822 WARN_ON(start > offset + len);
1824 end = start + frag_iter->len;
1825 if ((copy = end - offset) > 0) {
1828 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1830 if ((len -= copy) == 0)
1844 EXPORT_SYMBOL(skb_copy_bits);
1847 * Callback from splice_to_pipe(), if we need to release some pages
1848 * at the end of the spd in case we error'ed out in filling the pipe.
1850 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1852 put_page(spd->pages[i]);
1855 static struct page *linear_to_page(struct page *page, unsigned int *len,
1856 unsigned int *offset,
1859 struct page_frag *pfrag = sk_page_frag(sk);
1861 if (!sk_page_frag_refill(sk, pfrag))
1864 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1866 memcpy(page_address(pfrag->page) + pfrag->offset,
1867 page_address(page) + *offset, *len);
1868 *offset = pfrag->offset;
1869 pfrag->offset += *len;
1874 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1876 unsigned int offset)
1878 return spd->nr_pages &&
1879 spd->pages[spd->nr_pages - 1] == page &&
1880 (spd->partial[spd->nr_pages - 1].offset +
1881 spd->partial[spd->nr_pages - 1].len == offset);
1885 * Fill page/offset/length into spd, if it can hold more pages.
1887 static bool spd_fill_page(struct splice_pipe_desc *spd,
1888 struct pipe_inode_info *pipe, struct page *page,
1889 unsigned int *len, unsigned int offset,
1893 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1897 page = linear_to_page(page, len, &offset, sk);
1901 if (spd_can_coalesce(spd, page, offset)) {
1902 spd->partial[spd->nr_pages - 1].len += *len;
1906 spd->pages[spd->nr_pages] = page;
1907 spd->partial[spd->nr_pages].len = *len;
1908 spd->partial[spd->nr_pages].offset = offset;
1914 static bool __splice_segment(struct page *page, unsigned int poff,
1915 unsigned int plen, unsigned int *off,
1917 struct splice_pipe_desc *spd, bool linear,
1919 struct pipe_inode_info *pipe)
1924 /* skip this segment if already processed */
1930 /* ignore any bits we already processed */
1936 unsigned int flen = min(*len, plen);
1938 if (spd_fill_page(spd, pipe, page, &flen, poff,
1944 } while (*len && plen);
1950 * Map linear and fragment data from the skb to spd. It reports true if the
1951 * pipe is full or if we already spliced the requested length.
1953 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1954 unsigned int *offset, unsigned int *len,
1955 struct splice_pipe_desc *spd, struct sock *sk)
1958 struct sk_buff *iter;
1960 /* map the linear part :
1961 * If skb->head_frag is set, this 'linear' part is backed by a
1962 * fragment, and if the head is not shared with any clones then
1963 * we can avoid a copy since we own the head portion of this page.
1965 if (__splice_segment(virt_to_page(skb->data),
1966 (unsigned long) skb->data & (PAGE_SIZE - 1),
1969 skb_head_is_locked(skb),
1974 * then map the fragments
1976 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1977 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1979 if (__splice_segment(skb_frag_page(f),
1980 f->page_offset, skb_frag_size(f),
1981 offset, len, spd, false, sk, pipe))
1985 skb_walk_frags(skb, iter) {
1986 if (*offset >= iter->len) {
1987 *offset -= iter->len;
1990 /* __skb_splice_bits() only fails if the output has no room
1991 * left, so no point in going over the frag_list for the error
1994 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2002 * Map data from the skb to a pipe. Should handle both the linear part,
2003 * the fragments, and the frag list.
2005 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2006 struct pipe_inode_info *pipe, unsigned int tlen,
2009 struct partial_page partial[MAX_SKB_FRAGS];
2010 struct page *pages[MAX_SKB_FRAGS];
2011 struct splice_pipe_desc spd = {
2014 .nr_pages_max = MAX_SKB_FRAGS,
2016 .ops = &nosteal_pipe_buf_ops,
2017 .spd_release = sock_spd_release,
2021 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2024 ret = splice_to_pipe(pipe, &spd);
2028 EXPORT_SYMBOL_GPL(skb_splice_bits);
2031 * skb_store_bits - store bits from kernel buffer to skb
2032 * @skb: destination buffer
2033 * @offset: offset in destination
2034 * @from: source buffer
2035 * @len: number of bytes to copy
2037 * Copy the specified number of bytes from the source buffer to the
2038 * destination skb. This function handles all the messy bits of
2039 * traversing fragment lists and such.
2042 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2044 int start = skb_headlen(skb);
2045 struct sk_buff *frag_iter;
2048 if (offset > (int)skb->len - len)
2051 if ((copy = start - offset) > 0) {
2054 skb_copy_to_linear_data_offset(skb, offset, from, copy);
2055 if ((len -= copy) == 0)
2061 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2062 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2065 WARN_ON(start > offset + len);
2067 end = start + skb_frag_size(frag);
2068 if ((copy = end - offset) > 0) {
2074 vaddr = kmap_atomic(skb_frag_page(frag));
2075 memcpy(vaddr + frag->page_offset + offset - start,
2077 kunmap_atomic(vaddr);
2079 if ((len -= copy) == 0)
2087 skb_walk_frags(skb, frag_iter) {
2090 WARN_ON(start > offset + len);
2092 end = start + frag_iter->len;
2093 if ((copy = end - offset) > 0) {
2096 if (skb_store_bits(frag_iter, offset - start,
2099 if ((len -= copy) == 0)
2112 EXPORT_SYMBOL(skb_store_bits);
2114 /* Checksum skb data. */
2115 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2116 __wsum csum, const struct skb_checksum_ops *ops)
2118 int start = skb_headlen(skb);
2119 int i, copy = start - offset;
2120 struct sk_buff *frag_iter;
2123 /* Checksum header. */
2127 csum = ops->update(skb->data + offset, copy, csum);
2128 if ((len -= copy) == 0)
2134 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2136 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2138 WARN_ON(start > offset + len);
2140 end = start + skb_frag_size(frag);
2141 if ((copy = end - offset) > 0) {
2147 vaddr = kmap_atomic(skb_frag_page(frag));
2148 csum2 = ops->update(vaddr + frag->page_offset +
2149 offset - start, copy, 0);
2150 kunmap_atomic(vaddr);
2151 csum = ops->combine(csum, csum2, pos, copy);
2160 skb_walk_frags(skb, frag_iter) {
2163 WARN_ON(start > offset + len);
2165 end = start + frag_iter->len;
2166 if ((copy = end - offset) > 0) {
2170 csum2 = __skb_checksum(frag_iter, offset - start,
2172 csum = ops->combine(csum, csum2, pos, copy);
2173 if ((len -= copy) == 0)
2184 EXPORT_SYMBOL(__skb_checksum);
2186 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2187 int len, __wsum csum)
2189 const struct skb_checksum_ops ops = {
2190 .update = csum_partial_ext,
2191 .combine = csum_block_add_ext,
2194 return __skb_checksum(skb, offset, len, csum, &ops);
2196 EXPORT_SYMBOL(skb_checksum);
2198 /* Both of above in one bottle. */
2200 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2201 u8 *to, int len, __wsum csum)
2203 int start = skb_headlen(skb);
2204 int i, copy = start - offset;
2205 struct sk_buff *frag_iter;
2212 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2214 if ((len -= copy) == 0)
2221 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2224 WARN_ON(start > offset + len);
2226 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2227 if ((copy = end - offset) > 0) {
2230 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2234 vaddr = kmap_atomic(skb_frag_page(frag));
2235 csum2 = csum_partial_copy_nocheck(vaddr +
2239 kunmap_atomic(vaddr);
2240 csum = csum_block_add(csum, csum2, pos);
2250 skb_walk_frags(skb, frag_iter) {
2254 WARN_ON(start > offset + len);
2256 end = start + frag_iter->len;
2257 if ((copy = end - offset) > 0) {
2260 csum2 = skb_copy_and_csum_bits(frag_iter,
2263 csum = csum_block_add(csum, csum2, pos);
2264 if ((len -= copy) == 0)
2275 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2278 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2279 * @from: source buffer
2281 * Calculates the amount of linear headroom needed in the 'to' skb passed
2282 * into skb_zerocopy().
2285 skb_zerocopy_headlen(const struct sk_buff *from)
2287 unsigned int hlen = 0;
2289 if (!from->head_frag ||
2290 skb_headlen(from) < L1_CACHE_BYTES ||
2291 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
2292 hlen = skb_headlen(from);
2297 if (skb_has_frag_list(from))
2302 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2305 * skb_zerocopy - Zero copy skb to skb
2306 * @to: destination buffer
2307 * @from: source buffer
2308 * @len: number of bytes to copy from source buffer
2309 * @hlen: size of linear headroom in destination buffer
2311 * Copies up to `len` bytes from `from` to `to` by creating references
2312 * to the frags in the source buffer.
2314 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2315 * headroom in the `to` buffer.
2318 * 0: everything is OK
2319 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2320 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2323 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2326 int plen = 0; /* length of skb->head fragment */
2329 unsigned int offset;
2331 BUG_ON(!from->head_frag && !hlen);
2333 /* dont bother with small payloads */
2334 if (len <= skb_tailroom(to))
2335 return skb_copy_bits(from, 0, skb_put(to, len), len);
2338 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2343 plen = min_t(int, skb_headlen(from), len);
2345 page = virt_to_head_page(from->head);
2346 offset = from->data - (unsigned char *)page_address(page);
2347 __skb_fill_page_desc(to, 0, page, offset, plen);
2354 to->truesize += len + plen;
2355 to->len += len + plen;
2356 to->data_len += len + plen;
2358 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2363 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2366 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2367 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2368 len -= skb_shinfo(to)->frags[j].size;
2369 skb_frag_ref(to, j);
2372 skb_shinfo(to)->nr_frags = j;
2376 EXPORT_SYMBOL_GPL(skb_zerocopy);
2378 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2383 if (skb->ip_summed == CHECKSUM_PARTIAL)
2384 csstart = skb_checksum_start_offset(skb);
2386 csstart = skb_headlen(skb);
2388 BUG_ON(csstart > skb_headlen(skb));
2390 skb_copy_from_linear_data(skb, to, csstart);
2393 if (csstart != skb->len)
2394 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2395 skb->len - csstart, 0);
2397 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2398 long csstuff = csstart + skb->csum_offset;
2400 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2403 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2406 * skb_dequeue - remove from the head of the queue
2407 * @list: list to dequeue from
2409 * Remove the head of the list. The list lock is taken so the function
2410 * may be used safely with other locking list functions. The head item is
2411 * returned or %NULL if the list is empty.
2414 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2416 unsigned long flags;
2417 struct sk_buff *result;
2419 spin_lock_irqsave(&list->lock, flags);
2420 result = __skb_dequeue(list);
2421 spin_unlock_irqrestore(&list->lock, flags);
2424 EXPORT_SYMBOL(skb_dequeue);
2427 * skb_dequeue_tail - remove from the tail of the queue
2428 * @list: list to dequeue from
2430 * Remove the tail of the list. The list lock is taken so the function
2431 * may be used safely with other locking list functions. The tail item is
2432 * returned or %NULL if the list is empty.
2434 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2436 unsigned long flags;
2437 struct sk_buff *result;
2439 spin_lock_irqsave(&list->lock, flags);
2440 result = __skb_dequeue_tail(list);
2441 spin_unlock_irqrestore(&list->lock, flags);
2444 EXPORT_SYMBOL(skb_dequeue_tail);
2447 * skb_queue_purge - empty a list
2448 * @list: list to empty
2450 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2451 * the list and one reference dropped. This function takes the list
2452 * lock and is atomic with respect to other list locking functions.
2454 void skb_queue_purge(struct sk_buff_head *list)
2456 struct sk_buff *skb;
2457 while ((skb = skb_dequeue(list)) != NULL)
2460 EXPORT_SYMBOL(skb_queue_purge);
2463 * skb_rbtree_purge - empty a skb rbtree
2464 * @root: root of the rbtree to empty
2465 * Return value: the sum of truesizes of all purged skbs.
2467 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
2468 * the list and one reference dropped. This function does not take
2469 * any lock. Synchronization should be handled by the caller (e.g., TCP
2470 * out-of-order queue is protected by the socket lock).
2472 unsigned int skb_rbtree_purge(struct rb_root *root)
2474 struct rb_node *p = rb_first(root);
2475 unsigned int sum = 0;
2478 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
2481 rb_erase(&skb->rbnode, root);
2482 sum += skb->truesize;
2489 * skb_queue_head - queue a buffer at the list head
2490 * @list: list to use
2491 * @newsk: buffer to queue
2493 * Queue a buffer at the start of the list. This function takes the
2494 * list lock and can be used safely with other locking &sk_buff functions
2497 * A buffer cannot be placed on two lists at the same time.
2499 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2501 unsigned long flags;
2503 spin_lock_irqsave(&list->lock, flags);
2504 __skb_queue_head(list, newsk);
2505 spin_unlock_irqrestore(&list->lock, flags);
2507 EXPORT_SYMBOL(skb_queue_head);
2510 * skb_queue_tail - queue a buffer at the list tail
2511 * @list: list to use
2512 * @newsk: buffer to queue
2514 * Queue a buffer at the tail of the list. This function takes the
2515 * list lock and can be used safely with other locking &sk_buff functions
2518 * A buffer cannot be placed on two lists at the same time.
2520 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2522 unsigned long flags;
2524 spin_lock_irqsave(&list->lock, flags);
2525 __skb_queue_tail(list, newsk);
2526 spin_unlock_irqrestore(&list->lock, flags);
2528 EXPORT_SYMBOL(skb_queue_tail);
2531 * skb_unlink - remove a buffer from a list
2532 * @skb: buffer to remove
2533 * @list: list to use
2535 * Remove a packet from a list. The list locks are taken and this
2536 * function is atomic with respect to other list locked calls
2538 * You must know what list the SKB is on.
2540 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2542 unsigned long flags;
2544 spin_lock_irqsave(&list->lock, flags);
2545 __skb_unlink(skb, list);
2546 spin_unlock_irqrestore(&list->lock, flags);
2548 EXPORT_SYMBOL(skb_unlink);
2551 * skb_append - append a buffer
2552 * @old: buffer to insert after
2553 * @newsk: buffer to insert
2554 * @list: list to use
2556 * Place a packet after a given packet in a list. The list locks are taken
2557 * and this function is atomic with respect to other list locked calls.
2558 * A buffer cannot be placed on two lists at the same time.
2560 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2562 unsigned long flags;
2564 spin_lock_irqsave(&list->lock, flags);
2565 __skb_queue_after(list, old, newsk);
2566 spin_unlock_irqrestore(&list->lock, flags);
2568 EXPORT_SYMBOL(skb_append);
2571 * skb_insert - insert a buffer
2572 * @old: buffer to insert before
2573 * @newsk: buffer to insert
2574 * @list: list to use
2576 * Place a packet before a given packet in a list. The list locks are
2577 * taken and this function is atomic with respect to other list locked
2580 * A buffer cannot be placed on two lists at the same time.
2582 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2584 unsigned long flags;
2586 spin_lock_irqsave(&list->lock, flags);
2587 __skb_insert(newsk, old->prev, old, list);
2588 spin_unlock_irqrestore(&list->lock, flags);
2590 EXPORT_SYMBOL(skb_insert);
2592 static inline void skb_split_inside_header(struct sk_buff *skb,
2593 struct sk_buff* skb1,
2594 const u32 len, const int pos)
2598 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2600 /* And move data appendix as is. */
2601 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2602 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2604 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2605 skb_shinfo(skb)->nr_frags = 0;
2606 skb1->data_len = skb->data_len;
2607 skb1->len += skb1->data_len;
2610 skb_set_tail_pointer(skb, len);
2613 static inline void skb_split_no_header(struct sk_buff *skb,
2614 struct sk_buff* skb1,
2615 const u32 len, int pos)
2618 const int nfrags = skb_shinfo(skb)->nr_frags;
2620 skb_shinfo(skb)->nr_frags = 0;
2621 skb1->len = skb1->data_len = skb->len - len;
2623 skb->data_len = len - pos;
2625 for (i = 0; i < nfrags; i++) {
2626 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2628 if (pos + size > len) {
2629 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2633 * We have two variants in this case:
2634 * 1. Move all the frag to the second
2635 * part, if it is possible. F.e.
2636 * this approach is mandatory for TUX,
2637 * where splitting is expensive.
2638 * 2. Split is accurately. We make this.
2640 skb_frag_ref(skb, i);
2641 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2642 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2643 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2644 skb_shinfo(skb)->nr_frags++;
2648 skb_shinfo(skb)->nr_frags++;
2651 skb_shinfo(skb1)->nr_frags = k;
2655 * skb_split - Split fragmented skb to two parts at length len.
2656 * @skb: the buffer to split
2657 * @skb1: the buffer to receive the second part
2658 * @len: new length for skb
2660 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2662 int pos = skb_headlen(skb);
2664 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
2666 if (len < pos) /* Split line is inside header. */
2667 skb_split_inside_header(skb, skb1, len, pos);
2668 else /* Second chunk has no header, nothing to copy. */
2669 skb_split_no_header(skb, skb1, len, pos);
2671 EXPORT_SYMBOL(skb_split);
2673 /* Shifting from/to a cloned skb is a no-go.
2675 * Caller cannot keep skb_shinfo related pointers past calling here!
2677 static int skb_prepare_for_shift(struct sk_buff *skb)
2681 if (skb_cloned(skb)) {
2682 /* Save and restore truesize: pskb_expand_head() may reallocate
2683 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
2684 * cannot change truesize at this point.
2686 unsigned int save_truesize = skb->truesize;
2688 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2689 skb->truesize = save_truesize;
2695 * skb_shift - Shifts paged data partially from skb to another
2696 * @tgt: buffer into which tail data gets added
2697 * @skb: buffer from which the paged data comes from
2698 * @shiftlen: shift up to this many bytes
2700 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2701 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2702 * It's up to caller to free skb if everything was shifted.
2704 * If @tgt runs out of frags, the whole operation is aborted.
2706 * Skb cannot include anything else but paged data while tgt is allowed
2707 * to have non-paged data as well.
2709 * TODO: full sized shift could be optimized but that would need
2710 * specialized skb free'er to handle frags without up-to-date nr_frags.
2712 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2714 int from, to, merge, todo;
2715 struct skb_frag_struct *fragfrom, *fragto;
2717 BUG_ON(shiftlen > skb->len);
2718 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2722 to = skb_shinfo(tgt)->nr_frags;
2723 fragfrom = &skb_shinfo(skb)->frags[from];
2725 /* Actual merge is delayed until the point when we know we can
2726 * commit all, so that we don't have to undo partial changes
2729 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2730 fragfrom->page_offset)) {
2735 todo -= skb_frag_size(fragfrom);
2737 if (skb_prepare_for_shift(skb) ||
2738 skb_prepare_for_shift(tgt))
2741 /* All previous frag pointers might be stale! */
2742 fragfrom = &skb_shinfo(skb)->frags[from];
2743 fragto = &skb_shinfo(tgt)->frags[merge];
2745 skb_frag_size_add(fragto, shiftlen);
2746 skb_frag_size_sub(fragfrom, shiftlen);
2747 fragfrom->page_offset += shiftlen;
2755 /* Skip full, not-fitting skb to avoid expensive operations */
2756 if ((shiftlen == skb->len) &&
2757 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2760 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2763 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2764 if (to == MAX_SKB_FRAGS)
2767 fragfrom = &skb_shinfo(skb)->frags[from];
2768 fragto = &skb_shinfo(tgt)->frags[to];
2770 if (todo >= skb_frag_size(fragfrom)) {
2771 *fragto = *fragfrom;
2772 todo -= skb_frag_size(fragfrom);
2777 __skb_frag_ref(fragfrom);
2778 fragto->page = fragfrom->page;
2779 fragto->page_offset = fragfrom->page_offset;
2780 skb_frag_size_set(fragto, todo);
2782 fragfrom->page_offset += todo;
2783 skb_frag_size_sub(fragfrom, todo);
2791 /* Ready to "commit" this state change to tgt */
2792 skb_shinfo(tgt)->nr_frags = to;
2795 fragfrom = &skb_shinfo(skb)->frags[0];
2796 fragto = &skb_shinfo(tgt)->frags[merge];
2798 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2799 __skb_frag_unref(fragfrom);
2802 /* Reposition in the original skb */
2804 while (from < skb_shinfo(skb)->nr_frags)
2805 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2806 skb_shinfo(skb)->nr_frags = to;
2808 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2811 /* Most likely the tgt won't ever need its checksum anymore, skb on
2812 * the other hand might need it if it needs to be resent
2814 tgt->ip_summed = CHECKSUM_PARTIAL;
2815 skb->ip_summed = CHECKSUM_PARTIAL;
2817 /* Yak, is it really working this way? Some helper please? */
2818 skb->len -= shiftlen;
2819 skb->data_len -= shiftlen;
2820 skb->truesize -= shiftlen;
2821 tgt->len += shiftlen;
2822 tgt->data_len += shiftlen;
2823 tgt->truesize += shiftlen;
2829 * skb_prepare_seq_read - Prepare a sequential read of skb data
2830 * @skb: the buffer to read
2831 * @from: lower offset of data to be read
2832 * @to: upper offset of data to be read
2833 * @st: state variable
2835 * Initializes the specified state variable. Must be called before
2836 * invoking skb_seq_read() for the first time.
2838 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2839 unsigned int to, struct skb_seq_state *st)
2841 st->lower_offset = from;
2842 st->upper_offset = to;
2843 st->root_skb = st->cur_skb = skb;
2844 st->frag_idx = st->stepped_offset = 0;
2845 st->frag_data = NULL;
2847 EXPORT_SYMBOL(skb_prepare_seq_read);
2850 * skb_seq_read - Sequentially read skb data
2851 * @consumed: number of bytes consumed by the caller so far
2852 * @data: destination pointer for data to be returned
2853 * @st: state variable
2855 * Reads a block of skb data at @consumed relative to the
2856 * lower offset specified to skb_prepare_seq_read(). Assigns
2857 * the head of the data block to @data and returns the length
2858 * of the block or 0 if the end of the skb data or the upper
2859 * offset has been reached.
2861 * The caller is not required to consume all of the data
2862 * returned, i.e. @consumed is typically set to the number
2863 * of bytes already consumed and the next call to
2864 * skb_seq_read() will return the remaining part of the block.
2866 * Note 1: The size of each block of data returned can be arbitrary,
2867 * this limitation is the cost for zerocopy sequential
2868 * reads of potentially non linear data.
2870 * Note 2: Fragment lists within fragments are not implemented
2871 * at the moment, state->root_skb could be replaced with
2872 * a stack for this purpose.
2874 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2875 struct skb_seq_state *st)
2877 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2880 if (unlikely(abs_offset >= st->upper_offset)) {
2881 if (st->frag_data) {
2882 kunmap_atomic(st->frag_data);
2883 st->frag_data = NULL;
2889 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2891 if (abs_offset < block_limit && !st->frag_data) {
2892 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2893 return block_limit - abs_offset;
2896 if (st->frag_idx == 0 && !st->frag_data)
2897 st->stepped_offset += skb_headlen(st->cur_skb);
2899 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2900 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2901 block_limit = skb_frag_size(frag) + st->stepped_offset;
2903 if (abs_offset < block_limit) {
2905 st->frag_data = kmap_atomic(skb_frag_page(frag));
2907 *data = (u8 *) st->frag_data + frag->page_offset +
2908 (abs_offset - st->stepped_offset);
2910 return block_limit - abs_offset;
2913 if (st->frag_data) {
2914 kunmap_atomic(st->frag_data);
2915 st->frag_data = NULL;
2919 st->stepped_offset += skb_frag_size(frag);
2922 if (st->frag_data) {
2923 kunmap_atomic(st->frag_data);
2924 st->frag_data = NULL;
2927 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2928 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2931 } else if (st->cur_skb->next) {
2932 st->cur_skb = st->cur_skb->next;
2939 EXPORT_SYMBOL(skb_seq_read);
2942 * skb_abort_seq_read - Abort a sequential read of skb data
2943 * @st: state variable
2945 * Must be called if skb_seq_read() was not called until it
2948 void skb_abort_seq_read(struct skb_seq_state *st)
2951 kunmap_atomic(st->frag_data);
2953 EXPORT_SYMBOL(skb_abort_seq_read);
2955 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2957 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2958 struct ts_config *conf,
2959 struct ts_state *state)
2961 return skb_seq_read(offset, text, TS_SKB_CB(state));
2964 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2966 skb_abort_seq_read(TS_SKB_CB(state));
2970 * skb_find_text - Find a text pattern in skb data
2971 * @skb: the buffer to look in
2972 * @from: search offset
2974 * @config: textsearch configuration
2976 * Finds a pattern in the skb data according to the specified
2977 * textsearch configuration. Use textsearch_next() to retrieve
2978 * subsequent occurrences of the pattern. Returns the offset
2979 * to the first occurrence or UINT_MAX if no match was found.
2981 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2982 unsigned int to, struct ts_config *config)
2984 struct ts_state state;
2987 config->get_next_block = skb_ts_get_next_block;
2988 config->finish = skb_ts_finish;
2990 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2992 ret = textsearch_find(config, &state);
2993 return (ret <= to - from ? ret : UINT_MAX);
2995 EXPORT_SYMBOL(skb_find_text);
2998 * skb_append_datato_frags - append the user data to a skb
2999 * @sk: sock structure
3000 * @skb: skb structure to be appended with user data.
3001 * @getfrag: call back function to be used for getting the user data
3002 * @from: pointer to user message iov
3003 * @length: length of the iov message
3005 * Description: This procedure append the user data in the fragment part
3006 * of the skb if any page alloc fails user this procedure returns -ENOMEM
3008 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
3009 int (*getfrag)(void *from, char *to, int offset,
3010 int len, int odd, struct sk_buff *skb),
3011 void *from, int length)
3013 int frg_cnt = skb_shinfo(skb)->nr_frags;
3017 struct page_frag *pfrag = ¤t->task_frag;
3020 /* Return error if we don't have space for new frag */
3021 if (frg_cnt >= MAX_SKB_FRAGS)
3024 if (!sk_page_frag_refill(sk, pfrag))
3027 /* copy the user data to page */
3028 copy = min_t(int, length, pfrag->size - pfrag->offset);
3030 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
3031 offset, copy, 0, skb);
3035 /* copy was successful so update the size parameters */
3036 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
3039 pfrag->offset += copy;
3040 get_page(pfrag->page);
3042 skb->truesize += copy;
3043 atomic_add(copy, &sk->sk_wmem_alloc);
3045 skb->data_len += copy;
3049 } while (length > 0);
3053 EXPORT_SYMBOL(skb_append_datato_frags);
3055 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3056 int offset, size_t size)
3058 int i = skb_shinfo(skb)->nr_frags;
3060 if (skb_can_coalesce(skb, i, page, offset)) {
3061 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3062 } else if (i < MAX_SKB_FRAGS) {
3064 skb_fill_page_desc(skb, i, page, offset, size);
3071 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3074 * skb_pull_rcsum - pull skb and update receive checksum
3075 * @skb: buffer to update
3076 * @len: length of data pulled
3078 * This function performs an skb_pull on the packet and updates
3079 * the CHECKSUM_COMPLETE checksum. It should be used on
3080 * receive path processing instead of skb_pull unless you know
3081 * that the checksum difference is zero (e.g., a valid IP header)
3082 * or you are setting ip_summed to CHECKSUM_NONE.
3084 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3086 unsigned char *data = skb->data;
3088 BUG_ON(len > skb->len);
3089 __skb_pull(skb, len);
3090 skb_postpull_rcsum(skb, data, len);
3093 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3096 * skb_segment - Perform protocol segmentation on skb.
3097 * @head_skb: buffer to segment
3098 * @features: features for the output path (see dev->features)
3100 * This function performs segmentation on the given skb. It returns
3101 * a pointer to the first in a list of new skbs for the segments.
3102 * In case of error it returns ERR_PTR(err).
3104 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3105 netdev_features_t features)
3107 struct sk_buff *segs = NULL;
3108 struct sk_buff *tail = NULL;
3109 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3110 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3111 unsigned int mss = skb_shinfo(head_skb)->gso_size;
3112 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3113 struct sk_buff *frag_skb = head_skb;
3114 unsigned int offset = doffset;
3115 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3116 unsigned int partial_segs = 0;
3117 unsigned int headroom;
3118 unsigned int len = head_skb->len;
3121 int nfrags = skb_shinfo(head_skb)->nr_frags;
3127 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3128 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3129 /* gso_size is untrusted, and we have a frag_list with a linear
3130 * non head_frag head.
3132 * (we assume checking the first list_skb member suffices;
3133 * i.e if either of the list_skb members have non head_frag
3134 * head, then the first one has too).
3136 * If head_skb's headlen does not fit requested gso_size, it
3137 * means that the frag_list members do NOT terminate on exact
3138 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3139 * sharing. Therefore we must fallback to copying the frag_list
3140 * skbs; we do so by disabling SG.
3142 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3143 features &= ~NETIF_F_SG;
3146 __skb_push(head_skb, doffset);
3147 proto = skb_network_protocol(head_skb, &dummy);
3148 if (unlikely(!proto))
3149 return ERR_PTR(-EINVAL);
3151 sg = !!(features & NETIF_F_SG);
3152 csum = !!can_checksum_protocol(features, proto);
3154 if (sg && csum && (mss != GSO_BY_FRAGS)) {
3155 if (!(features & NETIF_F_GSO_PARTIAL)) {
3156 struct sk_buff *iter;
3157 unsigned int frag_len;
3160 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3163 /* If we get here then all the required
3164 * GSO features except frag_list are supported.
3165 * Try to split the SKB to multiple GSO SKBs
3166 * with no frag_list.
3167 * Currently we can do that only when the buffers don't
3168 * have a linear part and all the buffers except
3169 * the last are of the same length.
3171 frag_len = list_skb->len;
3172 skb_walk_frags(head_skb, iter) {
3173 if (frag_len != iter->len && iter->next)
3175 if (skb_headlen(iter))
3181 if (len != frag_len)
3185 /* GSO partial only requires that we trim off any excess that
3186 * doesn't fit into an MSS sized block, so take care of that
3189 partial_segs = len / mss;
3190 if (partial_segs > 1)
3191 mss *= partial_segs;
3197 headroom = skb_headroom(head_skb);
3198 pos = skb_headlen(head_skb);
3201 struct sk_buff *nskb;
3202 skb_frag_t *nskb_frag;
3206 if (unlikely(mss == GSO_BY_FRAGS)) {
3207 len = list_skb->len;
3209 len = head_skb->len - offset;
3214 hsize = skb_headlen(head_skb) - offset;
3217 if (hsize > len || !sg)
3220 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3221 (skb_headlen(list_skb) == len || sg)) {
3222 BUG_ON(skb_headlen(list_skb) > len);
3225 nfrags = skb_shinfo(list_skb)->nr_frags;
3226 frag = skb_shinfo(list_skb)->frags;
3227 frag_skb = list_skb;
3228 pos += skb_headlen(list_skb);
3230 while (pos < offset + len) {
3231 BUG_ON(i >= nfrags);
3233 size = skb_frag_size(frag);
3234 if (pos + size > offset + len)
3242 nskb = skb_clone(list_skb, GFP_ATOMIC);
3243 list_skb = list_skb->next;
3245 if (unlikely(!nskb))
3248 if (unlikely(pskb_trim(nskb, len))) {
3253 hsize = skb_end_offset(nskb);
3254 if (skb_cow_head(nskb, doffset + headroom)) {
3259 nskb->truesize += skb_end_offset(nskb) - hsize;
3260 skb_release_head_state(nskb);
3261 __skb_push(nskb, doffset);
3263 nskb = __alloc_skb(hsize + doffset + headroom,
3264 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3267 if (unlikely(!nskb))
3270 skb_reserve(nskb, headroom);
3271 __skb_put(nskb, doffset);
3280 __copy_skb_header(nskb, head_skb);
3282 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3283 skb_reset_mac_len(nskb);
3285 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3286 nskb->data - tnl_hlen,
3287 doffset + tnl_hlen);
3289 if (nskb->len == len + doffset)
3290 goto perform_csum_check;
3293 if (!nskb->remcsum_offload)
3294 nskb->ip_summed = CHECKSUM_NONE;
3295 SKB_GSO_CB(nskb)->csum =
3296 skb_copy_and_csum_bits(head_skb, offset,
3299 SKB_GSO_CB(nskb)->csum_start =
3300 skb_headroom(nskb) + doffset;
3304 nskb_frag = skb_shinfo(nskb)->frags;
3306 skb_copy_from_linear_data_offset(head_skb, offset,
3307 skb_put(nskb, hsize), hsize);
3309 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
3312 while (pos < offset + len) {
3314 BUG_ON(skb_headlen(list_skb));
3317 nfrags = skb_shinfo(list_skb)->nr_frags;
3318 frag = skb_shinfo(list_skb)->frags;
3319 frag_skb = list_skb;
3323 list_skb = list_skb->next;
3326 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3328 net_warn_ratelimited(
3329 "skb_segment: too many frags: %u %u\n",
3335 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3339 __skb_frag_ref(nskb_frag);
3340 size = skb_frag_size(nskb_frag);
3343 nskb_frag->page_offset += offset - pos;
3344 skb_frag_size_sub(nskb_frag, offset - pos);
3347 skb_shinfo(nskb)->nr_frags++;
3349 if (pos + size <= offset + len) {
3354 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3362 nskb->data_len = len - hsize;
3363 nskb->len += nskb->data_len;
3364 nskb->truesize += nskb->data_len;
3368 if (skb_has_shared_frag(nskb) &&
3369 __skb_linearize(nskb))
3372 if (!nskb->remcsum_offload)
3373 nskb->ip_summed = CHECKSUM_NONE;
3374 SKB_GSO_CB(nskb)->csum =
3375 skb_checksum(nskb, doffset,
3376 nskb->len - doffset, 0);
3377 SKB_GSO_CB(nskb)->csum_start =
3378 skb_headroom(nskb) + doffset;
3380 } while ((offset += len) < head_skb->len);
3382 /* Some callers want to get the end of the list.
3383 * Put it in segs->prev to avoid walking the list.
3384 * (see validate_xmit_skb_list() for example)
3389 struct sk_buff *iter;
3390 int type = skb_shinfo(head_skb)->gso_type;
3391 unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3393 /* Update type to add partial and then remove dodgy if set */
3394 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3395 type &= ~SKB_GSO_DODGY;
3397 /* Update GSO info and prepare to start updating headers on
3398 * our way back down the stack of protocols.
3400 for (iter = segs; iter; iter = iter->next) {
3401 skb_shinfo(iter)->gso_size = gso_size;
3402 skb_shinfo(iter)->gso_segs = partial_segs;
3403 skb_shinfo(iter)->gso_type = type;
3404 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
3407 if (tail->len - doffset <= gso_size)
3408 skb_shinfo(tail)->gso_size = 0;
3409 else if (tail != segs)
3410 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3413 /* Following permits correct backpressure, for protocols
3414 * using skb_set_owner_w().
3415 * Idea is to tranfert ownership from head_skb to last segment.
3417 if (head_skb->destructor == sock_wfree) {
3418 swap(tail->truesize, head_skb->truesize);
3419 swap(tail->destructor, head_skb->destructor);
3420 swap(tail->sk, head_skb->sk);
3425 kfree_skb_list(segs);
3426 return ERR_PTR(err);
3428 EXPORT_SYMBOL_GPL(skb_segment);
3430 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3432 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3433 unsigned int offset = skb_gro_offset(skb);
3434 unsigned int headlen = skb_headlen(skb);
3435 unsigned int len = skb_gro_len(skb);
3436 struct sk_buff *lp, *p = *head;
3437 unsigned int delta_truesize;
3439 if (unlikely(p->len + len >= 65536))
3442 lp = NAPI_GRO_CB(p)->last;
3443 pinfo = skb_shinfo(lp);
3445 if (headlen <= offset) {
3448 int i = skbinfo->nr_frags;
3449 int nr_frags = pinfo->nr_frags + i;
3451 if (nr_frags > MAX_SKB_FRAGS)
3455 pinfo->nr_frags = nr_frags;
3456 skbinfo->nr_frags = 0;
3458 frag = pinfo->frags + nr_frags;
3459 frag2 = skbinfo->frags + i;
3464 frag->page_offset += offset;
3465 skb_frag_size_sub(frag, offset);
3467 /* all fragments truesize : remove (head size + sk_buff) */
3468 delta_truesize = skb->truesize -
3469 SKB_TRUESIZE(skb_end_offset(skb));
3471 skb->truesize -= skb->data_len;
3472 skb->len -= skb->data_len;
3475 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3477 } else if (skb->head_frag) {
3478 int nr_frags = pinfo->nr_frags;
3479 skb_frag_t *frag = pinfo->frags + nr_frags;
3480 struct page *page = virt_to_head_page(skb->head);
3481 unsigned int first_size = headlen - offset;
3482 unsigned int first_offset;
3484 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3487 first_offset = skb->data -
3488 (unsigned char *)page_address(page) +
3491 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3493 frag->page.p = page;
3494 frag->page_offset = first_offset;
3495 skb_frag_size_set(frag, first_size);
3497 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3498 /* We dont need to clear skbinfo->nr_frags here */
3500 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3501 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3506 delta_truesize = skb->truesize;
3507 if (offset > headlen) {
3508 unsigned int eat = offset - headlen;
3510 skbinfo->frags[0].page_offset += eat;
3511 skb_frag_size_sub(&skbinfo->frags[0], eat);
3512 skb->data_len -= eat;
3517 __skb_pull(skb, offset);
3519 if (NAPI_GRO_CB(p)->last == p)
3520 skb_shinfo(p)->frag_list = skb;
3522 NAPI_GRO_CB(p)->last->next = skb;
3523 NAPI_GRO_CB(p)->last = skb;
3524 __skb_header_release(skb);
3528 NAPI_GRO_CB(p)->count++;
3530 p->truesize += delta_truesize;
3533 lp->data_len += len;
3534 lp->truesize += delta_truesize;
3537 NAPI_GRO_CB(skb)->same_flow = 1;
3540 EXPORT_SYMBOL_GPL(skb_gro_receive);
3542 void __init skb_init(void)
3544 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3545 sizeof(struct sk_buff),
3547 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3549 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3550 sizeof(struct sk_buff_fclones),
3552 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3557 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
3558 unsigned int recursion_level)
3560 int start = skb_headlen(skb);
3561 int i, copy = start - offset;
3562 struct sk_buff *frag_iter;
3565 if (unlikely(recursion_level >= 24))
3571 sg_set_buf(sg, skb->data + offset, copy);
3573 if ((len -= copy) == 0)
3578 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3581 WARN_ON(start > offset + len);
3583 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3584 if ((copy = end - offset) > 0) {
3585 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3586 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3591 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3592 frag->page_offset+offset-start);
3601 skb_walk_frags(skb, frag_iter) {
3604 WARN_ON(start > offset + len);
3606 end = start + frag_iter->len;
3607 if ((copy = end - offset) > 0) {
3608 if (unlikely(elt && sg_is_last(&sg[elt - 1])))
3613 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3614 copy, recursion_level + 1);
3615 if (unlikely(ret < 0))
3618 if ((len -= copy) == 0)
3629 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3630 * @skb: Socket buffer containing the buffers to be mapped
3631 * @sg: The scatter-gather list to map into
3632 * @offset: The offset into the buffer's contents to start mapping
3633 * @len: Length of buffer space to be mapped
3635 * Fill the specified scatter-gather list with mappings/pointers into a
3636 * region of the buffer space attached to a socket buffer. Returns either
3637 * the number of scatterlist items used, or -EMSGSIZE if the contents
3640 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3642 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
3647 sg_mark_end(&sg[nsg - 1]);
3651 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3653 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3654 * sglist without mark the sg which contain last skb data as the end.
3655 * So the caller can mannipulate sg list as will when padding new data after
3656 * the first call without calling sg_unmark_end to expend sg list.
3658 * Scenario to use skb_to_sgvec_nomark:
3660 * 2. skb_to_sgvec_nomark(payload1)
3661 * 3. skb_to_sgvec_nomark(payload2)
3663 * This is equivalent to:
3665 * 2. skb_to_sgvec(payload1)
3667 * 4. skb_to_sgvec(payload2)
3669 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3670 * is more preferable.
3672 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3673 int offset, int len)
3675 return __skb_to_sgvec(skb, sg, offset, len, 0);
3677 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3682 * skb_cow_data - Check that a socket buffer's data buffers are writable
3683 * @skb: The socket buffer to check.
3684 * @tailbits: Amount of trailing space to be added
3685 * @trailer: Returned pointer to the skb where the @tailbits space begins
3687 * Make sure that the data buffers attached to a socket buffer are
3688 * writable. If they are not, private copies are made of the data buffers
3689 * and the socket buffer is set to use these instead.
3691 * If @tailbits is given, make sure that there is space to write @tailbits
3692 * bytes of data beyond current end of socket buffer. @trailer will be
3693 * set to point to the skb in which this space begins.
3695 * The number of scatterlist elements required to completely map the
3696 * COW'd and extended socket buffer will be returned.
3698 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3702 struct sk_buff *skb1, **skb_p;
3704 /* If skb is cloned or its head is paged, reallocate
3705 * head pulling out all the pages (pages are considered not writable
3706 * at the moment even if they are anonymous).
3708 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3709 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3712 /* Easy case. Most of packets will go this way. */
3713 if (!skb_has_frag_list(skb)) {
3714 /* A little of trouble, not enough of space for trailer.
3715 * This should not happen, when stack is tuned to generate
3716 * good frames. OK, on miss we reallocate and reserve even more
3717 * space, 128 bytes is fair. */
3719 if (skb_tailroom(skb) < tailbits &&
3720 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3728 /* Misery. We are in troubles, going to mincer fragments... */
3731 skb_p = &skb_shinfo(skb)->frag_list;
3734 while ((skb1 = *skb_p) != NULL) {
3737 /* The fragment is partially pulled by someone,
3738 * this can happen on input. Copy it and everything
3741 if (skb_shared(skb1))
3744 /* If the skb is the last, worry about trailer. */
3746 if (skb1->next == NULL && tailbits) {
3747 if (skb_shinfo(skb1)->nr_frags ||
3748 skb_has_frag_list(skb1) ||
3749 skb_tailroom(skb1) < tailbits)
3750 ntail = tailbits + 128;
3756 skb_shinfo(skb1)->nr_frags ||
3757 skb_has_frag_list(skb1)) {
3758 struct sk_buff *skb2;
3760 /* Fuck, we are miserable poor guys... */
3762 skb2 = skb_copy(skb1, GFP_ATOMIC);
3764 skb2 = skb_copy_expand(skb1,
3768 if (unlikely(skb2 == NULL))
3772 skb_set_owner_w(skb2, skb1->sk);
3774 /* Looking around. Are we still alive?
3775 * OK, link new skb, drop old one */
3777 skb2->next = skb1->next;
3784 skb_p = &skb1->next;
3789 EXPORT_SYMBOL_GPL(skb_cow_data);
3791 static void sock_rmem_free(struct sk_buff *skb)
3793 struct sock *sk = skb->sk;
3795 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3799 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3801 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3803 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3804 (unsigned int)sk->sk_rcvbuf)
3809 skb->destructor = sock_rmem_free;
3810 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3812 /* before exiting rcu section, make sure dst is refcounted */
3815 skb_queue_tail(&sk->sk_error_queue, skb);
3816 if (!sock_flag(sk, SOCK_DEAD))
3817 sk->sk_error_report(sk);
3820 EXPORT_SYMBOL(sock_queue_err_skb);
3822 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3824 struct sk_buff_head *q = &sk->sk_error_queue;
3825 struct sk_buff *skb, *skb_next;
3826 unsigned long flags;
3829 spin_lock_irqsave(&q->lock, flags);
3830 skb = __skb_dequeue(q);
3831 if (skb && (skb_next = skb_peek(q)))
3832 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3833 spin_unlock_irqrestore(&q->lock, flags);
3837 sk->sk_error_report(sk);
3841 EXPORT_SYMBOL(sock_dequeue_err_skb);
3844 * skb_clone_sk - create clone of skb, and take reference to socket
3845 * @skb: the skb to clone
3847 * This function creates a clone of a buffer that holds a reference on
3848 * sk_refcnt. Buffers created via this function are meant to be
3849 * returned using sock_queue_err_skb, or free via kfree_skb.
3851 * When passing buffers allocated with this function to sock_queue_err_skb
3852 * it is necessary to wrap the call with sock_hold/sock_put in order to
3853 * prevent the socket from being released prior to being enqueued on
3854 * the sk_error_queue.
3856 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3858 struct sock *sk = skb->sk;
3859 struct sk_buff *clone;
3861 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3864 clone = skb_clone(skb, GFP_ATOMIC);
3871 clone->destructor = sock_efree;
3875 EXPORT_SYMBOL(skb_clone_sk);
3877 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3881 struct sock_exterr_skb *serr;
3884 serr = SKB_EXT_ERR(skb);
3885 memset(serr, 0, sizeof(*serr));
3886 serr->ee.ee_errno = ENOMSG;
3887 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3888 serr->ee.ee_info = tstype;
3889 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
3890 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3891 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3892 if (sk->sk_protocol == IPPROTO_TCP &&
3893 sk->sk_type == SOCK_STREAM)
3894 serr->ee.ee_data -= sk->sk_tskey;
3897 err = sock_queue_err_skb(sk, skb);
3903 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3907 if (likely(sysctl_tstamp_allow_data || tsonly))
3910 read_lock_bh(&sk->sk_callback_lock);
3911 ret = sk->sk_socket && sk->sk_socket->file &&
3912 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3913 read_unlock_bh(&sk->sk_callback_lock);
3917 void skb_complete_tx_timestamp(struct sk_buff *skb,
3918 struct skb_shared_hwtstamps *hwtstamps)
3920 struct sock *sk = skb->sk;
3922 if (!skb_may_tx_timestamp(sk, false))
3925 /* Take a reference to prevent skb_orphan() from freeing the socket,
3926 * but only if the socket refcount is not zero.
3928 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
3929 *skb_hwtstamps(skb) = *hwtstamps;
3930 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3938 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3940 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3941 struct skb_shared_hwtstamps *hwtstamps,
3942 struct sock *sk, int tstype)
3944 struct sk_buff *skb;
3950 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3951 if (!skb_may_tx_timestamp(sk, tsonly))
3955 skb = alloc_skb(0, GFP_ATOMIC);
3957 skb = skb_clone(orig_skb, GFP_ATOMIC);
3962 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
3964 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3968 *skb_hwtstamps(skb) = *hwtstamps;
3970 skb->tstamp = ktime_get_real();
3972 __skb_complete_tx_timestamp(skb, sk, tstype);
3974 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3976 void skb_tstamp_tx(struct sk_buff *orig_skb,
3977 struct skb_shared_hwtstamps *hwtstamps)
3979 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3982 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3984 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3986 struct sock *sk = skb->sk;
3987 struct sock_exterr_skb *serr;
3990 skb->wifi_acked_valid = 1;
3991 skb->wifi_acked = acked;
3993 serr = SKB_EXT_ERR(skb);
3994 memset(serr, 0, sizeof(*serr));
3995 serr->ee.ee_errno = ENOMSG;
3996 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3998 /* Take a reference to prevent skb_orphan() from freeing the socket,
3999 * but only if the socket refcount is not zero.
4001 if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
4002 err = sock_queue_err_skb(sk, skb);
4008 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4011 * skb_partial_csum_set - set up and verify partial csum values for packet
4012 * @skb: the skb to set
4013 * @start: the number of bytes after skb->data to start checksumming.
4014 * @off: the offset from start to place the checksum.
4016 * For untrusted partially-checksummed packets, we need to make sure the values
4017 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4019 * This function checks and sets those values and skb->ip_summed: if this
4020 * returns false you should drop the packet.
4022 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4024 if (unlikely(start > skb_headlen(skb)) ||
4025 unlikely((int)start + off > skb_headlen(skb) - 2)) {
4026 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
4027 start, off, skb_headlen(skb));
4030 skb->ip_summed = CHECKSUM_PARTIAL;
4031 skb->csum_start = skb_headroom(skb) + start;
4032 skb->csum_offset = off;
4033 skb_set_transport_header(skb, start);
4036 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4038 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4041 if (skb_headlen(skb) >= len)
4044 /* If we need to pullup then pullup to the max, so we
4045 * won't need to do it again.
4050 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4053 if (skb_headlen(skb) < len)
4059 #define MAX_TCP_HDR_LEN (15 * 4)
4061 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4062 typeof(IPPROTO_IP) proto,
4069 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4070 off + MAX_TCP_HDR_LEN);
4071 if (!err && !skb_partial_csum_set(skb, off,
4072 offsetof(struct tcphdr,
4075 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4078 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4079 off + sizeof(struct udphdr));
4080 if (!err && !skb_partial_csum_set(skb, off,
4081 offsetof(struct udphdr,
4084 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4087 return ERR_PTR(-EPROTO);
4090 /* This value should be large enough to cover a tagged ethernet header plus
4091 * maximally sized IP and TCP or UDP headers.
4093 #define MAX_IP_HDR_LEN 128
4095 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4104 err = skb_maybe_pull_tail(skb,
4105 sizeof(struct iphdr),
4110 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4113 off = ip_hdrlen(skb);
4120 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4122 return PTR_ERR(csum);
4125 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4128 ip_hdr(skb)->protocol, 0);
4135 /* This value should be large enough to cover a tagged ethernet header plus
4136 * an IPv6 header, all options, and a maximal TCP or UDP header.
4138 #define MAX_IPV6_HDR_LEN 256
4140 #define OPT_HDR(type, skb, off) \
4141 (type *)(skb_network_header(skb) + (off))
4143 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4156 off = sizeof(struct ipv6hdr);
4158 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4162 nexthdr = ipv6_hdr(skb)->nexthdr;
4164 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4165 while (off <= len && !done) {
4167 case IPPROTO_DSTOPTS:
4168 case IPPROTO_HOPOPTS:
4169 case IPPROTO_ROUTING: {
4170 struct ipv6_opt_hdr *hp;
4172 err = skb_maybe_pull_tail(skb,
4174 sizeof(struct ipv6_opt_hdr),
4179 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4180 nexthdr = hp->nexthdr;
4181 off += ipv6_optlen(hp);
4185 struct ip_auth_hdr *hp;
4187 err = skb_maybe_pull_tail(skb,
4189 sizeof(struct ip_auth_hdr),
4194 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4195 nexthdr = hp->nexthdr;
4196 off += ipv6_authlen(hp);
4199 case IPPROTO_FRAGMENT: {
4200 struct frag_hdr *hp;
4202 err = skb_maybe_pull_tail(skb,
4204 sizeof(struct frag_hdr),
4209 hp = OPT_HDR(struct frag_hdr, skb, off);
4211 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4214 nexthdr = hp->nexthdr;
4215 off += sizeof(struct frag_hdr);
4226 if (!done || fragment)
4229 csum = skb_checksum_setup_ip(skb, nexthdr, off);
4231 return PTR_ERR(csum);
4234 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4235 &ipv6_hdr(skb)->daddr,
4236 skb->len - off, nexthdr, 0);
4244 * skb_checksum_setup - set up partial checksum offset
4245 * @skb: the skb to set up
4246 * @recalculate: if true the pseudo-header checksum will be recalculated
4248 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4252 switch (skb->protocol) {
4253 case htons(ETH_P_IP):
4254 err = skb_checksum_setup_ipv4(skb, recalculate);
4257 case htons(ETH_P_IPV6):
4258 err = skb_checksum_setup_ipv6(skb, recalculate);
4268 EXPORT_SYMBOL(skb_checksum_setup);
4271 * skb_checksum_maybe_trim - maybe trims the given skb
4272 * @skb: the skb to check
4273 * @transport_len: the data length beyond the network header
4275 * Checks whether the given skb has data beyond the given transport length.
4276 * If so, returns a cloned skb trimmed to this transport length.
4277 * Otherwise returns the provided skb. Returns NULL in error cases
4278 * (e.g. transport_len exceeds skb length or out-of-memory).
4280 * Caller needs to set the skb transport header and free any returned skb if it
4281 * differs from the provided skb.
4283 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4284 unsigned int transport_len)
4286 struct sk_buff *skb_chk;
4287 unsigned int len = skb_transport_offset(skb) + transport_len;
4292 else if (skb->len == len)
4295 skb_chk = skb_clone(skb, GFP_ATOMIC);
4299 ret = pskb_trim_rcsum(skb_chk, len);
4309 * skb_checksum_trimmed - validate checksum of an skb
4310 * @skb: the skb to check
4311 * @transport_len: the data length beyond the network header
4312 * @skb_chkf: checksum function to use
4314 * Applies the given checksum function skb_chkf to the provided skb.
4315 * Returns a checked and maybe trimmed skb. Returns NULL on error.
4317 * If the skb has data beyond the given transport length, then a
4318 * trimmed & cloned skb is checked and returned.
4320 * Caller needs to set the skb transport header and free any returned skb if it
4321 * differs from the provided skb.
4323 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4324 unsigned int transport_len,
4325 __sum16(*skb_chkf)(struct sk_buff *skb))
4327 struct sk_buff *skb_chk;
4328 unsigned int offset = skb_transport_offset(skb);
4331 skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4335 if (!pskb_may_pull(skb_chk, offset))
4338 skb_pull_rcsum(skb_chk, offset);
4339 ret = skb_chkf(skb_chk);
4340 skb_push_rcsum(skb_chk, offset);
4348 if (skb_chk && skb_chk != skb)
4354 EXPORT_SYMBOL(skb_checksum_trimmed);
4356 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4358 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4361 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4363 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4366 skb_release_head_state(skb);
4367 kmem_cache_free(skbuff_head_cache, skb);
4372 EXPORT_SYMBOL(kfree_skb_partial);
4375 * skb_try_coalesce - try to merge skb to prior one
4377 * @from: buffer to add
4378 * @fragstolen: pointer to boolean
4379 * @delta_truesize: how much more was allocated than was requested
4381 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4382 bool *fragstolen, int *delta_truesize)
4384 int i, delta, len = from->len;
4386 *fragstolen = false;
4391 if (len <= skb_tailroom(to)) {
4393 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4394 *delta_truesize = 0;
4398 if (skb_has_frag_list(to) || skb_has_frag_list(from))
4401 if (skb_headlen(from) != 0) {
4403 unsigned int offset;
4405 if (skb_shinfo(to)->nr_frags +
4406 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4409 if (skb_head_is_locked(from))
4412 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4414 page = virt_to_head_page(from->head);
4415 offset = from->data - (unsigned char *)page_address(page);
4417 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4418 page, offset, skb_headlen(from));
4421 if (skb_shinfo(to)->nr_frags +
4422 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4425 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4428 WARN_ON_ONCE(delta < len);
4430 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4431 skb_shinfo(from)->frags,
4432 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4433 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4435 if (!skb_cloned(from))
4436 skb_shinfo(from)->nr_frags = 0;
4438 /* if the skb is not cloned this does nothing
4439 * since we set nr_frags to 0.
4441 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4442 skb_frag_ref(from, i);
4444 to->truesize += delta;
4446 to->data_len += len;
4448 *delta_truesize = delta;
4451 EXPORT_SYMBOL(skb_try_coalesce);
4454 * skb_scrub_packet - scrub an skb
4456 * @skb: buffer to clean
4457 * @xnet: packet is crossing netns
4459 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4460 * into/from a tunnel. Some information have to be cleared during these
4462 * skb_scrub_packet can also be used to clean a skb before injecting it in
4463 * another namespace (@xnet == true). We have to clear all information in the
4464 * skb that could impact namespace isolation.
4466 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4468 skb->tstamp.tv64 = 0;
4469 skb->pkt_type = PACKET_HOST;
4475 nf_reset_trace(skb);
4477 #ifdef CONFIG_NET_SWITCHDEV
4478 skb->offload_fwd_mark = 0;
4488 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4491 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4495 * skb_gso_transport_seglen is used to determine the real size of the
4496 * individual segments, including Layer4 headers (TCP/UDP).
4498 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4500 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4502 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4503 unsigned int thlen = 0;
4505 if (skb->encapsulation) {
4506 thlen = skb_inner_transport_header(skb) -
4507 skb_transport_header(skb);
4509 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4510 thlen += inner_tcp_hdrlen(skb);
4511 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4512 thlen = tcp_hdrlen(skb);
4513 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
4514 thlen = sizeof(struct sctphdr);
4516 /* UFO sets gso_size to the size of the fragmentation
4517 * payload, i.e. the size of the L4 (UDP) header is already
4520 return thlen + shinfo->gso_size;
4522 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4525 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
4527 * There are a couple of instances where we have a GSO skb, and we
4528 * want to determine what size it would be after it is segmented.
4530 * We might want to check:
4531 * - L3+L4+payload size (e.g. IP forwarding)
4532 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
4534 * This is a helper to do that correctly considering GSO_BY_FRAGS.
4536 * @seg_len: The segmented length (from skb_gso_*_seglen). In the
4537 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
4539 * @max_len: The maximum permissible length.
4541 * Returns true if the segmented length <= max length.
4543 static inline bool skb_gso_size_check(const struct sk_buff *skb,
4544 unsigned int seg_len,
4545 unsigned int max_len) {
4546 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4547 const struct sk_buff *iter;
4549 if (shinfo->gso_size != GSO_BY_FRAGS)
4550 return seg_len <= max_len;
4552 /* Undo this so we can re-use header sizes */
4553 seg_len -= GSO_BY_FRAGS;
4555 skb_walk_frags(skb, iter) {
4556 if (seg_len + skb_headlen(iter) > max_len)
4564 * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4567 * @mtu: MTU to validate against
4569 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4572 bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4574 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
4576 EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4579 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
4582 * @len: length to validate against
4584 * skb_gso_validate_mac_len validates if a given skb will fit a wanted
4585 * length once split, including L2, L3 and L4 headers and the payload.
4587 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
4589 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
4591 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
4593 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4597 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4602 mac_len = skb->data - skb_mac_header(skb);
4603 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
4604 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
4605 mac_len - VLAN_HLEN - ETH_TLEN);
4607 skb->mac_header += VLAN_HLEN;
4611 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4613 struct vlan_hdr *vhdr;
4616 if (unlikely(skb_vlan_tag_present(skb))) {
4617 /* vlan_tci is already set-up so leave this for another time */
4621 skb = skb_share_check(skb, GFP_ATOMIC);
4624 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
4625 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
4628 vhdr = (struct vlan_hdr *)skb->data;
4629 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4630 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4632 skb_pull_rcsum(skb, VLAN_HLEN);
4633 vlan_set_encap_proto(skb, vhdr);
4635 skb = skb_reorder_vlan_header(skb);
4639 skb_reset_network_header(skb);
4640 skb_reset_transport_header(skb);
4641 skb_reset_mac_len(skb);
4649 EXPORT_SYMBOL(skb_vlan_untag);
4651 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4653 if (!pskb_may_pull(skb, write_len))
4656 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4659 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4661 EXPORT_SYMBOL(skb_ensure_writable);
4663 /* remove VLAN header from packet and update csum accordingly.
4664 * expects a non skb_vlan_tag_present skb with a vlan tag payload
4666 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4668 struct vlan_hdr *vhdr;
4669 int offset = skb->data - skb_mac_header(skb);
4672 if (WARN_ONCE(offset,
4673 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4678 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4682 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4684 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4685 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4687 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4688 __skb_pull(skb, VLAN_HLEN);
4690 vlan_set_encap_proto(skb, vhdr);
4691 skb->mac_header += VLAN_HLEN;
4693 if (skb_network_offset(skb) < ETH_HLEN)
4694 skb_set_network_header(skb, ETH_HLEN);
4696 skb_reset_mac_len(skb);
4700 EXPORT_SYMBOL(__skb_vlan_pop);
4702 /* Pop a vlan tag either from hwaccel or from payload.
4703 * Expects skb->data at mac header.
4705 int skb_vlan_pop(struct sk_buff *skb)
4711 if (likely(skb_vlan_tag_present(skb))) {
4714 if (unlikely(!eth_type_vlan(skb->protocol)))
4717 err = __skb_vlan_pop(skb, &vlan_tci);
4721 /* move next vlan tag to hw accel tag */
4722 if (likely(!eth_type_vlan(skb->protocol)))
4725 vlan_proto = skb->protocol;
4726 err = __skb_vlan_pop(skb, &vlan_tci);
4730 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4733 EXPORT_SYMBOL(skb_vlan_pop);
4735 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4736 * Expects skb->data at mac header.
4738 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4740 if (skb_vlan_tag_present(skb)) {
4741 int offset = skb->data - skb_mac_header(skb);
4744 if (WARN_ONCE(offset,
4745 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4750 err = __vlan_insert_tag(skb, skb->vlan_proto,
4751 skb_vlan_tag_get(skb));
4755 skb->protocol = skb->vlan_proto;
4756 skb->mac_len += VLAN_HLEN;
4758 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4760 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4763 EXPORT_SYMBOL(skb_vlan_push);
4766 * alloc_skb_with_frags - allocate skb with page frags
4768 * @header_len: size of linear part
4769 * @data_len: needed length in frags
4770 * @max_page_order: max page order desired.
4771 * @errcode: pointer to error code if any
4772 * @gfp_mask: allocation mask
4774 * This can be used to allocate a paged skb, given a maximal order for frags.
4776 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4777 unsigned long data_len,
4782 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4783 unsigned long chunk;
4784 struct sk_buff *skb;
4789 *errcode = -EMSGSIZE;
4790 /* Note this test could be relaxed, if we succeed to allocate
4791 * high order pages...
4793 if (npages > MAX_SKB_FRAGS)
4796 gfp_head = gfp_mask;
4797 if (gfp_head & __GFP_DIRECT_RECLAIM)
4798 gfp_head |= __GFP_REPEAT;
4800 *errcode = -ENOBUFS;
4801 skb = alloc_skb(header_len, gfp_head);
4805 skb->truesize += npages << PAGE_SHIFT;
4807 for (i = 0; npages > 0; i++) {
4808 int order = max_page_order;
4811 if (npages >= 1 << order) {
4812 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4819 /* Do not retry other high order allocations */
4825 page = alloc_page(gfp_mask);
4829 chunk = min_t(unsigned long, data_len,
4830 PAGE_SIZE << order);
4831 skb_fill_page_desc(skb, i, page, 0, chunk);
4833 npages -= 1 << order;
4841 EXPORT_SYMBOL(alloc_skb_with_frags);
4843 /* carve out the first off bytes from skb when off < headlen */
4844 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
4845 const int headlen, gfp_t gfp_mask)
4848 int size = skb_end_offset(skb);
4849 int new_hlen = headlen - off;
4852 size = SKB_DATA_ALIGN(size);
4854 if (skb_pfmemalloc(skb))
4855 gfp_mask |= __GFP_MEMALLOC;
4856 data = kmalloc_reserve(size +
4857 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4858 gfp_mask, NUMA_NO_NODE, NULL);
4862 size = SKB_WITH_OVERHEAD(ksize(data));
4864 /* Copy real data, and all frags */
4865 skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
4868 memcpy((struct skb_shared_info *)(data + size),
4870 offsetof(struct skb_shared_info,
4871 frags[skb_shinfo(skb)->nr_frags]));
4872 if (skb_cloned(skb)) {
4873 /* drop the old head gracefully */
4874 if (skb_orphan_frags(skb, gfp_mask)) {
4878 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
4879 skb_frag_ref(skb, i);
4880 if (skb_has_frag_list(skb))
4881 skb_clone_fraglist(skb);
4882 skb_release_data(skb);
4884 /* we can reuse existing recount- all we did was
4893 #ifdef NET_SKBUFF_DATA_USES_OFFSET
4896 skb->end = skb->head + size;
4898 skb_set_tail_pointer(skb, skb_headlen(skb));
4899 skb_headers_offset_update(skb, 0);
4903 atomic_set(&skb_shinfo(skb)->dataref, 1);
4908 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
4910 /* carve out the first eat bytes from skb's frag_list. May recurse into
4913 static int pskb_carve_frag_list(struct sk_buff *skb,
4914 struct skb_shared_info *shinfo, int eat,
4917 struct sk_buff *list = shinfo->frag_list;
4918 struct sk_buff *clone = NULL;
4919 struct sk_buff *insp = NULL;
4923 pr_err("Not enough bytes to eat. Want %d\n", eat);
4926 if (list->len <= eat) {
4927 /* Eaten as whole. */
4932 /* Eaten partially. */
4933 if (skb_shared(list)) {
4934 clone = skb_clone(list, gfp_mask);
4940 /* This may be pulled without problems. */
4943 if (pskb_carve(list, eat, gfp_mask) < 0) {
4951 /* Free pulled out fragments. */
4952 while ((list = shinfo->frag_list) != insp) {
4953 shinfo->frag_list = list->next;
4956 /* And insert new clone at head. */
4959 shinfo->frag_list = clone;
4964 /* carve off first len bytes from skb. Split line (off) is in the
4965 * non-linear part of skb
4967 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
4968 int pos, gfp_t gfp_mask)
4971 int size = skb_end_offset(skb);
4973 const int nfrags = skb_shinfo(skb)->nr_frags;
4974 struct skb_shared_info *shinfo;
4976 size = SKB_DATA_ALIGN(size);
4978 if (skb_pfmemalloc(skb))
4979 gfp_mask |= __GFP_MEMALLOC;
4980 data = kmalloc_reserve(size +
4981 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
4982 gfp_mask, NUMA_NO_NODE, NULL);
4986 size = SKB_WITH_OVERHEAD(ksize(data));
4988 memcpy((struct skb_shared_info *)(data + size),
4989 skb_shinfo(skb), offsetof(struct skb_shared_info,
4990 frags[skb_shinfo(skb)->nr_frags]));
4991 if (skb_orphan_frags(skb, gfp_mask)) {
4995 shinfo = (struct skb_shared_info *)(data + size);
4996 for (i = 0; i < nfrags; i++) {
4997 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
4999 if (pos + fsize > off) {
5000 shinfo->frags[k] = skb_shinfo(skb)->frags[i];
5004 * We have two variants in this case:
5005 * 1. Move all the frag to the second
5006 * part, if it is possible. F.e.
5007 * this approach is mandatory for TUX,
5008 * where splitting is expensive.
5009 * 2. Split is accurately. We make this.
5011 shinfo->frags[0].page_offset += off - pos;
5012 skb_frag_size_sub(&shinfo->frags[0], off - pos);
5014 skb_frag_ref(skb, i);
5019 shinfo->nr_frags = k;
5020 if (skb_has_frag_list(skb))
5021 skb_clone_fraglist(skb);
5023 /* split line is in frag list */
5024 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
5025 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
5026 if (skb_has_frag_list(skb))
5027 kfree_skb_list(skb_shinfo(skb)->frag_list);
5031 skb_release_data(skb);
5036 #ifdef NET_SKBUFF_DATA_USES_OFFSET
5039 skb->end = skb->head + size;
5041 skb_reset_tail_pointer(skb);
5042 skb_headers_offset_update(skb, 0);
5047 skb->data_len = skb->len;
5048 atomic_set(&skb_shinfo(skb)->dataref, 1);
5052 /* remove len bytes from the beginning of the skb */
5053 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
5055 int headlen = skb_headlen(skb);
5058 return pskb_carve_inside_header(skb, len, headlen, gfp);
5060 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
5063 /* Extract to_copy bytes starting at off from skb, and return this in
5066 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
5067 int to_copy, gfp_t gfp)
5069 struct sk_buff *clone = skb_clone(skb, gfp);
5074 if (pskb_carve(clone, off, gfp) < 0 ||
5075 pskb_trim(clone, to_copy)) {
5081 EXPORT_SYMBOL(pskb_extract);