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, int 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,
185 unsigned int size, int gfp_mask)
191 skb = kmem_cache_alloc(skbuff_head_cache,
192 gfp_mask & ~__GFP_DMA);
197 size = SKB_DATA_ALIGN(size);
198 data = kmem_cache_alloc(cp, gfp_mask);
202 memset(skb, 0, offsetof(struct sk_buff, truesize));
203 skb->truesize = size + sizeof(struct sk_buff);
204 atomic_set(&skb->users, 1);
208 skb->end = data + size;
210 atomic_set(&(skb_shinfo(skb)->dataref), 1);
211 skb_shinfo(skb)->nr_frags = 0;
212 skb_shinfo(skb)->tso_size = 0;
213 skb_shinfo(skb)->tso_segs = 0;
214 skb_shinfo(skb)->frag_list = NULL;
218 kmem_cache_free(skbuff_head_cache, skb);
224 static void skb_drop_fraglist(struct sk_buff *skb)
226 struct sk_buff *list = skb_shinfo(skb)->frag_list;
228 skb_shinfo(skb)->frag_list = NULL;
231 struct sk_buff *this = list;
237 static void skb_clone_fraglist(struct sk_buff *skb)
239 struct sk_buff *list;
241 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
245 void skb_release_data(struct sk_buff *skb)
248 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
249 &skb_shinfo(skb)->dataref)) {
250 if (skb_shinfo(skb)->nr_frags) {
252 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
253 put_page(skb_shinfo(skb)->frags[i].page);
256 if (skb_shinfo(skb)->frag_list)
257 skb_drop_fraglist(skb);
264 * Free an skbuff by memory without cleaning the state.
266 void kfree_skbmem(struct sk_buff *skb)
268 skb_release_data(skb);
269 kmem_cache_free(skbuff_head_cache, skb);
273 * __kfree_skb - private function
276 * Free an sk_buff. Release anything attached to the buffer.
277 * Clean the state. This is an internal helper function. Users should
278 * always call kfree_skb
281 void __kfree_skb(struct sk_buff *skb)
283 BUG_ON(skb->list != NULL);
285 dst_release(skb->dst);
287 secpath_put(skb->sp);
289 if (skb->destructor) {
291 skb->destructor(skb);
293 #ifdef CONFIG_NETFILTER
294 nf_conntrack_put(skb->nfct);
295 #ifdef CONFIG_BRIDGE_NETFILTER
296 nf_bridge_put(skb->nf_bridge);
299 /* XXX: IS this still necessary? - JHS */
300 #ifdef CONFIG_NET_SCHED
302 #ifdef CONFIG_NET_CLS_ACT
312 * skb_clone - duplicate an sk_buff
313 * @skb: buffer to clone
314 * @gfp_mask: allocation priority
316 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
317 * copies share the same packet data but not structure. The new
318 * buffer has a reference count of 1. If the allocation fails the
319 * function returns %NULL otherwise the new buffer is returned.
321 * If this function is called from an interrupt gfp_mask() must be
325 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
327 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
332 #define C(x) n->x = skb->x
334 n->next = n->prev = NULL;
347 secpath_get(skb->sp);
349 memcpy(n->cb, skb->cb, sizeof(skb->cb));
360 n->destructor = NULL;
361 #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(skb->tc_verd);
380 n->tc_verd = CLR_TC_MUNGED(skb->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->nfcache = old->nfcache;
427 new->nfct = old->nfct;
428 nf_conntrack_get(old->nfct);
429 new->nfctinfo = old->nfctinfo;
430 #ifdef CONFIG_BRIDGE_NETFILTER
431 new->nf_bridge = old->nf_bridge;
432 nf_bridge_get(old->nf_bridge);
435 #ifdef CONFIG_NET_SCHED
436 #ifdef CONFIG_NET_CLS_ACT
437 new->tc_verd = old->tc_verd;
439 new->tc_index = old->tc_index;
441 atomic_set(&new->users, 1);
442 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
443 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
447 * skb_copy - create private copy of an sk_buff
448 * @skb: buffer to copy
449 * @gfp_mask: allocation priority
451 * Make a copy of both an &sk_buff and its data. This is used when the
452 * caller wishes to modify the data and needs a private copy of the
453 * data to alter. Returns %NULL on failure or the pointer to the buffer
454 * on success. The returned buffer has a reference count of 1.
456 * As by-product this function converts non-linear &sk_buff to linear
457 * one, so that &sk_buff becomes completely private and caller is allowed
458 * to modify all the data of returned buffer. This means that this
459 * function is not recommended for use in circumstances when only
460 * header is going to be modified. Use pskb_copy() instead.
463 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
465 int headerlen = skb->data - skb->head;
467 * Allocate the copy buffer
469 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
474 /* Set the data pointer */
475 skb_reserve(n, headerlen);
476 /* Set the tail pointer and length */
477 skb_put(n, skb->len);
479 n->ip_summed = skb->ip_summed;
481 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
484 copy_skb_header(n, skb);
490 * pskb_copy - create copy of an sk_buff with private head.
491 * @skb: buffer to copy
492 * @gfp_mask: allocation priority
494 * Make a copy of both an &sk_buff and part of its data, located
495 * in header. Fragmented data remain shared. This is used when
496 * the caller wishes to modify only header of &sk_buff and needs
497 * private copy of the header to alter. Returns %NULL on failure
498 * or the pointer to the buffer on success.
499 * The returned buffer has a reference count of 1.
502 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
505 * Allocate the copy buffer
507 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
512 /* Set the data pointer */
513 skb_reserve(n, skb->data - skb->head);
514 /* Set the tail pointer and length */
515 skb_put(n, skb_headlen(skb));
517 memcpy(n->data, skb->data, n->len);
519 n->ip_summed = skb->ip_summed;
521 n->data_len = skb->data_len;
524 if (skb_shinfo(skb)->nr_frags) {
527 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
528 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
529 get_page(skb_shinfo(n)->frags[i].page);
531 skb_shinfo(n)->nr_frags = i;
534 if (skb_shinfo(skb)->frag_list) {
535 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
536 skb_clone_fraglist(n);
539 copy_skb_header(n, skb);
545 * pskb_expand_head - reallocate header of &sk_buff
546 * @skb: buffer to reallocate
547 * @nhead: room to add at head
548 * @ntail: room to add at tail
549 * @gfp_mask: allocation priority
551 * Expands (or creates identical copy, if &nhead and &ntail are zero)
552 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
553 * reference count of 1. Returns zero in the case of success or error,
554 * if expansion failed. In the last case, &sk_buff is not changed.
556 * All the pointers pointing into skb header may change and must be
557 * reloaded after call to this function.
560 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int 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, int gfp_mask)
653 * Allocate the copy buffer
655 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
657 int head_copy_len, head_copy_off;
662 skb_reserve(n, newheadroom);
664 /* Set the tail pointer and length */
665 skb_put(n, skb->len);
667 head_copy_len = skb_headroom(skb);
669 if (newheadroom <= head_copy_len)
670 head_copy_len = newheadroom;
672 head_copy_off = newheadroom - head_copy_len;
674 /* Copy the linear header and data. */
675 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
676 skb->len + head_copy_len))
679 copy_skb_header(n, skb);
685 * skb_pad - zero pad the tail of an skb
686 * @skb: buffer to pad
689 * Ensure that a buffer is followed by a padding area that is zero
690 * filled. Used by network drivers which may DMA or transfer data
691 * beyond the buffer end onto the wire.
693 * May return NULL in out of memory cases.
696 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
698 struct sk_buff *nskb;
700 /* If the skbuff is non linear tailroom is always zero.. */
701 if (skb_tailroom(skb) >= pad) {
702 memset(skb->data+skb->len, 0, pad);
706 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
709 memset(nskb->data+nskb->len, 0, pad);
713 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
714 * If realloc==0 and trimming is impossible without change of data,
718 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
720 int offset = skb_headlen(skb);
721 int nfrags = skb_shinfo(skb)->nr_frags;
724 for (i = 0; i < nfrags; i++) {
725 int end = offset + skb_shinfo(skb)->frags[i].size;
727 if (skb_cloned(skb)) {
730 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
734 put_page(skb_shinfo(skb)->frags[i].page);
735 skb_shinfo(skb)->nr_frags--;
737 skb_shinfo(skb)->frags[i].size = len - offset;
744 skb->data_len -= skb->len - len;
747 if (len <= skb_headlen(skb)) {
750 skb->tail = skb->data + len;
751 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
752 skb_drop_fraglist(skb);
754 skb->data_len -= skb->len - len;
763 * __pskb_pull_tail - advance tail of skb header
764 * @skb: buffer to reallocate
765 * @delta: number of bytes to advance tail
767 * The function makes a sense only on a fragmented &sk_buff,
768 * it expands header moving its tail forward and copying necessary
769 * data from fragmented part.
771 * &sk_buff MUST have reference count of 1.
773 * Returns %NULL (and &sk_buff does not change) if pull failed
774 * or value of new tail of skb in the case of success.
776 * All the pointers pointing into skb header may change and must be
777 * reloaded after call to this function.
780 /* Moves tail of skb head forward, copying data from fragmented part,
781 * when it is necessary.
782 * 1. It may fail due to malloc failure.
783 * 2. It may change skb pointers.
785 * It is pretty complicated. Luckily, it is called only in exceptional cases.
787 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
789 /* If skb has not enough free space at tail, get new one
790 * plus 128 bytes for future expansions. If we have enough
791 * room at tail, reallocate without expansion only if skb is cloned.
793 int i, k, eat = (skb->tail + delta) - skb->end;
795 if (eat > 0 || skb_cloned(skb)) {
796 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
801 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
804 /* Optimization: no fragments, no reasons to preestimate
805 * size of pulled pages. Superb.
807 if (!skb_shinfo(skb)->frag_list)
810 /* Estimate size of pulled pages. */
812 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
813 if (skb_shinfo(skb)->frags[i].size >= eat)
815 eat -= skb_shinfo(skb)->frags[i].size;
818 /* If we need update frag list, we are in troubles.
819 * Certainly, it possible to add an offset to skb data,
820 * but taking into account that pulling is expected to
821 * be very rare operation, it is worth to fight against
822 * further bloating skb head and crucify ourselves here instead.
823 * Pure masohism, indeed. 8)8)
826 struct sk_buff *list = skb_shinfo(skb)->frag_list;
827 struct sk_buff *clone = NULL;
828 struct sk_buff *insp = NULL;
834 if (list->len <= eat) {
835 /* Eaten as whole. */
840 /* Eaten partially. */
842 if (skb_shared(list)) {
843 /* Sucks! We need to fork list. :-( */
844 clone = skb_clone(list, GFP_ATOMIC);
850 /* This may be pulled without
854 if (!pskb_pull(list, eat)) {
863 /* Free pulled out fragments. */
864 while ((list = skb_shinfo(skb)->frag_list) != insp) {
865 skb_shinfo(skb)->frag_list = list->next;
868 /* And insert new clone at head. */
871 skb_shinfo(skb)->frag_list = clone;
874 /* Success! Now we may commit changes to skb data. */
879 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
880 if (skb_shinfo(skb)->frags[i].size <= eat) {
881 put_page(skb_shinfo(skb)->frags[i].page);
882 eat -= skb_shinfo(skb)->frags[i].size;
884 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
886 skb_shinfo(skb)->frags[k].page_offset += eat;
887 skb_shinfo(skb)->frags[k].size -= eat;
893 skb_shinfo(skb)->nr_frags = k;
896 skb->data_len -= delta;
901 /* Copy some data bits from skb to kernel buffer. */
903 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
906 int start = skb_headlen(skb);
908 if (offset > (int)skb->len - len)
912 if ((copy = start - offset) > 0) {
915 memcpy(to, skb->data + offset, copy);
916 if ((len -= copy) == 0)
922 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
925 BUG_TRAP(start <= offset + len);
927 end = start + skb_shinfo(skb)->frags[i].size;
928 if ((copy = end - offset) > 0) {
934 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
936 vaddr + skb_shinfo(skb)->frags[i].page_offset+
937 offset - start, copy);
938 kunmap_skb_frag(vaddr);
940 if ((len -= copy) == 0)
948 if (skb_shinfo(skb)->frag_list) {
949 struct sk_buff *list = skb_shinfo(skb)->frag_list;
951 for (; list; list = list->next) {
954 BUG_TRAP(start <= offset + len);
956 end = start + list->len;
957 if ((copy = end - offset) > 0) {
960 if (skb_copy_bits(list, offset - start,
963 if ((len -= copy) == 0)
979 * skb_store_bits - store bits from kernel buffer to skb
980 * @skb: destination buffer
981 * @offset: offset in destination
982 * @from: source buffer
983 * @len: number of bytes to copy
985 * Copy the specified number of bytes from the source buffer to the
986 * destination skb. This function handles all the messy bits of
987 * traversing fragment lists and such.
990 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
993 int start = skb_headlen(skb);
995 if (offset > (int)skb->len - len)
998 if ((copy = start - offset) > 0) {
1001 memcpy(skb->data + offset, from, copy);
1002 if ((len -= copy) == 0)
1008 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1009 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1012 BUG_TRAP(start <= offset + len);
1014 end = start + frag->size;
1015 if ((copy = end - offset) > 0) {
1021 vaddr = kmap_skb_frag(frag);
1022 memcpy(vaddr + frag->page_offset + offset - start,
1024 kunmap_skb_frag(vaddr);
1026 if ((len -= copy) == 0)
1034 if (skb_shinfo(skb)->frag_list) {
1035 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1037 for (; list; list = list->next) {
1040 BUG_TRAP(start <= offset + len);
1042 end = start + list->len;
1043 if ((copy = end - offset) > 0) {
1046 if (skb_store_bits(list, offset - start,
1049 if ((len -= copy) == 0)
1064 EXPORT_SYMBOL(skb_store_bits);
1066 /* Checksum skb data. */
1068 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1069 int len, unsigned int csum)
1071 int start = skb_headlen(skb);
1072 int i, copy = start - offset;
1075 /* Checksum header. */
1079 csum = csum_partial(skb->data + offset, copy, csum);
1080 if ((len -= copy) == 0)
1086 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1089 BUG_TRAP(start <= offset + len);
1091 end = start + skb_shinfo(skb)->frags[i].size;
1092 if ((copy = end - offset) > 0) {
1095 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1099 vaddr = kmap_skb_frag(frag);
1100 csum2 = csum_partial(vaddr + frag->page_offset +
1101 offset - start, copy, 0);
1102 kunmap_skb_frag(vaddr);
1103 csum = csum_block_add(csum, csum2, pos);
1112 if (skb_shinfo(skb)->frag_list) {
1113 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1115 for (; list; list = list->next) {
1118 BUG_TRAP(start <= offset + len);
1120 end = start + list->len;
1121 if ((copy = end - offset) > 0) {
1125 csum2 = skb_checksum(list, offset - start,
1127 csum = csum_block_add(csum, csum2, pos);
1128 if ((len -= copy) == 0)
1142 /* Both of above in one bottle. */
1144 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1145 u8 *to, int len, unsigned int csum)
1147 int start = skb_headlen(skb);
1148 int i, copy = start - offset;
1155 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1157 if ((len -= copy) == 0)
1164 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 BUG_TRAP(start <= offset + len);
1169 end = start + skb_shinfo(skb)->frags[i].size;
1170 if ((copy = end - offset) > 0) {
1173 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1177 vaddr = kmap_skb_frag(frag);
1178 csum2 = csum_partial_copy_nocheck(vaddr +
1182 kunmap_skb_frag(vaddr);
1183 csum = csum_block_add(csum, csum2, pos);
1193 if (skb_shinfo(skb)->frag_list) {
1194 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1196 for (; list; list = list->next) {
1200 BUG_TRAP(start <= offset + len);
1202 end = start + list->len;
1203 if ((copy = end - offset) > 0) {
1206 csum2 = skb_copy_and_csum_bits(list,
1209 csum = csum_block_add(csum, csum2, pos);
1210 if ((len -= copy) == 0)
1224 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1229 if (skb->ip_summed == CHECKSUM_HW)
1230 csstart = skb->h.raw - skb->data;
1232 csstart = skb_headlen(skb);
1234 if (csstart > skb_headlen(skb))
1237 memcpy(to, skb->data, csstart);
1240 if (csstart != skb->len)
1241 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1242 skb->len - csstart, 0);
1244 if (skb->ip_summed == CHECKSUM_HW) {
1245 long csstuff = csstart + skb->csum;
1247 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1252 * skb_dequeue - remove from the head of the queue
1253 * @list: list to dequeue from
1255 * Remove the head of the list. The list lock is taken so the function
1256 * may be used safely with other locking list functions. The head item is
1257 * returned or %NULL if the list is empty.
1260 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1262 unsigned long flags;
1263 struct sk_buff *result;
1265 spin_lock_irqsave(&list->lock, flags);
1266 result = __skb_dequeue(list);
1267 spin_unlock_irqrestore(&list->lock, flags);
1272 * skb_dequeue_tail - remove from the tail of the queue
1273 * @list: list to dequeue from
1275 * Remove the tail of the list. The list lock is taken so the function
1276 * may be used safely with other locking list functions. The tail item is
1277 * returned or %NULL if the list is empty.
1279 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1281 unsigned long flags;
1282 struct sk_buff *result;
1284 spin_lock_irqsave(&list->lock, flags);
1285 result = __skb_dequeue_tail(list);
1286 spin_unlock_irqrestore(&list->lock, flags);
1291 * skb_queue_purge - empty a list
1292 * @list: list to empty
1294 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1295 * the list and one reference dropped. This function takes the list
1296 * lock and is atomic with respect to other list locking functions.
1298 void skb_queue_purge(struct sk_buff_head *list)
1300 struct sk_buff *skb;
1301 while ((skb = skb_dequeue(list)) != NULL)
1306 * skb_queue_head - queue a buffer at the list head
1307 * @list: list to use
1308 * @newsk: buffer to queue
1310 * Queue a buffer at the start of the list. This function takes the
1311 * list lock and can be used safely with other locking &sk_buff functions
1314 * A buffer cannot be placed on two lists at the same time.
1316 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1318 unsigned long flags;
1320 spin_lock_irqsave(&list->lock, flags);
1321 __skb_queue_head(list, newsk);
1322 spin_unlock_irqrestore(&list->lock, flags);
1326 * skb_queue_tail - queue a buffer at the list tail
1327 * @list: list to use
1328 * @newsk: buffer to queue
1330 * Queue a buffer at the tail of the list. This function takes the
1331 * list lock and can be used safely with other locking &sk_buff functions
1334 * A buffer cannot be placed on two lists at the same time.
1336 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1338 unsigned long flags;
1340 spin_lock_irqsave(&list->lock, flags);
1341 __skb_queue_tail(list, newsk);
1342 spin_unlock_irqrestore(&list->lock, flags);
1345 * skb_unlink - remove a buffer from a list
1346 * @skb: buffer to remove
1348 * Place a packet after a given packet in a list. The list locks are taken
1349 * and this function is atomic with respect to other list locked calls
1351 * Works even without knowing the list it is sitting on, which can be
1352 * handy at times. It also means that THE LIST MUST EXIST when you
1353 * unlink. Thus a list must have its contents unlinked before it is
1356 void skb_unlink(struct sk_buff *skb)
1358 struct sk_buff_head *list = skb->list;
1361 unsigned long flags;
1363 spin_lock_irqsave(&list->lock, flags);
1364 if (skb->list == list)
1365 __skb_unlink(skb, skb->list);
1366 spin_unlock_irqrestore(&list->lock, flags);
1372 * skb_append - append a buffer
1373 * @old: buffer to insert after
1374 * @newsk: buffer to insert
1376 * Place a packet after a given packet in a list. The list locks are taken
1377 * and this function is atomic with respect to other list locked calls.
1378 * A buffer cannot be placed on two lists at the same time.
1381 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1383 unsigned long flags;
1385 spin_lock_irqsave(&old->list->lock, flags);
1386 __skb_append(old, newsk);
1387 spin_unlock_irqrestore(&old->list->lock, flags);
1392 * skb_insert - insert a buffer
1393 * @old: buffer to insert before
1394 * @newsk: buffer to insert
1396 * Place a packet before a given packet in a list. The list locks are taken
1397 * and this function is atomic with respect to other list locked calls
1398 * A buffer cannot be placed on two lists at the same time.
1401 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1403 unsigned long flags;
1405 spin_lock_irqsave(&old->list->lock, flags);
1406 __skb_insert(newsk, old->prev, old, old->list);
1407 spin_unlock_irqrestore(&old->list->lock, flags);
1412 * Tune the memory allocator for a new MTU size.
1414 void skb_add_mtu(int mtu)
1416 /* Must match allocation in alloc_skb */
1417 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1419 kmem_add_cache_size(mtu);
1423 static inline void skb_split_inside_header(struct sk_buff *skb,
1424 struct sk_buff* skb1,
1425 const u32 len, const int pos)
1429 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1431 /* And move data appendix as is. */
1432 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1433 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1435 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1436 skb_shinfo(skb)->nr_frags = 0;
1437 skb1->data_len = skb->data_len;
1438 skb1->len += skb1->data_len;
1441 skb->tail = skb->data + len;
1444 static inline void skb_split_no_header(struct sk_buff *skb,
1445 struct sk_buff* skb1,
1446 const u32 len, int pos)
1449 const int nfrags = skb_shinfo(skb)->nr_frags;
1451 skb_shinfo(skb)->nr_frags = 0;
1452 skb1->len = skb1->data_len = skb->len - len;
1454 skb->data_len = len - pos;
1456 for (i = 0; i < nfrags; i++) {
1457 int size = skb_shinfo(skb)->frags[i].size;
1459 if (pos + size > len) {
1460 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1464 * We have two variants in this case:
1465 * 1. Move all the frag to the second
1466 * part, if it is possible. F.e.
1467 * this approach is mandatory for TUX,
1468 * where splitting is expensive.
1469 * 2. Split is accurately. We make this.
1471 get_page(skb_shinfo(skb)->frags[i].page);
1472 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1473 skb_shinfo(skb1)->frags[0].size -= len - pos;
1474 skb_shinfo(skb)->frags[i].size = len - pos;
1475 skb_shinfo(skb)->nr_frags++;
1479 skb_shinfo(skb)->nr_frags++;
1482 skb_shinfo(skb1)->nr_frags = k;
1486 * skb_split - Split fragmented skb to two parts at length len.
1487 * @skb: the buffer to split
1488 * @skb1: the buffer to receive the second part
1489 * @len: new length for skb
1491 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1493 int pos = skb_headlen(skb);
1495 if (len < pos) /* Split line is inside header. */
1496 skb_split_inside_header(skb, skb1, len, pos);
1497 else /* Second chunk has no header, nothing to copy. */
1498 skb_split_no_header(skb, skb1, len, pos);
1502 * skb_prepare_seq_read - Prepare a sequential read of skb data
1503 * @skb: the buffer to read
1504 * @from: lower offset of data to be read
1505 * @to: upper offset of data to be read
1506 * @st: state variable
1508 * Initializes the specified state variable. Must be called before
1509 * invoking skb_seq_read() for the first time.
1511 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1512 unsigned int to, struct skb_seq_state *st)
1514 st->lower_offset = from;
1515 st->upper_offset = to;
1516 st->root_skb = st->cur_skb = skb;
1517 st->frag_idx = st->stepped_offset = 0;
1518 st->frag_data = NULL;
1522 * skb_seq_read - Sequentially read skb data
1523 * @consumed: number of bytes consumed by the caller so far
1524 * @data: destination pointer for data to be returned
1525 * @st: state variable
1527 * Reads a block of skb data at &consumed relative to the
1528 * lower offset specified to skb_prepare_seq_read(). Assigns
1529 * the head of the data block to &data and returns the length
1530 * of the block or 0 if the end of the skb data or the upper
1531 * offset has been reached.
1533 * The caller is not required to consume all of the data
1534 * returned, i.e. &consumed is typically set to the number
1535 * of bytes already consumed and the next call to
1536 * skb_seq_read() will return the remaining part of the block.
1538 * Note: The size of each block of data returned can be arbitary,
1539 * this limitation is the cost for zerocopy seqeuental
1540 * reads of potentially non linear data.
1542 * Note: Fragment lists within fragments are not implemented
1543 * at the moment, state->root_skb could be replaced with
1544 * a stack for this purpose.
1546 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1547 struct skb_seq_state *st)
1549 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1552 if (unlikely(abs_offset >= st->upper_offset))
1556 block_limit = skb_headlen(st->cur_skb);
1558 if (abs_offset < block_limit) {
1559 *data = st->cur_skb->data + abs_offset;
1560 return block_limit - abs_offset;
1563 if (st->frag_idx == 0 && !st->frag_data)
1564 st->stepped_offset += skb_headlen(st->cur_skb);
1566 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1567 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1568 block_limit = frag->size + st->stepped_offset;
1570 if (abs_offset < block_limit) {
1572 st->frag_data = kmap_skb_frag(frag);
1574 *data = (u8 *) st->frag_data + frag->page_offset +
1575 (abs_offset - st->stepped_offset);
1577 return block_limit - abs_offset;
1580 if (st->frag_data) {
1581 kunmap_skb_frag(st->frag_data);
1582 st->frag_data = NULL;
1586 st->stepped_offset += frag->size;
1589 if (st->cur_skb->next) {
1590 st->cur_skb = st->cur_skb->next;
1593 } else if (st->root_skb == st->cur_skb &&
1594 skb_shinfo(st->root_skb)->frag_list) {
1595 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1603 * skb_abort_seq_read - Abort a sequential read of skb data
1604 * @st: state variable
1606 * Must be called if skb_seq_read() was not called until it
1609 void skb_abort_seq_read(struct skb_seq_state *st)
1612 kunmap_skb_frag(st->frag_data);
1615 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1617 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1618 struct ts_config *conf,
1619 struct ts_state *state)
1621 return skb_seq_read(offset, text, TS_SKB_CB(state));
1624 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1626 skb_abort_seq_read(TS_SKB_CB(state));
1630 * skb_find_text - Find a text pattern in skb data
1631 * @skb: the buffer to look in
1632 * @from: search offset
1634 * @config: textsearch configuration
1635 * @state: uninitialized textsearch state variable
1637 * Finds a pattern in the skb data according to the specified
1638 * textsearch configuration. Use textsearch_next() to retrieve
1639 * subsequent occurrences of the pattern. Returns the offset
1640 * to the first occurrence or UINT_MAX if no match was found.
1642 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1643 unsigned int to, struct ts_config *config,
1644 struct ts_state *state)
1646 config->get_next_block = skb_ts_get_next_block;
1647 config->finish = skb_ts_finish;
1649 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1651 return textsearch_find(config, state);
1654 void __init skb_init(void)
1656 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1657 sizeof(struct sk_buff),
1661 if (!skbuff_head_cache)
1662 panic("cannot create skbuff cache");
1665 EXPORT_SYMBOL(___pskb_trim);
1666 EXPORT_SYMBOL(__kfree_skb);
1667 EXPORT_SYMBOL(__pskb_pull_tail);
1668 EXPORT_SYMBOL(alloc_skb);
1669 EXPORT_SYMBOL(pskb_copy);
1670 EXPORT_SYMBOL(pskb_expand_head);
1671 EXPORT_SYMBOL(skb_checksum);
1672 EXPORT_SYMBOL(skb_clone);
1673 EXPORT_SYMBOL(skb_clone_fraglist);
1674 EXPORT_SYMBOL(skb_copy);
1675 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1676 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1677 EXPORT_SYMBOL(skb_copy_bits);
1678 EXPORT_SYMBOL(skb_copy_expand);
1679 EXPORT_SYMBOL(skb_over_panic);
1680 EXPORT_SYMBOL(skb_pad);
1681 EXPORT_SYMBOL(skb_realloc_headroom);
1682 EXPORT_SYMBOL(skb_under_panic);
1683 EXPORT_SYMBOL(skb_dequeue);
1684 EXPORT_SYMBOL(skb_dequeue_tail);
1685 EXPORT_SYMBOL(skb_insert);
1686 EXPORT_SYMBOL(skb_queue_purge);
1687 EXPORT_SYMBOL(skb_queue_head);
1688 EXPORT_SYMBOL(skb_queue_tail);
1689 EXPORT_SYMBOL(skb_unlink);
1690 EXPORT_SYMBOL(skb_append);
1691 EXPORT_SYMBOL(skb_split);
1692 EXPORT_SYMBOL(skb_prepare_seq_read);
1693 EXPORT_SYMBOL(skb_seq_read);
1694 EXPORT_SYMBOL(skb_abort_seq_read);
1695 EXPORT_SYMBOL(skb_find_text);