ia64/xen-unstable

view linux-2.6-xen-sparse/net/core/skbuff.c @ 9092:849723752858

Silence the messages that are emitted when removing nodes that already have
been removed. This is fine, in a cleanup script.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Mar 02 02:01:17 2006 +0100 (2006-03-02)
parents 3b74edc512b4
children 4ad317429111
line source
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
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
24 *
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).
30 *
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.
35 */
37 /*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
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;
74 /*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
80 /**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89 {
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();
95 }
97 /**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
106 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107 {
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();
113 }
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.
118 *
119 */
121 /**
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
127 *
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.
131 *
132 * Buffers may only be allocated from interrupts using a @gfp_mask of
133 * %GFP_ATOMIC.
134 */
135 #ifndef CONFIG_HAVE_ARCH_ALLOC_SKB
136 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
137 int fclone)
138 {
139 kmem_cache_t *cache;
140 struct skb_shared_info *shinfo;
141 struct sk_buff *skb;
142 u8 *data;
144 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
146 /* Get the HEAD */
147 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
148 if (!skb)
149 goto out;
151 /* Get the DATA. Size must match skb_add_mtu(). */
152 size = SKB_DATA_ALIGN(size);
153 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
154 if (!data)
155 goto nodata;
157 memset(skb, 0, offsetof(struct sk_buff, truesize));
158 skb->truesize = size + sizeof(struct sk_buff);
159 atomic_set(&skb->users, 1);
160 skb->head = data;
161 skb->data = data;
162 skb->tail = data;
163 skb->end = data + size;
164 /* make sure we initialize shinfo sequentially */
165 shinfo = skb_shinfo(skb);
166 atomic_set(&shinfo->dataref, 1);
167 shinfo->nr_frags = 0;
168 shinfo->tso_size = 0;
169 shinfo->tso_segs = 0;
170 shinfo->ufo_size = 0;
171 shinfo->ip6_frag_id = 0;
172 shinfo->frag_list = NULL;
174 if (fclone) {
175 struct sk_buff *child = skb + 1;
176 atomic_t *fclone_ref = (atomic_t *) (child + 1);
178 skb->fclone = SKB_FCLONE_ORIG;
179 atomic_set(fclone_ref, 1);
181 child->fclone = SKB_FCLONE_UNAVAILABLE;
182 }
183 out:
184 return skb;
185 nodata:
186 kmem_cache_free(cache, skb);
187 skb = NULL;
188 goto out;
189 }
190 #endif /* !CONFIG_HAVE_ARCH_ALLOC_SKB */
192 /**
193 * alloc_skb_from_cache - allocate a network buffer
194 * @cp: kmem_cache from which to allocate the data area
195 * (object size must be big enough for @size bytes + skb overheads)
196 * @size: size to allocate
197 * @gfp_mask: allocation mask
198 *
199 * Allocate a new &sk_buff. The returned buffer has no headroom and
200 * tail room of size bytes. The object has a reference count of one.
201 * The return is the buffer. On a failure the return is %NULL.
202 *
203 * Buffers may only be allocated from interrupts using a @gfp_mask of
204 * %GFP_ATOMIC.
205 */
206 struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
207 unsigned int size,
208 gfp_t gfp_mask,
209 int fclone)
210 {
211 kmem_cache_t *cache;
212 struct skb_shared_info *shinfo;
213 struct sk_buff *skb;
214 u8 *data;
216 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
218 /* Get the HEAD */
219 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
220 if (!skb)
221 goto out;
223 /* Get the DATA. */
224 size = SKB_DATA_ALIGN(size);
225 data = kmem_cache_alloc(cp, gfp_mask);
226 if (!data)
227 goto nodata;
229 memset(skb, 0, offsetof(struct sk_buff, truesize));
230 skb->truesize = size + sizeof(struct sk_buff);
231 atomic_set(&skb->users, 1);
232 skb->head = data;
233 skb->data = data;
234 skb->tail = data;
235 skb->end = data + size;
236 /* make sure we initialize shinfo sequentially */
237 shinfo = skb_shinfo(skb);
238 atomic_set(&shinfo->dataref, 1);
239 shinfo->nr_frags = 0;
240 shinfo->tso_size = 0;
241 shinfo->tso_segs = 0;
242 shinfo->ufo_size = 0;
243 shinfo->ip6_frag_id = 0;
244 shinfo->frag_list = NULL;
246 if (fclone) {
247 struct sk_buff *child = skb + 1;
248 atomic_t *fclone_ref = (atomic_t *) (child + 1);
250 skb->fclone = SKB_FCLONE_ORIG;
251 atomic_set(fclone_ref, 1);
253 child->fclone = SKB_FCLONE_UNAVAILABLE;
254 }
255 out:
256 return skb;
257 nodata:
258 kmem_cache_free(cache, skb);
259 skb = NULL;
260 goto out;
261 }
264 static void skb_drop_fraglist(struct sk_buff *skb)
265 {
266 struct sk_buff *list = skb_shinfo(skb)->frag_list;
268 skb_shinfo(skb)->frag_list = NULL;
270 do {
271 struct sk_buff *this = list;
272 list = list->next;
273 kfree_skb(this);
274 } while (list);
275 }
277 static void skb_clone_fraglist(struct sk_buff *skb)
278 {
279 struct sk_buff *list;
281 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
282 skb_get(list);
283 }
285 void skb_release_data(struct sk_buff *skb)
286 {
287 if (!skb->cloned ||
288 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
289 &skb_shinfo(skb)->dataref)) {
290 if (skb_shinfo(skb)->nr_frags) {
291 int i;
292 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
293 put_page(skb_shinfo(skb)->frags[i].page);
294 }
296 if (skb_shinfo(skb)->frag_list)
297 skb_drop_fraglist(skb);
299 kfree(skb->head);
300 }
301 }
303 /*
304 * Free an skbuff by memory without cleaning the state.
305 */
306 void kfree_skbmem(struct sk_buff *skb)
307 {
308 struct sk_buff *other;
309 atomic_t *fclone_ref;
311 skb_release_data(skb);
312 switch (skb->fclone) {
313 case SKB_FCLONE_UNAVAILABLE:
314 kmem_cache_free(skbuff_head_cache, skb);
315 break;
317 case SKB_FCLONE_ORIG:
318 fclone_ref = (atomic_t *) (skb + 2);
319 if (atomic_dec_and_test(fclone_ref))
320 kmem_cache_free(skbuff_fclone_cache, skb);
321 break;
323 case SKB_FCLONE_CLONE:
324 fclone_ref = (atomic_t *) (skb + 1);
325 other = skb - 1;
327 /* The clone portion is available for
328 * fast-cloning again.
329 */
330 skb->fclone = SKB_FCLONE_UNAVAILABLE;
332 if (atomic_dec_and_test(fclone_ref))
333 kmem_cache_free(skbuff_fclone_cache, other);
334 break;
335 };
336 }
338 /**
339 * __kfree_skb - private function
340 * @skb: buffer
341 *
342 * Free an sk_buff. Release anything attached to the buffer.
343 * Clean the state. This is an internal helper function. Users should
344 * always call kfree_skb
345 */
347 void __kfree_skb(struct sk_buff *skb)
348 {
349 dst_release(skb->dst);
350 #ifdef CONFIG_XFRM
351 secpath_put(skb->sp);
352 #endif
353 if (skb->destructor) {
354 WARN_ON(in_irq());
355 skb->destructor(skb);
356 }
357 #ifdef CONFIG_NETFILTER
358 nf_conntrack_put(skb->nfct);
359 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
360 nf_conntrack_put_reasm(skb->nfct_reasm);
361 #endif
362 #ifdef CONFIG_BRIDGE_NETFILTER
363 nf_bridge_put(skb->nf_bridge);
364 #endif
365 #endif
366 /* XXX: IS this still necessary? - JHS */
367 #ifdef CONFIG_NET_SCHED
368 skb->tc_index = 0;
369 #ifdef CONFIG_NET_CLS_ACT
370 skb->tc_verd = 0;
371 #endif
372 #endif
374 kfree_skbmem(skb);
375 }
377 /**
378 * skb_clone - duplicate an sk_buff
379 * @skb: buffer to clone
380 * @gfp_mask: allocation priority
381 *
382 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
383 * copies share the same packet data but not structure. The new
384 * buffer has a reference count of 1. If the allocation fails the
385 * function returns %NULL otherwise the new buffer is returned.
386 *
387 * If this function is called from an interrupt gfp_mask() must be
388 * %GFP_ATOMIC.
389 */
391 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
392 {
393 struct sk_buff *n;
395 n = skb + 1;
396 if (skb->fclone == SKB_FCLONE_ORIG &&
397 n->fclone == SKB_FCLONE_UNAVAILABLE) {
398 atomic_t *fclone_ref = (atomic_t *) (n + 1);
399 n->fclone = SKB_FCLONE_CLONE;
400 atomic_inc(fclone_ref);
401 } else {
402 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
403 if (!n)
404 return NULL;
405 n->fclone = SKB_FCLONE_UNAVAILABLE;
406 }
408 #define C(x) n->x = skb->x
410 n->next = n->prev = NULL;
411 n->sk = NULL;
412 C(tstamp);
413 C(dev);
414 C(h);
415 C(nh);
416 C(mac);
417 C(dst);
418 dst_clone(skb->dst);
419 C(sp);
420 #ifdef CONFIG_INET
421 secpath_get(skb->sp);
422 #endif
423 memcpy(n->cb, skb->cb, sizeof(skb->cb));
424 C(len);
425 C(data_len);
426 C(csum);
427 C(local_df);
428 n->cloned = 1;
429 n->nohdr = 0;
430 #ifdef CONFIG_XEN
431 C(proto_csum_valid);
432 C(proto_csum_blank);
433 #endif
434 C(pkt_type);
435 C(ip_summed);
436 C(priority);
437 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
438 C(ipvs_property);
439 #endif
440 C(protocol);
441 n->destructor = NULL;
442 #ifdef CONFIG_NETFILTER
443 C(nfmark);
444 C(nfct);
445 nf_conntrack_get(skb->nfct);
446 C(nfctinfo);
447 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
448 C(nfct_reasm);
449 nf_conntrack_get_reasm(skb->nfct_reasm);
450 #endif
451 #ifdef CONFIG_BRIDGE_NETFILTER
452 C(nf_bridge);
453 nf_bridge_get(skb->nf_bridge);
454 #endif
455 #endif /*CONFIG_NETFILTER*/
456 #ifdef CONFIG_NET_SCHED
457 C(tc_index);
458 #ifdef CONFIG_NET_CLS_ACT
459 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
460 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
461 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
462 C(input_dev);
463 #endif
465 #endif
466 C(truesize);
467 atomic_set(&n->users, 1);
468 C(head);
469 C(data);
470 C(tail);
471 C(end);
473 atomic_inc(&(skb_shinfo(skb)->dataref));
474 skb->cloned = 1;
476 return n;
477 }
479 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
480 {
481 /*
482 * Shift between the two data areas in bytes
483 */
484 unsigned long offset = new->data - old->data;
486 new->sk = NULL;
487 new->dev = old->dev;
488 new->priority = old->priority;
489 new->protocol = old->protocol;
490 new->dst = dst_clone(old->dst);
491 #ifdef CONFIG_INET
492 new->sp = secpath_get(old->sp);
493 #endif
494 new->h.raw = old->h.raw + offset;
495 new->nh.raw = old->nh.raw + offset;
496 new->mac.raw = old->mac.raw + offset;
497 memcpy(new->cb, old->cb, sizeof(old->cb));
498 new->local_df = old->local_df;
499 new->fclone = SKB_FCLONE_UNAVAILABLE;
500 new->pkt_type = old->pkt_type;
501 new->tstamp = old->tstamp;
502 new->destructor = NULL;
503 #ifdef CONFIG_NETFILTER
504 new->nfmark = old->nfmark;
505 new->nfct = old->nfct;
506 nf_conntrack_get(old->nfct);
507 new->nfctinfo = old->nfctinfo;
508 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
509 new->nfct_reasm = old->nfct_reasm;
510 nf_conntrack_get_reasm(old->nfct_reasm);
511 #endif
512 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
513 new->ipvs_property = old->ipvs_property;
514 #endif
515 #ifdef CONFIG_BRIDGE_NETFILTER
516 new->nf_bridge = old->nf_bridge;
517 nf_bridge_get(old->nf_bridge);
518 #endif
519 #endif
520 #ifdef CONFIG_NET_SCHED
521 #ifdef CONFIG_NET_CLS_ACT
522 new->tc_verd = old->tc_verd;
523 #endif
524 new->tc_index = old->tc_index;
525 #endif
526 atomic_set(&new->users, 1);
527 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
528 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
529 }
531 /**
532 * skb_copy - create private copy of an sk_buff
533 * @skb: buffer to copy
534 * @gfp_mask: allocation priority
535 *
536 * Make a copy of both an &sk_buff and its data. This is used when the
537 * caller wishes to modify the data and needs a private copy of the
538 * data to alter. Returns %NULL on failure or the pointer to the buffer
539 * on success. The returned buffer has a reference count of 1.
540 *
541 * As by-product this function converts non-linear &sk_buff to linear
542 * one, so that &sk_buff becomes completely private and caller is allowed
543 * to modify all the data of returned buffer. This means that this
544 * function is not recommended for use in circumstances when only
545 * header is going to be modified. Use pskb_copy() instead.
546 */
548 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
549 {
550 int headerlen = skb->data - skb->head;
551 /*
552 * Allocate the copy buffer
553 */
554 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
555 gfp_mask);
556 if (!n)
557 return NULL;
559 /* Set the data pointer */
560 skb_reserve(n, headerlen);
561 /* Set the tail pointer and length */
562 skb_put(n, skb->len);
563 n->csum = skb->csum;
564 n->ip_summed = skb->ip_summed;
566 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
567 BUG();
569 copy_skb_header(n, skb);
570 return n;
571 }
574 /**
575 * pskb_copy - create copy of an sk_buff with private head.
576 * @skb: buffer to copy
577 * @gfp_mask: allocation priority
578 *
579 * Make a copy of both an &sk_buff and part of its data, located
580 * in header. Fragmented data remain shared. This is used when
581 * the caller wishes to modify only header of &sk_buff and needs
582 * private copy of the header to alter. Returns %NULL on failure
583 * or the pointer to the buffer on success.
584 * The returned buffer has a reference count of 1.
585 */
587 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
588 {
589 /*
590 * Allocate the copy buffer
591 */
592 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
594 if (!n)
595 goto out;
597 /* Set the data pointer */
598 skb_reserve(n, skb->data - skb->head);
599 /* Set the tail pointer and length */
600 skb_put(n, skb_headlen(skb));
601 /* Copy the bytes */
602 memcpy(n->data, skb->data, n->len);
603 n->csum = skb->csum;
604 n->ip_summed = skb->ip_summed;
606 n->data_len = skb->data_len;
607 n->len = skb->len;
609 if (skb_shinfo(skb)->nr_frags) {
610 int i;
612 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
613 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
614 get_page(skb_shinfo(n)->frags[i].page);
615 }
616 skb_shinfo(n)->nr_frags = i;
617 }
619 if (skb_shinfo(skb)->frag_list) {
620 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
621 skb_clone_fraglist(n);
622 }
624 copy_skb_header(n, skb);
625 out:
626 return n;
627 }
629 /**
630 * pskb_expand_head - reallocate header of &sk_buff
631 * @skb: buffer to reallocate
632 * @nhead: room to add at head
633 * @ntail: room to add at tail
634 * @gfp_mask: allocation priority
635 *
636 * Expands (or creates identical copy, if &nhead and &ntail are zero)
637 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
638 * reference count of 1. Returns zero in the case of success or error,
639 * if expansion failed. In the last case, &sk_buff is not changed.
640 *
641 * All the pointers pointing into skb header may change and must be
642 * reloaded after call to this function.
643 */
645 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
646 gfp_t gfp_mask)
647 {
648 int i;
649 u8 *data;
650 int size = nhead + (skb->end - skb->head) + ntail;
651 long off;
653 if (skb_shared(skb))
654 BUG();
656 size = SKB_DATA_ALIGN(size);
658 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
659 if (!data)
660 goto nodata;
662 /* Copy only real data... and, alas, header. This should be
663 * optimized for the cases when header is void. */
664 memcpy(data + nhead, skb->head, skb->tail - skb->head);
665 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
667 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
668 get_page(skb_shinfo(skb)->frags[i].page);
670 if (skb_shinfo(skb)->frag_list)
671 skb_clone_fraglist(skb);
673 skb_release_data(skb);
675 off = (data + nhead) - skb->head;
677 skb->head = data;
678 skb->end = data + size;
679 skb->data += off;
680 skb->tail += off;
681 skb->mac.raw += off;
682 skb->h.raw += off;
683 skb->nh.raw += off;
684 skb->cloned = 0;
685 skb->nohdr = 0;
686 atomic_set(&skb_shinfo(skb)->dataref, 1);
687 return 0;
689 nodata:
690 return -ENOMEM;
691 }
693 /* Make private copy of skb with writable head and some headroom */
695 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
696 {
697 struct sk_buff *skb2;
698 int delta = headroom - skb_headroom(skb);
700 if (delta <= 0)
701 skb2 = pskb_copy(skb, GFP_ATOMIC);
702 else {
703 skb2 = skb_clone(skb, GFP_ATOMIC);
704 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
705 GFP_ATOMIC)) {
706 kfree_skb(skb2);
707 skb2 = NULL;
708 }
709 }
710 return skb2;
711 }
714 /**
715 * skb_copy_expand - copy and expand sk_buff
716 * @skb: buffer to copy
717 * @newheadroom: new free bytes at head
718 * @newtailroom: new free bytes at tail
719 * @gfp_mask: allocation priority
720 *
721 * Make a copy of both an &sk_buff and its data and while doing so
722 * allocate additional space.
723 *
724 * This is used when the caller wishes to modify the data and needs a
725 * private copy of the data to alter as well as more space for new fields.
726 * Returns %NULL on failure or the pointer to the buffer
727 * on success. The returned buffer has a reference count of 1.
728 *
729 * You must pass %GFP_ATOMIC as the allocation priority if this function
730 * is called from an interrupt.
731 *
732 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
733 * only by netfilter in the cases when checksum is recalculated? --ANK
734 */
735 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
736 int newheadroom, int newtailroom,
737 gfp_t gfp_mask)
738 {
739 /*
740 * Allocate the copy buffer
741 */
742 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
743 gfp_mask);
744 int head_copy_len, head_copy_off;
746 if (!n)
747 return NULL;
749 skb_reserve(n, newheadroom);
751 /* Set the tail pointer and length */
752 skb_put(n, skb->len);
754 head_copy_len = skb_headroom(skb);
755 head_copy_off = 0;
756 if (newheadroom <= head_copy_len)
757 head_copy_len = newheadroom;
758 else
759 head_copy_off = newheadroom - head_copy_len;
761 /* Copy the linear header and data. */
762 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
763 skb->len + head_copy_len))
764 BUG();
766 copy_skb_header(n, skb);
768 return n;
769 }
771 /**
772 * skb_pad - zero pad the tail of an skb
773 * @skb: buffer to pad
774 * @pad: space to pad
775 *
776 * Ensure that a buffer is followed by a padding area that is zero
777 * filled. Used by network drivers which may DMA or transfer data
778 * beyond the buffer end onto the wire.
779 *
780 * May return NULL in out of memory cases.
781 */
783 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
784 {
785 struct sk_buff *nskb;
787 /* If the skbuff is non linear tailroom is always zero.. */
788 if (skb_tailroom(skb) >= pad) {
789 memset(skb->data+skb->len, 0, pad);
790 return skb;
791 }
793 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
794 kfree_skb(skb);
795 if (nskb)
796 memset(nskb->data+nskb->len, 0, pad);
797 return nskb;
798 }
800 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
801 * If realloc==0 and trimming is impossible without change of data,
802 * it is BUG().
803 */
805 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
806 {
807 int offset = skb_headlen(skb);
808 int nfrags = skb_shinfo(skb)->nr_frags;
809 int i;
811 for (i = 0; i < nfrags; i++) {
812 int end = offset + skb_shinfo(skb)->frags[i].size;
813 if (end > len) {
814 if (skb_cloned(skb)) {
815 BUG_ON(!realloc);
816 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
817 return -ENOMEM;
818 }
819 if (len <= offset) {
820 put_page(skb_shinfo(skb)->frags[i].page);
821 skb_shinfo(skb)->nr_frags--;
822 } else {
823 skb_shinfo(skb)->frags[i].size = len - offset;
824 }
825 }
826 offset = end;
827 }
829 if (offset < len) {
830 skb->data_len -= skb->len - len;
831 skb->len = len;
832 } else {
833 if (len <= skb_headlen(skb)) {
834 skb->len = len;
835 skb->data_len = 0;
836 skb->tail = skb->data + len;
837 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
838 skb_drop_fraglist(skb);
839 } else {
840 skb->data_len -= skb->len - len;
841 skb->len = len;
842 }
843 }
845 return 0;
846 }
848 /**
849 * __pskb_pull_tail - advance tail of skb header
850 * @skb: buffer to reallocate
851 * @delta: number of bytes to advance tail
852 *
853 * The function makes a sense only on a fragmented &sk_buff,
854 * it expands header moving its tail forward and copying necessary
855 * data from fragmented part.
856 *
857 * &sk_buff MUST have reference count of 1.
858 *
859 * Returns %NULL (and &sk_buff does not change) if pull failed
860 * or value of new tail of skb in the case of success.
861 *
862 * All the pointers pointing into skb header may change and must be
863 * reloaded after call to this function.
864 */
866 /* Moves tail of skb head forward, copying data from fragmented part,
867 * when it is necessary.
868 * 1. It may fail due to malloc failure.
869 * 2. It may change skb pointers.
870 *
871 * It is pretty complicated. Luckily, it is called only in exceptional cases.
872 */
873 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
874 {
875 /* If skb has not enough free space at tail, get new one
876 * plus 128 bytes for future expansions. If we have enough
877 * room at tail, reallocate without expansion only if skb is cloned.
878 */
879 int i, k, eat = (skb->tail + delta) - skb->end;
881 if (eat > 0 || skb_cloned(skb)) {
882 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
883 GFP_ATOMIC))
884 return NULL;
885 }
887 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
888 BUG();
890 /* Optimization: no fragments, no reasons to preestimate
891 * size of pulled pages. Superb.
892 */
893 if (!skb_shinfo(skb)->frag_list)
894 goto pull_pages;
896 /* Estimate size of pulled pages. */
897 eat = delta;
898 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
899 if (skb_shinfo(skb)->frags[i].size >= eat)
900 goto pull_pages;
901 eat -= skb_shinfo(skb)->frags[i].size;
902 }
904 /* If we need update frag list, we are in troubles.
905 * Certainly, it possible to add an offset to skb data,
906 * but taking into account that pulling is expected to
907 * be very rare operation, it is worth to fight against
908 * further bloating skb head and crucify ourselves here instead.
909 * Pure masohism, indeed. 8)8)
910 */
911 if (eat) {
912 struct sk_buff *list = skb_shinfo(skb)->frag_list;
913 struct sk_buff *clone = NULL;
914 struct sk_buff *insp = NULL;
916 do {
917 BUG_ON(!list);
919 if (list->len <= eat) {
920 /* Eaten as whole. */
921 eat -= list->len;
922 list = list->next;
923 insp = list;
924 } else {
925 /* Eaten partially. */
927 if (skb_shared(list)) {
928 /* Sucks! We need to fork list. :-( */
929 clone = skb_clone(list, GFP_ATOMIC);
930 if (!clone)
931 return NULL;
932 insp = list->next;
933 list = clone;
934 } else {
935 /* This may be pulled without
936 * problems. */
937 insp = list;
938 }
939 if (!pskb_pull(list, eat)) {
940 if (clone)
941 kfree_skb(clone);
942 return NULL;
943 }
944 break;
945 }
946 } while (eat);
948 /* Free pulled out fragments. */
949 while ((list = skb_shinfo(skb)->frag_list) != insp) {
950 skb_shinfo(skb)->frag_list = list->next;
951 kfree_skb(list);
952 }
953 /* And insert new clone at head. */
954 if (clone) {
955 clone->next = list;
956 skb_shinfo(skb)->frag_list = clone;
957 }
958 }
959 /* Success! Now we may commit changes to skb data. */
961 pull_pages:
962 eat = delta;
963 k = 0;
964 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
965 if (skb_shinfo(skb)->frags[i].size <= eat) {
966 put_page(skb_shinfo(skb)->frags[i].page);
967 eat -= skb_shinfo(skb)->frags[i].size;
968 } else {
969 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
970 if (eat) {
971 skb_shinfo(skb)->frags[k].page_offset += eat;
972 skb_shinfo(skb)->frags[k].size -= eat;
973 eat = 0;
974 }
975 k++;
976 }
977 }
978 skb_shinfo(skb)->nr_frags = k;
980 skb->tail += delta;
981 skb->data_len -= delta;
983 return skb->tail;
984 }
986 /* Copy some data bits from skb to kernel buffer. */
988 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
989 {
990 int i, copy;
991 int start = skb_headlen(skb);
993 if (offset > (int)skb->len - len)
994 goto fault;
996 /* Copy header. */
997 if ((copy = start - offset) > 0) {
998 if (copy > len)
999 copy = len;
1000 memcpy(to, skb->data + offset, copy);
1001 if ((len -= copy) == 0)
1002 return 0;
1003 offset += copy;
1004 to += copy;
1007 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1008 int end;
1010 BUG_TRAP(start <= offset + len);
1012 end = start + skb_shinfo(skb)->frags[i].size;
1013 if ((copy = end - offset) > 0) {
1014 u8 *vaddr;
1016 if (copy > len)
1017 copy = len;
1019 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1020 memcpy(to,
1021 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1022 offset - start, copy);
1023 kunmap_skb_frag(vaddr);
1025 if ((len -= copy) == 0)
1026 return 0;
1027 offset += copy;
1028 to += copy;
1030 start = end;
1033 if (skb_shinfo(skb)->frag_list) {
1034 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1036 for (; list; list = list->next) {
1037 int end;
1039 BUG_TRAP(start <= offset + len);
1041 end = start + list->len;
1042 if ((copy = end - offset) > 0) {
1043 if (copy > len)
1044 copy = len;
1045 if (skb_copy_bits(list, offset - start,
1046 to, copy))
1047 goto fault;
1048 if ((len -= copy) == 0)
1049 return 0;
1050 offset += copy;
1051 to += copy;
1053 start = end;
1056 if (!len)
1057 return 0;
1059 fault:
1060 return -EFAULT;
1063 /**
1064 * skb_store_bits - store bits from kernel buffer to skb
1065 * @skb: destination buffer
1066 * @offset: offset in destination
1067 * @from: source buffer
1068 * @len: number of bytes to copy
1070 * Copy the specified number of bytes from the source buffer to the
1071 * destination skb. This function handles all the messy bits of
1072 * traversing fragment lists and such.
1073 */
1075 int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1077 int i, copy;
1078 int start = skb_headlen(skb);
1080 if (offset > (int)skb->len - len)
1081 goto fault;
1083 if ((copy = start - offset) > 0) {
1084 if (copy > len)
1085 copy = len;
1086 memcpy(skb->data + offset, from, copy);
1087 if ((len -= copy) == 0)
1088 return 0;
1089 offset += copy;
1090 from += copy;
1093 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1094 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1095 int end;
1097 BUG_TRAP(start <= offset + len);
1099 end = start + frag->size;
1100 if ((copy = end - offset) > 0) {
1101 u8 *vaddr;
1103 if (copy > len)
1104 copy = len;
1106 vaddr = kmap_skb_frag(frag);
1107 memcpy(vaddr + frag->page_offset + offset - start,
1108 from, copy);
1109 kunmap_skb_frag(vaddr);
1111 if ((len -= copy) == 0)
1112 return 0;
1113 offset += copy;
1114 from += copy;
1116 start = end;
1119 if (skb_shinfo(skb)->frag_list) {
1120 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1122 for (; list; list = list->next) {
1123 int end;
1125 BUG_TRAP(start <= offset + len);
1127 end = start + list->len;
1128 if ((copy = end - offset) > 0) {
1129 if (copy > len)
1130 copy = len;
1131 if (skb_store_bits(list, offset - start,
1132 from, copy))
1133 goto fault;
1134 if ((len -= copy) == 0)
1135 return 0;
1136 offset += copy;
1137 from += copy;
1139 start = end;
1142 if (!len)
1143 return 0;
1145 fault:
1146 return -EFAULT;
1149 EXPORT_SYMBOL(skb_store_bits);
1151 /* Checksum skb data. */
1153 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1154 int len, unsigned int csum)
1156 int start = skb_headlen(skb);
1157 int i, copy = start - offset;
1158 int pos = 0;
1160 /* Checksum header. */
1161 if (copy > 0) {
1162 if (copy > len)
1163 copy = len;
1164 csum = csum_partial(skb->data + offset, copy, csum);
1165 if ((len -= copy) == 0)
1166 return csum;
1167 offset += copy;
1168 pos = copy;
1171 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1172 int end;
1174 BUG_TRAP(start <= offset + len);
1176 end = start + skb_shinfo(skb)->frags[i].size;
1177 if ((copy = end - offset) > 0) {
1178 unsigned int csum2;
1179 u8 *vaddr;
1180 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1182 if (copy > len)
1183 copy = len;
1184 vaddr = kmap_skb_frag(frag);
1185 csum2 = csum_partial(vaddr + frag->page_offset +
1186 offset - start, copy, 0);
1187 kunmap_skb_frag(vaddr);
1188 csum = csum_block_add(csum, csum2, pos);
1189 if (!(len -= copy))
1190 return csum;
1191 offset += copy;
1192 pos += copy;
1194 start = end;
1197 if (skb_shinfo(skb)->frag_list) {
1198 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1200 for (; list; list = list->next) {
1201 int end;
1203 BUG_TRAP(start <= offset + len);
1205 end = start + list->len;
1206 if ((copy = end - offset) > 0) {
1207 unsigned int csum2;
1208 if (copy > len)
1209 copy = len;
1210 csum2 = skb_checksum(list, offset - start,
1211 copy, 0);
1212 csum = csum_block_add(csum, csum2, pos);
1213 if ((len -= copy) == 0)
1214 return csum;
1215 offset += copy;
1216 pos += copy;
1218 start = end;
1221 BUG_ON(len);
1223 return csum;
1226 /* Both of above in one bottle. */
1228 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1229 u8 *to, int len, unsigned int csum)
1231 int start = skb_headlen(skb);
1232 int i, copy = start - offset;
1233 int pos = 0;
1235 /* Copy header. */
1236 if (copy > 0) {
1237 if (copy > len)
1238 copy = len;
1239 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1240 copy, csum);
1241 if ((len -= copy) == 0)
1242 return csum;
1243 offset += copy;
1244 to += copy;
1245 pos = copy;
1248 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1249 int end;
1251 BUG_TRAP(start <= offset + len);
1253 end = start + skb_shinfo(skb)->frags[i].size;
1254 if ((copy = end - offset) > 0) {
1255 unsigned int csum2;
1256 u8 *vaddr;
1257 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1259 if (copy > len)
1260 copy = len;
1261 vaddr = kmap_skb_frag(frag);
1262 csum2 = csum_partial_copy_nocheck(vaddr +
1263 frag->page_offset +
1264 offset - start, to,
1265 copy, 0);
1266 kunmap_skb_frag(vaddr);
1267 csum = csum_block_add(csum, csum2, pos);
1268 if (!(len -= copy))
1269 return csum;
1270 offset += copy;
1271 to += copy;
1272 pos += copy;
1274 start = end;
1277 if (skb_shinfo(skb)->frag_list) {
1278 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1280 for (; list; list = list->next) {
1281 unsigned int csum2;
1282 int end;
1284 BUG_TRAP(start <= offset + len);
1286 end = start + list->len;
1287 if ((copy = end - offset) > 0) {
1288 if (copy > len)
1289 copy = len;
1290 csum2 = skb_copy_and_csum_bits(list,
1291 offset - start,
1292 to, copy, 0);
1293 csum = csum_block_add(csum, csum2, pos);
1294 if ((len -= copy) == 0)
1295 return csum;
1296 offset += copy;
1297 to += copy;
1298 pos += copy;
1300 start = end;
1303 BUG_ON(len);
1304 return csum;
1307 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1309 unsigned int csum;
1310 long csstart;
1312 if (skb->ip_summed == CHECKSUM_HW)
1313 csstart = skb->h.raw - skb->data;
1314 else
1315 csstart = skb_headlen(skb);
1317 BUG_ON(csstart > skb_headlen(skb));
1319 memcpy(to, skb->data, csstart);
1321 csum = 0;
1322 if (csstart != skb->len)
1323 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1324 skb->len - csstart, 0);
1326 if (skb->ip_summed == CHECKSUM_HW) {
1327 long csstuff = csstart + skb->csum;
1329 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1333 /**
1334 * skb_dequeue - remove from the head of the queue
1335 * @list: list to dequeue from
1337 * Remove the head of the list. The list lock is taken so the function
1338 * may be used safely with other locking list functions. The head item is
1339 * returned or %NULL if the list is empty.
1340 */
1342 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1344 unsigned long flags;
1345 struct sk_buff *result;
1347 spin_lock_irqsave(&list->lock, flags);
1348 result = __skb_dequeue(list);
1349 spin_unlock_irqrestore(&list->lock, flags);
1350 return result;
1353 /**
1354 * skb_dequeue_tail - remove from the tail of the queue
1355 * @list: list to dequeue from
1357 * Remove the tail of the list. The list lock is taken so the function
1358 * may be used safely with other locking list functions. The tail item is
1359 * returned or %NULL if the list is empty.
1360 */
1361 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1363 unsigned long flags;
1364 struct sk_buff *result;
1366 spin_lock_irqsave(&list->lock, flags);
1367 result = __skb_dequeue_tail(list);
1368 spin_unlock_irqrestore(&list->lock, flags);
1369 return result;
1372 /**
1373 * skb_queue_purge - empty a list
1374 * @list: list to empty
1376 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1377 * the list and one reference dropped. This function takes the list
1378 * lock and is atomic with respect to other list locking functions.
1379 */
1380 void skb_queue_purge(struct sk_buff_head *list)
1382 struct sk_buff *skb;
1383 while ((skb = skb_dequeue(list)) != NULL)
1384 kfree_skb(skb);
1387 /**
1388 * skb_queue_head - queue a buffer at the list head
1389 * @list: list to use
1390 * @newsk: buffer to queue
1392 * Queue a buffer at the start of the list. This function takes the
1393 * list lock and can be used safely with other locking &sk_buff functions
1394 * safely.
1396 * A buffer cannot be placed on two lists at the same time.
1397 */
1398 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1400 unsigned long flags;
1402 spin_lock_irqsave(&list->lock, flags);
1403 __skb_queue_head(list, newsk);
1404 spin_unlock_irqrestore(&list->lock, flags);
1407 /**
1408 * skb_queue_tail - queue a buffer at the list tail
1409 * @list: list to use
1410 * @newsk: buffer to queue
1412 * Queue a buffer at the tail of the list. This function takes the
1413 * list lock and can be used safely with other locking &sk_buff functions
1414 * safely.
1416 * A buffer cannot be placed on two lists at the same time.
1417 */
1418 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1420 unsigned long flags;
1422 spin_lock_irqsave(&list->lock, flags);
1423 __skb_queue_tail(list, newsk);
1424 spin_unlock_irqrestore(&list->lock, flags);
1427 /**
1428 * skb_unlink - remove a buffer from a list
1429 * @skb: buffer to remove
1430 * @list: list to use
1432 * Remove a packet from a list. The list locks are taken and this
1433 * function is atomic with respect to other list locked calls
1435 * You must know what list the SKB is on.
1436 */
1437 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1439 unsigned long flags;
1441 spin_lock_irqsave(&list->lock, flags);
1442 __skb_unlink(skb, list);
1443 spin_unlock_irqrestore(&list->lock, flags);
1446 /**
1447 * skb_append - append a buffer
1448 * @old: buffer to insert after
1449 * @newsk: buffer to insert
1450 * @list: list to use
1452 * Place a packet after a given packet in a list. The list locks are taken
1453 * and this function is atomic with respect to other list locked calls.
1454 * A buffer cannot be placed on two lists at the same time.
1455 */
1456 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1458 unsigned long flags;
1460 spin_lock_irqsave(&list->lock, flags);
1461 __skb_append(old, newsk, list);
1462 spin_unlock_irqrestore(&list->lock, flags);
1466 /**
1467 * skb_insert - insert a buffer
1468 * @old: buffer to insert before
1469 * @newsk: buffer to insert
1470 * @list: list to use
1472 * Place a packet before a given packet in a list. The list locks are
1473 * taken and this function is atomic with respect to other list locked
1474 * calls.
1476 * A buffer cannot be placed on two lists at the same time.
1477 */
1478 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1480 unsigned long flags;
1482 spin_lock_irqsave(&list->lock, flags);
1483 __skb_insert(newsk, old->prev, old, list);
1484 spin_unlock_irqrestore(&list->lock, flags);
1487 #if 0
1488 /*
1489 * Tune the memory allocator for a new MTU size.
1490 */
1491 void skb_add_mtu(int mtu)
1493 /* Must match allocation in alloc_skb */
1494 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1496 kmem_add_cache_size(mtu);
1498 #endif
1500 static inline void skb_split_inside_header(struct sk_buff *skb,
1501 struct sk_buff* skb1,
1502 const u32 len, const int pos)
1504 int i;
1506 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1508 /* And move data appendix as is. */
1509 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1510 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1512 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1513 skb_shinfo(skb)->nr_frags = 0;
1514 skb1->data_len = skb->data_len;
1515 skb1->len += skb1->data_len;
1516 skb->data_len = 0;
1517 skb->len = len;
1518 skb->tail = skb->data + len;
1521 static inline void skb_split_no_header(struct sk_buff *skb,
1522 struct sk_buff* skb1,
1523 const u32 len, int pos)
1525 int i, k = 0;
1526 const int nfrags = skb_shinfo(skb)->nr_frags;
1528 skb_shinfo(skb)->nr_frags = 0;
1529 skb1->len = skb1->data_len = skb->len - len;
1530 skb->len = len;
1531 skb->data_len = len - pos;
1533 for (i = 0; i < nfrags; i++) {
1534 int size = skb_shinfo(skb)->frags[i].size;
1536 if (pos + size > len) {
1537 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1539 if (pos < len) {
1540 /* Split frag.
1541 * We have two variants in this case:
1542 * 1. Move all the frag to the second
1543 * part, if it is possible. F.e.
1544 * this approach is mandatory for TUX,
1545 * where splitting is expensive.
1546 * 2. Split is accurately. We make this.
1547 */
1548 get_page(skb_shinfo(skb)->frags[i].page);
1549 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1550 skb_shinfo(skb1)->frags[0].size -= len - pos;
1551 skb_shinfo(skb)->frags[i].size = len - pos;
1552 skb_shinfo(skb)->nr_frags++;
1554 k++;
1555 } else
1556 skb_shinfo(skb)->nr_frags++;
1557 pos += size;
1559 skb_shinfo(skb1)->nr_frags = k;
1562 /**
1563 * skb_split - Split fragmented skb to two parts at length len.
1564 * @skb: the buffer to split
1565 * @skb1: the buffer to receive the second part
1566 * @len: new length for skb
1567 */
1568 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1570 int pos = skb_headlen(skb);
1572 if (len < pos) /* Split line is inside header. */
1573 skb_split_inside_header(skb, skb1, len, pos);
1574 else /* Second chunk has no header, nothing to copy. */
1575 skb_split_no_header(skb, skb1, len, pos);
1578 /**
1579 * skb_prepare_seq_read - Prepare a sequential read of skb data
1580 * @skb: the buffer to read
1581 * @from: lower offset of data to be read
1582 * @to: upper offset of data to be read
1583 * @st: state variable
1585 * Initializes the specified state variable. Must be called before
1586 * invoking skb_seq_read() for the first time.
1587 */
1588 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1589 unsigned int to, struct skb_seq_state *st)
1591 st->lower_offset = from;
1592 st->upper_offset = to;
1593 st->root_skb = st->cur_skb = skb;
1594 st->frag_idx = st->stepped_offset = 0;
1595 st->frag_data = NULL;
1598 /**
1599 * skb_seq_read - Sequentially read skb data
1600 * @consumed: number of bytes consumed by the caller so far
1601 * @data: destination pointer for data to be returned
1602 * @st: state variable
1604 * Reads a block of skb data at &consumed relative to the
1605 * lower offset specified to skb_prepare_seq_read(). Assigns
1606 * the head of the data block to &data and returns the length
1607 * of the block or 0 if the end of the skb data or the upper
1608 * offset has been reached.
1610 * The caller is not required to consume all of the data
1611 * returned, i.e. &consumed is typically set to the number
1612 * of bytes already consumed and the next call to
1613 * skb_seq_read() will return the remaining part of the block.
1615 * Note: The size of each block of data returned can be arbitary,
1616 * this limitation is the cost for zerocopy seqeuental
1617 * reads of potentially non linear data.
1619 * Note: Fragment lists within fragments are not implemented
1620 * at the moment, state->root_skb could be replaced with
1621 * a stack for this purpose.
1622 */
1623 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1624 struct skb_seq_state *st)
1626 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1627 skb_frag_t *frag;
1629 if (unlikely(abs_offset >= st->upper_offset))
1630 return 0;
1632 next_skb:
1633 block_limit = skb_headlen(st->cur_skb);
1635 if (abs_offset < block_limit) {
1636 *data = st->cur_skb->data + abs_offset;
1637 return block_limit - abs_offset;
1640 if (st->frag_idx == 0 && !st->frag_data)
1641 st->stepped_offset += skb_headlen(st->cur_skb);
1643 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1644 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1645 block_limit = frag->size + st->stepped_offset;
1647 if (abs_offset < block_limit) {
1648 if (!st->frag_data)
1649 st->frag_data = kmap_skb_frag(frag);
1651 *data = (u8 *) st->frag_data + frag->page_offset +
1652 (abs_offset - st->stepped_offset);
1654 return block_limit - abs_offset;
1657 if (st->frag_data) {
1658 kunmap_skb_frag(st->frag_data);
1659 st->frag_data = NULL;
1662 st->frag_idx++;
1663 st->stepped_offset += frag->size;
1666 if (st->cur_skb->next) {
1667 st->cur_skb = st->cur_skb->next;
1668 st->frag_idx = 0;
1669 goto next_skb;
1670 } else if (st->root_skb == st->cur_skb &&
1671 skb_shinfo(st->root_skb)->frag_list) {
1672 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1673 goto next_skb;
1676 return 0;
1679 /**
1680 * skb_abort_seq_read - Abort a sequential read of skb data
1681 * @st: state variable
1683 * Must be called if skb_seq_read() was not called until it
1684 * returned 0.
1685 */
1686 void skb_abort_seq_read(struct skb_seq_state *st)
1688 if (st->frag_data)
1689 kunmap_skb_frag(st->frag_data);
1692 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1694 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1695 struct ts_config *conf,
1696 struct ts_state *state)
1698 return skb_seq_read(offset, text, TS_SKB_CB(state));
1701 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1703 skb_abort_seq_read(TS_SKB_CB(state));
1706 /**
1707 * skb_find_text - Find a text pattern in skb data
1708 * @skb: the buffer to look in
1709 * @from: search offset
1710 * @to: search limit
1711 * @config: textsearch configuration
1712 * @state: uninitialized textsearch state variable
1714 * Finds a pattern in the skb data according to the specified
1715 * textsearch configuration. Use textsearch_next() to retrieve
1716 * subsequent occurrences of the pattern. Returns the offset
1717 * to the first occurrence or UINT_MAX if no match was found.
1718 */
1719 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1720 unsigned int to, struct ts_config *config,
1721 struct ts_state *state)
1723 config->get_next_block = skb_ts_get_next_block;
1724 config->finish = skb_ts_finish;
1726 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1728 return textsearch_find(config, state);
1731 /**
1732 * skb_append_datato_frags: - append the user data to a skb
1733 * @sk: sock structure
1734 * @skb: skb structure to be appened with user data.
1735 * @getfrag: call back function to be used for getting the user data
1736 * @from: pointer to user message iov
1737 * @length: length of the iov message
1739 * Description: This procedure append the user data in the fragment part
1740 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1741 */
1742 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1743 int (*getfrag)(void *from, char *to, int offset,
1744 int len, int odd, struct sk_buff *skb),
1745 void *from, int length)
1747 int frg_cnt = 0;
1748 skb_frag_t *frag = NULL;
1749 struct page *page = NULL;
1750 int copy, left;
1751 int offset = 0;
1752 int ret;
1754 do {
1755 /* Return error if we don't have space for new frag */
1756 frg_cnt = skb_shinfo(skb)->nr_frags;
1757 if (frg_cnt >= MAX_SKB_FRAGS)
1758 return -EFAULT;
1760 /* allocate a new page for next frag */
1761 page = alloc_pages(sk->sk_allocation, 0);
1763 /* If alloc_page fails just return failure and caller will
1764 * free previous allocated pages by doing kfree_skb()
1765 */
1766 if (page == NULL)
1767 return -ENOMEM;
1769 /* initialize the next frag */
1770 sk->sk_sndmsg_page = page;
1771 sk->sk_sndmsg_off = 0;
1772 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1773 skb->truesize += PAGE_SIZE;
1774 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1776 /* get the new initialized frag */
1777 frg_cnt = skb_shinfo(skb)->nr_frags;
1778 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1780 /* copy the user data to page */
1781 left = PAGE_SIZE - frag->page_offset;
1782 copy = (length > left)? left : length;
1784 ret = getfrag(from, (page_address(frag->page) +
1785 frag->page_offset + frag->size),
1786 offset, copy, 0, skb);
1787 if (ret < 0)
1788 return -EFAULT;
1790 /* copy was successful so update the size parameters */
1791 sk->sk_sndmsg_off += copy;
1792 frag->size += copy;
1793 skb->len += copy;
1794 skb->data_len += copy;
1795 offset += copy;
1796 length -= copy;
1798 } while (length > 0);
1800 return 0;
1803 void __init skb_init(void)
1805 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1806 sizeof(struct sk_buff),
1807 0,
1808 SLAB_HWCACHE_ALIGN,
1809 NULL, NULL);
1810 if (!skbuff_head_cache)
1811 panic("cannot create skbuff cache");
1813 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1814 (2*sizeof(struct sk_buff)) +
1815 sizeof(atomic_t),
1816 0,
1817 SLAB_HWCACHE_ALIGN,
1818 NULL, NULL);
1819 if (!skbuff_fclone_cache)
1820 panic("cannot create skbuff cache");
1823 EXPORT_SYMBOL(___pskb_trim);
1824 EXPORT_SYMBOL(__kfree_skb);
1825 EXPORT_SYMBOL(__pskb_pull_tail);
1826 EXPORT_SYMBOL(__alloc_skb);
1827 EXPORT_SYMBOL(pskb_copy);
1828 EXPORT_SYMBOL(pskb_expand_head);
1829 EXPORT_SYMBOL(skb_checksum);
1830 EXPORT_SYMBOL(skb_clone);
1831 EXPORT_SYMBOL(skb_clone_fraglist);
1832 EXPORT_SYMBOL(skb_copy);
1833 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1834 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1835 EXPORT_SYMBOL(skb_copy_bits);
1836 EXPORT_SYMBOL(skb_copy_expand);
1837 EXPORT_SYMBOL(skb_over_panic);
1838 EXPORT_SYMBOL(skb_pad);
1839 EXPORT_SYMBOL(skb_realloc_headroom);
1840 EXPORT_SYMBOL(skb_under_panic);
1841 EXPORT_SYMBOL(skb_dequeue);
1842 EXPORT_SYMBOL(skb_dequeue_tail);
1843 EXPORT_SYMBOL(skb_insert);
1844 EXPORT_SYMBOL(skb_queue_purge);
1845 EXPORT_SYMBOL(skb_queue_head);
1846 EXPORT_SYMBOL(skb_queue_tail);
1847 EXPORT_SYMBOL(skb_unlink);
1848 EXPORT_SYMBOL(skb_append);
1849 EXPORT_SYMBOL(skb_split);
1850 EXPORT_SYMBOL(skb_prepare_seq_read);
1851 EXPORT_SYMBOL(skb_seq_read);
1852 EXPORT_SYMBOL(skb_abort_seq_read);
1853 EXPORT_SYMBOL(skb_find_text);
1854 EXPORT_SYMBOL(skb_append_datato_frags);