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 * alloc_skb_from_cache - allocate a network buffer
168 * @cp: kmem_cache from which to allocate the data area
169 * (object size must be big enough for @size bytes + skb overheads)
170 * @size: size to allocate
171 * @gfp_mask: allocation mask
173 * Allocate a new &sk_buff. The returned buffer has no headroom and
174 * tail room of size bytes. The object has a reference count of one.
175 * The return is the buffer. On a failure the return is %NULL.
177 * Buffers may only be allocated from interrupts using a @gfp_mask of
180 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
181 unsigned int size, int gfp_mask)
187 skb = kmem_cache_alloc(skbuff_head_cache,
188 gfp_mask & ~__GFP_DMA);
193 size = SKB_DATA_ALIGN(size);
194 data = kmem_cache_alloc(cp, gfp_mask);
198 memset(skb, 0, offsetof(struct sk_buff, truesize));
199 skb->truesize = size + sizeof(struct sk_buff);
200 atomic_set(&skb->users, 1);
204 skb->end = data + size;
206 atomic_set(&(skb_shinfo(skb)->dataref), 1);
207 skb_shinfo(skb)->nr_frags = 0;
208 skb_shinfo(skb)->tso_size = 0;
209 skb_shinfo(skb)->tso_segs = 0;
210 skb_shinfo(skb)->frag_list = NULL;
214 kmem_cache_free(skbuff_head_cache, skb);
220 static void skb_drop_fraglist(struct sk_buff *skb)
222 struct sk_buff *list = skb_shinfo(skb)->frag_list;
224 skb_shinfo(skb)->frag_list = NULL;
227 struct sk_buff *this = list;
233 static void skb_clone_fraglist(struct sk_buff *skb)
235 struct sk_buff *list;
237 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
241 void skb_release_data(struct sk_buff *skb)
244 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
245 &skb_shinfo(skb)->dataref)) {
246 if (skb_shinfo(skb)->nr_frags) {
248 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
249 put_page(skb_shinfo(skb)->frags[i].page);
252 if (skb_shinfo(skb)->frag_list)
253 skb_drop_fraglist(skb);
260 * Free an skbuff by memory without cleaning the state.
262 void kfree_skbmem(struct sk_buff *skb)
264 skb_release_data(skb);
265 kmem_cache_free(skbuff_head_cache, skb);
269 * __kfree_skb - private function
272 * Free an sk_buff. Release anything attached to the buffer.
273 * Clean the state. This is an internal helper function. Users should
274 * always call kfree_skb
277 void __kfree_skb(struct sk_buff *skb)
279 BUG_ON(skb->list != NULL);
281 dst_release(skb->dst);
283 secpath_put(skb->sp);
285 if (skb->destructor) {
287 skb->destructor(skb);
289 #ifdef CONFIG_NETFILTER
290 nf_conntrack_put(skb->nfct);
291 #ifdef CONFIG_BRIDGE_NETFILTER
292 nf_bridge_put(skb->nf_bridge);
295 /* XXX: IS this still necessary? - JHS */
296 #ifdef CONFIG_NET_SCHED
298 #ifdef CONFIG_NET_CLS_ACT
308 * skb_clone - duplicate an sk_buff
309 * @skb: buffer to clone
310 * @gfp_mask: allocation priority
312 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
313 * copies share the same packet data but not structure. The new
314 * buffer has a reference count of 1. If the allocation fails the
315 * function returns %NULL otherwise the new buffer is returned.
317 * If this function is called from an interrupt gfp_mask() must be
321 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
323 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
328 #define C(x) n->x = skb->x
330 n->next = n->prev = NULL;
343 secpath_get(skb->sp);
345 memcpy(n->cb, skb->cb, sizeof(skb->cb));
357 n->destructor = NULL;
358 #ifdef CONFIG_NETFILTER
362 nf_conntrack_get(skb->nfct);
364 #ifdef CONFIG_NETFILTER_DEBUG
367 #ifdef CONFIG_BRIDGE_NETFILTER
369 nf_bridge_get(skb->nf_bridge);
371 #endif /*CONFIG_NETFILTER*/
372 #if defined(CONFIG_HIPPI)
375 #ifdef CONFIG_NET_SCHED
377 #ifdef CONFIG_NET_CLS_ACT
378 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
379 n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
380 n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
387 atomic_set(&n->users, 1);
393 atomic_inc(&(skb_shinfo(skb)->dataref));
399 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
402 * Shift between the two data areas in bytes
404 unsigned long offset = new->data - old->data;
409 new->real_dev = old->real_dev;
410 new->priority = old->priority;
411 new->protocol = old->protocol;
412 new->dst = dst_clone(old->dst);
414 new->sp = secpath_get(old->sp);
416 new->h.raw = old->h.raw + offset;
417 new->nh.raw = old->nh.raw + offset;
418 new->mac.raw = old->mac.raw + offset;
419 memcpy(new->cb, old->cb, sizeof(old->cb));
420 new->local_df = old->local_df;
421 new->pkt_type = old->pkt_type;
422 new->stamp = old->stamp;
423 new->destructor = NULL;
424 new->security = old->security;
425 #ifdef CONFIG_NETFILTER
426 new->nfmark = old->nfmark;
427 new->nfcache = old->nfcache;
428 new->nfct = old->nfct;
429 nf_conntrack_get(old->nfct);
430 new->nfctinfo = old->nfctinfo;
431 #ifdef CONFIG_NETFILTER_DEBUG
432 new->nf_debug = old->nf_debug;
434 #ifdef CONFIG_BRIDGE_NETFILTER
435 new->nf_bridge = old->nf_bridge;
436 nf_bridge_get(old->nf_bridge);
439 #ifdef CONFIG_NET_SCHED
440 #ifdef CONFIG_NET_CLS_ACT
441 new->tc_verd = old->tc_verd;
443 new->tc_index = old->tc_index;
445 atomic_set(&new->users, 1);
446 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
447 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
451 * skb_copy - create private copy of an sk_buff
452 * @skb: buffer to copy
453 * @gfp_mask: allocation priority
455 * Make a copy of both an &sk_buff and its data. This is used when the
456 * caller wishes to modify the data and needs a private copy of the
457 * data to alter. Returns %NULL on failure or the pointer to the buffer
458 * on success. The returned buffer has a reference count of 1.
460 * As by-product this function converts non-linear &sk_buff to linear
461 * one, so that &sk_buff becomes completely private and caller is allowed
462 * to modify all the data of returned buffer. This means that this
463 * function is not recommended for use in circumstances when only
464 * header is going to be modified. Use pskb_copy() instead.
467 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
469 int headerlen = skb->data - skb->head;
471 * Allocate the copy buffer
473 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
478 /* Set the data pointer */
479 skb_reserve(n, headerlen);
480 /* Set the tail pointer and length */
481 skb_put(n, skb->len);
483 n->ip_summed = skb->ip_summed;
485 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
488 copy_skb_header(n, skb);
494 * pskb_copy - create copy of an sk_buff with private head.
495 * @skb: buffer to copy
496 * @gfp_mask: allocation priority
498 * Make a copy of both an &sk_buff and part of its data, located
499 * in header. Fragmented data remain shared. This is used when
500 * the caller wishes to modify only header of &sk_buff and needs
501 * private copy of the header to alter. Returns %NULL on failure
502 * or the pointer to the buffer on success.
503 * The returned buffer has a reference count of 1.
506 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
509 * Allocate the copy buffer
511 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
516 /* Set the data pointer */
517 skb_reserve(n, skb->data - skb->head);
518 /* Set the tail pointer and length */
519 skb_put(n, skb_headlen(skb));
521 memcpy(n->data, skb->data, n->len);
523 n->ip_summed = skb->ip_summed;
525 n->data_len = skb->data_len;
528 if (skb_shinfo(skb)->nr_frags) {
531 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
532 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
533 get_page(skb_shinfo(n)->frags[i].page);
535 skb_shinfo(n)->nr_frags = i;
538 if (skb_shinfo(skb)->frag_list) {
539 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
540 skb_clone_fraglist(n);
543 copy_skb_header(n, skb);
549 * pskb_expand_head - reallocate header of &sk_buff
550 * @skb: buffer to reallocate
551 * @nhead: room to add at head
552 * @ntail: room to add at tail
553 * @gfp_mask: allocation priority
555 * Expands (or creates identical copy, if &nhead and &ntail are zero)
556 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
557 * reference count of 1. Returns zero in the case of success or error,
558 * if expansion failed. In the last case, &sk_buff is not changed.
560 * All the pointers pointing into skb header may change and must be
561 * reloaded after call to this function.
564 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
568 int size = nhead + (skb->end - skb->head) + ntail;
574 size = SKB_DATA_ALIGN(size);
576 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
580 /* Copy only real data... and, alas, header. This should be
581 * optimized for the cases when header is void. */
582 memcpy(data + nhead, skb->head, skb->tail - skb->head);
583 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
585 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
586 get_page(skb_shinfo(skb)->frags[i].page);
588 if (skb_shinfo(skb)->frag_list)
589 skb_clone_fraglist(skb);
591 skb_release_data(skb);
593 off = (data + nhead) - skb->head;
596 skb->end = data + size;
604 atomic_set(&skb_shinfo(skb)->dataref, 1);
611 /* Make private copy of skb with writable head and some headroom */
613 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
615 struct sk_buff *skb2;
616 int delta = headroom - skb_headroom(skb);
619 skb2 = pskb_copy(skb, GFP_ATOMIC);
621 skb2 = skb_clone(skb, GFP_ATOMIC);
622 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
633 * skb_copy_expand - copy and expand sk_buff
634 * @skb: buffer to copy
635 * @newheadroom: new free bytes at head
636 * @newtailroom: new free bytes at tail
637 * @gfp_mask: allocation priority
639 * Make a copy of both an &sk_buff and its data and while doing so
640 * allocate additional space.
642 * This is used when the caller wishes to modify the data and needs a
643 * private copy of the data to alter as well as more space for new fields.
644 * Returns %NULL on failure or the pointer to the buffer
645 * on success. The returned buffer has a reference count of 1.
647 * You must pass %GFP_ATOMIC as the allocation priority if this function
648 * is called from an interrupt.
650 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
651 * only by netfilter in the cases when checksum is recalculated? --ANK
653 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
654 int newheadroom, int newtailroom, int gfp_mask)
657 * Allocate the copy buffer
659 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
661 int head_copy_len, head_copy_off;
666 skb_reserve(n, newheadroom);
668 /* Set the tail pointer and length */
669 skb_put(n, skb->len);
671 head_copy_len = skb_headroom(skb);
673 if (newheadroom <= head_copy_len)
674 head_copy_len = newheadroom;
676 head_copy_off = newheadroom - head_copy_len;
678 /* Copy the linear header and data. */
679 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
680 skb->len + head_copy_len))
683 copy_skb_header(n, skb);
689 * skb_pad - zero pad the tail of an skb
690 * @skb: buffer to pad
693 * Ensure that a buffer is followed by a padding area that is zero
694 * filled. Used by network drivers which may DMA or transfer data
695 * beyond the buffer end onto the wire.
697 * May return NULL in out of memory cases.
700 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
702 struct sk_buff *nskb;
704 /* If the skbuff is non linear tailroom is always zero.. */
705 if (skb_tailroom(skb) >= pad) {
706 memset(skb->data+skb->len, 0, pad);
710 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
713 memset(nskb->data+nskb->len, 0, pad);
717 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
718 * If realloc==0 and trimming is impossible without change of data,
722 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
724 int offset = skb_headlen(skb);
725 int nfrags = skb_shinfo(skb)->nr_frags;
728 for (i = 0; i < nfrags; i++) {
729 int end = offset + skb_shinfo(skb)->frags[i].size;
731 if (skb_cloned(skb)) {
734 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
738 put_page(skb_shinfo(skb)->frags[i].page);
739 skb_shinfo(skb)->nr_frags--;
741 skb_shinfo(skb)->frags[i].size = len - offset;
748 skb->data_len -= skb->len - len;
751 if (len <= skb_headlen(skb)) {
754 skb->tail = skb->data + len;
755 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
756 skb_drop_fraglist(skb);
758 skb->data_len -= skb->len - len;
767 * __pskb_pull_tail - advance tail of skb header
768 * @skb: buffer to reallocate
769 * @delta: number of bytes to advance tail
771 * The function makes a sense only on a fragmented &sk_buff,
772 * it expands header moving its tail forward and copying necessary
773 * data from fragmented part.
775 * &sk_buff MUST have reference count of 1.
777 * Returns %NULL (and &sk_buff does not change) if pull failed
778 * or value of new tail of skb in the case of success.
780 * All the pointers pointing into skb header may change and must be
781 * reloaded after call to this function.
784 /* Moves tail of skb head forward, copying data from fragmented part,
785 * when it is necessary.
786 * 1. It may fail due to malloc failure.
787 * 2. It may change skb pointers.
789 * It is pretty complicated. Luckily, it is called only in exceptional cases.
791 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
793 /* If skb has not enough free space at tail, get new one
794 * plus 128 bytes for future expansions. If we have enough
795 * room at tail, reallocate without expansion only if skb is cloned.
797 int i, k, eat = (skb->tail + delta) - skb->end;
799 if (eat > 0 || skb_cloned(skb)) {
800 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
805 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
808 /* Optimization: no fragments, no reasons to preestimate
809 * size of pulled pages. Superb.
811 if (!skb_shinfo(skb)->frag_list)
814 /* Estimate size of pulled pages. */
816 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
817 if (skb_shinfo(skb)->frags[i].size >= eat)
819 eat -= skb_shinfo(skb)->frags[i].size;
822 /* If we need update frag list, we are in troubles.
823 * Certainly, it possible to add an offset to skb data,
824 * but taking into account that pulling is expected to
825 * be very rare operation, it is worth to fight against
826 * further bloating skb head and crucify ourselves here instead.
827 * Pure masohism, indeed. 8)8)
830 struct sk_buff *list = skb_shinfo(skb)->frag_list;
831 struct sk_buff *clone = NULL;
832 struct sk_buff *insp = NULL;
838 if (list->len <= eat) {
839 /* Eaten as whole. */
844 /* Eaten partially. */
846 if (skb_shared(list)) {
847 /* Sucks! We need to fork list. :-( */
848 clone = skb_clone(list, GFP_ATOMIC);
854 /* This may be pulled without
858 if (!pskb_pull(list, eat)) {
867 /* Free pulled out fragments. */
868 while ((list = skb_shinfo(skb)->frag_list) != insp) {
869 skb_shinfo(skb)->frag_list = list->next;
872 /* And insert new clone at head. */
875 skb_shinfo(skb)->frag_list = clone;
878 /* Success! Now we may commit changes to skb data. */
883 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
884 if (skb_shinfo(skb)->frags[i].size <= eat) {
885 put_page(skb_shinfo(skb)->frags[i].page);
886 eat -= skb_shinfo(skb)->frags[i].size;
888 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
890 skb_shinfo(skb)->frags[k].page_offset += eat;
891 skb_shinfo(skb)->frags[k].size -= eat;
897 skb_shinfo(skb)->nr_frags = k;
900 skb->data_len -= delta;
905 /* Copy some data bits from skb to kernel buffer. */
907 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
910 int start = skb_headlen(skb);
912 if (offset > (int)skb->len - len)
916 if ((copy = start - offset) > 0) {
919 memcpy(to, skb->data + offset, copy);
920 if ((len -= copy) == 0)
926 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
929 BUG_TRAP(start <= offset + len);
931 end = start + skb_shinfo(skb)->frags[i].size;
932 if ((copy = end - offset) > 0) {
938 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
940 vaddr + skb_shinfo(skb)->frags[i].page_offset+
941 offset - start, copy);
942 kunmap_skb_frag(vaddr);
944 if ((len -= copy) == 0)
952 if (skb_shinfo(skb)->frag_list) {
953 struct sk_buff *list = skb_shinfo(skb)->frag_list;
955 for (; list; list = list->next) {
958 BUG_TRAP(start <= offset + len);
960 end = start + list->len;
961 if ((copy = end - offset) > 0) {
964 if (skb_copy_bits(list, offset - start,
967 if ((len -= copy) == 0)
983 * skb_store_bits - store bits from kernel buffer to skb
984 * @skb: destination buffer
985 * @offset: offset in destination
986 * @from: source buffer
987 * @len: number of bytes to copy
989 * Copy the specified number of bytes from the source buffer to the
990 * destination skb. This function handles all the messy bits of
991 * traversing fragment lists and such.
994 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
997 int start = skb_headlen(skb);
999 if (offset > (int)skb->len - len)
1002 if ((copy = start - offset) > 0) {
1005 memcpy(skb->data + offset, from, copy);
1006 if ((len -= copy) == 0)
1012 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1013 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1016 BUG_TRAP(start <= offset + len);
1018 end = start + frag->size;
1019 if ((copy = end - offset) > 0) {
1025 vaddr = kmap_skb_frag(frag);
1026 memcpy(vaddr + frag->page_offset + offset - start,
1028 kunmap_skb_frag(vaddr);
1030 if ((len -= copy) == 0)
1038 if (skb_shinfo(skb)->frag_list) {
1039 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1041 for (; list; list = list->next) {
1044 BUG_TRAP(start <= offset + len);
1046 end = start + list->len;
1047 if ((copy = end - offset) > 0) {
1050 if (skb_store_bits(list, offset - start,
1053 if ((len -= copy) == 0)
1068 EXPORT_SYMBOL(skb_store_bits);
1070 /* Checksum skb data. */
1072 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1073 int len, unsigned int csum)
1075 int start = skb_headlen(skb);
1076 int i, copy = start - offset;
1079 /* Checksum header. */
1083 csum = csum_partial(skb->data + offset, copy, csum);
1084 if ((len -= copy) == 0)
1090 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1093 BUG_TRAP(start <= offset + len);
1095 end = start + skb_shinfo(skb)->frags[i].size;
1096 if ((copy = end - offset) > 0) {
1099 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1103 vaddr = kmap_skb_frag(frag);
1104 csum2 = csum_partial(vaddr + frag->page_offset +
1105 offset - start, copy, 0);
1106 kunmap_skb_frag(vaddr);
1107 csum = csum_block_add(csum, csum2, pos);
1116 if (skb_shinfo(skb)->frag_list) {
1117 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1119 for (; list; list = list->next) {
1122 BUG_TRAP(start <= offset + len);
1124 end = start + list->len;
1125 if ((copy = end - offset) > 0) {
1129 csum2 = skb_checksum(list, offset - start,
1131 csum = csum_block_add(csum, csum2, pos);
1132 if ((len -= copy) == 0)
1146 /* Both of above in one bottle. */
1148 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1149 u8 *to, int len, unsigned int csum)
1151 int start = skb_headlen(skb);
1152 int i, copy = start - offset;
1159 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1161 if ((len -= copy) == 0)
1168 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1171 BUG_TRAP(start <= offset + len);
1173 end = start + skb_shinfo(skb)->frags[i].size;
1174 if ((copy = end - offset) > 0) {
1177 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1181 vaddr = kmap_skb_frag(frag);
1182 csum2 = csum_partial_copy_nocheck(vaddr +
1186 kunmap_skb_frag(vaddr);
1187 csum = csum_block_add(csum, csum2, pos);
1197 if (skb_shinfo(skb)->frag_list) {
1198 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1200 for (; list; list = list->next) {
1204 BUG_TRAP(start <= offset + len);
1206 end = start + list->len;
1207 if ((copy = end - offset) > 0) {
1210 csum2 = skb_copy_and_csum_bits(list,
1213 csum = csum_block_add(csum, csum2, pos);
1214 if ((len -= copy) == 0)
1228 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1233 if (skb->ip_summed == CHECKSUM_HW)
1234 csstart = skb->h.raw - skb->data;
1236 csstart = skb_headlen(skb);
1238 if (csstart > skb_headlen(skb))
1241 memcpy(to, skb->data, csstart);
1244 if (csstart != skb->len)
1245 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1246 skb->len - csstart, 0);
1248 if (skb->ip_summed == CHECKSUM_HW) {
1249 long csstuff = csstart + skb->csum;
1251 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1256 * skb_dequeue - remove from the head of the queue
1257 * @list: list to dequeue from
1259 * Remove the head of the list. The list lock is taken so the function
1260 * may be used safely with other locking list functions. The head item is
1261 * returned or %NULL if the list is empty.
1264 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1266 unsigned long flags;
1267 struct sk_buff *result;
1269 spin_lock_irqsave(&list->lock, flags);
1270 result = __skb_dequeue(list);
1271 spin_unlock_irqrestore(&list->lock, flags);
1276 * skb_dequeue_tail - remove from the tail of the queue
1277 * @list: list to dequeue from
1279 * Remove the tail of the list. The list lock is taken so the function
1280 * may be used safely with other locking list functions. The tail item is
1281 * returned or %NULL if the list is empty.
1283 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1285 unsigned long flags;
1286 struct sk_buff *result;
1288 spin_lock_irqsave(&list->lock, flags);
1289 result = __skb_dequeue_tail(list);
1290 spin_unlock_irqrestore(&list->lock, flags);
1295 * skb_queue_purge - empty a list
1296 * @list: list to empty
1298 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1299 * the list and one reference dropped. This function takes the list
1300 * lock and is atomic with respect to other list locking functions.
1302 void skb_queue_purge(struct sk_buff_head *list)
1304 struct sk_buff *skb;
1305 while ((skb = skb_dequeue(list)) != NULL)
1310 * skb_queue_head - queue a buffer at the list head
1311 * @list: list to use
1312 * @newsk: buffer to queue
1314 * Queue a buffer at the start of the list. This function takes the
1315 * list lock and can be used safely with other locking &sk_buff functions
1318 * A buffer cannot be placed on two lists at the same time.
1320 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1322 unsigned long flags;
1324 spin_lock_irqsave(&list->lock, flags);
1325 __skb_queue_head(list, newsk);
1326 spin_unlock_irqrestore(&list->lock, flags);
1330 * skb_queue_tail - queue a buffer at the list tail
1331 * @list: list to use
1332 * @newsk: buffer to queue
1334 * Queue a buffer at the tail of the list. This function takes the
1335 * list lock and can be used safely with other locking &sk_buff functions
1338 * A buffer cannot be placed on two lists at the same time.
1340 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1342 unsigned long flags;
1344 spin_lock_irqsave(&list->lock, flags);
1345 __skb_queue_tail(list, newsk);
1346 spin_unlock_irqrestore(&list->lock, flags);
1349 * skb_unlink - remove a buffer from a list
1350 * @skb: buffer to remove
1352 * Place a packet after a given packet in a list. The list locks are taken
1353 * and this function is atomic with respect to other list locked calls
1355 * Works even without knowing the list it is sitting on, which can be
1356 * handy at times. It also means that THE LIST MUST EXIST when you
1357 * unlink. Thus a list must have its contents unlinked before it is
1360 void skb_unlink(struct sk_buff *skb)
1362 struct sk_buff_head *list = skb->list;
1365 unsigned long flags;
1367 spin_lock_irqsave(&list->lock, flags);
1368 if (skb->list == list)
1369 __skb_unlink(skb, skb->list);
1370 spin_unlock_irqrestore(&list->lock, flags);
1376 * skb_append - append a buffer
1377 * @old: buffer to insert after
1378 * @newsk: buffer to insert
1380 * Place a packet after a given packet in a list. The list locks are taken
1381 * and this function is atomic with respect to other list locked calls.
1382 * A buffer cannot be placed on two lists at the same time.
1385 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1387 unsigned long flags;
1389 spin_lock_irqsave(&old->list->lock, flags);
1390 __skb_append(old, newsk);
1391 spin_unlock_irqrestore(&old->list->lock, flags);
1396 * skb_insert - insert a buffer
1397 * @old: buffer to insert before
1398 * @newsk: buffer to insert
1400 * Place a packet before a given packet in a list. The list locks are taken
1401 * and this function is atomic with respect to other list locked calls
1402 * A buffer cannot be placed on two lists at the same time.
1405 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1407 unsigned long flags;
1409 spin_lock_irqsave(&old->list->lock, flags);
1410 __skb_insert(newsk, old->prev, old, old->list);
1411 spin_unlock_irqrestore(&old->list->lock, flags);
1416 * Tune the memory allocator for a new MTU size.
1418 void skb_add_mtu(int mtu)
1420 /* Must match allocation in alloc_skb */
1421 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1423 kmem_add_cache_size(mtu);
1427 static inline void skb_split_inside_header(struct sk_buff *skb,
1428 struct sk_buff* skb1,
1429 const u32 len, const int pos)
1433 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1435 /* And move data appendix as is. */
1436 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1437 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1439 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1440 skb_shinfo(skb)->nr_frags = 0;
1441 skb1->data_len = skb->data_len;
1442 skb1->len += skb1->data_len;
1445 skb->tail = skb->data + len;
1448 static inline void skb_split_no_header(struct sk_buff *skb,
1449 struct sk_buff* skb1,
1450 const u32 len, int pos)
1453 const int nfrags = skb_shinfo(skb)->nr_frags;
1455 skb_shinfo(skb)->nr_frags = 0;
1456 skb1->len = skb1->data_len = skb->len - len;
1458 skb->data_len = len - pos;
1460 for (i = 0; i < nfrags; i++) {
1461 int size = skb_shinfo(skb)->frags[i].size;
1463 if (pos + size > len) {
1464 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1468 * We have two variants in this case:
1469 * 1. Move all the frag to the second
1470 * part, if it is possible. F.e.
1471 * this approach is mandatory for TUX,
1472 * where splitting is expensive.
1473 * 2. Split is accurately. We make this.
1475 get_page(skb_shinfo(skb)->frags[i].page);
1476 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1477 skb_shinfo(skb1)->frags[0].size -= len - pos;
1478 skb_shinfo(skb)->frags[i].size = len - pos;
1479 skb_shinfo(skb)->nr_frags++;
1483 skb_shinfo(skb)->nr_frags++;
1486 skb_shinfo(skb1)->nr_frags = k;
1490 * skb_split - Split fragmented skb to two parts at length len.
1491 * @skb: the buffer to split
1492 * @skb1: the buffer to receive the second part
1493 * @len: new length for skb
1495 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1497 int pos = skb_headlen(skb);
1499 if (len < pos) /* Split line is inside header. */
1500 skb_split_inside_header(skb, skb1, len, pos);
1501 else /* Second chunk has no header, nothing to copy. */
1502 skb_split_no_header(skb, skb1, len, pos);
1505 void __init skb_init(void)
1507 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1508 sizeof(struct sk_buff),
1512 if (!skbuff_head_cache)
1513 panic("cannot create skbuff cache");
1516 EXPORT_SYMBOL(___pskb_trim);
1517 EXPORT_SYMBOL(__kfree_skb);
1518 EXPORT_SYMBOL(__pskb_pull_tail);
1519 EXPORT_SYMBOL(alloc_skb);
1520 EXPORT_SYMBOL(pskb_copy);
1521 EXPORT_SYMBOL(pskb_expand_head);
1522 EXPORT_SYMBOL(skb_checksum);
1523 EXPORT_SYMBOL(skb_clone);
1524 EXPORT_SYMBOL(skb_clone_fraglist);
1525 EXPORT_SYMBOL(skb_copy);
1526 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1527 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1528 EXPORT_SYMBOL(skb_copy_bits);
1529 EXPORT_SYMBOL(skb_copy_expand);
1530 EXPORT_SYMBOL(skb_over_panic);
1531 EXPORT_SYMBOL(skb_pad);
1532 EXPORT_SYMBOL(skb_realloc_headroom);
1533 EXPORT_SYMBOL(skb_under_panic);
1534 EXPORT_SYMBOL(skb_dequeue);
1535 EXPORT_SYMBOL(skb_dequeue_tail);
1536 EXPORT_SYMBOL(skb_insert);
1537 EXPORT_SYMBOL(skb_queue_purge);
1538 EXPORT_SYMBOL(skb_queue_head);
1539 EXPORT_SYMBOL(skb_queue_tail);
1540 EXPORT_SYMBOL(skb_unlink);
1541 EXPORT_SYMBOL(skb_append);
1542 EXPORT_SYMBOL(skb_split);