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>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/netdevice.h>
50 #ifdef CONFIG_NET_CLS_ACT
51 #include <net/pkt_sched.h>
53 #include <linux/string.h>
54 #include <linux/skbuff.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
59 #include <net/protocol.h>
62 #include <net/checksum.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
70 static struct kmem_cache *skbuff_head_cache __read_mostly;
71 static struct kmem_cache *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:%#lx end:%#lx dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data,
92 (unsigned long)skb->tail, (unsigned long)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:%#lx end:%#lx dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data,
111 (unsigned long)skb->tail, (unsigned long)skb->end,
112 skb->dev ? skb->dev->name : "<NULL>");
116 void skb_truesize_bug(struct sk_buff *skb)
118 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
119 "len=%u, sizeof(sk_buff)=%Zd\n",
120 skb->truesize, skb->len, sizeof(struct sk_buff));
122 EXPORT_SYMBOL(skb_truesize_bug);
124 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
125 * 'private' fields and also do memory statistics to find all the
131 * __alloc_skb - allocate a network buffer
132 * @size: size to allocate
133 * @gfp_mask: allocation mask
134 * @fclone: allocate from fclone cache instead of head cache
135 * and allocate a cloned (child) skb
136 * @node: numa node to allocate memory on
138 * Allocate a new &sk_buff. The returned buffer has no headroom and a
139 * tail room of size bytes. The object has a reference count of one.
140 * The return is the buffer. On a failure the return is %NULL.
142 * Buffers may only be allocated from interrupts using a @gfp_mask of
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),
168 * See comment in sk_buff definition, just before the 'tail' member
170 memset(skb, 0, offsetof(struct sk_buff, tail));
171 skb->truesize = size + sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
175 skb_reset_tail_pointer(skb);
176 skb->end = skb->tail + 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 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
206 * @dev: network device to receive on
207 * @length: length to allocate
208 * @gfp_mask: get_free_pages mask, passed to alloc_skb
210 * Allocate a new &sk_buff and assign it a usage count of one. The
211 * buffer has unspecified headroom built in. Users should allocate
212 * the headroom they think they need without accounting for the
213 * built in space. The built in space is used for optimisations.
215 * %NULL is returned if there is no free memory.
217 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
218 unsigned int length, gfp_t gfp_mask)
220 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
223 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
225 skb_reserve(skb, NET_SKB_PAD);
231 static void skb_drop_list(struct sk_buff **listp)
233 struct sk_buff *list = *listp;
238 struct sk_buff *this = list;
244 static inline void skb_drop_fraglist(struct sk_buff *skb)
246 skb_drop_list(&skb_shinfo(skb)->frag_list);
249 static void skb_clone_fraglist(struct sk_buff *skb)
251 struct sk_buff *list;
253 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
257 static void skb_release_data(struct sk_buff *skb)
260 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
261 &skb_shinfo(skb)->dataref)) {
262 if (skb_shinfo(skb)->nr_frags) {
264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
265 put_page(skb_shinfo(skb)->frags[i].page);
268 if (skb_shinfo(skb)->frag_list)
269 skb_drop_fraglist(skb);
276 * Free an skbuff by memory without cleaning the state.
278 void kfree_skbmem(struct sk_buff *skb)
280 struct sk_buff *other;
281 atomic_t *fclone_ref;
283 skb_release_data(skb);
284 switch (skb->fclone) {
285 case SKB_FCLONE_UNAVAILABLE:
286 kmem_cache_free(skbuff_head_cache, skb);
289 case SKB_FCLONE_ORIG:
290 fclone_ref = (atomic_t *) (skb + 2);
291 if (atomic_dec_and_test(fclone_ref))
292 kmem_cache_free(skbuff_fclone_cache, skb);
295 case SKB_FCLONE_CLONE:
296 fclone_ref = (atomic_t *) (skb + 1);
299 /* The clone portion is available for
300 * fast-cloning again.
302 skb->fclone = SKB_FCLONE_UNAVAILABLE;
304 if (atomic_dec_and_test(fclone_ref))
305 kmem_cache_free(skbuff_fclone_cache, other);
311 * __kfree_skb - private function
314 * Free an sk_buff. Release anything attached to the buffer.
315 * Clean the state. This is an internal helper function. Users should
316 * always call kfree_skb
319 void __kfree_skb(struct sk_buff *skb)
321 dst_release(skb->dst);
323 secpath_put(skb->sp);
325 if (skb->destructor) {
327 skb->destructor(skb);
329 #ifdef CONFIG_NETFILTER
330 nf_conntrack_put(skb->nfct);
331 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
332 nf_conntrack_put_reasm(skb->nfct_reasm);
334 #ifdef CONFIG_BRIDGE_NETFILTER
335 nf_bridge_put(skb->nf_bridge);
338 /* XXX: IS this still necessary? - JHS */
339 #ifdef CONFIG_NET_SCHED
341 #ifdef CONFIG_NET_CLS_ACT
350 * kfree_skb - free an sk_buff
351 * @skb: buffer to free
353 * Drop a reference to the buffer and free it if the usage count has
356 void kfree_skb(struct sk_buff *skb)
360 if (likely(atomic_read(&skb->users) == 1))
362 else if (likely(!atomic_dec_and_test(&skb->users)))
368 * skb_clone - duplicate an sk_buff
369 * @skb: buffer to clone
370 * @gfp_mask: allocation priority
372 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
373 * copies share the same packet data but not structure. The new
374 * buffer has a reference count of 1. If the allocation fails the
375 * function returns %NULL otherwise the new buffer is returned.
377 * If this function is called from an interrupt gfp_mask() must be
381 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
386 if (skb->fclone == SKB_FCLONE_ORIG &&
387 n->fclone == SKB_FCLONE_UNAVAILABLE) {
388 atomic_t *fclone_ref = (atomic_t *) (n + 1);
389 n->fclone = SKB_FCLONE_CLONE;
390 atomic_inc(fclone_ref);
392 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
395 n->fclone = SKB_FCLONE_UNAVAILABLE;
398 #define C(x) n->x = skb->x
400 n->next = n->prev = NULL;
411 secpath_get(skb->sp);
413 memcpy(n->cb, skb->cb, sizeof(skb->cb));
424 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
428 n->destructor = NULL;
431 #ifdef CONFIG_NET_SCHED
433 #ifdef CONFIG_NET_CLS_ACT
434 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
435 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
436 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
439 skb_copy_secmark(n, skb);
442 atomic_set(&n->users, 1);
448 atomic_inc(&(skb_shinfo(skb)->dataref));
454 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
456 #ifndef NET_SKBUFF_DATA_USES_OFFSET
458 * Shift between the two data areas in bytes
460 unsigned long offset = new->data - old->data;
464 new->priority = old->priority;
465 new->protocol = old->protocol;
466 new->dst = dst_clone(old->dst);
468 new->sp = secpath_get(old->sp);
470 new->transport_header = old->transport_header;
471 new->network_header = old->network_header;
472 new->mac_header = old->mac_header;
473 #ifndef NET_SKBUFF_DATA_USES_OFFSET
474 /* {transport,network,mac}_header are relative to skb->head */
475 new->transport_header += offset;
476 new->network_header += offset;
477 new->mac_header += offset;
479 memcpy(new->cb, old->cb, sizeof(old->cb));
480 new->local_df = old->local_df;
481 new->fclone = SKB_FCLONE_UNAVAILABLE;
482 new->pkt_type = old->pkt_type;
483 new->tstamp = old->tstamp;
484 new->destructor = NULL;
485 new->mark = old->mark;
487 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
488 new->ipvs_property = old->ipvs_property;
490 #ifdef CONFIG_NET_SCHED
491 #ifdef CONFIG_NET_CLS_ACT
492 new->tc_verd = old->tc_verd;
494 new->tc_index = old->tc_index;
496 skb_copy_secmark(new, old);
497 atomic_set(&new->users, 1);
498 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
499 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
500 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
504 * skb_copy - create private copy of an sk_buff
505 * @skb: buffer to copy
506 * @gfp_mask: allocation priority
508 * Make a copy of both an &sk_buff and its data. This is used when the
509 * caller wishes to modify the data and needs a private copy of the
510 * data to alter. Returns %NULL on failure or the pointer to the buffer
511 * on success. The returned buffer has a reference count of 1.
513 * As by-product this function converts non-linear &sk_buff to linear
514 * one, so that &sk_buff becomes completely private and caller is allowed
515 * to modify all the data of returned buffer. This means that this
516 * function is not recommended for use in circumstances when only
517 * header is going to be modified. Use pskb_copy() instead.
520 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
522 int headerlen = skb->data - skb->head;
524 * Allocate the copy buffer
527 #ifdef NET_SKBUFF_DATA_USES_OFFSET
528 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
530 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
535 /* Set the data pointer */
536 skb_reserve(n, headerlen);
537 /* Set the tail pointer and length */
538 skb_put(n, skb->len);
540 n->ip_summed = skb->ip_summed;
542 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
545 copy_skb_header(n, skb);
551 * pskb_copy - create copy of an sk_buff with private head.
552 * @skb: buffer to copy
553 * @gfp_mask: allocation priority
555 * Make a copy of both an &sk_buff and part of its data, located
556 * in header. Fragmented data remain shared. This is used when
557 * the caller wishes to modify only header of &sk_buff and needs
558 * private copy of the header to alter. Returns %NULL on failure
559 * or the pointer to the buffer on success.
560 * The returned buffer has a reference count of 1.
563 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
566 * Allocate the copy buffer
569 #ifdef NET_SKBUFF_DATA_USES_OFFSET
570 n = alloc_skb(skb->end, gfp_mask);
572 n = alloc_skb(skb->end - skb->head, gfp_mask);
577 /* Set the data pointer */
578 skb_reserve(n, skb->data - skb->head);
579 /* Set the tail pointer and length */
580 skb_put(n, skb_headlen(skb));
582 memcpy(n->data, skb->data, n->len);
584 n->ip_summed = skb->ip_summed;
586 n->truesize += skb->data_len;
587 n->data_len = skb->data_len;
590 if (skb_shinfo(skb)->nr_frags) {
593 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
594 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
595 get_page(skb_shinfo(n)->frags[i].page);
597 skb_shinfo(n)->nr_frags = i;
600 if (skb_shinfo(skb)->frag_list) {
601 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
602 skb_clone_fraglist(n);
605 copy_skb_header(n, skb);
611 * pskb_expand_head - reallocate header of &sk_buff
612 * @skb: buffer to reallocate
613 * @nhead: room to add at head
614 * @ntail: room to add at tail
615 * @gfp_mask: allocation priority
617 * Expands (or creates identical copy, if &nhead and &ntail are zero)
618 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
619 * reference count of 1. Returns zero in the case of success or error,
620 * if expansion failed. In the last case, &sk_buff is not changed.
622 * All the pointers pointing into skb header may change and must be
623 * reloaded after call to this function.
626 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
631 #ifdef NET_SKBUFF_DATA_USES_OFFSET
632 int size = nhead + skb->end + ntail;
634 int size = nhead + (skb->end - skb->head) + ntail;
641 size = SKB_DATA_ALIGN(size);
643 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
647 /* Copy only real data... and, alas, header. This should be
648 * optimized for the cases when header is void. */
649 memcpy(data + nhead, skb->head,
650 #ifdef NET_SKBUFF_DATA_USES_OFFSET
653 skb->tail - skb->head);
655 memcpy(data + size, skb_end_pointer(skb),
656 sizeof(struct skb_shared_info));
658 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
659 get_page(skb_shinfo(skb)->frags[i].page);
661 if (skb_shinfo(skb)->frag_list)
662 skb_clone_fraglist(skb);
664 skb_release_data(skb);
666 off = (data + nhead) - skb->head;
670 #ifdef NET_SKBUFF_DATA_USES_OFFSET
673 skb->end = skb->head + size;
674 /* {transport,network,mac}_header and tail are relative to skb->head */
676 skb->transport_header += off;
677 skb->network_header += off;
678 skb->mac_header += off;
682 atomic_set(&skb_shinfo(skb)->dataref, 1);
689 /* Make private copy of skb with writable head and some headroom */
691 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
693 struct sk_buff *skb2;
694 int delta = headroom - skb_headroom(skb);
697 skb2 = pskb_copy(skb, GFP_ATOMIC);
699 skb2 = skb_clone(skb, GFP_ATOMIC);
700 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
711 * skb_copy_expand - copy and expand sk_buff
712 * @skb: buffer to copy
713 * @newheadroom: new free bytes at head
714 * @newtailroom: new free bytes at tail
715 * @gfp_mask: allocation priority
717 * Make a copy of both an &sk_buff and its data and while doing so
718 * allocate additional space.
720 * This is used when the caller wishes to modify the data and needs a
721 * private copy of the data to alter as well as more space for new fields.
722 * Returns %NULL on failure or the pointer to the buffer
723 * on success. The returned buffer has a reference count of 1.
725 * You must pass %GFP_ATOMIC as the allocation priority if this function
726 * is called from an interrupt.
728 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
729 * only by netfilter in the cases when checksum is recalculated? --ANK
731 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
732 int newheadroom, int newtailroom,
736 * Allocate the copy buffer
738 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
740 int head_copy_len, head_copy_off;
745 skb_reserve(n, newheadroom);
747 /* Set the tail pointer and length */
748 skb_put(n, skb->len);
750 head_copy_len = skb_headroom(skb);
752 if (newheadroom <= head_copy_len)
753 head_copy_len = newheadroom;
755 head_copy_off = newheadroom - head_copy_len;
757 /* Copy the linear header and data. */
758 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
759 skb->len + head_copy_len))
762 copy_skb_header(n, skb);
768 * skb_pad - zero pad the tail of an skb
769 * @skb: buffer to pad
772 * Ensure that a buffer is followed by a padding area that is zero
773 * filled. Used by network drivers which may DMA or transfer data
774 * beyond the buffer end onto the wire.
776 * May return error in out of memory cases. The skb is freed on error.
779 int skb_pad(struct sk_buff *skb, int pad)
784 /* If the skbuff is non linear tailroom is always zero.. */
785 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
786 memset(skb->data+skb->len, 0, pad);
790 ntail = skb->data_len + pad - (skb->end - skb->tail);
791 if (likely(skb_cloned(skb) || ntail > 0)) {
792 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
797 /* FIXME: The use of this function with non-linear skb's really needs
800 err = skb_linearize(skb);
804 memset(skb->data + skb->len, 0, pad);
812 /* Trims skb to length len. It can change skb pointers.
815 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
817 struct sk_buff **fragp;
818 struct sk_buff *frag;
819 int offset = skb_headlen(skb);
820 int nfrags = skb_shinfo(skb)->nr_frags;
824 if (skb_cloned(skb) &&
825 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
832 for (; i < nfrags; i++) {
833 int end = offset + skb_shinfo(skb)->frags[i].size;
840 skb_shinfo(skb)->frags[i++].size = len - offset;
843 skb_shinfo(skb)->nr_frags = i;
845 for (; i < nfrags; i++)
846 put_page(skb_shinfo(skb)->frags[i].page);
848 if (skb_shinfo(skb)->frag_list)
849 skb_drop_fraglist(skb);
853 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
854 fragp = &frag->next) {
855 int end = offset + frag->len;
857 if (skb_shared(frag)) {
858 struct sk_buff *nfrag;
860 nfrag = skb_clone(frag, GFP_ATOMIC);
861 if (unlikely(!nfrag))
864 nfrag->next = frag->next;
876 unlikely((err = pskb_trim(frag, len - offset))))
880 skb_drop_list(&frag->next);
885 if (len > skb_headlen(skb)) {
886 skb->data_len -= skb->len - len;
891 skb_set_tail_pointer(skb, len);
898 * __pskb_pull_tail - advance tail of skb header
899 * @skb: buffer to reallocate
900 * @delta: number of bytes to advance tail
902 * The function makes a sense only on a fragmented &sk_buff,
903 * it expands header moving its tail forward and copying necessary
904 * data from fragmented part.
906 * &sk_buff MUST have reference count of 1.
908 * Returns %NULL (and &sk_buff does not change) if pull failed
909 * or value of new tail of skb in the case of success.
911 * All the pointers pointing into skb header may change and must be
912 * reloaded after call to this function.
915 /* Moves tail of skb head forward, copying data from fragmented part,
916 * when it is necessary.
917 * 1. It may fail due to malloc failure.
918 * 2. It may change skb pointers.
920 * It is pretty complicated. Luckily, it is called only in exceptional cases.
922 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
924 /* If skb has not enough free space at tail, get new one
925 * plus 128 bytes for future expansions. If we have enough
926 * room at tail, reallocate without expansion only if skb is cloned.
928 int i, k, eat = (skb->tail + delta) - skb->end;
930 if (eat > 0 || skb_cloned(skb)) {
931 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
936 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
939 /* Optimization: no fragments, no reasons to preestimate
940 * size of pulled pages. Superb.
942 if (!skb_shinfo(skb)->frag_list)
945 /* Estimate size of pulled pages. */
947 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
948 if (skb_shinfo(skb)->frags[i].size >= eat)
950 eat -= skb_shinfo(skb)->frags[i].size;
953 /* If we need update frag list, we are in troubles.
954 * Certainly, it possible to add an offset to skb data,
955 * but taking into account that pulling is expected to
956 * be very rare operation, it is worth to fight against
957 * further bloating skb head and crucify ourselves here instead.
958 * Pure masohism, indeed. 8)8)
961 struct sk_buff *list = skb_shinfo(skb)->frag_list;
962 struct sk_buff *clone = NULL;
963 struct sk_buff *insp = NULL;
968 if (list->len <= eat) {
969 /* Eaten as whole. */
974 /* Eaten partially. */
976 if (skb_shared(list)) {
977 /* Sucks! We need to fork list. :-( */
978 clone = skb_clone(list, GFP_ATOMIC);
984 /* This may be pulled without
988 if (!pskb_pull(list, eat)) {
997 /* Free pulled out fragments. */
998 while ((list = skb_shinfo(skb)->frag_list) != insp) {
999 skb_shinfo(skb)->frag_list = list->next;
1002 /* And insert new clone at head. */
1005 skb_shinfo(skb)->frag_list = clone;
1008 /* Success! Now we may commit changes to skb data. */
1013 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1014 if (skb_shinfo(skb)->frags[i].size <= eat) {
1015 put_page(skb_shinfo(skb)->frags[i].page);
1016 eat -= skb_shinfo(skb)->frags[i].size;
1018 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1020 skb_shinfo(skb)->frags[k].page_offset += eat;
1021 skb_shinfo(skb)->frags[k].size -= eat;
1027 skb_shinfo(skb)->nr_frags = k;
1030 skb->data_len -= delta;
1032 return skb_tail_pointer(skb);
1035 /* Copy some data bits from skb to kernel buffer. */
1037 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1040 int start = skb_headlen(skb);
1042 if (offset > (int)skb->len - len)
1046 if ((copy = start - offset) > 0) {
1049 memcpy(to, skb->data + offset, copy);
1050 if ((len -= copy) == 0)
1056 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1059 BUG_TRAP(start <= offset + len);
1061 end = start + skb_shinfo(skb)->frags[i].size;
1062 if ((copy = end - offset) > 0) {
1068 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1070 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1071 offset - start, copy);
1072 kunmap_skb_frag(vaddr);
1074 if ((len -= copy) == 0)
1082 if (skb_shinfo(skb)->frag_list) {
1083 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1085 for (; list; list = list->next) {
1088 BUG_TRAP(start <= offset + len);
1090 end = start + list->len;
1091 if ((copy = end - offset) > 0) {
1094 if (skb_copy_bits(list, offset - start,
1097 if ((len -= copy) == 0)
1113 * skb_store_bits - store bits from kernel buffer to skb
1114 * @skb: destination buffer
1115 * @offset: offset in destination
1116 * @from: source buffer
1117 * @len: number of bytes to copy
1119 * Copy the specified number of bytes from the source buffer to the
1120 * destination skb. This function handles all the messy bits of
1121 * traversing fragment lists and such.
1124 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1127 int start = skb_headlen(skb);
1129 if (offset > (int)skb->len - len)
1132 if ((copy = start - offset) > 0) {
1135 memcpy(skb->data + offset, from, copy);
1136 if ((len -= copy) == 0)
1142 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1143 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1146 BUG_TRAP(start <= offset + len);
1148 end = start + frag->size;
1149 if ((copy = end - offset) > 0) {
1155 vaddr = kmap_skb_frag(frag);
1156 memcpy(vaddr + frag->page_offset + offset - start,
1158 kunmap_skb_frag(vaddr);
1160 if ((len -= copy) == 0)
1168 if (skb_shinfo(skb)->frag_list) {
1169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1171 for (; list; list = list->next) {
1174 BUG_TRAP(start <= offset + len);
1176 end = start + list->len;
1177 if ((copy = end - offset) > 0) {
1180 if (skb_store_bits(list, offset - start,
1183 if ((len -= copy) == 0)
1198 EXPORT_SYMBOL(skb_store_bits);
1200 /* Checksum skb data. */
1202 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1203 int len, __wsum csum)
1205 int start = skb_headlen(skb);
1206 int i, copy = start - offset;
1209 /* Checksum header. */
1213 csum = csum_partial(skb->data + offset, copy, csum);
1214 if ((len -= copy) == 0)
1220 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1223 BUG_TRAP(start <= offset + len);
1225 end = start + skb_shinfo(skb)->frags[i].size;
1226 if ((copy = end - offset) > 0) {
1229 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1233 vaddr = kmap_skb_frag(frag);
1234 csum2 = csum_partial(vaddr + frag->page_offset +
1235 offset - start, copy, 0);
1236 kunmap_skb_frag(vaddr);
1237 csum = csum_block_add(csum, csum2, pos);
1246 if (skb_shinfo(skb)->frag_list) {
1247 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1249 for (; list; list = list->next) {
1252 BUG_TRAP(start <= offset + len);
1254 end = start + list->len;
1255 if ((copy = end - offset) > 0) {
1259 csum2 = skb_checksum(list, offset - start,
1261 csum = csum_block_add(csum, csum2, pos);
1262 if ((len -= copy) == 0)
1275 /* Both of above in one bottle. */
1277 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1278 u8 *to, int len, __wsum csum)
1280 int start = skb_headlen(skb);
1281 int i, copy = start - offset;
1288 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1290 if ((len -= copy) == 0)
1297 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1300 BUG_TRAP(start <= offset + len);
1302 end = start + skb_shinfo(skb)->frags[i].size;
1303 if ((copy = end - offset) > 0) {
1306 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1310 vaddr = kmap_skb_frag(frag);
1311 csum2 = csum_partial_copy_nocheck(vaddr +
1315 kunmap_skb_frag(vaddr);
1316 csum = csum_block_add(csum, csum2, pos);
1326 if (skb_shinfo(skb)->frag_list) {
1327 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1329 for (; list; list = list->next) {
1333 BUG_TRAP(start <= offset + len);
1335 end = start + list->len;
1336 if ((copy = end - offset) > 0) {
1339 csum2 = skb_copy_and_csum_bits(list,
1342 csum = csum_block_add(csum, csum2, pos);
1343 if ((len -= copy) == 0)
1356 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1361 if (skb->ip_summed == CHECKSUM_PARTIAL)
1362 csstart = skb_transport_offset(skb);
1364 csstart = skb_headlen(skb);
1366 BUG_ON(csstart > skb_headlen(skb));
1368 memcpy(to, skb->data, csstart);
1371 if (csstart != skb->len)
1372 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1373 skb->len - csstart, 0);
1375 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1376 long csstuff = csstart + skb->csum_offset;
1378 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1383 * skb_dequeue - remove from the head of the queue
1384 * @list: list to dequeue from
1386 * Remove the head of the list. The list lock is taken so the function
1387 * may be used safely with other locking list functions. The head item is
1388 * returned or %NULL if the list is empty.
1391 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1393 unsigned long flags;
1394 struct sk_buff *result;
1396 spin_lock_irqsave(&list->lock, flags);
1397 result = __skb_dequeue(list);
1398 spin_unlock_irqrestore(&list->lock, flags);
1403 * skb_dequeue_tail - remove from the tail of the queue
1404 * @list: list to dequeue from
1406 * Remove the tail of the list. The list lock is taken so the function
1407 * may be used safely with other locking list functions. The tail item is
1408 * returned or %NULL if the list is empty.
1410 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1412 unsigned long flags;
1413 struct sk_buff *result;
1415 spin_lock_irqsave(&list->lock, flags);
1416 result = __skb_dequeue_tail(list);
1417 spin_unlock_irqrestore(&list->lock, flags);
1422 * skb_queue_purge - empty a list
1423 * @list: list to empty
1425 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1426 * the list and one reference dropped. This function takes the list
1427 * lock and is atomic with respect to other list locking functions.
1429 void skb_queue_purge(struct sk_buff_head *list)
1431 struct sk_buff *skb;
1432 while ((skb = skb_dequeue(list)) != NULL)
1437 * skb_queue_head - queue a buffer at the list head
1438 * @list: list to use
1439 * @newsk: buffer to queue
1441 * Queue a buffer at the start of the list. This function takes the
1442 * list lock and can be used safely with other locking &sk_buff functions
1445 * A buffer cannot be placed on two lists at the same time.
1447 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1449 unsigned long flags;
1451 spin_lock_irqsave(&list->lock, flags);
1452 __skb_queue_head(list, newsk);
1453 spin_unlock_irqrestore(&list->lock, flags);
1457 * skb_queue_tail - queue a buffer at the list tail
1458 * @list: list to use
1459 * @newsk: buffer to queue
1461 * Queue a buffer at the tail of the list. This function takes the
1462 * list lock and can be used safely with other locking &sk_buff functions
1465 * A buffer cannot be placed on two lists at the same time.
1467 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1469 unsigned long flags;
1471 spin_lock_irqsave(&list->lock, flags);
1472 __skb_queue_tail(list, newsk);
1473 spin_unlock_irqrestore(&list->lock, flags);
1477 * skb_unlink - remove a buffer from a list
1478 * @skb: buffer to remove
1479 * @list: list to use
1481 * Remove a packet from a list. The list locks are taken and this
1482 * function is atomic with respect to other list locked calls
1484 * You must know what list the SKB is on.
1486 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1488 unsigned long flags;
1490 spin_lock_irqsave(&list->lock, flags);
1491 __skb_unlink(skb, list);
1492 spin_unlock_irqrestore(&list->lock, flags);
1496 * skb_append - append a buffer
1497 * @old: buffer to insert after
1498 * @newsk: buffer to insert
1499 * @list: list to use
1501 * Place a packet after a given packet in a list. The list locks are taken
1502 * and this function is atomic with respect to other list locked calls.
1503 * A buffer cannot be placed on two lists at the same time.
1505 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1507 unsigned long flags;
1509 spin_lock_irqsave(&list->lock, flags);
1510 __skb_append(old, newsk, list);
1511 spin_unlock_irqrestore(&list->lock, flags);
1516 * skb_insert - insert a buffer
1517 * @old: buffer to insert before
1518 * @newsk: buffer to insert
1519 * @list: list to use
1521 * Place a packet before a given packet in a list. The list locks are
1522 * taken and this function is atomic with respect to other list locked
1525 * A buffer cannot be placed on two lists at the same time.
1527 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1529 unsigned long flags;
1531 spin_lock_irqsave(&list->lock, flags);
1532 __skb_insert(newsk, old->prev, old, list);
1533 spin_unlock_irqrestore(&list->lock, flags);
1538 * Tune the memory allocator for a new MTU size.
1540 void skb_add_mtu(int mtu)
1542 /* Must match allocation in alloc_skb */
1543 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1545 kmem_add_cache_size(mtu);
1549 static inline void skb_split_inside_header(struct sk_buff *skb,
1550 struct sk_buff* skb1,
1551 const u32 len, const int pos)
1555 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1557 /* And move data appendix as is. */
1558 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1559 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1561 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1562 skb_shinfo(skb)->nr_frags = 0;
1563 skb1->data_len = skb->data_len;
1564 skb1->len += skb1->data_len;
1567 skb_set_tail_pointer(skb, len);
1570 static inline void skb_split_no_header(struct sk_buff *skb,
1571 struct sk_buff* skb1,
1572 const u32 len, int pos)
1575 const int nfrags = skb_shinfo(skb)->nr_frags;
1577 skb_shinfo(skb)->nr_frags = 0;
1578 skb1->len = skb1->data_len = skb->len - len;
1580 skb->data_len = len - pos;
1582 for (i = 0; i < nfrags; i++) {
1583 int size = skb_shinfo(skb)->frags[i].size;
1585 if (pos + size > len) {
1586 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1590 * We have two variants in this case:
1591 * 1. Move all the frag to the second
1592 * part, if it is possible. F.e.
1593 * this approach is mandatory for TUX,
1594 * where splitting is expensive.
1595 * 2. Split is accurately. We make this.
1597 get_page(skb_shinfo(skb)->frags[i].page);
1598 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1599 skb_shinfo(skb1)->frags[0].size -= len - pos;
1600 skb_shinfo(skb)->frags[i].size = len - pos;
1601 skb_shinfo(skb)->nr_frags++;
1605 skb_shinfo(skb)->nr_frags++;
1608 skb_shinfo(skb1)->nr_frags = k;
1612 * skb_split - Split fragmented skb to two parts at length len.
1613 * @skb: the buffer to split
1614 * @skb1: the buffer to receive the second part
1615 * @len: new length for skb
1617 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1619 int pos = skb_headlen(skb);
1621 if (len < pos) /* Split line is inside header. */
1622 skb_split_inside_header(skb, skb1, len, pos);
1623 else /* Second chunk has no header, nothing to copy. */
1624 skb_split_no_header(skb, skb1, len, pos);
1628 * skb_prepare_seq_read - Prepare a sequential read of skb data
1629 * @skb: the buffer to read
1630 * @from: lower offset of data to be read
1631 * @to: upper offset of data to be read
1632 * @st: state variable
1634 * Initializes the specified state variable. Must be called before
1635 * invoking skb_seq_read() for the first time.
1637 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1638 unsigned int to, struct skb_seq_state *st)
1640 st->lower_offset = from;
1641 st->upper_offset = to;
1642 st->root_skb = st->cur_skb = skb;
1643 st->frag_idx = st->stepped_offset = 0;
1644 st->frag_data = NULL;
1648 * skb_seq_read - Sequentially read skb data
1649 * @consumed: number of bytes consumed by the caller so far
1650 * @data: destination pointer for data to be returned
1651 * @st: state variable
1653 * Reads a block of skb data at &consumed relative to the
1654 * lower offset specified to skb_prepare_seq_read(). Assigns
1655 * the head of the data block to &data and returns the length
1656 * of the block or 0 if the end of the skb data or the upper
1657 * offset has been reached.
1659 * The caller is not required to consume all of the data
1660 * returned, i.e. &consumed is typically set to the number
1661 * of bytes already consumed and the next call to
1662 * skb_seq_read() will return the remaining part of the block.
1664 * Note: The size of each block of data returned can be arbitary,
1665 * this limitation is the cost for zerocopy seqeuental
1666 * reads of potentially non linear data.
1668 * Note: Fragment lists within fragments are not implemented
1669 * at the moment, state->root_skb could be replaced with
1670 * a stack for this purpose.
1672 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1673 struct skb_seq_state *st)
1675 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1678 if (unlikely(abs_offset >= st->upper_offset))
1682 block_limit = skb_headlen(st->cur_skb);
1684 if (abs_offset < block_limit) {
1685 *data = st->cur_skb->data + abs_offset;
1686 return block_limit - abs_offset;
1689 if (st->frag_idx == 0 && !st->frag_data)
1690 st->stepped_offset += skb_headlen(st->cur_skb);
1692 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1693 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1694 block_limit = frag->size + st->stepped_offset;
1696 if (abs_offset < block_limit) {
1698 st->frag_data = kmap_skb_frag(frag);
1700 *data = (u8 *) st->frag_data + frag->page_offset +
1701 (abs_offset - st->stepped_offset);
1703 return block_limit - abs_offset;
1706 if (st->frag_data) {
1707 kunmap_skb_frag(st->frag_data);
1708 st->frag_data = NULL;
1712 st->stepped_offset += frag->size;
1715 if (st->cur_skb->next) {
1716 st->cur_skb = st->cur_skb->next;
1719 } else if (st->root_skb == st->cur_skb &&
1720 skb_shinfo(st->root_skb)->frag_list) {
1721 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1729 * skb_abort_seq_read - Abort a sequential read of skb data
1730 * @st: state variable
1732 * Must be called if skb_seq_read() was not called until it
1735 void skb_abort_seq_read(struct skb_seq_state *st)
1738 kunmap_skb_frag(st->frag_data);
1741 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1743 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1744 struct ts_config *conf,
1745 struct ts_state *state)
1747 return skb_seq_read(offset, text, TS_SKB_CB(state));
1750 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1752 skb_abort_seq_read(TS_SKB_CB(state));
1756 * skb_find_text - Find a text pattern in skb data
1757 * @skb: the buffer to look in
1758 * @from: search offset
1760 * @config: textsearch configuration
1761 * @state: uninitialized textsearch state variable
1763 * Finds a pattern in the skb data according to the specified
1764 * textsearch configuration. Use textsearch_next() to retrieve
1765 * subsequent occurrences of the pattern. Returns the offset
1766 * to the first occurrence or UINT_MAX if no match was found.
1768 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1769 unsigned int to, struct ts_config *config,
1770 struct ts_state *state)
1774 config->get_next_block = skb_ts_get_next_block;
1775 config->finish = skb_ts_finish;
1777 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1779 ret = textsearch_find(config, state);
1780 return (ret <= to - from ? ret : UINT_MAX);
1784 * skb_append_datato_frags: - append the user data to a skb
1785 * @sk: sock structure
1786 * @skb: skb structure to be appened with user data.
1787 * @getfrag: call back function to be used for getting the user data
1788 * @from: pointer to user message iov
1789 * @length: length of the iov message
1791 * Description: This procedure append the user data in the fragment part
1792 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1794 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1795 int (*getfrag)(void *from, char *to, int offset,
1796 int len, int odd, struct sk_buff *skb),
1797 void *from, int length)
1800 skb_frag_t *frag = NULL;
1801 struct page *page = NULL;
1807 /* Return error if we don't have space for new frag */
1808 frg_cnt = skb_shinfo(skb)->nr_frags;
1809 if (frg_cnt >= MAX_SKB_FRAGS)
1812 /* allocate a new page for next frag */
1813 page = alloc_pages(sk->sk_allocation, 0);
1815 /* If alloc_page fails just return failure and caller will
1816 * free previous allocated pages by doing kfree_skb()
1821 /* initialize the next frag */
1822 sk->sk_sndmsg_page = page;
1823 sk->sk_sndmsg_off = 0;
1824 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1825 skb->truesize += PAGE_SIZE;
1826 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1828 /* get the new initialized frag */
1829 frg_cnt = skb_shinfo(skb)->nr_frags;
1830 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1832 /* copy the user data to page */
1833 left = PAGE_SIZE - frag->page_offset;
1834 copy = (length > left)? left : length;
1836 ret = getfrag(from, (page_address(frag->page) +
1837 frag->page_offset + frag->size),
1838 offset, copy, 0, skb);
1842 /* copy was successful so update the size parameters */
1843 sk->sk_sndmsg_off += copy;
1846 skb->data_len += copy;
1850 } while (length > 0);
1856 * skb_pull_rcsum - pull skb and update receive checksum
1857 * @skb: buffer to update
1858 * @start: start of data before pull
1859 * @len: length of data pulled
1861 * This function performs an skb_pull on the packet and updates
1862 * update the CHECKSUM_COMPLETE checksum. It should be used on
1863 * receive path processing instead of skb_pull unless you know
1864 * that the checksum difference is zero (e.g., a valid IP header)
1865 * or you are setting ip_summed to CHECKSUM_NONE.
1867 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1869 BUG_ON(len > skb->len);
1871 BUG_ON(skb->len < skb->data_len);
1872 skb_postpull_rcsum(skb, skb->data, len);
1873 return skb->data += len;
1876 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1879 * skb_segment - Perform protocol segmentation on skb.
1880 * @skb: buffer to segment
1881 * @features: features for the output path (see dev->features)
1883 * This function performs segmentation on the given skb. It returns
1884 * the segment at the given position. It returns NULL if there are
1885 * no more segments to generate, or when an error is encountered.
1887 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1889 struct sk_buff *segs = NULL;
1890 struct sk_buff *tail = NULL;
1891 unsigned int mss = skb_shinfo(skb)->gso_size;
1892 unsigned int doffset = skb->data - skb_mac_header(skb);
1893 unsigned int offset = doffset;
1894 unsigned int headroom;
1896 int sg = features & NETIF_F_SG;
1897 int nfrags = skb_shinfo(skb)->nr_frags;
1902 __skb_push(skb, doffset);
1903 headroom = skb_headroom(skb);
1904 pos = skb_headlen(skb);
1907 struct sk_buff *nskb;
1913 len = skb->len - offset;
1917 hsize = skb_headlen(skb) - offset;
1920 if (hsize > len || !sg)
1923 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1924 if (unlikely(!nskb))
1933 nskb->dev = skb->dev;
1934 nskb->priority = skb->priority;
1935 nskb->protocol = skb->protocol;
1936 nskb->dst = dst_clone(skb->dst);
1937 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1938 nskb->pkt_type = skb->pkt_type;
1939 nskb->mac_len = skb->mac_len;
1941 skb_reserve(nskb, headroom);
1942 skb_reset_mac_header(nskb);
1943 skb_set_network_header(nskb, skb->mac_len);
1944 nskb->transport_header = (nskb->network_header +
1945 skb_network_header_len(skb));
1946 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1949 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1955 frag = skb_shinfo(nskb)->frags;
1958 nskb->ip_summed = CHECKSUM_PARTIAL;
1959 nskb->csum = skb->csum;
1960 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1962 while (pos < offset + len) {
1963 BUG_ON(i >= nfrags);
1965 *frag = skb_shinfo(skb)->frags[i];
1966 get_page(frag->page);
1970 frag->page_offset += offset - pos;
1971 frag->size -= offset - pos;
1976 if (pos + size <= offset + len) {
1980 frag->size -= pos + size - (offset + len);
1987 skb_shinfo(nskb)->nr_frags = k;
1988 nskb->data_len = len - hsize;
1989 nskb->len += nskb->data_len;
1990 nskb->truesize += nskb->data_len;
1991 } while ((offset += len) < skb->len);
1996 while ((skb = segs)) {
2000 return ERR_PTR(err);
2003 EXPORT_SYMBOL_GPL(skb_segment);
2005 void __init skb_init(void)
2007 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2008 sizeof(struct sk_buff),
2010 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2012 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2013 (2*sizeof(struct sk_buff)) +
2016 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2020 EXPORT_SYMBOL(___pskb_trim);
2021 EXPORT_SYMBOL(__kfree_skb);
2022 EXPORT_SYMBOL(kfree_skb);
2023 EXPORT_SYMBOL(__pskb_pull_tail);
2024 EXPORT_SYMBOL(__alloc_skb);
2025 EXPORT_SYMBOL(__netdev_alloc_skb);
2026 EXPORT_SYMBOL(pskb_copy);
2027 EXPORT_SYMBOL(pskb_expand_head);
2028 EXPORT_SYMBOL(skb_checksum);
2029 EXPORT_SYMBOL(skb_clone);
2030 EXPORT_SYMBOL(skb_clone_fraglist);
2031 EXPORT_SYMBOL(skb_copy);
2032 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2033 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2034 EXPORT_SYMBOL(skb_copy_bits);
2035 EXPORT_SYMBOL(skb_copy_expand);
2036 EXPORT_SYMBOL(skb_over_panic);
2037 EXPORT_SYMBOL(skb_pad);
2038 EXPORT_SYMBOL(skb_realloc_headroom);
2039 EXPORT_SYMBOL(skb_under_panic);
2040 EXPORT_SYMBOL(skb_dequeue);
2041 EXPORT_SYMBOL(skb_dequeue_tail);
2042 EXPORT_SYMBOL(skb_insert);
2043 EXPORT_SYMBOL(skb_queue_purge);
2044 EXPORT_SYMBOL(skb_queue_head);
2045 EXPORT_SYMBOL(skb_queue_tail);
2046 EXPORT_SYMBOL(skb_unlink);
2047 EXPORT_SYMBOL(skb_append);
2048 EXPORT_SYMBOL(skb_split);
2049 EXPORT_SYMBOL(skb_prepare_seq_read);
2050 EXPORT_SYMBOL(skb_seq_read);
2051 EXPORT_SYMBOL(skb_abort_seq_read);
2052 EXPORT_SYMBOL(skb_find_text);
2053 EXPORT_SYMBOL(skb_append_datato_frags);