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 __read_mostly;
72 static kmem_cache_t *skbuff_fclone_cache __read_mostly;
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
81 * skb_over_panic - private function
86 * Out of line support code for skb_put(). Not user callable.
88 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
98 * skb_under_panic - private function
103 * Out of line support code for skb_push(). Not user callable.
106 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
115 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
116 * 'private' fields and also do memory statistics to find all the
122 * __alloc_skb - allocate a network buffer
123 * @size: size to allocate
124 * @gfp_mask: allocation mask
125 * @fclone: allocate from fclone cache instead of head cache
126 * and allocate a cloned (child) skb
128 * Allocate a new &sk_buff. The returned buffer has no headroom and a
129 * tail room of size bytes. The object has a reference count of one.
130 * The return is the buffer. On a failure the return is %NULL.
132 * Buffers may only be allocated from interrupts using a @gfp_mask of
135 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
139 struct skb_shared_info *shinfo;
143 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
146 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
150 /* Get the DATA. Size must match skb_add_mtu(). */
151 size = SKB_DATA_ALIGN(size);
152 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
156 memset(skb, 0, offsetof(struct sk_buff, truesize));
157 skb->truesize = size + sizeof(struct sk_buff);
158 atomic_set(&skb->users, 1);
162 skb->end = data + size;
163 /* make sure we initialize shinfo sequentially */
164 shinfo = skb_shinfo(skb);
165 atomic_set(&shinfo->dataref, 1);
166 shinfo->nr_frags = 0;
167 shinfo->tso_size = 0;
168 shinfo->tso_segs = 0;
169 shinfo->ufo_size = 0;
170 shinfo->ip6_frag_id = 0;
171 shinfo->frag_list = NULL;
174 struct sk_buff *child = skb + 1;
175 atomic_t *fclone_ref = (atomic_t *) (child + 1);
177 skb->fclone = SKB_FCLONE_ORIG;
178 atomic_set(fclone_ref, 1);
180 child->fclone = SKB_FCLONE_UNAVAILABLE;
185 kmem_cache_free(cache, skb);
191 * alloc_skb_from_cache - allocate a network buffer
192 * @cp: kmem_cache from which to allocate the data area
193 * (object size must be big enough for @size bytes + skb overheads)
194 * @size: size to allocate
195 * @gfp_mask: allocation mask
197 * Allocate a new &sk_buff. The returned buffer has no headroom and
198 * tail room of size bytes. The object has a reference count of one.
199 * The return is the buffer. On a failure the return is %NULL.
201 * Buffers may only be allocated from interrupts using a @gfp_mask of
204 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
212 skb = kmem_cache_alloc(skbuff_head_cache,
213 gfp_mask & ~__GFP_DMA);
218 size = SKB_DATA_ALIGN(size);
219 data = kmem_cache_alloc(cp, gfp_mask);
223 memset(skb, 0, offsetof(struct sk_buff, truesize));
224 skb->truesize = size + sizeof(struct sk_buff);
225 atomic_set(&skb->users, 1);
229 skb->end = data + size;
231 atomic_set(&(skb_shinfo(skb)->dataref), 1);
232 skb_shinfo(skb)->nr_frags = 0;
233 skb_shinfo(skb)->tso_size = 0;
234 skb_shinfo(skb)->tso_segs = 0;
235 skb_shinfo(skb)->ufo_size = 0;
236 skb_shinfo(skb)->frag_list = NULL;
240 kmem_cache_free(skbuff_head_cache, skb);
246 static void skb_drop_list(struct sk_buff **listp)
248 struct sk_buff *list = *listp;
253 struct sk_buff *this = list;
259 static inline void skb_drop_fraglist(struct sk_buff *skb)
261 skb_drop_list(&skb_shinfo(skb)->frag_list);
264 static void skb_clone_fraglist(struct sk_buff *skb)
266 struct sk_buff *list;
268 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
272 void skb_release_data(struct sk_buff *skb)
275 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
276 &skb_shinfo(skb)->dataref)) {
277 if (skb_shinfo(skb)->nr_frags) {
279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
280 put_page(skb_shinfo(skb)->frags[i].page);
283 if (skb_shinfo(skb)->frag_list)
284 skb_drop_fraglist(skb);
291 * Free an skbuff by memory without cleaning the state.
293 void kfree_skbmem(struct sk_buff *skb)
295 struct sk_buff *other;
296 atomic_t *fclone_ref;
298 skb_release_data(skb);
299 switch (skb->fclone) {
300 case SKB_FCLONE_UNAVAILABLE:
301 kmem_cache_free(skbuff_head_cache, skb);
304 case SKB_FCLONE_ORIG:
305 fclone_ref = (atomic_t *) (skb + 2);
306 if (atomic_dec_and_test(fclone_ref))
307 kmem_cache_free(skbuff_fclone_cache, skb);
310 case SKB_FCLONE_CLONE:
311 fclone_ref = (atomic_t *) (skb + 1);
314 /* The clone portion is available for
315 * fast-cloning again.
317 skb->fclone = SKB_FCLONE_UNAVAILABLE;
319 if (atomic_dec_and_test(fclone_ref))
320 kmem_cache_free(skbuff_fclone_cache, other);
326 * __kfree_skb - private function
329 * Free an sk_buff. Release anything attached to the buffer.
330 * Clean the state. This is an internal helper function. Users should
331 * always call kfree_skb
334 void __kfree_skb(struct sk_buff *skb)
336 dst_release(skb->dst);
338 secpath_put(skb->sp);
340 if (skb->destructor) {
342 skb->destructor(skb);
344 #ifdef CONFIG_NETFILTER
345 nf_conntrack_put(skb->nfct);
346 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
347 nf_conntrack_put_reasm(skb->nfct_reasm);
349 #ifdef CONFIG_BRIDGE_NETFILTER
350 nf_bridge_put(skb->nf_bridge);
353 /* XXX: IS this still necessary? - JHS */
354 #ifdef CONFIG_NET_SCHED
356 #ifdef CONFIG_NET_CLS_ACT
365 * skb_clone - duplicate an sk_buff
366 * @skb: buffer to clone
367 * @gfp_mask: allocation priority
369 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
370 * copies share the same packet data but not structure. The new
371 * buffer has a reference count of 1. If the allocation fails the
372 * function returns %NULL otherwise the new buffer is returned.
374 * If this function is called from an interrupt gfp_mask() must be
378 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
383 if (skb->fclone == SKB_FCLONE_ORIG &&
384 n->fclone == SKB_FCLONE_UNAVAILABLE) {
385 atomic_t *fclone_ref = (atomic_t *) (n + 1);
386 n->fclone = SKB_FCLONE_CLONE;
387 atomic_inc(fclone_ref);
389 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
392 n->fclone = SKB_FCLONE_UNAVAILABLE;
395 #define C(x) n->x = skb->x
397 n->next = n->prev = NULL;
408 secpath_get(skb->sp);
410 memcpy(n->cb, skb->cb, sizeof(skb->cb));
420 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
424 n->destructor = NULL;
425 #ifdef CONFIG_NETFILTER
428 nf_conntrack_get(skb->nfct);
430 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
432 nf_conntrack_get_reasm(skb->nfct_reasm);
434 #ifdef CONFIG_BRIDGE_NETFILTER
436 nf_bridge_get(skb->nf_bridge);
438 #endif /*CONFIG_NETFILTER*/
439 #ifdef CONFIG_NET_SCHED
441 #ifdef CONFIG_NET_CLS_ACT
442 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
443 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
444 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
449 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
452 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
456 atomic_set(&n->users, 1);
462 atomic_inc(&(skb_shinfo(skb)->dataref));
468 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
471 * Shift between the two data areas in bytes
473 unsigned long offset = new->data - old->data;
477 new->priority = old->priority;
478 new->protocol = old->protocol;
479 new->dst = dst_clone(old->dst);
481 new->sp = secpath_get(old->sp);
483 new->h.raw = old->h.raw + offset;
484 new->nh.raw = old->nh.raw + offset;
485 new->mac.raw = old->mac.raw + offset;
486 memcpy(new->cb, old->cb, sizeof(old->cb));
487 new->local_df = old->local_df;
488 new->fclone = SKB_FCLONE_UNAVAILABLE;
489 new->pkt_type = old->pkt_type;
490 new->tstamp = old->tstamp;
491 new->destructor = NULL;
492 #ifdef CONFIG_NETFILTER
493 new->nfmark = old->nfmark;
494 new->nfct = old->nfct;
495 nf_conntrack_get(old->nfct);
496 new->nfctinfo = old->nfctinfo;
497 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
498 new->nfct_reasm = old->nfct_reasm;
499 nf_conntrack_get_reasm(old->nfct_reasm);
501 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
502 new->ipvs_property = old->ipvs_property;
504 #ifdef CONFIG_BRIDGE_NETFILTER
505 new->nf_bridge = old->nf_bridge;
506 nf_bridge_get(old->nf_bridge);
509 #ifdef CONFIG_NET_SCHED
510 #ifdef CONFIG_NET_CLS_ACT
511 new->tc_verd = old->tc_verd;
513 new->tc_index = old->tc_index;
515 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
518 atomic_set(&new->users, 1);
519 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
520 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
521 skb_shinfo(new)->ufo_size = skb_shinfo(old)->ufo_size;
525 * skb_copy - create private copy of an sk_buff
526 * @skb: buffer to copy
527 * @gfp_mask: allocation priority
529 * Make a copy of both an &sk_buff and its data. This is used when the
530 * caller wishes to modify the data and needs a private copy of the
531 * data to alter. Returns %NULL on failure or the pointer to the buffer
532 * on success. The returned buffer has a reference count of 1.
534 * As by-product this function converts non-linear &sk_buff to linear
535 * one, so that &sk_buff becomes completely private and caller is allowed
536 * to modify all the data of returned buffer. This means that this
537 * function is not recommended for use in circumstances when only
538 * header is going to be modified. Use pskb_copy() instead.
541 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
543 int headerlen = skb->data - skb->head;
545 * Allocate the copy buffer
547 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
552 /* Set the data pointer */
553 skb_reserve(n, headerlen);
554 /* Set the tail pointer and length */
555 skb_put(n, skb->len);
557 n->ip_summed = skb->ip_summed;
559 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
562 copy_skb_header(n, skb);
568 * pskb_copy - create copy of an sk_buff with private head.
569 * @skb: buffer to copy
570 * @gfp_mask: allocation priority
572 * Make a copy of both an &sk_buff and part of its data, located
573 * in header. Fragmented data remain shared. This is used when
574 * the caller wishes to modify only header of &sk_buff and needs
575 * private copy of the header to alter. Returns %NULL on failure
576 * or the pointer to the buffer on success.
577 * The returned buffer has a reference count of 1.
580 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
583 * Allocate the copy buffer
585 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
590 /* Set the data pointer */
591 skb_reserve(n, skb->data - skb->head);
592 /* Set the tail pointer and length */
593 skb_put(n, skb_headlen(skb));
595 memcpy(n->data, skb->data, n->len);
597 n->ip_summed = skb->ip_summed;
599 n->truesize += skb->data_len;
600 n->data_len = skb->data_len;
603 if (skb_shinfo(skb)->nr_frags) {
606 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
607 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
608 get_page(skb_shinfo(n)->frags[i].page);
610 skb_shinfo(n)->nr_frags = i;
613 if (skb_shinfo(skb)->frag_list) {
614 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
615 skb_clone_fraglist(n);
618 copy_skb_header(n, skb);
624 * pskb_expand_head - reallocate header of &sk_buff
625 * @skb: buffer to reallocate
626 * @nhead: room to add at head
627 * @ntail: room to add at tail
628 * @gfp_mask: allocation priority
630 * Expands (or creates identical copy, if &nhead and &ntail are zero)
631 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
632 * reference count of 1. Returns zero in the case of success or error,
633 * if expansion failed. In the last case, &sk_buff is not changed.
635 * All the pointers pointing into skb header may change and must be
636 * reloaded after call to this function.
639 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
644 int size = nhead + (skb->end - skb->head) + ntail;
650 size = SKB_DATA_ALIGN(size);
652 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
656 /* Copy only real data... and, alas, header. This should be
657 * optimized for the cases when header is void. */
658 memcpy(data + nhead, skb->head, skb->tail - skb->head);
659 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
661 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
662 get_page(skb_shinfo(skb)->frags[i].page);
664 if (skb_shinfo(skb)->frag_list)
665 skb_clone_fraglist(skb);
667 skb_release_data(skb);
669 off = (data + nhead) - skb->head;
672 skb->end = data + size;
680 atomic_set(&skb_shinfo(skb)->dataref, 1);
687 /* Make private copy of skb with writable head and some headroom */
689 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
691 struct sk_buff *skb2;
692 int delta = headroom - skb_headroom(skb);
695 skb2 = pskb_copy(skb, GFP_ATOMIC);
697 skb2 = skb_clone(skb, GFP_ATOMIC);
698 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
709 * skb_copy_expand - copy and expand sk_buff
710 * @skb: buffer to copy
711 * @newheadroom: new free bytes at head
712 * @newtailroom: new free bytes at tail
713 * @gfp_mask: allocation priority
715 * Make a copy of both an &sk_buff and its data and while doing so
716 * allocate additional space.
718 * This is used when the caller wishes to modify the data and needs a
719 * private copy of the data to alter as well as more space for new fields.
720 * Returns %NULL on failure or the pointer to the buffer
721 * on success. The returned buffer has a reference count of 1.
723 * You must pass %GFP_ATOMIC as the allocation priority if this function
724 * is called from an interrupt.
726 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
727 * only by netfilter in the cases when checksum is recalculated? --ANK
729 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
730 int newheadroom, int newtailroom,
734 * Allocate the copy buffer
736 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
738 int head_copy_len, head_copy_off;
743 skb_reserve(n, newheadroom);
745 /* Set the tail pointer and length */
746 skb_put(n, skb->len);
748 head_copy_len = skb_headroom(skb);
750 if (newheadroom <= head_copy_len)
751 head_copy_len = newheadroom;
753 head_copy_off = newheadroom - head_copy_len;
755 /* Copy the linear header and data. */
756 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
757 skb->len + head_copy_len))
760 copy_skb_header(n, skb);
766 * skb_pad - zero pad the tail of an skb
767 * @skb: buffer to pad
770 * Ensure that a buffer is followed by a padding area that is zero
771 * filled. Used by network drivers which may DMA or transfer data
772 * beyond the buffer end onto the wire.
774 * May return NULL in out of memory cases.
777 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
779 struct sk_buff *nskb;
781 /* If the skbuff is non linear tailroom is always zero.. */
782 if (skb_tailroom(skb) >= pad) {
783 memset(skb->data+skb->len, 0, pad);
787 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
790 memset(nskb->data+nskb->len, 0, pad);
794 /* Trims skb to length len. It can change skb pointers.
797 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
799 struct sk_buff **fragp;
800 struct sk_buff *frag;
801 int offset = skb_headlen(skb);
802 int nfrags = skb_shinfo(skb)->nr_frags;
806 if (skb_cloned(skb) &&
807 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
814 for (; i < nfrags; i++) {
815 int end = offset + skb_shinfo(skb)->frags[i].size;
822 skb_shinfo(skb)->frags[i++].size = len - offset;
825 skb_shinfo(skb)->nr_frags = i;
827 for (; i < nfrags; i++)
828 put_page(skb_shinfo(skb)->frags[i].page);
830 if (skb_shinfo(skb)->frag_list)
831 skb_drop_fraglist(skb);
835 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
836 fragp = &frag->next) {
837 int end = offset + frag->len;
839 if (skb_shared(frag)) {
840 struct sk_buff *nfrag;
842 nfrag = skb_clone(frag, GFP_ATOMIC);
843 if (unlikely(!nfrag))
846 nfrag->next = frag->next;
858 unlikely((err = pskb_trim(frag, len - offset))))
862 skb_drop_list(&frag->next);
867 if (len > skb_headlen(skb)) {
868 skb->data_len -= skb->len - len;
873 skb->tail = skb->data + len;
880 * __pskb_pull_tail - advance tail of skb header
881 * @skb: buffer to reallocate
882 * @delta: number of bytes to advance tail
884 * The function makes a sense only on a fragmented &sk_buff,
885 * it expands header moving its tail forward and copying necessary
886 * data from fragmented part.
888 * &sk_buff MUST have reference count of 1.
890 * Returns %NULL (and &sk_buff does not change) if pull failed
891 * or value of new tail of skb in the case of success.
893 * All the pointers pointing into skb header may change and must be
894 * reloaded after call to this function.
897 /* Moves tail of skb head forward, copying data from fragmented part,
898 * when it is necessary.
899 * 1. It may fail due to malloc failure.
900 * 2. It may change skb pointers.
902 * It is pretty complicated. Luckily, it is called only in exceptional cases.
904 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
906 /* If skb has not enough free space at tail, get new one
907 * plus 128 bytes for future expansions. If we have enough
908 * room at tail, reallocate without expansion only if skb is cloned.
910 int i, k, eat = (skb->tail + delta) - skb->end;
912 if (eat > 0 || skb_cloned(skb)) {
913 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
918 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
921 /* Optimization: no fragments, no reasons to preestimate
922 * size of pulled pages. Superb.
924 if (!skb_shinfo(skb)->frag_list)
927 /* Estimate size of pulled pages. */
929 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
930 if (skb_shinfo(skb)->frags[i].size >= eat)
932 eat -= skb_shinfo(skb)->frags[i].size;
935 /* If we need update frag list, we are in troubles.
936 * Certainly, it possible to add an offset to skb data,
937 * but taking into account that pulling is expected to
938 * be very rare operation, it is worth to fight against
939 * further bloating skb head and crucify ourselves here instead.
940 * Pure masohism, indeed. 8)8)
943 struct sk_buff *list = skb_shinfo(skb)->frag_list;
944 struct sk_buff *clone = NULL;
945 struct sk_buff *insp = NULL;
950 if (list->len <= eat) {
951 /* Eaten as whole. */
956 /* Eaten partially. */
958 if (skb_shared(list)) {
959 /* Sucks! We need to fork list. :-( */
960 clone = skb_clone(list, GFP_ATOMIC);
966 /* This may be pulled without
970 if (!pskb_pull(list, eat)) {
979 /* Free pulled out fragments. */
980 while ((list = skb_shinfo(skb)->frag_list) != insp) {
981 skb_shinfo(skb)->frag_list = list->next;
984 /* And insert new clone at head. */
987 skb_shinfo(skb)->frag_list = clone;
990 /* Success! Now we may commit changes to skb data. */
995 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
996 if (skb_shinfo(skb)->frags[i].size <= eat) {
997 put_page(skb_shinfo(skb)->frags[i].page);
998 eat -= skb_shinfo(skb)->frags[i].size;
1000 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1002 skb_shinfo(skb)->frags[k].page_offset += eat;
1003 skb_shinfo(skb)->frags[k].size -= eat;
1009 skb_shinfo(skb)->nr_frags = k;
1012 skb->data_len -= delta;
1017 /* Copy some data bits from skb to kernel buffer. */
1019 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1022 int start = skb_headlen(skb);
1024 if (offset > (int)skb->len - len)
1028 if ((copy = start - offset) > 0) {
1031 memcpy(to, skb->data + offset, copy);
1032 if ((len -= copy) == 0)
1038 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1041 BUG_TRAP(start <= offset + len);
1043 end = start + skb_shinfo(skb)->frags[i].size;
1044 if ((copy = end - offset) > 0) {
1050 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1052 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1053 offset - start, copy);
1054 kunmap_skb_frag(vaddr);
1056 if ((len -= copy) == 0)
1064 if (skb_shinfo(skb)->frag_list) {
1065 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1067 for (; list; list = list->next) {
1070 BUG_TRAP(start <= offset + len);
1072 end = start + list->len;
1073 if ((copy = end - offset) > 0) {
1076 if (skb_copy_bits(list, offset - start,
1079 if ((len -= copy) == 0)
1095 * skb_store_bits - store bits from kernel buffer to skb
1096 * @skb: destination buffer
1097 * @offset: offset in destination
1098 * @from: source buffer
1099 * @len: number of bytes to copy
1101 * Copy the specified number of bytes from the source buffer to the
1102 * destination skb. This function handles all the messy bits of
1103 * traversing fragment lists and such.
1106 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1109 int start = skb_headlen(skb);
1111 if (offset > (int)skb->len - len)
1114 if ((copy = start - offset) > 0) {
1117 memcpy(skb->data + offset, from, copy);
1118 if ((len -= copy) == 0)
1124 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1125 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1128 BUG_TRAP(start <= offset + len);
1130 end = start + frag->size;
1131 if ((copy = end - offset) > 0) {
1137 vaddr = kmap_skb_frag(frag);
1138 memcpy(vaddr + frag->page_offset + offset - start,
1140 kunmap_skb_frag(vaddr);
1142 if ((len -= copy) == 0)
1150 if (skb_shinfo(skb)->frag_list) {
1151 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1153 for (; list; list = list->next) {
1156 BUG_TRAP(start <= offset + len);
1158 end = start + list->len;
1159 if ((copy = end - offset) > 0) {
1162 if (skb_store_bits(list, offset - start,
1165 if ((len -= copy) == 0)
1180 EXPORT_SYMBOL(skb_store_bits);
1182 /* Checksum skb data. */
1184 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1185 int len, unsigned int csum)
1187 int start = skb_headlen(skb);
1188 int i, copy = start - offset;
1191 /* Checksum header. */
1195 csum = csum_partial(skb->data + offset, copy, csum);
1196 if ((len -= copy) == 0)
1202 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1205 BUG_TRAP(start <= offset + len);
1207 end = start + skb_shinfo(skb)->frags[i].size;
1208 if ((copy = end - offset) > 0) {
1211 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1215 vaddr = kmap_skb_frag(frag);
1216 csum2 = csum_partial(vaddr + frag->page_offset +
1217 offset - start, copy, 0);
1218 kunmap_skb_frag(vaddr);
1219 csum = csum_block_add(csum, csum2, pos);
1228 if (skb_shinfo(skb)->frag_list) {
1229 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1231 for (; list; list = list->next) {
1234 BUG_TRAP(start <= offset + len);
1236 end = start + list->len;
1237 if ((copy = end - offset) > 0) {
1241 csum2 = skb_checksum(list, offset - start,
1243 csum = csum_block_add(csum, csum2, pos);
1244 if ((len -= copy) == 0)
1257 /* Both of above in one bottle. */
1259 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1260 u8 *to, int len, unsigned int csum)
1262 int start = skb_headlen(skb);
1263 int i, copy = start - offset;
1270 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1272 if ((len -= copy) == 0)
1279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1282 BUG_TRAP(start <= offset + len);
1284 end = start + skb_shinfo(skb)->frags[i].size;
1285 if ((copy = end - offset) > 0) {
1288 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1292 vaddr = kmap_skb_frag(frag);
1293 csum2 = csum_partial_copy_nocheck(vaddr +
1297 kunmap_skb_frag(vaddr);
1298 csum = csum_block_add(csum, csum2, pos);
1308 if (skb_shinfo(skb)->frag_list) {
1309 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1311 for (; list; list = list->next) {
1315 BUG_TRAP(start <= offset + len);
1317 end = start + list->len;
1318 if ((copy = end - offset) > 0) {
1321 csum2 = skb_copy_and_csum_bits(list,
1324 csum = csum_block_add(csum, csum2, pos);
1325 if ((len -= copy) == 0)
1338 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1343 if (skb->ip_summed == CHECKSUM_HW)
1344 csstart = skb->h.raw - skb->data;
1346 csstart = skb_headlen(skb);
1348 BUG_ON(csstart > skb_headlen(skb));
1350 memcpy(to, skb->data, csstart);
1353 if (csstart != skb->len)
1354 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1355 skb->len - csstart, 0);
1357 if (skb->ip_summed == CHECKSUM_HW) {
1358 long csstuff = csstart + skb->csum;
1360 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1365 * skb_dequeue - remove from the head of the queue
1366 * @list: list to dequeue from
1368 * Remove the head of the list. The list lock is taken so the function
1369 * may be used safely with other locking list functions. The head item is
1370 * returned or %NULL if the list is empty.
1373 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1375 unsigned long flags;
1376 struct sk_buff *result;
1378 spin_lock_irqsave(&list->lock, flags);
1379 result = __skb_dequeue(list);
1380 spin_unlock_irqrestore(&list->lock, flags);
1385 * skb_dequeue_tail - remove from the tail of the queue
1386 * @list: list to dequeue from
1388 * Remove the tail of the list. The list lock is taken so the function
1389 * may be used safely with other locking list functions. The tail item is
1390 * returned or %NULL if the list is empty.
1392 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1394 unsigned long flags;
1395 struct sk_buff *result;
1397 spin_lock_irqsave(&list->lock, flags);
1398 result = __skb_dequeue_tail(list);
1399 spin_unlock_irqrestore(&list->lock, flags);
1404 * skb_queue_purge - empty a list
1405 * @list: list to empty
1407 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1408 * the list and one reference dropped. This function takes the list
1409 * lock and is atomic with respect to other list locking functions.
1411 void skb_queue_purge(struct sk_buff_head *list)
1413 struct sk_buff *skb;
1414 while ((skb = skb_dequeue(list)) != NULL)
1419 * skb_queue_head - queue a buffer at the list head
1420 * @list: list to use
1421 * @newsk: buffer to queue
1423 * Queue a buffer at the start of the list. This function takes the
1424 * list lock and can be used safely with other locking &sk_buff functions
1427 * A buffer cannot be placed on two lists at the same time.
1429 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1431 unsigned long flags;
1433 spin_lock_irqsave(&list->lock, flags);
1434 __skb_queue_head(list, newsk);
1435 spin_unlock_irqrestore(&list->lock, flags);
1439 * skb_queue_tail - queue a buffer at the list tail
1440 * @list: list to use
1441 * @newsk: buffer to queue
1443 * Queue a buffer at the tail of the list. This function takes the
1444 * list lock and can be used safely with other locking &sk_buff functions
1447 * A buffer cannot be placed on two lists at the same time.
1449 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1451 unsigned long flags;
1453 spin_lock_irqsave(&list->lock, flags);
1454 __skb_queue_tail(list, newsk);
1455 spin_unlock_irqrestore(&list->lock, flags);
1459 * skb_unlink - remove a buffer from a list
1460 * @skb: buffer to remove
1461 * @list: list to use
1463 * Remove a packet from a list. The list locks are taken and this
1464 * function is atomic with respect to other list locked calls
1466 * You must know what list the SKB is on.
1468 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1470 unsigned long flags;
1472 spin_lock_irqsave(&list->lock, flags);
1473 __skb_unlink(skb, list);
1474 spin_unlock_irqrestore(&list->lock, flags);
1478 * skb_append - append a buffer
1479 * @old: buffer to insert after
1480 * @newsk: buffer to insert
1481 * @list: list to use
1483 * Place a packet after a given packet in a list. The list locks are taken
1484 * and this function is atomic with respect to other list locked calls.
1485 * A buffer cannot be placed on two lists at the same time.
1487 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1489 unsigned long flags;
1491 spin_lock_irqsave(&list->lock, flags);
1492 __skb_append(old, newsk, list);
1493 spin_unlock_irqrestore(&list->lock, flags);
1498 * skb_insert - insert a buffer
1499 * @old: buffer to insert before
1500 * @newsk: buffer to insert
1501 * @list: list to use
1503 * Place a packet before a given packet in a list. The list locks are
1504 * taken and this function is atomic with respect to other list locked
1507 * A buffer cannot be placed on two lists at the same time.
1509 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1511 unsigned long flags;
1513 spin_lock_irqsave(&list->lock, flags);
1514 __skb_insert(newsk, old->prev, old, list);
1515 spin_unlock_irqrestore(&list->lock, flags);
1520 * Tune the memory allocator for a new MTU size.
1522 void skb_add_mtu(int mtu)
1524 /* Must match allocation in alloc_skb */
1525 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1527 kmem_add_cache_size(mtu);
1531 static inline void skb_split_inside_header(struct sk_buff *skb,
1532 struct sk_buff* skb1,
1533 const u32 len, const int pos)
1537 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1539 /* And move data appendix as is. */
1540 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1541 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1543 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1544 skb_shinfo(skb)->nr_frags = 0;
1545 skb1->data_len = skb->data_len;
1546 skb1->len += skb1->data_len;
1549 skb->tail = skb->data + len;
1552 static inline void skb_split_no_header(struct sk_buff *skb,
1553 struct sk_buff* skb1,
1554 const u32 len, int pos)
1557 const int nfrags = skb_shinfo(skb)->nr_frags;
1559 skb_shinfo(skb)->nr_frags = 0;
1560 skb1->len = skb1->data_len = skb->len - len;
1562 skb->data_len = len - pos;
1564 for (i = 0; i < nfrags; i++) {
1565 int size = skb_shinfo(skb)->frags[i].size;
1567 if (pos + size > len) {
1568 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1572 * We have two variants in this case:
1573 * 1. Move all the frag to the second
1574 * part, if it is possible. F.e.
1575 * this approach is mandatory for TUX,
1576 * where splitting is expensive.
1577 * 2. Split is accurately. We make this.
1579 get_page(skb_shinfo(skb)->frags[i].page);
1580 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1581 skb_shinfo(skb1)->frags[0].size -= len - pos;
1582 skb_shinfo(skb)->frags[i].size = len - pos;
1583 skb_shinfo(skb)->nr_frags++;
1587 skb_shinfo(skb)->nr_frags++;
1590 skb_shinfo(skb1)->nr_frags = k;
1594 * skb_split - Split fragmented skb to two parts at length len.
1595 * @skb: the buffer to split
1596 * @skb1: the buffer to receive the second part
1597 * @len: new length for skb
1599 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1601 int pos = skb_headlen(skb);
1603 if (len < pos) /* Split line is inside header. */
1604 skb_split_inside_header(skb, skb1, len, pos);
1605 else /* Second chunk has no header, nothing to copy. */
1606 skb_split_no_header(skb, skb1, len, pos);
1610 * skb_prepare_seq_read - Prepare a sequential read of skb data
1611 * @skb: the buffer to read
1612 * @from: lower offset of data to be read
1613 * @to: upper offset of data to be read
1614 * @st: state variable
1616 * Initializes the specified state variable. Must be called before
1617 * invoking skb_seq_read() for the first time.
1619 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1620 unsigned int to, struct skb_seq_state *st)
1622 st->lower_offset = from;
1623 st->upper_offset = to;
1624 st->root_skb = st->cur_skb = skb;
1625 st->frag_idx = st->stepped_offset = 0;
1626 st->frag_data = NULL;
1630 * skb_seq_read - Sequentially read skb data
1631 * @consumed: number of bytes consumed by the caller so far
1632 * @data: destination pointer for data to be returned
1633 * @st: state variable
1635 * Reads a block of skb data at &consumed relative to the
1636 * lower offset specified to skb_prepare_seq_read(). Assigns
1637 * the head of the data block to &data and returns the length
1638 * of the block or 0 if the end of the skb data or the upper
1639 * offset has been reached.
1641 * The caller is not required to consume all of the data
1642 * returned, i.e. &consumed is typically set to the number
1643 * of bytes already consumed and the next call to
1644 * skb_seq_read() will return the remaining part of the block.
1646 * Note: The size of each block of data returned can be arbitary,
1647 * this limitation is the cost for zerocopy seqeuental
1648 * reads of potentially non linear data.
1650 * Note: Fragment lists within fragments are not implemented
1651 * at the moment, state->root_skb could be replaced with
1652 * a stack for this purpose.
1654 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1655 struct skb_seq_state *st)
1657 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1660 if (unlikely(abs_offset >= st->upper_offset))
1664 block_limit = skb_headlen(st->cur_skb);
1666 if (abs_offset < block_limit) {
1667 *data = st->cur_skb->data + abs_offset;
1668 return block_limit - abs_offset;
1671 if (st->frag_idx == 0 && !st->frag_data)
1672 st->stepped_offset += skb_headlen(st->cur_skb);
1674 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1675 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1676 block_limit = frag->size + st->stepped_offset;
1678 if (abs_offset < block_limit) {
1680 st->frag_data = kmap_skb_frag(frag);
1682 *data = (u8 *) st->frag_data + frag->page_offset +
1683 (abs_offset - st->stepped_offset);
1685 return block_limit - abs_offset;
1688 if (st->frag_data) {
1689 kunmap_skb_frag(st->frag_data);
1690 st->frag_data = NULL;
1694 st->stepped_offset += frag->size;
1697 if (st->cur_skb->next) {
1698 st->cur_skb = st->cur_skb->next;
1701 } else if (st->root_skb == st->cur_skb &&
1702 skb_shinfo(st->root_skb)->frag_list) {
1703 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1711 * skb_abort_seq_read - Abort a sequential read of skb data
1712 * @st: state variable
1714 * Must be called if skb_seq_read() was not called until it
1717 void skb_abort_seq_read(struct skb_seq_state *st)
1720 kunmap_skb_frag(st->frag_data);
1723 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1725 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1726 struct ts_config *conf,
1727 struct ts_state *state)
1729 return skb_seq_read(offset, text, TS_SKB_CB(state));
1732 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1734 skb_abort_seq_read(TS_SKB_CB(state));
1738 * skb_find_text - Find a text pattern in skb data
1739 * @skb: the buffer to look in
1740 * @from: search offset
1742 * @config: textsearch configuration
1743 * @state: uninitialized textsearch state variable
1745 * Finds a pattern in the skb data according to the specified
1746 * textsearch configuration. Use textsearch_next() to retrieve
1747 * subsequent occurrences of the pattern. Returns the offset
1748 * to the first occurrence or UINT_MAX if no match was found.
1750 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1751 unsigned int to, struct ts_config *config,
1752 struct ts_state *state)
1754 config->get_next_block = skb_ts_get_next_block;
1755 config->finish = skb_ts_finish;
1757 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1759 return textsearch_find(config, state);
1763 * skb_append_datato_frags: - append the user data to a skb
1764 * @sk: sock structure
1765 * @skb: skb structure to be appened with user data.
1766 * @getfrag: call back function to be used for getting the user data
1767 * @from: pointer to user message iov
1768 * @length: length of the iov message
1770 * Description: This procedure append the user data in the fragment part
1771 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1773 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1774 int (*getfrag)(void *from, char *to, int offset,
1775 int len, int odd, struct sk_buff *skb),
1776 void *from, int length)
1779 skb_frag_t *frag = NULL;
1780 struct page *page = NULL;
1786 /* Return error if we don't have space for new frag */
1787 frg_cnt = skb_shinfo(skb)->nr_frags;
1788 if (frg_cnt >= MAX_SKB_FRAGS)
1791 /* allocate a new page for next frag */
1792 page = alloc_pages(sk->sk_allocation, 0);
1794 /* If alloc_page fails just return failure and caller will
1795 * free previous allocated pages by doing kfree_skb()
1800 /* initialize the next frag */
1801 sk->sk_sndmsg_page = page;
1802 sk->sk_sndmsg_off = 0;
1803 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1804 skb->truesize += PAGE_SIZE;
1805 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1807 /* get the new initialized frag */
1808 frg_cnt = skb_shinfo(skb)->nr_frags;
1809 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1811 /* copy the user data to page */
1812 left = PAGE_SIZE - frag->page_offset;
1813 copy = (length > left)? left : length;
1815 ret = getfrag(from, (page_address(frag->page) +
1816 frag->page_offset + frag->size),
1817 offset, copy, 0, skb);
1821 /* copy was successful so update the size parameters */
1822 sk->sk_sndmsg_off += copy;
1825 skb->data_len += copy;
1829 } while (length > 0);
1834 void __init skb_init(void)
1836 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1837 sizeof(struct sk_buff),
1841 if (!skbuff_head_cache)
1842 panic("cannot create skbuff cache");
1844 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1845 (2*sizeof(struct sk_buff)) +
1850 if (!skbuff_fclone_cache)
1851 panic("cannot create skbuff cache");
1854 EXPORT_SYMBOL(___pskb_trim);
1855 EXPORT_SYMBOL(__kfree_skb);
1856 EXPORT_SYMBOL(__pskb_pull_tail);
1857 EXPORT_SYMBOL(__alloc_skb);
1858 EXPORT_SYMBOL(pskb_copy);
1859 EXPORT_SYMBOL(pskb_expand_head);
1860 EXPORT_SYMBOL(skb_checksum);
1861 EXPORT_SYMBOL(skb_clone);
1862 EXPORT_SYMBOL(skb_clone_fraglist);
1863 EXPORT_SYMBOL(skb_copy);
1864 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1865 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1866 EXPORT_SYMBOL(skb_copy_bits);
1867 EXPORT_SYMBOL(skb_copy_expand);
1868 EXPORT_SYMBOL(skb_over_panic);
1869 EXPORT_SYMBOL(skb_pad);
1870 EXPORT_SYMBOL(skb_realloc_headroom);
1871 EXPORT_SYMBOL(skb_under_panic);
1872 EXPORT_SYMBOL(skb_dequeue);
1873 EXPORT_SYMBOL(skb_dequeue_tail);
1874 EXPORT_SYMBOL(skb_insert);
1875 EXPORT_SYMBOL(skb_queue_purge);
1876 EXPORT_SYMBOL(skb_queue_head);
1877 EXPORT_SYMBOL(skb_queue_tail);
1878 EXPORT_SYMBOL(skb_unlink);
1879 EXPORT_SYMBOL(skb_append);
1880 EXPORT_SYMBOL(skb_split);
1881 EXPORT_SYMBOL(skb_prepare_seq_read);
1882 EXPORT_SYMBOL(skb_seq_read);
1883 EXPORT_SYMBOL(skb_abort_seq_read);
1884 EXPORT_SYMBOL(skb_find_text);
1885 EXPORT_SYMBOL(skb_append_datato_frags);