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 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/kmemcheck.h>
44 #include <linux/interrupt.h>
46 #include <linux/inet.h>
47 #include <linux/slab.h>
48 #include <linux/tcp.h>
49 #include <linux/udp.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 <asm/system.h>
72 #include <trace/events/skb.h>
76 static struct kmem_cache *skbuff_head_cache __read_mostly;
77 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
80 * Keep out-of-line to prevent kernel bloat.
81 * __builtin_return_address is not used because it is not always
86 * skb_over_panic - private function
91 * Out of line support code for skb_put(). Not user callable.
93 static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
95 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
96 "data:%p tail:%#lx end:%#lx dev:%s\n",
97 here, skb->len, sz, skb->head, skb->data,
98 (unsigned long)skb->tail, (unsigned long)skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
104 * skb_under_panic - private function
109 * Out of line support code for skb_push(). Not user callable.
112 static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
114 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
115 "data:%p tail:%#lx end:%#lx dev:%s\n",
116 here, skb->len, sz, skb->head, skb->data,
117 (unsigned long)skb->tail, (unsigned long)skb->end,
118 skb->dev ? skb->dev->name : "<NULL>");
122 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
129 * __alloc_skb - allocate a network buffer
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
134 * @node: numa node to allocate memory on
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
144 int fclone, int node)
146 struct kmem_cache *cache;
147 struct skb_shared_info *shinfo;
151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
154 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
159 /* We do our best to align skb_shared_info on a separate cache
160 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
161 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
162 * Both skb->head and skb_shared_info are cache line aligned.
164 size = SKB_DATA_ALIGN(size);
165 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
166 data = kmalloc_node_track_caller(size, gfp_mask, node);
169 /* kmalloc(size) might give us more room than requested.
170 * Put skb_shared_info exactly at the end of allocated zone,
171 * to allow max possible filling before reallocation.
173 size = SKB_WITH_OVERHEAD(ksize(data));
174 prefetchw(data + size);
177 * Only clear those fields we need to clear, not those that we will
178 * actually initialise below. Hence, don't put any more fields after
179 * the tail pointer in struct sk_buff!
181 memset(skb, 0, offsetof(struct sk_buff, tail));
182 /* Account for allocated memory : skb + skb->head */
183 skb->truesize = SKB_TRUESIZE(size);
184 atomic_set(&skb->users, 1);
187 skb_reset_tail_pointer(skb);
188 skb->end = skb->tail + size;
189 #ifdef NET_SKBUFF_DATA_USES_OFFSET
190 skb->mac_header = ~0U;
193 /* make sure we initialize shinfo sequentially */
194 shinfo = skb_shinfo(skb);
195 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
196 atomic_set(&shinfo->dataref, 1);
197 kmemcheck_annotate_variable(shinfo->destructor_arg);
200 struct sk_buff *child = skb + 1;
201 atomic_t *fclone_ref = (atomic_t *) (child + 1);
203 kmemcheck_annotate_bitfield(child, flags1);
204 kmemcheck_annotate_bitfield(child, flags2);
205 skb->fclone = SKB_FCLONE_ORIG;
206 atomic_set(fclone_ref, 1);
208 child->fclone = SKB_FCLONE_UNAVAILABLE;
213 kmem_cache_free(cache, skb);
217 EXPORT_SYMBOL(__alloc_skb);
220 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
221 * @dev: network device to receive on
222 * @length: length to allocate
223 * @gfp_mask: get_free_pages mask, passed to alloc_skb
225 * Allocate a new &sk_buff and assign it a usage count of one. The
226 * buffer has unspecified headroom built in. Users should allocate
227 * the headroom they think they need without accounting for the
228 * built in space. The built in space is used for optimisations.
230 * %NULL is returned if there is no free memory.
232 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
233 unsigned int length, gfp_t gfp_mask)
237 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
239 skb_reserve(skb, NET_SKB_PAD);
244 EXPORT_SYMBOL(__netdev_alloc_skb);
246 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
249 skb_fill_page_desc(skb, i, page, off, size);
251 skb->data_len += size;
252 skb->truesize += size;
254 EXPORT_SYMBOL(skb_add_rx_frag);
257 * dev_alloc_skb - allocate an skbuff for receiving
258 * @length: length to allocate
260 * Allocate a new &sk_buff and assign it a usage count of one. The
261 * buffer has unspecified headroom built in. Users should allocate
262 * the headroom they think they need without accounting for the
263 * built in space. The built in space is used for optimisations.
265 * %NULL is returned if there is no free memory. Although this function
266 * allocates memory it can be called from an interrupt.
268 struct sk_buff *dev_alloc_skb(unsigned int length)
271 * There is more code here than it seems:
272 * __dev_alloc_skb is an inline
274 return __dev_alloc_skb(length, GFP_ATOMIC);
276 EXPORT_SYMBOL(dev_alloc_skb);
278 static void skb_drop_list(struct sk_buff **listp)
280 struct sk_buff *list = *listp;
285 struct sk_buff *this = list;
291 static inline void skb_drop_fraglist(struct sk_buff *skb)
293 skb_drop_list(&skb_shinfo(skb)->frag_list);
296 static void skb_clone_fraglist(struct sk_buff *skb)
298 struct sk_buff *list;
300 skb_walk_frags(skb, list)
304 static void skb_release_data(struct sk_buff *skb)
307 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
308 &skb_shinfo(skb)->dataref)) {
309 if (skb_shinfo(skb)->nr_frags) {
311 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
312 skb_frag_unref(skb, i);
316 * If skb buf is from userspace, we need to notify the caller
317 * the lower device DMA has done;
319 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
320 struct ubuf_info *uarg;
322 uarg = skb_shinfo(skb)->destructor_arg;
324 uarg->callback(uarg);
327 if (skb_has_frag_list(skb))
328 skb_drop_fraglist(skb);
335 * Free an skbuff by memory without cleaning the state.
337 static void kfree_skbmem(struct sk_buff *skb)
339 struct sk_buff *other;
340 atomic_t *fclone_ref;
342 switch (skb->fclone) {
343 case SKB_FCLONE_UNAVAILABLE:
344 kmem_cache_free(skbuff_head_cache, skb);
347 case SKB_FCLONE_ORIG:
348 fclone_ref = (atomic_t *) (skb + 2);
349 if (atomic_dec_and_test(fclone_ref))
350 kmem_cache_free(skbuff_fclone_cache, skb);
353 case SKB_FCLONE_CLONE:
354 fclone_ref = (atomic_t *) (skb + 1);
357 /* The clone portion is available for
358 * fast-cloning again.
360 skb->fclone = SKB_FCLONE_UNAVAILABLE;
362 if (atomic_dec_and_test(fclone_ref))
363 kmem_cache_free(skbuff_fclone_cache, other);
368 static void skb_release_head_state(struct sk_buff *skb)
372 secpath_put(skb->sp);
374 if (skb->destructor) {
376 skb->destructor(skb);
378 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
379 nf_conntrack_put(skb->nfct);
381 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
382 nf_conntrack_put_reasm(skb->nfct_reasm);
384 #ifdef CONFIG_BRIDGE_NETFILTER
385 nf_bridge_put(skb->nf_bridge);
387 /* XXX: IS this still necessary? - JHS */
388 #ifdef CONFIG_NET_SCHED
390 #ifdef CONFIG_NET_CLS_ACT
396 /* Free everything but the sk_buff shell. */
397 static void skb_release_all(struct sk_buff *skb)
399 skb_release_head_state(skb);
400 skb_release_data(skb);
404 * __kfree_skb - private function
407 * Free an sk_buff. Release anything attached to the buffer.
408 * Clean the state. This is an internal helper function. Users should
409 * always call kfree_skb
412 void __kfree_skb(struct sk_buff *skb)
414 skb_release_all(skb);
417 EXPORT_SYMBOL(__kfree_skb);
420 * kfree_skb - free an sk_buff
421 * @skb: buffer to free
423 * Drop a reference to the buffer and free it if the usage count has
426 void kfree_skb(struct sk_buff *skb)
430 if (likely(atomic_read(&skb->users) == 1))
432 else if (likely(!atomic_dec_and_test(&skb->users)))
434 trace_kfree_skb(skb, __builtin_return_address(0));
437 EXPORT_SYMBOL(kfree_skb);
440 * consume_skb - free an skbuff
441 * @skb: buffer to free
443 * Drop a ref to the buffer and free it if the usage count has hit zero
444 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
445 * is being dropped after a failure and notes that
447 void consume_skb(struct sk_buff *skb)
451 if (likely(atomic_read(&skb->users) == 1))
453 else if (likely(!atomic_dec_and_test(&skb->users)))
455 trace_consume_skb(skb);
458 EXPORT_SYMBOL(consume_skb);
461 * skb_recycle - clean up an skb for reuse
464 * Recycles the skb to be reused as a receive buffer. This
465 * function does any necessary reference count dropping, and
466 * cleans up the skbuff as if it just came from __alloc_skb().
468 void skb_recycle(struct sk_buff *skb)
470 struct skb_shared_info *shinfo;
472 skb_release_head_state(skb);
474 shinfo = skb_shinfo(skb);
475 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
476 atomic_set(&shinfo->dataref, 1);
478 memset(skb, 0, offsetof(struct sk_buff, tail));
479 skb->data = skb->head + NET_SKB_PAD;
480 skb_reset_tail_pointer(skb);
482 EXPORT_SYMBOL(skb_recycle);
485 * skb_recycle_check - check if skb can be reused for receive
487 * @skb_size: minimum receive buffer size
489 * Checks that the skb passed in is not shared or cloned, and
490 * that it is linear and its head portion at least as large as
491 * skb_size so that it can be recycled as a receive buffer.
492 * If these conditions are met, this function does any necessary
493 * reference count dropping and cleans up the skbuff as if it
494 * just came from __alloc_skb().
496 bool skb_recycle_check(struct sk_buff *skb, int skb_size)
498 if (!skb_is_recycleable(skb, skb_size))
505 EXPORT_SYMBOL(skb_recycle_check);
507 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
509 new->tstamp = old->tstamp;
511 new->transport_header = old->transport_header;
512 new->network_header = old->network_header;
513 new->mac_header = old->mac_header;
514 skb_dst_copy(new, old);
515 new->rxhash = old->rxhash;
516 new->ooo_okay = old->ooo_okay;
517 new->l4_rxhash = old->l4_rxhash;
519 new->sp = secpath_get(old->sp);
521 memcpy(new->cb, old->cb, sizeof(old->cb));
522 new->csum = old->csum;
523 new->local_df = old->local_df;
524 new->pkt_type = old->pkt_type;
525 new->ip_summed = old->ip_summed;
526 skb_copy_queue_mapping(new, old);
527 new->priority = old->priority;
528 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
529 new->ipvs_property = old->ipvs_property;
531 new->protocol = old->protocol;
532 new->mark = old->mark;
533 new->skb_iif = old->skb_iif;
535 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
536 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
537 new->nf_trace = old->nf_trace;
539 #ifdef CONFIG_NET_SCHED
540 new->tc_index = old->tc_index;
541 #ifdef CONFIG_NET_CLS_ACT
542 new->tc_verd = old->tc_verd;
545 new->vlan_tci = old->vlan_tci;
547 skb_copy_secmark(new, old);
551 * You should not add any new code to this function. Add it to
552 * __copy_skb_header above instead.
554 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
556 #define C(x) n->x = skb->x
558 n->next = n->prev = NULL;
560 __copy_skb_header(n, skb);
565 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
568 n->destructor = NULL;
574 atomic_set(&n->users, 1);
576 atomic_inc(&(skb_shinfo(skb)->dataref));
584 * skb_morph - morph one skb into another
585 * @dst: the skb to receive the contents
586 * @src: the skb to supply the contents
588 * This is identical to skb_clone except that the target skb is
589 * supplied by the user.
591 * The target skb is returned upon exit.
593 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
595 skb_release_all(dst);
596 return __skb_clone(dst, src);
598 EXPORT_SYMBOL_GPL(skb_morph);
600 /* skb_copy_ubufs - copy userspace skb frags buffers to kernel
601 * @skb: the skb to modify
602 * @gfp_mask: allocation priority
604 * This must be called on SKBTX_DEV_ZEROCOPY skb.
605 * It will copy all frags into kernel and drop the reference
606 * to userspace pages.
608 * If this function is called from an interrupt gfp_mask() must be
611 * Returns 0 on success or a negative error code on failure
612 * to allocate kernel memory to copy to.
614 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
617 int num_frags = skb_shinfo(skb)->nr_frags;
618 struct page *page, *head = NULL;
619 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
621 for (i = 0; i < num_frags; i++) {
623 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
625 page = alloc_page(GFP_ATOMIC);
628 struct page *next = (struct page *)head->private;
634 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
635 memcpy(page_address(page),
636 vaddr + f->page_offset, skb_frag_size(f));
637 kunmap_skb_frag(vaddr);
638 page->private = (unsigned long)head;
642 /* skb frags release userspace buffers */
643 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
644 skb_frag_unref(skb, i);
646 uarg->callback(uarg);
648 /* skb frags point to kernel buffers */
649 for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
650 __skb_fill_page_desc(skb, i-1, head, 0,
651 skb_shinfo(skb)->frags[i - 1].size);
652 head = (struct page *)head->private;
655 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
658 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
661 * skb_clone - duplicate an sk_buff
662 * @skb: buffer to clone
663 * @gfp_mask: allocation priority
665 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
666 * copies share the same packet data but not structure. The new
667 * buffer has a reference count of 1. If the allocation fails the
668 * function returns %NULL otherwise the new buffer is returned.
670 * If this function is called from an interrupt gfp_mask() must be
674 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
678 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
679 if (skb_copy_ubufs(skb, gfp_mask))
684 if (skb->fclone == SKB_FCLONE_ORIG &&
685 n->fclone == SKB_FCLONE_UNAVAILABLE) {
686 atomic_t *fclone_ref = (atomic_t *) (n + 1);
687 n->fclone = SKB_FCLONE_CLONE;
688 atomic_inc(fclone_ref);
690 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
694 kmemcheck_annotate_bitfield(n, flags1);
695 kmemcheck_annotate_bitfield(n, flags2);
696 n->fclone = SKB_FCLONE_UNAVAILABLE;
699 return __skb_clone(n, skb);
701 EXPORT_SYMBOL(skb_clone);
703 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
705 #ifndef NET_SKBUFF_DATA_USES_OFFSET
707 * Shift between the two data areas in bytes
709 unsigned long offset = new->data - old->data;
712 __copy_skb_header(new, old);
714 #ifndef NET_SKBUFF_DATA_USES_OFFSET
715 /* {transport,network,mac}_header are relative to skb->head */
716 new->transport_header += offset;
717 new->network_header += offset;
718 if (skb_mac_header_was_set(new))
719 new->mac_header += offset;
721 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
722 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
723 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
727 * skb_copy - create private copy of an sk_buff
728 * @skb: buffer to copy
729 * @gfp_mask: allocation priority
731 * Make a copy of both an &sk_buff and its data. This is used when the
732 * caller wishes to modify the data and needs a private copy of the
733 * data to alter. Returns %NULL on failure or the pointer to the buffer
734 * on success. The returned buffer has a reference count of 1.
736 * As by-product this function converts non-linear &sk_buff to linear
737 * one, so that &sk_buff becomes completely private and caller is allowed
738 * to modify all the data of returned buffer. This means that this
739 * function is not recommended for use in circumstances when only
740 * header is going to be modified. Use pskb_copy() instead.
743 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
745 int headerlen = skb_headroom(skb);
746 unsigned int size = skb_end_offset(skb) + skb->data_len;
747 struct sk_buff *n = alloc_skb(size, gfp_mask);
752 /* Set the data pointer */
753 skb_reserve(n, headerlen);
754 /* Set the tail pointer and length */
755 skb_put(n, skb->len);
757 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
760 copy_skb_header(n, skb);
763 EXPORT_SYMBOL(skb_copy);
766 * pskb_copy - create copy of an sk_buff with private head.
767 * @skb: buffer to copy
768 * @gfp_mask: allocation priority
770 * Make a copy of both an &sk_buff and part of its data, located
771 * in header. Fragmented data remain shared. This is used when
772 * the caller wishes to modify only header of &sk_buff and needs
773 * private copy of the header to alter. Returns %NULL on failure
774 * or the pointer to the buffer on success.
775 * The returned buffer has a reference count of 1.
778 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
780 unsigned int size = skb_end_pointer(skb) - skb->head;
781 struct sk_buff *n = alloc_skb(size, gfp_mask);
786 /* Set the data pointer */
787 skb_reserve(n, skb_headroom(skb));
788 /* Set the tail pointer and length */
789 skb_put(n, skb_headlen(skb));
791 skb_copy_from_linear_data(skb, n->data, n->len);
793 n->truesize += skb->data_len;
794 n->data_len = skb->data_len;
797 if (skb_shinfo(skb)->nr_frags) {
800 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
801 if (skb_copy_ubufs(skb, gfp_mask)) {
807 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
808 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
809 skb_frag_ref(skb, i);
811 skb_shinfo(n)->nr_frags = i;
814 if (skb_has_frag_list(skb)) {
815 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
816 skb_clone_fraglist(n);
819 copy_skb_header(n, skb);
823 EXPORT_SYMBOL(pskb_copy);
826 * pskb_expand_head - reallocate header of &sk_buff
827 * @skb: buffer to reallocate
828 * @nhead: room to add at head
829 * @ntail: room to add at tail
830 * @gfp_mask: allocation priority
832 * Expands (or creates identical copy, if &nhead and &ntail are zero)
833 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
834 * reference count of 1. Returns zero in the case of success or error,
835 * if expansion failed. In the last case, &sk_buff is not changed.
837 * All the pointers pointing into skb header may change and must be
838 * reloaded after call to this function.
841 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
846 int size = nhead + skb_end_offset(skb) + ntail;
855 size = SKB_DATA_ALIGN(size);
857 /* Check if we can avoid taking references on fragments if we own
858 * the last reference on skb->head. (see skb_release_data())
863 int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
864 fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
868 size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
869 memmove(skb->head + size, skb_shinfo(skb),
870 offsetof(struct skb_shared_info,
871 frags[skb_shinfo(skb)->nr_frags]));
872 memmove(skb->head + nhead, skb->head,
873 skb_tail_pointer(skb) - skb->head);
878 data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
882 size = SKB_WITH_OVERHEAD(ksize(data));
884 /* Copy only real data... and, alas, header. This should be
885 * optimized for the cases when header is void.
887 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
889 memcpy((struct skb_shared_info *)(data + size),
891 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
896 /* copy this zero copy skb frags */
897 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
898 if (skb_copy_ubufs(skb, gfp_mask))
901 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
902 skb_frag_ref(skb, i);
904 if (skb_has_frag_list(skb))
905 skb_clone_fraglist(skb);
907 skb_release_data(skb);
909 off = (data + nhead) - skb->head;
914 #ifdef NET_SKBUFF_DATA_USES_OFFSET
918 skb->end = skb->head + size;
920 /* {transport,network,mac}_header and tail are relative to skb->head */
922 skb->transport_header += off;
923 skb->network_header += off;
924 if (skb_mac_header_was_set(skb))
925 skb->mac_header += off;
926 /* Only adjust this if it actually is csum_start rather than csum */
927 if (skb->ip_summed == CHECKSUM_PARTIAL)
928 skb->csum_start += nhead;
932 atomic_set(&skb_shinfo(skb)->dataref, 1);
940 EXPORT_SYMBOL(pskb_expand_head);
942 /* Make private copy of skb with writable head and some headroom */
944 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
946 struct sk_buff *skb2;
947 int delta = headroom - skb_headroom(skb);
950 skb2 = pskb_copy(skb, GFP_ATOMIC);
952 skb2 = skb_clone(skb, GFP_ATOMIC);
953 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
961 EXPORT_SYMBOL(skb_realloc_headroom);
964 * skb_copy_expand - copy and expand sk_buff
965 * @skb: buffer to copy
966 * @newheadroom: new free bytes at head
967 * @newtailroom: new free bytes at tail
968 * @gfp_mask: allocation priority
970 * Make a copy of both an &sk_buff and its data and while doing so
971 * allocate additional space.
973 * This is used when the caller wishes to modify the data and needs a
974 * private copy of the data to alter as well as more space for new fields.
975 * Returns %NULL on failure or the pointer to the buffer
976 * on success. The returned buffer has a reference count of 1.
978 * You must pass %GFP_ATOMIC as the allocation priority if this function
979 * is called from an interrupt.
981 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
982 int newheadroom, int newtailroom,
986 * Allocate the copy buffer
988 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
990 int oldheadroom = skb_headroom(skb);
991 int head_copy_len, head_copy_off;
997 skb_reserve(n, newheadroom);
999 /* Set the tail pointer and length */
1000 skb_put(n, skb->len);
1002 head_copy_len = oldheadroom;
1004 if (newheadroom <= head_copy_len)
1005 head_copy_len = newheadroom;
1007 head_copy_off = newheadroom - head_copy_len;
1009 /* Copy the linear header and data. */
1010 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1011 skb->len + head_copy_len))
1014 copy_skb_header(n, skb);
1016 off = newheadroom - oldheadroom;
1017 if (n->ip_summed == CHECKSUM_PARTIAL)
1018 n->csum_start += off;
1019 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1020 n->transport_header += off;
1021 n->network_header += off;
1022 if (skb_mac_header_was_set(skb))
1023 n->mac_header += off;
1028 EXPORT_SYMBOL(skb_copy_expand);
1031 * skb_pad - zero pad the tail of an skb
1032 * @skb: buffer to pad
1033 * @pad: space to pad
1035 * Ensure that a buffer is followed by a padding area that is zero
1036 * filled. Used by network drivers which may DMA or transfer data
1037 * beyond the buffer end onto the wire.
1039 * May return error in out of memory cases. The skb is freed on error.
1042 int skb_pad(struct sk_buff *skb, int pad)
1047 /* If the skbuff is non linear tailroom is always zero.. */
1048 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1049 memset(skb->data+skb->len, 0, pad);
1053 ntail = skb->data_len + pad - (skb->end - skb->tail);
1054 if (likely(skb_cloned(skb) || ntail > 0)) {
1055 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1060 /* FIXME: The use of this function with non-linear skb's really needs
1063 err = skb_linearize(skb);
1067 memset(skb->data + skb->len, 0, pad);
1074 EXPORT_SYMBOL(skb_pad);
1077 * skb_put - add data to a buffer
1078 * @skb: buffer to use
1079 * @len: amount of data to add
1081 * This function extends the used data area of the buffer. If this would
1082 * exceed the total buffer size the kernel will panic. A pointer to the
1083 * first byte of the extra data is returned.
1085 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1087 unsigned char *tmp = skb_tail_pointer(skb);
1088 SKB_LINEAR_ASSERT(skb);
1091 if (unlikely(skb->tail > skb->end))
1092 skb_over_panic(skb, len, __builtin_return_address(0));
1095 EXPORT_SYMBOL(skb_put);
1098 * skb_push - add data to the start of a buffer
1099 * @skb: buffer to use
1100 * @len: amount of data to add
1102 * This function extends the used data area of the buffer at the buffer
1103 * start. If this would exceed the total buffer headroom the kernel will
1104 * panic. A pointer to the first byte of the extra data is returned.
1106 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1110 if (unlikely(skb->data<skb->head))
1111 skb_under_panic(skb, len, __builtin_return_address(0));
1114 EXPORT_SYMBOL(skb_push);
1117 * skb_pull - remove data from the start of a buffer
1118 * @skb: buffer to use
1119 * @len: amount of data to remove
1121 * This function removes data from the start of a buffer, returning
1122 * the memory to the headroom. A pointer to the next data in the buffer
1123 * is returned. Once the data has been pulled future pushes will overwrite
1126 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1128 return skb_pull_inline(skb, len);
1130 EXPORT_SYMBOL(skb_pull);
1133 * skb_trim - remove end from a buffer
1134 * @skb: buffer to alter
1137 * Cut the length of a buffer down by removing data from the tail. If
1138 * the buffer is already under the length specified it is not modified.
1139 * The skb must be linear.
1141 void skb_trim(struct sk_buff *skb, unsigned int len)
1144 __skb_trim(skb, len);
1146 EXPORT_SYMBOL(skb_trim);
1148 /* Trims skb to length len. It can change skb pointers.
1151 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1153 struct sk_buff **fragp;
1154 struct sk_buff *frag;
1155 int offset = skb_headlen(skb);
1156 int nfrags = skb_shinfo(skb)->nr_frags;
1160 if (skb_cloned(skb) &&
1161 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1168 for (; i < nfrags; i++) {
1169 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1176 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1179 skb_shinfo(skb)->nr_frags = i;
1181 for (; i < nfrags; i++)
1182 skb_frag_unref(skb, i);
1184 if (skb_has_frag_list(skb))
1185 skb_drop_fraglist(skb);
1189 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1190 fragp = &frag->next) {
1191 int end = offset + frag->len;
1193 if (skb_shared(frag)) {
1194 struct sk_buff *nfrag;
1196 nfrag = skb_clone(frag, GFP_ATOMIC);
1197 if (unlikely(!nfrag))
1200 nfrag->next = frag->next;
1212 unlikely((err = pskb_trim(frag, len - offset))))
1216 skb_drop_list(&frag->next);
1221 if (len > skb_headlen(skb)) {
1222 skb->data_len -= skb->len - len;
1227 skb_set_tail_pointer(skb, len);
1232 EXPORT_SYMBOL(___pskb_trim);
1235 * __pskb_pull_tail - advance tail of skb header
1236 * @skb: buffer to reallocate
1237 * @delta: number of bytes to advance tail
1239 * The function makes a sense only on a fragmented &sk_buff,
1240 * it expands header moving its tail forward and copying necessary
1241 * data from fragmented part.
1243 * &sk_buff MUST have reference count of 1.
1245 * Returns %NULL (and &sk_buff does not change) if pull failed
1246 * or value of new tail of skb in the case of success.
1248 * All the pointers pointing into skb header may change and must be
1249 * reloaded after call to this function.
1252 /* Moves tail of skb head forward, copying data from fragmented part,
1253 * when it is necessary.
1254 * 1. It may fail due to malloc failure.
1255 * 2. It may change skb pointers.
1257 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1259 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1261 /* If skb has not enough free space at tail, get new one
1262 * plus 128 bytes for future expansions. If we have enough
1263 * room at tail, reallocate without expansion only if skb is cloned.
1265 int i, k, eat = (skb->tail + delta) - skb->end;
1267 if (eat > 0 || skb_cloned(skb)) {
1268 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1273 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1276 /* Optimization: no fragments, no reasons to preestimate
1277 * size of pulled pages. Superb.
1279 if (!skb_has_frag_list(skb))
1282 /* Estimate size of pulled pages. */
1284 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1285 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1292 /* If we need update frag list, we are in troubles.
1293 * Certainly, it possible to add an offset to skb data,
1294 * but taking into account that pulling is expected to
1295 * be very rare operation, it is worth to fight against
1296 * further bloating skb head and crucify ourselves here instead.
1297 * Pure masohism, indeed. 8)8)
1300 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1301 struct sk_buff *clone = NULL;
1302 struct sk_buff *insp = NULL;
1307 if (list->len <= eat) {
1308 /* Eaten as whole. */
1313 /* Eaten partially. */
1315 if (skb_shared(list)) {
1316 /* Sucks! We need to fork list. :-( */
1317 clone = skb_clone(list, GFP_ATOMIC);
1323 /* This may be pulled without
1327 if (!pskb_pull(list, eat)) {
1335 /* Free pulled out fragments. */
1336 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1337 skb_shinfo(skb)->frag_list = list->next;
1340 /* And insert new clone at head. */
1343 skb_shinfo(skb)->frag_list = clone;
1346 /* Success! Now we may commit changes to skb data. */
1351 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1352 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1355 skb_frag_unref(skb, i);
1358 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1360 skb_shinfo(skb)->frags[k].page_offset += eat;
1361 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1367 skb_shinfo(skb)->nr_frags = k;
1370 skb->data_len -= delta;
1372 return skb_tail_pointer(skb);
1374 EXPORT_SYMBOL(__pskb_pull_tail);
1377 * skb_copy_bits - copy bits from skb to kernel buffer
1379 * @offset: offset in source
1380 * @to: destination buffer
1381 * @len: number of bytes to copy
1383 * Copy the specified number of bytes from the source skb to the
1384 * destination buffer.
1387 * If its prototype is ever changed,
1388 * check arch/{*}/net/{*}.S files,
1389 * since it is called from BPF assembly code.
1391 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1393 int start = skb_headlen(skb);
1394 struct sk_buff *frag_iter;
1397 if (offset > (int)skb->len - len)
1401 if ((copy = start - offset) > 0) {
1404 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1405 if ((len -= copy) == 0)
1411 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1414 WARN_ON(start > offset + len);
1416 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1417 if ((copy = end - offset) > 0) {
1423 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1425 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1426 offset - start, copy);
1427 kunmap_skb_frag(vaddr);
1429 if ((len -= copy) == 0)
1437 skb_walk_frags(skb, frag_iter) {
1440 WARN_ON(start > offset + len);
1442 end = start + frag_iter->len;
1443 if ((copy = end - offset) > 0) {
1446 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1448 if ((len -= copy) == 0)
1462 EXPORT_SYMBOL(skb_copy_bits);
1465 * Callback from splice_to_pipe(), if we need to release some pages
1466 * at the end of the spd in case we error'ed out in filling the pipe.
1468 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1470 put_page(spd->pages[i]);
1473 static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1474 unsigned int *offset,
1475 struct sk_buff *skb, struct sock *sk)
1477 struct page *p = sk->sk_sndmsg_page;
1482 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1486 off = sk->sk_sndmsg_off = 0;
1487 /* hold one ref to this page until it's full */
1491 off = sk->sk_sndmsg_off;
1492 mlen = PAGE_SIZE - off;
1493 if (mlen < 64 && mlen < *len) {
1498 *len = min_t(unsigned int, *len, mlen);
1501 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1502 sk->sk_sndmsg_off += *len;
1510 * Fill page/offset/length into spd, if it can hold more pages.
1512 static inline int spd_fill_page(struct splice_pipe_desc *spd,
1513 struct pipe_inode_info *pipe, struct page *page,
1514 unsigned int *len, unsigned int offset,
1515 struct sk_buff *skb, int linear,
1518 if (unlikely(spd->nr_pages == pipe->buffers))
1522 page = linear_to_page(page, len, &offset, skb, sk);
1528 spd->pages[spd->nr_pages] = page;
1529 spd->partial[spd->nr_pages].len = *len;
1530 spd->partial[spd->nr_pages].offset = offset;
1536 static inline void __segment_seek(struct page **page, unsigned int *poff,
1537 unsigned int *plen, unsigned int off)
1542 n = *poff / PAGE_SIZE;
1544 *page = nth_page(*page, n);
1546 *poff = *poff % PAGE_SIZE;
1550 static inline int __splice_segment(struct page *page, unsigned int poff,
1551 unsigned int plen, unsigned int *off,
1552 unsigned int *len, struct sk_buff *skb,
1553 struct splice_pipe_desc *spd, int linear,
1555 struct pipe_inode_info *pipe)
1560 /* skip this segment if already processed */
1566 /* ignore any bits we already processed */
1568 __segment_seek(&page, &poff, &plen, *off);
1573 unsigned int flen = min(*len, plen);
1575 /* the linear region may spread across several pages */
1576 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1578 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1581 __segment_seek(&page, &poff, &plen, flen);
1584 } while (*len && plen);
1590 * Map linear and fragment data from the skb to spd. It reports failure if the
1591 * pipe is full or if we already spliced the requested length.
1593 static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1594 unsigned int *offset, unsigned int *len,
1595 struct splice_pipe_desc *spd, struct sock *sk)
1600 * map the linear part
1602 if (__splice_segment(virt_to_page(skb->data),
1603 (unsigned long) skb->data & (PAGE_SIZE - 1),
1605 offset, len, skb, spd, 1, sk, pipe))
1609 * then map the fragments
1611 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1612 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1614 if (__splice_segment(skb_frag_page(f),
1615 f->page_offset, skb_frag_size(f),
1616 offset, len, skb, spd, 0, sk, pipe))
1624 * Map data from the skb to a pipe. Should handle both the linear part,
1625 * the fragments, and the frag list. It does NOT handle frag lists within
1626 * the frag list, if such a thing exists. We'd probably need to recurse to
1627 * handle that cleanly.
1629 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1630 struct pipe_inode_info *pipe, unsigned int tlen,
1633 struct partial_page partial[PIPE_DEF_BUFFERS];
1634 struct page *pages[PIPE_DEF_BUFFERS];
1635 struct splice_pipe_desc spd = {
1638 .nr_pages_max = MAX_SKB_FRAGS,
1640 .ops = &nosteal_pipe_buf_ops,
1641 .spd_release = sock_spd_release,
1643 struct sk_buff *frag_iter;
1644 struct sock *sk = skb->sk;
1647 if (splice_grow_spd(pipe, &spd))
1651 * __skb_splice_bits() only fails if the output has no room left,
1652 * so no point in going over the frag_list for the error case.
1654 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1660 * now see if we have a frag_list to map
1662 skb_walk_frags(skb, frag_iter) {
1665 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1672 * Drop the socket lock, otherwise we have reverse
1673 * locking dependencies between sk_lock and i_mutex
1674 * here as compared to sendfile(). We enter here
1675 * with the socket lock held, and splice_to_pipe() will
1676 * grab the pipe inode lock. For sendfile() emulation,
1677 * we call into ->sendpage() with the i_mutex lock held
1678 * and networking will grab the socket lock.
1681 ret = splice_to_pipe(pipe, &spd);
1685 splice_shrink_spd(&spd);
1690 * skb_store_bits - store bits from kernel buffer to skb
1691 * @skb: destination buffer
1692 * @offset: offset in destination
1693 * @from: source buffer
1694 * @len: number of bytes to copy
1696 * Copy the specified number of bytes from the source buffer to the
1697 * destination skb. This function handles all the messy bits of
1698 * traversing fragment lists and such.
1701 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1703 int start = skb_headlen(skb);
1704 struct sk_buff *frag_iter;
1707 if (offset > (int)skb->len - len)
1710 if ((copy = start - offset) > 0) {
1713 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1714 if ((len -= copy) == 0)
1720 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1721 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1724 WARN_ON(start > offset + len);
1726 end = start + skb_frag_size(frag);
1727 if ((copy = end - offset) > 0) {
1733 vaddr = kmap_skb_frag(frag);
1734 memcpy(vaddr + frag->page_offset + offset - start,
1736 kunmap_skb_frag(vaddr);
1738 if ((len -= copy) == 0)
1746 skb_walk_frags(skb, frag_iter) {
1749 WARN_ON(start > offset + len);
1751 end = start + frag_iter->len;
1752 if ((copy = end - offset) > 0) {
1755 if (skb_store_bits(frag_iter, offset - start,
1758 if ((len -= copy) == 0)
1771 EXPORT_SYMBOL(skb_store_bits);
1773 /* Checksum skb data. */
1775 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1776 int len, __wsum csum)
1778 int start = skb_headlen(skb);
1779 int i, copy = start - offset;
1780 struct sk_buff *frag_iter;
1783 /* Checksum header. */
1787 csum = csum_partial(skb->data + offset, copy, csum);
1788 if ((len -= copy) == 0)
1794 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1797 WARN_ON(start > offset + len);
1799 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1800 if ((copy = end - offset) > 0) {
1803 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1807 vaddr = kmap_skb_frag(frag);
1808 csum2 = csum_partial(vaddr + frag->page_offset +
1809 offset - start, copy, 0);
1810 kunmap_skb_frag(vaddr);
1811 csum = csum_block_add(csum, csum2, pos);
1820 skb_walk_frags(skb, frag_iter) {
1823 WARN_ON(start > offset + len);
1825 end = start + frag_iter->len;
1826 if ((copy = end - offset) > 0) {
1830 csum2 = skb_checksum(frag_iter, offset - start,
1832 csum = csum_block_add(csum, csum2, pos);
1833 if ((len -= copy) == 0)
1844 EXPORT_SYMBOL(skb_checksum);
1846 /* Both of above in one bottle. */
1848 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1849 u8 *to, int len, __wsum csum)
1851 int start = skb_headlen(skb);
1852 int i, copy = start - offset;
1853 struct sk_buff *frag_iter;
1860 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1862 if ((len -= copy) == 0)
1869 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1872 WARN_ON(start > offset + len);
1874 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1875 if ((copy = end - offset) > 0) {
1878 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1882 vaddr = kmap_skb_frag(frag);
1883 csum2 = csum_partial_copy_nocheck(vaddr +
1887 kunmap_skb_frag(vaddr);
1888 csum = csum_block_add(csum, csum2, pos);
1898 skb_walk_frags(skb, frag_iter) {
1902 WARN_ON(start > offset + len);
1904 end = start + frag_iter->len;
1905 if ((copy = end - offset) > 0) {
1908 csum2 = skb_copy_and_csum_bits(frag_iter,
1911 csum = csum_block_add(csum, csum2, pos);
1912 if ((len -= copy) == 0)
1923 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1925 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1930 if (skb->ip_summed == CHECKSUM_PARTIAL)
1931 csstart = skb_checksum_start_offset(skb);
1933 csstart = skb_headlen(skb);
1935 BUG_ON(csstart > skb_headlen(skb));
1937 skb_copy_from_linear_data(skb, to, csstart);
1940 if (csstart != skb->len)
1941 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1942 skb->len - csstart, 0);
1944 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1945 long csstuff = csstart + skb->csum_offset;
1947 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1950 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1953 * skb_dequeue - remove from the head of the queue
1954 * @list: list to dequeue from
1956 * Remove the head of the list. The list lock is taken so the function
1957 * may be used safely with other locking list functions. The head item is
1958 * returned or %NULL if the list is empty.
1961 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1963 unsigned long flags;
1964 struct sk_buff *result;
1966 spin_lock_irqsave(&list->lock, flags);
1967 result = __skb_dequeue(list);
1968 spin_unlock_irqrestore(&list->lock, flags);
1971 EXPORT_SYMBOL(skb_dequeue);
1974 * skb_dequeue_tail - remove from the tail of the queue
1975 * @list: list to dequeue from
1977 * Remove the tail of the list. The list lock is taken so the function
1978 * may be used safely with other locking list functions. The tail item is
1979 * returned or %NULL if the list is empty.
1981 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1983 unsigned long flags;
1984 struct sk_buff *result;
1986 spin_lock_irqsave(&list->lock, flags);
1987 result = __skb_dequeue_tail(list);
1988 spin_unlock_irqrestore(&list->lock, flags);
1991 EXPORT_SYMBOL(skb_dequeue_tail);
1994 * skb_queue_purge - empty a list
1995 * @list: list to empty
1997 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1998 * the list and one reference dropped. This function takes the list
1999 * lock and is atomic with respect to other list locking functions.
2001 void skb_queue_purge(struct sk_buff_head *list)
2003 struct sk_buff *skb;
2004 while ((skb = skb_dequeue(list)) != NULL)
2007 EXPORT_SYMBOL(skb_queue_purge);
2010 * skb_queue_head - queue a buffer at the list head
2011 * @list: list to use
2012 * @newsk: buffer to queue
2014 * Queue a buffer at the start of the list. This function takes the
2015 * list lock and can be used safely with other locking &sk_buff functions
2018 * A buffer cannot be placed on two lists at the same time.
2020 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2022 unsigned long flags;
2024 spin_lock_irqsave(&list->lock, flags);
2025 __skb_queue_head(list, newsk);
2026 spin_unlock_irqrestore(&list->lock, flags);
2028 EXPORT_SYMBOL(skb_queue_head);
2031 * skb_queue_tail - queue a buffer at the list tail
2032 * @list: list to use
2033 * @newsk: buffer to queue
2035 * Queue a buffer at the tail of the list. This function takes the
2036 * list lock and can be used safely with other locking &sk_buff functions
2039 * A buffer cannot be placed on two lists at the same time.
2041 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2043 unsigned long flags;
2045 spin_lock_irqsave(&list->lock, flags);
2046 __skb_queue_tail(list, newsk);
2047 spin_unlock_irqrestore(&list->lock, flags);
2049 EXPORT_SYMBOL(skb_queue_tail);
2052 * skb_unlink - remove a buffer from a list
2053 * @skb: buffer to remove
2054 * @list: list to use
2056 * Remove a packet from a list. The list locks are taken and this
2057 * function is atomic with respect to other list locked calls
2059 * You must know what list the SKB is on.
2061 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2063 unsigned long flags;
2065 spin_lock_irqsave(&list->lock, flags);
2066 __skb_unlink(skb, list);
2067 spin_unlock_irqrestore(&list->lock, flags);
2069 EXPORT_SYMBOL(skb_unlink);
2072 * skb_append - append a buffer
2073 * @old: buffer to insert after
2074 * @newsk: buffer to insert
2075 * @list: list to use
2077 * Place a packet after a given packet in a list. The list locks are taken
2078 * and this function is atomic with respect to other list locked calls.
2079 * A buffer cannot be placed on two lists at the same time.
2081 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2083 unsigned long flags;
2085 spin_lock_irqsave(&list->lock, flags);
2086 __skb_queue_after(list, old, newsk);
2087 spin_unlock_irqrestore(&list->lock, flags);
2089 EXPORT_SYMBOL(skb_append);
2092 * skb_insert - insert a buffer
2093 * @old: buffer to insert before
2094 * @newsk: buffer to insert
2095 * @list: list to use
2097 * Place a packet before a given packet in a list. The list locks are
2098 * taken and this function is atomic with respect to other list locked
2101 * A buffer cannot be placed on two lists at the same time.
2103 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2105 unsigned long flags;
2107 spin_lock_irqsave(&list->lock, flags);
2108 __skb_insert(newsk, old->prev, old, list);
2109 spin_unlock_irqrestore(&list->lock, flags);
2111 EXPORT_SYMBOL(skb_insert);
2113 static inline void skb_split_inside_header(struct sk_buff *skb,
2114 struct sk_buff* skb1,
2115 const u32 len, const int pos)
2119 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2121 /* And move data appendix as is. */
2122 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2123 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2125 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2126 skb_shinfo(skb)->nr_frags = 0;
2127 skb1->data_len = skb->data_len;
2128 skb1->len += skb1->data_len;
2131 skb_set_tail_pointer(skb, len);
2134 static inline void skb_split_no_header(struct sk_buff *skb,
2135 struct sk_buff* skb1,
2136 const u32 len, int pos)
2139 const int nfrags = skb_shinfo(skb)->nr_frags;
2141 skb_shinfo(skb)->nr_frags = 0;
2142 skb1->len = skb1->data_len = skb->len - len;
2144 skb->data_len = len - pos;
2146 for (i = 0; i < nfrags; i++) {
2147 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2149 if (pos + size > len) {
2150 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2154 * We have two variants in this case:
2155 * 1. Move all the frag to the second
2156 * part, if it is possible. F.e.
2157 * this approach is mandatory for TUX,
2158 * where splitting is expensive.
2159 * 2. Split is accurately. We make this.
2161 skb_frag_ref(skb, i);
2162 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2163 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2164 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2165 skb_shinfo(skb)->nr_frags++;
2169 skb_shinfo(skb)->nr_frags++;
2172 skb_shinfo(skb1)->nr_frags = k;
2176 * skb_split - Split fragmented skb to two parts at length len.
2177 * @skb: the buffer to split
2178 * @skb1: the buffer to receive the second part
2179 * @len: new length for skb
2181 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2183 int pos = skb_headlen(skb);
2185 if (len < pos) /* Split line is inside header. */
2186 skb_split_inside_header(skb, skb1, len, pos);
2187 else /* Second chunk has no header, nothing to copy. */
2188 skb_split_no_header(skb, skb1, len, pos);
2190 EXPORT_SYMBOL(skb_split);
2192 /* Shifting from/to a cloned skb is a no-go.
2194 * Caller cannot keep skb_shinfo related pointers past calling here!
2196 static int skb_prepare_for_shift(struct sk_buff *skb)
2198 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2202 * skb_shift - Shifts paged data partially from skb to another
2203 * @tgt: buffer into which tail data gets added
2204 * @skb: buffer from which the paged data comes from
2205 * @shiftlen: shift up to this many bytes
2207 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2208 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2209 * It's up to caller to free skb if everything was shifted.
2211 * If @tgt runs out of frags, the whole operation is aborted.
2213 * Skb cannot include anything else but paged data while tgt is allowed
2214 * to have non-paged data as well.
2216 * TODO: full sized shift could be optimized but that would need
2217 * specialized skb free'er to handle frags without up-to-date nr_frags.
2219 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2221 int from, to, merge, todo;
2222 struct skb_frag_struct *fragfrom, *fragto;
2224 BUG_ON(shiftlen > skb->len);
2225 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2229 to = skb_shinfo(tgt)->nr_frags;
2230 fragfrom = &skb_shinfo(skb)->frags[from];
2232 /* Actual merge is delayed until the point when we know we can
2233 * commit all, so that we don't have to undo partial changes
2236 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2237 fragfrom->page_offset)) {
2242 todo -= skb_frag_size(fragfrom);
2244 if (skb_prepare_for_shift(skb) ||
2245 skb_prepare_for_shift(tgt))
2248 /* All previous frag pointers might be stale! */
2249 fragfrom = &skb_shinfo(skb)->frags[from];
2250 fragto = &skb_shinfo(tgt)->frags[merge];
2252 skb_frag_size_add(fragto, shiftlen);
2253 skb_frag_size_sub(fragfrom, shiftlen);
2254 fragfrom->page_offset += shiftlen;
2262 /* Skip full, not-fitting skb to avoid expensive operations */
2263 if ((shiftlen == skb->len) &&
2264 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2267 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2270 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2271 if (to == MAX_SKB_FRAGS)
2274 fragfrom = &skb_shinfo(skb)->frags[from];
2275 fragto = &skb_shinfo(tgt)->frags[to];
2277 if (todo >= skb_frag_size(fragfrom)) {
2278 *fragto = *fragfrom;
2279 todo -= skb_frag_size(fragfrom);
2284 __skb_frag_ref(fragfrom);
2285 fragto->page = fragfrom->page;
2286 fragto->page_offset = fragfrom->page_offset;
2287 skb_frag_size_set(fragto, todo);
2289 fragfrom->page_offset += todo;
2290 skb_frag_size_sub(fragfrom, todo);
2298 /* Ready to "commit" this state change to tgt */
2299 skb_shinfo(tgt)->nr_frags = to;
2302 fragfrom = &skb_shinfo(skb)->frags[0];
2303 fragto = &skb_shinfo(tgt)->frags[merge];
2305 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2306 __skb_frag_unref(fragfrom);
2309 /* Reposition in the original skb */
2311 while (from < skb_shinfo(skb)->nr_frags)
2312 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2313 skb_shinfo(skb)->nr_frags = to;
2315 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2318 /* Most likely the tgt won't ever need its checksum anymore, skb on
2319 * the other hand might need it if it needs to be resent
2321 tgt->ip_summed = CHECKSUM_PARTIAL;
2322 skb->ip_summed = CHECKSUM_PARTIAL;
2324 /* Yak, is it really working this way? Some helper please? */
2325 skb->len -= shiftlen;
2326 skb->data_len -= shiftlen;
2327 skb->truesize -= shiftlen;
2328 tgt->len += shiftlen;
2329 tgt->data_len += shiftlen;
2330 tgt->truesize += shiftlen;
2336 * skb_prepare_seq_read - Prepare a sequential read of skb data
2337 * @skb: the buffer to read
2338 * @from: lower offset of data to be read
2339 * @to: upper offset of data to be read
2340 * @st: state variable
2342 * Initializes the specified state variable. Must be called before
2343 * invoking skb_seq_read() for the first time.
2345 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2346 unsigned int to, struct skb_seq_state *st)
2348 st->lower_offset = from;
2349 st->upper_offset = to;
2350 st->root_skb = st->cur_skb = skb;
2351 st->frag_idx = st->stepped_offset = 0;
2352 st->frag_data = NULL;
2354 EXPORT_SYMBOL(skb_prepare_seq_read);
2357 * skb_seq_read - Sequentially read skb data
2358 * @consumed: number of bytes consumed by the caller so far
2359 * @data: destination pointer for data to be returned
2360 * @st: state variable
2362 * Reads a block of skb data at &consumed relative to the
2363 * lower offset specified to skb_prepare_seq_read(). Assigns
2364 * the head of the data block to &data and returns the length
2365 * of the block or 0 if the end of the skb data or the upper
2366 * offset has been reached.
2368 * The caller is not required to consume all of the data
2369 * returned, i.e. &consumed is typically set to the number
2370 * of bytes already consumed and the next call to
2371 * skb_seq_read() will return the remaining part of the block.
2373 * Note 1: The size of each block of data returned can be arbitrary,
2374 * this limitation is the cost for zerocopy seqeuental
2375 * reads of potentially non linear data.
2377 * Note 2: Fragment lists within fragments are not implemented
2378 * at the moment, state->root_skb could be replaced with
2379 * a stack for this purpose.
2381 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2382 struct skb_seq_state *st)
2384 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2387 if (unlikely(abs_offset >= st->upper_offset))
2391 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2393 if (abs_offset < block_limit && !st->frag_data) {
2394 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2395 return block_limit - abs_offset;
2398 if (st->frag_idx == 0 && !st->frag_data)
2399 st->stepped_offset += skb_headlen(st->cur_skb);
2401 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2402 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2403 block_limit = skb_frag_size(frag) + st->stepped_offset;
2405 if (abs_offset < block_limit) {
2407 st->frag_data = kmap_skb_frag(frag);
2409 *data = (u8 *) st->frag_data + frag->page_offset +
2410 (abs_offset - st->stepped_offset);
2412 return block_limit - abs_offset;
2415 if (st->frag_data) {
2416 kunmap_skb_frag(st->frag_data);
2417 st->frag_data = NULL;
2421 st->stepped_offset += skb_frag_size(frag);
2424 if (st->frag_data) {
2425 kunmap_skb_frag(st->frag_data);
2426 st->frag_data = NULL;
2429 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2430 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2433 } else if (st->cur_skb->next) {
2434 st->cur_skb = st->cur_skb->next;
2441 EXPORT_SYMBOL(skb_seq_read);
2444 * skb_abort_seq_read - Abort a sequential read of skb data
2445 * @st: state variable
2447 * Must be called if skb_seq_read() was not called until it
2450 void skb_abort_seq_read(struct skb_seq_state *st)
2453 kunmap_skb_frag(st->frag_data);
2455 EXPORT_SYMBOL(skb_abort_seq_read);
2457 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2459 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2460 struct ts_config *conf,
2461 struct ts_state *state)
2463 return skb_seq_read(offset, text, TS_SKB_CB(state));
2466 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2468 skb_abort_seq_read(TS_SKB_CB(state));
2472 * skb_find_text - Find a text pattern in skb data
2473 * @skb: the buffer to look in
2474 * @from: search offset
2476 * @config: textsearch configuration
2477 * @state: uninitialized textsearch state variable
2479 * Finds a pattern in the skb data according to the specified
2480 * textsearch configuration. Use textsearch_next() to retrieve
2481 * subsequent occurrences of the pattern. Returns the offset
2482 * to the first occurrence or UINT_MAX if no match was found.
2484 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2485 unsigned int to, struct ts_config *config,
2486 struct ts_state *state)
2490 config->get_next_block = skb_ts_get_next_block;
2491 config->finish = skb_ts_finish;
2493 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2495 ret = textsearch_find(config, state);
2496 return (ret <= to - from ? ret : UINT_MAX);
2498 EXPORT_SYMBOL(skb_find_text);
2501 * skb_append_datato_frags: - append the user data to a skb
2502 * @sk: sock structure
2503 * @skb: skb structure to be appened with user data.
2504 * @getfrag: call back function to be used for getting the user data
2505 * @from: pointer to user message iov
2506 * @length: length of the iov message
2508 * Description: This procedure append the user data in the fragment part
2509 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2511 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2512 int (*getfrag)(void *from, char *to, int offset,
2513 int len, int odd, struct sk_buff *skb),
2514 void *from, int length)
2517 skb_frag_t *frag = NULL;
2518 struct page *page = NULL;
2524 /* Return error if we don't have space for new frag */
2525 frg_cnt = skb_shinfo(skb)->nr_frags;
2526 if (frg_cnt >= MAX_SKB_FRAGS)
2529 /* allocate a new page for next frag */
2530 page = alloc_pages(sk->sk_allocation, 0);
2532 /* If alloc_page fails just return failure and caller will
2533 * free previous allocated pages by doing kfree_skb()
2538 /* initialize the next frag */
2539 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2540 skb->truesize += PAGE_SIZE;
2541 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2543 /* get the new initialized frag */
2544 frg_cnt = skb_shinfo(skb)->nr_frags;
2545 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2547 /* copy the user data to page */
2548 left = PAGE_SIZE - frag->page_offset;
2549 copy = (length > left)? left : length;
2551 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2552 offset, copy, 0, skb);
2556 /* copy was successful so update the size parameters */
2557 skb_frag_size_add(frag, copy);
2559 skb->data_len += copy;
2563 } while (length > 0);
2567 EXPORT_SYMBOL(skb_append_datato_frags);
2570 * skb_pull_rcsum - pull skb and update receive checksum
2571 * @skb: buffer to update
2572 * @len: length of data pulled
2574 * This function performs an skb_pull on the packet and updates
2575 * the CHECKSUM_COMPLETE checksum. It should be used on
2576 * receive path processing instead of skb_pull unless you know
2577 * that the checksum difference is zero (e.g., a valid IP header)
2578 * or you are setting ip_summed to CHECKSUM_NONE.
2580 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2582 unsigned char *data = skb->data;
2584 BUG_ON(len > skb->len);
2585 __skb_pull(skb, len);
2586 skb_postpull_rcsum(skb, data, len);
2589 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2592 * skb_segment - Perform protocol segmentation on skb.
2593 * @skb: buffer to segment
2594 * @features: features for the output path (see dev->features)
2596 * This function performs segmentation on the given skb. It returns
2597 * a pointer to the first in a list of new skbs for the segments.
2598 * In case of error it returns ERR_PTR(err).
2600 struct sk_buff *skb_segment(struct sk_buff *skb, u32 features)
2602 struct sk_buff *segs = NULL;
2603 struct sk_buff *tail = NULL;
2604 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2605 unsigned int mss = skb_shinfo(skb)->gso_size;
2606 unsigned int doffset = skb->data - skb_mac_header(skb);
2607 unsigned int offset = doffset;
2608 unsigned int headroom;
2610 int sg = !!(features & NETIF_F_SG);
2611 int nfrags = skb_shinfo(skb)->nr_frags;
2616 __skb_push(skb, doffset);
2617 headroom = skb_headroom(skb);
2618 pos = skb_headlen(skb);
2621 struct sk_buff *nskb;
2626 len = skb->len - offset;
2630 hsize = skb_headlen(skb) - offset;
2633 if (hsize > len || !sg)
2636 if (!hsize && i >= nfrags) {
2637 BUG_ON(fskb->len != len);
2640 nskb = skb_clone(fskb, GFP_ATOMIC);
2643 if (unlikely(!nskb))
2646 hsize = skb_end_offset(nskb);
2647 if (skb_cow_head(nskb, doffset + headroom)) {
2652 nskb->truesize += skb_end_offset(nskb) - hsize;
2653 skb_release_head_state(nskb);
2654 __skb_push(nskb, doffset);
2656 nskb = alloc_skb(hsize + doffset + headroom,
2659 if (unlikely(!nskb))
2662 skb_reserve(nskb, headroom);
2663 __skb_put(nskb, doffset);
2672 __copy_skb_header(nskb, skb);
2673 nskb->mac_len = skb->mac_len;
2675 /* nskb and skb might have different headroom */
2676 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2677 nskb->csum_start += skb_headroom(nskb) - headroom;
2679 skb_reset_mac_header(nskb);
2680 skb_set_network_header(nskb, skb->mac_len);
2681 nskb->transport_header = (nskb->network_header +
2682 skb_network_header_len(skb));
2683 skb_copy_from_linear_data(skb, nskb->data, doffset);
2685 if (fskb != skb_shinfo(skb)->frag_list)
2689 nskb->ip_summed = CHECKSUM_NONE;
2690 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2696 frag = skb_shinfo(nskb)->frags;
2698 skb_copy_from_linear_data_offset(skb, offset,
2699 skb_put(nskb, hsize), hsize);
2701 while (pos < offset + len && i < nfrags) {
2702 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
2705 *frag = skb_shinfo(skb)->frags[i];
2706 __skb_frag_ref(frag);
2707 size = skb_frag_size(frag);
2710 frag->page_offset += offset - pos;
2711 skb_frag_size_sub(frag, offset - pos);
2714 skb_shinfo(nskb)->nr_frags++;
2716 if (pos + size <= offset + len) {
2720 skb_frag_size_sub(frag, pos + size - (offset + len));
2727 if (pos < offset + len) {
2728 struct sk_buff *fskb2 = fskb;
2730 BUG_ON(pos + fskb->len != offset + len);
2736 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2742 SKB_FRAG_ASSERT(nskb);
2743 skb_shinfo(nskb)->frag_list = fskb2;
2747 nskb->data_len = len - hsize;
2748 nskb->len += nskb->data_len;
2749 nskb->truesize += nskb->data_len;
2750 } while ((offset += len) < skb->len);
2755 while ((skb = segs)) {
2759 return ERR_PTR(err);
2761 EXPORT_SYMBOL_GPL(skb_segment);
2763 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2765 struct sk_buff *p = *head;
2766 struct sk_buff *nskb;
2767 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2768 struct skb_shared_info *pinfo = skb_shinfo(p);
2769 unsigned int headroom;
2770 unsigned int len = skb_gro_len(skb);
2771 unsigned int offset = skb_gro_offset(skb);
2772 unsigned int headlen = skb_headlen(skb);
2774 if (p->len + len >= 65536)
2777 if (pinfo->frag_list)
2779 else if (headlen <= offset) {
2782 int i = skbinfo->nr_frags;
2783 int nr_frags = pinfo->nr_frags + i;
2787 if (nr_frags > MAX_SKB_FRAGS)
2790 pinfo->nr_frags = nr_frags;
2791 skbinfo->nr_frags = 0;
2793 frag = pinfo->frags + nr_frags;
2794 frag2 = skbinfo->frags + i;
2799 frag->page_offset += offset;
2800 skb_frag_size_sub(frag, offset);
2802 skb->truesize -= skb->data_len;
2803 skb->len -= skb->data_len;
2806 NAPI_GRO_CB(skb)->free = 1;
2808 } else if (skb_gro_len(p) != pinfo->gso_size)
2811 headroom = skb_headroom(p);
2812 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2813 if (unlikely(!nskb))
2816 __copy_skb_header(nskb, p);
2817 nskb->mac_len = p->mac_len;
2819 skb_reserve(nskb, headroom);
2820 __skb_put(nskb, skb_gro_offset(p));
2822 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
2823 skb_set_network_header(nskb, skb_network_offset(p));
2824 skb_set_transport_header(nskb, skb_transport_offset(p));
2826 __skb_pull(p, skb_gro_offset(p));
2827 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2828 p->data - skb_mac_header(p));
2830 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2831 skb_shinfo(nskb)->frag_list = p;
2832 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
2833 pinfo->gso_size = 0;
2834 skb_header_release(p);
2837 nskb->data_len += p->len;
2838 nskb->truesize += p->len;
2839 nskb->len += p->len;
2842 nskb->next = p->next;
2848 if (offset > headlen) {
2849 unsigned int eat = offset - headlen;
2851 skbinfo->frags[0].page_offset += eat;
2852 skb_frag_size_sub(&skbinfo->frags[0], eat);
2853 skb->data_len -= eat;
2858 __skb_pull(skb, offset);
2860 p->prev->next = skb;
2862 skb_header_release(skb);
2865 NAPI_GRO_CB(p)->count++;
2870 NAPI_GRO_CB(skb)->same_flow = 1;
2873 EXPORT_SYMBOL_GPL(skb_gro_receive);
2875 void __init skb_init(void)
2877 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2878 sizeof(struct sk_buff),
2880 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2882 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2883 (2*sizeof(struct sk_buff)) +
2886 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2891 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2892 * @skb: Socket buffer containing the buffers to be mapped
2893 * @sg: The scatter-gather list to map into
2894 * @offset: The offset into the buffer's contents to start mapping
2895 * @len: Length of buffer space to be mapped
2897 * Fill the specified scatter-gather list with mappings/pointers into a
2898 * region of the buffer space attached to a socket buffer.
2901 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2903 int start = skb_headlen(skb);
2904 int i, copy = start - offset;
2905 struct sk_buff *frag_iter;
2911 sg_set_buf(sg, skb->data + offset, copy);
2913 if ((len -= copy) == 0)
2918 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2921 WARN_ON(start > offset + len);
2923 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2924 if ((copy = end - offset) > 0) {
2925 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2929 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
2930 frag->page_offset+offset-start);
2939 skb_walk_frags(skb, frag_iter) {
2942 WARN_ON(start > offset + len);
2944 end = start + frag_iter->len;
2945 if ((copy = end - offset) > 0) {
2948 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
2950 if ((len -= copy) == 0)
2960 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2962 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2964 sg_mark_end(&sg[nsg - 1]);
2968 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2971 * skb_cow_data - Check that a socket buffer's data buffers are writable
2972 * @skb: The socket buffer to check.
2973 * @tailbits: Amount of trailing space to be added
2974 * @trailer: Returned pointer to the skb where the @tailbits space begins
2976 * Make sure that the data buffers attached to a socket buffer are
2977 * writable. If they are not, private copies are made of the data buffers
2978 * and the socket buffer is set to use these instead.
2980 * If @tailbits is given, make sure that there is space to write @tailbits
2981 * bytes of data beyond current end of socket buffer. @trailer will be
2982 * set to point to the skb in which this space begins.
2984 * The number of scatterlist elements required to completely map the
2985 * COW'd and extended socket buffer will be returned.
2987 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2991 struct sk_buff *skb1, **skb_p;
2993 /* If skb is cloned or its head is paged, reallocate
2994 * head pulling out all the pages (pages are considered not writable
2995 * at the moment even if they are anonymous).
2997 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2998 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3001 /* Easy case. Most of packets will go this way. */
3002 if (!skb_has_frag_list(skb)) {
3003 /* A little of trouble, not enough of space for trailer.
3004 * This should not happen, when stack is tuned to generate
3005 * good frames. OK, on miss we reallocate and reserve even more
3006 * space, 128 bytes is fair. */
3008 if (skb_tailroom(skb) < tailbits &&
3009 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3017 /* Misery. We are in troubles, going to mincer fragments... */
3020 skb_p = &skb_shinfo(skb)->frag_list;
3023 while ((skb1 = *skb_p) != NULL) {
3026 /* The fragment is partially pulled by someone,
3027 * this can happen on input. Copy it and everything
3030 if (skb_shared(skb1))
3033 /* If the skb is the last, worry about trailer. */
3035 if (skb1->next == NULL && tailbits) {
3036 if (skb_shinfo(skb1)->nr_frags ||
3037 skb_has_frag_list(skb1) ||
3038 skb_tailroom(skb1) < tailbits)
3039 ntail = tailbits + 128;
3045 skb_shinfo(skb1)->nr_frags ||
3046 skb_has_frag_list(skb1)) {
3047 struct sk_buff *skb2;
3049 /* Fuck, we are miserable poor guys... */
3051 skb2 = skb_copy(skb1, GFP_ATOMIC);
3053 skb2 = skb_copy_expand(skb1,
3057 if (unlikely(skb2 == NULL))
3061 skb_set_owner_w(skb2, skb1->sk);
3063 /* Looking around. Are we still alive?
3064 * OK, link new skb, drop old one */
3066 skb2->next = skb1->next;
3073 skb_p = &skb1->next;
3078 EXPORT_SYMBOL_GPL(skb_cow_data);
3080 static void sock_rmem_free(struct sk_buff *skb)
3082 struct sock *sk = skb->sk;
3084 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3088 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3090 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3094 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3095 (unsigned)sk->sk_rcvbuf)
3100 skb->destructor = sock_rmem_free;
3101 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3103 /* before exiting rcu section, make sure dst is refcounted */
3106 skb_queue_tail(&sk->sk_error_queue, skb);
3107 if (!sock_flag(sk, SOCK_DEAD))
3108 sk->sk_data_ready(sk, len);
3111 EXPORT_SYMBOL(sock_queue_err_skb);
3113 void skb_tstamp_tx(struct sk_buff *orig_skb,
3114 struct skb_shared_hwtstamps *hwtstamps)
3116 struct sock *sk = orig_skb->sk;
3117 struct sock_exterr_skb *serr;
3118 struct sk_buff *skb;
3124 skb = skb_clone(orig_skb, GFP_ATOMIC);
3129 *skb_hwtstamps(skb) =
3133 * no hardware time stamps available,
3134 * so keep the shared tx_flags and only
3135 * store software time stamp
3137 skb->tstamp = ktime_get_real();
3140 serr = SKB_EXT_ERR(skb);
3141 memset(serr, 0, sizeof(*serr));
3142 serr->ee.ee_errno = ENOMSG;
3143 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3145 err = sock_queue_err_skb(sk, skb);
3150 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3154 * skb_partial_csum_set - set up and verify partial csum values for packet
3155 * @skb: the skb to set
3156 * @start: the number of bytes after skb->data to start checksumming.
3157 * @off: the offset from start to place the checksum.
3159 * For untrusted partially-checksummed packets, we need to make sure the values
3160 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3162 * This function checks and sets those values and skb->ip_summed: if this
3163 * returns false you should drop the packet.
3165 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3167 if (unlikely(start > skb_headlen(skb)) ||
3168 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3169 if (net_ratelimit())
3171 "bad partial csum: csum=%u/%u len=%u\n",
3172 start, off, skb_headlen(skb));
3175 skb->ip_summed = CHECKSUM_PARTIAL;
3176 skb->csum_start = skb_headroom(skb) + start;
3177 skb->csum_offset = off;
3180 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3182 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3184 if (net_ratelimit())
3185 pr_warning("%s: received packets cannot be forwarded"
3186 " while LRO is enabled\n", skb->dev->name);
3188 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3191 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3195 * skb_gso_transport_seglen is used to determine the real size of the
3196 * individual segments, including Layer4 headers (TCP/UDP).
3198 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3200 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3202 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3204 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3205 return tcp_hdrlen(skb) + shinfo->gso_size;
3207 /* UFO sets gso_size to the size of the fragmentation
3208 * payload, i.e. the size of the L4 (UDP) header is already
3211 return shinfo->gso_size;
3213 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);