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)
158 void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip,
162 bool ret_pfmemalloc = false;
165 * Try a regular allocation, when that fails and we're not entitled
166 * to the reserves, fail.
168 obj = kmalloc_node_track_caller(size,
169 flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
171 if (obj || !(gfp_pfmemalloc_allowed(flags)))
174 /* Try again but now we are using pfmemalloc reserves */
175 ret_pfmemalloc = true;
176 obj = kmalloc_node_track_caller(size, flags, node);
180 *pfmemalloc = ret_pfmemalloc;
185 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
186 * 'private' fields and also do memory statistics to find all the
192 * __alloc_skb - allocate a network buffer
193 * @size: size to allocate
194 * @gfp_mask: allocation mask
195 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
196 * instead of head cache and allocate a cloned (child) skb.
197 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
198 * allocations in case the data is required for writeback
199 * @node: numa node to allocate memory on
201 * Allocate a new &sk_buff. The returned buffer has no headroom and a
202 * tail room of at least size bytes. The object has a reference count
203 * of one. The return is the buffer. On a failure the return is %NULL.
205 * Buffers may only be allocated from interrupts using a @gfp_mask of
208 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
211 struct kmem_cache *cache;
212 struct skb_shared_info *shinfo;
217 cache = (flags & SKB_ALLOC_FCLONE)
218 ? skbuff_fclone_cache : skbuff_head_cache;
220 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
221 gfp_mask |= __GFP_MEMALLOC;
224 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
229 /* We do our best to align skb_shared_info on a separate cache
230 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
231 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
232 * Both skb->head and skb_shared_info are cache line aligned.
234 size = SKB_DATA_ALIGN(size);
235 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
236 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
239 /* kmalloc(size) might give us more room than requested.
240 * Put skb_shared_info exactly at the end of allocated zone,
241 * to allow max possible filling before reallocation.
243 size = SKB_WITH_OVERHEAD(ksize(data));
244 prefetchw(data + size);
247 * Only clear those fields we need to clear, not those that we will
248 * actually initialise below. Hence, don't put any more fields after
249 * the tail pointer in struct sk_buff!
251 memset(skb, 0, offsetof(struct sk_buff, tail));
252 /* Account for allocated memory : skb + skb->head */
253 skb->truesize = SKB_TRUESIZE(size);
254 skb->pfmemalloc = pfmemalloc;
255 atomic_set(&skb->users, 1);
258 skb_reset_tail_pointer(skb);
259 skb->end = skb->tail + size;
260 #ifdef NET_SKBUFF_DATA_USES_OFFSET
261 skb->mac_header = ~0U;
264 /* make sure we initialize shinfo sequentially */
265 shinfo = skb_shinfo(skb);
266 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
267 atomic_set(&shinfo->dataref, 1);
268 kmemcheck_annotate_variable(shinfo->destructor_arg);
270 if (flags & SKB_ALLOC_FCLONE) {
271 struct sk_buff *child = skb + 1;
272 atomic_t *fclone_ref = (atomic_t *) (child + 1);
274 kmemcheck_annotate_bitfield(child, flags1);
275 kmemcheck_annotate_bitfield(child, flags2);
276 skb->fclone = SKB_FCLONE_ORIG;
277 atomic_set(fclone_ref, 1);
279 child->fclone = SKB_FCLONE_UNAVAILABLE;
280 child->pfmemalloc = pfmemalloc;
285 kmem_cache_free(cache, skb);
289 EXPORT_SYMBOL(__alloc_skb);
292 * build_skb - build a network buffer
293 * @data: data buffer provided by caller
294 * @frag_size: size of fragment, or 0 if head was kmalloced
296 * Allocate a new &sk_buff. Caller provides space holding head and
297 * skb_shared_info. @data must have been allocated by kmalloc()
298 * The return is the new skb buffer.
299 * On a failure the return is %NULL, and @data is not freed.
301 * Before IO, driver allocates only data buffer where NIC put incoming frame
302 * Driver should add room at head (NET_SKB_PAD) and
303 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
304 * After IO, driver calls build_skb(), to allocate sk_buff and populate it
305 * before giving packet to stack.
306 * RX rings only contains data buffers, not full skbs.
308 struct sk_buff *build_skb(void *data, unsigned int frag_size)
310 struct skb_shared_info *shinfo;
312 unsigned int size = frag_size ? : ksize(data);
314 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
318 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
320 memset(skb, 0, offsetof(struct sk_buff, tail));
321 skb->truesize = SKB_TRUESIZE(size);
322 skb->head_frag = frag_size != 0;
323 atomic_set(&skb->users, 1);
326 skb_reset_tail_pointer(skb);
327 skb->end = skb->tail + size;
328 #ifdef NET_SKBUFF_DATA_USES_OFFSET
329 skb->mac_header = ~0U;
332 /* make sure we initialize shinfo sequentially */
333 shinfo = skb_shinfo(skb);
334 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
335 atomic_set(&shinfo->dataref, 1);
336 kmemcheck_annotate_variable(shinfo->destructor_arg);
340 EXPORT_SYMBOL(build_skb);
342 struct netdev_alloc_cache {
345 unsigned int pagecnt_bias;
347 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
349 #define NETDEV_PAGECNT_BIAS (PAGE_SIZE / SMP_CACHE_BYTES)
351 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
353 struct netdev_alloc_cache *nc;
357 local_irq_save(flags);
358 nc = &__get_cpu_var(netdev_alloc_cache);
359 if (unlikely(!nc->page)) {
361 nc->page = alloc_page(gfp_mask);
362 if (unlikely(!nc->page))
365 atomic_set(&nc->page->_count, NETDEV_PAGECNT_BIAS);
366 nc->pagecnt_bias = NETDEV_PAGECNT_BIAS;
370 if (nc->offset + fragsz > PAGE_SIZE) {
371 /* avoid unnecessary locked operations if possible */
372 if ((atomic_read(&nc->page->_count) == nc->pagecnt_bias) ||
373 atomic_sub_and_test(nc->pagecnt_bias, &nc->page->_count))
378 data = page_address(nc->page) + nc->offset;
379 nc->offset += fragsz;
382 local_irq_restore(flags);
387 * netdev_alloc_frag - allocate a page fragment
388 * @fragsz: fragment size
390 * Allocates a frag from a page for receive buffer.
391 * Uses GFP_ATOMIC allocations.
393 void *netdev_alloc_frag(unsigned int fragsz)
395 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
397 EXPORT_SYMBOL(netdev_alloc_frag);
400 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
401 * @dev: network device to receive on
402 * @length: length to allocate
403 * @gfp_mask: get_free_pages mask, passed to alloc_skb
405 * Allocate a new &sk_buff and assign it a usage count of one. The
406 * buffer has unspecified headroom built in. Users should allocate
407 * the headroom they think they need without accounting for the
408 * built in space. The built in space is used for optimisations.
410 * %NULL is returned if there is no free memory.
412 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
413 unsigned int length, gfp_t gfp_mask)
415 struct sk_buff *skb = NULL;
416 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
417 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
419 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
422 if (sk_memalloc_socks())
423 gfp_mask |= __GFP_MEMALLOC;
425 data = __netdev_alloc_frag(fragsz, gfp_mask);
428 skb = build_skb(data, fragsz);
430 put_page(virt_to_head_page(data));
433 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
434 SKB_ALLOC_RX, NUMA_NO_NODE);
437 skb_reserve(skb, NET_SKB_PAD);
442 EXPORT_SYMBOL(__netdev_alloc_skb);
444 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
445 int size, unsigned int truesize)
447 skb_fill_page_desc(skb, i, page, off, size);
449 skb->data_len += size;
450 skb->truesize += truesize;
452 EXPORT_SYMBOL(skb_add_rx_frag);
454 static void skb_drop_list(struct sk_buff **listp)
456 struct sk_buff *list = *listp;
461 struct sk_buff *this = list;
467 static inline void skb_drop_fraglist(struct sk_buff *skb)
469 skb_drop_list(&skb_shinfo(skb)->frag_list);
472 static void skb_clone_fraglist(struct sk_buff *skb)
474 struct sk_buff *list;
476 skb_walk_frags(skb, list)
480 static void skb_free_head(struct sk_buff *skb)
483 put_page(virt_to_head_page(skb->head));
488 static void skb_release_data(struct sk_buff *skb)
491 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
492 &skb_shinfo(skb)->dataref)) {
493 if (skb_shinfo(skb)->nr_frags) {
495 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
496 skb_frag_unref(skb, i);
500 * If skb buf is from userspace, we need to notify the caller
501 * the lower device DMA has done;
503 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
504 struct ubuf_info *uarg;
506 uarg = skb_shinfo(skb)->destructor_arg;
508 uarg->callback(uarg);
511 if (skb_has_frag_list(skb))
512 skb_drop_fraglist(skb);
519 * Free an skbuff by memory without cleaning the state.
521 static void kfree_skbmem(struct sk_buff *skb)
523 struct sk_buff *other;
524 atomic_t *fclone_ref;
526 switch (skb->fclone) {
527 case SKB_FCLONE_UNAVAILABLE:
528 kmem_cache_free(skbuff_head_cache, skb);
531 case SKB_FCLONE_ORIG:
532 fclone_ref = (atomic_t *) (skb + 2);
533 if (atomic_dec_and_test(fclone_ref))
534 kmem_cache_free(skbuff_fclone_cache, skb);
537 case SKB_FCLONE_CLONE:
538 fclone_ref = (atomic_t *) (skb + 1);
541 /* The clone portion is available for
542 * fast-cloning again.
544 skb->fclone = SKB_FCLONE_UNAVAILABLE;
546 if (atomic_dec_and_test(fclone_ref))
547 kmem_cache_free(skbuff_fclone_cache, other);
552 static void skb_release_head_state(struct sk_buff *skb)
556 secpath_put(skb->sp);
558 if (skb->destructor) {
560 skb->destructor(skb);
562 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
563 nf_conntrack_put(skb->nfct);
565 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
566 nf_conntrack_put_reasm(skb->nfct_reasm);
568 #ifdef CONFIG_BRIDGE_NETFILTER
569 nf_bridge_put(skb->nf_bridge);
571 /* XXX: IS this still necessary? - JHS */
572 #ifdef CONFIG_NET_SCHED
574 #ifdef CONFIG_NET_CLS_ACT
580 /* Free everything but the sk_buff shell. */
581 static void skb_release_all(struct sk_buff *skb)
583 skb_release_head_state(skb);
584 skb_release_data(skb);
588 * __kfree_skb - private function
591 * Free an sk_buff. Release anything attached to the buffer.
592 * Clean the state. This is an internal helper function. Users should
593 * always call kfree_skb
596 void __kfree_skb(struct sk_buff *skb)
598 skb_release_all(skb);
601 EXPORT_SYMBOL(__kfree_skb);
604 * kfree_skb - free an sk_buff
605 * @skb: buffer to free
607 * Drop a reference to the buffer and free it if the usage count has
610 void kfree_skb(struct sk_buff *skb)
614 if (likely(atomic_read(&skb->users) == 1))
616 else if (likely(!atomic_dec_and_test(&skb->users)))
618 trace_kfree_skb(skb, __builtin_return_address(0));
621 EXPORT_SYMBOL(kfree_skb);
624 * consume_skb - free an skbuff
625 * @skb: buffer to free
627 * Drop a ref to the buffer and free it if the usage count has hit zero
628 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
629 * is being dropped after a failure and notes that
631 void consume_skb(struct sk_buff *skb)
635 if (likely(atomic_read(&skb->users) == 1))
637 else if (likely(!atomic_dec_and_test(&skb->users)))
639 trace_consume_skb(skb);
642 EXPORT_SYMBOL(consume_skb);
645 * skb_recycle - clean up an skb for reuse
648 * Recycles the skb to be reused as a receive buffer. This
649 * function does any necessary reference count dropping, and
650 * cleans up the skbuff as if it just came from __alloc_skb().
652 void skb_recycle(struct sk_buff *skb)
654 struct skb_shared_info *shinfo;
656 skb_release_head_state(skb);
658 shinfo = skb_shinfo(skb);
659 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
660 atomic_set(&shinfo->dataref, 1);
662 memset(skb, 0, offsetof(struct sk_buff, tail));
663 skb->data = skb->head + NET_SKB_PAD;
664 skb_reset_tail_pointer(skb);
666 EXPORT_SYMBOL(skb_recycle);
669 * skb_recycle_check - check if skb can be reused for receive
671 * @skb_size: minimum receive buffer size
673 * Checks that the skb passed in is not shared or cloned, and
674 * that it is linear and its head portion at least as large as
675 * skb_size so that it can be recycled as a receive buffer.
676 * If these conditions are met, this function does any necessary
677 * reference count dropping and cleans up the skbuff as if it
678 * just came from __alloc_skb().
680 bool skb_recycle_check(struct sk_buff *skb, int skb_size)
682 if (!skb_is_recycleable(skb, skb_size))
689 EXPORT_SYMBOL(skb_recycle_check);
691 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
693 new->tstamp = old->tstamp;
695 new->transport_header = old->transport_header;
696 new->network_header = old->network_header;
697 new->mac_header = old->mac_header;
698 skb_dst_copy(new, old);
699 new->rxhash = old->rxhash;
700 new->ooo_okay = old->ooo_okay;
701 new->l4_rxhash = old->l4_rxhash;
702 new->no_fcs = old->no_fcs;
704 new->sp = secpath_get(old->sp);
706 memcpy(new->cb, old->cb, sizeof(old->cb));
707 new->csum = old->csum;
708 new->local_df = old->local_df;
709 new->pkt_type = old->pkt_type;
710 new->ip_summed = old->ip_summed;
711 skb_copy_queue_mapping(new, old);
712 new->priority = old->priority;
713 #if IS_ENABLED(CONFIG_IP_VS)
714 new->ipvs_property = old->ipvs_property;
716 new->pfmemalloc = old->pfmemalloc;
717 new->protocol = old->protocol;
718 new->mark = old->mark;
719 new->skb_iif = old->skb_iif;
721 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
722 new->nf_trace = old->nf_trace;
724 #ifdef CONFIG_NET_SCHED
725 new->tc_index = old->tc_index;
726 #ifdef CONFIG_NET_CLS_ACT
727 new->tc_verd = old->tc_verd;
730 new->vlan_tci = old->vlan_tci;
732 skb_copy_secmark(new, old);
736 * You should not add any new code to this function. Add it to
737 * __copy_skb_header above instead.
739 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
741 #define C(x) n->x = skb->x
743 n->next = n->prev = NULL;
745 __copy_skb_header(n, skb);
750 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
753 n->destructor = NULL;
760 atomic_set(&n->users, 1);
762 atomic_inc(&(skb_shinfo(skb)->dataref));
770 * skb_morph - morph one skb into another
771 * @dst: the skb to receive the contents
772 * @src: the skb to supply the contents
774 * This is identical to skb_clone except that the target skb is
775 * supplied by the user.
777 * The target skb is returned upon exit.
779 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
781 skb_release_all(dst);
782 return __skb_clone(dst, src);
784 EXPORT_SYMBOL_GPL(skb_morph);
787 * skb_copy_ubufs - copy userspace skb frags buffers to kernel
788 * @skb: the skb to modify
789 * @gfp_mask: allocation priority
791 * This must be called on SKBTX_DEV_ZEROCOPY skb.
792 * It will copy all frags into kernel and drop the reference
793 * to userspace pages.
795 * If this function is called from an interrupt gfp_mask() must be
798 * Returns 0 on success or a negative error code on failure
799 * to allocate kernel memory to copy to.
801 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
804 int num_frags = skb_shinfo(skb)->nr_frags;
805 struct page *page, *head = NULL;
806 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
808 for (i = 0; i < num_frags; i++) {
810 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
812 page = alloc_page(gfp_mask);
815 struct page *next = (struct page *)head->private;
821 vaddr = kmap_atomic(skb_frag_page(f));
822 memcpy(page_address(page),
823 vaddr + f->page_offset, skb_frag_size(f));
824 kunmap_atomic(vaddr);
825 page->private = (unsigned long)head;
829 /* skb frags release userspace buffers */
830 for (i = 0; i < num_frags; i++)
831 skb_frag_unref(skb, i);
833 uarg->callback(uarg);
835 /* skb frags point to kernel buffers */
836 for (i = num_frags - 1; i >= 0; i--) {
837 __skb_fill_page_desc(skb, i, head, 0,
838 skb_shinfo(skb)->frags[i].size);
839 head = (struct page *)head->private;
842 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
845 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
848 * skb_clone - duplicate an sk_buff
849 * @skb: buffer to clone
850 * @gfp_mask: allocation priority
852 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
853 * copies share the same packet data but not structure. The new
854 * buffer has a reference count of 1. If the allocation fails the
855 * function returns %NULL otherwise the new buffer is returned.
857 * If this function is called from an interrupt gfp_mask() must be
861 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
865 if (skb_orphan_frags(skb, gfp_mask))
869 if (skb->fclone == SKB_FCLONE_ORIG &&
870 n->fclone == SKB_FCLONE_UNAVAILABLE) {
871 atomic_t *fclone_ref = (atomic_t *) (n + 1);
872 n->fclone = SKB_FCLONE_CLONE;
873 atomic_inc(fclone_ref);
875 if (skb_pfmemalloc(skb))
876 gfp_mask |= __GFP_MEMALLOC;
878 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
882 kmemcheck_annotate_bitfield(n, flags1);
883 kmemcheck_annotate_bitfield(n, flags2);
884 n->fclone = SKB_FCLONE_UNAVAILABLE;
887 return __skb_clone(n, skb);
889 EXPORT_SYMBOL(skb_clone);
891 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
893 #ifndef NET_SKBUFF_DATA_USES_OFFSET
895 * Shift between the two data areas in bytes
897 unsigned long offset = new->data - old->data;
900 __copy_skb_header(new, old);
902 #ifndef NET_SKBUFF_DATA_USES_OFFSET
903 /* {transport,network,mac}_header are relative to skb->head */
904 new->transport_header += offset;
905 new->network_header += offset;
906 if (skb_mac_header_was_set(new))
907 new->mac_header += offset;
909 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
910 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
911 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
914 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
916 if (skb_pfmemalloc(skb))
922 * skb_copy - create private copy of an sk_buff
923 * @skb: buffer to copy
924 * @gfp_mask: allocation priority
926 * Make a copy of both an &sk_buff and its data. This is used when the
927 * caller wishes to modify the data and needs a private copy of the
928 * data to alter. Returns %NULL on failure or the pointer to the buffer
929 * on success. The returned buffer has a reference count of 1.
931 * As by-product this function converts non-linear &sk_buff to linear
932 * one, so that &sk_buff becomes completely private and caller is allowed
933 * to modify all the data of returned buffer. This means that this
934 * function is not recommended for use in circumstances when only
935 * header is going to be modified. Use pskb_copy() instead.
938 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
940 int headerlen = skb_headroom(skb);
941 unsigned int size = skb_end_offset(skb) + skb->data_len;
942 struct sk_buff *n = __alloc_skb(size, gfp_mask,
943 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
948 /* Set the data pointer */
949 skb_reserve(n, headerlen);
950 /* Set the tail pointer and length */
951 skb_put(n, skb->len);
953 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
956 copy_skb_header(n, skb);
959 EXPORT_SYMBOL(skb_copy);
962 * __pskb_copy - create copy of an sk_buff with private head.
963 * @skb: buffer to copy
964 * @headroom: headroom of new skb
965 * @gfp_mask: allocation priority
967 * Make a copy of both an &sk_buff and part of its data, located
968 * in header. Fragmented data remain shared. This is used when
969 * the caller wishes to modify only header of &sk_buff and needs
970 * private copy of the header to alter. Returns %NULL on failure
971 * or the pointer to the buffer on success.
972 * The returned buffer has a reference count of 1.
975 struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
977 unsigned int size = skb_headlen(skb) + headroom;
978 struct sk_buff *n = __alloc_skb(size, gfp_mask,
979 skb_alloc_rx_flag(skb), NUMA_NO_NODE);
984 /* Set the data pointer */
985 skb_reserve(n, headroom);
986 /* Set the tail pointer and length */
987 skb_put(n, skb_headlen(skb));
989 skb_copy_from_linear_data(skb, n->data, n->len);
991 n->truesize += skb->data_len;
992 n->data_len = skb->data_len;
995 if (skb_shinfo(skb)->nr_frags) {
998 if (skb_orphan_frags(skb, gfp_mask)) {
1003 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1004 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1005 skb_frag_ref(skb, i);
1007 skb_shinfo(n)->nr_frags = i;
1010 if (skb_has_frag_list(skb)) {
1011 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1012 skb_clone_fraglist(n);
1015 copy_skb_header(n, skb);
1019 EXPORT_SYMBOL(__pskb_copy);
1022 * pskb_expand_head - reallocate header of &sk_buff
1023 * @skb: buffer to reallocate
1024 * @nhead: room to add at head
1025 * @ntail: room to add at tail
1026 * @gfp_mask: allocation priority
1028 * Expands (or creates identical copy, if &nhead and &ntail are zero)
1029 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
1030 * reference count of 1. Returns zero in the case of success or error,
1031 * if expansion failed. In the last case, &sk_buff is not changed.
1033 * All the pointers pointing into skb header may change and must be
1034 * reloaded after call to this function.
1037 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1042 int size = nhead + skb_end_offset(skb) + ntail;
1047 if (skb_shared(skb))
1050 size = SKB_DATA_ALIGN(size);
1052 if (skb_pfmemalloc(skb))
1053 gfp_mask |= __GFP_MEMALLOC;
1054 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1055 gfp_mask, NUMA_NO_NODE, NULL);
1058 size = SKB_WITH_OVERHEAD(ksize(data));
1060 /* Copy only real data... and, alas, header. This should be
1061 * optimized for the cases when header is void.
1063 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1065 memcpy((struct skb_shared_info *)(data + size),
1067 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1070 * if shinfo is shared we must drop the old head gracefully, but if it
1071 * is not we can just drop the old head and let the existing refcount
1072 * be since all we did is relocate the values
1074 if (skb_cloned(skb)) {
1075 /* copy this zero copy skb frags */
1076 if (skb_orphan_frags(skb, gfp_mask))
1078 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1079 skb_frag_ref(skb, i);
1081 if (skb_has_frag_list(skb))
1082 skb_clone_fraglist(skb);
1084 skb_release_data(skb);
1088 off = (data + nhead) - skb->head;
1093 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1097 skb->end = skb->head + size;
1099 /* {transport,network,mac}_header and tail are relative to skb->head */
1101 skb->transport_header += off;
1102 skb->network_header += off;
1103 if (skb_mac_header_was_set(skb))
1104 skb->mac_header += off;
1105 /* Only adjust this if it actually is csum_start rather than csum */
1106 if (skb->ip_summed == CHECKSUM_PARTIAL)
1107 skb->csum_start += nhead;
1111 atomic_set(&skb_shinfo(skb)->dataref, 1);
1119 EXPORT_SYMBOL(pskb_expand_head);
1121 /* Make private copy of skb with writable head and some headroom */
1123 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1125 struct sk_buff *skb2;
1126 int delta = headroom - skb_headroom(skb);
1129 skb2 = pskb_copy(skb, GFP_ATOMIC);
1131 skb2 = skb_clone(skb, GFP_ATOMIC);
1132 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1140 EXPORT_SYMBOL(skb_realloc_headroom);
1143 * skb_copy_expand - copy and expand sk_buff
1144 * @skb: buffer to copy
1145 * @newheadroom: new free bytes at head
1146 * @newtailroom: new free bytes at tail
1147 * @gfp_mask: allocation priority
1149 * Make a copy of both an &sk_buff and its data and while doing so
1150 * allocate additional space.
1152 * This is used when the caller wishes to modify the data and needs a
1153 * private copy of the data to alter as well as more space for new fields.
1154 * Returns %NULL on failure or the pointer to the buffer
1155 * on success. The returned buffer has a reference count of 1.
1157 * You must pass %GFP_ATOMIC as the allocation priority if this function
1158 * is called from an interrupt.
1160 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1161 int newheadroom, int newtailroom,
1165 * Allocate the copy buffer
1167 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1168 gfp_mask, skb_alloc_rx_flag(skb),
1170 int oldheadroom = skb_headroom(skb);
1171 int head_copy_len, head_copy_off;
1177 skb_reserve(n, newheadroom);
1179 /* Set the tail pointer and length */
1180 skb_put(n, skb->len);
1182 head_copy_len = oldheadroom;
1184 if (newheadroom <= head_copy_len)
1185 head_copy_len = newheadroom;
1187 head_copy_off = newheadroom - head_copy_len;
1189 /* Copy the linear header and data. */
1190 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1191 skb->len + head_copy_len))
1194 copy_skb_header(n, skb);
1196 off = newheadroom - oldheadroom;
1197 if (n->ip_summed == CHECKSUM_PARTIAL)
1198 n->csum_start += off;
1199 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1200 n->transport_header += off;
1201 n->network_header += off;
1202 if (skb_mac_header_was_set(skb))
1203 n->mac_header += off;
1208 EXPORT_SYMBOL(skb_copy_expand);
1211 * skb_pad - zero pad the tail of an skb
1212 * @skb: buffer to pad
1213 * @pad: space to pad
1215 * Ensure that a buffer is followed by a padding area that is zero
1216 * filled. Used by network drivers which may DMA or transfer data
1217 * beyond the buffer end onto the wire.
1219 * May return error in out of memory cases. The skb is freed on error.
1222 int skb_pad(struct sk_buff *skb, int pad)
1227 /* If the skbuff is non linear tailroom is always zero.. */
1228 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1229 memset(skb->data+skb->len, 0, pad);
1233 ntail = skb->data_len + pad - (skb->end - skb->tail);
1234 if (likely(skb_cloned(skb) || ntail > 0)) {
1235 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1240 /* FIXME: The use of this function with non-linear skb's really needs
1243 err = skb_linearize(skb);
1247 memset(skb->data + skb->len, 0, pad);
1254 EXPORT_SYMBOL(skb_pad);
1257 * skb_put - add data to a buffer
1258 * @skb: buffer to use
1259 * @len: amount of data to add
1261 * This function extends the used data area of the buffer. If this would
1262 * exceed the total buffer size the kernel will panic. A pointer to the
1263 * first byte of the extra data is returned.
1265 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1267 unsigned char *tmp = skb_tail_pointer(skb);
1268 SKB_LINEAR_ASSERT(skb);
1271 if (unlikely(skb->tail > skb->end))
1272 skb_over_panic(skb, len, __builtin_return_address(0));
1275 EXPORT_SYMBOL(skb_put);
1278 * skb_push - add data to the start of a buffer
1279 * @skb: buffer to use
1280 * @len: amount of data to add
1282 * This function extends the used data area of the buffer at the buffer
1283 * start. If this would exceed the total buffer headroom the kernel will
1284 * panic. A pointer to the first byte of the extra data is returned.
1286 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1290 if (unlikely(skb->data<skb->head))
1291 skb_under_panic(skb, len, __builtin_return_address(0));
1294 EXPORT_SYMBOL(skb_push);
1297 * skb_pull - remove data from the start of a buffer
1298 * @skb: buffer to use
1299 * @len: amount of data to remove
1301 * This function removes data from the start of a buffer, returning
1302 * the memory to the headroom. A pointer to the next data in the buffer
1303 * is returned. Once the data has been pulled future pushes will overwrite
1306 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1308 return skb_pull_inline(skb, len);
1310 EXPORT_SYMBOL(skb_pull);
1313 * skb_trim - remove end from a buffer
1314 * @skb: buffer to alter
1317 * Cut the length of a buffer down by removing data from the tail. If
1318 * the buffer is already under the length specified it is not modified.
1319 * The skb must be linear.
1321 void skb_trim(struct sk_buff *skb, unsigned int len)
1324 __skb_trim(skb, len);
1326 EXPORT_SYMBOL(skb_trim);
1328 /* Trims skb to length len. It can change skb pointers.
1331 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1333 struct sk_buff **fragp;
1334 struct sk_buff *frag;
1335 int offset = skb_headlen(skb);
1336 int nfrags = skb_shinfo(skb)->nr_frags;
1340 if (skb_cloned(skb) &&
1341 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1348 for (; i < nfrags; i++) {
1349 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1356 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1359 skb_shinfo(skb)->nr_frags = i;
1361 for (; i < nfrags; i++)
1362 skb_frag_unref(skb, i);
1364 if (skb_has_frag_list(skb))
1365 skb_drop_fraglist(skb);
1369 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1370 fragp = &frag->next) {
1371 int end = offset + frag->len;
1373 if (skb_shared(frag)) {
1374 struct sk_buff *nfrag;
1376 nfrag = skb_clone(frag, GFP_ATOMIC);
1377 if (unlikely(!nfrag))
1380 nfrag->next = frag->next;
1392 unlikely((err = pskb_trim(frag, len - offset))))
1396 skb_drop_list(&frag->next);
1401 if (len > skb_headlen(skb)) {
1402 skb->data_len -= skb->len - len;
1407 skb_set_tail_pointer(skb, len);
1412 EXPORT_SYMBOL(___pskb_trim);
1415 * __pskb_pull_tail - advance tail of skb header
1416 * @skb: buffer to reallocate
1417 * @delta: number of bytes to advance tail
1419 * The function makes a sense only on a fragmented &sk_buff,
1420 * it expands header moving its tail forward and copying necessary
1421 * data from fragmented part.
1423 * &sk_buff MUST have reference count of 1.
1425 * Returns %NULL (and &sk_buff does not change) if pull failed
1426 * or value of new tail of skb in the case of success.
1428 * All the pointers pointing into skb header may change and must be
1429 * reloaded after call to this function.
1432 /* Moves tail of skb head forward, copying data from fragmented part,
1433 * when it is necessary.
1434 * 1. It may fail due to malloc failure.
1435 * 2. It may change skb pointers.
1437 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1439 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1441 /* If skb has not enough free space at tail, get new one
1442 * plus 128 bytes for future expansions. If we have enough
1443 * room at tail, reallocate without expansion only if skb is cloned.
1445 int i, k, eat = (skb->tail + delta) - skb->end;
1447 if (eat > 0 || skb_cloned(skb)) {
1448 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1453 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1456 /* Optimization: no fragments, no reasons to preestimate
1457 * size of pulled pages. Superb.
1459 if (!skb_has_frag_list(skb))
1462 /* Estimate size of pulled pages. */
1464 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1465 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1472 /* If we need update frag list, we are in troubles.
1473 * Certainly, it possible to add an offset to skb data,
1474 * but taking into account that pulling is expected to
1475 * be very rare operation, it is worth to fight against
1476 * further bloating skb head and crucify ourselves here instead.
1477 * Pure masohism, indeed. 8)8)
1480 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1481 struct sk_buff *clone = NULL;
1482 struct sk_buff *insp = NULL;
1487 if (list->len <= eat) {
1488 /* Eaten as whole. */
1493 /* Eaten partially. */
1495 if (skb_shared(list)) {
1496 /* Sucks! We need to fork list. :-( */
1497 clone = skb_clone(list, GFP_ATOMIC);
1503 /* This may be pulled without
1507 if (!pskb_pull(list, eat)) {
1515 /* Free pulled out fragments. */
1516 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1517 skb_shinfo(skb)->frag_list = list->next;
1520 /* And insert new clone at head. */
1523 skb_shinfo(skb)->frag_list = clone;
1526 /* Success! Now we may commit changes to skb data. */
1531 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1532 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1535 skb_frag_unref(skb, i);
1538 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1540 skb_shinfo(skb)->frags[k].page_offset += eat;
1541 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1547 skb_shinfo(skb)->nr_frags = k;
1550 skb->data_len -= delta;
1552 return skb_tail_pointer(skb);
1554 EXPORT_SYMBOL(__pskb_pull_tail);
1557 * skb_copy_bits - copy bits from skb to kernel buffer
1559 * @offset: offset in source
1560 * @to: destination buffer
1561 * @len: number of bytes to copy
1563 * Copy the specified number of bytes from the source skb to the
1564 * destination buffer.
1567 * If its prototype is ever changed,
1568 * check arch/{*}/net/{*}.S files,
1569 * since it is called from BPF assembly code.
1571 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1573 int start = skb_headlen(skb);
1574 struct sk_buff *frag_iter;
1577 if (offset > (int)skb->len - len)
1581 if ((copy = start - offset) > 0) {
1584 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1585 if ((len -= copy) == 0)
1591 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1593 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1595 WARN_ON(start > offset + len);
1597 end = start + skb_frag_size(f);
1598 if ((copy = end - offset) > 0) {
1604 vaddr = kmap_atomic(skb_frag_page(f));
1606 vaddr + f->page_offset + offset - start,
1608 kunmap_atomic(vaddr);
1610 if ((len -= copy) == 0)
1618 skb_walk_frags(skb, frag_iter) {
1621 WARN_ON(start > offset + len);
1623 end = start + frag_iter->len;
1624 if ((copy = end - offset) > 0) {
1627 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1629 if ((len -= copy) == 0)
1643 EXPORT_SYMBOL(skb_copy_bits);
1646 * Callback from splice_to_pipe(), if we need to release some pages
1647 * at the end of the spd in case we error'ed out in filling the pipe.
1649 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1651 put_page(spd->pages[i]);
1654 static struct page *linear_to_page(struct page *page, unsigned int *len,
1655 unsigned int *offset,
1656 struct sk_buff *skb, struct sock *sk)
1658 struct page_frag *pfrag = sk_page_frag(sk);
1660 if (!sk_page_frag_refill(sk, pfrag))
1663 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1665 memcpy(page_address(pfrag->page) + pfrag->offset,
1666 page_address(page) + *offset, *len);
1667 *offset = pfrag->offset;
1668 pfrag->offset += *len;
1673 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1675 unsigned int offset)
1677 return spd->nr_pages &&
1678 spd->pages[spd->nr_pages - 1] == page &&
1679 (spd->partial[spd->nr_pages - 1].offset +
1680 spd->partial[spd->nr_pages - 1].len == offset);
1684 * Fill page/offset/length into spd, if it can hold more pages.
1686 static bool spd_fill_page(struct splice_pipe_desc *spd,
1687 struct pipe_inode_info *pipe, struct page *page,
1688 unsigned int *len, unsigned int offset,
1689 struct sk_buff *skb, bool linear,
1692 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1696 page = linear_to_page(page, len, &offset, skb, sk);
1700 if (spd_can_coalesce(spd, page, offset)) {
1701 spd->partial[spd->nr_pages - 1].len += *len;
1705 spd->pages[spd->nr_pages] = page;
1706 spd->partial[spd->nr_pages].len = *len;
1707 spd->partial[spd->nr_pages].offset = offset;
1713 static inline void __segment_seek(struct page **page, unsigned int *poff,
1714 unsigned int *plen, unsigned int off)
1719 n = *poff / PAGE_SIZE;
1721 *page = nth_page(*page, n);
1723 *poff = *poff % PAGE_SIZE;
1727 static bool __splice_segment(struct page *page, unsigned int poff,
1728 unsigned int plen, unsigned int *off,
1729 unsigned int *len, struct sk_buff *skb,
1730 struct splice_pipe_desc *spd, bool linear,
1732 struct pipe_inode_info *pipe)
1737 /* skip this segment if already processed */
1743 /* ignore any bits we already processed */
1745 __segment_seek(&page, &poff, &plen, *off);
1750 unsigned int flen = min(*len, plen);
1752 /* the linear region may spread across several pages */
1753 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1755 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1758 __segment_seek(&page, &poff, &plen, flen);
1761 } while (*len && plen);
1767 * Map linear and fragment data from the skb to spd. It reports true if the
1768 * pipe is full or if we already spliced the requested length.
1770 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1771 unsigned int *offset, unsigned int *len,
1772 struct splice_pipe_desc *spd, struct sock *sk)
1776 /* map the linear part :
1777 * If skb->head_frag is set, this 'linear' part is backed by a
1778 * fragment, and if the head is not shared with any clones then
1779 * we can avoid a copy since we own the head portion of this page.
1781 if (__splice_segment(virt_to_page(skb->data),
1782 (unsigned long) skb->data & (PAGE_SIZE - 1),
1784 offset, len, skb, spd,
1785 skb_head_is_locked(skb),
1790 * then map the fragments
1792 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1793 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1795 if (__splice_segment(skb_frag_page(f),
1796 f->page_offset, skb_frag_size(f),
1797 offset, len, skb, spd, false, sk, pipe))
1805 * Map data from the skb to a pipe. Should handle both the linear part,
1806 * the fragments, and the frag list. It does NOT handle frag lists within
1807 * the frag list, if such a thing exists. We'd probably need to recurse to
1808 * handle that cleanly.
1810 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1811 struct pipe_inode_info *pipe, unsigned int tlen,
1814 struct partial_page partial[MAX_SKB_FRAGS];
1815 struct page *pages[MAX_SKB_FRAGS];
1816 struct splice_pipe_desc spd = {
1819 .nr_pages_max = MAX_SKB_FRAGS,
1821 .ops = &sock_pipe_buf_ops,
1822 .spd_release = sock_spd_release,
1824 struct sk_buff *frag_iter;
1825 struct sock *sk = skb->sk;
1829 * __skb_splice_bits() only fails if the output has no room left,
1830 * so no point in going over the frag_list for the error case.
1832 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1838 * now see if we have a frag_list to map
1840 skb_walk_frags(skb, frag_iter) {
1843 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1850 * Drop the socket lock, otherwise we have reverse
1851 * locking dependencies between sk_lock and i_mutex
1852 * here as compared to sendfile(). We enter here
1853 * with the socket lock held, and splice_to_pipe() will
1854 * grab the pipe inode lock. For sendfile() emulation,
1855 * we call into ->sendpage() with the i_mutex lock held
1856 * and networking will grab the socket lock.
1859 ret = splice_to_pipe(pipe, &spd);
1867 * skb_store_bits - store bits from kernel buffer to skb
1868 * @skb: destination buffer
1869 * @offset: offset in destination
1870 * @from: source buffer
1871 * @len: number of bytes to copy
1873 * Copy the specified number of bytes from the source buffer to the
1874 * destination skb. This function handles all the messy bits of
1875 * traversing fragment lists and such.
1878 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1880 int start = skb_headlen(skb);
1881 struct sk_buff *frag_iter;
1884 if (offset > (int)skb->len - len)
1887 if ((copy = start - offset) > 0) {
1890 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1891 if ((len -= copy) == 0)
1897 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1898 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1901 WARN_ON(start > offset + len);
1903 end = start + skb_frag_size(frag);
1904 if ((copy = end - offset) > 0) {
1910 vaddr = kmap_atomic(skb_frag_page(frag));
1911 memcpy(vaddr + frag->page_offset + offset - start,
1913 kunmap_atomic(vaddr);
1915 if ((len -= copy) == 0)
1923 skb_walk_frags(skb, frag_iter) {
1926 WARN_ON(start > offset + len);
1928 end = start + frag_iter->len;
1929 if ((copy = end - offset) > 0) {
1932 if (skb_store_bits(frag_iter, offset - start,
1935 if ((len -= copy) == 0)
1948 EXPORT_SYMBOL(skb_store_bits);
1950 /* Checksum skb data. */
1952 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1953 int len, __wsum csum)
1955 int start = skb_headlen(skb);
1956 int i, copy = start - offset;
1957 struct sk_buff *frag_iter;
1960 /* Checksum header. */
1964 csum = csum_partial(skb->data + offset, copy, csum);
1965 if ((len -= copy) == 0)
1971 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1973 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1975 WARN_ON(start > offset + len);
1977 end = start + skb_frag_size(frag);
1978 if ((copy = end - offset) > 0) {
1984 vaddr = kmap_atomic(skb_frag_page(frag));
1985 csum2 = csum_partial(vaddr + frag->page_offset +
1986 offset - start, copy, 0);
1987 kunmap_atomic(vaddr);
1988 csum = csum_block_add(csum, csum2, pos);
1997 skb_walk_frags(skb, frag_iter) {
2000 WARN_ON(start > offset + len);
2002 end = start + frag_iter->len;
2003 if ((copy = end - offset) > 0) {
2007 csum2 = skb_checksum(frag_iter, offset - start,
2009 csum = csum_block_add(csum, csum2, pos);
2010 if ((len -= copy) == 0)
2021 EXPORT_SYMBOL(skb_checksum);
2023 /* Both of above in one bottle. */
2025 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2026 u8 *to, int len, __wsum csum)
2028 int start = skb_headlen(skb);
2029 int i, copy = start - offset;
2030 struct sk_buff *frag_iter;
2037 csum = csum_partial_copy_nocheck(skb->data + offset, to,
2039 if ((len -= copy) == 0)
2046 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2049 WARN_ON(start > offset + len);
2051 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2052 if ((copy = end - offset) > 0) {
2055 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2059 vaddr = kmap_atomic(skb_frag_page(frag));
2060 csum2 = csum_partial_copy_nocheck(vaddr +
2064 kunmap_atomic(vaddr);
2065 csum = csum_block_add(csum, csum2, pos);
2075 skb_walk_frags(skb, frag_iter) {
2079 WARN_ON(start > offset + len);
2081 end = start + frag_iter->len;
2082 if ((copy = end - offset) > 0) {
2085 csum2 = skb_copy_and_csum_bits(frag_iter,
2088 csum = csum_block_add(csum, csum2, pos);
2089 if ((len -= copy) == 0)
2100 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2102 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2107 if (skb->ip_summed == CHECKSUM_PARTIAL)
2108 csstart = skb_checksum_start_offset(skb);
2110 csstart = skb_headlen(skb);
2112 BUG_ON(csstart > skb_headlen(skb));
2114 skb_copy_from_linear_data(skb, to, csstart);
2117 if (csstart != skb->len)
2118 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2119 skb->len - csstart, 0);
2121 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2122 long csstuff = csstart + skb->csum_offset;
2124 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2127 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2130 * skb_dequeue - remove from the head of the queue
2131 * @list: list to dequeue from
2133 * Remove the head of the list. The list lock is taken so the function
2134 * may be used safely with other locking list functions. The head item is
2135 * returned or %NULL if the list is empty.
2138 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2140 unsigned long flags;
2141 struct sk_buff *result;
2143 spin_lock_irqsave(&list->lock, flags);
2144 result = __skb_dequeue(list);
2145 spin_unlock_irqrestore(&list->lock, flags);
2148 EXPORT_SYMBOL(skb_dequeue);
2151 * skb_dequeue_tail - remove from the tail of the queue
2152 * @list: list to dequeue from
2154 * Remove the tail of the list. The list lock is taken so the function
2155 * may be used safely with other locking list functions. The tail item is
2156 * returned or %NULL if the list is empty.
2158 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2160 unsigned long flags;
2161 struct sk_buff *result;
2163 spin_lock_irqsave(&list->lock, flags);
2164 result = __skb_dequeue_tail(list);
2165 spin_unlock_irqrestore(&list->lock, flags);
2168 EXPORT_SYMBOL(skb_dequeue_tail);
2171 * skb_queue_purge - empty a list
2172 * @list: list to empty
2174 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2175 * the list and one reference dropped. This function takes the list
2176 * lock and is atomic with respect to other list locking functions.
2178 void skb_queue_purge(struct sk_buff_head *list)
2180 struct sk_buff *skb;
2181 while ((skb = skb_dequeue(list)) != NULL)
2184 EXPORT_SYMBOL(skb_queue_purge);
2187 * skb_queue_head - queue a buffer at the list head
2188 * @list: list to use
2189 * @newsk: buffer to queue
2191 * Queue a buffer at the start of the list. This function takes the
2192 * list lock and can be used safely with other locking &sk_buff functions
2195 * A buffer cannot be placed on two lists at the same time.
2197 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2199 unsigned long flags;
2201 spin_lock_irqsave(&list->lock, flags);
2202 __skb_queue_head(list, newsk);
2203 spin_unlock_irqrestore(&list->lock, flags);
2205 EXPORT_SYMBOL(skb_queue_head);
2208 * skb_queue_tail - queue a buffer at the list tail
2209 * @list: list to use
2210 * @newsk: buffer to queue
2212 * Queue a buffer at the tail of the list. This function takes the
2213 * list lock and can be used safely with other locking &sk_buff functions
2216 * A buffer cannot be placed on two lists at the same time.
2218 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2220 unsigned long flags;
2222 spin_lock_irqsave(&list->lock, flags);
2223 __skb_queue_tail(list, newsk);
2224 spin_unlock_irqrestore(&list->lock, flags);
2226 EXPORT_SYMBOL(skb_queue_tail);
2229 * skb_unlink - remove a buffer from a list
2230 * @skb: buffer to remove
2231 * @list: list to use
2233 * Remove a packet from a list. The list locks are taken and this
2234 * function is atomic with respect to other list locked calls
2236 * You must know what list the SKB is on.
2238 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2240 unsigned long flags;
2242 spin_lock_irqsave(&list->lock, flags);
2243 __skb_unlink(skb, list);
2244 spin_unlock_irqrestore(&list->lock, flags);
2246 EXPORT_SYMBOL(skb_unlink);
2249 * skb_append - append a buffer
2250 * @old: buffer to insert after
2251 * @newsk: buffer to insert
2252 * @list: list to use
2254 * Place a packet after a given packet in a list. The list locks are taken
2255 * and this function is atomic with respect to other list locked calls.
2256 * A buffer cannot be placed on two lists at the same time.
2258 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2260 unsigned long flags;
2262 spin_lock_irqsave(&list->lock, flags);
2263 __skb_queue_after(list, old, newsk);
2264 spin_unlock_irqrestore(&list->lock, flags);
2266 EXPORT_SYMBOL(skb_append);
2269 * skb_insert - insert a buffer
2270 * @old: buffer to insert before
2271 * @newsk: buffer to insert
2272 * @list: list to use
2274 * Place a packet before a given packet in a list. The list locks are
2275 * taken and this function is atomic with respect to other list locked
2278 * A buffer cannot be placed on two lists at the same time.
2280 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2282 unsigned long flags;
2284 spin_lock_irqsave(&list->lock, flags);
2285 __skb_insert(newsk, old->prev, old, list);
2286 spin_unlock_irqrestore(&list->lock, flags);
2288 EXPORT_SYMBOL(skb_insert);
2290 static inline void skb_split_inside_header(struct sk_buff *skb,
2291 struct sk_buff* skb1,
2292 const u32 len, const int pos)
2296 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2298 /* And move data appendix as is. */
2299 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2300 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2302 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2303 skb_shinfo(skb)->nr_frags = 0;
2304 skb1->data_len = skb->data_len;
2305 skb1->len += skb1->data_len;
2308 skb_set_tail_pointer(skb, len);
2311 static inline void skb_split_no_header(struct sk_buff *skb,
2312 struct sk_buff* skb1,
2313 const u32 len, int pos)
2316 const int nfrags = skb_shinfo(skb)->nr_frags;
2318 skb_shinfo(skb)->nr_frags = 0;
2319 skb1->len = skb1->data_len = skb->len - len;
2321 skb->data_len = len - pos;
2323 for (i = 0; i < nfrags; i++) {
2324 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2326 if (pos + size > len) {
2327 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2331 * We have two variants in this case:
2332 * 1. Move all the frag to the second
2333 * part, if it is possible. F.e.
2334 * this approach is mandatory for TUX,
2335 * where splitting is expensive.
2336 * 2. Split is accurately. We make this.
2338 skb_frag_ref(skb, i);
2339 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2340 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2341 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2342 skb_shinfo(skb)->nr_frags++;
2346 skb_shinfo(skb)->nr_frags++;
2349 skb_shinfo(skb1)->nr_frags = k;
2353 * skb_split - Split fragmented skb to two parts at length len.
2354 * @skb: the buffer to split
2355 * @skb1: the buffer to receive the second part
2356 * @len: new length for skb
2358 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2360 int pos = skb_headlen(skb);
2362 if (len < pos) /* Split line is inside header. */
2363 skb_split_inside_header(skb, skb1, len, pos);
2364 else /* Second chunk has no header, nothing to copy. */
2365 skb_split_no_header(skb, skb1, len, pos);
2367 EXPORT_SYMBOL(skb_split);
2369 /* Shifting from/to a cloned skb is a no-go.
2371 * Caller cannot keep skb_shinfo related pointers past calling here!
2373 static int skb_prepare_for_shift(struct sk_buff *skb)
2375 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2379 * skb_shift - Shifts paged data partially from skb to another
2380 * @tgt: buffer into which tail data gets added
2381 * @skb: buffer from which the paged data comes from
2382 * @shiftlen: shift up to this many bytes
2384 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2385 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2386 * It's up to caller to free skb if everything was shifted.
2388 * If @tgt runs out of frags, the whole operation is aborted.
2390 * Skb cannot include anything else but paged data while tgt is allowed
2391 * to have non-paged data as well.
2393 * TODO: full sized shift could be optimized but that would need
2394 * specialized skb free'er to handle frags without up-to-date nr_frags.
2396 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2398 int from, to, merge, todo;
2399 struct skb_frag_struct *fragfrom, *fragto;
2401 BUG_ON(shiftlen > skb->len);
2402 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2406 to = skb_shinfo(tgt)->nr_frags;
2407 fragfrom = &skb_shinfo(skb)->frags[from];
2409 /* Actual merge is delayed until the point when we know we can
2410 * commit all, so that we don't have to undo partial changes
2413 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2414 fragfrom->page_offset)) {
2419 todo -= skb_frag_size(fragfrom);
2421 if (skb_prepare_for_shift(skb) ||
2422 skb_prepare_for_shift(tgt))
2425 /* All previous frag pointers might be stale! */
2426 fragfrom = &skb_shinfo(skb)->frags[from];
2427 fragto = &skb_shinfo(tgt)->frags[merge];
2429 skb_frag_size_add(fragto, shiftlen);
2430 skb_frag_size_sub(fragfrom, shiftlen);
2431 fragfrom->page_offset += shiftlen;
2439 /* Skip full, not-fitting skb to avoid expensive operations */
2440 if ((shiftlen == skb->len) &&
2441 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2444 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2447 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2448 if (to == MAX_SKB_FRAGS)
2451 fragfrom = &skb_shinfo(skb)->frags[from];
2452 fragto = &skb_shinfo(tgt)->frags[to];
2454 if (todo >= skb_frag_size(fragfrom)) {
2455 *fragto = *fragfrom;
2456 todo -= skb_frag_size(fragfrom);
2461 __skb_frag_ref(fragfrom);
2462 fragto->page = fragfrom->page;
2463 fragto->page_offset = fragfrom->page_offset;
2464 skb_frag_size_set(fragto, todo);
2466 fragfrom->page_offset += todo;
2467 skb_frag_size_sub(fragfrom, todo);
2475 /* Ready to "commit" this state change to tgt */
2476 skb_shinfo(tgt)->nr_frags = to;
2479 fragfrom = &skb_shinfo(skb)->frags[0];
2480 fragto = &skb_shinfo(tgt)->frags[merge];
2482 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2483 __skb_frag_unref(fragfrom);
2486 /* Reposition in the original skb */
2488 while (from < skb_shinfo(skb)->nr_frags)
2489 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2490 skb_shinfo(skb)->nr_frags = to;
2492 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2495 /* Most likely the tgt won't ever need its checksum anymore, skb on
2496 * the other hand might need it if it needs to be resent
2498 tgt->ip_summed = CHECKSUM_PARTIAL;
2499 skb->ip_summed = CHECKSUM_PARTIAL;
2501 /* Yak, is it really working this way? Some helper please? */
2502 skb->len -= shiftlen;
2503 skb->data_len -= shiftlen;
2504 skb->truesize -= shiftlen;
2505 tgt->len += shiftlen;
2506 tgt->data_len += shiftlen;
2507 tgt->truesize += shiftlen;
2513 * skb_prepare_seq_read - Prepare a sequential read of skb data
2514 * @skb: the buffer to read
2515 * @from: lower offset of data to be read
2516 * @to: upper offset of data to be read
2517 * @st: state variable
2519 * Initializes the specified state variable. Must be called before
2520 * invoking skb_seq_read() for the first time.
2522 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2523 unsigned int to, struct skb_seq_state *st)
2525 st->lower_offset = from;
2526 st->upper_offset = to;
2527 st->root_skb = st->cur_skb = skb;
2528 st->frag_idx = st->stepped_offset = 0;
2529 st->frag_data = NULL;
2531 EXPORT_SYMBOL(skb_prepare_seq_read);
2534 * skb_seq_read - Sequentially read skb data
2535 * @consumed: number of bytes consumed by the caller so far
2536 * @data: destination pointer for data to be returned
2537 * @st: state variable
2539 * Reads a block of skb data at &consumed relative to the
2540 * lower offset specified to skb_prepare_seq_read(). Assigns
2541 * the head of the data block to &data and returns the length
2542 * of the block or 0 if the end of the skb data or the upper
2543 * offset has been reached.
2545 * The caller is not required to consume all of the data
2546 * returned, i.e. &consumed is typically set to the number
2547 * of bytes already consumed and the next call to
2548 * skb_seq_read() will return the remaining part of the block.
2550 * Note 1: The size of each block of data returned can be arbitrary,
2551 * this limitation is the cost for zerocopy seqeuental
2552 * reads of potentially non linear data.
2554 * Note 2: Fragment lists within fragments are not implemented
2555 * at the moment, state->root_skb could be replaced with
2556 * a stack for this purpose.
2558 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2559 struct skb_seq_state *st)
2561 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2564 if (unlikely(abs_offset >= st->upper_offset))
2568 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2570 if (abs_offset < block_limit && !st->frag_data) {
2571 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2572 return block_limit - abs_offset;
2575 if (st->frag_idx == 0 && !st->frag_data)
2576 st->stepped_offset += skb_headlen(st->cur_skb);
2578 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2579 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2580 block_limit = skb_frag_size(frag) + st->stepped_offset;
2582 if (abs_offset < block_limit) {
2584 st->frag_data = kmap_atomic(skb_frag_page(frag));
2586 *data = (u8 *) st->frag_data + frag->page_offset +
2587 (abs_offset - st->stepped_offset);
2589 return block_limit - abs_offset;
2592 if (st->frag_data) {
2593 kunmap_atomic(st->frag_data);
2594 st->frag_data = NULL;
2598 st->stepped_offset += skb_frag_size(frag);
2601 if (st->frag_data) {
2602 kunmap_atomic(st->frag_data);
2603 st->frag_data = NULL;
2606 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2607 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2610 } else if (st->cur_skb->next) {
2611 st->cur_skb = st->cur_skb->next;
2618 EXPORT_SYMBOL(skb_seq_read);
2621 * skb_abort_seq_read - Abort a sequential read of skb data
2622 * @st: state variable
2624 * Must be called if skb_seq_read() was not called until it
2627 void skb_abort_seq_read(struct skb_seq_state *st)
2630 kunmap_atomic(st->frag_data);
2632 EXPORT_SYMBOL(skb_abort_seq_read);
2634 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2636 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2637 struct ts_config *conf,
2638 struct ts_state *state)
2640 return skb_seq_read(offset, text, TS_SKB_CB(state));
2643 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2645 skb_abort_seq_read(TS_SKB_CB(state));
2649 * skb_find_text - Find a text pattern in skb data
2650 * @skb: the buffer to look in
2651 * @from: search offset
2653 * @config: textsearch configuration
2654 * @state: uninitialized textsearch state variable
2656 * Finds a pattern in the skb data according to the specified
2657 * textsearch configuration. Use textsearch_next() to retrieve
2658 * subsequent occurrences of the pattern. Returns the offset
2659 * to the first occurrence or UINT_MAX if no match was found.
2661 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2662 unsigned int to, struct ts_config *config,
2663 struct ts_state *state)
2667 config->get_next_block = skb_ts_get_next_block;
2668 config->finish = skb_ts_finish;
2670 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2672 ret = textsearch_find(config, state);
2673 return (ret <= to - from ? ret : UINT_MAX);
2675 EXPORT_SYMBOL(skb_find_text);
2678 * skb_append_datato_frags - append the user data to a skb
2679 * @sk: sock structure
2680 * @skb: skb structure to be appened with user data.
2681 * @getfrag: call back function to be used for getting the user data
2682 * @from: pointer to user message iov
2683 * @length: length of the iov message
2685 * Description: This procedure append the user data in the fragment part
2686 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2688 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2689 int (*getfrag)(void *from, char *to, int offset,
2690 int len, int odd, struct sk_buff *skb),
2691 void *from, int length)
2694 skb_frag_t *frag = NULL;
2695 struct page *page = NULL;
2701 /* Return error if we don't have space for new frag */
2702 frg_cnt = skb_shinfo(skb)->nr_frags;
2703 if (frg_cnt >= MAX_SKB_FRAGS)
2706 /* allocate a new page for next frag */
2707 page = alloc_pages(sk->sk_allocation, 0);
2709 /* If alloc_page fails just return failure and caller will
2710 * free previous allocated pages by doing kfree_skb()
2715 /* initialize the next frag */
2716 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2717 skb->truesize += PAGE_SIZE;
2718 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2720 /* get the new initialized frag */
2721 frg_cnt = skb_shinfo(skb)->nr_frags;
2722 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2724 /* copy the user data to page */
2725 left = PAGE_SIZE - frag->page_offset;
2726 copy = (length > left)? left : length;
2728 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2729 offset, copy, 0, skb);
2733 /* copy was successful so update the size parameters */
2734 skb_frag_size_add(frag, copy);
2736 skb->data_len += copy;
2740 } while (length > 0);
2744 EXPORT_SYMBOL(skb_append_datato_frags);
2747 * skb_pull_rcsum - pull skb and update receive checksum
2748 * @skb: buffer to update
2749 * @len: length of data pulled
2751 * This function performs an skb_pull on the packet and updates
2752 * the CHECKSUM_COMPLETE checksum. It should be used on
2753 * receive path processing instead of skb_pull unless you know
2754 * that the checksum difference is zero (e.g., a valid IP header)
2755 * or you are setting ip_summed to CHECKSUM_NONE.
2757 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2759 BUG_ON(len > skb->len);
2761 BUG_ON(skb->len < skb->data_len);
2762 skb_postpull_rcsum(skb, skb->data, len);
2763 return skb->data += len;
2765 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2768 * skb_segment - Perform protocol segmentation on skb.
2769 * @skb: buffer to segment
2770 * @features: features for the output path (see dev->features)
2772 * This function performs segmentation on the given skb. It returns
2773 * a pointer to the first in a list of new skbs for the segments.
2774 * In case of error it returns ERR_PTR(err).
2776 struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2778 struct sk_buff *segs = NULL;
2779 struct sk_buff *tail = NULL;
2780 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2781 unsigned int mss = skb_shinfo(skb)->gso_size;
2782 unsigned int doffset = skb->data - skb_mac_header(skb);
2783 unsigned int offset = doffset;
2784 unsigned int headroom;
2786 int sg = !!(features & NETIF_F_SG);
2787 int nfrags = skb_shinfo(skb)->nr_frags;
2792 __skb_push(skb, doffset);
2793 headroom = skb_headroom(skb);
2794 pos = skb_headlen(skb);
2797 struct sk_buff *nskb;
2802 len = skb->len - offset;
2806 hsize = skb_headlen(skb) - offset;
2809 if (hsize > len || !sg)
2812 if (!hsize && i >= nfrags) {
2813 BUG_ON(fskb->len != len);
2816 nskb = skb_clone(fskb, GFP_ATOMIC);
2819 if (unlikely(!nskb))
2822 hsize = skb_end_offset(nskb);
2823 if (skb_cow_head(nskb, doffset + headroom)) {
2828 nskb->truesize += skb_end_offset(nskb) - hsize;
2829 skb_release_head_state(nskb);
2830 __skb_push(nskb, doffset);
2832 nskb = __alloc_skb(hsize + doffset + headroom,
2833 GFP_ATOMIC, skb_alloc_rx_flag(skb),
2836 if (unlikely(!nskb))
2839 skb_reserve(nskb, headroom);
2840 __skb_put(nskb, doffset);
2849 __copy_skb_header(nskb, skb);
2850 nskb->mac_len = skb->mac_len;
2852 /* nskb and skb might have different headroom */
2853 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2854 nskb->csum_start += skb_headroom(nskb) - headroom;
2856 skb_reset_mac_header(nskb);
2857 skb_set_network_header(nskb, skb->mac_len);
2858 nskb->transport_header = (nskb->network_header +
2859 skb_network_header_len(skb));
2860 skb_copy_from_linear_data(skb, nskb->data, doffset);
2862 if (fskb != skb_shinfo(skb)->frag_list)
2866 nskb->ip_summed = CHECKSUM_NONE;
2867 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2873 frag = skb_shinfo(nskb)->frags;
2875 skb_copy_from_linear_data_offset(skb, offset,
2876 skb_put(nskb, hsize), hsize);
2878 while (pos < offset + len && i < nfrags) {
2879 *frag = skb_shinfo(skb)->frags[i];
2880 __skb_frag_ref(frag);
2881 size = skb_frag_size(frag);
2884 frag->page_offset += offset - pos;
2885 skb_frag_size_sub(frag, offset - pos);
2888 skb_shinfo(nskb)->nr_frags++;
2890 if (pos + size <= offset + len) {
2894 skb_frag_size_sub(frag, pos + size - (offset + len));
2901 if (pos < offset + len) {
2902 struct sk_buff *fskb2 = fskb;
2904 BUG_ON(pos + fskb->len != offset + len);
2910 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2916 SKB_FRAG_ASSERT(nskb);
2917 skb_shinfo(nskb)->frag_list = fskb2;
2921 nskb->data_len = len - hsize;
2922 nskb->len += nskb->data_len;
2923 nskb->truesize += nskb->data_len;
2924 } while ((offset += len) < skb->len);
2929 while ((skb = segs)) {
2933 return ERR_PTR(err);
2935 EXPORT_SYMBOL_GPL(skb_segment);
2937 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2939 struct sk_buff *p = *head;
2940 struct sk_buff *nskb;
2941 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2942 struct skb_shared_info *pinfo = skb_shinfo(p);
2943 unsigned int headroom;
2944 unsigned int len = skb_gro_len(skb);
2945 unsigned int offset = skb_gro_offset(skb);
2946 unsigned int headlen = skb_headlen(skb);
2947 unsigned int delta_truesize;
2949 if (p->len + len >= 65536)
2952 if (pinfo->frag_list)
2954 else if (headlen <= offset) {
2957 int i = skbinfo->nr_frags;
2958 int nr_frags = pinfo->nr_frags + i;
2962 if (nr_frags > MAX_SKB_FRAGS)
2965 pinfo->nr_frags = nr_frags;
2966 skbinfo->nr_frags = 0;
2968 frag = pinfo->frags + nr_frags;
2969 frag2 = skbinfo->frags + i;
2974 frag->page_offset += offset;
2975 skb_frag_size_sub(frag, offset);
2977 /* all fragments truesize : remove (head size + sk_buff) */
2978 delta_truesize = skb->truesize -
2979 SKB_TRUESIZE(skb_end_offset(skb));
2981 skb->truesize -= skb->data_len;
2982 skb->len -= skb->data_len;
2985 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
2987 } else if (skb->head_frag) {
2988 int nr_frags = pinfo->nr_frags;
2989 skb_frag_t *frag = pinfo->frags + nr_frags;
2990 struct page *page = virt_to_head_page(skb->head);
2991 unsigned int first_size = headlen - offset;
2992 unsigned int first_offset;
2994 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2997 first_offset = skb->data -
2998 (unsigned char *)page_address(page) +
3001 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3003 frag->page.p = page;
3004 frag->page_offset = first_offset;
3005 skb_frag_size_set(frag, first_size);
3007 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3008 /* We dont need to clear skbinfo->nr_frags here */
3010 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3011 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3013 } else if (skb_gro_len(p) != pinfo->gso_size)
3016 headroom = skb_headroom(p);
3017 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3018 if (unlikely(!nskb))
3021 __copy_skb_header(nskb, p);
3022 nskb->mac_len = p->mac_len;
3024 skb_reserve(nskb, headroom);
3025 __skb_put(nskb, skb_gro_offset(p));
3027 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3028 skb_set_network_header(nskb, skb_network_offset(p));
3029 skb_set_transport_header(nskb, skb_transport_offset(p));
3031 __skb_pull(p, skb_gro_offset(p));
3032 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3033 p->data - skb_mac_header(p));
3035 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
3036 skb_shinfo(nskb)->frag_list = p;
3037 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3038 pinfo->gso_size = 0;
3039 skb_header_release(p);
3042 nskb->data_len += p->len;
3043 nskb->truesize += p->truesize;
3044 nskb->len += p->len;
3047 nskb->next = p->next;
3053 delta_truesize = skb->truesize;
3054 if (offset > headlen) {
3055 unsigned int eat = offset - headlen;
3057 skbinfo->frags[0].page_offset += eat;
3058 skb_frag_size_sub(&skbinfo->frags[0], eat);
3059 skb->data_len -= eat;
3064 __skb_pull(skb, offset);
3066 p->prev->next = skb;
3068 skb_header_release(skb);
3071 NAPI_GRO_CB(p)->count++;
3073 p->truesize += delta_truesize;
3076 NAPI_GRO_CB(skb)->same_flow = 1;
3079 EXPORT_SYMBOL_GPL(skb_gro_receive);
3081 void __init skb_init(void)
3083 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3084 sizeof(struct sk_buff),
3086 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3088 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3089 (2*sizeof(struct sk_buff)) +
3092 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3097 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3098 * @skb: Socket buffer containing the buffers to be mapped
3099 * @sg: The scatter-gather list to map into
3100 * @offset: The offset into the buffer's contents to start mapping
3101 * @len: Length of buffer space to be mapped
3103 * Fill the specified scatter-gather list with mappings/pointers into a
3104 * region of the buffer space attached to a socket buffer.
3107 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3109 int start = skb_headlen(skb);
3110 int i, copy = start - offset;
3111 struct sk_buff *frag_iter;
3117 sg_set_buf(sg, skb->data + offset, copy);
3119 if ((len -= copy) == 0)
3124 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3127 WARN_ON(start > offset + len);
3129 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3130 if ((copy = end - offset) > 0) {
3131 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3135 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3136 frag->page_offset+offset-start);
3145 skb_walk_frags(skb, frag_iter) {
3148 WARN_ON(start > offset + len);
3150 end = start + frag_iter->len;
3151 if ((copy = end - offset) > 0) {
3154 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3156 if ((len -= copy) == 0)
3166 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3168 int nsg = __skb_to_sgvec(skb, sg, offset, len);
3170 sg_mark_end(&sg[nsg - 1]);
3174 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3177 * skb_cow_data - Check that a socket buffer's data buffers are writable
3178 * @skb: The socket buffer to check.
3179 * @tailbits: Amount of trailing space to be added
3180 * @trailer: Returned pointer to the skb where the @tailbits space begins
3182 * Make sure that the data buffers attached to a socket buffer are
3183 * writable. If they are not, private copies are made of the data buffers
3184 * and the socket buffer is set to use these instead.
3186 * If @tailbits is given, make sure that there is space to write @tailbits
3187 * bytes of data beyond current end of socket buffer. @trailer will be
3188 * set to point to the skb in which this space begins.
3190 * The number of scatterlist elements required to completely map the
3191 * COW'd and extended socket buffer will be returned.
3193 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3197 struct sk_buff *skb1, **skb_p;
3199 /* If skb is cloned or its head is paged, reallocate
3200 * head pulling out all the pages (pages are considered not writable
3201 * at the moment even if they are anonymous).
3203 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3204 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3207 /* Easy case. Most of packets will go this way. */
3208 if (!skb_has_frag_list(skb)) {
3209 /* A little of trouble, not enough of space for trailer.
3210 * This should not happen, when stack is tuned to generate
3211 * good frames. OK, on miss we reallocate and reserve even more
3212 * space, 128 bytes is fair. */
3214 if (skb_tailroom(skb) < tailbits &&
3215 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3223 /* Misery. We are in troubles, going to mincer fragments... */
3226 skb_p = &skb_shinfo(skb)->frag_list;
3229 while ((skb1 = *skb_p) != NULL) {
3232 /* The fragment is partially pulled by someone,
3233 * this can happen on input. Copy it and everything
3236 if (skb_shared(skb1))
3239 /* If the skb is the last, worry about trailer. */
3241 if (skb1->next == NULL && tailbits) {
3242 if (skb_shinfo(skb1)->nr_frags ||
3243 skb_has_frag_list(skb1) ||
3244 skb_tailroom(skb1) < tailbits)
3245 ntail = tailbits + 128;
3251 skb_shinfo(skb1)->nr_frags ||
3252 skb_has_frag_list(skb1)) {
3253 struct sk_buff *skb2;
3255 /* Fuck, we are miserable poor guys... */
3257 skb2 = skb_copy(skb1, GFP_ATOMIC);
3259 skb2 = skb_copy_expand(skb1,
3263 if (unlikely(skb2 == NULL))
3267 skb_set_owner_w(skb2, skb1->sk);
3269 /* Looking around. Are we still alive?
3270 * OK, link new skb, drop old one */
3272 skb2->next = skb1->next;
3279 skb_p = &skb1->next;
3284 EXPORT_SYMBOL_GPL(skb_cow_data);
3286 static void sock_rmem_free(struct sk_buff *skb)
3288 struct sock *sk = skb->sk;
3290 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3294 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3296 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3300 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3301 (unsigned int)sk->sk_rcvbuf)
3306 skb->destructor = sock_rmem_free;
3307 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3309 /* before exiting rcu section, make sure dst is refcounted */
3312 skb_queue_tail(&sk->sk_error_queue, skb);
3313 if (!sock_flag(sk, SOCK_DEAD))
3314 sk->sk_data_ready(sk, len);
3317 EXPORT_SYMBOL(sock_queue_err_skb);
3319 void skb_tstamp_tx(struct sk_buff *orig_skb,
3320 struct skb_shared_hwtstamps *hwtstamps)
3322 struct sock *sk = orig_skb->sk;
3323 struct sock_exterr_skb *serr;
3324 struct sk_buff *skb;
3330 skb = skb_clone(orig_skb, GFP_ATOMIC);
3335 *skb_hwtstamps(skb) =
3339 * no hardware time stamps available,
3340 * so keep the shared tx_flags and only
3341 * store software time stamp
3343 skb->tstamp = ktime_get_real();
3346 serr = SKB_EXT_ERR(skb);
3347 memset(serr, 0, sizeof(*serr));
3348 serr->ee.ee_errno = ENOMSG;
3349 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3351 err = sock_queue_err_skb(sk, skb);
3356 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3358 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3360 struct sock *sk = skb->sk;
3361 struct sock_exterr_skb *serr;
3364 skb->wifi_acked_valid = 1;
3365 skb->wifi_acked = acked;
3367 serr = SKB_EXT_ERR(skb);
3368 memset(serr, 0, sizeof(*serr));
3369 serr->ee.ee_errno = ENOMSG;
3370 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3372 err = sock_queue_err_skb(sk, skb);
3376 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3380 * skb_partial_csum_set - set up and verify partial csum values for packet
3381 * @skb: the skb to set
3382 * @start: the number of bytes after skb->data to start checksumming.
3383 * @off: the offset from start to place the checksum.
3385 * For untrusted partially-checksummed packets, we need to make sure the values
3386 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3388 * This function checks and sets those values and skb->ip_summed: if this
3389 * returns false you should drop the packet.
3391 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3393 if (unlikely(start > skb_headlen(skb)) ||
3394 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3395 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3396 start, off, skb_headlen(skb));
3399 skb->ip_summed = CHECKSUM_PARTIAL;
3400 skb->csum_start = skb_headroom(skb) + start;
3401 skb->csum_offset = off;
3404 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3406 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3408 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3411 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3413 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3416 kmem_cache_free(skbuff_head_cache, skb);
3420 EXPORT_SYMBOL(kfree_skb_partial);
3423 * skb_try_coalesce - try to merge skb to prior one
3425 * @from: buffer to add
3426 * @fragstolen: pointer to boolean
3427 * @delta_truesize: how much more was allocated than was requested
3429 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3430 bool *fragstolen, int *delta_truesize)
3432 int i, delta, len = from->len;
3434 *fragstolen = false;
3439 if (len <= skb_tailroom(to)) {
3440 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3441 *delta_truesize = 0;
3445 if (skb_has_frag_list(to) || skb_has_frag_list(from))
3448 if (skb_headlen(from) != 0) {
3450 unsigned int offset;
3452 if (skb_shinfo(to)->nr_frags +
3453 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3456 if (skb_head_is_locked(from))
3459 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3461 page = virt_to_head_page(from->head);
3462 offset = from->data - (unsigned char *)page_address(page);
3464 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3465 page, offset, skb_headlen(from));
3468 if (skb_shinfo(to)->nr_frags +
3469 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3472 delta = from->truesize -
3473 SKB_TRUESIZE(skb_end_pointer(from) - from->head);
3476 WARN_ON_ONCE(delta < len);
3478 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3479 skb_shinfo(from)->frags,
3480 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3481 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3483 if (!skb_cloned(from))
3484 skb_shinfo(from)->nr_frags = 0;
3486 /* if the skb is cloned this does nothing since we set nr_frags to 0 */
3487 for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3488 skb_frag_ref(from, i);
3490 to->truesize += delta;
3492 to->data_len += len;
3494 *delta_truesize = delta;
3497 EXPORT_SYMBOL(skb_try_coalesce);