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;
343 secpath_get(skb->sp);
345 memcpy(n->cb, skb->cb, sizeof(skb->cb));
356 n->destructor = NULL;
357 #ifdef CONFIG_NETFILTER
360 nf_conntrack_get(skb->nfct);
362 #ifdef CONFIG_BRIDGE_NETFILTER
364 nf_bridge_get(skb->nf_bridge);
366 #endif /*CONFIG_NETFILTER*/
367 #ifdef CONFIG_NET_SCHED
369 #ifdef CONFIG_NET_CLS_ACT
370 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
371 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
372 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
378 atomic_set(&n->users, 1);
384 atomic_inc(&(skb_shinfo(skb)->dataref));
390 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
393 * Shift between the two data areas in bytes
395 unsigned long offset = new->data - old->data;
399 new->priority = old->priority;
400 new->protocol = old->protocol;
401 new->dst = dst_clone(old->dst);
403 new->sp = secpath_get(old->sp);
405 new->h.raw = old->h.raw + offset;
406 new->nh.raw = old->nh.raw + offset;
407 new->mac.raw = old->mac.raw + offset;
408 memcpy(new->cb, old->cb, sizeof(old->cb));
409 new->local_df = old->local_df;
410 new->pkt_type = old->pkt_type;
411 new->stamp = old->stamp;
412 new->destructor = NULL;
413 #ifdef CONFIG_NETFILTER
414 new->nfmark = old->nfmark;
415 new->nfct = old->nfct;
416 nf_conntrack_get(old->nfct);
417 new->nfctinfo = old->nfctinfo;
418 #ifdef CONFIG_BRIDGE_NETFILTER
419 new->nf_bridge = old->nf_bridge;
420 nf_bridge_get(old->nf_bridge);
423 #ifdef CONFIG_NET_SCHED
424 #ifdef CONFIG_NET_CLS_ACT
425 new->tc_verd = old->tc_verd;
427 new->tc_index = old->tc_index;
429 atomic_set(&new->users, 1);
430 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
431 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
435 * skb_copy - create private copy of an sk_buff
436 * @skb: buffer to copy
437 * @gfp_mask: allocation priority
439 * Make a copy of both an &sk_buff and its data. This is used when the
440 * caller wishes to modify the data and needs a private copy of the
441 * data to alter. Returns %NULL on failure or the pointer to the buffer
442 * on success. The returned buffer has a reference count of 1.
444 * As by-product this function converts non-linear &sk_buff to linear
445 * one, so that &sk_buff becomes completely private and caller is allowed
446 * to modify all the data of returned buffer. This means that this
447 * function is not recommended for use in circumstances when only
448 * header is going to be modified. Use pskb_copy() instead.
451 struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask)
453 int headerlen = skb->data - skb->head;
455 * Allocate the copy buffer
457 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
462 /* Set the data pointer */
463 skb_reserve(n, headerlen);
464 /* Set the tail pointer and length */
465 skb_put(n, skb->len);
467 n->ip_summed = skb->ip_summed;
469 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
472 copy_skb_header(n, skb);
478 * pskb_copy - create copy of an sk_buff with private head.
479 * @skb: buffer to copy
480 * @gfp_mask: allocation priority
482 * Make a copy of both an &sk_buff and part of its data, located
483 * in header. Fragmented data remain shared. This is used when
484 * the caller wishes to modify only header of &sk_buff and needs
485 * private copy of the header to alter. Returns %NULL on failure
486 * or the pointer to the buffer on success.
487 * The returned buffer has a reference count of 1.
490 struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask)
493 * Allocate the copy buffer
495 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
500 /* Set the data pointer */
501 skb_reserve(n, skb->data - skb->head);
502 /* Set the tail pointer and length */
503 skb_put(n, skb_headlen(skb));
505 memcpy(n->data, skb->data, n->len);
507 n->ip_summed = skb->ip_summed;
509 n->data_len = skb->data_len;
512 if (skb_shinfo(skb)->nr_frags) {
515 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
516 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
517 get_page(skb_shinfo(n)->frags[i].page);
519 skb_shinfo(n)->nr_frags = i;
522 if (skb_shinfo(skb)->frag_list) {
523 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
524 skb_clone_fraglist(n);
527 copy_skb_header(n, skb);
533 * pskb_expand_head - reallocate header of &sk_buff
534 * @skb: buffer to reallocate
535 * @nhead: room to add at head
536 * @ntail: room to add at tail
537 * @gfp_mask: allocation priority
539 * Expands (or creates identical copy, if &nhead and &ntail are zero)
540 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
541 * reference count of 1. Returns zero in the case of success or error,
542 * if expansion failed. In the last case, &sk_buff is not changed.
544 * All the pointers pointing into skb header may change and must be
545 * reloaded after call to this function.
548 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
549 unsigned int __nocast gfp_mask)
553 int size = nhead + (skb->end - skb->head) + ntail;
559 size = SKB_DATA_ALIGN(size);
561 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
565 /* Copy only real data... and, alas, header. This should be
566 * optimized for the cases when header is void. */
567 memcpy(data + nhead, skb->head, skb->tail - skb->head);
568 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
570 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
571 get_page(skb_shinfo(skb)->frags[i].page);
573 if (skb_shinfo(skb)->frag_list)
574 skb_clone_fraglist(skb);
576 skb_release_data(skb);
578 off = (data + nhead) - skb->head;
581 skb->end = data + size;
589 atomic_set(&skb_shinfo(skb)->dataref, 1);
596 /* Make private copy of skb with writable head and some headroom */
598 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
600 struct sk_buff *skb2;
601 int delta = headroom - skb_headroom(skb);
604 skb2 = pskb_copy(skb, GFP_ATOMIC);
606 skb2 = skb_clone(skb, GFP_ATOMIC);
607 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
618 * skb_copy_expand - copy and expand sk_buff
619 * @skb: buffer to copy
620 * @newheadroom: new free bytes at head
621 * @newtailroom: new free bytes at tail
622 * @gfp_mask: allocation priority
624 * Make a copy of both an &sk_buff and its data and while doing so
625 * allocate additional space.
627 * This is used when the caller wishes to modify the data and needs a
628 * private copy of the data to alter as well as more space for new fields.
629 * Returns %NULL on failure or the pointer to the buffer
630 * on success. The returned buffer has a reference count of 1.
632 * You must pass %GFP_ATOMIC as the allocation priority if this function
633 * is called from an interrupt.
635 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
636 * only by netfilter in the cases when checksum is recalculated? --ANK
638 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
639 int newheadroom, int newtailroom,
640 unsigned int __nocast gfp_mask)
643 * Allocate the copy buffer
645 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
647 int head_copy_len, head_copy_off;
652 skb_reserve(n, newheadroom);
654 /* Set the tail pointer and length */
655 skb_put(n, skb->len);
657 head_copy_len = skb_headroom(skb);
659 if (newheadroom <= head_copy_len)
660 head_copy_len = newheadroom;
662 head_copy_off = newheadroom - head_copy_len;
664 /* Copy the linear header and data. */
665 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
666 skb->len + head_copy_len))
669 copy_skb_header(n, skb);
675 * skb_pad - zero pad the tail of an skb
676 * @skb: buffer to pad
679 * Ensure that a buffer is followed by a padding area that is zero
680 * filled. Used by network drivers which may DMA or transfer data
681 * beyond the buffer end onto the wire.
683 * May return NULL in out of memory cases.
686 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
688 struct sk_buff *nskb;
690 /* If the skbuff is non linear tailroom is always zero.. */
691 if (skb_tailroom(skb) >= pad) {
692 memset(skb->data+skb->len, 0, pad);
696 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
699 memset(nskb->data+nskb->len, 0, pad);
703 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
704 * If realloc==0 and trimming is impossible without change of data,
708 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
710 int offset = skb_headlen(skb);
711 int nfrags = skb_shinfo(skb)->nr_frags;
714 for (i = 0; i < nfrags; i++) {
715 int end = offset + skb_shinfo(skb)->frags[i].size;
717 if (skb_cloned(skb)) {
720 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
724 put_page(skb_shinfo(skb)->frags[i].page);
725 skb_shinfo(skb)->nr_frags--;
727 skb_shinfo(skb)->frags[i].size = len - offset;
734 skb->data_len -= skb->len - len;
737 if (len <= skb_headlen(skb)) {
740 skb->tail = skb->data + len;
741 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
742 skb_drop_fraglist(skb);
744 skb->data_len -= skb->len - len;
753 * __pskb_pull_tail - advance tail of skb header
754 * @skb: buffer to reallocate
755 * @delta: number of bytes to advance tail
757 * The function makes a sense only on a fragmented &sk_buff,
758 * it expands header moving its tail forward and copying necessary
759 * data from fragmented part.
761 * &sk_buff MUST have reference count of 1.
763 * Returns %NULL (and &sk_buff does not change) if pull failed
764 * or value of new tail of skb in the case of success.
766 * All the pointers pointing into skb header may change and must be
767 * reloaded after call to this function.
770 /* Moves tail of skb head forward, copying data from fragmented part,
771 * when it is necessary.
772 * 1. It may fail due to malloc failure.
773 * 2. It may change skb pointers.
775 * It is pretty complicated. Luckily, it is called only in exceptional cases.
777 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
779 /* If skb has not enough free space at tail, get new one
780 * plus 128 bytes for future expansions. If we have enough
781 * room at tail, reallocate without expansion only if skb is cloned.
783 int i, k, eat = (skb->tail + delta) - skb->end;
785 if (eat > 0 || skb_cloned(skb)) {
786 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
791 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
794 /* Optimization: no fragments, no reasons to preestimate
795 * size of pulled pages. Superb.
797 if (!skb_shinfo(skb)->frag_list)
800 /* Estimate size of pulled pages. */
802 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
803 if (skb_shinfo(skb)->frags[i].size >= eat)
805 eat -= skb_shinfo(skb)->frags[i].size;
808 /* If we need update frag list, we are in troubles.
809 * Certainly, it possible to add an offset to skb data,
810 * but taking into account that pulling is expected to
811 * be very rare operation, it is worth to fight against
812 * further bloating skb head and crucify ourselves here instead.
813 * Pure masohism, indeed. 8)8)
816 struct sk_buff *list = skb_shinfo(skb)->frag_list;
817 struct sk_buff *clone = NULL;
818 struct sk_buff *insp = NULL;
824 if (list->len <= eat) {
825 /* Eaten as whole. */
830 /* Eaten partially. */
832 if (skb_shared(list)) {
833 /* Sucks! We need to fork list. :-( */
834 clone = skb_clone(list, GFP_ATOMIC);
840 /* This may be pulled without
844 if (!pskb_pull(list, eat)) {
853 /* Free pulled out fragments. */
854 while ((list = skb_shinfo(skb)->frag_list) != insp) {
855 skb_shinfo(skb)->frag_list = list->next;
858 /* And insert new clone at head. */
861 skb_shinfo(skb)->frag_list = clone;
864 /* Success! Now we may commit changes to skb data. */
869 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
870 if (skb_shinfo(skb)->frags[i].size <= eat) {
871 put_page(skb_shinfo(skb)->frags[i].page);
872 eat -= skb_shinfo(skb)->frags[i].size;
874 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
876 skb_shinfo(skb)->frags[k].page_offset += eat;
877 skb_shinfo(skb)->frags[k].size -= eat;
883 skb_shinfo(skb)->nr_frags = k;
886 skb->data_len -= delta;
891 /* Copy some data bits from skb to kernel buffer. */
893 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
896 int start = skb_headlen(skb);
898 if (offset > (int)skb->len - len)
902 if ((copy = start - offset) > 0) {
905 memcpy(to, skb->data + offset, copy);
906 if ((len -= copy) == 0)
912 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
915 BUG_TRAP(start <= offset + len);
917 end = start + skb_shinfo(skb)->frags[i].size;
918 if ((copy = end - offset) > 0) {
924 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
926 vaddr + skb_shinfo(skb)->frags[i].page_offset+
927 offset - start, copy);
928 kunmap_skb_frag(vaddr);
930 if ((len -= copy) == 0)
938 if (skb_shinfo(skb)->frag_list) {
939 struct sk_buff *list = skb_shinfo(skb)->frag_list;
941 for (; list; list = list->next) {
944 BUG_TRAP(start <= offset + len);
946 end = start + list->len;
947 if ((copy = end - offset) > 0) {
950 if (skb_copy_bits(list, offset - start,
953 if ((len -= copy) == 0)
969 * skb_store_bits - store bits from kernel buffer to skb
970 * @skb: destination buffer
971 * @offset: offset in destination
972 * @from: source buffer
973 * @len: number of bytes to copy
975 * Copy the specified number of bytes from the source buffer to the
976 * destination skb. This function handles all the messy bits of
977 * traversing fragment lists and such.
980 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
983 int start = skb_headlen(skb);
985 if (offset > (int)skb->len - len)
988 if ((copy = start - offset) > 0) {
991 memcpy(skb->data + offset, from, copy);
992 if ((len -= copy) == 0)
998 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
999 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1002 BUG_TRAP(start <= offset + len);
1004 end = start + frag->size;
1005 if ((copy = end - offset) > 0) {
1011 vaddr = kmap_skb_frag(frag);
1012 memcpy(vaddr + frag->page_offset + offset - start,
1014 kunmap_skb_frag(vaddr);
1016 if ((len -= copy) == 0)
1024 if (skb_shinfo(skb)->frag_list) {
1025 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1027 for (; list; list = list->next) {
1030 BUG_TRAP(start <= offset + len);
1032 end = start + list->len;
1033 if ((copy = end - offset) > 0) {
1036 if (skb_store_bits(list, offset - start,
1039 if ((len -= copy) == 0)
1054 EXPORT_SYMBOL(skb_store_bits);
1056 /* Checksum skb data. */
1058 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1059 int len, unsigned int csum)
1061 int start = skb_headlen(skb);
1062 int i, copy = start - offset;
1065 /* Checksum header. */
1069 csum = csum_partial(skb->data + offset, copy, csum);
1070 if ((len -= copy) == 0)
1076 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1079 BUG_TRAP(start <= offset + len);
1081 end = start + skb_shinfo(skb)->frags[i].size;
1082 if ((copy = end - offset) > 0) {
1085 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1089 vaddr = kmap_skb_frag(frag);
1090 csum2 = csum_partial(vaddr + frag->page_offset +
1091 offset - start, copy, 0);
1092 kunmap_skb_frag(vaddr);
1093 csum = csum_block_add(csum, csum2, pos);
1102 if (skb_shinfo(skb)->frag_list) {
1103 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1105 for (; list; list = list->next) {
1108 BUG_TRAP(start <= offset + len);
1110 end = start + list->len;
1111 if ((copy = end - offset) > 0) {
1115 csum2 = skb_checksum(list, offset - start,
1117 csum = csum_block_add(csum, csum2, pos);
1118 if ((len -= copy) == 0)
1132 /* Both of above in one bottle. */
1134 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1135 u8 *to, int len, unsigned int csum)
1137 int start = skb_headlen(skb);
1138 int i, copy = start - offset;
1145 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1147 if ((len -= copy) == 0)
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1157 BUG_TRAP(start <= offset + len);
1159 end = start + skb_shinfo(skb)->frags[i].size;
1160 if ((copy = end - offset) > 0) {
1163 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1167 vaddr = kmap_skb_frag(frag);
1168 csum2 = csum_partial_copy_nocheck(vaddr +
1172 kunmap_skb_frag(vaddr);
1173 csum = csum_block_add(csum, csum2, pos);
1183 if (skb_shinfo(skb)->frag_list) {
1184 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1186 for (; list; list = list->next) {
1190 BUG_TRAP(start <= offset + len);
1192 end = start + list->len;
1193 if ((copy = end - offset) > 0) {
1196 csum2 = skb_copy_and_csum_bits(list,
1199 csum = csum_block_add(csum, csum2, pos);
1200 if ((len -= copy) == 0)
1214 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1219 if (skb->ip_summed == CHECKSUM_HW)
1220 csstart = skb->h.raw - skb->data;
1222 csstart = skb_headlen(skb);
1224 if (csstart > skb_headlen(skb))
1227 memcpy(to, skb->data, csstart);
1230 if (csstart != skb->len)
1231 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1232 skb->len - csstart, 0);
1234 if (skb->ip_summed == CHECKSUM_HW) {
1235 long csstuff = csstart + skb->csum;
1237 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1242 * skb_dequeue - remove from the head of the queue
1243 * @list: list to dequeue from
1245 * Remove the head of the list. The list lock is taken so the function
1246 * may be used safely with other locking list functions. The head item is
1247 * returned or %NULL if the list is empty.
1250 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1252 unsigned long flags;
1253 struct sk_buff *result;
1255 spin_lock_irqsave(&list->lock, flags);
1256 result = __skb_dequeue(list);
1257 spin_unlock_irqrestore(&list->lock, flags);
1262 * skb_dequeue_tail - remove from the tail of the queue
1263 * @list: list to dequeue from
1265 * Remove the tail of the list. The list lock is taken so the function
1266 * may be used safely with other locking list functions. The tail item is
1267 * returned or %NULL if the list is empty.
1269 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1271 unsigned long flags;
1272 struct sk_buff *result;
1274 spin_lock_irqsave(&list->lock, flags);
1275 result = __skb_dequeue_tail(list);
1276 spin_unlock_irqrestore(&list->lock, flags);
1281 * skb_queue_purge - empty a list
1282 * @list: list to empty
1284 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1285 * the list and one reference dropped. This function takes the list
1286 * lock and is atomic with respect to other list locking functions.
1288 void skb_queue_purge(struct sk_buff_head *list)
1290 struct sk_buff *skb;
1291 while ((skb = skb_dequeue(list)) != NULL)
1296 * skb_queue_head - queue a buffer at the list head
1297 * @list: list to use
1298 * @newsk: buffer to queue
1300 * Queue a buffer at the start of the list. This function takes the
1301 * list lock and can be used safely with other locking &sk_buff functions
1304 * A buffer cannot be placed on two lists at the same time.
1306 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1308 unsigned long flags;
1310 spin_lock_irqsave(&list->lock, flags);
1311 __skb_queue_head(list, newsk);
1312 spin_unlock_irqrestore(&list->lock, flags);
1316 * skb_queue_tail - queue a buffer at the list tail
1317 * @list: list to use
1318 * @newsk: buffer to queue
1320 * Queue a buffer at the tail of the list. This function takes the
1321 * list lock and can be used safely with other locking &sk_buff functions
1324 * A buffer cannot be placed on two lists at the same time.
1326 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1328 unsigned long flags;
1330 spin_lock_irqsave(&list->lock, flags);
1331 __skb_queue_tail(list, newsk);
1332 spin_unlock_irqrestore(&list->lock, flags);
1336 * skb_unlink - remove a buffer from a list
1337 * @skb: buffer to remove
1338 * @list: list to use
1340 * Remove a packet from a list. The list locks are taken and this
1341 * function is atomic with respect to other list locked calls
1343 * You must know what list the SKB is on.
1345 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1347 unsigned long flags;
1349 spin_lock_irqsave(&list->lock, flags);
1350 __skb_unlink(skb, list);
1351 spin_unlock_irqrestore(&list->lock, flags);
1355 * skb_append - append a buffer
1356 * @old: buffer to insert after
1357 * @newsk: buffer to insert
1358 * @list: list to use
1360 * Place a packet after a given packet in a list. The list locks are taken
1361 * and this function is atomic with respect to other list locked calls.
1362 * A buffer cannot be placed on two lists at the same time.
1364 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1366 unsigned long flags;
1368 spin_lock_irqsave(&list->lock, flags);
1369 __skb_append(old, newsk, list);
1370 spin_unlock_irqrestore(&list->lock, flags);
1375 * skb_insert - insert a buffer
1376 * @old: buffer to insert before
1377 * @newsk: buffer to insert
1378 * @list: list to use
1380 * Place a packet before a given packet in a list. The list locks are
1381 * taken and this function is atomic with respect to other list locked
1384 * A buffer cannot be placed on two lists at the same time.
1386 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1388 unsigned long flags;
1390 spin_lock_irqsave(&list->lock, flags);
1391 __skb_insert(newsk, old->prev, old, list);
1392 spin_unlock_irqrestore(&list->lock, flags);
1397 * Tune the memory allocator for a new MTU size.
1399 void skb_add_mtu(int mtu)
1401 /* Must match allocation in alloc_skb */
1402 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1404 kmem_add_cache_size(mtu);
1408 static inline void skb_split_inside_header(struct sk_buff *skb,
1409 struct sk_buff* skb1,
1410 const u32 len, const int pos)
1414 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1416 /* And move data appendix as is. */
1417 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1418 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1420 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1421 skb_shinfo(skb)->nr_frags = 0;
1422 skb1->data_len = skb->data_len;
1423 skb1->len += skb1->data_len;
1426 skb->tail = skb->data + len;
1429 static inline void skb_split_no_header(struct sk_buff *skb,
1430 struct sk_buff* skb1,
1431 const u32 len, int pos)
1434 const int nfrags = skb_shinfo(skb)->nr_frags;
1436 skb_shinfo(skb)->nr_frags = 0;
1437 skb1->len = skb1->data_len = skb->len - len;
1439 skb->data_len = len - pos;
1441 for (i = 0; i < nfrags; i++) {
1442 int size = skb_shinfo(skb)->frags[i].size;
1444 if (pos + size > len) {
1445 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1449 * We have two variants in this case:
1450 * 1. Move all the frag to the second
1451 * part, if it is possible. F.e.
1452 * this approach is mandatory for TUX,
1453 * where splitting is expensive.
1454 * 2. Split is accurately. We make this.
1456 get_page(skb_shinfo(skb)->frags[i].page);
1457 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1458 skb_shinfo(skb1)->frags[0].size -= len - pos;
1459 skb_shinfo(skb)->frags[i].size = len - pos;
1460 skb_shinfo(skb)->nr_frags++;
1464 skb_shinfo(skb)->nr_frags++;
1467 skb_shinfo(skb1)->nr_frags = k;
1471 * skb_split - Split fragmented skb to two parts at length len.
1472 * @skb: the buffer to split
1473 * @skb1: the buffer to receive the second part
1474 * @len: new length for skb
1476 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1478 int pos = skb_headlen(skb);
1480 if (len < pos) /* Split line is inside header. */
1481 skb_split_inside_header(skb, skb1, len, pos);
1482 else /* Second chunk has no header, nothing to copy. */
1483 skb_split_no_header(skb, skb1, len, pos);
1487 * skb_prepare_seq_read - Prepare a sequential read of skb data
1488 * @skb: the buffer to read
1489 * @from: lower offset of data to be read
1490 * @to: upper offset of data to be read
1491 * @st: state variable
1493 * Initializes the specified state variable. Must be called before
1494 * invoking skb_seq_read() for the first time.
1496 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1497 unsigned int to, struct skb_seq_state *st)
1499 st->lower_offset = from;
1500 st->upper_offset = to;
1501 st->root_skb = st->cur_skb = skb;
1502 st->frag_idx = st->stepped_offset = 0;
1503 st->frag_data = NULL;
1507 * skb_seq_read - Sequentially read skb data
1508 * @consumed: number of bytes consumed by the caller so far
1509 * @data: destination pointer for data to be returned
1510 * @st: state variable
1512 * Reads a block of skb data at &consumed relative to the
1513 * lower offset specified to skb_prepare_seq_read(). Assigns
1514 * the head of the data block to &data and returns the length
1515 * of the block or 0 if the end of the skb data or the upper
1516 * offset has been reached.
1518 * The caller is not required to consume all of the data
1519 * returned, i.e. &consumed is typically set to the number
1520 * of bytes already consumed and the next call to
1521 * skb_seq_read() will return the remaining part of the block.
1523 * Note: The size of each block of data returned can be arbitary,
1524 * this limitation is the cost for zerocopy seqeuental
1525 * reads of potentially non linear data.
1527 * Note: Fragment lists within fragments are not implemented
1528 * at the moment, state->root_skb could be replaced with
1529 * a stack for this purpose.
1531 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1532 struct skb_seq_state *st)
1534 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1537 if (unlikely(abs_offset >= st->upper_offset))
1541 block_limit = skb_headlen(st->cur_skb);
1543 if (abs_offset < block_limit) {
1544 *data = st->cur_skb->data + abs_offset;
1545 return block_limit - abs_offset;
1548 if (st->frag_idx == 0 && !st->frag_data)
1549 st->stepped_offset += skb_headlen(st->cur_skb);
1551 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1552 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1553 block_limit = frag->size + st->stepped_offset;
1555 if (abs_offset < block_limit) {
1557 st->frag_data = kmap_skb_frag(frag);
1559 *data = (u8 *) st->frag_data + frag->page_offset +
1560 (abs_offset - st->stepped_offset);
1562 return block_limit - abs_offset;
1565 if (st->frag_data) {
1566 kunmap_skb_frag(st->frag_data);
1567 st->frag_data = NULL;
1571 st->stepped_offset += frag->size;
1574 if (st->cur_skb->next) {
1575 st->cur_skb = st->cur_skb->next;
1578 } else if (st->root_skb == st->cur_skb &&
1579 skb_shinfo(st->root_skb)->frag_list) {
1580 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1588 * skb_abort_seq_read - Abort a sequential read of skb data
1589 * @st: state variable
1591 * Must be called if skb_seq_read() was not called until it
1594 void skb_abort_seq_read(struct skb_seq_state *st)
1597 kunmap_skb_frag(st->frag_data);
1600 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1602 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1603 struct ts_config *conf,
1604 struct ts_state *state)
1606 return skb_seq_read(offset, text, TS_SKB_CB(state));
1609 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1611 skb_abort_seq_read(TS_SKB_CB(state));
1615 * skb_find_text - Find a text pattern in skb data
1616 * @skb: the buffer to look in
1617 * @from: search offset
1619 * @config: textsearch configuration
1620 * @state: uninitialized textsearch state variable
1622 * Finds a pattern in the skb data according to the specified
1623 * textsearch configuration. Use textsearch_next() to retrieve
1624 * subsequent occurrences of the pattern. Returns the offset
1625 * to the first occurrence or UINT_MAX if no match was found.
1627 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1628 unsigned int to, struct ts_config *config,
1629 struct ts_state *state)
1631 config->get_next_block = skb_ts_get_next_block;
1632 config->finish = skb_ts_finish;
1634 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1636 return textsearch_find(config, state);
1639 void __init skb_init(void)
1641 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1642 sizeof(struct sk_buff),
1646 if (!skbuff_head_cache)
1647 panic("cannot create skbuff cache");
1650 EXPORT_SYMBOL(___pskb_trim);
1651 EXPORT_SYMBOL(__kfree_skb);
1652 EXPORT_SYMBOL(__pskb_pull_tail);
1653 EXPORT_SYMBOL(alloc_skb);
1654 EXPORT_SYMBOL(pskb_copy);
1655 EXPORT_SYMBOL(pskb_expand_head);
1656 EXPORT_SYMBOL(skb_checksum);
1657 EXPORT_SYMBOL(skb_clone);
1658 EXPORT_SYMBOL(skb_clone_fraglist);
1659 EXPORT_SYMBOL(skb_copy);
1660 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1661 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1662 EXPORT_SYMBOL(skb_copy_bits);
1663 EXPORT_SYMBOL(skb_copy_expand);
1664 EXPORT_SYMBOL(skb_over_panic);
1665 EXPORT_SYMBOL(skb_pad);
1666 EXPORT_SYMBOL(skb_realloc_headroom);
1667 EXPORT_SYMBOL(skb_under_panic);
1668 EXPORT_SYMBOL(skb_dequeue);
1669 EXPORT_SYMBOL(skb_dequeue_tail);
1670 EXPORT_SYMBOL(skb_insert);
1671 EXPORT_SYMBOL(skb_queue_purge);
1672 EXPORT_SYMBOL(skb_queue_head);
1673 EXPORT_SYMBOL(skb_queue_tail);
1674 EXPORT_SYMBOL(skb_unlink);
1675 EXPORT_SYMBOL(skb_append);
1676 EXPORT_SYMBOL(skb_split);
1677 EXPORT_SYMBOL(skb_prepare_seq_read);
1678 EXPORT_SYMBOL(skb_seq_read);
1679 EXPORT_SYMBOL(skb_abort_seq_read);
1680 EXPORT_SYMBOL(skb_find_text);