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/config.h>
42 #include <linux/module.h>
43 #include <linux/types.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
47 #include <linux/interrupt.h>
49 #include <linux/inet.h>
50 #include <linux/slab.h>
51 #include <linux/netdevice.h>
52 #ifdef CONFIG_NET_CLS_ACT
53 #include <net/pkt_sched.h>
55 #include <linux/string.h>
56 #include <linux/skbuff.h>
57 #include <linux/cache.h>
58 #include <linux/rtnetlink.h>
59 #include <linux/init.h>
60 #include <linux/highmem.h>
62 #include <net/protocol.h>
65 #include <net/checksum.h>
68 #include <asm/uaccess.h>
69 #include <asm/system.h>
71 static kmem_cache_t *skbuff_head_cache;
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 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
115 * 'private' fields and also do memory statistics to find all the
121 * alloc_skb - allocate a network buffer
122 * @size: size to allocate
123 * @gfp_mask: allocation mask
125 * Allocate a new &sk_buff. The returned buffer has no headroom and a
126 * tail room of size bytes. The object has a reference count of one.
127 * The return is the buffer. On a failure the return is %NULL.
129 * Buffers may only be allocated from interrupts using a @gfp_mask of
132 struct sk_buff *alloc_skb(unsigned int size, unsigned int __nocast gfp_mask)
138 skb = kmem_cache_alloc(skbuff_head_cache,
139 gfp_mask & ~__GFP_DMA);
143 /* Get the DATA. Size must match skb_add_mtu(). */
144 size = SKB_DATA_ALIGN(size);
145 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
149 memset(skb, 0, offsetof(struct sk_buff, truesize));
150 skb->truesize = size + sizeof(struct sk_buff);
151 atomic_set(&skb->users, 1);
155 skb->end = data + size;
157 atomic_set(&(skb_shinfo(skb)->dataref), 1);
158 skb_shinfo(skb)->nr_frags = 0;
159 skb_shinfo(skb)->tso_size = 0;
160 skb_shinfo(skb)->tso_segs = 0;
161 skb_shinfo(skb)->frag_list = NULL;
165 kmem_cache_free(skbuff_head_cache, skb);
171 * alloc_skb_from_cache - allocate a network buffer
172 * @cp: kmem_cache from which to allocate the data area
173 * (object size must be big enough for @size bytes + skb overheads)
174 * @size: size to allocate
175 * @gfp_mask: allocation mask
177 * Allocate a new &sk_buff. The returned buffer has no headroom and
178 * tail room of size bytes. The object has a reference count of one.
179 * The return is the buffer. On a failure the return is %NULL.
181 * Buffers may only be allocated from interrupts using a @gfp_mask of
184 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
186 unsigned int __nocast gfp_mask)
192 skb = kmem_cache_alloc(skbuff_head_cache,
193 gfp_mask & ~__GFP_DMA);
198 size = SKB_DATA_ALIGN(size);
199 data = kmem_cache_alloc(cp, gfp_mask);
203 memset(skb, 0, offsetof(struct sk_buff, truesize));
204 skb->truesize = size + sizeof(struct sk_buff);
205 atomic_set(&skb->users, 1);
209 skb->end = data + size;
211 atomic_set(&(skb_shinfo(skb)->dataref), 1);
212 skb_shinfo(skb)->nr_frags = 0;
213 skb_shinfo(skb)->tso_size = 0;
214 skb_shinfo(skb)->tso_segs = 0;
215 skb_shinfo(skb)->frag_list = NULL;
219 kmem_cache_free(skbuff_head_cache, skb);
225 static void skb_drop_fraglist(struct sk_buff *skb)
227 struct sk_buff *list = skb_shinfo(skb)->frag_list;
229 skb_shinfo(skb)->frag_list = NULL;
232 struct sk_buff *this = list;
238 static void skb_clone_fraglist(struct sk_buff *skb)
240 struct sk_buff *list;
242 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
246 void skb_release_data(struct sk_buff *skb)
249 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
250 &skb_shinfo(skb)->dataref)) {
251 if (skb_shinfo(skb)->nr_frags) {
253 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
254 put_page(skb_shinfo(skb)->frags[i].page);
257 if (skb_shinfo(skb)->frag_list)
258 skb_drop_fraglist(skb);
265 * Free an skbuff by memory without cleaning the state.
267 void kfree_skbmem(struct sk_buff *skb)
269 skb_release_data(skb);
270 kmem_cache_free(skbuff_head_cache, skb);
274 * __kfree_skb - private function
277 * Free an sk_buff. Release anything attached to the buffer.
278 * Clean the state. This is an internal helper function. Users should
279 * always call kfree_skb
282 void __kfree_skb(struct sk_buff *skb)
284 BUG_ON(skb->list != NULL);
286 dst_release(skb->dst);
288 secpath_put(skb->sp);
290 if (skb->destructor) {
292 skb->destructor(skb);
294 #ifdef CONFIG_NETFILTER
295 nf_conntrack_put(skb->nfct);
296 #ifdef CONFIG_BRIDGE_NETFILTER
297 nf_bridge_put(skb->nf_bridge);
300 /* XXX: IS this still necessary? - JHS */
301 #ifdef CONFIG_NET_SCHED
303 #ifdef CONFIG_NET_CLS_ACT
313 * skb_clone - duplicate an sk_buff
314 * @skb: buffer to clone
315 * @gfp_mask: allocation priority
317 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
318 * copies share the same packet data but not structure. The new
319 * buffer has a reference count of 1. If the allocation fails the
320 * function returns %NULL otherwise the new buffer is returned.
322 * If this function is called from an interrupt gfp_mask() must be
326 struct sk_buff *skb_clone(struct sk_buff *skb, unsigned int __nocast gfp_mask)
328 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
333 #define C(x) n->x = skb->x
335 n->next = n->prev = NULL;
348 secpath_get(skb->sp);
350 memcpy(n->cb, skb->cb, sizeof(skb->cb));
361 n->destructor = NULL;
362 #ifdef CONFIG_NETFILTER
365 nf_conntrack_get(skb->nfct);
367 #ifdef CONFIG_BRIDGE_NETFILTER
369 nf_bridge_get(skb->nf_bridge);
371 #endif /*CONFIG_NETFILTER*/
372 #if defined(CONFIG_HIPPI)
375 #ifdef CONFIG_NET_SCHED
377 #ifdef CONFIG_NET_CLS_ACT
378 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
379 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
380 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
387 atomic_set(&n->users, 1);
393 atomic_inc(&(skb_shinfo(skb)->dataref));
399 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
402 * Shift between the two data areas in bytes
404 unsigned long offset = new->data - old->data;
409 new->real_dev = old->real_dev;
410 new->priority = old->priority;
411 new->protocol = old->protocol;
412 new->dst = dst_clone(old->dst);
414 new->sp = secpath_get(old->sp);
416 new->h.raw = old->h.raw + offset;
417 new->nh.raw = old->nh.raw + offset;
418 new->mac.raw = old->mac.raw + offset;
419 memcpy(new->cb, old->cb, sizeof(old->cb));
420 new->local_df = old->local_df;
421 new->pkt_type = old->pkt_type;
422 new->stamp = old->stamp;
423 new->destructor = NULL;
424 #ifdef CONFIG_NETFILTER
425 new->nfmark = old->nfmark;
426 new->nfct = old->nfct;
427 nf_conntrack_get(old->nfct);
428 new->nfctinfo = old->nfctinfo;
429 #ifdef CONFIG_BRIDGE_NETFILTER
430 new->nf_bridge = old->nf_bridge;
431 nf_bridge_get(old->nf_bridge);
434 #ifdef CONFIG_NET_SCHED
435 #ifdef CONFIG_NET_CLS_ACT
436 new->tc_verd = old->tc_verd;
438 new->tc_index = old->tc_index;
440 atomic_set(&new->users, 1);
441 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
442 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
446 * skb_copy - create private copy of an sk_buff
447 * @skb: buffer to copy
448 * @gfp_mask: allocation priority
450 * Make a copy of both an &sk_buff and its data. This is used when the
451 * caller wishes to modify the data and needs a private copy of the
452 * data to alter. Returns %NULL on failure or the pointer to the buffer
453 * on success. The returned buffer has a reference count of 1.
455 * As by-product this function converts non-linear &sk_buff to linear
456 * one, so that &sk_buff becomes completely private and caller is allowed
457 * to modify all the data of returned buffer. This means that this
458 * function is not recommended for use in circumstances when only
459 * header is going to be modified. Use pskb_copy() instead.
462 struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask)
464 int headerlen = skb->data - skb->head;
466 * Allocate the copy buffer
468 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
473 /* Set the data pointer */
474 skb_reserve(n, headerlen);
475 /* Set the tail pointer and length */
476 skb_put(n, skb->len);
478 n->ip_summed = skb->ip_summed;
480 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
483 copy_skb_header(n, skb);
489 * pskb_copy - create copy of an sk_buff with private head.
490 * @skb: buffer to copy
491 * @gfp_mask: allocation priority
493 * Make a copy of both an &sk_buff and part of its data, located
494 * in header. Fragmented data remain shared. This is used when
495 * the caller wishes to modify only header of &sk_buff and needs
496 * private copy of the header to alter. Returns %NULL on failure
497 * or the pointer to the buffer on success.
498 * The returned buffer has a reference count of 1.
501 struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask)
504 * Allocate the copy buffer
506 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
511 /* Set the data pointer */
512 skb_reserve(n, skb->data - skb->head);
513 /* Set the tail pointer and length */
514 skb_put(n, skb_headlen(skb));
516 memcpy(n->data, skb->data, n->len);
518 n->ip_summed = skb->ip_summed;
520 n->data_len = skb->data_len;
523 if (skb_shinfo(skb)->nr_frags) {
526 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
527 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
528 get_page(skb_shinfo(n)->frags[i].page);
530 skb_shinfo(n)->nr_frags = i;
533 if (skb_shinfo(skb)->frag_list) {
534 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
535 skb_clone_fraglist(n);
538 copy_skb_header(n, skb);
544 * pskb_expand_head - reallocate header of &sk_buff
545 * @skb: buffer to reallocate
546 * @nhead: room to add at head
547 * @ntail: room to add at tail
548 * @gfp_mask: allocation priority
550 * Expands (or creates identical copy, if &nhead and &ntail are zero)
551 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
552 * reference count of 1. Returns zero in the case of success or error,
553 * if expansion failed. In the last case, &sk_buff is not changed.
555 * All the pointers pointing into skb header may change and must be
556 * reloaded after call to this function.
559 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
560 unsigned int __nocast gfp_mask)
564 int size = nhead + (skb->end - skb->head) + ntail;
570 size = SKB_DATA_ALIGN(size);
572 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
576 /* Copy only real data... and, alas, header. This should be
577 * optimized for the cases when header is void. */
578 memcpy(data + nhead, skb->head, skb->tail - skb->head);
579 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
581 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
582 get_page(skb_shinfo(skb)->frags[i].page);
584 if (skb_shinfo(skb)->frag_list)
585 skb_clone_fraglist(skb);
587 skb_release_data(skb);
589 off = (data + nhead) - skb->head;
592 skb->end = data + size;
600 atomic_set(&skb_shinfo(skb)->dataref, 1);
607 /* Make private copy of skb with writable head and some headroom */
609 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
611 struct sk_buff *skb2;
612 int delta = headroom - skb_headroom(skb);
615 skb2 = pskb_copy(skb, GFP_ATOMIC);
617 skb2 = skb_clone(skb, GFP_ATOMIC);
618 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
629 * skb_copy_expand - copy and expand sk_buff
630 * @skb: buffer to copy
631 * @newheadroom: new free bytes at head
632 * @newtailroom: new free bytes at tail
633 * @gfp_mask: allocation priority
635 * Make a copy of both an &sk_buff and its data and while doing so
636 * allocate additional space.
638 * This is used when the caller wishes to modify the data and needs a
639 * private copy of the data to alter as well as more space for new fields.
640 * Returns %NULL on failure or the pointer to the buffer
641 * on success. The returned buffer has a reference count of 1.
643 * You must pass %GFP_ATOMIC as the allocation priority if this function
644 * is called from an interrupt.
646 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
647 * only by netfilter in the cases when checksum is recalculated? --ANK
649 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
650 int newheadroom, int newtailroom,
651 unsigned int __nocast gfp_mask)
654 * Allocate the copy buffer
656 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
658 int head_copy_len, head_copy_off;
663 skb_reserve(n, newheadroom);
665 /* Set the tail pointer and length */
666 skb_put(n, skb->len);
668 head_copy_len = skb_headroom(skb);
670 if (newheadroom <= head_copy_len)
671 head_copy_len = newheadroom;
673 head_copy_off = newheadroom - head_copy_len;
675 /* Copy the linear header and data. */
676 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
677 skb->len + head_copy_len))
680 copy_skb_header(n, skb);
686 * skb_pad - zero pad the tail of an skb
687 * @skb: buffer to pad
690 * Ensure that a buffer is followed by a padding area that is zero
691 * filled. Used by network drivers which may DMA or transfer data
692 * beyond the buffer end onto the wire.
694 * May return NULL in out of memory cases.
697 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
699 struct sk_buff *nskb;
701 /* If the skbuff is non linear tailroom is always zero.. */
702 if (skb_tailroom(skb) >= pad) {
703 memset(skb->data+skb->len, 0, pad);
707 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
710 memset(nskb->data+nskb->len, 0, pad);
714 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
715 * If realloc==0 and trimming is impossible without change of data,
719 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
721 int offset = skb_headlen(skb);
722 int nfrags = skb_shinfo(skb)->nr_frags;
725 for (i = 0; i < nfrags; i++) {
726 int end = offset + skb_shinfo(skb)->frags[i].size;
728 if (skb_cloned(skb)) {
731 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
735 put_page(skb_shinfo(skb)->frags[i].page);
736 skb_shinfo(skb)->nr_frags--;
738 skb_shinfo(skb)->frags[i].size = len - offset;
745 skb->data_len -= skb->len - len;
748 if (len <= skb_headlen(skb)) {
751 skb->tail = skb->data + len;
752 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
753 skb_drop_fraglist(skb);
755 skb->data_len -= skb->len - len;
764 * __pskb_pull_tail - advance tail of skb header
765 * @skb: buffer to reallocate
766 * @delta: number of bytes to advance tail
768 * The function makes a sense only on a fragmented &sk_buff,
769 * it expands header moving its tail forward and copying necessary
770 * data from fragmented part.
772 * &sk_buff MUST have reference count of 1.
774 * Returns %NULL (and &sk_buff does not change) if pull failed
775 * or value of new tail of skb in the case of success.
777 * All the pointers pointing into skb header may change and must be
778 * reloaded after call to this function.
781 /* Moves tail of skb head forward, copying data from fragmented part,
782 * when it is necessary.
783 * 1. It may fail due to malloc failure.
784 * 2. It may change skb pointers.
786 * It is pretty complicated. Luckily, it is called only in exceptional cases.
788 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
790 /* If skb has not enough free space at tail, get new one
791 * plus 128 bytes for future expansions. If we have enough
792 * room at tail, reallocate without expansion only if skb is cloned.
794 int i, k, eat = (skb->tail + delta) - skb->end;
796 if (eat > 0 || skb_cloned(skb)) {
797 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
802 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
805 /* Optimization: no fragments, no reasons to preestimate
806 * size of pulled pages. Superb.
808 if (!skb_shinfo(skb)->frag_list)
811 /* Estimate size of pulled pages. */
813 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
814 if (skb_shinfo(skb)->frags[i].size >= eat)
816 eat -= skb_shinfo(skb)->frags[i].size;
819 /* If we need update frag list, we are in troubles.
820 * Certainly, it possible to add an offset to skb data,
821 * but taking into account that pulling is expected to
822 * be very rare operation, it is worth to fight against
823 * further bloating skb head and crucify ourselves here instead.
824 * Pure masohism, indeed. 8)8)
827 struct sk_buff *list = skb_shinfo(skb)->frag_list;
828 struct sk_buff *clone = NULL;
829 struct sk_buff *insp = NULL;
835 if (list->len <= eat) {
836 /* Eaten as whole. */
841 /* Eaten partially. */
843 if (skb_shared(list)) {
844 /* Sucks! We need to fork list. :-( */
845 clone = skb_clone(list, GFP_ATOMIC);
851 /* This may be pulled without
855 if (!pskb_pull(list, eat)) {
864 /* Free pulled out fragments. */
865 while ((list = skb_shinfo(skb)->frag_list) != insp) {
866 skb_shinfo(skb)->frag_list = list->next;
869 /* And insert new clone at head. */
872 skb_shinfo(skb)->frag_list = clone;
875 /* Success! Now we may commit changes to skb data. */
880 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
881 if (skb_shinfo(skb)->frags[i].size <= eat) {
882 put_page(skb_shinfo(skb)->frags[i].page);
883 eat -= skb_shinfo(skb)->frags[i].size;
885 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
887 skb_shinfo(skb)->frags[k].page_offset += eat;
888 skb_shinfo(skb)->frags[k].size -= eat;
894 skb_shinfo(skb)->nr_frags = k;
897 skb->data_len -= delta;
902 /* Copy some data bits from skb to kernel buffer. */
904 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
907 int start = skb_headlen(skb);
909 if (offset > (int)skb->len - len)
913 if ((copy = start - offset) > 0) {
916 memcpy(to, skb->data + offset, copy);
917 if ((len -= copy) == 0)
923 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
926 BUG_TRAP(start <= offset + len);
928 end = start + skb_shinfo(skb)->frags[i].size;
929 if ((copy = end - offset) > 0) {
935 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
937 vaddr + skb_shinfo(skb)->frags[i].page_offset+
938 offset - start, copy);
939 kunmap_skb_frag(vaddr);
941 if ((len -= copy) == 0)
949 if (skb_shinfo(skb)->frag_list) {
950 struct sk_buff *list = skb_shinfo(skb)->frag_list;
952 for (; list; list = list->next) {
955 BUG_TRAP(start <= offset + len);
957 end = start + list->len;
958 if ((copy = end - offset) > 0) {
961 if (skb_copy_bits(list, offset - start,
964 if ((len -= copy) == 0)
980 * skb_store_bits - store bits from kernel buffer to skb
981 * @skb: destination buffer
982 * @offset: offset in destination
983 * @from: source buffer
984 * @len: number of bytes to copy
986 * Copy the specified number of bytes from the source buffer to the
987 * destination skb. This function handles all the messy bits of
988 * traversing fragment lists and such.
991 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
994 int start = skb_headlen(skb);
996 if (offset > (int)skb->len - len)
999 if ((copy = start - offset) > 0) {
1002 memcpy(skb->data + offset, from, copy);
1003 if ((len -= copy) == 0)
1009 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1010 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1013 BUG_TRAP(start <= offset + len);
1015 end = start + frag->size;
1016 if ((copy = end - offset) > 0) {
1022 vaddr = kmap_skb_frag(frag);
1023 memcpy(vaddr + frag->page_offset + offset - start,
1025 kunmap_skb_frag(vaddr);
1027 if ((len -= copy) == 0)
1035 if (skb_shinfo(skb)->frag_list) {
1036 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1038 for (; list; list = list->next) {
1041 BUG_TRAP(start <= offset + len);
1043 end = start + list->len;
1044 if ((copy = end - offset) > 0) {
1047 if (skb_store_bits(list, offset - start,
1050 if ((len -= copy) == 0)
1065 EXPORT_SYMBOL(skb_store_bits);
1067 /* Checksum skb data. */
1069 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1070 int len, unsigned int csum)
1072 int start = skb_headlen(skb);
1073 int i, copy = start - offset;
1076 /* Checksum header. */
1080 csum = csum_partial(skb->data + offset, copy, csum);
1081 if ((len -= copy) == 0)
1087 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1090 BUG_TRAP(start <= offset + len);
1092 end = start + skb_shinfo(skb)->frags[i].size;
1093 if ((copy = end - offset) > 0) {
1096 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1100 vaddr = kmap_skb_frag(frag);
1101 csum2 = csum_partial(vaddr + frag->page_offset +
1102 offset - start, copy, 0);
1103 kunmap_skb_frag(vaddr);
1104 csum = csum_block_add(csum, csum2, pos);
1113 if (skb_shinfo(skb)->frag_list) {
1114 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1116 for (; list; list = list->next) {
1119 BUG_TRAP(start <= offset + len);
1121 end = start + list->len;
1122 if ((copy = end - offset) > 0) {
1126 csum2 = skb_checksum(list, offset - start,
1128 csum = csum_block_add(csum, csum2, pos);
1129 if ((len -= copy) == 0)
1143 /* Both of above in one bottle. */
1145 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1146 u8 *to, int len, unsigned int csum)
1148 int start = skb_headlen(skb);
1149 int i, copy = start - offset;
1156 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1158 if ((len -= copy) == 0)
1165 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1168 BUG_TRAP(start <= offset + len);
1170 end = start + skb_shinfo(skb)->frags[i].size;
1171 if ((copy = end - offset) > 0) {
1174 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1178 vaddr = kmap_skb_frag(frag);
1179 csum2 = csum_partial_copy_nocheck(vaddr +
1183 kunmap_skb_frag(vaddr);
1184 csum = csum_block_add(csum, csum2, pos);
1194 if (skb_shinfo(skb)->frag_list) {
1195 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1197 for (; list; list = list->next) {
1201 BUG_TRAP(start <= offset + len);
1203 end = start + list->len;
1204 if ((copy = end - offset) > 0) {
1207 csum2 = skb_copy_and_csum_bits(list,
1210 csum = csum_block_add(csum, csum2, pos);
1211 if ((len -= copy) == 0)
1225 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1230 if (skb->ip_summed == CHECKSUM_HW)
1231 csstart = skb->h.raw - skb->data;
1233 csstart = skb_headlen(skb);
1235 if (csstart > skb_headlen(skb))
1238 memcpy(to, skb->data, csstart);
1241 if (csstart != skb->len)
1242 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1243 skb->len - csstart, 0);
1245 if (skb->ip_summed == CHECKSUM_HW) {
1246 long csstuff = csstart + skb->csum;
1248 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1253 * skb_dequeue - remove from the head of the queue
1254 * @list: list to dequeue from
1256 * Remove the head of the list. The list lock is taken so the function
1257 * may be used safely with other locking list functions. The head item is
1258 * returned or %NULL if the list is empty.
1261 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1263 unsigned long flags;
1264 struct sk_buff *result;
1266 spin_lock_irqsave(&list->lock, flags);
1267 result = __skb_dequeue(list);
1268 spin_unlock_irqrestore(&list->lock, flags);
1273 * skb_dequeue_tail - remove from the tail of the queue
1274 * @list: list to dequeue from
1276 * Remove the tail of the list. The list lock is taken so the function
1277 * may be used safely with other locking list functions. The tail item is
1278 * returned or %NULL if the list is empty.
1280 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1282 unsigned long flags;
1283 struct sk_buff *result;
1285 spin_lock_irqsave(&list->lock, flags);
1286 result = __skb_dequeue_tail(list);
1287 spin_unlock_irqrestore(&list->lock, flags);
1292 * skb_queue_purge - empty a list
1293 * @list: list to empty
1295 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1296 * the list and one reference dropped. This function takes the list
1297 * lock and is atomic with respect to other list locking functions.
1299 void skb_queue_purge(struct sk_buff_head *list)
1301 struct sk_buff *skb;
1302 while ((skb = skb_dequeue(list)) != NULL)
1307 * skb_queue_head - queue a buffer at the list head
1308 * @list: list to use
1309 * @newsk: buffer to queue
1311 * Queue a buffer at the start of the list. This function takes the
1312 * list lock and can be used safely with other locking &sk_buff functions
1315 * A buffer cannot be placed on two lists at the same time.
1317 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1319 unsigned long flags;
1321 spin_lock_irqsave(&list->lock, flags);
1322 __skb_queue_head(list, newsk);
1323 spin_unlock_irqrestore(&list->lock, flags);
1327 * skb_queue_tail - queue a buffer at the list tail
1328 * @list: list to use
1329 * @newsk: buffer to queue
1331 * Queue a buffer at the tail of the list. This function takes the
1332 * list lock and can be used safely with other locking &sk_buff functions
1335 * A buffer cannot be placed on two lists at the same time.
1337 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1339 unsigned long flags;
1341 spin_lock_irqsave(&list->lock, flags);
1342 __skb_queue_tail(list, newsk);
1343 spin_unlock_irqrestore(&list->lock, flags);
1346 * skb_unlink - remove a buffer from a list
1347 * @skb: buffer to remove
1349 * Place a packet after a given packet in a list. The list locks are taken
1350 * and this function is atomic with respect to other list locked calls
1352 * Works even without knowing the list it is sitting on, which can be
1353 * handy at times. It also means that THE LIST MUST EXIST when you
1354 * unlink. Thus a list must have its contents unlinked before it is
1357 void skb_unlink(struct sk_buff *skb)
1359 struct sk_buff_head *list = skb->list;
1362 unsigned long flags;
1364 spin_lock_irqsave(&list->lock, flags);
1365 if (skb->list == list)
1366 __skb_unlink(skb, skb->list);
1367 spin_unlock_irqrestore(&list->lock, flags);
1373 * skb_append - append a buffer
1374 * @old: buffer to insert after
1375 * @newsk: buffer to insert
1377 * Place a packet after a given packet in a list. The list locks are taken
1378 * and this function is atomic with respect to other list locked calls.
1379 * A buffer cannot be placed on two lists at the same time.
1382 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1384 unsigned long flags;
1386 spin_lock_irqsave(&old->list->lock, flags);
1387 __skb_append(old, newsk);
1388 spin_unlock_irqrestore(&old->list->lock, flags);
1393 * skb_insert - insert a buffer
1394 * @old: buffer to insert before
1395 * @newsk: buffer to insert
1397 * Place a packet before a given packet in a list. The list locks are taken
1398 * and this function is atomic with respect to other list locked calls
1399 * A buffer cannot be placed on two lists at the same time.
1402 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1404 unsigned long flags;
1406 spin_lock_irqsave(&old->list->lock, flags);
1407 __skb_insert(newsk, old->prev, old, old->list);
1408 spin_unlock_irqrestore(&old->list->lock, flags);
1413 * Tune the memory allocator for a new MTU size.
1415 void skb_add_mtu(int mtu)
1417 /* Must match allocation in alloc_skb */
1418 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1420 kmem_add_cache_size(mtu);
1424 static inline void skb_split_inside_header(struct sk_buff *skb,
1425 struct sk_buff* skb1,
1426 const u32 len, const int pos)
1430 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1432 /* And move data appendix as is. */
1433 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1434 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1436 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1437 skb_shinfo(skb)->nr_frags = 0;
1438 skb1->data_len = skb->data_len;
1439 skb1->len += skb1->data_len;
1442 skb->tail = skb->data + len;
1445 static inline void skb_split_no_header(struct sk_buff *skb,
1446 struct sk_buff* skb1,
1447 const u32 len, int pos)
1450 const int nfrags = skb_shinfo(skb)->nr_frags;
1452 skb_shinfo(skb)->nr_frags = 0;
1453 skb1->len = skb1->data_len = skb->len - len;
1455 skb->data_len = len - pos;
1457 for (i = 0; i < nfrags; i++) {
1458 int size = skb_shinfo(skb)->frags[i].size;
1460 if (pos + size > len) {
1461 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1465 * We have two variants in this case:
1466 * 1. Move all the frag to the second
1467 * part, if it is possible. F.e.
1468 * this approach is mandatory for TUX,
1469 * where splitting is expensive.
1470 * 2. Split is accurately. We make this.
1472 get_page(skb_shinfo(skb)->frags[i].page);
1473 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1474 skb_shinfo(skb1)->frags[0].size -= len - pos;
1475 skb_shinfo(skb)->frags[i].size = len - pos;
1476 skb_shinfo(skb)->nr_frags++;
1480 skb_shinfo(skb)->nr_frags++;
1483 skb_shinfo(skb1)->nr_frags = k;
1487 * skb_split - Split fragmented skb to two parts at length len.
1488 * @skb: the buffer to split
1489 * @skb1: the buffer to receive the second part
1490 * @len: new length for skb
1492 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1494 int pos = skb_headlen(skb);
1496 if (len < pos) /* Split line is inside header. */
1497 skb_split_inside_header(skb, skb1, len, pos);
1498 else /* Second chunk has no header, nothing to copy. */
1499 skb_split_no_header(skb, skb1, len, pos);
1503 * skb_prepare_seq_read - Prepare a sequential read of skb data
1504 * @skb: the buffer to read
1505 * @from: lower offset of data to be read
1506 * @to: upper offset of data to be read
1507 * @st: state variable
1509 * Initializes the specified state variable. Must be called before
1510 * invoking skb_seq_read() for the first time.
1512 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1513 unsigned int to, struct skb_seq_state *st)
1515 st->lower_offset = from;
1516 st->upper_offset = to;
1517 st->root_skb = st->cur_skb = skb;
1518 st->frag_idx = st->stepped_offset = 0;
1519 st->frag_data = NULL;
1523 * skb_seq_read - Sequentially read skb data
1524 * @consumed: number of bytes consumed by the caller so far
1525 * @data: destination pointer for data to be returned
1526 * @st: state variable
1528 * Reads a block of skb data at &consumed relative to the
1529 * lower offset specified to skb_prepare_seq_read(). Assigns
1530 * the head of the data block to &data and returns the length
1531 * of the block or 0 if the end of the skb data or the upper
1532 * offset has been reached.
1534 * The caller is not required to consume all of the data
1535 * returned, i.e. &consumed is typically set to the number
1536 * of bytes already consumed and the next call to
1537 * skb_seq_read() will return the remaining part of the block.
1539 * Note: The size of each block of data returned can be arbitary,
1540 * this limitation is the cost for zerocopy seqeuental
1541 * reads of potentially non linear data.
1543 * Note: Fragment lists within fragments are not implemented
1544 * at the moment, state->root_skb could be replaced with
1545 * a stack for this purpose.
1547 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1548 struct skb_seq_state *st)
1550 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1553 if (unlikely(abs_offset >= st->upper_offset))
1557 block_limit = skb_headlen(st->cur_skb);
1559 if (abs_offset < block_limit) {
1560 *data = st->cur_skb->data + abs_offset;
1561 return block_limit - abs_offset;
1564 if (st->frag_idx == 0 && !st->frag_data)
1565 st->stepped_offset += skb_headlen(st->cur_skb);
1567 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1568 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1569 block_limit = frag->size + st->stepped_offset;
1571 if (abs_offset < block_limit) {
1573 st->frag_data = kmap_skb_frag(frag);
1575 *data = (u8 *) st->frag_data + frag->page_offset +
1576 (abs_offset - st->stepped_offset);
1578 return block_limit - abs_offset;
1581 if (st->frag_data) {
1582 kunmap_skb_frag(st->frag_data);
1583 st->frag_data = NULL;
1587 st->stepped_offset += frag->size;
1590 if (st->cur_skb->next) {
1591 st->cur_skb = st->cur_skb->next;
1594 } else if (st->root_skb == st->cur_skb &&
1595 skb_shinfo(st->root_skb)->frag_list) {
1596 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1604 * skb_abort_seq_read - Abort a sequential read of skb data
1605 * @st: state variable
1607 * Must be called if skb_seq_read() was not called until it
1610 void skb_abort_seq_read(struct skb_seq_state *st)
1613 kunmap_skb_frag(st->frag_data);
1616 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1618 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1619 struct ts_config *conf,
1620 struct ts_state *state)
1622 return skb_seq_read(offset, text, TS_SKB_CB(state));
1625 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1627 skb_abort_seq_read(TS_SKB_CB(state));
1631 * skb_find_text - Find a text pattern in skb data
1632 * @skb: the buffer to look in
1633 * @from: search offset
1635 * @config: textsearch configuration
1636 * @state: uninitialized textsearch state variable
1638 * Finds a pattern in the skb data according to the specified
1639 * textsearch configuration. Use textsearch_next() to retrieve
1640 * subsequent occurrences of the pattern. Returns the offset
1641 * to the first occurrence or UINT_MAX if no match was found.
1643 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1644 unsigned int to, struct ts_config *config,
1645 struct ts_state *state)
1647 config->get_next_block = skb_ts_get_next_block;
1648 config->finish = skb_ts_finish;
1650 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1652 return textsearch_find(config, state);
1655 void __init skb_init(void)
1657 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1658 sizeof(struct sk_buff),
1662 if (!skbuff_head_cache)
1663 panic("cannot create skbuff cache");
1666 EXPORT_SYMBOL(___pskb_trim);
1667 EXPORT_SYMBOL(__kfree_skb);
1668 EXPORT_SYMBOL(__pskb_pull_tail);
1669 EXPORT_SYMBOL(alloc_skb);
1670 EXPORT_SYMBOL(pskb_copy);
1671 EXPORT_SYMBOL(pskb_expand_head);
1672 EXPORT_SYMBOL(skb_checksum);
1673 EXPORT_SYMBOL(skb_clone);
1674 EXPORT_SYMBOL(skb_clone_fraglist);
1675 EXPORT_SYMBOL(skb_copy);
1676 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1677 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1678 EXPORT_SYMBOL(skb_copy_bits);
1679 EXPORT_SYMBOL(skb_copy_expand);
1680 EXPORT_SYMBOL(skb_over_panic);
1681 EXPORT_SYMBOL(skb_pad);
1682 EXPORT_SYMBOL(skb_realloc_headroom);
1683 EXPORT_SYMBOL(skb_under_panic);
1684 EXPORT_SYMBOL(skb_dequeue);
1685 EXPORT_SYMBOL(skb_dequeue_tail);
1686 EXPORT_SYMBOL(skb_insert);
1687 EXPORT_SYMBOL(skb_queue_purge);
1688 EXPORT_SYMBOL(skb_queue_head);
1689 EXPORT_SYMBOL(skb_queue_tail);
1690 EXPORT_SYMBOL(skb_unlink);
1691 EXPORT_SYMBOL(skb_append);
1692 EXPORT_SYMBOL(skb_split);
1693 EXPORT_SYMBOL(skb_prepare_seq_read);
1694 EXPORT_SYMBOL(skb_seq_read);
1695 EXPORT_SYMBOL(skb_abort_seq_read);
1696 EXPORT_SYMBOL(skb_find_text);