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 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/cache.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/init.h>
57 #include <linux/highmem.h>
59 #include <net/protocol.h>
62 #include <net/checksum.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
68 static kmem_cache_t *skbuff_head_cache;
71 * Keep out-of-line to prevent kernel bloat.
72 * __builtin_return_address is not used because it is not always
77 * skb_over_panic - private function
82 * Out of line support code for skb_put(). Not user callable.
84 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
86 printk(KERN_INFO "skput:over: %p:%d put:%d dev:%s",
87 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
92 * skb_under_panic - private function
97 * Out of line support code for skb_push(). Not user callable.
100 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
102 printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s",
103 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
107 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
108 * 'private' fields and also do memory statistics to find all the
114 * alloc_skb - allocate a network buffer
115 * @size: size to allocate
116 * @gfp_mask: allocation mask
118 * Allocate a new &sk_buff. The returned buffer has no headroom and a
119 * tail room of size bytes. The object has a reference count of one.
120 * The return is the buffer. On a failure the return is %NULL.
122 * Buffers may only be allocated from interrupts using a @gfp_mask of
125 struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
131 skb = kmem_cache_alloc(skbuff_head_cache,
132 gfp_mask & ~__GFP_DMA);
136 /* Get the DATA. Size must match skb_add_mtu(). */
137 size = SKB_DATA_ALIGN(size);
138 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
142 memset(skb, 0, offsetof(struct sk_buff, truesize));
143 skb->truesize = size + sizeof(struct sk_buff);
144 atomic_set(&skb->users, 1);
148 skb->end = data + size;
150 atomic_set(&(skb_shinfo(skb)->dataref), 1);
151 skb_shinfo(skb)->nr_frags = 0;
152 skb_shinfo(skb)->tso_size = 0;
153 skb_shinfo(skb)->tso_segs = 0;
154 skb_shinfo(skb)->frag_list = NULL;
158 kmem_cache_free(skbuff_head_cache, skb);
164 static void skb_drop_fraglist(struct sk_buff *skb)
166 struct sk_buff *list = skb_shinfo(skb)->frag_list;
168 skb_shinfo(skb)->frag_list = NULL;
171 struct sk_buff *this = list;
177 static void skb_clone_fraglist(struct sk_buff *skb)
179 struct sk_buff *list;
181 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
185 void skb_release_data(struct sk_buff *skb)
188 atomic_dec_and_test(&(skb_shinfo(skb)->dataref))) {
189 if (skb_shinfo(skb)->nr_frags) {
191 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
192 put_page(skb_shinfo(skb)->frags[i].page);
195 if (skb_shinfo(skb)->frag_list)
196 skb_drop_fraglist(skb);
203 * Free an skbuff by memory without cleaning the state.
205 void kfree_skbmem(struct sk_buff *skb)
207 skb_release_data(skb);
208 kmem_cache_free(skbuff_head_cache, skb);
212 * __kfree_skb - private function
215 * Free an sk_buff. Release anything attached to the buffer.
216 * Clean the state. This is an internal helper function. Users should
217 * always call kfree_skb
220 void __kfree_skb(struct sk_buff *skb)
223 printk(KERN_WARNING "Warning: kfree_skb passed an skb still "
224 "on a list (from %p).\n", NET_CALLER(skb));
228 dst_release(skb->dst);
230 secpath_put(skb->sp);
232 if(skb->destructor) {
234 printk(KERN_WARNING "Warning: kfree_skb on "
235 "hard IRQ %p\n", NET_CALLER(skb));
236 skb->destructor(skb);
238 #ifdef CONFIG_NETFILTER
239 nf_conntrack_put(skb->nfct);
240 #ifdef CONFIG_BRIDGE_NETFILTER
241 nf_bridge_put(skb->nf_bridge);
248 * skb_clone - duplicate an sk_buff
249 * @skb: buffer to clone
250 * @gfp_mask: allocation priority
252 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
253 * copies share the same packet data but not structure. The new
254 * buffer has a reference count of 1. If the allocation fails the
255 * function returns %NULL otherwise the new buffer is returned.
257 * If this function is called from an interrupt gfp_mask() must be
261 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
263 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
268 #define C(x) n->x = skb->x
270 n->next = n->prev = NULL;
283 secpath_get(skb->sp);
285 memcpy(n->cb, skb->cb, sizeof(skb->cb));
296 n->destructor = NULL;
297 #ifdef CONFIG_NETFILTER
301 nf_conntrack_get(skb->nfct);
302 #ifdef CONFIG_NETFILTER_DEBUG
305 #ifdef CONFIG_BRIDGE_NETFILTER
307 nf_bridge_get(skb->nf_bridge);
309 #endif /*CONFIG_NETFILTER*/
310 #if defined(CONFIG_HIPPI)
313 #ifdef CONFIG_NET_SCHED
317 atomic_set(&n->users, 1);
323 atomic_inc(&(skb_shinfo(skb)->dataref));
329 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
332 * Shift between the two data areas in bytes
334 unsigned long offset = new->data - old->data;
339 new->real_dev = old->real_dev;
340 new->priority = old->priority;
341 new->protocol = old->protocol;
342 new->dst = dst_clone(old->dst);
344 new->sp = secpath_get(old->sp);
346 new->h.raw = old->h.raw + offset;
347 new->nh.raw = old->nh.raw + offset;
348 new->mac.raw = old->mac.raw + offset;
349 memcpy(new->cb, old->cb, sizeof(old->cb));
350 new->local_df = old->local_df;
351 new->pkt_type = old->pkt_type;
352 new->stamp = old->stamp;
353 new->destructor = NULL;
354 new->security = old->security;
355 #ifdef CONFIG_NETFILTER
356 new->nfmark = old->nfmark;
357 new->nfcache = old->nfcache;
358 new->nfct = old->nfct;
359 nf_conntrack_get(old->nfct);
360 #ifdef CONFIG_NETFILTER_DEBUG
361 new->nf_debug = old->nf_debug;
363 #ifdef CONFIG_BRIDGE_NETFILTER
364 new->nf_bridge = old->nf_bridge;
365 nf_bridge_get(old->nf_bridge);
368 #ifdef CONFIG_NET_SCHED
369 new->tc_index = old->tc_index;
371 atomic_set(&new->users, 1);
375 * skb_copy - create private copy of an sk_buff
376 * @skb: buffer to copy
377 * @gfp_mask: allocation priority
379 * Make a copy of both an &sk_buff and its data. This is used when the
380 * caller wishes to modify the data and needs a private copy of the
381 * data to alter. Returns %NULL on failure or the pointer to the buffer
382 * on success. The returned buffer has a reference count of 1.
384 * As by-product this function converts non-linear &sk_buff to linear
385 * one, so that &sk_buff becomes completely private and caller is allowed
386 * to modify all the data of returned buffer. This means that this
387 * function is not recommended for use in circumstances when only
388 * header is going to be modified. Use pskb_copy() instead.
391 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
393 int headerlen = skb->data - skb->head;
395 * Allocate the copy buffer
397 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
402 /* Set the data pointer */
403 skb_reserve(n, headerlen);
404 /* Set the tail pointer and length */
405 skb_put(n, skb->len);
407 n->ip_summed = skb->ip_summed;
409 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
412 copy_skb_header(n, skb);
418 * pskb_copy - create copy of an sk_buff with private head.
419 * @skb: buffer to copy
420 * @gfp_mask: allocation priority
422 * Make a copy of both an &sk_buff and part of its data, located
423 * in header. Fragmented data remain shared. This is used when
424 * the caller wishes to modify only header of &sk_buff and needs
425 * private copy of the header to alter. Returns %NULL on failure
426 * or the pointer to the buffer on success.
427 * The returned buffer has a reference count of 1.
430 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
433 * Allocate the copy buffer
435 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
440 /* Set the data pointer */
441 skb_reserve(n, skb->data - skb->head);
442 /* Set the tail pointer and length */
443 skb_put(n, skb_headlen(skb));
445 memcpy(n->data, skb->data, n->len);
447 n->ip_summed = skb->ip_summed;
449 n->data_len = skb->data_len;
452 if (skb_shinfo(skb)->nr_frags) {
455 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
456 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
457 get_page(skb_shinfo(n)->frags[i].page);
459 skb_shinfo(n)->nr_frags = i;
461 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
462 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
464 if (skb_shinfo(skb)->frag_list) {
465 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
466 skb_clone_fraglist(n);
469 copy_skb_header(n, skb);
475 * pskb_expand_head - reallocate header of &sk_buff
476 * @skb: buffer to reallocate
477 * @nhead: room to add at head
478 * @ntail: room to add at tail
479 * @gfp_mask: allocation priority
481 * Expands (or creates identical copy, if &nhead and &ntail are zero)
482 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
483 * reference count of 1. Returns zero in the case of success or error,
484 * if expansion failed. In the last case, &sk_buff is not changed.
486 * All the pointers pointing into skb header may change and must be
487 * reloaded after call to this function.
490 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
494 int size = nhead + (skb->end - skb->head) + ntail;
500 size = SKB_DATA_ALIGN(size);
502 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
506 /* Copy only real data... and, alas, header. This should be
507 * optimized for the cases when header is void. */
508 memcpy(data + nhead, skb->head, skb->tail - skb->head);
509 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
511 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
512 get_page(skb_shinfo(skb)->frags[i].page);
514 if (skb_shinfo(skb)->frag_list)
515 skb_clone_fraglist(skb);
517 skb_release_data(skb);
519 off = (data + nhead) - skb->head;
522 skb->end = data + size;
529 atomic_set(&skb_shinfo(skb)->dataref, 1);
536 /* Make private copy of skb with writable head and some headroom */
538 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
540 struct sk_buff *skb2;
541 int delta = headroom - skb_headroom(skb);
544 skb2 = pskb_copy(skb, GFP_ATOMIC);
546 skb2 = skb_clone(skb, GFP_ATOMIC);
547 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
558 * skb_copy_expand - copy and expand sk_buff
559 * @skb: buffer to copy
560 * @newheadroom: new free bytes at head
561 * @newtailroom: new free bytes at tail
562 * @gfp_mask: allocation priority
564 * Make a copy of both an &sk_buff and its data and while doing so
565 * allocate additional space.
567 * This is used when the caller wishes to modify the data and needs a
568 * private copy of the data to alter as well as more space for new fields.
569 * Returns %NULL on failure or the pointer to the buffer
570 * on success. The returned buffer has a reference count of 1.
572 * You must pass %GFP_ATOMIC as the allocation priority if this function
573 * is called from an interrupt.
575 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
576 * only by netfilter in the cases when checksum is recalculated? --ANK
578 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
579 int newheadroom, int newtailroom, int gfp_mask)
582 * Allocate the copy buffer
584 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
586 int head_copy_len, head_copy_off;
591 skb_reserve(n, newheadroom);
593 /* Set the tail pointer and length */
594 skb_put(n, skb->len);
596 head_copy_len = skb_headroom(skb);
598 if (newheadroom <= head_copy_len)
599 head_copy_len = newheadroom;
601 head_copy_off = newheadroom - head_copy_len;
603 /* Copy the linear header and data. */
604 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
605 skb->len + head_copy_len))
608 copy_skb_header(n, skb);
609 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
610 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
616 * skb_pad - zero pad the tail of an skb
617 * @skb: buffer to pad
620 * Ensure that a buffer is followed by a padding area that is zero
621 * filled. Used by network drivers which may DMA or transfer data
622 * beyond the buffer end onto the wire.
624 * May return NULL in out of memory cases.
627 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
629 struct sk_buff *nskb;
631 /* If the skbuff is non linear tailroom is always zero.. */
632 if (skb_tailroom(skb) >= pad) {
633 memset(skb->data+skb->len, 0, pad);
637 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
640 memset(nskb->data+nskb->len, 0, pad);
644 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
645 * If realloc==0 and trimming is impossible without change of data,
649 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
651 int offset = skb_headlen(skb);
652 int nfrags = skb_shinfo(skb)->nr_frags;
655 for (i = 0; i < nfrags; i++) {
656 int end = offset + skb_shinfo(skb)->frags[i].size;
658 if (skb_cloned(skb)) {
661 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
665 put_page(skb_shinfo(skb)->frags[i].page);
666 skb_shinfo(skb)->nr_frags--;
668 skb_shinfo(skb)->frags[i].size = len - offset;
675 skb->data_len -= skb->len - len;
678 if (len <= skb_headlen(skb)) {
681 skb->tail = skb->data + len;
682 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
683 skb_drop_fraglist(skb);
685 skb->data_len -= skb->len - len;
694 * __pskb_pull_tail - advance tail of skb header
695 * @skb: buffer to reallocate
696 * @delta: number of bytes to advance tail
698 * The function makes a sense only on a fragmented &sk_buff,
699 * it expands header moving its tail forward and copying necessary
700 * data from fragmented part.
702 * &sk_buff MUST have reference count of 1.
704 * Returns %NULL (and &sk_buff does not change) if pull failed
705 * or value of new tail of skb in the case of success.
707 * All the pointers pointing into skb header may change and must be
708 * reloaded after call to this function.
711 /* Moves tail of skb head forward, copying data from fragmented part,
712 * when it is necessary.
713 * 1. It may fail due to malloc failure.
714 * 2. It may change skb pointers.
716 * It is pretty complicated. Luckily, it is called only in exceptional cases.
718 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
720 /* If skb has not enough free space at tail, get new one
721 * plus 128 bytes for future expansions. If we have enough
722 * room at tail, reallocate without expansion only if skb is cloned.
724 int i, k, eat = (skb->tail + delta) - skb->end;
726 if (eat > 0 || skb_cloned(skb)) {
727 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
732 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
735 /* Optimization: no fragments, no reasons to preestimate
736 * size of pulled pages. Superb.
738 if (!skb_shinfo(skb)->frag_list)
741 /* Estimate size of pulled pages. */
743 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
744 if (skb_shinfo(skb)->frags[i].size >= eat)
746 eat -= skb_shinfo(skb)->frags[i].size;
749 /* If we need update frag list, we are in troubles.
750 * Certainly, it possible to add an offset to skb data,
751 * but taking into account that pulling is expected to
752 * be very rare operation, it is worth to fight against
753 * further bloating skb head and crucify ourselves here instead.
754 * Pure masohism, indeed. 8)8)
757 struct sk_buff *list = skb_shinfo(skb)->frag_list;
758 struct sk_buff *clone = NULL;
759 struct sk_buff *insp = NULL;
765 if (list->len <= eat) {
766 /* Eaten as whole. */
771 /* Eaten partially. */
773 if (skb_shared(list)) {
774 /* Sucks! We need to fork list. :-( */
775 clone = skb_clone(list, GFP_ATOMIC);
781 /* This may be pulled without
785 if (!pskb_pull(list, eat)) {
794 /* Free pulled out fragments. */
795 while ((list = skb_shinfo(skb)->frag_list) != insp) {
796 skb_shinfo(skb)->frag_list = list->next;
799 /* And insert new clone at head. */
802 skb_shinfo(skb)->frag_list = clone;
805 /* Success! Now we may commit changes to skb data. */
810 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
811 if (skb_shinfo(skb)->frags[i].size <= eat) {
812 put_page(skb_shinfo(skb)->frags[i].page);
813 eat -= skb_shinfo(skb)->frags[i].size;
815 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
817 skb_shinfo(skb)->frags[k].page_offset += eat;
818 skb_shinfo(skb)->frags[k].size -= eat;
824 skb_shinfo(skb)->nr_frags = k;
827 skb->data_len -= delta;
832 /* Copy some data bits from skb to kernel buffer. */
834 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
837 int start = skb_headlen(skb);
839 if (offset > (int)skb->len - len)
843 if ((copy = start - offset) > 0) {
846 memcpy(to, skb->data + offset, copy);
847 if ((len -= copy) == 0)
853 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
856 BUG_TRAP(start <= offset + len);
858 end = start + skb_shinfo(skb)->frags[i].size;
859 if ((copy = end - offset) > 0) {
865 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
867 vaddr + skb_shinfo(skb)->frags[i].page_offset+
868 offset - start, copy);
869 kunmap_skb_frag(vaddr);
871 if ((len -= copy) == 0)
879 if (skb_shinfo(skb)->frag_list) {
880 struct sk_buff *list = skb_shinfo(skb)->frag_list;
882 for (; list; list = list->next) {
885 BUG_TRAP(start <= offset + len);
887 end = start + list->len;
888 if ((copy = end - offset) > 0) {
891 if (skb_copy_bits(list, offset - start,
894 if ((len -= copy) == 0)
909 /* Checksum skb data. */
911 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
912 int len, unsigned int csum)
914 int start = skb_headlen(skb);
915 int i, copy = start - offset;
918 /* Checksum header. */
922 csum = csum_partial(skb->data + offset, copy, csum);
923 if ((len -= copy) == 0)
929 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
932 BUG_TRAP(start <= offset + len);
934 end = start + skb_shinfo(skb)->frags[i].size;
935 if ((copy = end - offset) > 0) {
938 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
942 vaddr = kmap_skb_frag(frag);
943 csum2 = csum_partial(vaddr + frag->page_offset +
944 offset - start, copy, 0);
945 kunmap_skb_frag(vaddr);
946 csum = csum_block_add(csum, csum2, pos);
955 if (skb_shinfo(skb)->frag_list) {
956 struct sk_buff *list = skb_shinfo(skb)->frag_list;
958 for (; list; list = list->next) {
961 BUG_TRAP(start <= offset + len);
963 end = start + list->len;
964 if ((copy = end - offset) > 0) {
968 csum2 = skb_checksum(list, offset - start,
970 csum = csum_block_add(csum, csum2, pos);
971 if ((len -= copy) == 0)
985 /* Both of above in one bottle. */
987 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
988 u8 *to, int len, unsigned int csum)
990 int start = skb_headlen(skb);
991 int i, copy = start - offset;
998 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1000 if ((len -= copy) == 0)
1007 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1010 BUG_TRAP(start <= offset + len);
1012 end = start + skb_shinfo(skb)->frags[i].size;
1013 if ((copy = end - offset) > 0) {
1016 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1020 vaddr = kmap_skb_frag(frag);
1021 csum2 = csum_partial_copy_nocheck(vaddr +
1025 kunmap_skb_frag(vaddr);
1026 csum = csum_block_add(csum, csum2, pos);
1036 if (skb_shinfo(skb)->frag_list) {
1037 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1039 for (; list; list = list->next) {
1043 BUG_TRAP(start <= offset + len);
1045 end = start + list->len;
1046 if ((copy = end - offset) > 0) {
1049 csum2 = skb_copy_and_csum_bits(list,
1052 csum = csum_block_add(csum, csum2, pos);
1053 if ((len -= copy) == 0)
1067 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1072 if (skb->ip_summed == CHECKSUM_HW)
1073 csstart = skb->h.raw - skb->data;
1075 csstart = skb_headlen(skb);
1077 if (csstart > skb_headlen(skb))
1080 memcpy(to, skb->data, csstart);
1083 if (csstart != skb->len)
1084 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1085 skb->len - csstart, 0);
1087 if (skb->ip_summed == CHECKSUM_HW) {
1088 long csstuff = csstart + skb->csum;
1090 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1095 * skb_dequeue - remove from the head of the queue
1096 * @list: list to dequeue from
1098 * Remove the head of the list. The list lock is taken so the function
1099 * may be used safely with other locking list functions. The head item is
1100 * returned or %NULL if the list is empty.
1103 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1105 unsigned long flags;
1106 struct sk_buff *result;
1108 spin_lock_irqsave(&list->lock, flags);
1109 result = __skb_dequeue(list);
1110 spin_unlock_irqrestore(&list->lock, flags);
1115 * skb_dequeue_tail - remove from the tail of the queue
1116 * @list: list to dequeue from
1118 * Remove the tail of the list. The list lock is taken so the function
1119 * may be used safely with other locking list functions. The tail item is
1120 * returned or %NULL if the list is empty.
1122 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1124 unsigned long flags;
1125 struct sk_buff *result;
1127 spin_lock_irqsave(&list->lock, flags);
1128 result = __skb_dequeue_tail(list);
1129 spin_unlock_irqrestore(&list->lock, flags);
1134 * skb_queue_purge - empty a list
1135 * @list: list to empty
1137 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1138 * the list and one reference dropped. This function takes the list
1139 * lock and is atomic with respect to other list locking functions.
1141 void skb_queue_purge(struct sk_buff_head *list)
1143 struct sk_buff *skb;
1144 while ((skb = skb_dequeue(list)) != NULL)
1149 * skb_queue_head - queue a buffer at the list head
1150 * @list: list to use
1151 * @newsk: buffer to queue
1153 * Queue a buffer at the start of the list. This function takes the
1154 * list lock and can be used safely with other locking &sk_buff functions
1157 * A buffer cannot be placed on two lists at the same time.
1159 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1161 unsigned long flags;
1163 spin_lock_irqsave(&list->lock, flags);
1164 __skb_queue_head(list, newsk);
1165 spin_unlock_irqrestore(&list->lock, flags);
1169 * skb_queue_tail - queue a buffer at the list tail
1170 * @list: list to use
1171 * @newsk: buffer to queue
1173 * Queue a buffer at the tail of the list. This function takes the
1174 * list lock and can be used safely with other locking &sk_buff functions
1177 * A buffer cannot be placed on two lists at the same time.
1179 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1181 unsigned long flags;
1183 spin_lock_irqsave(&list->lock, flags);
1184 __skb_queue_tail(list, newsk);
1185 spin_unlock_irqrestore(&list->lock, flags);
1188 * skb_unlink - remove a buffer from a list
1189 * @skb: buffer to remove
1191 * Place a packet after a given packet in a list. The list locks are taken
1192 * and this function is atomic with respect to other list locked calls
1194 * Works even without knowing the list it is sitting on, which can be
1195 * handy at times. It also means that THE LIST MUST EXIST when you
1196 * unlink. Thus a list must have its contents unlinked before it is
1199 void skb_unlink(struct sk_buff *skb)
1201 struct sk_buff_head *list = skb->list;
1204 unsigned long flags;
1206 spin_lock_irqsave(&list->lock, flags);
1207 if (skb->list == list)
1208 __skb_unlink(skb, skb->list);
1209 spin_unlock_irqrestore(&list->lock, flags);
1215 * skb_append - append a buffer
1216 * @old: buffer to insert after
1217 * @newsk: buffer to insert
1219 * Place a packet after a given packet in a list. The list locks are taken
1220 * and this function is atomic with respect to other list locked calls.
1221 * A buffer cannot be placed on two lists at the same time.
1224 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1226 unsigned long flags;
1228 spin_lock_irqsave(&old->list->lock, flags);
1229 __skb_append(old, newsk);
1230 spin_unlock_irqrestore(&old->list->lock, flags);
1235 * skb_insert - insert a buffer
1236 * @old: buffer to insert before
1237 * @newsk: buffer to insert
1239 * Place a packet before a given packet in a list. The list locks are taken
1240 * and this function is atomic with respect to other list locked calls
1241 * A buffer cannot be placed on two lists at the same time.
1244 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1246 unsigned long flags;
1248 spin_lock_irqsave(&old->list->lock, flags);
1249 __skb_insert(newsk, old->prev, old, old->list);
1250 spin_unlock_irqrestore(&old->list->lock, flags);
1255 * Tune the memory allocator for a new MTU size.
1257 void skb_add_mtu(int mtu)
1259 /* Must match allocation in alloc_skb */
1260 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1262 kmem_add_cache_size(mtu);
1266 void __init skb_init(void)
1268 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1269 sizeof(struct sk_buff),
1273 if (!skbuff_head_cache)
1274 panic("cannot create skbuff cache");
1277 EXPORT_SYMBOL(___pskb_trim);
1278 EXPORT_SYMBOL(__kfree_skb);
1279 EXPORT_SYMBOL(__pskb_pull_tail);
1280 EXPORT_SYMBOL(alloc_skb);
1281 EXPORT_SYMBOL(pskb_copy);
1282 EXPORT_SYMBOL(pskb_expand_head);
1283 EXPORT_SYMBOL(skb_checksum);
1284 EXPORT_SYMBOL(skb_clone);
1285 EXPORT_SYMBOL(skb_clone_fraglist);
1286 EXPORT_SYMBOL(skb_copy);
1287 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1288 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1289 EXPORT_SYMBOL(skb_copy_bits);
1290 EXPORT_SYMBOL(skb_copy_expand);
1291 EXPORT_SYMBOL(skb_over_panic);
1292 EXPORT_SYMBOL(skb_pad);
1293 EXPORT_SYMBOL(skb_realloc_headroom);
1294 EXPORT_SYMBOL(skb_under_panic);
1295 EXPORT_SYMBOL(skb_dequeue);
1296 EXPORT_SYMBOL(skb_dequeue_tail);
1297 EXPORT_SYMBOL(skb_insert);
1298 EXPORT_SYMBOL(skb_queue_purge);
1299 EXPORT_SYMBOL(skb_queue_head);
1300 EXPORT_SYMBOL(skb_queue_tail);
1301 EXPORT_SYMBOL(skb_unlink);
1302 EXPORT_SYMBOL(skb_append);