[PATCH] update OBSOLETE_OSS_DRIVER schedule and dependencies
[usb.git] / net / core / skbuff.c
blob09464fa8d72f8b10c881b44678e8aa1084a3bcfb
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)->frag_list = NULL;
236 out:
237 return skb;
238 nodata:
239 kmem_cache_free(skbuff_head_cache, skb);
240 skb = NULL;
241 goto out;
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;
251 do {
252 struct sk_buff *this = list;
253 list = list->next;
254 kfree_skb(this);
255 } while (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)
263 skb_get(list);
266 void skb_release_data(struct sk_buff *skb)
268 if (!skb->cloned ||
269 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
270 &skb_shinfo(skb)->dataref)) {
271 if (skb_shinfo(skb)->nr_frags) {
272 int i;
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);
280 kfree(skb->head);
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);
296 break;
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);
302 break;
304 case SKB_FCLONE_CLONE:
305 fclone_ref = (atomic_t *) (skb + 1);
306 other = 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);
315 break;
320 * __kfree_skb - private function
321 * @skb: buffer
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);
331 #ifdef CONFIG_XFRM
332 secpath_put(skb->sp);
333 #endif
334 if (skb->destructor) {
335 WARN_ON(in_irq());
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);
342 #endif
343 #ifdef CONFIG_BRIDGE_NETFILTER
344 nf_bridge_put(skb->nf_bridge);
345 #endif
346 #endif
347 /* XXX: IS this still necessary? - JHS */
348 #ifdef CONFIG_NET_SCHED
349 skb->tc_index = 0;
350 #ifdef CONFIG_NET_CLS_ACT
351 skb->tc_verd = 0;
352 #endif
353 #endif
355 kfree_skbmem(skb);
359 * kfree_skb - free an sk_buff
360 * @skb: buffer to free
362 * Drop a reference to the buffer and free it if the usage count has
363 * hit zero.
365 void kfree_skb(struct sk_buff *skb)
367 if (unlikely(!skb))
368 return;
369 if (likely(atomic_read(&skb->users) == 1))
370 smp_rmb();
371 else if (likely(!atomic_dec_and_test(&skb->users)))
372 return;
373 __kfree_skb(skb);
377 * skb_clone - duplicate an sk_buff
378 * @skb: buffer to clone
379 * @gfp_mask: allocation priority
381 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
382 * copies share the same packet data but not structure. The new
383 * buffer has a reference count of 1. If the allocation fails the
384 * function returns %NULL otherwise the new buffer is returned.
386 * If this function is called from an interrupt gfp_mask() must be
387 * %GFP_ATOMIC.
390 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
392 struct sk_buff *n;
394 n = skb + 1;
395 if (skb->fclone == SKB_FCLONE_ORIG &&
396 n->fclone == SKB_FCLONE_UNAVAILABLE) {
397 atomic_t *fclone_ref = (atomic_t *) (n + 1);
398 n->fclone = SKB_FCLONE_CLONE;
399 atomic_inc(fclone_ref);
400 } else {
401 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
402 if (!n)
403 return NULL;
404 n->fclone = SKB_FCLONE_UNAVAILABLE;
407 #define C(x) n->x = skb->x
409 n->next = n->prev = NULL;
410 n->sk = NULL;
411 C(tstamp);
412 C(dev);
413 C(h);
414 C(nh);
415 C(mac);
416 C(dst);
417 dst_clone(skb->dst);
418 C(sp);
419 #ifdef CONFIG_INET
420 secpath_get(skb->sp);
421 #endif
422 memcpy(n->cb, skb->cb, sizeof(skb->cb));
423 C(len);
424 C(data_len);
425 C(csum);
426 C(local_df);
427 n->cloned = 1;
428 n->nohdr = 0;
429 C(pkt_type);
430 C(ip_summed);
431 C(priority);
432 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
433 C(ipvs_property);
434 #endif
435 C(protocol);
436 n->destructor = NULL;
437 #ifdef CONFIG_NETFILTER
438 C(nfmark);
439 C(nfct);
440 nf_conntrack_get(skb->nfct);
441 C(nfctinfo);
442 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
443 C(nfct_reasm);
444 nf_conntrack_get_reasm(skb->nfct_reasm);
445 #endif
446 #ifdef CONFIG_BRIDGE_NETFILTER
447 C(nf_bridge);
448 nf_bridge_get(skb->nf_bridge);
449 #endif
450 #endif /*CONFIG_NETFILTER*/
451 #ifdef CONFIG_NET_SCHED
452 C(tc_index);
453 #ifdef CONFIG_NET_CLS_ACT
454 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
455 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
456 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
457 C(input_dev);
458 #endif
460 #endif
461 C(truesize);
462 atomic_set(&n->users, 1);
463 C(head);
464 C(data);
465 C(tail);
466 C(end);
468 atomic_inc(&(skb_shinfo(skb)->dataref));
469 skb->cloned = 1;
471 return n;
474 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
477 * Shift between the two data areas in bytes
479 unsigned long offset = new->data - old->data;
481 new->sk = NULL;
482 new->dev = old->dev;
483 new->priority = old->priority;
484 new->protocol = old->protocol;
485 new->dst = dst_clone(old->dst);
486 #ifdef CONFIG_INET
487 new->sp = secpath_get(old->sp);
488 #endif
489 new->h.raw = old->h.raw + offset;
490 new->nh.raw = old->nh.raw + offset;
491 new->mac.raw = old->mac.raw + offset;
492 memcpy(new->cb, old->cb, sizeof(old->cb));
493 new->local_df = old->local_df;
494 new->fclone = SKB_FCLONE_UNAVAILABLE;
495 new->pkt_type = old->pkt_type;
496 new->tstamp = old->tstamp;
497 new->destructor = NULL;
498 #ifdef CONFIG_NETFILTER
499 new->nfmark = old->nfmark;
500 new->nfct = old->nfct;
501 nf_conntrack_get(old->nfct);
502 new->nfctinfo = old->nfctinfo;
503 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
504 new->nfct_reasm = old->nfct_reasm;
505 nf_conntrack_get_reasm(old->nfct_reasm);
506 #endif
507 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
508 new->ipvs_property = old->ipvs_property;
509 #endif
510 #ifdef CONFIG_BRIDGE_NETFILTER
511 new->nf_bridge = old->nf_bridge;
512 nf_bridge_get(old->nf_bridge);
513 #endif
514 #endif
515 #ifdef CONFIG_NET_SCHED
516 #ifdef CONFIG_NET_CLS_ACT
517 new->tc_verd = old->tc_verd;
518 #endif
519 new->tc_index = old->tc_index;
520 #endif
521 atomic_set(&new->users, 1);
522 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
523 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
527 * skb_copy - create private copy of an sk_buff
528 * @skb: buffer to copy
529 * @gfp_mask: allocation priority
531 * Make a copy of both an &sk_buff and its data. This is used when the
532 * caller wishes to modify the data and needs a private copy of the
533 * data to alter. Returns %NULL on failure or the pointer to the buffer
534 * on success. The returned buffer has a reference count of 1.
536 * As by-product this function converts non-linear &sk_buff to linear
537 * one, so that &sk_buff becomes completely private and caller is allowed
538 * to modify all the data of returned buffer. This means that this
539 * function is not recommended for use in circumstances when only
540 * header is going to be modified. Use pskb_copy() instead.
543 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
545 int headerlen = skb->data - skb->head;
547 * Allocate the copy buffer
549 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
550 gfp_mask);
551 if (!n)
552 return NULL;
554 /* Set the data pointer */
555 skb_reserve(n, headerlen);
556 /* Set the tail pointer and length */
557 skb_put(n, skb->len);
558 n->csum = skb->csum;
559 n->ip_summed = skb->ip_summed;
561 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
562 BUG();
564 copy_skb_header(n, skb);
565 return n;
570 * pskb_copy - create copy of an sk_buff with private head.
571 * @skb: buffer to copy
572 * @gfp_mask: allocation priority
574 * Make a copy of both an &sk_buff and part of its data, located
575 * in header. Fragmented data remain shared. This is used when
576 * the caller wishes to modify only header of &sk_buff and needs
577 * private copy of the header to alter. Returns %NULL on failure
578 * or the pointer to the buffer on success.
579 * The returned buffer has a reference count of 1.
582 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
585 * Allocate the copy buffer
587 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
589 if (!n)
590 goto out;
592 /* Set the data pointer */
593 skb_reserve(n, skb->data - skb->head);
594 /* Set the tail pointer and length */
595 skb_put(n, skb_headlen(skb));
596 /* Copy the bytes */
597 memcpy(n->data, skb->data, n->len);
598 n->csum = skb->csum;
599 n->ip_summed = skb->ip_summed;
601 n->data_len = skb->data_len;
602 n->len = skb->len;
604 if (skb_shinfo(skb)->nr_frags) {
605 int i;
607 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
608 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
609 get_page(skb_shinfo(n)->frags[i].page);
611 skb_shinfo(n)->nr_frags = i;
614 if (skb_shinfo(skb)->frag_list) {
615 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
616 skb_clone_fraglist(n);
619 copy_skb_header(n, skb);
620 out:
621 return n;
625 * pskb_expand_head - reallocate header of &sk_buff
626 * @skb: buffer to reallocate
627 * @nhead: room to add at head
628 * @ntail: room to add at tail
629 * @gfp_mask: allocation priority
631 * Expands (or creates identical copy, if &nhead and &ntail are zero)
632 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
633 * reference count of 1. Returns zero in the case of success or error,
634 * if expansion failed. In the last case, &sk_buff is not changed.
636 * All the pointers pointing into skb header may change and must be
637 * reloaded after call to this function.
640 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
641 gfp_t gfp_mask)
643 int i;
644 u8 *data;
645 int size = nhead + (skb->end - skb->head) + ntail;
646 long off;
648 if (skb_shared(skb))
649 BUG();
651 size = SKB_DATA_ALIGN(size);
653 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
654 if (!data)
655 goto nodata;
657 /* Copy only real data... and, alas, header. This should be
658 * optimized for the cases when header is void. */
659 memcpy(data + nhead, skb->head, skb->tail - skb->head);
660 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
663 get_page(skb_shinfo(skb)->frags[i].page);
665 if (skb_shinfo(skb)->frag_list)
666 skb_clone_fraglist(skb);
668 skb_release_data(skb);
670 off = (data + nhead) - skb->head;
672 skb->head = data;
673 skb->end = data + size;
674 skb->data += off;
675 skb->tail += off;
676 skb->mac.raw += off;
677 skb->h.raw += off;
678 skb->nh.raw += off;
679 skb->cloned = 0;
680 skb->nohdr = 0;
681 atomic_set(&skb_shinfo(skb)->dataref, 1);
682 return 0;
684 nodata:
685 return -ENOMEM;
688 /* Make private copy of skb with writable head and some headroom */
690 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
692 struct sk_buff *skb2;
693 int delta = headroom - skb_headroom(skb);
695 if (delta <= 0)
696 skb2 = pskb_copy(skb, GFP_ATOMIC);
697 else {
698 skb2 = skb_clone(skb, GFP_ATOMIC);
699 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
700 GFP_ATOMIC)) {
701 kfree_skb(skb2);
702 skb2 = NULL;
705 return skb2;
710 * skb_copy_expand - copy and expand sk_buff
711 * @skb: buffer to copy
712 * @newheadroom: new free bytes at head
713 * @newtailroom: new free bytes at tail
714 * @gfp_mask: allocation priority
716 * Make a copy of both an &sk_buff and its data and while doing so
717 * allocate additional space.
719 * This is used when the caller wishes to modify the data and needs a
720 * private copy of the data to alter as well as more space for new fields.
721 * Returns %NULL on failure or the pointer to the buffer
722 * on success. The returned buffer has a reference count of 1.
724 * You must pass %GFP_ATOMIC as the allocation priority if this function
725 * is called from an interrupt.
727 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
728 * only by netfilter in the cases when checksum is recalculated? --ANK
730 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
731 int newheadroom, int newtailroom,
732 gfp_t gfp_mask)
735 * Allocate the copy buffer
737 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
738 gfp_mask);
739 int head_copy_len, head_copy_off;
741 if (!n)
742 return NULL;
744 skb_reserve(n, newheadroom);
746 /* Set the tail pointer and length */
747 skb_put(n, skb->len);
749 head_copy_len = skb_headroom(skb);
750 head_copy_off = 0;
751 if (newheadroom <= head_copy_len)
752 head_copy_len = newheadroom;
753 else
754 head_copy_off = newheadroom - head_copy_len;
756 /* Copy the linear header and data. */
757 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
758 skb->len + head_copy_len))
759 BUG();
761 copy_skb_header(n, skb);
763 return n;
767 * skb_pad - zero pad the tail of an skb
768 * @skb: buffer to pad
769 * @pad: space to pad
771 * Ensure that a buffer is followed by a padding area that is zero
772 * filled. Used by network drivers which may DMA or transfer data
773 * beyond the buffer end onto the wire.
775 * May return NULL in out of memory cases.
778 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
780 struct sk_buff *nskb;
782 /* If the skbuff is non linear tailroom is always zero.. */
783 if (skb_tailroom(skb) >= pad) {
784 memset(skb->data+skb->len, 0, pad);
785 return skb;
788 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
789 kfree_skb(skb);
790 if (nskb)
791 memset(nskb->data+nskb->len, 0, pad);
792 return nskb;
795 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
796 * If realloc==0 and trimming is impossible without change of data,
797 * it is BUG().
800 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
802 int offset = skb_headlen(skb);
803 int nfrags = skb_shinfo(skb)->nr_frags;
804 int i;
806 for (i = 0; i < nfrags; i++) {
807 int end = offset + skb_shinfo(skb)->frags[i].size;
808 if (end > len) {
809 if (skb_cloned(skb)) {
810 BUG_ON(!realloc);
811 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
812 return -ENOMEM;
814 if (len <= offset) {
815 put_page(skb_shinfo(skb)->frags[i].page);
816 skb_shinfo(skb)->nr_frags--;
817 } else {
818 skb_shinfo(skb)->frags[i].size = len - offset;
821 offset = end;
824 if (offset < len) {
825 skb->data_len -= skb->len - len;
826 skb->len = len;
827 } else {
828 if (len <= skb_headlen(skb)) {
829 skb->len = len;
830 skb->data_len = 0;
831 skb->tail = skb->data + len;
832 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
833 skb_drop_fraglist(skb);
834 } else {
835 skb->data_len -= skb->len - len;
836 skb->len = len;
840 return 0;
844 * __pskb_pull_tail - advance tail of skb header
845 * @skb: buffer to reallocate
846 * @delta: number of bytes to advance tail
848 * The function makes a sense only on a fragmented &sk_buff,
849 * it expands header moving its tail forward and copying necessary
850 * data from fragmented part.
852 * &sk_buff MUST have reference count of 1.
854 * Returns %NULL (and &sk_buff does not change) if pull failed
855 * or value of new tail of skb in the case of success.
857 * All the pointers pointing into skb header may change and must be
858 * reloaded after call to this function.
861 /* Moves tail of skb head forward, copying data from fragmented part,
862 * when it is necessary.
863 * 1. It may fail due to malloc failure.
864 * 2. It may change skb pointers.
866 * It is pretty complicated. Luckily, it is called only in exceptional cases.
868 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
870 /* If skb has not enough free space at tail, get new one
871 * plus 128 bytes for future expansions. If we have enough
872 * room at tail, reallocate without expansion only if skb is cloned.
874 int i, k, eat = (skb->tail + delta) - skb->end;
876 if (eat > 0 || skb_cloned(skb)) {
877 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
878 GFP_ATOMIC))
879 return NULL;
882 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
883 BUG();
885 /* Optimization: no fragments, no reasons to preestimate
886 * size of pulled pages. Superb.
888 if (!skb_shinfo(skb)->frag_list)
889 goto pull_pages;
891 /* Estimate size of pulled pages. */
892 eat = delta;
893 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
894 if (skb_shinfo(skb)->frags[i].size >= eat)
895 goto pull_pages;
896 eat -= skb_shinfo(skb)->frags[i].size;
899 /* If we need update frag list, we are in troubles.
900 * Certainly, it possible to add an offset to skb data,
901 * but taking into account that pulling is expected to
902 * be very rare operation, it is worth to fight against
903 * further bloating skb head and crucify ourselves here instead.
904 * Pure masohism, indeed. 8)8)
906 if (eat) {
907 struct sk_buff *list = skb_shinfo(skb)->frag_list;
908 struct sk_buff *clone = NULL;
909 struct sk_buff *insp = NULL;
911 do {
912 BUG_ON(!list);
914 if (list->len <= eat) {
915 /* Eaten as whole. */
916 eat -= list->len;
917 list = list->next;
918 insp = list;
919 } else {
920 /* Eaten partially. */
922 if (skb_shared(list)) {
923 /* Sucks! We need to fork list. :-( */
924 clone = skb_clone(list, GFP_ATOMIC);
925 if (!clone)
926 return NULL;
927 insp = list->next;
928 list = clone;
929 } else {
930 /* This may be pulled without
931 * problems. */
932 insp = list;
934 if (!pskb_pull(list, eat)) {
935 if (clone)
936 kfree_skb(clone);
937 return NULL;
939 break;
941 } while (eat);
943 /* Free pulled out fragments. */
944 while ((list = skb_shinfo(skb)->frag_list) != insp) {
945 skb_shinfo(skb)->frag_list = list->next;
946 kfree_skb(list);
948 /* And insert new clone at head. */
949 if (clone) {
950 clone->next = list;
951 skb_shinfo(skb)->frag_list = clone;
954 /* Success! Now we may commit changes to skb data. */
956 pull_pages:
957 eat = delta;
958 k = 0;
959 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
960 if (skb_shinfo(skb)->frags[i].size <= eat) {
961 put_page(skb_shinfo(skb)->frags[i].page);
962 eat -= skb_shinfo(skb)->frags[i].size;
963 } else {
964 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
965 if (eat) {
966 skb_shinfo(skb)->frags[k].page_offset += eat;
967 skb_shinfo(skb)->frags[k].size -= eat;
968 eat = 0;
970 k++;
973 skb_shinfo(skb)->nr_frags = k;
975 skb->tail += delta;
976 skb->data_len -= delta;
978 return skb->tail;
981 /* Copy some data bits from skb to kernel buffer. */
983 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
985 int i, copy;
986 int start = skb_headlen(skb);
988 if (offset > (int)skb->len - len)
989 goto fault;
991 /* Copy header. */
992 if ((copy = start - offset) > 0) {
993 if (copy > len)
994 copy = len;
995 memcpy(to, skb->data + offset, copy);
996 if ((len -= copy) == 0)
997 return 0;
998 offset += copy;
999 to += copy;
1002 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1003 int end;
1005 BUG_TRAP(start <= offset + len);
1007 end = start + skb_shinfo(skb)->frags[i].size;
1008 if ((copy = end - offset) > 0) {
1009 u8 *vaddr;
1011 if (copy > len)
1012 copy = len;
1014 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1015 memcpy(to,
1016 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1017 offset - start, copy);
1018 kunmap_skb_frag(vaddr);
1020 if ((len -= copy) == 0)
1021 return 0;
1022 offset += copy;
1023 to += copy;
1025 start = end;
1028 if (skb_shinfo(skb)->frag_list) {
1029 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1031 for (; list; list = list->next) {
1032 int end;
1034 BUG_TRAP(start <= offset + len);
1036 end = start + list->len;
1037 if ((copy = end - offset) > 0) {
1038 if (copy > len)
1039 copy = len;
1040 if (skb_copy_bits(list, offset - start,
1041 to, copy))
1042 goto fault;
1043 if ((len -= copy) == 0)
1044 return 0;
1045 offset += copy;
1046 to += copy;
1048 start = end;
1051 if (!len)
1052 return 0;
1054 fault:
1055 return -EFAULT;
1059 * skb_store_bits - store bits from kernel buffer to skb
1060 * @skb: destination buffer
1061 * @offset: offset in destination
1062 * @from: source buffer
1063 * @len: number of bytes to copy
1065 * Copy the specified number of bytes from the source buffer to the
1066 * destination skb. This function handles all the messy bits of
1067 * traversing fragment lists and such.
1070 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1072 int i, copy;
1073 int start = skb_headlen(skb);
1075 if (offset > (int)skb->len - len)
1076 goto fault;
1078 if ((copy = start - offset) > 0) {
1079 if (copy > len)
1080 copy = len;
1081 memcpy(skb->data + offset, from, copy);
1082 if ((len -= copy) == 0)
1083 return 0;
1084 offset += copy;
1085 from += copy;
1088 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1089 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1090 int end;
1092 BUG_TRAP(start <= offset + len);
1094 end = start + frag->size;
1095 if ((copy = end - offset) > 0) {
1096 u8 *vaddr;
1098 if (copy > len)
1099 copy = len;
1101 vaddr = kmap_skb_frag(frag);
1102 memcpy(vaddr + frag->page_offset + offset - start,
1103 from, copy);
1104 kunmap_skb_frag(vaddr);
1106 if ((len -= copy) == 0)
1107 return 0;
1108 offset += copy;
1109 from += copy;
1111 start = end;
1114 if (skb_shinfo(skb)->frag_list) {
1115 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1117 for (; list; list = list->next) {
1118 int end;
1120 BUG_TRAP(start <= offset + len);
1122 end = start + list->len;
1123 if ((copy = end - offset) > 0) {
1124 if (copy > len)
1125 copy = len;
1126 if (skb_store_bits(list, offset - start,
1127 from, copy))
1128 goto fault;
1129 if ((len -= copy) == 0)
1130 return 0;
1131 offset += copy;
1132 from += copy;
1134 start = end;
1137 if (!len)
1138 return 0;
1140 fault:
1141 return -EFAULT;
1144 EXPORT_SYMBOL(skb_store_bits);
1146 /* Checksum skb data. */
1148 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1149 int len, unsigned int csum)
1151 int start = skb_headlen(skb);
1152 int i, copy = start - offset;
1153 int pos = 0;
1155 /* Checksum header. */
1156 if (copy > 0) {
1157 if (copy > len)
1158 copy = len;
1159 csum = csum_partial(skb->data + offset, copy, csum);
1160 if ((len -= copy) == 0)
1161 return csum;
1162 offset += copy;
1163 pos = copy;
1166 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 int end;
1169 BUG_TRAP(start <= offset + len);
1171 end = start + skb_shinfo(skb)->frags[i].size;
1172 if ((copy = end - offset) > 0) {
1173 unsigned int csum2;
1174 u8 *vaddr;
1175 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1177 if (copy > len)
1178 copy = len;
1179 vaddr = kmap_skb_frag(frag);
1180 csum2 = csum_partial(vaddr + frag->page_offset +
1181 offset - start, copy, 0);
1182 kunmap_skb_frag(vaddr);
1183 csum = csum_block_add(csum, csum2, pos);
1184 if (!(len -= copy))
1185 return csum;
1186 offset += copy;
1187 pos += copy;
1189 start = end;
1192 if (skb_shinfo(skb)->frag_list) {
1193 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1195 for (; list; list = list->next) {
1196 int end;
1198 BUG_TRAP(start <= offset + len);
1200 end = start + list->len;
1201 if ((copy = end - offset) > 0) {
1202 unsigned int csum2;
1203 if (copy > len)
1204 copy = len;
1205 csum2 = skb_checksum(list, offset - start,
1206 copy, 0);
1207 csum = csum_block_add(csum, csum2, pos);
1208 if ((len -= copy) == 0)
1209 return csum;
1210 offset += copy;
1211 pos += copy;
1213 start = end;
1216 BUG_ON(len);
1218 return csum;
1221 /* Both of above in one bottle. */
1223 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1224 u8 *to, int len, unsigned int csum)
1226 int start = skb_headlen(skb);
1227 int i, copy = start - offset;
1228 int pos = 0;
1230 /* Copy header. */
1231 if (copy > 0) {
1232 if (copy > len)
1233 copy = len;
1234 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1235 copy, csum);
1236 if ((len -= copy) == 0)
1237 return csum;
1238 offset += copy;
1239 to += copy;
1240 pos = copy;
1243 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1244 int end;
1246 BUG_TRAP(start <= offset + len);
1248 end = start + skb_shinfo(skb)->frags[i].size;
1249 if ((copy = end - offset) > 0) {
1250 unsigned int csum2;
1251 u8 *vaddr;
1252 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1254 if (copy > len)
1255 copy = len;
1256 vaddr = kmap_skb_frag(frag);
1257 csum2 = csum_partial_copy_nocheck(vaddr +
1258 frag->page_offset +
1259 offset - start, to,
1260 copy, 0);
1261 kunmap_skb_frag(vaddr);
1262 csum = csum_block_add(csum, csum2, pos);
1263 if (!(len -= copy))
1264 return csum;
1265 offset += copy;
1266 to += copy;
1267 pos += copy;
1269 start = end;
1272 if (skb_shinfo(skb)->frag_list) {
1273 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1275 for (; list; list = list->next) {
1276 unsigned int csum2;
1277 int end;
1279 BUG_TRAP(start <= offset + len);
1281 end = start + list->len;
1282 if ((copy = end - offset) > 0) {
1283 if (copy > len)
1284 copy = len;
1285 csum2 = skb_copy_and_csum_bits(list,
1286 offset - start,
1287 to, copy, 0);
1288 csum = csum_block_add(csum, csum2, pos);
1289 if ((len -= copy) == 0)
1290 return csum;
1291 offset += copy;
1292 to += copy;
1293 pos += copy;
1295 start = end;
1298 BUG_ON(len);
1299 return csum;
1302 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1304 unsigned int csum;
1305 long csstart;
1307 if (skb->ip_summed == CHECKSUM_HW)
1308 csstart = skb->h.raw - skb->data;
1309 else
1310 csstart = skb_headlen(skb);
1312 BUG_ON(csstart > skb_headlen(skb));
1314 memcpy(to, skb->data, csstart);
1316 csum = 0;
1317 if (csstart != skb->len)
1318 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1319 skb->len - csstart, 0);
1321 if (skb->ip_summed == CHECKSUM_HW) {
1322 long csstuff = csstart + skb->csum;
1324 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1329 * skb_dequeue - remove from the head of the queue
1330 * @list: list to dequeue from
1332 * Remove the head of the list. The list lock is taken so the function
1333 * may be used safely with other locking list functions. The head item is
1334 * returned or %NULL if the list is empty.
1337 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1339 unsigned long flags;
1340 struct sk_buff *result;
1342 spin_lock_irqsave(&list->lock, flags);
1343 result = __skb_dequeue(list);
1344 spin_unlock_irqrestore(&list->lock, flags);
1345 return result;
1349 * skb_dequeue_tail - remove from the tail of the queue
1350 * @list: list to dequeue from
1352 * Remove the tail of the list. The list lock is taken so the function
1353 * may be used safely with other locking list functions. The tail item is
1354 * returned or %NULL if the list is empty.
1356 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1358 unsigned long flags;
1359 struct sk_buff *result;
1361 spin_lock_irqsave(&list->lock, flags);
1362 result = __skb_dequeue_tail(list);
1363 spin_unlock_irqrestore(&list->lock, flags);
1364 return result;
1368 * skb_queue_purge - empty a list
1369 * @list: list to empty
1371 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1372 * the list and one reference dropped. This function takes the list
1373 * lock and is atomic with respect to other list locking functions.
1375 void skb_queue_purge(struct sk_buff_head *list)
1377 struct sk_buff *skb;
1378 while ((skb = skb_dequeue(list)) != NULL)
1379 kfree_skb(skb);
1383 * skb_queue_head - queue a buffer at the list head
1384 * @list: list to use
1385 * @newsk: buffer to queue
1387 * Queue a buffer at the start of the list. This function takes the
1388 * list lock and can be used safely with other locking &sk_buff functions
1389 * safely.
1391 * A buffer cannot be placed on two lists at the same time.
1393 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1395 unsigned long flags;
1397 spin_lock_irqsave(&list->lock, flags);
1398 __skb_queue_head(list, newsk);
1399 spin_unlock_irqrestore(&list->lock, flags);
1403 * skb_queue_tail - queue a buffer at the list tail
1404 * @list: list to use
1405 * @newsk: buffer to queue
1407 * Queue a buffer at the tail of the list. This function takes the
1408 * list lock and can be used safely with other locking &sk_buff functions
1409 * safely.
1411 * A buffer cannot be placed on two lists at the same time.
1413 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1415 unsigned long flags;
1417 spin_lock_irqsave(&list->lock, flags);
1418 __skb_queue_tail(list, newsk);
1419 spin_unlock_irqrestore(&list->lock, flags);
1423 * skb_unlink - remove a buffer from a list
1424 * @skb: buffer to remove
1425 * @list: list to use
1427 * Remove a packet from a list. The list locks are taken and this
1428 * function is atomic with respect to other list locked calls
1430 * You must know what list the SKB is on.
1432 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1434 unsigned long flags;
1436 spin_lock_irqsave(&list->lock, flags);
1437 __skb_unlink(skb, list);
1438 spin_unlock_irqrestore(&list->lock, flags);
1442 * skb_append - append a buffer
1443 * @old: buffer to insert after
1444 * @newsk: buffer to insert
1445 * @list: list to use
1447 * Place a packet after a given packet in a list. The list locks are taken
1448 * and this function is atomic with respect to other list locked calls.
1449 * A buffer cannot be placed on two lists at the same time.
1451 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1453 unsigned long flags;
1455 spin_lock_irqsave(&list->lock, flags);
1456 __skb_append(old, newsk, list);
1457 spin_unlock_irqrestore(&list->lock, flags);
1462 * skb_insert - insert a buffer
1463 * @old: buffer to insert before
1464 * @newsk: buffer to insert
1465 * @list: list to use
1467 * Place a packet before a given packet in a list. The list locks are
1468 * taken and this function is atomic with respect to other list locked
1469 * calls.
1471 * A buffer cannot be placed on two lists at the same time.
1473 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1475 unsigned long flags;
1477 spin_lock_irqsave(&list->lock, flags);
1478 __skb_insert(newsk, old->prev, old, list);
1479 spin_unlock_irqrestore(&list->lock, flags);
1482 #if 0
1484 * Tune the memory allocator for a new MTU size.
1486 void skb_add_mtu(int mtu)
1488 /* Must match allocation in alloc_skb */
1489 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1491 kmem_add_cache_size(mtu);
1493 #endif
1495 static inline void skb_split_inside_header(struct sk_buff *skb,
1496 struct sk_buff* skb1,
1497 const u32 len, const int pos)
1499 int i;
1501 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1503 /* And move data appendix as is. */
1504 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1505 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1507 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1508 skb_shinfo(skb)->nr_frags = 0;
1509 skb1->data_len = skb->data_len;
1510 skb1->len += skb1->data_len;
1511 skb->data_len = 0;
1512 skb->len = len;
1513 skb->tail = skb->data + len;
1516 static inline void skb_split_no_header(struct sk_buff *skb,
1517 struct sk_buff* skb1,
1518 const u32 len, int pos)
1520 int i, k = 0;
1521 const int nfrags = skb_shinfo(skb)->nr_frags;
1523 skb_shinfo(skb)->nr_frags = 0;
1524 skb1->len = skb1->data_len = skb->len - len;
1525 skb->len = len;
1526 skb->data_len = len - pos;
1528 for (i = 0; i < nfrags; i++) {
1529 int size = skb_shinfo(skb)->frags[i].size;
1531 if (pos + size > len) {
1532 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1534 if (pos < len) {
1535 /* Split frag.
1536 * We have two variants in this case:
1537 * 1. Move all the frag to the second
1538 * part, if it is possible. F.e.
1539 * this approach is mandatory for TUX,
1540 * where splitting is expensive.
1541 * 2. Split is accurately. We make this.
1543 get_page(skb_shinfo(skb)->frags[i].page);
1544 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1545 skb_shinfo(skb1)->frags[0].size -= len - pos;
1546 skb_shinfo(skb)->frags[i].size = len - pos;
1547 skb_shinfo(skb)->nr_frags++;
1549 k++;
1550 } else
1551 skb_shinfo(skb)->nr_frags++;
1552 pos += size;
1554 skb_shinfo(skb1)->nr_frags = k;
1558 * skb_split - Split fragmented skb to two parts at length len.
1559 * @skb: the buffer to split
1560 * @skb1: the buffer to receive the second part
1561 * @len: new length for skb
1563 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1565 int pos = skb_headlen(skb);
1567 if (len < pos) /* Split line is inside header. */
1568 skb_split_inside_header(skb, skb1, len, pos);
1569 else /* Second chunk has no header, nothing to copy. */
1570 skb_split_no_header(skb, skb1, len, pos);
1574 * skb_prepare_seq_read - Prepare a sequential read of skb data
1575 * @skb: the buffer to read
1576 * @from: lower offset of data to be read
1577 * @to: upper offset of data to be read
1578 * @st: state variable
1580 * Initializes the specified state variable. Must be called before
1581 * invoking skb_seq_read() for the first time.
1583 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1584 unsigned int to, struct skb_seq_state *st)
1586 st->lower_offset = from;
1587 st->upper_offset = to;
1588 st->root_skb = st->cur_skb = skb;
1589 st->frag_idx = st->stepped_offset = 0;
1590 st->frag_data = NULL;
1594 * skb_seq_read - Sequentially read skb data
1595 * @consumed: number of bytes consumed by the caller so far
1596 * @data: destination pointer for data to be returned
1597 * @st: state variable
1599 * Reads a block of skb data at &consumed relative to the
1600 * lower offset specified to skb_prepare_seq_read(). Assigns
1601 * the head of the data block to &data and returns the length
1602 * of the block or 0 if the end of the skb data or the upper
1603 * offset has been reached.
1605 * The caller is not required to consume all of the data
1606 * returned, i.e. &consumed is typically set to the number
1607 * of bytes already consumed and the next call to
1608 * skb_seq_read() will return the remaining part of the block.
1610 * Note: The size of each block of data returned can be arbitary,
1611 * this limitation is the cost for zerocopy seqeuental
1612 * reads of potentially non linear data.
1614 * Note: Fragment lists within fragments are not implemented
1615 * at the moment, state->root_skb could be replaced with
1616 * a stack for this purpose.
1618 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1619 struct skb_seq_state *st)
1621 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1622 skb_frag_t *frag;
1624 if (unlikely(abs_offset >= st->upper_offset))
1625 return 0;
1627 next_skb:
1628 block_limit = skb_headlen(st->cur_skb);
1630 if (abs_offset < block_limit) {
1631 *data = st->cur_skb->data + abs_offset;
1632 return block_limit - abs_offset;
1635 if (st->frag_idx == 0 && !st->frag_data)
1636 st->stepped_offset += skb_headlen(st->cur_skb);
1638 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1639 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1640 block_limit = frag->size + st->stepped_offset;
1642 if (abs_offset < block_limit) {
1643 if (!st->frag_data)
1644 st->frag_data = kmap_skb_frag(frag);
1646 *data = (u8 *) st->frag_data + frag->page_offset +
1647 (abs_offset - st->stepped_offset);
1649 return block_limit - abs_offset;
1652 if (st->frag_data) {
1653 kunmap_skb_frag(st->frag_data);
1654 st->frag_data = NULL;
1657 st->frag_idx++;
1658 st->stepped_offset += frag->size;
1661 if (st->cur_skb->next) {
1662 st->cur_skb = st->cur_skb->next;
1663 st->frag_idx = 0;
1664 goto next_skb;
1665 } else if (st->root_skb == st->cur_skb &&
1666 skb_shinfo(st->root_skb)->frag_list) {
1667 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1668 goto next_skb;
1671 return 0;
1675 * skb_abort_seq_read - Abort a sequential read of skb data
1676 * @st: state variable
1678 * Must be called if skb_seq_read() was not called until it
1679 * returned 0.
1681 void skb_abort_seq_read(struct skb_seq_state *st)
1683 if (st->frag_data)
1684 kunmap_skb_frag(st->frag_data);
1687 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1689 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1690 struct ts_config *conf,
1691 struct ts_state *state)
1693 return skb_seq_read(offset, text, TS_SKB_CB(state));
1696 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1698 skb_abort_seq_read(TS_SKB_CB(state));
1702 * skb_find_text - Find a text pattern in skb data
1703 * @skb: the buffer to look in
1704 * @from: search offset
1705 * @to: search limit
1706 * @config: textsearch configuration
1707 * @state: uninitialized textsearch state variable
1709 * Finds a pattern in the skb data according to the specified
1710 * textsearch configuration. Use textsearch_next() to retrieve
1711 * subsequent occurrences of the pattern. Returns the offset
1712 * to the first occurrence or UINT_MAX if no match was found.
1714 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1715 unsigned int to, struct ts_config *config,
1716 struct ts_state *state)
1718 config->get_next_block = skb_ts_get_next_block;
1719 config->finish = skb_ts_finish;
1721 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1723 return textsearch_find(config, state);
1727 * skb_append_datato_frags: - append the user data to a skb
1728 * @sk: sock structure
1729 * @skb: skb structure to be appened with user data.
1730 * @getfrag: call back function to be used for getting the user data
1731 * @from: pointer to user message iov
1732 * @length: length of the iov message
1734 * Description: This procedure append the user data in the fragment part
1735 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1737 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1738 int (*getfrag)(void *from, char *to, int offset,
1739 int len, int odd, struct sk_buff *skb),
1740 void *from, int length)
1742 int frg_cnt = 0;
1743 skb_frag_t *frag = NULL;
1744 struct page *page = NULL;
1745 int copy, left;
1746 int offset = 0;
1747 int ret;
1749 do {
1750 /* Return error if we don't have space for new frag */
1751 frg_cnt = skb_shinfo(skb)->nr_frags;
1752 if (frg_cnt >= MAX_SKB_FRAGS)
1753 return -EFAULT;
1755 /* allocate a new page for next frag */
1756 page = alloc_pages(sk->sk_allocation, 0);
1758 /* If alloc_page fails just return failure and caller will
1759 * free previous allocated pages by doing kfree_skb()
1761 if (page == NULL)
1762 return -ENOMEM;
1764 /* initialize the next frag */
1765 sk->sk_sndmsg_page = page;
1766 sk->sk_sndmsg_off = 0;
1767 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1768 skb->truesize += PAGE_SIZE;
1769 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1771 /* get the new initialized frag */
1772 frg_cnt = skb_shinfo(skb)->nr_frags;
1773 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1775 /* copy the user data to page */
1776 left = PAGE_SIZE - frag->page_offset;
1777 copy = (length > left)? left : length;
1779 ret = getfrag(from, (page_address(frag->page) +
1780 frag->page_offset + frag->size),
1781 offset, copy, 0, skb);
1782 if (ret < 0)
1783 return -EFAULT;
1785 /* copy was successful so update the size parameters */
1786 sk->sk_sndmsg_off += copy;
1787 frag->size += copy;
1788 skb->len += copy;
1789 skb->data_len += copy;
1790 offset += copy;
1791 length -= copy;
1793 } while (length > 0);
1795 return 0;
1799 * skb_pull_rcsum - pull skb and update receive checksum
1800 * @skb: buffer to update
1801 * @start: start of data before pull
1802 * @len: length of data pulled
1804 * This function performs an skb_pull on the packet and updates
1805 * update the CHECKSUM_HW checksum. It should be used on receive
1806 * path processing instead of skb_pull unless you know that the
1807 * checksum difference is zero (e.g., a valid IP header) or you
1808 * are setting ip_summed to CHECKSUM_NONE.
1810 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1812 BUG_ON(len > skb->len);
1813 skb->len -= len;
1814 BUG_ON(skb->len < skb->data_len);
1815 skb_postpull_rcsum(skb, skb->data, len);
1816 return skb->data += len;
1819 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1821 void __init skb_init(void)
1823 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1824 sizeof(struct sk_buff),
1826 SLAB_HWCACHE_ALIGN,
1827 NULL, NULL);
1828 if (!skbuff_head_cache)
1829 panic("cannot create skbuff cache");
1831 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1832 (2*sizeof(struct sk_buff)) +
1833 sizeof(atomic_t),
1835 SLAB_HWCACHE_ALIGN,
1836 NULL, NULL);
1837 if (!skbuff_fclone_cache)
1838 panic("cannot create skbuff cache");
1841 EXPORT_SYMBOL(___pskb_trim);
1842 EXPORT_SYMBOL(__kfree_skb);
1843 EXPORT_SYMBOL(kfree_skb);
1844 EXPORT_SYMBOL(__pskb_pull_tail);
1845 EXPORT_SYMBOL(__alloc_skb);
1846 EXPORT_SYMBOL(pskb_copy);
1847 EXPORT_SYMBOL(pskb_expand_head);
1848 EXPORT_SYMBOL(skb_checksum);
1849 EXPORT_SYMBOL(skb_clone);
1850 EXPORT_SYMBOL(skb_clone_fraglist);
1851 EXPORT_SYMBOL(skb_copy);
1852 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1853 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1854 EXPORT_SYMBOL(skb_copy_bits);
1855 EXPORT_SYMBOL(skb_copy_expand);
1856 EXPORT_SYMBOL(skb_over_panic);
1857 EXPORT_SYMBOL(skb_pad);
1858 EXPORT_SYMBOL(skb_realloc_headroom);
1859 EXPORT_SYMBOL(skb_under_panic);
1860 EXPORT_SYMBOL(skb_dequeue);
1861 EXPORT_SYMBOL(skb_dequeue_tail);
1862 EXPORT_SYMBOL(skb_insert);
1863 EXPORT_SYMBOL(skb_queue_purge);
1864 EXPORT_SYMBOL(skb_queue_head);
1865 EXPORT_SYMBOL(skb_queue_tail);
1866 EXPORT_SYMBOL(skb_unlink);
1867 EXPORT_SYMBOL(skb_append);
1868 EXPORT_SYMBOL(skb_split);
1869 EXPORT_SYMBOL(skb_prepare_seq_read);
1870 EXPORT_SYMBOL(skb_seq_read);
1871 EXPORT_SYMBOL(skb_abort_seq_read);
1872 EXPORT_SYMBOL(skb_find_text);
1873 EXPORT_SYMBOL(skb_append_datato_frags);