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 __read_mostly;
72 static kmem_cache_t *skbuff_fclone_cache __read_mostly;
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
81 * skb_over_panic - private function
86 * Out of line support code for skb_put(). Not user callable.
88 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
98 * skb_under_panic - private function
103 * Out of line support code for skb_push(). Not user callable.
106 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
115 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
116 * 'private' fields and also do memory statistics to find all the
122 * __alloc_skb - allocate a network buffer
123 * @size: size to allocate
124 * @gfp_mask: allocation mask
125 * @fclone: allocate from fclone cache instead of head cache
126 * and allocate a cloned (child) skb
128 * Allocate a new &sk_buff. The returned buffer has no headroom and a
129 * tail room of size bytes. The object has a reference count of one.
130 * The return is the buffer. On a failure the return is %NULL.
132 * Buffers may only be allocated from interrupts using a @gfp_mask of
135 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
139 struct skb_shared_info *shinfo;
143 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
146 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
150 /* Get the DATA. Size must match skb_add_mtu(). */
151 size = SKB_DATA_ALIGN(size);
152 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
156 memset(skb, 0, offsetof(struct sk_buff, truesize));
157 skb->truesize = size + sizeof(struct sk_buff);
158 atomic_set(&skb->users, 1);
162 skb->end = data + size;
163 /* make sure we initialize shinfo sequentially */
164 shinfo = skb_shinfo(skb);
165 atomic_set(&shinfo->dataref, 1);
166 shinfo->nr_frags = 0;
167 shinfo->tso_size = 0;
168 shinfo->tso_segs = 0;
169 shinfo->ufo_size = 0;
170 shinfo->ip6_frag_id = 0;
171 shinfo->frag_list = NULL;
174 struct sk_buff *child = skb + 1;
175 atomic_t *fclone_ref = (atomic_t *) (child + 1);
177 skb->fclone = SKB_FCLONE_ORIG;
178 atomic_set(fclone_ref, 1);
180 child->fclone = SKB_FCLONE_UNAVAILABLE;
185 kmem_cache_free(cache, skb);
191 * alloc_skb_from_cache - allocate a network buffer
192 * @cp: kmem_cache from which to allocate the data area
193 * (object size must be big enough for @size bytes + skb overheads)
194 * @size: size to allocate
195 * @gfp_mask: allocation mask
197 * Allocate a new &sk_buff. The returned buffer has no headroom and
198 * tail room of size bytes. The object has a reference count of one.
199 * The return is the buffer. On a failure the return is %NULL.
201 * Buffers may only be allocated from interrupts using a @gfp_mask of
204 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
212 skb = kmem_cache_alloc(skbuff_head_cache,
213 gfp_mask & ~__GFP_DMA);
218 size = SKB_DATA_ALIGN(size);
219 data = kmem_cache_alloc(cp, gfp_mask);
223 memset(skb, 0, offsetof(struct sk_buff, truesize));
224 skb->truesize = size + sizeof(struct sk_buff);
225 atomic_set(&skb->users, 1);
229 skb->end = data + size;
231 atomic_set(&(skb_shinfo(skb)->dataref), 1);
232 skb_shinfo(skb)->nr_frags = 0;
233 skb_shinfo(skb)->tso_size = 0;
234 skb_shinfo(skb)->tso_segs = 0;
235 skb_shinfo(skb)->frag_list = NULL;
239 kmem_cache_free(skbuff_head_cache, skb);
245 static void skb_drop_fraglist(struct sk_buff *skb)
247 struct sk_buff *list = skb_shinfo(skb)->frag_list;
249 skb_shinfo(skb)->frag_list = NULL;
252 struct sk_buff *this = list;
258 static void skb_clone_fraglist(struct sk_buff *skb)
260 struct sk_buff *list;
262 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
266 void skb_release_data(struct sk_buff *skb)
269 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
270 &skb_shinfo(skb)->dataref)) {
271 if (skb_shinfo(skb)->nr_frags) {
273 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
274 put_page(skb_shinfo(skb)->frags[i].page);
277 if (skb_shinfo(skb)->frag_list)
278 skb_drop_fraglist(skb);
285 * Free an skbuff by memory without cleaning the state.
287 void kfree_skbmem(struct sk_buff *skb)
289 struct sk_buff *other;
290 atomic_t *fclone_ref;
292 skb_release_data(skb);
293 switch (skb->fclone) {
294 case SKB_FCLONE_UNAVAILABLE:
295 kmem_cache_free(skbuff_head_cache, skb);
298 case SKB_FCLONE_ORIG:
299 fclone_ref = (atomic_t *) (skb + 2);
300 if (atomic_dec_and_test(fclone_ref))
301 kmem_cache_free(skbuff_fclone_cache, skb);
304 case SKB_FCLONE_CLONE:
305 fclone_ref = (atomic_t *) (skb + 1);
308 /* The clone portion is available for
309 * fast-cloning again.
311 skb->fclone = SKB_FCLONE_UNAVAILABLE;
313 if (atomic_dec_and_test(fclone_ref))
314 kmem_cache_free(skbuff_fclone_cache, other);
320 * __kfree_skb - private function
323 * Free an sk_buff. Release anything attached to the buffer.
324 * Clean the state. This is an internal helper function. Users should
325 * always call kfree_skb
328 void __kfree_skb(struct sk_buff *skb)
330 dst_release(skb->dst);
332 secpath_put(skb->sp);
334 if (skb->destructor) {
336 skb->destructor(skb);
338 #ifdef CONFIG_NETFILTER
339 nf_conntrack_put(skb->nfct);
340 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
341 nf_conntrack_put_reasm(skb->nfct_reasm);
343 #ifdef CONFIG_BRIDGE_NETFILTER
344 nf_bridge_put(skb->nf_bridge);
347 /* XXX: IS this still necessary? - JHS */
348 #ifdef CONFIG_NET_SCHED
350 #ifdef CONFIG_NET_CLS_ACT
359 * skb_clone - duplicate an sk_buff
360 * @skb: buffer to clone
361 * @gfp_mask: allocation priority
363 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
364 * copies share the same packet data but not structure. The new
365 * buffer has a reference count of 1. If the allocation fails the
366 * function returns %NULL otherwise the new buffer is returned.
368 * If this function is called from an interrupt gfp_mask() must be
372 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
377 if (skb->fclone == SKB_FCLONE_ORIG &&
378 n->fclone == SKB_FCLONE_UNAVAILABLE) {
379 atomic_t *fclone_ref = (atomic_t *) (n + 1);
380 n->fclone = SKB_FCLONE_CLONE;
381 atomic_inc(fclone_ref);
383 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
386 n->fclone = SKB_FCLONE_UNAVAILABLE;
389 #define C(x) n->x = skb->x
391 n->next = n->prev = NULL;
402 secpath_get(skb->sp);
404 memcpy(n->cb, skb->cb, sizeof(skb->cb));
414 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
418 n->destructor = NULL;
419 #ifdef CONFIG_NETFILTER
422 nf_conntrack_get(skb->nfct);
424 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
426 nf_conntrack_get_reasm(skb->nfct_reasm);
428 #ifdef CONFIG_BRIDGE_NETFILTER
430 nf_bridge_get(skb->nf_bridge);
432 #endif /*CONFIG_NETFILTER*/
433 #ifdef CONFIG_NET_SCHED
435 #ifdef CONFIG_NET_CLS_ACT
436 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
437 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
438 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
444 atomic_set(&n->users, 1);
450 atomic_inc(&(skb_shinfo(skb)->dataref));
456 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
459 * Shift between the two data areas in bytes
461 unsigned long offset = new->data - old->data;
465 new->priority = old->priority;
466 new->protocol = old->protocol;
467 new->dst = dst_clone(old->dst);
469 new->sp = secpath_get(old->sp);
471 new->h.raw = old->h.raw + offset;
472 new->nh.raw = old->nh.raw + offset;
473 new->mac.raw = old->mac.raw + offset;
474 memcpy(new->cb, old->cb, sizeof(old->cb));
475 new->local_df = old->local_df;
476 new->fclone = SKB_FCLONE_UNAVAILABLE;
477 new->pkt_type = old->pkt_type;
478 new->tstamp = old->tstamp;
479 new->destructor = NULL;
480 #ifdef CONFIG_NETFILTER
481 new->nfmark = old->nfmark;
482 new->nfct = old->nfct;
483 nf_conntrack_get(old->nfct);
484 new->nfctinfo = old->nfctinfo;
485 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
486 new->nfct_reasm = old->nfct_reasm;
487 nf_conntrack_get_reasm(old->nfct_reasm);
489 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
490 new->ipvs_property = old->ipvs_property;
492 #ifdef CONFIG_BRIDGE_NETFILTER
493 new->nf_bridge = old->nf_bridge;
494 nf_bridge_get(old->nf_bridge);
497 #ifdef CONFIG_NET_SCHED
498 #ifdef CONFIG_NET_CLS_ACT
499 new->tc_verd = old->tc_verd;
501 new->tc_index = old->tc_index;
503 atomic_set(&new->users, 1);
504 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
505 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
509 * skb_copy - create private copy of an sk_buff
510 * @skb: buffer to copy
511 * @gfp_mask: allocation priority
513 * Make a copy of both an &sk_buff and its data. This is used when the
514 * caller wishes to modify the data and needs a private copy of the
515 * data to alter. Returns %NULL on failure or the pointer to the buffer
516 * on success. The returned buffer has a reference count of 1.
518 * As by-product this function converts non-linear &sk_buff to linear
519 * one, so that &sk_buff becomes completely private and caller is allowed
520 * to modify all the data of returned buffer. This means that this
521 * function is not recommended for use in circumstances when only
522 * header is going to be modified. Use pskb_copy() instead.
525 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
527 int headerlen = skb->data - skb->head;
529 * Allocate the copy buffer
531 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
536 /* Set the data pointer */
537 skb_reserve(n, headerlen);
538 /* Set the tail pointer and length */
539 skb_put(n, skb->len);
541 n->ip_summed = skb->ip_summed;
543 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
546 copy_skb_header(n, skb);
552 * pskb_copy - create copy of an sk_buff with private head.
553 * @skb: buffer to copy
554 * @gfp_mask: allocation priority
556 * Make a copy of both an &sk_buff and part of its data, located
557 * in header. Fragmented data remain shared. This is used when
558 * the caller wishes to modify only header of &sk_buff and needs
559 * private copy of the header to alter. Returns %NULL on failure
560 * or the pointer to the buffer on success.
561 * The returned buffer has a reference count of 1.
564 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
567 * Allocate the copy buffer
569 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
574 /* Set the data pointer */
575 skb_reserve(n, skb->data - skb->head);
576 /* Set the tail pointer and length */
577 skb_put(n, skb_headlen(skb));
579 memcpy(n->data, skb->data, n->len);
581 n->ip_summed = skb->ip_summed;
583 n->data_len = skb->data_len;
586 if (skb_shinfo(skb)->nr_frags) {
589 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
590 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
591 get_page(skb_shinfo(n)->frags[i].page);
593 skb_shinfo(n)->nr_frags = i;
596 if (skb_shinfo(skb)->frag_list) {
597 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
598 skb_clone_fraglist(n);
601 copy_skb_header(n, skb);
607 * pskb_expand_head - reallocate header of &sk_buff
608 * @skb: buffer to reallocate
609 * @nhead: room to add at head
610 * @ntail: room to add at tail
611 * @gfp_mask: allocation priority
613 * Expands (or creates identical copy, if &nhead and &ntail are zero)
614 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
615 * reference count of 1. Returns zero in the case of success or error,
616 * if expansion failed. In the last case, &sk_buff is not changed.
618 * All the pointers pointing into skb header may change and must be
619 * reloaded after call to this function.
622 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
627 int size = nhead + (skb->end - skb->head) + ntail;
633 size = SKB_DATA_ALIGN(size);
635 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
639 /* Copy only real data... and, alas, header. This should be
640 * optimized for the cases when header is void. */
641 memcpy(data + nhead, skb->head, skb->tail - skb->head);
642 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
644 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
645 get_page(skb_shinfo(skb)->frags[i].page);
647 if (skb_shinfo(skb)->frag_list)
648 skb_clone_fraglist(skb);
650 skb_release_data(skb);
652 off = (data + nhead) - skb->head;
655 skb->end = data + size;
663 atomic_set(&skb_shinfo(skb)->dataref, 1);
670 /* Make private copy of skb with writable head and some headroom */
672 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
674 struct sk_buff *skb2;
675 int delta = headroom - skb_headroom(skb);
678 skb2 = pskb_copy(skb, GFP_ATOMIC);
680 skb2 = skb_clone(skb, GFP_ATOMIC);
681 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
692 * skb_copy_expand - copy and expand sk_buff
693 * @skb: buffer to copy
694 * @newheadroom: new free bytes at head
695 * @newtailroom: new free bytes at tail
696 * @gfp_mask: allocation priority
698 * Make a copy of both an &sk_buff and its data and while doing so
699 * allocate additional space.
701 * This is used when the caller wishes to modify the data and needs a
702 * private copy of the data to alter as well as more space for new fields.
703 * Returns %NULL on failure or the pointer to the buffer
704 * on success. The returned buffer has a reference count of 1.
706 * You must pass %GFP_ATOMIC as the allocation priority if this function
707 * is called from an interrupt.
709 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
710 * only by netfilter in the cases when checksum is recalculated? --ANK
712 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
713 int newheadroom, int newtailroom,
717 * Allocate the copy buffer
719 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
721 int head_copy_len, head_copy_off;
726 skb_reserve(n, newheadroom);
728 /* Set the tail pointer and length */
729 skb_put(n, skb->len);
731 head_copy_len = skb_headroom(skb);
733 if (newheadroom <= head_copy_len)
734 head_copy_len = newheadroom;
736 head_copy_off = newheadroom - head_copy_len;
738 /* Copy the linear header and data. */
739 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
740 skb->len + head_copy_len))
743 copy_skb_header(n, skb);
749 * skb_pad - zero pad the tail of an skb
750 * @skb: buffer to pad
753 * Ensure that a buffer is followed by a padding area that is zero
754 * filled. Used by network drivers which may DMA or transfer data
755 * beyond the buffer end onto the wire.
757 * May return NULL in out of memory cases.
760 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
762 struct sk_buff *nskb;
764 /* If the skbuff is non linear tailroom is always zero.. */
765 if (skb_tailroom(skb) >= pad) {
766 memset(skb->data+skb->len, 0, pad);
770 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
773 memset(nskb->data+nskb->len, 0, pad);
777 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
778 * If realloc==0 and trimming is impossible without change of data,
782 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
784 int offset = skb_headlen(skb);
785 int nfrags = skb_shinfo(skb)->nr_frags;
788 for (i = 0; i < nfrags; i++) {
789 int end = offset + skb_shinfo(skb)->frags[i].size;
791 if (skb_cloned(skb)) {
793 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
797 put_page(skb_shinfo(skb)->frags[i].page);
798 skb_shinfo(skb)->nr_frags--;
800 skb_shinfo(skb)->frags[i].size = len - offset;
807 skb->data_len -= skb->len - len;
810 if (len <= skb_headlen(skb)) {
813 skb->tail = skb->data + len;
814 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
815 skb_drop_fraglist(skb);
817 skb->data_len -= skb->len - len;
826 * __pskb_pull_tail - advance tail of skb header
827 * @skb: buffer to reallocate
828 * @delta: number of bytes to advance tail
830 * The function makes a sense only on a fragmented &sk_buff,
831 * it expands header moving its tail forward and copying necessary
832 * data from fragmented part.
834 * &sk_buff MUST have reference count of 1.
836 * Returns %NULL (and &sk_buff does not change) if pull failed
837 * or value of new tail of skb in the case of success.
839 * All the pointers pointing into skb header may change and must be
840 * reloaded after call to this function.
843 /* Moves tail of skb head forward, copying data from fragmented part,
844 * when it is necessary.
845 * 1. It may fail due to malloc failure.
846 * 2. It may change skb pointers.
848 * It is pretty complicated. Luckily, it is called only in exceptional cases.
850 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
852 /* If skb has not enough free space at tail, get new one
853 * plus 128 bytes for future expansions. If we have enough
854 * room at tail, reallocate without expansion only if skb is cloned.
856 int i, k, eat = (skb->tail + delta) - skb->end;
858 if (eat > 0 || skb_cloned(skb)) {
859 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
864 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
867 /* Optimization: no fragments, no reasons to preestimate
868 * size of pulled pages. Superb.
870 if (!skb_shinfo(skb)->frag_list)
873 /* Estimate size of pulled pages. */
875 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
876 if (skb_shinfo(skb)->frags[i].size >= eat)
878 eat -= skb_shinfo(skb)->frags[i].size;
881 /* If we need update frag list, we are in troubles.
882 * Certainly, it possible to add an offset to skb data,
883 * but taking into account that pulling is expected to
884 * be very rare operation, it is worth to fight against
885 * further bloating skb head and crucify ourselves here instead.
886 * Pure masohism, indeed. 8)8)
889 struct sk_buff *list = skb_shinfo(skb)->frag_list;
890 struct sk_buff *clone = NULL;
891 struct sk_buff *insp = NULL;
896 if (list->len <= eat) {
897 /* Eaten as whole. */
902 /* Eaten partially. */
904 if (skb_shared(list)) {
905 /* Sucks! We need to fork list. :-( */
906 clone = skb_clone(list, GFP_ATOMIC);
912 /* This may be pulled without
916 if (!pskb_pull(list, eat)) {
925 /* Free pulled out fragments. */
926 while ((list = skb_shinfo(skb)->frag_list) != insp) {
927 skb_shinfo(skb)->frag_list = list->next;
930 /* And insert new clone at head. */
933 skb_shinfo(skb)->frag_list = clone;
936 /* Success! Now we may commit changes to skb data. */
941 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
942 if (skb_shinfo(skb)->frags[i].size <= eat) {
943 put_page(skb_shinfo(skb)->frags[i].page);
944 eat -= skb_shinfo(skb)->frags[i].size;
946 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
948 skb_shinfo(skb)->frags[k].page_offset += eat;
949 skb_shinfo(skb)->frags[k].size -= eat;
955 skb_shinfo(skb)->nr_frags = k;
958 skb->data_len -= delta;
963 /* Copy some data bits from skb to kernel buffer. */
965 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
968 int start = skb_headlen(skb);
970 if (offset > (int)skb->len - len)
974 if ((copy = start - offset) > 0) {
977 memcpy(to, skb->data + offset, copy);
978 if ((len -= copy) == 0)
984 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
987 BUG_TRAP(start <= offset + len);
989 end = start + skb_shinfo(skb)->frags[i].size;
990 if ((copy = end - offset) > 0) {
996 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
998 vaddr + skb_shinfo(skb)->frags[i].page_offset+
999 offset - start, copy);
1000 kunmap_skb_frag(vaddr);
1002 if ((len -= copy) == 0)
1010 if (skb_shinfo(skb)->frag_list) {
1011 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1013 for (; list; list = list->next) {
1016 BUG_TRAP(start <= offset + len);
1018 end = start + list->len;
1019 if ((copy = end - offset) > 0) {
1022 if (skb_copy_bits(list, offset - start,
1025 if ((len -= copy) == 0)
1041 * skb_store_bits - store bits from kernel buffer to skb
1042 * @skb: destination buffer
1043 * @offset: offset in destination
1044 * @from: source buffer
1045 * @len: number of bytes to copy
1047 * Copy the specified number of bytes from the source buffer to the
1048 * destination skb. This function handles all the messy bits of
1049 * traversing fragment lists and such.
1052 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1055 int start = skb_headlen(skb);
1057 if (offset > (int)skb->len - len)
1060 if ((copy = start - offset) > 0) {
1063 memcpy(skb->data + offset, from, copy);
1064 if ((len -= copy) == 0)
1070 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1071 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1074 BUG_TRAP(start <= offset + len);
1076 end = start + frag->size;
1077 if ((copy = end - offset) > 0) {
1083 vaddr = kmap_skb_frag(frag);
1084 memcpy(vaddr + frag->page_offset + offset - start,
1086 kunmap_skb_frag(vaddr);
1088 if ((len -= copy) == 0)
1096 if (skb_shinfo(skb)->frag_list) {
1097 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1099 for (; list; list = list->next) {
1102 BUG_TRAP(start <= offset + len);
1104 end = start + list->len;
1105 if ((copy = end - offset) > 0) {
1108 if (skb_store_bits(list, offset - start,
1111 if ((len -= copy) == 0)
1126 EXPORT_SYMBOL(skb_store_bits);
1128 /* Checksum skb data. */
1130 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1131 int len, unsigned int csum)
1133 int start = skb_headlen(skb);
1134 int i, copy = start - offset;
1137 /* Checksum header. */
1141 csum = csum_partial(skb->data + offset, copy, csum);
1142 if ((len -= copy) == 0)
1148 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1151 BUG_TRAP(start <= offset + len);
1153 end = start + skb_shinfo(skb)->frags[i].size;
1154 if ((copy = end - offset) > 0) {
1157 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1161 vaddr = kmap_skb_frag(frag);
1162 csum2 = csum_partial(vaddr + frag->page_offset +
1163 offset - start, copy, 0);
1164 kunmap_skb_frag(vaddr);
1165 csum = csum_block_add(csum, csum2, pos);
1174 if (skb_shinfo(skb)->frag_list) {
1175 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1177 for (; list; list = list->next) {
1180 BUG_TRAP(start <= offset + len);
1182 end = start + list->len;
1183 if ((copy = end - offset) > 0) {
1187 csum2 = skb_checksum(list, offset - start,
1189 csum = csum_block_add(csum, csum2, pos);
1190 if ((len -= copy) == 0)
1203 /* Both of above in one bottle. */
1205 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1206 u8 *to, int len, unsigned int csum)
1208 int start = skb_headlen(skb);
1209 int i, copy = start - offset;
1216 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1218 if ((len -= copy) == 0)
1225 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1228 BUG_TRAP(start <= offset + len);
1230 end = start + skb_shinfo(skb)->frags[i].size;
1231 if ((copy = end - offset) > 0) {
1234 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1238 vaddr = kmap_skb_frag(frag);
1239 csum2 = csum_partial_copy_nocheck(vaddr +
1243 kunmap_skb_frag(vaddr);
1244 csum = csum_block_add(csum, csum2, pos);
1254 if (skb_shinfo(skb)->frag_list) {
1255 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1257 for (; list; list = list->next) {
1261 BUG_TRAP(start <= offset + len);
1263 end = start + list->len;
1264 if ((copy = end - offset) > 0) {
1267 csum2 = skb_copy_and_csum_bits(list,
1270 csum = csum_block_add(csum, csum2, pos);
1271 if ((len -= copy) == 0)
1284 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1289 if (skb->ip_summed == CHECKSUM_HW)
1290 csstart = skb->h.raw - skb->data;
1292 csstart = skb_headlen(skb);
1294 BUG_ON(csstart > skb_headlen(skb));
1296 memcpy(to, skb->data, csstart);
1299 if (csstart != skb->len)
1300 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1301 skb->len - csstart, 0);
1303 if (skb->ip_summed == CHECKSUM_HW) {
1304 long csstuff = csstart + skb->csum;
1306 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1311 * skb_dequeue - remove from the head of the queue
1312 * @list: list to dequeue from
1314 * Remove the head of the list. The list lock is taken so the function
1315 * may be used safely with other locking list functions. The head item is
1316 * returned or %NULL if the list is empty.
1319 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1321 unsigned long flags;
1322 struct sk_buff *result;
1324 spin_lock_irqsave(&list->lock, flags);
1325 result = __skb_dequeue(list);
1326 spin_unlock_irqrestore(&list->lock, flags);
1331 * skb_dequeue_tail - remove from the tail of the queue
1332 * @list: list to dequeue from
1334 * Remove the tail of the list. The list lock is taken so the function
1335 * may be used safely with other locking list functions. The tail item is
1336 * returned or %NULL if the list is empty.
1338 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1340 unsigned long flags;
1341 struct sk_buff *result;
1343 spin_lock_irqsave(&list->lock, flags);
1344 result = __skb_dequeue_tail(list);
1345 spin_unlock_irqrestore(&list->lock, flags);
1350 * skb_queue_purge - empty a list
1351 * @list: list to empty
1353 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1354 * the list and one reference dropped. This function takes the list
1355 * lock and is atomic with respect to other list locking functions.
1357 void skb_queue_purge(struct sk_buff_head *list)
1359 struct sk_buff *skb;
1360 while ((skb = skb_dequeue(list)) != NULL)
1365 * skb_queue_head - queue a buffer at the list head
1366 * @list: list to use
1367 * @newsk: buffer to queue
1369 * Queue a buffer at the start of the list. This function takes the
1370 * list lock and can be used safely with other locking &sk_buff functions
1373 * A buffer cannot be placed on two lists at the same time.
1375 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1377 unsigned long flags;
1379 spin_lock_irqsave(&list->lock, flags);
1380 __skb_queue_head(list, newsk);
1381 spin_unlock_irqrestore(&list->lock, flags);
1385 * skb_queue_tail - queue a buffer at the list tail
1386 * @list: list to use
1387 * @newsk: buffer to queue
1389 * Queue a buffer at the tail of the list. This function takes the
1390 * list lock and can be used safely with other locking &sk_buff functions
1393 * A buffer cannot be placed on two lists at the same time.
1395 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1397 unsigned long flags;
1399 spin_lock_irqsave(&list->lock, flags);
1400 __skb_queue_tail(list, newsk);
1401 spin_unlock_irqrestore(&list->lock, flags);
1405 * skb_unlink - remove a buffer from a list
1406 * @skb: buffer to remove
1407 * @list: list to use
1409 * Remove a packet from a list. The list locks are taken and this
1410 * function is atomic with respect to other list locked calls
1412 * You must know what list the SKB is on.
1414 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1416 unsigned long flags;
1418 spin_lock_irqsave(&list->lock, flags);
1419 __skb_unlink(skb, list);
1420 spin_unlock_irqrestore(&list->lock, flags);
1424 * skb_append - append a buffer
1425 * @old: buffer to insert after
1426 * @newsk: buffer to insert
1427 * @list: list to use
1429 * Place a packet after a given packet in a list. The list locks are taken
1430 * and this function is atomic with respect to other list locked calls.
1431 * A buffer cannot be placed on two lists at the same time.
1433 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1435 unsigned long flags;
1437 spin_lock_irqsave(&list->lock, flags);
1438 __skb_append(old, newsk, list);
1439 spin_unlock_irqrestore(&list->lock, flags);
1444 * skb_insert - insert a buffer
1445 * @old: buffer to insert before
1446 * @newsk: buffer to insert
1447 * @list: list to use
1449 * Place a packet before a given packet in a list. The list locks are
1450 * taken and this function is atomic with respect to other list locked
1453 * A buffer cannot be placed on two lists at the same time.
1455 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1457 unsigned long flags;
1459 spin_lock_irqsave(&list->lock, flags);
1460 __skb_insert(newsk, old->prev, old, list);
1461 spin_unlock_irqrestore(&list->lock, flags);
1466 * Tune the memory allocator for a new MTU size.
1468 void skb_add_mtu(int mtu)
1470 /* Must match allocation in alloc_skb */
1471 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1473 kmem_add_cache_size(mtu);
1477 static inline void skb_split_inside_header(struct sk_buff *skb,
1478 struct sk_buff* skb1,
1479 const u32 len, const int pos)
1483 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1485 /* And move data appendix as is. */
1486 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1487 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1489 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1490 skb_shinfo(skb)->nr_frags = 0;
1491 skb1->data_len = skb->data_len;
1492 skb1->len += skb1->data_len;
1495 skb->tail = skb->data + len;
1498 static inline void skb_split_no_header(struct sk_buff *skb,
1499 struct sk_buff* skb1,
1500 const u32 len, int pos)
1503 const int nfrags = skb_shinfo(skb)->nr_frags;
1505 skb_shinfo(skb)->nr_frags = 0;
1506 skb1->len = skb1->data_len = skb->len - len;
1508 skb->data_len = len - pos;
1510 for (i = 0; i < nfrags; i++) {
1511 int size = skb_shinfo(skb)->frags[i].size;
1513 if (pos + size > len) {
1514 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1518 * We have two variants in this case:
1519 * 1. Move all the frag to the second
1520 * part, if it is possible. F.e.
1521 * this approach is mandatory for TUX,
1522 * where splitting is expensive.
1523 * 2. Split is accurately. We make this.
1525 get_page(skb_shinfo(skb)->frags[i].page);
1526 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1527 skb_shinfo(skb1)->frags[0].size -= len - pos;
1528 skb_shinfo(skb)->frags[i].size = len - pos;
1529 skb_shinfo(skb)->nr_frags++;
1533 skb_shinfo(skb)->nr_frags++;
1536 skb_shinfo(skb1)->nr_frags = k;
1540 * skb_split - Split fragmented skb to two parts at length len.
1541 * @skb: the buffer to split
1542 * @skb1: the buffer to receive the second part
1543 * @len: new length for skb
1545 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1547 int pos = skb_headlen(skb);
1549 if (len < pos) /* Split line is inside header. */
1550 skb_split_inside_header(skb, skb1, len, pos);
1551 else /* Second chunk has no header, nothing to copy. */
1552 skb_split_no_header(skb, skb1, len, pos);
1556 * skb_prepare_seq_read - Prepare a sequential read of skb data
1557 * @skb: the buffer to read
1558 * @from: lower offset of data to be read
1559 * @to: upper offset of data to be read
1560 * @st: state variable
1562 * Initializes the specified state variable. Must be called before
1563 * invoking skb_seq_read() for the first time.
1565 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1566 unsigned int to, struct skb_seq_state *st)
1568 st->lower_offset = from;
1569 st->upper_offset = to;
1570 st->root_skb = st->cur_skb = skb;
1571 st->frag_idx = st->stepped_offset = 0;
1572 st->frag_data = NULL;
1576 * skb_seq_read - Sequentially read skb data
1577 * @consumed: number of bytes consumed by the caller so far
1578 * @data: destination pointer for data to be returned
1579 * @st: state variable
1581 * Reads a block of skb data at &consumed relative to the
1582 * lower offset specified to skb_prepare_seq_read(). Assigns
1583 * the head of the data block to &data and returns the length
1584 * of the block or 0 if the end of the skb data or the upper
1585 * offset has been reached.
1587 * The caller is not required to consume all of the data
1588 * returned, i.e. &consumed is typically set to the number
1589 * of bytes already consumed and the next call to
1590 * skb_seq_read() will return the remaining part of the block.
1592 * Note: The size of each block of data returned can be arbitary,
1593 * this limitation is the cost for zerocopy seqeuental
1594 * reads of potentially non linear data.
1596 * Note: Fragment lists within fragments are not implemented
1597 * at the moment, state->root_skb could be replaced with
1598 * a stack for this purpose.
1600 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1601 struct skb_seq_state *st)
1603 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1606 if (unlikely(abs_offset >= st->upper_offset))
1610 block_limit = skb_headlen(st->cur_skb);
1612 if (abs_offset < block_limit) {
1613 *data = st->cur_skb->data + abs_offset;
1614 return block_limit - abs_offset;
1617 if (st->frag_idx == 0 && !st->frag_data)
1618 st->stepped_offset += skb_headlen(st->cur_skb);
1620 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1621 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1622 block_limit = frag->size + st->stepped_offset;
1624 if (abs_offset < block_limit) {
1626 st->frag_data = kmap_skb_frag(frag);
1628 *data = (u8 *) st->frag_data + frag->page_offset +
1629 (abs_offset - st->stepped_offset);
1631 return block_limit - abs_offset;
1634 if (st->frag_data) {
1635 kunmap_skb_frag(st->frag_data);
1636 st->frag_data = NULL;
1640 st->stepped_offset += frag->size;
1643 if (st->cur_skb->next) {
1644 st->cur_skb = st->cur_skb->next;
1647 } else if (st->root_skb == st->cur_skb &&
1648 skb_shinfo(st->root_skb)->frag_list) {
1649 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1657 * skb_abort_seq_read - Abort a sequential read of skb data
1658 * @st: state variable
1660 * Must be called if skb_seq_read() was not called until it
1663 void skb_abort_seq_read(struct skb_seq_state *st)
1666 kunmap_skb_frag(st->frag_data);
1669 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1671 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1672 struct ts_config *conf,
1673 struct ts_state *state)
1675 return skb_seq_read(offset, text, TS_SKB_CB(state));
1678 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1680 skb_abort_seq_read(TS_SKB_CB(state));
1684 * skb_find_text - Find a text pattern in skb data
1685 * @skb: the buffer to look in
1686 * @from: search offset
1688 * @config: textsearch configuration
1689 * @state: uninitialized textsearch state variable
1691 * Finds a pattern in the skb data according to the specified
1692 * textsearch configuration. Use textsearch_next() to retrieve
1693 * subsequent occurrences of the pattern. Returns the offset
1694 * to the first occurrence or UINT_MAX if no match was found.
1696 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1697 unsigned int to, struct ts_config *config,
1698 struct ts_state *state)
1700 config->get_next_block = skb_ts_get_next_block;
1701 config->finish = skb_ts_finish;
1703 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1705 return textsearch_find(config, state);
1709 * skb_append_datato_frags: - append the user data to a skb
1710 * @sk: sock structure
1711 * @skb: skb structure to be appened with user data.
1712 * @getfrag: call back function to be used for getting the user data
1713 * @from: pointer to user message iov
1714 * @length: length of the iov message
1716 * Description: This procedure append the user data in the fragment part
1717 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1719 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1720 int (*getfrag)(void *from, char *to, int offset,
1721 int len, int odd, struct sk_buff *skb),
1722 void *from, int length)
1725 skb_frag_t *frag = NULL;
1726 struct page *page = NULL;
1732 /* Return error if we don't have space for new frag */
1733 frg_cnt = skb_shinfo(skb)->nr_frags;
1734 if (frg_cnt >= MAX_SKB_FRAGS)
1737 /* allocate a new page for next frag */
1738 page = alloc_pages(sk->sk_allocation, 0);
1740 /* If alloc_page fails just return failure and caller will
1741 * free previous allocated pages by doing kfree_skb()
1746 /* initialize the next frag */
1747 sk->sk_sndmsg_page = page;
1748 sk->sk_sndmsg_off = 0;
1749 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1750 skb->truesize += PAGE_SIZE;
1751 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1753 /* get the new initialized frag */
1754 frg_cnt = skb_shinfo(skb)->nr_frags;
1755 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1757 /* copy the user data to page */
1758 left = PAGE_SIZE - frag->page_offset;
1759 copy = (length > left)? left : length;
1761 ret = getfrag(from, (page_address(frag->page) +
1762 frag->page_offset + frag->size),
1763 offset, copy, 0, skb);
1767 /* copy was successful so update the size parameters */
1768 sk->sk_sndmsg_off += copy;
1771 skb->data_len += copy;
1775 } while (length > 0);
1780 void __init skb_init(void)
1782 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1783 sizeof(struct sk_buff),
1787 if (!skbuff_head_cache)
1788 panic("cannot create skbuff cache");
1790 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1791 (2*sizeof(struct sk_buff)) +
1796 if (!skbuff_fclone_cache)
1797 panic("cannot create skbuff cache");
1800 EXPORT_SYMBOL(___pskb_trim);
1801 EXPORT_SYMBOL(__kfree_skb);
1802 EXPORT_SYMBOL(__pskb_pull_tail);
1803 EXPORT_SYMBOL(__alloc_skb);
1804 EXPORT_SYMBOL(pskb_copy);
1805 EXPORT_SYMBOL(pskb_expand_head);
1806 EXPORT_SYMBOL(skb_checksum);
1807 EXPORT_SYMBOL(skb_clone);
1808 EXPORT_SYMBOL(skb_clone_fraglist);
1809 EXPORT_SYMBOL(skb_copy);
1810 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1811 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1812 EXPORT_SYMBOL(skb_copy_bits);
1813 EXPORT_SYMBOL(skb_copy_expand);
1814 EXPORT_SYMBOL(skb_over_panic);
1815 EXPORT_SYMBOL(skb_pad);
1816 EXPORT_SYMBOL(skb_realloc_headroom);
1817 EXPORT_SYMBOL(skb_under_panic);
1818 EXPORT_SYMBOL(skb_dequeue);
1819 EXPORT_SYMBOL(skb_dequeue_tail);
1820 EXPORT_SYMBOL(skb_insert);
1821 EXPORT_SYMBOL(skb_queue_purge);
1822 EXPORT_SYMBOL(skb_queue_head);
1823 EXPORT_SYMBOL(skb_queue_tail);
1824 EXPORT_SYMBOL(skb_unlink);
1825 EXPORT_SYMBOL(skb_append);
1826 EXPORT_SYMBOL(skb_split);
1827 EXPORT_SYMBOL(skb_prepare_seq_read);
1828 EXPORT_SYMBOL(skb_seq_read);
1829 EXPORT_SYMBOL(skb_abort_seq_read);
1830 EXPORT_SYMBOL(skb_find_text);
1831 EXPORT_SYMBOL(skb_append_datato_frags);