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 kfree_skb_list(*listp);
284 static inline void skb_drop_fraglist(struct sk_buff *skb)
286 skb_drop_list(&skb_shinfo(skb)->frag_list);
289 static void skb_clone_fraglist(struct sk_buff *skb)
291 struct sk_buff *list;
293 skb_walk_frags(skb, list)
297 static void skb_release_data(struct sk_buff *skb)
300 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
301 &skb_shinfo(skb)->dataref)) {
302 if (skb_shinfo(skb)->nr_frags) {
304 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
305 skb_frag_unref(skb, i);
309 * If skb buf is from userspace, we need to notify the caller
310 * the lower device DMA has done;
312 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
313 struct ubuf_info *uarg;
315 uarg = skb_shinfo(skb)->destructor_arg;
317 uarg->callback(uarg);
320 if (skb_has_frag_list(skb))
321 skb_drop_fraglist(skb);
328 * Free an skbuff by memory without cleaning the state.
330 static void kfree_skbmem(struct sk_buff *skb)
332 struct sk_buff *other;
333 atomic_t *fclone_ref;
335 switch (skb->fclone) {
336 case SKB_FCLONE_UNAVAILABLE:
337 kmem_cache_free(skbuff_head_cache, skb);
340 case SKB_FCLONE_ORIG:
341 fclone_ref = (atomic_t *) (skb + 2);
342 if (atomic_dec_and_test(fclone_ref))
343 kmem_cache_free(skbuff_fclone_cache, skb);
346 case SKB_FCLONE_CLONE:
347 fclone_ref = (atomic_t *) (skb + 1);
350 /* The clone portion is available for
351 * fast-cloning again.
353 skb->fclone = SKB_FCLONE_UNAVAILABLE;
355 if (atomic_dec_and_test(fclone_ref))
356 kmem_cache_free(skbuff_fclone_cache, other);
361 static void skb_release_head_state(struct sk_buff *skb)
365 secpath_put(skb->sp);
367 if (skb->destructor) {
369 skb->destructor(skb);
371 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
372 nf_conntrack_put(skb->nfct);
374 #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
375 nf_conntrack_put_reasm(skb->nfct_reasm);
377 #ifdef CONFIG_BRIDGE_NETFILTER
378 nf_bridge_put(skb->nf_bridge);
380 /* XXX: IS this still necessary? - JHS */
381 #ifdef CONFIG_NET_SCHED
383 #ifdef CONFIG_NET_CLS_ACT
389 /* Free everything but the sk_buff shell. */
390 static void skb_release_all(struct sk_buff *skb)
392 skb_release_head_state(skb);
393 skb_release_data(skb);
397 * __kfree_skb - private function
400 * Free an sk_buff. Release anything attached to the buffer.
401 * Clean the state. This is an internal helper function. Users should
402 * always call kfree_skb
405 void __kfree_skb(struct sk_buff *skb)
407 skb_release_all(skb);
410 EXPORT_SYMBOL(__kfree_skb);
413 * kfree_skb - free an sk_buff
414 * @skb: buffer to free
416 * Drop a reference to the buffer and free it if the usage count has
419 void kfree_skb(struct sk_buff *skb)
423 if (likely(atomic_read(&skb->users) == 1))
425 else if (likely(!atomic_dec_and_test(&skb->users)))
427 trace_kfree_skb(skb, __builtin_return_address(0));
430 EXPORT_SYMBOL(kfree_skb);
432 void kfree_skb_list(struct sk_buff *segs)
435 struct sk_buff *next = segs->next;
441 EXPORT_SYMBOL(kfree_skb_list);
444 * consume_skb - free an skbuff
445 * @skb: buffer to free
447 * Drop a ref to the buffer and free it if the usage count has hit zero
448 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
449 * is being dropped after a failure and notes that
451 void consume_skb(struct sk_buff *skb)
455 if (likely(atomic_read(&skb->users) == 1))
457 else if (likely(!atomic_dec_and_test(&skb->users)))
459 trace_consume_skb(skb);
462 EXPORT_SYMBOL(consume_skb);
465 * skb_recycle - clean up an skb for reuse
468 * Recycles the skb to be reused as a receive buffer. This
469 * function does any necessary reference count dropping, and
470 * cleans up the skbuff as if it just came from __alloc_skb().
472 void skb_recycle(struct sk_buff *skb)
474 struct skb_shared_info *shinfo;
476 skb_release_head_state(skb);
478 shinfo = skb_shinfo(skb);
479 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
480 atomic_set(&shinfo->dataref, 1);
482 memset(skb, 0, offsetof(struct sk_buff, tail));
483 skb->data = skb->head + NET_SKB_PAD;
484 skb_reset_tail_pointer(skb);
486 EXPORT_SYMBOL(skb_recycle);
489 * skb_recycle_check - check if skb can be reused for receive
491 * @skb_size: minimum receive buffer size
493 * Checks that the skb passed in is not shared or cloned, and
494 * that it is linear and its head portion at least as large as
495 * skb_size so that it can be recycled as a receive buffer.
496 * If these conditions are met, this function does any necessary
497 * reference count dropping and cleans up the skbuff as if it
498 * just came from __alloc_skb().
500 bool skb_recycle_check(struct sk_buff *skb, int skb_size)
502 if (!skb_is_recycleable(skb, skb_size))
509 EXPORT_SYMBOL(skb_recycle_check);
511 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
513 new->tstamp = old->tstamp;
515 new->transport_header = old->transport_header;
516 new->network_header = old->network_header;
517 new->mac_header = old->mac_header;
518 skb_dst_copy(new, old);
519 new->rxhash = old->rxhash;
520 new->ooo_okay = old->ooo_okay;
521 new->l4_rxhash = old->l4_rxhash;
523 new->sp = secpath_get(old->sp);
525 memcpy(new->cb, old->cb, sizeof(old->cb));
526 new->csum = old->csum;
527 new->local_df = old->local_df;
528 new->pkt_type = old->pkt_type;
529 new->ip_summed = old->ip_summed;
530 skb_copy_queue_mapping(new, old);
531 new->priority = old->priority;
532 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
533 new->ipvs_property = old->ipvs_property;
535 new->protocol = old->protocol;
536 new->mark = old->mark;
537 new->skb_iif = old->skb_iif;
539 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
540 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
541 new->nf_trace = old->nf_trace;
543 #ifdef CONFIG_NET_SCHED
544 new->tc_index = old->tc_index;
545 #ifdef CONFIG_NET_CLS_ACT
546 new->tc_verd = old->tc_verd;
549 new->vlan_tci = old->vlan_tci;
551 skb_copy_secmark(new, old);
555 * You should not add any new code to this function. Add it to
556 * __copy_skb_header above instead.
558 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
560 #define C(x) n->x = skb->x
562 n->next = n->prev = NULL;
564 __copy_skb_header(n, skb);
569 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
572 n->destructor = NULL;
578 atomic_set(&n->users, 1);
580 atomic_inc(&(skb_shinfo(skb)->dataref));
588 * skb_morph - morph one skb into another
589 * @dst: the skb to receive the contents
590 * @src: the skb to supply the contents
592 * This is identical to skb_clone except that the target skb is
593 * supplied by the user.
595 * The target skb is returned upon exit.
597 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
599 skb_release_all(dst);
600 return __skb_clone(dst, src);
602 EXPORT_SYMBOL_GPL(skb_morph);
604 /* skb_copy_ubufs - copy userspace skb frags buffers to kernel
605 * @skb: the skb to modify
606 * @gfp_mask: allocation priority
608 * This must be called on SKBTX_DEV_ZEROCOPY skb.
609 * It will copy all frags into kernel and drop the reference
610 * to userspace pages.
612 * If this function is called from an interrupt gfp_mask() must be
615 * Returns 0 on success or a negative error code on failure
616 * to allocate kernel memory to copy to.
618 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
621 int num_frags = skb_shinfo(skb)->nr_frags;
622 struct page *page, *head = NULL;
623 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
625 for (i = 0; i < num_frags; i++) {
627 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
629 page = alloc_page(GFP_ATOMIC);
632 struct page *next = (struct page *)head->private;
638 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
639 memcpy(page_address(page),
640 vaddr + f->page_offset, skb_frag_size(f));
641 kunmap_skb_frag(vaddr);
642 page->private = (unsigned long)head;
646 /* skb frags release userspace buffers */
647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
648 skb_frag_unref(skb, i);
650 uarg->callback(uarg);
652 /* skb frags point to kernel buffers */
653 for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
654 __skb_fill_page_desc(skb, i-1, head, 0,
655 skb_shinfo(skb)->frags[i - 1].size);
656 head = (struct page *)head->private;
659 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
662 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
665 * skb_clone - duplicate an sk_buff
666 * @skb: buffer to clone
667 * @gfp_mask: allocation priority
669 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
670 * copies share the same packet data but not structure. The new
671 * buffer has a reference count of 1. If the allocation fails the
672 * function returns %NULL otherwise the new buffer is returned.
674 * If this function is called from an interrupt gfp_mask() must be
678 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
682 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
683 if (skb_copy_ubufs(skb, gfp_mask))
688 if (skb->fclone == SKB_FCLONE_ORIG &&
689 n->fclone == SKB_FCLONE_UNAVAILABLE) {
690 atomic_t *fclone_ref = (atomic_t *) (n + 1);
691 n->fclone = SKB_FCLONE_CLONE;
692 atomic_inc(fclone_ref);
694 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
698 kmemcheck_annotate_bitfield(n, flags1);
699 kmemcheck_annotate_bitfield(n, flags2);
700 n->fclone = SKB_FCLONE_UNAVAILABLE;
703 return __skb_clone(n, skb);
705 EXPORT_SYMBOL(skb_clone);
707 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
709 #ifndef NET_SKBUFF_DATA_USES_OFFSET
711 * Shift between the two data areas in bytes
713 unsigned long offset = new->data - old->data;
716 __copy_skb_header(new, old);
718 #ifndef NET_SKBUFF_DATA_USES_OFFSET
719 /* {transport,network,mac}_header are relative to skb->head */
720 new->transport_header += offset;
721 new->network_header += offset;
722 if (skb_mac_header_was_set(new))
723 new->mac_header += offset;
725 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
726 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
727 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
731 * skb_copy - create private copy of an sk_buff
732 * @skb: buffer to copy
733 * @gfp_mask: allocation priority
735 * Make a copy of both an &sk_buff and its data. This is used when the
736 * caller wishes to modify the data and needs a private copy of the
737 * data to alter. Returns %NULL on failure or the pointer to the buffer
738 * on success. The returned buffer has a reference count of 1.
740 * As by-product this function converts non-linear &sk_buff to linear
741 * one, so that &sk_buff becomes completely private and caller is allowed
742 * to modify all the data of returned buffer. This means that this
743 * function is not recommended for use in circumstances when only
744 * header is going to be modified. Use pskb_copy() instead.
747 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
749 int headerlen = skb_headroom(skb);
750 unsigned int size = skb_end_offset(skb) + skb->data_len;
751 struct sk_buff *n = alloc_skb(size, gfp_mask);
756 /* Set the data pointer */
757 skb_reserve(n, headerlen);
758 /* Set the tail pointer and length */
759 skb_put(n, skb->len);
761 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
764 copy_skb_header(n, skb);
767 EXPORT_SYMBOL(skb_copy);
770 * pskb_copy - create copy of an sk_buff with private head.
771 * @skb: buffer to copy
772 * @gfp_mask: allocation priority
774 * Make a copy of both an &sk_buff and part of its data, located
775 * in header. Fragmented data remain shared. This is used when
776 * the caller wishes to modify only header of &sk_buff and needs
777 * private copy of the header to alter. Returns %NULL on failure
778 * or the pointer to the buffer on success.
779 * The returned buffer has a reference count of 1.
782 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
784 unsigned int size = skb_end_pointer(skb) - skb->head;
785 struct sk_buff *n = alloc_skb(size, gfp_mask);
790 /* Set the data pointer */
791 skb_reserve(n, skb_headroom(skb));
792 /* Set the tail pointer and length */
793 skb_put(n, skb_headlen(skb));
795 skb_copy_from_linear_data(skb, n->data, n->len);
797 n->truesize += skb->data_len;
798 n->data_len = skb->data_len;
801 if (skb_shinfo(skb)->nr_frags) {
804 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
805 if (skb_copy_ubufs(skb, gfp_mask)) {
811 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
812 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
813 skb_frag_ref(skb, i);
815 skb_shinfo(n)->nr_frags = i;
818 if (skb_has_frag_list(skb)) {
819 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
820 skb_clone_fraglist(n);
823 copy_skb_header(n, skb);
827 EXPORT_SYMBOL(pskb_copy);
830 * pskb_expand_head - reallocate header of &sk_buff
831 * @skb: buffer to reallocate
832 * @nhead: room to add at head
833 * @ntail: room to add at tail
834 * @gfp_mask: allocation priority
836 * Expands (or creates identical copy, if &nhead and &ntail are zero)
837 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
838 * reference count of 1. Returns zero in the case of success or error,
839 * if expansion failed. In the last case, &sk_buff is not changed.
841 * All the pointers pointing into skb header may change and must be
842 * reloaded after call to this function.
845 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
850 int size = nhead + skb_end_offset(skb) + ntail;
859 size = SKB_DATA_ALIGN(size);
861 /* Check if we can avoid taking references on fragments if we own
862 * the last reference on skb->head. (see skb_release_data())
867 int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
868 fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
872 size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
873 memmove(skb->head + size, skb_shinfo(skb),
874 offsetof(struct skb_shared_info,
875 frags[skb_shinfo(skb)->nr_frags]));
876 memmove(skb->head + nhead, skb->head,
877 skb_tail_pointer(skb) - skb->head);
882 data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
886 size = SKB_WITH_OVERHEAD(ksize(data));
888 /* Copy only real data... and, alas, header. This should be
889 * optimized for the cases when header is void.
891 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
893 memcpy((struct skb_shared_info *)(data + size),
895 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
900 /* copy this zero copy skb frags */
901 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
902 if (skb_copy_ubufs(skb, gfp_mask))
905 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
906 skb_frag_ref(skb, i);
908 if (skb_has_frag_list(skb))
909 skb_clone_fraglist(skb);
911 skb_release_data(skb);
913 off = (data + nhead) - skb->head;
918 #ifdef NET_SKBUFF_DATA_USES_OFFSET
922 skb->end = skb->head + size;
924 /* {transport,network,mac}_header and tail are relative to skb->head */
926 skb->transport_header += off;
927 skb->network_header += off;
928 if (skb_mac_header_was_set(skb))
929 skb->mac_header += off;
930 /* Only adjust this if it actually is csum_start rather than csum */
931 if (skb->ip_summed == CHECKSUM_PARTIAL)
932 skb->csum_start += nhead;
936 atomic_set(&skb_shinfo(skb)->dataref, 1);
944 EXPORT_SYMBOL(pskb_expand_head);
946 /* Make private copy of skb with writable head and some headroom */
948 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
950 struct sk_buff *skb2;
951 int delta = headroom - skb_headroom(skb);
954 skb2 = pskb_copy(skb, GFP_ATOMIC);
956 skb2 = skb_clone(skb, GFP_ATOMIC);
957 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
965 EXPORT_SYMBOL(skb_realloc_headroom);
968 * skb_copy_expand - copy and expand sk_buff
969 * @skb: buffer to copy
970 * @newheadroom: new free bytes at head
971 * @newtailroom: new free bytes at tail
972 * @gfp_mask: allocation priority
974 * Make a copy of both an &sk_buff and its data and while doing so
975 * allocate additional space.
977 * This is used when the caller wishes to modify the data and needs a
978 * private copy of the data to alter as well as more space for new fields.
979 * Returns %NULL on failure or the pointer to the buffer
980 * on success. The returned buffer has a reference count of 1.
982 * You must pass %GFP_ATOMIC as the allocation priority if this function
983 * is called from an interrupt.
985 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
986 int newheadroom, int newtailroom,
990 * Allocate the copy buffer
992 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
994 int oldheadroom = skb_headroom(skb);
995 int head_copy_len, head_copy_off;
1001 skb_reserve(n, newheadroom);
1003 /* Set the tail pointer and length */
1004 skb_put(n, skb->len);
1006 head_copy_len = oldheadroom;
1008 if (newheadroom <= head_copy_len)
1009 head_copy_len = newheadroom;
1011 head_copy_off = newheadroom - head_copy_len;
1013 /* Copy the linear header and data. */
1014 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1015 skb->len + head_copy_len))
1018 copy_skb_header(n, skb);
1020 off = newheadroom - oldheadroom;
1021 if (n->ip_summed == CHECKSUM_PARTIAL)
1022 n->csum_start += off;
1023 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1024 n->transport_header += off;
1025 n->network_header += off;
1026 if (skb_mac_header_was_set(skb))
1027 n->mac_header += off;
1032 EXPORT_SYMBOL(skb_copy_expand);
1035 * skb_pad - zero pad the tail of an skb
1036 * @skb: buffer to pad
1037 * @pad: space to pad
1039 * Ensure that a buffer is followed by a padding area that is zero
1040 * filled. Used by network drivers which may DMA or transfer data
1041 * beyond the buffer end onto the wire.
1043 * May return error in out of memory cases. The skb is freed on error.
1046 int skb_pad(struct sk_buff *skb, int pad)
1051 /* If the skbuff is non linear tailroom is always zero.. */
1052 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1053 memset(skb->data+skb->len, 0, pad);
1057 ntail = skb->data_len + pad - (skb->end - skb->tail);
1058 if (likely(skb_cloned(skb) || ntail > 0)) {
1059 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1064 /* FIXME: The use of this function with non-linear skb's really needs
1067 err = skb_linearize(skb);
1071 memset(skb->data + skb->len, 0, pad);
1078 EXPORT_SYMBOL(skb_pad);
1081 * skb_put - add data to a buffer
1082 * @skb: buffer to use
1083 * @len: amount of data to add
1085 * This function extends the used data area of the buffer. If this would
1086 * exceed the total buffer size the kernel will panic. A pointer to the
1087 * first byte of the extra data is returned.
1089 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1091 unsigned char *tmp = skb_tail_pointer(skb);
1092 SKB_LINEAR_ASSERT(skb);
1095 if (unlikely(skb->tail > skb->end))
1096 skb_over_panic(skb, len, __builtin_return_address(0));
1099 EXPORT_SYMBOL(skb_put);
1102 * skb_push - add data to the start of a buffer
1103 * @skb: buffer to use
1104 * @len: amount of data to add
1106 * This function extends the used data area of the buffer at the buffer
1107 * start. If this would exceed the total buffer headroom the kernel will
1108 * panic. A pointer to the first byte of the extra data is returned.
1110 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1114 if (unlikely(skb->data<skb->head))
1115 skb_under_panic(skb, len, __builtin_return_address(0));
1118 EXPORT_SYMBOL(skb_push);
1121 * skb_pull - remove data from the start of a buffer
1122 * @skb: buffer to use
1123 * @len: amount of data to remove
1125 * This function removes data from the start of a buffer, returning
1126 * the memory to the headroom. A pointer to the next data in the buffer
1127 * is returned. Once the data has been pulled future pushes will overwrite
1130 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1132 return skb_pull_inline(skb, len);
1134 EXPORT_SYMBOL(skb_pull);
1137 * skb_trim - remove end from a buffer
1138 * @skb: buffer to alter
1141 * Cut the length of a buffer down by removing data from the tail. If
1142 * the buffer is already under the length specified it is not modified.
1143 * The skb must be linear.
1145 void skb_trim(struct sk_buff *skb, unsigned int len)
1148 __skb_trim(skb, len);
1150 EXPORT_SYMBOL(skb_trim);
1152 /* Trims skb to length len. It can change skb pointers.
1155 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1157 struct sk_buff **fragp;
1158 struct sk_buff *frag;
1159 int offset = skb_headlen(skb);
1160 int nfrags = skb_shinfo(skb)->nr_frags;
1164 if (skb_cloned(skb) &&
1165 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1172 for (; i < nfrags; i++) {
1173 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1180 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1183 skb_shinfo(skb)->nr_frags = i;
1185 for (; i < nfrags; i++)
1186 skb_frag_unref(skb, i);
1188 if (skb_has_frag_list(skb))
1189 skb_drop_fraglist(skb);
1193 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1194 fragp = &frag->next) {
1195 int end = offset + frag->len;
1197 if (skb_shared(frag)) {
1198 struct sk_buff *nfrag;
1200 nfrag = skb_clone(frag, GFP_ATOMIC);
1201 if (unlikely(!nfrag))
1204 nfrag->next = frag->next;
1216 unlikely((err = pskb_trim(frag, len - offset))))
1220 skb_drop_list(&frag->next);
1225 if (len > skb_headlen(skb)) {
1226 skb->data_len -= skb->len - len;
1231 skb_set_tail_pointer(skb, len);
1236 EXPORT_SYMBOL(___pskb_trim);
1239 * __pskb_pull_tail - advance tail of skb header
1240 * @skb: buffer to reallocate
1241 * @delta: number of bytes to advance tail
1243 * The function makes a sense only on a fragmented &sk_buff,
1244 * it expands header moving its tail forward and copying necessary
1245 * data from fragmented part.
1247 * &sk_buff MUST have reference count of 1.
1249 * Returns %NULL (and &sk_buff does not change) if pull failed
1250 * or value of new tail of skb in the case of success.
1252 * All the pointers pointing into skb header may change and must be
1253 * reloaded after call to this function.
1256 /* Moves tail of skb head forward, copying data from fragmented part,
1257 * when it is necessary.
1258 * 1. It may fail due to malloc failure.
1259 * 2. It may change skb pointers.
1261 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1263 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1265 /* If skb has not enough free space at tail, get new one
1266 * plus 128 bytes for future expansions. If we have enough
1267 * room at tail, reallocate without expansion only if skb is cloned.
1269 int i, k, eat = (skb->tail + delta) - skb->end;
1271 if (eat > 0 || skb_cloned(skb)) {
1272 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1277 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1280 /* Optimization: no fragments, no reasons to preestimate
1281 * size of pulled pages. Superb.
1283 if (!skb_has_frag_list(skb))
1286 /* Estimate size of pulled pages. */
1288 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1289 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1296 /* If we need update frag list, we are in troubles.
1297 * Certainly, it possible to add an offset to skb data,
1298 * but taking into account that pulling is expected to
1299 * be very rare operation, it is worth to fight against
1300 * further bloating skb head and crucify ourselves here instead.
1301 * Pure masohism, indeed. 8)8)
1304 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1305 struct sk_buff *clone = NULL;
1306 struct sk_buff *insp = NULL;
1311 if (list->len <= eat) {
1312 /* Eaten as whole. */
1317 /* Eaten partially. */
1319 if (skb_shared(list)) {
1320 /* Sucks! We need to fork list. :-( */
1321 clone = skb_clone(list, GFP_ATOMIC);
1327 /* This may be pulled without
1331 if (!pskb_pull(list, eat)) {
1339 /* Free pulled out fragments. */
1340 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1341 skb_shinfo(skb)->frag_list = list->next;
1344 /* And insert new clone at head. */
1347 skb_shinfo(skb)->frag_list = clone;
1350 /* Success! Now we may commit changes to skb data. */
1355 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1356 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1359 skb_frag_unref(skb, i);
1362 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1364 skb_shinfo(skb)->frags[k].page_offset += eat;
1365 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1371 skb_shinfo(skb)->nr_frags = k;
1374 skb->data_len -= delta;
1376 return skb_tail_pointer(skb);
1378 EXPORT_SYMBOL(__pskb_pull_tail);
1381 * skb_copy_bits - copy bits from skb to kernel buffer
1383 * @offset: offset in source
1384 * @to: destination buffer
1385 * @len: number of bytes to copy
1387 * Copy the specified number of bytes from the source skb to the
1388 * destination buffer.
1391 * If its prototype is ever changed,
1392 * check arch/{*}/net/{*}.S files,
1393 * since it is called from BPF assembly code.
1395 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1397 int start = skb_headlen(skb);
1398 struct sk_buff *frag_iter;
1401 if (offset > (int)skb->len - len)
1405 if ((copy = start - offset) > 0) {
1408 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1409 if ((len -= copy) == 0)
1415 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1418 WARN_ON(start > offset + len);
1420 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1421 if ((copy = end - offset) > 0) {
1427 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1429 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1430 offset - start, copy);
1431 kunmap_skb_frag(vaddr);
1433 if ((len -= copy) == 0)
1441 skb_walk_frags(skb, frag_iter) {
1444 WARN_ON(start > offset + len);
1446 end = start + frag_iter->len;
1447 if ((copy = end - offset) > 0) {
1450 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1452 if ((len -= copy) == 0)
1466 EXPORT_SYMBOL(skb_copy_bits);
1469 * Callback from splice_to_pipe(), if we need to release some pages
1470 * at the end of the spd in case we error'ed out in filling the pipe.
1472 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1474 put_page(spd->pages[i]);
1477 static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1478 unsigned int *offset,
1479 struct sk_buff *skb, struct sock *sk)
1481 struct page *p = sk->sk_sndmsg_page;
1486 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1490 off = sk->sk_sndmsg_off = 0;
1491 /* hold one ref to this page until it's full */
1495 off = sk->sk_sndmsg_off;
1496 mlen = PAGE_SIZE - off;
1497 if (mlen < 64 && mlen < *len) {
1502 *len = min_t(unsigned int, *len, mlen);
1505 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1506 sk->sk_sndmsg_off += *len;
1514 * Fill page/offset/length into spd, if it can hold more pages.
1516 static inline int spd_fill_page(struct splice_pipe_desc *spd,
1517 struct pipe_inode_info *pipe, struct page *page,
1518 unsigned int *len, unsigned int offset,
1519 struct sk_buff *skb, int linear,
1522 if (unlikely(spd->nr_pages == pipe->buffers))
1526 page = linear_to_page(page, len, &offset, skb, sk);
1532 spd->pages[spd->nr_pages] = page;
1533 spd->partial[spd->nr_pages].len = *len;
1534 spd->partial[spd->nr_pages].offset = offset;
1540 static inline void __segment_seek(struct page **page, unsigned int *poff,
1541 unsigned int *plen, unsigned int off)
1546 n = *poff / PAGE_SIZE;
1548 *page = nth_page(*page, n);
1550 *poff = *poff % PAGE_SIZE;
1554 static inline int __splice_segment(struct page *page, unsigned int poff,
1555 unsigned int plen, unsigned int *off,
1556 unsigned int *len, struct sk_buff *skb,
1557 struct splice_pipe_desc *spd, int linear,
1559 struct pipe_inode_info *pipe)
1564 /* skip this segment if already processed */
1570 /* ignore any bits we already processed */
1572 __segment_seek(&page, &poff, &plen, *off);
1577 unsigned int flen = min(*len, plen);
1579 /* the linear region may spread across several pages */
1580 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1582 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1585 __segment_seek(&page, &poff, &plen, flen);
1588 } while (*len && plen);
1594 * Map linear and fragment data from the skb to spd. It reports failure if the
1595 * pipe is full or if we already spliced the requested length.
1597 static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1598 unsigned int *offset, unsigned int *len,
1599 struct splice_pipe_desc *spd, struct sock *sk)
1604 * map the linear part
1606 if (__splice_segment(virt_to_page(skb->data),
1607 (unsigned long) skb->data & (PAGE_SIZE - 1),
1609 offset, len, skb, spd, 1, sk, pipe))
1613 * then map the fragments
1615 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1616 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1618 if (__splice_segment(skb_frag_page(f),
1619 f->page_offset, skb_frag_size(f),
1620 offset, len, skb, spd, 0, sk, pipe))
1628 * Map data from the skb to a pipe. Should handle both the linear part,
1629 * the fragments, and the frag list. It does NOT handle frag lists within
1630 * the frag list, if such a thing exists. We'd probably need to recurse to
1631 * handle that cleanly.
1633 int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1634 struct pipe_inode_info *pipe, unsigned int tlen,
1637 struct partial_page partial[PIPE_DEF_BUFFERS];
1638 struct page *pages[PIPE_DEF_BUFFERS];
1639 struct splice_pipe_desc spd = {
1642 .nr_pages_max = MAX_SKB_FRAGS,
1644 .ops = &nosteal_pipe_buf_ops,
1645 .spd_release = sock_spd_release,
1647 struct sk_buff *frag_iter;
1648 struct sock *sk = skb->sk;
1651 if (splice_grow_spd(pipe, &spd))
1655 * __skb_splice_bits() only fails if the output has no room left,
1656 * so no point in going over the frag_list for the error case.
1658 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1664 * now see if we have a frag_list to map
1666 skb_walk_frags(skb, frag_iter) {
1669 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1676 * Drop the socket lock, otherwise we have reverse
1677 * locking dependencies between sk_lock and i_mutex
1678 * here as compared to sendfile(). We enter here
1679 * with the socket lock held, and splice_to_pipe() will
1680 * grab the pipe inode lock. For sendfile() emulation,
1681 * we call into ->sendpage() with the i_mutex lock held
1682 * and networking will grab the socket lock.
1685 ret = splice_to_pipe(pipe, &spd);
1689 splice_shrink_spd(&spd);
1694 * skb_store_bits - store bits from kernel buffer to skb
1695 * @skb: destination buffer
1696 * @offset: offset in destination
1697 * @from: source buffer
1698 * @len: number of bytes to copy
1700 * Copy the specified number of bytes from the source buffer to the
1701 * destination skb. This function handles all the messy bits of
1702 * traversing fragment lists and such.
1705 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1707 int start = skb_headlen(skb);
1708 struct sk_buff *frag_iter;
1711 if (offset > (int)skb->len - len)
1714 if ((copy = start - offset) > 0) {
1717 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1718 if ((len -= copy) == 0)
1724 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1725 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1728 WARN_ON(start > offset + len);
1730 end = start + skb_frag_size(frag);
1731 if ((copy = end - offset) > 0) {
1737 vaddr = kmap_skb_frag(frag);
1738 memcpy(vaddr + frag->page_offset + offset - start,
1740 kunmap_skb_frag(vaddr);
1742 if ((len -= copy) == 0)
1750 skb_walk_frags(skb, frag_iter) {
1753 WARN_ON(start > offset + len);
1755 end = start + frag_iter->len;
1756 if ((copy = end - offset) > 0) {
1759 if (skb_store_bits(frag_iter, offset - start,
1762 if ((len -= copy) == 0)
1775 EXPORT_SYMBOL(skb_store_bits);
1777 /* Checksum skb data. */
1779 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1780 int len, __wsum csum)
1782 int start = skb_headlen(skb);
1783 int i, copy = start - offset;
1784 struct sk_buff *frag_iter;
1787 /* Checksum header. */
1791 csum = csum_partial(skb->data + offset, copy, csum);
1792 if ((len -= copy) == 0)
1798 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1801 WARN_ON(start > offset + len);
1803 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1804 if ((copy = end - offset) > 0) {
1807 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1811 vaddr = kmap_skb_frag(frag);
1812 csum2 = csum_partial(vaddr + frag->page_offset +
1813 offset - start, copy, 0);
1814 kunmap_skb_frag(vaddr);
1815 csum = csum_block_add(csum, csum2, pos);
1824 skb_walk_frags(skb, frag_iter) {
1827 WARN_ON(start > offset + len);
1829 end = start + frag_iter->len;
1830 if ((copy = end - offset) > 0) {
1834 csum2 = skb_checksum(frag_iter, offset - start,
1836 csum = csum_block_add(csum, csum2, pos);
1837 if ((len -= copy) == 0)
1848 EXPORT_SYMBOL(skb_checksum);
1850 /* Both of above in one bottle. */
1852 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1853 u8 *to, int len, __wsum csum)
1855 int start = skb_headlen(skb);
1856 int i, copy = start - offset;
1857 struct sk_buff *frag_iter;
1864 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1866 if ((len -= copy) == 0)
1873 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1876 WARN_ON(start > offset + len);
1878 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1879 if ((copy = end - offset) > 0) {
1882 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1886 vaddr = kmap_skb_frag(frag);
1887 csum2 = csum_partial_copy_nocheck(vaddr +
1891 kunmap_skb_frag(vaddr);
1892 csum = csum_block_add(csum, csum2, pos);
1902 skb_walk_frags(skb, frag_iter) {
1906 WARN_ON(start > offset + len);
1908 end = start + frag_iter->len;
1909 if ((copy = end - offset) > 0) {
1912 csum2 = skb_copy_and_csum_bits(frag_iter,
1915 csum = csum_block_add(csum, csum2, pos);
1916 if ((len -= copy) == 0)
1927 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1929 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1934 if (skb->ip_summed == CHECKSUM_PARTIAL)
1935 csstart = skb_checksum_start_offset(skb);
1937 csstart = skb_headlen(skb);
1939 BUG_ON(csstart > skb_headlen(skb));
1941 skb_copy_from_linear_data(skb, to, csstart);
1944 if (csstart != skb->len)
1945 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1946 skb->len - csstart, 0);
1948 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1949 long csstuff = csstart + skb->csum_offset;
1951 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1954 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1957 * skb_dequeue - remove from the head of the queue
1958 * @list: list to dequeue from
1960 * Remove the head of the list. The list lock is taken so the function
1961 * may be used safely with other locking list functions. The head item is
1962 * returned or %NULL if the list is empty.
1965 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1967 unsigned long flags;
1968 struct sk_buff *result;
1970 spin_lock_irqsave(&list->lock, flags);
1971 result = __skb_dequeue(list);
1972 spin_unlock_irqrestore(&list->lock, flags);
1975 EXPORT_SYMBOL(skb_dequeue);
1978 * skb_dequeue_tail - remove from the tail of the queue
1979 * @list: list to dequeue from
1981 * Remove the tail of the list. The list lock is taken so the function
1982 * may be used safely with other locking list functions. The tail item is
1983 * returned or %NULL if the list is empty.
1985 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1987 unsigned long flags;
1988 struct sk_buff *result;
1990 spin_lock_irqsave(&list->lock, flags);
1991 result = __skb_dequeue_tail(list);
1992 spin_unlock_irqrestore(&list->lock, flags);
1995 EXPORT_SYMBOL(skb_dequeue_tail);
1998 * skb_queue_purge - empty a list
1999 * @list: list to empty
2001 * Delete all buffers on an &sk_buff list. Each buffer is removed from
2002 * the list and one reference dropped. This function takes the list
2003 * lock and is atomic with respect to other list locking functions.
2005 void skb_queue_purge(struct sk_buff_head *list)
2007 struct sk_buff *skb;
2008 while ((skb = skb_dequeue(list)) != NULL)
2011 EXPORT_SYMBOL(skb_queue_purge);
2014 * skb_queue_head - queue a buffer at the list head
2015 * @list: list to use
2016 * @newsk: buffer to queue
2018 * Queue a buffer at the start of the list. This function takes the
2019 * list lock and can be used safely with other locking &sk_buff functions
2022 * A buffer cannot be placed on two lists at the same time.
2024 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2026 unsigned long flags;
2028 spin_lock_irqsave(&list->lock, flags);
2029 __skb_queue_head(list, newsk);
2030 spin_unlock_irqrestore(&list->lock, flags);
2032 EXPORT_SYMBOL(skb_queue_head);
2035 * skb_queue_tail - queue a buffer at the list tail
2036 * @list: list to use
2037 * @newsk: buffer to queue
2039 * Queue a buffer at the tail of the list. This function takes the
2040 * list lock and can be used safely with other locking &sk_buff functions
2043 * A buffer cannot be placed on two lists at the same time.
2045 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2047 unsigned long flags;
2049 spin_lock_irqsave(&list->lock, flags);
2050 __skb_queue_tail(list, newsk);
2051 spin_unlock_irqrestore(&list->lock, flags);
2053 EXPORT_SYMBOL(skb_queue_tail);
2056 * skb_unlink - remove a buffer from a list
2057 * @skb: buffer to remove
2058 * @list: list to use
2060 * Remove a packet from a list. The list locks are taken and this
2061 * function is atomic with respect to other list locked calls
2063 * You must know what list the SKB is on.
2065 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2067 unsigned long flags;
2069 spin_lock_irqsave(&list->lock, flags);
2070 __skb_unlink(skb, list);
2071 spin_unlock_irqrestore(&list->lock, flags);
2073 EXPORT_SYMBOL(skb_unlink);
2076 * skb_append - append a buffer
2077 * @old: buffer to insert after
2078 * @newsk: buffer to insert
2079 * @list: list to use
2081 * Place a packet after a given packet in a list. The list locks are taken
2082 * and this function is atomic with respect to other list locked calls.
2083 * A buffer cannot be placed on two lists at the same time.
2085 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2087 unsigned long flags;
2089 spin_lock_irqsave(&list->lock, flags);
2090 __skb_queue_after(list, old, newsk);
2091 spin_unlock_irqrestore(&list->lock, flags);
2093 EXPORT_SYMBOL(skb_append);
2096 * skb_insert - insert a buffer
2097 * @old: buffer to insert before
2098 * @newsk: buffer to insert
2099 * @list: list to use
2101 * Place a packet before a given packet in a list. The list locks are
2102 * taken and this function is atomic with respect to other list locked
2105 * A buffer cannot be placed on two lists at the same time.
2107 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2109 unsigned long flags;
2111 spin_lock_irqsave(&list->lock, flags);
2112 __skb_insert(newsk, old->prev, old, list);
2113 spin_unlock_irqrestore(&list->lock, flags);
2115 EXPORT_SYMBOL(skb_insert);
2117 static inline void skb_split_inside_header(struct sk_buff *skb,
2118 struct sk_buff* skb1,
2119 const u32 len, const int pos)
2123 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2125 /* And move data appendix as is. */
2126 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2127 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2129 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2130 skb_shinfo(skb)->nr_frags = 0;
2131 skb1->data_len = skb->data_len;
2132 skb1->len += skb1->data_len;
2135 skb_set_tail_pointer(skb, len);
2138 static inline void skb_split_no_header(struct sk_buff *skb,
2139 struct sk_buff* skb1,
2140 const u32 len, int pos)
2143 const int nfrags = skb_shinfo(skb)->nr_frags;
2145 skb_shinfo(skb)->nr_frags = 0;
2146 skb1->len = skb1->data_len = skb->len - len;
2148 skb->data_len = len - pos;
2150 for (i = 0; i < nfrags; i++) {
2151 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2153 if (pos + size > len) {
2154 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2158 * We have two variants in this case:
2159 * 1. Move all the frag to the second
2160 * part, if it is possible. F.e.
2161 * this approach is mandatory for TUX,
2162 * where splitting is expensive.
2163 * 2. Split is accurately. We make this.
2165 skb_frag_ref(skb, i);
2166 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2167 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2168 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2169 skb_shinfo(skb)->nr_frags++;
2173 skb_shinfo(skb)->nr_frags++;
2176 skb_shinfo(skb1)->nr_frags = k;
2180 * skb_split - Split fragmented skb to two parts at length len.
2181 * @skb: the buffer to split
2182 * @skb1: the buffer to receive the second part
2183 * @len: new length for skb
2185 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2187 int pos = skb_headlen(skb);
2189 if (len < pos) /* Split line is inside header. */
2190 skb_split_inside_header(skb, skb1, len, pos);
2191 else /* Second chunk has no header, nothing to copy. */
2192 skb_split_no_header(skb, skb1, len, pos);
2194 EXPORT_SYMBOL(skb_split);
2196 /* Shifting from/to a cloned skb is a no-go.
2198 * Caller cannot keep skb_shinfo related pointers past calling here!
2200 static int skb_prepare_for_shift(struct sk_buff *skb)
2202 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2206 * skb_shift - Shifts paged data partially from skb to another
2207 * @tgt: buffer into which tail data gets added
2208 * @skb: buffer from which the paged data comes from
2209 * @shiftlen: shift up to this many bytes
2211 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2212 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2213 * It's up to caller to free skb if everything was shifted.
2215 * If @tgt runs out of frags, the whole operation is aborted.
2217 * Skb cannot include anything else but paged data while tgt is allowed
2218 * to have non-paged data as well.
2220 * TODO: full sized shift could be optimized but that would need
2221 * specialized skb free'er to handle frags without up-to-date nr_frags.
2223 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2225 int from, to, merge, todo;
2226 struct skb_frag_struct *fragfrom, *fragto;
2228 BUG_ON(shiftlen > skb->len);
2229 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2233 to = skb_shinfo(tgt)->nr_frags;
2234 fragfrom = &skb_shinfo(skb)->frags[from];
2236 /* Actual merge is delayed until the point when we know we can
2237 * commit all, so that we don't have to undo partial changes
2240 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2241 fragfrom->page_offset)) {
2246 todo -= skb_frag_size(fragfrom);
2248 if (skb_prepare_for_shift(skb) ||
2249 skb_prepare_for_shift(tgt))
2252 /* All previous frag pointers might be stale! */
2253 fragfrom = &skb_shinfo(skb)->frags[from];
2254 fragto = &skb_shinfo(tgt)->frags[merge];
2256 skb_frag_size_add(fragto, shiftlen);
2257 skb_frag_size_sub(fragfrom, shiftlen);
2258 fragfrom->page_offset += shiftlen;
2266 /* Skip full, not-fitting skb to avoid expensive operations */
2267 if ((shiftlen == skb->len) &&
2268 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2271 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2274 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2275 if (to == MAX_SKB_FRAGS)
2278 fragfrom = &skb_shinfo(skb)->frags[from];
2279 fragto = &skb_shinfo(tgt)->frags[to];
2281 if (todo >= skb_frag_size(fragfrom)) {
2282 *fragto = *fragfrom;
2283 todo -= skb_frag_size(fragfrom);
2288 __skb_frag_ref(fragfrom);
2289 fragto->page = fragfrom->page;
2290 fragto->page_offset = fragfrom->page_offset;
2291 skb_frag_size_set(fragto, todo);
2293 fragfrom->page_offset += todo;
2294 skb_frag_size_sub(fragfrom, todo);
2302 /* Ready to "commit" this state change to tgt */
2303 skb_shinfo(tgt)->nr_frags = to;
2306 fragfrom = &skb_shinfo(skb)->frags[0];
2307 fragto = &skb_shinfo(tgt)->frags[merge];
2309 skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2310 __skb_frag_unref(fragfrom);
2313 /* Reposition in the original skb */
2315 while (from < skb_shinfo(skb)->nr_frags)
2316 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2317 skb_shinfo(skb)->nr_frags = to;
2319 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2322 /* Most likely the tgt won't ever need its checksum anymore, skb on
2323 * the other hand might need it if it needs to be resent
2325 tgt->ip_summed = CHECKSUM_PARTIAL;
2326 skb->ip_summed = CHECKSUM_PARTIAL;
2328 /* Yak, is it really working this way? Some helper please? */
2329 skb->len -= shiftlen;
2330 skb->data_len -= shiftlen;
2331 skb->truesize -= shiftlen;
2332 tgt->len += shiftlen;
2333 tgt->data_len += shiftlen;
2334 tgt->truesize += shiftlen;
2340 * skb_prepare_seq_read - Prepare a sequential read of skb data
2341 * @skb: the buffer to read
2342 * @from: lower offset of data to be read
2343 * @to: upper offset of data to be read
2344 * @st: state variable
2346 * Initializes the specified state variable. Must be called before
2347 * invoking skb_seq_read() for the first time.
2349 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2350 unsigned int to, struct skb_seq_state *st)
2352 st->lower_offset = from;
2353 st->upper_offset = to;
2354 st->root_skb = st->cur_skb = skb;
2355 st->frag_idx = st->stepped_offset = 0;
2356 st->frag_data = NULL;
2358 EXPORT_SYMBOL(skb_prepare_seq_read);
2361 * skb_seq_read - Sequentially read skb data
2362 * @consumed: number of bytes consumed by the caller so far
2363 * @data: destination pointer for data to be returned
2364 * @st: state variable
2366 * Reads a block of skb data at &consumed relative to the
2367 * lower offset specified to skb_prepare_seq_read(). Assigns
2368 * the head of the data block to &data and returns the length
2369 * of the block or 0 if the end of the skb data or the upper
2370 * offset has been reached.
2372 * The caller is not required to consume all of the data
2373 * returned, i.e. &consumed is typically set to the number
2374 * of bytes already consumed and the next call to
2375 * skb_seq_read() will return the remaining part of the block.
2377 * Note 1: The size of each block of data returned can be arbitrary,
2378 * this limitation is the cost for zerocopy seqeuental
2379 * reads of potentially non linear data.
2381 * Note 2: Fragment lists within fragments are not implemented
2382 * at the moment, state->root_skb could be replaced with
2383 * a stack for this purpose.
2385 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2386 struct skb_seq_state *st)
2388 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2391 if (unlikely(abs_offset >= st->upper_offset))
2395 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2397 if (abs_offset < block_limit && !st->frag_data) {
2398 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2399 return block_limit - abs_offset;
2402 if (st->frag_idx == 0 && !st->frag_data)
2403 st->stepped_offset += skb_headlen(st->cur_skb);
2405 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2406 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2407 block_limit = skb_frag_size(frag) + st->stepped_offset;
2409 if (abs_offset < block_limit) {
2411 st->frag_data = kmap_skb_frag(frag);
2413 *data = (u8 *) st->frag_data + frag->page_offset +
2414 (abs_offset - st->stepped_offset);
2416 return block_limit - abs_offset;
2419 if (st->frag_data) {
2420 kunmap_skb_frag(st->frag_data);
2421 st->frag_data = NULL;
2425 st->stepped_offset += skb_frag_size(frag);
2428 if (st->frag_data) {
2429 kunmap_skb_frag(st->frag_data);
2430 st->frag_data = NULL;
2433 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2434 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2437 } else if (st->cur_skb->next) {
2438 st->cur_skb = st->cur_skb->next;
2445 EXPORT_SYMBOL(skb_seq_read);
2448 * skb_abort_seq_read - Abort a sequential read of skb data
2449 * @st: state variable
2451 * Must be called if skb_seq_read() was not called until it
2454 void skb_abort_seq_read(struct skb_seq_state *st)
2457 kunmap_skb_frag(st->frag_data);
2459 EXPORT_SYMBOL(skb_abort_seq_read);
2461 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2463 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2464 struct ts_config *conf,
2465 struct ts_state *state)
2467 return skb_seq_read(offset, text, TS_SKB_CB(state));
2470 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2472 skb_abort_seq_read(TS_SKB_CB(state));
2476 * skb_find_text - Find a text pattern in skb data
2477 * @skb: the buffer to look in
2478 * @from: search offset
2480 * @config: textsearch configuration
2481 * @state: uninitialized textsearch state variable
2483 * Finds a pattern in the skb data according to the specified
2484 * textsearch configuration. Use textsearch_next() to retrieve
2485 * subsequent occurrences of the pattern. Returns the offset
2486 * to the first occurrence or UINT_MAX if no match was found.
2488 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2489 unsigned int to, struct ts_config *config,
2490 struct ts_state *state)
2494 config->get_next_block = skb_ts_get_next_block;
2495 config->finish = skb_ts_finish;
2497 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2499 ret = textsearch_find(config, state);
2500 return (ret <= to - from ? ret : UINT_MAX);
2502 EXPORT_SYMBOL(skb_find_text);
2505 * skb_append_datato_frags: - append the user data to a skb
2506 * @sk: sock structure
2507 * @skb: skb structure to be appened with user data.
2508 * @getfrag: call back function to be used for getting the user data
2509 * @from: pointer to user message iov
2510 * @length: length of the iov message
2512 * Description: This procedure append the user data in the fragment part
2513 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2515 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2516 int (*getfrag)(void *from, char *to, int offset,
2517 int len, int odd, struct sk_buff *skb),
2518 void *from, int length)
2521 skb_frag_t *frag = NULL;
2522 struct page *page = NULL;
2528 /* Return error if we don't have space for new frag */
2529 frg_cnt = skb_shinfo(skb)->nr_frags;
2530 if (frg_cnt >= MAX_SKB_FRAGS)
2533 /* allocate a new page for next frag */
2534 page = alloc_pages(sk->sk_allocation, 0);
2536 /* If alloc_page fails just return failure and caller will
2537 * free previous allocated pages by doing kfree_skb()
2542 /* initialize the next frag */
2543 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2544 skb->truesize += PAGE_SIZE;
2545 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2547 /* get the new initialized frag */
2548 frg_cnt = skb_shinfo(skb)->nr_frags;
2549 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2551 /* copy the user data to page */
2552 left = PAGE_SIZE - frag->page_offset;
2553 copy = (length > left)? left : length;
2555 ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2556 offset, copy, 0, skb);
2560 /* copy was successful so update the size parameters */
2561 skb_frag_size_add(frag, copy);
2563 skb->data_len += copy;
2567 } while (length > 0);
2571 EXPORT_SYMBOL(skb_append_datato_frags);
2574 * skb_pull_rcsum - pull skb and update receive checksum
2575 * @skb: buffer to update
2576 * @len: length of data pulled
2578 * This function performs an skb_pull on the packet and updates
2579 * the CHECKSUM_COMPLETE checksum. It should be used on
2580 * receive path processing instead of skb_pull unless you know
2581 * that the checksum difference is zero (e.g., a valid IP header)
2582 * or you are setting ip_summed to CHECKSUM_NONE.
2584 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2586 unsigned char *data = skb->data;
2588 BUG_ON(len > skb->len);
2589 __skb_pull(skb, len);
2590 skb_postpull_rcsum(skb, data, len);
2593 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2596 * skb_segment - Perform protocol segmentation on skb.
2597 * @skb: buffer to segment
2598 * @features: features for the output path (see dev->features)
2600 * This function performs segmentation on the given skb. It returns
2601 * a pointer to the first in a list of new skbs for the segments.
2602 * In case of error it returns ERR_PTR(err).
2604 struct sk_buff *skb_segment(struct sk_buff *skb, u32 features)
2606 struct sk_buff *segs = NULL;
2607 struct sk_buff *tail = NULL;
2608 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2609 unsigned int mss = skb_shinfo(skb)->gso_size;
2610 unsigned int doffset = skb->data - skb_mac_header(skb);
2611 unsigned int offset = doffset;
2612 unsigned int headroom;
2614 int sg = !!(features & NETIF_F_SG);
2615 int nfrags = skb_shinfo(skb)->nr_frags;
2620 __skb_push(skb, doffset);
2621 headroom = skb_headroom(skb);
2622 pos = skb_headlen(skb);
2625 struct sk_buff *nskb;
2630 len = skb->len - offset;
2634 hsize = skb_headlen(skb) - offset;
2637 if (hsize > len || !sg)
2640 if (!hsize && i >= nfrags) {
2641 BUG_ON(fskb->len != len);
2644 nskb = skb_clone(fskb, GFP_ATOMIC);
2647 if (unlikely(!nskb))
2650 hsize = skb_end_offset(nskb);
2651 if (skb_cow_head(nskb, doffset + headroom)) {
2656 nskb->truesize += skb_end_offset(nskb) - hsize;
2657 skb_release_head_state(nskb);
2658 __skb_push(nskb, doffset);
2660 nskb = alloc_skb(hsize + doffset + headroom,
2663 if (unlikely(!nskb))
2666 skb_reserve(nskb, headroom);
2667 __skb_put(nskb, doffset);
2676 __copy_skb_header(nskb, skb);
2677 nskb->mac_len = skb->mac_len;
2679 /* nskb and skb might have different headroom */
2680 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2681 nskb->csum_start += skb_headroom(nskb) - headroom;
2683 skb_reset_mac_header(nskb);
2684 skb_set_network_header(nskb, skb->mac_len);
2685 nskb->transport_header = (nskb->network_header +
2686 skb_network_header_len(skb));
2687 skb_copy_from_linear_data(skb, nskb->data, doffset);
2689 if (fskb != skb_shinfo(skb)->frag_list)
2693 nskb->ip_summed = CHECKSUM_NONE;
2694 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2700 frag = skb_shinfo(nskb)->frags;
2702 skb_copy_from_linear_data_offset(skb, offset,
2703 skb_put(nskb, hsize), hsize);
2705 while (pos < offset + len && i < nfrags) {
2706 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
2709 *frag = skb_shinfo(skb)->frags[i];
2710 __skb_frag_ref(frag);
2711 size = skb_frag_size(frag);
2714 frag->page_offset += offset - pos;
2715 skb_frag_size_sub(frag, offset - pos);
2718 skb_shinfo(nskb)->nr_frags++;
2720 if (pos + size <= offset + len) {
2724 skb_frag_size_sub(frag, pos + size - (offset + len));
2731 if (pos < offset + len) {
2732 struct sk_buff *fskb2 = fskb;
2734 BUG_ON(pos + fskb->len != offset + len);
2740 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2746 SKB_FRAG_ASSERT(nskb);
2747 skb_shinfo(nskb)->frag_list = fskb2;
2751 nskb->data_len = len - hsize;
2752 nskb->len += nskb->data_len;
2753 nskb->truesize += nskb->data_len;
2754 } while ((offset += len) < skb->len);
2759 while ((skb = segs)) {
2763 return ERR_PTR(err);
2765 EXPORT_SYMBOL_GPL(skb_segment);
2767 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2769 struct sk_buff *p = *head;
2770 struct sk_buff *nskb;
2771 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2772 struct skb_shared_info *pinfo = skb_shinfo(p);
2773 unsigned int headroom;
2774 unsigned int len = skb_gro_len(skb);
2775 unsigned int offset = skb_gro_offset(skb);
2776 unsigned int headlen = skb_headlen(skb);
2778 if (p->len + len >= 65536)
2781 if (pinfo->frag_list)
2783 else if (headlen <= offset) {
2786 int i = skbinfo->nr_frags;
2787 int nr_frags = pinfo->nr_frags + i;
2791 if (nr_frags > MAX_SKB_FRAGS)
2794 pinfo->nr_frags = nr_frags;
2795 skbinfo->nr_frags = 0;
2797 frag = pinfo->frags + nr_frags;
2798 frag2 = skbinfo->frags + i;
2803 frag->page_offset += offset;
2804 skb_frag_size_sub(frag, offset);
2806 skb->truesize -= skb->data_len;
2807 skb->len -= skb->data_len;
2810 NAPI_GRO_CB(skb)->free = 1;
2812 } else if (skb_gro_len(p) != pinfo->gso_size)
2815 headroom = skb_headroom(p);
2816 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2817 if (unlikely(!nskb))
2820 __copy_skb_header(nskb, p);
2821 nskb->mac_len = p->mac_len;
2823 skb_reserve(nskb, headroom);
2824 __skb_put(nskb, skb_gro_offset(p));
2826 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
2827 skb_set_network_header(nskb, skb_network_offset(p));
2828 skb_set_transport_header(nskb, skb_transport_offset(p));
2830 __skb_pull(p, skb_gro_offset(p));
2831 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2832 p->data - skb_mac_header(p));
2834 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2835 skb_shinfo(nskb)->frag_list = p;
2836 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
2837 pinfo->gso_size = 0;
2838 skb_header_release(p);
2841 nskb->data_len += p->len;
2842 nskb->truesize += p->len;
2843 nskb->len += p->len;
2846 nskb->next = p->next;
2852 if (offset > headlen) {
2853 unsigned int eat = offset - headlen;
2855 skbinfo->frags[0].page_offset += eat;
2856 skb_frag_size_sub(&skbinfo->frags[0], eat);
2857 skb->data_len -= eat;
2862 __skb_pull(skb, offset);
2864 p->prev->next = skb;
2866 skb_header_release(skb);
2869 NAPI_GRO_CB(p)->count++;
2874 NAPI_GRO_CB(skb)->same_flow = 1;
2877 EXPORT_SYMBOL_GPL(skb_gro_receive);
2879 void __init skb_init(void)
2881 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2882 sizeof(struct sk_buff),
2884 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2886 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2887 (2*sizeof(struct sk_buff)) +
2890 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2895 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2896 * @skb: Socket buffer containing the buffers to be mapped
2897 * @sg: The scatter-gather list to map into
2898 * @offset: The offset into the buffer's contents to start mapping
2899 * @len: Length of buffer space to be mapped
2901 * Fill the specified scatter-gather list with mappings/pointers into a
2902 * region of the buffer space attached to a socket buffer.
2905 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2907 int start = skb_headlen(skb);
2908 int i, copy = start - offset;
2909 struct sk_buff *frag_iter;
2915 sg_set_buf(sg, skb->data + offset, copy);
2917 if ((len -= copy) == 0)
2922 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2925 WARN_ON(start > offset + len);
2927 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2928 if ((copy = end - offset) > 0) {
2929 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2933 sg_set_page(&sg[elt], skb_frag_page(frag), copy,
2934 frag->page_offset+offset-start);
2943 skb_walk_frags(skb, frag_iter) {
2946 WARN_ON(start > offset + len);
2948 end = start + frag_iter->len;
2949 if ((copy = end - offset) > 0) {
2952 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
2954 if ((len -= copy) == 0)
2964 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2966 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2968 sg_mark_end(&sg[nsg - 1]);
2972 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2975 * skb_cow_data - Check that a socket buffer's data buffers are writable
2976 * @skb: The socket buffer to check.
2977 * @tailbits: Amount of trailing space to be added
2978 * @trailer: Returned pointer to the skb where the @tailbits space begins
2980 * Make sure that the data buffers attached to a socket buffer are
2981 * writable. If they are not, private copies are made of the data buffers
2982 * and the socket buffer is set to use these instead.
2984 * If @tailbits is given, make sure that there is space to write @tailbits
2985 * bytes of data beyond current end of socket buffer. @trailer will be
2986 * set to point to the skb in which this space begins.
2988 * The number of scatterlist elements required to completely map the
2989 * COW'd and extended socket buffer will be returned.
2991 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2995 struct sk_buff *skb1, **skb_p;
2997 /* If skb is cloned or its head is paged, reallocate
2998 * head pulling out all the pages (pages are considered not writable
2999 * at the moment even if they are anonymous).
3001 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3002 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3005 /* Easy case. Most of packets will go this way. */
3006 if (!skb_has_frag_list(skb)) {
3007 /* A little of trouble, not enough of space for trailer.
3008 * This should not happen, when stack is tuned to generate
3009 * good frames. OK, on miss we reallocate and reserve even more
3010 * space, 128 bytes is fair. */
3012 if (skb_tailroom(skb) < tailbits &&
3013 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3021 /* Misery. We are in troubles, going to mincer fragments... */
3024 skb_p = &skb_shinfo(skb)->frag_list;
3027 while ((skb1 = *skb_p) != NULL) {
3030 /* The fragment is partially pulled by someone,
3031 * this can happen on input. Copy it and everything
3034 if (skb_shared(skb1))
3037 /* If the skb is the last, worry about trailer. */
3039 if (skb1->next == NULL && tailbits) {
3040 if (skb_shinfo(skb1)->nr_frags ||
3041 skb_has_frag_list(skb1) ||
3042 skb_tailroom(skb1) < tailbits)
3043 ntail = tailbits + 128;
3049 skb_shinfo(skb1)->nr_frags ||
3050 skb_has_frag_list(skb1)) {
3051 struct sk_buff *skb2;
3053 /* Fuck, we are miserable poor guys... */
3055 skb2 = skb_copy(skb1, GFP_ATOMIC);
3057 skb2 = skb_copy_expand(skb1,
3061 if (unlikely(skb2 == NULL))
3065 skb_set_owner_w(skb2, skb1->sk);
3067 /* Looking around. Are we still alive?
3068 * OK, link new skb, drop old one */
3070 skb2->next = skb1->next;
3077 skb_p = &skb1->next;
3082 EXPORT_SYMBOL_GPL(skb_cow_data);
3084 static void sock_rmem_free(struct sk_buff *skb)
3086 struct sock *sk = skb->sk;
3088 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3092 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3094 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3096 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3097 (unsigned)sk->sk_rcvbuf)
3102 skb->destructor = sock_rmem_free;
3103 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3105 /* before exiting rcu section, make sure dst is refcounted */
3108 skb_queue_tail(&sk->sk_error_queue, skb);
3109 if (!sock_flag(sk, SOCK_DEAD))
3110 sk->sk_error_report(sk);
3113 EXPORT_SYMBOL(sock_queue_err_skb);
3115 void skb_tstamp_tx(struct sk_buff *orig_skb,
3116 struct skb_shared_hwtstamps *hwtstamps)
3118 struct sock *sk = orig_skb->sk;
3119 struct sock_exterr_skb *serr;
3120 struct sk_buff *skb;
3126 skb = skb_clone(orig_skb, GFP_ATOMIC);
3131 *skb_hwtstamps(skb) =
3135 * no hardware time stamps available,
3136 * so keep the shared tx_flags and only
3137 * store software time stamp
3139 skb->tstamp = ktime_get_real();
3142 serr = SKB_EXT_ERR(skb);
3143 memset(serr, 0, sizeof(*serr));
3144 serr->ee.ee_errno = ENOMSG;
3145 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3147 err = sock_queue_err_skb(sk, skb);
3152 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3156 * skb_partial_csum_set - set up and verify partial csum values for packet
3157 * @skb: the skb to set
3158 * @start: the number of bytes after skb->data to start checksumming.
3159 * @off: the offset from start to place the checksum.
3161 * For untrusted partially-checksummed packets, we need to make sure the values
3162 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3164 * This function checks and sets those values and skb->ip_summed: if this
3165 * returns false you should drop the packet.
3167 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3169 if (unlikely(start > skb_headlen(skb)) ||
3170 unlikely((int)start + off > skb_headlen(skb) - 2)) {
3171 if (net_ratelimit())
3173 "bad partial csum: csum=%u/%u len=%u\n",
3174 start, off, skb_headlen(skb));
3177 skb->ip_summed = CHECKSUM_PARTIAL;
3178 skb->csum_start = skb_headroom(skb) + start;
3179 skb->csum_offset = off;
3182 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3184 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3186 if (net_ratelimit())
3187 pr_warning("%s: received packets cannot be forwarded"
3188 " while LRO is enabled\n", skb->dev->name);
3190 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3193 * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3197 * skb_gso_transport_seglen is used to determine the real size of the
3198 * individual segments, including Layer4 headers (TCP/UDP).
3200 * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3202 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3204 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3206 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3207 return tcp_hdrlen(skb) + shinfo->gso_size;
3209 /* UFO sets gso_size to the size of the fragmentation
3210 * payload, i.e. the size of the L4 (UDP) header is already
3213 return shinfo->gso_size;
3215 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);