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 * lockdep: lock class key used by skb_queue_head_init():
76 struct lock_class_key skb_queue_lock_key;
78 EXPORT_SYMBOL(skb_queue_lock_key);
81 * Keep out-of-line to prevent kernel bloat.
82 * __builtin_return_address is not used because it is not always
87 * skb_over_panic - private function
92 * Out of line support code for skb_put(). Not user callable.
94 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
96 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
97 "data:%p tail:%p end:%p dev:%s\n",
98 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
99 skb->dev ? skb->dev->name : "<NULL>");
104 * skb_under_panic - private function
109 * Out of line support code for skb_push(). Not user callable.
112 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
114 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
115 "data:%p tail:%p end:%p dev:%s\n",
116 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
117 skb->dev ? skb->dev->name : "<NULL>");
121 void skb_truesize_bug(struct sk_buff *skb)
123 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
124 "len=%u, sizeof(sk_buff)=%Zd\n",
125 skb->truesize, skb->len, sizeof(struct sk_buff));
127 EXPORT_SYMBOL(skb_truesize_bug);
129 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
130 * 'private' fields and also do memory statistics to find all the
136 * __alloc_skb - allocate a network buffer
137 * @size: size to allocate
138 * @gfp_mask: allocation mask
139 * @fclone: allocate from fclone cache instead of head cache
140 * and allocate a cloned (child) skb
142 * Allocate a new &sk_buff. The returned buffer has no headroom and a
143 * tail room of size bytes. The object has a reference count of one.
144 * The return is the buffer. On a failure the return is %NULL.
146 * Buffers may only be allocated from interrupts using a @gfp_mask of
149 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
153 struct skb_shared_info *shinfo;
157 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
160 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
164 /* Get the DATA. Size must match skb_add_mtu(). */
165 size = SKB_DATA_ALIGN(size);
166 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
170 memset(skb, 0, offsetof(struct sk_buff, truesize));
171 skb->truesize = size + sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
176 skb->end = data + size;
177 /* make sure we initialize shinfo sequentially */
178 shinfo = skb_shinfo(skb);
179 atomic_set(&shinfo->dataref, 1);
180 shinfo->nr_frags = 0;
181 shinfo->gso_size = 0;
182 shinfo->gso_segs = 0;
183 shinfo->gso_type = 0;
184 shinfo->ip6_frag_id = 0;
185 shinfo->frag_list = NULL;
188 struct sk_buff *child = skb + 1;
189 atomic_t *fclone_ref = (atomic_t *) (child + 1);
191 skb->fclone = SKB_FCLONE_ORIG;
192 atomic_set(fclone_ref, 1);
194 child->fclone = SKB_FCLONE_UNAVAILABLE;
199 kmem_cache_free(cache, skb);
205 * alloc_skb_from_cache - allocate a network buffer
206 * @cp: kmem_cache from which to allocate the data area
207 * (object size must be big enough for @size bytes + skb overheads)
208 * @size: size to allocate
209 * @gfp_mask: allocation mask
211 * Allocate a new &sk_buff. The returned buffer has no headroom and
212 * tail room of size bytes. The object has a reference count of one.
213 * The return is the buffer. On a failure the return is %NULL.
215 * Buffers may only be allocated from interrupts using a @gfp_mask of
218 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
226 skb = kmem_cache_alloc(skbuff_head_cache,
227 gfp_mask & ~__GFP_DMA);
232 size = SKB_DATA_ALIGN(size);
233 data = kmem_cache_alloc(cp, gfp_mask);
237 memset(skb, 0, offsetof(struct sk_buff, truesize));
238 skb->truesize = size + sizeof(struct sk_buff);
239 atomic_set(&skb->users, 1);
243 skb->end = data + size;
245 atomic_set(&(skb_shinfo(skb)->dataref), 1);
246 skb_shinfo(skb)->nr_frags = 0;
247 skb_shinfo(skb)->gso_size = 0;
248 skb_shinfo(skb)->gso_segs = 0;
249 skb_shinfo(skb)->gso_type = 0;
250 skb_shinfo(skb)->frag_list = NULL;
254 kmem_cache_free(skbuff_head_cache, skb);
260 static void skb_drop_fraglist(struct sk_buff *skb)
262 struct sk_buff *list = skb_shinfo(skb)->frag_list;
264 skb_shinfo(skb)->frag_list = NULL;
267 struct sk_buff *this = list;
273 static void skb_clone_fraglist(struct sk_buff *skb)
275 struct sk_buff *list;
277 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
281 static void skb_release_data(struct sk_buff *skb)
284 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
285 &skb_shinfo(skb)->dataref)) {
286 if (skb_shinfo(skb)->nr_frags) {
288 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
289 put_page(skb_shinfo(skb)->frags[i].page);
292 if (skb_shinfo(skb)->frag_list)
293 skb_drop_fraglist(skb);
300 * Free an skbuff by memory without cleaning the state.
302 void kfree_skbmem(struct sk_buff *skb)
304 struct sk_buff *other;
305 atomic_t *fclone_ref;
307 skb_release_data(skb);
308 switch (skb->fclone) {
309 case SKB_FCLONE_UNAVAILABLE:
310 kmem_cache_free(skbuff_head_cache, skb);
313 case SKB_FCLONE_ORIG:
314 fclone_ref = (atomic_t *) (skb + 2);
315 if (atomic_dec_and_test(fclone_ref))
316 kmem_cache_free(skbuff_fclone_cache, skb);
319 case SKB_FCLONE_CLONE:
320 fclone_ref = (atomic_t *) (skb + 1);
323 /* The clone portion is available for
324 * fast-cloning again.
326 skb->fclone = SKB_FCLONE_UNAVAILABLE;
328 if (atomic_dec_and_test(fclone_ref))
329 kmem_cache_free(skbuff_fclone_cache, other);
335 * __kfree_skb - private function
338 * Free an sk_buff. Release anything attached to the buffer.
339 * Clean the state. This is an internal helper function. Users should
340 * always call kfree_skb
343 void __kfree_skb(struct sk_buff *skb)
345 dst_release(skb->dst);
347 secpath_put(skb->sp);
349 if (skb->destructor) {
351 skb->destructor(skb);
353 #ifdef CONFIG_NETFILTER
354 nf_conntrack_put(skb->nfct);
355 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
356 nf_conntrack_put_reasm(skb->nfct_reasm);
358 #ifdef CONFIG_BRIDGE_NETFILTER
359 nf_bridge_put(skb->nf_bridge);
362 /* XXX: IS this still necessary? - JHS */
363 #ifdef CONFIG_NET_SCHED
365 #ifdef CONFIG_NET_CLS_ACT
374 * kfree_skb - free an sk_buff
375 * @skb: buffer to free
377 * Drop a reference to the buffer and free it if the usage count has
380 void kfree_skb(struct sk_buff *skb)
384 if (likely(atomic_read(&skb->users) == 1))
386 else if (likely(!atomic_dec_and_test(&skb->users)))
392 * skb_clone - duplicate an sk_buff
393 * @skb: buffer to clone
394 * @gfp_mask: allocation priority
396 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
397 * copies share the same packet data but not structure. The new
398 * buffer has a reference count of 1. If the allocation fails the
399 * function returns %NULL otherwise the new buffer is returned.
401 * If this function is called from an interrupt gfp_mask() must be
405 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
410 if (skb->fclone == SKB_FCLONE_ORIG &&
411 n->fclone == SKB_FCLONE_UNAVAILABLE) {
412 atomic_t *fclone_ref = (atomic_t *) (n + 1);
413 n->fclone = SKB_FCLONE_CLONE;
414 atomic_inc(fclone_ref);
416 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
419 n->fclone = SKB_FCLONE_UNAVAILABLE;
422 #define C(x) n->x = skb->x
424 n->next = n->prev = NULL;
435 secpath_get(skb->sp);
437 memcpy(n->cb, skb->cb, sizeof(skb->cb));
447 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
451 n->destructor = NULL;
452 #ifdef CONFIG_NETFILTER
455 nf_conntrack_get(skb->nfct);
457 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
459 nf_conntrack_get_reasm(skb->nfct_reasm);
461 #ifdef CONFIG_BRIDGE_NETFILTER
463 nf_bridge_get(skb->nf_bridge);
465 #endif /*CONFIG_NETFILTER*/
466 #ifdef CONFIG_NET_SCHED
468 #ifdef CONFIG_NET_CLS_ACT
469 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
470 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
471 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
474 skb_copy_secmark(n, skb);
477 atomic_set(&n->users, 1);
483 atomic_inc(&(skb_shinfo(skb)->dataref));
489 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
492 * Shift between the two data areas in bytes
494 unsigned long offset = new->data - old->data;
498 new->priority = old->priority;
499 new->protocol = old->protocol;
500 new->dst = dst_clone(old->dst);
502 new->sp = secpath_get(old->sp);
504 new->h.raw = old->h.raw + offset;
505 new->nh.raw = old->nh.raw + offset;
506 new->mac.raw = old->mac.raw + offset;
507 memcpy(new->cb, old->cb, sizeof(old->cb));
508 new->local_df = old->local_df;
509 new->fclone = SKB_FCLONE_UNAVAILABLE;
510 new->pkt_type = old->pkt_type;
511 new->tstamp = old->tstamp;
512 new->destructor = NULL;
513 #ifdef CONFIG_NETFILTER
514 new->nfmark = old->nfmark;
515 new->nfct = old->nfct;
516 nf_conntrack_get(old->nfct);
517 new->nfctinfo = old->nfctinfo;
518 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
519 new->nfct_reasm = old->nfct_reasm;
520 nf_conntrack_get_reasm(old->nfct_reasm);
522 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
523 new->ipvs_property = old->ipvs_property;
525 #ifdef CONFIG_BRIDGE_NETFILTER
526 new->nf_bridge = old->nf_bridge;
527 nf_bridge_get(old->nf_bridge);
530 #ifdef CONFIG_NET_SCHED
531 #ifdef CONFIG_NET_CLS_ACT
532 new->tc_verd = old->tc_verd;
534 new->tc_index = old->tc_index;
536 skb_copy_secmark(new, old);
537 atomic_set(&new->users, 1);
538 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
539 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
540 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
544 * skb_copy - create private copy of an sk_buff
545 * @skb: buffer to copy
546 * @gfp_mask: allocation priority
548 * Make a copy of both an &sk_buff and its data. This is used when the
549 * caller wishes to modify the data and needs a private copy of the
550 * data to alter. Returns %NULL on failure or the pointer to the buffer
551 * on success. The returned buffer has a reference count of 1.
553 * As by-product this function converts non-linear &sk_buff to linear
554 * one, so that &sk_buff becomes completely private and caller is allowed
555 * to modify all the data of returned buffer. This means that this
556 * function is not recommended for use in circumstances when only
557 * header is going to be modified. Use pskb_copy() instead.
560 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
562 int headerlen = skb->data - skb->head;
564 * Allocate the copy buffer
566 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
571 /* Set the data pointer */
572 skb_reserve(n, headerlen);
573 /* Set the tail pointer and length */
574 skb_put(n, skb->len);
576 n->ip_summed = skb->ip_summed;
578 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
581 copy_skb_header(n, skb);
587 * pskb_copy - create copy of an sk_buff with private head.
588 * @skb: buffer to copy
589 * @gfp_mask: allocation priority
591 * Make a copy of both an &sk_buff and part of its data, located
592 * in header. Fragmented data remain shared. This is used when
593 * the caller wishes to modify only header of &sk_buff and needs
594 * private copy of the header to alter. Returns %NULL on failure
595 * or the pointer to the buffer on success.
596 * The returned buffer has a reference count of 1.
599 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
602 * Allocate the copy buffer
604 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
609 /* Set the data pointer */
610 skb_reserve(n, skb->data - skb->head);
611 /* Set the tail pointer and length */
612 skb_put(n, skb_headlen(skb));
614 memcpy(n->data, skb->data, n->len);
616 n->ip_summed = skb->ip_summed;
618 n->data_len = skb->data_len;
621 if (skb_shinfo(skb)->nr_frags) {
624 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
625 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
626 get_page(skb_shinfo(n)->frags[i].page);
628 skb_shinfo(n)->nr_frags = i;
631 if (skb_shinfo(skb)->frag_list) {
632 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
633 skb_clone_fraglist(n);
636 copy_skb_header(n, skb);
642 * pskb_expand_head - reallocate header of &sk_buff
643 * @skb: buffer to reallocate
644 * @nhead: room to add at head
645 * @ntail: room to add at tail
646 * @gfp_mask: allocation priority
648 * Expands (or creates identical copy, if &nhead and &ntail are zero)
649 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
650 * reference count of 1. Returns zero in the case of success or error,
651 * if expansion failed. In the last case, &sk_buff is not changed.
653 * All the pointers pointing into skb header may change and must be
654 * reloaded after call to this function.
657 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
662 int size = nhead + (skb->end - skb->head) + ntail;
668 size = SKB_DATA_ALIGN(size);
670 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
674 /* Copy only real data... and, alas, header. This should be
675 * optimized for the cases when header is void. */
676 memcpy(data + nhead, skb->head, skb->tail - skb->head);
677 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
679 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
680 get_page(skb_shinfo(skb)->frags[i].page);
682 if (skb_shinfo(skb)->frag_list)
683 skb_clone_fraglist(skb);
685 skb_release_data(skb);
687 off = (data + nhead) - skb->head;
690 skb->end = data + size;
698 atomic_set(&skb_shinfo(skb)->dataref, 1);
705 /* Make private copy of skb with writable head and some headroom */
707 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
709 struct sk_buff *skb2;
710 int delta = headroom - skb_headroom(skb);
713 skb2 = pskb_copy(skb, GFP_ATOMIC);
715 skb2 = skb_clone(skb, GFP_ATOMIC);
716 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
727 * skb_copy_expand - copy and expand sk_buff
728 * @skb: buffer to copy
729 * @newheadroom: new free bytes at head
730 * @newtailroom: new free bytes at tail
731 * @gfp_mask: allocation priority
733 * Make a copy of both an &sk_buff and its data and while doing so
734 * allocate additional space.
736 * This is used when the caller wishes to modify the data and needs a
737 * private copy of the data to alter as well as more space for new fields.
738 * Returns %NULL on failure or the pointer to the buffer
739 * on success. The returned buffer has a reference count of 1.
741 * You must pass %GFP_ATOMIC as the allocation priority if this function
742 * is called from an interrupt.
744 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
745 * only by netfilter in the cases when checksum is recalculated? --ANK
747 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
748 int newheadroom, int newtailroom,
752 * Allocate the copy buffer
754 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
756 int head_copy_len, head_copy_off;
761 skb_reserve(n, newheadroom);
763 /* Set the tail pointer and length */
764 skb_put(n, skb->len);
766 head_copy_len = skb_headroom(skb);
768 if (newheadroom <= head_copy_len)
769 head_copy_len = newheadroom;
771 head_copy_off = newheadroom - head_copy_len;
773 /* Copy the linear header and data. */
774 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
775 skb->len + head_copy_len))
778 copy_skb_header(n, skb);
784 * skb_pad - zero pad the tail of an skb
785 * @skb: buffer to pad
788 * Ensure that a buffer is followed by a padding area that is zero
789 * filled. Used by network drivers which may DMA or transfer data
790 * beyond the buffer end onto the wire.
792 * May return error in out of memory cases. The skb is freed on error.
795 int skb_pad(struct sk_buff *skb, int pad)
800 /* If the skbuff is non linear tailroom is always zero.. */
801 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
802 memset(skb->data+skb->len, 0, pad);
806 ntail = skb->data_len + pad - (skb->end - skb->tail);
807 if (likely(skb_cloned(skb) || ntail > 0)) {
808 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
813 /* FIXME: The use of this function with non-linear skb's really needs
816 err = skb_linearize(skb);
820 memset(skb->data + skb->len, 0, pad);
828 /* Trims skb to length len. It can change skb pointers.
831 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
833 int offset = skb_headlen(skb);
834 int nfrags = skb_shinfo(skb)->nr_frags;
837 for (i = 0; i < nfrags; i++) {
838 int end = offset + skb_shinfo(skb)->frags[i].size;
840 if (skb_cloned(skb)) {
841 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
845 put_page(skb_shinfo(skb)->frags[i].page);
846 skb_shinfo(skb)->nr_frags--;
848 skb_shinfo(skb)->frags[i].size = len - offset;
855 skb->data_len -= skb->len - len;
858 if (len <= skb_headlen(skb)) {
861 skb->tail = skb->data + len;
862 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
863 skb_drop_fraglist(skb);
865 skb->data_len -= skb->len - len;
874 * __pskb_pull_tail - advance tail of skb header
875 * @skb: buffer to reallocate
876 * @delta: number of bytes to advance tail
878 * The function makes a sense only on a fragmented &sk_buff,
879 * it expands header moving its tail forward and copying necessary
880 * data from fragmented part.
882 * &sk_buff MUST have reference count of 1.
884 * Returns %NULL (and &sk_buff does not change) if pull failed
885 * or value of new tail of skb in the case of success.
887 * All the pointers pointing into skb header may change and must be
888 * reloaded after call to this function.
891 /* Moves tail of skb head forward, copying data from fragmented part,
892 * when it is necessary.
893 * 1. It may fail due to malloc failure.
894 * 2. It may change skb pointers.
896 * It is pretty complicated. Luckily, it is called only in exceptional cases.
898 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
900 /* If skb has not enough free space at tail, get new one
901 * plus 128 bytes for future expansions. If we have enough
902 * room at tail, reallocate without expansion only if skb is cloned.
904 int i, k, eat = (skb->tail + delta) - skb->end;
906 if (eat > 0 || skb_cloned(skb)) {
907 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
912 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
915 /* Optimization: no fragments, no reasons to preestimate
916 * size of pulled pages. Superb.
918 if (!skb_shinfo(skb)->frag_list)
921 /* Estimate size of pulled pages. */
923 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
924 if (skb_shinfo(skb)->frags[i].size >= eat)
926 eat -= skb_shinfo(skb)->frags[i].size;
929 /* If we need update frag list, we are in troubles.
930 * Certainly, it possible to add an offset to skb data,
931 * but taking into account that pulling is expected to
932 * be very rare operation, it is worth to fight against
933 * further bloating skb head and crucify ourselves here instead.
934 * Pure masohism, indeed. 8)8)
937 struct sk_buff *list = skb_shinfo(skb)->frag_list;
938 struct sk_buff *clone = NULL;
939 struct sk_buff *insp = NULL;
944 if (list->len <= eat) {
945 /* Eaten as whole. */
950 /* Eaten partially. */
952 if (skb_shared(list)) {
953 /* Sucks! We need to fork list. :-( */
954 clone = skb_clone(list, GFP_ATOMIC);
960 /* This may be pulled without
964 if (!pskb_pull(list, eat)) {
973 /* Free pulled out fragments. */
974 while ((list = skb_shinfo(skb)->frag_list) != insp) {
975 skb_shinfo(skb)->frag_list = list->next;
978 /* And insert new clone at head. */
981 skb_shinfo(skb)->frag_list = clone;
984 /* Success! Now we may commit changes to skb data. */
989 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
990 if (skb_shinfo(skb)->frags[i].size <= eat) {
991 put_page(skb_shinfo(skb)->frags[i].page);
992 eat -= skb_shinfo(skb)->frags[i].size;
994 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
996 skb_shinfo(skb)->frags[k].page_offset += eat;
997 skb_shinfo(skb)->frags[k].size -= eat;
1003 skb_shinfo(skb)->nr_frags = k;
1006 skb->data_len -= delta;
1011 /* Copy some data bits from skb to kernel buffer. */
1013 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1016 int start = skb_headlen(skb);
1018 if (offset > (int)skb->len - len)
1022 if ((copy = start - offset) > 0) {
1025 memcpy(to, skb->data + offset, copy);
1026 if ((len -= copy) == 0)
1032 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1035 BUG_TRAP(start <= offset + len);
1037 end = start + skb_shinfo(skb)->frags[i].size;
1038 if ((copy = end - offset) > 0) {
1044 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1046 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1047 offset - start, copy);
1048 kunmap_skb_frag(vaddr);
1050 if ((len -= copy) == 0)
1058 if (skb_shinfo(skb)->frag_list) {
1059 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1061 for (; list; list = list->next) {
1064 BUG_TRAP(start <= offset + len);
1066 end = start + list->len;
1067 if ((copy = end - offset) > 0) {
1070 if (skb_copy_bits(list, offset - start,
1073 if ((len -= copy) == 0)
1089 * skb_store_bits - store bits from kernel buffer to skb
1090 * @skb: destination buffer
1091 * @offset: offset in destination
1092 * @from: source buffer
1093 * @len: number of bytes to copy
1095 * Copy the specified number of bytes from the source buffer to the
1096 * destination skb. This function handles all the messy bits of
1097 * traversing fragment lists and such.
1100 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1103 int start = skb_headlen(skb);
1105 if (offset > (int)skb->len - len)
1108 if ((copy = start - offset) > 0) {
1111 memcpy(skb->data + offset, from, copy);
1112 if ((len -= copy) == 0)
1118 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1119 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1122 BUG_TRAP(start <= offset + len);
1124 end = start + frag->size;
1125 if ((copy = end - offset) > 0) {
1131 vaddr = kmap_skb_frag(frag);
1132 memcpy(vaddr + frag->page_offset + offset - start,
1134 kunmap_skb_frag(vaddr);
1136 if ((len -= copy) == 0)
1144 if (skb_shinfo(skb)->frag_list) {
1145 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1147 for (; list; list = list->next) {
1150 BUG_TRAP(start <= offset + len);
1152 end = start + list->len;
1153 if ((copy = end - offset) > 0) {
1156 if (skb_store_bits(list, offset - start,
1159 if ((len -= copy) == 0)
1174 EXPORT_SYMBOL(skb_store_bits);
1176 /* Checksum skb data. */
1178 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1179 int len, unsigned int csum)
1181 int start = skb_headlen(skb);
1182 int i, copy = start - offset;
1185 /* Checksum header. */
1189 csum = csum_partial(skb->data + offset, copy, csum);
1190 if ((len -= copy) == 0)
1196 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1199 BUG_TRAP(start <= offset + len);
1201 end = start + skb_shinfo(skb)->frags[i].size;
1202 if ((copy = end - offset) > 0) {
1205 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1209 vaddr = kmap_skb_frag(frag);
1210 csum2 = csum_partial(vaddr + frag->page_offset +
1211 offset - start, copy, 0);
1212 kunmap_skb_frag(vaddr);
1213 csum = csum_block_add(csum, csum2, pos);
1222 if (skb_shinfo(skb)->frag_list) {
1223 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1225 for (; list; list = list->next) {
1228 BUG_TRAP(start <= offset + len);
1230 end = start + list->len;
1231 if ((copy = end - offset) > 0) {
1235 csum2 = skb_checksum(list, offset - start,
1237 csum = csum_block_add(csum, csum2, pos);
1238 if ((len -= copy) == 0)
1251 /* Both of above in one bottle. */
1253 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1254 u8 *to, int len, unsigned int csum)
1256 int start = skb_headlen(skb);
1257 int i, copy = start - offset;
1264 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1266 if ((len -= copy) == 0)
1273 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1276 BUG_TRAP(start <= offset + len);
1278 end = start + skb_shinfo(skb)->frags[i].size;
1279 if ((copy = end - offset) > 0) {
1282 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1286 vaddr = kmap_skb_frag(frag);
1287 csum2 = csum_partial_copy_nocheck(vaddr +
1291 kunmap_skb_frag(vaddr);
1292 csum = csum_block_add(csum, csum2, pos);
1302 if (skb_shinfo(skb)->frag_list) {
1303 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1305 for (; list; list = list->next) {
1309 BUG_TRAP(start <= offset + len);
1311 end = start + list->len;
1312 if ((copy = end - offset) > 0) {
1315 csum2 = skb_copy_and_csum_bits(list,
1318 csum = csum_block_add(csum, csum2, pos);
1319 if ((len -= copy) == 0)
1332 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1337 if (skb->ip_summed == CHECKSUM_HW)
1338 csstart = skb->h.raw - skb->data;
1340 csstart = skb_headlen(skb);
1342 BUG_ON(csstart > skb_headlen(skb));
1344 memcpy(to, skb->data, csstart);
1347 if (csstart != skb->len)
1348 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1349 skb->len - csstart, 0);
1351 if (skb->ip_summed == CHECKSUM_HW) {
1352 long csstuff = csstart + skb->csum;
1354 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1359 * skb_dequeue - remove from the head of the queue
1360 * @list: list to dequeue from
1362 * Remove the head of the list. The list lock is taken so the function
1363 * may be used safely with other locking list functions. The head item is
1364 * returned or %NULL if the list is empty.
1367 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1369 unsigned long flags;
1370 struct sk_buff *result;
1372 spin_lock_irqsave(&list->lock, flags);
1373 result = __skb_dequeue(list);
1374 spin_unlock_irqrestore(&list->lock, flags);
1379 * skb_dequeue_tail - remove from the tail of the queue
1380 * @list: list to dequeue from
1382 * Remove the tail of the list. The list lock is taken so the function
1383 * may be used safely with other locking list functions. The tail item is
1384 * returned or %NULL if the list is empty.
1386 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1388 unsigned long flags;
1389 struct sk_buff *result;
1391 spin_lock_irqsave(&list->lock, flags);
1392 result = __skb_dequeue_tail(list);
1393 spin_unlock_irqrestore(&list->lock, flags);
1398 * skb_queue_purge - empty a list
1399 * @list: list to empty
1401 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1402 * the list and one reference dropped. This function takes the list
1403 * lock and is atomic with respect to other list locking functions.
1405 void skb_queue_purge(struct sk_buff_head *list)
1407 struct sk_buff *skb;
1408 while ((skb = skb_dequeue(list)) != NULL)
1413 * skb_queue_head - queue a buffer at the list head
1414 * @list: list to use
1415 * @newsk: buffer to queue
1417 * Queue a buffer at the start of the list. This function takes the
1418 * list lock and can be used safely with other locking &sk_buff functions
1421 * A buffer cannot be placed on two lists at the same time.
1423 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1425 unsigned long flags;
1427 spin_lock_irqsave(&list->lock, flags);
1428 __skb_queue_head(list, newsk);
1429 spin_unlock_irqrestore(&list->lock, flags);
1433 * skb_queue_tail - queue a buffer at the list tail
1434 * @list: list to use
1435 * @newsk: buffer to queue
1437 * Queue a buffer at the tail of the list. This function takes the
1438 * list lock and can be used safely with other locking &sk_buff functions
1441 * A buffer cannot be placed on two lists at the same time.
1443 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1445 unsigned long flags;
1447 spin_lock_irqsave(&list->lock, flags);
1448 __skb_queue_tail(list, newsk);
1449 spin_unlock_irqrestore(&list->lock, flags);
1453 * skb_unlink - remove a buffer from a list
1454 * @skb: buffer to remove
1455 * @list: list to use
1457 * Remove a packet from a list. The list locks are taken and this
1458 * function is atomic with respect to other list locked calls
1460 * You must know what list the SKB is on.
1462 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1464 unsigned long flags;
1466 spin_lock_irqsave(&list->lock, flags);
1467 __skb_unlink(skb, list);
1468 spin_unlock_irqrestore(&list->lock, flags);
1472 * skb_append - append a buffer
1473 * @old: buffer to insert after
1474 * @newsk: buffer to insert
1475 * @list: list to use
1477 * Place a packet after a given packet in a list. The list locks are taken
1478 * and this function is atomic with respect to other list locked calls.
1479 * A buffer cannot be placed on two lists at the same time.
1481 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1483 unsigned long flags;
1485 spin_lock_irqsave(&list->lock, flags);
1486 __skb_append(old, newsk, list);
1487 spin_unlock_irqrestore(&list->lock, flags);
1492 * skb_insert - insert a buffer
1493 * @old: buffer to insert before
1494 * @newsk: buffer to insert
1495 * @list: list to use
1497 * Place a packet before a given packet in a list. The list locks are
1498 * taken and this function is atomic with respect to other list locked
1501 * A buffer cannot be placed on two lists at the same time.
1503 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1505 unsigned long flags;
1507 spin_lock_irqsave(&list->lock, flags);
1508 __skb_insert(newsk, old->prev, old, list);
1509 spin_unlock_irqrestore(&list->lock, flags);
1514 * Tune the memory allocator for a new MTU size.
1516 void skb_add_mtu(int mtu)
1518 /* Must match allocation in alloc_skb */
1519 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1521 kmem_add_cache_size(mtu);
1525 static inline void skb_split_inside_header(struct sk_buff *skb,
1526 struct sk_buff* skb1,
1527 const u32 len, const int pos)
1531 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1533 /* And move data appendix as is. */
1534 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1535 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1537 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1538 skb_shinfo(skb)->nr_frags = 0;
1539 skb1->data_len = skb->data_len;
1540 skb1->len += skb1->data_len;
1543 skb->tail = skb->data + len;
1546 static inline void skb_split_no_header(struct sk_buff *skb,
1547 struct sk_buff* skb1,
1548 const u32 len, int pos)
1551 const int nfrags = skb_shinfo(skb)->nr_frags;
1553 skb_shinfo(skb)->nr_frags = 0;
1554 skb1->len = skb1->data_len = skb->len - len;
1556 skb->data_len = len - pos;
1558 for (i = 0; i < nfrags; i++) {
1559 int size = skb_shinfo(skb)->frags[i].size;
1561 if (pos + size > len) {
1562 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1566 * We have two variants in this case:
1567 * 1. Move all the frag to the second
1568 * part, if it is possible. F.e.
1569 * this approach is mandatory for TUX,
1570 * where splitting is expensive.
1571 * 2. Split is accurately. We make this.
1573 get_page(skb_shinfo(skb)->frags[i].page);
1574 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1575 skb_shinfo(skb1)->frags[0].size -= len - pos;
1576 skb_shinfo(skb)->frags[i].size = len - pos;
1577 skb_shinfo(skb)->nr_frags++;
1581 skb_shinfo(skb)->nr_frags++;
1584 skb_shinfo(skb1)->nr_frags = k;
1588 * skb_split - Split fragmented skb to two parts at length len.
1589 * @skb: the buffer to split
1590 * @skb1: the buffer to receive the second part
1591 * @len: new length for skb
1593 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1595 int pos = skb_headlen(skb);
1597 if (len < pos) /* Split line is inside header. */
1598 skb_split_inside_header(skb, skb1, len, pos);
1599 else /* Second chunk has no header, nothing to copy. */
1600 skb_split_no_header(skb, skb1, len, pos);
1604 * skb_prepare_seq_read - Prepare a sequential read of skb data
1605 * @skb: the buffer to read
1606 * @from: lower offset of data to be read
1607 * @to: upper offset of data to be read
1608 * @st: state variable
1610 * Initializes the specified state variable. Must be called before
1611 * invoking skb_seq_read() for the first time.
1613 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1614 unsigned int to, struct skb_seq_state *st)
1616 st->lower_offset = from;
1617 st->upper_offset = to;
1618 st->root_skb = st->cur_skb = skb;
1619 st->frag_idx = st->stepped_offset = 0;
1620 st->frag_data = NULL;
1624 * skb_seq_read - Sequentially read skb data
1625 * @consumed: number of bytes consumed by the caller so far
1626 * @data: destination pointer for data to be returned
1627 * @st: state variable
1629 * Reads a block of skb data at &consumed relative to the
1630 * lower offset specified to skb_prepare_seq_read(). Assigns
1631 * the head of the data block to &data and returns the length
1632 * of the block or 0 if the end of the skb data or the upper
1633 * offset has been reached.
1635 * The caller is not required to consume all of the data
1636 * returned, i.e. &consumed is typically set to the number
1637 * of bytes already consumed and the next call to
1638 * skb_seq_read() will return the remaining part of the block.
1640 * Note: The size of each block of data returned can be arbitary,
1641 * this limitation is the cost for zerocopy seqeuental
1642 * reads of potentially non linear data.
1644 * Note: Fragment lists within fragments are not implemented
1645 * at the moment, state->root_skb could be replaced with
1646 * a stack for this purpose.
1648 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1649 struct skb_seq_state *st)
1651 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1654 if (unlikely(abs_offset >= st->upper_offset))
1658 block_limit = skb_headlen(st->cur_skb);
1660 if (abs_offset < block_limit) {
1661 *data = st->cur_skb->data + abs_offset;
1662 return block_limit - abs_offset;
1665 if (st->frag_idx == 0 && !st->frag_data)
1666 st->stepped_offset += skb_headlen(st->cur_skb);
1668 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1669 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1670 block_limit = frag->size + st->stepped_offset;
1672 if (abs_offset < block_limit) {
1674 st->frag_data = kmap_skb_frag(frag);
1676 *data = (u8 *) st->frag_data + frag->page_offset +
1677 (abs_offset - st->stepped_offset);
1679 return block_limit - abs_offset;
1682 if (st->frag_data) {
1683 kunmap_skb_frag(st->frag_data);
1684 st->frag_data = NULL;
1688 st->stepped_offset += frag->size;
1691 if (st->cur_skb->next) {
1692 st->cur_skb = st->cur_skb->next;
1695 } else if (st->root_skb == st->cur_skb &&
1696 skb_shinfo(st->root_skb)->frag_list) {
1697 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1705 * skb_abort_seq_read - Abort a sequential read of skb data
1706 * @st: state variable
1708 * Must be called if skb_seq_read() was not called until it
1711 void skb_abort_seq_read(struct skb_seq_state *st)
1714 kunmap_skb_frag(st->frag_data);
1717 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1719 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1720 struct ts_config *conf,
1721 struct ts_state *state)
1723 return skb_seq_read(offset, text, TS_SKB_CB(state));
1726 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1728 skb_abort_seq_read(TS_SKB_CB(state));
1732 * skb_find_text - Find a text pattern in skb data
1733 * @skb: the buffer to look in
1734 * @from: search offset
1736 * @config: textsearch configuration
1737 * @state: uninitialized textsearch state variable
1739 * Finds a pattern in the skb data according to the specified
1740 * textsearch configuration. Use textsearch_next() to retrieve
1741 * subsequent occurrences of the pattern. Returns the offset
1742 * to the first occurrence or UINT_MAX if no match was found.
1744 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1745 unsigned int to, struct ts_config *config,
1746 struct ts_state *state)
1750 config->get_next_block = skb_ts_get_next_block;
1751 config->finish = skb_ts_finish;
1753 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1755 ret = textsearch_find(config, state);
1756 return (ret <= to - from ? ret : UINT_MAX);
1760 * skb_append_datato_frags: - append the user data to a skb
1761 * @sk: sock structure
1762 * @skb: skb structure to be appened with user data.
1763 * @getfrag: call back function to be used for getting the user data
1764 * @from: pointer to user message iov
1765 * @length: length of the iov message
1767 * Description: This procedure append the user data in the fragment part
1768 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1770 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1771 int (*getfrag)(void *from, char *to, int offset,
1772 int len, int odd, struct sk_buff *skb),
1773 void *from, int length)
1776 skb_frag_t *frag = NULL;
1777 struct page *page = NULL;
1783 /* Return error if we don't have space for new frag */
1784 frg_cnt = skb_shinfo(skb)->nr_frags;
1785 if (frg_cnt >= MAX_SKB_FRAGS)
1788 /* allocate a new page for next frag */
1789 page = alloc_pages(sk->sk_allocation, 0);
1791 /* If alloc_page fails just return failure and caller will
1792 * free previous allocated pages by doing kfree_skb()
1797 /* initialize the next frag */
1798 sk->sk_sndmsg_page = page;
1799 sk->sk_sndmsg_off = 0;
1800 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1801 skb->truesize += PAGE_SIZE;
1802 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1804 /* get the new initialized frag */
1805 frg_cnt = skb_shinfo(skb)->nr_frags;
1806 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1808 /* copy the user data to page */
1809 left = PAGE_SIZE - frag->page_offset;
1810 copy = (length > left)? left : length;
1812 ret = getfrag(from, (page_address(frag->page) +
1813 frag->page_offset + frag->size),
1814 offset, copy, 0, skb);
1818 /* copy was successful so update the size parameters */
1819 sk->sk_sndmsg_off += copy;
1822 skb->data_len += copy;
1826 } while (length > 0);
1832 * skb_pull_rcsum - pull skb and update receive checksum
1833 * @skb: buffer to update
1834 * @start: start of data before pull
1835 * @len: length of data pulled
1837 * This function performs an skb_pull on the packet and updates
1838 * update the CHECKSUM_HW checksum. It should be used on receive
1839 * path processing instead of skb_pull unless you know that the
1840 * checksum difference is zero (e.g., a valid IP header) or you
1841 * are setting ip_summed to CHECKSUM_NONE.
1843 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1845 BUG_ON(len > skb->len);
1847 BUG_ON(skb->len < skb->data_len);
1848 skb_postpull_rcsum(skb, skb->data, len);
1849 return skb->data += len;
1852 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1855 * skb_segment - Perform protocol segmentation on skb.
1856 * @skb: buffer to segment
1857 * @features: features for the output path (see dev->features)
1859 * This function performs segmentation on the given skb. It returns
1860 * the segment at the given position. It returns NULL if there are
1861 * no more segments to generate, or when an error is encountered.
1863 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1865 struct sk_buff *segs = NULL;
1866 struct sk_buff *tail = NULL;
1867 unsigned int mss = skb_shinfo(skb)->gso_size;
1868 unsigned int doffset = skb->data - skb->mac.raw;
1869 unsigned int offset = doffset;
1870 unsigned int headroom;
1872 int sg = features & NETIF_F_SG;
1873 int nfrags = skb_shinfo(skb)->nr_frags;
1878 __skb_push(skb, doffset);
1879 headroom = skb_headroom(skb);
1880 pos = skb_headlen(skb);
1883 struct sk_buff *nskb;
1889 len = skb->len - offset;
1893 hsize = skb_headlen(skb) - offset;
1896 nsize = hsize + doffset;
1897 if (nsize > len + doffset || !sg)
1898 nsize = len + doffset;
1900 nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
1901 if (unlikely(!nskb))
1910 nskb->dev = skb->dev;
1911 nskb->priority = skb->priority;
1912 nskb->protocol = skb->protocol;
1913 nskb->dst = dst_clone(skb->dst);
1914 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1915 nskb->pkt_type = skb->pkt_type;
1916 nskb->mac_len = skb->mac_len;
1918 skb_reserve(nskb, headroom);
1919 nskb->mac.raw = nskb->data;
1920 nskb->nh.raw = nskb->data + skb->mac_len;
1921 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1922 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1925 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1931 frag = skb_shinfo(nskb)->frags;
1934 nskb->ip_summed = CHECKSUM_HW;
1935 nskb->csum = skb->csum;
1936 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1938 while (pos < offset + len) {
1939 BUG_ON(i >= nfrags);
1941 *frag = skb_shinfo(skb)->frags[i];
1942 get_page(frag->page);
1946 frag->page_offset += offset - pos;
1947 frag->size -= offset - pos;
1952 if (pos + size <= offset + len) {
1956 frag->size -= pos + size - (offset + len);
1963 skb_shinfo(nskb)->nr_frags = k;
1964 nskb->data_len = len - hsize;
1965 nskb->len += nskb->data_len;
1966 nskb->truesize += nskb->data_len;
1967 } while ((offset += len) < skb->len);
1972 while ((skb = segs)) {
1976 return ERR_PTR(err);
1979 EXPORT_SYMBOL_GPL(skb_segment);
1981 void __init skb_init(void)
1983 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1984 sizeof(struct sk_buff),
1988 if (!skbuff_head_cache)
1989 panic("cannot create skbuff cache");
1991 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1992 (2*sizeof(struct sk_buff)) +
1997 if (!skbuff_fclone_cache)
1998 panic("cannot create skbuff cache");
2001 EXPORT_SYMBOL(___pskb_trim);
2002 EXPORT_SYMBOL(__kfree_skb);
2003 EXPORT_SYMBOL(kfree_skb);
2004 EXPORT_SYMBOL(__pskb_pull_tail);
2005 EXPORT_SYMBOL(__alloc_skb);
2006 EXPORT_SYMBOL(pskb_copy);
2007 EXPORT_SYMBOL(pskb_expand_head);
2008 EXPORT_SYMBOL(skb_checksum);
2009 EXPORT_SYMBOL(skb_clone);
2010 EXPORT_SYMBOL(skb_clone_fraglist);
2011 EXPORT_SYMBOL(skb_copy);
2012 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2013 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2014 EXPORT_SYMBOL(skb_copy_bits);
2015 EXPORT_SYMBOL(skb_copy_expand);
2016 EXPORT_SYMBOL(skb_over_panic);
2017 EXPORT_SYMBOL(skb_pad);
2018 EXPORT_SYMBOL(skb_realloc_headroom);
2019 EXPORT_SYMBOL(skb_under_panic);
2020 EXPORT_SYMBOL(skb_dequeue);
2021 EXPORT_SYMBOL(skb_dequeue_tail);
2022 EXPORT_SYMBOL(skb_insert);
2023 EXPORT_SYMBOL(skb_queue_purge);
2024 EXPORT_SYMBOL(skb_queue_head);
2025 EXPORT_SYMBOL(skb_queue_tail);
2026 EXPORT_SYMBOL(skb_unlink);
2027 EXPORT_SYMBOL(skb_append);
2028 EXPORT_SYMBOL(skb_split);
2029 EXPORT_SYMBOL(skb_prepare_seq_read);
2030 EXPORT_SYMBOL(skb_seq_read);
2031 EXPORT_SYMBOL(skb_abort_seq_read);
2032 EXPORT_SYMBOL(skb_find_text);
2033 EXPORT_SYMBOL(skb_append_datato_frags);