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_dec_and_test(&(skb_shinfo(skb)->dataref))) {
245 if (skb_shinfo(skb)->nr_frags) {
247 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
248 put_page(skb_shinfo(skb)->frags[i].page);
251 if (skb_shinfo(skb)->frag_list)
252 skb_drop_fraglist(skb);
259 * Free an skbuff by memory without cleaning the state.
261 void kfree_skbmem(struct sk_buff *skb)
263 skb_release_data(skb);
264 kmem_cache_free(skbuff_head_cache, skb);
268 * __kfree_skb - private function
271 * Free an sk_buff. Release anything attached to the buffer.
272 * Clean the state. This is an internal helper function. Users should
273 * always call kfree_skb
276 void __kfree_skb(struct sk_buff *skb)
279 printk(KERN_WARNING "Warning: kfree_skb passed an skb still "
280 "on a list (from %p).\n", NET_CALLER(skb));
284 dst_release(skb->dst);
286 secpath_put(skb->sp);
288 if(skb->destructor) {
290 printk(KERN_WARNING "Warning: kfree_skb on "
291 "hard IRQ %p\n", NET_CALLER(skb));
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, int 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_NETFILTER_DEBUG
371 #ifdef CONFIG_BRIDGE_NETFILTER
373 nf_bridge_get(skb->nf_bridge);
375 #endif /*CONFIG_NETFILTER*/
376 #if defined(CONFIG_HIPPI)
379 #ifdef CONFIG_NET_SCHED
381 #ifdef CONFIG_NET_CLS_ACT
382 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
383 n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
384 n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
391 atomic_set(&n->users, 1);
397 atomic_inc(&(skb_shinfo(skb)->dataref));
403 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
406 * Shift between the two data areas in bytes
408 unsigned long offset = new->data - old->data;
413 new->real_dev = old->real_dev;
414 new->priority = old->priority;
415 new->protocol = old->protocol;
416 new->dst = dst_clone(old->dst);
418 new->sp = secpath_get(old->sp);
420 new->h.raw = old->h.raw + offset;
421 new->nh.raw = old->nh.raw + offset;
422 new->mac.raw = old->mac.raw + offset;
423 memcpy(new->cb, old->cb, sizeof(old->cb));
424 new->local_df = old->local_df;
425 new->pkt_type = old->pkt_type;
426 new->stamp = old->stamp;
427 new->destructor = NULL;
428 new->security = old->security;
429 #ifdef CONFIG_NETFILTER
430 new->nfmark = old->nfmark;
431 new->nfcache = old->nfcache;
432 new->nfct = old->nfct;
433 nf_conntrack_get(old->nfct);
434 new->nfctinfo = old->nfctinfo;
435 #ifdef CONFIG_NETFILTER_DEBUG
436 new->nf_debug = old->nf_debug;
438 #ifdef CONFIG_BRIDGE_NETFILTER
439 new->nf_bridge = old->nf_bridge;
440 nf_bridge_get(old->nf_bridge);
443 #ifdef CONFIG_NET_SCHED
444 #ifdef CONFIG_NET_CLS_ACT
445 new->tc_verd = old->tc_verd;
447 new->tc_index = old->tc_index;
449 atomic_set(&new->users, 1);
450 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
451 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
455 * skb_copy - create private copy of an sk_buff
456 * @skb: buffer to copy
457 * @gfp_mask: allocation priority
459 * Make a copy of both an &sk_buff and its data. This is used when the
460 * caller wishes to modify the data and needs a private copy of the
461 * data to alter. Returns %NULL on failure or the pointer to the buffer
462 * on success. The returned buffer has a reference count of 1.
464 * As by-product this function converts non-linear &sk_buff to linear
465 * one, so that &sk_buff becomes completely private and caller is allowed
466 * to modify all the data of returned buffer. This means that this
467 * function is not recommended for use in circumstances when only
468 * header is going to be modified. Use pskb_copy() instead.
471 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
473 int headerlen = skb->data - skb->head;
475 * Allocate the copy buffer
477 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
482 /* Set the data pointer */
483 skb_reserve(n, headerlen);
484 /* Set the tail pointer and length */
485 skb_put(n, skb->len);
487 n->ip_summed = skb->ip_summed;
489 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
492 copy_skb_header(n, skb);
498 * pskb_copy - create copy of an sk_buff with private head.
499 * @skb: buffer to copy
500 * @gfp_mask: allocation priority
502 * Make a copy of both an &sk_buff and part of its data, located
503 * in header. Fragmented data remain shared. This is used when
504 * the caller wishes to modify only header of &sk_buff and needs
505 * private copy of the header to alter. Returns %NULL on failure
506 * or the pointer to the buffer on success.
507 * The returned buffer has a reference count of 1.
510 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
513 * Allocate the copy buffer
515 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
520 /* Set the data pointer */
521 skb_reserve(n, skb->data - skb->head);
522 /* Set the tail pointer and length */
523 skb_put(n, skb_headlen(skb));
525 memcpy(n->data, skb->data, n->len);
527 n->ip_summed = skb->ip_summed;
529 n->data_len = skb->data_len;
532 if (skb_shinfo(skb)->nr_frags) {
535 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
536 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
537 get_page(skb_shinfo(n)->frags[i].page);
539 skb_shinfo(n)->nr_frags = i;
542 if (skb_shinfo(skb)->frag_list) {
543 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
544 skb_clone_fraglist(n);
547 copy_skb_header(n, skb);
553 * pskb_expand_head - reallocate header of &sk_buff
554 * @skb: buffer to reallocate
555 * @nhead: room to add at head
556 * @ntail: room to add at tail
557 * @gfp_mask: allocation priority
559 * Expands (or creates identical copy, if &nhead and &ntail are zero)
560 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
561 * reference count of 1. Returns zero in the case of success or error,
562 * if expansion failed. In the last case, &sk_buff is not changed.
564 * All the pointers pointing into skb header may change and must be
565 * reloaded after call to this function.
568 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
572 int size = nhead + (skb->end - skb->head) + ntail;
578 size = SKB_DATA_ALIGN(size);
580 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
584 /* Copy only real data... and, alas, header. This should be
585 * optimized for the cases when header is void. */
586 memcpy(data + nhead, skb->head, skb->tail - skb->head);
587 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
589 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
590 get_page(skb_shinfo(skb)->frags[i].page);
592 if (skb_shinfo(skb)->frag_list)
593 skb_clone_fraglist(skb);
595 skb_release_data(skb);
597 off = (data + nhead) - skb->head;
600 skb->end = data + size;
607 atomic_set(&skb_shinfo(skb)->dataref, 1);
614 /* Make private copy of skb with writable head and some headroom */
616 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
618 struct sk_buff *skb2;
619 int delta = headroom - skb_headroom(skb);
622 skb2 = pskb_copy(skb, GFP_ATOMIC);
624 skb2 = skb_clone(skb, GFP_ATOMIC);
625 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
636 * skb_copy_expand - copy and expand sk_buff
637 * @skb: buffer to copy
638 * @newheadroom: new free bytes at head
639 * @newtailroom: new free bytes at tail
640 * @gfp_mask: allocation priority
642 * Make a copy of both an &sk_buff and its data and while doing so
643 * allocate additional space.
645 * This is used when the caller wishes to modify the data and needs a
646 * private copy of the data to alter as well as more space for new fields.
647 * Returns %NULL on failure or the pointer to the buffer
648 * on success. The returned buffer has a reference count of 1.
650 * You must pass %GFP_ATOMIC as the allocation priority if this function
651 * is called from an interrupt.
653 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
654 * only by netfilter in the cases when checksum is recalculated? --ANK
656 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
657 int newheadroom, int newtailroom, int gfp_mask)
660 * Allocate the copy buffer
662 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
664 int head_copy_len, head_copy_off;
669 skb_reserve(n, newheadroom);
671 /* Set the tail pointer and length */
672 skb_put(n, skb->len);
674 head_copy_len = skb_headroom(skb);
676 if (newheadroom <= head_copy_len)
677 head_copy_len = newheadroom;
679 head_copy_off = newheadroom - head_copy_len;
681 /* Copy the linear header and data. */
682 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
683 skb->len + head_copy_len))
686 copy_skb_header(n, skb);
692 * skb_pad - zero pad the tail of an skb
693 * @skb: buffer to pad
696 * Ensure that a buffer is followed by a padding area that is zero
697 * filled. Used by network drivers which may DMA or transfer data
698 * beyond the buffer end onto the wire.
700 * May return NULL in out of memory cases.
703 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
705 struct sk_buff *nskb;
707 /* If the skbuff is non linear tailroom is always zero.. */
708 if (skb_tailroom(skb) >= pad) {
709 memset(skb->data+skb->len, 0, pad);
713 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
716 memset(nskb->data+nskb->len, 0, pad);
720 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
721 * If realloc==0 and trimming is impossible without change of data,
725 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
727 int offset = skb_headlen(skb);
728 int nfrags = skb_shinfo(skb)->nr_frags;
731 for (i = 0; i < nfrags; i++) {
732 int end = offset + skb_shinfo(skb)->frags[i].size;
734 if (skb_cloned(skb)) {
737 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
741 put_page(skb_shinfo(skb)->frags[i].page);
742 skb_shinfo(skb)->nr_frags--;
744 skb_shinfo(skb)->frags[i].size = len - offset;
751 skb->data_len -= skb->len - len;
754 if (len <= skb_headlen(skb)) {
757 skb->tail = skb->data + len;
758 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
759 skb_drop_fraglist(skb);
761 skb->data_len -= skb->len - len;
770 * __pskb_pull_tail - advance tail of skb header
771 * @skb: buffer to reallocate
772 * @delta: number of bytes to advance tail
774 * The function makes a sense only on a fragmented &sk_buff,
775 * it expands header moving its tail forward and copying necessary
776 * data from fragmented part.
778 * &sk_buff MUST have reference count of 1.
780 * Returns %NULL (and &sk_buff does not change) if pull failed
781 * or value of new tail of skb in the case of success.
783 * All the pointers pointing into skb header may change and must be
784 * reloaded after call to this function.
787 /* Moves tail of skb head forward, copying data from fragmented part,
788 * when it is necessary.
789 * 1. It may fail due to malloc failure.
790 * 2. It may change skb pointers.
792 * It is pretty complicated. Luckily, it is called only in exceptional cases.
794 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
796 /* If skb has not enough free space at tail, get new one
797 * plus 128 bytes for future expansions. If we have enough
798 * room at tail, reallocate without expansion only if skb is cloned.
800 int i, k, eat = (skb->tail + delta) - skb->end;
802 if (eat > 0 || skb_cloned(skb)) {
803 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
808 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
811 /* Optimization: no fragments, no reasons to preestimate
812 * size of pulled pages. Superb.
814 if (!skb_shinfo(skb)->frag_list)
817 /* Estimate size of pulled pages. */
819 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
820 if (skb_shinfo(skb)->frags[i].size >= eat)
822 eat -= skb_shinfo(skb)->frags[i].size;
825 /* If we need update frag list, we are in troubles.
826 * Certainly, it possible to add an offset to skb data,
827 * but taking into account that pulling is expected to
828 * be very rare operation, it is worth to fight against
829 * further bloating skb head and crucify ourselves here instead.
830 * Pure masohism, indeed. 8)8)
833 struct sk_buff *list = skb_shinfo(skb)->frag_list;
834 struct sk_buff *clone = NULL;
835 struct sk_buff *insp = NULL;
841 if (list->len <= eat) {
842 /* Eaten as whole. */
847 /* Eaten partially. */
849 if (skb_shared(list)) {
850 /* Sucks! We need to fork list. :-( */
851 clone = skb_clone(list, GFP_ATOMIC);
857 /* This may be pulled without
861 if (!pskb_pull(list, eat)) {
870 /* Free pulled out fragments. */
871 while ((list = skb_shinfo(skb)->frag_list) != insp) {
872 skb_shinfo(skb)->frag_list = list->next;
875 /* And insert new clone at head. */
878 skb_shinfo(skb)->frag_list = clone;
881 /* Success! Now we may commit changes to skb data. */
886 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
887 if (skb_shinfo(skb)->frags[i].size <= eat) {
888 put_page(skb_shinfo(skb)->frags[i].page);
889 eat -= skb_shinfo(skb)->frags[i].size;
891 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
893 skb_shinfo(skb)->frags[k].page_offset += eat;
894 skb_shinfo(skb)->frags[k].size -= eat;
900 skb_shinfo(skb)->nr_frags = k;
903 skb->data_len -= delta;
908 /* Copy some data bits from skb to kernel buffer. */
910 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
913 int start = skb_headlen(skb);
915 if (offset > (int)skb->len - len)
919 if ((copy = start - offset) > 0) {
922 memcpy(to, skb->data + offset, copy);
923 if ((len -= copy) == 0)
929 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
932 BUG_TRAP(start <= offset + len);
934 end = start + skb_shinfo(skb)->frags[i].size;
935 if ((copy = end - offset) > 0) {
941 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
943 vaddr + skb_shinfo(skb)->frags[i].page_offset+
944 offset - start, copy);
945 kunmap_skb_frag(vaddr);
947 if ((len -= copy) == 0)
955 if (skb_shinfo(skb)->frag_list) {
956 struct sk_buff *list = skb_shinfo(skb)->frag_list;
958 for (; list; list = list->next) {
961 BUG_TRAP(start <= offset + len);
963 end = start + list->len;
964 if ((copy = end - offset) > 0) {
967 if (skb_copy_bits(list, offset - start,
970 if ((len -= copy) == 0)
985 /* Keep iterating until skb_iter_next returns false. */
986 void skb_iter_first(const struct sk_buff *skb, struct skb_iter *i)
988 i->len = skb_headlen(skb);
989 i->data = (unsigned char *)skb->data;
994 int skb_iter_next(const struct sk_buff *skb, struct skb_iter *i)
996 /* Unmap previous, if not head fragment. */
998 kunmap_skb_frag(i->data);
1002 /* We're iterating through fraglist. */
1003 if (i->nextfrag < skb_shinfo(i->fraglist)->nr_frags) {
1004 i->data = kmap_skb_frag(&skb_shinfo(i->fraglist)
1005 ->frags[i->nextfrag]);
1006 i->len = skb_shinfo(i->fraglist)->frags[i->nextfrag]
1011 /* Fragments with fragments? Too hard! */
1012 BUG_ON(skb_shinfo(i->fraglist)->frag_list);
1013 i->fraglist = i->fraglist->next;
1017 i->len = skb_headlen(i->fraglist);
1018 i->data = i->fraglist->data;
1023 if (i->nextfrag < skb_shinfo(skb)->nr_frags) {
1024 i->data = kmap_skb_frag(&skb_shinfo(skb)->frags[i->nextfrag]);
1025 i->len = skb_shinfo(skb)->frags[i->nextfrag].size;
1030 i->fraglist = skb_shinfo(skb)->frag_list;
1035 /* Bug trap for callers */
1040 void skb_iter_abort(const struct sk_buff *skb, struct skb_iter *i)
1042 /* Unmap previous, if not head fragment. */
1043 if (i->data && i->nextfrag)
1044 kunmap_skb_frag(i->data);
1045 /* Bug trap for callers */
1049 /* Checksum skb data. */
1051 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1052 int len, unsigned int csum)
1054 int start = skb_headlen(skb);
1055 int i, copy = start - offset;
1058 /* Checksum header. */
1062 csum = csum_partial(skb->data + offset, copy, csum);
1063 if ((len -= copy) == 0)
1069 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1072 BUG_TRAP(start <= offset + len);
1074 end = start + skb_shinfo(skb)->frags[i].size;
1075 if ((copy = end - offset) > 0) {
1078 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1082 vaddr = kmap_skb_frag(frag);
1083 csum2 = csum_partial(vaddr + frag->page_offset +
1084 offset - start, copy, 0);
1085 kunmap_skb_frag(vaddr);
1086 csum = csum_block_add(csum, csum2, pos);
1095 if (skb_shinfo(skb)->frag_list) {
1096 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1098 for (; list; list = list->next) {
1101 BUG_TRAP(start <= offset + len);
1103 end = start + list->len;
1104 if ((copy = end - offset) > 0) {
1108 csum2 = skb_checksum(list, offset - start,
1110 csum = csum_block_add(csum, csum2, pos);
1111 if ((len -= copy) == 0)
1125 /* Both of above in one bottle. */
1127 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1128 u8 *to, int len, unsigned int csum)
1130 int start = skb_headlen(skb);
1131 int i, copy = start - offset;
1138 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1140 if ((len -= copy) == 0)
1147 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1150 BUG_TRAP(start <= offset + len);
1152 end = start + skb_shinfo(skb)->frags[i].size;
1153 if ((copy = end - offset) > 0) {
1156 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1160 vaddr = kmap_skb_frag(frag);
1161 csum2 = csum_partial_copy_nocheck(vaddr +
1165 kunmap_skb_frag(vaddr);
1166 csum = csum_block_add(csum, csum2, pos);
1176 if (skb_shinfo(skb)->frag_list) {
1177 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1179 for (; list; list = list->next) {
1183 BUG_TRAP(start <= offset + len);
1185 end = start + list->len;
1186 if ((copy = end - offset) > 0) {
1189 csum2 = skb_copy_and_csum_bits(list,
1192 csum = csum_block_add(csum, csum2, pos);
1193 if ((len -= copy) == 0)
1207 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1212 if (skb->ip_summed == CHECKSUM_HW)
1213 csstart = skb->h.raw - skb->data;
1215 csstart = skb_headlen(skb);
1217 if (csstart > skb_headlen(skb))
1220 memcpy(to, skb->data, csstart);
1223 if (csstart != skb->len)
1224 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1225 skb->len - csstart, 0);
1227 if (skb->ip_summed == CHECKSUM_HW) {
1228 long csstuff = csstart + skb->csum;
1230 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1235 * skb_dequeue - remove from the head of the queue
1236 * @list: list to dequeue from
1238 * Remove the head of the list. The list lock is taken so the function
1239 * may be used safely with other locking list functions. The head item is
1240 * returned or %NULL if the list is empty.
1243 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1245 unsigned long flags;
1246 struct sk_buff *result;
1248 spin_lock_irqsave(&list->lock, flags);
1249 result = __skb_dequeue(list);
1250 spin_unlock_irqrestore(&list->lock, flags);
1255 * skb_dequeue_tail - remove from the tail of the queue
1256 * @list: list to dequeue from
1258 * Remove the tail of the list. The list lock is taken so the function
1259 * may be used safely with other locking list functions. The tail item is
1260 * returned or %NULL if the list is empty.
1262 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1264 unsigned long flags;
1265 struct sk_buff *result;
1267 spin_lock_irqsave(&list->lock, flags);
1268 result = __skb_dequeue_tail(list);
1269 spin_unlock_irqrestore(&list->lock, flags);
1274 * skb_queue_purge - empty a list
1275 * @list: list to empty
1277 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1278 * the list and one reference dropped. This function takes the list
1279 * lock and is atomic with respect to other list locking functions.
1281 void skb_queue_purge(struct sk_buff_head *list)
1283 struct sk_buff *skb;
1284 while ((skb = skb_dequeue(list)) != NULL)
1289 * skb_queue_head - queue a buffer at the list head
1290 * @list: list to use
1291 * @newsk: buffer to queue
1293 * Queue a buffer at the start of the list. This function takes the
1294 * list lock and can be used safely with other locking &sk_buff functions
1297 * A buffer cannot be placed on two lists at the same time.
1299 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1301 unsigned long flags;
1303 spin_lock_irqsave(&list->lock, flags);
1304 __skb_queue_head(list, newsk);
1305 spin_unlock_irqrestore(&list->lock, flags);
1309 * skb_queue_tail - queue a buffer at the list tail
1310 * @list: list to use
1311 * @newsk: buffer to queue
1313 * Queue a buffer at the tail 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_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1321 unsigned long flags;
1323 spin_lock_irqsave(&list->lock, flags);
1324 __skb_queue_tail(list, newsk);
1325 spin_unlock_irqrestore(&list->lock, flags);
1328 * skb_unlink - remove a buffer from a list
1329 * @skb: buffer to remove
1331 * Place a packet after a given packet in a list. The list locks are taken
1332 * and this function is atomic with respect to other list locked calls
1334 * Works even without knowing the list it is sitting on, which can be
1335 * handy at times. It also means that THE LIST MUST EXIST when you
1336 * unlink. Thus a list must have its contents unlinked before it is
1339 void skb_unlink(struct sk_buff *skb)
1341 struct sk_buff_head *list = skb->list;
1344 unsigned long flags;
1346 spin_lock_irqsave(&list->lock, flags);
1347 if (skb->list == list)
1348 __skb_unlink(skb, skb->list);
1349 spin_unlock_irqrestore(&list->lock, flags);
1355 * skb_append - append a buffer
1356 * @old: buffer to insert after
1357 * @newsk: buffer to insert
1359 * Place a packet after a given packet in a list. The list locks are taken
1360 * and this function is atomic with respect to other list locked calls.
1361 * A buffer cannot be placed on two lists at the same time.
1364 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1366 unsigned long flags;
1368 spin_lock_irqsave(&old->list->lock, flags);
1369 __skb_append(old, newsk);
1370 spin_unlock_irqrestore(&old->list->lock, flags);
1375 * skb_insert - insert a buffer
1376 * @old: buffer to insert before
1377 * @newsk: buffer to insert
1379 * Place a packet before 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_insert(struct sk_buff *old, struct sk_buff *newsk)
1386 unsigned long flags;
1388 spin_lock_irqsave(&old->list->lock, flags);
1389 __skb_insert(newsk, old->prev, old, old->list);
1390 spin_unlock_irqrestore(&old->list->lock, flags);
1395 * Tune the memory allocator for a new MTU size.
1397 void skb_add_mtu(int mtu)
1399 /* Must match allocation in alloc_skb */
1400 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1402 kmem_add_cache_size(mtu);
1406 static inline void skb_split_inside_header(struct sk_buff *skb,
1407 struct sk_buff* skb1,
1408 const u32 len, const int pos)
1412 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1414 /* And move data appendix as is. */
1415 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1416 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1418 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1419 skb_shinfo(skb)->nr_frags = 0;
1420 skb1->data_len = skb->data_len;
1421 skb1->len += skb1->data_len;
1424 skb->tail = skb->data + len;
1427 static inline void skb_split_no_header(struct sk_buff *skb,
1428 struct sk_buff* skb1,
1429 const u32 len, int pos)
1432 const int nfrags = skb_shinfo(skb)->nr_frags;
1434 skb_shinfo(skb)->nr_frags = 0;
1435 skb1->len = skb1->data_len = skb->len - len;
1437 skb->data_len = len - pos;
1439 for (i = 0; i < nfrags; i++) {
1440 int size = skb_shinfo(skb)->frags[i].size;
1442 if (pos + size > len) {
1443 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1447 * We have to variants in this case:
1448 * 1. Move all the frag to the second
1449 * part, if it is possible. F.e.
1450 * this approach is mandatory for TUX,
1451 * where splitting is expensive.
1452 * 2. Split is accurately. We make this.
1454 get_page(skb_shinfo(skb)->frags[i].page);
1455 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1456 skb_shinfo(skb1)->frags[0].size -= len - pos;
1457 skb_shinfo(skb)->frags[i].size = len - pos;
1458 skb_shinfo(skb)->nr_frags++;
1462 skb_shinfo(skb)->nr_frags++;
1465 skb_shinfo(skb1)->nr_frags = k;
1469 * skb_split - Split fragmented skb to two parts at length len.
1471 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1473 int pos = skb_headlen(skb);
1475 if (len < pos) /* Split line is inside header. */
1476 skb_split_inside_header(skb, skb1, len, pos);
1477 else /* Second chunk has no header, nothing to copy. */
1478 skb_split_no_header(skb, skb1, len, pos);
1481 void __init skb_init(void)
1483 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1484 sizeof(struct sk_buff),
1488 if (!skbuff_head_cache)
1489 panic("cannot create skbuff cache");
1492 EXPORT_SYMBOL(___pskb_trim);
1493 EXPORT_SYMBOL(__kfree_skb);
1494 EXPORT_SYMBOL(__pskb_pull_tail);
1495 EXPORT_SYMBOL(alloc_skb);
1496 EXPORT_SYMBOL(pskb_copy);
1497 EXPORT_SYMBOL(pskb_expand_head);
1498 EXPORT_SYMBOL(skb_checksum);
1499 EXPORT_SYMBOL(skb_clone);
1500 EXPORT_SYMBOL(skb_clone_fraglist);
1501 EXPORT_SYMBOL(skb_copy);
1502 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1503 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1504 EXPORT_SYMBOL(skb_copy_bits);
1505 EXPORT_SYMBOL(skb_copy_expand);
1506 EXPORT_SYMBOL(skb_over_panic);
1507 EXPORT_SYMBOL(skb_pad);
1508 EXPORT_SYMBOL(skb_realloc_headroom);
1509 EXPORT_SYMBOL(skb_under_panic);
1510 EXPORT_SYMBOL(skb_dequeue);
1511 EXPORT_SYMBOL(skb_dequeue_tail);
1512 EXPORT_SYMBOL(skb_insert);
1513 EXPORT_SYMBOL(skb_queue_purge);
1514 EXPORT_SYMBOL(skb_queue_head);
1515 EXPORT_SYMBOL(skb_queue_tail);
1516 EXPORT_SYMBOL(skb_unlink);
1517 EXPORT_SYMBOL(skb_append);
1518 EXPORT_SYMBOL(skb_split);
1519 EXPORT_SYMBOL(skb_iter_first);
1520 EXPORT_SYMBOL(skb_iter_next);
1521 EXPORT_SYMBOL(skb_iter_abort);