2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
67 #include <net/protocol.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
78 struct kmem_cache *skbuff_head_cache __read_mostly;
79 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 * skb_panic - private function for out-of-line support
86 * @msg: skb_over_panic or skb_under_panic
88 * Out-of-line support for skb_put() and skb_push().
89 * Called via the wrapper skb_over_panic() or skb_under_panic().
90 * Keep out of line to prevent kernel bloat.
91 * __builtin_return_address is not used because it is not always reliable.
93 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
96 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
97 msg, addr, skb->len, sz, skb->head, skb->data,
98 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
103 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
105 skb_panic(skb, sz, addr, __func__);
108 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
110 skb_panic(skb, sz, addr, __func__);
114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115 * the caller if emergency pfmemalloc reserves are being used. If it is and
116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117 * may be used. Otherwise, the packet data may be discarded until enough
120 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
123 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
124 unsigned long ip, bool *pfmemalloc)
127 bool ret_pfmemalloc = false;
130 * Try a regular allocation, when that fails and we're not entitled
131 * to the reserves, fail.
133 obj = kmalloc_node_track_caller(size,
134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
136 if (obj || !(gfp_pfmemalloc_allowed(flags)))
139 /* Try again but now we are using pfmemalloc reserves */
140 ret_pfmemalloc = true;
141 obj = kmalloc_node_track_caller(size, flags, node);
145 *pfmemalloc = ret_pfmemalloc;
150 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
151 * 'private' fields and also do memory statistics to find all the
156 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
161 skb = kmem_cache_alloc_node(skbuff_head_cache,
162 gfp_mask & ~__GFP_DMA, node);
167 * Only clear those fields we need to clear, not those that we will
168 * actually initialise below. Hence, don't put any more fields after
169 * the tail pointer in struct sk_buff!
171 memset(skb, 0, offsetof(struct sk_buff, tail));
173 skb->truesize = sizeof(struct sk_buff);
174 atomic_set(&skb->users, 1);
176 skb->mac_header = (typeof(skb->mac_header))~0U;
182 * __alloc_skb - allocate a network buffer
183 * @size: size to allocate
184 * @gfp_mask: allocation mask
185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186 * instead of head cache and allocate a cloned (child) skb.
187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188 * allocations in case the data is required for writeback
189 * @node: numa node to allocate memory on
191 * Allocate a new &sk_buff. The returned buffer has no headroom and a
192 * tail room of at least size bytes. The object has a reference count
193 * of one. The return is the buffer. On a failure the return is %NULL.
195 * Buffers may only be allocated from interrupts using a @gfp_mask of
198 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
201 struct kmem_cache *cache;
202 struct skb_shared_info *shinfo;
207 cache = (flags & SKB_ALLOC_FCLONE)
208 ? skbuff_fclone_cache : skbuff_head_cache;
210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211 gfp_mask |= __GFP_MEMALLOC;
214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
219 /* We do our best to align skb_shared_info on a separate cache
220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
221 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
222 * Both skb->head and skb_shared_info are cache line aligned.
224 size = SKB_DATA_ALIGN(size);
225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
229 /* kmalloc(size) might give us more room than requested.
230 * Put skb_shared_info exactly at the end of allocated zone,
231 * to allow max possible filling before reallocation.
233 size = SKB_WITH_OVERHEAD(ksize(data));
234 prefetchw(data + size);
237 * Only clear those fields we need to clear, not those that we will
238 * actually initialise below. Hence, don't put any more fields after
239 * the tail pointer in struct sk_buff!
241 memset(skb, 0, offsetof(struct sk_buff, tail));
242 /* Account for allocated memory : skb + skb->head */
243 skb->truesize = SKB_TRUESIZE(size);
244 skb->pfmemalloc = pfmemalloc;
245 atomic_set(&skb->users, 1);
248 skb_reset_tail_pointer(skb);
249 skb->end = skb->tail + size;
250 skb->mac_header = (typeof(skb->mac_header))~0U;
251 skb->transport_header = (typeof(skb->transport_header))~0U;
253 /* make sure we initialize shinfo sequentially */
254 shinfo = skb_shinfo(skb);
255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
256 atomic_set(&shinfo->dataref, 1);
257 kmemcheck_annotate_variable(shinfo->destructor_arg);
259 if (flags & SKB_ALLOC_FCLONE) {
260 struct sk_buff_fclones *fclones;
262 fclones = container_of(skb, struct sk_buff_fclones, skb1);
264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
265 skb->fclone = SKB_FCLONE_ORIG;
266 atomic_set(&fclones->fclone_ref, 1);
268 fclones->skb2.fclone = SKB_FCLONE_CLONE;
269 fclones->skb2.pfmemalloc = pfmemalloc;
274 kmem_cache_free(cache, skb);
278 EXPORT_SYMBOL(__alloc_skb);
281 * build_skb - build a network buffer
282 * @data: data buffer provided by caller
283 * @frag_size: size of fragment, or 0 if head was kmalloced
285 * Allocate a new &sk_buff. Caller provides space holding head and
286 * skb_shared_info. @data must have been allocated by kmalloc() only if
287 * @frag_size is 0, otherwise data should come from the page allocator.
288 * The return is the new skb buffer.
289 * On a failure the return is %NULL, and @data is not freed.
291 * Before IO, driver allocates only data buffer where NIC put incoming frame
292 * Driver should add room at head (NET_SKB_PAD) and
293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
295 * before giving packet to stack.
296 * RX rings only contains data buffers, not full skbs.
298 struct sk_buff *build_skb(void *data, unsigned int frag_size)
300 struct skb_shared_info *shinfo;
302 unsigned int size = frag_size ? : ksize(data);
304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
310 memset(skb, 0, offsetof(struct sk_buff, tail));
311 skb->truesize = SKB_TRUESIZE(size);
312 skb->head_frag = frag_size != 0;
313 atomic_set(&skb->users, 1);
316 skb_reset_tail_pointer(skb);
317 skb->end = skb->tail + size;
318 skb->mac_header = (typeof(skb->mac_header))~0U;
319 skb->transport_header = (typeof(skb->transport_header))~0U;
321 /* make sure we initialize shinfo sequentially */
322 shinfo = skb_shinfo(skb);
323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324 atomic_set(&shinfo->dataref, 1);
325 kmemcheck_annotate_variable(shinfo->destructor_arg);
329 EXPORT_SYMBOL(build_skb);
331 struct netdev_alloc_cache {
332 struct page_frag frag;
333 /* we maintain a pagecount bias, so that we dont dirty cache line
334 * containing page->_count every time we allocate a fragment.
336 unsigned int pagecnt_bias;
338 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
340 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
342 struct netdev_alloc_cache *nc;
347 local_irq_save(flags);
348 nc = this_cpu_ptr(&netdev_alloc_cache);
349 if (unlikely(!nc->frag.page)) {
351 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
352 gfp_t gfp = gfp_mask;
355 gfp |= __GFP_COMP | __GFP_NOWARN;
356 nc->frag.page = alloc_pages(gfp, order);
357 if (likely(nc->frag.page))
362 nc->frag.size = PAGE_SIZE << order;
363 /* Even if we own the page, we do not use atomic_set().
364 * This would break get_page_unless_zero() users.
366 atomic_add(NETDEV_PAGECNT_MAX_BIAS - 1,
367 &nc->frag.page->_count);
368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
372 if (nc->frag.offset + fragsz > nc->frag.size) {
373 if (atomic_read(&nc->frag.page->_count) != nc->pagecnt_bias) {
374 if (!atomic_sub_and_test(nc->pagecnt_bias,
375 &nc->frag.page->_count))
377 /* OK, page count is 0, we can safely set it */
378 atomic_set(&nc->frag.page->_count,
379 NETDEV_PAGECNT_MAX_BIAS);
381 atomic_add(NETDEV_PAGECNT_MAX_BIAS - nc->pagecnt_bias,
382 &nc->frag.page->_count);
384 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
388 data = page_address(nc->frag.page) + nc->frag.offset;
389 nc->frag.offset += fragsz;
392 local_irq_restore(flags);
397 * netdev_alloc_frag - allocate a page fragment
398 * @fragsz: fragment size
400 * Allocates a frag from a page for receive buffer.
401 * Uses GFP_ATOMIC allocations.
403 void *netdev_alloc_frag(unsigned int fragsz)
405 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
407 EXPORT_SYMBOL(netdev_alloc_frag);
410 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
411 * @dev: network device to receive on
412 * @length: length to allocate
413 * @gfp_mask: get_free_pages mask, passed to alloc_skb
415 * Allocate a new &sk_buff and assign it a usage count of one. The
416 * buffer has unspecified headroom built in. Users should allocate
417 * the headroom they think they need without accounting for the
418 * built in space. The built in space is used for optimisations.
420 * %NULL is returned if there is no free memory.
422 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
423 unsigned int length, gfp_t gfp_mask)
425 struct sk_buff *skb = NULL;
426 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
427 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
429 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
432 if (sk_memalloc_socks())
433 gfp_mask |= __GFP_MEMALLOC;
435 data = __netdev_alloc_frag(fragsz, gfp_mask);
438 skb = build_skb(data, fragsz);
440 put_page(virt_to_head_page(data));
443 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
444 SKB_ALLOC_RX, NUMA_NO_NODE);
447 skb_reserve(skb, NET_SKB_PAD);
452 EXPORT_SYMBOL(__netdev_alloc_skb);
454 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
455 int size, unsigned int truesize)
457 skb_fill_page_desc(skb, i, page, off, size);
459 skb->data_len += size;
460 skb->truesize += truesize;
462 EXPORT_SYMBOL(skb_add_rx_frag);
464 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
465 unsigned int truesize)
467 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
469 skb_frag_size_add(frag, size);
471 skb->data_len += size;
472 skb->truesize += truesize;
474 EXPORT_SYMBOL(skb_coalesce_rx_frag);
476 static void skb_drop_list(struct sk_buff **listp)
478 kfree_skb_list(*listp);
482 static inline void skb_drop_fraglist(struct sk_buff *skb)
484 skb_drop_list(&skb_shinfo(skb)->frag_list);
487 static void skb_clone_fraglist(struct sk_buff *skb)
489 struct sk_buff *list;
491 skb_walk_frags(skb, list)
495 static void skb_free_head(struct sk_buff *skb)
498 put_page(virt_to_head_page(skb->head));
503 static void skb_release_data(struct sk_buff *skb)
505 struct skb_shared_info *shinfo = skb_shinfo(skb);
509 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
513 for (i = 0; i < shinfo->nr_frags; i++)
514 __skb_frag_unref(&shinfo->frags[i]);
517 * If skb buf is from userspace, we need to notify the caller
518 * the lower device DMA has done;
520 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
521 struct ubuf_info *uarg;
523 uarg = shinfo->destructor_arg;
525 uarg->callback(uarg, true);
528 if (shinfo->frag_list)
529 kfree_skb_list(shinfo->frag_list);
535 * Free an skbuff by memory without cleaning the state.
537 static void kfree_skbmem(struct sk_buff *skb)
539 struct sk_buff_fclones *fclones;
541 switch (skb->fclone) {
542 case SKB_FCLONE_UNAVAILABLE:
543 kmem_cache_free(skbuff_head_cache, skb);
546 case SKB_FCLONE_ORIG:
547 fclones = container_of(skb, struct sk_buff_fclones, skb1);
549 /* We usually free the clone (TX completion) before original skb
550 * This test would have no chance to be true for the clone,
551 * while here, branch prediction will be good.
553 if (atomic_read(&fclones->fclone_ref) == 1)
557 default: /* SKB_FCLONE_CLONE */
558 fclones = container_of(skb, struct sk_buff_fclones, skb2);
561 if (!atomic_dec_and_test(&fclones->fclone_ref))
564 kmem_cache_free(skbuff_fclone_cache, fclones);
567 static void skb_release_head_state(struct sk_buff *skb)
571 secpath_put(skb->sp);
573 if (skb->destructor) {
575 skb->destructor(skb);
577 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
578 nf_conntrack_put(skb->nfct);
580 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
581 nf_bridge_put(skb->nf_bridge);
583 /* XXX: IS this still necessary? - JHS */
584 #ifdef CONFIG_NET_SCHED
586 #ifdef CONFIG_NET_CLS_ACT
592 /* Free everything but the sk_buff shell. */
593 static void skb_release_all(struct sk_buff *skb)
595 skb_release_head_state(skb);
596 if (likely(skb->head))
597 skb_release_data(skb);
601 * __kfree_skb - private function
604 * Free an sk_buff. Release anything attached to the buffer.
605 * Clean the state. This is an internal helper function. Users should
606 * always call kfree_skb
609 void __kfree_skb(struct sk_buff *skb)
611 skb_release_all(skb);
614 EXPORT_SYMBOL(__kfree_skb);
617 * kfree_skb - free an sk_buff
618 * @skb: buffer to free
620 * Drop a reference to the buffer and free it if the usage count has
623 void kfree_skb(struct sk_buff *skb)
627 if (likely(atomic_read(&skb->users) == 1))
629 else if (likely(!atomic_dec_and_test(&skb->users)))
631 trace_kfree_skb(skb, __builtin_return_address(0));
634 EXPORT_SYMBOL(kfree_skb);
636 void kfree_skb_list(struct sk_buff *segs)
639 struct sk_buff *next = segs->next;
645 EXPORT_SYMBOL(kfree_skb_list);
648 * skb_tx_error - report an sk_buff xmit error
649 * @skb: buffer that triggered an error
651 * Report xmit error if a device callback is tracking this skb.
652 * skb must be freed afterwards.
654 void skb_tx_error(struct sk_buff *skb)
656 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
657 struct ubuf_info *uarg;
659 uarg = skb_shinfo(skb)->destructor_arg;
661 uarg->callback(uarg, false);
662 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
665 EXPORT_SYMBOL(skb_tx_error);
668 * consume_skb - free an skbuff
669 * @skb: buffer to free
671 * Drop a ref to the buffer and free it if the usage count has hit zero
672 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
673 * is being dropped after a failure and notes that
675 void consume_skb(struct sk_buff *skb)
679 if (likely(atomic_read(&skb->users) == 1))
681 else if (likely(!atomic_dec_and_test(&skb->users)))
683 trace_consume_skb(skb);
686 EXPORT_SYMBOL(consume_skb);
688 /* Make sure a field is enclosed inside headers_start/headers_end section */
689 #define CHECK_SKB_FIELD(field) \
690 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
691 offsetof(struct sk_buff, headers_start)); \
692 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
693 offsetof(struct sk_buff, headers_end)); \
695 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
697 new->tstamp = old->tstamp;
698 /* We do not copy old->sk */
700 memcpy(new->cb, old->cb, sizeof(old->cb));
701 skb_dst_copy(new, old);
703 new->sp = secpath_get(old->sp);
705 __nf_copy(new, old, false);
707 /* Note : this field could be in headers_start/headers_end section
708 * It is not yet because we do not want to have a 16 bit hole
710 new->queue_mapping = old->queue_mapping;
712 memcpy(&new->headers_start, &old->headers_start,
713 offsetof(struct sk_buff, headers_end) -
714 offsetof(struct sk_buff, headers_start));
715 CHECK_SKB_FIELD(protocol);
716 CHECK_SKB_FIELD(csum);
717 CHECK_SKB_FIELD(hash);
718 CHECK_SKB_FIELD(priority);
719 CHECK_SKB_FIELD(skb_iif);
720 CHECK_SKB_FIELD(vlan_proto);
721 CHECK_SKB_FIELD(vlan_tci);
722 CHECK_SKB_FIELD(transport_header);
723 CHECK_SKB_FIELD(network_header);
724 CHECK_SKB_FIELD(mac_header);
725 CHECK_SKB_FIELD(inner_protocol);
726 CHECK_SKB_FIELD(inner_transport_header);
727 CHECK_SKB_FIELD(inner_network_header);
728 CHECK_SKB_FIELD(inner_mac_header);
729 CHECK_SKB_FIELD(mark);
730 #ifdef CONFIG_NETWORK_SECMARK
731 CHECK_SKB_FIELD(secmark);
733 #ifdef CONFIG_NET_RX_BUSY_POLL
734 CHECK_SKB_FIELD(napi_id);
736 #ifdef CONFIG_NET_SCHED
737 CHECK_SKB_FIELD(tc_index);
738 #ifdef CONFIG_NET_CLS_ACT
739 CHECK_SKB_FIELD(tc_verd);
746 * You should not add any new code to this function. Add it to
747 * __copy_skb_header above instead.
749 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
751 #define C(x) n->x = skb->x
753 n->next = n->prev = NULL;
755 __copy_skb_header(n, skb);
760 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
763 n->destructor = NULL;
770 atomic_set(&n->users, 1);
772 atomic_inc(&(skb_shinfo(skb)->dataref));
780 * skb_morph - morph one skb into another
781 * @dst: the skb to receive the contents
782 * @src: the skb to supply the contents
784 * This is identical to skb_clone except that the target skb is
785 * supplied by the user.
787 * The target skb is returned upon exit.
789 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
791 skb_release_all(dst);
792 return __skb_clone(dst, src);
794 EXPORT_SYMBOL_GPL(skb_morph);
797 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
798 * @skb: the skb to modify
799 * @gfp_mask: allocation priority
801 * This must be called on SKBTX_DEV_ZEROCOPY skb.
802 * It will copy all frags into kernel and drop the reference
803 * to userspace pages.
805 * If this function is called from an interrupt gfp_mask() must be
808 * Returns 0 on success or a negative error code on failure
809 * to allocate kernel memory to copy to.
811 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
814 int num_frags = skb_shinfo(skb)->nr_frags;
815 struct page *page, *head = NULL;
816 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
818 for (i = 0; i < num_frags; i++) {
820 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
822 page = alloc_page(gfp_mask);
825 struct page *next = (struct page *)page_private(head);
831 vaddr = kmap_atomic(skb_frag_page(f));
832 memcpy(page_address(page),
833 vaddr + f->page_offset, skb_frag_size(f));
834 kunmap_atomic(vaddr);
835 set_page_private(page, (unsigned long)head);
839 /* skb frags release userspace buffers */
840 for (i = 0; i < num_frags; i++)
841 skb_frag_unref(skb, i);
843 uarg->callback(uarg, false);
845 /* skb frags point to kernel buffers */
846 for (i = num_frags - 1; i >= 0; i--) {
847 __skb_fill_page_desc(skb, i, head, 0,
848 skb_shinfo(skb)->frags[i].size);
849 head = (struct page *)page_private(head);
852 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
855 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
858 * skb_clone - duplicate an sk_buff
859 * @skb: buffer to clone
860 * @gfp_mask: allocation priority
862 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
863 * copies share the same packet data but not structure. The new
864 * buffer has a reference count of 1. If the allocation fails the
865 * function returns %NULL otherwise the new buffer is returned.
867 * If this function is called from an interrupt gfp_mask() must be
871 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
873 struct sk_buff_fclones *fclones = container_of(skb,
874 struct sk_buff_fclones,
878 if (skb_orphan_frags(skb, gfp_mask))
881 if (skb->fclone == SKB_FCLONE_ORIG &&
882 atomic_read(&fclones->fclone_ref) == 1) {
884 atomic_set(&fclones->fclone_ref, 2);
886 if (skb_pfmemalloc(skb))
887 gfp_mask |= __GFP_MEMALLOC;
889 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
893 kmemcheck_annotate_bitfield(n, flags1);
894 n->fclone = SKB_FCLONE_UNAVAILABLE;
897 return __skb_clone(n, skb);
899 EXPORT_SYMBOL(skb_clone);
901 static void skb_headers_offset_update(struct sk_buff *skb, int off)
903 /* Only adjust this if it actually is csum_start rather than csum */
904 if (skb->ip_summed == CHECKSUM_PARTIAL)
905 skb->csum_start += off;
906 /* {transport,network,mac}_header and tail are relative to skb->head */
907 skb->transport_header += off;
908 skb->network_header += off;
909 if (skb_mac_header_was_set(skb))
910 skb->mac_header += off;
911 skb->inner_transport_header += off;
912 skb->inner_network_header += off;
913 skb->inner_mac_header += off;
916 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
918 __copy_skb_header(new, old);
920 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
921 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
922 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
925 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
927 if (skb_pfmemalloc(skb))
933 * skb_copy - create private copy of an sk_buff
934 * @skb: buffer to copy
935 * @gfp_mask: allocation priority
937 * Make a copy of both an &sk_buff and its data. This is used when the
938 * caller wishes to modify the data and needs a private copy of the
939 * data to alter. Returns %NULL on failure or the pointer to the buffer
940 * on success. The returned buffer has a reference count of 1.
942 * As by-product this function converts non-linear &sk_buff to linear
943 * one, so that &sk_buff becomes completely private and caller is allowed
944 * to modify all the data of returned buffer. This means that this
945 * function is not recommended for use in circumstances when only
946 * header is going to be modified. Use pskb_copy() instead.
949 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
951 int headerlen = skb_headroom(skb);
952 unsigned int size = skb_end_offset(skb) + skb->data_len;
953 struct sk_buff *n = __alloc_skb(size, gfp_mask,
954 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
959 /* Set the data pointer */
960 skb_reserve(n, headerlen);
961 /* Set the tail pointer and length */
962 skb_put(n, skb->len);
964 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
967 copy_skb_header(n, skb);
970 EXPORT_SYMBOL(skb_copy);
973 * __pskb_copy_fclone - create copy of an sk_buff with private head.
974 * @skb: buffer to copy
975 * @headroom: headroom of new skb
976 * @gfp_mask: allocation priority
977 * @fclone: if true allocate the copy of the skb from the fclone
978 * cache instead of the head cache; it is recommended to set this
979 * to true for the cases where the copy will likely be cloned
981 * Make a copy of both an &sk_buff and part of its data, located
982 * in header. Fragmented data remain shared. This is used when
983 * the caller wishes to modify only header of &sk_buff and needs
984 * private copy of the header to alter. Returns %NULL on failure
985 * or the pointer to the buffer on success.
986 * The returned buffer has a reference count of 1.
989 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
990 gfp_t gfp_mask, bool fclone)
992 unsigned int size = skb_headlen(skb) + headroom;
993 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
994 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
999 /* Set the data pointer */
1000 skb_reserve(n, headroom);
1001 /* Set the tail pointer and length */
1002 skb_put(n, skb_headlen(skb));
1003 /* Copy the bytes */
1004 skb_copy_from_linear_data(skb, n->data, n->len);
1006 n->truesize += skb->data_len;
1007 n->data_len = skb->data_len;
1010 if (skb_shinfo(skb)->nr_frags) {
1013 if (skb_orphan_frags(skb, gfp_mask)) {
1018 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1019 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1020 skb_frag_ref(skb, i);
1022 skb_shinfo(n)->nr_frags = i;
1025 if (skb_has_frag_list(skb)) {
1026 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1027 skb_clone_fraglist(n);
1030 copy_skb_header(n, skb);
1034 EXPORT_SYMBOL(__pskb_copy_fclone);
1037 * pskb_expand_head - reallocate header of &sk_buff
1038 * @skb: buffer to reallocate
1039 * @nhead: room to add at head
1040 * @ntail: room to add at tail
1041 * @gfp_mask: allocation priority
1043 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1044 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1045 * reference count of 1. Returns zero in the case of success or error,
1046 * if expansion failed. In the last case, &sk_buff is not changed.
1048 * All the pointers pointing into skb header may change and must be
1049 * reloaded after call to this function.
1052 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1057 int size = nhead + skb_end_offset(skb) + ntail;
1062 if (skb_shared(skb))
1065 size = SKB_DATA_ALIGN(size);
1067 if (skb_pfmemalloc(skb))
1068 gfp_mask |= __GFP_MEMALLOC;
1069 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1070 gfp_mask, NUMA_NO_NODE, NULL);
1073 size = SKB_WITH_OVERHEAD(ksize(data));
1075 /* Copy only real data... and, alas, header. This should be
1076 * optimized for the cases when header is void.
1078 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1080 memcpy((struct skb_shared_info *)(data + size),
1082 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1085 * if shinfo is shared we must drop the old head gracefully, but if it
1086 * is not we can just drop the old head and let the existing refcount
1087 * be since all we did is relocate the values
1089 if (skb_cloned(skb)) {
1090 /* copy this zero copy skb frags */
1091 if (skb_orphan_frags(skb, gfp_mask))
1093 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1094 skb_frag_ref(skb, i);
1096 if (skb_has_frag_list(skb))
1097 skb_clone_fraglist(skb);
1099 skb_release_data(skb);
1103 off = (data + nhead) - skb->head;
1108 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1112 skb->end = skb->head + size;
1115 skb_headers_offset_update(skb, nhead);
1119 atomic_set(&skb_shinfo(skb)->dataref, 1);
1127 EXPORT_SYMBOL(pskb_expand_head);
1129 /* Make private copy of skb with writable head and some headroom */
1131 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1133 struct sk_buff *skb2;
1134 int delta = headroom - skb_headroom(skb);
1137 skb2 = pskb_copy(skb, GFP_ATOMIC);
1139 skb2 = skb_clone(skb, GFP_ATOMIC);
1140 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1148 EXPORT_SYMBOL(skb_realloc_headroom);
1151 * skb_copy_expand - copy and expand sk_buff
1152 * @skb: buffer to copy
1153 * @newheadroom: new free bytes at head
1154 * @newtailroom: new free bytes at tail
1155 * @gfp_mask: allocation priority
1157 * Make a copy of both an &sk_buff and its data and while doing so
1158 * allocate additional space.
1160 * This is used when the caller wishes to modify the data and needs a
1161 * private copy of the data to alter as well as more space for new fields.
1162 * Returns %NULL on failure or the pointer to the buffer
1163 * on success. The returned buffer has a reference count of 1.
1165 * You must pass %GFP_ATOMIC as the allocation priority if this function
1166 * is called from an interrupt.
1168 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1169 int newheadroom, int newtailroom,
1173 * Allocate the copy buffer
1175 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1176 gfp_mask, skb_alloc_rx_flag(skb),
1178 int oldheadroom = skb_headroom(skb);
1179 int head_copy_len, head_copy_off;
1184 skb_reserve(n, newheadroom);
1186 /* Set the tail pointer and length */
1187 skb_put(n, skb->len);
1189 head_copy_len = oldheadroom;
1191 if (newheadroom <= head_copy_len)
1192 head_copy_len = newheadroom;
1194 head_copy_off = newheadroom - head_copy_len;
1196 /* Copy the linear header and data. */
1197 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1198 skb->len + head_copy_len))
1201 copy_skb_header(n, skb);
1203 skb_headers_offset_update(n, newheadroom - oldheadroom);
1207 EXPORT_SYMBOL(skb_copy_expand);
1210 * skb_pad - zero pad the tail of an skb
1211 * @skb: buffer to pad
1212 * @pad: space to pad
1214 * Ensure that a buffer is followed by a padding area that is zero
1215 * filled. Used by network drivers which may DMA or transfer data
1216 * beyond the buffer end onto the wire.
1218 * May return error in out of memory cases. The skb is freed on error.
1221 int skb_pad(struct sk_buff *skb, int pad)
1226 /* If the skbuff is non linear tailroom is always zero.. */
1227 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1228 memset(skb->data+skb->len, 0, pad);
1232 ntail = skb->data_len + pad - (skb->end - skb->tail);
1233 if (likely(skb_cloned(skb) || ntail > 0)) {
1234 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1239 /* FIXME: The use of this function with non-linear skb's really needs
1242 err = skb_linearize(skb);
1246 memset(skb->data + skb->len, 0, pad);
1253 EXPORT_SYMBOL(skb_pad);
1256 * pskb_put - add data to the tail of a potentially fragmented buffer
1257 * @skb: start of the buffer to use
1258 * @tail: tail fragment of the buffer to use
1259 * @len: amount of data to add
1261 * This function extends the used data area of the potentially
1262 * fragmented buffer. @tail must be the last fragment of @skb -- or
1263 * @skb itself. If this would exceed the total buffer size the kernel
1264 * will panic. A pointer to the first byte of the extra data is
1268 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1271 skb->data_len += len;
1274 return skb_put(tail, len);
1276 EXPORT_SYMBOL_GPL(pskb_put);
1279 * skb_put - add data to a buffer
1280 * @skb: buffer to use
1281 * @len: amount of data to add
1283 * This function extends the used data area of the buffer. If this would
1284 * exceed the total buffer size the kernel will panic. A pointer to the
1285 * first byte of the extra data is returned.
1287 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1289 unsigned char *tmp = skb_tail_pointer(skb);
1290 SKB_LINEAR_ASSERT(skb);
1293 if (unlikely(skb->tail > skb->end))
1294 skb_over_panic(skb, len, __builtin_return_address(0));
1297 EXPORT_SYMBOL(skb_put);
1300 * skb_push - add data to the start of a buffer
1301 * @skb: buffer to use
1302 * @len: amount of data to add
1304 * This function extends the used data area of the buffer at the buffer
1305 * start. If this would exceed the total buffer headroom the kernel will
1306 * panic. A pointer to the first byte of the extra data is returned.
1308 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1312 if (unlikely(skb->data<skb->head))
1313 skb_under_panic(skb, len, __builtin_return_address(0));
1316 EXPORT_SYMBOL(skb_push);
1319 * skb_pull - remove data from the start of a buffer
1320 * @skb: buffer to use
1321 * @len: amount of data to remove
1323 * This function removes data from the start of a buffer, returning
1324 * the memory to the headroom. A pointer to the next data in the buffer
1325 * is returned. Once the data has been pulled future pushes will overwrite
1328 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1330 return skb_pull_inline(skb, len);
1332 EXPORT_SYMBOL(skb_pull);
1335 * skb_trim - remove end from a buffer
1336 * @skb: buffer to alter
1339 * Cut the length of a buffer down by removing data from the tail. If
1340 * the buffer is already under the length specified it is not modified.
1341 * The skb must be linear.
1343 void skb_trim(struct sk_buff *skb, unsigned int len)
1346 __skb_trim(skb, len);
1348 EXPORT_SYMBOL(skb_trim);
1350 /* Trims skb to length len. It can change skb pointers.
1353 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1355 struct sk_buff **fragp;
1356 struct sk_buff *frag;
1357 int offset = skb_headlen(skb);
1358 int nfrags = skb_shinfo(skb)->nr_frags;
1362 if (skb_cloned(skb) &&
1363 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1370 for (; i < nfrags; i++) {
1371 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1378 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1381 skb_shinfo(skb)->nr_frags = i;
1383 for (; i < nfrags; i++)
1384 skb_frag_unref(skb, i);
1386 if (skb_has_frag_list(skb))
1387 skb_drop_fraglist(skb);
1391 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1392 fragp = &frag->next) {
1393 int end = offset + frag->len;
1395 if (skb_shared(frag)) {
1396 struct sk_buff *nfrag;
1398 nfrag = skb_clone(frag, GFP_ATOMIC);
1399 if (unlikely(!nfrag))
1402 nfrag->next = frag->next;
1414 unlikely((err = pskb_trim(frag, len - offset))))
1418 skb_drop_list(&frag->next);
1423 if (len > skb_headlen(skb)) {
1424 skb->data_len -= skb->len - len;
1429 skb_set_tail_pointer(skb, len);
1434 EXPORT_SYMBOL(___pskb_trim);
1437 * __pskb_pull_tail - advance tail of skb header
1438 * @skb: buffer to reallocate
1439 * @delta: number of bytes to advance tail
1441 * The function makes a sense only on a fragmented &sk_buff,
1442 * it expands header moving its tail forward and copying necessary
1443 * data from fragmented part.
1445 * &sk_buff MUST have reference count of 1.
1447 * Returns %NULL (and &sk_buff does not change) if pull failed
1448 * or value of new tail of skb in the case of success.
1450 * All the pointers pointing into skb header may change and must be
1451 * reloaded after call to this function.
1454 /* Moves tail of skb head forward, copying data from fragmented part,
1455 * when it is necessary.
1456 * 1. It may fail due to malloc failure.
1457 * 2. It may change skb pointers.
1459 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1461 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1463 /* If skb has not enough free space at tail, get new one
1464 * plus 128 bytes for future expansions. If we have enough
1465 * room at tail, reallocate without expansion only if skb is cloned.
1467 int i, k, eat = (skb->tail + delta) - skb->end;
1469 if (eat > 0 || skb_cloned(skb)) {
1470 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1475 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1478 /* Optimization: no fragments, no reasons to preestimate
1479 * size of pulled pages. Superb.
1481 if (!skb_has_frag_list(skb))
1484 /* Estimate size of pulled pages. */
1486 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1487 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1494 /* If we need update frag list, we are in troubles.
1495 * Certainly, it possible to add an offset to skb data,
1496 * but taking into account that pulling is expected to
1497 * be very rare operation, it is worth to fight against
1498 * further bloating skb head and crucify ourselves here instead.
1499 * Pure masohism, indeed. 8)8)
1502 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1503 struct sk_buff *clone = NULL;
1504 struct sk_buff *insp = NULL;
1509 if (list->len <= eat) {
1510 /* Eaten as whole. */
1515 /* Eaten partially. */
1517 if (skb_shared(list)) {
1518 /* Sucks! We need to fork list. :-( */
1519 clone = skb_clone(list, GFP_ATOMIC);
1525 /* This may be pulled without
1529 if (!pskb_pull(list, eat)) {
1537 /* Free pulled out fragments. */
1538 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1539 skb_shinfo(skb)->frag_list = list->next;
1542 /* And insert new clone at head. */
1545 skb_shinfo(skb)->frag_list = clone;
1548 /* Success! Now we may commit changes to skb data. */
1553 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1554 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1557 skb_frag_unref(skb, i);
1560 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1562 skb_shinfo(skb)->frags[k].page_offset += eat;
1563 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1569 skb_shinfo(skb)->nr_frags = k;
1572 skb->data_len -= delta;
1574 return skb_tail_pointer(skb);
1576 EXPORT_SYMBOL(__pskb_pull_tail);
1579 * skb_copy_bits - copy bits from skb to kernel buffer
1581 * @offset: offset in source
1582 * @to: destination buffer
1583 * @len: number of bytes to copy
1585 * Copy the specified number of bytes from the source skb to the
1586 * destination buffer.
1589 * If its prototype is ever changed,
1590 * check arch/{*}/net/{*}.S files,
1591 * since it is called from BPF assembly code.
1593 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1595 int start = skb_headlen(skb);
1596 struct sk_buff *frag_iter;
1599 if (offset > (int)skb->len - len)
1603 if ((copy = start - offset) > 0) {
1606 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1607 if ((len -= copy) == 0)
1613 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1615 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1617 WARN_ON(start > offset + len);
1619 end = start + skb_frag_size(f);
1620 if ((copy = end - offset) > 0) {
1626 vaddr = kmap_atomic(skb_frag_page(f));
1628 vaddr + f->page_offset + offset - start,
1630 kunmap_atomic(vaddr);
1632 if ((len -= copy) == 0)
1640 skb_walk_frags(skb, frag_iter) {
1643 WARN_ON(start > offset + len);
1645 end = start + frag_iter->len;
1646 if ((copy = end - offset) > 0) {
1649 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1651 if ((len -= copy) == 0)
1665 EXPORT_SYMBOL(skb_copy_bits);
1668 * Callback from splice_to_pipe(), if we need to release some pages
1669 * at the end of the spd in case we error'ed out in filling the pipe.
1671 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1673 put_page(spd->pages[i]);
1676 static struct page *linear_to_page(struct page *page, unsigned int *len,
1677 unsigned int *offset,
1680 struct page_frag *pfrag = sk_page_frag(sk);
1682 if (!sk_page_frag_refill(sk, pfrag))
1685 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1687 memcpy(page_address(pfrag->page) + pfrag->offset,
1688 page_address(page) + *offset, *len);
1689 *offset = pfrag->offset;
1690 pfrag->offset += *len;
1695 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1697 unsigned int offset)
1699 return spd->nr_pages &&
1700 spd->pages[spd->nr_pages - 1] == page &&
1701 (spd->partial[spd->nr_pages - 1].offset +
1702 spd->partial[spd->nr_pages - 1].len == offset);
1706 * Fill page/offset/length into spd, if it can hold more pages.
1708 static bool spd_fill_page(struct splice_pipe_desc *spd,
1709 struct pipe_inode_info *pipe, struct page *page,
1710 unsigned int *len, unsigned int offset,
1714 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1718 page = linear_to_page(page, len, &offset, sk);
1722 if (spd_can_coalesce(spd, page, offset)) {
1723 spd->partial[spd->nr_pages - 1].len += *len;
1727 spd->pages[spd->nr_pages] = page;
1728 spd->partial[spd->nr_pages].len = *len;
1729 spd->partial[spd->nr_pages].offset = offset;
1735 static bool __splice_segment(struct page *page, unsigned int poff,
1736 unsigned int plen, unsigned int *off,
1738 struct splice_pipe_desc *spd, bool linear,
1740 struct pipe_inode_info *pipe)
1745 /* skip this segment if already processed */
1751 /* ignore any bits we already processed */
1757 unsigned int flen = min(*len, plen);
1759 if (spd_fill_page(spd, pipe, page, &flen, poff,
1765 } while (*len && plen);
1771 * Map linear and fragment data from the skb to spd. It reports true if the
1772 * pipe is full or if we already spliced the requested length.
1774 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1775 unsigned int *offset, unsigned int *len,
1776 struct splice_pipe_desc *spd, struct sock *sk)
1780 /* map the linear part :
1781 * If skb->head_frag is set, this 'linear' part is backed by a
1782 * fragment, and if the head is not shared with any clones then
1783 * we can avoid a copy since we own the head portion of this page.
1785 if (__splice_segment(virt_to_page(skb->data),
1786 (unsigned long) skb->data & (PAGE_SIZE - 1),
1789 skb_head_is_locked(skb),
1794 * then map the fragments
1796 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1797 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1799 if (__splice_segment(skb_frag_page(f),
1800 f->page_offset, skb_frag_size(f),
1801 offset, len, spd, false, sk, pipe))
1809 * Map data from the skb to a pipe. Should handle both the linear part,
1810 * the fragments, and the frag list. It does NOT handle frag lists within
1811 * the frag list, if such a thing exists. We'd probably need to recurse to
1812 * handle that cleanly.
1814 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1815 struct pipe_inode_info *pipe, unsigned int tlen,
1818 struct partial_page partial[MAX_SKB_FRAGS];
1819 struct page *pages[MAX_SKB_FRAGS];
1820 struct splice_pipe_desc spd = {
1823 .nr_pages_max = MAX_SKB_FRAGS,
1825 .ops = &nosteal_pipe_buf_ops,
1826 .spd_release = sock_spd_release,
1828 struct sk_buff *frag_iter;
1829 struct sock *sk = skb->sk;
1833 * __skb_splice_bits() only fails if the output has no room left,
1834 * so no point in going over the frag_list for the error case.
1836 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1842 * now see if we have a frag_list to map
1844 skb_walk_frags(skb, frag_iter) {
1847 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1854 * Drop the socket lock, otherwise we have reverse
1855 * locking dependencies between sk_lock and i_mutex
1856 * here as compared to sendfile(). We enter here
1857 * with the socket lock held, and splice_to_pipe() will
1858 * grab the pipe inode lock. For sendfile() emulation,
1859 * we call into ->sendpage() with the i_mutex lock held
1860 * and networking will grab the socket lock.
1863 ret = splice_to_pipe(pipe, &spd);
1871 * skb_store_bits - store bits from kernel buffer to skb
1872 * @skb: destination buffer
1873 * @offset: offset in destination
1874 * @from: source buffer
1875 * @len: number of bytes to copy
1877 * Copy the specified number of bytes from the source buffer to the
1878 * destination skb. This function handles all the messy bits of
1879 * traversing fragment lists and such.
1882 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1884 int start = skb_headlen(skb);
1885 struct sk_buff *frag_iter;
1888 if (offset > (int)skb->len - len)
1891 if ((copy = start - offset) > 0) {
1894 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1895 if ((len -= copy) == 0)
1901 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1902 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1905 WARN_ON(start > offset + len);
1907 end = start + skb_frag_size(frag);
1908 if ((copy = end - offset) > 0) {
1914 vaddr = kmap_atomic(skb_frag_page(frag));
1915 memcpy(vaddr + frag->page_offset + offset - start,
1917 kunmap_atomic(vaddr);
1919 if ((len -= copy) == 0)
1927 skb_walk_frags(skb, frag_iter) {
1930 WARN_ON(start > offset + len);
1932 end = start + frag_iter->len;
1933 if ((copy = end - offset) > 0) {
1936 if (skb_store_bits(frag_iter, offset - start,
1939 if ((len -= copy) == 0)
1952 EXPORT_SYMBOL(skb_store_bits);
1954 /* Checksum skb data. */
1955 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1956 __wsum csum, const struct skb_checksum_ops *ops)
1958 int start = skb_headlen(skb);
1959 int i, copy = start - offset;
1960 struct sk_buff *frag_iter;
1963 /* Checksum header. */
1967 csum = ops->update(skb->data + offset, copy, csum);
1968 if ((len -= copy) == 0)
1974 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1976 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1978 WARN_ON(start > offset + len);
1980 end = start + skb_frag_size(frag);
1981 if ((copy = end - offset) > 0) {
1987 vaddr = kmap_atomic(skb_frag_page(frag));
1988 csum2 = ops->update(vaddr + frag->page_offset +
1989 offset - start, copy, 0);
1990 kunmap_atomic(vaddr);
1991 csum = ops->combine(csum, csum2, pos, copy);
2000 skb_walk_frags(skb, frag_iter) {
2003 WARN_ON(start > offset + len);
2005 end = start + frag_iter->len;
2006 if ((copy = end - offset) > 0) {
2010 csum2 = __skb_checksum(frag_iter, offset - start,
2012 csum = ops->combine(csum, csum2, pos, copy);
2013 if ((len -= copy) == 0)
2024 EXPORT_SYMBOL(__skb_checksum);
2026 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2027 int len, __wsum csum)
2029 const struct skb_checksum_ops ops = {
2030 .update = csum_partial_ext,
2031 .combine = csum_block_add_ext,
2034 return __skb_checksum(skb, offset, len, csum, &ops);
2036 EXPORT_SYMBOL(skb_checksum);
2038 /* Both of above in one bottle. */
2040 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2041 u8 *to, int len, __wsum csum)
2043 int start = skb_headlen(skb);
2044 int i, copy = start - offset;
2045 struct sk_buff *frag_iter;
2052 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2054 if ((len -= copy) == 0)
2061 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2064 WARN_ON(start > offset + len);
2066 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2067 if ((copy = end - offset) > 0) {
2070 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2074 vaddr = kmap_atomic(skb_frag_page(frag));
2075 csum2 = csum_partial_copy_nocheck(vaddr +
2079 kunmap_atomic(vaddr);
2080 csum = csum_block_add(csum, csum2, pos);
2090 skb_walk_frags(skb, frag_iter) {
2094 WARN_ON(start > offset + len);
2096 end = start + frag_iter->len;
2097 if ((copy = end - offset) > 0) {
2100 csum2 = skb_copy_and_csum_bits(frag_iter,
2103 csum = csum_block_add(csum, csum2, pos);
2104 if ((len -= copy) == 0)
2115 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2118 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2119 * @from: source buffer
2121 * Calculates the amount of linear headroom needed in the 'to' skb passed
2122 * into skb_zerocopy().
2125 skb_zerocopy_headlen(const struct sk_buff *from)
2127 unsigned int hlen = 0;
2129 if (!from->head_frag ||
2130 skb_headlen(from) < L1_CACHE_BYTES ||
2131 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2132 hlen = skb_headlen(from);
2134 if (skb_has_frag_list(from))
2139 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2142 * skb_zerocopy - Zero copy skb to skb
2143 * @to: destination buffer
2144 * @from: source buffer
2145 * @len: number of bytes to copy from source buffer
2146 * @hlen: size of linear headroom in destination buffer
2148 * Copies up to `len` bytes from `from` to `to` by creating references
2149 * to the frags in the source buffer.
2151 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2152 * headroom in the `to` buffer.
2155 * 0: everything is OK
2156 * -ENOMEM: couldn't orphan frags of @from due to lack of memory
2157 * -EFAULT: skb_copy_bits() found some problem with skb geometry
2160 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2163 int plen = 0; /* length of skb->head fragment */
2166 unsigned int offset;
2168 BUG_ON(!from->head_frag && !hlen);
2170 /* dont bother with small payloads */
2171 if (len <= skb_tailroom(to))
2172 return skb_copy_bits(from, 0, skb_put(to, len), len);
2175 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2180 plen = min_t(int, skb_headlen(from), len);
2182 page = virt_to_head_page(from->head);
2183 offset = from->data - (unsigned char *)page_address(page);
2184 __skb_fill_page_desc(to, 0, page, offset, plen);
2191 to->truesize += len + plen;
2192 to->len += len + plen;
2193 to->data_len += len + plen;
2195 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2200 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2203 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2204 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2205 len -= skb_shinfo(to)->frags[j].size;
2206 skb_frag_ref(to, j);
2209 skb_shinfo(to)->nr_frags = j;
2213 EXPORT_SYMBOL_GPL(skb_zerocopy);
2215 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2220 if (skb->ip_summed == CHECKSUM_PARTIAL)
2221 csstart = skb_checksum_start_offset(skb);
2223 csstart = skb_headlen(skb);
2225 BUG_ON(csstart > skb_headlen(skb));
2227 skb_copy_from_linear_data(skb, to, csstart);
2230 if (csstart != skb->len)
2231 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2232 skb->len - csstart, 0);
2234 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2235 long csstuff = csstart + skb->csum_offset;
2237 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2240 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2243 * skb_dequeue - remove from the head of the queue
2244 * @list: list to dequeue from
2246 * Remove the head of the list. The list lock is taken so the function
2247 * may be used safely with other locking list functions. The head item is
2248 * returned or %NULL if the list is empty.
2251 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2253 unsigned long flags;
2254 struct sk_buff *result;
2256 spin_lock_irqsave(&list->lock, flags);
2257 result = __skb_dequeue(list);
2258 spin_unlock_irqrestore(&list->lock, flags);
2261 EXPORT_SYMBOL(skb_dequeue);
2264 * skb_dequeue_tail - remove from the tail of the queue
2265 * @list: list to dequeue from
2267 * Remove the tail of the list. The list lock is taken so the function
2268 * may be used safely with other locking list functions. The tail item is
2269 * returned or %NULL if the list is empty.
2271 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2273 unsigned long flags;
2274 struct sk_buff *result;
2276 spin_lock_irqsave(&list->lock, flags);
2277 result = __skb_dequeue_tail(list);
2278 spin_unlock_irqrestore(&list->lock, flags);
2281 EXPORT_SYMBOL(skb_dequeue_tail);
2284 * skb_queue_purge - empty a list
2285 * @list: list to empty
2287 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2288 * the list and one reference dropped. This function takes the list
2289 * lock and is atomic with respect to other list locking functions.
2291 void skb_queue_purge(struct sk_buff_head *list)
2293 struct sk_buff *skb;
2294 while ((skb = skb_dequeue(list)) != NULL)
2297 EXPORT_SYMBOL(skb_queue_purge);
2300 * skb_queue_head - queue a buffer at the list head
2301 * @list: list to use
2302 * @newsk: buffer to queue
2304 * Queue a buffer at the start of the list. This function takes the
2305 * list lock and can be used safely with other locking &sk_buff functions
2308 * A buffer cannot be placed on two lists at the same time.
2310 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2312 unsigned long flags;
2314 spin_lock_irqsave(&list->lock, flags);
2315 __skb_queue_head(list, newsk);
2316 spin_unlock_irqrestore(&list->lock, flags);
2318 EXPORT_SYMBOL(skb_queue_head);
2321 * skb_queue_tail - queue a buffer at the list tail
2322 * @list: list to use
2323 * @newsk: buffer to queue
2325 * Queue a buffer at the tail of the list. This function takes the
2326 * list lock and can be used safely with other locking &sk_buff functions
2329 * A buffer cannot be placed on two lists at the same time.
2331 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2333 unsigned long flags;
2335 spin_lock_irqsave(&list->lock, flags);
2336 __skb_queue_tail(list, newsk);
2337 spin_unlock_irqrestore(&list->lock, flags);
2339 EXPORT_SYMBOL(skb_queue_tail);
2342 * skb_unlink - remove a buffer from a list
2343 * @skb: buffer to remove
2344 * @list: list to use
2346 * Remove a packet from a list. The list locks are taken and this
2347 * function is atomic with respect to other list locked calls
2349 * You must know what list the SKB is on.
2351 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2353 unsigned long flags;
2355 spin_lock_irqsave(&list->lock, flags);
2356 __skb_unlink(skb, list);
2357 spin_unlock_irqrestore(&list->lock, flags);
2359 EXPORT_SYMBOL(skb_unlink);
2362 * skb_append - append a buffer
2363 * @old: buffer to insert after
2364 * @newsk: buffer to insert
2365 * @list: list to use
2367 * Place a packet after a given packet in a list. The list locks are taken
2368 * and this function is atomic with respect to other list locked calls.
2369 * A buffer cannot be placed on two lists at the same time.
2371 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2373 unsigned long flags;
2375 spin_lock_irqsave(&list->lock, flags);
2376 __skb_queue_after(list, old, newsk);
2377 spin_unlock_irqrestore(&list->lock, flags);
2379 EXPORT_SYMBOL(skb_append);
2382 * skb_insert - insert a buffer
2383 * @old: buffer to insert before
2384 * @newsk: buffer to insert
2385 * @list: list to use
2387 * Place a packet before a given packet in a list. The list locks are
2388 * taken and this function is atomic with respect to other list locked
2391 * A buffer cannot be placed on two lists at the same time.
2393 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2395 unsigned long flags;
2397 spin_lock_irqsave(&list->lock, flags);
2398 __skb_insert(newsk, old->prev, old, list);
2399 spin_unlock_irqrestore(&list->lock, flags);
2401 EXPORT_SYMBOL(skb_insert);
2403 static inline void skb_split_inside_header(struct sk_buff *skb,
2404 struct sk_buff* skb1,
2405 const u32 len, const int pos)
2409 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2411 /* And move data appendix as is. */
2412 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2413 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2415 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2416 skb_shinfo(skb)->nr_frags = 0;
2417 skb1->data_len = skb->data_len;
2418 skb1->len += skb1->data_len;
2421 skb_set_tail_pointer(skb, len);
2424 static inline void skb_split_no_header(struct sk_buff *skb,
2425 struct sk_buff* skb1,
2426 const u32 len, int pos)
2429 const int nfrags = skb_shinfo(skb)->nr_frags;
2431 skb_shinfo(skb)->nr_frags = 0;
2432 skb1->len = skb1->data_len = skb->len - len;
2434 skb->data_len = len - pos;
2436 for (i = 0; i < nfrags; i++) {
2437 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2439 if (pos + size > len) {
2440 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2444 * We have two variants in this case:
2445 * 1. Move all the frag to the second
2446 * part, if it is possible. F.e.
2447 * this approach is mandatory for TUX,
2448 * where splitting is expensive.
2449 * 2. Split is accurately. We make this.
2451 skb_frag_ref(skb, i);
2452 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2453 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2454 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2455 skb_shinfo(skb)->nr_frags++;
2459 skb_shinfo(skb)->nr_frags++;
2462 skb_shinfo(skb1)->nr_frags = k;
2466 * skb_split - Split fragmented skb to two parts at length len.
2467 * @skb: the buffer to split
2468 * @skb1: the buffer to receive the second part
2469 * @len: new length for skb
2471 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2473 int pos = skb_headlen(skb);
2475 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2476 if (len < pos) /* Split line is inside header. */
2477 skb_split_inside_header(skb, skb1, len, pos);
2478 else /* Second chunk has no header, nothing to copy. */
2479 skb_split_no_header(skb, skb1, len, pos);
2481 EXPORT_SYMBOL(skb_split);
2483 /* Shifting from/to a cloned skb is a no-go.
2485 * Caller cannot keep skb_shinfo related pointers past calling here!
2487 static int skb_prepare_for_shift(struct sk_buff *skb)
2489 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2493 * skb_shift - Shifts paged data partially from skb to another
2494 * @tgt: buffer into which tail data gets added
2495 * @skb: buffer from which the paged data comes from
2496 * @shiftlen: shift up to this many bytes
2498 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2499 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2500 * It's up to caller to free skb if everything was shifted.
2502 * If @tgt runs out of frags, the whole operation is aborted.
2504 * Skb cannot include anything else but paged data while tgt is allowed
2505 * to have non-paged data as well.
2507 * TODO: full sized shift could be optimized but that would need
2508 * specialized skb free'er to handle frags without up-to-date nr_frags.
2510 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2512 int from, to, merge, todo;
2513 struct skb_frag_struct *fragfrom, *fragto;
2515 BUG_ON(shiftlen > skb->len);
2516 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2520 to = skb_shinfo(tgt)->nr_frags;
2521 fragfrom = &skb_shinfo(skb)->frags[from];
2523 /* Actual merge is delayed until the point when we know we can
2524 * commit all, so that we don't have to undo partial changes
2527 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2528 fragfrom->page_offset)) {
2533 todo -= skb_frag_size(fragfrom);
2535 if (skb_prepare_for_shift(skb) ||
2536 skb_prepare_for_shift(tgt))
2539 /* All previous frag pointers might be stale! */
2540 fragfrom = &skb_shinfo(skb)->frags[from];
2541 fragto = &skb_shinfo(tgt)->frags[merge];
2543 skb_frag_size_add(fragto, shiftlen);
2544 skb_frag_size_sub(fragfrom, shiftlen);
2545 fragfrom->page_offset += shiftlen;
2553 /* Skip full, not-fitting skb to avoid expensive operations */
2554 if ((shiftlen == skb->len) &&
2555 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2558 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2561 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2562 if (to == MAX_SKB_FRAGS)
2565 fragfrom = &skb_shinfo(skb)->frags[from];
2566 fragto = &skb_shinfo(tgt)->frags[to];
2568 if (todo >= skb_frag_size(fragfrom)) {
2569 *fragto = *fragfrom;
2570 todo -= skb_frag_size(fragfrom);
2575 __skb_frag_ref(fragfrom);
2576 fragto->page = fragfrom->page;
2577 fragto->page_offset = fragfrom->page_offset;
2578 skb_frag_size_set(fragto, todo);
2580 fragfrom->page_offset += todo;
2581 skb_frag_size_sub(fragfrom, todo);
2589 /* Ready to "commit" this state change to tgt */
2590 skb_shinfo(tgt)->nr_frags = to;
2593 fragfrom = &skb_shinfo(skb)->frags[0];
2594 fragto = &skb_shinfo(tgt)->frags[merge];
2596 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2597 __skb_frag_unref(fragfrom);
2600 /* Reposition in the original skb */
2602 while (from < skb_shinfo(skb)->nr_frags)
2603 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2604 skb_shinfo(skb)->nr_frags = to;
2606 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2609 /* Most likely the tgt won't ever need its checksum anymore, skb on
2610 * the other hand might need it if it needs to be resent
2612 tgt->ip_summed = CHECKSUM_PARTIAL;
2613 skb->ip_summed = CHECKSUM_PARTIAL;
2615 /* Yak, is it really working this way? Some helper please? */
2616 skb->len -= shiftlen;
2617 skb->data_len -= shiftlen;
2618 skb->truesize -= shiftlen;
2619 tgt->len += shiftlen;
2620 tgt->data_len += shiftlen;
2621 tgt->truesize += shiftlen;
2627 * skb_prepare_seq_read - Prepare a sequential read of skb data
2628 * @skb: the buffer to read
2629 * @from: lower offset of data to be read
2630 * @to: upper offset of data to be read
2631 * @st: state variable
2633 * Initializes the specified state variable. Must be called before
2634 * invoking skb_seq_read() for the first time.
2636 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2637 unsigned int to, struct skb_seq_state *st)
2639 st->lower_offset = from;
2640 st->upper_offset = to;
2641 st->root_skb = st->cur_skb = skb;
2642 st->frag_idx = st->stepped_offset = 0;
2643 st->frag_data = NULL;
2645 EXPORT_SYMBOL(skb_prepare_seq_read);
2648 * skb_seq_read - Sequentially read skb data
2649 * @consumed: number of bytes consumed by the caller so far
2650 * @data: destination pointer for data to be returned
2651 * @st: state variable
2653 * Reads a block of skb data at @consumed relative to the
2654 * lower offset specified to skb_prepare_seq_read(). Assigns
2655 * the head of the data block to @data and returns the length
2656 * of the block or 0 if the end of the skb data or the upper
2657 * offset has been reached.
2659 * The caller is not required to consume all of the data
2660 * returned, i.e. @consumed is typically set to the number
2661 * of bytes already consumed and the next call to
2662 * skb_seq_read() will return the remaining part of the block.
2664 * Note 1: The size of each block of data returned can be arbitrary,
2665 * this limitation is the cost for zerocopy sequential
2666 * reads of potentially non linear data.
2668 * Note 2: Fragment lists within fragments are not implemented
2669 * at the moment, state->root_skb could be replaced with
2670 * a stack for this purpose.
2672 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2673 struct skb_seq_state *st)
2675 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2678 if (unlikely(abs_offset >= st->upper_offset)) {
2679 if (st->frag_data) {
2680 kunmap_atomic(st->frag_data);
2681 st->frag_data = NULL;
2687 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2689 if (abs_offset < block_limit && !st->frag_data) {
2690 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2691 return block_limit - abs_offset;
2694 if (st->frag_idx == 0 && !st->frag_data)
2695 st->stepped_offset += skb_headlen(st->cur_skb);
2697 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2698 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2699 block_limit = skb_frag_size(frag) + st->stepped_offset;
2701 if (abs_offset < block_limit) {
2703 st->frag_data = kmap_atomic(skb_frag_page(frag));
2705 *data = (u8 *) st->frag_data + frag->page_offset +
2706 (abs_offset - st->stepped_offset);
2708 return block_limit - abs_offset;
2711 if (st->frag_data) {
2712 kunmap_atomic(st->frag_data);
2713 st->frag_data = NULL;
2717 st->stepped_offset += skb_frag_size(frag);
2720 if (st->frag_data) {
2721 kunmap_atomic(st->frag_data);
2722 st->frag_data = NULL;
2725 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2726 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2729 } else if (st->cur_skb->next) {
2730 st->cur_skb = st->cur_skb->next;
2737 EXPORT_SYMBOL(skb_seq_read);
2740 * skb_abort_seq_read - Abort a sequential read of skb data
2741 * @st: state variable
2743 * Must be called if skb_seq_read() was not called until it
2746 void skb_abort_seq_read(struct skb_seq_state *st)
2749 kunmap_atomic(st->frag_data);
2751 EXPORT_SYMBOL(skb_abort_seq_read);
2753 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2755 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2756 struct ts_config *conf,
2757 struct ts_state *state)
2759 return skb_seq_read(offset, text, TS_SKB_CB(state));
2762 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2764 skb_abort_seq_read(TS_SKB_CB(state));
2768 * skb_find_text - Find a text pattern in skb data
2769 * @skb: the buffer to look in
2770 * @from: search offset
2772 * @config: textsearch configuration
2773 * @state: uninitialized textsearch state variable
2775 * Finds a pattern in the skb data according to the specified
2776 * textsearch configuration. Use textsearch_next() to retrieve
2777 * subsequent occurrences of the pattern. Returns the offset
2778 * to the first occurrence or UINT_MAX if no match was found.
2780 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2781 unsigned int to, struct ts_config *config,
2782 struct ts_state *state)
2786 config->get_next_block = skb_ts_get_next_block;
2787 config->finish = skb_ts_finish;
2789 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2791 ret = textsearch_find(config, state);
2792 return (ret <= to - from ? ret : UINT_MAX);
2794 EXPORT_SYMBOL(skb_find_text);
2797 * skb_append_datato_frags - append the user data to a skb
2798 * @sk: sock structure
2799 * @skb: skb structure to be appended with user data.
2800 * @getfrag: call back function to be used for getting the user data
2801 * @from: pointer to user message iov
2802 * @length: length of the iov message
2804 * Description: This procedure append the user data in the fragment part
2805 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2807 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2808 int (*getfrag)(void *from, char *to, int offset,
2809 int len, int odd, struct sk_buff *skb),
2810 void *from, int length)
2812 int frg_cnt = skb_shinfo(skb)->nr_frags;
2816 struct page_frag *pfrag = ¤t->task_frag;
2819 /* Return error if we don't have space for new frag */
2820 if (frg_cnt >= MAX_SKB_FRAGS)
2823 if (!sk_page_frag_refill(sk, pfrag))
2826 /* copy the user data to page */
2827 copy = min_t(int, length, pfrag->size - pfrag->offset);
2829 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2830 offset, copy, 0, skb);
2834 /* copy was successful so update the size parameters */
2835 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2838 pfrag->offset += copy;
2839 get_page(pfrag->page);
2841 skb->truesize += copy;
2842 atomic_add(copy, &sk->sk_wmem_alloc);
2844 skb->data_len += copy;
2848 } while (length > 0);
2852 EXPORT_SYMBOL(skb_append_datato_frags);
2855 * skb_pull_rcsum - pull skb and update receive checksum
2856 * @skb: buffer to update
2857 * @len: length of data pulled
2859 * This function performs an skb_pull on the packet and updates
2860 * the CHECKSUM_COMPLETE checksum. It should be used on
2861 * receive path processing instead of skb_pull unless you know
2862 * that the checksum difference is zero (e.g., a valid IP header)
2863 * or you are setting ip_summed to CHECKSUM_NONE.
2865 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2867 BUG_ON(len > skb->len);
2869 BUG_ON(skb->len < skb->data_len);
2870 skb_postpull_rcsum(skb, skb->data, len);
2871 return skb->data += len;
2873 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2876 * skb_segment - Perform protocol segmentation on skb.
2877 * @head_skb: buffer to segment
2878 * @features: features for the output path (see dev->features)
2880 * This function performs segmentation on the given skb. It returns
2881 * a pointer to the first in a list of new skbs for the segments.
2882 * In case of error it returns ERR_PTR(err).
2884 struct sk_buff *skb_segment(struct sk_buff *head_skb,
2885 netdev_features_t features)
2887 struct sk_buff *segs = NULL;
2888 struct sk_buff *tail = NULL;
2889 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2890 skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2891 unsigned int mss = skb_shinfo(head_skb)->gso_size;
2892 unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
2893 struct sk_buff *frag_skb = head_skb;
2894 unsigned int offset = doffset;
2895 unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2896 unsigned int headroom;
2900 int sg = !!(features & NETIF_F_SG);
2901 int nfrags = skb_shinfo(head_skb)->nr_frags;
2907 __skb_push(head_skb, doffset);
2908 proto = skb_network_protocol(head_skb, &dummy);
2909 if (unlikely(!proto))
2910 return ERR_PTR(-EINVAL);
2912 csum = !head_skb->encap_hdr_csum &&
2913 !!can_checksum_protocol(features, proto);
2915 headroom = skb_headroom(head_skb);
2916 pos = skb_headlen(head_skb);
2919 struct sk_buff *nskb;
2920 skb_frag_t *nskb_frag;
2924 len = head_skb->len - offset;
2928 hsize = skb_headlen(head_skb) - offset;
2931 if (hsize > len || !sg)
2934 if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
2935 (skb_headlen(list_skb) == len || sg)) {
2936 BUG_ON(skb_headlen(list_skb) > len);
2939 nfrags = skb_shinfo(list_skb)->nr_frags;
2940 frag = skb_shinfo(list_skb)->frags;
2941 frag_skb = list_skb;
2942 pos += skb_headlen(list_skb);
2944 while (pos < offset + len) {
2945 BUG_ON(i >= nfrags);
2947 size = skb_frag_size(frag);
2948 if (pos + size > offset + len)
2956 nskb = skb_clone(list_skb, GFP_ATOMIC);
2957 list_skb = list_skb->next;
2959 if (unlikely(!nskb))
2962 if (unlikely(pskb_trim(nskb, len))) {
2967 hsize = skb_end_offset(nskb);
2968 if (skb_cow_head(nskb, doffset + headroom)) {
2973 nskb->truesize += skb_end_offset(nskb) - hsize;
2974 skb_release_head_state(nskb);
2975 __skb_push(nskb, doffset);
2977 nskb = __alloc_skb(hsize + doffset + headroom,
2978 GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
2981 if (unlikely(!nskb))
2984 skb_reserve(nskb, headroom);
2985 __skb_put(nskb, doffset);
2994 __copy_skb_header(nskb, head_skb);
2996 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
2997 skb_reset_mac_len(nskb);
2999 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3000 nskb->data - tnl_hlen,
3001 doffset + tnl_hlen);
3003 if (nskb->len == len + doffset)
3004 goto perform_csum_check;
3006 if (!sg && !nskb->remcsum_offload) {
3007 nskb->ip_summed = CHECKSUM_NONE;
3008 nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3011 SKB_GSO_CB(nskb)->csum_start =
3012 skb_headroom(nskb) + doffset;
3016 nskb_frag = skb_shinfo(nskb)->frags;
3018 skb_copy_from_linear_data_offset(head_skb, offset,
3019 skb_put(nskb, hsize), hsize);
3021 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3024 while (pos < offset + len) {
3026 BUG_ON(skb_headlen(list_skb));
3029 nfrags = skb_shinfo(list_skb)->nr_frags;
3030 frag = skb_shinfo(list_skb)->frags;
3031 frag_skb = list_skb;
3035 list_skb = list_skb->next;
3038 if (unlikely(skb_shinfo(nskb)->nr_frags >=
3040 net_warn_ratelimited(
3041 "skb_segment: too many frags: %u %u\n",
3046 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3050 __skb_frag_ref(nskb_frag);
3051 size = skb_frag_size(nskb_frag);
3054 nskb_frag->page_offset += offset - pos;
3055 skb_frag_size_sub(nskb_frag, offset - pos);
3058 skb_shinfo(nskb)->nr_frags++;
3060 if (pos + size <= offset + len) {
3065 skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3073 nskb->data_len = len - hsize;
3074 nskb->len += nskb->data_len;
3075 nskb->truesize += nskb->data_len;
3078 if (!csum && !nskb->remcsum_offload) {
3079 nskb->csum = skb_checksum(nskb, doffset,
3080 nskb->len - doffset, 0);
3081 nskb->ip_summed = CHECKSUM_NONE;
3082 SKB_GSO_CB(nskb)->csum_start =
3083 skb_headroom(nskb) + doffset;
3085 } while ((offset += len) < head_skb->len);
3087 /* Some callers want to get the end of the list.
3088 * Put it in segs->prev to avoid walking the list.
3089 * (see validate_xmit_skb_list() for example)
3093 /* Following permits correct backpressure, for protocols
3094 * using skb_set_owner_w().
3095 * Idea is to tranfert ownership from head_skb to last segment.
3097 if (head_skb->destructor == sock_wfree) {
3098 swap(tail->truesize, head_skb->truesize);
3099 swap(tail->destructor, head_skb->destructor);
3100 swap(tail->sk, head_skb->sk);
3105 kfree_skb_list(segs);
3106 return ERR_PTR(err);
3108 EXPORT_SYMBOL_GPL(skb_segment);
3110 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3112 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3113 unsigned int offset = skb_gro_offset(skb);
3114 unsigned int headlen = skb_headlen(skb);
3115 struct sk_buff *nskb, *lp, *p = *head;
3116 unsigned int len = skb_gro_len(skb);
3117 unsigned int delta_truesize;
3118 unsigned int headroom;
3120 if (unlikely(p->len + len >= 65536))
3123 lp = NAPI_GRO_CB(p)->last;
3124 pinfo = skb_shinfo(lp);
3126 if (headlen <= offset) {
3129 int i = skbinfo->nr_frags;
3130 int nr_frags = pinfo->nr_frags + i;
3132 if (nr_frags > MAX_SKB_FRAGS)
3136 pinfo->nr_frags = nr_frags;
3137 skbinfo->nr_frags = 0;
3139 frag = pinfo->frags + nr_frags;
3140 frag2 = skbinfo->frags + i;
3145 frag->page_offset += offset;
3146 skb_frag_size_sub(frag, offset);
3148 /* all fragments truesize : remove (head size + sk_buff) */
3149 delta_truesize = skb->truesize -
3150 SKB_TRUESIZE(skb_end_offset(skb));
3152 skb->truesize -= skb->data_len;
3153 skb->len -= skb->data_len;
3156 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3158 } else if (skb->head_frag) {
3159 int nr_frags = pinfo->nr_frags;
3160 skb_frag_t *frag = pinfo->frags + nr_frags;
3161 struct page *page = virt_to_head_page(skb->head);
3162 unsigned int first_size = headlen - offset;
3163 unsigned int first_offset;
3165 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3168 first_offset = skb->data -
3169 (unsigned char *)page_address(page) +
3172 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3174 frag->page.p = page;
3175 frag->page_offset = first_offset;
3176 skb_frag_size_set(frag, first_size);
3178 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3179 /* We dont need to clear skbinfo->nr_frags here */
3181 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3182 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3185 /* switch back to head shinfo */
3186 pinfo = skb_shinfo(p);
3188 if (pinfo->frag_list)
3190 if (skb_gro_len(p) != pinfo->gso_size)
3193 headroom = skb_headroom(p);
3194 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3195 if (unlikely(!nskb))
3198 __copy_skb_header(nskb, p);
3199 nskb->mac_len = p->mac_len;
3201 skb_reserve(nskb, headroom);
3202 __skb_put(nskb, skb_gro_offset(p));
3204 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3205 skb_set_network_header(nskb, skb_network_offset(p));
3206 skb_set_transport_header(nskb, skb_transport_offset(p));
3208 __skb_pull(p, skb_gro_offset(p));
3209 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3210 p->data - skb_mac_header(p));
3212 skb_shinfo(nskb)->frag_list = p;
3213 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3214 pinfo->gso_size = 0;
3215 __skb_header_release(p);
3216 NAPI_GRO_CB(nskb)->last = p;
3218 nskb->data_len += p->len;
3219 nskb->truesize += p->truesize;
3220 nskb->len += p->len;
3223 nskb->next = p->next;
3229 delta_truesize = skb->truesize;
3230 if (offset > headlen) {
3231 unsigned int eat = offset - headlen;
3233 skbinfo->frags[0].page_offset += eat;
3234 skb_frag_size_sub(&skbinfo->frags[0], eat);
3235 skb->data_len -= eat;
3240 __skb_pull(skb, offset);
3242 if (NAPI_GRO_CB(p)->last == p)
3243 skb_shinfo(p)->frag_list = skb;
3245 NAPI_GRO_CB(p)->last->next = skb;
3246 NAPI_GRO_CB(p)->last = skb;
3247 __skb_header_release(skb);
3251 NAPI_GRO_CB(p)->count++;
3253 p->truesize += delta_truesize;
3256 lp->data_len += len;
3257 lp->truesize += delta_truesize;
3260 NAPI_GRO_CB(skb)->same_flow = 1;
3264 void __init skb_init(void)
3266 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3267 sizeof(struct sk_buff),
3269 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3271 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3272 sizeof(struct sk_buff_fclones),
3274 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3279 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3280 * @skb: Socket buffer containing the buffers to be mapped
3281 * @sg: The scatter-gather list to map into
3282 * @offset: The offset into the buffer's contents to start mapping
3283 * @len: Length of buffer space to be mapped
3285 * Fill the specified scatter-gather list with mappings/pointers into a
3286 * region of the buffer space attached to a socket buffer.
3289 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3291 int start = skb_headlen(skb);
3292 int i, copy = start - offset;
3293 struct sk_buff *frag_iter;
3299 sg_set_buf(sg, skb->data + offset, copy);
3301 if ((len -= copy) == 0)
3306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3309 WARN_ON(start > offset + len);
3311 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3312 if ((copy = end - offset) > 0) {
3313 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3317 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3318 frag->page_offset+offset-start);
3327 skb_walk_frags(skb, frag_iter) {
3330 WARN_ON(start > offset + len);
3332 end = start + frag_iter->len;
3333 if ((copy = end - offset) > 0) {
3336 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3338 if ((len -= copy) == 0)
3348 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3349 * sglist without mark the sg which contain last skb data as the end.
3350 * So the caller can mannipulate sg list as will when padding new data after
3351 * the first call without calling sg_unmark_end to expend sg list.
3353 * Scenario to use skb_to_sgvec_nomark:
3355 * 2. skb_to_sgvec_nomark(payload1)
3356 * 3. skb_to_sgvec_nomark(payload2)
3358 * This is equivalent to:
3360 * 2. skb_to_sgvec(payload1)
3362 * 4. skb_to_sgvec(payload2)
3364 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3365 * is more preferable.
3367 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3368 int offset, int len)
3370 return __skb_to_sgvec(skb, sg, offset, len);
3372 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3374 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3376 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3378 sg_mark_end(&sg[nsg - 1]);
3382 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3385 * skb_cow_data - Check that a socket buffer's data buffers are writable
3386 * @skb: The socket buffer to check.
3387 * @tailbits: Amount of trailing space to be added
3388 * @trailer: Returned pointer to the skb where the @tailbits space begins
3390 * Make sure that the data buffers attached to a socket buffer are
3391 * writable. If they are not, private copies are made of the data buffers
3392 * and the socket buffer is set to use these instead.
3394 * If @tailbits is given, make sure that there is space to write @tailbits
3395 * bytes of data beyond current end of socket buffer. @trailer will be
3396 * set to point to the skb in which this space begins.
3398 * The number of scatterlist elements required to completely map the
3399 * COW'd and extended socket buffer will be returned.
3401 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3405 struct sk_buff *skb1, **skb_p;
3407 /* If skb is cloned or its head is paged, reallocate
3408 * head pulling out all the pages (pages are considered not writable
3409 * at the moment even if they are anonymous).
3411 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3412 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3415 /* Easy case. Most of packets will go this way. */
3416 if (!skb_has_frag_list(skb)) {
3417 /* A little of trouble, not enough of space for trailer.
3418 * This should not happen, when stack is tuned to generate
3419 * good frames. OK, on miss we reallocate and reserve even more
3420 * space, 128 bytes is fair. */
3422 if (skb_tailroom(skb) < tailbits &&
3423 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3431 /* Misery. We are in troubles, going to mincer fragments... */
3434 skb_p = &skb_shinfo(skb)->frag_list;
3437 while ((skb1 = *skb_p) != NULL) {
3440 /* The fragment is partially pulled by someone,
3441 * this can happen on input. Copy it and everything
3444 if (skb_shared(skb1))
3447 /* If the skb is the last, worry about trailer. */
3449 if (skb1->next == NULL && tailbits) {
3450 if (skb_shinfo(skb1)->nr_frags ||
3451 skb_has_frag_list(skb1) ||
3452 skb_tailroom(skb1) < tailbits)
3453 ntail = tailbits + 128;
3459 skb_shinfo(skb1)->nr_frags ||
3460 skb_has_frag_list(skb1)) {
3461 struct sk_buff *skb2;
3463 /* Fuck, we are miserable poor guys... */
3465 skb2 = skb_copy(skb1, GFP_ATOMIC);
3467 skb2 = skb_copy_expand(skb1,
3471 if (unlikely(skb2 == NULL))
3475 skb_set_owner_w(skb2, skb1->sk);
3477 /* Looking around. Are we still alive?
3478 * OK, link new skb, drop old one */
3480 skb2->next = skb1->next;
3487 skb_p = &skb1->next;
3492 EXPORT_SYMBOL_GPL(skb_cow_data);
3494 static void sock_rmem_free(struct sk_buff *skb)
3496 struct sock *sk = skb->sk;
3498 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3502 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3504 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3506 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3507 (unsigned int)sk->sk_rcvbuf)
3512 skb->destructor = sock_rmem_free;
3513 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3515 /* before exiting rcu section, make sure dst is refcounted */
3518 skb_queue_tail(&sk->sk_error_queue, skb);
3519 if (!sock_flag(sk, SOCK_DEAD))
3520 sk->sk_data_ready(sk);
3523 EXPORT_SYMBOL(sock_queue_err_skb);
3525 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3527 struct sk_buff_head *q = &sk->sk_error_queue;
3528 struct sk_buff *skb, *skb_next;
3531 spin_lock_bh(&q->lock);
3532 skb = __skb_dequeue(q);
3533 if (skb && (skb_next = skb_peek(q)))
3534 err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3535 spin_unlock_bh(&q->lock);
3539 sk->sk_error_report(sk);
3543 EXPORT_SYMBOL(sock_dequeue_err_skb);
3546 * skb_clone_sk - create clone of skb, and take reference to socket
3547 * @skb: the skb to clone
3549 * This function creates a clone of a buffer that holds a reference on
3550 * sk_refcnt. Buffers created via this function are meant to be
3551 * returned using sock_queue_err_skb, or free via kfree_skb.
3553 * When passing buffers allocated with this function to sock_queue_err_skb
3554 * it is necessary to wrap the call with sock_hold/sock_put in order to
3555 * prevent the socket from being released prior to being enqueued on
3556 * the sk_error_queue.
3558 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3560 struct sock *sk = skb->sk;
3561 struct sk_buff *clone;
3563 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3566 clone = skb_clone(skb, GFP_ATOMIC);
3573 clone->destructor = sock_efree;
3577 EXPORT_SYMBOL(skb_clone_sk);
3579 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3583 struct sock_exterr_skb *serr;
3586 serr = SKB_EXT_ERR(skb);
3587 memset(serr, 0, sizeof(*serr));
3588 serr->ee.ee_errno = ENOMSG;
3589 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3590 serr->ee.ee_info = tstype;
3591 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3592 serr->ee.ee_data = skb_shinfo(skb)->tskey;
3593 if (sk->sk_protocol == IPPROTO_TCP)
3594 serr->ee.ee_data -= sk->sk_tskey;
3597 err = sock_queue_err_skb(sk, skb);
3603 void skb_complete_tx_timestamp(struct sk_buff *skb,
3604 struct skb_shared_hwtstamps *hwtstamps)
3606 struct sock *sk = skb->sk;
3608 /* take a reference to prevent skb_orphan() from freeing the socket */
3611 *skb_hwtstamps(skb) = *hwtstamps;
3612 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3616 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3618 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3619 struct skb_shared_hwtstamps *hwtstamps,
3620 struct sock *sk, int tstype)
3622 struct sk_buff *skb;
3628 *skb_hwtstamps(orig_skb) = *hwtstamps;
3630 orig_skb->tstamp = ktime_get_real();
3632 skb = skb_clone(orig_skb, GFP_ATOMIC);
3636 __skb_complete_tx_timestamp(skb, sk, tstype);
3638 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3640 void skb_tstamp_tx(struct sk_buff *orig_skb,
3641 struct skb_shared_hwtstamps *hwtstamps)
3643 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3646 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3648 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3650 struct sock *sk = skb->sk;
3651 struct sock_exterr_skb *serr;
3654 skb->wifi_acked_valid = 1;
3655 skb->wifi_acked = acked;
3657 serr = SKB_EXT_ERR(skb);
3658 memset(serr, 0, sizeof(*serr));
3659 serr->ee.ee_errno = ENOMSG;
3660 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3662 /* take a reference to prevent skb_orphan() from freeing the socket */
3665 err = sock_queue_err_skb(sk, skb);
3671 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3675 * skb_partial_csum_set - set up and verify partial csum values for packet
3676 * @skb: the skb to set
3677 * @start: the number of bytes after skb->data to start checksumming.
3678 * @off: the offset from start to place the checksum.
3680 * For untrusted partially-checksummed packets, we need to make sure the values
3681 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3683 * This function checks and sets those values and skb->ip_summed: if this
3684 * returns false you should drop the packet.
3686 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3688 if (unlikely(start > skb_headlen(skb)) ||
3689 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3690 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3691 start, off, skb_headlen(skb));
3694 skb->ip_summed = CHECKSUM_PARTIAL;
3695 skb->csum_start = skb_headroom(skb) + start;
3696 skb->csum_offset = off;
3697 skb_set_transport_header(skb, start);
3700 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3702 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3705 if (skb_headlen(skb) >= len)
3708 /* If we need to pullup then pullup to the max, so we
3709 * won't need to do it again.
3714 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3717 if (skb_headlen(skb) < len)
3723 #define MAX_TCP_HDR_LEN (15 * 4)
3725 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3726 typeof(IPPROTO_IP) proto,
3733 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3734 off + MAX_TCP_HDR_LEN);
3735 if (!err && !skb_partial_csum_set(skb, off,
3736 offsetof(struct tcphdr,
3739 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3742 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3743 off + sizeof(struct udphdr));
3744 if (!err && !skb_partial_csum_set(skb, off,
3745 offsetof(struct udphdr,
3748 return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3751 return ERR_PTR(-EPROTO);
3754 /* This value should be large enough to cover a tagged ethernet header plus
3755 * maximally sized IP and TCP or UDP headers.
3757 #define MAX_IP_HDR_LEN 128
3759 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3768 err = skb_maybe_pull_tail(skb,
3769 sizeof(struct iphdr),
3774 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3777 off = ip_hdrlen(skb);
3784 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3786 return PTR_ERR(csum);
3789 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3792 ip_hdr(skb)->protocol, 0);
3799 /* This value should be large enough to cover a tagged ethernet header plus
3800 * an IPv6 header, all options, and a maximal TCP or UDP header.
3802 #define MAX_IPV6_HDR_LEN 256
3804 #define OPT_HDR(type, skb, off) \
3805 (type *)(skb_network_header(skb) + (off))
3807 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3820 off = sizeof(struct ipv6hdr);
3822 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3826 nexthdr = ipv6_hdr(skb)->nexthdr;
3828 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3829 while (off <= len && !done) {
3831 case IPPROTO_DSTOPTS:
3832 case IPPROTO_HOPOPTS:
3833 case IPPROTO_ROUTING: {
3834 struct ipv6_opt_hdr *hp;
3836 err = skb_maybe_pull_tail(skb,
3838 sizeof(struct ipv6_opt_hdr),
3843 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3844 nexthdr = hp->nexthdr;
3845 off += ipv6_optlen(hp);
3849 struct ip_auth_hdr *hp;
3851 err = skb_maybe_pull_tail(skb,
3853 sizeof(struct ip_auth_hdr),
3858 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3859 nexthdr = hp->nexthdr;
3860 off += ipv6_authlen(hp);
3863 case IPPROTO_FRAGMENT: {
3864 struct frag_hdr *hp;
3866 err = skb_maybe_pull_tail(skb,
3868 sizeof(struct frag_hdr),
3873 hp = OPT_HDR(struct frag_hdr, skb, off);
3875 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3878 nexthdr = hp->nexthdr;
3879 off += sizeof(struct frag_hdr);
3890 if (!done || fragment)
3893 csum = skb_checksum_setup_ip(skb, nexthdr, off);
3895 return PTR_ERR(csum);
3898 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3899 &ipv6_hdr(skb)->daddr,
3900 skb->len - off, nexthdr, 0);
3908 * skb_checksum_setup - set up partial checksum offset
3909 * @skb: the skb to set up
3910 * @recalculate: if true the pseudo-header checksum will be recalculated
3912 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3916 switch (skb->protocol) {
3917 case htons(ETH_P_IP):
3918 err = skb_checksum_setup_ipv4(skb, recalculate);
3921 case htons(ETH_P_IPV6):
3922 err = skb_checksum_setup_ipv6(skb, recalculate);
3932 EXPORT_SYMBOL(skb_checksum_setup);
3934 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3936 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3939 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3941 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3944 skb_release_head_state(skb);
3945 kmem_cache_free(skbuff_head_cache, skb);
3950 EXPORT_SYMBOL(kfree_skb_partial);
3953 * skb_try_coalesce - try to merge skb to prior one
3955 * @from: buffer to add
3956 * @fragstolen: pointer to boolean
3957 * @delta_truesize: how much more was allocated than was requested
3959 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3960 bool *fragstolen, int *delta_truesize)
3962 int i, delta, len = from->len;
3964 *fragstolen = false;
3969 if (len <= skb_tailroom(to)) {
3971 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3972 *delta_truesize = 0;
3976 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3979 if (skb_headlen(from) != 0) {
3981 unsigned int offset;
3983 if (skb_shinfo(to)->nr_frags +
3984 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3987 if (skb_head_is_locked(from))
3990 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3992 page = virt_to_head_page(from->head);
3993 offset = from->data - (unsigned char *)page_address(page);
3995 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3996 page, offset, skb_headlen(from));
3999 if (skb_shinfo(to)->nr_frags +
4000 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4003 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4006 WARN_ON_ONCE(delta < len);
4008 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4009 skb_shinfo(from)->frags,
4010 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4011 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4013 if (!skb_cloned(from))
4014 skb_shinfo(from)->nr_frags = 0;
4016 /* if the skb is not cloned this does nothing
4017 * since we set nr_frags to 0.
4019 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4020 skb_frag_ref(from, i);
4022 to->truesize += delta;
4024 to->data_len += len;
4026 *delta_truesize = delta;
4029 EXPORT_SYMBOL(skb_try_coalesce);
4032 * skb_scrub_packet - scrub an skb
4034 * @skb: buffer to clean
4035 * @xnet: packet is crossing netns
4037 * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4038 * into/from a tunnel. Some information have to be cleared during these
4040 * skb_scrub_packet can also be used to clean a skb before injecting it in
4041 * another namespace (@xnet == true). We have to clear all information in the
4042 * skb that could impact namespace isolation.
4044 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4048 skb->tstamp.tv64 = 0;
4049 skb->pkt_type = PACKET_HOST;
4056 nf_reset_trace(skb);
4058 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4061 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4065 * skb_gso_transport_seglen is used to determine the real size of the
4066 * individual segments, including Layer4 headers (TCP/UDP).
4068 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4070 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4072 const struct skb_shared_info *shinfo = skb_shinfo(skb);
4073 unsigned int thlen = 0;
4075 if (skb->encapsulation) {
4076 thlen = skb_inner_transport_header(skb) -
4077 skb_transport_header(skb);
4079 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4080 thlen += inner_tcp_hdrlen(skb);
4081 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4082 thlen = tcp_hdrlen(skb);
4084 /* UFO sets gso_size to the size of the fragmentation
4085 * payload, i.e. the size of the L4 (UDP) header is already
4088 return thlen + shinfo->gso_size;
4090 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4092 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4094 if (skb_cow(skb, skb_headroom(skb)) < 0) {
4099 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
4100 skb->mac_header += VLAN_HLEN;
4104 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4106 struct vlan_hdr *vhdr;
4109 if (unlikely(vlan_tx_tag_present(skb))) {
4110 /* vlan_tci is already set-up so leave this for another time */
4114 skb = skb_share_check(skb, GFP_ATOMIC);
4118 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4121 vhdr = (struct vlan_hdr *)skb->data;
4122 vlan_tci = ntohs(vhdr->h_vlan_TCI);
4123 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4125 skb_pull_rcsum(skb, VLAN_HLEN);
4126 vlan_set_encap_proto(skb, vhdr);
4128 skb = skb_reorder_vlan_header(skb);
4132 skb_reset_network_header(skb);
4133 skb_reset_transport_header(skb);
4134 skb_reset_mac_len(skb);
4142 EXPORT_SYMBOL(skb_vlan_untag);
4144 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4146 if (!pskb_may_pull(skb, write_len))
4149 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4152 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4154 EXPORT_SYMBOL(skb_ensure_writable);
4156 /* remove VLAN header from packet and update csum accordingly. */
4157 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4159 struct vlan_hdr *vhdr;
4160 unsigned int offset = skb->data - skb_mac_header(skb);
4163 __skb_push(skb, offset);
4164 err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4168 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4170 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4171 *vlan_tci = ntohs(vhdr->h_vlan_TCI);
4173 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4174 __skb_pull(skb, VLAN_HLEN);
4176 vlan_set_encap_proto(skb, vhdr);
4177 skb->mac_header += VLAN_HLEN;
4179 if (skb_network_offset(skb) < ETH_HLEN)
4180 skb_set_network_header(skb, ETH_HLEN);
4182 skb_reset_mac_len(skb);
4184 __skb_pull(skb, offset);
4189 int skb_vlan_pop(struct sk_buff *skb)
4195 if (likely(vlan_tx_tag_present(skb))) {
4198 if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4199 skb->protocol != htons(ETH_P_8021AD)) ||
4200 skb->len < VLAN_ETH_HLEN))
4203 err = __skb_vlan_pop(skb, &vlan_tci);
4207 /* move next vlan tag to hw accel tag */
4208 if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4209 skb->protocol != htons(ETH_P_8021AD)) ||
4210 skb->len < VLAN_ETH_HLEN))
4213 vlan_proto = skb->protocol;
4214 err = __skb_vlan_pop(skb, &vlan_tci);
4218 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4221 EXPORT_SYMBOL(skb_vlan_pop);
4223 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4225 if (vlan_tx_tag_present(skb)) {
4226 unsigned int offset = skb->data - skb_mac_header(skb);
4229 /* __vlan_insert_tag expect skb->data pointing to mac header.
4230 * So change skb->data before calling it and change back to
4231 * original position later
4233 __skb_push(skb, offset);
4234 err = __vlan_insert_tag(skb, skb->vlan_proto,
4235 vlan_tx_tag_get(skb));
4238 skb->protocol = skb->vlan_proto;
4239 skb->mac_len += VLAN_HLEN;
4240 __skb_pull(skb, offset);
4242 if (skb->ip_summed == CHECKSUM_COMPLETE)
4243 skb->csum = csum_add(skb->csum, csum_partial(skb->data
4244 + (2 * ETH_ALEN), VLAN_HLEN, 0));
4246 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4249 EXPORT_SYMBOL(skb_vlan_push);
4252 * alloc_skb_with_frags - allocate skb with page frags
4254 * @header_len: size of linear part
4255 * @data_len: needed length in frags
4256 * @max_page_order: max page order desired.
4257 * @errcode: pointer to error code if any
4258 * @gfp_mask: allocation mask
4260 * This can be used to allocate a paged skb, given a maximal order for frags.
4262 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4263 unsigned long data_len,
4268 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4269 unsigned long chunk;
4270 struct sk_buff *skb;
4275 *errcode = -EMSGSIZE;
4276 /* Note this test could be relaxed, if we succeed to allocate
4277 * high order pages...
4279 if (npages > MAX_SKB_FRAGS)
4282 gfp_head = gfp_mask;
4283 if (gfp_head & __GFP_WAIT)
4284 gfp_head |= __GFP_REPEAT;
4286 *errcode = -ENOBUFS;
4287 skb = alloc_skb(header_len, gfp_head);
4291 skb->truesize += npages << PAGE_SHIFT;
4293 for (i = 0; npages > 0; i++) {
4294 int order = max_page_order;
4297 if (npages >= 1 << order) {
4298 page = alloc_pages(gfp_mask |
4305 /* Do not retry other high order allocations */
4311 page = alloc_page(gfp_mask);
4315 chunk = min_t(unsigned long, data_len,
4316 PAGE_SIZE << order);
4317 skb_fill_page_desc(skb, i, page, 0, chunk);
4319 npages -= 1 << order;
4327 EXPORT_SYMBOL(alloc_skb_with_frags);