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>
59 #include <linux/highmem.h>
61 #include <net/protocol.h>
64 #include <net/checksum.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
70 static kmem_cache_t *skbuff_head_cache __read_mostly;
71 static kmem_cache_t *skbuff_fclone_cache __read_mostly;
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
80 * skb_over_panic - private function
85 * Out of line support code for skb_put(). Not user callable.
87 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
97 * skb_under_panic - private function
102 * Out of line support code for skb_push(). Not user callable.
105 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
114 void skb_truesize_bug(struct sk_buff *skb)
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
120 EXPORT_SYMBOL(skb_truesize_bug);
122 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
129 * __alloc_skb - allocate a network buffer
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
142 #ifndef CONFIG_HAVE_ARCH_ALLOC_SKB
143 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
147 struct skb_shared_info *shinfo;
151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
154 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
160 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
164 memset(skb, 0, offsetof(struct sk_buff, truesize));
165 skb->truesize = size + sizeof(struct sk_buff);
166 atomic_set(&skb->users, 1);
170 skb->end = data + size;
171 /* make sure we initialize shinfo sequentially */
172 shinfo = skb_shinfo(skb);
173 atomic_set(&shinfo->dataref, 1);
174 shinfo->nr_frags = 0;
175 shinfo->gso_size = 0;
176 shinfo->gso_segs = 0;
177 shinfo->gso_type = 0;
178 shinfo->ip6_frag_id = 0;
179 shinfo->frag_list = NULL;
182 struct sk_buff *child = skb + 1;
183 atomic_t *fclone_ref = (atomic_t *) (child + 1);
185 skb->fclone = SKB_FCLONE_ORIG;
186 atomic_set(fclone_ref, 1);
188 child->fclone = SKB_FCLONE_UNAVAILABLE;
193 kmem_cache_free(cache, skb);
197 #endif /* !CONFIG_HAVE_ARCH_ALLOC_SKB */
200 * alloc_skb_from_cache - allocate a network buffer
201 * @cp: kmem_cache from which to allocate the data area
202 * (object size must be big enough for @size bytes + skb overheads)
203 * @size: size to allocate
204 * @gfp_mask: allocation mask
206 * Allocate a new &sk_buff. The returned buffer has no headroom and
207 * tail room of size bytes. The object has a reference count of one.
208 * The return is the buffer. On a failure the return is %NULL.
210 * Buffers may only be allocated from interrupts using a @gfp_mask of
213 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
222 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
225 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
230 size = SKB_DATA_ALIGN(size);
231 data = kmem_cache_alloc(cp, gfp_mask);
235 memset(skb, 0, offsetof(struct sk_buff, truesize));
236 skb->truesize = size + sizeof(struct sk_buff);
237 atomic_set(&skb->users, 1);
241 skb->end = data + size;
243 atomic_set(&(skb_shinfo(skb)->dataref), 1);
244 skb_shinfo(skb)->nr_frags = 0;
245 skb_shinfo(skb)->gso_size = 0;
246 skb_shinfo(skb)->gso_segs = 0;
247 skb_shinfo(skb)->gso_type = 0;
248 skb_shinfo(skb)->frag_list = NULL;
251 struct sk_buff *child = skb + 1;
252 atomic_t *fclone_ref = (atomic_t *) (child + 1);
254 skb->fclone = SKB_FCLONE_ORIG;
255 atomic_set(fclone_ref, 1);
257 child->fclone = SKB_FCLONE_UNAVAILABLE;
262 kmem_cache_free(cache, skb);
268 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
269 * @dev: network device to receive on
270 * @length: length to allocate
271 * @gfp_mask: get_free_pages mask, passed to alloc_skb
273 * Allocate a new &sk_buff and assign it a usage count of one. The
274 * buffer has unspecified headroom built in. Users should allocate
275 * the headroom they think they need without accounting for the
276 * built in space. The built in space is used for optimisations.
278 * %NULL is returned if there is no free memory.
280 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
281 unsigned int length, gfp_t gfp_mask)
285 skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
287 skb_reserve(skb, NET_SKB_PAD);
293 static void skb_drop_list(struct sk_buff **listp)
295 struct sk_buff *list = *listp;
300 struct sk_buff *this = list;
306 static inline void skb_drop_fraglist(struct sk_buff *skb)
308 skb_drop_list(&skb_shinfo(skb)->frag_list);
311 static void skb_clone_fraglist(struct sk_buff *skb)
313 struct sk_buff *list;
315 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
319 static void skb_release_data(struct sk_buff *skb)
322 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
323 &skb_shinfo(skb)->dataref)) {
324 if (skb_shinfo(skb)->nr_frags) {
326 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
327 put_page(skb_shinfo(skb)->frags[i].page);
330 if (skb_shinfo(skb)->frag_list)
331 skb_drop_fraglist(skb);
338 * Free an skbuff by memory without cleaning the state.
340 void kfree_skbmem(struct sk_buff *skb)
342 struct sk_buff *other;
343 atomic_t *fclone_ref;
345 skb_release_data(skb);
346 switch (skb->fclone) {
347 case SKB_FCLONE_UNAVAILABLE:
348 kmem_cache_free(skbuff_head_cache, skb);
351 case SKB_FCLONE_ORIG:
352 fclone_ref = (atomic_t *) (skb + 2);
353 if (atomic_dec_and_test(fclone_ref))
354 kmem_cache_free(skbuff_fclone_cache, skb);
357 case SKB_FCLONE_CLONE:
358 fclone_ref = (atomic_t *) (skb + 1);
361 /* The clone portion is available for
362 * fast-cloning again.
364 skb->fclone = SKB_FCLONE_UNAVAILABLE;
366 if (atomic_dec_and_test(fclone_ref))
367 kmem_cache_free(skbuff_fclone_cache, other);
373 * __kfree_skb - private function
376 * Free an sk_buff. Release anything attached to the buffer.
377 * Clean the state. This is an internal helper function. Users should
378 * always call kfree_skb
381 void __kfree_skb(struct sk_buff *skb)
383 dst_release(skb->dst);
385 secpath_put(skb->sp);
387 if (skb->destructor) {
389 skb->destructor(skb);
391 #ifdef CONFIG_NETFILTER
392 nf_conntrack_put(skb->nfct);
393 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
394 nf_conntrack_put_reasm(skb->nfct_reasm);
396 #ifdef CONFIG_BRIDGE_NETFILTER
397 nf_bridge_put(skb->nf_bridge);
400 /* XXX: IS this still necessary? - JHS */
401 #ifdef CONFIG_NET_SCHED
403 #ifdef CONFIG_NET_CLS_ACT
412 * kfree_skb - free an sk_buff
413 * @skb: buffer to free
415 * Drop a reference to the buffer and free it if the usage count has
418 void kfree_skb(struct sk_buff *skb)
422 if (likely(atomic_read(&skb->users) == 1))
424 else if (likely(!atomic_dec_and_test(&skb->users)))
430 * skb_clone - duplicate an sk_buff
431 * @skb: buffer to clone
432 * @gfp_mask: allocation priority
434 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
435 * copies share the same packet data but not structure. The new
436 * buffer has a reference count of 1. If the allocation fails the
437 * function returns %NULL otherwise the new buffer is returned.
439 * If this function is called from an interrupt gfp_mask() must be
443 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
448 if (skb->fclone == SKB_FCLONE_ORIG &&
449 n->fclone == SKB_FCLONE_UNAVAILABLE) {
450 atomic_t *fclone_ref = (atomic_t *) (n + 1);
451 n->fclone = SKB_FCLONE_CLONE;
452 atomic_inc(fclone_ref);
454 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
457 n->fclone = SKB_FCLONE_UNAVAILABLE;
460 #define C(x) n->x = skb->x
462 n->next = n->prev = NULL;
473 secpath_get(skb->sp);
475 memcpy(n->cb, skb->cb, sizeof(skb->cb));
489 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
493 n->destructor = NULL;
494 #ifdef CONFIG_NETFILTER
497 nf_conntrack_get(skb->nfct);
499 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
501 nf_conntrack_get_reasm(skb->nfct_reasm);
503 #ifdef CONFIG_BRIDGE_NETFILTER
505 nf_bridge_get(skb->nf_bridge);
507 #endif /*CONFIG_NETFILTER*/
508 #ifdef CONFIG_NET_SCHED
510 #ifdef CONFIG_NET_CLS_ACT
511 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
512 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
513 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
516 skb_copy_secmark(n, skb);
519 atomic_set(&n->users, 1);
525 atomic_inc(&(skb_shinfo(skb)->dataref));
531 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
534 * Shift between the two data areas in bytes
536 unsigned long offset = new->data - old->data;
540 new->priority = old->priority;
541 new->protocol = old->protocol;
542 new->dst = dst_clone(old->dst);
544 new->sp = secpath_get(old->sp);
546 new->h.raw = old->h.raw + offset;
547 new->nh.raw = old->nh.raw + offset;
548 new->mac.raw = old->mac.raw + offset;
549 memcpy(new->cb, old->cb, sizeof(old->cb));
550 new->local_df = old->local_df;
551 new->fclone = SKB_FCLONE_UNAVAILABLE;
552 new->pkt_type = old->pkt_type;
553 new->tstamp = old->tstamp;
554 new->destructor = NULL;
555 #ifdef CONFIG_NETFILTER
556 new->nfmark = old->nfmark;
557 new->nfct = old->nfct;
558 nf_conntrack_get(old->nfct);
559 new->nfctinfo = old->nfctinfo;
560 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
561 new->nfct_reasm = old->nfct_reasm;
562 nf_conntrack_get_reasm(old->nfct_reasm);
564 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
565 new->ipvs_property = old->ipvs_property;
567 #ifdef CONFIG_BRIDGE_NETFILTER
568 new->nf_bridge = old->nf_bridge;
569 nf_bridge_get(old->nf_bridge);
572 #ifdef CONFIG_NET_SCHED
573 #ifdef CONFIG_NET_CLS_ACT
574 new->tc_verd = old->tc_verd;
576 new->tc_index = old->tc_index;
578 skb_copy_secmark(new, old);
579 atomic_set(&new->users, 1);
580 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
581 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
582 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
586 * skb_copy - create private copy of an sk_buff
587 * @skb: buffer to copy
588 * @gfp_mask: allocation priority
590 * Make a copy of both an &sk_buff and its data. This is used when the
591 * caller wishes to modify the data and needs a private copy of the
592 * data to alter. Returns %NULL on failure or the pointer to the buffer
593 * on success. The returned buffer has a reference count of 1.
595 * As by-product this function converts non-linear &sk_buff to linear
596 * one, so that &sk_buff becomes completely private and caller is allowed
597 * to modify all the data of returned buffer. This means that this
598 * function is not recommended for use in circumstances when only
599 * header is going to be modified. Use pskb_copy() instead.
602 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
604 int headerlen = skb->data - skb->head;
606 * Allocate the copy buffer
608 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
613 /* Set the data pointer */
614 skb_reserve(n, headerlen);
615 /* Set the tail pointer and length */
616 skb_put(n, skb->len);
618 n->ip_summed = skb->ip_summed;
620 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
623 copy_skb_header(n, skb);
629 * pskb_copy - create copy of an sk_buff with private head.
630 * @skb: buffer to copy
631 * @gfp_mask: allocation priority
633 * Make a copy of both an &sk_buff and part of its data, located
634 * in header. Fragmented data remain shared. This is used when
635 * the caller wishes to modify only header of &sk_buff and needs
636 * private copy of the header to alter. Returns %NULL on failure
637 * or the pointer to the buffer on success.
638 * The returned buffer has a reference count of 1.
641 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
644 * Allocate the copy buffer
646 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
651 /* Set the data pointer */
652 skb_reserve(n, skb->data - skb->head);
653 /* Set the tail pointer and length */
654 skb_put(n, skb_headlen(skb));
656 memcpy(n->data, skb->data, n->len);
658 n->ip_summed = skb->ip_summed;
660 n->data_len = skb->data_len;
663 if (skb_shinfo(skb)->nr_frags) {
666 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
667 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
668 get_page(skb_shinfo(n)->frags[i].page);
670 skb_shinfo(n)->nr_frags = i;
673 if (skb_shinfo(skb)->frag_list) {
674 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
675 skb_clone_fraglist(n);
678 copy_skb_header(n, skb);
684 * pskb_expand_head - reallocate header of &sk_buff
685 * @skb: buffer to reallocate
686 * @nhead: room to add at head
687 * @ntail: room to add at tail
688 * @gfp_mask: allocation priority
690 * Expands (or creates identical copy, if &nhead and &ntail are zero)
691 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
692 * reference count of 1. Returns zero in the case of success or error,
693 * if expansion failed. In the last case, &sk_buff is not changed.
695 * All the pointers pointing into skb header may change and must be
696 * reloaded after call to this function.
699 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
704 int size = nhead + (skb->end - skb->head) + ntail;
710 size = SKB_DATA_ALIGN(size);
712 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
716 /* Copy only real data... and, alas, header. This should be
717 * optimized for the cases when header is void. */
718 memcpy(data + nhead, skb->head, skb->tail - skb->head);
719 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
721 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
722 get_page(skb_shinfo(skb)->frags[i].page);
724 if (skb_shinfo(skb)->frag_list)
725 skb_clone_fraglist(skb);
727 skb_release_data(skb);
729 off = (data + nhead) - skb->head;
732 skb->end = data + size;
740 atomic_set(&skb_shinfo(skb)->dataref, 1);
747 /* Make private copy of skb with writable head and some headroom */
749 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
751 struct sk_buff *skb2;
752 int delta = headroom - skb_headroom(skb);
755 skb2 = pskb_copy(skb, GFP_ATOMIC);
757 skb2 = skb_clone(skb, GFP_ATOMIC);
758 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
769 * skb_copy_expand - copy and expand sk_buff
770 * @skb: buffer to copy
771 * @newheadroom: new free bytes at head
772 * @newtailroom: new free bytes at tail
773 * @gfp_mask: allocation priority
775 * Make a copy of both an &sk_buff and its data and while doing so
776 * allocate additional space.
778 * This is used when the caller wishes to modify the data and needs a
779 * private copy of the data to alter as well as more space for new fields.
780 * Returns %NULL on failure or the pointer to the buffer
781 * on success. The returned buffer has a reference count of 1.
783 * You must pass %GFP_ATOMIC as the allocation priority if this function
784 * is called from an interrupt.
786 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
787 * only by netfilter in the cases when checksum is recalculated? --ANK
789 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
790 int newheadroom, int newtailroom,
794 * Allocate the copy buffer
796 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
798 int head_copy_len, head_copy_off;
803 skb_reserve(n, newheadroom);
805 /* Set the tail pointer and length */
806 skb_put(n, skb->len);
808 head_copy_len = skb_headroom(skb);
810 if (newheadroom <= head_copy_len)
811 head_copy_len = newheadroom;
813 head_copy_off = newheadroom - head_copy_len;
815 /* Copy the linear header and data. */
816 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
817 skb->len + head_copy_len))
820 copy_skb_header(n, skb);
826 * skb_pad - zero pad the tail of an skb
827 * @skb: buffer to pad
830 * Ensure that a buffer is followed by a padding area that is zero
831 * filled. Used by network drivers which may DMA or transfer data
832 * beyond the buffer end onto the wire.
834 * May return error in out of memory cases. The skb is freed on error.
837 int skb_pad(struct sk_buff *skb, int pad)
842 /* If the skbuff is non linear tailroom is always zero.. */
843 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
844 memset(skb->data+skb->len, 0, pad);
848 ntail = skb->data_len + pad - (skb->end - skb->tail);
849 if (likely(skb_cloned(skb) || ntail > 0)) {
850 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
855 /* FIXME: The use of this function with non-linear skb's really needs
858 err = skb_linearize(skb);
862 memset(skb->data + skb->len, 0, pad);
870 /* Trims skb to length len. It can change skb pointers.
873 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
875 struct sk_buff **fragp;
876 struct sk_buff *frag;
877 int offset = skb_headlen(skb);
878 int nfrags = skb_shinfo(skb)->nr_frags;
882 if (skb_cloned(skb) &&
883 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
890 for (; i < nfrags; i++) {
891 int end = offset + skb_shinfo(skb)->frags[i].size;
898 skb_shinfo(skb)->frags[i++].size = len - offset;
901 skb_shinfo(skb)->nr_frags = i;
903 for (; i < nfrags; i++)
904 put_page(skb_shinfo(skb)->frags[i].page);
906 if (skb_shinfo(skb)->frag_list)
907 skb_drop_fraglist(skb);
911 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
912 fragp = &frag->next) {
913 int end = offset + frag->len;
915 if (skb_shared(frag)) {
916 struct sk_buff *nfrag;
918 nfrag = skb_clone(frag, GFP_ATOMIC);
919 if (unlikely(!nfrag))
922 nfrag->next = frag->next;
934 unlikely((err = pskb_trim(frag, len - offset))))
938 skb_drop_list(&frag->next);
943 if (len > skb_headlen(skb)) {
944 skb->data_len -= skb->len - len;
949 skb->tail = skb->data + len;
956 * __pskb_pull_tail - advance tail of skb header
957 * @skb: buffer to reallocate
958 * @delta: number of bytes to advance tail
960 * The function makes a sense only on a fragmented &sk_buff,
961 * it expands header moving its tail forward and copying necessary
962 * data from fragmented part.
964 * &sk_buff MUST have reference count of 1.
966 * Returns %NULL (and &sk_buff does not change) if pull failed
967 * or value of new tail of skb in the case of success.
969 * All the pointers pointing into skb header may change and must be
970 * reloaded after call to this function.
973 /* Moves tail of skb head forward, copying data from fragmented part,
974 * when it is necessary.
975 * 1. It may fail due to malloc failure.
976 * 2. It may change skb pointers.
978 * It is pretty complicated. Luckily, it is called only in exceptional cases.
980 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
982 /* If skb has not enough free space at tail, get new one
983 * plus 128 bytes for future expansions. If we have enough
984 * room at tail, reallocate without expansion only if skb is cloned.
986 int i, k, eat = (skb->tail + delta) - skb->end;
988 if (eat > 0 || skb_cloned(skb)) {
989 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
994 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
997 /* Optimization: no fragments, no reasons to preestimate
998 * size of pulled pages. Superb.
1000 if (!skb_shinfo(skb)->frag_list)
1003 /* Estimate size of pulled pages. */
1005 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1006 if (skb_shinfo(skb)->frags[i].size >= eat)
1008 eat -= skb_shinfo(skb)->frags[i].size;
1011 /* If we need update frag list, we are in troubles.
1012 * Certainly, it possible to add an offset to skb data,
1013 * but taking into account that pulling is expected to
1014 * be very rare operation, it is worth to fight against
1015 * further bloating skb head and crucify ourselves here instead.
1016 * Pure masohism, indeed. 8)8)
1019 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1020 struct sk_buff *clone = NULL;
1021 struct sk_buff *insp = NULL;
1026 if (list->len <= eat) {
1027 /* Eaten as whole. */
1032 /* Eaten partially. */
1034 if (skb_shared(list)) {
1035 /* Sucks! We need to fork list. :-( */
1036 clone = skb_clone(list, GFP_ATOMIC);
1042 /* This may be pulled without
1046 if (!pskb_pull(list, eat)) {
1055 /* Free pulled out fragments. */
1056 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1057 skb_shinfo(skb)->frag_list = list->next;
1060 /* And insert new clone at head. */
1063 skb_shinfo(skb)->frag_list = clone;
1066 /* Success! Now we may commit changes to skb data. */
1071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1072 if (skb_shinfo(skb)->frags[i].size <= eat) {
1073 put_page(skb_shinfo(skb)->frags[i].page);
1074 eat -= skb_shinfo(skb)->frags[i].size;
1076 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1078 skb_shinfo(skb)->frags[k].page_offset += eat;
1079 skb_shinfo(skb)->frags[k].size -= eat;
1085 skb_shinfo(skb)->nr_frags = k;
1088 skb->data_len -= delta;
1093 /* Copy some data bits from skb to kernel buffer. */
1095 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1098 int start = skb_headlen(skb);
1100 if (offset > (int)skb->len - len)
1104 if ((copy = start - offset) > 0) {
1107 memcpy(to, skb->data + offset, copy);
1108 if ((len -= copy) == 0)
1114 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1117 BUG_TRAP(start <= offset + len);
1119 end = start + skb_shinfo(skb)->frags[i].size;
1120 if ((copy = end - offset) > 0) {
1126 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1128 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1129 offset - start, copy);
1130 kunmap_skb_frag(vaddr);
1132 if ((len -= copy) == 0)
1140 if (skb_shinfo(skb)->frag_list) {
1141 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1143 for (; list; list = list->next) {
1146 BUG_TRAP(start <= offset + len);
1148 end = start + list->len;
1149 if ((copy = end - offset) > 0) {
1152 if (skb_copy_bits(list, offset - start,
1155 if ((len -= copy) == 0)
1171 * skb_store_bits - store bits from kernel buffer to skb
1172 * @skb: destination buffer
1173 * @offset: offset in destination
1174 * @from: source buffer
1175 * @len: number of bytes to copy
1177 * Copy the specified number of bytes from the source buffer to the
1178 * destination skb. This function handles all the messy bits of
1179 * traversing fragment lists and such.
1182 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1185 int start = skb_headlen(skb);
1187 if (offset > (int)skb->len - len)
1190 if ((copy = start - offset) > 0) {
1193 memcpy(skb->data + offset, from, copy);
1194 if ((len -= copy) == 0)
1200 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1201 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1204 BUG_TRAP(start <= offset + len);
1206 end = start + frag->size;
1207 if ((copy = end - offset) > 0) {
1213 vaddr = kmap_skb_frag(frag);
1214 memcpy(vaddr + frag->page_offset + offset - start,
1216 kunmap_skb_frag(vaddr);
1218 if ((len -= copy) == 0)
1226 if (skb_shinfo(skb)->frag_list) {
1227 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1229 for (; list; list = list->next) {
1232 BUG_TRAP(start <= offset + len);
1234 end = start + list->len;
1235 if ((copy = end - offset) > 0) {
1238 if (skb_store_bits(list, offset - start,
1241 if ((len -= copy) == 0)
1256 EXPORT_SYMBOL(skb_store_bits);
1258 /* Checksum skb data. */
1260 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1261 int len, unsigned int csum)
1263 int start = skb_headlen(skb);
1264 int i, copy = start - offset;
1267 /* Checksum header. */
1271 csum = csum_partial(skb->data + offset, copy, csum);
1272 if ((len -= copy) == 0)
1278 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1281 BUG_TRAP(start <= offset + len);
1283 end = start + skb_shinfo(skb)->frags[i].size;
1284 if ((copy = end - offset) > 0) {
1287 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1291 vaddr = kmap_skb_frag(frag);
1292 csum2 = csum_partial(vaddr + frag->page_offset +
1293 offset - start, copy, 0);
1294 kunmap_skb_frag(vaddr);
1295 csum = csum_block_add(csum, csum2, pos);
1304 if (skb_shinfo(skb)->frag_list) {
1305 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1307 for (; list; list = list->next) {
1310 BUG_TRAP(start <= offset + len);
1312 end = start + list->len;
1313 if ((copy = end - offset) > 0) {
1317 csum2 = skb_checksum(list, offset - start,
1319 csum = csum_block_add(csum, csum2, pos);
1320 if ((len -= copy) == 0)
1333 /* Both of above in one bottle. */
1335 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1336 u8 *to, int len, unsigned int csum)
1338 int start = skb_headlen(skb);
1339 int i, copy = start - offset;
1346 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1348 if ((len -= copy) == 0)
1355 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1358 BUG_TRAP(start <= offset + len);
1360 end = start + skb_shinfo(skb)->frags[i].size;
1361 if ((copy = end - offset) > 0) {
1364 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1368 vaddr = kmap_skb_frag(frag);
1369 csum2 = csum_partial_copy_nocheck(vaddr +
1373 kunmap_skb_frag(vaddr);
1374 csum = csum_block_add(csum, csum2, pos);
1384 if (skb_shinfo(skb)->frag_list) {
1385 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1387 for (; list; list = list->next) {
1391 BUG_TRAP(start <= offset + len);
1393 end = start + list->len;
1394 if ((copy = end - offset) > 0) {
1397 csum2 = skb_copy_and_csum_bits(list,
1400 csum = csum_block_add(csum, csum2, pos);
1401 if ((len -= copy) == 0)
1414 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1419 if (skb->ip_summed == CHECKSUM_HW)
1420 csstart = skb->h.raw - skb->data;
1422 csstart = skb_headlen(skb);
1424 BUG_ON(csstart > skb_headlen(skb));
1426 memcpy(to, skb->data, csstart);
1429 if (csstart != skb->len)
1430 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1431 skb->len - csstart, 0);
1433 if (skb->ip_summed == CHECKSUM_HW) {
1434 long csstuff = csstart + skb->csum;
1436 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1441 * skb_dequeue - remove from the head of the queue
1442 * @list: list to dequeue from
1444 * Remove the head of the list. The list lock is taken so the function
1445 * may be used safely with other locking list functions. The head item is
1446 * returned or %NULL if the list is empty.
1449 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1451 unsigned long flags;
1452 struct sk_buff *result;
1454 spin_lock_irqsave(&list->lock, flags);
1455 result = __skb_dequeue(list);
1456 spin_unlock_irqrestore(&list->lock, flags);
1461 * skb_dequeue_tail - remove from the tail of the queue
1462 * @list: list to dequeue from
1464 * Remove the tail of the list. The list lock is taken so the function
1465 * may be used safely with other locking list functions. The tail item is
1466 * returned or %NULL if the list is empty.
1468 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1470 unsigned long flags;
1471 struct sk_buff *result;
1473 spin_lock_irqsave(&list->lock, flags);
1474 result = __skb_dequeue_tail(list);
1475 spin_unlock_irqrestore(&list->lock, flags);
1480 * skb_queue_purge - empty a list
1481 * @list: list to empty
1483 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1484 * the list and one reference dropped. This function takes the list
1485 * lock and is atomic with respect to other list locking functions.
1487 void skb_queue_purge(struct sk_buff_head *list)
1489 struct sk_buff *skb;
1490 while ((skb = skb_dequeue(list)) != NULL)
1495 * skb_queue_head - queue a buffer at the list head
1496 * @list: list to use
1497 * @newsk: buffer to queue
1499 * Queue a buffer at the start of the list. This function takes the
1500 * list lock and can be used safely with other locking &sk_buff functions
1503 * A buffer cannot be placed on two lists at the same time.
1505 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1507 unsigned long flags;
1509 spin_lock_irqsave(&list->lock, flags);
1510 __skb_queue_head(list, newsk);
1511 spin_unlock_irqrestore(&list->lock, flags);
1515 * skb_queue_tail - queue a buffer at the list tail
1516 * @list: list to use
1517 * @newsk: buffer to queue
1519 * Queue a buffer at the tail of the list. This function takes the
1520 * list lock and can be used safely with other locking &sk_buff functions
1523 * A buffer cannot be placed on two lists at the same time.
1525 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1527 unsigned long flags;
1529 spin_lock_irqsave(&list->lock, flags);
1530 __skb_queue_tail(list, newsk);
1531 spin_unlock_irqrestore(&list->lock, flags);
1535 * skb_unlink - remove a buffer from a list
1536 * @skb: buffer to remove
1537 * @list: list to use
1539 * Remove a packet from a list. The list locks are taken and this
1540 * function is atomic with respect to other list locked calls
1542 * You must know what list the SKB is on.
1544 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1546 unsigned long flags;
1548 spin_lock_irqsave(&list->lock, flags);
1549 __skb_unlink(skb, list);
1550 spin_unlock_irqrestore(&list->lock, flags);
1554 * skb_append - append a buffer
1555 * @old: buffer to insert after
1556 * @newsk: buffer to insert
1557 * @list: list to use
1559 * Place a packet after a given packet in a list. The list locks are taken
1560 * and this function is atomic with respect to other list locked calls.
1561 * A buffer cannot be placed on two lists at the same time.
1563 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1565 unsigned long flags;
1567 spin_lock_irqsave(&list->lock, flags);
1568 __skb_append(old, newsk, list);
1569 spin_unlock_irqrestore(&list->lock, flags);
1574 * skb_insert - insert a buffer
1575 * @old: buffer to insert before
1576 * @newsk: buffer to insert
1577 * @list: list to use
1579 * Place a packet before a given packet in a list. The list locks are
1580 * taken and this function is atomic with respect to other list locked
1583 * A buffer cannot be placed on two lists at the same time.
1585 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1587 unsigned long flags;
1589 spin_lock_irqsave(&list->lock, flags);
1590 __skb_insert(newsk, old->prev, old, list);
1591 spin_unlock_irqrestore(&list->lock, flags);
1596 * Tune the memory allocator for a new MTU size.
1598 void skb_add_mtu(int mtu)
1600 /* Must match allocation in alloc_skb */
1601 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1603 kmem_add_cache_size(mtu);
1607 static inline void skb_split_inside_header(struct sk_buff *skb,
1608 struct sk_buff* skb1,
1609 const u32 len, const int pos)
1613 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1615 /* And move data appendix as is. */
1616 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1617 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1619 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1620 skb_shinfo(skb)->nr_frags = 0;
1621 skb1->data_len = skb->data_len;
1622 skb1->len += skb1->data_len;
1625 skb->tail = skb->data + len;
1628 static inline void skb_split_no_header(struct sk_buff *skb,
1629 struct sk_buff* skb1,
1630 const u32 len, int pos)
1633 const int nfrags = skb_shinfo(skb)->nr_frags;
1635 skb_shinfo(skb)->nr_frags = 0;
1636 skb1->len = skb1->data_len = skb->len - len;
1638 skb->data_len = len - pos;
1640 for (i = 0; i < nfrags; i++) {
1641 int size = skb_shinfo(skb)->frags[i].size;
1643 if (pos + size > len) {
1644 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1648 * We have two variants in this case:
1649 * 1. Move all the frag to the second
1650 * part, if it is possible. F.e.
1651 * this approach is mandatory for TUX,
1652 * where splitting is expensive.
1653 * 2. Split is accurately. We make this.
1655 get_page(skb_shinfo(skb)->frags[i].page);
1656 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1657 skb_shinfo(skb1)->frags[0].size -= len - pos;
1658 skb_shinfo(skb)->frags[i].size = len - pos;
1659 skb_shinfo(skb)->nr_frags++;
1663 skb_shinfo(skb)->nr_frags++;
1666 skb_shinfo(skb1)->nr_frags = k;
1670 * skb_split - Split fragmented skb to two parts at length len.
1671 * @skb: the buffer to split
1672 * @skb1: the buffer to receive the second part
1673 * @len: new length for skb
1675 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1677 int pos = skb_headlen(skb);
1679 if (len < pos) /* Split line is inside header. */
1680 skb_split_inside_header(skb, skb1, len, pos);
1681 else /* Second chunk has no header, nothing to copy. */
1682 skb_split_no_header(skb, skb1, len, pos);
1686 * skb_prepare_seq_read - Prepare a sequential read of skb data
1687 * @skb: the buffer to read
1688 * @from: lower offset of data to be read
1689 * @to: upper offset of data to be read
1690 * @st: state variable
1692 * Initializes the specified state variable. Must be called before
1693 * invoking skb_seq_read() for the first time.
1695 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1696 unsigned int to, struct skb_seq_state *st)
1698 st->lower_offset = from;
1699 st->upper_offset = to;
1700 st->root_skb = st->cur_skb = skb;
1701 st->frag_idx = st->stepped_offset = 0;
1702 st->frag_data = NULL;
1706 * skb_seq_read - Sequentially read skb data
1707 * @consumed: number of bytes consumed by the caller so far
1708 * @data: destination pointer for data to be returned
1709 * @st: state variable
1711 * Reads a block of skb data at &consumed relative to the
1712 * lower offset specified to skb_prepare_seq_read(). Assigns
1713 * the head of the data block to &data and returns the length
1714 * of the block or 0 if the end of the skb data or the upper
1715 * offset has been reached.
1717 * The caller is not required to consume all of the data
1718 * returned, i.e. &consumed is typically set to the number
1719 * of bytes already consumed and the next call to
1720 * skb_seq_read() will return the remaining part of the block.
1722 * Note: The size of each block of data returned can be arbitary,
1723 * this limitation is the cost for zerocopy seqeuental
1724 * reads of potentially non linear data.
1726 * Note: Fragment lists within fragments are not implemented
1727 * at the moment, state->root_skb could be replaced with
1728 * a stack for this purpose.
1730 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1731 struct skb_seq_state *st)
1733 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1736 if (unlikely(abs_offset >= st->upper_offset))
1740 block_limit = skb_headlen(st->cur_skb);
1742 if (abs_offset < block_limit) {
1743 *data = st->cur_skb->data + abs_offset;
1744 return block_limit - abs_offset;
1747 if (st->frag_idx == 0 && !st->frag_data)
1748 st->stepped_offset += skb_headlen(st->cur_skb);
1750 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1751 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1752 block_limit = frag->size + st->stepped_offset;
1754 if (abs_offset < block_limit) {
1756 st->frag_data = kmap_skb_frag(frag);
1758 *data = (u8 *) st->frag_data + frag->page_offset +
1759 (abs_offset - st->stepped_offset);
1761 return block_limit - abs_offset;
1764 if (st->frag_data) {
1765 kunmap_skb_frag(st->frag_data);
1766 st->frag_data = NULL;
1770 st->stepped_offset += frag->size;
1773 if (st->cur_skb->next) {
1774 st->cur_skb = st->cur_skb->next;
1777 } else if (st->root_skb == st->cur_skb &&
1778 skb_shinfo(st->root_skb)->frag_list) {
1779 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1787 * skb_abort_seq_read - Abort a sequential read of skb data
1788 * @st: state variable
1790 * Must be called if skb_seq_read() was not called until it
1793 void skb_abort_seq_read(struct skb_seq_state *st)
1796 kunmap_skb_frag(st->frag_data);
1799 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1801 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1802 struct ts_config *conf,
1803 struct ts_state *state)
1805 return skb_seq_read(offset, text, TS_SKB_CB(state));
1808 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1810 skb_abort_seq_read(TS_SKB_CB(state));
1814 * skb_find_text - Find a text pattern in skb data
1815 * @skb: the buffer to look in
1816 * @from: search offset
1818 * @config: textsearch configuration
1819 * @state: uninitialized textsearch state variable
1821 * Finds a pattern in the skb data according to the specified
1822 * textsearch configuration. Use textsearch_next() to retrieve
1823 * subsequent occurrences of the pattern. Returns the offset
1824 * to the first occurrence or UINT_MAX if no match was found.
1826 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1827 unsigned int to, struct ts_config *config,
1828 struct ts_state *state)
1832 config->get_next_block = skb_ts_get_next_block;
1833 config->finish = skb_ts_finish;
1835 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1837 ret = textsearch_find(config, state);
1838 return (ret <= to - from ? ret : UINT_MAX);
1842 * skb_append_datato_frags: - append the user data to a skb
1843 * @sk: sock structure
1844 * @skb: skb structure to be appened with user data.
1845 * @getfrag: call back function to be used for getting the user data
1846 * @from: pointer to user message iov
1847 * @length: length of the iov message
1849 * Description: This procedure append the user data in the fragment part
1850 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1852 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1853 int (*getfrag)(void *from, char *to, int offset,
1854 int len, int odd, struct sk_buff *skb),
1855 void *from, int length)
1858 skb_frag_t *frag = NULL;
1859 struct page *page = NULL;
1865 /* Return error if we don't have space for new frag */
1866 frg_cnt = skb_shinfo(skb)->nr_frags;
1867 if (frg_cnt >= MAX_SKB_FRAGS)
1870 /* allocate a new page for next frag */
1871 page = alloc_pages(sk->sk_allocation, 0);
1873 /* If alloc_page fails just return failure and caller will
1874 * free previous allocated pages by doing kfree_skb()
1879 /* initialize the next frag */
1880 sk->sk_sndmsg_page = page;
1881 sk->sk_sndmsg_off = 0;
1882 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1883 skb->truesize += PAGE_SIZE;
1884 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1886 /* get the new initialized frag */
1887 frg_cnt = skb_shinfo(skb)->nr_frags;
1888 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1890 /* copy the user data to page */
1891 left = PAGE_SIZE - frag->page_offset;
1892 copy = (length > left)? left : length;
1894 ret = getfrag(from, (page_address(frag->page) +
1895 frag->page_offset + frag->size),
1896 offset, copy, 0, skb);
1900 /* copy was successful so update the size parameters */
1901 sk->sk_sndmsg_off += copy;
1904 skb->data_len += copy;
1908 } while (length > 0);
1914 * skb_pull_rcsum - pull skb and update receive checksum
1915 * @skb: buffer to update
1916 * @start: start of data before pull
1917 * @len: length of data pulled
1919 * This function performs an skb_pull on the packet and updates
1920 * update the CHECKSUM_HW checksum. It should be used on receive
1921 * path processing instead of skb_pull unless you know that the
1922 * checksum difference is zero (e.g., a valid IP header) or you
1923 * are setting ip_summed to CHECKSUM_NONE.
1925 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1927 BUG_ON(len > skb->len);
1929 BUG_ON(skb->len < skb->data_len);
1930 skb_postpull_rcsum(skb, skb->data, len);
1931 return skb->data += len;
1934 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1937 * skb_segment - Perform protocol segmentation on skb.
1938 * @skb: buffer to segment
1939 * @features: features for the output path (see dev->features)
1941 * This function performs segmentation on the given skb. It returns
1942 * the segment at the given position. It returns NULL if there are
1943 * no more segments to generate, or when an error is encountered.
1945 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1947 struct sk_buff *segs = NULL;
1948 struct sk_buff *tail = NULL;
1949 unsigned int mss = skb_shinfo(skb)->gso_size;
1950 unsigned int doffset = skb->data - skb->mac.raw;
1951 unsigned int offset = doffset;
1952 unsigned int headroom;
1954 int sg = features & NETIF_F_SG;
1955 int nfrags = skb_shinfo(skb)->nr_frags;
1960 __skb_push(skb, doffset);
1961 headroom = skb_headroom(skb);
1962 pos = skb_headlen(skb);
1965 struct sk_buff *nskb;
1971 len = skb->len - offset;
1975 hsize = skb_headlen(skb) - offset;
1978 if (hsize > len || !sg)
1981 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1982 if (unlikely(!nskb))
1991 nskb->dev = skb->dev;
1992 nskb->priority = skb->priority;
1993 nskb->protocol = skb->protocol;
1994 nskb->dst = dst_clone(skb->dst);
1995 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1996 nskb->pkt_type = skb->pkt_type;
1997 nskb->mac_len = skb->mac_len;
1999 skb_reserve(nskb, headroom);
2000 nskb->mac.raw = nskb->data;
2001 nskb->nh.raw = nskb->data + skb->mac_len;
2002 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
2003 memcpy(skb_put(nskb, doffset), skb->data, doffset);
2006 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2012 frag = skb_shinfo(nskb)->frags;
2015 nskb->ip_summed = CHECKSUM_HW;
2016 nskb->csum = skb->csum;
2017 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2019 while (pos < offset + len) {
2020 BUG_ON(i >= nfrags);
2022 *frag = skb_shinfo(skb)->frags[i];
2023 get_page(frag->page);
2027 frag->page_offset += offset - pos;
2028 frag->size -= offset - pos;
2033 if (pos + size <= offset + len) {
2037 frag->size -= pos + size - (offset + len);
2044 skb_shinfo(nskb)->nr_frags = k;
2045 nskb->data_len = len - hsize;
2046 nskb->len += nskb->data_len;
2047 nskb->truesize += nskb->data_len;
2048 } while ((offset += len) < skb->len);
2053 while ((skb = segs)) {
2057 return ERR_PTR(err);
2060 EXPORT_SYMBOL_GPL(skb_segment);
2062 void __init skb_init(void)
2064 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2065 sizeof(struct sk_buff),
2069 if (!skbuff_head_cache)
2070 panic("cannot create skbuff cache");
2072 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2073 (2*sizeof(struct sk_buff)) +
2078 if (!skbuff_fclone_cache)
2079 panic("cannot create skbuff cache");
2082 EXPORT_SYMBOL(___pskb_trim);
2083 EXPORT_SYMBOL(__kfree_skb);
2084 EXPORT_SYMBOL(kfree_skb);
2085 EXPORT_SYMBOL(__pskb_pull_tail);
2086 EXPORT_SYMBOL(__alloc_skb);
2087 EXPORT_SYMBOL(__netdev_alloc_skb);
2088 EXPORT_SYMBOL(pskb_copy);
2089 EXPORT_SYMBOL(pskb_expand_head);
2090 EXPORT_SYMBOL(skb_checksum);
2091 EXPORT_SYMBOL(skb_clone);
2092 EXPORT_SYMBOL(skb_clone_fraglist);
2093 EXPORT_SYMBOL(skb_copy);
2094 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2095 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2096 EXPORT_SYMBOL(skb_copy_bits);
2097 EXPORT_SYMBOL(skb_copy_expand);
2098 EXPORT_SYMBOL(skb_over_panic);
2099 EXPORT_SYMBOL(skb_pad);
2100 EXPORT_SYMBOL(skb_realloc_headroom);
2101 EXPORT_SYMBOL(skb_under_panic);
2102 EXPORT_SYMBOL(skb_dequeue);
2103 EXPORT_SYMBOL(skb_dequeue_tail);
2104 EXPORT_SYMBOL(skb_insert);
2105 EXPORT_SYMBOL(skb_queue_purge);
2106 EXPORT_SYMBOL(skb_queue_head);
2107 EXPORT_SYMBOL(skb_queue_tail);
2108 EXPORT_SYMBOL(skb_unlink);
2109 EXPORT_SYMBOL(skb_append);
2110 EXPORT_SYMBOL(skb_split);
2111 EXPORT_SYMBOL(skb_prepare_seq_read);
2112 EXPORT_SYMBOL(skb_seq_read);
2113 EXPORT_SYMBOL(skb_abort_seq_read);
2114 EXPORT_SYMBOL(skb_find_text);
2115 EXPORT_SYMBOL(skb_append_datato_frags);