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/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/splice.h>
57 #include <linux/cache.h>
58 #include <linux/rtnetlink.h>
59 #include <linux/init.h>
60 #include <linux/scatterlist.h>
61 #include <linux/errqueue.h>
62 #include <linux/prefetch.h>
64 #include <net/protocol.h>
67 #include <net/checksum.h>
70 #include <asm/uaccess.h>
71 #include <trace/events/skb.h>
72 #include <linux/highmem.h>
74 struct kmem_cache *skbuff_head_cache __read_mostly;
75 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
77 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
78 struct pipe_buffer *buf)
83 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
84 struct pipe_buffer *buf)
89 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
96 /* Pipe buffer operations for a socket. */
97 static const struct pipe_buf_operations sock_pipe_buf_ops = {
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
114 * skb_over_panic - private function
119 * Out of line support code for skb_put(). Not user callable.
121 static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
123 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
124 __func__, here, skb->len, sz, skb->head, skb->data,
125 (unsigned long)skb->tail, (unsigned long)skb->end,
126 skb->dev ? skb->dev->name : "<NULL>");
131 * skb_under_panic - private function
136 * Out of line support code for skb_push(). Not user callable.
139 static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
142 __func__, here, skb->len, sz, skb->head, skb->data,
143 (unsigned long)skb->tail, (unsigned long)skb->end,
144 skb->dev ? skb->dev->name : "<NULL>");
150 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
151 * the caller if emergency pfmemalloc reserves are being used. If it is and
152 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
153 * may be used. Otherwise, the packet data may be discarded until enough
156 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
157 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
159 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
160 unsigned long ip, bool *pfmemalloc)
163 bool ret_pfmemalloc = false;
166 * Try a regular allocation, when that fails and we're not entitled
167 * to the reserves, fail.
169 obj = kmalloc_node_track_caller(size,
170 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
172 if (obj || !(gfp_pfmemalloc_allowed(flags)))
175 /* Try again but now we are using pfmemalloc reserves */
176 ret_pfmemalloc = true;
177 obj = kmalloc_node_track_caller(size, flags, node);
181 *pfmemalloc = ret_pfmemalloc;
186 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
187 * 'private' fields and also do memory statistics to find all the
193 * __alloc_skb - allocate a network buffer
194 * @size: size to allocate
195 * @gfp_mask: allocation mask
196 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
197 * instead of head cache and allocate a cloned (child) skb.
198 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
199 * allocations in case the data is required for writeback
200 * @node: numa node to allocate memory on
202 * Allocate a new &sk_buff. The returned buffer has no headroom and a
203 * tail room of at least size bytes. The object has a reference count
204 * of one. The return is the buffer. On a failure the return is %NULL.
206 * Buffers may only be allocated from interrupts using a @gfp_mask of
209 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
212 struct kmem_cache *cache;
213 struct skb_shared_info *shinfo;
218 cache = (flags & SKB_ALLOC_FCLONE)
219 ? skbuff_fclone_cache : skbuff_head_cache;
221 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
222 gfp_mask |= __GFP_MEMALLOC;
225 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
230 /* We do our best to align skb_shared_info on a separate cache
231 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
232 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
233 * Both skb->head and skb_shared_info are cache line aligned.
235 size = SKB_DATA_ALIGN(size);
236 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
237 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
240 /* kmalloc(size) might give us more room than requested.
241 * Put skb_shared_info exactly at the end of allocated zone,
242 * to allow max possible filling before reallocation.
244 size = SKB_WITH_OVERHEAD(ksize(data));
245 prefetchw(data + size);
248 * Only clear those fields we need to clear, not those that we will
249 * actually initialise below. Hence, don't put any more fields after
250 * the tail pointer in struct sk_buff!
252 memset(skb, 0, offsetof(struct sk_buff, tail));
253 /* Account for allocated memory : skb + skb->head */
254 skb->truesize = SKB_TRUESIZE(size);
255 skb->pfmemalloc = pfmemalloc;
256 atomic_set(&skb->users, 1);
259 skb_reset_tail_pointer(skb);
260 skb->end = skb->tail + size;
261 #ifdef NET_SKBUFF_DATA_USES_OFFSET
262 skb->mac_header = ~0U;
265 /* make sure we initialize shinfo sequentially */
266 shinfo = skb_shinfo(skb);
267 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
268 atomic_set(&shinfo->dataref, 1);
269 kmemcheck_annotate_variable(shinfo->destructor_arg);
271 if (flags & SKB_ALLOC_FCLONE) {
272 struct sk_buff *child = skb + 1;
273 atomic_t *fclone_ref = (atomic_t *) (child + 1);
275 kmemcheck_annotate_bitfield(child, flags1);
276 kmemcheck_annotate_bitfield(child, flags2);
277 skb->fclone = SKB_FCLONE_ORIG;
278 atomic_set(fclone_ref, 1);
280 child->fclone = SKB_FCLONE_UNAVAILABLE;
281 child->pfmemalloc = pfmemalloc;
286 kmem_cache_free(cache, skb);
290 EXPORT_SYMBOL(__alloc_skb);
293 * build_skb - build a network buffer
294 * @data: data buffer provided by caller
295 * @frag_size: size of fragment, or 0 if head was kmalloced
297 * Allocate a new &sk_buff. Caller provides space holding head and
298 * skb_shared_info. @data must have been allocated by kmalloc()
299 * The return is the new skb buffer.
300 * On a failure the return is %NULL, and @data is not freed.
302 * Before IO, driver allocates only data buffer where NIC put incoming frame
303 * Driver should add room at head (NET_SKB_PAD) and
304 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
305 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
306 * before giving packet to stack.
307 * RX rings only contains data buffers, not full skbs.
309 struct sk_buff *build_skb(void *data, unsigned int frag_size)
311 struct skb_shared_info *shinfo;
313 unsigned int size = frag_size ? : ksize(data);
315 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
319 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
321 memset(skb, 0, offsetof(struct sk_buff, tail));
322 skb->truesize = SKB_TRUESIZE(size);
323 skb->head_frag = frag_size != 0;
324 atomic_set(&skb->users, 1);
327 skb_reset_tail_pointer(skb);
328 skb->end = skb->tail + size;
329 #ifdef NET_SKBUFF_DATA_USES_OFFSET
330 skb->mac_header = ~0U;
333 /* make sure we initialize shinfo sequentially */
334 shinfo = skb_shinfo(skb);
335 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
336 atomic_set(&shinfo->dataref, 1);
337 kmemcheck_annotate_variable(shinfo->destructor_arg);
341 EXPORT_SYMBOL(build_skb);
343 struct netdev_alloc_cache {
344 struct page_frag frag;
345 /* we maintain a pagecount bias, so that we dont dirty cache line
346 * containing page->_count every time we allocate a fragment.
348 unsigned int pagecnt_bias;
350 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
352 #define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768)
353 #define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER)
354 #define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE
356 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
358 struct netdev_alloc_cache *nc;
363 local_irq_save(flags);
364 nc = &__get_cpu_var(netdev_alloc_cache);
365 if (unlikely(!nc->frag.page)) {
367 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
368 gfp_t gfp = gfp_mask;
371 gfp |= __GFP_COMP | __GFP_NOWARN;
372 nc->frag.page = alloc_pages(gfp, order);
373 if (likely(nc->frag.page))
378 nc->frag.size = PAGE_SIZE << order;
380 atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
381 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
385 if (nc->frag.offset + fragsz > nc->frag.size) {
386 /* avoid unnecessary locked operations if possible */
387 if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
388 atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
393 data = page_address(nc->frag.page) + nc->frag.offset;
394 nc->frag.offset += fragsz;
397 local_irq_restore(flags);
402 * netdev_alloc_frag - allocate a page fragment
403 * @fragsz: fragment size
405 * Allocates a frag from a page for receive buffer.
406 * Uses GFP_ATOMIC allocations.
408 void *netdev_alloc_frag(unsigned int fragsz)
410 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
412 EXPORT_SYMBOL(netdev_alloc_frag);
415 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
416 * @dev: network device to receive on
417 * @length: length to allocate
418 * @gfp_mask: get_free_pages mask, passed to alloc_skb
420 * Allocate a new &sk_buff and assign it a usage count of one. The
421 * buffer has unspecified headroom built in. Users should allocate
422 * the headroom they think they need without accounting for the
423 * built in space. The built in space is used for optimisations.
425 * %NULL is returned if there is no free memory.
427 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
428 unsigned int length, gfp_t gfp_mask)
430 struct sk_buff *skb = NULL;
431 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
432 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
434 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
437 if (sk_memalloc_socks())
438 gfp_mask |= __GFP_MEMALLOC;
440 data = __netdev_alloc_frag(fragsz, gfp_mask);
443 skb = build_skb(data, fragsz);
445 put_page(virt_to_head_page(data));
448 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
449 SKB_ALLOC_RX, NUMA_NO_NODE);
452 skb_reserve(skb, NET_SKB_PAD);
457 EXPORT_SYMBOL(__netdev_alloc_skb);
459 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
460 int size, unsigned int truesize)
462 skb_fill_page_desc(skb, i, page, off, size);
464 skb->data_len += size;
465 skb->truesize += truesize;
467 EXPORT_SYMBOL(skb_add_rx_frag);
469 static void skb_drop_list(struct sk_buff **listp)
471 struct sk_buff *list = *listp;
476 struct sk_buff *this = list;
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)
506 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
507 &skb_shinfo(skb)->dataref)) {
508 if (skb_shinfo(skb)->nr_frags) {
510 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
511 skb_frag_unref(skb, i);
515 * If skb buf is from userspace, we need to notify the caller
516 * the lower device DMA has done;
518 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
519 struct ubuf_info *uarg;
521 uarg = skb_shinfo(skb)->destructor_arg;
523 uarg->callback(uarg, true);
526 if (skb_has_frag_list(skb))
527 skb_drop_fraglist(skb);
534 * Free an skbuff by memory without cleaning the state.
536 static void kfree_skbmem(struct sk_buff *skb)
538 struct sk_buff *other;
539 atomic_t *fclone_ref;
541 switch (skb->fclone) {
542 case SKB_FCLONE_UNAVAILABLE:
543 kmem_cache_free(skbuff_head_cache, skb);
546 case SKB_FCLONE_ORIG:
547 fclone_ref = (atomic_t *) (skb + 2);
548 if (atomic_dec_and_test(fclone_ref))
549 kmem_cache_free(skbuff_fclone_cache, skb);
552 case SKB_FCLONE_CLONE:
553 fclone_ref = (atomic_t *) (skb + 1);
556 /* The clone portion is available for
557 * fast-cloning again.
559 skb->fclone = SKB_FCLONE_UNAVAILABLE;
561 if (atomic_dec_and_test(fclone_ref))
562 kmem_cache_free(skbuff_fclone_cache, other);
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 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
581 nf_conntrack_put_reasm(skb->nfct_reasm);
583 #ifdef CONFIG_BRIDGE_NETFILTER
584 nf_bridge_put(skb->nf_bridge);
586 /* XXX: IS this still necessary? - JHS */
587 #ifdef CONFIG_NET_SCHED
589 #ifdef CONFIG_NET_CLS_ACT
595 /* Free everything but the sk_buff shell. */
596 static void skb_release_all(struct sk_buff *skb)
598 skb_release_head_state(skb);
599 skb_release_data(skb);
603 * __kfree_skb - private function
606 * Free an sk_buff. Release anything attached to the buffer.
607 * Clean the state. This is an internal helper function. Users should
608 * always call kfree_skb
611 void __kfree_skb(struct sk_buff *skb)
613 skb_release_all(skb);
616 EXPORT_SYMBOL(__kfree_skb);
619 * kfree_skb - free an sk_buff
620 * @skb: buffer to free
622 * Drop a reference to the buffer and free it if the usage count has
625 void kfree_skb(struct sk_buff *skb)
629 if (likely(atomic_read(&skb->users) == 1))
631 else if (likely(!atomic_dec_and_test(&skb->users)))
633 trace_kfree_skb(skb, __builtin_return_address(0));
636 EXPORT_SYMBOL(kfree_skb);
639 * skb_tx_error - report an sk_buff xmit error
640 * @skb: buffer that triggered an error
642 * Report xmit error if a device callback is tracking this skb.
643 * skb must be freed afterwards.
645 void skb_tx_error(struct sk_buff *skb)
647 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
648 struct ubuf_info *uarg;
650 uarg = skb_shinfo(skb)->destructor_arg;
652 uarg->callback(uarg, false);
653 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
656 EXPORT_SYMBOL(skb_tx_error);
659 * consume_skb - free an skbuff
660 * @skb: buffer to free
662 * Drop a ref to the buffer and free it if the usage count has hit zero
663 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
664 * is being dropped after a failure and notes that
666 void consume_skb(struct sk_buff *skb)
670 if (likely(atomic_read(&skb->users) == 1))
672 else if (likely(!atomic_dec_and_test(&skb->users)))
674 trace_consume_skb(skb);
677 EXPORT_SYMBOL(consume_skb);
679 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
681 new->tstamp = old->tstamp;
683 new->transport_header = old->transport_header;
684 new->network_header = old->network_header;
685 new->mac_header = old->mac_header;
686 new->inner_transport_header = old->inner_transport_header;
687 new->inner_network_header = old->inner_transport_header;
688 skb_dst_copy(new, old);
689 new->rxhash = old->rxhash;
690 new->ooo_okay = old->ooo_okay;
691 new->l4_rxhash = old->l4_rxhash;
692 new->no_fcs = old->no_fcs;
693 new->encapsulation = old->encapsulation;
695 new->sp = secpath_get(old->sp);
697 memcpy(new->cb, old->cb, sizeof(old->cb));
698 new->csum = old->csum;
699 new->local_df = old->local_df;
700 new->pkt_type = old->pkt_type;
701 new->ip_summed = old->ip_summed;
702 skb_copy_queue_mapping(new, old);
703 new->priority = old->priority;
704 #if IS_ENABLED(CONFIG_IP_VS)
705 new->ipvs_property = old->ipvs_property;
707 new->pfmemalloc = old->pfmemalloc;
708 new->protocol = old->protocol;
709 new->mark = old->mark;
710 new->skb_iif = old->skb_iif;
712 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
713 new->nf_trace = old->nf_trace;
715 #ifdef CONFIG_NET_SCHED
716 new->tc_index = old->tc_index;
717 #ifdef CONFIG_NET_CLS_ACT
718 new->tc_verd = old->tc_verd;
721 new->vlan_tci = old->vlan_tci;
723 skb_copy_secmark(new, old);
727 * You should not add any new code to this function. Add it to
728 * __copy_skb_header above instead.
730 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
732 #define C(x) n->x = skb->x
734 n->next = n->prev = NULL;
736 __copy_skb_header(n, skb);
741 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
744 n->destructor = NULL;
751 atomic_set(&n->users, 1);
753 atomic_inc(&(skb_shinfo(skb)->dataref));
761 * skb_morph - morph one skb into another
762 * @dst: the skb to receive the contents
763 * @src: the skb to supply the contents
765 * This is identical to skb_clone except that the target skb is
766 * supplied by the user.
768 * The target skb is returned upon exit.
770 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
772 skb_release_all(dst);
773 return __skb_clone(dst, src);
775 EXPORT_SYMBOL_GPL(skb_morph);
778 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
779 * @skb: the skb to modify
780 * @gfp_mask: allocation priority
782 * This must be called on SKBTX_DEV_ZEROCOPY skb.
783 * It will copy all frags into kernel and drop the reference
784 * to userspace pages.
786 * If this function is called from an interrupt gfp_mask() must be
789 * Returns 0 on success or a negative error code on failure
790 * to allocate kernel memory to copy to.
792 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
795 int num_frags = skb_shinfo(skb)->nr_frags;
796 struct page *page, *head = NULL;
797 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
799 for (i = 0; i < num_frags; i++) {
801 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
803 page = alloc_page(gfp_mask);
806 struct page *next = (struct page *)head->private;
812 vaddr = kmap_atomic(skb_frag_page(f));
813 memcpy(page_address(page),
814 vaddr + f->page_offset, skb_frag_size(f));
815 kunmap_atomic(vaddr);
816 page->private = (unsigned long)head;
820 /* skb frags release userspace buffers */
821 for (i = 0; i < num_frags; i++)
822 skb_frag_unref(skb, i);
824 uarg->callback(uarg, false);
826 /* skb frags point to kernel buffers */
827 for (i = num_frags - 1; i >= 0; i--) {
828 __skb_fill_page_desc(skb, i, head, 0,
829 skb_shinfo(skb)->frags[i].size);
830 head = (struct page *)head->private;
833 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
836 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
839 * skb_clone - duplicate an sk_buff
840 * @skb: buffer to clone
841 * @gfp_mask: allocation priority
843 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
844 * copies share the same packet data but not structure. The new
845 * buffer has a reference count of 1. If the allocation fails the
846 * function returns %NULL otherwise the new buffer is returned.
848 * If this function is called from an interrupt gfp_mask() must be
852 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
856 if (skb_orphan_frags(skb, gfp_mask))
860 if (skb->fclone == SKB_FCLONE_ORIG &&
861 n->fclone == SKB_FCLONE_UNAVAILABLE) {
862 atomic_t *fclone_ref = (atomic_t *) (n + 1);
863 n->fclone = SKB_FCLONE_CLONE;
864 atomic_inc(fclone_ref);
866 if (skb_pfmemalloc(skb))
867 gfp_mask |= __GFP_MEMALLOC;
869 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
873 kmemcheck_annotate_bitfield(n, flags1);
874 kmemcheck_annotate_bitfield(n, flags2);
875 n->fclone = SKB_FCLONE_UNAVAILABLE;
878 return __skb_clone(n, skb);
880 EXPORT_SYMBOL(skb_clone);
882 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
884 #ifndef NET_SKBUFF_DATA_USES_OFFSET
886 * Shift between the two data areas in bytes
888 unsigned long offset = new->data - old->data;
891 __copy_skb_header(new, old);
893 #ifndef NET_SKBUFF_DATA_USES_OFFSET
894 /* {transport,network,mac}_header are relative to skb->head */
895 new->transport_header += offset;
896 new->network_header += offset;
897 if (skb_mac_header_was_set(new))
898 new->mac_header += offset;
899 new->inner_transport_header += offset;
900 new->inner_network_header += offset;
902 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
903 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
904 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
907 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
909 if (skb_pfmemalloc(skb))
915 * skb_copy - create private copy of an sk_buff
916 * @skb: buffer to copy
917 * @gfp_mask: allocation priority
919 * Make a copy of both an &sk_buff and its data. This is used when the
920 * caller wishes to modify the data and needs a private copy of the
921 * data to alter. Returns %NULL on failure or the pointer to the buffer
922 * on success. The returned buffer has a reference count of 1.
924 * As by-product this function converts non-linear &sk_buff to linear
925 * one, so that &sk_buff becomes completely private and caller is allowed
926 * to modify all the data of returned buffer. This means that this
927 * function is not recommended for use in circumstances when only
928 * header is going to be modified. Use pskb_copy() instead.
931 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
933 int headerlen = skb_headroom(skb);
934 unsigned int size = skb_end_offset(skb) + skb->data_len;
935 struct sk_buff *n = __alloc_skb(size, gfp_mask,
936 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
941 /* Set the data pointer */
942 skb_reserve(n, headerlen);
943 /* Set the tail pointer and length */
944 skb_put(n, skb->len);
946 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
949 copy_skb_header(n, skb);
952 EXPORT_SYMBOL(skb_copy);
955 * __pskb_copy - create copy of an sk_buff with private head.
956 * @skb: buffer to copy
957 * @headroom: headroom of new skb
958 * @gfp_mask: allocation priority
960 * Make a copy of both an &sk_buff and part of its data, located
961 * in header. Fragmented data remain shared. This is used when
962 * the caller wishes to modify only header of &sk_buff and needs
963 * private copy of the header to alter. Returns %NULL on failure
964 * or the pointer to the buffer on success.
965 * The returned buffer has a reference count of 1.
968 struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
970 unsigned int size = skb_headlen(skb) + headroom;
971 struct sk_buff *n = __alloc_skb(size, gfp_mask,
972 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
977 /* Set the data pointer */
978 skb_reserve(n, headroom);
979 /* Set the tail pointer and length */
980 skb_put(n, skb_headlen(skb));
982 skb_copy_from_linear_data(skb, n->data, n->len);
984 n->truesize += skb->data_len;
985 n->data_len = skb->data_len;
988 if (skb_shinfo(skb)->nr_frags) {
991 if (skb_orphan_frags(skb, gfp_mask)) {
996 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
997 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
998 skb_frag_ref(skb, i);
1000 skb_shinfo(n)->nr_frags = i;
1003 if (skb_has_frag_list(skb)) {
1004 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1005 skb_clone_fraglist(n);
1008 copy_skb_header(n, skb);
1012 EXPORT_SYMBOL(__pskb_copy);
1015 * pskb_expand_head - reallocate header of &sk_buff
1016 * @skb: buffer to reallocate
1017 * @nhead: room to add at head
1018 * @ntail: room to add at tail
1019 * @gfp_mask: allocation priority
1021 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1022 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1023 * reference count of 1. Returns zero in the case of success or error,
1024 * if expansion failed. In the last case, &sk_buff is not changed.
1026 * All the pointers pointing into skb header may change and must be
1027 * reloaded after call to this function.
1030 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1035 int size = nhead + skb_end_offset(skb) + ntail;
1040 if (skb_shared(skb))
1043 size = SKB_DATA_ALIGN(size);
1045 if (skb_pfmemalloc(skb))
1046 gfp_mask |= __GFP_MEMALLOC;
1047 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1048 gfp_mask, NUMA_NO_NODE, NULL);
1051 size = SKB_WITH_OVERHEAD(ksize(data));
1053 /* Copy only real data... and, alas, header. This should be
1054 * optimized for the cases when header is void.
1056 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1058 memcpy((struct skb_shared_info *)(data + size),
1060 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1063 * if shinfo is shared we must drop the old head gracefully, but if it
1064 * is not we can just drop the old head and let the existing refcount
1065 * be since all we did is relocate the values
1067 if (skb_cloned(skb)) {
1068 /* copy this zero copy skb frags */
1069 if (skb_orphan_frags(skb, gfp_mask))
1071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1072 skb_frag_ref(skb, i);
1074 if (skb_has_frag_list(skb))
1075 skb_clone_fraglist(skb);
1077 skb_release_data(skb);
1081 off = (data + nhead) - skb->head;
1086 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1090 skb->end = skb->head + size;
1092 /* {transport,network,mac}_header and tail are relative to skb->head */
1094 skb->transport_header += off;
1095 skb->network_header += off;
1096 if (skb_mac_header_was_set(skb))
1097 skb->mac_header += off;
1098 skb->inner_transport_header += off;
1099 skb->inner_network_header += off;
1100 /* Only adjust this if it actually is csum_start rather than csum */
1101 if (skb->ip_summed == CHECKSUM_PARTIAL)
1102 skb->csum_start += nhead;
1106 atomic_set(&skb_shinfo(skb)->dataref, 1);
1114 EXPORT_SYMBOL(pskb_expand_head);
1116 /* Make private copy of skb with writable head and some headroom */
1118 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1120 struct sk_buff *skb2;
1121 int delta = headroom - skb_headroom(skb);
1124 skb2 = pskb_copy(skb, GFP_ATOMIC);
1126 skb2 = skb_clone(skb, GFP_ATOMIC);
1127 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1135 EXPORT_SYMBOL(skb_realloc_headroom);
1138 * skb_copy_expand - copy and expand sk_buff
1139 * @skb: buffer to copy
1140 * @newheadroom: new free bytes at head
1141 * @newtailroom: new free bytes at tail
1142 * @gfp_mask: allocation priority
1144 * Make a copy of both an &sk_buff and its data and while doing so
1145 * allocate additional space.
1147 * This is used when the caller wishes to modify the data and needs a
1148 * private copy of the data to alter as well as more space for new fields.
1149 * Returns %NULL on failure or the pointer to the buffer
1150 * on success. The returned buffer has a reference count of 1.
1152 * You must pass %GFP_ATOMIC as the allocation priority if this function
1153 * is called from an interrupt.
1155 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1156 int newheadroom, int newtailroom,
1160 * Allocate the copy buffer
1162 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1163 gfp_mask, skb_alloc_rx_flag(skb),
1165 int oldheadroom = skb_headroom(skb);
1166 int head_copy_len, head_copy_off;
1172 skb_reserve(n, newheadroom);
1174 /* Set the tail pointer and length */
1175 skb_put(n, skb->len);
1177 head_copy_len = oldheadroom;
1179 if (newheadroom <= head_copy_len)
1180 head_copy_len = newheadroom;
1182 head_copy_off = newheadroom - head_copy_len;
1184 /* Copy the linear header and data. */
1185 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1186 skb->len + head_copy_len))
1189 copy_skb_header(n, skb);
1191 off = newheadroom - oldheadroom;
1192 if (n->ip_summed == CHECKSUM_PARTIAL)
1193 n->csum_start += off;
1194 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1195 n->transport_header += off;
1196 n->network_header += off;
1197 if (skb_mac_header_was_set(skb))
1198 n->mac_header += off;
1199 n->inner_transport_header += off;
1200 n->inner_network_header += off;
1205 EXPORT_SYMBOL(skb_copy_expand);
1208 * skb_pad - zero pad the tail of an skb
1209 * @skb: buffer to pad
1210 * @pad: space to pad
1212 * Ensure that a buffer is followed by a padding area that is zero
1213 * filled. Used by network drivers which may DMA or transfer data
1214 * beyond the buffer end onto the wire.
1216 * May return error in out of memory cases. The skb is freed on error.
1219 int skb_pad(struct sk_buff *skb, int pad)
1224 /* If the skbuff is non linear tailroom is always zero.. */
1225 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1226 memset(skb->data+skb->len, 0, pad);
1230 ntail = skb->data_len + pad - (skb->end - skb->tail);
1231 if (likely(skb_cloned(skb) || ntail > 0)) {
1232 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1237 /* FIXME: The use of this function with non-linear skb's really needs
1240 err = skb_linearize(skb);
1244 memset(skb->data + skb->len, 0, pad);
1251 EXPORT_SYMBOL(skb_pad);
1254 * skb_put - add data to a buffer
1255 * @skb: buffer to use
1256 * @len: amount of data to add
1258 * This function extends the used data area of the buffer. If this would
1259 * exceed the total buffer size the kernel will panic. A pointer to the
1260 * first byte of the extra data is returned.
1262 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1264 unsigned char *tmp = skb_tail_pointer(skb);
1265 SKB_LINEAR_ASSERT(skb);
1268 if (unlikely(skb->tail > skb->end))
1269 skb_over_panic(skb, len, __builtin_return_address(0));
1272 EXPORT_SYMBOL(skb_put);
1275 * skb_push - add data to the start of a buffer
1276 * @skb: buffer to use
1277 * @len: amount of data to add
1279 * This function extends the used data area of the buffer at the buffer
1280 * start. If this would exceed the total buffer headroom the kernel will
1281 * panic. A pointer to the first byte of the extra data is returned.
1283 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1287 if (unlikely(skb->data<skb->head))
1288 skb_under_panic(skb, len, __builtin_return_address(0));
1291 EXPORT_SYMBOL(skb_push);
1294 * skb_pull - remove data from the start of a buffer
1295 * @skb: buffer to use
1296 * @len: amount of data to remove
1298 * This function removes data from the start of a buffer, returning
1299 * the memory to the headroom. A pointer to the next data in the buffer
1300 * is returned. Once the data has been pulled future pushes will overwrite
1303 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1305 return skb_pull_inline(skb, len);
1307 EXPORT_SYMBOL(skb_pull);
1310 * skb_trim - remove end from a buffer
1311 * @skb: buffer to alter
1314 * Cut the length of a buffer down by removing data from the tail. If
1315 * the buffer is already under the length specified it is not modified.
1316 * The skb must be linear.
1318 void skb_trim(struct sk_buff *skb, unsigned int len)
1321 __skb_trim(skb, len);
1323 EXPORT_SYMBOL(skb_trim);
1325 /* Trims skb to length len. It can change skb pointers.
1328 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1330 struct sk_buff **fragp;
1331 struct sk_buff *frag;
1332 int offset = skb_headlen(skb);
1333 int nfrags = skb_shinfo(skb)->nr_frags;
1337 if (skb_cloned(skb) &&
1338 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1345 for (; i < nfrags; i++) {
1346 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1353 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1356 skb_shinfo(skb)->nr_frags = i;
1358 for (; i < nfrags; i++)
1359 skb_frag_unref(skb, i);
1361 if (skb_has_frag_list(skb))
1362 skb_drop_fraglist(skb);
1366 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1367 fragp = &frag->next) {
1368 int end = offset + frag->len;
1370 if (skb_shared(frag)) {
1371 struct sk_buff *nfrag;
1373 nfrag = skb_clone(frag, GFP_ATOMIC);
1374 if (unlikely(!nfrag))
1377 nfrag->next = frag->next;
1389 unlikely((err = pskb_trim(frag, len - offset))))
1393 skb_drop_list(&frag->next);
1398 if (len > skb_headlen(skb)) {
1399 skb->data_len -= skb->len - len;
1404 skb_set_tail_pointer(skb, len);
1409 EXPORT_SYMBOL(___pskb_trim);
1412 * __pskb_pull_tail - advance tail of skb header
1413 * @skb: buffer to reallocate
1414 * @delta: number of bytes to advance tail
1416 * The function makes a sense only on a fragmented &sk_buff,
1417 * it expands header moving its tail forward and copying necessary
1418 * data from fragmented part.
1420 * &sk_buff MUST have reference count of 1.
1422 * Returns %NULL (and &sk_buff does not change) if pull failed
1423 * or value of new tail of skb in the case of success.
1425 * All the pointers pointing into skb header may change and must be
1426 * reloaded after call to this function.
1429 /* Moves tail of skb head forward, copying data from fragmented part,
1430 * when it is necessary.
1431 * 1. It may fail due to malloc failure.
1432 * 2. It may change skb pointers.
1434 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1436 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1438 /* If skb has not enough free space at tail, get new one
1439 * plus 128 bytes for future expansions. If we have enough
1440 * room at tail, reallocate without expansion only if skb is cloned.
1442 int i, k, eat = (skb->tail + delta) - skb->end;
1444 if (eat > 0 || skb_cloned(skb)) {
1445 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1450 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1453 /* Optimization: no fragments, no reasons to preestimate
1454 * size of pulled pages. Superb.
1456 if (!skb_has_frag_list(skb))
1459 /* Estimate size of pulled pages. */
1461 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1462 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1469 /* If we need update frag list, we are in troubles.
1470 * Certainly, it possible to add an offset to skb data,
1471 * but taking into account that pulling is expected to
1472 * be very rare operation, it is worth to fight against
1473 * further bloating skb head and crucify ourselves here instead.
1474 * Pure masohism, indeed. 8)8)
1477 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1478 struct sk_buff *clone = NULL;
1479 struct sk_buff *insp = NULL;
1484 if (list->len <= eat) {
1485 /* Eaten as whole. */
1490 /* Eaten partially. */
1492 if (skb_shared(list)) {
1493 /* Sucks! We need to fork list. :-( */
1494 clone = skb_clone(list, GFP_ATOMIC);
1500 /* This may be pulled without
1504 if (!pskb_pull(list, eat)) {
1512 /* Free pulled out fragments. */
1513 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1514 skb_shinfo(skb)->frag_list = list->next;
1517 /* And insert new clone at head. */
1520 skb_shinfo(skb)->frag_list = clone;
1523 /* Success! Now we may commit changes to skb data. */
1528 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1529 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1532 skb_frag_unref(skb, i);
1535 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1537 skb_shinfo(skb)->frags[k].page_offset += eat;
1538 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1544 skb_shinfo(skb)->nr_frags = k;
1547 skb->data_len -= delta;
1549 return skb_tail_pointer(skb);
1551 EXPORT_SYMBOL(__pskb_pull_tail);
1554 * skb_copy_bits - copy bits from skb to kernel buffer
1556 * @offset: offset in source
1557 * @to: destination buffer
1558 * @len: number of bytes to copy
1560 * Copy the specified number of bytes from the source skb to the
1561 * destination buffer.
1564 * If its prototype is ever changed,
1565 * check arch/{*}/net/{*}.S files,
1566 * since it is called from BPF assembly code.
1568 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1570 int start = skb_headlen(skb);
1571 struct sk_buff *frag_iter;
1574 if (offset > (int)skb->len - len)
1578 if ((copy = start - offset) > 0) {
1581 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1582 if ((len -= copy) == 0)
1588 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1590 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1592 WARN_ON(start > offset + len);
1594 end = start + skb_frag_size(f);
1595 if ((copy = end - offset) > 0) {
1601 vaddr = kmap_atomic(skb_frag_page(f));
1603 vaddr + f->page_offset + offset - start,
1605 kunmap_atomic(vaddr);
1607 if ((len -= copy) == 0)
1615 skb_walk_frags(skb, frag_iter) {
1618 WARN_ON(start > offset + len);
1620 end = start + frag_iter->len;
1621 if ((copy = end - offset) > 0) {
1624 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1626 if ((len -= copy) == 0)
1640 EXPORT_SYMBOL(skb_copy_bits);
1643 * Callback from splice_to_pipe(), if we need to release some pages
1644 * at the end of the spd in case we error'ed out in filling the pipe.
1646 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1648 put_page(spd->pages[i]);
1651 static struct page *linear_to_page(struct page *page, unsigned int *len,
1652 unsigned int *offset,
1653 struct sk_buff *skb, struct sock *sk)
1655 struct page_frag *pfrag = sk_page_frag(sk);
1657 if (!sk_page_frag_refill(sk, pfrag))
1660 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1662 memcpy(page_address(pfrag->page) + pfrag->offset,
1663 page_address(page) + *offset, *len);
1664 *offset = pfrag->offset;
1665 pfrag->offset += *len;
1670 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1672 unsigned int offset)
1674 return spd->nr_pages &&
1675 spd->pages[spd->nr_pages - 1] == page &&
1676 (spd->partial[spd->nr_pages - 1].offset +
1677 spd->partial[spd->nr_pages - 1].len == offset);
1681 * Fill page/offset/length into spd, if it can hold more pages.
1683 static bool spd_fill_page(struct splice_pipe_desc *spd,
1684 struct pipe_inode_info *pipe, struct page *page,
1685 unsigned int *len, unsigned int offset,
1686 struct sk_buff *skb, bool linear,
1689 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1693 page = linear_to_page(page, len, &offset, skb, sk);
1697 if (spd_can_coalesce(spd, page, offset)) {
1698 spd->partial[spd->nr_pages - 1].len += *len;
1702 spd->pages[spd->nr_pages] = page;
1703 spd->partial[spd->nr_pages].len = *len;
1704 spd->partial[spd->nr_pages].offset = offset;
1710 static inline void __segment_seek(struct page **page, unsigned int *poff,
1711 unsigned int *plen, unsigned int off)
1716 n = *poff / PAGE_SIZE;
1718 *page = nth_page(*page, n);
1720 *poff = *poff % PAGE_SIZE;
1724 static bool __splice_segment(struct page *page, unsigned int poff,
1725 unsigned int plen, unsigned int *off,
1726 unsigned int *len, struct sk_buff *skb,
1727 struct splice_pipe_desc *spd, bool linear,
1729 struct pipe_inode_info *pipe)
1734 /* skip this segment if already processed */
1740 /* ignore any bits we already processed */
1742 __segment_seek(&page, &poff, &plen, *off);
1747 unsigned int flen = min(*len, plen);
1749 /* the linear region may spread across several pages */
1750 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1752 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1755 __segment_seek(&page, &poff, &plen, flen);
1758 } while (*len && plen);
1764 * Map linear and fragment data from the skb to spd. It reports true if the
1765 * pipe is full or if we already spliced the requested length.
1767 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1768 unsigned int *offset, unsigned int *len,
1769 struct splice_pipe_desc *spd, struct sock *sk)
1773 /* map the linear part :
1774 * If skb->head_frag is set, this 'linear' part is backed by a
1775 * fragment, and if the head is not shared with any clones then
1776 * we can avoid a copy since we own the head portion of this page.
1778 if (__splice_segment(virt_to_page(skb->data),
1779 (unsigned long) skb->data & (PAGE_SIZE - 1),
1781 offset, len, skb, spd,
1782 skb_head_is_locked(skb),
1787 * then map the fragments
1789 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1790 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1792 if (__splice_segment(skb_frag_page(f),
1793 f->page_offset, skb_frag_size(f),
1794 offset, len, skb, spd, false, sk, pipe))
1802 * Map data from the skb to a pipe. Should handle both the linear part,
1803 * the fragments, and the frag list. It does NOT handle frag lists within
1804 * the frag list, if such a thing exists. We'd probably need to recurse to
1805 * handle that cleanly.
1807 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1808 struct pipe_inode_info *pipe, unsigned int tlen,
1811 struct partial_page partial[MAX_SKB_FRAGS];
1812 struct page *pages[MAX_SKB_FRAGS];
1813 struct splice_pipe_desc spd = {
1816 .nr_pages_max = MAX_SKB_FRAGS,
1818 .ops = &sock_pipe_buf_ops,
1819 .spd_release = sock_spd_release,
1821 struct sk_buff *frag_iter;
1822 struct sock *sk = skb->sk;
1826 * __skb_splice_bits() only fails if the output has no room left,
1827 * so no point in going over the frag_list for the error case.
1829 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1835 * now see if we have a frag_list to map
1837 skb_walk_frags(skb, frag_iter) {
1840 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1847 * Drop the socket lock, otherwise we have reverse
1848 * locking dependencies between sk_lock and i_mutex
1849 * here as compared to sendfile(). We enter here
1850 * with the socket lock held, and splice_to_pipe() will
1851 * grab the pipe inode lock. For sendfile() emulation,
1852 * we call into ->sendpage() with the i_mutex lock held
1853 * and networking will grab the socket lock.
1856 ret = splice_to_pipe(pipe, &spd);
1864 * skb_store_bits - store bits from kernel buffer to skb
1865 * @skb: destination buffer
1866 * @offset: offset in destination
1867 * @from: source buffer
1868 * @len: number of bytes to copy
1870 * Copy the specified number of bytes from the source buffer to the
1871 * destination skb. This function handles all the messy bits of
1872 * traversing fragment lists and such.
1875 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1877 int start = skb_headlen(skb);
1878 struct sk_buff *frag_iter;
1881 if (offset > (int)skb->len - len)
1884 if ((copy = start - offset) > 0) {
1887 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1888 if ((len -= copy) == 0)
1894 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1895 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1898 WARN_ON(start > offset + len);
1900 end = start + skb_frag_size(frag);
1901 if ((copy = end - offset) > 0) {
1907 vaddr = kmap_atomic(skb_frag_page(frag));
1908 memcpy(vaddr + frag->page_offset + offset - start,
1910 kunmap_atomic(vaddr);
1912 if ((len -= copy) == 0)
1920 skb_walk_frags(skb, frag_iter) {
1923 WARN_ON(start > offset + len);
1925 end = start + frag_iter->len;
1926 if ((copy = end - offset) > 0) {
1929 if (skb_store_bits(frag_iter, offset - start,
1932 if ((len -= copy) == 0)
1945 EXPORT_SYMBOL(skb_store_bits);
1947 /* Checksum skb data. */
1949 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1950 int len, __wsum csum)
1952 int start = skb_headlen(skb);
1953 int i, copy = start - offset;
1954 struct sk_buff *frag_iter;
1957 /* Checksum header. */
1961 csum = csum_partial(skb->data + offset, copy, csum);
1962 if ((len -= copy) == 0)
1968 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1970 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1972 WARN_ON(start > offset + len);
1974 end = start + skb_frag_size(frag);
1975 if ((copy = end - offset) > 0) {
1981 vaddr = kmap_atomic(skb_frag_page(frag));
1982 csum2 = csum_partial(vaddr + frag->page_offset +
1983 offset - start, copy, 0);
1984 kunmap_atomic(vaddr);
1985 csum = csum_block_add(csum, csum2, pos);
1994 skb_walk_frags(skb, frag_iter) {
1997 WARN_ON(start > offset + len);
1999 end = start + frag_iter->len;
2000 if ((copy = end - offset) > 0) {
2004 csum2 = skb_checksum(frag_iter, offset - start,
2006 csum = csum_block_add(csum, csum2, pos);
2007 if ((len -= copy) == 0)
2018 EXPORT_SYMBOL(skb_checksum);
2020 /* Both of above in one bottle. */
2022 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2023 u8 *to, int len, __wsum csum)
2025 int start = skb_headlen(skb);
2026 int i, copy = start - offset;
2027 struct sk_buff *frag_iter;
2034 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2036 if ((len -= copy) == 0)
2043 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2046 WARN_ON(start > offset + len);
2048 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2049 if ((copy = end - offset) > 0) {
2052 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2056 vaddr = kmap_atomic(skb_frag_page(frag));
2057 csum2 = csum_partial_copy_nocheck(vaddr +
2061 kunmap_atomic(vaddr);
2062 csum = csum_block_add(csum, csum2, pos);
2072 skb_walk_frags(skb, frag_iter) {
2076 WARN_ON(start > offset + len);
2078 end = start + frag_iter->len;
2079 if ((copy = end - offset) > 0) {
2082 csum2 = skb_copy_and_csum_bits(frag_iter,
2085 csum = csum_block_add(csum, csum2, pos);
2086 if ((len -= copy) == 0)
2097 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2099 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2104 if (skb->ip_summed == CHECKSUM_PARTIAL)
2105 csstart = skb_checksum_start_offset(skb);
2107 csstart = skb_headlen(skb);
2109 BUG_ON(csstart > skb_headlen(skb));
2111 skb_copy_from_linear_data(skb, to, csstart);
2114 if (csstart != skb->len)
2115 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2116 skb->len - csstart, 0);
2118 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2119 long csstuff = csstart + skb->csum_offset;
2121 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2124 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2127 * skb_dequeue - remove from the head of the queue
2128 * @list: list to dequeue from
2130 * Remove the head of the list. The list lock is taken so the function
2131 * may be used safely with other locking list functions. The head item is
2132 * returned or %NULL if the list is empty.
2135 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2137 unsigned long flags;
2138 struct sk_buff *result;
2140 spin_lock_irqsave(&list->lock, flags);
2141 result = __skb_dequeue(list);
2142 spin_unlock_irqrestore(&list->lock, flags);
2145 EXPORT_SYMBOL(skb_dequeue);
2148 * skb_dequeue_tail - remove from the tail of the queue
2149 * @list: list to dequeue from
2151 * Remove the tail of the list. The list lock is taken so the function
2152 * may be used safely with other locking list functions. The tail item is
2153 * returned or %NULL if the list is empty.
2155 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2157 unsigned long flags;
2158 struct sk_buff *result;
2160 spin_lock_irqsave(&list->lock, flags);
2161 result = __skb_dequeue_tail(list);
2162 spin_unlock_irqrestore(&list->lock, flags);
2165 EXPORT_SYMBOL(skb_dequeue_tail);
2168 * skb_queue_purge - empty a list
2169 * @list: list to empty
2171 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2172 * the list and one reference dropped. This function takes the list
2173 * lock and is atomic with respect to other list locking functions.
2175 void skb_queue_purge(struct sk_buff_head *list)
2177 struct sk_buff *skb;
2178 while ((skb = skb_dequeue(list)) != NULL)
2181 EXPORT_SYMBOL(skb_queue_purge);
2184 * skb_queue_head - queue a buffer at the list head
2185 * @list: list to use
2186 * @newsk: buffer to queue
2188 * Queue a buffer at the start of the list. This function takes the
2189 * list lock and can be used safely with other locking &sk_buff functions
2192 * A buffer cannot be placed on two lists at the same time.
2194 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2196 unsigned long flags;
2198 spin_lock_irqsave(&list->lock, flags);
2199 __skb_queue_head(list, newsk);
2200 spin_unlock_irqrestore(&list->lock, flags);
2202 EXPORT_SYMBOL(skb_queue_head);
2205 * skb_queue_tail - queue a buffer at the list tail
2206 * @list: list to use
2207 * @newsk: buffer to queue
2209 * Queue a buffer at the tail of the list. This function takes the
2210 * list lock and can be used safely with other locking &sk_buff functions
2213 * A buffer cannot be placed on two lists at the same time.
2215 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2217 unsigned long flags;
2219 spin_lock_irqsave(&list->lock, flags);
2220 __skb_queue_tail(list, newsk);
2221 spin_unlock_irqrestore(&list->lock, flags);
2223 EXPORT_SYMBOL(skb_queue_tail);
2226 * skb_unlink - remove a buffer from a list
2227 * @skb: buffer to remove
2228 * @list: list to use
2230 * Remove a packet from a list. The list locks are taken and this
2231 * function is atomic with respect to other list locked calls
2233 * You must know what list the SKB is on.
2235 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2237 unsigned long flags;
2239 spin_lock_irqsave(&list->lock, flags);
2240 __skb_unlink(skb, list);
2241 spin_unlock_irqrestore(&list->lock, flags);
2243 EXPORT_SYMBOL(skb_unlink);
2246 * skb_append - append a buffer
2247 * @old: buffer to insert after
2248 * @newsk: buffer to insert
2249 * @list: list to use
2251 * Place a packet after a given packet in a list. The list locks are taken
2252 * and this function is atomic with respect to other list locked calls.
2253 * A buffer cannot be placed on two lists at the same time.
2255 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2257 unsigned long flags;
2259 spin_lock_irqsave(&list->lock, flags);
2260 __skb_queue_after(list, old, newsk);
2261 spin_unlock_irqrestore(&list->lock, flags);
2263 EXPORT_SYMBOL(skb_append);
2266 * skb_insert - insert a buffer
2267 * @old: buffer to insert before
2268 * @newsk: buffer to insert
2269 * @list: list to use
2271 * Place a packet before a given packet in a list. The list locks are
2272 * taken and this function is atomic with respect to other list locked
2275 * A buffer cannot be placed on two lists at the same time.
2277 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2279 unsigned long flags;
2281 spin_lock_irqsave(&list->lock, flags);
2282 __skb_insert(newsk, old->prev, old, list);
2283 spin_unlock_irqrestore(&list->lock, flags);
2285 EXPORT_SYMBOL(skb_insert);
2287 static inline void skb_split_inside_header(struct sk_buff *skb,
2288 struct sk_buff* skb1,
2289 const u32 len, const int pos)
2293 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2295 /* And move data appendix as is. */
2296 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2297 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2299 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2300 skb_shinfo(skb)->nr_frags = 0;
2301 skb1->data_len = skb->data_len;
2302 skb1->len += skb1->data_len;
2305 skb_set_tail_pointer(skb, len);
2308 static inline void skb_split_no_header(struct sk_buff *skb,
2309 struct sk_buff* skb1,
2310 const u32 len, int pos)
2313 const int nfrags = skb_shinfo(skb)->nr_frags;
2315 skb_shinfo(skb)->nr_frags = 0;
2316 skb1->len = skb1->data_len = skb->len - len;
2318 skb->data_len = len - pos;
2320 for (i = 0; i < nfrags; i++) {
2321 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2323 if (pos + size > len) {
2324 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2328 * We have two variants in this case:
2329 * 1. Move all the frag to the second
2330 * part, if it is possible. F.e.
2331 * this approach is mandatory for TUX,
2332 * where splitting is expensive.
2333 * 2. Split is accurately. We make this.
2335 skb_frag_ref(skb, i);
2336 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2337 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2338 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2339 skb_shinfo(skb)->nr_frags++;
2343 skb_shinfo(skb)->nr_frags++;
2346 skb_shinfo(skb1)->nr_frags = k;
2350 * skb_split - Split fragmented skb to two parts at length len.
2351 * @skb: the buffer to split
2352 * @skb1: the buffer to receive the second part
2353 * @len: new length for skb
2355 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2357 int pos = skb_headlen(skb);
2359 if (len < pos) /* Split line is inside header. */
2360 skb_split_inside_header(skb, skb1, len, pos);
2361 else /* Second chunk has no header, nothing to copy. */
2362 skb_split_no_header(skb, skb1, len, pos);
2364 EXPORT_SYMBOL(skb_split);
2366 /* Shifting from/to a cloned skb is a no-go.
2368 * Caller cannot keep skb_shinfo related pointers past calling here!
2370 static int skb_prepare_for_shift(struct sk_buff *skb)
2372 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2376 * skb_shift - Shifts paged data partially from skb to another
2377 * @tgt: buffer into which tail data gets added
2378 * @skb: buffer from which the paged data comes from
2379 * @shiftlen: shift up to this many bytes
2381 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2382 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2383 * It's up to caller to free skb if everything was shifted.
2385 * If @tgt runs out of frags, the whole operation is aborted.
2387 * Skb cannot include anything else but paged data while tgt is allowed
2388 * to have non-paged data as well.
2390 * TODO: full sized shift could be optimized but that would need
2391 * specialized skb free'er to handle frags without up-to-date nr_frags.
2393 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2395 int from, to, merge, todo;
2396 struct skb_frag_struct *fragfrom, *fragto;
2398 BUG_ON(shiftlen > skb->len);
2399 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2403 to = skb_shinfo(tgt)->nr_frags;
2404 fragfrom = &skb_shinfo(skb)->frags[from];
2406 /* Actual merge is delayed until the point when we know we can
2407 * commit all, so that we don't have to undo partial changes
2410 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2411 fragfrom->page_offset)) {
2416 todo -= skb_frag_size(fragfrom);
2418 if (skb_prepare_for_shift(skb) ||
2419 skb_prepare_for_shift(tgt))
2422 /* All previous frag pointers might be stale! */
2423 fragfrom = &skb_shinfo(skb)->frags[from];
2424 fragto = &skb_shinfo(tgt)->frags[merge];
2426 skb_frag_size_add(fragto, shiftlen);
2427 skb_frag_size_sub(fragfrom, shiftlen);
2428 fragfrom->page_offset += shiftlen;
2436 /* Skip full, not-fitting skb to avoid expensive operations */
2437 if ((shiftlen == skb->len) &&
2438 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2441 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2444 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2445 if (to == MAX_SKB_FRAGS)
2448 fragfrom = &skb_shinfo(skb)->frags[from];
2449 fragto = &skb_shinfo(tgt)->frags[to];
2451 if (todo >= skb_frag_size(fragfrom)) {
2452 *fragto = *fragfrom;
2453 todo -= skb_frag_size(fragfrom);
2458 __skb_frag_ref(fragfrom);
2459 fragto->page = fragfrom->page;
2460 fragto->page_offset = fragfrom->page_offset;
2461 skb_frag_size_set(fragto, todo);
2463 fragfrom->page_offset += todo;
2464 skb_frag_size_sub(fragfrom, todo);
2472 /* Ready to "commit" this state change to tgt */
2473 skb_shinfo(tgt)->nr_frags = to;
2476 fragfrom = &skb_shinfo(skb)->frags[0];
2477 fragto = &skb_shinfo(tgt)->frags[merge];
2479 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2480 __skb_frag_unref(fragfrom);
2483 /* Reposition in the original skb */
2485 while (from < skb_shinfo(skb)->nr_frags)
2486 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2487 skb_shinfo(skb)->nr_frags = to;
2489 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2492 /* Most likely the tgt won't ever need its checksum anymore, skb on
2493 * the other hand might need it if it needs to be resent
2495 tgt->ip_summed = CHECKSUM_PARTIAL;
2496 skb->ip_summed = CHECKSUM_PARTIAL;
2498 /* Yak, is it really working this way? Some helper please? */
2499 skb->len -= shiftlen;
2500 skb->data_len -= shiftlen;
2501 skb->truesize -= shiftlen;
2502 tgt->len += shiftlen;
2503 tgt->data_len += shiftlen;
2504 tgt->truesize += shiftlen;
2510 * skb_prepare_seq_read - Prepare a sequential read of skb data
2511 * @skb: the buffer to read
2512 * @from: lower offset of data to be read
2513 * @to: upper offset of data to be read
2514 * @st: state variable
2516 * Initializes the specified state variable. Must be called before
2517 * invoking skb_seq_read() for the first time.
2519 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2520 unsigned int to, struct skb_seq_state *st)
2522 st->lower_offset = from;
2523 st->upper_offset = to;
2524 st->root_skb = st->cur_skb = skb;
2525 st->frag_idx = st->stepped_offset = 0;
2526 st->frag_data = NULL;
2528 EXPORT_SYMBOL(skb_prepare_seq_read);
2531 * skb_seq_read - Sequentially read skb data
2532 * @consumed: number of bytes consumed by the caller so far
2533 * @data: destination pointer for data to be returned
2534 * @st: state variable
2536 * Reads a block of skb data at &consumed relative to the
2537 * lower offset specified to skb_prepare_seq_read(). Assigns
2538 * the head of the data block to &data and returns the length
2539 * of the block or 0 if the end of the skb data or the upper
2540 * offset has been reached.
2542 * The caller is not required to consume all of the data
2543 * returned, i.e. &consumed is typically set to the number
2544 * of bytes already consumed and the next call to
2545 * skb_seq_read() will return the remaining part of the block.
2547 * Note 1: The size of each block of data returned can be arbitrary,
2548 * this limitation is the cost for zerocopy seqeuental
2549 * reads of potentially non linear data.
2551 * Note 2: Fragment lists within fragments are not implemented
2552 * at the moment, state->root_skb could be replaced with
2553 * a stack for this purpose.
2555 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2556 struct skb_seq_state *st)
2558 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2561 if (unlikely(abs_offset >= st->upper_offset))
2565 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2567 if (abs_offset < block_limit && !st->frag_data) {
2568 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2569 return block_limit - abs_offset;
2572 if (st->frag_idx == 0 && !st->frag_data)
2573 st->stepped_offset += skb_headlen(st->cur_skb);
2575 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2576 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2577 block_limit = skb_frag_size(frag) + st->stepped_offset;
2579 if (abs_offset < block_limit) {
2581 st->frag_data = kmap_atomic(skb_frag_page(frag));
2583 *data = (u8 *) st->frag_data + frag->page_offset +
2584 (abs_offset - st->stepped_offset);
2586 return block_limit - abs_offset;
2589 if (st->frag_data) {
2590 kunmap_atomic(st->frag_data);
2591 st->frag_data = NULL;
2595 st->stepped_offset += skb_frag_size(frag);
2598 if (st->frag_data) {
2599 kunmap_atomic(st->frag_data);
2600 st->frag_data = NULL;
2603 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2604 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2607 } else if (st->cur_skb->next) {
2608 st->cur_skb = st->cur_skb->next;
2615 EXPORT_SYMBOL(skb_seq_read);
2618 * skb_abort_seq_read - Abort a sequential read of skb data
2619 * @st: state variable
2621 * Must be called if skb_seq_read() was not called until it
2624 void skb_abort_seq_read(struct skb_seq_state *st)
2627 kunmap_atomic(st->frag_data);
2629 EXPORT_SYMBOL(skb_abort_seq_read);
2631 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2633 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2634 struct ts_config *conf,
2635 struct ts_state *state)
2637 return skb_seq_read(offset, text, TS_SKB_CB(state));
2640 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2642 skb_abort_seq_read(TS_SKB_CB(state));
2646 * skb_find_text - Find a text pattern in skb data
2647 * @skb: the buffer to look in
2648 * @from: search offset
2650 * @config: textsearch configuration
2651 * @state: uninitialized textsearch state variable
2653 * Finds a pattern in the skb data according to the specified
2654 * textsearch configuration. Use textsearch_next() to retrieve
2655 * subsequent occurrences of the pattern. Returns the offset
2656 * to the first occurrence or UINT_MAX if no match was found.
2658 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2659 unsigned int to, struct ts_config *config,
2660 struct ts_state *state)
2664 config->get_next_block = skb_ts_get_next_block;
2665 config->finish = skb_ts_finish;
2667 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2669 ret = textsearch_find(config, state);
2670 return (ret <= to - from ? ret : UINT_MAX);
2672 EXPORT_SYMBOL(skb_find_text);
2675 * skb_append_datato_frags - append the user data to a skb
2676 * @sk: sock structure
2677 * @skb: skb structure to be appened with user data.
2678 * @getfrag: call back function to be used for getting the user data
2679 * @from: pointer to user message iov
2680 * @length: length of the iov message
2682 * Description: This procedure append the user data in the fragment part
2683 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2685 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2686 int (*getfrag)(void *from, char *to, int offset,
2687 int len, int odd, struct sk_buff *skb),
2688 void *from, int length)
2690 int frg_cnt = skb_shinfo(skb)->nr_frags;
2694 struct page_frag *pfrag = ¤t->task_frag;
2697 /* Return error if we don't have space for new frag */
2698 if (frg_cnt >= MAX_SKB_FRAGS)
2701 if (!sk_page_frag_refill(sk, pfrag))
2704 /* copy the user data to page */
2705 copy = min_t(int, length, pfrag->size - pfrag->offset);
2707 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2708 offset, copy, 0, skb);
2712 /* copy was successful so update the size parameters */
2713 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2716 pfrag->offset += copy;
2717 get_page(pfrag->page);
2719 skb->truesize += copy;
2720 atomic_add(copy, &sk->sk_wmem_alloc);
2722 skb->data_len += copy;
2726 } while (length > 0);
2730 EXPORT_SYMBOL(skb_append_datato_frags);
2733 * skb_pull_rcsum - pull skb and update receive checksum
2734 * @skb: buffer to update
2735 * @len: length of data pulled
2737 * This function performs an skb_pull on the packet and updates
2738 * the CHECKSUM_COMPLETE checksum. It should be used on
2739 * receive path processing instead of skb_pull unless you know
2740 * that the checksum difference is zero (e.g., a valid IP header)
2741 * or you are setting ip_summed to CHECKSUM_NONE.
2743 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2745 BUG_ON(len > skb->len);
2747 BUG_ON(skb->len < skb->data_len);
2748 skb_postpull_rcsum(skb, skb->data, len);
2749 return skb->data += len;
2751 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2754 * skb_segment - Perform protocol segmentation on skb.
2755 * @skb: buffer to segment
2756 * @features: features for the output path (see dev->features)
2758 * This function performs segmentation on the given skb. It returns
2759 * a pointer to the first in a list of new skbs for the segments.
2760 * In case of error it returns ERR_PTR(err).
2762 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2764 struct sk_buff *segs = NULL;
2765 struct sk_buff *tail = NULL;
2766 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2767 unsigned int mss = skb_shinfo(skb)->gso_size;
2768 unsigned int doffset = skb->data - skb_mac_header(skb);
2769 unsigned int offset = doffset;
2770 unsigned int headroom;
2772 int sg = !!(features & NETIF_F_SG);
2773 int nfrags = skb_shinfo(skb)->nr_frags;
2778 __skb_push(skb, doffset);
2779 headroom = skb_headroom(skb);
2780 pos = skb_headlen(skb);
2783 struct sk_buff *nskb;
2788 len = skb->len - offset;
2792 hsize = skb_headlen(skb) - offset;
2795 if (hsize > len || !sg)
2798 if (!hsize && i >= nfrags) {
2799 BUG_ON(fskb->len != len);
2802 nskb = skb_clone(fskb, GFP_ATOMIC);
2805 if (unlikely(!nskb))
2808 hsize = skb_end_offset(nskb);
2809 if (skb_cow_head(nskb, doffset + headroom)) {
2814 nskb->truesize += skb_end_offset(nskb) - hsize;
2815 skb_release_head_state(nskb);
2816 __skb_push(nskb, doffset);
2818 nskb = __alloc_skb(hsize + doffset + headroom,
2819 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2822 if (unlikely(!nskb))
2825 skb_reserve(nskb, headroom);
2826 __skb_put(nskb, doffset);
2835 __copy_skb_header(nskb, skb);
2836 nskb->mac_len = skb->mac_len;
2838 /* nskb and skb might have different headroom */
2839 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2840 nskb->csum_start += skb_headroom(nskb) - headroom;
2842 skb_reset_mac_header(nskb);
2843 skb_set_network_header(nskb, skb->mac_len);
2844 nskb->transport_header = (nskb->network_header +
2845 skb_network_header_len(skb));
2846 skb_copy_from_linear_data(skb, nskb->data, doffset);
2848 if (fskb != skb_shinfo(skb)->frag_list)
2852 nskb->ip_summed = CHECKSUM_NONE;
2853 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2859 frag = skb_shinfo(nskb)->frags;
2861 skb_copy_from_linear_data_offset(skb, offset,
2862 skb_put(nskb, hsize), hsize);
2864 while (pos < offset + len && i < nfrags) {
2865 *frag = skb_shinfo(skb)->frags[i];
2866 __skb_frag_ref(frag);
2867 size = skb_frag_size(frag);
2870 frag->page_offset += offset - pos;
2871 skb_frag_size_sub(frag, offset - pos);
2874 skb_shinfo(nskb)->nr_frags++;
2876 if (pos + size <= offset + len) {
2880 skb_frag_size_sub(frag, pos + size - (offset + len));
2887 if (pos < offset + len) {
2888 struct sk_buff *fskb2 = fskb;
2890 BUG_ON(pos + fskb->len != offset + len);
2896 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2902 SKB_FRAG_ASSERT(nskb);
2903 skb_shinfo(nskb)->frag_list = fskb2;
2907 nskb->data_len = len - hsize;
2908 nskb->len += nskb->data_len;
2909 nskb->truesize += nskb->data_len;
2910 } while ((offset += len) < skb->len);
2915 while ((skb = segs)) {
2919 return ERR_PTR(err);
2921 EXPORT_SYMBOL_GPL(skb_segment);
2923 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2925 struct sk_buff *p = *head;
2926 struct sk_buff *nskb;
2927 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2928 struct skb_shared_info *pinfo = skb_shinfo(p);
2929 unsigned int headroom;
2930 unsigned int len = skb_gro_len(skb);
2931 unsigned int offset = skb_gro_offset(skb);
2932 unsigned int headlen = skb_headlen(skb);
2933 unsigned int delta_truesize;
2935 if (p->len + len >= 65536)
2938 if (pinfo->frag_list)
2940 else if (headlen <= offset) {
2943 int i = skbinfo->nr_frags;
2944 int nr_frags = pinfo->nr_frags + i;
2948 if (nr_frags > MAX_SKB_FRAGS)
2951 pinfo->nr_frags = nr_frags;
2952 skbinfo->nr_frags = 0;
2954 frag = pinfo->frags + nr_frags;
2955 frag2 = skbinfo->frags + i;
2960 frag->page_offset += offset;
2961 skb_frag_size_sub(frag, offset);
2963 /* all fragments truesize : remove (head size + sk_buff) */
2964 delta_truesize = skb->truesize -
2965 SKB_TRUESIZE(skb_end_offset(skb));
2967 skb->truesize -= skb->data_len;
2968 skb->len -= skb->data_len;
2971 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
2973 } else if (skb->head_frag) {
2974 int nr_frags = pinfo->nr_frags;
2975 skb_frag_t *frag = pinfo->frags + nr_frags;
2976 struct page *page = virt_to_head_page(skb->head);
2977 unsigned int first_size = headlen - offset;
2978 unsigned int first_offset;
2980 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2983 first_offset = skb->data -
2984 (unsigned char *)page_address(page) +
2987 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2989 frag->page.p = page;
2990 frag->page_offset = first_offset;
2991 skb_frag_size_set(frag, first_size);
2993 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2994 /* We dont need to clear skbinfo->nr_frags here */
2996 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
2997 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2999 } else if (skb_gro_len(p) != pinfo->gso_size)
3002 headroom = skb_headroom(p);
3003 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3004 if (unlikely(!nskb))
3007 __copy_skb_header(nskb, p);
3008 nskb->mac_len = p->mac_len;
3010 skb_reserve(nskb, headroom);
3011 __skb_put(nskb, skb_gro_offset(p));
3013 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3014 skb_set_network_header(nskb, skb_network_offset(p));
3015 skb_set_transport_header(nskb, skb_transport_offset(p));
3017 __skb_pull(p, skb_gro_offset(p));
3018 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3019 p->data - skb_mac_header(p));
3021 skb_shinfo(nskb)->frag_list = p;
3022 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3023 pinfo->gso_size = 0;
3024 skb_header_release(p);
3025 NAPI_GRO_CB(nskb)->last = p;
3027 nskb->data_len += p->len;
3028 nskb->truesize += p->truesize;
3029 nskb->len += p->len;
3032 nskb->next = p->next;
3038 delta_truesize = skb->truesize;
3039 if (offset > headlen) {
3040 unsigned int eat = offset - headlen;
3042 skbinfo->frags[0].page_offset += eat;
3043 skb_frag_size_sub(&skbinfo->frags[0], eat);
3044 skb->data_len -= eat;
3049 __skb_pull(skb, offset);
3051 NAPI_GRO_CB(p)->last->next = skb;
3052 NAPI_GRO_CB(p)->last = skb;
3053 skb_header_release(skb);
3056 NAPI_GRO_CB(p)->count++;
3058 p->truesize += delta_truesize;
3061 NAPI_GRO_CB(skb)->same_flow = 1;
3064 EXPORT_SYMBOL_GPL(skb_gro_receive);
3066 void __init skb_init(void)
3068 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3069 sizeof(struct sk_buff),
3071 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3073 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3074 (2*sizeof(struct sk_buff)) +
3077 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3082 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3083 * @skb: Socket buffer containing the buffers to be mapped
3084 * @sg: The scatter-gather list to map into
3085 * @offset: The offset into the buffer's contents to start mapping
3086 * @len: Length of buffer space to be mapped
3088 * Fill the specified scatter-gather list with mappings/pointers into a
3089 * region of the buffer space attached to a socket buffer.
3092 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3094 int start = skb_headlen(skb);
3095 int i, copy = start - offset;
3096 struct sk_buff *frag_iter;
3102 sg_set_buf(sg, skb->data + offset, copy);
3104 if ((len -= copy) == 0)
3109 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3112 WARN_ON(start > offset + len);
3114 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3115 if ((copy = end - offset) > 0) {
3116 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3120 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3121 frag->page_offset+offset-start);
3130 skb_walk_frags(skb, frag_iter) {
3133 WARN_ON(start > offset + len);
3135 end = start + frag_iter->len;
3136 if ((copy = end - offset) > 0) {
3139 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3141 if ((len -= copy) == 0)
3151 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3153 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3155 sg_mark_end(&sg[nsg - 1]);
3159 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3162 * skb_cow_data - Check that a socket buffer's data buffers are writable
3163 * @skb: The socket buffer to check.
3164 * @tailbits: Amount of trailing space to be added
3165 * @trailer: Returned pointer to the skb where the @tailbits space begins
3167 * Make sure that the data buffers attached to a socket buffer are
3168 * writable. If they are not, private copies are made of the data buffers
3169 * and the socket buffer is set to use these instead.
3171 * If @tailbits is given, make sure that there is space to write @tailbits
3172 * bytes of data beyond current end of socket buffer. @trailer will be
3173 * set to point to the skb in which this space begins.
3175 * The number of scatterlist elements required to completely map the
3176 * COW'd and extended socket buffer will be returned.
3178 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3182 struct sk_buff *skb1, **skb_p;
3184 /* If skb is cloned or its head is paged, reallocate
3185 * head pulling out all the pages (pages are considered not writable
3186 * at the moment even if they are anonymous).
3188 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3189 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3192 /* Easy case. Most of packets will go this way. */
3193 if (!skb_has_frag_list(skb)) {
3194 /* A little of trouble, not enough of space for trailer.
3195 * This should not happen, when stack is tuned to generate
3196 * good frames. OK, on miss we reallocate and reserve even more
3197 * space, 128 bytes is fair. */
3199 if (skb_tailroom(skb) < tailbits &&
3200 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3208 /* Misery. We are in troubles, going to mincer fragments... */
3211 skb_p = &skb_shinfo(skb)->frag_list;
3214 while ((skb1 = *skb_p) != NULL) {
3217 /* The fragment is partially pulled by someone,
3218 * this can happen on input. Copy it and everything
3221 if (skb_shared(skb1))
3224 /* If the skb is the last, worry about trailer. */
3226 if (skb1->next == NULL && tailbits) {
3227 if (skb_shinfo(skb1)->nr_frags ||
3228 skb_has_frag_list(skb1) ||
3229 skb_tailroom(skb1) < tailbits)
3230 ntail = tailbits + 128;
3236 skb_shinfo(skb1)->nr_frags ||
3237 skb_has_frag_list(skb1)) {
3238 struct sk_buff *skb2;
3240 /* Fuck, we are miserable poor guys... */
3242 skb2 = skb_copy(skb1, GFP_ATOMIC);
3244 skb2 = skb_copy_expand(skb1,
3248 if (unlikely(skb2 == NULL))
3252 skb_set_owner_w(skb2, skb1->sk);
3254 /* Looking around. Are we still alive?
3255 * OK, link new skb, drop old one */
3257 skb2->next = skb1->next;
3264 skb_p = &skb1->next;
3269 EXPORT_SYMBOL_GPL(skb_cow_data);
3271 static void sock_rmem_free(struct sk_buff *skb)
3273 struct sock *sk = skb->sk;
3275 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3279 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3281 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3285 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3286 (unsigned int)sk->sk_rcvbuf)
3291 skb->destructor = sock_rmem_free;
3292 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3294 /* before exiting rcu section, make sure dst is refcounted */
3297 skb_queue_tail(&sk->sk_error_queue, skb);
3298 if (!sock_flag(sk, SOCK_DEAD))
3299 sk->sk_data_ready(sk, len);
3302 EXPORT_SYMBOL(sock_queue_err_skb);
3304 void skb_tstamp_tx(struct sk_buff *orig_skb,
3305 struct skb_shared_hwtstamps *hwtstamps)
3307 struct sock *sk = orig_skb->sk;
3308 struct sock_exterr_skb *serr;
3309 struct sk_buff *skb;
3315 skb = skb_clone(orig_skb, GFP_ATOMIC);
3320 *skb_hwtstamps(skb) =
3324 * no hardware time stamps available,
3325 * so keep the shared tx_flags and only
3326 * store software time stamp
3328 skb->tstamp = ktime_get_real();
3331 serr = SKB_EXT_ERR(skb);
3332 memset(serr, 0, sizeof(*serr));
3333 serr->ee.ee_errno = ENOMSG;
3334 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3336 err = sock_queue_err_skb(sk, skb);
3341 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3343 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3345 struct sock *sk = skb->sk;
3346 struct sock_exterr_skb *serr;
3349 skb->wifi_acked_valid = 1;
3350 skb->wifi_acked = acked;
3352 serr = SKB_EXT_ERR(skb);
3353 memset(serr, 0, sizeof(*serr));
3354 serr->ee.ee_errno = ENOMSG;
3355 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3357 err = sock_queue_err_skb(sk, skb);
3361 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3365 * skb_partial_csum_set - set up and verify partial csum values for packet
3366 * @skb: the skb to set
3367 * @start: the number of bytes after skb->data to start checksumming.
3368 * @off: the offset from start to place the checksum.
3370 * For untrusted partially-checksummed packets, we need to make sure the values
3371 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3373 * This function checks and sets those values and skb->ip_summed: if this
3374 * returns false you should drop the packet.
3376 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3378 if (unlikely(start > skb_headlen(skb)) ||
3379 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3380 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3381 start, off, skb_headlen(skb));
3384 skb->ip_summed = CHECKSUM_PARTIAL;
3385 skb->csum_start = skb_headroom(skb) + start;
3386 skb->csum_offset = off;
3389 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3391 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3393 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3396 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3398 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3401 skb_release_head_state(skb);
3402 kmem_cache_free(skbuff_head_cache, skb);
3407 EXPORT_SYMBOL(kfree_skb_partial);
3410 * skb_try_coalesce - try to merge skb to prior one
3412 * @from: buffer to add
3413 * @fragstolen: pointer to boolean
3414 * @delta_truesize: how much more was allocated than was requested
3416 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3417 bool *fragstolen, int *delta_truesize)
3419 int i, delta, len = from->len;
3421 *fragstolen = false;
3426 if (len <= skb_tailroom(to)) {
3427 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3428 *delta_truesize = 0;
3432 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3435 if (skb_headlen(from) != 0) {
3437 unsigned int offset;
3439 if (skb_shinfo(to)->nr_frags +
3440 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3443 if (skb_head_is_locked(from))
3446 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3448 page = virt_to_head_page(from->head);
3449 offset = from->data - (unsigned char *)page_address(page);
3451 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3452 page, offset, skb_headlen(from));
3455 if (skb_shinfo(to)->nr_frags +
3456 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3459 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3462 WARN_ON_ONCE(delta < len);
3464 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3465 skb_shinfo(from)->frags,
3466 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3467 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3469 if (!skb_cloned(from))
3470 skb_shinfo(from)->nr_frags = 0;
3472 /* if the skb is not cloned this does nothing
3473 * since we set nr_frags to 0.
3475 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3476 skb_frag_ref(from, i);
3478 to->truesize += delta;
3480 to->data_len += len;
3482 *delta_truesize = delta;
3485 EXPORT_SYMBOL(skb_try_coalesce);