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_INFO "skput:over: %p:%d put:%d dev:%s",
90 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
95 * skb_under_panic - private function
100 * Out of line support code for skb_push(). Not user callable.
103 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
105 printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s",
106 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
110 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
111 * 'private' fields and also do memory statistics to find all the
117 * alloc_skb - allocate a network buffer
118 * @size: size to allocate
119 * @gfp_mask: allocation mask
121 * Allocate a new &sk_buff. The returned buffer has no headroom and a
122 * tail room of size bytes. The object has a reference count of one.
123 * The return is the buffer. On a failure the return is %NULL.
125 * Buffers may only be allocated from interrupts using a @gfp_mask of
128 struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
134 skb = kmem_cache_alloc(skbuff_head_cache,
135 gfp_mask & ~__GFP_DMA);
139 /* Get the DATA. Size must match skb_add_mtu(). */
140 size = SKB_DATA_ALIGN(size);
141 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
145 memset(skb, 0, offsetof(struct sk_buff, truesize));
146 skb->truesize = size + sizeof(struct sk_buff);
147 atomic_set(&skb->users, 1);
151 skb->end = data + size;
153 atomic_set(&(skb_shinfo(skb)->dataref), 1);
154 skb_shinfo(skb)->nr_frags = 0;
155 skb_shinfo(skb)->tso_size = 0;
156 skb_shinfo(skb)->tso_segs = 0;
157 skb_shinfo(skb)->frag_list = NULL;
161 kmem_cache_free(skbuff_head_cache, skb);
167 static void skb_drop_fraglist(struct sk_buff *skb)
169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
171 skb_shinfo(skb)->frag_list = NULL;
174 struct sk_buff *this = list;
180 static void skb_clone_fraglist(struct sk_buff *skb)
182 struct sk_buff *list;
184 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
188 void skb_release_data(struct sk_buff *skb)
191 atomic_dec_and_test(&(skb_shinfo(skb)->dataref))) {
192 if (skb_shinfo(skb)->nr_frags) {
194 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
195 put_page(skb_shinfo(skb)->frags[i].page);
198 if (skb_shinfo(skb)->frag_list)
199 skb_drop_fraglist(skb);
206 * Free an skbuff by memory without cleaning the state.
208 void kfree_skbmem(struct sk_buff *skb)
210 skb_release_data(skb);
211 kmem_cache_free(skbuff_head_cache, skb);
215 * __kfree_skb - private function
218 * Free an sk_buff. Release anything attached to the buffer.
219 * Clean the state. This is an internal helper function. Users should
220 * always call kfree_skb
223 void __kfree_skb(struct sk_buff *skb)
226 printk(KERN_WARNING "Warning: kfree_skb passed an skb still "
227 "on a list (from %p).\n", NET_CALLER(skb));
231 dst_release(skb->dst);
233 secpath_put(skb->sp);
235 if(skb->destructor) {
237 printk(KERN_WARNING "Warning: kfree_skb on "
238 "hard IRQ %p\n", NET_CALLER(skb));
239 skb->destructor(skb);
241 #ifdef CONFIG_NETFILTER
242 nf_conntrack_put(skb->nfct);
243 #ifdef CONFIG_BRIDGE_NETFILTER
244 nf_bridge_put(skb->nf_bridge);
247 /* XXX: IS this still necessary? - JHS */
248 #ifdef CONFIG_NET_SCHED
250 #ifdef CONFIG_NET_CLS_ACT
260 * skb_clone - duplicate an sk_buff
261 * @skb: buffer to clone
262 * @gfp_mask: allocation priority
264 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
265 * copies share the same packet data but not structure. The new
266 * buffer has a reference count of 1. If the allocation fails the
267 * function returns %NULL otherwise the new buffer is returned.
269 * If this function is called from an interrupt gfp_mask() must be
273 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
275 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
280 #define C(x) n->x = skb->x
282 n->next = n->prev = NULL;
295 secpath_get(skb->sp);
297 memcpy(n->cb, skb->cb, sizeof(skb->cb));
308 n->destructor = NULL;
309 #ifdef CONFIG_NETFILTER
313 nf_conntrack_get(skb->nfct);
315 #ifdef CONFIG_NETFILTER_DEBUG
318 #ifdef CONFIG_BRIDGE_NETFILTER
320 nf_bridge_get(skb->nf_bridge);
322 #endif /*CONFIG_NETFILTER*/
323 #if defined(CONFIG_HIPPI)
326 #ifdef CONFIG_NET_SCHED
328 #ifdef CONFIG_NET_CLS_ACT
329 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
330 n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
331 n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
338 atomic_set(&n->users, 1);
344 atomic_inc(&(skb_shinfo(skb)->dataref));
350 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
353 * Shift between the two data areas in bytes
355 unsigned long offset = new->data - old->data;
360 new->real_dev = old->real_dev;
361 new->priority = old->priority;
362 new->protocol = old->protocol;
363 new->dst = dst_clone(old->dst);
365 new->sp = secpath_get(old->sp);
367 new->h.raw = old->h.raw + offset;
368 new->nh.raw = old->nh.raw + offset;
369 new->mac.raw = old->mac.raw + offset;
370 memcpy(new->cb, old->cb, sizeof(old->cb));
371 new->local_df = old->local_df;
372 new->pkt_type = old->pkt_type;
373 new->stamp = old->stamp;
374 new->destructor = NULL;
375 new->security = old->security;
376 #ifdef CONFIG_NETFILTER
377 new->nfmark = old->nfmark;
378 new->nfcache = old->nfcache;
379 new->nfct = old->nfct;
380 nf_conntrack_get(old->nfct);
381 new->nfctinfo = old->nfctinfo;
382 #ifdef CONFIG_NETFILTER_DEBUG
383 new->nf_debug = old->nf_debug;
385 #ifdef CONFIG_BRIDGE_NETFILTER
386 new->nf_bridge = old->nf_bridge;
387 nf_bridge_get(old->nf_bridge);
390 #ifdef CONFIG_NET_SCHED
391 #ifdef CONFIG_NET_CLS_ACT
392 new->tc_verd = old->tc_verd;
394 new->tc_index = old->tc_index;
396 atomic_set(&new->users, 1);
400 * skb_copy - create private copy of an sk_buff
401 * @skb: buffer to copy
402 * @gfp_mask: allocation priority
404 * Make a copy of both an &sk_buff and its data. This is used when the
405 * caller wishes to modify the data and needs a private copy of the
406 * data to alter. Returns %NULL on failure or the pointer to the buffer
407 * on success. The returned buffer has a reference count of 1.
409 * As by-product this function converts non-linear &sk_buff to linear
410 * one, so that &sk_buff becomes completely private and caller is allowed
411 * to modify all the data of returned buffer. This means that this
412 * function is not recommended for use in circumstances when only
413 * header is going to be modified. Use pskb_copy() instead.
416 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
418 int headerlen = skb->data - skb->head;
420 * Allocate the copy buffer
422 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
427 /* Set the data pointer */
428 skb_reserve(n, headerlen);
429 /* Set the tail pointer and length */
430 skb_put(n, skb->len);
432 n->ip_summed = skb->ip_summed;
434 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
437 copy_skb_header(n, skb);
443 * pskb_copy - create copy of an sk_buff with private head.
444 * @skb: buffer to copy
445 * @gfp_mask: allocation priority
447 * Make a copy of both an &sk_buff and part of its data, located
448 * in header. Fragmented data remain shared. This is used when
449 * the caller wishes to modify only header of &sk_buff and needs
450 * private copy of the header to alter. Returns %NULL on failure
451 * or the pointer to the buffer on success.
452 * The returned buffer has a reference count of 1.
455 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
458 * Allocate the copy buffer
460 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
465 /* Set the data pointer */
466 skb_reserve(n, skb->data - skb->head);
467 /* Set the tail pointer and length */
468 skb_put(n, skb_headlen(skb));
470 memcpy(n->data, skb->data, n->len);
472 n->ip_summed = skb->ip_summed;
474 n->data_len = skb->data_len;
477 if (skb_shinfo(skb)->nr_frags) {
480 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
481 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
482 get_page(skb_shinfo(n)->frags[i].page);
484 skb_shinfo(n)->nr_frags = i;
486 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
487 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
489 if (skb_shinfo(skb)->frag_list) {
490 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
491 skb_clone_fraglist(n);
494 copy_skb_header(n, skb);
500 * pskb_expand_head - reallocate header of &sk_buff
501 * @skb: buffer to reallocate
502 * @nhead: room to add at head
503 * @ntail: room to add at tail
504 * @gfp_mask: allocation priority
506 * Expands (or creates identical copy, if &nhead and &ntail are zero)
507 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
508 * reference count of 1. Returns zero in the case of success or error,
509 * if expansion failed. In the last case, &sk_buff is not changed.
511 * All the pointers pointing into skb header may change and must be
512 * reloaded after call to this function.
515 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
519 int size = nhead + (skb->end - skb->head) + ntail;
525 size = SKB_DATA_ALIGN(size);
527 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
531 /* Copy only real data... and, alas, header. This should be
532 * optimized for the cases when header is void. */
533 memcpy(data + nhead, skb->head, skb->tail - skb->head);
534 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
536 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
537 get_page(skb_shinfo(skb)->frags[i].page);
539 if (skb_shinfo(skb)->frag_list)
540 skb_clone_fraglist(skb);
542 skb_release_data(skb);
544 off = (data + nhead) - skb->head;
547 skb->end = data + size;
554 atomic_set(&skb_shinfo(skb)->dataref, 1);
561 /* Make private copy of skb with writable head and some headroom */
563 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
565 struct sk_buff *skb2;
566 int delta = headroom - skb_headroom(skb);
569 skb2 = pskb_copy(skb, GFP_ATOMIC);
571 skb2 = skb_clone(skb, GFP_ATOMIC);
572 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
583 * skb_copy_expand - copy and expand sk_buff
584 * @skb: buffer to copy
585 * @newheadroom: new free bytes at head
586 * @newtailroom: new free bytes at tail
587 * @gfp_mask: allocation priority
589 * Make a copy of both an &sk_buff and its data and while doing so
590 * allocate additional space.
592 * This is used when the caller wishes to modify the data and needs a
593 * private copy of the data to alter as well as more space for new fields.
594 * Returns %NULL on failure or the pointer to the buffer
595 * on success. The returned buffer has a reference count of 1.
597 * You must pass %GFP_ATOMIC as the allocation priority if this function
598 * is called from an interrupt.
600 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
601 * only by netfilter in the cases when checksum is recalculated? --ANK
603 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
604 int newheadroom, int newtailroom, int gfp_mask)
607 * Allocate the copy buffer
609 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
611 int head_copy_len, head_copy_off;
616 skb_reserve(n, newheadroom);
618 /* Set the tail pointer and length */
619 skb_put(n, skb->len);
621 head_copy_len = skb_headroom(skb);
623 if (newheadroom <= head_copy_len)
624 head_copy_len = newheadroom;
626 head_copy_off = newheadroom - head_copy_len;
628 /* Copy the linear header and data. */
629 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
630 skb->len + head_copy_len))
633 copy_skb_header(n, skb);
634 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
635 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
641 * skb_pad - zero pad the tail of an skb
642 * @skb: buffer to pad
645 * Ensure that a buffer is followed by a padding area that is zero
646 * filled. Used by network drivers which may DMA or transfer data
647 * beyond the buffer end onto the wire.
649 * May return NULL in out of memory cases.
652 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
654 struct sk_buff *nskb;
656 /* If the skbuff is non linear tailroom is always zero.. */
657 if (skb_tailroom(skb) >= pad) {
658 memset(skb->data+skb->len, 0, pad);
662 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
665 memset(nskb->data+nskb->len, 0, pad);
669 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
670 * If realloc==0 and trimming is impossible without change of data,
674 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
676 int offset = skb_headlen(skb);
677 int nfrags = skb_shinfo(skb)->nr_frags;
680 for (i = 0; i < nfrags; i++) {
681 int end = offset + skb_shinfo(skb)->frags[i].size;
683 if (skb_cloned(skb)) {
686 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
690 put_page(skb_shinfo(skb)->frags[i].page);
691 skb_shinfo(skb)->nr_frags--;
693 skb_shinfo(skb)->frags[i].size = len - offset;
700 skb->data_len -= skb->len - len;
703 if (len <= skb_headlen(skb)) {
706 skb->tail = skb->data + len;
707 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
708 skb_drop_fraglist(skb);
710 skb->data_len -= skb->len - len;
719 * __pskb_pull_tail - advance tail of skb header
720 * @skb: buffer to reallocate
721 * @delta: number of bytes to advance tail
723 * The function makes a sense only on a fragmented &sk_buff,
724 * it expands header moving its tail forward and copying necessary
725 * data from fragmented part.
727 * &sk_buff MUST have reference count of 1.
729 * Returns %NULL (and &sk_buff does not change) if pull failed
730 * or value of new tail of skb in the case of success.
732 * All the pointers pointing into skb header may change and must be
733 * reloaded after call to this function.
736 /* Moves tail of skb head forward, copying data from fragmented part,
737 * when it is necessary.
738 * 1. It may fail due to malloc failure.
739 * 2. It may change skb pointers.
741 * It is pretty complicated. Luckily, it is called only in exceptional cases.
743 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
745 /* If skb has not enough free space at tail, get new one
746 * plus 128 bytes for future expansions. If we have enough
747 * room at tail, reallocate without expansion only if skb is cloned.
749 int i, k, eat = (skb->tail + delta) - skb->end;
751 if (eat > 0 || skb_cloned(skb)) {
752 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
757 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
760 /* Optimization: no fragments, no reasons to preestimate
761 * size of pulled pages. Superb.
763 if (!skb_shinfo(skb)->frag_list)
766 /* Estimate size of pulled pages. */
768 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
769 if (skb_shinfo(skb)->frags[i].size >= eat)
771 eat -= skb_shinfo(skb)->frags[i].size;
774 /* If we need update frag list, we are in troubles.
775 * Certainly, it possible to add an offset to skb data,
776 * but taking into account that pulling is expected to
777 * be very rare operation, it is worth to fight against
778 * further bloating skb head and crucify ourselves here instead.
779 * Pure masohism, indeed. 8)8)
782 struct sk_buff *list = skb_shinfo(skb)->frag_list;
783 struct sk_buff *clone = NULL;
784 struct sk_buff *insp = NULL;
790 if (list->len <= eat) {
791 /* Eaten as whole. */
796 /* Eaten partially. */
798 if (skb_shared(list)) {
799 /* Sucks! We need to fork list. :-( */
800 clone = skb_clone(list, GFP_ATOMIC);
806 /* This may be pulled without
810 if (!pskb_pull(list, eat)) {
819 /* Free pulled out fragments. */
820 while ((list = skb_shinfo(skb)->frag_list) != insp) {
821 skb_shinfo(skb)->frag_list = list->next;
824 /* And insert new clone at head. */
827 skb_shinfo(skb)->frag_list = clone;
830 /* Success! Now we may commit changes to skb data. */
835 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
836 if (skb_shinfo(skb)->frags[i].size <= eat) {
837 put_page(skb_shinfo(skb)->frags[i].page);
838 eat -= skb_shinfo(skb)->frags[i].size;
840 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
842 skb_shinfo(skb)->frags[k].page_offset += eat;
843 skb_shinfo(skb)->frags[k].size -= eat;
849 skb_shinfo(skb)->nr_frags = k;
852 skb->data_len -= delta;
857 /* Copy some data bits from skb to kernel buffer. */
859 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
862 int start = skb_headlen(skb);
864 if (offset > (int)skb->len - len)
868 if ((copy = start - offset) > 0) {
871 memcpy(to, skb->data + offset, copy);
872 if ((len -= copy) == 0)
878 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
881 BUG_TRAP(start <= offset + len);
883 end = start + skb_shinfo(skb)->frags[i].size;
884 if ((copy = end - offset) > 0) {
890 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
892 vaddr + skb_shinfo(skb)->frags[i].page_offset+
893 offset - start, copy);
894 kunmap_skb_frag(vaddr);
896 if ((len -= copy) == 0)
904 if (skb_shinfo(skb)->frag_list) {
905 struct sk_buff *list = skb_shinfo(skb)->frag_list;
907 for (; list; list = list->next) {
910 BUG_TRAP(start <= offset + len);
912 end = start + list->len;
913 if ((copy = end - offset) > 0) {
916 if (skb_copy_bits(list, offset - start,
919 if ((len -= copy) == 0)
934 /* Keep iterating until skb_iter_next returns false. */
935 void skb_iter_first(const struct sk_buff *skb, struct skb_iter *i)
937 i->len = skb_headlen(skb);
938 i->data = (unsigned char *)skb->data;
943 int skb_iter_next(const struct sk_buff *skb, struct skb_iter *i)
945 /* Unmap previous, if not head fragment. */
947 kunmap_skb_frag(i->data);
951 /* We're iterating through fraglist. */
952 if (i->nextfrag < skb_shinfo(i->fraglist)->nr_frags) {
953 i->data = kmap_skb_frag(&skb_shinfo(i->fraglist)
954 ->frags[i->nextfrag]);
955 i->len = skb_shinfo(i->fraglist)->frags[i->nextfrag]
960 /* Fragments with fragments? Too hard! */
961 BUG_ON(skb_shinfo(i->fraglist)->frag_list);
962 i->fraglist = i->fraglist->next;
966 i->len = skb_headlen(i->fraglist);
967 i->data = i->fraglist->data;
972 if (i->nextfrag < skb_shinfo(skb)->nr_frags) {
973 i->data = kmap_skb_frag(&skb_shinfo(skb)->frags[i->nextfrag]);
974 i->len = skb_shinfo(skb)->frags[i->nextfrag].size;
979 i->fraglist = skb_shinfo(skb)->frag_list;
984 /* Bug trap for callers */
989 void skb_iter_abort(const struct sk_buff *skb, struct skb_iter *i)
991 /* Unmap previous, if not head fragment. */
992 if (i->data && i->nextfrag)
993 kunmap_skb_frag(i->data);
994 /* Bug trap for callers */
998 /* Checksum skb data. */
1000 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1001 int len, unsigned int csum)
1003 int start = skb_headlen(skb);
1004 int i, copy = start - offset;
1007 /* Checksum header. */
1011 csum = csum_partial(skb->data + offset, copy, csum);
1012 if ((len -= copy) == 0)
1018 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1021 BUG_TRAP(start <= offset + len);
1023 end = start + skb_shinfo(skb)->frags[i].size;
1024 if ((copy = end - offset) > 0) {
1027 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1031 vaddr = kmap_skb_frag(frag);
1032 csum2 = csum_partial(vaddr + frag->page_offset +
1033 offset - start, copy, 0);
1034 kunmap_skb_frag(vaddr);
1035 csum = csum_block_add(csum, csum2, pos);
1044 if (skb_shinfo(skb)->frag_list) {
1045 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1047 for (; list; list = list->next) {
1050 BUG_TRAP(start <= offset + len);
1052 end = start + list->len;
1053 if ((copy = end - offset) > 0) {
1057 csum2 = skb_checksum(list, offset - start,
1059 csum = csum_block_add(csum, csum2, pos);
1060 if ((len -= copy) == 0)
1074 /* Both of above in one bottle. */
1076 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1077 u8 *to, int len, unsigned int csum)
1079 int start = skb_headlen(skb);
1080 int i, copy = start - offset;
1087 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1089 if ((len -= copy) == 0)
1096 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1099 BUG_TRAP(start <= offset + len);
1101 end = start + skb_shinfo(skb)->frags[i].size;
1102 if ((copy = end - offset) > 0) {
1105 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1109 vaddr = kmap_skb_frag(frag);
1110 csum2 = csum_partial_copy_nocheck(vaddr +
1114 kunmap_skb_frag(vaddr);
1115 csum = csum_block_add(csum, csum2, pos);
1125 if (skb_shinfo(skb)->frag_list) {
1126 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1128 for (; list; list = list->next) {
1132 BUG_TRAP(start <= offset + len);
1134 end = start + list->len;
1135 if ((copy = end - offset) > 0) {
1138 csum2 = skb_copy_and_csum_bits(list,
1141 csum = csum_block_add(csum, csum2, pos);
1142 if ((len -= copy) == 0)
1156 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1161 if (skb->ip_summed == CHECKSUM_HW)
1162 csstart = skb->h.raw - skb->data;
1164 csstart = skb_headlen(skb);
1166 if (csstart > skb_headlen(skb))
1169 memcpy(to, skb->data, csstart);
1172 if (csstart != skb->len)
1173 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1174 skb->len - csstart, 0);
1176 if (skb->ip_summed == CHECKSUM_HW) {
1177 long csstuff = csstart + skb->csum;
1179 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1184 * skb_dequeue - remove from the head of the queue
1185 * @list: list to dequeue from
1187 * Remove the head of the list. The list lock is taken so the function
1188 * may be used safely with other locking list functions. The head item is
1189 * returned or %NULL if the list is empty.
1192 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1194 unsigned long flags;
1195 struct sk_buff *result;
1197 spin_lock_irqsave(&list->lock, flags);
1198 result = __skb_dequeue(list);
1199 spin_unlock_irqrestore(&list->lock, flags);
1204 * skb_dequeue_tail - remove from the tail of the queue
1205 * @list: list to dequeue from
1207 * Remove the tail of the list. The list lock is taken so the function
1208 * may be used safely with other locking list functions. The tail item is
1209 * returned or %NULL if the list is empty.
1211 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1213 unsigned long flags;
1214 struct sk_buff *result;
1216 spin_lock_irqsave(&list->lock, flags);
1217 result = __skb_dequeue_tail(list);
1218 spin_unlock_irqrestore(&list->lock, flags);
1223 * skb_queue_purge - empty a list
1224 * @list: list to empty
1226 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1227 * the list and one reference dropped. This function takes the list
1228 * lock and is atomic with respect to other list locking functions.
1230 void skb_queue_purge(struct sk_buff_head *list)
1232 struct sk_buff *skb;
1233 while ((skb = skb_dequeue(list)) != NULL)
1238 * skb_queue_head - queue a buffer at the list head
1239 * @list: list to use
1240 * @newsk: buffer to queue
1242 * Queue a buffer at the start of the list. This function takes the
1243 * list lock and can be used safely with other locking &sk_buff functions
1246 * A buffer cannot be placed on two lists at the same time.
1248 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1250 unsigned long flags;
1252 spin_lock_irqsave(&list->lock, flags);
1253 __skb_queue_head(list, newsk);
1254 spin_unlock_irqrestore(&list->lock, flags);
1258 * skb_queue_tail - queue a buffer at the list tail
1259 * @list: list to use
1260 * @newsk: buffer to queue
1262 * Queue a buffer at the tail of the list. This function takes the
1263 * list lock and can be used safely with other locking &sk_buff functions
1266 * A buffer cannot be placed on two lists at the same time.
1268 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1270 unsigned long flags;
1272 spin_lock_irqsave(&list->lock, flags);
1273 __skb_queue_tail(list, newsk);
1274 spin_unlock_irqrestore(&list->lock, flags);
1277 * skb_unlink - remove a buffer from a list
1278 * @skb: buffer to remove
1280 * Place a packet after a given packet in a list. The list locks are taken
1281 * and this function is atomic with respect to other list locked calls
1283 * Works even without knowing the list it is sitting on, which can be
1284 * handy at times. It also means that THE LIST MUST EXIST when you
1285 * unlink. Thus a list must have its contents unlinked before it is
1288 void skb_unlink(struct sk_buff *skb)
1290 struct sk_buff_head *list = skb->list;
1293 unsigned long flags;
1295 spin_lock_irqsave(&list->lock, flags);
1296 if (skb->list == list)
1297 __skb_unlink(skb, skb->list);
1298 spin_unlock_irqrestore(&list->lock, flags);
1304 * skb_append - append a buffer
1305 * @old: buffer to insert after
1306 * @newsk: buffer to insert
1308 * Place a packet after a given packet in a list. The list locks are taken
1309 * and this function is atomic with respect to other list locked calls.
1310 * A buffer cannot be placed on two lists at the same time.
1313 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1315 unsigned long flags;
1317 spin_lock_irqsave(&old->list->lock, flags);
1318 __skb_append(old, newsk);
1319 spin_unlock_irqrestore(&old->list->lock, flags);
1324 * skb_insert - insert a buffer
1325 * @old: buffer to insert before
1326 * @newsk: buffer to insert
1328 * Place a packet before a given packet in a list. The list locks are taken
1329 * and this function is atomic with respect to other list locked calls
1330 * A buffer cannot be placed on two lists at the same time.
1333 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1335 unsigned long flags;
1337 spin_lock_irqsave(&old->list->lock, flags);
1338 __skb_insert(newsk, old->prev, old, old->list);
1339 spin_unlock_irqrestore(&old->list->lock, flags);
1344 * Tune the memory allocator for a new MTU size.
1346 void skb_add_mtu(int mtu)
1348 /* Must match allocation in alloc_skb */
1349 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1351 kmem_add_cache_size(mtu);
1355 static void inline skb_split_inside_header(struct sk_buff *skb,
1356 struct sk_buff* skb1,
1357 const u32 len, const int pos)
1361 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1363 /* And move data appendix as is. */
1364 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1365 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1367 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1368 skb_shinfo(skb)->nr_frags = 0;
1369 skb1->data_len = skb->data_len;
1370 skb1->len += skb1->data_len;
1373 skb->tail = skb->data + len;
1376 static void inline skb_split_no_header(struct sk_buff *skb,
1377 struct sk_buff* skb1,
1378 const u32 len, int pos)
1381 const int nfrags = skb_shinfo(skb)->nr_frags;
1383 skb_shinfo(skb)->nr_frags = 0;
1384 skb1->len = skb1->data_len = skb->len - len;
1386 skb->data_len = len - pos;
1388 for (i = 0; i < nfrags; i++) {
1389 int size = skb_shinfo(skb)->frags[i].size;
1391 if (pos + size > len) {
1392 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1396 * We have to variants in this case:
1397 * 1. Move all the frag to the second
1398 * part, if it is possible. F.e.
1399 * this approach is mandatory for TUX,
1400 * where splitting is expensive.
1401 * 2. Split is accurately. We make this.
1403 get_page(skb_shinfo(skb)->frags[i].page);
1404 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1405 skb_shinfo(skb1)->frags[0].size -= len - pos;
1406 skb_shinfo(skb)->frags[i].size = len - pos;
1407 skb_shinfo(skb)->nr_frags++;
1411 skb_shinfo(skb)->nr_frags++;
1414 skb_shinfo(skb1)->nr_frags = k;
1418 * skb_split - Split fragmented skb to two parts at length len.
1420 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1422 int pos = skb_headlen(skb);
1424 if (len < pos) /* Split line is inside header. */
1425 skb_split_inside_header(skb, skb1, len, pos);
1426 else /* Second chunk has no header, nothing to copy. */
1427 skb_split_no_header(skb, skb1, len, pos);
1430 void __init skb_init(void)
1432 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1433 sizeof(struct sk_buff),
1437 if (!skbuff_head_cache)
1438 panic("cannot create skbuff cache");
1441 EXPORT_SYMBOL(___pskb_trim);
1442 EXPORT_SYMBOL(__kfree_skb);
1443 EXPORT_SYMBOL(__pskb_pull_tail);
1444 EXPORT_SYMBOL(alloc_skb);
1445 EXPORT_SYMBOL(pskb_copy);
1446 EXPORT_SYMBOL(pskb_expand_head);
1447 EXPORT_SYMBOL(skb_checksum);
1448 EXPORT_SYMBOL(skb_clone);
1449 EXPORT_SYMBOL(skb_clone_fraglist);
1450 EXPORT_SYMBOL(skb_copy);
1451 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1452 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1453 EXPORT_SYMBOL(skb_copy_bits);
1454 EXPORT_SYMBOL(skb_copy_expand);
1455 EXPORT_SYMBOL(skb_over_panic);
1456 EXPORT_SYMBOL(skb_pad);
1457 EXPORT_SYMBOL(skb_realloc_headroom);
1458 EXPORT_SYMBOL(skb_under_panic);
1459 EXPORT_SYMBOL(skb_dequeue);
1460 EXPORT_SYMBOL(skb_dequeue_tail);
1461 EXPORT_SYMBOL(skb_insert);
1462 EXPORT_SYMBOL(skb_queue_purge);
1463 EXPORT_SYMBOL(skb_queue_head);
1464 EXPORT_SYMBOL(skb_queue_tail);
1465 EXPORT_SYMBOL(skb_unlink);
1466 EXPORT_SYMBOL(skb_append);
1467 EXPORT_SYMBOL(skb_split);
1468 EXPORT_SYMBOL(skb_iter_first);
1469 EXPORT_SYMBOL(skb_iter_next);
1470 EXPORT_SYMBOL(skb_iter_abort);