[NET]: Copy mac_len in skb_clone() as well
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / skbuff.c
blob890512eb66ce1834824170a3bf74cf3b4e0ada94
1 /*
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 $
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
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
25 * NOTE:
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>
46 #include <linux/mm.h>
47 #include <linux/interrupt.h>
48 #include <linux/in.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>
54 #endif
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>
63 #include <net/dst.h>
64 #include <net/sock.h>
65 #include <net/checksum.h>
66 #include <net/xfrm.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
77 * reliable.
80 /**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
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>");
94 BUG();
97 /**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
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>");
112 BUG();
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
117 * [BEEP] leaks.
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
133 * %GFP_ATOMIC.
135 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
136 int fclone)
138 kmem_cache_t *cache;
139 struct skb_shared_info *shinfo;
140 struct sk_buff *skb;
141 u8 *data;
143 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
145 /* Get the HEAD */
146 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
147 if (!skb)
148 goto out;
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);
153 if (!data)
154 goto nodata;
156 memset(skb, 0, offsetof(struct sk_buff, truesize));
157 skb->truesize = size + sizeof(struct sk_buff);
158 atomic_set(&skb->users, 1);
159 skb->head = data;
160 skb->data = data;
161 skb->tail = data;
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;
173 if (fclone) {
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;
182 out:
183 return skb;
184 nodata:
185 kmem_cache_free(cache, skb);
186 skb = NULL;
187 goto out;
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
202 * %GFP_ATOMIC.
204 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
205 unsigned int size,
206 gfp_t gfp_mask)
208 struct sk_buff *skb;
209 u8 *data;
211 /* Get the HEAD */
212 skb = kmem_cache_alloc(skbuff_head_cache,
213 gfp_mask & ~__GFP_DMA);
214 if (!skb)
215 goto out;
217 /* Get the DATA. */
218 size = SKB_DATA_ALIGN(size);
219 data = kmem_cache_alloc(cp, gfp_mask);
220 if (!data)
221 goto nodata;
223 memset(skb, 0, offsetof(struct sk_buff, truesize));
224 skb->truesize = size + sizeof(struct sk_buff);
225 atomic_set(&skb->users, 1);
226 skb->head = data;
227 skb->data = data;
228 skb->tail = data;
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)->ufo_size = 0;
236 skb_shinfo(skb)->frag_list = NULL;
237 out:
238 return skb;
239 nodata:
240 kmem_cache_free(skbuff_head_cache, skb);
241 skb = NULL;
242 goto out;
246 static void skb_drop_list(struct sk_buff **listp)
248 struct sk_buff *list = *listp;
250 *listp = NULL;
252 do {
253 struct sk_buff *this = list;
254 list = list->next;
255 kfree_skb(this);
256 } while (list);
259 static inline void skb_drop_fraglist(struct sk_buff *skb)
261 skb_drop_list(&skb_shinfo(skb)->frag_list);
264 static void skb_clone_fraglist(struct sk_buff *skb)
266 struct sk_buff *list;
268 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
269 skb_get(list);
272 void skb_release_data(struct sk_buff *skb)
274 if (!skb->cloned ||
275 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
276 &skb_shinfo(skb)->dataref)) {
277 if (skb_shinfo(skb)->nr_frags) {
278 int i;
279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
280 put_page(skb_shinfo(skb)->frags[i].page);
283 if (skb_shinfo(skb)->frag_list)
284 skb_drop_fraglist(skb);
286 kfree(skb->head);
291 * Free an skbuff by memory without cleaning the state.
293 void kfree_skbmem(struct sk_buff *skb)
295 struct sk_buff *other;
296 atomic_t *fclone_ref;
298 skb_release_data(skb);
299 switch (skb->fclone) {
300 case SKB_FCLONE_UNAVAILABLE:
301 kmem_cache_free(skbuff_head_cache, skb);
302 break;
304 case SKB_FCLONE_ORIG:
305 fclone_ref = (atomic_t *) (skb + 2);
306 if (atomic_dec_and_test(fclone_ref))
307 kmem_cache_free(skbuff_fclone_cache, skb);
308 break;
310 case SKB_FCLONE_CLONE:
311 fclone_ref = (atomic_t *) (skb + 1);
312 other = skb - 1;
314 /* The clone portion is available for
315 * fast-cloning again.
317 skb->fclone = SKB_FCLONE_UNAVAILABLE;
319 if (atomic_dec_and_test(fclone_ref))
320 kmem_cache_free(skbuff_fclone_cache, other);
321 break;
326 * __kfree_skb - private function
327 * @skb: buffer
329 * Free an sk_buff. Release anything attached to the buffer.
330 * Clean the state. This is an internal helper function. Users should
331 * always call kfree_skb
334 void __kfree_skb(struct sk_buff *skb)
336 dst_release(skb->dst);
337 #ifdef CONFIG_XFRM
338 secpath_put(skb->sp);
339 #endif
340 if (skb->destructor) {
341 WARN_ON(in_irq());
342 skb->destructor(skb);
344 #ifdef CONFIG_NETFILTER
345 nf_conntrack_put(skb->nfct);
346 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
347 nf_conntrack_put_reasm(skb->nfct_reasm);
348 #endif
349 #ifdef CONFIG_BRIDGE_NETFILTER
350 nf_bridge_put(skb->nf_bridge);
351 #endif
352 #endif
353 /* XXX: IS this still necessary? - JHS */
354 #ifdef CONFIG_NET_SCHED
355 skb->tc_index = 0;
356 #ifdef CONFIG_NET_CLS_ACT
357 skb->tc_verd = 0;
358 #endif
359 #endif
361 kfree_skbmem(skb);
365 * skb_clone - duplicate an sk_buff
366 * @skb: buffer to clone
367 * @gfp_mask: allocation priority
369 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
370 * copies share the same packet data but not structure. The new
371 * buffer has a reference count of 1. If the allocation fails the
372 * function returns %NULL otherwise the new buffer is returned.
374 * If this function is called from an interrupt gfp_mask() must be
375 * %GFP_ATOMIC.
378 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
380 struct sk_buff *n;
382 n = skb + 1;
383 if (skb->fclone == SKB_FCLONE_ORIG &&
384 n->fclone == SKB_FCLONE_UNAVAILABLE) {
385 atomic_t *fclone_ref = (atomic_t *) (n + 1);
386 n->fclone = SKB_FCLONE_CLONE;
387 atomic_inc(fclone_ref);
388 } else {
389 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
390 if (!n)
391 return NULL;
392 n->fclone = SKB_FCLONE_UNAVAILABLE;
395 #define C(x) n->x = skb->x
397 n->next = n->prev = NULL;
398 n->sk = NULL;
399 C(tstamp);
400 C(dev);
401 C(h);
402 C(nh);
403 C(mac);
404 C(dst);
405 dst_clone(skb->dst);
406 C(sp);
407 #ifdef CONFIG_INET
408 secpath_get(skb->sp);
409 #endif
410 memcpy(n->cb, skb->cb, sizeof(skb->cb));
411 C(len);
412 C(data_len);
413 C(mac_len);
414 C(csum);
415 C(local_df);
416 n->cloned = 1;
417 n->nohdr = 0;
418 C(pkt_type);
419 C(ip_summed);
420 C(priority);
421 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
422 C(ipvs_property);
423 #endif
424 C(protocol);
425 n->destructor = NULL;
426 #ifdef CONFIG_NETFILTER
427 C(nfmark);
428 C(nfct);
429 nf_conntrack_get(skb->nfct);
430 C(nfctinfo);
431 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
432 C(nfct_reasm);
433 nf_conntrack_get_reasm(skb->nfct_reasm);
434 #endif
435 #ifdef CONFIG_BRIDGE_NETFILTER
436 C(nf_bridge);
437 nf_bridge_get(skb->nf_bridge);
438 #endif
439 #endif /*CONFIG_NETFILTER*/
440 #ifdef CONFIG_NET_SCHED
441 C(tc_index);
442 #ifdef CONFIG_NET_CLS_ACT
443 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
444 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
445 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
446 C(input_dev);
447 #endif
449 #endif
450 C(truesize);
451 atomic_set(&n->users, 1);
452 C(head);
453 C(data);
454 C(tail);
455 C(end);
457 atomic_inc(&(skb_shinfo(skb)->dataref));
458 skb->cloned = 1;
460 return n;
463 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
466 * Shift between the two data areas in bytes
468 unsigned long offset = new->data - old->data;
470 new->sk = NULL;
471 new->dev = old->dev;
472 new->priority = old->priority;
473 new->protocol = old->protocol;
474 new->dst = dst_clone(old->dst);
475 #ifdef CONFIG_INET
476 new->sp = secpath_get(old->sp);
477 #endif
478 new->h.raw = old->h.raw + offset;
479 new->nh.raw = old->nh.raw + offset;
480 new->mac.raw = old->mac.raw + offset;
481 memcpy(new->cb, old->cb, sizeof(old->cb));
482 new->local_df = old->local_df;
483 new->fclone = SKB_FCLONE_UNAVAILABLE;
484 new->pkt_type = old->pkt_type;
485 new->tstamp = old->tstamp;
486 new->destructor = NULL;
487 #ifdef CONFIG_NETFILTER
488 new->nfmark = old->nfmark;
489 new->nfct = old->nfct;
490 nf_conntrack_get(old->nfct);
491 new->nfctinfo = old->nfctinfo;
492 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
493 new->nfct_reasm = old->nfct_reasm;
494 nf_conntrack_get_reasm(old->nfct_reasm);
495 #endif
496 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
497 new->ipvs_property = old->ipvs_property;
498 #endif
499 #ifdef CONFIG_BRIDGE_NETFILTER
500 new->nf_bridge = old->nf_bridge;
501 nf_bridge_get(old->nf_bridge);
502 #endif
503 #endif
504 #ifdef CONFIG_NET_SCHED
505 #ifdef CONFIG_NET_CLS_ACT
506 new->tc_verd = old->tc_verd;
507 #endif
508 new->tc_index = old->tc_index;
509 #endif
510 atomic_set(&new->users, 1);
511 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
512 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
513 skb_shinfo(new)->ufo_size = skb_shinfo(old)->ufo_size;
517 * skb_copy - create private copy of an sk_buff
518 * @skb: buffer to copy
519 * @gfp_mask: allocation priority
521 * Make a copy of both an &sk_buff and its data. This is used when the
522 * caller wishes to modify the data and needs a private copy of the
523 * data to alter. Returns %NULL on failure or the pointer to the buffer
524 * on success. The returned buffer has a reference count of 1.
526 * As by-product this function converts non-linear &sk_buff to linear
527 * one, so that &sk_buff becomes completely private and caller is allowed
528 * to modify all the data of returned buffer. This means that this
529 * function is not recommended for use in circumstances when only
530 * header is going to be modified. Use pskb_copy() instead.
533 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
535 int headerlen = skb->data - skb->head;
537 * Allocate the copy buffer
539 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
540 gfp_mask);
541 if (!n)
542 return NULL;
544 /* Set the data pointer */
545 skb_reserve(n, headerlen);
546 /* Set the tail pointer and length */
547 skb_put(n, skb->len);
548 n->csum = skb->csum;
549 n->ip_summed = skb->ip_summed;
551 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
552 BUG();
554 copy_skb_header(n, skb);
555 return n;
560 * pskb_copy - create copy of an sk_buff with private head.
561 * @skb: buffer to copy
562 * @gfp_mask: allocation priority
564 * Make a copy of both an &sk_buff and part of its data, located
565 * in header. Fragmented data remain shared. This is used when
566 * the caller wishes to modify only header of &sk_buff and needs
567 * private copy of the header to alter. Returns %NULL on failure
568 * or the pointer to the buffer on success.
569 * The returned buffer has a reference count of 1.
572 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
575 * Allocate the copy buffer
577 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
579 if (!n)
580 goto out;
582 /* Set the data pointer */
583 skb_reserve(n, skb->data - skb->head);
584 /* Set the tail pointer and length */
585 skb_put(n, skb_headlen(skb));
586 /* Copy the bytes */
587 memcpy(n->data, skb->data, n->len);
588 n->csum = skb->csum;
589 n->ip_summed = skb->ip_summed;
591 n->truesize += skb->data_len;
592 n->data_len = skb->data_len;
593 n->len = skb->len;
595 if (skb_shinfo(skb)->nr_frags) {
596 int i;
598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
599 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
600 get_page(skb_shinfo(n)->frags[i].page);
602 skb_shinfo(n)->nr_frags = i;
605 if (skb_shinfo(skb)->frag_list) {
606 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
607 skb_clone_fraglist(n);
610 copy_skb_header(n, skb);
611 out:
612 return n;
616 * pskb_expand_head - reallocate header of &sk_buff
617 * @skb: buffer to reallocate
618 * @nhead: room to add at head
619 * @ntail: room to add at tail
620 * @gfp_mask: allocation priority
622 * Expands (or creates identical copy, if &nhead and &ntail are zero)
623 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
624 * reference count of 1. Returns zero in the case of success or error,
625 * if expansion failed. In the last case, &sk_buff is not changed.
627 * All the pointers pointing into skb header may change and must be
628 * reloaded after call to this function.
631 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
632 gfp_t gfp_mask)
634 int i;
635 u8 *data;
636 int size = nhead + (skb->end - skb->head) + ntail;
637 long off;
639 if (skb_shared(skb))
640 BUG();
642 size = SKB_DATA_ALIGN(size);
644 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
645 if (!data)
646 goto nodata;
648 /* Copy only real data... and, alas, header. This should be
649 * optimized for the cases when header is void. */
650 memcpy(data + nhead, skb->head, skb->tail - skb->head);
651 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
653 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
654 get_page(skb_shinfo(skb)->frags[i].page);
656 if (skb_shinfo(skb)->frag_list)
657 skb_clone_fraglist(skb);
659 skb_release_data(skb);
661 off = (data + nhead) - skb->head;
663 skb->head = data;
664 skb->end = data + size;
665 skb->data += off;
666 skb->tail += off;
667 skb->mac.raw += off;
668 skb->h.raw += off;
669 skb->nh.raw += off;
670 skb->cloned = 0;
671 skb->nohdr = 0;
672 atomic_set(&skb_shinfo(skb)->dataref, 1);
673 return 0;
675 nodata:
676 return -ENOMEM;
679 /* Make private copy of skb with writable head and some headroom */
681 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
683 struct sk_buff *skb2;
684 int delta = headroom - skb_headroom(skb);
686 if (delta <= 0)
687 skb2 = pskb_copy(skb, GFP_ATOMIC);
688 else {
689 skb2 = skb_clone(skb, GFP_ATOMIC);
690 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
691 GFP_ATOMIC)) {
692 kfree_skb(skb2);
693 skb2 = NULL;
696 return skb2;
701 * skb_copy_expand - copy and expand sk_buff
702 * @skb: buffer to copy
703 * @newheadroom: new free bytes at head
704 * @newtailroom: new free bytes at tail
705 * @gfp_mask: allocation priority
707 * Make a copy of both an &sk_buff and its data and while doing so
708 * allocate additional space.
710 * This is used when the caller wishes to modify the data and needs a
711 * private copy of the data to alter as well as more space for new fields.
712 * Returns %NULL on failure or the pointer to the buffer
713 * on success. The returned buffer has a reference count of 1.
715 * You must pass %GFP_ATOMIC as the allocation priority if this function
716 * is called from an interrupt.
718 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
719 * only by netfilter in the cases when checksum is recalculated? --ANK
721 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
722 int newheadroom, int newtailroom,
723 gfp_t gfp_mask)
726 * Allocate the copy buffer
728 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
729 gfp_mask);
730 int head_copy_len, head_copy_off;
732 if (!n)
733 return NULL;
735 skb_reserve(n, newheadroom);
737 /* Set the tail pointer and length */
738 skb_put(n, skb->len);
740 head_copy_len = skb_headroom(skb);
741 head_copy_off = 0;
742 if (newheadroom <= head_copy_len)
743 head_copy_len = newheadroom;
744 else
745 head_copy_off = newheadroom - head_copy_len;
747 /* Copy the linear header and data. */
748 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
749 skb->len + head_copy_len))
750 BUG();
752 copy_skb_header(n, skb);
754 return n;
758 * skb_pad - zero pad the tail of an skb
759 * @skb: buffer to pad
760 * @pad: space to pad
762 * Ensure that a buffer is followed by a padding area that is zero
763 * filled. Used by network drivers which may DMA or transfer data
764 * beyond the buffer end onto the wire.
766 * May return NULL in out of memory cases.
769 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
771 struct sk_buff *nskb;
773 /* If the skbuff is non linear tailroom is always zero.. */
774 if (skb_tailroom(skb) >= pad) {
775 memset(skb->data+skb->len, 0, pad);
776 return skb;
779 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
780 kfree_skb(skb);
781 if (nskb)
782 memset(nskb->data+nskb->len, 0, pad);
783 return nskb;
786 /* Trims skb to length len. It can change skb pointers.
789 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
791 struct sk_buff **fragp;
792 struct sk_buff *frag;
793 int offset = skb_headlen(skb);
794 int nfrags = skb_shinfo(skb)->nr_frags;
795 int i;
796 int err;
798 if (skb_cloned(skb) &&
799 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
800 return err;
802 i = 0;
803 if (offset >= len)
804 goto drop_pages;
806 for (; i < nfrags; i++) {
807 int end = offset + skb_shinfo(skb)->frags[i].size;
809 if (end < len) {
810 offset = end;
811 continue;
814 skb_shinfo(skb)->frags[i++].size = len - offset;
816 drop_pages:
817 skb_shinfo(skb)->nr_frags = i;
819 for (; i < nfrags; i++)
820 put_page(skb_shinfo(skb)->frags[i].page);
822 if (skb_shinfo(skb)->frag_list)
823 skb_drop_fraglist(skb);
824 goto done;
827 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
828 fragp = &frag->next) {
829 int end = offset + frag->len;
831 if (skb_shared(frag)) {
832 struct sk_buff *nfrag;
834 nfrag = skb_clone(frag, GFP_ATOMIC);
835 if (unlikely(!nfrag))
836 return -ENOMEM;
838 nfrag->next = frag->next;
839 kfree_skb(frag);
840 frag = nfrag;
841 *fragp = frag;
844 if (end < len) {
845 offset = end;
846 continue;
849 if (end > len &&
850 unlikely((err = pskb_trim(frag, len - offset))))
851 return err;
853 if (frag->next)
854 skb_drop_list(&frag->next);
855 break;
858 done:
859 if (len > skb_headlen(skb)) {
860 skb->data_len -= skb->len - len;
861 skb->len = len;
862 } else {
863 skb->len = len;
864 skb->data_len = 0;
865 skb->tail = skb->data + len;
868 return 0;
872 * __pskb_pull_tail - advance tail of skb header
873 * @skb: buffer to reallocate
874 * @delta: number of bytes to advance tail
876 * The function makes a sense only on a fragmented &sk_buff,
877 * it expands header moving its tail forward and copying necessary
878 * data from fragmented part.
880 * &sk_buff MUST have reference count of 1.
882 * Returns %NULL (and &sk_buff does not change) if pull failed
883 * or value of new tail of skb in the case of success.
885 * All the pointers pointing into skb header may change and must be
886 * reloaded after call to this function.
889 /* Moves tail of skb head forward, copying data from fragmented part,
890 * when it is necessary.
891 * 1. It may fail due to malloc failure.
892 * 2. It may change skb pointers.
894 * It is pretty complicated. Luckily, it is called only in exceptional cases.
896 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
898 /* If skb has not enough free space at tail, get new one
899 * plus 128 bytes for future expansions. If we have enough
900 * room at tail, reallocate without expansion only if skb is cloned.
902 int i, k, eat = (skb->tail + delta) - skb->end;
904 if (eat > 0 || skb_cloned(skb)) {
905 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
906 GFP_ATOMIC))
907 return NULL;
910 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
911 BUG();
913 /* Optimization: no fragments, no reasons to preestimate
914 * size of pulled pages. Superb.
916 if (!skb_shinfo(skb)->frag_list)
917 goto pull_pages;
919 /* Estimate size of pulled pages. */
920 eat = delta;
921 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
922 if (skb_shinfo(skb)->frags[i].size >= eat)
923 goto pull_pages;
924 eat -= skb_shinfo(skb)->frags[i].size;
927 /* If we need update frag list, we are in troubles.
928 * Certainly, it possible to add an offset to skb data,
929 * but taking into account that pulling is expected to
930 * be very rare operation, it is worth to fight against
931 * further bloating skb head and crucify ourselves here instead.
932 * Pure masohism, indeed. 8)8)
934 if (eat) {
935 struct sk_buff *list = skb_shinfo(skb)->frag_list;
936 struct sk_buff *clone = NULL;
937 struct sk_buff *insp = NULL;
939 do {
940 BUG_ON(!list);
942 if (list->len <= eat) {
943 /* Eaten as whole. */
944 eat -= list->len;
945 list = list->next;
946 insp = list;
947 } else {
948 /* Eaten partially. */
950 if (skb_shared(list)) {
951 /* Sucks! We need to fork list. :-( */
952 clone = skb_clone(list, GFP_ATOMIC);
953 if (!clone)
954 return NULL;
955 insp = list->next;
956 list = clone;
957 } else {
958 /* This may be pulled without
959 * problems. */
960 insp = list;
962 if (!pskb_pull(list, eat)) {
963 if (clone)
964 kfree_skb(clone);
965 return NULL;
967 break;
969 } while (eat);
971 /* Free pulled out fragments. */
972 while ((list = skb_shinfo(skb)->frag_list) != insp) {
973 skb_shinfo(skb)->frag_list = list->next;
974 kfree_skb(list);
976 /* And insert new clone at head. */
977 if (clone) {
978 clone->next = list;
979 skb_shinfo(skb)->frag_list = clone;
982 /* Success! Now we may commit changes to skb data. */
984 pull_pages:
985 eat = delta;
986 k = 0;
987 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
988 if (skb_shinfo(skb)->frags[i].size <= eat) {
989 put_page(skb_shinfo(skb)->frags[i].page);
990 eat -= skb_shinfo(skb)->frags[i].size;
991 } else {
992 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
993 if (eat) {
994 skb_shinfo(skb)->frags[k].page_offset += eat;
995 skb_shinfo(skb)->frags[k].size -= eat;
996 eat = 0;
998 k++;
1001 skb_shinfo(skb)->nr_frags = k;
1003 skb->tail += delta;
1004 skb->data_len -= delta;
1006 return skb->tail;
1009 /* Copy some data bits from skb to kernel buffer. */
1011 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1013 int i, copy;
1014 int start = skb_headlen(skb);
1016 if (offset > (int)skb->len - len)
1017 goto fault;
1019 /* Copy header. */
1020 if ((copy = start - offset) > 0) {
1021 if (copy > len)
1022 copy = len;
1023 memcpy(to, skb->data + offset, copy);
1024 if ((len -= copy) == 0)
1025 return 0;
1026 offset += copy;
1027 to += copy;
1030 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1031 int end;
1033 BUG_TRAP(start <= offset + len);
1035 end = start + skb_shinfo(skb)->frags[i].size;
1036 if ((copy = end - offset) > 0) {
1037 u8 *vaddr;
1039 if (copy > len)
1040 copy = len;
1042 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1043 memcpy(to,
1044 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1045 offset - start, copy);
1046 kunmap_skb_frag(vaddr);
1048 if ((len -= copy) == 0)
1049 return 0;
1050 offset += copy;
1051 to += copy;
1053 start = end;
1056 if (skb_shinfo(skb)->frag_list) {
1057 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1059 for (; list; list = list->next) {
1060 int end;
1062 BUG_TRAP(start <= offset + len);
1064 end = start + list->len;
1065 if ((copy = end - offset) > 0) {
1066 if (copy > len)
1067 copy = len;
1068 if (skb_copy_bits(list, offset - start,
1069 to, copy))
1070 goto fault;
1071 if ((len -= copy) == 0)
1072 return 0;
1073 offset += copy;
1074 to += copy;
1076 start = end;
1079 if (!len)
1080 return 0;
1082 fault:
1083 return -EFAULT;
1087 * skb_store_bits - store bits from kernel buffer to skb
1088 * @skb: destination buffer
1089 * @offset: offset in destination
1090 * @from: source buffer
1091 * @len: number of bytes to copy
1093 * Copy the specified number of bytes from the source buffer to the
1094 * destination skb. This function handles all the messy bits of
1095 * traversing fragment lists and such.
1098 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1100 int i, copy;
1101 int start = skb_headlen(skb);
1103 if (offset > (int)skb->len - len)
1104 goto fault;
1106 if ((copy = start - offset) > 0) {
1107 if (copy > len)
1108 copy = len;
1109 memcpy(skb->data + offset, from, copy);
1110 if ((len -= copy) == 0)
1111 return 0;
1112 offset += copy;
1113 from += copy;
1116 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1117 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1118 int end;
1120 BUG_TRAP(start <= offset + len);
1122 end = start + frag->size;
1123 if ((copy = end - offset) > 0) {
1124 u8 *vaddr;
1126 if (copy > len)
1127 copy = len;
1129 vaddr = kmap_skb_frag(frag);
1130 memcpy(vaddr + frag->page_offset + offset - start,
1131 from, copy);
1132 kunmap_skb_frag(vaddr);
1134 if ((len -= copy) == 0)
1135 return 0;
1136 offset += copy;
1137 from += copy;
1139 start = end;
1142 if (skb_shinfo(skb)->frag_list) {
1143 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1145 for (; list; list = list->next) {
1146 int end;
1148 BUG_TRAP(start <= offset + len);
1150 end = start + list->len;
1151 if ((copy = end - offset) > 0) {
1152 if (copy > len)
1153 copy = len;
1154 if (skb_store_bits(list, offset - start,
1155 from, copy))
1156 goto fault;
1157 if ((len -= copy) == 0)
1158 return 0;
1159 offset += copy;
1160 from += copy;
1162 start = end;
1165 if (!len)
1166 return 0;
1168 fault:
1169 return -EFAULT;
1172 EXPORT_SYMBOL(skb_store_bits);
1174 /* Checksum skb data. */
1176 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1177 int len, unsigned int csum)
1179 int start = skb_headlen(skb);
1180 int i, copy = start - offset;
1181 int pos = 0;
1183 /* Checksum header. */
1184 if (copy > 0) {
1185 if (copy > len)
1186 copy = len;
1187 csum = csum_partial(skb->data + offset, copy, csum);
1188 if ((len -= copy) == 0)
1189 return csum;
1190 offset += copy;
1191 pos = copy;
1194 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1195 int end;
1197 BUG_TRAP(start <= offset + len);
1199 end = start + skb_shinfo(skb)->frags[i].size;
1200 if ((copy = end - offset) > 0) {
1201 unsigned int csum2;
1202 u8 *vaddr;
1203 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1205 if (copy > len)
1206 copy = len;
1207 vaddr = kmap_skb_frag(frag);
1208 csum2 = csum_partial(vaddr + frag->page_offset +
1209 offset - start, copy, 0);
1210 kunmap_skb_frag(vaddr);
1211 csum = csum_block_add(csum, csum2, pos);
1212 if (!(len -= copy))
1213 return csum;
1214 offset += copy;
1215 pos += copy;
1217 start = end;
1220 if (skb_shinfo(skb)->frag_list) {
1221 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1223 for (; list; list = list->next) {
1224 int end;
1226 BUG_TRAP(start <= offset + len);
1228 end = start + list->len;
1229 if ((copy = end - offset) > 0) {
1230 unsigned int csum2;
1231 if (copy > len)
1232 copy = len;
1233 csum2 = skb_checksum(list, offset - start,
1234 copy, 0);
1235 csum = csum_block_add(csum, csum2, pos);
1236 if ((len -= copy) == 0)
1237 return csum;
1238 offset += copy;
1239 pos += copy;
1241 start = end;
1244 BUG_ON(len);
1246 return csum;
1249 /* Both of above in one bottle. */
1251 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1252 u8 *to, int len, unsigned int csum)
1254 int start = skb_headlen(skb);
1255 int i, copy = start - offset;
1256 int pos = 0;
1258 /* Copy header. */
1259 if (copy > 0) {
1260 if (copy > len)
1261 copy = len;
1262 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1263 copy, csum);
1264 if ((len -= copy) == 0)
1265 return csum;
1266 offset += copy;
1267 to += copy;
1268 pos = copy;
1271 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1272 int end;
1274 BUG_TRAP(start <= offset + len);
1276 end = start + skb_shinfo(skb)->frags[i].size;
1277 if ((copy = end - offset) > 0) {
1278 unsigned int csum2;
1279 u8 *vaddr;
1280 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1282 if (copy > len)
1283 copy = len;
1284 vaddr = kmap_skb_frag(frag);
1285 csum2 = csum_partial_copy_nocheck(vaddr +
1286 frag->page_offset +
1287 offset - start, to,
1288 copy, 0);
1289 kunmap_skb_frag(vaddr);
1290 csum = csum_block_add(csum, csum2, pos);
1291 if (!(len -= copy))
1292 return csum;
1293 offset += copy;
1294 to += copy;
1295 pos += copy;
1297 start = end;
1300 if (skb_shinfo(skb)->frag_list) {
1301 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1303 for (; list; list = list->next) {
1304 unsigned int csum2;
1305 int end;
1307 BUG_TRAP(start <= offset + len);
1309 end = start + list->len;
1310 if ((copy = end - offset) > 0) {
1311 if (copy > len)
1312 copy = len;
1313 csum2 = skb_copy_and_csum_bits(list,
1314 offset - start,
1315 to, copy, 0);
1316 csum = csum_block_add(csum, csum2, pos);
1317 if ((len -= copy) == 0)
1318 return csum;
1319 offset += copy;
1320 to += copy;
1321 pos += copy;
1323 start = end;
1326 BUG_ON(len);
1327 return csum;
1330 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1332 unsigned int csum;
1333 long csstart;
1335 if (skb->ip_summed == CHECKSUM_HW)
1336 csstart = skb->h.raw - skb->data;
1337 else
1338 csstart = skb_headlen(skb);
1340 BUG_ON(csstart > skb_headlen(skb));
1342 memcpy(to, skb->data, csstart);
1344 csum = 0;
1345 if (csstart != skb->len)
1346 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1347 skb->len - csstart, 0);
1349 if (skb->ip_summed == CHECKSUM_HW) {
1350 long csstuff = csstart + skb->csum;
1352 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1357 * skb_dequeue - remove from the head of the queue
1358 * @list: list to dequeue from
1360 * Remove the head of the list. The list lock is taken so the function
1361 * may be used safely with other locking list functions. The head item is
1362 * returned or %NULL if the list is empty.
1365 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1367 unsigned long flags;
1368 struct sk_buff *result;
1370 spin_lock_irqsave(&list->lock, flags);
1371 result = __skb_dequeue(list);
1372 spin_unlock_irqrestore(&list->lock, flags);
1373 return result;
1377 * skb_dequeue_tail - remove from the tail of the queue
1378 * @list: list to dequeue from
1380 * Remove the tail of the list. The list lock is taken so the function
1381 * may be used safely with other locking list functions. The tail item is
1382 * returned or %NULL if the list is empty.
1384 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1386 unsigned long flags;
1387 struct sk_buff *result;
1389 spin_lock_irqsave(&list->lock, flags);
1390 result = __skb_dequeue_tail(list);
1391 spin_unlock_irqrestore(&list->lock, flags);
1392 return result;
1396 * skb_queue_purge - empty a list
1397 * @list: list to empty
1399 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1400 * the list and one reference dropped. This function takes the list
1401 * lock and is atomic with respect to other list locking functions.
1403 void skb_queue_purge(struct sk_buff_head *list)
1405 struct sk_buff *skb;
1406 while ((skb = skb_dequeue(list)) != NULL)
1407 kfree_skb(skb);
1411 * skb_queue_head - queue a buffer at the list head
1412 * @list: list to use
1413 * @newsk: buffer to queue
1415 * Queue a buffer at the start of the list. This function takes the
1416 * list lock and can be used safely with other locking &sk_buff functions
1417 * safely.
1419 * A buffer cannot be placed on two lists at the same time.
1421 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1423 unsigned long flags;
1425 spin_lock_irqsave(&list->lock, flags);
1426 __skb_queue_head(list, newsk);
1427 spin_unlock_irqrestore(&list->lock, flags);
1431 * skb_queue_tail - queue a buffer at the list tail
1432 * @list: list to use
1433 * @newsk: buffer to queue
1435 * Queue a buffer at the tail of the list. This function takes the
1436 * list lock and can be used safely with other locking &sk_buff functions
1437 * safely.
1439 * A buffer cannot be placed on two lists at the same time.
1441 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1443 unsigned long flags;
1445 spin_lock_irqsave(&list->lock, flags);
1446 __skb_queue_tail(list, newsk);
1447 spin_unlock_irqrestore(&list->lock, flags);
1451 * skb_unlink - remove a buffer from a list
1452 * @skb: buffer to remove
1453 * @list: list to use
1455 * Remove a packet from a list. The list locks are taken and this
1456 * function is atomic with respect to other list locked calls
1458 * You must know what list the SKB is on.
1460 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1462 unsigned long flags;
1464 spin_lock_irqsave(&list->lock, flags);
1465 __skb_unlink(skb, list);
1466 spin_unlock_irqrestore(&list->lock, flags);
1470 * skb_append - append a buffer
1471 * @old: buffer to insert after
1472 * @newsk: buffer to insert
1473 * @list: list to use
1475 * Place a packet after a given packet in a list. The list locks are taken
1476 * and this function is atomic with respect to other list locked calls.
1477 * A buffer cannot be placed on two lists at the same time.
1479 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1481 unsigned long flags;
1483 spin_lock_irqsave(&list->lock, flags);
1484 __skb_append(old, newsk, list);
1485 spin_unlock_irqrestore(&list->lock, flags);
1490 * skb_insert - insert a buffer
1491 * @old: buffer to insert before
1492 * @newsk: buffer to insert
1493 * @list: list to use
1495 * Place a packet before a given packet in a list. The list locks are
1496 * taken and this function is atomic with respect to other list locked
1497 * calls.
1499 * A buffer cannot be placed on two lists at the same time.
1501 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1503 unsigned long flags;
1505 spin_lock_irqsave(&list->lock, flags);
1506 __skb_insert(newsk, old->prev, old, list);
1507 spin_unlock_irqrestore(&list->lock, flags);
1510 #if 0
1512 * Tune the memory allocator for a new MTU size.
1514 void skb_add_mtu(int mtu)
1516 /* Must match allocation in alloc_skb */
1517 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1519 kmem_add_cache_size(mtu);
1521 #endif
1523 static inline void skb_split_inside_header(struct sk_buff *skb,
1524 struct sk_buff* skb1,
1525 const u32 len, const int pos)
1527 int i;
1529 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1531 /* And move data appendix as is. */
1532 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1533 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1535 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1536 skb_shinfo(skb)->nr_frags = 0;
1537 skb1->data_len = skb->data_len;
1538 skb1->len += skb1->data_len;
1539 skb->data_len = 0;
1540 skb->len = len;
1541 skb->tail = skb->data + len;
1544 static inline void skb_split_no_header(struct sk_buff *skb,
1545 struct sk_buff* skb1,
1546 const u32 len, int pos)
1548 int i, k = 0;
1549 const int nfrags = skb_shinfo(skb)->nr_frags;
1551 skb_shinfo(skb)->nr_frags = 0;
1552 skb1->len = skb1->data_len = skb->len - len;
1553 skb->len = len;
1554 skb->data_len = len - pos;
1556 for (i = 0; i < nfrags; i++) {
1557 int size = skb_shinfo(skb)->frags[i].size;
1559 if (pos + size > len) {
1560 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1562 if (pos < len) {
1563 /* Split frag.
1564 * We have two variants in this case:
1565 * 1. Move all the frag to the second
1566 * part, if it is possible. F.e.
1567 * this approach is mandatory for TUX,
1568 * where splitting is expensive.
1569 * 2. Split is accurately. We make this.
1571 get_page(skb_shinfo(skb)->frags[i].page);
1572 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1573 skb_shinfo(skb1)->frags[0].size -= len - pos;
1574 skb_shinfo(skb)->frags[i].size = len - pos;
1575 skb_shinfo(skb)->nr_frags++;
1577 k++;
1578 } else
1579 skb_shinfo(skb)->nr_frags++;
1580 pos += size;
1582 skb_shinfo(skb1)->nr_frags = k;
1586 * skb_split - Split fragmented skb to two parts at length len.
1587 * @skb: the buffer to split
1588 * @skb1: the buffer to receive the second part
1589 * @len: new length for skb
1591 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1593 int pos = skb_headlen(skb);
1595 if (len < pos) /* Split line is inside header. */
1596 skb_split_inside_header(skb, skb1, len, pos);
1597 else /* Second chunk has no header, nothing to copy. */
1598 skb_split_no_header(skb, skb1, len, pos);
1602 * skb_prepare_seq_read - Prepare a sequential read of skb data
1603 * @skb: the buffer to read
1604 * @from: lower offset of data to be read
1605 * @to: upper offset of data to be read
1606 * @st: state variable
1608 * Initializes the specified state variable. Must be called before
1609 * invoking skb_seq_read() for the first time.
1611 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1612 unsigned int to, struct skb_seq_state *st)
1614 st->lower_offset = from;
1615 st->upper_offset = to;
1616 st->root_skb = st->cur_skb = skb;
1617 st->frag_idx = st->stepped_offset = 0;
1618 st->frag_data = NULL;
1622 * skb_seq_read - Sequentially read skb data
1623 * @consumed: number of bytes consumed by the caller so far
1624 * @data: destination pointer for data to be returned
1625 * @st: state variable
1627 * Reads a block of skb data at &consumed relative to the
1628 * lower offset specified to skb_prepare_seq_read(). Assigns
1629 * the head of the data block to &data and returns the length
1630 * of the block or 0 if the end of the skb data or the upper
1631 * offset has been reached.
1633 * The caller is not required to consume all of the data
1634 * returned, i.e. &consumed is typically set to the number
1635 * of bytes already consumed and the next call to
1636 * skb_seq_read() will return the remaining part of the block.
1638 * Note: The size of each block of data returned can be arbitary,
1639 * this limitation is the cost for zerocopy seqeuental
1640 * reads of potentially non linear data.
1642 * Note: Fragment lists within fragments are not implemented
1643 * at the moment, state->root_skb could be replaced with
1644 * a stack for this purpose.
1646 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1647 struct skb_seq_state *st)
1649 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1650 skb_frag_t *frag;
1652 if (unlikely(abs_offset >= st->upper_offset))
1653 return 0;
1655 next_skb:
1656 block_limit = skb_headlen(st->cur_skb);
1658 if (abs_offset < block_limit) {
1659 *data = st->cur_skb->data + abs_offset;
1660 return block_limit - abs_offset;
1663 if (st->frag_idx == 0 && !st->frag_data)
1664 st->stepped_offset += skb_headlen(st->cur_skb);
1666 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1667 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1668 block_limit = frag->size + st->stepped_offset;
1670 if (abs_offset < block_limit) {
1671 if (!st->frag_data)
1672 st->frag_data = kmap_skb_frag(frag);
1674 *data = (u8 *) st->frag_data + frag->page_offset +
1675 (abs_offset - st->stepped_offset);
1677 return block_limit - abs_offset;
1680 if (st->frag_data) {
1681 kunmap_skb_frag(st->frag_data);
1682 st->frag_data = NULL;
1685 st->frag_idx++;
1686 st->stepped_offset += frag->size;
1689 if (st->cur_skb->next) {
1690 st->cur_skb = st->cur_skb->next;
1691 st->frag_idx = 0;
1692 goto next_skb;
1693 } else if (st->root_skb == st->cur_skb &&
1694 skb_shinfo(st->root_skb)->frag_list) {
1695 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1696 goto next_skb;
1699 return 0;
1703 * skb_abort_seq_read - Abort a sequential read of skb data
1704 * @st: state variable
1706 * Must be called if skb_seq_read() was not called until it
1707 * returned 0.
1709 void skb_abort_seq_read(struct skb_seq_state *st)
1711 if (st->frag_data)
1712 kunmap_skb_frag(st->frag_data);
1715 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1717 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1718 struct ts_config *conf,
1719 struct ts_state *state)
1721 return skb_seq_read(offset, text, TS_SKB_CB(state));
1724 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1726 skb_abort_seq_read(TS_SKB_CB(state));
1730 * skb_find_text - Find a text pattern in skb data
1731 * @skb: the buffer to look in
1732 * @from: search offset
1733 * @to: search limit
1734 * @config: textsearch configuration
1735 * @state: uninitialized textsearch state variable
1737 * Finds a pattern in the skb data according to the specified
1738 * textsearch configuration. Use textsearch_next() to retrieve
1739 * subsequent occurrences of the pattern. Returns the offset
1740 * to the first occurrence or UINT_MAX if no match was found.
1742 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1743 unsigned int to, struct ts_config *config,
1744 struct ts_state *state)
1746 config->get_next_block = skb_ts_get_next_block;
1747 config->finish = skb_ts_finish;
1749 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1751 return textsearch_find(config, state);
1755 * skb_append_datato_frags: - append the user data to a skb
1756 * @sk: sock structure
1757 * @skb: skb structure to be appened with user data.
1758 * @getfrag: call back function to be used for getting the user data
1759 * @from: pointer to user message iov
1760 * @length: length of the iov message
1762 * Description: This procedure append the user data in the fragment part
1763 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1765 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1766 int (*getfrag)(void *from, char *to, int offset,
1767 int len, int odd, struct sk_buff *skb),
1768 void *from, int length)
1770 int frg_cnt = 0;
1771 skb_frag_t *frag = NULL;
1772 struct page *page = NULL;
1773 int copy, left;
1774 int offset = 0;
1775 int ret;
1777 do {
1778 /* Return error if we don't have space for new frag */
1779 frg_cnt = skb_shinfo(skb)->nr_frags;
1780 if (frg_cnt >= MAX_SKB_FRAGS)
1781 return -EFAULT;
1783 /* allocate a new page for next frag */
1784 page = alloc_pages(sk->sk_allocation, 0);
1786 /* If alloc_page fails just return failure and caller will
1787 * free previous allocated pages by doing kfree_skb()
1789 if (page == NULL)
1790 return -ENOMEM;
1792 /* initialize the next frag */
1793 sk->sk_sndmsg_page = page;
1794 sk->sk_sndmsg_off = 0;
1795 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1796 skb->truesize += PAGE_SIZE;
1797 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1799 /* get the new initialized frag */
1800 frg_cnt = skb_shinfo(skb)->nr_frags;
1801 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1803 /* copy the user data to page */
1804 left = PAGE_SIZE - frag->page_offset;
1805 copy = (length > left)? left : length;
1807 ret = getfrag(from, (page_address(frag->page) +
1808 frag->page_offset + frag->size),
1809 offset, copy, 0, skb);
1810 if (ret < 0)
1811 return -EFAULT;
1813 /* copy was successful so update the size parameters */
1814 sk->sk_sndmsg_off += copy;
1815 frag->size += copy;
1816 skb->len += copy;
1817 skb->data_len += copy;
1818 offset += copy;
1819 length -= copy;
1821 } while (length > 0);
1823 return 0;
1826 void __init skb_init(void)
1828 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1829 sizeof(struct sk_buff),
1831 SLAB_HWCACHE_ALIGN,
1832 NULL, NULL);
1833 if (!skbuff_head_cache)
1834 panic("cannot create skbuff cache");
1836 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1837 (2*sizeof(struct sk_buff)) +
1838 sizeof(atomic_t),
1840 SLAB_HWCACHE_ALIGN,
1841 NULL, NULL);
1842 if (!skbuff_fclone_cache)
1843 panic("cannot create skbuff cache");
1846 EXPORT_SYMBOL(___pskb_trim);
1847 EXPORT_SYMBOL(__kfree_skb);
1848 EXPORT_SYMBOL(__pskb_pull_tail);
1849 EXPORT_SYMBOL(__alloc_skb);
1850 EXPORT_SYMBOL(pskb_copy);
1851 EXPORT_SYMBOL(pskb_expand_head);
1852 EXPORT_SYMBOL(skb_checksum);
1853 EXPORT_SYMBOL(skb_clone);
1854 EXPORT_SYMBOL(skb_clone_fraglist);
1855 EXPORT_SYMBOL(skb_copy);
1856 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1857 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1858 EXPORT_SYMBOL(skb_copy_bits);
1859 EXPORT_SYMBOL(skb_copy_expand);
1860 EXPORT_SYMBOL(skb_over_panic);
1861 EXPORT_SYMBOL(skb_pad);
1862 EXPORT_SYMBOL(skb_realloc_headroom);
1863 EXPORT_SYMBOL(skb_under_panic);
1864 EXPORT_SYMBOL(skb_dequeue);
1865 EXPORT_SYMBOL(skb_dequeue_tail);
1866 EXPORT_SYMBOL(skb_insert);
1867 EXPORT_SYMBOL(skb_queue_purge);
1868 EXPORT_SYMBOL(skb_queue_head);
1869 EXPORT_SYMBOL(skb_queue_tail);
1870 EXPORT_SYMBOL(skb_unlink);
1871 EXPORT_SYMBOL(skb_append);
1872 EXPORT_SYMBOL(skb_split);
1873 EXPORT_SYMBOL(skb_prepare_seq_read);
1874 EXPORT_SYMBOL(skb_seq_read);
1875 EXPORT_SYMBOL(skb_abort_seq_read);
1876 EXPORT_SYMBOL(skb_find_text);
1877 EXPORT_SYMBOL(skb_append_datato_frags);