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 dst_release(skb->dst);
286 secpath_put(skb->sp);
288 if (skb->destructor) {
290 skb->destructor(skb);
292 #ifdef CONFIG_NETFILTER
293 nf_conntrack_put(skb->nfct);
294 #ifdef CONFIG_BRIDGE_NETFILTER
295 nf_bridge_put(skb->nf_bridge);
298 /* XXX: IS this still necessary? - JHS */
299 #ifdef CONFIG_NET_SCHED
301 #ifdef CONFIG_NET_CLS_ACT
310 * skb_clone - duplicate an sk_buff
311 * @skb: buffer to clone
312 * @gfp_mask: allocation priority
314 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
315 * copies share the same packet data but not structure. The new
316 * buffer has a reference count of 1. If the allocation fails the
317 * function returns %NULL otherwise the new buffer is returned.
319 * If this function is called from an interrupt gfp_mask() must be
323 struct sk_buff *skb_clone(struct sk_buff *skb, unsigned int __nocast gfp_mask)
325 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
330 #define C(x) n->x = skb->x
332 n->next = n->prev = NULL;
344 secpath_get(skb->sp);
346 memcpy(n->cb, skb->cb, sizeof(skb->cb));
357 n->destructor = NULL;
358 #ifdef CONFIG_NETFILTER
361 nf_conntrack_get(skb->nfct);
363 #ifdef CONFIG_BRIDGE_NETFILTER
365 nf_bridge_get(skb->nf_bridge);
367 #endif /*CONFIG_NETFILTER*/
368 #if defined(CONFIG_HIPPI)
371 #ifdef CONFIG_NET_SCHED
373 #ifdef CONFIG_NET_CLS_ACT
374 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
375 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
376 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
382 atomic_set(&n->users, 1);
388 atomic_inc(&(skb_shinfo(skb)->dataref));
394 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
397 * Shift between the two data areas in bytes
399 unsigned long offset = new->data - old->data;
403 new->real_dev = old->real_dev;
404 new->priority = old->priority;
405 new->protocol = old->protocol;
406 new->dst = dst_clone(old->dst);
408 new->sp = secpath_get(old->sp);
410 new->h.raw = old->h.raw + offset;
411 new->nh.raw = old->nh.raw + offset;
412 new->mac.raw = old->mac.raw + offset;
413 memcpy(new->cb, old->cb, sizeof(old->cb));
414 new->local_df = old->local_df;
415 new->pkt_type = old->pkt_type;
416 new->stamp = old->stamp;
417 new->destructor = NULL;
418 #ifdef CONFIG_NETFILTER
419 new->nfmark = old->nfmark;
420 new->nfct = old->nfct;
421 nf_conntrack_get(old->nfct);
422 new->nfctinfo = old->nfctinfo;
423 #ifdef CONFIG_BRIDGE_NETFILTER
424 new->nf_bridge = old->nf_bridge;
425 nf_bridge_get(old->nf_bridge);
428 #ifdef CONFIG_NET_SCHED
429 #ifdef CONFIG_NET_CLS_ACT
430 new->tc_verd = old->tc_verd;
432 new->tc_index = old->tc_index;
434 atomic_set(&new->users, 1);
435 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
436 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
440 * skb_copy - create private copy of an sk_buff
441 * @skb: buffer to copy
442 * @gfp_mask: allocation priority
444 * Make a copy of both an &sk_buff and its data. This is used when the
445 * caller wishes to modify the data and needs a private copy of the
446 * data to alter. Returns %NULL on failure or the pointer to the buffer
447 * on success. The returned buffer has a reference count of 1.
449 * As by-product this function converts non-linear &sk_buff to linear
450 * one, so that &sk_buff becomes completely private and caller is allowed
451 * to modify all the data of returned buffer. This means that this
452 * function is not recommended for use in circumstances when only
453 * header is going to be modified. Use pskb_copy() instead.
456 struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask)
458 int headerlen = skb->data - skb->head;
460 * Allocate the copy buffer
462 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
467 /* Set the data pointer */
468 skb_reserve(n, headerlen);
469 /* Set the tail pointer and length */
470 skb_put(n, skb->len);
472 n->ip_summed = skb->ip_summed;
474 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
477 copy_skb_header(n, skb);
483 * pskb_copy - create copy of an sk_buff with private head.
484 * @skb: buffer to copy
485 * @gfp_mask: allocation priority
487 * Make a copy of both an &sk_buff and part of its data, located
488 * in header. Fragmented data remain shared. This is used when
489 * the caller wishes to modify only header of &sk_buff and needs
490 * private copy of the header to alter. Returns %NULL on failure
491 * or the pointer to the buffer on success.
492 * The returned buffer has a reference count of 1.
495 struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask)
498 * Allocate the copy buffer
500 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
505 /* Set the data pointer */
506 skb_reserve(n, skb->data - skb->head);
507 /* Set the tail pointer and length */
508 skb_put(n, skb_headlen(skb));
510 memcpy(n->data, skb->data, n->len);
512 n->ip_summed = skb->ip_summed;
514 n->data_len = skb->data_len;
517 if (skb_shinfo(skb)->nr_frags) {
520 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
521 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
522 get_page(skb_shinfo(n)->frags[i].page);
524 skb_shinfo(n)->nr_frags = i;
527 if (skb_shinfo(skb)->frag_list) {
528 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
529 skb_clone_fraglist(n);
532 copy_skb_header(n, skb);
538 * pskb_expand_head - reallocate header of &sk_buff
539 * @skb: buffer to reallocate
540 * @nhead: room to add at head
541 * @ntail: room to add at tail
542 * @gfp_mask: allocation priority
544 * Expands (or creates identical copy, if &nhead and &ntail are zero)
545 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
546 * reference count of 1. Returns zero in the case of success or error,
547 * if expansion failed. In the last case, &sk_buff is not changed.
549 * All the pointers pointing into skb header may change and must be
550 * reloaded after call to this function.
553 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
554 unsigned int __nocast gfp_mask)
558 int size = nhead + (skb->end - skb->head) + ntail;
564 size = SKB_DATA_ALIGN(size);
566 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
570 /* Copy only real data... and, alas, header. This should be
571 * optimized for the cases when header is void. */
572 memcpy(data + nhead, skb->head, skb->tail - skb->head);
573 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
575 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
576 get_page(skb_shinfo(skb)->frags[i].page);
578 if (skb_shinfo(skb)->frag_list)
579 skb_clone_fraglist(skb);
581 skb_release_data(skb);
583 off = (data + nhead) - skb->head;
586 skb->end = data + size;
594 atomic_set(&skb_shinfo(skb)->dataref, 1);
601 /* Make private copy of skb with writable head and some headroom */
603 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
605 struct sk_buff *skb2;
606 int delta = headroom - skb_headroom(skb);
609 skb2 = pskb_copy(skb, GFP_ATOMIC);
611 skb2 = skb_clone(skb, GFP_ATOMIC);
612 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
623 * skb_copy_expand - copy and expand sk_buff
624 * @skb: buffer to copy
625 * @newheadroom: new free bytes at head
626 * @newtailroom: new free bytes at tail
627 * @gfp_mask: allocation priority
629 * Make a copy of both an &sk_buff and its data and while doing so
630 * allocate additional space.
632 * This is used when the caller wishes to modify the data and needs a
633 * private copy of the data to alter as well as more space for new fields.
634 * Returns %NULL on failure or the pointer to the buffer
635 * on success. The returned buffer has a reference count of 1.
637 * You must pass %GFP_ATOMIC as the allocation priority if this function
638 * is called from an interrupt.
640 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
641 * only by netfilter in the cases when checksum is recalculated? --ANK
643 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
644 int newheadroom, int newtailroom,
645 unsigned int __nocast gfp_mask)
648 * Allocate the copy buffer
650 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
652 int head_copy_len, head_copy_off;
657 skb_reserve(n, newheadroom);
659 /* Set the tail pointer and length */
660 skb_put(n, skb->len);
662 head_copy_len = skb_headroom(skb);
664 if (newheadroom <= head_copy_len)
665 head_copy_len = newheadroom;
667 head_copy_off = newheadroom - head_copy_len;
669 /* Copy the linear header and data. */
670 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
671 skb->len + head_copy_len))
674 copy_skb_header(n, skb);
680 * skb_pad - zero pad the tail of an skb
681 * @skb: buffer to pad
684 * Ensure that a buffer is followed by a padding area that is zero
685 * filled. Used by network drivers which may DMA or transfer data
686 * beyond the buffer end onto the wire.
688 * May return NULL in out of memory cases.
691 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
693 struct sk_buff *nskb;
695 /* If the skbuff is non linear tailroom is always zero.. */
696 if (skb_tailroom(skb) >= pad) {
697 memset(skb->data+skb->len, 0, pad);
701 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
704 memset(nskb->data+nskb->len, 0, pad);
708 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
709 * If realloc==0 and trimming is impossible without change of data,
713 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
715 int offset = skb_headlen(skb);
716 int nfrags = skb_shinfo(skb)->nr_frags;
719 for (i = 0; i < nfrags; i++) {
720 int end = offset + skb_shinfo(skb)->frags[i].size;
722 if (skb_cloned(skb)) {
725 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
729 put_page(skb_shinfo(skb)->frags[i].page);
730 skb_shinfo(skb)->nr_frags--;
732 skb_shinfo(skb)->frags[i].size = len - offset;
739 skb->data_len -= skb->len - len;
742 if (len <= skb_headlen(skb)) {
745 skb->tail = skb->data + len;
746 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
747 skb_drop_fraglist(skb);
749 skb->data_len -= skb->len - len;
758 * __pskb_pull_tail - advance tail of skb header
759 * @skb: buffer to reallocate
760 * @delta: number of bytes to advance tail
762 * The function makes a sense only on a fragmented &sk_buff,
763 * it expands header moving its tail forward and copying necessary
764 * data from fragmented part.
766 * &sk_buff MUST have reference count of 1.
768 * Returns %NULL (and &sk_buff does not change) if pull failed
769 * or value of new tail of skb in the case of success.
771 * All the pointers pointing into skb header may change and must be
772 * reloaded after call to this function.
775 /* Moves tail of skb head forward, copying data from fragmented part,
776 * when it is necessary.
777 * 1. It may fail due to malloc failure.
778 * 2. It may change skb pointers.
780 * It is pretty complicated. Luckily, it is called only in exceptional cases.
782 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
784 /* If skb has not enough free space at tail, get new one
785 * plus 128 bytes for future expansions. If we have enough
786 * room at tail, reallocate without expansion only if skb is cloned.
788 int i, k, eat = (skb->tail + delta) - skb->end;
790 if (eat > 0 || skb_cloned(skb)) {
791 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
796 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
799 /* Optimization: no fragments, no reasons to preestimate
800 * size of pulled pages. Superb.
802 if (!skb_shinfo(skb)->frag_list)
805 /* Estimate size of pulled pages. */
807 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
808 if (skb_shinfo(skb)->frags[i].size >= eat)
810 eat -= skb_shinfo(skb)->frags[i].size;
813 /* If we need update frag list, we are in troubles.
814 * Certainly, it possible to add an offset to skb data,
815 * but taking into account that pulling is expected to
816 * be very rare operation, it is worth to fight against
817 * further bloating skb head and crucify ourselves here instead.
818 * Pure masohism, indeed. 8)8)
821 struct sk_buff *list = skb_shinfo(skb)->frag_list;
822 struct sk_buff *clone = NULL;
823 struct sk_buff *insp = NULL;
829 if (list->len <= eat) {
830 /* Eaten as whole. */
835 /* Eaten partially. */
837 if (skb_shared(list)) {
838 /* Sucks! We need to fork list. :-( */
839 clone = skb_clone(list, GFP_ATOMIC);
845 /* This may be pulled without
849 if (!pskb_pull(list, eat)) {
858 /* Free pulled out fragments. */
859 while ((list = skb_shinfo(skb)->frag_list) != insp) {
860 skb_shinfo(skb)->frag_list = list->next;
863 /* And insert new clone at head. */
866 skb_shinfo(skb)->frag_list = clone;
869 /* Success! Now we may commit changes to skb data. */
874 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
875 if (skb_shinfo(skb)->frags[i].size <= eat) {
876 put_page(skb_shinfo(skb)->frags[i].page);
877 eat -= skb_shinfo(skb)->frags[i].size;
879 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
881 skb_shinfo(skb)->frags[k].page_offset += eat;
882 skb_shinfo(skb)->frags[k].size -= eat;
888 skb_shinfo(skb)->nr_frags = k;
891 skb->data_len -= delta;
896 /* Copy some data bits from skb to kernel buffer. */
898 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
901 int start = skb_headlen(skb);
903 if (offset > (int)skb->len - len)
907 if ((copy = start - offset) > 0) {
910 memcpy(to, skb->data + offset, copy);
911 if ((len -= copy) == 0)
917 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
920 BUG_TRAP(start <= offset + len);
922 end = start + skb_shinfo(skb)->frags[i].size;
923 if ((copy = end - offset) > 0) {
929 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
931 vaddr + skb_shinfo(skb)->frags[i].page_offset+
932 offset - start, copy);
933 kunmap_skb_frag(vaddr);
935 if ((len -= copy) == 0)
943 if (skb_shinfo(skb)->frag_list) {
944 struct sk_buff *list = skb_shinfo(skb)->frag_list;
946 for (; list; list = list->next) {
949 BUG_TRAP(start <= offset + len);
951 end = start + list->len;
952 if ((copy = end - offset) > 0) {
955 if (skb_copy_bits(list, offset - start,
958 if ((len -= copy) == 0)
974 * skb_store_bits - store bits from kernel buffer to skb
975 * @skb: destination buffer
976 * @offset: offset in destination
977 * @from: source buffer
978 * @len: number of bytes to copy
980 * Copy the specified number of bytes from the source buffer to the
981 * destination skb. This function handles all the messy bits of
982 * traversing fragment lists and such.
985 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
988 int start = skb_headlen(skb);
990 if (offset > (int)skb->len - len)
993 if ((copy = start - offset) > 0) {
996 memcpy(skb->data + offset, from, copy);
997 if ((len -= copy) == 0)
1003 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1004 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1007 BUG_TRAP(start <= offset + len);
1009 end = start + frag->size;
1010 if ((copy = end - offset) > 0) {
1016 vaddr = kmap_skb_frag(frag);
1017 memcpy(vaddr + frag->page_offset + offset - start,
1019 kunmap_skb_frag(vaddr);
1021 if ((len -= copy) == 0)
1029 if (skb_shinfo(skb)->frag_list) {
1030 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1032 for (; list; list = list->next) {
1035 BUG_TRAP(start <= offset + len);
1037 end = start + list->len;
1038 if ((copy = end - offset) > 0) {
1041 if (skb_store_bits(list, offset - start,
1044 if ((len -= copy) == 0)
1059 EXPORT_SYMBOL(skb_store_bits);
1061 /* Checksum skb data. */
1063 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1064 int len, unsigned int csum)
1066 int start = skb_headlen(skb);
1067 int i, copy = start - offset;
1070 /* Checksum header. */
1074 csum = csum_partial(skb->data + offset, copy, csum);
1075 if ((len -= copy) == 0)
1081 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1084 BUG_TRAP(start <= offset + len);
1086 end = start + skb_shinfo(skb)->frags[i].size;
1087 if ((copy = end - offset) > 0) {
1090 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1094 vaddr = kmap_skb_frag(frag);
1095 csum2 = csum_partial(vaddr + frag->page_offset +
1096 offset - start, copy, 0);
1097 kunmap_skb_frag(vaddr);
1098 csum = csum_block_add(csum, csum2, pos);
1107 if (skb_shinfo(skb)->frag_list) {
1108 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1110 for (; list; list = list->next) {
1113 BUG_TRAP(start <= offset + len);
1115 end = start + list->len;
1116 if ((copy = end - offset) > 0) {
1120 csum2 = skb_checksum(list, offset - start,
1122 csum = csum_block_add(csum, csum2, pos);
1123 if ((len -= copy) == 0)
1137 /* Both of above in one bottle. */
1139 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1140 u8 *to, int len, unsigned int csum)
1142 int start = skb_headlen(skb);
1143 int i, copy = start - offset;
1150 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1152 if ((len -= copy) == 0)
1159 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1162 BUG_TRAP(start <= offset + len);
1164 end = start + skb_shinfo(skb)->frags[i].size;
1165 if ((copy = end - offset) > 0) {
1168 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1172 vaddr = kmap_skb_frag(frag);
1173 csum2 = csum_partial_copy_nocheck(vaddr +
1177 kunmap_skb_frag(vaddr);
1178 csum = csum_block_add(csum, csum2, pos);
1188 if (skb_shinfo(skb)->frag_list) {
1189 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1191 for (; list; list = list->next) {
1195 BUG_TRAP(start <= offset + len);
1197 end = start + list->len;
1198 if ((copy = end - offset) > 0) {
1201 csum2 = skb_copy_and_csum_bits(list,
1204 csum = csum_block_add(csum, csum2, pos);
1205 if ((len -= copy) == 0)
1219 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1224 if (skb->ip_summed == CHECKSUM_HW)
1225 csstart = skb->h.raw - skb->data;
1227 csstart = skb_headlen(skb);
1229 if (csstart > skb_headlen(skb))
1232 memcpy(to, skb->data, csstart);
1235 if (csstart != skb->len)
1236 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1237 skb->len - csstart, 0);
1239 if (skb->ip_summed == CHECKSUM_HW) {
1240 long csstuff = csstart + skb->csum;
1242 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1247 * skb_dequeue - remove from the head of the queue
1248 * @list: list to dequeue from
1250 * Remove the head of the list. The list lock is taken so the function
1251 * may be used safely with other locking list functions. The head item is
1252 * returned or %NULL if the list is empty.
1255 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1257 unsigned long flags;
1258 struct sk_buff *result;
1260 spin_lock_irqsave(&list->lock, flags);
1261 result = __skb_dequeue(list);
1262 spin_unlock_irqrestore(&list->lock, flags);
1267 * skb_dequeue_tail - remove from the tail of the queue
1268 * @list: list to dequeue from
1270 * Remove the tail of the list. The list lock is taken so the function
1271 * may be used safely with other locking list functions. The tail item is
1272 * returned or %NULL if the list is empty.
1274 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1276 unsigned long flags;
1277 struct sk_buff *result;
1279 spin_lock_irqsave(&list->lock, flags);
1280 result = __skb_dequeue_tail(list);
1281 spin_unlock_irqrestore(&list->lock, flags);
1286 * skb_queue_purge - empty a list
1287 * @list: list to empty
1289 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1290 * the list and one reference dropped. This function takes the list
1291 * lock and is atomic with respect to other list locking functions.
1293 void skb_queue_purge(struct sk_buff_head *list)
1295 struct sk_buff *skb;
1296 while ((skb = skb_dequeue(list)) != NULL)
1301 * skb_queue_head - queue a buffer at the list head
1302 * @list: list to use
1303 * @newsk: buffer to queue
1305 * Queue a buffer at the start of the list. This function takes the
1306 * list lock and can be used safely with other locking &sk_buff functions
1309 * A buffer cannot be placed on two lists at the same time.
1311 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1313 unsigned long flags;
1315 spin_lock_irqsave(&list->lock, flags);
1316 __skb_queue_head(list, newsk);
1317 spin_unlock_irqrestore(&list->lock, flags);
1321 * skb_queue_tail - queue a buffer at the list tail
1322 * @list: list to use
1323 * @newsk: buffer to queue
1325 * Queue a buffer at the tail of the list. This function takes the
1326 * list lock and can be used safely with other locking &sk_buff functions
1329 * A buffer cannot be placed on two lists at the same time.
1331 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1333 unsigned long flags;
1335 spin_lock_irqsave(&list->lock, flags);
1336 __skb_queue_tail(list, newsk);
1337 spin_unlock_irqrestore(&list->lock, flags);
1341 * skb_unlink - remove a buffer from a list
1342 * @skb: buffer to remove
1343 * @list: list to use
1345 * Remove a packet from a list. The list locks are taken and this
1346 * function is atomic with respect to other list locked calls
1348 * You must know what list the SKB is on.
1350 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1352 unsigned long flags;
1354 spin_lock_irqsave(&list->lock, flags);
1355 __skb_unlink(skb, list);
1356 spin_unlock_irqrestore(&list->lock, flags);
1360 * skb_append - append a buffer
1361 * @old: buffer to insert after
1362 * @newsk: buffer to insert
1363 * @list: list to use
1365 * Place a packet after a given packet in a list. The list locks are taken
1366 * and this function is atomic with respect to other list locked calls.
1367 * A buffer cannot be placed on two lists at the same time.
1369 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1371 unsigned long flags;
1373 spin_lock_irqsave(&list->lock, flags);
1374 __skb_append(old, newsk, list);
1375 spin_unlock_irqrestore(&list->lock, flags);
1380 * skb_insert - insert a buffer
1381 * @old: buffer to insert before
1382 * @newsk: buffer to insert
1383 * @list: list to use
1385 * Place a packet before a given packet in a list. The list locks are
1386 * taken and this function is atomic with respect to other list locked
1389 * A buffer cannot be placed on two lists at the same time.
1391 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1393 unsigned long flags;
1395 spin_lock_irqsave(&list->lock, flags);
1396 __skb_insert(newsk, old->prev, old, list);
1397 spin_unlock_irqrestore(&list->lock, flags);
1402 * Tune the memory allocator for a new MTU size.
1404 void skb_add_mtu(int mtu)
1406 /* Must match allocation in alloc_skb */
1407 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1409 kmem_add_cache_size(mtu);
1413 static inline void skb_split_inside_header(struct sk_buff *skb,
1414 struct sk_buff* skb1,
1415 const u32 len, const int pos)
1419 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1421 /* And move data appendix as is. */
1422 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1423 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1425 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1426 skb_shinfo(skb)->nr_frags = 0;
1427 skb1->data_len = skb->data_len;
1428 skb1->len += skb1->data_len;
1431 skb->tail = skb->data + len;
1434 static inline void skb_split_no_header(struct sk_buff *skb,
1435 struct sk_buff* skb1,
1436 const u32 len, int pos)
1439 const int nfrags = skb_shinfo(skb)->nr_frags;
1441 skb_shinfo(skb)->nr_frags = 0;
1442 skb1->len = skb1->data_len = skb->len - len;
1444 skb->data_len = len - pos;
1446 for (i = 0; i < nfrags; i++) {
1447 int size = skb_shinfo(skb)->frags[i].size;
1449 if (pos + size > len) {
1450 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1454 * We have two variants in this case:
1455 * 1. Move all the frag to the second
1456 * part, if it is possible. F.e.
1457 * this approach is mandatory for TUX,
1458 * where splitting is expensive.
1459 * 2. Split is accurately. We make this.
1461 get_page(skb_shinfo(skb)->frags[i].page);
1462 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1463 skb_shinfo(skb1)->frags[0].size -= len - pos;
1464 skb_shinfo(skb)->frags[i].size = len - pos;
1465 skb_shinfo(skb)->nr_frags++;
1469 skb_shinfo(skb)->nr_frags++;
1472 skb_shinfo(skb1)->nr_frags = k;
1476 * skb_split - Split fragmented skb to two parts at length len.
1477 * @skb: the buffer to split
1478 * @skb1: the buffer to receive the second part
1479 * @len: new length for skb
1481 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1483 int pos = skb_headlen(skb);
1485 if (len < pos) /* Split line is inside header. */
1486 skb_split_inside_header(skb, skb1, len, pos);
1487 else /* Second chunk has no header, nothing to copy. */
1488 skb_split_no_header(skb, skb1, len, pos);
1492 * skb_prepare_seq_read - Prepare a sequential read of skb data
1493 * @skb: the buffer to read
1494 * @from: lower offset of data to be read
1495 * @to: upper offset of data to be read
1496 * @st: state variable
1498 * Initializes the specified state variable. Must be called before
1499 * invoking skb_seq_read() for the first time.
1501 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1502 unsigned int to, struct skb_seq_state *st)
1504 st->lower_offset = from;
1505 st->upper_offset = to;
1506 st->root_skb = st->cur_skb = skb;
1507 st->frag_idx = st->stepped_offset = 0;
1508 st->frag_data = NULL;
1512 * skb_seq_read - Sequentially read skb data
1513 * @consumed: number of bytes consumed by the caller so far
1514 * @data: destination pointer for data to be returned
1515 * @st: state variable
1517 * Reads a block of skb data at &consumed relative to the
1518 * lower offset specified to skb_prepare_seq_read(). Assigns
1519 * the head of the data block to &data and returns the length
1520 * of the block or 0 if the end of the skb data or the upper
1521 * offset has been reached.
1523 * The caller is not required to consume all of the data
1524 * returned, i.e. &consumed is typically set to the number
1525 * of bytes already consumed and the next call to
1526 * skb_seq_read() will return the remaining part of the block.
1528 * Note: The size of each block of data returned can be arbitary,
1529 * this limitation is the cost for zerocopy seqeuental
1530 * reads of potentially non linear data.
1532 * Note: Fragment lists within fragments are not implemented
1533 * at the moment, state->root_skb could be replaced with
1534 * a stack for this purpose.
1536 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1537 struct skb_seq_state *st)
1539 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1542 if (unlikely(abs_offset >= st->upper_offset))
1546 block_limit = skb_headlen(st->cur_skb);
1548 if (abs_offset < block_limit) {
1549 *data = st->cur_skb->data + abs_offset;
1550 return block_limit - abs_offset;
1553 if (st->frag_idx == 0 && !st->frag_data)
1554 st->stepped_offset += skb_headlen(st->cur_skb);
1556 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1557 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1558 block_limit = frag->size + st->stepped_offset;
1560 if (abs_offset < block_limit) {
1562 st->frag_data = kmap_skb_frag(frag);
1564 *data = (u8 *) st->frag_data + frag->page_offset +
1565 (abs_offset - st->stepped_offset);
1567 return block_limit - abs_offset;
1570 if (st->frag_data) {
1571 kunmap_skb_frag(st->frag_data);
1572 st->frag_data = NULL;
1576 st->stepped_offset += frag->size;
1579 if (st->cur_skb->next) {
1580 st->cur_skb = st->cur_skb->next;
1583 } else if (st->root_skb == st->cur_skb &&
1584 skb_shinfo(st->root_skb)->frag_list) {
1585 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1593 * skb_abort_seq_read - Abort a sequential read of skb data
1594 * @st: state variable
1596 * Must be called if skb_seq_read() was not called until it
1599 void skb_abort_seq_read(struct skb_seq_state *st)
1602 kunmap_skb_frag(st->frag_data);
1605 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1607 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1608 struct ts_config *conf,
1609 struct ts_state *state)
1611 return skb_seq_read(offset, text, TS_SKB_CB(state));
1614 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1616 skb_abort_seq_read(TS_SKB_CB(state));
1620 * skb_find_text - Find a text pattern in skb data
1621 * @skb: the buffer to look in
1622 * @from: search offset
1624 * @config: textsearch configuration
1625 * @state: uninitialized textsearch state variable
1627 * Finds a pattern in the skb data according to the specified
1628 * textsearch configuration. Use textsearch_next() to retrieve
1629 * subsequent occurrences of the pattern. Returns the offset
1630 * to the first occurrence or UINT_MAX if no match was found.
1632 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1633 unsigned int to, struct ts_config *config,
1634 struct ts_state *state)
1636 config->get_next_block = skb_ts_get_next_block;
1637 config->finish = skb_ts_finish;
1639 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1641 return textsearch_find(config, state);
1644 void __init skb_init(void)
1646 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1647 sizeof(struct sk_buff),
1651 if (!skbuff_head_cache)
1652 panic("cannot create skbuff cache");
1655 EXPORT_SYMBOL(___pskb_trim);
1656 EXPORT_SYMBOL(__kfree_skb);
1657 EXPORT_SYMBOL(__pskb_pull_tail);
1658 EXPORT_SYMBOL(alloc_skb);
1659 EXPORT_SYMBOL(pskb_copy);
1660 EXPORT_SYMBOL(pskb_expand_head);
1661 EXPORT_SYMBOL(skb_checksum);
1662 EXPORT_SYMBOL(skb_clone);
1663 EXPORT_SYMBOL(skb_clone_fraglist);
1664 EXPORT_SYMBOL(skb_copy);
1665 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1666 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1667 EXPORT_SYMBOL(skb_copy_bits);
1668 EXPORT_SYMBOL(skb_copy_expand);
1669 EXPORT_SYMBOL(skb_over_panic);
1670 EXPORT_SYMBOL(skb_pad);
1671 EXPORT_SYMBOL(skb_realloc_headroom);
1672 EXPORT_SYMBOL(skb_under_panic);
1673 EXPORT_SYMBOL(skb_dequeue);
1674 EXPORT_SYMBOL(skb_dequeue_tail);
1675 EXPORT_SYMBOL(skb_insert);
1676 EXPORT_SYMBOL(skb_queue_purge);
1677 EXPORT_SYMBOL(skb_queue_head);
1678 EXPORT_SYMBOL(skb_queue_tail);
1679 EXPORT_SYMBOL(skb_unlink);
1680 EXPORT_SYMBOL(skb_append);
1681 EXPORT_SYMBOL(skb_split);
1682 EXPORT_SYMBOL(skb_prepare_seq_read);
1683 EXPORT_SYMBOL(skb_seq_read);
1684 EXPORT_SYMBOL(skb_abort_seq_read);
1685 EXPORT_SYMBOL(skb_find_text);