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 skb->skb_tag = nx_current_nid();
175 /* make sure we initialize shinfo sequentially */
176 shinfo = skb_shinfo(skb);
177 atomic_set(&shinfo->dataref, 1);
178 shinfo->nr_frags = 0;
179 shinfo->gso_size = 0;
180 shinfo->gso_segs = 0;
181 shinfo->gso_type = 0;
182 shinfo->ip6_frag_id = 0;
183 shinfo->frag_list = NULL;
186 struct sk_buff *child = skb + 1;
187 atomic_t *fclone_ref = (atomic_t *) (child + 1);
189 skb->fclone = SKB_FCLONE_ORIG;
190 atomic_set(fclone_ref, 1);
192 child->fclone = SKB_FCLONE_UNAVAILABLE;
197 kmem_cache_free(cache, skb);
201 #endif /* !CONFIG_HAVE_ARCH_ALLOC_SKB */
204 * alloc_skb_from_cache - allocate a network buffer
205 * @cp: kmem_cache from which to allocate the data area
206 * (object size must be big enough for @size bytes + skb overheads)
207 * @size: size to allocate
208 * @gfp_mask: allocation mask
210 * Allocate a new &sk_buff. The returned buffer has no headroom and
211 * tail room of size bytes. The object has a reference count of one.
212 * The return is the buffer. On a failure the return is %NULL.
214 * Buffers may only be allocated from interrupts using a @gfp_mask of
217 struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
222 struct kmem_cache *cache;
226 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
229 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
234 size = SKB_DATA_ALIGN(size);
235 data = kmem_cache_alloc(cp, gfp_mask);
239 memset(skb, 0, offsetof(struct sk_buff, truesize));
240 skb->truesize = size + sizeof(struct sk_buff);
241 atomic_set(&skb->users, 1);
245 skb->end = data + size;
246 skb->skb_tag = nx_current_nid();
248 atomic_set(&(skb_shinfo(skb)->dataref), 1);
249 skb_shinfo(skb)->nr_frags = 0;
250 skb_shinfo(skb)->gso_size = 0;
251 skb_shinfo(skb)->gso_segs = 0;
252 skb_shinfo(skb)->gso_type = 0;
253 skb_shinfo(skb)->frag_list = NULL;
256 struct sk_buff *child = skb + 1;
257 atomic_t *fclone_ref = (atomic_t *) (child + 1);
259 skb->fclone = SKB_FCLONE_ORIG;
260 atomic_set(fclone_ref, 1);
262 child->fclone = SKB_FCLONE_UNAVAILABLE;
267 kmem_cache_free(cache, skb);
273 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
274 * @dev: network device to receive on
275 * @length: length to allocate
276 * @gfp_mask: get_free_pages mask, passed to alloc_skb
278 * Allocate a new &sk_buff and assign it a usage count of one. The
279 * buffer has unspecified headroom built in. Users should allocate
280 * the headroom they think they need without accounting for the
281 * built in space. The built in space is used for optimisations.
283 * %NULL is returned if there is no free memory.
285 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
286 unsigned int length, gfp_t gfp_mask)
288 int node = dev->class_dev.dev ? dev_to_node(dev->class_dev.dev) : -1;
291 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
293 skb_reserve(skb, NET_SKB_PAD);
299 static void skb_drop_list(struct sk_buff **listp)
301 struct sk_buff *list = *listp;
306 struct sk_buff *this = list;
312 static inline void skb_drop_fraglist(struct sk_buff *skb)
314 skb_drop_list(&skb_shinfo(skb)->frag_list);
317 static void skb_clone_fraglist(struct sk_buff *skb)
319 struct sk_buff *list;
321 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
325 static void skb_release_data(struct sk_buff *skb)
328 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
329 &skb_shinfo(skb)->dataref)) {
330 if (skb_shinfo(skb)->nr_frags) {
332 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
333 put_page(skb_shinfo(skb)->frags[i].page);
336 if (skb_shinfo(skb)->frag_list)
337 skb_drop_fraglist(skb);
344 * Free an skbuff by memory without cleaning the state.
346 void kfree_skbmem(struct sk_buff *skb)
348 struct sk_buff *other;
349 atomic_t *fclone_ref;
351 skb_release_data(skb);
352 switch (skb->fclone) {
353 case SKB_FCLONE_UNAVAILABLE:
354 kmem_cache_free(skbuff_head_cache, skb);
357 case SKB_FCLONE_ORIG:
358 fclone_ref = (atomic_t *) (skb + 2);
359 if (atomic_dec_and_test(fclone_ref))
360 kmem_cache_free(skbuff_fclone_cache, skb);
363 case SKB_FCLONE_CLONE:
364 fclone_ref = (atomic_t *) (skb + 1);
367 /* The clone portion is available for
368 * fast-cloning again.
370 skb->fclone = SKB_FCLONE_UNAVAILABLE;
372 if (atomic_dec_and_test(fclone_ref))
373 kmem_cache_free(skbuff_fclone_cache, other);
379 * __kfree_skb - private function
382 * Free an sk_buff. Release anything attached to the buffer.
383 * Clean the state. This is an internal helper function. Users should
384 * always call kfree_skb
387 void __kfree_skb(struct sk_buff *skb)
389 dst_release(skb->dst);
391 secpath_put(skb->sp);
393 if (skb->destructor) {
395 skb->destructor(skb);
397 #ifdef CONFIG_NETFILTER
398 nf_conntrack_put(skb->nfct);
399 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
400 nf_conntrack_put_reasm(skb->nfct_reasm);
402 #ifdef CONFIG_BRIDGE_NETFILTER
403 nf_bridge_put(skb->nf_bridge);
406 /* XXX: IS this still necessary? - JHS */
407 #ifdef CONFIG_NET_SCHED
409 #ifdef CONFIG_NET_CLS_ACT
418 * kfree_skb - free an sk_buff
419 * @skb: buffer to free
421 * Drop a reference to the buffer and free it if the usage count has
424 void kfree_skb(struct sk_buff *skb)
428 if (likely(atomic_read(&skb->users) == 1))
430 else if (likely(!atomic_dec_and_test(&skb->users)))
436 * skb_clone - duplicate an sk_buff
437 * @skb: buffer to clone
438 * @gfp_mask: allocation priority
440 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
441 * copies share the same packet data but not structure. The new
442 * buffer has a reference count of 1. If the allocation fails the
443 * function returns %NULL otherwise the new buffer is returned.
445 * If this function is called from an interrupt gfp_mask() must be
449 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
454 if (skb->fclone == SKB_FCLONE_ORIG &&
455 n->fclone == SKB_FCLONE_UNAVAILABLE) {
456 atomic_t *fclone_ref = (atomic_t *) (n + 1);
457 n->fclone = SKB_FCLONE_CLONE;
458 atomic_inc(fclone_ref);
460 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
463 n->fclone = SKB_FCLONE_UNAVAILABLE;
466 #define C(x) n->x = skb->x
468 n->next = n->prev = NULL;
479 secpath_get(skb->sp);
481 memcpy(n->cb, skb->cb, sizeof(skb->cb));
496 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
500 n->destructor = NULL;
502 #ifdef CONFIG_NETFILTER
504 nf_conntrack_get(skb->nfct);
506 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
508 nf_conntrack_get_reasm(skb->nfct_reasm);
510 #ifdef CONFIG_BRIDGE_NETFILTER
512 nf_bridge_get(skb->nf_bridge);
514 #endif /*CONFIG_NETFILTER*/
515 #ifdef CONFIG_NET_SCHED
517 #ifdef CONFIG_NET_CLS_ACT
518 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
519 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
520 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
523 skb_copy_secmark(n, skb);
526 atomic_set(&n->users, 1);
532 /* Sapan: Cloned skbs aren't owned by anyone. Let the cloner decide who it belongs to. */
534 atomic_inc(&(skb_shinfo(skb)->dataref));
540 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
543 * Shift between the two data areas in bytes
545 unsigned long offset = new->data - old->data;
549 new->priority = old->priority;
550 new->protocol = old->protocol;
551 new->dst = dst_clone(old->dst);
553 new->sp = secpath_get(old->sp);
555 new->h.raw = old->h.raw + offset;
556 new->nh.raw = old->nh.raw + offset;
557 new->mac.raw = old->mac.raw + offset;
558 memcpy(new->cb, old->cb, sizeof(old->cb));
559 new->local_df = old->local_df;
560 new->fclone = SKB_FCLONE_UNAVAILABLE;
561 new->pkt_type = old->pkt_type;
562 new->tstamp = old->tstamp;
563 new->destructor = NULL;
564 new->mark = old->mark;
565 #ifdef CONFIG_NETFILTER
566 new->nfct = old->nfct;
567 nf_conntrack_get(old->nfct);
568 new->nfctinfo = old->nfctinfo;
569 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
570 new->nfct_reasm = old->nfct_reasm;
571 nf_conntrack_get_reasm(old->nfct_reasm);
573 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
574 new->ipvs_property = old->ipvs_property;
576 #ifdef CONFIG_BRIDGE_NETFILTER
577 new->nf_bridge = old->nf_bridge;
578 nf_bridge_get(old->nf_bridge);
581 #ifdef CONFIG_NET_SCHED
582 #ifdef CONFIG_NET_CLS_ACT
583 new->tc_verd = old->tc_verd;
585 new->tc_index = old->tc_index;
587 skb_copy_secmark(new, old);
588 new->skb_tag = old->skb_tag;
590 atomic_set(&new->users, 1);
591 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
592 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
593 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
597 * skb_copy - create private copy of an sk_buff
598 * @skb: buffer to copy
599 * @gfp_mask: allocation priority
601 * Make a copy of both an &sk_buff and its data. This is used when the
602 * caller wishes to modify the data and needs a private copy of the
603 * data to alter. Returns %NULL on failure or the pointer to the buffer
604 * on success. The returned buffer has a reference count of 1.
606 * As by-product this function converts non-linear &sk_buff to linear
607 * one, so that &sk_buff becomes completely private and caller is allowed
608 * to modify all the data of returned buffer. This means that this
609 * function is not recommended for use in circumstances when only
610 * header is going to be modified. Use pskb_copy() instead.
613 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
615 int headerlen = skb->data - skb->head;
617 * Allocate the copy buffer
619 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
624 /* Set the data pointer */
625 skb_reserve(n, headerlen);
626 /* Set the tail pointer and length */
627 skb_put(n, skb->len);
629 n->ip_summed = skb->ip_summed;
631 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
634 copy_skb_header(n, skb);
640 * pskb_copy - create copy of an sk_buff with private head.
641 * @skb: buffer to copy
642 * @gfp_mask: allocation priority
644 * Make a copy of both an &sk_buff and part of its data, located
645 * in header. Fragmented data remain shared. This is used when
646 * the caller wishes to modify only header of &sk_buff and needs
647 * private copy of the header to alter. Returns %NULL on failure
648 * or the pointer to the buffer on success.
649 * The returned buffer has a reference count of 1.
652 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
655 * Allocate the copy buffer
657 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
662 /* Set the data pointer */
663 skb_reserve(n, skb->data - skb->head);
664 /* Set the tail pointer and length */
665 skb_put(n, skb_headlen(skb));
667 memcpy(n->data, skb->data, n->len);
669 n->ip_summed = skb->ip_summed;
671 n->truesize += skb->data_len;
672 n->data_len = skb->data_len;
675 if (skb_shinfo(skb)->nr_frags) {
678 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
679 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
680 get_page(skb_shinfo(n)->frags[i].page);
682 skb_shinfo(n)->nr_frags = i;
685 if (skb_shinfo(skb)->frag_list) {
686 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
687 skb_clone_fraglist(n);
690 copy_skb_header(n, skb);
696 * pskb_expand_head - reallocate header of &sk_buff
697 * @skb: buffer to reallocate
698 * @nhead: room to add at head
699 * @ntail: room to add at tail
700 * @gfp_mask: allocation priority
702 * Expands (or creates identical copy, if &nhead and &ntail are zero)
703 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
704 * reference count of 1. Returns zero in the case of success or error,
705 * if expansion failed. In the last case, &sk_buff is not changed.
707 * All the pointers pointing into skb header may change and must be
708 * reloaded after call to this function.
711 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
716 int size = nhead + (skb->end - skb->head) + ntail;
722 size = SKB_DATA_ALIGN(size);
724 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
728 /* Copy only real data... and, alas, header. This should be
729 * optimized for the cases when header is void. */
730 memcpy(data + nhead, skb->head, skb->tail - skb->head);
731 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
733 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
734 get_page(skb_shinfo(skb)->frags[i].page);
736 if (skb_shinfo(skb)->frag_list)
737 skb_clone_fraglist(skb);
739 skb_release_data(skb);
741 off = (data + nhead) - skb->head;
744 skb->end = data + size;
752 atomic_set(&skb_shinfo(skb)->dataref, 1);
759 /* Make private copy of skb with writable head and some headroom */
761 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
763 struct sk_buff *skb2;
764 int delta = headroom - skb_headroom(skb);
767 skb2 = pskb_copy(skb, GFP_ATOMIC);
769 skb2 = skb_clone(skb, GFP_ATOMIC);
770 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
781 * skb_copy_expand - copy and expand sk_buff
782 * @skb: buffer to copy
783 * @newheadroom: new free bytes at head
784 * @newtailroom: new free bytes at tail
785 * @gfp_mask: allocation priority
787 * Make a copy of both an &sk_buff and its data and while doing so
788 * allocate additional space.
790 * This is used when the caller wishes to modify the data and needs a
791 * private copy of the data to alter as well as more space for new fields.
792 * Returns %NULL on failure or the pointer to the buffer
793 * on success. The returned buffer has a reference count of 1.
795 * You must pass %GFP_ATOMIC as the allocation priority if this function
796 * is called from an interrupt.
798 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
799 * only by netfilter in the cases when checksum is recalculated? --ANK
801 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
802 int newheadroom, int newtailroom,
806 * Allocate the copy buffer
808 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
810 int head_copy_len, head_copy_off;
815 skb_reserve(n, newheadroom);
817 /* Set the tail pointer and length */
818 skb_put(n, skb->len);
820 head_copy_len = skb_headroom(skb);
822 if (newheadroom <= head_copy_len)
823 head_copy_len = newheadroom;
825 head_copy_off = newheadroom - head_copy_len;
827 /* Copy the linear header and data. */
828 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
829 skb->len + head_copy_len))
832 copy_skb_header(n, skb);
838 * skb_pad - zero pad the tail of an skb
839 * @skb: buffer to pad
842 * Ensure that a buffer is followed by a padding area that is zero
843 * filled. Used by network drivers which may DMA or transfer data
844 * beyond the buffer end onto the wire.
846 * May return error in out of memory cases. The skb is freed on error.
849 int skb_pad(struct sk_buff *skb, int pad)
854 /* If the skbuff is non linear tailroom is always zero.. */
855 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
856 memset(skb->data+skb->len, 0, pad);
860 ntail = skb->data_len + pad - (skb->end - skb->tail);
861 if (likely(skb_cloned(skb) || ntail > 0)) {
862 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
867 /* FIXME: The use of this function with non-linear skb's really needs
870 err = skb_linearize(skb);
874 memset(skb->data + skb->len, 0, pad);
882 /* Trims skb to length len. It can change skb pointers.
885 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
887 struct sk_buff **fragp;
888 struct sk_buff *frag;
889 int offset = skb_headlen(skb);
890 int nfrags = skb_shinfo(skb)->nr_frags;
894 if (skb_cloned(skb) &&
895 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
902 for (; i < nfrags; i++) {
903 int end = offset + skb_shinfo(skb)->frags[i].size;
910 skb_shinfo(skb)->frags[i++].size = len - offset;
913 skb_shinfo(skb)->nr_frags = i;
915 for (; i < nfrags; i++)
916 put_page(skb_shinfo(skb)->frags[i].page);
918 if (skb_shinfo(skb)->frag_list)
919 skb_drop_fraglist(skb);
923 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
924 fragp = &frag->next) {
925 int end = offset + frag->len;
927 if (skb_shared(frag)) {
928 struct sk_buff *nfrag;
930 nfrag = skb_clone(frag, GFP_ATOMIC);
931 if (unlikely(!nfrag))
934 nfrag->next = frag->next;
946 unlikely((err = pskb_trim(frag, len - offset))))
950 skb_drop_list(&frag->next);
955 if (len > skb_headlen(skb)) {
956 skb->data_len -= skb->len - len;
961 skb->tail = skb->data + len;
968 * __pskb_pull_tail - advance tail of skb header
969 * @skb: buffer to reallocate
970 * @delta: number of bytes to advance tail
972 * The function makes a sense only on a fragmented &sk_buff,
973 * it expands header moving its tail forward and copying necessary
974 * data from fragmented part.
976 * &sk_buff MUST have reference count of 1.
978 * Returns %NULL (and &sk_buff does not change) if pull failed
979 * or value of new tail of skb in the case of success.
981 * All the pointers pointing into skb header may change and must be
982 * reloaded after call to this function.
985 /* Moves tail of skb head forward, copying data from fragmented part,
986 * when it is necessary.
987 * 1. It may fail due to malloc failure.
988 * 2. It may change skb pointers.
990 * It is pretty complicated. Luckily, it is called only in exceptional cases.
992 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
994 /* If skb has not enough free space at tail, get new one
995 * plus 128 bytes for future expansions. If we have enough
996 * room at tail, reallocate without expansion only if skb is cloned.
998 int i, k, eat = (skb->tail + delta) - skb->end;
1000 if (eat > 0 || skb_cloned(skb)) {
1001 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1006 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
1009 /* Optimization: no fragments, no reasons to preestimate
1010 * size of pulled pages. Superb.
1012 if (!skb_shinfo(skb)->frag_list)
1015 /* Estimate size of pulled pages. */
1017 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1018 if (skb_shinfo(skb)->frags[i].size >= eat)
1020 eat -= skb_shinfo(skb)->frags[i].size;
1023 /* If we need update frag list, we are in troubles.
1024 * Certainly, it possible to add an offset to skb data,
1025 * but taking into account that pulling is expected to
1026 * be very rare operation, it is worth to fight against
1027 * further bloating skb head and crucify ourselves here instead.
1028 * Pure masohism, indeed. 8)8)
1031 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1032 struct sk_buff *clone = NULL;
1033 struct sk_buff *insp = NULL;
1038 if (list->len <= eat) {
1039 /* Eaten as whole. */
1044 /* Eaten partially. */
1046 if (skb_shared(list)) {
1047 /* Sucks! We need to fork list. :-( */
1048 clone = skb_clone(list, GFP_ATOMIC);
1054 /* This may be pulled without
1058 if (!pskb_pull(list, eat)) {
1067 /* Free pulled out fragments. */
1068 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1069 skb_shinfo(skb)->frag_list = list->next;
1072 /* And insert new clone at head. */
1075 skb_shinfo(skb)->frag_list = clone;
1078 /* Success! Now we may commit changes to skb data. */
1083 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1084 if (skb_shinfo(skb)->frags[i].size <= eat) {
1085 put_page(skb_shinfo(skb)->frags[i].page);
1086 eat -= skb_shinfo(skb)->frags[i].size;
1088 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1090 skb_shinfo(skb)->frags[k].page_offset += eat;
1091 skb_shinfo(skb)->frags[k].size -= eat;
1097 skb_shinfo(skb)->nr_frags = k;
1100 skb->data_len -= delta;
1105 /* Copy some data bits from skb to kernel buffer. */
1107 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1110 int start = skb_headlen(skb);
1112 if (offset > (int)skb->len - len)
1116 if ((copy = start - offset) > 0) {
1119 memcpy(to, skb->data + offset, copy);
1120 if ((len -= copy) == 0)
1126 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1129 BUG_TRAP(start <= offset + len);
1131 end = start + skb_shinfo(skb)->frags[i].size;
1132 if ((copy = end - offset) > 0) {
1138 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1140 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1141 offset - start, copy);
1142 kunmap_skb_frag(vaddr);
1144 if ((len -= copy) == 0)
1152 if (skb_shinfo(skb)->frag_list) {
1153 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1155 for (; list; list = list->next) {
1158 BUG_TRAP(start <= offset + len);
1160 end = start + list->len;
1161 if ((copy = end - offset) > 0) {
1164 if (skb_copy_bits(list, offset - start,
1167 if ((len -= copy) == 0)
1183 * skb_store_bits - store bits from kernel buffer to skb
1184 * @skb: destination buffer
1185 * @offset: offset in destination
1186 * @from: source buffer
1187 * @len: number of bytes to copy
1189 * Copy the specified number of bytes from the source buffer to the
1190 * destination skb. This function handles all the messy bits of
1191 * traversing fragment lists and such.
1194 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1197 int start = skb_headlen(skb);
1199 if (offset > (int)skb->len - len)
1202 if ((copy = start - offset) > 0) {
1205 memcpy(skb->data + offset, from, copy);
1206 if ((len -= copy) == 0)
1212 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1213 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1216 BUG_TRAP(start <= offset + len);
1218 end = start + frag->size;
1219 if ((copy = end - offset) > 0) {
1225 vaddr = kmap_skb_frag(frag);
1226 memcpy(vaddr + frag->page_offset + offset - start,
1228 kunmap_skb_frag(vaddr);
1230 if ((len -= copy) == 0)
1238 if (skb_shinfo(skb)->frag_list) {
1239 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1241 for (; list; list = list->next) {
1244 BUG_TRAP(start <= offset + len);
1246 end = start + list->len;
1247 if ((copy = end - offset) > 0) {
1250 if (skb_store_bits(list, offset - start,
1253 if ((len -= copy) == 0)
1268 EXPORT_SYMBOL(skb_store_bits);
1270 /* Checksum skb data. */
1272 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1273 int len, __wsum csum)
1275 int start = skb_headlen(skb);
1276 int i, copy = start - offset;
1279 /* Checksum header. */
1283 csum = csum_partial(skb->data + offset, copy, csum);
1284 if ((len -= copy) == 0)
1290 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1293 BUG_TRAP(start <= offset + len);
1295 end = start + skb_shinfo(skb)->frags[i].size;
1296 if ((copy = end - offset) > 0) {
1299 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1303 vaddr = kmap_skb_frag(frag);
1304 csum2 = csum_partial(vaddr + frag->page_offset +
1305 offset - start, copy, 0);
1306 kunmap_skb_frag(vaddr);
1307 csum = csum_block_add(csum, csum2, pos);
1316 if (skb_shinfo(skb)->frag_list) {
1317 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1319 for (; list; list = list->next) {
1322 BUG_TRAP(start <= offset + len);
1324 end = start + list->len;
1325 if ((copy = end - offset) > 0) {
1329 csum2 = skb_checksum(list, offset - start,
1331 csum = csum_block_add(csum, csum2, pos);
1332 if ((len -= copy) == 0)
1345 /* Both of above in one bottle. */
1347 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1348 u8 *to, int len, __wsum csum)
1350 int start = skb_headlen(skb);
1351 int i, copy = start - offset;
1358 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1360 if ((len -= copy) == 0)
1367 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1370 BUG_TRAP(start <= offset + len);
1372 end = start + skb_shinfo(skb)->frags[i].size;
1373 if ((copy = end - offset) > 0) {
1376 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1380 vaddr = kmap_skb_frag(frag);
1381 csum2 = csum_partial_copy_nocheck(vaddr +
1385 kunmap_skb_frag(vaddr);
1386 csum = csum_block_add(csum, csum2, pos);
1396 if (skb_shinfo(skb)->frag_list) {
1397 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1399 for (; list; list = list->next) {
1403 BUG_TRAP(start <= offset + len);
1405 end = start + list->len;
1406 if ((copy = end - offset) > 0) {
1409 csum2 = skb_copy_and_csum_bits(list,
1412 csum = csum_block_add(csum, csum2, pos);
1413 if ((len -= copy) == 0)
1426 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1431 if (skb->ip_summed == CHECKSUM_PARTIAL)
1432 csstart = skb->h.raw - skb->data;
1434 csstart = skb_headlen(skb);
1436 BUG_ON(csstart > skb_headlen(skb));
1438 memcpy(to, skb->data, csstart);
1441 if (csstart != skb->len)
1442 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1443 skb->len - csstart, 0);
1445 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1446 long csstuff = csstart + skb->csum_offset;
1448 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1453 * skb_dequeue - remove from the head of the queue
1454 * @list: list to dequeue from
1456 * Remove the head of the list. The list lock is taken so the function
1457 * may be used safely with other locking list functions. The head item is
1458 * returned or %NULL if the list is empty.
1461 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1463 unsigned long flags;
1464 struct sk_buff *result;
1466 spin_lock_irqsave(&list->lock, flags);
1467 result = __skb_dequeue(list);
1468 spin_unlock_irqrestore(&list->lock, flags);
1473 * skb_dequeue_tail - remove from the tail of the queue
1474 * @list: list to dequeue from
1476 * Remove the tail of the list. The list lock is taken so the function
1477 * may be used safely with other locking list functions. The tail item is
1478 * returned or %NULL if the list is empty.
1480 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1482 unsigned long flags;
1483 struct sk_buff *result;
1485 spin_lock_irqsave(&list->lock, flags);
1486 result = __skb_dequeue_tail(list);
1487 spin_unlock_irqrestore(&list->lock, flags);
1492 * skb_queue_purge - empty a list
1493 * @list: list to empty
1495 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1496 * the list and one reference dropped. This function takes the list
1497 * lock and is atomic with respect to other list locking functions.
1499 void skb_queue_purge(struct sk_buff_head *list)
1501 struct sk_buff *skb;
1502 while ((skb = skb_dequeue(list)) != NULL)
1507 * skb_queue_head - queue a buffer at the list head
1508 * @list: list to use
1509 * @newsk: buffer to queue
1511 * Queue a buffer at the start of the list. This function takes the
1512 * list lock and can be used safely with other locking &sk_buff functions
1515 * A buffer cannot be placed on two lists at the same time.
1517 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1519 unsigned long flags;
1521 spin_lock_irqsave(&list->lock, flags);
1522 __skb_queue_head(list, newsk);
1523 spin_unlock_irqrestore(&list->lock, flags);
1527 * skb_queue_tail - queue a buffer at the list tail
1528 * @list: list to use
1529 * @newsk: buffer to queue
1531 * Queue a buffer at the tail of the list. This function takes the
1532 * list lock and can be used safely with other locking &sk_buff functions
1535 * A buffer cannot be placed on two lists at the same time.
1537 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1539 unsigned long flags;
1541 spin_lock_irqsave(&list->lock, flags);
1542 __skb_queue_tail(list, newsk);
1543 spin_unlock_irqrestore(&list->lock, flags);
1547 * skb_unlink - remove a buffer from a list
1548 * @skb: buffer to remove
1549 * @list: list to use
1551 * Remove a packet from a list. The list locks are taken and this
1552 * function is atomic with respect to other list locked calls
1554 * You must know what list the SKB is on.
1556 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1558 unsigned long flags;
1560 spin_lock_irqsave(&list->lock, flags);
1561 __skb_unlink(skb, list);
1562 spin_unlock_irqrestore(&list->lock, flags);
1566 * skb_append - append a buffer
1567 * @old: buffer to insert after
1568 * @newsk: buffer to insert
1569 * @list: list to use
1571 * Place a packet after a given packet in a list. The list locks are taken
1572 * and this function is atomic with respect to other list locked calls.
1573 * A buffer cannot be placed on two lists at the same time.
1575 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1577 unsigned long flags;
1579 spin_lock_irqsave(&list->lock, flags);
1580 __skb_append(old, newsk, list);
1581 spin_unlock_irqrestore(&list->lock, flags);
1586 * skb_insert - insert a buffer
1587 * @old: buffer to insert before
1588 * @newsk: buffer to insert
1589 * @list: list to use
1591 * Place a packet before a given packet in a list. The list locks are
1592 * taken and this function is atomic with respect to other list locked
1595 * A buffer cannot be placed on two lists at the same time.
1597 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1599 unsigned long flags;
1601 spin_lock_irqsave(&list->lock, flags);
1602 __skb_insert(newsk, old->prev, old, list);
1603 spin_unlock_irqrestore(&list->lock, flags);
1608 * Tune the memory allocator for a new MTU size.
1610 void skb_add_mtu(int mtu)
1612 /* Must match allocation in alloc_skb */
1613 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1615 kmem_add_cache_size(mtu);
1619 static inline void skb_split_inside_header(struct sk_buff *skb,
1620 struct sk_buff* skb1,
1621 const u32 len, const int pos)
1625 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1627 /* And move data appendix as is. */
1628 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1629 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1631 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1632 skb_shinfo(skb)->nr_frags = 0;
1633 skb1->data_len = skb->data_len;
1634 skb1->len += skb1->data_len;
1637 skb->tail = skb->data + len;
1640 static inline void skb_split_no_header(struct sk_buff *skb,
1641 struct sk_buff* skb1,
1642 const u32 len, int pos)
1645 const int nfrags = skb_shinfo(skb)->nr_frags;
1647 skb_shinfo(skb)->nr_frags = 0;
1648 skb1->len = skb1->data_len = skb->len - len;
1650 skb->data_len = len - pos;
1652 for (i = 0; i < nfrags; i++) {
1653 int size = skb_shinfo(skb)->frags[i].size;
1655 if (pos + size > len) {
1656 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1660 * We have two variants in this case:
1661 * 1. Move all the frag to the second
1662 * part, if it is possible. F.e.
1663 * this approach is mandatory for TUX,
1664 * where splitting is expensive.
1665 * 2. Split is accurately. We make this.
1667 get_page(skb_shinfo(skb)->frags[i].page);
1668 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1669 skb_shinfo(skb1)->frags[0].size -= len - pos;
1670 skb_shinfo(skb)->frags[i].size = len - pos;
1671 skb_shinfo(skb)->nr_frags++;
1675 skb_shinfo(skb)->nr_frags++;
1678 skb_shinfo(skb1)->nr_frags = k;
1682 * skb_split - Split fragmented skb to two parts at length len.
1683 * @skb: the buffer to split
1684 * @skb1: the buffer to receive the second part
1685 * @len: new length for skb
1687 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1689 int pos = skb_headlen(skb);
1691 if (len < pos) /* Split line is inside header. */
1692 skb_split_inside_header(skb, skb1, len, pos);
1693 else /* Second chunk has no header, nothing to copy. */
1694 skb_split_no_header(skb, skb1, len, pos);
1698 * skb_prepare_seq_read - Prepare a sequential read of skb data
1699 * @skb: the buffer to read
1700 * @from: lower offset of data to be read
1701 * @to: upper offset of data to be read
1702 * @st: state variable
1704 * Initializes the specified state variable. Must be called before
1705 * invoking skb_seq_read() for the first time.
1707 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1708 unsigned int to, struct skb_seq_state *st)
1710 st->lower_offset = from;
1711 st->upper_offset = to;
1712 st->root_skb = st->cur_skb = skb;
1713 st->frag_idx = st->stepped_offset = 0;
1714 st->frag_data = NULL;
1718 * skb_seq_read - Sequentially read skb data
1719 * @consumed: number of bytes consumed by the caller so far
1720 * @data: destination pointer for data to be returned
1721 * @st: state variable
1723 * Reads a block of skb data at &consumed relative to the
1724 * lower offset specified to skb_prepare_seq_read(). Assigns
1725 * the head of the data block to &data and returns the length
1726 * of the block or 0 if the end of the skb data or the upper
1727 * offset has been reached.
1729 * The caller is not required to consume all of the data
1730 * returned, i.e. &consumed is typically set to the number
1731 * of bytes already consumed and the next call to
1732 * skb_seq_read() will return the remaining part of the block.
1734 * Note: The size of each block of data returned can be arbitary,
1735 * this limitation is the cost for zerocopy seqeuental
1736 * reads of potentially non linear data.
1738 * Note: Fragment lists within fragments are not implemented
1739 * at the moment, state->root_skb could be replaced with
1740 * a stack for this purpose.
1742 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1743 struct skb_seq_state *st)
1745 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1748 if (unlikely(abs_offset >= st->upper_offset))
1752 block_limit = skb_headlen(st->cur_skb);
1754 if (abs_offset < block_limit) {
1755 *data = st->cur_skb->data + abs_offset;
1756 return block_limit - abs_offset;
1759 if (st->frag_idx == 0 && !st->frag_data)
1760 st->stepped_offset += skb_headlen(st->cur_skb);
1762 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1763 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1764 block_limit = frag->size + st->stepped_offset;
1766 if (abs_offset < block_limit) {
1768 st->frag_data = kmap_skb_frag(frag);
1770 *data = (u8 *) st->frag_data + frag->page_offset +
1771 (abs_offset - st->stepped_offset);
1773 return block_limit - abs_offset;
1776 if (st->frag_data) {
1777 kunmap_skb_frag(st->frag_data);
1778 st->frag_data = NULL;
1782 st->stepped_offset += frag->size;
1785 if (st->cur_skb->next) {
1786 st->cur_skb = st->cur_skb->next;
1789 } else if (st->root_skb == st->cur_skb &&
1790 skb_shinfo(st->root_skb)->frag_list) {
1791 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1799 * skb_abort_seq_read - Abort a sequential read of skb data
1800 * @st: state variable
1802 * Must be called if skb_seq_read() was not called until it
1805 void skb_abort_seq_read(struct skb_seq_state *st)
1808 kunmap_skb_frag(st->frag_data);
1811 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1813 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1814 struct ts_config *conf,
1815 struct ts_state *state)
1817 return skb_seq_read(offset, text, TS_SKB_CB(state));
1820 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1822 skb_abort_seq_read(TS_SKB_CB(state));
1826 * skb_find_text - Find a text pattern in skb data
1827 * @skb: the buffer to look in
1828 * @from: search offset
1830 * @config: textsearch configuration
1831 * @state: uninitialized textsearch state variable
1833 * Finds a pattern in the skb data according to the specified
1834 * textsearch configuration. Use textsearch_next() to retrieve
1835 * subsequent occurrences of the pattern. Returns the offset
1836 * to the first occurrence or UINT_MAX if no match was found.
1838 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1839 unsigned int to, struct ts_config *config,
1840 struct ts_state *state)
1844 config->get_next_block = skb_ts_get_next_block;
1845 config->finish = skb_ts_finish;
1847 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1849 ret = textsearch_find(config, state);
1850 return (ret <= to - from ? ret : UINT_MAX);
1854 * skb_append_datato_frags: - append the user data to a skb
1855 * @sk: sock structure
1856 * @skb: skb structure to be appened with user data.
1857 * @getfrag: call back function to be used for getting the user data
1858 * @from: pointer to user message iov
1859 * @length: length of the iov message
1861 * Description: This procedure append the user data in the fragment part
1862 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1864 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1865 int (*getfrag)(void *from, char *to, int offset,
1866 int len, int odd, struct sk_buff *skb),
1867 void *from, int length)
1870 skb_frag_t *frag = NULL;
1871 struct page *page = NULL;
1877 /* Return error if we don't have space for new frag */
1878 frg_cnt = skb_shinfo(skb)->nr_frags;
1879 if (frg_cnt >= MAX_SKB_FRAGS)
1882 /* allocate a new page for next frag */
1883 page = alloc_pages(sk->sk_allocation, 0);
1885 /* If alloc_page fails just return failure and caller will
1886 * free previous allocated pages by doing kfree_skb()
1891 /* initialize the next frag */
1892 sk->sk_sndmsg_page = page;
1893 sk->sk_sndmsg_off = 0;
1894 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1895 skb->truesize += PAGE_SIZE;
1896 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1898 /* get the new initialized frag */
1899 frg_cnt = skb_shinfo(skb)->nr_frags;
1900 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1902 /* copy the user data to page */
1903 left = PAGE_SIZE - frag->page_offset;
1904 copy = (length > left)? left : length;
1906 ret = getfrag(from, (page_address(frag->page) +
1907 frag->page_offset + frag->size),
1908 offset, copy, 0, skb);
1912 /* copy was successful so update the size parameters */
1913 sk->sk_sndmsg_off += copy;
1916 skb->data_len += copy;
1920 } while (length > 0);
1926 * skb_pull_rcsum - pull skb and update receive checksum
1927 * @skb: buffer to update
1928 * @start: start of data before pull
1929 * @len: length of data pulled
1931 * This function performs an skb_pull on the packet and updates
1932 * update the CHECKSUM_COMPLETE checksum. It should be used on
1933 * receive path processing instead of skb_pull unless you know
1934 * that the checksum difference is zero (e.g., a valid IP header)
1935 * or you are setting ip_summed to CHECKSUM_NONE.
1937 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1939 BUG_ON(len > skb->len);
1941 BUG_ON(skb->len < skb->data_len);
1942 skb_postpull_rcsum(skb, skb->data, len);
1943 return skb->data += len;
1946 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1949 * skb_segment - Perform protocol segmentation on skb.
1950 * @skb: buffer to segment
1951 * @features: features for the output path (see dev->features)
1953 * This function performs segmentation on the given skb. It returns
1954 * the segment at the given position. It returns NULL if there are
1955 * no more segments to generate, or when an error is encountered.
1957 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1959 struct sk_buff *segs = NULL;
1960 struct sk_buff *tail = NULL;
1961 unsigned int mss = skb_shinfo(skb)->gso_size;
1962 unsigned int doffset = skb->data - skb->mac.raw;
1963 unsigned int offset = doffset;
1964 unsigned int headroom;
1966 int sg = features & NETIF_F_SG;
1967 int nfrags = skb_shinfo(skb)->nr_frags;
1972 __skb_push(skb, doffset);
1973 headroom = skb_headroom(skb);
1974 pos = skb_headlen(skb);
1977 struct sk_buff *nskb;
1983 len = skb->len - offset;
1987 hsize = skb_headlen(skb) - offset;
1990 if (hsize > len || !sg)
1993 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1994 if (unlikely(!nskb))
2003 nskb->dev = skb->dev;
2004 nskb->priority = skb->priority;
2005 nskb->protocol = skb->protocol;
2006 nskb->dst = dst_clone(skb->dst);
2007 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2008 nskb->pkt_type = skb->pkt_type;
2009 nskb->mac_len = skb->mac_len;
2011 skb_reserve(nskb, headroom);
2012 nskb->mac.raw = nskb->data;
2013 nskb->nh.raw = nskb->data + skb->mac_len;
2014 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
2015 memcpy(skb_put(nskb, doffset), skb->data, doffset);
2018 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2024 frag = skb_shinfo(nskb)->frags;
2027 nskb->ip_summed = CHECKSUM_PARTIAL;
2028 nskb->csum = skb->csum;
2029 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2031 while (pos < offset + len) {
2032 BUG_ON(i >= nfrags);
2034 *frag = skb_shinfo(skb)->frags[i];
2035 get_page(frag->page);
2039 frag->page_offset += offset - pos;
2040 frag->size -= offset - pos;
2045 if (pos + size <= offset + len) {
2049 frag->size -= pos + size - (offset + len);
2056 skb_shinfo(nskb)->nr_frags = k;
2057 nskb->data_len = len - hsize;
2058 nskb->len += nskb->data_len;
2059 nskb->truesize += nskb->data_len;
2060 } while ((offset += len) < skb->len);
2065 while ((skb = segs)) {
2069 return ERR_PTR(err);
2072 EXPORT_SYMBOL_GPL(skb_segment);
2074 void __init skb_init(void)
2076 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2077 sizeof(struct sk_buff),
2079 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2081 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2082 (2*sizeof(struct sk_buff)) +
2085 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2089 EXPORT_SYMBOL(___pskb_trim);
2090 EXPORT_SYMBOL(__kfree_skb);
2091 EXPORT_SYMBOL(kfree_skb);
2092 EXPORT_SYMBOL(__pskb_pull_tail);
2093 EXPORT_SYMBOL(__alloc_skb);
2094 EXPORT_SYMBOL(__netdev_alloc_skb);
2095 EXPORT_SYMBOL(pskb_copy);
2096 EXPORT_SYMBOL(pskb_expand_head);
2097 EXPORT_SYMBOL(skb_checksum);
2098 EXPORT_SYMBOL(skb_clone);
2099 EXPORT_SYMBOL(skb_clone_fraglist);
2100 EXPORT_SYMBOL(skb_copy);
2101 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2102 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2103 EXPORT_SYMBOL(skb_copy_bits);
2104 EXPORT_SYMBOL(skb_copy_expand);
2105 EXPORT_SYMBOL(skb_over_panic);
2106 EXPORT_SYMBOL(skb_pad);
2107 EXPORT_SYMBOL(skb_realloc_headroom);
2108 EXPORT_SYMBOL(skb_under_panic);
2109 EXPORT_SYMBOL(skb_dequeue);
2110 EXPORT_SYMBOL(skb_dequeue_tail);
2111 EXPORT_SYMBOL(skb_insert);
2112 EXPORT_SYMBOL(skb_queue_purge);
2113 EXPORT_SYMBOL(skb_queue_head);
2114 EXPORT_SYMBOL(skb_queue_tail);
2115 EXPORT_SYMBOL(skb_unlink);
2116 EXPORT_SYMBOL(skb_append);
2117 EXPORT_SYMBOL(skb_split);
2118 EXPORT_SYMBOL(skb_prepare_seq_read);
2119 EXPORT_SYMBOL(skb_seq_read);
2120 EXPORT_SYMBOL(skb_abort_seq_read);
2121 EXPORT_SYMBOL(skb_find_text);
2122 EXPORT_SYMBOL(skb_append_datato_frags);