2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
10 * Alan Cox : Fixed the worst of the load
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
38 * The functions in this file will not compile correctly with gcc 2.4.x
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
60 #include <net/protocol.h>
63 #include <net/checksum.h>
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
71 static struct kmem_cache *skbuff_head_cache __read_mostly;
72 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
81 * skb_over_panic - private function
86 * Out of line support code for skb_put(). Not user callable.
88 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
98 * skb_under_panic - private function
103 * Out of line support code for skb_push(). Not user callable.
106 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
115 void skb_truesize_bug(struct sk_buff *skb)
117 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb->truesize, skb->len, sizeof(struct sk_buff));
121 EXPORT_SYMBOL(skb_truesize_bug);
123 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
130 * __alloc_skb - allocate a network buffer
131 * @size: size to allocate
132 * @gfp_mask: allocation mask
133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
135 * @node: numa node to allocate memory on
137 * Allocate a new &sk_buff. The returned buffer has no headroom and a
138 * tail room of size bytes. The object has a reference count of one.
139 * The return is the buffer. On a failure the return is %NULL.
141 * Buffers may only be allocated from interrupts using a @gfp_mask of
144 #ifndef CONFIG_HAVE_ARCH_ALLOC_SKB
145 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
146 int fclone, int node)
148 struct kmem_cache *cache;
149 struct skb_shared_info *shinfo;
153 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
156 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
160 /* Get the DATA. Size must match skb_add_mtu(). */
161 size = SKB_DATA_ALIGN(size);
162 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
167 memset(skb, 0, offsetof(struct sk_buff, truesize));
168 skb->truesize = size + sizeof(struct sk_buff);
169 atomic_set(&skb->users, 1);
173 skb->end = data + size;
174 /* make sure we initialize shinfo sequentially */
175 shinfo = skb_shinfo(skb);
176 atomic_set(&shinfo->dataref, 1);
177 shinfo->nr_frags = 0;
178 shinfo->gso_size = 0;
179 shinfo->gso_segs = 0;
180 shinfo->gso_type = 0;
181 shinfo->ip6_frag_id = 0;
182 shinfo->frag_list = NULL;
185 struct sk_buff *child = skb + 1;
186 atomic_t *fclone_ref = (atomic_t *) (child + 1);
188 skb->fclone = SKB_FCLONE_ORIG;
189 atomic_set(fclone_ref, 1);
191 child->fclone = SKB_FCLONE_UNAVAILABLE;
196 kmem_cache_free(cache, skb);
200 #endif /* !CONFIG_HAVE_ARCH_ALLOC_SKB */
203 * alloc_skb_from_cache - allocate a network buffer
204 * @cp: kmem_cache from which to allocate the data area
205 * (object size must be big enough for @size bytes + skb overheads)
206 * @size: size to allocate
207 * @gfp_mask: allocation mask
209 * Allocate a new &sk_buff. The returned buffer has no headroom and
210 * tail room of size bytes. The object has a reference count of one.
211 * The return is the buffer. On a failure the return is %NULL.
213 * Buffers may only be allocated from interrupts using a @gfp_mask of
216 struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
221 struct kmem_cache *cache;
225 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
228 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
233 size = SKB_DATA_ALIGN(size);
234 data = kmem_cache_alloc(cp, gfp_mask);
238 memset(skb, 0, offsetof(struct sk_buff, truesize));
239 skb->truesize = size + sizeof(struct sk_buff);
240 atomic_set(&skb->users, 1);
244 skb->end = data + size;
246 atomic_set(&(skb_shinfo(skb)->dataref), 1);
247 skb_shinfo(skb)->nr_frags = 0;
248 skb_shinfo(skb)->gso_size = 0;
249 skb_shinfo(skb)->gso_segs = 0;
250 skb_shinfo(skb)->gso_type = 0;
251 skb_shinfo(skb)->frag_list = NULL;
254 struct sk_buff *child = skb + 1;
255 atomic_t *fclone_ref = (atomic_t *) (child + 1);
257 skb->fclone = SKB_FCLONE_ORIG;
258 atomic_set(fclone_ref, 1);
260 child->fclone = SKB_FCLONE_UNAVAILABLE;
265 kmem_cache_free(cache, skb);
271 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
272 * @dev: network device to receive on
273 * @length: length to allocate
274 * @gfp_mask: get_free_pages mask, passed to alloc_skb
276 * Allocate a new &sk_buff and assign it a usage count of one. The
277 * buffer has unspecified headroom built in. Users should allocate
278 * the headroom they think they need without accounting for the
279 * built in space. The built in space is used for optimisations.
281 * %NULL is returned if there is no free memory.
283 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
284 unsigned int length, gfp_t gfp_mask)
286 int node = dev->class_dev.dev ? dev_to_node(dev->class_dev.dev) : -1;
289 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
291 skb_reserve(skb, NET_SKB_PAD);
297 static void skb_drop_list(struct sk_buff **listp)
299 struct sk_buff *list = *listp;
304 struct sk_buff *this = list;
310 static inline void skb_drop_fraglist(struct sk_buff *skb)
312 skb_drop_list(&skb_shinfo(skb)->frag_list);
315 static void skb_clone_fraglist(struct sk_buff *skb)
317 struct sk_buff *list;
319 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
323 static void skb_release_data(struct sk_buff *skb)
326 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
327 &skb_shinfo(skb)->dataref)) {
328 if (skb_shinfo(skb)->nr_frags) {
330 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
331 put_page(skb_shinfo(skb)->frags[i].page);
334 if (skb_shinfo(skb)->frag_list)
335 skb_drop_fraglist(skb);
342 * Free an skbuff by memory without cleaning the state.
344 void kfree_skbmem(struct sk_buff *skb)
346 struct sk_buff *other;
347 atomic_t *fclone_ref;
349 skb_release_data(skb);
350 switch (skb->fclone) {
351 case SKB_FCLONE_UNAVAILABLE:
352 kmem_cache_free(skbuff_head_cache, skb);
355 case SKB_FCLONE_ORIG:
356 fclone_ref = (atomic_t *) (skb + 2);
357 if (atomic_dec_and_test(fclone_ref))
358 kmem_cache_free(skbuff_fclone_cache, skb);
361 case SKB_FCLONE_CLONE:
362 fclone_ref = (atomic_t *) (skb + 1);
365 /* The clone portion is available for
366 * fast-cloning again.
368 skb->fclone = SKB_FCLONE_UNAVAILABLE;
370 if (atomic_dec_and_test(fclone_ref))
371 kmem_cache_free(skbuff_fclone_cache, other);
377 * __kfree_skb - private function
380 * Free an sk_buff. Release anything attached to the buffer.
381 * Clean the state. This is an internal helper function. Users should
382 * always call kfree_skb
385 void __kfree_skb(struct sk_buff *skb)
387 dst_release(skb->dst);
389 secpath_put(skb->sp);
391 if (skb->destructor) {
393 skb->destructor(skb);
395 #ifdef CONFIG_NETFILTER
396 nf_conntrack_put(skb->nfct);
397 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
398 nf_conntrack_put_reasm(skb->nfct_reasm);
400 #ifdef CONFIG_BRIDGE_NETFILTER
401 nf_bridge_put(skb->nf_bridge);
404 /* XXX: IS this still necessary? - JHS */
405 #ifdef CONFIG_NET_SCHED
407 #ifdef CONFIG_NET_CLS_ACT
416 * kfree_skb - free an sk_buff
417 * @skb: buffer to free
419 * Drop a reference to the buffer and free it if the usage count has
422 void kfree_skb(struct sk_buff *skb)
426 if (likely(atomic_read(&skb->users) == 1))
428 else if (likely(!atomic_dec_and_test(&skb->users)))
434 * skb_clone - duplicate an sk_buff
435 * @skb: buffer to clone
436 * @gfp_mask: allocation priority
438 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
439 * copies share the same packet data but not structure. The new
440 * buffer has a reference count of 1. If the allocation fails the
441 * function returns %NULL otherwise the new buffer is returned.
443 * If this function is called from an interrupt gfp_mask() must be
447 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
452 if (skb->fclone == SKB_FCLONE_ORIG &&
453 n->fclone == SKB_FCLONE_UNAVAILABLE) {
454 atomic_t *fclone_ref = (atomic_t *) (n + 1);
455 n->fclone = SKB_FCLONE_CLONE;
456 atomic_inc(fclone_ref);
458 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
461 n->fclone = SKB_FCLONE_UNAVAILABLE;
464 #define C(x) n->x = skb->x
466 n->next = n->prev = NULL;
477 secpath_get(skb->sp);
479 memcpy(n->cb, skb->cb, sizeof(skb->cb));
494 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
498 n->destructor = NULL;
500 #ifdef CONFIG_NETFILTER
502 nf_conntrack_get(skb->nfct);
504 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
506 nf_conntrack_get_reasm(skb->nfct_reasm);
508 #ifdef CONFIG_BRIDGE_NETFILTER
510 nf_bridge_get(skb->nf_bridge);
512 #endif /*CONFIG_NETFILTER*/
513 #ifdef CONFIG_NET_SCHED
515 #ifdef CONFIG_NET_CLS_ACT
516 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
517 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
518 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
521 skb_copy_secmark(n, skb);
524 atomic_set(&n->users, 1);
530 atomic_inc(&(skb_shinfo(skb)->dataref));
536 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
539 * Shift between the two data areas in bytes
541 unsigned long offset = new->data - old->data;
545 new->priority = old->priority;
546 new->protocol = old->protocol;
547 new->dst = dst_clone(old->dst);
549 new->sp = secpath_get(old->sp);
551 new->h.raw = old->h.raw + offset;
552 new->nh.raw = old->nh.raw + offset;
553 new->mac.raw = old->mac.raw + offset;
554 memcpy(new->cb, old->cb, sizeof(old->cb));
555 new->local_df = old->local_df;
556 new->fclone = SKB_FCLONE_UNAVAILABLE;
557 new->pkt_type = old->pkt_type;
558 new->tstamp = old->tstamp;
559 new->destructor = NULL;
560 new->mark = old->mark;
561 #ifdef CONFIG_NETFILTER
562 new->nfct = old->nfct;
563 nf_conntrack_get(old->nfct);
564 new->nfctinfo = old->nfctinfo;
565 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
566 new->nfct_reasm = old->nfct_reasm;
567 nf_conntrack_get_reasm(old->nfct_reasm);
569 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
570 new->ipvs_property = old->ipvs_property;
572 #ifdef CONFIG_BRIDGE_NETFILTER
573 new->nf_bridge = old->nf_bridge;
574 nf_bridge_get(old->nf_bridge);
577 #ifdef CONFIG_NET_SCHED
578 #ifdef CONFIG_NET_CLS_ACT
579 new->tc_verd = old->tc_verd;
581 new->tc_index = old->tc_index;
583 skb_copy_secmark(new, old);
584 atomic_set(&new->users, 1);
585 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
586 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
587 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
591 * skb_copy - create private copy of an sk_buff
592 * @skb: buffer to copy
593 * @gfp_mask: allocation priority
595 * Make a copy of both an &sk_buff and its data. This is used when the
596 * caller wishes to modify the data and needs a private copy of the
597 * data to alter. Returns %NULL on failure or the pointer to the buffer
598 * on success. The returned buffer has a reference count of 1.
600 * As by-product this function converts non-linear &sk_buff to linear
601 * one, so that &sk_buff becomes completely private and caller is allowed
602 * to modify all the data of returned buffer. This means that this
603 * function is not recommended for use in circumstances when only
604 * header is going to be modified. Use pskb_copy() instead.
607 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
609 int headerlen = skb->data - skb->head;
611 * Allocate the copy buffer
613 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
618 /* Set the data pointer */
619 skb_reserve(n, headerlen);
620 /* Set the tail pointer and length */
621 skb_put(n, skb->len);
623 n->ip_summed = skb->ip_summed;
625 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
628 copy_skb_header(n, skb);
634 * pskb_copy - create copy of an sk_buff with private head.
635 * @skb: buffer to copy
636 * @gfp_mask: allocation priority
638 * Make a copy of both an &sk_buff and part of its data, located
639 * in header. Fragmented data remain shared. This is used when
640 * the caller wishes to modify only header of &sk_buff and needs
641 * private copy of the header to alter. Returns %NULL on failure
642 * or the pointer to the buffer on success.
643 * The returned buffer has a reference count of 1.
646 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
649 * Allocate the copy buffer
651 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
656 /* Set the data pointer */
657 skb_reserve(n, skb->data - skb->head);
658 /* Set the tail pointer and length */
659 skb_put(n, skb_headlen(skb));
661 memcpy(n->data, skb->data, n->len);
663 n->ip_summed = skb->ip_summed;
665 n->truesize += skb->data_len;
666 n->data_len = skb->data_len;
669 if (skb_shinfo(skb)->nr_frags) {
672 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
673 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
674 get_page(skb_shinfo(n)->frags[i].page);
676 skb_shinfo(n)->nr_frags = i;
679 if (skb_shinfo(skb)->frag_list) {
680 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
681 skb_clone_fraglist(n);
684 copy_skb_header(n, skb);
690 * pskb_expand_head - reallocate header of &sk_buff
691 * @skb: buffer to reallocate
692 * @nhead: room to add at head
693 * @ntail: room to add at tail
694 * @gfp_mask: allocation priority
696 * Expands (or creates identical copy, if &nhead and &ntail are zero)
697 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
698 * reference count of 1. Returns zero in the case of success or error,
699 * if expansion failed. In the last case, &sk_buff is not changed.
701 * All the pointers pointing into skb header may change and must be
702 * reloaded after call to this function.
705 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
710 int size = nhead + (skb->end - skb->head) + ntail;
716 size = SKB_DATA_ALIGN(size);
718 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
722 /* Copy only real data... and, alas, header. This should be
723 * optimized for the cases when header is void. */
724 memcpy(data + nhead, skb->head, skb->tail - skb->head);
725 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
727 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
728 get_page(skb_shinfo(skb)->frags[i].page);
730 if (skb_shinfo(skb)->frag_list)
731 skb_clone_fraglist(skb);
733 skb_release_data(skb);
735 off = (data + nhead) - skb->head;
738 skb->end = data + size;
746 atomic_set(&skb_shinfo(skb)->dataref, 1);
753 /* Make private copy of skb with writable head and some headroom */
755 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
757 struct sk_buff *skb2;
758 int delta = headroom - skb_headroom(skb);
761 skb2 = pskb_copy(skb, GFP_ATOMIC);
763 skb2 = skb_clone(skb, GFP_ATOMIC);
764 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
775 * skb_copy_expand - copy and expand sk_buff
776 * @skb: buffer to copy
777 * @newheadroom: new free bytes at head
778 * @newtailroom: new free bytes at tail
779 * @gfp_mask: allocation priority
781 * Make a copy of both an &sk_buff and its data and while doing so
782 * allocate additional space.
784 * This is used when the caller wishes to modify the data and needs a
785 * private copy of the data to alter as well as more space for new fields.
786 * Returns %NULL on failure or the pointer to the buffer
787 * on success. The returned buffer has a reference count of 1.
789 * You must pass %GFP_ATOMIC as the allocation priority if this function
790 * is called from an interrupt.
792 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
793 * only by netfilter in the cases when checksum is recalculated? --ANK
795 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
796 int newheadroom, int newtailroom,
800 * Allocate the copy buffer
802 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
804 int head_copy_len, head_copy_off;
809 skb_reserve(n, newheadroom);
811 /* Set the tail pointer and length */
812 skb_put(n, skb->len);
814 head_copy_len = skb_headroom(skb);
816 if (newheadroom <= head_copy_len)
817 head_copy_len = newheadroom;
819 head_copy_off = newheadroom - head_copy_len;
821 /* Copy the linear header and data. */
822 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
823 skb->len + head_copy_len))
826 copy_skb_header(n, skb);
832 * skb_pad - zero pad the tail of an skb
833 * @skb: buffer to pad
836 * Ensure that a buffer is followed by a padding area that is zero
837 * filled. Used by network drivers which may DMA or transfer data
838 * beyond the buffer end onto the wire.
840 * May return error in out of memory cases. The skb is freed on error.
843 int skb_pad(struct sk_buff *skb, int pad)
848 /* If the skbuff is non linear tailroom is always zero.. */
849 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
850 memset(skb->data+skb->len, 0, pad);
854 ntail = skb->data_len + pad - (skb->end - skb->tail);
855 if (likely(skb_cloned(skb) || ntail > 0)) {
856 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
861 /* FIXME: The use of this function with non-linear skb's really needs
864 err = skb_linearize(skb);
868 memset(skb->data + skb->len, 0, pad);
876 /* Trims skb to length len. It can change skb pointers.
879 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
881 struct sk_buff **fragp;
882 struct sk_buff *frag;
883 int offset = skb_headlen(skb);
884 int nfrags = skb_shinfo(skb)->nr_frags;
888 if (skb_cloned(skb) &&
889 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
896 for (; i < nfrags; i++) {
897 int end = offset + skb_shinfo(skb)->frags[i].size;
904 skb_shinfo(skb)->frags[i++].size = len - offset;
907 skb_shinfo(skb)->nr_frags = i;
909 for (; i < nfrags; i++)
910 put_page(skb_shinfo(skb)->frags[i].page);
912 if (skb_shinfo(skb)->frag_list)
913 skb_drop_fraglist(skb);
917 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
918 fragp = &frag->next) {
919 int end = offset + frag->len;
921 if (skb_shared(frag)) {
922 struct sk_buff *nfrag;
924 nfrag = skb_clone(frag, GFP_ATOMIC);
925 if (unlikely(!nfrag))
928 nfrag->next = frag->next;
940 unlikely((err = pskb_trim(frag, len - offset))))
944 skb_drop_list(&frag->next);
949 if (len > skb_headlen(skb)) {
950 skb->data_len -= skb->len - len;
955 skb->tail = skb->data + len;
962 * __pskb_pull_tail - advance tail of skb header
963 * @skb: buffer to reallocate
964 * @delta: number of bytes to advance tail
966 * The function makes a sense only on a fragmented &sk_buff,
967 * it expands header moving its tail forward and copying necessary
968 * data from fragmented part.
970 * &sk_buff MUST have reference count of 1.
972 * Returns %NULL (and &sk_buff does not change) if pull failed
973 * or value of new tail of skb in the case of success.
975 * All the pointers pointing into skb header may change and must be
976 * reloaded after call to this function.
979 /* Moves tail of skb head forward, copying data from fragmented part,
980 * when it is necessary.
981 * 1. It may fail due to malloc failure.
982 * 2. It may change skb pointers.
984 * It is pretty complicated. Luckily, it is called only in exceptional cases.
986 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
988 /* If skb has not enough free space at tail, get new one
989 * plus 128 bytes for future expansions. If we have enough
990 * room at tail, reallocate without expansion only if skb is cloned.
992 int i, k, eat = (skb->tail + delta) - skb->end;
994 if (eat > 0 || skb_cloned(skb)) {
995 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1000 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
1003 /* Optimization: no fragments, no reasons to preestimate
1004 * size of pulled pages. Superb.
1006 if (!skb_shinfo(skb)->frag_list)
1009 /* Estimate size of pulled pages. */
1011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1012 if (skb_shinfo(skb)->frags[i].size >= eat)
1014 eat -= skb_shinfo(skb)->frags[i].size;
1017 /* If we need update frag list, we are in troubles.
1018 * Certainly, it possible to add an offset to skb data,
1019 * but taking into account that pulling is expected to
1020 * be very rare operation, it is worth to fight against
1021 * further bloating skb head and crucify ourselves here instead.
1022 * Pure masohism, indeed. 8)8)
1025 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1026 struct sk_buff *clone = NULL;
1027 struct sk_buff *insp = NULL;
1032 if (list->len <= eat) {
1033 /* Eaten as whole. */
1038 /* Eaten partially. */
1040 if (skb_shared(list)) {
1041 /* Sucks! We need to fork list. :-( */
1042 clone = skb_clone(list, GFP_ATOMIC);
1048 /* This may be pulled without
1052 if (!pskb_pull(list, eat)) {
1061 /* Free pulled out fragments. */
1062 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1063 skb_shinfo(skb)->frag_list = list->next;
1066 /* And insert new clone at head. */
1069 skb_shinfo(skb)->frag_list = clone;
1072 /* Success! Now we may commit changes to skb data. */
1077 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1078 if (skb_shinfo(skb)->frags[i].size <= eat) {
1079 put_page(skb_shinfo(skb)->frags[i].page);
1080 eat -= skb_shinfo(skb)->frags[i].size;
1082 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1084 skb_shinfo(skb)->frags[k].page_offset += eat;
1085 skb_shinfo(skb)->frags[k].size -= eat;
1091 skb_shinfo(skb)->nr_frags = k;
1094 skb->data_len -= delta;
1099 /* Copy some data bits from skb to kernel buffer. */
1101 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1104 int start = skb_headlen(skb);
1106 if (offset > (int)skb->len - len)
1110 if ((copy = start - offset) > 0) {
1113 memcpy(to, skb->data + offset, copy);
1114 if ((len -= copy) == 0)
1120 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1123 BUG_TRAP(start <= offset + len);
1125 end = start + skb_shinfo(skb)->frags[i].size;
1126 if ((copy = end - offset) > 0) {
1132 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1134 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1135 offset - start, copy);
1136 kunmap_skb_frag(vaddr);
1138 if ((len -= copy) == 0)
1146 if (skb_shinfo(skb)->frag_list) {
1147 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1149 for (; list; list = list->next) {
1152 BUG_TRAP(start <= offset + len);
1154 end = start + list->len;
1155 if ((copy = end - offset) > 0) {
1158 if (skb_copy_bits(list, offset - start,
1161 if ((len -= copy) == 0)
1177 * skb_store_bits - store bits from kernel buffer to skb
1178 * @skb: destination buffer
1179 * @offset: offset in destination
1180 * @from: source buffer
1181 * @len: number of bytes to copy
1183 * Copy the specified number of bytes from the source buffer to the
1184 * destination skb. This function handles all the messy bits of
1185 * traversing fragment lists and such.
1188 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1191 int start = skb_headlen(skb);
1193 if (offset > (int)skb->len - len)
1196 if ((copy = start - offset) > 0) {
1199 memcpy(skb->data + offset, from, copy);
1200 if ((len -= copy) == 0)
1206 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1207 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1210 BUG_TRAP(start <= offset + len);
1212 end = start + frag->size;
1213 if ((copy = end - offset) > 0) {
1219 vaddr = kmap_skb_frag(frag);
1220 memcpy(vaddr + frag->page_offset + offset - start,
1222 kunmap_skb_frag(vaddr);
1224 if ((len -= copy) == 0)
1232 if (skb_shinfo(skb)->frag_list) {
1233 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1235 for (; list; list = list->next) {
1238 BUG_TRAP(start <= offset + len);
1240 end = start + list->len;
1241 if ((copy = end - offset) > 0) {
1244 if (skb_store_bits(list, offset - start,
1247 if ((len -= copy) == 0)
1262 EXPORT_SYMBOL(skb_store_bits);
1264 /* Checksum skb data. */
1266 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1267 int len, __wsum csum)
1269 int start = skb_headlen(skb);
1270 int i, copy = start - offset;
1273 /* Checksum header. */
1277 csum = csum_partial(skb->data + offset, copy, csum);
1278 if ((len -= copy) == 0)
1284 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1287 BUG_TRAP(start <= offset + len);
1289 end = start + skb_shinfo(skb)->frags[i].size;
1290 if ((copy = end - offset) > 0) {
1293 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1297 vaddr = kmap_skb_frag(frag);
1298 csum2 = csum_partial(vaddr + frag->page_offset +
1299 offset - start, copy, 0);
1300 kunmap_skb_frag(vaddr);
1301 csum = csum_block_add(csum, csum2, pos);
1310 if (skb_shinfo(skb)->frag_list) {
1311 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1313 for (; list; list = list->next) {
1316 BUG_TRAP(start <= offset + len);
1318 end = start + list->len;
1319 if ((copy = end - offset) > 0) {
1323 csum2 = skb_checksum(list, offset - start,
1325 csum = csum_block_add(csum, csum2, pos);
1326 if ((len -= copy) == 0)
1339 /* Both of above in one bottle. */
1341 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1342 u8 *to, int len, __wsum csum)
1344 int start = skb_headlen(skb);
1345 int i, copy = start - offset;
1352 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1354 if ((len -= copy) == 0)
1361 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1364 BUG_TRAP(start <= offset + len);
1366 end = start + skb_shinfo(skb)->frags[i].size;
1367 if ((copy = end - offset) > 0) {
1370 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1374 vaddr = kmap_skb_frag(frag);
1375 csum2 = csum_partial_copy_nocheck(vaddr +
1379 kunmap_skb_frag(vaddr);
1380 csum = csum_block_add(csum, csum2, pos);
1390 if (skb_shinfo(skb)->frag_list) {
1391 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1393 for (; list; list = list->next) {
1397 BUG_TRAP(start <= offset + len);
1399 end = start + list->len;
1400 if ((copy = end - offset) > 0) {
1403 csum2 = skb_copy_and_csum_bits(list,
1406 csum = csum_block_add(csum, csum2, pos);
1407 if ((len -= copy) == 0)
1420 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1425 if (skb->ip_summed == CHECKSUM_PARTIAL)
1426 csstart = skb->h.raw - skb->data;
1428 csstart = skb_headlen(skb);
1430 BUG_ON(csstart > skb_headlen(skb));
1432 memcpy(to, skb->data, csstart);
1435 if (csstart != skb->len)
1436 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1437 skb->len - csstart, 0);
1439 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1440 long csstuff = csstart + skb->csum_offset;
1442 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1447 * skb_dequeue - remove from the head of the queue
1448 * @list: list to dequeue from
1450 * Remove the head of the list. The list lock is taken so the function
1451 * may be used safely with other locking list functions. The head item is
1452 * returned or %NULL if the list is empty.
1455 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1457 unsigned long flags;
1458 struct sk_buff *result;
1460 spin_lock_irqsave(&list->lock, flags);
1461 result = __skb_dequeue(list);
1462 spin_unlock_irqrestore(&list->lock, flags);
1467 * skb_dequeue_tail - remove from the tail of the queue
1468 * @list: list to dequeue from
1470 * Remove the tail of the list. The list lock is taken so the function
1471 * may be used safely with other locking list functions. The tail item is
1472 * returned or %NULL if the list is empty.
1474 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1476 unsigned long flags;
1477 struct sk_buff *result;
1479 spin_lock_irqsave(&list->lock, flags);
1480 result = __skb_dequeue_tail(list);
1481 spin_unlock_irqrestore(&list->lock, flags);
1486 * skb_queue_purge - empty a list
1487 * @list: list to empty
1489 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1490 * the list and one reference dropped. This function takes the list
1491 * lock and is atomic with respect to other list locking functions.
1493 void skb_queue_purge(struct sk_buff_head *list)
1495 struct sk_buff *skb;
1496 while ((skb = skb_dequeue(list)) != NULL)
1501 * skb_queue_head - queue a buffer at the list head
1502 * @list: list to use
1503 * @newsk: buffer to queue
1505 * Queue a buffer at the start of the list. This function takes the
1506 * list lock and can be used safely with other locking &sk_buff functions
1509 * A buffer cannot be placed on two lists at the same time.
1511 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1513 unsigned long flags;
1515 spin_lock_irqsave(&list->lock, flags);
1516 __skb_queue_head(list, newsk);
1517 spin_unlock_irqrestore(&list->lock, flags);
1521 * skb_queue_tail - queue a buffer at the list tail
1522 * @list: list to use
1523 * @newsk: buffer to queue
1525 * Queue a buffer at the tail of the list. This function takes the
1526 * list lock and can be used safely with other locking &sk_buff functions
1529 * A buffer cannot be placed on two lists at the same time.
1531 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1533 unsigned long flags;
1535 spin_lock_irqsave(&list->lock, flags);
1536 __skb_queue_tail(list, newsk);
1537 spin_unlock_irqrestore(&list->lock, flags);
1541 * skb_unlink - remove a buffer from a list
1542 * @skb: buffer to remove
1543 * @list: list to use
1545 * Remove a packet from a list. The list locks are taken and this
1546 * function is atomic with respect to other list locked calls
1548 * You must know what list the SKB is on.
1550 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1552 unsigned long flags;
1554 spin_lock_irqsave(&list->lock, flags);
1555 __skb_unlink(skb, list);
1556 spin_unlock_irqrestore(&list->lock, flags);
1560 * skb_append - append a buffer
1561 * @old: buffer to insert after
1562 * @newsk: buffer to insert
1563 * @list: list to use
1565 * Place a packet after a given packet in a list. The list locks are taken
1566 * and this function is atomic with respect to other list locked calls.
1567 * A buffer cannot be placed on two lists at the same time.
1569 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1571 unsigned long flags;
1573 spin_lock_irqsave(&list->lock, flags);
1574 __skb_append(old, newsk, list);
1575 spin_unlock_irqrestore(&list->lock, flags);
1580 * skb_insert - insert a buffer
1581 * @old: buffer to insert before
1582 * @newsk: buffer to insert
1583 * @list: list to use
1585 * Place a packet before a given packet in a list. The list locks are
1586 * taken and this function is atomic with respect to other list locked
1589 * A buffer cannot be placed on two lists at the same time.
1591 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1593 unsigned long flags;
1595 spin_lock_irqsave(&list->lock, flags);
1596 __skb_insert(newsk, old->prev, old, list);
1597 spin_unlock_irqrestore(&list->lock, flags);
1602 * Tune the memory allocator for a new MTU size.
1604 void skb_add_mtu(int mtu)
1606 /* Must match allocation in alloc_skb */
1607 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1609 kmem_add_cache_size(mtu);
1613 static inline void skb_split_inside_header(struct sk_buff *skb,
1614 struct sk_buff* skb1,
1615 const u32 len, const int pos)
1619 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1621 /* And move data appendix as is. */
1622 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1623 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1625 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1626 skb_shinfo(skb)->nr_frags = 0;
1627 skb1->data_len = skb->data_len;
1628 skb1->len += skb1->data_len;
1631 skb->tail = skb->data + len;
1634 static inline void skb_split_no_header(struct sk_buff *skb,
1635 struct sk_buff* skb1,
1636 const u32 len, int pos)
1639 const int nfrags = skb_shinfo(skb)->nr_frags;
1641 skb_shinfo(skb)->nr_frags = 0;
1642 skb1->len = skb1->data_len = skb->len - len;
1644 skb->data_len = len - pos;
1646 for (i = 0; i < nfrags; i++) {
1647 int size = skb_shinfo(skb)->frags[i].size;
1649 if (pos + size > len) {
1650 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1654 * We have two variants in this case:
1655 * 1. Move all the frag to the second
1656 * part, if it is possible. F.e.
1657 * this approach is mandatory for TUX,
1658 * where splitting is expensive.
1659 * 2. Split is accurately. We make this.
1661 get_page(skb_shinfo(skb)->frags[i].page);
1662 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1663 skb_shinfo(skb1)->frags[0].size -= len - pos;
1664 skb_shinfo(skb)->frags[i].size = len - pos;
1665 skb_shinfo(skb)->nr_frags++;
1669 skb_shinfo(skb)->nr_frags++;
1672 skb_shinfo(skb1)->nr_frags = k;
1676 * skb_split - Split fragmented skb to two parts at length len.
1677 * @skb: the buffer to split
1678 * @skb1: the buffer to receive the second part
1679 * @len: new length for skb
1681 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1683 int pos = skb_headlen(skb);
1685 if (len < pos) /* Split line is inside header. */
1686 skb_split_inside_header(skb, skb1, len, pos);
1687 else /* Second chunk has no header, nothing to copy. */
1688 skb_split_no_header(skb, skb1, len, pos);
1692 * skb_prepare_seq_read - Prepare a sequential read of skb data
1693 * @skb: the buffer to read
1694 * @from: lower offset of data to be read
1695 * @to: upper offset of data to be read
1696 * @st: state variable
1698 * Initializes the specified state variable. Must be called before
1699 * invoking skb_seq_read() for the first time.
1701 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1702 unsigned int to, struct skb_seq_state *st)
1704 st->lower_offset = from;
1705 st->upper_offset = to;
1706 st->root_skb = st->cur_skb = skb;
1707 st->frag_idx = st->stepped_offset = 0;
1708 st->frag_data = NULL;
1712 * skb_seq_read - Sequentially read skb data
1713 * @consumed: number of bytes consumed by the caller so far
1714 * @data: destination pointer for data to be returned
1715 * @st: state variable
1717 * Reads a block of skb data at &consumed relative to the
1718 * lower offset specified to skb_prepare_seq_read(). Assigns
1719 * the head of the data block to &data and returns the length
1720 * of the block or 0 if the end of the skb data or the upper
1721 * offset has been reached.
1723 * The caller is not required to consume all of the data
1724 * returned, i.e. &consumed is typically set to the number
1725 * of bytes already consumed and the next call to
1726 * skb_seq_read() will return the remaining part of the block.
1728 * Note: The size of each block of data returned can be arbitary,
1729 * this limitation is the cost for zerocopy seqeuental
1730 * reads of potentially non linear data.
1732 * Note: Fragment lists within fragments are not implemented
1733 * at the moment, state->root_skb could be replaced with
1734 * a stack for this purpose.
1736 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1737 struct skb_seq_state *st)
1739 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1742 if (unlikely(abs_offset >= st->upper_offset))
1746 block_limit = skb_headlen(st->cur_skb);
1748 if (abs_offset < block_limit) {
1749 *data = st->cur_skb->data + abs_offset;
1750 return block_limit - abs_offset;
1753 if (st->frag_idx == 0 && !st->frag_data)
1754 st->stepped_offset += skb_headlen(st->cur_skb);
1756 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1757 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1758 block_limit = frag->size + st->stepped_offset;
1760 if (abs_offset < block_limit) {
1762 st->frag_data = kmap_skb_frag(frag);
1764 *data = (u8 *) st->frag_data + frag->page_offset +
1765 (abs_offset - st->stepped_offset);
1767 return block_limit - abs_offset;
1770 if (st->frag_data) {
1771 kunmap_skb_frag(st->frag_data);
1772 st->frag_data = NULL;
1776 st->stepped_offset += frag->size;
1779 if (st->cur_skb->next) {
1780 st->cur_skb = st->cur_skb->next;
1783 } else if (st->root_skb == st->cur_skb &&
1784 skb_shinfo(st->root_skb)->frag_list) {
1785 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1793 * skb_abort_seq_read - Abort a sequential read of skb data
1794 * @st: state variable
1796 * Must be called if skb_seq_read() was not called until it
1799 void skb_abort_seq_read(struct skb_seq_state *st)
1802 kunmap_skb_frag(st->frag_data);
1805 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1807 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1808 struct ts_config *conf,
1809 struct ts_state *state)
1811 return skb_seq_read(offset, text, TS_SKB_CB(state));
1814 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1816 skb_abort_seq_read(TS_SKB_CB(state));
1820 * skb_find_text - Find a text pattern in skb data
1821 * @skb: the buffer to look in
1822 * @from: search offset
1824 * @config: textsearch configuration
1825 * @state: uninitialized textsearch state variable
1827 * Finds a pattern in the skb data according to the specified
1828 * textsearch configuration. Use textsearch_next() to retrieve
1829 * subsequent occurrences of the pattern. Returns the offset
1830 * to the first occurrence or UINT_MAX if no match was found.
1832 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1833 unsigned int to, struct ts_config *config,
1834 struct ts_state *state)
1838 config->get_next_block = skb_ts_get_next_block;
1839 config->finish = skb_ts_finish;
1841 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1843 ret = textsearch_find(config, state);
1844 return (ret <= to - from ? ret : UINT_MAX);
1848 * skb_append_datato_frags: - append the user data to a skb
1849 * @sk: sock structure
1850 * @skb: skb structure to be appened with user data.
1851 * @getfrag: call back function to be used for getting the user data
1852 * @from: pointer to user message iov
1853 * @length: length of the iov message
1855 * Description: This procedure append the user data in the fragment part
1856 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1858 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1859 int (*getfrag)(void *from, char *to, int offset,
1860 int len, int odd, struct sk_buff *skb),
1861 void *from, int length)
1864 skb_frag_t *frag = NULL;
1865 struct page *page = NULL;
1871 /* Return error if we don't have space for new frag */
1872 frg_cnt = skb_shinfo(skb)->nr_frags;
1873 if (frg_cnt >= MAX_SKB_FRAGS)
1876 /* allocate a new page for next frag */
1877 page = alloc_pages(sk->sk_allocation, 0);
1879 /* If alloc_page fails just return failure and caller will
1880 * free previous allocated pages by doing kfree_skb()
1885 /* initialize the next frag */
1886 sk->sk_sndmsg_page = page;
1887 sk->sk_sndmsg_off = 0;
1888 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1889 skb->truesize += PAGE_SIZE;
1890 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1892 /* get the new initialized frag */
1893 frg_cnt = skb_shinfo(skb)->nr_frags;
1894 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1896 /* copy the user data to page */
1897 left = PAGE_SIZE - frag->page_offset;
1898 copy = (length > left)? left : length;
1900 ret = getfrag(from, (page_address(frag->page) +
1901 frag->page_offset + frag->size),
1902 offset, copy, 0, skb);
1906 /* copy was successful so update the size parameters */
1907 sk->sk_sndmsg_off += copy;
1910 skb->data_len += copy;
1914 } while (length > 0);
1920 * skb_pull_rcsum - pull skb and update receive checksum
1921 * @skb: buffer to update
1922 * @start: start of data before pull
1923 * @len: length of data pulled
1925 * This function performs an skb_pull on the packet and updates
1926 * update the CHECKSUM_COMPLETE checksum. It should be used on
1927 * receive path processing instead of skb_pull unless you know
1928 * that the checksum difference is zero (e.g., a valid IP header)
1929 * or you are setting ip_summed to CHECKSUM_NONE.
1931 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1933 BUG_ON(len > skb->len);
1935 BUG_ON(skb->len < skb->data_len);
1936 skb_postpull_rcsum(skb, skb->data, len);
1937 return skb->data += len;
1940 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1943 * skb_segment - Perform protocol segmentation on skb.
1944 * @skb: buffer to segment
1945 * @features: features for the output path (see dev->features)
1947 * This function performs segmentation on the given skb. It returns
1948 * the segment at the given position. It returns NULL if there are
1949 * no more segments to generate, or when an error is encountered.
1951 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1953 struct sk_buff *segs = NULL;
1954 struct sk_buff *tail = NULL;
1955 unsigned int mss = skb_shinfo(skb)->gso_size;
1956 unsigned int doffset = skb->data - skb->mac.raw;
1957 unsigned int offset = doffset;
1958 unsigned int headroom;
1960 int sg = features & NETIF_F_SG;
1961 int nfrags = skb_shinfo(skb)->nr_frags;
1966 __skb_push(skb, doffset);
1967 headroom = skb_headroom(skb);
1968 pos = skb_headlen(skb);
1971 struct sk_buff *nskb;
1977 len = skb->len - offset;
1981 hsize = skb_headlen(skb) - offset;
1984 if (hsize > len || !sg)
1987 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1988 if (unlikely(!nskb))
1997 nskb->dev = skb->dev;
1998 nskb->priority = skb->priority;
1999 nskb->protocol = skb->protocol;
2000 nskb->dst = dst_clone(skb->dst);
2001 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2002 nskb->pkt_type = skb->pkt_type;
2003 nskb->mac_len = skb->mac_len;
2005 skb_reserve(nskb, headroom);
2006 nskb->mac.raw = nskb->data;
2007 nskb->nh.raw = nskb->data + skb->mac_len;
2008 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
2009 memcpy(skb_put(nskb, doffset), skb->data, doffset);
2012 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2018 frag = skb_shinfo(nskb)->frags;
2021 nskb->ip_summed = CHECKSUM_PARTIAL;
2022 nskb->csum = skb->csum;
2023 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2025 while (pos < offset + len) {
2026 BUG_ON(i >= nfrags);
2028 *frag = skb_shinfo(skb)->frags[i];
2029 get_page(frag->page);
2033 frag->page_offset += offset - pos;
2034 frag->size -= offset - pos;
2039 if (pos + size <= offset + len) {
2043 frag->size -= pos + size - (offset + len);
2050 skb_shinfo(nskb)->nr_frags = k;
2051 nskb->data_len = len - hsize;
2052 nskb->len += nskb->data_len;
2053 nskb->truesize += nskb->data_len;
2054 } while ((offset += len) < skb->len);
2059 while ((skb = segs)) {
2063 return ERR_PTR(err);
2066 EXPORT_SYMBOL_GPL(skb_segment);
2068 void __init skb_init(void)
2070 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2071 sizeof(struct sk_buff),
2073 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2075 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2076 (2*sizeof(struct sk_buff)) +
2079 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2083 EXPORT_SYMBOL(___pskb_trim);
2084 EXPORT_SYMBOL(__kfree_skb);
2085 EXPORT_SYMBOL(kfree_skb);
2086 EXPORT_SYMBOL(__pskb_pull_tail);
2087 EXPORT_SYMBOL(__alloc_skb);
2088 EXPORT_SYMBOL(__netdev_alloc_skb);
2089 EXPORT_SYMBOL(pskb_copy);
2090 EXPORT_SYMBOL(pskb_expand_head);
2091 EXPORT_SYMBOL(skb_checksum);
2092 EXPORT_SYMBOL(skb_clone);
2093 EXPORT_SYMBOL(skb_clone_fraglist);
2094 EXPORT_SYMBOL(skb_copy);
2095 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2096 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2097 EXPORT_SYMBOL(skb_copy_bits);
2098 EXPORT_SYMBOL(skb_copy_expand);
2099 EXPORT_SYMBOL(skb_over_panic);
2100 EXPORT_SYMBOL(skb_pad);
2101 EXPORT_SYMBOL(skb_realloc_headroom);
2102 EXPORT_SYMBOL(skb_under_panic);
2103 EXPORT_SYMBOL(skb_dequeue);
2104 EXPORT_SYMBOL(skb_dequeue_tail);
2105 EXPORT_SYMBOL(skb_insert);
2106 EXPORT_SYMBOL(skb_queue_purge);
2107 EXPORT_SYMBOL(skb_queue_head);
2108 EXPORT_SYMBOL(skb_queue_tail);
2109 EXPORT_SYMBOL(skb_unlink);
2110 EXPORT_SYMBOL(skb_append);
2111 EXPORT_SYMBOL(skb_split);
2112 EXPORT_SYMBOL(skb_prepare_seq_read);
2113 EXPORT_SYMBOL(skb_seq_read);
2114 EXPORT_SYMBOL(skb_abort_seq_read);
2115 EXPORT_SYMBOL(skb_find_text);
2116 EXPORT_SYMBOL(skb_append_datato_frags);