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 BUG_ON(skb->list != NULL);
286 dst_release(skb->dst);
288 secpath_put(skb->sp);
290 if (skb->destructor) {
292 skb->destructor(skb);
294 #ifdef CONFIG_NETFILTER
295 nf_conntrack_put(skb->nfct);
296 #ifdef CONFIG_BRIDGE_NETFILTER
297 nf_bridge_put(skb->nf_bridge);
300 /* XXX: IS this still necessary? - JHS */
301 #ifdef CONFIG_NET_SCHED
303 #ifdef CONFIG_NET_CLS_ACT
313 * skb_clone - duplicate an sk_buff
314 * @skb: buffer to clone
315 * @gfp_mask: allocation priority
317 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
318 * copies share the same packet data but not structure. The new
319 * buffer has a reference count of 1. If the allocation fails the
320 * function returns %NULL otherwise the new buffer is returned.
322 * If this function is called from an interrupt gfp_mask() must be
326 struct sk_buff *skb_clone(struct sk_buff *skb, unsigned int __nocast gfp_mask)
328 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
333 #define C(x) n->x = skb->x
335 n->next = n->prev = NULL;
348 secpath_get(skb->sp);
350 memcpy(n->cb, skb->cb, sizeof(skb->cb));
361 n->destructor = NULL;
362 #ifdef CONFIG_NETFILTER
366 nf_conntrack_get(skb->nfct);
368 #ifdef CONFIG_BRIDGE_NETFILTER
370 nf_bridge_get(skb->nf_bridge);
372 #endif /*CONFIG_NETFILTER*/
373 #if defined(CONFIG_HIPPI)
376 #ifdef CONFIG_NET_SCHED
378 #ifdef CONFIG_NET_CLS_ACT
379 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
380 n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
381 n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
388 atomic_set(&n->users, 1);
394 atomic_inc(&(skb_shinfo(skb)->dataref));
400 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
403 * Shift between the two data areas in bytes
405 unsigned long offset = new->data - old->data;
410 new->real_dev = old->real_dev;
411 new->priority = old->priority;
412 new->protocol = old->protocol;
413 new->dst = dst_clone(old->dst);
415 new->sp = secpath_get(old->sp);
417 new->h.raw = old->h.raw + offset;
418 new->nh.raw = old->nh.raw + offset;
419 new->mac.raw = old->mac.raw + offset;
420 memcpy(new->cb, old->cb, sizeof(old->cb));
421 new->local_df = old->local_df;
422 new->pkt_type = old->pkt_type;
423 new->stamp = old->stamp;
424 new->destructor = NULL;
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_BRIDGE_NETFILTER
432 new->nf_bridge = old->nf_bridge;
433 nf_bridge_get(old->nf_bridge);
436 #ifdef CONFIG_NET_SCHED
437 #ifdef CONFIG_NET_CLS_ACT
438 new->tc_verd = old->tc_verd;
440 new->tc_index = old->tc_index;
442 atomic_set(&new->users, 1);
443 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
444 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
448 * skb_copy - create private copy of an sk_buff
449 * @skb: buffer to copy
450 * @gfp_mask: allocation priority
452 * Make a copy of both an &sk_buff and its data. This is used when the
453 * caller wishes to modify the data and needs a private copy of the
454 * data to alter. Returns %NULL on failure or the pointer to the buffer
455 * on success. The returned buffer has a reference count of 1.
457 * As by-product this function converts non-linear &sk_buff to linear
458 * one, so that &sk_buff becomes completely private and caller is allowed
459 * to modify all the data of returned buffer. This means that this
460 * function is not recommended for use in circumstances when only
461 * header is going to be modified. Use pskb_copy() instead.
464 struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask)
466 int headerlen = skb->data - skb->head;
468 * Allocate the copy buffer
470 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
475 /* Set the data pointer */
476 skb_reserve(n, headerlen);
477 /* Set the tail pointer and length */
478 skb_put(n, skb->len);
480 n->ip_summed = skb->ip_summed;
482 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
485 copy_skb_header(n, skb);
491 * pskb_copy - create copy of an sk_buff with private head.
492 * @skb: buffer to copy
493 * @gfp_mask: allocation priority
495 * Make a copy of both an &sk_buff and part of its data, located
496 * in header. Fragmented data remain shared. This is used when
497 * the caller wishes to modify only header of &sk_buff and needs
498 * private copy of the header to alter. Returns %NULL on failure
499 * or the pointer to the buffer on success.
500 * The returned buffer has a reference count of 1.
503 struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask)
506 * Allocate the copy buffer
508 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
513 /* Set the data pointer */
514 skb_reserve(n, skb->data - skb->head);
515 /* Set the tail pointer and length */
516 skb_put(n, skb_headlen(skb));
518 memcpy(n->data, skb->data, n->len);
520 n->ip_summed = skb->ip_summed;
522 n->data_len = skb->data_len;
525 if (skb_shinfo(skb)->nr_frags) {
528 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
529 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
530 get_page(skb_shinfo(n)->frags[i].page);
532 skb_shinfo(n)->nr_frags = i;
535 if (skb_shinfo(skb)->frag_list) {
536 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
537 skb_clone_fraglist(n);
540 copy_skb_header(n, skb);
546 * pskb_expand_head - reallocate header of &sk_buff
547 * @skb: buffer to reallocate
548 * @nhead: room to add at head
549 * @ntail: room to add at tail
550 * @gfp_mask: allocation priority
552 * Expands (or creates identical copy, if &nhead and &ntail are zero)
553 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
554 * reference count of 1. Returns zero in the case of success or error,
555 * if expansion failed. In the last case, &sk_buff is not changed.
557 * All the pointers pointing into skb header may change and must be
558 * reloaded after call to this function.
561 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
562 unsigned int __nocast gfp_mask)
566 int size = nhead + (skb->end - skb->head) + ntail;
572 size = SKB_DATA_ALIGN(size);
574 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
578 /* Copy only real data... and, alas, header. This should be
579 * optimized for the cases when header is void. */
580 memcpy(data + nhead, skb->head, skb->tail - skb->head);
581 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
583 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
584 get_page(skb_shinfo(skb)->frags[i].page);
586 if (skb_shinfo(skb)->frag_list)
587 skb_clone_fraglist(skb);
589 skb_release_data(skb);
591 off = (data + nhead) - skb->head;
594 skb->end = data + size;
602 atomic_set(&skb_shinfo(skb)->dataref, 1);
609 /* Make private copy of skb with writable head and some headroom */
611 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
613 struct sk_buff *skb2;
614 int delta = headroom - skb_headroom(skb);
617 skb2 = pskb_copy(skb, GFP_ATOMIC);
619 skb2 = skb_clone(skb, GFP_ATOMIC);
620 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
631 * skb_copy_expand - copy and expand sk_buff
632 * @skb: buffer to copy
633 * @newheadroom: new free bytes at head
634 * @newtailroom: new free bytes at tail
635 * @gfp_mask: allocation priority
637 * Make a copy of both an &sk_buff and its data and while doing so
638 * allocate additional space.
640 * This is used when the caller wishes to modify the data and needs a
641 * private copy of the data to alter as well as more space for new fields.
642 * Returns %NULL on failure or the pointer to the buffer
643 * on success. The returned buffer has a reference count of 1.
645 * You must pass %GFP_ATOMIC as the allocation priority if this function
646 * is called from an interrupt.
648 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
649 * only by netfilter in the cases when checksum is recalculated? --ANK
651 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
652 int newheadroom, int newtailroom,
653 unsigned int __nocast gfp_mask)
656 * Allocate the copy buffer
658 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
660 int head_copy_len, head_copy_off;
665 skb_reserve(n, newheadroom);
667 /* Set the tail pointer and length */
668 skb_put(n, skb->len);
670 head_copy_len = skb_headroom(skb);
672 if (newheadroom <= head_copy_len)
673 head_copy_len = newheadroom;
675 head_copy_off = newheadroom - head_copy_len;
677 /* Copy the linear header and data. */
678 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
679 skb->len + head_copy_len))
682 copy_skb_header(n, skb);
688 * skb_pad - zero pad the tail of an skb
689 * @skb: buffer to pad
692 * Ensure that a buffer is followed by a padding area that is zero
693 * filled. Used by network drivers which may DMA or transfer data
694 * beyond the buffer end onto the wire.
696 * May return NULL in out of memory cases.
699 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
701 struct sk_buff *nskb;
703 /* If the skbuff is non linear tailroom is always zero.. */
704 if (skb_tailroom(skb) >= pad) {
705 memset(skb->data+skb->len, 0, pad);
709 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
712 memset(nskb->data+nskb->len, 0, pad);
716 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
717 * If realloc==0 and trimming is impossible without change of data,
721 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
723 int offset = skb_headlen(skb);
724 int nfrags = skb_shinfo(skb)->nr_frags;
727 for (i = 0; i < nfrags; i++) {
728 int end = offset + skb_shinfo(skb)->frags[i].size;
730 if (skb_cloned(skb)) {
733 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
737 put_page(skb_shinfo(skb)->frags[i].page);
738 skb_shinfo(skb)->nr_frags--;
740 skb_shinfo(skb)->frags[i].size = len - offset;
747 skb->data_len -= skb->len - len;
750 if (len <= skb_headlen(skb)) {
753 skb->tail = skb->data + len;
754 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
755 skb_drop_fraglist(skb);
757 skb->data_len -= skb->len - len;
766 * __pskb_pull_tail - advance tail of skb header
767 * @skb: buffer to reallocate
768 * @delta: number of bytes to advance tail
770 * The function makes a sense only on a fragmented &sk_buff,
771 * it expands header moving its tail forward and copying necessary
772 * data from fragmented part.
774 * &sk_buff MUST have reference count of 1.
776 * Returns %NULL (and &sk_buff does not change) if pull failed
777 * or value of new tail of skb in the case of success.
779 * All the pointers pointing into skb header may change and must be
780 * reloaded after call to this function.
783 /* Moves tail of skb head forward, copying data from fragmented part,
784 * when it is necessary.
785 * 1. It may fail due to malloc failure.
786 * 2. It may change skb pointers.
788 * It is pretty complicated. Luckily, it is called only in exceptional cases.
790 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
792 /* If skb has not enough free space at tail, get new one
793 * plus 128 bytes for future expansions. If we have enough
794 * room at tail, reallocate without expansion only if skb is cloned.
796 int i, k, eat = (skb->tail + delta) - skb->end;
798 if (eat > 0 || skb_cloned(skb)) {
799 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
804 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
807 /* Optimization: no fragments, no reasons to preestimate
808 * size of pulled pages. Superb.
810 if (!skb_shinfo(skb)->frag_list)
813 /* Estimate size of pulled pages. */
815 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
816 if (skb_shinfo(skb)->frags[i].size >= eat)
818 eat -= skb_shinfo(skb)->frags[i].size;
821 /* If we need update frag list, we are in troubles.
822 * Certainly, it possible to add an offset to skb data,
823 * but taking into account that pulling is expected to
824 * be very rare operation, it is worth to fight against
825 * further bloating skb head and crucify ourselves here instead.
826 * Pure masohism, indeed. 8)8)
829 struct sk_buff *list = skb_shinfo(skb)->frag_list;
830 struct sk_buff *clone = NULL;
831 struct sk_buff *insp = NULL;
837 if (list->len <= eat) {
838 /* Eaten as whole. */
843 /* Eaten partially. */
845 if (skb_shared(list)) {
846 /* Sucks! We need to fork list. :-( */
847 clone = skb_clone(list, GFP_ATOMIC);
853 /* This may be pulled without
857 if (!pskb_pull(list, eat)) {
866 /* Free pulled out fragments. */
867 while ((list = skb_shinfo(skb)->frag_list) != insp) {
868 skb_shinfo(skb)->frag_list = list->next;
871 /* And insert new clone at head. */
874 skb_shinfo(skb)->frag_list = clone;
877 /* Success! Now we may commit changes to skb data. */
882 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
883 if (skb_shinfo(skb)->frags[i].size <= eat) {
884 put_page(skb_shinfo(skb)->frags[i].page);
885 eat -= skb_shinfo(skb)->frags[i].size;
887 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
889 skb_shinfo(skb)->frags[k].page_offset += eat;
890 skb_shinfo(skb)->frags[k].size -= eat;
896 skb_shinfo(skb)->nr_frags = k;
899 skb->data_len -= delta;
904 /* Copy some data bits from skb to kernel buffer. */
906 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
909 int start = skb_headlen(skb);
911 if (offset > (int)skb->len - len)
915 if ((copy = start - offset) > 0) {
918 memcpy(to, skb->data + offset, copy);
919 if ((len -= copy) == 0)
925 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
928 BUG_TRAP(start <= offset + len);
930 end = start + skb_shinfo(skb)->frags[i].size;
931 if ((copy = end - offset) > 0) {
937 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
939 vaddr + skb_shinfo(skb)->frags[i].page_offset+
940 offset - start, copy);
941 kunmap_skb_frag(vaddr);
943 if ((len -= copy) == 0)
951 if (skb_shinfo(skb)->frag_list) {
952 struct sk_buff *list = skb_shinfo(skb)->frag_list;
954 for (; list; list = list->next) {
957 BUG_TRAP(start <= offset + len);
959 end = start + list->len;
960 if ((copy = end - offset) > 0) {
963 if (skb_copy_bits(list, offset - start,
966 if ((len -= copy) == 0)
982 * skb_store_bits - store bits from kernel buffer to skb
983 * @skb: destination buffer
984 * @offset: offset in destination
985 * @from: source buffer
986 * @len: number of bytes to copy
988 * Copy the specified number of bytes from the source buffer to the
989 * destination skb. This function handles all the messy bits of
990 * traversing fragment lists and such.
993 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
996 int start = skb_headlen(skb);
998 if (offset > (int)skb->len - len)
1001 if ((copy = start - offset) > 0) {
1004 memcpy(skb->data + offset, from, copy);
1005 if ((len -= copy) == 0)
1011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1012 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1015 BUG_TRAP(start <= offset + len);
1017 end = start + frag->size;
1018 if ((copy = end - offset) > 0) {
1024 vaddr = kmap_skb_frag(frag);
1025 memcpy(vaddr + frag->page_offset + offset - start,
1027 kunmap_skb_frag(vaddr);
1029 if ((len -= copy) == 0)
1037 if (skb_shinfo(skb)->frag_list) {
1038 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1040 for (; list; list = list->next) {
1043 BUG_TRAP(start <= offset + len);
1045 end = start + list->len;
1046 if ((copy = end - offset) > 0) {
1049 if (skb_store_bits(list, offset - start,
1052 if ((len -= copy) == 0)
1067 EXPORT_SYMBOL(skb_store_bits);
1069 /* Checksum skb data. */
1071 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1072 int len, unsigned int csum)
1074 int start = skb_headlen(skb);
1075 int i, copy = start - offset;
1078 /* Checksum header. */
1082 csum = csum_partial(skb->data + offset, copy, csum);
1083 if ((len -= copy) == 0)
1089 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1092 BUG_TRAP(start <= offset + len);
1094 end = start + skb_shinfo(skb)->frags[i].size;
1095 if ((copy = end - offset) > 0) {
1098 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1102 vaddr = kmap_skb_frag(frag);
1103 csum2 = csum_partial(vaddr + frag->page_offset +
1104 offset - start, copy, 0);
1105 kunmap_skb_frag(vaddr);
1106 csum = csum_block_add(csum, csum2, pos);
1115 if (skb_shinfo(skb)->frag_list) {
1116 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1118 for (; list; list = list->next) {
1121 BUG_TRAP(start <= offset + len);
1123 end = start + list->len;
1124 if ((copy = end - offset) > 0) {
1128 csum2 = skb_checksum(list, offset - start,
1130 csum = csum_block_add(csum, csum2, pos);
1131 if ((len -= copy) == 0)
1145 /* Both of above in one bottle. */
1147 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1148 u8 *to, int len, unsigned int csum)
1150 int start = skb_headlen(skb);
1151 int i, copy = start - offset;
1158 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1160 if ((len -= copy) == 0)
1167 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1170 BUG_TRAP(start <= offset + len);
1172 end = start + skb_shinfo(skb)->frags[i].size;
1173 if ((copy = end - offset) > 0) {
1176 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1180 vaddr = kmap_skb_frag(frag);
1181 csum2 = csum_partial_copy_nocheck(vaddr +
1185 kunmap_skb_frag(vaddr);
1186 csum = csum_block_add(csum, csum2, pos);
1196 if (skb_shinfo(skb)->frag_list) {
1197 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1199 for (; list; list = list->next) {
1203 BUG_TRAP(start <= offset + len);
1205 end = start + list->len;
1206 if ((copy = end - offset) > 0) {
1209 csum2 = skb_copy_and_csum_bits(list,
1212 csum = csum_block_add(csum, csum2, pos);
1213 if ((len -= copy) == 0)
1227 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1232 if (skb->ip_summed == CHECKSUM_HW)
1233 csstart = skb->h.raw - skb->data;
1235 csstart = skb_headlen(skb);
1237 if (csstart > skb_headlen(skb))
1240 memcpy(to, skb->data, csstart);
1243 if (csstart != skb->len)
1244 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1245 skb->len - csstart, 0);
1247 if (skb->ip_summed == CHECKSUM_HW) {
1248 long csstuff = csstart + skb->csum;
1250 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1255 * skb_dequeue - remove from the head of the queue
1256 * @list: list to dequeue from
1258 * Remove the head of the list. The list lock is taken so the function
1259 * may be used safely with other locking list functions. The head item is
1260 * returned or %NULL if the list is empty.
1263 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1265 unsigned long flags;
1266 struct sk_buff *result;
1268 spin_lock_irqsave(&list->lock, flags);
1269 result = __skb_dequeue(list);
1270 spin_unlock_irqrestore(&list->lock, flags);
1275 * skb_dequeue_tail - remove from the tail of the queue
1276 * @list: list to dequeue from
1278 * Remove the tail of the list. The list lock is taken so the function
1279 * may be used safely with other locking list functions. The tail item is
1280 * returned or %NULL if the list is empty.
1282 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1284 unsigned long flags;
1285 struct sk_buff *result;
1287 spin_lock_irqsave(&list->lock, flags);
1288 result = __skb_dequeue_tail(list);
1289 spin_unlock_irqrestore(&list->lock, flags);
1294 * skb_queue_purge - empty a list
1295 * @list: list to empty
1297 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1298 * the list and one reference dropped. This function takes the list
1299 * lock and is atomic with respect to other list locking functions.
1301 void skb_queue_purge(struct sk_buff_head *list)
1303 struct sk_buff *skb;
1304 while ((skb = skb_dequeue(list)) != NULL)
1309 * skb_queue_head - queue a buffer at the list head
1310 * @list: list to use
1311 * @newsk: buffer to queue
1313 * Queue a buffer at the start of the list. This function takes the
1314 * list lock and can be used safely with other locking &sk_buff functions
1317 * A buffer cannot be placed on two lists at the same time.
1319 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1321 unsigned long flags;
1323 spin_lock_irqsave(&list->lock, flags);
1324 __skb_queue_head(list, newsk);
1325 spin_unlock_irqrestore(&list->lock, flags);
1329 * skb_queue_tail - queue a buffer at the list tail
1330 * @list: list to use
1331 * @newsk: buffer to queue
1333 * Queue a buffer at the tail of the list. This function takes the
1334 * list lock and can be used safely with other locking &sk_buff functions
1337 * A buffer cannot be placed on two lists at the same time.
1339 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1341 unsigned long flags;
1343 spin_lock_irqsave(&list->lock, flags);
1344 __skb_queue_tail(list, newsk);
1345 spin_unlock_irqrestore(&list->lock, flags);
1348 * skb_unlink - remove a buffer from a list
1349 * @skb: buffer to remove
1351 * Place a packet after a given packet in a list. The list locks are taken
1352 * and this function is atomic with respect to other list locked calls
1354 * Works even without knowing the list it is sitting on, which can be
1355 * handy at times. It also means that THE LIST MUST EXIST when you
1356 * unlink. Thus a list must have its contents unlinked before it is
1359 void skb_unlink(struct sk_buff *skb)
1361 struct sk_buff_head *list = skb->list;
1364 unsigned long flags;
1366 spin_lock_irqsave(&list->lock, flags);
1367 if (skb->list == list)
1368 __skb_unlink(skb, skb->list);
1369 spin_unlock_irqrestore(&list->lock, flags);
1375 * skb_append - append a buffer
1376 * @old: buffer to insert after
1377 * @newsk: buffer to insert
1379 * Place a packet after a given packet in a list. The list locks are taken
1380 * and this function is atomic with respect to other list locked calls.
1381 * A buffer cannot be placed on two lists at the same time.
1384 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1386 unsigned long flags;
1388 spin_lock_irqsave(&old->list->lock, flags);
1389 __skb_append(old, newsk);
1390 spin_unlock_irqrestore(&old->list->lock, flags);
1395 * skb_insert - insert a buffer
1396 * @old: buffer to insert before
1397 * @newsk: buffer to insert
1399 * Place a packet before a given packet in a list. The list locks are taken
1400 * and this function is atomic with respect to other list locked calls
1401 * A buffer cannot be placed on two lists at the same time.
1404 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1406 unsigned long flags;
1408 spin_lock_irqsave(&old->list->lock, flags);
1409 __skb_insert(newsk, old->prev, old, old->list);
1410 spin_unlock_irqrestore(&old->list->lock, flags);
1415 * Tune the memory allocator for a new MTU size.
1417 void skb_add_mtu(int mtu)
1419 /* Must match allocation in alloc_skb */
1420 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1422 kmem_add_cache_size(mtu);
1426 static inline void skb_split_inside_header(struct sk_buff *skb,
1427 struct sk_buff* skb1,
1428 const u32 len, const int pos)
1432 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1434 /* And move data appendix as is. */
1435 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1436 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1438 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1439 skb_shinfo(skb)->nr_frags = 0;
1440 skb1->data_len = skb->data_len;
1441 skb1->len += skb1->data_len;
1444 skb->tail = skb->data + len;
1447 static inline void skb_split_no_header(struct sk_buff *skb,
1448 struct sk_buff* skb1,
1449 const u32 len, int pos)
1452 const int nfrags = skb_shinfo(skb)->nr_frags;
1454 skb_shinfo(skb)->nr_frags = 0;
1455 skb1->len = skb1->data_len = skb->len - len;
1457 skb->data_len = len - pos;
1459 for (i = 0; i < nfrags; i++) {
1460 int size = skb_shinfo(skb)->frags[i].size;
1462 if (pos + size > len) {
1463 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1467 * We have two variants in this case:
1468 * 1. Move all the frag to the second
1469 * part, if it is possible. F.e.
1470 * this approach is mandatory for TUX,
1471 * where splitting is expensive.
1472 * 2. Split is accurately. We make this.
1474 get_page(skb_shinfo(skb)->frags[i].page);
1475 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1476 skb_shinfo(skb1)->frags[0].size -= len - pos;
1477 skb_shinfo(skb)->frags[i].size = len - pos;
1478 skb_shinfo(skb)->nr_frags++;
1482 skb_shinfo(skb)->nr_frags++;
1485 skb_shinfo(skb1)->nr_frags = k;
1489 * skb_split - Split fragmented skb to two parts at length len.
1490 * @skb: the buffer to split
1491 * @skb1: the buffer to receive the second part
1492 * @len: new length for skb
1494 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1496 int pos = skb_headlen(skb);
1498 if (len < pos) /* Split line is inside header. */
1499 skb_split_inside_header(skb, skb1, len, pos);
1500 else /* Second chunk has no header, nothing to copy. */
1501 skb_split_no_header(skb, skb1, len, pos);
1505 * skb_prepare_seq_read - Prepare a sequential read of skb data
1506 * @skb: the buffer to read
1507 * @from: lower offset of data to be read
1508 * @to: upper offset of data to be read
1509 * @st: state variable
1511 * Initializes the specified state variable. Must be called before
1512 * invoking skb_seq_read() for the first time.
1514 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1515 unsigned int to, struct skb_seq_state *st)
1517 st->lower_offset = from;
1518 st->upper_offset = to;
1519 st->root_skb = st->cur_skb = skb;
1520 st->frag_idx = st->stepped_offset = 0;
1521 st->frag_data = NULL;
1525 * skb_seq_read - Sequentially read skb data
1526 * @consumed: number of bytes consumed by the caller so far
1527 * @data: destination pointer for data to be returned
1528 * @st: state variable
1530 * Reads a block of skb data at &consumed relative to the
1531 * lower offset specified to skb_prepare_seq_read(). Assigns
1532 * the head of the data block to &data and returns the length
1533 * of the block or 0 if the end of the skb data or the upper
1534 * offset has been reached.
1536 * The caller is not required to consume all of the data
1537 * returned, i.e. &consumed is typically set to the number
1538 * of bytes already consumed and the next call to
1539 * skb_seq_read() will return the remaining part of the block.
1541 * Note: The size of each block of data returned can be arbitary,
1542 * this limitation is the cost for zerocopy seqeuental
1543 * reads of potentially non linear data.
1545 * Note: Fragment lists within fragments are not implemented
1546 * at the moment, state->root_skb could be replaced with
1547 * a stack for this purpose.
1549 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1550 struct skb_seq_state *st)
1552 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1555 if (unlikely(abs_offset >= st->upper_offset))
1559 block_limit = skb_headlen(st->cur_skb);
1561 if (abs_offset < block_limit) {
1562 *data = st->cur_skb->data + abs_offset;
1563 return block_limit - abs_offset;
1566 if (st->frag_idx == 0 && !st->frag_data)
1567 st->stepped_offset += skb_headlen(st->cur_skb);
1569 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1570 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1571 block_limit = frag->size + st->stepped_offset;
1573 if (abs_offset < block_limit) {
1575 st->frag_data = kmap_skb_frag(frag);
1577 *data = (u8 *) st->frag_data + frag->page_offset +
1578 (abs_offset - st->stepped_offset);
1580 return block_limit - abs_offset;
1583 if (st->frag_data) {
1584 kunmap_skb_frag(st->frag_data);
1585 st->frag_data = NULL;
1589 st->stepped_offset += frag->size;
1592 if (st->cur_skb->next) {
1593 st->cur_skb = st->cur_skb->next;
1596 } else if (st->root_skb == st->cur_skb &&
1597 skb_shinfo(st->root_skb)->frag_list) {
1598 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1606 * skb_abort_seq_read - Abort a sequential read of skb data
1607 * @st: state variable
1609 * Must be called if skb_seq_read() was not called until it
1612 void skb_abort_seq_read(struct skb_seq_state *st)
1615 kunmap_skb_frag(st->frag_data);
1618 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1620 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1621 struct ts_config *conf,
1622 struct ts_state *state)
1624 return skb_seq_read(offset, text, TS_SKB_CB(state));
1627 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1629 skb_abort_seq_read(TS_SKB_CB(state));
1633 * skb_find_text - Find a text pattern in skb data
1634 * @skb: the buffer to look in
1635 * @from: search offset
1637 * @config: textsearch configuration
1638 * @state: uninitialized textsearch state variable
1640 * Finds a pattern in the skb data according to the specified
1641 * textsearch configuration. Use textsearch_next() to retrieve
1642 * subsequent occurrences of the pattern. Returns the offset
1643 * to the first occurrence or UINT_MAX if no match was found.
1645 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1646 unsigned int to, struct ts_config *config,
1647 struct ts_state *state)
1649 config->get_next_block = skb_ts_get_next_block;
1650 config->finish = skb_ts_finish;
1652 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1654 return textsearch_find(config, state);
1657 void __init skb_init(void)
1659 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1660 sizeof(struct sk_buff),
1664 if (!skbuff_head_cache)
1665 panic("cannot create skbuff cache");
1668 EXPORT_SYMBOL(___pskb_trim);
1669 EXPORT_SYMBOL(__kfree_skb);
1670 EXPORT_SYMBOL(__pskb_pull_tail);
1671 EXPORT_SYMBOL(alloc_skb);
1672 EXPORT_SYMBOL(pskb_copy);
1673 EXPORT_SYMBOL(pskb_expand_head);
1674 EXPORT_SYMBOL(skb_checksum);
1675 EXPORT_SYMBOL(skb_clone);
1676 EXPORT_SYMBOL(skb_clone_fraglist);
1677 EXPORT_SYMBOL(skb_copy);
1678 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1679 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1680 EXPORT_SYMBOL(skb_copy_bits);
1681 EXPORT_SYMBOL(skb_copy_expand);
1682 EXPORT_SYMBOL(skb_over_panic);
1683 EXPORT_SYMBOL(skb_pad);
1684 EXPORT_SYMBOL(skb_realloc_headroom);
1685 EXPORT_SYMBOL(skb_under_panic);
1686 EXPORT_SYMBOL(skb_dequeue);
1687 EXPORT_SYMBOL(skb_dequeue_tail);
1688 EXPORT_SYMBOL(skb_insert);
1689 EXPORT_SYMBOL(skb_queue_purge);
1690 EXPORT_SYMBOL(skb_queue_head);
1691 EXPORT_SYMBOL(skb_queue_tail);
1692 EXPORT_SYMBOL(skb_unlink);
1693 EXPORT_SYMBOL(skb_append);
1694 EXPORT_SYMBOL(skb_split);
1695 EXPORT_SYMBOL(skb_prepare_seq_read);
1696 EXPORT_SYMBOL(skb_seq_read);
1697 EXPORT_SYMBOL(skb_abort_seq_read);
1698 EXPORT_SYMBOL(skb_find_text);